stvii.c revision 1.2 1 /* $NetBSD: stvii.c,v 1.2 2011/09/07 13:37:49 macallan Exp $ */
2
3 /*-
4 * Copyright (C) 2011 Michael Lorenz.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 /*
28 * a driver for the ST7 microcontroller found in Gdium Liberty 1000 notebooks
29 */
30
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: stvii.c,v 1.2 2011/09/07 13:37:49 macallan Exp $");
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/device.h>
39 #include <sys/malloc.h>
40 #include <sys/sysctl.h>
41 #include <sys/kthread.h>
42 #include <sys/proc.h>
43
44 #include <dev/sysmon/sysmonvar.h>
45 #include <dev/sysmon/sysmon_taskq.h>
46
47 #include <dev/i2c/i2cvar.h>
48
49 #include "opt_stvii.h"
50
51 #ifdef STVII_DEBUG
52 #define DPRINTF aprint_error
53 #else
54 #define DPRINTF while (0) printf
55 #endif
56
57 /* register definitions from OpenBSD */
58 #define ST7_VERSION 0x00 /* only on later mobos */
59
60 #define ST7_STATUS 0x01
61 #define STS_LID_CLOSED 0x01
62 #define STS_POWER_BTN_DOWN 0x02
63 #define STS_BATTERY_PRESENT 0x04 /* not available on old mobo */
64 #define STS_POWER_AVAILABLE 0x08
65 #define STS_WAVELAN_BTN_DOWN 0x10 /* ``enable'' on old mobo */
66 #define STS_AC_AVAILABLE 0x20
67 #define ST7_CONTROL 0x02
68 #define STC_DDR_CLOCK 0x01
69 #define STC_CHARGE_LED_LIT 0x02
70 #define STC_BEEP 0x04
71 #define STC_DDR_POWER 0x08
72 #define STC_TRICKLE 0x10 /* trickle charge rate */
73 #define STC_RADIO_ENABLE 0x20 /* enable wavelan rf, later mobos */
74 #define STC_MAIN_POWER 0x40
75 #define STC_CHARGE_ENABLE 0x80
76 #define ST7_BATTERY_L 0x03
77 #define ST7_BATTERY_H 0x04
78 #define ST7_SIGNATURE 0x05
79 #define STSIG_EC_CONTROL 0x00
80 #define STSIG_OS_CONTROL 0xae
81 /* rough battery operating state limits */
82 #define STSEC_BAT_MIN_VOLT 7000000 /* 7V */
83 #define STSEC_BAT_MAX_VOLT 8000000 /* 8V */
84
85 #define BAT_AC_PRESENT 0
86 #define BAT_BATTERY_PRESENT 1
87 #define BAT_CHARGING 2
88 #define BAT_CHARGE 3
89 #define BAT_MAX_CHARGE 4
90 #define BAT_NSENSORS 5
91
92 struct stvii_softc {
93 device_t sc_dev;
94 i2c_tag_t sc_i2c;
95 int sc_address, sc_version;
96 int sc_sleep;
97 struct sysmon_envsys *sc_sme;
98 envsys_data_t sc_sensor[BAT_NSENSORS];
99 struct sysmon_pswitch sc_sm_acpower;
100 struct sysmon_pswitch sc_sm_lid;
101 struct sysmon_pswitch sc_sm_powerbutton;
102 };
103
104 static void stvii_attach(device_t, device_t, void *);
105 static int stvii_match(device_t, cfdata_t, void *);
106 static void stvii_writereg(struct stvii_softc *, int, uint8_t);
107 static int stvii_readreg(struct stvii_softc *, int);
108 static void stvii_worker(void *);
109
110 CFATTACH_DECL_NEW(stvii, sizeof(struct stvii_softc),
111 stvii_match, stvii_attach, NULL, NULL);
112
113 static int
114 stvii_match(device_t parent, cfdata_t cf, void *aux)
115 {
116 struct i2c_attach_args *args = aux;
117 int ret = -1;
118 uint8_t out = ST7_VERSION, in = 0;
119
120 /* see if we can talk to something at address 0x40 */
121 if (args->ia_addr == 0x40) {
122 iic_acquire_bus(args->ia_tag, 0);
123 ret = iic_exec(args->ia_tag, I2C_OP_READ_WITH_STOP, args->ia_addr,
124 &out, 1, &in, 1, 0);
125 DPRINTF("%02x\n", in);
126 iic_release_bus(args->ia_tag, 0);
127 }
128 return (ret >= 0);
129 }
130
131 static void
132 stvii_attach(device_t parent, device_t self, void *aux)
133 {
134 struct stvii_softc *sc = device_private(self);
135 struct i2c_attach_args *args = aux;
136 uint8_t ver, reg;
137
138 sc->sc_dev = self;
139 sc->sc_address = args->ia_addr;
140 aprint_normal(": ST7 Microcontroller\n");
141 sc->sc_i2c = args->ia_tag;
142 ver = stvii_readreg(sc, ST7_VERSION);
143 sc->sc_version = ver;
144 aprint_normal_dev(sc->sc_dev, "firmware version %d.%d\n", (ver >> 4) & 0xf, ver & 0xf);
145 #ifdef STVII_DEBUG
146 {
147 int i;
148
149 for (i = 0; i < 6; i++) {
150 printf("%02x ", stvii_readreg(sc, i));
151 }
152 printf("\n");
153 }
154 #endif
155 stvii_writereg(sc, ST7_SIGNATURE, STSIG_EC_CONTROL);
156 reg = stvii_readreg(sc, ST7_CONTROL);
157 reg |= STC_RADIO_ENABLE;
158 stvii_writereg(sc, ST7_CONTROL, reg);
159 reg = stvii_readreg(sc, ST7_CONTROL);
160
161 if (kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL, stvii_worker, sc,
162 NULL, "stvii") != 0) {
163 aprint_error_dev(sc->sc_dev, "Failed to start kernel thread\n");
164 }
165
166 memset(&sc->sc_sm_acpower, 0, sizeof(struct sysmon_pswitch));
167 sc->sc_sm_acpower.smpsw_name = "AC Power";
168 sc->sc_sm_acpower.smpsw_type = PSWITCH_TYPE_ACADAPTER;
169 if (sysmon_pswitch_register(&sc->sc_sm_acpower) != 0)
170 printf("%s: unable to register AC power status with sysmon\n",
171 device_xname(sc->sc_dev));
172 memset(&sc->sc_sm_lid, 0, sizeof(struct sysmon_pswitch));
173 sc->sc_sm_lid.smpsw_name = "Lid Switch";
174 sc->sc_sm_lid.smpsw_type = PSWITCH_TYPE_LID;
175 if (sysmon_pswitch_register(&sc->sc_sm_lid) != 0)
176 printf("%s: unable to register lid switch with sysmon\n",
177 device_xname(sc->sc_dev));
178 memset(&sc->sc_sm_powerbutton, 0, sizeof(struct sysmon_pswitch));
179 sc->sc_sm_powerbutton.smpsw_name = "Power Button";
180 sc->sc_sm_powerbutton.smpsw_type = PSWITCH_TYPE_POWER;
181 if (sysmon_pswitch_register(&sc->sc_sm_powerbutton) != 0)
182 printf("%s: unable to register power button with sysmon\n",
183 device_xname(sc->sc_dev));
184 }
185
186 static void
187 stvii_writereg(struct stvii_softc *sc, int reg, uint8_t val)
188 {
189 uint8_t out[2] = {reg, val};
190
191 if ((reg < 0) || (reg > 5))
192 return;
193
194 iic_acquire_bus(sc->sc_i2c, 0);
195 iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP, sc->sc_address, out, 2, NULL, 0, 0);
196 iic_release_bus(sc->sc_i2c, 0);
197 }
198
199 static int
200 stvii_readreg(struct stvii_softc *sc, int reg)
201 {
202 uint8_t inreg[1], outreg[1];
203 int ret = 1, bail = 0;
204
205 if ((reg < 0) || (reg > 5))
206 return 0xff;
207 inreg[0] = 0x77;
208 outreg[0] = reg;
209 iic_acquire_bus(sc->sc_i2c, 0);
210 while ((ret != 0) && (bail < 10)) {
211 ret = iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
212 sc->sc_address, outreg, 1, inreg, 1, 0);
213 bail++;
214 delay(10);
215 }
216 iic_release_bus(sc->sc_i2c, 0);
217 if (ret != 0)
218 return -1;
219 return inreg[0];
220 }
221
222 static int
223 stvii_battery_level(struct stvii_softc *sc)
224 {
225 int bl, bh;
226
227 bl = stvii_readreg(sc, ST7_BATTERY_L);
228 bh = stvii_readreg(sc, ST7_BATTERY_H);
229 return (bl & 3) | (bh << 2);
230 }
231
232 static void
233 stvii_worker(void *cookie)
234 {
235 struct stvii_softc *sc = cookie;
236 int status = 0, st;
237 int battery_level = 0, bl;
238 int ok = TRUE;
239
240 while (ok) {
241 st = stvii_readreg(sc, ST7_STATUS);
242 /*
243 * I get i2c timeouts when the power button is pressed.
244 * According to the linux driver this happens on firmware
245 * version 0x13 and newer, mine is 0x16.
246 * So, when we see read errors on the right version we assume
247 * it's the power button as long as the lid is open
248 * ( the button is inside the lid )
249 */
250 if ((st == -1) && (sc->sc_version >= 0x13)) {
251 if ((status & (STS_LID_CLOSED | STS_POWER_BTN_DOWN) )
252 == 0) {
253 st = status | STS_POWER_BTN_DOWN;
254 }
255 }
256 if ((st != -1) && (st != status)) {
257 if ((status ^ st) & STS_LID_CLOSED) {
258 sysmon_pswitch_event(&sc->sc_sm_lid,
259 ((st & STS_LID_CLOSED) ?
260 PSWITCH_EVENT_PRESSED :
261 PSWITCH_EVENT_RELEASED));
262 }
263 if ((status ^ st) & STS_AC_AVAILABLE) {
264 sysmon_pswitch_event(&sc->sc_sm_acpower,
265 ((st & STS_AC_AVAILABLE) ?
266 PSWITCH_EVENT_PRESSED :
267 PSWITCH_EVENT_RELEASED));
268 }
269 if ((status ^ st) & STS_POWER_BTN_DOWN) {
270 sysmon_pswitch_event(&sc->sc_sm_powerbutton,
271 ((st & STS_POWER_BTN_DOWN) ?
272 PSWITCH_EVENT_PRESSED :
273 PSWITCH_EVENT_RELEASED));
274 }
275 status = st;
276 }
277 if (0) {
278 bl = stvii_battery_level(sc);
279 if (bl != battery_level) {
280 printf("battery: %d\n", bl);
281 battery_level = bl;
282 }
283 }
284 tsleep(&sc->sc_sleep, 0, "stvii", hz / 2);
285 }
286 }
287