1
2
3
4
5
6
7
8
9
10
11
12
13
14
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>
26
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
44
45#define TWL4030_BCIMFSTS1 0x01
46
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
67
68#define TWL4030_BATSTSPCHG BIT(2)
69#define TWL4030_BATSTSMCHG BIT(6)
70
71
72#define TWL4030_WOVF BIT(0)
73#define TWL4030_TMOVF BIT(1)
74#define TWL4030_ICHGHIGH BIT(2)
75#define TWL4030_ICHGLOW BIT(3)
76#define TWL4030_ICHGEOC BIT(4)
77#define TWL4030_TBATOR2 BIT(5)
78#define TWL4030_TBATOR1 BIT(6)
79#define TWL4030_BATSTS BIT(7)
80
81#define TWL4030_VBATLVL BIT(0)
82#define TWL4030_VBATOV BIT(1)
83#define TWL4030_VBUSOV BIT(2)
84#define TWL4030_ACCHGOV BIT(3)
85
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
93
94
95
96
97
98static inline int ac_available(struct iio_channel *channel_vac)
99{
100 int val, err;
101
102 if (!channel_vac)
103 return 0;
104
105 err = iio_read_channel_processed(channel_vac, &val);
106 if (err < 0)
107 return 0;
108 return val > 4500;
109}
110
111static bool allow_usb;
112module_param(allow_usb, bool, 0644);
113MODULE_PARM_DESC(allow_usb, "Allow USB charge drawing default current");
114
115struct 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;
125
126
127
128
129
130
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;
136#define CHARGE_OFF 0
137#define CHARGE_AUTO 1
138#define CHARGE_LINEAR 2
139
140
141
142
143
144
145 unsigned int usb_cur_target;
146 struct delayed_work current_worker;
147#define USB_CUR_STEP 20000
148#define USB_MIN_VOLT 4750000
149#define USB_CUR_DELAY msecs_to_jiffies(100)
150#define USB_MAX_CURRENT 1700000
151
152 unsigned long event;
153};
154
155
156static char *modes[] = { "off", "auto", "continuous" };
157
158
159
160
161static int twl4030_clear_set(u8 mod_no, u8 clear, u8 set, u8 reg)
162{
163 u8 val = 0;
164 int ret;
165
166 ret = twl_i2c_read_u8(mod_no, &val, reg);
167 if (ret)
168 return ret;
169
170 val &= ~clear;
171 val |= set;
172
173 return twl_i2c_write_u8(mod_no, val, reg);
174}
175
176static int twl4030_bci_read(u8 reg, u8 *val)
177{
178 return twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, val, reg);
179}
180
181static int twl4030_clear_set_boot_bci(u8 clear, u8 set)
182{
183 return twl4030_clear_set(TWL_MODULE_PM_MASTER, clear,
184 TWL4030_CONFIG_DONE | TWL4030_BCIAUTOWEN | set,
185 TWL4030_PM_MASTER_BOOT_BCI);
186}
187
188static int twl4030bci_read_adc_val(u8 reg)
189{
190 int ret, temp;
191 u8 val;
192
193
194 ret = twl4030_bci_read(reg + 1, &val);
195 if (ret)
196 return ret;
197
198 temp = (int)(val & 0x03) << 8;
199
200
201 ret = twl4030_bci_read(reg, &val);
202 if (ret)
203 return ret;
204
205 return temp | val;
206}
207
208
209
210
211
212
213
214
215
216
217
218
219static int regval2ua(int regval, bool cgain)
220{
221 if (cgain)
222 return (regval * 16618 - 8500 * 1000) / 5;
223 else
224 return (regval * 16618 - 8500 * 1000) / 10;
225}
226
227
228
229
230static int ua2regval(int ua, bool cgain)
231{
232 int ret;
233 if (cgain)
234 ua /= 2;
235 ret = (ua * 10 + 8500 * 1000) / 16618;
236
237 if (ret < 512)
238 ret = 512;
239 return ret;
240}
241
242static int twl4030_charger_update_current(struct twl4030_bci *bci)
243{
244 int status;
245 int cur;
246 unsigned reg, cur_reg;
247 u8 bcictl1, oldreg, fullreg;
248 bool cgain = false;
249 u8 boot_bci;
250
251
252
253
254
255 if (ac_available(bci->channel_vac)) {
256 cur = bci->ac_cur;
257 bci->ac_is_active = true;
258 } else {
259 cur = bci->usb_cur;
260 bci->ac_is_active = false;
261 if (cur > bci->usb_cur_target) {
262 cur = bci->usb_cur_target;
263 bci->usb_cur = cur;
264 }
265 if (cur < bci->usb_cur_target)
266 schedule_delayed_work(&bci->current_worker, USB_CUR_DELAY);
267 }
268
269
270 if (bci->ichg_eoc >= 200000)
271 cgain = true;
272 if (bci->ichg_lo >= 400000)
273 cgain = true;
274 if (bci->ichg_hi >= 820000)
275 cgain = true;
276 if (cur > 852000)
277 cgain = true;
278
279 status = twl4030_bci_read(TWL4030_BCICTL1, &bcictl1);
280 if (status < 0)
281 return status;
282 if (twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &boot_bci,
283 TWL4030_PM_MASTER_BOOT_BCI) < 0)
284 boot_bci = 0;
285 boot_bci &= 7;
286
287 if ((!!cgain) != !!(bcictl1 & TWL4030_CGAIN))
288
289
290
291
292 twl4030_clear_set_boot_bci(boot_bci, 0);
293
294
295
296
297
298
299 reg = ua2regval(bci->ichg_eoc, cgain);
300 if (reg > 0x278)
301 reg = 0x278;
302 if (reg < 0x200)
303 reg = 0x200;
304 reg = (reg >> 3) & 0xf;
305 fullreg = reg << 4;
306
307
308
309
310
311 reg = ua2regval(bci->ichg_lo, cgain);
312 if (reg > 0x2F0)
313 reg = 0x2F0;
314 if (reg < 0x200)
315 reg = 0x200;
316 reg = (reg >> 4) & 0xf;
317 fullreg |= reg;
318
319
320 status = twl4030_bci_read(TWL4030_BCIMFTH8, &oldreg);
321 if (status < 0)
322 return status;
323 if (oldreg != fullreg) {
324 status = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0xF4,
325 TWL4030_BCIMFKEY);
326 if (status < 0)
327 return status;
328 twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
329 fullreg, TWL4030_BCIMFTH8);
330 }
331
332
333 reg = ua2regval(bci->ichg_hi, cgain);
334 if (reg > 0x3E0)
335 reg = 0x3E0;
336 if (reg < 0x200)
337 reg = 0x200;
338 fullreg = (reg >> 5) & 0xF;
339 fullreg <<= 4;
340 status = twl4030_bci_read(TWL4030_BCIMFTH9, &oldreg);
341 if (status < 0)
342 return status;
343 if ((oldreg & 0xF0) != fullreg) {
344 fullreg |= (oldreg & 0x0F);
345 status = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0xE7,
346 TWL4030_BCIMFKEY);
347 if (status < 0)
348 return status;
349 twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
350 fullreg, TWL4030_BCIMFTH9);
351 }
352
353
354
355
356
357 reg = ua2regval(cur, cgain);
358
359 if (reg > 0x3ff)
360 reg = 0x3ff;
361 status = twl4030_bci_read(TWL4030_BCIIREF1, &oldreg);
362 if (status < 0)
363 return status;
364 cur_reg = oldreg;
365 status = twl4030_bci_read(TWL4030_BCIIREF2, &oldreg);
366 if (status < 0)
367 return status;
368 cur_reg |= oldreg << 8;
369 if (reg != oldreg) {
370
371
372 status = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0xE7,
373 TWL4030_BCIMFKEY);
374 if (status < 0)
375 return status;
376 status = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
377 (reg & 0x100) ? 3 : 2,
378 TWL4030_BCIIREF2);
379 if (status < 0)
380 return status;
381
382
383 status = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0xE7,
384 TWL4030_BCIMFKEY);
385 if (status < 0)
386 return status;
387 status = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
388 reg & 0xff,
389 TWL4030_BCIIREF1);
390 }
391 if ((!!cgain) != !!(bcictl1 & TWL4030_CGAIN)) {
392
393 bcictl1 ^= TWL4030_CGAIN;
394 twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
395 bcictl1, TWL4030_BCICTL1);
396 twl4030_clear_set_boot_bci(0, boot_bci);
397 }
398 return 0;
399}
400
401static int twl4030_charger_get_current(void);
402
403static void twl4030_current_worker(struct work_struct *data)
404{
405 int v, curr;
406 int res;
407 struct twl4030_bci *bci = container_of(data, struct twl4030_bci,
408 current_worker.work);
409
410 res = twl4030bci_read_adc_val(TWL4030_BCIVBUS);
411 if (res < 0)
412 v = 0;
413 else
414
415 v = res * 6843;
416 curr = twl4030_charger_get_current();
417
418 dev_dbg(bci->dev, "v=%d cur=%d limit=%d target=%d\n", v, curr,
419 bci->usb_cur, bci->usb_cur_target);
420
421 if (v < USB_MIN_VOLT) {
422
423 bci->usb_cur -= USB_CUR_STEP;
424 bci->usb_cur_target = bci->usb_cur;
425 } else if (bci->usb_cur >= bci->usb_cur_target ||
426 bci->usb_cur + USB_CUR_STEP > USB_MAX_CURRENT) {
427
428 return;
429 } else {
430 bci->usb_cur += USB_CUR_STEP;
431 schedule_delayed_work(&bci->current_worker, USB_CUR_DELAY);
432 }
433 twl4030_charger_update_current(bci);
434}
435
436
437
438
439static int twl4030_charger_enable_usb(struct twl4030_bci *bci, bool enable)
440{
441 int ret;
442
443 if (bci->usb_mode == CHARGE_OFF)
444 enable = false;
445 if (enable && !IS_ERR_OR_NULL(bci->transceiver)) {
446
447 twl4030_charger_update_current(bci);
448
449
450 if (!bci->usb_enabled) {
451 pm_runtime_get_sync(bci->transceiver->dev);
452 bci->usb_enabled = 1;
453 }
454
455 if (bci->usb_mode == CHARGE_AUTO)
456
457 ret = twl4030_clear_set_boot_bci(0, TWL4030_BCIAUTOUSB);
458
459
460 ret = twl4030_clear_set(TWL_MODULE_MAIN_CHARGE, 0,
461 TWL4030_USBFASTMCHG, TWL4030_BCIMFSTS4);
462 if (bci->usb_mode == CHARGE_LINEAR) {
463 twl4030_clear_set_boot_bci(TWL4030_BCIAUTOAC|TWL4030_CVENAC, 0);
464
465 ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0x33,
466 TWL4030_BCIWDKEY);
467
468 ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0x2a,
469 TWL4030_BCIMDKEY);
470
471 ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0x26,
472 TWL4030_BCIMDKEY);
473
474 ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0xf3,
475 TWL4030_BCIWDKEY);
476
477 ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0x9c,
478 TWL4030_BCIMFKEY);
479
480
481
482
483
484 ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0xf0,
485 TWL4030_BCIMFEN3);
486 }
487 } else {
488 ret = twl4030_clear_set_boot_bci(TWL4030_BCIAUTOUSB, 0);
489 ret |= twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0x2a,
490 TWL4030_BCIMDKEY);
491 if (bci->usb_enabled) {
492 pm_runtime_mark_last_busy(bci->transceiver->dev);
493 pm_runtime_put_autosuspend(bci->transceiver->dev);
494 bci->usb_enabled = 0;
495 }
496 bci->usb_cur = 0;
497 }
498
499 return ret;
500}
501
502
503
504
505static int twl4030_charger_enable_ac(struct twl4030_bci *bci, bool enable)
506{
507 int ret;
508
509 if (bci->ac_mode == CHARGE_OFF)
510 enable = false;
511
512 if (enable)
513 ret = twl4030_clear_set_boot_bci(0, TWL4030_BCIAUTOAC);
514 else
515 ret = twl4030_clear_set_boot_bci(TWL4030_BCIAUTOAC, 0);
516
517 return ret;
518}
519
520
521
522
523static int twl4030_charger_enable_backup(int uvolt, int uamp)
524{
525 int ret;
526 u8 flags;
527
528 if (uvolt < 2500000 ||
529 uamp < 25) {
530
531 ret = twl4030_clear_set(TWL_MODULE_PM_RECEIVER,
532 TWL4030_BBCHEN, 0, TWL4030_BB_CFG);
533 return ret;
534 }
535
536 flags = TWL4030_BBCHEN;
537 if (uvolt >= 3200000)
538 flags |= TWL4030_BBSEL_3V2;
539 else if (uvolt >= 3100000)
540 flags |= TWL4030_BBSEL_3V1;
541 else if (uvolt >= 3000000)
542 flags |= TWL4030_BBSEL_3V0;
543 else
544 flags |= TWL4030_BBSEL_2V5;
545
546 if (uamp >= 1000)
547 flags |= TWL4030_BBISEL_1000uA;
548 else if (uamp >= 500)
549 flags |= TWL4030_BBISEL_500uA;
550 else if (uamp >= 150)
551 flags |= TWL4030_BBISEL_150uA;
552 else
553 flags |= TWL4030_BBISEL_25uA;
554
555 ret = twl4030_clear_set(TWL_MODULE_PM_RECEIVER,
556 TWL4030_BBSEL_MASK | TWL4030_BBISEL_MASK,
557 flags,
558 TWL4030_BB_CFG);
559
560 return ret;
561}
562
563
564
565
566static irqreturn_t twl4030_charger_interrupt(int irq, void *arg)
567{
568 struct twl4030_bci *bci = arg;
569
570 dev_dbg(bci->dev, "CHG_PRES irq\n");
571
572 bci->ac_cur = 500000;
573 twl4030_charger_update_current(bci);
574 power_supply_changed(bci->ac);
575 power_supply_changed(bci->usb);
576
577 return IRQ_HANDLED;
578}
579
580
581
582
583static irqreturn_t twl4030_bci_interrupt(int irq, void *arg)
584{
585 struct twl4030_bci *bci = arg;
586 u8 irqs1, irqs2;
587 int ret;
588
589 ret = twl_i2c_read_u8(TWL4030_MODULE_INTERRUPTS, &irqs1,
590 TWL4030_INTERRUPTS_BCIISR1A);
591 if (ret < 0)
592 return IRQ_HANDLED;
593
594 ret = twl_i2c_read_u8(TWL4030_MODULE_INTERRUPTS, &irqs2,
595 TWL4030_INTERRUPTS_BCIISR2A);
596 if (ret < 0)
597 return IRQ_HANDLED;
598
599 dev_dbg(bci->dev, "BCI irq %02x %02x\n", irqs2, irqs1);
600
601 if (irqs1 & (TWL4030_ICHGLOW | TWL4030_ICHGEOC)) {
602
603 power_supply_changed(bci->ac);
604 power_supply_changed(bci->usb);
605 }
606 twl4030_charger_update_current(bci);
607
608
609 if (irqs1 & (TWL4030_TBATOR2 | TWL4030_TBATOR1))
610 dev_warn(bci->dev, "battery temperature out of range\n");
611
612 if (irqs1 & TWL4030_BATSTS)
613 dev_crit(bci->dev, "battery disconnected\n");
614
615 if (irqs2 & TWL4030_VBATOV)
616 dev_crit(bci->dev, "VBAT overvoltage\n");
617
618 if (irqs2 & TWL4030_VBUSOV)
619 dev_crit(bci->dev, "VBUS overvoltage\n");
620
621 if (irqs2 & TWL4030_ACCHGOV)
622 dev_crit(bci->dev, "Ac charger overvoltage\n");
623
624 return IRQ_HANDLED;
625}
626
627
628
629
630static ssize_t
631twl4030_bci_max_current_store(struct device *dev, struct device_attribute *attr,
632 const char *buf, size_t n)
633{
634 struct twl4030_bci *bci = dev_get_drvdata(dev->parent);
635 int cur = 0;
636 int status = 0;
637 status = kstrtoint(buf, 10, &cur);
638 if (status)
639 return status;
640 if (cur < 0)
641 return -EINVAL;
642 if (dev == &bci->ac->dev)
643 bci->ac_cur = cur;
644 else
645 bci->usb_cur_target = cur;
646
647 twl4030_charger_update_current(bci);
648 return n;
649}
650
651
652
653
654static ssize_t twl4030_bci_max_current_show(struct device *dev,
655 struct device_attribute *attr, char *buf)
656{
657 int status = 0;
658 int cur = -1;
659 u8 bcictl1;
660 struct twl4030_bci *bci = dev_get_drvdata(dev->parent);
661
662 if (dev == &bci->ac->dev) {
663 if (!bci->ac_is_active)
664 cur = bci->ac_cur;
665 } else {
666 if (bci->ac_is_active)
667 cur = bci->usb_cur_target;
668 }
669 if (cur < 0) {
670 cur = twl4030bci_read_adc_val(TWL4030_BCIIREF1);
671 if (cur < 0)
672 return cur;
673 status = twl4030_bci_read(TWL4030_BCICTL1, &bcictl1);
674 if (status < 0)
675 return status;
676 cur = regval2ua(cur, bcictl1 & TWL4030_CGAIN);
677 }
678 return scnprintf(buf, PAGE_SIZE, "%u\n", cur);
679}
680
681static DEVICE_ATTR(max_current, 0644, twl4030_bci_max_current_show,
682 twl4030_bci_max_current_store);
683
684static void twl4030_bci_usb_work(struct work_struct *data)
685{
686 struct twl4030_bci *bci = container_of(data, struct twl4030_bci, work);
687
688 switch (bci->event) {
689 case USB_EVENT_VBUS:
690 case USB_EVENT_CHARGER:
691 twl4030_charger_enable_usb(bci, true);
692 break;
693 case USB_EVENT_NONE:
694 twl4030_charger_enable_usb(bci, false);
695 break;
696 }
697}
698
699static int twl4030_bci_usb_ncb(struct notifier_block *nb, unsigned long val,
700 void *priv)
701{
702 struct twl4030_bci *bci = container_of(nb, struct twl4030_bci, usb_nb);
703
704 dev_dbg(bci->dev, "OTG notify %lu\n", val);
705
706
707 if (allow_usb)
708 bci->usb_cur_target = 500000;
709 else
710 bci->usb_cur_target = 100000;
711
712 bci->event = val;
713 schedule_work(&bci->work);
714
715 return NOTIFY_OK;
716}
717
718
719
720
721static ssize_t
722twl4030_bci_mode_store(struct device *dev, struct device_attribute *attr,
723 const char *buf, size_t n)
724{
725 struct twl4030_bci *bci = dev_get_drvdata(dev->parent);
726 int mode;
727 int status;
728
729 if (sysfs_streq(buf, modes[0]))
730 mode = 0;
731 else if (sysfs_streq(buf, modes[1]))
732 mode = 1;
733 else if (sysfs_streq(buf, modes[2]))
734 mode = 2;
735 else
736 return -EINVAL;
737 if (dev == &bci->ac->dev) {
738 if (mode == 2)
739 return -EINVAL;
740 twl4030_charger_enable_ac(bci, false);
741 bci->ac_mode = mode;
742 status = twl4030_charger_enable_ac(bci, true);
743 } else {
744 twl4030_charger_enable_usb(bci, false);
745 bci->usb_mode = mode;
746 status = twl4030_charger_enable_usb(bci, true);
747 }
748 return (status == 0) ? n : status;
749}
750
751
752
753
754static ssize_t
755twl4030_bci_mode_show(struct device *dev,
756 struct device_attribute *attr, char *buf)
757{
758 struct twl4030_bci *bci = dev_get_drvdata(dev->parent);
759 int len = 0;
760 int i;
761 int mode = bci->usb_mode;
762
763 if (dev == &bci->ac->dev)
764 mode = bci->ac_mode;
765
766 for (i = 0; i < ARRAY_SIZE(modes); i++)
767 if (mode == i)
768 len += snprintf(buf+len, PAGE_SIZE-len,
769 "[%s] ", modes[i]);
770 else
771 len += snprintf(buf+len, PAGE_SIZE-len,
772 "%s ", modes[i]);
773 buf[len-1] = '\n';
774 return len;
775}
776static DEVICE_ATTR(mode, 0644, twl4030_bci_mode_show,
777 twl4030_bci_mode_store);
778
779static int twl4030_charger_get_current(void)
780{
781 int curr;
782 int ret;
783 u8 bcictl1;
784
785 curr = twl4030bci_read_adc_val(TWL4030_BCIICHG);
786 if (curr < 0)
787 return curr;
788
789 ret = twl4030_bci_read(TWL4030_BCICTL1, &bcictl1);
790 if (ret)
791 return ret;
792
793 return regval2ua(curr, bcictl1 & TWL4030_CGAIN);
794}
795
796
797
798
799
800static int twl4030bci_state(struct twl4030_bci *bci)
801{
802 int ret;
803 u8 state;
804
805 ret = twl4030_bci_read(TWL4030_BCIMSTATEC, &state);
806 if (ret) {
807 pr_err("twl4030_bci: error reading BCIMSTATEC\n");
808 return ret;
809 }
810
811 dev_dbg(bci->dev, "state: %02x\n", state);
812
813 return state;
814}
815
816static int twl4030_bci_state_to_status(int state)
817{
818 state &= TWL4030_MSTATEC_MASK;
819 if (TWL4030_MSTATEC_QUICK1 <= state && state <= TWL4030_MSTATEC_QUICK7)
820 return POWER_SUPPLY_STATUS_CHARGING;
821 else if (TWL4030_MSTATEC_COMPLETE1 <= state &&
822 state <= TWL4030_MSTATEC_COMPLETE4)
823 return POWER_SUPPLY_STATUS_FULL;
824 else
825 return POWER_SUPPLY_STATUS_NOT_CHARGING;
826}
827
828static int twl4030_bci_get_property(struct power_supply *psy,
829 enum power_supply_property psp,
830 union power_supply_propval *val)
831{
832 struct twl4030_bci *bci = dev_get_drvdata(psy->dev.parent);
833 int is_charging;
834 int state;
835 int ret;
836
837 state = twl4030bci_state(bci);
838 if (state < 0)
839 return state;
840
841 if (psy->desc->type == POWER_SUPPLY_TYPE_USB)
842 is_charging = state & TWL4030_MSTATEC_USB;
843 else
844 is_charging = state & TWL4030_MSTATEC_AC;
845 if (!is_charging) {
846 u8 s;
847 twl4030_bci_read(TWL4030_BCIMDEN, &s);
848 if (psy->desc->type == POWER_SUPPLY_TYPE_USB)
849 is_charging = s & 1;
850 else
851 is_charging = s & 2;
852 if (is_charging)
853
854 state = TWL4030_MSTATEC_QUICK1;
855 }
856
857 switch (psp) {
858 case POWER_SUPPLY_PROP_STATUS:
859 if (is_charging)
860 val->intval = twl4030_bci_state_to_status(state);
861 else
862 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
863 break;
864 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
865
866 if (!is_charging)
867 return -ENODATA;
868 if (psy->desc->type == POWER_SUPPLY_TYPE_USB) {
869 ret = twl4030bci_read_adc_val(TWL4030_BCIVBUS);
870 if (ret < 0)
871 return ret;
872
873 val->intval = ret * 6843;
874 } else {
875 ret = twl4030bci_read_adc_val(TWL4030_BCIVAC);
876 if (ret < 0)
877 return ret;
878
879 val->intval = ret * 9775;
880 }
881 break;
882 case POWER_SUPPLY_PROP_CURRENT_NOW:
883 if (!is_charging)
884 return -ENODATA;
885
886 ret = twl4030_charger_get_current();
887 if (ret < 0)
888 return ret;
889 val->intval = ret;
890 break;
891 case POWER_SUPPLY_PROP_ONLINE:
892 val->intval = is_charging &&
893 twl4030_bci_state_to_status(state) !=
894 POWER_SUPPLY_STATUS_NOT_CHARGING;
895 break;
896 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
897 val->intval = -1;
898 if (psy->desc->type != POWER_SUPPLY_TYPE_USB) {
899 if (!bci->ac_is_active)
900 val->intval = bci->ac_cur;
901 } else {
902 if (bci->ac_is_active)
903 val->intval = bci->usb_cur_target;
904 }
905 if (val->intval < 0) {
906 u8 bcictl1;
907
908 val->intval = twl4030bci_read_adc_val(TWL4030_BCIIREF1);
909 if (val->intval < 0)
910 return val->intval;
911 ret = twl4030_bci_read(TWL4030_BCICTL1, &bcictl1);
912 if (ret < 0)
913 return ret;
914 val->intval = regval2ua(val->intval, bcictl1 &
915 TWL4030_CGAIN);
916 }
917 break;
918 default:
919 return -EINVAL;
920 }
921
922 return 0;
923}
924
925static int twl4030_bci_set_property(struct power_supply *psy,
926 enum power_supply_property psp,
927 const union power_supply_propval *val)
928{
929 struct twl4030_bci *bci = dev_get_drvdata(psy->dev.parent);
930
931 switch (psp) {
932 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
933 if (psy->desc->type == POWER_SUPPLY_TYPE_USB)
934 bci->usb_cur_target = val->intval;
935 else
936 bci->ac_cur = val->intval;
937 twl4030_charger_update_current(bci);
938 break;
939 default:
940 return -EINVAL;
941 }
942
943 return 0;
944}
945
946static int twl4030_bci_property_is_writeable(struct power_supply *psy,
947 enum power_supply_property psp)
948{
949 switch (psp) {
950 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
951 return true;
952 default:
953 return false;
954 }
955}
956
957static enum power_supply_property twl4030_charger_props[] = {
958 POWER_SUPPLY_PROP_STATUS,
959 POWER_SUPPLY_PROP_ONLINE,
960 POWER_SUPPLY_PROP_VOLTAGE_NOW,
961 POWER_SUPPLY_PROP_CURRENT_NOW,
962 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
963};
964
965#ifdef CONFIG_OF
966static const struct twl4030_bci_platform_data *
967twl4030_bci_parse_dt(struct device *dev)
968{
969 struct device_node *np = dev->of_node;
970 struct twl4030_bci_platform_data *pdata;
971 u32 num;
972
973 if (!np)
974 return NULL;
975 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
976 if (!pdata)
977 return pdata;
978
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;
984}
985#else
986static inline const struct twl4030_bci_platform_data *
987twl4030_bci_parse_dt(struct device *dev)
988{
989 return NULL;
990}
991#endif
992
993static 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,
999 .set_property = twl4030_bci_set_property,
1000 .property_is_writeable = twl4030_bci_property_is_writeable,
1001};
1002
1003static const struct power_supply_desc twl4030_bci_usb_desc = {
1004 .name = "twl4030_usb",
1005 .type = POWER_SUPPLY_TYPE_USB,
1006 .properties = twl4030_charger_props,
1007 .num_properties = ARRAY_SIZE(twl4030_charger_props),
1008 .get_property = twl4030_bci_get_property,
1009 .set_property = twl4030_bci_set_property,
1010 .property_is_writeable = twl4030_bci_property_is_writeable,
1011};
1012
1013static int twl4030_bci_probe(struct platform_device *pdev)
1014{
1015 struct twl4030_bci *bci;
1016 const struct twl4030_bci_platform_data *pdata = pdev->dev.platform_data;
1017 int ret;
1018 u32 reg;
1019
1020 bci = devm_kzalloc(&pdev->dev, sizeof(*bci), GFP_KERNEL);
1021 if (bci == NULL)
1022 return -ENOMEM;
1023
1024 if (!pdata)
1025 pdata = twl4030_bci_parse_dt(&pdev->dev);
1026
1027 bci->ichg_eoc = 80100;
1028 bci->ichg_lo = 241000;
1029 bci->ichg_hi = 500000;
1030 bci->ac_cur = 500000;
1031 if (allow_usb)
1032 bci->usb_cur_target = 500000;
1033 else
1034 bci->usb_cur_target = 100000;
1035 bci->usb_mode = CHARGE_AUTO;
1036 bci->ac_mode = CHARGE_AUTO;
1037
1038 bci->dev = &pdev->dev;
1039 bci->irq_chg = platform_get_irq(pdev, 0);
1040 bci->irq_bci = platform_get_irq(pdev, 1);
1041
1042 platform_set_drvdata(pdev, bci);
1043
1044 bci->ac = devm_power_supply_register(&pdev->dev, &twl4030_bci_ac_desc,
1045 NULL);
1046 if (IS_ERR(bci->ac)) {
1047 ret = PTR_ERR(bci->ac);
1048 dev_err(&pdev->dev, "failed to register ac: %d\n", ret);
1049 return ret;
1050 }
1051
1052 bci->usb = devm_power_supply_register(&pdev->dev, &twl4030_bci_usb_desc,
1053 NULL);
1054 if (IS_ERR(bci->usb)) {
1055 ret = PTR_ERR(bci->usb);
1056 dev_err(&pdev->dev, "failed to register usb: %d\n", ret);
1057 return ret;
1058 }
1059
1060 ret = devm_request_threaded_irq(&pdev->dev, bci->irq_chg, NULL,
1061 twl4030_charger_interrupt, IRQF_ONESHOT, pdev->name,
1062 bci);
1063 if (ret < 0) {
1064 dev_err(&pdev->dev, "could not request irq %d, status %d\n",
1065 bci->irq_chg, ret);
1066 return ret;
1067 }
1068
1069 ret = devm_request_threaded_irq(&pdev->dev, bci->irq_bci, NULL,
1070 twl4030_bci_interrupt, IRQF_ONESHOT, pdev->name, bci);
1071 if (ret < 0) {
1072 dev_err(&pdev->dev, "could not request irq %d, status %d\n",
1073 bci->irq_bci, ret);
1074 return ret;
1075 }
1076
1077 bci->channel_vac = iio_channel_get(&pdev->dev, "vac");
1078 if (IS_ERR(bci->channel_vac)) {
1079 bci->channel_vac = NULL;
1080 dev_warn(&pdev->dev, "could not request vac iio channel");
1081 }
1082
1083 INIT_WORK(&bci->work, twl4030_bci_usb_work);
1084 INIT_DELAYED_WORK(&bci->current_worker, twl4030_current_worker);
1085
1086 bci->usb_nb.notifier_call = twl4030_bci_usb_ncb;
1087 if (bci->dev->of_node) {
1088 struct device_node *phynode;
1089
1090 phynode = of_find_compatible_node(bci->dev->of_node->parent,
1091 NULL, "ti,twl4030-usb");
1092 if (phynode)
1093 bci->transceiver = devm_usb_get_phy_by_node(
1094 bci->dev, phynode, &bci->usb_nb);
1095 }
1096
1097
1098 reg = ~(u32)(TWL4030_ICHGLOW | TWL4030_ICHGEOC | TWL4030_TBATOR2 |
1099 TWL4030_TBATOR1 | TWL4030_BATSTS);
1100 ret = twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS, reg,
1101 TWL4030_INTERRUPTS_BCIIMR1A);
1102 if (ret < 0) {
1103 dev_err(&pdev->dev, "failed to unmask interrupts: %d\n", ret);
1104 goto fail;
1105 }
1106
1107 reg = ~(u32)(TWL4030_VBATOV | TWL4030_VBUSOV | TWL4030_ACCHGOV);
1108 ret = twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS, reg,
1109 TWL4030_INTERRUPTS_BCIIMR2A);
1110 if (ret < 0)
1111 dev_warn(&pdev->dev, "failed to unmask interrupts: %d\n", ret);
1112
1113 twl4030_charger_update_current(bci);
1114 if (device_create_file(&bci->usb->dev, &dev_attr_max_current))
1115 dev_warn(&pdev->dev, "could not create sysfs file\n");
1116 if (device_create_file(&bci->usb->dev, &dev_attr_mode))
1117 dev_warn(&pdev->dev, "could not create sysfs file\n");
1118 if (device_create_file(&bci->ac->dev, &dev_attr_mode))
1119 dev_warn(&pdev->dev, "could not create sysfs file\n");
1120 if (device_create_file(&bci->ac->dev, &dev_attr_max_current))
1121 dev_warn(&pdev->dev, "could not create sysfs file\n");
1122
1123 twl4030_charger_enable_ac(bci, true);
1124 if (!IS_ERR_OR_NULL(bci->transceiver))
1125 twl4030_bci_usb_ncb(&bci->usb_nb,
1126 bci->transceiver->last_event,
1127 NULL);
1128 else
1129 twl4030_charger_enable_usb(bci, false);
1130 if (pdata)
1131 twl4030_charger_enable_backup(pdata->bb_uvolt,
1132 pdata->bb_uamp);
1133 else
1134 twl4030_charger_enable_backup(0, 0);
1135
1136 return 0;
1137fail:
1138 iio_channel_release(bci->channel_vac);
1139
1140 return ret;
1141}
1142
1143static int twl4030_bci_remove(struct platform_device *pdev)
1144{
1145 struct twl4030_bci *bci = platform_get_drvdata(pdev);
1146
1147 twl4030_charger_enable_ac(bci, false);
1148 twl4030_charger_enable_usb(bci, false);
1149 twl4030_charger_enable_backup(0, 0);
1150
1151 iio_channel_release(bci->channel_vac);
1152
1153 device_remove_file(&bci->usb->dev, &dev_attr_max_current);
1154 device_remove_file(&bci->usb->dev, &dev_attr_mode);
1155 device_remove_file(&bci->ac->dev, &dev_attr_max_current);
1156 device_remove_file(&bci->ac->dev, &dev_attr_mode);
1157
1158 twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS, 0xff,
1159 TWL4030_INTERRUPTS_BCIIMR1A);
1160 twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS, 0xff,
1161 TWL4030_INTERRUPTS_BCIIMR2A);
1162
1163 return 0;
1164}
1165
1166static const struct of_device_id twl_bci_of_match[] = {
1167 {.compatible = "ti,twl4030-bci", },
1168 { }
1169};
1170MODULE_DEVICE_TABLE(of, twl_bci_of_match);
1171
1172static struct platform_driver twl4030_bci_driver = {
1173 .probe = twl4030_bci_probe,
1174 .remove = twl4030_bci_remove,
1175 .driver = {
1176 .name = "twl4030_bci",
1177 .of_match_table = of_match_ptr(twl_bci_of_match),
1178 },
1179};
1180module_platform_driver(twl4030_bci_driver);
1181
1182MODULE_AUTHOR("Gražvydas Ignotas");
1183MODULE_DESCRIPTION("TWL4030 Battery Charger Interface driver");
1184MODULE_LICENSE("GPL");
1185MODULE_ALIAS("platform:twl4030_bci");
1186