HID: hiddev: Fix slab-out-of-bounds write in hiddev_ioctl_usage()
[linux/fpc-iii.git] / drivers / power / twl4030_charger.c
blob5b1f147b11cb0d77116810d4458a9967d4b8a10c
1 /*
2 * TWL4030/TPS65950 BCI (Battery Charger Interface) driver
4 * Copyright (C) 2010 Gražvydas Ignotas <notasas@gmail.com>
6 * based on twl4030_bci_battery.c by TI
7 * Copyright (C) 2008 Texas Instruments, Inc.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/err.h>
19 #include <linux/platform_device.h>
20 #include <linux/interrupt.h>
21 #include <linux/i2c/twl.h>
22 #include <linux/power_supply.h>
23 #include <linux/notifier.h>
24 #include <linux/usb/otg.h>
25 #include <linux/iio/consumer.h>
27 #define TWL4030_BCIMDEN 0x00
28 #define TWL4030_BCIMDKEY 0x01
29 #define TWL4030_BCIMSTATEC 0x02
30 #define TWL4030_BCIICHG 0x08
31 #define TWL4030_BCIVAC 0x0a
32 #define TWL4030_BCIVBUS 0x0c
33 #define TWL4030_BCIMFSTS3 0x0F
34 #define TWL4030_BCIMFSTS4 0x10
35 #define TWL4030_BCICTL1 0x23
36 #define TWL4030_BB_CFG 0x12
37 #define TWL4030_BCIIREF1 0x27
38 #define TWL4030_BCIIREF2 0x28
39 #define TWL4030_BCIMFKEY 0x11
40 #define TWL4030_BCIMFEN3 0x14
41 #define TWL4030_BCIMFTH8 0x1d
42 #define TWL4030_BCIMFTH9 0x1e
43 #define TWL4030_BCIWDKEY 0x21
45 #define TWL4030_BCIMFSTS1 0x01
47 #define TWL4030_BCIAUTOWEN BIT(5)
48 #define TWL4030_CONFIG_DONE BIT(4)
49 #define TWL4030_CVENAC BIT(2)
50 #define TWL4030_BCIAUTOUSB BIT(1)
51 #define TWL4030_BCIAUTOAC BIT(0)
52 #define TWL4030_CGAIN BIT(5)
53 #define TWL4030_USBFASTMCHG BIT(2)
54 #define TWL4030_STS_VBUS BIT(7)
55 #define TWL4030_STS_USB_ID BIT(2)
56 #define TWL4030_BBCHEN BIT(4)
57 #define TWL4030_BBSEL_MASK 0x0c
58 #define TWL4030_BBSEL_2V5 0x00
59 #define TWL4030_BBSEL_3V0 0x04
60 #define TWL4030_BBSEL_3V1 0x08
61 #define TWL4030_BBSEL_3V2 0x0c
62 #define TWL4030_BBISEL_MASK 0x03
63 #define TWL4030_BBISEL_25uA 0x00
64 #define TWL4030_BBISEL_150uA 0x01
65 #define TWL4030_BBISEL_500uA 0x02
66 #define TWL4030_BBISEL_1000uA 0x03
68 #define TWL4030_BATSTSPCHG BIT(2)
69 #define TWL4030_BATSTSMCHG BIT(6)
71 /* BCI interrupts */
72 #define TWL4030_WOVF BIT(0) /* Watchdog overflow */
73 #define TWL4030_TMOVF BIT(1) /* Timer overflow */
74 #define TWL4030_ICHGHIGH BIT(2) /* Battery charge current high */
75 #define TWL4030_ICHGLOW BIT(3) /* Battery cc. low / FSM state change */
76 #define TWL4030_ICHGEOC BIT(4) /* Battery current end-of-charge */
77 #define TWL4030_TBATOR2 BIT(5) /* Battery temperature out of range 2 */
78 #define TWL4030_TBATOR1 BIT(6) /* Battery temperature out of range 1 */
79 #define TWL4030_BATSTS BIT(7) /* Battery status */
81 #define TWL4030_VBATLVL BIT(0) /* VBAT level */
82 #define TWL4030_VBATOV BIT(1) /* VBAT overvoltage */
83 #define TWL4030_VBUSOV BIT(2) /* VBUS overvoltage */
84 #define TWL4030_ACCHGOV BIT(3) /* Ac charger overvoltage */
86 #define TWL4030_MSTATEC_USB BIT(4)
87 #define TWL4030_MSTATEC_AC BIT(5)
88 #define TWL4030_MSTATEC_MASK 0x0f
89 #define TWL4030_MSTATEC_QUICK1 0x02
90 #define TWL4030_MSTATEC_QUICK7 0x07
91 #define TWL4030_MSTATEC_COMPLETE1 0x0b
92 #define TWL4030_MSTATEC_COMPLETE4 0x0e
95 * If AC (Accessory Charger) voltage exceeds 4.5V (MADC 11)
96 * then AC is available.
98 static inline int ac_available(struct iio_channel *channel_vac)
100 int val, err;
102 if (!channel_vac)
103 return 0;
105 err = iio_read_channel_processed(channel_vac, &val);
106 if (err < 0)
107 return 0;
108 return val > 4500;
111 static bool allow_usb;
112 module_param(allow_usb, bool, 0644);
113 MODULE_PARM_DESC(allow_usb, "Allow USB charge drawing default current");
115 struct twl4030_bci {
116 struct device *dev;
117 struct power_supply *ac;
118 struct power_supply *usb;
119 struct usb_phy *transceiver;
120 struct notifier_block usb_nb;
121 struct work_struct work;
122 int irq_chg;
123 int irq_bci;
124 int usb_enabled;
127 * ichg_* and *_cur values in uA. If any are 'large', we set
128 * CGAIN to '1' which doubles the range for half the
129 * precision.
131 unsigned int ichg_eoc, ichg_lo, ichg_hi;
132 unsigned int usb_cur, ac_cur;
133 struct iio_channel *channel_vac;
134 bool ac_is_active;
135 int usb_mode, ac_mode; /* charging mode requested */
136 #define CHARGE_OFF 0
137 #define CHARGE_AUTO 1
138 #define CHARGE_LINEAR 2
140 /* When setting the USB current we slowly increase the
141 * requested current until target is reached or the voltage
142 * drops below 4.75V. In the latter case we step back one
143 * step.
145 unsigned int usb_cur_target;
146 struct delayed_work current_worker;
147 #define USB_CUR_STEP 20000 /* 20mA at a time */
148 #define USB_MIN_VOLT 4750000 /* 4.75V */
149 #define USB_CUR_DELAY msecs_to_jiffies(100)
150 #define USB_MAX_CURRENT 1700000 /* TWL4030 caps at 1.7A */
152 unsigned long event;
155 /* strings for 'usb_mode' values */
156 static char *modes[] = { "off", "auto", "continuous" };
159 * clear and set bits on an given register on a given module
161 static int twl4030_clear_set(u8 mod_no, u8 clear, u8 set, u8 reg)
163 u8 val = 0;
164 int ret;
166 ret = twl_i2c_read_u8(mod_no, &val, reg);
167 if (ret)
168 return ret;
170 val &= ~clear;
171 val |= set;
173 return twl_i2c_write_u8(mod_no, val, reg);
176 static int twl4030_bci_read(u8 reg, u8 *val)
178 return twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, val, reg);
181 static int twl4030_clear_set_boot_bci(u8 clear, u8 set)
183 return twl4030_clear_set(TWL_MODULE_PM_MASTER, clear,
184 TWL4030_CONFIG_DONE | TWL4030_BCIAUTOWEN | set,
185 TWL4030_PM_MASTER_BOOT_BCI);
188 static int twl4030bci_read_adc_val(u8 reg)
190 int ret, temp;
191 u8 val;
193 /* read MSB */
194 ret = twl4030_bci_read(reg + 1, &val);
195 if (ret)
196 return ret;
198 temp = (int)(val & 0x03) << 8;
200 /* read LSB */
201 ret = twl4030_bci_read(reg, &val);
202 if (ret)
203 return ret;
205 return temp | val;
209 * Check if Battery Pack was present
211 static int twl4030_is_battery_present(struct twl4030_bci *bci)
213 int ret;
214 u8 val = 0;
216 /* Battery presence in Main charge? */
217 ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, &val, TWL4030_BCIMFSTS3);
218 if (ret)
219 return ret;
220 if (val & TWL4030_BATSTSMCHG)
221 return 0;
224 * OK, It could be that bootloader did not enable main charger,
225 * pre-charge is h/w auto. So, Battery presence in Pre-charge?
227 ret = twl_i2c_read_u8(TWL4030_MODULE_PRECHARGE, &val,
228 TWL4030_BCIMFSTS1);
229 if (ret)
230 return ret;
231 if (val & TWL4030_BATSTSPCHG)
232 return 0;
234 return -ENODEV;
238 * TI provided formulas:
239 * CGAIN == 0: ICHG = (BCIICHG * 1.7) / (2^10 - 1) - 0.85
240 * CGAIN == 1: ICHG = (BCIICHG * 3.4) / (2^10 - 1) - 1.7
241 * Here we use integer approximation of:
242 * CGAIN == 0: val * 1.6618 - 0.85 * 1000
243 * CGAIN == 1: (val * 1.6618 - 0.85 * 1000) * 2
246 * convert twl register value for currents into uA
248 static int regval2ua(int regval, bool cgain)
250 if (cgain)
251 return (regval * 16618 - 8500 * 1000) / 5;
252 else
253 return (regval * 16618 - 8500 * 1000) / 10;
257 * convert uA currents into twl register value
259 static int ua2regval(int ua, bool cgain)
261 int ret;
262 if (cgain)
263 ua /= 2;
264 ret = (ua * 10 + 8500 * 1000) / 16618;
265 /* rounding problems */
266 if (ret < 512)
267 ret = 512;
268 return ret;
271 static int twl4030_charger_update_current(struct twl4030_bci *bci)
273 int status;
274 int cur;
275 unsigned reg, cur_reg;
276 u8 bcictl1, oldreg, fullreg;
277 bool cgain = false;
278 u8 boot_bci;
281 * If AC (Accessory Charger) voltage exceeds 4.5V (MADC 11)
282 * and AC is enabled, set current for 'ac'
284 if (ac_available(bci->channel_vac)) {
285 cur = bci->ac_cur;
286 bci->ac_is_active = true;
287 } else {
288 cur = bci->usb_cur;
289 bci->ac_is_active = false;
290 if (cur > bci->usb_cur_target) {
291 cur = bci->usb_cur_target;
292 bci->usb_cur = cur;
294 if (cur < bci->usb_cur_target)
295 schedule_delayed_work(&bci->current_worker, USB_CUR_DELAY);
298 /* First, check thresholds and see if cgain is needed */
299 if (bci->ichg_eoc >= 200000)
300 cgain = true;
301 if (bci->ichg_lo >= 400000)
302 cgain = true;
303 if (bci->ichg_hi >= 820000)
304 cgain = true;
305 if (cur > 852000)
306 cgain = true;
308 status = twl4030_bci_read(TWL4030_BCICTL1, &bcictl1);
309 if (status < 0)
310 return status;
311 if (twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &boot_bci,
312 TWL4030_PM_MASTER_BOOT_BCI) < 0)
313 boot_bci = 0;
314 boot_bci &= 7;
316 if ((!!cgain) != !!(bcictl1 & TWL4030_CGAIN))
317 /* Need to turn for charging while we change the
318 * CGAIN bit. Leave it off while everything is
319 * updated.
321 twl4030_clear_set_boot_bci(boot_bci, 0);
324 * For ichg_eoc, the hardware only supports reg values matching
325 * 100XXXX000, and requires the XXXX be stored in the high nibble
326 * of TWL4030_BCIMFTH8.
328 reg = ua2regval(bci->ichg_eoc, cgain);
329 if (reg > 0x278)
330 reg = 0x278;
331 if (reg < 0x200)
332 reg = 0x200;
333 reg = (reg >> 3) & 0xf;
334 fullreg = reg << 4;
337 * For ichg_lo, reg value must match 10XXXX0000.
338 * XXXX is stored in low nibble of TWL4030_BCIMFTH8.
340 reg = ua2regval(bci->ichg_lo, cgain);
341 if (reg > 0x2F0)
342 reg = 0x2F0;
343 if (reg < 0x200)
344 reg = 0x200;
345 reg = (reg >> 4) & 0xf;
346 fullreg |= reg;
348 /* ichg_eoc and ichg_lo live in same register */
349 status = twl4030_bci_read(TWL4030_BCIMFTH8, &oldreg);
350 if (status < 0)
351 return status;
352 if (oldreg != fullreg) {
353 status = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0xF4,
354 TWL4030_BCIMFKEY);
355 if (status < 0)
356 return status;
357 twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
358 fullreg, TWL4030_BCIMFTH8);
361 /* ichg_hi threshold must be 1XXXX01100 (I think) */
362 reg = ua2regval(bci->ichg_hi, cgain);
363 if (reg > 0x3E0)
364 reg = 0x3E0;
365 if (reg < 0x200)
366 reg = 0x200;
367 fullreg = (reg >> 5) & 0xF;
368 fullreg <<= 4;
369 status = twl4030_bci_read(TWL4030_BCIMFTH9, &oldreg);
370 if (status < 0)
371 return status;
372 if ((oldreg & 0xF0) != fullreg) {
373 fullreg |= (oldreg & 0x0F);
374 status = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0xE7,
375 TWL4030_BCIMFKEY);
376 if (status < 0)
377 return status;
378 twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
379 fullreg, TWL4030_BCIMFTH9);
383 * And finally, set the current. This is stored in
384 * two registers.
386 reg = ua2regval(cur, cgain);
387 /* we have only 10 bits */
388 if (reg > 0x3ff)
389 reg = 0x3ff;
390 status = twl4030_bci_read(TWL4030_BCIIREF1, &oldreg);
391 if (status < 0)
392 return status;
393 cur_reg = oldreg;
394 status = twl4030_bci_read(TWL4030_BCIIREF2, &oldreg);
395 if (status < 0)
396 return status;
397 cur_reg |= oldreg << 8;
398 if (reg != oldreg) {
399 /* disable write protection for one write access for
400 * BCIIREF */
401 status = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0xE7,
402 TWL4030_BCIMFKEY);
403 if (status < 0)
404 return status;
405 status = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
406 (reg & 0x100) ? 3 : 2,
407 TWL4030_BCIIREF2);
408 if (status < 0)
409 return status;
410 /* disable write protection for one write access for
411 * BCIIREF */
412 status = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0xE7,
413 TWL4030_BCIMFKEY);
414 if (status < 0)
415 return status;
416 status = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
417 reg & 0xff,
418 TWL4030_BCIIREF1);
420 if ((!!cgain) != !!(bcictl1 & TWL4030_CGAIN)) {
421 /* Flip CGAIN and re-enable charging */
422 bcictl1 ^= TWL4030_CGAIN;
423 twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
424 bcictl1, TWL4030_BCICTL1);
425 twl4030_clear_set_boot_bci(0, boot_bci);
427 return 0;
430 static int twl4030_charger_get_current(void);
432 static void twl4030_current_worker(struct work_struct *data)
434 int v, curr;
435 int res;
436 struct twl4030_bci *bci = container_of(data, struct twl4030_bci,
437 current_worker.work);
439 res = twl4030bci_read_adc_val(TWL4030_BCIVBUS);
440 if (res < 0)
441 v = 0;
442 else
443 /* BCIVBUS uses ADCIN8, 7/1023 V/step */
444 v = res * 6843;
445 curr = twl4030_charger_get_current();
447 dev_dbg(bci->dev, "v=%d cur=%d limit=%d target=%d\n", v, curr,
448 bci->usb_cur, bci->usb_cur_target);
450 if (v < USB_MIN_VOLT) {
451 /* Back up and stop adjusting. */
452 if (bci->usb_cur >= USB_CUR_STEP)
453 bci->usb_cur -= USB_CUR_STEP;
454 bci->usb_cur_target = bci->usb_cur;
455 } else if (bci->usb_cur >= bci->usb_cur_target ||
456 bci->usb_cur + USB_CUR_STEP > USB_MAX_CURRENT) {
457 /* Reached target and voltage is OK - stop */
458 return;
459 } else {
460 bci->usb_cur += USB_CUR_STEP;
461 schedule_delayed_work(&bci->current_worker, USB_CUR_DELAY);
463 twl4030_charger_update_current(bci);
467 * Enable/Disable USB Charge functionality.
469 static int twl4030_charger_enable_usb(struct twl4030_bci *bci, bool enable)
471 int ret;
472 u32 reg;
474 if (bci->usb_mode == CHARGE_OFF)
475 enable = false;
476 if (enable && !IS_ERR_OR_NULL(bci->transceiver)) {
478 twl4030_charger_update_current(bci);
480 /* Need to keep phy powered */
481 if (!bci->usb_enabled) {
482 pm_runtime_get_sync(bci->transceiver->dev);
483 bci->usb_enabled = 1;
486 if (bci->usb_mode == CHARGE_AUTO) {
487 /* Enable interrupts now. */
488 reg = ~(u32)(TWL4030_ICHGLOW | TWL4030_ICHGEOC |
489 TWL4030_TBATOR2 | TWL4030_TBATOR1 |
490 TWL4030_BATSTS);
491 ret = twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS, reg,
492 TWL4030_INTERRUPTS_BCIIMR1A);
493 if (ret < 0) {
494 dev_err(bci->dev,
495 "failed to unmask interrupts: %d\n",
496 ret);
497 return ret;
499 /* forcing the field BCIAUTOUSB (BOOT_BCI[1]) to 1 */
500 ret = twl4030_clear_set_boot_bci(0, TWL4030_BCIAUTOUSB);
503 /* forcing USBFASTMCHG(BCIMFSTS4[2]) to 1 */
504 ret = twl4030_clear_set(TWL_MODULE_MAIN_CHARGE, 0,
505 TWL4030_USBFASTMCHG, TWL4030_BCIMFSTS4);
506 if (bci->usb_mode == CHARGE_LINEAR) {
507 /* Enable interrupts now. */
508 reg = ~(u32)(TWL4030_ICHGLOW | TWL4030_TBATOR2 |
509 TWL4030_TBATOR1 | TWL4030_BATSTS);
510 ret = twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS, reg,
511 TWL4030_INTERRUPTS_BCIIMR1A);
512 if (ret < 0) {
513 dev_err(bci->dev,
514 "failed to unmask interrupts: %d\n",
515 ret);
516 return ret;
518 twl4030_clear_set_boot_bci(TWL4030_BCIAUTOAC|TWL4030_CVENAC, 0);
519 /* Watch dog key: WOVF acknowledge */
520 ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0x33,
521 TWL4030_BCIWDKEY);
522 /* 0x24 + EKEY6: off mode */
523 ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0x2a,
524 TWL4030_BCIMDKEY);
525 /* EKEY2: Linear charge: USB path */
526 ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0x26,
527 TWL4030_BCIMDKEY);
528 /* WDKEY5: stop watchdog count */
529 ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0xf3,
530 TWL4030_BCIWDKEY);
531 /* enable MFEN3 access */
532 ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0x9c,
533 TWL4030_BCIMFKEY);
534 /* ICHGEOCEN - end-of-charge monitor (current < 80mA)
535 * (charging continues)
536 * ICHGLOWEN - current level monitor (charge continues)
537 * don't monitor over-current or heat save
539 ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0xf0,
540 TWL4030_BCIMFEN3);
542 } else {
543 ret = twl4030_clear_set_boot_bci(TWL4030_BCIAUTOUSB, 0);
544 ret |= twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0x2a,
545 TWL4030_BCIMDKEY);
546 if (bci->usb_enabled) {
547 pm_runtime_mark_last_busy(bci->transceiver->dev);
548 pm_runtime_put_autosuspend(bci->transceiver->dev);
549 bci->usb_enabled = 0;
551 bci->usb_cur = 0;
554 return ret;
558 * Enable/Disable AC Charge funtionality.
560 static int twl4030_charger_enable_ac(struct twl4030_bci *bci, bool enable)
562 int ret;
564 if (bci->ac_mode == CHARGE_OFF)
565 enable = false;
567 if (enable)
568 ret = twl4030_clear_set_boot_bci(0, TWL4030_BCIAUTOAC);
569 else
570 ret = twl4030_clear_set_boot_bci(TWL4030_BCIAUTOAC, 0);
572 return ret;
576 * Enable/Disable charging of Backup Battery.
578 static int twl4030_charger_enable_backup(int uvolt, int uamp)
580 int ret;
581 u8 flags;
583 if (uvolt < 2500000 ||
584 uamp < 25) {
585 /* disable charging of backup battery */
586 ret = twl4030_clear_set(TWL_MODULE_PM_RECEIVER,
587 TWL4030_BBCHEN, 0, TWL4030_BB_CFG);
588 return ret;
591 flags = TWL4030_BBCHEN;
592 if (uvolt >= 3200000)
593 flags |= TWL4030_BBSEL_3V2;
594 else if (uvolt >= 3100000)
595 flags |= TWL4030_BBSEL_3V1;
596 else if (uvolt >= 3000000)
597 flags |= TWL4030_BBSEL_3V0;
598 else
599 flags |= TWL4030_BBSEL_2V5;
601 if (uamp >= 1000)
602 flags |= TWL4030_BBISEL_1000uA;
603 else if (uamp >= 500)
604 flags |= TWL4030_BBISEL_500uA;
605 else if (uamp >= 150)
606 flags |= TWL4030_BBISEL_150uA;
607 else
608 flags |= TWL4030_BBISEL_25uA;
610 ret = twl4030_clear_set(TWL_MODULE_PM_RECEIVER,
611 TWL4030_BBSEL_MASK | TWL4030_BBISEL_MASK,
612 flags,
613 TWL4030_BB_CFG);
615 return ret;
619 * TWL4030 CHG_PRES (AC charger presence) events
621 static irqreturn_t twl4030_charger_interrupt(int irq, void *arg)
623 struct twl4030_bci *bci = arg;
625 dev_dbg(bci->dev, "CHG_PRES irq\n");
626 /* reset current on each 'plug' event */
627 bci->ac_cur = 500000;
628 twl4030_charger_update_current(bci);
629 power_supply_changed(bci->ac);
630 power_supply_changed(bci->usb);
632 return IRQ_HANDLED;
636 * TWL4030 BCI monitoring events
638 static irqreturn_t twl4030_bci_interrupt(int irq, void *arg)
640 struct twl4030_bci *bci = arg;
641 u8 irqs1, irqs2;
642 int ret;
644 ret = twl_i2c_read_u8(TWL4030_MODULE_INTERRUPTS, &irqs1,
645 TWL4030_INTERRUPTS_BCIISR1A);
646 if (ret < 0)
647 return IRQ_HANDLED;
649 ret = twl_i2c_read_u8(TWL4030_MODULE_INTERRUPTS, &irqs2,
650 TWL4030_INTERRUPTS_BCIISR2A);
651 if (ret < 0)
652 return IRQ_HANDLED;
654 dev_dbg(bci->dev, "BCI irq %02x %02x\n", irqs2, irqs1);
656 if (irqs1 & (TWL4030_ICHGLOW | TWL4030_ICHGEOC)) {
657 /* charger state change, inform the core */
658 power_supply_changed(bci->ac);
659 power_supply_changed(bci->usb);
661 twl4030_charger_update_current(bci);
663 /* various monitoring events, for now we just log them here */
664 if (irqs1 & (TWL4030_TBATOR2 | TWL4030_TBATOR1))
665 dev_warn(bci->dev, "battery temperature out of range\n");
667 if (irqs1 & TWL4030_BATSTS)
668 dev_crit(bci->dev, "battery disconnected\n");
670 if (irqs2 & TWL4030_VBATOV)
671 dev_crit(bci->dev, "VBAT overvoltage\n");
673 if (irqs2 & TWL4030_VBUSOV)
674 dev_crit(bci->dev, "VBUS overvoltage\n");
676 if (irqs2 & TWL4030_ACCHGOV)
677 dev_crit(bci->dev, "Ac charger overvoltage\n");
679 return IRQ_HANDLED;
683 * Provide "max_current" attribute in sysfs.
685 static ssize_t
686 twl4030_bci_max_current_store(struct device *dev, struct device_attribute *attr,
687 const char *buf, size_t n)
689 struct twl4030_bci *bci = dev_get_drvdata(dev->parent);
690 int cur = 0;
691 int status = 0;
692 status = kstrtoint(buf, 10, &cur);
693 if (status)
694 return status;
695 if (cur < 0)
696 return -EINVAL;
697 if (dev == &bci->ac->dev)
698 bci->ac_cur = cur;
699 else
700 bci->usb_cur_target = cur;
702 twl4030_charger_update_current(bci);
703 return n;
707 * sysfs max_current show
709 static ssize_t twl4030_bci_max_current_show(struct device *dev,
710 struct device_attribute *attr, char *buf)
712 int status = 0;
713 int cur = -1;
714 u8 bcictl1;
715 struct twl4030_bci *bci = dev_get_drvdata(dev->parent);
717 if (dev == &bci->ac->dev) {
718 if (!bci->ac_is_active)
719 cur = bci->ac_cur;
720 } else {
721 if (bci->ac_is_active)
722 cur = bci->usb_cur_target;
724 if (cur < 0) {
725 cur = twl4030bci_read_adc_val(TWL4030_BCIIREF1);
726 if (cur < 0)
727 return cur;
728 status = twl4030_bci_read(TWL4030_BCICTL1, &bcictl1);
729 if (status < 0)
730 return status;
731 cur = regval2ua(cur, bcictl1 & TWL4030_CGAIN);
733 return scnprintf(buf, PAGE_SIZE, "%u\n", cur);
736 static DEVICE_ATTR(max_current, 0644, twl4030_bci_max_current_show,
737 twl4030_bci_max_current_store);
739 static void twl4030_bci_usb_work(struct work_struct *data)
741 struct twl4030_bci *bci = container_of(data, struct twl4030_bci, work);
743 switch (bci->event) {
744 case USB_EVENT_VBUS:
745 case USB_EVENT_CHARGER:
746 twl4030_charger_enable_usb(bci, true);
747 break;
748 case USB_EVENT_NONE:
749 twl4030_charger_enable_usb(bci, false);
750 break;
754 static int twl4030_bci_usb_ncb(struct notifier_block *nb, unsigned long val,
755 void *priv)
757 struct twl4030_bci *bci = container_of(nb, struct twl4030_bci, usb_nb);
759 dev_dbg(bci->dev, "OTG notify %lu\n", val);
761 /* reset current on each 'plug' event */
762 if (allow_usb)
763 bci->usb_cur_target = 500000;
764 else
765 bci->usb_cur_target = 100000;
767 bci->event = val;
768 schedule_work(&bci->work);
770 return NOTIFY_OK;
774 * sysfs charger enabled store
776 static ssize_t
777 twl4030_bci_mode_store(struct device *dev, struct device_attribute *attr,
778 const char *buf, size_t n)
780 struct twl4030_bci *bci = dev_get_drvdata(dev->parent);
781 int mode;
782 int status;
784 if (sysfs_streq(buf, modes[0]))
785 mode = 0;
786 else if (sysfs_streq(buf, modes[1]))
787 mode = 1;
788 else if (sysfs_streq(buf, modes[2]))
789 mode = 2;
790 else
791 return -EINVAL;
792 if (dev == &bci->ac->dev) {
793 if (mode == 2)
794 return -EINVAL;
795 twl4030_charger_enable_ac(bci, false);
796 bci->ac_mode = mode;
797 status = twl4030_charger_enable_ac(bci, true);
798 } else {
799 twl4030_charger_enable_usb(bci, false);
800 bci->usb_mode = mode;
801 status = twl4030_charger_enable_usb(bci, true);
803 return (status == 0) ? n : status;
807 * sysfs charger enabled show
809 static ssize_t
810 twl4030_bci_mode_show(struct device *dev,
811 struct device_attribute *attr, char *buf)
813 struct twl4030_bci *bci = dev_get_drvdata(dev->parent);
814 int len = 0;
815 int i;
816 int mode = bci->usb_mode;
818 if (dev == &bci->ac->dev)
819 mode = bci->ac_mode;
821 for (i = 0; i < ARRAY_SIZE(modes); i++)
822 if (mode == i)
823 len += snprintf(buf+len, PAGE_SIZE-len,
824 "[%s] ", modes[i]);
825 else
826 len += snprintf(buf+len, PAGE_SIZE-len,
827 "%s ", modes[i]);
828 buf[len-1] = '\n';
829 return len;
831 static DEVICE_ATTR(mode, 0644, twl4030_bci_mode_show,
832 twl4030_bci_mode_store);
834 static int twl4030_charger_get_current(void)
836 int curr;
837 int ret;
838 u8 bcictl1;
840 curr = twl4030bci_read_adc_val(TWL4030_BCIICHG);
841 if (curr < 0)
842 return curr;
844 ret = twl4030_bci_read(TWL4030_BCICTL1, &bcictl1);
845 if (ret)
846 return ret;
848 return regval2ua(curr, bcictl1 & TWL4030_CGAIN);
852 * Returns the main charge FSM state
853 * Or < 0 on failure.
855 static int twl4030bci_state(struct twl4030_bci *bci)
857 int ret;
858 u8 state;
860 ret = twl4030_bci_read(TWL4030_BCIMSTATEC, &state);
861 if (ret) {
862 pr_err("twl4030_bci: error reading BCIMSTATEC\n");
863 return ret;
866 dev_dbg(bci->dev, "state: %02x\n", state);
868 return state;
871 static int twl4030_bci_state_to_status(int state)
873 state &= TWL4030_MSTATEC_MASK;
874 if (TWL4030_MSTATEC_QUICK1 <= state && state <= TWL4030_MSTATEC_QUICK7)
875 return POWER_SUPPLY_STATUS_CHARGING;
876 else if (TWL4030_MSTATEC_COMPLETE1 <= state &&
877 state <= TWL4030_MSTATEC_COMPLETE4)
878 return POWER_SUPPLY_STATUS_FULL;
879 else
880 return POWER_SUPPLY_STATUS_NOT_CHARGING;
883 static int twl4030_bci_get_property(struct power_supply *psy,
884 enum power_supply_property psp,
885 union power_supply_propval *val)
887 struct twl4030_bci *bci = dev_get_drvdata(psy->dev.parent);
888 int is_charging;
889 int state;
890 int ret;
892 state = twl4030bci_state(bci);
893 if (state < 0)
894 return state;
896 if (psy->desc->type == POWER_SUPPLY_TYPE_USB)
897 is_charging = state & TWL4030_MSTATEC_USB;
898 else
899 is_charging = state & TWL4030_MSTATEC_AC;
900 if (!is_charging) {
901 u8 s;
902 twl4030_bci_read(TWL4030_BCIMDEN, &s);
903 if (psy->desc->type == POWER_SUPPLY_TYPE_USB)
904 is_charging = s & 1;
905 else
906 is_charging = s & 2;
907 if (is_charging)
908 /* A little white lie */
909 state = TWL4030_MSTATEC_QUICK1;
912 switch (psp) {
913 case POWER_SUPPLY_PROP_STATUS:
914 if (is_charging)
915 val->intval = twl4030_bci_state_to_status(state);
916 else
917 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
918 break;
919 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
920 /* charging must be active for meaningful result */
921 if (!is_charging)
922 return -ENODATA;
923 if (psy->desc->type == POWER_SUPPLY_TYPE_USB) {
924 ret = twl4030bci_read_adc_val(TWL4030_BCIVBUS);
925 if (ret < 0)
926 return ret;
927 /* BCIVBUS uses ADCIN8, 7/1023 V/step */
928 val->intval = ret * 6843;
929 } else {
930 ret = twl4030bci_read_adc_val(TWL4030_BCIVAC);
931 if (ret < 0)
932 return ret;
933 /* BCIVAC uses ADCIN11, 10/1023 V/step */
934 val->intval = ret * 9775;
936 break;
937 case POWER_SUPPLY_PROP_CURRENT_NOW:
938 if (!is_charging)
939 return -ENODATA;
940 /* current measurement is shared between AC and USB */
941 ret = twl4030_charger_get_current();
942 if (ret < 0)
943 return ret;
944 val->intval = ret;
945 break;
946 case POWER_SUPPLY_PROP_ONLINE:
947 val->intval = is_charging &&
948 twl4030_bci_state_to_status(state) !=
949 POWER_SUPPLY_STATUS_NOT_CHARGING;
950 break;
951 default:
952 return -EINVAL;
955 return 0;
958 static enum power_supply_property twl4030_charger_props[] = {
959 POWER_SUPPLY_PROP_STATUS,
960 POWER_SUPPLY_PROP_ONLINE,
961 POWER_SUPPLY_PROP_VOLTAGE_NOW,
962 POWER_SUPPLY_PROP_CURRENT_NOW,
965 #ifdef CONFIG_OF
966 static const struct twl4030_bci_platform_data *
967 twl4030_bci_parse_dt(struct device *dev)
969 struct device_node *np = dev->of_node;
970 struct twl4030_bci_platform_data *pdata;
971 u32 num;
973 if (!np)
974 return NULL;
975 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
976 if (!pdata)
977 return pdata;
979 if (of_property_read_u32(np, "ti,bb-uvolt", &num) == 0)
980 pdata->bb_uvolt = num;
981 if (of_property_read_u32(np, "ti,bb-uamp", &num) == 0)
982 pdata->bb_uamp = num;
983 return pdata;
985 #else
986 static inline const struct twl4030_bci_platform_data *
987 twl4030_bci_parse_dt(struct device *dev)
989 return NULL;
991 #endif
993 static const struct power_supply_desc twl4030_bci_ac_desc = {
994 .name = "twl4030_ac",
995 .type = POWER_SUPPLY_TYPE_MAINS,
996 .properties = twl4030_charger_props,
997 .num_properties = ARRAY_SIZE(twl4030_charger_props),
998 .get_property = twl4030_bci_get_property,
1001 static const struct power_supply_desc twl4030_bci_usb_desc = {
1002 .name = "twl4030_usb",
1003 .type = POWER_SUPPLY_TYPE_USB,
1004 .properties = twl4030_charger_props,
1005 .num_properties = ARRAY_SIZE(twl4030_charger_props),
1006 .get_property = twl4030_bci_get_property,
1009 static int twl4030_bci_probe(struct platform_device *pdev)
1011 struct twl4030_bci *bci;
1012 const struct twl4030_bci_platform_data *pdata = pdev->dev.platform_data;
1013 int ret;
1014 u32 reg;
1016 bci = devm_kzalloc(&pdev->dev, sizeof(*bci), GFP_KERNEL);
1017 if (bci == NULL)
1018 return -ENOMEM;
1020 if (!pdata)
1021 pdata = twl4030_bci_parse_dt(&pdev->dev);
1023 bci->ichg_eoc = 80100; /* Stop charging when current drops to here */
1024 bci->ichg_lo = 241000; /* Low threshold */
1025 bci->ichg_hi = 500000; /* High threshold */
1026 bci->ac_cur = 500000; /* 500mA */
1027 if (allow_usb)
1028 bci->usb_cur_target = 500000; /* 500mA */
1029 else
1030 bci->usb_cur_target = 100000; /* 100mA */
1031 bci->usb_mode = CHARGE_AUTO;
1032 bci->ac_mode = CHARGE_AUTO;
1034 bci->dev = &pdev->dev;
1035 bci->irq_chg = platform_get_irq(pdev, 0);
1036 bci->irq_bci = platform_get_irq(pdev, 1);
1038 /* Only proceed further *IF* battery is physically present */
1039 ret = twl4030_is_battery_present(bci);
1040 if (ret) {
1041 dev_crit(&pdev->dev, "Battery was not detected:%d\n", ret);
1042 return ret;
1045 platform_set_drvdata(pdev, bci);
1047 bci->ac = devm_power_supply_register(&pdev->dev, &twl4030_bci_ac_desc,
1048 NULL);
1049 if (IS_ERR(bci->ac)) {
1050 ret = PTR_ERR(bci->ac);
1051 dev_err(&pdev->dev, "failed to register ac: %d\n", ret);
1052 return ret;
1055 bci->usb = devm_power_supply_register(&pdev->dev, &twl4030_bci_usb_desc,
1056 NULL);
1057 if (IS_ERR(bci->usb)) {
1058 ret = PTR_ERR(bci->usb);
1059 dev_err(&pdev->dev, "failed to register usb: %d\n", ret);
1060 return ret;
1063 ret = devm_request_threaded_irq(&pdev->dev, bci->irq_chg, NULL,
1064 twl4030_charger_interrupt, IRQF_ONESHOT, pdev->name,
1065 bci);
1066 if (ret < 0) {
1067 dev_err(&pdev->dev, "could not request irq %d, status %d\n",
1068 bci->irq_chg, ret);
1069 return ret;
1072 ret = devm_request_threaded_irq(&pdev->dev, bci->irq_bci, NULL,
1073 twl4030_bci_interrupt, IRQF_ONESHOT, pdev->name, bci);
1074 if (ret < 0) {
1075 dev_err(&pdev->dev, "could not request irq %d, status %d\n",
1076 bci->irq_bci, ret);
1077 return ret;
1080 bci->channel_vac = iio_channel_get(&pdev->dev, "vac");
1081 if (IS_ERR(bci->channel_vac)) {
1082 bci->channel_vac = NULL;
1083 dev_warn(&pdev->dev, "could not request vac iio channel");
1086 INIT_WORK(&bci->work, twl4030_bci_usb_work);
1087 INIT_DELAYED_WORK(&bci->current_worker, twl4030_current_worker);
1089 bci->usb_nb.notifier_call = twl4030_bci_usb_ncb;
1090 if (bci->dev->of_node) {
1091 struct device_node *phynode;
1093 phynode = of_find_compatible_node(bci->dev->of_node->parent,
1094 NULL, "ti,twl4030-usb");
1095 if (phynode)
1096 bci->transceiver = devm_usb_get_phy_by_node(
1097 bci->dev, phynode, &bci->usb_nb);
1100 /* Enable interrupts now. */
1101 reg = ~(u32)(TWL4030_ICHGLOW | TWL4030_ICHGEOC | TWL4030_TBATOR2 |
1102 TWL4030_TBATOR1 | TWL4030_BATSTS);
1103 ret = twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS, reg,
1104 TWL4030_INTERRUPTS_BCIIMR1A);
1105 if (ret < 0) {
1106 dev_err(&pdev->dev, "failed to unmask interrupts: %d\n", ret);
1107 goto fail;
1110 reg = ~(u32)(TWL4030_VBATOV | TWL4030_VBUSOV | TWL4030_ACCHGOV);
1111 ret = twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS, reg,
1112 TWL4030_INTERRUPTS_BCIIMR2A);
1113 if (ret < 0)
1114 dev_warn(&pdev->dev, "failed to unmask interrupts: %d\n", ret);
1116 twl4030_charger_update_current(bci);
1117 if (device_create_file(&bci->usb->dev, &dev_attr_max_current))
1118 dev_warn(&pdev->dev, "could not create sysfs file\n");
1119 if (device_create_file(&bci->usb->dev, &dev_attr_mode))
1120 dev_warn(&pdev->dev, "could not create sysfs file\n");
1121 if (device_create_file(&bci->ac->dev, &dev_attr_mode))
1122 dev_warn(&pdev->dev, "could not create sysfs file\n");
1123 if (device_create_file(&bci->ac->dev, &dev_attr_max_current))
1124 dev_warn(&pdev->dev, "could not create sysfs file\n");
1126 twl4030_charger_enable_ac(bci, true);
1127 if (!IS_ERR_OR_NULL(bci->transceiver))
1128 twl4030_bci_usb_ncb(&bci->usb_nb,
1129 bci->transceiver->last_event,
1130 NULL);
1131 else
1132 twl4030_charger_enable_usb(bci, false);
1133 if (pdata)
1134 twl4030_charger_enable_backup(pdata->bb_uvolt,
1135 pdata->bb_uamp);
1136 else
1137 twl4030_charger_enable_backup(0, 0);
1139 return 0;
1140 fail:
1141 iio_channel_release(bci->channel_vac);
1143 return ret;
1146 static int __exit twl4030_bci_remove(struct platform_device *pdev)
1148 struct twl4030_bci *bci = platform_get_drvdata(pdev);
1150 twl4030_charger_enable_ac(bci, false);
1151 twl4030_charger_enable_usb(bci, false);
1152 twl4030_charger_enable_backup(0, 0);
1154 iio_channel_release(bci->channel_vac);
1156 device_remove_file(&bci->usb->dev, &dev_attr_max_current);
1157 device_remove_file(&bci->usb->dev, &dev_attr_mode);
1158 device_remove_file(&bci->ac->dev, &dev_attr_max_current);
1159 device_remove_file(&bci->ac->dev, &dev_attr_mode);
1160 /* mask interrupts */
1161 twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS, 0xff,
1162 TWL4030_INTERRUPTS_BCIIMR1A);
1163 twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS, 0xff,
1164 TWL4030_INTERRUPTS_BCIIMR2A);
1166 return 0;
1169 static const struct of_device_id twl_bci_of_match[] = {
1170 {.compatible = "ti,twl4030-bci", },
1173 MODULE_DEVICE_TABLE(of, twl_bci_of_match);
1175 static struct platform_driver twl4030_bci_driver = {
1176 .probe = twl4030_bci_probe,
1177 .driver = {
1178 .name = "twl4030_bci",
1179 .of_match_table = of_match_ptr(twl_bci_of_match),
1181 .remove = __exit_p(twl4030_bci_remove),
1183 module_platform_driver(twl4030_bci_driver);
1185 MODULE_AUTHOR("Gražvydas Ignotas");
1186 MODULE_DESCRIPTION("TWL4030 Battery Charger Interface driver");
1187 MODULE_LICENSE("GPL");
1188 MODULE_ALIAS("platform:twl4030_bci");