Commit | Line | Data |
---|---|---|
72cd7995 DB |
1 | /* |
2 | * tps65010 - driver for tps6501x power management chips | |
3 | * | |
4 | * Copyright (C) 2004 Texas Instruments | |
5 | * Copyright (C) 2004-2005 David Brownell | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
20 | */ | |
72cd7995 | 21 | |
72cd7995 DB |
22 | #include <linux/kernel.h> |
23 | #include <linux/module.h> | |
24 | #include <linux/init.h> | |
25 | #include <linux/slab.h> | |
26 | #include <linux/interrupt.h> | |
72cd7995 DB |
27 | #include <linux/i2c.h> |
28 | #include <linux/delay.h> | |
29 | #include <linux/workqueue.h> | |
72cd7995 DB |
30 | #include <linux/debugfs.h> |
31 | #include <linux/seq_file.h> | |
5c085d36 | 32 | #include <linux/mutex.h> |
72cd7995 | 33 | |
6d16bfb5 | 34 | #include <linux/i2c/tps65010.h> |
72cd7995 DB |
35 | |
36 | /*-------------------------------------------------------------------------*/ | |
37 | ||
38 | #define DRIVER_VERSION "2 May 2005" | |
4801bc25 | 39 | #define DRIVER_NAME (tps65010_driver.driver.name) |
72cd7995 DB |
40 | |
41 | MODULE_DESCRIPTION("TPS6501x Power Management Driver"); | |
42 | MODULE_LICENSE("GPL"); | |
43 | ||
72cd7995 DB |
44 | static struct i2c_driver tps65010_driver; |
45 | ||
46 | /*-------------------------------------------------------------------------*/ | |
47 | ||
48 | /* This driver handles a family of multipurpose chips, which incorporate | |
49 | * voltage regulators, lithium ion/polymer battery charging, GPIOs, LEDs, | |
50 | * and other features often needed in portable devices like cell phones | |
51 | * or digital cameras. | |
52 | * | |
53 | * The tps65011 and tps65013 have different voltage settings compared | |
54 | * to tps65010 and tps65012. The tps65013 has a NO_CHG status/irq. | |
55 | * All except tps65010 have "wait" mode, possibly defaulted so that | |
56 | * battery-insert != device-on. | |
57 | * | |
58 | * We could distinguish between some models by checking VDCDC1.UVLO or | |
59 | * other registers, unless they've been changed already after powerup | |
60 | * as part of board setup by a bootloader. | |
61 | */ | |
62 | enum tps_model { | |
63 | TPS_UNKNOWN = 0, | |
64 | TPS65010, | |
65 | TPS65011, | |
66 | TPS65012, | |
67 | TPS65013, | |
68 | }; | |
69 | ||
70 | struct tps65010 { | |
b5067f8f | 71 | struct i2c_client *client; |
5c085d36 | 72 | struct mutex lock; |
8c1bc04e | 73 | struct delayed_work work; |
72cd7995 DB |
74 | struct dentry *file; |
75 | unsigned charging:1; | |
76 | unsigned por:1; | |
77 | unsigned model:8; | |
78 | u16 vbus; | |
79 | unsigned long flags; | |
80 | #define FLAG_VBUS_CHANGED 0 | |
81 | #define FLAG_IRQ_ENABLE 1 | |
82 | ||
83 | /* copies of last register state */ | |
84 | u8 chgstatus, regstatus, chgconf; | |
85 | u8 nmask1, nmask2; | |
86 | ||
65fc50e5 | 87 | /* not currently tracking GPIO state */ |
72cd7995 DB |
88 | }; |
89 | ||
4801bc25 | 90 | #define POWER_POLL_DELAY msecs_to_jiffies(5000) |
72cd7995 DB |
91 | |
92 | /*-------------------------------------------------------------------------*/ | |
93 | ||
94 | #if defined(DEBUG) || defined(CONFIG_DEBUG_FS) | |
95 | ||
96 | static void dbg_chgstat(char *buf, size_t len, u8 chgstatus) | |
97 | { | |
98 | snprintf(buf, len, "%02x%s%s%s%s%s%s%s%s\n", | |
99 | chgstatus, | |
100 | (chgstatus & TPS_CHG_USB) ? " USB" : "", | |
101 | (chgstatus & TPS_CHG_AC) ? " AC" : "", | |
102 | (chgstatus & TPS_CHG_THERM) ? " therm" : "", | |
103 | (chgstatus & TPS_CHG_TERM) ? " done" : | |
104 | ((chgstatus & (TPS_CHG_USB|TPS_CHG_AC)) | |
105 | ? " (charging)" : ""), | |
106 | (chgstatus & TPS_CHG_TAPER_TMO) ? " taper_tmo" : "", | |
107 | (chgstatus & TPS_CHG_CHG_TMO) ? " charge_tmo" : "", | |
108 | (chgstatus & TPS_CHG_PRECHG_TMO) ? " prechg_tmo" : "", | |
109 | (chgstatus & TPS_CHG_TEMP_ERR) ? " temp_err" : ""); | |
110 | } | |
111 | ||
112 | static void dbg_regstat(char *buf, size_t len, u8 regstatus) | |
113 | { | |
114 | snprintf(buf, len, "%02x %s%s%s%s%s%s%s%s\n", | |
115 | regstatus, | |
116 | (regstatus & TPS_REG_ONOFF) ? "off" : "(on)", | |
117 | (regstatus & TPS_REG_COVER) ? " uncover" : "", | |
118 | (regstatus & TPS_REG_UVLO) ? " UVLO" : "", | |
119 | (regstatus & TPS_REG_NO_CHG) ? " NO_CHG" : "", | |
65fc50e5 | 120 | (regstatus & TPS_REG_PG_LD02) ? " ld02_bad" : "", |
72cd7995 DB |
121 | (regstatus & TPS_REG_PG_LD01) ? " ld01_bad" : "", |
122 | (regstatus & TPS_REG_PG_MAIN) ? " main_bad" : "", | |
123 | (regstatus & TPS_REG_PG_CORE) ? " core_bad" : ""); | |
124 | } | |
125 | ||
126 | static void dbg_chgconf(int por, char *buf, size_t len, u8 chgconfig) | |
127 | { | |
65fc50e5 | 128 | const char *hibit; |
72cd7995 DB |
129 | |
130 | if (por) | |
131 | hibit = (chgconfig & TPS_CHARGE_POR) | |
132 | ? "POR=69ms" : "POR=1sec"; | |
133 | else | |
134 | hibit = (chgconfig & TPS65013_AUA) ? "AUA" : ""; | |
135 | ||
136 | snprintf(buf, len, "%02x %s%s%s AC=%d%% USB=%dmA %sCharge\n", | |
137 | chgconfig, hibit, | |
138 | (chgconfig & TPS_CHARGE_RESET) ? " reset" : "", | |
139 | (chgconfig & TPS_CHARGE_FAST) ? " fast" : "", | |
140 | ({int p; switch ((chgconfig >> 3) & 3) { | |
141 | case 3: p = 100; break; | |
142 | case 2: p = 75; break; | |
143 | case 1: p = 50; break; | |
144 | default: p = 25; break; | |
145 | }; p; }), | |
146 | (chgconfig & TPS_VBUS_CHARGING) | |
147 | ? ((chgconfig & TPS_VBUS_500MA) ? 500 : 100) | |
148 | : 0, | |
149 | (chgconfig & TPS_CHARGE_ENABLE) ? "" : "No"); | |
150 | } | |
151 | ||
152 | #endif | |
153 | ||
154 | #ifdef DEBUG | |
155 | ||
156 | static void show_chgstatus(const char *label, u8 chgstatus) | |
157 | { | |
158 | char buf [100]; | |
159 | ||
160 | dbg_chgstat(buf, sizeof buf, chgstatus); | |
161 | pr_debug("%s: %s %s", DRIVER_NAME, label, buf); | |
162 | } | |
163 | ||
164 | static void show_regstatus(const char *label, u8 regstatus) | |
165 | { | |
166 | char buf [100]; | |
167 | ||
168 | dbg_regstat(buf, sizeof buf, regstatus); | |
169 | pr_debug("%s: %s %s", DRIVER_NAME, label, buf); | |
170 | } | |
171 | ||
172 | static void show_chgconfig(int por, const char *label, u8 chgconfig) | |
173 | { | |
174 | char buf [100]; | |
175 | ||
176 | dbg_chgconf(por, buf, sizeof buf, chgconfig); | |
177 | pr_debug("%s: %s %s", DRIVER_NAME, label, buf); | |
178 | } | |
179 | ||
180 | #else | |
181 | ||
182 | static inline void show_chgstatus(const char *label, u8 chgstatus) { } | |
183 | static inline void show_regstatus(const char *label, u8 chgstatus) { } | |
184 | static inline void show_chgconfig(int por, const char *label, u8 chgconfig) { } | |
185 | ||
186 | #endif | |
187 | ||
188 | #ifdef CONFIG_DEBUG_FS | |
189 | ||
190 | static int dbg_show(struct seq_file *s, void *_) | |
191 | { | |
192 | struct tps65010 *tps = s->private; | |
193 | u8 value, v2; | |
194 | unsigned i; | |
195 | char buf[100]; | |
196 | const char *chip; | |
197 | ||
198 | switch (tps->model) { | |
199 | case TPS65010: chip = "tps65010"; break; | |
200 | case TPS65011: chip = "tps65011"; break; | |
201 | case TPS65012: chip = "tps65012"; break; | |
202 | case TPS65013: chip = "tps65013"; break; | |
203 | default: chip = NULL; break; | |
204 | } | |
205 | seq_printf(s, "driver %s\nversion %s\nchip %s\n\n", | |
206 | DRIVER_NAME, DRIVER_VERSION, chip); | |
207 | ||
5c085d36 | 208 | mutex_lock(&tps->lock); |
72cd7995 DB |
209 | |
210 | /* FIXME how can we tell whether a battery is present? | |
211 | * likely involves a charge gauging chip (like BQ26501). | |
212 | */ | |
213 | ||
214 | seq_printf(s, "%scharging\n\n", tps->charging ? "" : "(not) "); | |
215 | ||
216 | ||
217 | /* registers for monitoring battery charging and status; note | |
218 | * that reading chgstat and regstat may ack IRQs... | |
219 | */ | |
b5067f8f | 220 | value = i2c_smbus_read_byte_data(tps->client, TPS_CHGCONFIG); |
72cd7995 DB |
221 | dbg_chgconf(tps->por, buf, sizeof buf, value); |
222 | seq_printf(s, "chgconfig %s", buf); | |
223 | ||
b5067f8f | 224 | value = i2c_smbus_read_byte_data(tps->client, TPS_CHGSTATUS); |
72cd7995 DB |
225 | dbg_chgstat(buf, sizeof buf, value); |
226 | seq_printf(s, "chgstat %s", buf); | |
b5067f8f | 227 | value = i2c_smbus_read_byte_data(tps->client, TPS_MASK1); |
72cd7995 DB |
228 | dbg_chgstat(buf, sizeof buf, value); |
229 | seq_printf(s, "mask1 %s", buf); | |
230 | /* ignore ackint1 */ | |
231 | ||
b5067f8f | 232 | value = i2c_smbus_read_byte_data(tps->client, TPS_REGSTATUS); |
72cd7995 DB |
233 | dbg_regstat(buf, sizeof buf, value); |
234 | seq_printf(s, "regstat %s", buf); | |
b5067f8f | 235 | value = i2c_smbus_read_byte_data(tps->client, TPS_MASK2); |
72cd7995 DB |
236 | dbg_regstat(buf, sizeof buf, value); |
237 | seq_printf(s, "mask2 %s\n", buf); | |
238 | /* ignore ackint2 */ | |
239 | ||
240 | (void) schedule_delayed_work(&tps->work, POWER_POLL_DELAY); | |
241 | ||
242 | ||
243 | /* VMAIN voltage, enable lowpower, etc */ | |
b5067f8f | 244 | value = i2c_smbus_read_byte_data(tps->client, TPS_VDCDC1); |
72cd7995 DB |
245 | seq_printf(s, "vdcdc1 %02x\n", value); |
246 | ||
247 | /* VCORE voltage, vibrator on/off */ | |
b5067f8f | 248 | value = i2c_smbus_read_byte_data(tps->client, TPS_VDCDC2); |
72cd7995 DB |
249 | seq_printf(s, "vdcdc2 %02x\n", value); |
250 | ||
251 | /* both LD0s, and their lowpower behavior */ | |
b5067f8f | 252 | value = i2c_smbus_read_byte_data(tps->client, TPS_VREGS1); |
72cd7995 DB |
253 | seq_printf(s, "vregs1 %02x\n\n", value); |
254 | ||
255 | ||
256 | /* LEDs and GPIOs */ | |
b5067f8f DB |
257 | value = i2c_smbus_read_byte_data(tps->client, TPS_LED1_ON); |
258 | v2 = i2c_smbus_read_byte_data(tps->client, TPS_LED1_PER); | |
72cd7995 DB |
259 | seq_printf(s, "led1 %s, on=%02x, per=%02x, %d/%d msec\n", |
260 | (value & 0x80) | |
261 | ? ((v2 & 0x80) ? "on" : "off") | |
262 | : ((v2 & 0x80) ? "blink" : "(nPG)"), | |
263 | value, v2, | |
264 | (value & 0x7f) * 10, (v2 & 0x7f) * 100); | |
265 | ||
b5067f8f DB |
266 | value = i2c_smbus_read_byte_data(tps->client, TPS_LED2_ON); |
267 | v2 = i2c_smbus_read_byte_data(tps->client, TPS_LED2_PER); | |
72cd7995 DB |
268 | seq_printf(s, "led2 %s, on=%02x, per=%02x, %d/%d msec\n", |
269 | (value & 0x80) | |
270 | ? ((v2 & 0x80) ? "on" : "off") | |
271 | : ((v2 & 0x80) ? "blink" : "off"), | |
272 | value, v2, | |
273 | (value & 0x7f) * 10, (v2 & 0x7f) * 100); | |
274 | ||
b5067f8f DB |
275 | value = i2c_smbus_read_byte_data(tps->client, TPS_DEFGPIO); |
276 | v2 = i2c_smbus_read_byte_data(tps->client, TPS_MASK3); | |
72cd7995 DB |
277 | seq_printf(s, "defgpio %02x mask3 %02x\n", value, v2); |
278 | ||
279 | for (i = 0; i < 4; i++) { | |
65fc50e5 | 280 | if (value & (1 << (4 + i))) |
72cd7995 DB |
281 | seq_printf(s, " gpio%d-out %s\n", i + 1, |
282 | (value & (1 << i)) ? "low" : "hi "); | |
283 | else | |
284 | seq_printf(s, " gpio%d-in %s %s %s\n", i + 1, | |
285 | (value & (1 << i)) ? "hi " : "low", | |
286 | (v2 & (1 << i)) ? "no-irq" : "irq", | |
287 | (v2 & (1 << (4 + i))) ? "rising" : "falling"); | |
288 | } | |
289 | ||
5c085d36 | 290 | mutex_unlock(&tps->lock); |
72cd7995 DB |
291 | return 0; |
292 | } | |
293 | ||
294 | static int dbg_tps_open(struct inode *inode, struct file *file) | |
295 | { | |
8e18e294 | 296 | return single_open(file, dbg_show, inode->i_private); |
72cd7995 DB |
297 | } |
298 | ||
2b8693c0 | 299 | static const struct file_operations debug_fops = { |
72cd7995 DB |
300 | .open = dbg_tps_open, |
301 | .read = seq_read, | |
302 | .llseek = seq_lseek, | |
303 | .release = single_release, | |
304 | }; | |
305 | ||
306 | #define DEBUG_FOPS &debug_fops | |
307 | ||
308 | #else | |
309 | #define DEBUG_FOPS NULL | |
310 | #endif | |
311 | ||
312 | /*-------------------------------------------------------------------------*/ | |
313 | ||
314 | /* handle IRQS in a task context, so we can use I2C calls */ | |
315 | static void tps65010_interrupt(struct tps65010 *tps) | |
316 | { | |
317 | u8 tmp = 0, mask, poll; | |
318 | ||
8c1bc04e | 319 | /* IRQs won't trigger for certain events, but we can get |
72cd7995 DB |
320 | * others by polling (normally, with external power applied). |
321 | */ | |
322 | poll = 0; | |
323 | ||
324 | /* regstatus irqs */ | |
325 | if (tps->nmask2) { | |
b5067f8f | 326 | tmp = i2c_smbus_read_byte_data(tps->client, TPS_REGSTATUS); |
72cd7995 DB |
327 | mask = tmp ^ tps->regstatus; |
328 | tps->regstatus = tmp; | |
329 | mask &= tps->nmask2; | |
330 | } else | |
331 | mask = 0; | |
332 | if (mask) { | |
333 | tps->regstatus = tmp; | |
334 | /* may need to shut something down ... */ | |
335 | ||
336 | /* "off" usually means deep sleep */ | |
337 | if (tmp & TPS_REG_ONOFF) { | |
338 | pr_info("%s: power off button\n", DRIVER_NAME); | |
339 | #if 0 | |
340 | /* REVISIT: this might need its own workqueue | |
341 | * plus tweaks including deadlock avoidance ... | |
ab3bfca7 | 342 | * also needs to get error handling and probably |
b0cb1a19 | 343 | * an #ifdef CONFIG_HIBERNATION |
72cd7995 | 344 | */ |
a3d25c27 | 345 | hibernate(); |
72cd7995 DB |
346 | #endif |
347 | poll = 1; | |
348 | } | |
349 | } | |
350 | ||
351 | /* chgstatus irqs */ | |
352 | if (tps->nmask1) { | |
b5067f8f | 353 | tmp = i2c_smbus_read_byte_data(tps->client, TPS_CHGSTATUS); |
72cd7995 DB |
354 | mask = tmp ^ tps->chgstatus; |
355 | tps->chgstatus = tmp; | |
356 | mask &= tps->nmask1; | |
357 | } else | |
358 | mask = 0; | |
359 | if (mask) { | |
360 | unsigned charging = 0; | |
361 | ||
362 | show_chgstatus("chg/irq", tmp); | |
363 | if (tmp & (TPS_CHG_USB|TPS_CHG_AC)) | |
364 | show_chgconfig(tps->por, "conf", tps->chgconf); | |
365 | ||
366 | /* Unless it was turned off or disabled, we charge any | |
367 | * battery whenever there's power available for it | |
368 | * and the charger hasn't been disabled. | |
369 | */ | |
370 | if (!(tps->chgstatus & ~(TPS_CHG_USB|TPS_CHG_AC)) | |
371 | && (tps->chgstatus & (TPS_CHG_USB|TPS_CHG_AC)) | |
372 | && (tps->chgconf & TPS_CHARGE_ENABLE) | |
373 | ) { | |
374 | if (tps->chgstatus & TPS_CHG_USB) { | |
375 | /* VBUS options are readonly until reconnect */ | |
376 | if (mask & TPS_CHG_USB) | |
377 | set_bit(FLAG_VBUS_CHANGED, &tps->flags); | |
378 | charging = 1; | |
379 | } else if (tps->chgstatus & TPS_CHG_AC) | |
380 | charging = 1; | |
381 | } | |
382 | if (charging != tps->charging) { | |
383 | tps->charging = charging; | |
384 | pr_info("%s: battery %scharging\n", | |
385 | DRIVER_NAME, charging ? "" : | |
386 | ((tps->chgstatus & (TPS_CHG_USB|TPS_CHG_AC)) | |
387 | ? "NOT " : "dis")); | |
388 | } | |
389 | } | |
390 | ||
391 | /* always poll to detect (a) power removal, without tps65013 | |
392 | * NO_CHG IRQ; or (b) restart of charging after stop. | |
393 | */ | |
394 | if ((tps->model != TPS65013 || !tps->charging) | |
395 | && (tps->chgstatus & (TPS_CHG_USB|TPS_CHG_AC))) | |
396 | poll = 1; | |
397 | if (poll) | |
398 | (void) schedule_delayed_work(&tps->work, POWER_POLL_DELAY); | |
399 | ||
400 | /* also potentially gpio-in rise or fall */ | |
401 | } | |
402 | ||
403 | /* handle IRQs and polling using keventd for now */ | |
8c1bc04e | 404 | static void tps65010_work(struct work_struct *work) |
72cd7995 | 405 | { |
8c1bc04e | 406 | struct tps65010 *tps; |
72cd7995 | 407 | |
8c1bc04e | 408 | tps = container_of(work, struct tps65010, work.work); |
5c085d36 | 409 | mutex_lock(&tps->lock); |
72cd7995 DB |
410 | |
411 | tps65010_interrupt(tps); | |
412 | ||
413 | if (test_and_clear_bit(FLAG_VBUS_CHANGED, &tps->flags)) { | |
414 | int status; | |
415 | u8 chgconfig, tmp; | |
416 | ||
b5067f8f | 417 | chgconfig = i2c_smbus_read_byte_data(tps->client, |
72cd7995 DB |
418 | TPS_CHGCONFIG); |
419 | chgconfig &= ~(TPS_VBUS_500MA | TPS_VBUS_CHARGING); | |
420 | if (tps->vbus == 500) | |
421 | chgconfig |= TPS_VBUS_500MA | TPS_VBUS_CHARGING; | |
422 | else if (tps->vbus >= 100) | |
423 | chgconfig |= TPS_VBUS_CHARGING; | |
424 | ||
b5067f8f | 425 | status = i2c_smbus_write_byte_data(tps->client, |
72cd7995 DB |
426 | TPS_CHGCONFIG, chgconfig); |
427 | ||
428 | /* vbus update fails unless VBUS is connected! */ | |
b5067f8f | 429 | tmp = i2c_smbus_read_byte_data(tps->client, TPS_CHGCONFIG); |
72cd7995 DB |
430 | tps->chgconf = tmp; |
431 | show_chgconfig(tps->por, "update vbus", tmp); | |
432 | } | |
433 | ||
434 | if (test_and_clear_bit(FLAG_IRQ_ENABLE, &tps->flags)) | |
8056c6cb | 435 | enable_irq(tps->client->irq); |
72cd7995 | 436 | |
5c085d36 | 437 | mutex_unlock(&tps->lock); |
72cd7995 DB |
438 | } |
439 | ||
7d12e780 | 440 | static irqreturn_t tps65010_irq(int irq, void *_tps) |
72cd7995 DB |
441 | { |
442 | struct tps65010 *tps = _tps; | |
443 | ||
444 | disable_irq_nosync(irq); | |
445 | set_bit(FLAG_IRQ_ENABLE, &tps->flags); | |
8c1bc04e | 446 | (void) schedule_work(&tps->work.work); |
72cd7995 DB |
447 | return IRQ_HANDLED; |
448 | } | |
449 | ||
450 | /*-------------------------------------------------------------------------*/ | |
451 | ||
452 | static struct tps65010 *the_tps; | |
453 | ||
8056c6cb | 454 | static int __exit tps65010_remove(struct i2c_client *client) |
72cd7995 | 455 | { |
8056c6cb | 456 | struct tps65010 *tps = i2c_get_clientdata(client); |
72cd7995 | 457 | |
8056c6cb DB |
458 | if (client->irq > 0) |
459 | free_irq(client->irq, tps); | |
8c1bc04e DB |
460 | cancel_delayed_work(&tps->work); |
461 | flush_scheduled_work(); | |
72cd7995 | 462 | debugfs_remove(tps->file); |
8056c6cb | 463 | kfree(tps); |
65fc50e5 | 464 | the_tps = NULL; |
72cd7995 DB |
465 | return 0; |
466 | } | |
467 | ||
8056c6cb | 468 | static int tps65010_probe(struct i2c_client *client) |
72cd7995 DB |
469 | { |
470 | struct tps65010 *tps; | |
471 | int status; | |
472 | ||
473 | if (the_tps) { | |
8056c6cb DB |
474 | dev_dbg(&client->dev, "only one tps6501x chip allowed\n"); |
475 | return -ENODEV; | |
72cd7995 DB |
476 | } |
477 | ||
8056c6cb DB |
478 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
479 | return -EINVAL; | |
480 | ||
5263ebb5 | 481 | tps = kzalloc(sizeof *tps, GFP_KERNEL); |
72cd7995 | 482 | if (!tps) |
8056c6cb | 483 | return -ENOMEM; |
72cd7995 | 484 | |
5c085d36 | 485 | mutex_init(&tps->lock); |
8c1bc04e | 486 | INIT_DELAYED_WORK(&tps->work, tps65010_work); |
8056c6cb | 487 | tps->client = client; |
72cd7995 | 488 | |
8056c6cb | 489 | if (strcmp(client->name, "tps65010") == 0) |
72cd7995 | 490 | tps->model = TPS65010; |
8056c6cb DB |
491 | else if (strcmp(client->name, "tps65011") == 0) |
492 | tps->model = TPS65011; | |
493 | else if (strcmp(client->name, "tps65012") == 0) | |
494 | tps->model = TPS65012; | |
495 | else if (strcmp(client->name, "tps65013") == 0) | |
72cd7995 | 496 | tps->model = TPS65013; |
8056c6cb DB |
497 | else { |
498 | dev_warn(&client->dev, "unknown chip '%s'\n", client->name); | |
499 | status = -ENODEV; | |
500 | goto fail1; | |
72cd7995 | 501 | } |
72cd7995 | 502 | |
8056c6cb DB |
503 | /* the IRQ is active low, but many gpio lines can't support that |
504 | * so this driver uses falling-edge triggers instead. | |
505 | */ | |
506 | if (client->irq > 0) { | |
507 | status = request_irq(client->irq, tps65010_irq, | |
508 | IRQF_SAMPLE_RANDOM | IRQF_TRIGGER_FALLING, | |
509 | DRIVER_NAME, tps); | |
72cd7995 | 510 | if (status < 0) { |
b5067f8f | 511 | dev_dbg(&client->dev, "can't get IRQ %d, err %d\n", |
8056c6cb | 512 | client->irq, status); |
72cd7995 DB |
513 | goto fail1; |
514 | } | |
72cd7995 DB |
515 | /* annoying race here, ideally we'd have an option |
516 | * to claim the irq now and enable it later. | |
8056c6cb | 517 | * FIXME genirq IRQF_NOAUTOEN now solves that ... |
72cd7995 | 518 | */ |
8056c6cb | 519 | disable_irq(client->irq); |
72cd7995 | 520 | set_bit(FLAG_IRQ_ENABLE, &tps->flags); |
72cd7995 | 521 | } else |
8056c6cb | 522 | dev_warn(&client->dev, "IRQ not configured!\n"); |
72cd7995 DB |
523 | |
524 | ||
525 | switch (tps->model) { | |
526 | case TPS65010: | |
527 | case TPS65012: | |
528 | tps->por = 1; | |
529 | break; | |
530 | case TPS_UNKNOWN: | |
531 | printk(KERN_WARNING "%s: unknown TPS chip\n", DRIVER_NAME); | |
532 | break; | |
533 | /* else CHGCONFIG.POR is replaced by AUA, enabling a WAIT mode */ | |
534 | } | |
b5067f8f | 535 | tps->chgconf = i2c_smbus_read_byte_data(client, TPS_CHGCONFIG); |
72cd7995 DB |
536 | show_chgconfig(tps->por, "conf/init", tps->chgconf); |
537 | ||
538 | show_chgstatus("chg/init", | |
b5067f8f | 539 | i2c_smbus_read_byte_data(client, TPS_CHGSTATUS)); |
72cd7995 | 540 | show_regstatus("reg/init", |
b5067f8f | 541 | i2c_smbus_read_byte_data(client, TPS_REGSTATUS)); |
72cd7995 DB |
542 | |
543 | pr_debug("%s: vdcdc1 0x%02x, vdcdc2 %02x, vregs1 %02x\n", DRIVER_NAME, | |
b5067f8f DB |
544 | i2c_smbus_read_byte_data(client, TPS_VDCDC1), |
545 | i2c_smbus_read_byte_data(client, TPS_VDCDC2), | |
546 | i2c_smbus_read_byte_data(client, TPS_VREGS1)); | |
72cd7995 | 547 | pr_debug("%s: defgpio 0x%02x, mask3 0x%02x\n", DRIVER_NAME, |
b5067f8f DB |
548 | i2c_smbus_read_byte_data(client, TPS_DEFGPIO), |
549 | i2c_smbus_read_byte_data(client, TPS_MASK3)); | |
72cd7995 | 550 | |
72cd7995 DB |
551 | the_tps = tps; |
552 | ||
553 | #if defined(CONFIG_USB_GADGET) && !defined(CONFIG_USB_OTG) | |
554 | /* USB hosts can't draw VBUS. OTG devices could, later | |
555 | * when OTG infrastructure enables it. USB peripherals | |
556 | * could be relying on VBUS while booting, though. | |
557 | */ | |
558 | tps->vbus = 100; | |
559 | #endif | |
560 | ||
561 | /* unmask the "interesting" irqs, then poll once to | |
562 | * kickstart monitoring, initialize shadowed status | |
563 | * registers, and maybe disable VBUS draw. | |
564 | */ | |
565 | tps->nmask1 = ~0; | |
b5067f8f | 566 | (void) i2c_smbus_write_byte_data(client, TPS_MASK1, ~tps->nmask1); |
72cd7995 DB |
567 | |
568 | tps->nmask2 = TPS_REG_ONOFF; | |
569 | if (tps->model == TPS65013) | |
570 | tps->nmask2 |= TPS_REG_NO_CHG; | |
b5067f8f | 571 | (void) i2c_smbus_write_byte_data(client, TPS_MASK2, ~tps->nmask2); |
72cd7995 | 572 | |
b5067f8f DB |
573 | (void) i2c_smbus_write_byte_data(client, TPS_MASK3, 0x0f |
574 | | i2c_smbus_read_byte_data(client, TPS_MASK3)); | |
72cd7995 | 575 | |
8c1bc04e | 576 | tps65010_work(&tps->work.work); |
72cd7995 DB |
577 | |
578 | tps->file = debugfs_create_file(DRIVER_NAME, S_IRUGO, NULL, | |
579 | tps, DEBUG_FOPS); | |
580 | return 0; | |
65fc50e5 | 581 | fail1: |
582 | kfree(tps); | |
8056c6cb | 583 | return status; |
72cd7995 DB |
584 | } |
585 | ||
586 | static struct i2c_driver tps65010_driver = { | |
a9718b0c | 587 | .driver = { |
a9718b0c LR |
588 | .name = "tps65010", |
589 | }, | |
8056c6cb DB |
590 | .probe = tps65010_probe, |
591 | .remove = __exit_p(tps65010_remove), | |
72cd7995 DB |
592 | }; |
593 | ||
594 | /*-------------------------------------------------------------------------*/ | |
595 | ||
596 | /* Draw from VBUS: | |
597 | * 0 mA -- DON'T DRAW (might supply power instead) | |
598 | * 100 mA -- usb unit load (slowest charge rate) | |
599 | * 500 mA -- usb high power (fast battery charge) | |
600 | */ | |
601 | int tps65010_set_vbus_draw(unsigned mA) | |
602 | { | |
603 | unsigned long flags; | |
604 | ||
605 | if (!the_tps) | |
606 | return -ENODEV; | |
607 | ||
608 | /* assumes non-SMP */ | |
609 | local_irq_save(flags); | |
610 | if (mA >= 500) | |
611 | mA = 500; | |
612 | else if (mA >= 100) | |
613 | mA = 100; | |
614 | else | |
615 | mA = 0; | |
616 | the_tps->vbus = mA; | |
617 | if ((the_tps->chgstatus & TPS_CHG_USB) | |
618 | && test_and_set_bit( | |
619 | FLAG_VBUS_CHANGED, &the_tps->flags)) { | |
620 | /* gadget drivers call this in_irq() */ | |
8c1bc04e | 621 | (void) schedule_work(&the_tps->work.work); |
72cd7995 DB |
622 | } |
623 | local_irq_restore(flags); | |
624 | ||
625 | return 0; | |
626 | } | |
627 | EXPORT_SYMBOL(tps65010_set_vbus_draw); | |
628 | ||
629 | /*-------------------------------------------------------------------------*/ | |
630 | /* tps65010_set_gpio_out_value parameter: | |
631 | * gpio: GPIO1, GPIO2, GPIO3 or GPIO4 | |
632 | * value: LOW or HIGH | |
633 | */ | |
634 | int tps65010_set_gpio_out_value(unsigned gpio, unsigned value) | |
635 | { | |
636 | int status; | |
637 | unsigned defgpio; | |
638 | ||
639 | if (!the_tps) | |
640 | return -ENODEV; | |
641 | if ((gpio < GPIO1) || (gpio > GPIO4)) | |
642 | return -EINVAL; | |
643 | ||
5c085d36 | 644 | mutex_lock(&the_tps->lock); |
72cd7995 | 645 | |
b5067f8f | 646 | defgpio = i2c_smbus_read_byte_data(the_tps->client, TPS_DEFGPIO); |
72cd7995 DB |
647 | |
648 | /* Configure GPIO for output */ | |
649 | defgpio |= 1 << (gpio + 3); | |
650 | ||
651 | /* Writing 1 forces a logic 0 on that GPIO and vice versa */ | |
652 | switch (value) { | |
653 | case LOW: | |
654 | defgpio |= 1 << (gpio - 1); /* set GPIO low by writing 1 */ | |
655 | break; | |
656 | /* case HIGH: */ | |
657 | default: | |
658 | defgpio &= ~(1 << (gpio - 1)); /* set GPIO high by writing 0 */ | |
659 | break; | |
660 | } | |
661 | ||
b5067f8f | 662 | status = i2c_smbus_write_byte_data(the_tps->client, |
72cd7995 DB |
663 | TPS_DEFGPIO, defgpio); |
664 | ||
665 | pr_debug("%s: gpio%dout = %s, defgpio 0x%02x\n", DRIVER_NAME, | |
666 | gpio, value ? "high" : "low", | |
b5067f8f | 667 | i2c_smbus_read_byte_data(the_tps->client, TPS_DEFGPIO)); |
72cd7995 | 668 | |
5c085d36 | 669 | mutex_unlock(&the_tps->lock); |
72cd7995 DB |
670 | return status; |
671 | } | |
672 | EXPORT_SYMBOL(tps65010_set_gpio_out_value); | |
673 | ||
674 | /*-------------------------------------------------------------------------*/ | |
675 | /* tps65010_set_led parameter: | |
676 | * led: LED1 or LED2 | |
677 | * mode: ON, OFF or BLINK | |
678 | */ | |
679 | int tps65010_set_led(unsigned led, unsigned mode) | |
680 | { | |
681 | int status; | |
682 | unsigned led_on, led_per, offs; | |
683 | ||
684 | if (!the_tps) | |
685 | return -ENODEV; | |
686 | ||
65fc50e5 | 687 | if (led == LED1) |
72cd7995 DB |
688 | offs = 0; |
689 | else { | |
690 | offs = 2; | |
691 | led = LED2; | |
692 | } | |
693 | ||
5c085d36 | 694 | mutex_lock(&the_tps->lock); |
72cd7995 | 695 | |
65fc50e5 | 696 | pr_debug("%s: led%i_on 0x%02x\n", DRIVER_NAME, led, |
b5067f8f | 697 | i2c_smbus_read_byte_data(the_tps->client, |
65fc50e5 | 698 | TPS_LED1_ON + offs)); |
72cd7995 | 699 | |
65fc50e5 | 700 | pr_debug("%s: led%i_per 0x%02x\n", DRIVER_NAME, led, |
b5067f8f | 701 | i2c_smbus_read_byte_data(the_tps->client, |
65fc50e5 | 702 | TPS_LED1_PER + offs)); |
72cd7995 DB |
703 | |
704 | switch (mode) { | |
705 | case OFF: | |
706 | led_on = 1 << 7; | |
707 | led_per = 0 << 7; | |
708 | break; | |
709 | case ON: | |
710 | led_on = 1 << 7; | |
711 | led_per = 1 << 7; | |
712 | break; | |
713 | case BLINK: | |
714 | led_on = 0x30 | (0 << 7); | |
715 | led_per = 0x08 | (1 << 7); | |
716 | break; | |
717 | default: | |
65fc50e5 | 718 | printk(KERN_ERR "%s: Wrong mode parameter for set_led()\n", |
72cd7995 | 719 | DRIVER_NAME); |
5c085d36 | 720 | mutex_unlock(&the_tps->lock); |
72cd7995 DB |
721 | return -EINVAL; |
722 | } | |
723 | ||
b5067f8f | 724 | status = i2c_smbus_write_byte_data(the_tps->client, |
72cd7995 DB |
725 | TPS_LED1_ON + offs, led_on); |
726 | ||
727 | if (status != 0) { | |
728 | printk(KERN_ERR "%s: Failed to write led%i_on register\n", | |
729 | DRIVER_NAME, led); | |
5c085d36 | 730 | mutex_unlock(&the_tps->lock); |
72cd7995 DB |
731 | return status; |
732 | } | |
733 | ||
65fc50e5 | 734 | pr_debug("%s: led%i_on 0x%02x\n", DRIVER_NAME, led, |
b5067f8f | 735 | i2c_smbus_read_byte_data(the_tps->client, TPS_LED1_ON + offs)); |
72cd7995 | 736 | |
b5067f8f | 737 | status = i2c_smbus_write_byte_data(the_tps->client, |
72cd7995 DB |
738 | TPS_LED1_PER + offs, led_per); |
739 | ||
740 | if (status != 0) { | |
741 | printk(KERN_ERR "%s: Failed to write led%i_per register\n", | |
742 | DRIVER_NAME, led); | |
5c085d36 | 743 | mutex_unlock(&the_tps->lock); |
72cd7995 DB |
744 | return status; |
745 | } | |
746 | ||
65fc50e5 | 747 | pr_debug("%s: led%i_per 0x%02x\n", DRIVER_NAME, led, |
b5067f8f | 748 | i2c_smbus_read_byte_data(the_tps->client, |
65fc50e5 | 749 | TPS_LED1_PER + offs)); |
72cd7995 | 750 | |
5c085d36 | 751 | mutex_unlock(&the_tps->lock); |
72cd7995 DB |
752 | |
753 | return status; | |
754 | } | |
755 | EXPORT_SYMBOL(tps65010_set_led); | |
756 | ||
757 | /*-------------------------------------------------------------------------*/ | |
758 | /* tps65010_set_vib parameter: | |
759 | * value: ON or OFF | |
760 | */ | |
761 | int tps65010_set_vib(unsigned value) | |
762 | { | |
763 | int status; | |
764 | unsigned vdcdc2; | |
765 | ||
766 | if (!the_tps) | |
767 | return -ENODEV; | |
768 | ||
5c085d36 | 769 | mutex_lock(&the_tps->lock); |
72cd7995 | 770 | |
b5067f8f | 771 | vdcdc2 = i2c_smbus_read_byte_data(the_tps->client, TPS_VDCDC2); |
72cd7995 DB |
772 | vdcdc2 &= ~(1 << 1); |
773 | if (value) | |
774 | vdcdc2 |= (1 << 1); | |
b5067f8f | 775 | status = i2c_smbus_write_byte_data(the_tps->client, |
72cd7995 DB |
776 | TPS_VDCDC2, vdcdc2); |
777 | ||
778 | pr_debug("%s: vibrator %s\n", DRIVER_NAME, value ? "on" : "off"); | |
779 | ||
5c085d36 | 780 | mutex_unlock(&the_tps->lock); |
72cd7995 DB |
781 | return status; |
782 | } | |
783 | EXPORT_SYMBOL(tps65010_set_vib); | |
784 | ||
785 | /*-------------------------------------------------------------------------*/ | |
786 | /* tps65010_set_low_pwr parameter: | |
787 | * mode: ON or OFF | |
788 | */ | |
789 | int tps65010_set_low_pwr(unsigned mode) | |
790 | { | |
791 | int status; | |
792 | unsigned vdcdc1; | |
793 | ||
794 | if (!the_tps) | |
795 | return -ENODEV; | |
796 | ||
5c085d36 | 797 | mutex_lock(&the_tps->lock); |
72cd7995 DB |
798 | |
799 | pr_debug("%s: %s low_pwr, vdcdc1 0x%02x\n", DRIVER_NAME, | |
800 | mode ? "enable" : "disable", | |
b5067f8f | 801 | i2c_smbus_read_byte_data(the_tps->client, TPS_VDCDC1)); |
72cd7995 | 802 | |
b5067f8f | 803 | vdcdc1 = i2c_smbus_read_byte_data(the_tps->client, TPS_VDCDC1); |
72cd7995 DB |
804 | |
805 | switch (mode) { | |
806 | case OFF: | |
807 | vdcdc1 &= ~TPS_ENABLE_LP; /* disable ENABLE_LP bit */ | |
808 | break; | |
809 | /* case ON: */ | |
810 | default: | |
811 | vdcdc1 |= TPS_ENABLE_LP; /* enable ENABLE_LP bit */ | |
812 | break; | |
813 | } | |
814 | ||
b5067f8f | 815 | status = i2c_smbus_write_byte_data(the_tps->client, |
72cd7995 DB |
816 | TPS_VDCDC1, vdcdc1); |
817 | ||
818 | if (status != 0) | |
819 | printk(KERN_ERR "%s: Failed to write vdcdc1 register\n", | |
65fc50e5 | 820 | DRIVER_NAME); |
72cd7995 DB |
821 | else |
822 | pr_debug("%s: vdcdc1 0x%02x\n", DRIVER_NAME, | |
b5067f8f | 823 | i2c_smbus_read_byte_data(the_tps->client, TPS_VDCDC1)); |
72cd7995 | 824 | |
5c085d36 | 825 | mutex_unlock(&the_tps->lock); |
72cd7995 DB |
826 | |
827 | return status; | |
828 | } | |
829 | EXPORT_SYMBOL(tps65010_set_low_pwr); | |
830 | ||
831 | /*-------------------------------------------------------------------------*/ | |
832 | /* tps65010_config_vregs1 parameter: | |
833 | * value to be written to VREGS1 register | |
834 | * Note: The complete register is written, set all bits you need | |
835 | */ | |
836 | int tps65010_config_vregs1(unsigned value) | |
837 | { | |
838 | int status; | |
839 | ||
840 | if (!the_tps) | |
841 | return -ENODEV; | |
842 | ||
5c085d36 | 843 | mutex_lock(&the_tps->lock); |
72cd7995 DB |
844 | |
845 | pr_debug("%s: vregs1 0x%02x\n", DRIVER_NAME, | |
b5067f8f | 846 | i2c_smbus_read_byte_data(the_tps->client, TPS_VREGS1)); |
72cd7995 | 847 | |
b5067f8f | 848 | status = i2c_smbus_write_byte_data(the_tps->client, |
72cd7995 DB |
849 | TPS_VREGS1, value); |
850 | ||
851 | if (status != 0) | |
852 | printk(KERN_ERR "%s: Failed to write vregs1 register\n", | |
65fc50e5 | 853 | DRIVER_NAME); |
72cd7995 DB |
854 | else |
855 | pr_debug("%s: vregs1 0x%02x\n", DRIVER_NAME, | |
b5067f8f | 856 | i2c_smbus_read_byte_data(the_tps->client, TPS_VREGS1)); |
72cd7995 | 857 | |
5c085d36 | 858 | mutex_unlock(&the_tps->lock); |
72cd7995 DB |
859 | |
860 | return status; | |
861 | } | |
862 | EXPORT_SYMBOL(tps65010_config_vregs1); | |
863 | ||
864 | /*-------------------------------------------------------------------------*/ | |
865 | /* tps65013_set_low_pwr parameter: | |
866 | * mode: ON or OFF | |
867 | */ | |
868 | ||
869 | /* FIXME: Assumes AC or USB power is present. Setting AUA bit is not | |
870 | required if power supply is through a battery */ | |
871 | ||
872 | int tps65013_set_low_pwr(unsigned mode) | |
873 | { | |
874 | int status; | |
875 | unsigned vdcdc1, chgconfig; | |
876 | ||
877 | if (!the_tps || the_tps->por) | |
878 | return -ENODEV; | |
879 | ||
5c085d36 | 880 | mutex_lock(&the_tps->lock); |
72cd7995 DB |
881 | |
882 | pr_debug("%s: %s low_pwr, chgconfig 0x%02x vdcdc1 0x%02x\n", | |
883 | DRIVER_NAME, | |
884 | mode ? "enable" : "disable", | |
b5067f8f DB |
885 | i2c_smbus_read_byte_data(the_tps->client, TPS_CHGCONFIG), |
886 | i2c_smbus_read_byte_data(the_tps->client, TPS_VDCDC1)); | |
72cd7995 | 887 | |
b5067f8f DB |
888 | chgconfig = i2c_smbus_read_byte_data(the_tps->client, TPS_CHGCONFIG); |
889 | vdcdc1 = i2c_smbus_read_byte_data(the_tps->client, TPS_VDCDC1); | |
72cd7995 DB |
890 | |
891 | switch (mode) { | |
892 | case OFF: | |
893 | chgconfig &= ~TPS65013_AUA; /* disable AUA bit */ | |
894 | vdcdc1 &= ~TPS_ENABLE_LP; /* disable ENABLE_LP bit */ | |
895 | break; | |
896 | /* case ON: */ | |
897 | default: | |
898 | chgconfig |= TPS65013_AUA; /* enable AUA bit */ | |
899 | vdcdc1 |= TPS_ENABLE_LP; /* enable ENABLE_LP bit */ | |
900 | break; | |
901 | } | |
902 | ||
b5067f8f | 903 | status = i2c_smbus_write_byte_data(the_tps->client, |
72cd7995 DB |
904 | TPS_CHGCONFIG, chgconfig); |
905 | if (status != 0) { | |
906 | printk(KERN_ERR "%s: Failed to write chconfig register\n", | |
907 | DRIVER_NAME); | |
5c085d36 | 908 | mutex_unlock(&the_tps->lock); |
72cd7995 DB |
909 | return status; |
910 | } | |
911 | ||
b5067f8f | 912 | chgconfig = i2c_smbus_read_byte_data(the_tps->client, TPS_CHGCONFIG); |
72cd7995 DB |
913 | the_tps->chgconf = chgconfig; |
914 | show_chgconfig(0, "chgconf", chgconfig); | |
915 | ||
b5067f8f | 916 | status = i2c_smbus_write_byte_data(the_tps->client, |
72cd7995 DB |
917 | TPS_VDCDC1, vdcdc1); |
918 | ||
919 | if (status != 0) | |
920 | printk(KERN_ERR "%s: Failed to write vdcdc1 register\n", | |
921 | DRIVER_NAME); | |
922 | else | |
923 | pr_debug("%s: vdcdc1 0x%02x\n", DRIVER_NAME, | |
b5067f8f | 924 | i2c_smbus_read_byte_data(the_tps->client, TPS_VDCDC1)); |
72cd7995 | 925 | |
5c085d36 | 926 | mutex_unlock(&the_tps->lock); |
72cd7995 DB |
927 | |
928 | return status; | |
929 | } | |
930 | EXPORT_SYMBOL(tps65013_set_low_pwr); | |
931 | ||
932 | /*-------------------------------------------------------------------------*/ | |
933 | ||
934 | static int __init tps_init(void) | |
935 | { | |
936 | u32 tries = 3; | |
937 | int status = -ENODEV; | |
938 | ||
939 | printk(KERN_INFO "%s: version %s\n", DRIVER_NAME, DRIVER_VERSION); | |
940 | ||
941 | /* some boards have startup glitches */ | |
942 | while (tries--) { | |
943 | status = i2c_add_driver(&tps65010_driver); | |
944 | if (the_tps) | |
945 | break; | |
946 | i2c_del_driver(&tps65010_driver); | |
947 | if (!tries) { | |
948 | printk(KERN_ERR "%s: no chip?\n", DRIVER_NAME); | |
949 | return -ENODEV; | |
950 | } | |
951 | pr_debug("%s: re-probe ...\n", DRIVER_NAME); | |
952 | msleep(10); | |
953 | } | |
954 | ||
72cd7995 DB |
955 | return status; |
956 | } | |
957 | /* NOTE: this MUST be initialized before the other parts of the system | |
958 | * that rely on it ... but after the i2c bus on which this relies. | |
959 | * That is, much earlier than on PC-type systems, which don't often use | |
960 | * I2C as a core system bus. | |
961 | */ | |
962 | subsys_initcall(tps_init); | |
963 | ||
964 | static void __exit tps_exit(void) | |
965 | { | |
966 | i2c_del_driver(&tps65010_driver); | |
967 | } | |
968 | module_exit(tps_exit); | |
969 |