1
2
3
4
5
6
7
8
9
10
11
12
13#include <linux/init.h>
14#include <linux/module.h>
15#include <linux/device.h>
16#include <linux/interrupt.h>
17#include <linux/delay.h>
18#include <linux/slab.h>
19#include <linux/platform_device.h>
20#include <linux/power_supply.h>
21#include <linux/completion.h>
22#include <linux/regulator/consumer.h>
23#include <linux/err.h>
24#include <linux/workqueue.h>
25#include <linux/kobject.h>
26#include <linux/mfd/abx500/ab8500.h>
27#include <linux/mfd/abx500.h>
28#include <linux/mfd/abx500/ab8500-bm.h>
29#include <linux/mfd/abx500/ab8500-gpadc.h>
30#include <linux/mfd/abx500/ux500_chargalg.h>
31#include <linux/usb/otg.h>
32
33
34#define NO_PW_CONN 0
35#define AC_PW_CONN 1
36#define USB_PW_CONN 2
37
38#define MAIN_WDOG_ENA 0x01
39#define MAIN_WDOG_KICK 0x02
40#define MAIN_WDOG_DIS 0x00
41#define CHARG_WD_KICK 0x01
42#define MAIN_CH_ENA 0x01
43#define MAIN_CH_NO_OVERSHOOT_ENA_N 0x02
44#define USB_CH_ENA 0x01
45#define USB_CHG_NO_OVERSHOOT_ENA_N 0x02
46#define MAIN_CH_DET 0x01
47#define MAIN_CH_CV_ON 0x04
48#define USB_CH_CV_ON 0x08
49#define VBUS_DET_DBNC100 0x02
50#define VBUS_DET_DBNC1 0x01
51#define OTP_ENABLE_WD 0x01
52
53#define MAIN_CH_INPUT_CURR_SHIFT 4
54#define VBUS_IN_CURR_LIM_SHIFT 4
55
56#define LED_INDICATOR_PWM_ENA 0x01
57#define LED_INDICATOR_PWM_DIS 0x00
58#define LED_IND_CUR_5MA 0x04
59#define LED_INDICATOR_PWM_DUTY_252_256 0xBF
60
61
62#define MAIN_CH_TH_PROT 0x02
63#define VBUS_CH_NOK 0x08
64#define USB_CH_TH_PROT 0x02
65#define VBUS_OVV_TH 0x01
66#define MAIN_CH_NOK 0x01
67#define VBUS_DET 0x80
68
69
70#define AB8500_USB_LINK_STATUS 0x78
71#define AB8500_STD_HOST_SUSP 0x18
72
73
74#define WD_TIMER 0x30
75#define WD_KICK_INTERVAL (60 * HZ)
76
77
78#define LOW_VOLT_REG 0x4E
79
80
81enum ab8500_charger_link_status {
82 USB_STAT_NOT_CONFIGURED,
83 USB_STAT_STD_HOST_NC,
84 USB_STAT_STD_HOST_C_NS,
85 USB_STAT_STD_HOST_C_S,
86 USB_STAT_HOST_CHG_NM,
87 USB_STAT_HOST_CHG_HS,
88 USB_STAT_HOST_CHG_HS_CHIRP,
89 USB_STAT_DEDICATED_CHG,
90 USB_STAT_ACA_RID_A,
91 USB_STAT_ACA_RID_B,
92 USB_STAT_ACA_RID_C_NM,
93 USB_STAT_ACA_RID_C_HS,
94 USB_STAT_ACA_RID_C_HS_CHIRP,
95 USB_STAT_HM_IDGND,
96 USB_STAT_RESERVED,
97 USB_STAT_NOT_VALID_LINK,
98};
99
100enum ab8500_usb_state {
101 AB8500_BM_USB_STATE_RESET_HS,
102 AB8500_BM_USB_STATE_RESET_FS,
103 AB8500_BM_USB_STATE_CONFIGURED,
104 AB8500_BM_USB_STATE_SUSPEND,
105 AB8500_BM_USB_STATE_RESUME,
106 AB8500_BM_USB_STATE_MAX,
107};
108
109
110#define USB_CH_IP_CUR_LVL_0P05 50
111#define USB_CH_IP_CUR_LVL_0P09 98
112#define USB_CH_IP_CUR_LVL_0P19 193
113#define USB_CH_IP_CUR_LVL_0P29 290
114#define USB_CH_IP_CUR_LVL_0P38 380
115#define USB_CH_IP_CUR_LVL_0P45 450
116#define USB_CH_IP_CUR_LVL_0P5 500
117#define USB_CH_IP_CUR_LVL_0P6 600
118#define USB_CH_IP_CUR_LVL_0P7 700
119#define USB_CH_IP_CUR_LVL_0P8 800
120#define USB_CH_IP_CUR_LVL_0P9 900
121#define USB_CH_IP_CUR_LVL_1P0 1000
122#define USB_CH_IP_CUR_LVL_1P1 1100
123#define USB_CH_IP_CUR_LVL_1P3 1300
124#define USB_CH_IP_CUR_LVL_1P4 1400
125#define USB_CH_IP_CUR_LVL_1P5 1500
126
127#define VBAT_TRESH_IP_CUR_RED 3800
128
129#define to_ab8500_charger_usb_device_info(x) container_of((x), \
130 struct ab8500_charger, usb_chg)
131#define to_ab8500_charger_ac_device_info(x) container_of((x), \
132 struct ab8500_charger, ac_chg)
133
134
135
136
137
138
139struct ab8500_charger_interrupts {
140 char *name;
141 irqreturn_t (*isr)(int irq, void *data);
142};
143
144struct ab8500_charger_info {
145 int charger_connected;
146 int charger_online;
147 int charger_voltage;
148 int cv_active;
149 bool wd_expired;
150};
151
152struct ab8500_charger_event_flags {
153 bool mainextchnotok;
154 bool main_thermal_prot;
155 bool usb_thermal_prot;
156 bool vbus_ovv;
157 bool usbchargernotok;
158 bool chgwdexp;
159 bool vbus_collapse;
160};
161
162struct ab8500_charger_usb_state {
163 bool usb_changed;
164 int usb_current;
165 enum ab8500_usb_state state;
166 spinlock_t usb_lock;
167};
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210struct ab8500_charger {
211 struct device *dev;
212 int max_usb_in_curr;
213 bool vbus_detected;
214 bool vbus_detected_start;
215 bool ac_conn;
216 bool vddadc_en_ac;
217 bool vddadc_en_usb;
218 int vbat;
219 int old_vbat;
220 bool autopower;
221 struct ab8500 *parent;
222 struct ab8500_gpadc *gpadc;
223 struct abx500_charger_platform_data *pdata;
224 struct abx500_bm_data *bat;
225 struct ab8500_charger_event_flags flags;
226 struct ab8500_charger_usb_state usb_state;
227 struct ux500_charger ac_chg;
228 struct ux500_charger usb_chg;
229 struct ab8500_charger_info ac;
230 struct ab8500_charger_info usb;
231 struct regulator *regu;
232 struct workqueue_struct *charger_wq;
233 struct delayed_work check_vbat_work;
234 struct delayed_work check_hw_failure_work;
235 struct delayed_work check_usbchgnotok_work;
236 struct delayed_work kick_wd_work;
237 struct work_struct ac_work;
238 struct work_struct detect_usb_type_work;
239 struct work_struct usb_link_status_work;
240 struct work_struct usb_state_changed_work;
241 struct work_struct check_main_thermal_prot_work;
242 struct work_struct check_usb_thermal_prot_work;
243 struct usb_phy *usb_phy;
244 struct notifier_block nb;
245};
246
247
248static enum power_supply_property ab8500_charger_ac_props[] = {
249 POWER_SUPPLY_PROP_HEALTH,
250 POWER_SUPPLY_PROP_PRESENT,
251 POWER_SUPPLY_PROP_ONLINE,
252 POWER_SUPPLY_PROP_VOLTAGE_NOW,
253 POWER_SUPPLY_PROP_VOLTAGE_AVG,
254 POWER_SUPPLY_PROP_CURRENT_NOW,
255};
256
257
258static enum power_supply_property ab8500_charger_usb_props[] = {
259 POWER_SUPPLY_PROP_HEALTH,
260 POWER_SUPPLY_PROP_CURRENT_AVG,
261 POWER_SUPPLY_PROP_PRESENT,
262 POWER_SUPPLY_PROP_ONLINE,
263 POWER_SUPPLY_PROP_VOLTAGE_NOW,
264 POWER_SUPPLY_PROP_VOLTAGE_AVG,
265 POWER_SUPPLY_PROP_CURRENT_NOW,
266};
267
268
269
270
271
272
273
274static void ab8500_power_loss_handling(struct ab8500_charger *di)
275{
276 u8 reg;
277 int ret;
278
279 dev_dbg(di->dev, "Autopower : %d\n", di->autopower);
280
281
282 ret = abx500_get_register_interruptible(di->dev, 0x15, 0x00, ®);
283 if (ret) {
284 dev_err(di->dev, "%d write failed\n", __LINE__);
285 return;
286 }
287
288
289 ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x2);
290 if (ret) {
291 dev_err(di->dev, "%d write failed\n", __LINE__);
292 return;
293 }
294
295 if (di->autopower)
296 reg |= 0x8;
297 else
298 reg &= ~0x8;
299
300
301 ret = abx500_set_register_interruptible(di->dev, 0x15, 0x00, reg);
302 if (ret) {
303 dev_err(di->dev, "%d write failed\n", __LINE__);
304 return;
305 }
306
307
308 ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x0);
309 if (ret) {
310 dev_err(di->dev, "%d write failed\n", __LINE__);
311 return;
312 }
313}
314
315
316
317
318
319
320
321
322static void ab8500_power_supply_changed(struct ab8500_charger *di,
323 struct power_supply *psy)
324{
325 if (di->pdata->autopower_cfg) {
326 if (!di->usb.charger_connected &&
327 !di->ac.charger_connected &&
328 di->autopower) {
329 di->autopower = false;
330 ab8500_power_loss_handling(di);
331 } else if (!di->autopower &&
332 (di->ac.charger_connected ||
333 di->usb.charger_connected)) {
334 di->autopower = true;
335 ab8500_power_loss_handling(di);
336 }
337 }
338 power_supply_changed(psy);
339}
340
341static void ab8500_charger_set_usb_connected(struct ab8500_charger *di,
342 bool connected)
343{
344 if (connected != di->usb.charger_connected) {
345 dev_dbg(di->dev, "USB connected:%i\n", connected);
346 di->usb.charger_connected = connected;
347 sysfs_notify(&di->usb_chg.psy.dev->kobj, NULL, "present");
348 }
349}
350
351
352
353
354
355
356
357static int ab8500_charger_get_ac_voltage(struct ab8500_charger *di)
358{
359 int vch;
360
361
362 if (di->ac.charger_connected) {
363 vch = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_V);
364 if (vch < 0)
365 dev_err(di->dev, "%s gpadc conv failed,\n", __func__);
366 } else {
367 vch = 0;
368 }
369 return vch;
370}
371
372
373
374
375
376
377
378static int ab8500_charger_ac_cv(struct ab8500_charger *di)
379{
380 u8 val;
381 int ret = 0;
382
383
384 if (di->ac.charger_online) {
385 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
386 AB8500_CH_STATUS1_REG, &val);
387 if (ret < 0) {
388 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
389 return 0;
390 }
391
392 if (val & MAIN_CH_CV_ON)
393 ret = 1;
394 else
395 ret = 0;
396 }
397
398 return ret;
399}
400
401
402
403
404
405
406
407
408static int ab8500_charger_get_vbus_voltage(struct ab8500_charger *di)
409{
410 int vch;
411
412
413 if (di->usb.charger_connected) {
414 vch = ab8500_gpadc_convert(di->gpadc, VBUS_V);
415 if (vch < 0)
416 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
417 } else {
418 vch = 0;
419 }
420 return vch;
421}
422
423
424
425
426
427
428
429
430static int ab8500_charger_get_usb_current(struct ab8500_charger *di)
431{
432 int ich;
433
434
435 if (di->usb.charger_online) {
436 ich = ab8500_gpadc_convert(di->gpadc, USB_CHARGER_C);
437 if (ich < 0)
438 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
439 } else {
440 ich = 0;
441 }
442 return ich;
443}
444
445
446
447
448
449
450
451
452static int ab8500_charger_get_ac_current(struct ab8500_charger *di)
453{
454 int ich;
455
456
457 if (di->ac.charger_online) {
458 ich = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_C);
459 if (ich < 0)
460 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
461 } else {
462 ich = 0;
463 }
464 return ich;
465}
466
467
468
469
470
471
472
473static int ab8500_charger_usb_cv(struct ab8500_charger *di)
474{
475 int ret;
476 u8 val;
477
478
479 if (di->usb.charger_online) {
480 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
481 AB8500_CH_USBCH_STAT1_REG, &val);
482 if (ret < 0) {
483 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
484 return 0;
485 }
486
487 if (val & USB_CH_CV_ON)
488 ret = 1;
489 else
490 ret = 0;
491 } else {
492 ret = 0;
493 }
494
495 return ret;
496}
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514static int ab8500_charger_detect_chargers(struct ab8500_charger *di)
515{
516 int result = NO_PW_CONN;
517 int ret;
518 u8 val;
519
520
521 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
522 AB8500_CH_STATUS1_REG, &val);
523 if (ret < 0) {
524 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
525 return ret;
526 }
527
528 if (val & MAIN_CH_DET)
529 result = AC_PW_CONN;
530
531
532 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
533 AB8500_CH_USBCH_STAT1_REG, &val);
534 if (ret < 0) {
535 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
536 return ret;
537 }
538
539 if ((val & VBUS_DET_DBNC1) && (val & VBUS_DET_DBNC100))
540 result |= USB_PW_CONN;
541
542 return result;
543}
544
545
546
547
548
549
550
551
552
553
554static int ab8500_charger_max_usb_curr(struct ab8500_charger *di,
555 enum ab8500_charger_link_status link_status)
556{
557 int ret = 0;
558
559 switch (link_status) {
560 case USB_STAT_STD_HOST_NC:
561 case USB_STAT_STD_HOST_C_NS:
562 case USB_STAT_STD_HOST_C_S:
563 dev_dbg(di->dev, "USB Type - Standard host is "
564 "detected through USB driver\n");
565 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P09;
566 break;
567 case USB_STAT_HOST_CHG_HS_CHIRP:
568 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5;
569 break;
570 case USB_STAT_HOST_CHG_HS:
571 case USB_STAT_ACA_RID_C_HS:
572 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P9;
573 break;
574 case USB_STAT_ACA_RID_A:
575
576
577
578
579 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P1;
580 break;
581 case USB_STAT_ACA_RID_B:
582
583
584
585
586 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P3;
587 break;
588 case USB_STAT_DEDICATED_CHG:
589 case USB_STAT_HOST_CHG_NM:
590 case USB_STAT_ACA_RID_C_HS_CHIRP:
591 case USB_STAT_ACA_RID_C_NM:
592 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P5;
593 break;
594 case USB_STAT_RESERVED:
595
596
597
598
599
600
601
602
603 di->flags.vbus_collapse = true;
604 dev_dbg(di->dev, "USB Type - USB_STAT_RESERVED "
605 "VBUS has collapsed\n");
606 ret = -1;
607 break;
608 case USB_STAT_HM_IDGND:
609 case USB_STAT_NOT_CONFIGURED:
610 case USB_STAT_NOT_VALID_LINK:
611 dev_err(di->dev, "USB Type - Charging not allowed\n");
612 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05;
613 ret = -ENXIO;
614 break;
615 default:
616 dev_err(di->dev, "USB Type - Unknown\n");
617 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05;
618 ret = -ENXIO;
619 break;
620 };
621
622 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
623 link_status, di->max_usb_in_curr);
624
625 return ret;
626}
627
628
629
630
631
632
633
634
635static int ab8500_charger_read_usb_type(struct ab8500_charger *di)
636{
637 int ret;
638 u8 val;
639
640 ret = abx500_get_register_interruptible(di->dev,
641 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG, &val);
642 if (ret < 0) {
643 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
644 return ret;
645 }
646 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
647 AB8500_USB_LINE_STAT_REG, &val);
648 if (ret < 0) {
649 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
650 return ret;
651 }
652
653
654 val = (val & AB8500_USB_LINK_STATUS) >> 3;
655 ret = ab8500_charger_max_usb_curr(di,
656 (enum ab8500_charger_link_status) val);
657
658 return ret;
659}
660
661
662
663
664
665
666
667
668static int ab8500_charger_detect_usb_type(struct ab8500_charger *di)
669{
670 int i, ret;
671 u8 val;
672
673
674
675
676
677
678 for (i = 0; i < 10; i++) {
679 msleep(250);
680 ret = abx500_get_register_interruptible(di->dev,
681 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG,
682 &val);
683 if (ret < 0) {
684 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
685 return ret;
686 }
687 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
688 AB8500_USB_LINE_STAT_REG, &val);
689 if (ret < 0) {
690 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
691 return ret;
692 }
693
694
695
696
697
698
699
700 val = (val & AB8500_USB_LINK_STATUS) >> 3;
701 if (val)
702 break;
703 }
704 ret = ab8500_charger_max_usb_curr(di,
705 (enum ab8500_charger_link_status) val);
706
707 return ret;
708}
709
710
711
712
713
714static int ab8500_charger_voltage_map[] = {
715 3500 ,
716 3525 ,
717 3550 ,
718 3575 ,
719 3600 ,
720 3625 ,
721 3650 ,
722 3675 ,
723 3700 ,
724 3725 ,
725 3750 ,
726 3775 ,
727 3800 ,
728 3825 ,
729 3850 ,
730 3875 ,
731 3900 ,
732 3925 ,
733 3950 ,
734 3975 ,
735 4000 ,
736 4025 ,
737 4050 ,
738 4060 ,
739 4070 ,
740 4080 ,
741 4090 ,
742 4100 ,
743 4110 ,
744 4120 ,
745 4130 ,
746 4140 ,
747 4150 ,
748 4160 ,
749 4170 ,
750 4180 ,
751 4190 ,
752 4200 ,
753 4210 ,
754 4220 ,
755 4230 ,
756 4240 ,
757 4250 ,
758 4260 ,
759 4270 ,
760 4280 ,
761 4290 ,
762 4300 ,
763 4310 ,
764 4320 ,
765 4330 ,
766 4340 ,
767 4350 ,
768 4360 ,
769 4370 ,
770 4380 ,
771 4390 ,
772 4400 ,
773 4410 ,
774 4420 ,
775 4430 ,
776 4440 ,
777 4450 ,
778 4460 ,
779 4470 ,
780 4480 ,
781 4490 ,
782 4500 ,
783 4510 ,
784 4520 ,
785 4530 ,
786 4540 ,
787 4550 ,
788 4560 ,
789 4570 ,
790 4580 ,
791 4590 ,
792 4600 ,
793};
794
795
796
797
798
799static int ab8500_charger_current_map[] = {
800 100 ,
801 200 ,
802 300 ,
803 400 ,
804 500 ,
805 600 ,
806 700 ,
807 800 ,
808 900 ,
809 1000 ,
810 1100 ,
811 1200 ,
812 1300 ,
813 1400 ,
814 1500 ,
815};
816
817
818
819
820
821static int ab8500_charger_vbus_in_curr_map[] = {
822 USB_CH_IP_CUR_LVL_0P05,
823 USB_CH_IP_CUR_LVL_0P09,
824 USB_CH_IP_CUR_LVL_0P19,
825 USB_CH_IP_CUR_LVL_0P29,
826 USB_CH_IP_CUR_LVL_0P38,
827 USB_CH_IP_CUR_LVL_0P45,
828 USB_CH_IP_CUR_LVL_0P5,
829 USB_CH_IP_CUR_LVL_0P6,
830 USB_CH_IP_CUR_LVL_0P7,
831 USB_CH_IP_CUR_LVL_0P8,
832 USB_CH_IP_CUR_LVL_0P9,
833 USB_CH_IP_CUR_LVL_1P0,
834 USB_CH_IP_CUR_LVL_1P1,
835 USB_CH_IP_CUR_LVL_1P3,
836 USB_CH_IP_CUR_LVL_1P4,
837 USB_CH_IP_CUR_LVL_1P5,
838};
839
840static int ab8500_voltage_to_regval(int voltage)
841{
842 int i;
843
844
845 if (voltage < ab8500_charger_voltage_map[0])
846 return LOW_VOLT_REG;
847
848 for (i = 1; i < ARRAY_SIZE(ab8500_charger_voltage_map); i++) {
849 if (voltage < ab8500_charger_voltage_map[i])
850 return i - 1;
851 }
852
853
854 i = ARRAY_SIZE(ab8500_charger_voltage_map) - 1;
855 if (voltage == ab8500_charger_voltage_map[i])
856 return i;
857 else
858 return -1;
859}
860
861static int ab8500_current_to_regval(int curr)
862{
863 int i;
864
865 if (curr < ab8500_charger_current_map[0])
866 return 0;
867
868 for (i = 0; i < ARRAY_SIZE(ab8500_charger_current_map); i++) {
869 if (curr < ab8500_charger_current_map[i])
870 return i - 1;
871 }
872
873
874 i = ARRAY_SIZE(ab8500_charger_current_map) - 1;
875 if (curr == ab8500_charger_current_map[i])
876 return i;
877 else
878 return -1;
879}
880
881static int ab8500_vbus_in_curr_to_regval(int curr)
882{
883 int i;
884
885 if (curr < ab8500_charger_vbus_in_curr_map[0])
886 return 0;
887
888 for (i = 0; i < ARRAY_SIZE(ab8500_charger_vbus_in_curr_map); i++) {
889 if (curr < ab8500_charger_vbus_in_curr_map[i])
890 return i - 1;
891 }
892
893
894 i = ARRAY_SIZE(ab8500_charger_vbus_in_curr_map) - 1;
895 if (curr == ab8500_charger_vbus_in_curr_map[i])
896 return i;
897 else
898 return -1;
899}
900
901
902
903
904
905
906
907
908
909
910static int ab8500_charger_get_usb_cur(struct ab8500_charger *di)
911{
912 switch (di->usb_state.usb_current) {
913 case 100:
914 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P09;
915 break;
916 case 200:
917 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P19;
918 break;
919 case 300:
920 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P29;
921 break;
922 case 400:
923 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P38;
924 break;
925 case 500:
926 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5;
927 break;
928 default:
929 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05;
930 return -1;
931 break;
932 };
933 return 0;
934}
935
936
937
938
939
940
941
942
943
944static int ab8500_charger_set_vbus_in_curr(struct ab8500_charger *di,
945 int ich_in)
946{
947 int ret;
948 int input_curr_index;
949 int min_value;
950
951
952 min_value = min(di->bat->chg_params->usb_curr_max, ich_in);
953
954 switch (min_value) {
955 case 100:
956 if (di->vbat < VBAT_TRESH_IP_CUR_RED)
957 min_value = USB_CH_IP_CUR_LVL_0P05;
958 break;
959 case 500:
960 if (di->vbat < VBAT_TRESH_IP_CUR_RED)
961 min_value = USB_CH_IP_CUR_LVL_0P45;
962 break;
963 default:
964 break;
965 }
966
967 input_curr_index = ab8500_vbus_in_curr_to_regval(min_value);
968 if (input_curr_index < 0) {
969 dev_err(di->dev, "VBUS input current limit too high\n");
970 return -ENXIO;
971 }
972
973 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
974 AB8500_USBCH_IPT_CRNTLVL_REG,
975 input_curr_index << VBUS_IN_CURR_LIM_SHIFT);
976 if (ret)
977 dev_err(di->dev, "%s write failed\n", __func__);
978
979 return ret;
980}
981
982
983
984
985
986
987
988
989
990static int ab8500_charger_led_en(struct ab8500_charger *di, int on)
991{
992 int ret;
993
994 if (on) {
995
996 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
997 AB8500_LED_INDICATOR_PWM_CTRL,
998 (LED_IND_CUR_5MA | LED_INDICATOR_PWM_ENA));
999 if (ret) {
1000 dev_err(di->dev, "Power ON LED failed\n");
1001 return ret;
1002 }
1003
1004 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1005 AB8500_LED_INDICATOR_PWM_DUTY,
1006 LED_INDICATOR_PWM_DUTY_252_256);
1007 if (ret) {
1008 dev_err(di->dev, "Set LED PWM duty cycle failed\n");
1009 return ret;
1010 }
1011 } else {
1012
1013 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1014 AB8500_LED_INDICATOR_PWM_CTRL,
1015 LED_INDICATOR_PWM_DIS);
1016 if (ret) {
1017 dev_err(di->dev, "Power-off LED failed\n");
1018 return ret;
1019 }
1020 }
1021
1022 return ret;
1023}
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035static int ab8500_charger_ac_en(struct ux500_charger *charger,
1036 int enable, int vset, int iset)
1037{
1038 int ret;
1039 int volt_index;
1040 int curr_index;
1041 int input_curr_index;
1042 u8 overshoot = 0;
1043
1044 struct ab8500_charger *di = to_ab8500_charger_ac_device_info(charger);
1045
1046 if (enable) {
1047
1048 if (!di->ac.charger_connected) {
1049 dev_err(di->dev, "AC charger not connected\n");
1050 return -ENXIO;
1051 }
1052
1053
1054 dev_dbg(di->dev, "Enable AC: %dmV %dmA\n", vset, iset);
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066 if (!di->vddadc_en_ac) {
1067 regulator_enable(di->regu);
1068 di->vddadc_en_ac = true;
1069 }
1070
1071
1072 volt_index = ab8500_voltage_to_regval(vset);
1073 curr_index = ab8500_current_to_regval(iset);
1074 input_curr_index = ab8500_current_to_regval(
1075 di->bat->chg_params->ac_curr_max);
1076 if (volt_index < 0 || curr_index < 0 || input_curr_index < 0) {
1077 dev_err(di->dev,
1078 "Charger voltage or current too high, "
1079 "charging not started\n");
1080 return -ENXIO;
1081 }
1082
1083
1084 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1085 AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1086 if (ret) {
1087 dev_err(di->dev, "%s write failed\n", __func__);
1088 return ret;
1089 }
1090
1091 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1092 AB8500_MCH_IPT_CURLVL_REG,
1093 input_curr_index << MAIN_CH_INPUT_CURR_SHIFT);
1094 if (ret) {
1095 dev_err(di->dev, "%s write failed\n", __func__);
1096 return ret;
1097 }
1098
1099 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1100 AB8500_CH_OPT_CRNTLVL_REG, (u8) curr_index);
1101 if (ret) {
1102 dev_err(di->dev, "%s write failed\n", __func__);
1103 return ret;
1104 }
1105
1106
1107 if (!di->bat->enable_overshoot)
1108 overshoot = MAIN_CH_NO_OVERSHOOT_ENA_N;
1109
1110
1111 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1112 AB8500_MCH_CTRL1, MAIN_CH_ENA | overshoot);
1113 if (ret) {
1114 dev_err(di->dev, "%s write failed\n", __func__);
1115 return ret;
1116 }
1117
1118
1119 ret = ab8500_charger_led_en(di, true);
1120 if (ret < 0)
1121 dev_err(di->dev, "failed to enable LED\n");
1122
1123 di->ac.charger_online = 1;
1124 } else {
1125
1126 if (is_ab8500_1p1_or_earlier(di->parent)) {
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137 if (di->ac_conn) {
1138 queue_delayed_work(di->charger_wq,
1139 &di->kick_wd_work,
1140 round_jiffies(WD_KICK_INTERVAL));
1141 }
1142
1143
1144
1145
1146
1147
1148
1149
1150 ret = abx500_set_register_interruptible(di->dev,
1151 AB8500_CHARGER,
1152 AB8500_CH_VOLT_LVL_REG, CH_VOL_LVL_3P5);
1153 if (ret) {
1154 dev_err(di->dev,
1155 "%s write failed\n", __func__);
1156 return ret;
1157 }
1158
1159 ret = abx500_set_register_interruptible(di->dev,
1160 AB8500_CHARGER,
1161 AB8500_CH_OPT_CRNTLVL_REG, CH_OP_CUR_LVL_0P1);
1162 if (ret) {
1163 dev_err(di->dev,
1164 "%s write failed\n", __func__);
1165 return ret;
1166 }
1167 } else {
1168 ret = abx500_set_register_interruptible(di->dev,
1169 AB8500_CHARGER,
1170 AB8500_MCH_CTRL1, 0);
1171 if (ret) {
1172 dev_err(di->dev,
1173 "%s write failed\n", __func__);
1174 return ret;
1175 }
1176 }
1177
1178 ret = ab8500_charger_led_en(di, false);
1179 if (ret < 0)
1180 dev_err(di->dev, "failed to disable LED\n");
1181
1182 di->ac.charger_online = 0;
1183 di->ac.wd_expired = false;
1184
1185
1186 if (di->vddadc_en_ac) {
1187 regulator_disable(di->regu);
1188 di->vddadc_en_ac = false;
1189 }
1190
1191 dev_dbg(di->dev, "%s Disabled AC charging\n", __func__);
1192 }
1193 ab8500_power_supply_changed(di, &di->ac_chg.psy);
1194
1195 return ret;
1196}
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208static int ab8500_charger_usb_en(struct ux500_charger *charger,
1209 int enable, int vset, int ich_out)
1210{
1211 int ret;
1212 int volt_index;
1213 int curr_index;
1214 u8 overshoot = 0;
1215
1216 struct ab8500_charger *di = to_ab8500_charger_usb_device_info(charger);
1217
1218 if (enable) {
1219
1220 if (!di->usb.charger_connected) {
1221 dev_err(di->dev, "USB charger not connected\n");
1222 return -ENXIO;
1223 }
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235 if (!di->vddadc_en_usb) {
1236 regulator_enable(di->regu);
1237 di->vddadc_en_usb = true;
1238 }
1239
1240
1241 dev_dbg(di->dev, "Enable USB: %dmV %dmA\n", vset, ich_out);
1242
1243
1244 volt_index = ab8500_voltage_to_regval(vset);
1245 curr_index = ab8500_current_to_regval(ich_out);
1246 if (volt_index < 0 || curr_index < 0) {
1247 dev_err(di->dev,
1248 "Charger voltage or current too high, "
1249 "charging not started\n");
1250 return -ENXIO;
1251 }
1252
1253
1254 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1255 AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1256 if (ret) {
1257 dev_err(di->dev, "%s write failed\n", __func__);
1258 return ret;
1259 }
1260
1261 ret = ab8500_charger_set_vbus_in_curr(di, di->max_usb_in_curr);
1262 if (ret) {
1263 dev_err(di->dev, "setting USBChInputCurr failed\n");
1264 return ret;
1265 }
1266
1267 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1268 AB8500_CH_OPT_CRNTLVL_REG, (u8) curr_index);
1269 if (ret) {
1270 dev_err(di->dev, "%s write failed\n", __func__);
1271 return ret;
1272 }
1273
1274 if (!di->bat->enable_overshoot)
1275 overshoot = USB_CHG_NO_OVERSHOOT_ENA_N;
1276
1277
1278 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1279 AB8500_USBCH_CTRL1_REG, USB_CH_ENA | overshoot);
1280 if (ret) {
1281 dev_err(di->dev, "%s write failed\n", __func__);
1282 return ret;
1283 }
1284
1285
1286 ret = ab8500_charger_led_en(di, true);
1287 if (ret < 0)
1288 dev_err(di->dev, "failed to enable LED\n");
1289
1290 queue_delayed_work(di->charger_wq, &di->check_vbat_work, HZ);
1291
1292 di->usb.charger_online = 1;
1293 } else {
1294
1295 ret = abx500_set_register_interruptible(di->dev,
1296 AB8500_CHARGER,
1297 AB8500_USBCH_CTRL1_REG, 0);
1298 if (ret) {
1299 dev_err(di->dev,
1300 "%s write failed\n", __func__);
1301 return ret;
1302 }
1303
1304 ret = ab8500_charger_led_en(di, false);
1305 if (ret < 0)
1306 dev_err(di->dev, "failed to disable LED\n");
1307
1308 di->usb.charger_online = 0;
1309 di->usb.wd_expired = false;
1310
1311
1312 if (di->vddadc_en_usb) {
1313 regulator_disable(di->regu);
1314 di->vddadc_en_usb = false;
1315 }
1316
1317 dev_dbg(di->dev, "%s Disabled USB charging\n", __func__);
1318
1319
1320 if (delayed_work_pending(&di->check_vbat_work))
1321 cancel_delayed_work(&di->check_vbat_work);
1322
1323 }
1324 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1325
1326 return ret;
1327}
1328
1329
1330
1331
1332
1333
1334
1335
1336static int ab8500_charger_watchdog_kick(struct ux500_charger *charger)
1337{
1338 int ret;
1339 struct ab8500_charger *di;
1340
1341 if (charger->psy.type == POWER_SUPPLY_TYPE_MAINS)
1342 di = to_ab8500_charger_ac_device_info(charger);
1343 else if (charger->psy.type == POWER_SUPPLY_TYPE_USB)
1344 di = to_ab8500_charger_usb_device_info(charger);
1345 else
1346 return -ENXIO;
1347
1348 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1349 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
1350 if (ret)
1351 dev_err(di->dev, "Failed to kick WD!\n");
1352
1353 return ret;
1354}
1355
1356
1357
1358
1359
1360
1361
1362
1363static int ab8500_charger_update_charger_current(struct ux500_charger *charger,
1364 int ich_out)
1365{
1366 int ret;
1367 int curr_index;
1368 struct ab8500_charger *di;
1369
1370 if (charger->psy.type == POWER_SUPPLY_TYPE_MAINS)
1371 di = to_ab8500_charger_ac_device_info(charger);
1372 else if (charger->psy.type == POWER_SUPPLY_TYPE_USB)
1373 di = to_ab8500_charger_usb_device_info(charger);
1374 else
1375 return -ENXIO;
1376
1377 curr_index = ab8500_current_to_regval(ich_out);
1378 if (curr_index < 0) {
1379 dev_err(di->dev,
1380 "Charger current too high, "
1381 "charging not started\n");
1382 return -ENXIO;
1383 }
1384
1385 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1386 AB8500_CH_OPT_CRNTLVL_REG, (u8) curr_index);
1387 if (ret) {
1388 dev_err(di->dev, "%s write failed\n", __func__);
1389 return ret;
1390 }
1391
1392
1393 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1394 AB8500_CHARGER_CTRL,
1395 0x1);
1396 if (ret) {
1397 dev_err(di->dev, "%s write failed\n", __func__);
1398 return ret;
1399 }
1400
1401 return ret;
1402}
1403
1404static int ab8500_charger_get_ext_psy_data(struct device *dev, void *data)
1405{
1406 struct power_supply *psy;
1407 struct power_supply *ext;
1408 struct ab8500_charger *di;
1409 union power_supply_propval ret;
1410 int i, j;
1411 bool psy_found = false;
1412 struct ux500_charger *usb_chg;
1413
1414 usb_chg = (struct ux500_charger *)data;
1415 psy = &usb_chg->psy;
1416
1417 di = to_ab8500_charger_usb_device_info(usb_chg);
1418
1419 ext = dev_get_drvdata(dev);
1420
1421
1422 for (i = 0; i < ext->num_supplicants; i++) {
1423 if (!strcmp(ext->supplied_to[i], psy->name))
1424 psy_found = true;
1425 }
1426
1427 if (!psy_found)
1428 return 0;
1429
1430
1431 for (j = 0; j < ext->num_properties; j++) {
1432 enum power_supply_property prop;
1433 prop = ext->properties[j];
1434
1435 if (ext->get_property(ext, prop, &ret))
1436 continue;
1437
1438 switch (prop) {
1439 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1440 switch (ext->type) {
1441 case POWER_SUPPLY_TYPE_BATTERY:
1442 di->vbat = ret.intval / 1000;
1443 break;
1444 default:
1445 break;
1446 }
1447 break;
1448 default:
1449 break;
1450 }
1451 }
1452 return 0;
1453}
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464static void ab8500_charger_check_vbat_work(struct work_struct *work)
1465{
1466 int t = 10;
1467 struct ab8500_charger *di = container_of(work,
1468 struct ab8500_charger, check_vbat_work.work);
1469
1470 class_for_each_device(power_supply_class, NULL,
1471 &di->usb_chg.psy, ab8500_charger_get_ext_psy_data);
1472
1473
1474 if (di->old_vbat == 0)
1475 di->old_vbat = di->vbat;
1476
1477 if (!((di->old_vbat <= VBAT_TRESH_IP_CUR_RED &&
1478 di->vbat <= VBAT_TRESH_IP_CUR_RED) ||
1479 (di->old_vbat > VBAT_TRESH_IP_CUR_RED &&
1480 di->vbat > VBAT_TRESH_IP_CUR_RED))) {
1481
1482 dev_dbg(di->dev, "Vbat did cross threshold, curr: %d, new: %d,"
1483 " old: %d\n", di->max_usb_in_curr, di->vbat,
1484 di->old_vbat);
1485 ab8500_charger_set_vbus_in_curr(di, di->max_usb_in_curr);
1486 power_supply_changed(&di->usb_chg.psy);
1487 }
1488
1489 di->old_vbat = di->vbat;
1490
1491
1492
1493
1494
1495 if (di->vbat < (VBAT_TRESH_IP_CUR_RED + 100) &&
1496 (di->vbat > (VBAT_TRESH_IP_CUR_RED - 100)))
1497 t = 1;
1498
1499 queue_delayed_work(di->charger_wq, &di->check_vbat_work, t * HZ);
1500}
1501
1502
1503
1504
1505
1506
1507
1508static void ab8500_charger_check_hw_failure_work(struct work_struct *work)
1509{
1510 int ret;
1511 u8 reg_value;
1512
1513 struct ab8500_charger *di = container_of(work,
1514 struct ab8500_charger, check_hw_failure_work.work);
1515
1516
1517 if (di->flags.mainextchnotok) {
1518 ret = abx500_get_register_interruptible(di->dev,
1519 AB8500_CHARGER, AB8500_CH_STATUS2_REG, ®_value);
1520 if (ret < 0) {
1521 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1522 return;
1523 }
1524 if (!(reg_value & MAIN_CH_NOK)) {
1525 di->flags.mainextchnotok = false;
1526 ab8500_power_supply_changed(di, &di->ac_chg.psy);
1527 }
1528 }
1529 if (di->flags.vbus_ovv) {
1530 ret = abx500_get_register_interruptible(di->dev,
1531 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG,
1532 ®_value);
1533 if (ret < 0) {
1534 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1535 return;
1536 }
1537 if (!(reg_value & VBUS_OVV_TH)) {
1538 di->flags.vbus_ovv = false;
1539 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1540 }
1541 }
1542
1543 if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
1544 queue_delayed_work(di->charger_wq,
1545 &di->check_hw_failure_work, round_jiffies(HZ));
1546 }
1547}
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563static void ab8500_charger_kick_watchdog_work(struct work_struct *work)
1564{
1565 int ret;
1566
1567 struct ab8500_charger *di = container_of(work,
1568 struct ab8500_charger, kick_wd_work.work);
1569
1570 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1571 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
1572 if (ret)
1573 dev_err(di->dev, "Failed to kick WD!\n");
1574
1575
1576 queue_delayed_work(di->charger_wq,
1577 &di->kick_wd_work, round_jiffies(WD_KICK_INTERVAL));
1578}
1579
1580
1581
1582
1583
1584
1585
1586static void ab8500_charger_ac_work(struct work_struct *work)
1587{
1588 int ret;
1589
1590 struct ab8500_charger *di = container_of(work,
1591 struct ab8500_charger, ac_work);
1592
1593
1594
1595
1596
1597
1598 ret = ab8500_charger_detect_chargers(di);
1599 if (ret < 0)
1600 return;
1601
1602 if (ret & AC_PW_CONN) {
1603 di->ac.charger_connected = 1;
1604 di->ac_conn = true;
1605 } else {
1606 di->ac.charger_connected = 0;
1607 }
1608
1609 ab8500_power_supply_changed(di, &di->ac_chg.psy);
1610 sysfs_notify(&di->ac_chg.psy.dev->kobj, NULL, "present");
1611}
1612
1613
1614
1615
1616
1617
1618
1619static void ab8500_charger_detect_usb_type_work(struct work_struct *work)
1620{
1621 int ret;
1622
1623 struct ab8500_charger *di = container_of(work,
1624 struct ab8500_charger, detect_usb_type_work);
1625
1626
1627
1628
1629
1630
1631 ret = ab8500_charger_detect_chargers(di);
1632 if (ret < 0)
1633 return;
1634
1635 if (!(ret & USB_PW_CONN)) {
1636 di->vbus_detected = 0;
1637 ab8500_charger_set_usb_connected(di, false);
1638 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1639 } else {
1640 di->vbus_detected = 1;
1641
1642 if (is_ab8500_1p1_or_earlier(di->parent)) {
1643 ret = ab8500_charger_detect_usb_type(di);
1644 if (!ret) {
1645 ab8500_charger_set_usb_connected(di, true);
1646 ab8500_power_supply_changed(di,
1647 &di->usb_chg.psy);
1648 }
1649 } else {
1650
1651
1652
1653
1654
1655
1656 if (di->vbus_detected_start) {
1657 di->vbus_detected_start = false;
1658 ret = ab8500_charger_detect_usb_type(di);
1659 if (!ret) {
1660 ab8500_charger_set_usb_connected(di,
1661 true);
1662 ab8500_power_supply_changed(di,
1663 &di->usb_chg.psy);
1664 }
1665 }
1666 }
1667 }
1668}
1669
1670
1671
1672
1673
1674
1675
1676static void ab8500_charger_usb_link_status_work(struct work_struct *work)
1677{
1678 int ret;
1679
1680 struct ab8500_charger *di = container_of(work,
1681 struct ab8500_charger, usb_link_status_work);
1682
1683
1684
1685
1686
1687
1688 ret = ab8500_charger_detect_chargers(di);
1689 if (ret < 0)
1690 return;
1691
1692 if (!(ret & USB_PW_CONN)) {
1693 di->vbus_detected = 0;
1694 ab8500_charger_set_usb_connected(di, false);
1695 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1696 } else {
1697 di->vbus_detected = 1;
1698 ret = ab8500_charger_read_usb_type(di);
1699 if (!ret) {
1700
1701 ret = ab8500_charger_set_vbus_in_curr(di,
1702 di->max_usb_in_curr);
1703 if (ret)
1704 return;
1705
1706 ab8500_charger_set_usb_connected(di, true);
1707 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1708 } else if (ret == -ENXIO) {
1709
1710 ab8500_charger_set_usb_connected(di, false);
1711 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1712 }
1713 }
1714}
1715
1716static void ab8500_charger_usb_state_changed_work(struct work_struct *work)
1717{
1718 int ret;
1719 unsigned long flags;
1720
1721 struct ab8500_charger *di = container_of(work,
1722 struct ab8500_charger, usb_state_changed_work);
1723
1724 if (!di->vbus_detected)
1725 return;
1726
1727 spin_lock_irqsave(&di->usb_state.usb_lock, flags);
1728 di->usb_state.usb_changed = false;
1729 spin_unlock_irqrestore(&di->usb_state.usb_lock, flags);
1730
1731
1732
1733
1734
1735 msleep(250);
1736
1737 if (di->usb_state.usb_changed)
1738 return;
1739
1740 dev_dbg(di->dev, "%s USB state: 0x%02x mA: %d\n",
1741 __func__, di->usb_state.state, di->usb_state.usb_current);
1742
1743 switch (di->usb_state.state) {
1744 case AB8500_BM_USB_STATE_RESET_HS:
1745 case AB8500_BM_USB_STATE_RESET_FS:
1746 case AB8500_BM_USB_STATE_SUSPEND:
1747 case AB8500_BM_USB_STATE_MAX:
1748 ab8500_charger_set_usb_connected(di, false);
1749 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1750 break;
1751
1752 case AB8500_BM_USB_STATE_RESUME:
1753
1754
1755
1756
1757 msleep(1000);
1758
1759 case AB8500_BM_USB_STATE_CONFIGURED:
1760
1761
1762
1763
1764 if (!ab8500_charger_get_usb_cur(di)) {
1765
1766 ret = ab8500_charger_set_vbus_in_curr(di,
1767 di->max_usb_in_curr);
1768 if (ret)
1769 return;
1770
1771 ab8500_charger_set_usb_connected(di, true);
1772 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1773 }
1774 break;
1775
1776 default:
1777 break;
1778 };
1779}
1780
1781
1782
1783
1784
1785
1786
1787static void ab8500_charger_check_usbchargernotok_work(struct work_struct *work)
1788{
1789 int ret;
1790 u8 reg_value;
1791 bool prev_status;
1792
1793 struct ab8500_charger *di = container_of(work,
1794 struct ab8500_charger, check_usbchgnotok_work.work);
1795
1796
1797 ret = abx500_get_register_interruptible(di->dev,
1798 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, ®_value);
1799 if (ret < 0) {
1800 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1801 return;
1802 }
1803 prev_status = di->flags.usbchargernotok;
1804
1805 if (reg_value & VBUS_CH_NOK) {
1806 di->flags.usbchargernotok = true;
1807
1808 queue_delayed_work(di->charger_wq,
1809 &di->check_usbchgnotok_work, HZ);
1810 } else {
1811 di->flags.usbchargernotok = false;
1812 di->flags.vbus_collapse = false;
1813 }
1814
1815 if (prev_status != di->flags.usbchargernotok)
1816 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1817}
1818
1819
1820
1821
1822
1823
1824
1825static void ab8500_charger_check_main_thermal_prot_work(
1826 struct work_struct *work)
1827{
1828 int ret;
1829 u8 reg_value;
1830
1831 struct ab8500_charger *di = container_of(work,
1832 struct ab8500_charger, check_main_thermal_prot_work);
1833
1834
1835 ret = abx500_get_register_interruptible(di->dev,
1836 AB8500_CHARGER, AB8500_CH_STATUS2_REG, ®_value);
1837 if (ret < 0) {
1838 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1839 return;
1840 }
1841 if (reg_value & MAIN_CH_TH_PROT)
1842 di->flags.main_thermal_prot = true;
1843 else
1844 di->flags.main_thermal_prot = false;
1845
1846 ab8500_power_supply_changed(di, &di->ac_chg.psy);
1847}
1848
1849
1850
1851
1852
1853
1854
1855static void ab8500_charger_check_usb_thermal_prot_work(
1856 struct work_struct *work)
1857{
1858 int ret;
1859 u8 reg_value;
1860
1861 struct ab8500_charger *di = container_of(work,
1862 struct ab8500_charger, check_usb_thermal_prot_work);
1863
1864
1865 ret = abx500_get_register_interruptible(di->dev,
1866 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, ®_value);
1867 if (ret < 0) {
1868 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1869 return;
1870 }
1871 if (reg_value & USB_CH_TH_PROT)
1872 di->flags.usb_thermal_prot = true;
1873 else
1874 di->flags.usb_thermal_prot = false;
1875
1876 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1877}
1878
1879
1880
1881
1882
1883
1884
1885
1886static irqreturn_t ab8500_charger_mainchunplugdet_handler(int irq, void *_di)
1887{
1888 struct ab8500_charger *di = _di;
1889
1890 dev_dbg(di->dev, "Main charger unplugged\n");
1891 queue_work(di->charger_wq, &di->ac_work);
1892
1893 return IRQ_HANDLED;
1894}
1895
1896
1897
1898
1899
1900
1901
1902
1903static irqreturn_t ab8500_charger_mainchplugdet_handler(int irq, void *_di)
1904{
1905 struct ab8500_charger *di = _di;
1906
1907 dev_dbg(di->dev, "Main charger plugged\n");
1908 queue_work(di->charger_wq, &di->ac_work);
1909
1910 return IRQ_HANDLED;
1911}
1912
1913
1914
1915
1916
1917
1918
1919
1920static irqreturn_t ab8500_charger_mainextchnotok_handler(int irq, void *_di)
1921{
1922 struct ab8500_charger *di = _di;
1923
1924 dev_dbg(di->dev, "Main charger not ok\n");
1925 di->flags.mainextchnotok = true;
1926 ab8500_power_supply_changed(di, &di->ac_chg.psy);
1927
1928
1929 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
1930
1931 return IRQ_HANDLED;
1932}
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942static irqreturn_t ab8500_charger_mainchthprotr_handler(int irq, void *_di)
1943{
1944 struct ab8500_charger *di = _di;
1945
1946 dev_dbg(di->dev,
1947 "Die temp above Main charger thermal protection threshold\n");
1948 queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
1949
1950 return IRQ_HANDLED;
1951}
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961static irqreturn_t ab8500_charger_mainchthprotf_handler(int irq, void *_di)
1962{
1963 struct ab8500_charger *di = _di;
1964
1965 dev_dbg(di->dev,
1966 "Die temp ok for Main charger thermal protection threshold\n");
1967 queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
1968
1969 return IRQ_HANDLED;
1970}
1971
1972
1973
1974
1975
1976
1977
1978
1979static irqreturn_t ab8500_charger_vbusdetf_handler(int irq, void *_di)
1980{
1981 struct ab8500_charger *di = _di;
1982
1983 dev_dbg(di->dev, "VBUS falling detected\n");
1984 queue_work(di->charger_wq, &di->detect_usb_type_work);
1985
1986 return IRQ_HANDLED;
1987}
1988
1989
1990
1991
1992
1993
1994
1995
1996static irqreturn_t ab8500_charger_vbusdetr_handler(int irq, void *_di)
1997{
1998 struct ab8500_charger *di = _di;
1999
2000 di->vbus_detected = true;
2001 dev_dbg(di->dev, "VBUS rising detected\n");
2002 queue_work(di->charger_wq, &di->detect_usb_type_work);
2003
2004 return IRQ_HANDLED;
2005}
2006
2007
2008
2009
2010
2011
2012
2013
2014static irqreturn_t ab8500_charger_usblinkstatus_handler(int irq, void *_di)
2015{
2016 struct ab8500_charger *di = _di;
2017
2018 dev_dbg(di->dev, "USB link status changed\n");
2019
2020 queue_work(di->charger_wq, &di->usb_link_status_work);
2021
2022 return IRQ_HANDLED;
2023}
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033static irqreturn_t ab8500_charger_usbchthprotr_handler(int irq, void *_di)
2034{
2035 struct ab8500_charger *di = _di;
2036
2037 dev_dbg(di->dev,
2038 "Die temp above USB charger thermal protection threshold\n");
2039 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2040
2041 return IRQ_HANDLED;
2042}
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052static irqreturn_t ab8500_charger_usbchthprotf_handler(int irq, void *_di)
2053{
2054 struct ab8500_charger *di = _di;
2055
2056 dev_dbg(di->dev,
2057 "Die temp ok for USB charger thermal protection threshold\n");
2058 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2059
2060 return IRQ_HANDLED;
2061}
2062
2063
2064
2065
2066
2067
2068
2069
2070static irqreturn_t ab8500_charger_usbchargernotokr_handler(int irq, void *_di)
2071{
2072 struct ab8500_charger *di = _di;
2073
2074 dev_dbg(di->dev, "Not allowed USB charger detected\n");
2075 queue_delayed_work(di->charger_wq, &di->check_usbchgnotok_work, 0);
2076
2077 return IRQ_HANDLED;
2078}
2079
2080
2081
2082
2083
2084
2085
2086
2087static irqreturn_t ab8500_charger_chwdexp_handler(int irq, void *_di)
2088{
2089 struct ab8500_charger *di = _di;
2090
2091 dev_dbg(di->dev, "Charger watchdog expired\n");
2092
2093
2094
2095
2096
2097 if (di->ac.charger_online) {
2098 di->ac.wd_expired = true;
2099 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2100 }
2101 if (di->usb.charger_online) {
2102 di->usb.wd_expired = true;
2103 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2104 }
2105
2106 return IRQ_HANDLED;
2107}
2108
2109
2110
2111
2112
2113
2114
2115
2116static irqreturn_t ab8500_charger_vbusovv_handler(int irq, void *_di)
2117{
2118 struct ab8500_charger *di = _di;
2119
2120 dev_dbg(di->dev, "VBUS overvoltage detected\n");
2121 di->flags.vbus_ovv = true;
2122 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2123
2124
2125 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
2126
2127 return IRQ_HANDLED;
2128}
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144static int ab8500_charger_ac_get_property(struct power_supply *psy,
2145 enum power_supply_property psp,
2146 union power_supply_propval *val)
2147{
2148 struct ab8500_charger *di;
2149
2150 di = to_ab8500_charger_ac_device_info(psy_to_ux500_charger(psy));
2151
2152 switch (psp) {
2153 case POWER_SUPPLY_PROP_HEALTH:
2154 if (di->flags.mainextchnotok)
2155 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
2156 else if (di->ac.wd_expired || di->usb.wd_expired)
2157 val->intval = POWER_SUPPLY_HEALTH_DEAD;
2158 else if (di->flags.main_thermal_prot)
2159 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
2160 else
2161 val->intval = POWER_SUPPLY_HEALTH_GOOD;
2162 break;
2163 case POWER_SUPPLY_PROP_ONLINE:
2164 val->intval = di->ac.charger_online;
2165 break;
2166 case POWER_SUPPLY_PROP_PRESENT:
2167 val->intval = di->ac.charger_connected;
2168 break;
2169 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
2170 di->ac.charger_voltage = ab8500_charger_get_ac_voltage(di);
2171 val->intval = di->ac.charger_voltage * 1000;
2172 break;
2173 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
2174
2175
2176
2177
2178 di->ac.cv_active = ab8500_charger_ac_cv(di);
2179 val->intval = di->ac.cv_active;
2180 break;
2181 case POWER_SUPPLY_PROP_CURRENT_NOW:
2182 val->intval = ab8500_charger_get_ac_current(di) * 1000;
2183 break;
2184 default:
2185 return -EINVAL;
2186 }
2187 return 0;
2188}
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204static int ab8500_charger_usb_get_property(struct power_supply *psy,
2205 enum power_supply_property psp,
2206 union power_supply_propval *val)
2207{
2208 struct ab8500_charger *di;
2209
2210 di = to_ab8500_charger_usb_device_info(psy_to_ux500_charger(psy));
2211
2212 switch (psp) {
2213 case POWER_SUPPLY_PROP_HEALTH:
2214 if (di->flags.usbchargernotok)
2215 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
2216 else if (di->ac.wd_expired || di->usb.wd_expired)
2217 val->intval = POWER_SUPPLY_HEALTH_DEAD;
2218 else if (di->flags.usb_thermal_prot)
2219 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
2220 else if (di->flags.vbus_ovv)
2221 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
2222 else
2223 val->intval = POWER_SUPPLY_HEALTH_GOOD;
2224 break;
2225 case POWER_SUPPLY_PROP_ONLINE:
2226 val->intval = di->usb.charger_online;
2227 break;
2228 case POWER_SUPPLY_PROP_PRESENT:
2229 val->intval = di->usb.charger_connected;
2230 break;
2231 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
2232 di->usb.charger_voltage = ab8500_charger_get_vbus_voltage(di);
2233 val->intval = di->usb.charger_voltage * 1000;
2234 break;
2235 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
2236
2237
2238
2239
2240 di->usb.cv_active = ab8500_charger_usb_cv(di);
2241 val->intval = di->usb.cv_active;
2242 break;
2243 case POWER_SUPPLY_PROP_CURRENT_NOW:
2244 val->intval = ab8500_charger_get_usb_current(di) * 1000;
2245 break;
2246 case POWER_SUPPLY_PROP_CURRENT_AVG:
2247
2248
2249
2250
2251 if (di->flags.vbus_collapse)
2252 val->intval = 1;
2253 else
2254 val->intval = 0;
2255 break;
2256 default:
2257 return -EINVAL;
2258 }
2259 return 0;
2260}
2261
2262
2263
2264
2265
2266
2267
2268
2269static int ab8500_charger_init_hw_registers(struct ab8500_charger *di)
2270{
2271 int ret = 0;
2272
2273
2274 if (!is_ab8500_1p1_or_earlier(di->parent)) {
2275 ret = abx500_set_register_interruptible(di->dev,
2276 AB8500_CHARGER,
2277 AB8500_CH_VOLT_LVL_MAX_REG, CH_VOL_LVL_4P6);
2278 if (ret) {
2279 dev_err(di->dev,
2280 "failed to set CH_VOLT_LVL_MAX_REG\n");
2281 goto out;
2282 }
2283
2284 ret = abx500_set_register_interruptible(di->dev,
2285 AB8500_CHARGER,
2286 AB8500_CH_OPT_CRNTLVL_MAX_REG, CH_OP_CUR_LVL_1P6);
2287 if (ret) {
2288 dev_err(di->dev,
2289 "failed to set CH_OPT_CRNTLVL_MAX_REG\n");
2290 goto out;
2291 }
2292 }
2293
2294
2295 ret = abx500_set_register_interruptible(di->dev,
2296 AB8500_CHARGER,
2297 AB8500_USBCH_CTRL2_REG,
2298 VBUS_OVV_SELECT_6P3V | VBUS_AUTO_IN_CURR_LIM_ENA);
2299 if (ret) {
2300 dev_err(di->dev, "failed to set VBUS OVV\n");
2301 goto out;
2302 }
2303
2304
2305 ret = abx500_set_register_interruptible(di->dev,
2306 AB8500_OTP_EMUL, AB8500_OTP_CONF_15, OTP_ENABLE_WD);
2307 if (ret) {
2308 dev_err(di->dev, "failed to enable main WD in OTP\n");
2309 goto out;
2310 }
2311
2312
2313 ret = abx500_set_register_interruptible(di->dev,
2314 AB8500_SYS_CTRL2_BLOCK,
2315 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_ENA);
2316 if (ret) {
2317 dev_err(di->dev, "faile to enable main watchdog\n");
2318 goto out;
2319 }
2320
2321
2322
2323
2324
2325
2326
2327 udelay(63);
2328
2329
2330 ret = abx500_set_register_interruptible(di->dev,
2331 AB8500_SYS_CTRL2_BLOCK,
2332 AB8500_MAIN_WDOG_CTRL_REG,
2333 (MAIN_WDOG_ENA | MAIN_WDOG_KICK));
2334 if (ret) {
2335 dev_err(di->dev, "failed to kick main watchdog\n");
2336 goto out;
2337 }
2338
2339
2340 ret = abx500_set_register_interruptible(di->dev,
2341 AB8500_SYS_CTRL2_BLOCK,
2342 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_DIS);
2343 if (ret) {
2344 dev_err(di->dev, "failed to disable main watchdog\n");
2345 goto out;
2346 }
2347
2348
2349 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
2350 AB8500_CH_WD_TIMER_REG, WD_TIMER);
2351 if (ret) {
2352 dev_err(di->dev, "failed to set charger watchdog timeout\n");
2353 goto out;
2354 }
2355
2356
2357 ret = abx500_set_register_interruptible(di->dev,
2358 AB8500_RTC,
2359 AB8500_RTC_BACKUP_CHG_REG,
2360 di->bat->bkup_bat_v |
2361 di->bat->bkup_bat_i);
2362 if (ret) {
2363 dev_err(di->dev, "failed to setup backup battery charging\n");
2364 goto out;
2365 }
2366
2367
2368 abx500_mask_and_set_register_interruptible(di->dev,
2369 AB8500_RTC, AB8500_RTC_CTRL_REG,
2370 RTC_BUP_CH_ENA, RTC_BUP_CH_ENA);
2371 if (ret < 0)
2372 dev_err(di->dev, "%s mask and set failed\n", __func__);
2373
2374out:
2375 return ret;
2376}
2377
2378
2379
2380
2381static struct ab8500_charger_interrupts ab8500_charger_irq[] = {
2382 {"MAIN_CH_UNPLUG_DET", ab8500_charger_mainchunplugdet_handler},
2383 {"MAIN_CHARGE_PLUG_DET", ab8500_charger_mainchplugdet_handler},
2384 {"MAIN_EXT_CH_NOT_OK", ab8500_charger_mainextchnotok_handler},
2385 {"MAIN_CH_TH_PROT_R", ab8500_charger_mainchthprotr_handler},
2386 {"MAIN_CH_TH_PROT_F", ab8500_charger_mainchthprotf_handler},
2387 {"VBUS_DET_F", ab8500_charger_vbusdetf_handler},
2388 {"VBUS_DET_R", ab8500_charger_vbusdetr_handler},
2389 {"USB_LINK_STATUS", ab8500_charger_usblinkstatus_handler},
2390 {"USB_CH_TH_PROT_R", ab8500_charger_usbchthprotr_handler},
2391 {"USB_CH_TH_PROT_F", ab8500_charger_usbchthprotf_handler},
2392 {"USB_CHARGER_NOT_OKR", ab8500_charger_usbchargernotokr_handler},
2393 {"VBUS_OVV", ab8500_charger_vbusovv_handler},
2394 {"CH_WD_EXP", ab8500_charger_chwdexp_handler},
2395};
2396
2397static int ab8500_charger_usb_notifier_call(struct notifier_block *nb,
2398 unsigned long event, void *power)
2399{
2400 struct ab8500_charger *di =
2401 container_of(nb, struct ab8500_charger, nb);
2402 enum ab8500_usb_state bm_usb_state;
2403 unsigned mA = *((unsigned *)power);
2404
2405 if (event != USB_EVENT_VBUS) {
2406 dev_dbg(di->dev, "not a standard host, returning\n");
2407 return NOTIFY_DONE;
2408 }
2409
2410
2411
2412
2413 if ((di->usb_state.usb_current == 2) && (mA > 2))
2414 bm_usb_state = AB8500_BM_USB_STATE_RESUME;
2415 else if (mA == 0)
2416 bm_usb_state = AB8500_BM_USB_STATE_RESET_HS;
2417 else if (mA == 2)
2418 bm_usb_state = AB8500_BM_USB_STATE_SUSPEND;
2419 else if (mA >= 8)
2420 bm_usb_state = AB8500_BM_USB_STATE_CONFIGURED;
2421 else
2422 bm_usb_state = AB8500_BM_USB_STATE_RESET_FS;
2423
2424 dev_dbg(di->dev, "%s usb_state: 0x%02x mA: %d\n",
2425 __func__, bm_usb_state, mA);
2426
2427 spin_lock(&di->usb_state.usb_lock);
2428 di->usb_state.usb_changed = true;
2429 spin_unlock(&di->usb_state.usb_lock);
2430
2431 di->usb_state.state = bm_usb_state;
2432 di->usb_state.usb_current = mA;
2433
2434 queue_work(di->charger_wq, &di->usb_state_changed_work);
2435
2436 return NOTIFY_OK;
2437}
2438
2439#if defined(CONFIG_PM)
2440static int ab8500_charger_resume(struct platform_device *pdev)
2441{
2442 int ret;
2443 struct ab8500_charger *di = platform_get_drvdata(pdev);
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454 if (di->ac_conn && is_ab8500_1p1_or_earlier(di->parent)) {
2455 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
2456 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
2457 if (ret)
2458 dev_err(di->dev, "Failed to kick WD!\n");
2459
2460
2461 if (!delayed_work_pending(
2462 &di->kick_wd_work)) {
2463 queue_delayed_work(di->charger_wq, &di->kick_wd_work,
2464 round_jiffies(WD_KICK_INTERVAL));
2465 }
2466 }
2467
2468
2469 if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
2470 queue_delayed_work(di->charger_wq,
2471 &di->check_hw_failure_work, 0);
2472 }
2473
2474 return 0;
2475}
2476
2477static int ab8500_charger_suspend(struct platform_device *pdev,
2478 pm_message_t state)
2479{
2480 struct ab8500_charger *di = platform_get_drvdata(pdev);
2481
2482
2483 if (delayed_work_pending(&di->check_hw_failure_work))
2484 cancel_delayed_work(&di->check_hw_failure_work);
2485
2486 return 0;
2487}
2488#else
2489#define ab8500_charger_suspend NULL
2490#define ab8500_charger_resume NULL
2491#endif
2492
2493static int __devexit ab8500_charger_remove(struct platform_device *pdev)
2494{
2495 struct ab8500_charger *di = platform_get_drvdata(pdev);
2496 int i, irq, ret;
2497
2498
2499 ab8500_charger_ac_en(&di->ac_chg, false, 0, 0);
2500
2501
2502 ab8500_charger_usb_en(&di->usb_chg, false, 0, 0);
2503
2504
2505 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
2506 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
2507 free_irq(irq, di);
2508 }
2509
2510
2511 regulator_put(di->regu);
2512
2513
2514 ret = abx500_mask_and_set_register_interruptible(di->dev,
2515 AB8500_RTC, AB8500_RTC_CTRL_REG, RTC_BUP_CH_ENA, 0);
2516 if (ret < 0)
2517 dev_err(di->dev, "%s mask and set failed\n", __func__);
2518
2519 usb_unregister_notifier(di->usb_phy, &di->nb);
2520 usb_put_transceiver(di->usb_phy);
2521
2522
2523 destroy_workqueue(di->charger_wq);
2524
2525 flush_scheduled_work();
2526 power_supply_unregister(&di->usb_chg.psy);
2527 power_supply_unregister(&di->ac_chg.psy);
2528 platform_set_drvdata(pdev, NULL);
2529 kfree(di);
2530
2531 return 0;
2532}
2533
2534static int __devinit ab8500_charger_probe(struct platform_device *pdev)
2535{
2536 int irq, i, charger_status, ret = 0;
2537 struct abx500_bm_plat_data *plat_data = pdev->dev.platform_data;
2538 struct ab8500_charger *di;
2539
2540 if (!plat_data) {
2541 dev_err(&pdev->dev, "No platform data\n");
2542 return -EINVAL;
2543 }
2544
2545 di = kzalloc(sizeof(*di), GFP_KERNEL);
2546 if (!di)
2547 return -ENOMEM;
2548
2549
2550 di->dev = &pdev->dev;
2551 di->parent = dev_get_drvdata(pdev->dev.parent);
2552 di->gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
2553
2554
2555 spin_lock_init(&di->usb_state.usb_lock);
2556
2557
2558 di->pdata = plat_data->charger;
2559 if (!di->pdata) {
2560 dev_err(di->dev, "no charger platform data supplied\n");
2561 ret = -EINVAL;
2562 goto free_device_info;
2563 }
2564
2565
2566 di->bat = plat_data->battery;
2567 if (!di->bat) {
2568 dev_err(di->dev, "no battery platform data supplied\n");
2569 ret = -EINVAL;
2570 goto free_device_info;
2571 }
2572
2573 di->autopower = false;
2574
2575
2576
2577 di->ac_chg.psy.name = "ab8500_ac";
2578 di->ac_chg.psy.type = POWER_SUPPLY_TYPE_MAINS;
2579 di->ac_chg.psy.properties = ab8500_charger_ac_props;
2580 di->ac_chg.psy.num_properties = ARRAY_SIZE(ab8500_charger_ac_props);
2581 di->ac_chg.psy.get_property = ab8500_charger_ac_get_property;
2582 di->ac_chg.psy.supplied_to = di->pdata->supplied_to;
2583 di->ac_chg.psy.num_supplicants = di->pdata->num_supplicants;
2584
2585 di->ac_chg.ops.enable = &ab8500_charger_ac_en;
2586 di->ac_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
2587 di->ac_chg.ops.update_curr = &ab8500_charger_update_charger_current;
2588 di->ac_chg.max_out_volt = ab8500_charger_voltage_map[
2589 ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
2590 di->ac_chg.max_out_curr = ab8500_charger_current_map[
2591 ARRAY_SIZE(ab8500_charger_current_map) - 1];
2592
2593
2594
2595 di->usb_chg.psy.name = "ab8500_usb";
2596 di->usb_chg.psy.type = POWER_SUPPLY_TYPE_USB;
2597 di->usb_chg.psy.properties = ab8500_charger_usb_props;
2598 di->usb_chg.psy.num_properties = ARRAY_SIZE(ab8500_charger_usb_props);
2599 di->usb_chg.psy.get_property = ab8500_charger_usb_get_property;
2600 di->usb_chg.psy.supplied_to = di->pdata->supplied_to;
2601 di->usb_chg.psy.num_supplicants = di->pdata->num_supplicants;
2602
2603 di->usb_chg.ops.enable = &ab8500_charger_usb_en;
2604 di->usb_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
2605 di->usb_chg.ops.update_curr = &ab8500_charger_update_charger_current;
2606 di->usb_chg.max_out_volt = ab8500_charger_voltage_map[
2607 ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
2608 di->usb_chg.max_out_curr = ab8500_charger_current_map[
2609 ARRAY_SIZE(ab8500_charger_current_map) - 1];
2610
2611
2612
2613 di->charger_wq =
2614 create_singlethread_workqueue("ab8500_charger_wq");
2615 if (di->charger_wq == NULL) {
2616 dev_err(di->dev, "failed to create work queue\n");
2617 goto free_device_info;
2618 }
2619
2620
2621 INIT_DELAYED_WORK_DEFERRABLE(&di->check_hw_failure_work,
2622 ab8500_charger_check_hw_failure_work);
2623 INIT_DELAYED_WORK_DEFERRABLE(&di->check_usbchgnotok_work,
2624 ab8500_charger_check_usbchargernotok_work);
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635 INIT_DELAYED_WORK_DEFERRABLE(&di->kick_wd_work,
2636 ab8500_charger_kick_watchdog_work);
2637
2638 INIT_DELAYED_WORK_DEFERRABLE(&di->check_vbat_work,
2639 ab8500_charger_check_vbat_work);
2640
2641
2642 INIT_WORK(&di->usb_link_status_work,
2643 ab8500_charger_usb_link_status_work);
2644 INIT_WORK(&di->ac_work, ab8500_charger_ac_work);
2645 INIT_WORK(&di->detect_usb_type_work,
2646 ab8500_charger_detect_usb_type_work);
2647
2648 INIT_WORK(&di->usb_state_changed_work,
2649 ab8500_charger_usb_state_changed_work);
2650
2651
2652 INIT_WORK(&di->check_main_thermal_prot_work,
2653 ab8500_charger_check_main_thermal_prot_work);
2654 INIT_WORK(&di->check_usb_thermal_prot_work,
2655 ab8500_charger_check_usb_thermal_prot_work);
2656
2657
2658
2659
2660
2661
2662 di->regu = regulator_get(di->dev, "vddadc");
2663 if (IS_ERR(di->regu)) {
2664 ret = PTR_ERR(di->regu);
2665 dev_err(di->dev, "failed to get vddadc regulator\n");
2666 goto free_charger_wq;
2667 }
2668
2669
2670
2671 ret = ab8500_charger_init_hw_registers(di);
2672 if (ret) {
2673 dev_err(di->dev, "failed to initialize ABB registers\n");
2674 goto free_regulator;
2675 }
2676
2677
2678 ret = power_supply_register(di->dev, &di->ac_chg.psy);
2679 if (ret) {
2680 dev_err(di->dev, "failed to register AC charger\n");
2681 goto free_regulator;
2682 }
2683
2684
2685 ret = power_supply_register(di->dev, &di->usb_chg.psy);
2686 if (ret) {
2687 dev_err(di->dev, "failed to register USB charger\n");
2688 goto free_ac;
2689 }
2690
2691 di->usb_phy = usb_get_transceiver();
2692 if (!di->usb_phy) {
2693 dev_err(di->dev, "failed to get usb transceiver\n");
2694 ret = -EINVAL;
2695 goto free_usb;
2696 }
2697 di->nb.notifier_call = ab8500_charger_usb_notifier_call;
2698 ret = usb_register_notifier(di->usb_phy, &di->nb);
2699 if (ret) {
2700 dev_err(di->dev, "failed to register usb notifier\n");
2701 goto put_usb_phy;
2702 }
2703
2704
2705 charger_status = ab8500_charger_detect_chargers(di);
2706 if (charger_status & AC_PW_CONN) {
2707 di->ac.charger_connected = 1;
2708 di->ac_conn = true;
2709 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2710 sysfs_notify(&di->ac_chg.psy.dev->kobj, NULL, "present");
2711 }
2712
2713 if (charger_status & USB_PW_CONN) {
2714 dev_dbg(di->dev, "VBUS Detect during startup\n");
2715 di->vbus_detected = true;
2716 di->vbus_detected_start = true;
2717 queue_work(di->charger_wq,
2718 &di->detect_usb_type_work);
2719 }
2720
2721
2722 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
2723 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
2724 ret = request_threaded_irq(irq, NULL, ab8500_charger_irq[i].isr,
2725 IRQF_SHARED | IRQF_NO_SUSPEND,
2726 ab8500_charger_irq[i].name, di);
2727
2728 if (ret != 0) {
2729 dev_err(di->dev, "failed to request %s IRQ %d: %d\n"
2730 , ab8500_charger_irq[i].name, irq, ret);
2731 goto free_irq;
2732 }
2733 dev_dbg(di->dev, "Requested %s IRQ %d: %d\n",
2734 ab8500_charger_irq[i].name, irq, ret);
2735 }
2736
2737 platform_set_drvdata(pdev, di);
2738
2739 return ret;
2740
2741free_irq:
2742 usb_unregister_notifier(di->usb_phy, &di->nb);
2743
2744
2745 for (i = i - 1; i >= 0; i--) {
2746 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
2747 free_irq(irq, di);
2748 }
2749put_usb_phy:
2750 usb_put_transceiver(di->usb_phy);
2751free_usb:
2752 power_supply_unregister(&di->usb_chg.psy);
2753free_ac:
2754 power_supply_unregister(&di->ac_chg.psy);
2755free_regulator:
2756 regulator_put(di->regu);
2757free_charger_wq:
2758 destroy_workqueue(di->charger_wq);
2759free_device_info:
2760 kfree(di);
2761
2762 return ret;
2763}
2764
2765static struct platform_driver ab8500_charger_driver = {
2766 .probe = ab8500_charger_probe,
2767 .remove = __devexit_p(ab8500_charger_remove),
2768 .suspend = ab8500_charger_suspend,
2769 .resume = ab8500_charger_resume,
2770 .driver = {
2771 .name = "ab8500-charger",
2772 .owner = THIS_MODULE,
2773 },
2774};
2775
2776static int __init ab8500_charger_init(void)
2777{
2778 return platform_driver_register(&ab8500_charger_driver);
2779}
2780
2781static void __exit ab8500_charger_exit(void)
2782{
2783 platform_driver_unregister(&ab8500_charger_driver);
2784}
2785
2786subsys_initcall_sync(ab8500_charger_init);
2787module_exit(ab8500_charger_exit);
2788
2789MODULE_LICENSE("GPL v2");
2790MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy");
2791MODULE_ALIAS("platform:ab8500-charger");
2792MODULE_DESCRIPTION("AB8500 charger management driver");
2793