1
2
3
4
5
6
7
8
9
10
11
12
13
14#include <linux/kernel.h>
15#include <linux/slab.h>
16#include <linux/module.h>
17#include <linux/platform_device.h>
18#include <linux/of.h>
19#include <linux/of_platform.h>
20#include <linux/interrupt.h>
21#include <linux/power_supply.h>
22#include <linux/notifier.h>
23#include <linux/usb/phy.h>
24#include <linux/iio/consumer.h>
25#include <linux/mfd/da9150/core.h>
26#include <linux/mfd/da9150/registers.h>
27
28
29struct da9150_charger {
30 struct da9150 *da9150;
31 struct device *dev;
32
33 struct power_supply *usb;
34 struct power_supply *battery;
35 struct power_supply *supply_online;
36
37 struct usb_phy *usb_phy;
38 struct notifier_block otg_nb;
39 struct work_struct otg_work;
40 unsigned long usb_event;
41
42 struct iio_channel *ibus_chan;
43 struct iio_channel *vbus_chan;
44 struct iio_channel *tjunc_chan;
45 struct iio_channel *vbat_chan;
46};
47
48static inline int da9150_charger_supply_online(struct da9150_charger *charger,
49 struct power_supply *psy,
50 union power_supply_propval *val)
51{
52 val->intval = (psy == charger->supply_online) ? 1 : 0;
53
54 return 0;
55}
56
57
58static int da9150_charger_vbus_voltage_now(struct da9150_charger *charger,
59 union power_supply_propval *val)
60{
61 int v_val, ret;
62
63
64 ret = iio_read_channel_processed(charger->vbus_chan, &v_val);
65 if (ret < 0)
66 return ret;
67
68
69 val->intval = v_val * 1000;
70
71 return 0;
72}
73
74static int da9150_charger_ibus_current_avg(struct da9150_charger *charger,
75 union power_supply_propval *val)
76{
77 int i_val, ret;
78
79
80 ret = iio_read_channel_processed(charger->ibus_chan, &i_val);
81 if (ret < 0)
82 return ret;
83
84
85 val->intval = i_val * 1000;
86
87 return 0;
88}
89
90static int da9150_charger_tjunc_temp(struct da9150_charger *charger,
91 union power_supply_propval *val)
92{
93 int t_val, ret;
94
95
96 ret = iio_read_channel_processed(charger->tjunc_chan, &t_val);
97 if (ret < 0)
98 return ret;
99
100
101 val->intval = t_val / 100;
102
103 return 0;
104}
105
106static enum power_supply_property da9150_charger_props[] = {
107 POWER_SUPPLY_PROP_ONLINE,
108 POWER_SUPPLY_PROP_VOLTAGE_NOW,
109 POWER_SUPPLY_PROP_CURRENT_AVG,
110 POWER_SUPPLY_PROP_TEMP,
111};
112
113static int da9150_charger_get_prop(struct power_supply *psy,
114 enum power_supply_property psp,
115 union power_supply_propval *val)
116{
117 struct da9150_charger *charger = dev_get_drvdata(psy->dev.parent);
118 int ret;
119
120 switch (psp) {
121 case POWER_SUPPLY_PROP_ONLINE:
122 ret = da9150_charger_supply_online(charger, psy, val);
123 break;
124 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
125 ret = da9150_charger_vbus_voltage_now(charger, val);
126 break;
127 case POWER_SUPPLY_PROP_CURRENT_AVG:
128 ret = da9150_charger_ibus_current_avg(charger, val);
129 break;
130 case POWER_SUPPLY_PROP_TEMP:
131 ret = da9150_charger_tjunc_temp(charger, val);
132 break;
133 default:
134 ret = -EINVAL;
135 break;
136 }
137
138 return ret;
139}
140
141
142static int da9150_charger_battery_status(struct da9150_charger *charger,
143 union power_supply_propval *val)
144{
145 u8 reg;
146
147
148 reg = da9150_reg_read(charger->da9150, DA9150_STATUS_H);
149
150 if (((reg & DA9150_VBUS_STAT_MASK) == DA9150_VBUS_STAT_OFF) ||
151 ((reg & DA9150_VBUS_STAT_MASK) == DA9150_VBUS_STAT_WAIT)) {
152 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
153
154 return 0;
155 }
156
157 reg = da9150_reg_read(charger->da9150, DA9150_STATUS_J);
158
159
160 switch (reg & DA9150_CHG_STAT_MASK) {
161 case DA9150_CHG_STAT_ACT:
162 case DA9150_CHG_STAT_PRE:
163 case DA9150_CHG_STAT_CC:
164 case DA9150_CHG_STAT_CV:
165 val->intval = POWER_SUPPLY_STATUS_CHARGING;
166 break;
167 case DA9150_CHG_STAT_OFF:
168 case DA9150_CHG_STAT_SUSP:
169 case DA9150_CHG_STAT_TEMP:
170 case DA9150_CHG_STAT_TIME:
171 case DA9150_CHG_STAT_BAT:
172 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
173 break;
174 case DA9150_CHG_STAT_FULL:
175 val->intval = POWER_SUPPLY_STATUS_FULL;
176 break;
177 default:
178 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
179 break;
180 }
181
182 return 0;
183}
184
185static int da9150_charger_battery_health(struct da9150_charger *charger,
186 union power_supply_propval *val)
187{
188 u8 reg;
189
190 reg = da9150_reg_read(charger->da9150, DA9150_STATUS_J);
191
192
193 switch (reg & DA9150_CHG_TEMP_MASK) {
194 case DA9150_CHG_TEMP_UNDER:
195 val->intval = POWER_SUPPLY_HEALTH_COLD;
196 return 0;
197 case DA9150_CHG_TEMP_OVER:
198 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
199 return 0;
200 default:
201 break;
202 }
203
204
205 switch (reg & DA9150_CHG_STAT_MASK) {
206 case DA9150_CHG_STAT_ACT:
207 case DA9150_CHG_STAT_PRE:
208 val->intval = POWER_SUPPLY_HEALTH_DEAD;
209 break;
210 case DA9150_CHG_STAT_TIME:
211 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
212 break;
213 default:
214 val->intval = POWER_SUPPLY_HEALTH_GOOD;
215 break;
216 }
217
218 return 0;
219}
220
221static int da9150_charger_battery_present(struct da9150_charger *charger,
222 union power_supply_propval *val)
223{
224 u8 reg;
225
226
227 reg = da9150_reg_read(charger->da9150, DA9150_STATUS_J);
228 if ((reg & DA9150_CHG_STAT_MASK) == DA9150_CHG_STAT_BAT)
229 val->intval = 0;
230 else
231 val->intval = 1;
232
233 return 0;
234}
235
236static int da9150_charger_battery_charge_type(struct da9150_charger *charger,
237 union power_supply_propval *val)
238{
239 u8 reg;
240
241 reg = da9150_reg_read(charger->da9150, DA9150_STATUS_J);
242
243 switch (reg & DA9150_CHG_STAT_MASK) {
244 case DA9150_CHG_STAT_CC:
245 val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
246 break;
247 case DA9150_CHG_STAT_ACT:
248 case DA9150_CHG_STAT_PRE:
249 case DA9150_CHG_STAT_CV:
250 val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
251 break;
252 default:
253 val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
254 break;
255 }
256
257 return 0;
258}
259
260static int da9150_charger_battery_voltage_min(struct da9150_charger *charger,
261 union power_supply_propval *val)
262{
263 u8 reg;
264
265 reg = da9150_reg_read(charger->da9150, DA9150_PPR_CHGCTRL_C);
266
267
268 val->intval = ((reg & DA9150_CHG_VFAULT_MASK) * 50000) + 2500000;
269
270 return 0;
271}
272
273static int da9150_charger_battery_voltage_now(struct da9150_charger *charger,
274 union power_supply_propval *val)
275{
276 int v_val, ret;
277
278
279 ret = iio_read_channel_processed(charger->vbat_chan, &v_val);
280 if (ret < 0)
281 return ret;
282
283 val->intval = v_val * 1000;
284
285 return 0;
286}
287
288static int da9150_charger_battery_current_max(struct da9150_charger *charger,
289 union power_supply_propval *val)
290{
291 int reg;
292
293 reg = da9150_reg_read(charger->da9150, DA9150_PPR_CHGCTRL_D);
294
295
296 val->intval = reg * 25000;
297
298 return 0;
299}
300
301static int da9150_charger_battery_voltage_max(struct da9150_charger *charger,
302 union power_supply_propval *val)
303{
304 u8 reg;
305
306 reg = da9150_reg_read(charger->da9150, DA9150_PPR_CHGCTRL_B);
307
308
309 val->intval = ((reg & DA9150_CHG_VBAT_MASK) * 25000) + 3650000;
310 return 0;
311}
312
313static enum power_supply_property da9150_charger_bat_props[] = {
314 POWER_SUPPLY_PROP_STATUS,
315 POWER_SUPPLY_PROP_ONLINE,
316 POWER_SUPPLY_PROP_HEALTH,
317 POWER_SUPPLY_PROP_PRESENT,
318 POWER_SUPPLY_PROP_CHARGE_TYPE,
319 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
320 POWER_SUPPLY_PROP_VOLTAGE_NOW,
321 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
322 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
323};
324
325static int da9150_charger_battery_get_prop(struct power_supply *psy,
326 enum power_supply_property psp,
327 union power_supply_propval *val)
328{
329 struct da9150_charger *charger = dev_get_drvdata(psy->dev.parent);
330 int ret;
331
332 switch (psp) {
333 case POWER_SUPPLY_PROP_STATUS:
334 ret = da9150_charger_battery_status(charger, val);
335 break;
336 case POWER_SUPPLY_PROP_ONLINE:
337 ret = da9150_charger_supply_online(charger, psy, val);
338 break;
339 case POWER_SUPPLY_PROP_HEALTH:
340 ret = da9150_charger_battery_health(charger, val);
341 break;
342 case POWER_SUPPLY_PROP_PRESENT:
343 ret = da9150_charger_battery_present(charger, val);
344 break;
345 case POWER_SUPPLY_PROP_CHARGE_TYPE:
346 ret = da9150_charger_battery_charge_type(charger, val);
347 break;
348 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
349 ret = da9150_charger_battery_voltage_min(charger, val);
350 break;
351 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
352 ret = da9150_charger_battery_voltage_now(charger, val);
353 break;
354 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
355 ret = da9150_charger_battery_current_max(charger, val);
356 break;
357 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
358 ret = da9150_charger_battery_voltage_max(charger, val);
359 break;
360 default:
361 ret = -EINVAL;
362 break;
363 }
364
365 return ret;
366}
367
368static irqreturn_t da9150_charger_chg_irq(int irq, void *data)
369{
370 struct da9150_charger *charger = data;
371
372 power_supply_changed(charger->battery);
373
374 return IRQ_HANDLED;
375}
376
377static irqreturn_t da9150_charger_tjunc_irq(int irq, void *data)
378{
379 struct da9150_charger *charger = data;
380
381
382 dev_crit(charger->dev, "TJunc over temperature!!!\n");
383 power_supply_changed(charger->usb);
384
385 return IRQ_HANDLED;
386}
387
388static irqreturn_t da9150_charger_vfault_irq(int irq, void *data)
389{
390 struct da9150_charger *charger = data;
391
392
393 dev_crit(charger->dev, "VSYS under voltage!!!\n");
394 power_supply_changed(charger->usb);
395 power_supply_changed(charger->battery);
396
397 return IRQ_HANDLED;
398}
399
400static irqreturn_t da9150_charger_vbus_irq(int irq, void *data)
401{
402 struct da9150_charger *charger = data;
403 u8 reg;
404
405 reg = da9150_reg_read(charger->da9150, DA9150_STATUS_H);
406
407
408 switch (reg & DA9150_VBUS_STAT_MASK) {
409 case DA9150_VBUS_STAT_OFF:
410 case DA9150_VBUS_STAT_WAIT:
411 charger->supply_online = charger->battery;
412 break;
413 case DA9150_VBUS_STAT_CHG:
414 charger->supply_online = charger->usb;
415 break;
416 default:
417 dev_warn(charger->dev, "Unknown VBUS state - reg = 0x%x\n",
418 reg);
419 charger->supply_online = NULL;
420 break;
421 }
422
423 power_supply_changed(charger->usb);
424 power_supply_changed(charger->battery);
425
426 return IRQ_HANDLED;
427}
428
429static void da9150_charger_otg_work(struct work_struct *data)
430{
431 struct da9150_charger *charger =
432 container_of(data, struct da9150_charger, otg_work);
433
434 switch (charger->usb_event) {
435 case USB_EVENT_ID:
436
437 da9150_set_bits(charger->da9150, DA9150_PPR_BKCTRL_A,
438 DA9150_VBUS_MODE_MASK, DA9150_VBUS_MODE_OTG);
439 break;
440 case USB_EVENT_NONE:
441
442 power_supply_changed(charger->usb);
443 power_supply_changed(charger->battery);
444 da9150_set_bits(charger->da9150, DA9150_PPR_BKCTRL_A,
445 DA9150_VBUS_MODE_MASK, DA9150_VBUS_MODE_CHG);
446 break;
447 }
448}
449
450static int da9150_charger_otg_ncb(struct notifier_block *nb, unsigned long val,
451 void *priv)
452{
453 struct da9150_charger *charger =
454 container_of(nb, struct da9150_charger, otg_nb);
455
456 dev_dbg(charger->dev, "DA9150 OTG notify %lu\n", val);
457
458 charger->usb_event = val;
459 schedule_work(&charger->otg_work);
460
461 return NOTIFY_OK;
462}
463
464static int da9150_charger_register_irq(struct platform_device *pdev,
465 irq_handler_t handler,
466 const char *irq_name)
467{
468 struct device *dev = &pdev->dev;
469 struct da9150_charger *charger = platform_get_drvdata(pdev);
470 int irq, ret;
471
472 irq = platform_get_irq_byname(pdev, irq_name);
473 if (irq < 0) {
474 dev_err(dev, "Failed to get IRQ CHG_STATUS: %d\n", irq);
475 return irq;
476 }
477
478 ret = request_threaded_irq(irq, NULL, handler, IRQF_ONESHOT, irq_name,
479 charger);
480 if (ret)
481 dev_err(dev, "Failed to request IRQ %d: %d\n", irq, ret);
482
483 return ret;
484}
485
486static void da9150_charger_unregister_irq(struct platform_device *pdev,
487 const char *irq_name)
488{
489 struct device *dev = &pdev->dev;
490 struct da9150_charger *charger = platform_get_drvdata(pdev);
491 int irq;
492
493 irq = platform_get_irq_byname(pdev, irq_name);
494 if (irq < 0) {
495 dev_err(dev, "Failed to get IRQ CHG_STATUS: %d\n", irq);
496 return;
497 }
498
499 free_irq(irq, charger);
500}
501
502static const struct power_supply_desc usb_desc = {
503 .name = "da9150-usb",
504 .type = POWER_SUPPLY_TYPE_USB,
505 .properties = da9150_charger_props,
506 .num_properties = ARRAY_SIZE(da9150_charger_props),
507 .get_property = da9150_charger_get_prop,
508};
509
510static const struct power_supply_desc battery_desc = {
511 .name = "da9150-battery",
512 .type = POWER_SUPPLY_TYPE_BATTERY,
513 .properties = da9150_charger_bat_props,
514 .num_properties = ARRAY_SIZE(da9150_charger_bat_props),
515 .get_property = da9150_charger_battery_get_prop,
516};
517
518static int da9150_charger_probe(struct platform_device *pdev)
519{
520 struct device *dev = &pdev->dev;
521 struct da9150 *da9150 = dev_get_drvdata(dev->parent);
522 struct da9150_charger *charger;
523 u8 reg;
524 int ret;
525
526 charger = devm_kzalloc(dev, sizeof(struct da9150_charger), GFP_KERNEL);
527 if (!charger)
528 return -ENOMEM;
529
530 platform_set_drvdata(pdev, charger);
531 charger->da9150 = da9150;
532 charger->dev = dev;
533
534
535 charger->ibus_chan = iio_channel_get(dev, "CHAN_IBUS");
536 if (IS_ERR(charger->ibus_chan)) {
537 ret = PTR_ERR(charger->ibus_chan);
538 goto ibus_chan_fail;
539 }
540
541 charger->vbus_chan = iio_channel_get(dev, "CHAN_VBUS");
542 if (IS_ERR(charger->vbus_chan)) {
543 ret = PTR_ERR(charger->vbus_chan);
544 goto vbus_chan_fail;
545 }
546
547 charger->tjunc_chan = iio_channel_get(dev, "CHAN_TJUNC");
548 if (IS_ERR(charger->tjunc_chan)) {
549 ret = PTR_ERR(charger->tjunc_chan);
550 goto tjunc_chan_fail;
551 }
552
553 charger->vbat_chan = iio_channel_get(dev, "CHAN_VBAT");
554 if (IS_ERR(charger->vbat_chan)) {
555 ret = PTR_ERR(charger->vbat_chan);
556 goto vbat_chan_fail;
557 }
558
559
560 charger->usb = power_supply_register(dev, &usb_desc, NULL);
561 if (IS_ERR(charger->usb)) {
562 ret = PTR_ERR(charger->usb);
563 goto usb_fail;
564 }
565
566 charger->battery = power_supply_register(dev, &battery_desc, NULL);
567 if (IS_ERR(charger->battery)) {
568 ret = PTR_ERR(charger->battery);
569 goto battery_fail;
570 }
571
572
573 reg = da9150_reg_read(da9150, DA9150_STATUS_H);
574
575 switch (reg & DA9150_VBUS_STAT_MASK) {
576 case DA9150_VBUS_STAT_OFF:
577 case DA9150_VBUS_STAT_WAIT:
578 charger->supply_online = charger->battery;
579 break;
580 case DA9150_VBUS_STAT_CHG:
581 charger->supply_online = charger->usb;
582 break;
583 default:
584 dev_warn(dev, "Unknown VBUS state - reg = 0x%x\n", reg);
585 charger->supply_online = NULL;
586 break;
587 }
588
589
590 charger->usb_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
591 if (!IS_ERR_OR_NULL(charger->usb_phy)) {
592 INIT_WORK(&charger->otg_work, da9150_charger_otg_work);
593 charger->otg_nb.notifier_call = da9150_charger_otg_ncb;
594 usb_register_notifier(charger->usb_phy, &charger->otg_nb);
595 }
596
597
598 ret = da9150_charger_register_irq(pdev, da9150_charger_chg_irq,
599 "CHG_STATUS");
600 if (ret < 0)
601 goto chg_irq_fail;
602
603 ret = da9150_charger_register_irq(pdev, da9150_charger_tjunc_irq,
604 "CHG_TJUNC");
605 if (ret < 0)
606 goto tjunc_irq_fail;
607
608 ret = da9150_charger_register_irq(pdev, da9150_charger_vfault_irq,
609 "CHG_VFAULT");
610 if (ret < 0)
611 goto vfault_irq_fail;
612
613 ret = da9150_charger_register_irq(pdev, da9150_charger_vbus_irq,
614 "CHG_VBUS");
615 if (ret < 0)
616 goto vbus_irq_fail;
617
618 return 0;
619
620
621vbus_irq_fail:
622 da9150_charger_unregister_irq(pdev, "CHG_VFAULT");
623vfault_irq_fail:
624 da9150_charger_unregister_irq(pdev, "CHG_TJUNC");
625tjunc_irq_fail:
626 da9150_charger_unregister_irq(pdev, "CHG_STATUS");
627chg_irq_fail:
628 if (!IS_ERR_OR_NULL(charger->usb_phy))
629 usb_unregister_notifier(charger->usb_phy, &charger->otg_nb);
630battery_fail:
631 power_supply_unregister(charger->usb);
632
633usb_fail:
634 iio_channel_release(charger->vbat_chan);
635
636vbat_chan_fail:
637 iio_channel_release(charger->tjunc_chan);
638
639tjunc_chan_fail:
640 iio_channel_release(charger->vbus_chan);
641
642vbus_chan_fail:
643 iio_channel_release(charger->ibus_chan);
644
645ibus_chan_fail:
646 return ret;
647}
648
649static int da9150_charger_remove(struct platform_device *pdev)
650{
651 struct da9150_charger *charger = platform_get_drvdata(pdev);
652 int irq;
653
654
655 irq = platform_get_irq_byname(pdev, "CHG_VBUS");
656 free_irq(irq, charger);
657
658 irq = platform_get_irq_byname(pdev, "CHG_VFAULT");
659 free_irq(irq, charger);
660
661 irq = platform_get_irq_byname(pdev, "CHG_TJUNC");
662 free_irq(irq, charger);
663
664 irq = platform_get_irq_byname(pdev, "CHG_STATUS");
665 free_irq(irq, charger);
666
667 if (!IS_ERR_OR_NULL(charger->usb_phy))
668 usb_unregister_notifier(charger->usb_phy, &charger->otg_nb);
669
670 power_supply_unregister(charger->battery);
671 power_supply_unregister(charger->usb);
672
673
674 iio_channel_release(charger->ibus_chan);
675 iio_channel_release(charger->vbus_chan);
676 iio_channel_release(charger->tjunc_chan);
677 iio_channel_release(charger->vbat_chan);
678
679 return 0;
680}
681
682static struct platform_driver da9150_charger_driver = {
683 .driver = {
684 .name = "da9150-charger",
685 },
686 .probe = da9150_charger_probe,
687 .remove = da9150_charger_remove,
688};
689
690module_platform_driver(da9150_charger_driver);
691
692MODULE_DESCRIPTION("Charger Driver for DA9150");
693MODULE_AUTHOR("Adam Thomson <Adam.Thomson.Opensource@diasemi.com>");
694MODULE_LICENSE("GPL");
695