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