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/notifier.h>
19#include <linux/slab.h>
20#include <linux/platform_device.h>
21#include <linux/power_supply.h>
22#include <linux/completion.h>
23#include <linux/regulator/consumer.h>
24#include <linux/err.h>
25#include <linux/workqueue.h>
26#include <linux/kobject.h>
27#include <linux/of.h>
28#include <linux/mfd/core.h>
29#include <linux/mfd/abx500/ab8500.h>
30#include <linux/mfd/abx500.h>
31#include <linux/mfd/abx500/ab8500-bm.h>
32#include <linux/mfd/abx500/ab8500-gpadc.h>
33#include <linux/mfd/abx500/ux500_chargalg.h>
34#include <linux/usb/otg.h>
35#include <linux/mutex.h>
36
37
38#define NO_PW_CONN 0
39#define AC_PW_CONN 1
40#define USB_PW_CONN 2
41
42#define MAIN_WDOG_ENA 0x01
43#define MAIN_WDOG_KICK 0x02
44#define MAIN_WDOG_DIS 0x00
45#define CHARG_WD_KICK 0x01
46#define MAIN_CH_ENA 0x01
47#define MAIN_CH_NO_OVERSHOOT_ENA_N 0x02
48#define USB_CH_ENA 0x01
49#define USB_CHG_NO_OVERSHOOT_ENA_N 0x02
50#define MAIN_CH_DET 0x01
51#define MAIN_CH_CV_ON 0x04
52#define USB_CH_CV_ON 0x08
53#define VBUS_DET_DBNC100 0x02
54#define VBUS_DET_DBNC1 0x01
55#define OTP_ENABLE_WD 0x01
56#define DROP_COUNT_RESET 0x01
57#define USB_CH_DET 0x01
58
59#define MAIN_CH_INPUT_CURR_SHIFT 4
60#define VBUS_IN_CURR_LIM_SHIFT 4
61#define AB8540_VBUS_IN_CURR_LIM_SHIFT 2
62#define AUTO_VBUS_IN_CURR_LIM_SHIFT 4
63#define AB8540_AUTO_VBUS_IN_CURR_MASK 0x3F
64#define VBUS_IN_CURR_LIM_RETRY_SET_TIME 30
65
66#define LED_INDICATOR_PWM_ENA 0x01
67#define LED_INDICATOR_PWM_DIS 0x00
68#define LED_IND_CUR_5MA 0x04
69#define LED_INDICATOR_PWM_DUTY_252_256 0xBF
70
71
72#define MAIN_CH_TH_PROT 0x02
73#define VBUS_CH_NOK 0x08
74#define USB_CH_TH_PROT 0x02
75#define VBUS_OVV_TH 0x01
76#define MAIN_CH_NOK 0x01
77#define VBUS_DET 0x80
78
79#define MAIN_CH_STATUS2_MAINCHGDROP 0x80
80#define MAIN_CH_STATUS2_MAINCHARGERDETDBNC 0x40
81#define USB_CH_VBUSDROP 0x40
82#define USB_CH_VBUSDETDBNC 0x01
83
84
85#define AB8500_USB_LINK_STATUS 0x78
86#define AB8505_USB_LINK_STATUS 0xF8
87#define AB8500_STD_HOST_SUSP 0x18
88#define USB_LINK_STATUS_SHIFT 3
89
90
91#define WD_TIMER 0x30
92#define WD_KICK_INTERVAL (60 * HZ)
93
94
95#define LOW_VOLT_REG 0x4E
96
97
98#define STEP_UDELAY 1000
99
100#define CHARGER_STATUS_POLL 10
101
102#define CHG_WD_INTERVAL (60 * HZ)
103
104#define AB8500_SW_CONTROL_FALLBACK 0x03
105
106#define WAIT_ACA_RID_ENUMERATION (5 * 1000)
107
108#define AB8500_SYS_CHARGER_CONTROL_REG 0x52
109#define EXTERNAL_CHARGER_DISABLE_REG_VAL 0x03
110#define EXTERNAL_CHARGER_ENABLE_REG_VAL 0x07
111
112
113enum ab8500_charger_link_status {
114 USB_STAT_NOT_CONFIGURED,
115 USB_STAT_STD_HOST_NC,
116 USB_STAT_STD_HOST_C_NS,
117 USB_STAT_STD_HOST_C_S,
118 USB_STAT_HOST_CHG_NM,
119 USB_STAT_HOST_CHG_HS,
120 USB_STAT_HOST_CHG_HS_CHIRP,
121 USB_STAT_DEDICATED_CHG,
122 USB_STAT_ACA_RID_A,
123 USB_STAT_ACA_RID_B,
124 USB_STAT_ACA_RID_C_NM,
125 USB_STAT_ACA_RID_C_HS,
126 USB_STAT_ACA_RID_C_HS_CHIRP,
127 USB_STAT_HM_IDGND,
128 USB_STAT_RESERVED,
129 USB_STAT_NOT_VALID_LINK,
130 USB_STAT_PHY_EN,
131 USB_STAT_SUP_NO_IDGND_VBUS,
132 USB_STAT_SUP_IDGND_VBUS,
133 USB_STAT_CHARGER_LINE_1,
134 USB_STAT_CARKIT_1,
135 USB_STAT_CARKIT_2,
136 USB_STAT_ACA_DOCK_CHARGER,
137};
138
139enum ab8500_usb_state {
140 AB8500_BM_USB_STATE_RESET_HS,
141 AB8500_BM_USB_STATE_RESET_FS,
142 AB8500_BM_USB_STATE_CONFIGURED,
143 AB8500_BM_USB_STATE_SUSPEND,
144 AB8500_BM_USB_STATE_RESUME,
145 AB8500_BM_USB_STATE_MAX,
146};
147
148
149#define USB_CH_IP_CUR_LVL_0P05 50
150#define USB_CH_IP_CUR_LVL_0P09 98
151#define USB_CH_IP_CUR_LVL_0P19 193
152#define USB_CH_IP_CUR_LVL_0P29 290
153#define USB_CH_IP_CUR_LVL_0P38 380
154#define USB_CH_IP_CUR_LVL_0P45 450
155#define USB_CH_IP_CUR_LVL_0P5 500
156#define USB_CH_IP_CUR_LVL_0P6 600
157#define USB_CH_IP_CUR_LVL_0P7 700
158#define USB_CH_IP_CUR_LVL_0P8 800
159#define USB_CH_IP_CUR_LVL_0P9 900
160#define USB_CH_IP_CUR_LVL_1P0 1000
161#define USB_CH_IP_CUR_LVL_1P1 1100
162#define USB_CH_IP_CUR_LVL_1P3 1300
163#define USB_CH_IP_CUR_LVL_1P4 1400
164#define USB_CH_IP_CUR_LVL_1P5 1500
165
166#define VBAT_TRESH_IP_CUR_RED 3800
167
168#define to_ab8500_charger_usb_device_info(x) container_of((x), \
169 struct ab8500_charger, usb_chg)
170#define to_ab8500_charger_ac_device_info(x) container_of((x), \
171 struct ab8500_charger, ac_chg)
172
173
174
175
176
177
178struct ab8500_charger_interrupts {
179 char *name;
180 irqreturn_t (*isr)(int irq, void *data);
181};
182
183struct ab8500_charger_info {
184 int charger_connected;
185 int charger_online;
186 int charger_voltage;
187 int cv_active;
188 bool wd_expired;
189 int charger_current;
190};
191
192struct ab8500_charger_event_flags {
193 bool mainextchnotok;
194 bool main_thermal_prot;
195 bool usb_thermal_prot;
196 bool vbus_ovv;
197 bool usbchargernotok;
198 bool chgwdexp;
199 bool vbus_collapse;
200 bool vbus_drop_end;
201};
202
203struct ab8500_charger_usb_state {
204 int usb_current;
205 int usb_current_tmp;
206 enum ab8500_usb_state state;
207 enum ab8500_usb_state state_tmp;
208 spinlock_t usb_lock;
209};
210
211struct ab8500_charger_max_usb_in_curr {
212 int usb_type_max;
213 int set_max;
214 int calculated_max;
215};
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272struct ab8500_charger {
273 struct device *dev;
274 bool vbus_detected;
275 bool vbus_detected_start;
276 bool ac_conn;
277 bool vddadc_en_ac;
278 bool vddadc_en_usb;
279 int vbat;
280 int old_vbat;
281 bool usb_device_is_unrecognised;
282 bool autopower;
283 bool autopower_cfg;
284 int invalid_charger_detect_state;
285 int is_aca_rid;
286 atomic_t current_stepping_sessions;
287 struct ab8500 *parent;
288 struct ab8500_gpadc *gpadc;
289 struct abx500_bm_data *bm;
290 struct ab8500_charger_event_flags flags;
291 struct ab8500_charger_usb_state usb_state;
292 struct ab8500_charger_max_usb_in_curr max_usb_in_curr;
293 struct ux500_charger ac_chg;
294 struct ux500_charger usb_chg;
295 struct ab8500_charger_info ac;
296 struct ab8500_charger_info usb;
297 struct regulator *regu;
298 struct workqueue_struct *charger_wq;
299 struct mutex usb_ipt_crnt_lock;
300 struct delayed_work check_vbat_work;
301 struct delayed_work check_hw_failure_work;
302 struct delayed_work check_usbchgnotok_work;
303 struct delayed_work kick_wd_work;
304 struct delayed_work usb_state_changed_work;
305 struct delayed_work attach_work;
306 struct delayed_work ac_charger_attached_work;
307 struct delayed_work usb_charger_attached_work;
308 struct delayed_work vbus_drop_end_work;
309 struct work_struct ac_work;
310 struct work_struct detect_usb_type_work;
311 struct work_struct usb_link_status_work;
312 struct work_struct check_main_thermal_prot_work;
313 struct work_struct check_usb_thermal_prot_work;
314 struct usb_phy *usb_phy;
315 struct notifier_block nb;
316 struct mutex charger_attached_mutex;
317};
318
319
320static enum power_supply_property ab8500_charger_ac_props[] = {
321 POWER_SUPPLY_PROP_HEALTH,
322 POWER_SUPPLY_PROP_PRESENT,
323 POWER_SUPPLY_PROP_ONLINE,
324 POWER_SUPPLY_PROP_VOLTAGE_NOW,
325 POWER_SUPPLY_PROP_VOLTAGE_AVG,
326 POWER_SUPPLY_PROP_CURRENT_NOW,
327};
328
329
330static enum power_supply_property ab8500_charger_usb_props[] = {
331 POWER_SUPPLY_PROP_HEALTH,
332 POWER_SUPPLY_PROP_CURRENT_AVG,
333 POWER_SUPPLY_PROP_PRESENT,
334 POWER_SUPPLY_PROP_ONLINE,
335 POWER_SUPPLY_PROP_VOLTAGE_NOW,
336 POWER_SUPPLY_PROP_VOLTAGE_AVG,
337 POWER_SUPPLY_PROP_CURRENT_NOW,
338};
339
340
341
342
343
344static void ab8500_enable_disable_sw_fallback(struct ab8500_charger *di,
345 bool fallback)
346{
347 u8 val;
348 u8 reg;
349 u8 bank;
350 u8 bit;
351 int ret;
352
353 dev_dbg(di->dev, "SW Fallback: %d\n", fallback);
354
355 if (is_ab8500(di->parent)) {
356 bank = 0x15;
357 reg = 0x0;
358 bit = 3;
359 } else {
360 bank = AB8500_SYS_CTRL1_BLOCK;
361 reg = AB8500_SW_CONTROL_FALLBACK;
362 bit = 0;
363 }
364
365
366 ret = abx500_get_register_interruptible(di->dev, bank, reg, &val);
367 if (ret < 0) {
368 dev_err(di->dev, "%d read failed\n", __LINE__);
369 return;
370 }
371
372 if (is_ab8500(di->parent)) {
373
374 ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x2);
375 if (ret) {
376 dev_err(di->dev, "%d write failed\n", __LINE__);
377 goto disable_otp;
378 }
379 }
380
381 if (fallback)
382 val |= (1 << bit);
383 else
384 val &= ~(1 << bit);
385
386
387 ret = abx500_set_register_interruptible(di->dev, bank, reg, val);
388 if (ret) {
389 dev_err(di->dev, "%d write failed\n", __LINE__);
390 }
391
392disable_otp:
393 if (is_ab8500(di->parent)) {
394
395 ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x0);
396 if (ret) {
397 dev_err(di->dev, "%d write failed\n", __LINE__);
398 }
399 }
400}
401
402
403
404
405
406
407
408
409static void ab8500_power_supply_changed(struct ab8500_charger *di,
410 struct power_supply *psy)
411{
412 if (di->autopower_cfg) {
413 if (!di->usb.charger_connected &&
414 !di->ac.charger_connected &&
415 di->autopower) {
416 di->autopower = false;
417 ab8500_enable_disable_sw_fallback(di, false);
418 } else if (!di->autopower &&
419 (di->ac.charger_connected ||
420 di->usb.charger_connected)) {
421 di->autopower = true;
422 ab8500_enable_disable_sw_fallback(di, true);
423 }
424 }
425 power_supply_changed(psy);
426}
427
428static void ab8500_charger_set_usb_connected(struct ab8500_charger *di,
429 bool connected)
430{
431 if (connected != di->usb.charger_connected) {
432 dev_dbg(di->dev, "USB connected:%i\n", connected);
433 di->usb.charger_connected = connected;
434
435 if (!connected)
436 di->flags.vbus_drop_end = false;
437
438 sysfs_notify(&di->usb_chg.psy.dev->kobj, NULL, "present");
439
440 if (connected) {
441 mutex_lock(&di->charger_attached_mutex);
442 mutex_unlock(&di->charger_attached_mutex);
443
444 if (is_ab8500(di->parent))
445 queue_delayed_work(di->charger_wq,
446 &di->usb_charger_attached_work,
447 HZ);
448 } else {
449 cancel_delayed_work_sync(&di->usb_charger_attached_work);
450 mutex_lock(&di->charger_attached_mutex);
451 mutex_unlock(&di->charger_attached_mutex);
452 }
453 }
454}
455
456
457
458
459
460
461
462static int ab8500_charger_get_ac_voltage(struct ab8500_charger *di)
463{
464 int vch;
465
466
467 if (di->ac.charger_connected) {
468 vch = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_V);
469 if (vch < 0)
470 dev_err(di->dev, "%s gpadc conv failed,\n", __func__);
471 } else {
472 vch = 0;
473 }
474 return vch;
475}
476
477
478
479
480
481
482
483static int ab8500_charger_ac_cv(struct ab8500_charger *di)
484{
485 u8 val;
486 int ret = 0;
487
488
489 if (di->ac.charger_online) {
490 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
491 AB8500_CH_STATUS1_REG, &val);
492 if (ret < 0) {
493 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
494 return 0;
495 }
496
497 if (val & MAIN_CH_CV_ON)
498 ret = 1;
499 else
500 ret = 0;
501 }
502
503 return ret;
504}
505
506
507
508
509
510
511
512
513static int ab8500_charger_get_vbus_voltage(struct ab8500_charger *di)
514{
515 int vch;
516
517
518 if (di->usb.charger_connected) {
519 vch = ab8500_gpadc_convert(di->gpadc, VBUS_V);
520 if (vch < 0)
521 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
522 } else {
523 vch = 0;
524 }
525 return vch;
526}
527
528
529
530
531
532
533
534
535static int ab8500_charger_get_usb_current(struct ab8500_charger *di)
536{
537 int ich;
538
539
540 if (di->usb.charger_online) {
541 ich = ab8500_gpadc_convert(di->gpadc, USB_CHARGER_C);
542 if (ich < 0)
543 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
544 } else {
545 ich = 0;
546 }
547 return ich;
548}
549
550
551
552
553
554
555
556
557static int ab8500_charger_get_ac_current(struct ab8500_charger *di)
558{
559 int ich;
560
561
562 if (di->ac.charger_online) {
563 ich = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_C);
564 if (ich < 0)
565 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
566 } else {
567 ich = 0;
568 }
569 return ich;
570}
571
572
573
574
575
576
577
578static int ab8500_charger_usb_cv(struct ab8500_charger *di)
579{
580 int ret;
581 u8 val;
582
583
584 if (di->usb.charger_online) {
585 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
586 AB8500_CH_USBCH_STAT1_REG, &val);
587 if (ret < 0) {
588 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
589 return 0;
590 }
591
592 if (val & USB_CH_CV_ON)
593 ret = 1;
594 else
595 ret = 0;
596 } else {
597 ret = 0;
598 }
599
600 return ret;
601}
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620static int ab8500_charger_detect_chargers(struct ab8500_charger *di, bool probe)
621{
622 int result = NO_PW_CONN;
623 int ret;
624 u8 val;
625
626
627 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
628 AB8500_CH_STATUS1_REG, &val);
629 if (ret < 0) {
630 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
631 return ret;
632 }
633
634 if (val & MAIN_CH_DET)
635 result = AC_PW_CONN;
636
637
638
639 if (!probe) {
640
641
642
643
644
645
646 msleep(110);
647 }
648 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
649 AB8500_CH_USBCH_STAT1_REG, &val);
650 if (ret < 0) {
651 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
652 return ret;
653 }
654 dev_dbg(di->dev,
655 "%s AB8500_CH_USBCH_STAT1_REG %x\n", __func__,
656 val);
657 if ((val & VBUS_DET_DBNC1) && (val & VBUS_DET_DBNC100))
658 result |= USB_PW_CONN;
659
660 return result;
661}
662
663
664
665
666
667
668
669
670
671
672static int ab8500_charger_max_usb_curr(struct ab8500_charger *di,
673 enum ab8500_charger_link_status link_status)
674{
675 int ret = 0;
676
677 di->usb_device_is_unrecognised = false;
678
679
680
681
682
683
684
685
686 switch (link_status) {
687 case USB_STAT_STD_HOST_NC:
688 case USB_STAT_STD_HOST_C_NS:
689 case USB_STAT_STD_HOST_C_S:
690 dev_dbg(di->dev, "USB Type - Standard host is "
691 "detected through USB driver\n");
692 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
693 di->is_aca_rid = 0;
694 break;
695 case USB_STAT_HOST_CHG_HS_CHIRP:
696 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
697 di->is_aca_rid = 0;
698 break;
699 case USB_STAT_HOST_CHG_HS:
700 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
701 di->is_aca_rid = 0;
702 break;
703 case USB_STAT_ACA_RID_C_HS:
704 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P9;
705 di->is_aca_rid = 0;
706 break;
707 case USB_STAT_ACA_RID_A:
708
709
710
711
712 dev_dbg(di->dev, "USB_STAT_ACA_RID_A detected\n");
713 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
714 di->is_aca_rid = 1;
715 break;
716 case USB_STAT_ACA_RID_B:
717
718
719
720
721 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_1P3;
722 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
723 di->max_usb_in_curr.usb_type_max);
724 di->is_aca_rid = 1;
725 break;
726 case USB_STAT_HOST_CHG_NM:
727 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
728 di->is_aca_rid = 0;
729 break;
730 case USB_STAT_DEDICATED_CHG:
731 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_1P5;
732 di->is_aca_rid = 0;
733 break;
734 case USB_STAT_ACA_RID_C_HS_CHIRP:
735 case USB_STAT_ACA_RID_C_NM:
736 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_1P5;
737 di->is_aca_rid = 1;
738 break;
739 case USB_STAT_NOT_CONFIGURED:
740 if (di->vbus_detected) {
741 di->usb_device_is_unrecognised = true;
742 dev_dbg(di->dev, "USB Type - Legacy charger.\n");
743 di->max_usb_in_curr.usb_type_max =
744 USB_CH_IP_CUR_LVL_1P5;
745 break;
746 }
747 case USB_STAT_HM_IDGND:
748 dev_err(di->dev, "USB Type - Charging not allowed\n");
749 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P05;
750 ret = -ENXIO;
751 break;
752 case USB_STAT_RESERVED:
753 if (is_ab8500(di->parent)) {
754 di->flags.vbus_collapse = true;
755 dev_err(di->dev, "USB Type - USB_STAT_RESERVED "
756 "VBUS has collapsed\n");
757 ret = -ENXIO;
758 break;
759 } else {
760 dev_dbg(di->dev, "USB Type - Charging not allowed\n");
761 di->max_usb_in_curr.usb_type_max =
762 USB_CH_IP_CUR_LVL_0P05;
763 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
764 link_status,
765 di->max_usb_in_curr.usb_type_max);
766 ret = -ENXIO;
767 break;
768 }
769 case USB_STAT_CARKIT_1:
770 case USB_STAT_CARKIT_2:
771 case USB_STAT_ACA_DOCK_CHARGER:
772 case USB_STAT_CHARGER_LINE_1:
773 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
774 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
775 di->max_usb_in_curr.usb_type_max);
776 break;
777 case USB_STAT_NOT_VALID_LINK:
778 dev_err(di->dev, "USB Type invalid - try charging anyway\n");
779 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
780 break;
781
782 default:
783 dev_err(di->dev, "USB Type - Unknown\n");
784 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P05;
785 ret = -ENXIO;
786 break;
787 };
788
789 di->max_usb_in_curr.set_max = di->max_usb_in_curr.usb_type_max;
790 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
791 link_status, di->max_usb_in_curr.set_max);
792
793 return ret;
794}
795
796
797
798
799
800
801
802
803static int ab8500_charger_read_usb_type(struct ab8500_charger *di)
804{
805 int ret;
806 u8 val;
807
808 ret = abx500_get_register_interruptible(di->dev,
809 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG, &val);
810 if (ret < 0) {
811 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
812 return ret;
813 }
814 if (is_ab8500(di->parent))
815 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
816 AB8500_USB_LINE_STAT_REG, &val);
817 else
818 ret = abx500_get_register_interruptible(di->dev,
819 AB8500_USB, AB8500_USB_LINK1_STAT_REG, &val);
820 if (ret < 0) {
821 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
822 return ret;
823 }
824
825
826 if (is_ab8500(di->parent))
827 val = (val & AB8500_USB_LINK_STATUS) >> USB_LINK_STATUS_SHIFT;
828 else
829 val = (val & AB8505_USB_LINK_STATUS) >> USB_LINK_STATUS_SHIFT;
830 ret = ab8500_charger_max_usb_curr(di,
831 (enum ab8500_charger_link_status) val);
832
833 return ret;
834}
835
836
837
838
839
840
841
842
843static int ab8500_charger_detect_usb_type(struct ab8500_charger *di)
844{
845 int i, ret;
846 u8 val;
847
848
849
850
851
852
853 for (i = 0; i < 10; i++) {
854 msleep(250);
855 ret = abx500_get_register_interruptible(di->dev,
856 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG,
857 &val);
858 dev_dbg(di->dev, "%s AB8500_IT_SOURCE21_REG %x\n",
859 __func__, val);
860 if (ret < 0) {
861 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
862 return ret;
863 }
864
865 if (is_ab8500(di->parent))
866 ret = abx500_get_register_interruptible(di->dev,
867 AB8500_USB, AB8500_USB_LINE_STAT_REG, &val);
868 else
869 ret = abx500_get_register_interruptible(di->dev,
870 AB8500_USB, AB8500_USB_LINK1_STAT_REG, &val);
871 if (ret < 0) {
872 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
873 return ret;
874 }
875 dev_dbg(di->dev, "%s AB8500_USB_LINE_STAT_REG %x\n", __func__,
876 val);
877
878
879
880
881
882
883
884 if (is_ab8500(di->parent))
885 val = (val & AB8500_USB_LINK_STATUS) >>
886 USB_LINK_STATUS_SHIFT;
887 else
888 val = (val & AB8505_USB_LINK_STATUS) >>
889 USB_LINK_STATUS_SHIFT;
890 if (val)
891 break;
892 }
893 ret = ab8500_charger_max_usb_curr(di,
894 (enum ab8500_charger_link_status) val);
895
896 return ret;
897}
898
899
900
901
902
903static int ab8500_charger_voltage_map[] = {
904 3500 ,
905 3525 ,
906 3550 ,
907 3575 ,
908 3600 ,
909 3625 ,
910 3650 ,
911 3675 ,
912 3700 ,
913 3725 ,
914 3750 ,
915 3775 ,
916 3800 ,
917 3825 ,
918 3850 ,
919 3875 ,
920 3900 ,
921 3925 ,
922 3950 ,
923 3975 ,
924 4000 ,
925 4025 ,
926 4050 ,
927 4060 ,
928 4070 ,
929 4080 ,
930 4090 ,
931 4100 ,
932 4110 ,
933 4120 ,
934 4130 ,
935 4140 ,
936 4150 ,
937 4160 ,
938 4170 ,
939 4180 ,
940 4190 ,
941 4200 ,
942 4210 ,
943 4220 ,
944 4230 ,
945 4240 ,
946 4250 ,
947 4260 ,
948 4270 ,
949 4280 ,
950 4290 ,
951 4300 ,
952 4310 ,
953 4320 ,
954 4330 ,
955 4340 ,
956 4350 ,
957 4360 ,
958 4370 ,
959 4380 ,
960 4390 ,
961 4400 ,
962 4410 ,
963 4420 ,
964 4430 ,
965 4440 ,
966 4450 ,
967 4460 ,
968 4470 ,
969 4480 ,
970 4490 ,
971 4500 ,
972 4510 ,
973 4520 ,
974 4530 ,
975 4540 ,
976 4550 ,
977 4560 ,
978 4570 ,
979 4580 ,
980 4590 ,
981 4600 ,
982};
983
984static int ab8500_voltage_to_regval(int voltage)
985{
986 int i;
987
988
989 if (voltage < ab8500_charger_voltage_map[0])
990 return LOW_VOLT_REG;
991
992 for (i = 1; i < ARRAY_SIZE(ab8500_charger_voltage_map); i++) {
993 if (voltage < ab8500_charger_voltage_map[i])
994 return i - 1;
995 }
996
997
998 i = ARRAY_SIZE(ab8500_charger_voltage_map) - 1;
999 if (voltage == ab8500_charger_voltage_map[i])
1000 return i;
1001 else
1002 return -1;
1003}
1004
1005static int ab8500_current_to_regval(struct ab8500_charger *di, int curr)
1006{
1007 int i;
1008
1009 if (curr < di->bm->chg_output_curr[0])
1010 return 0;
1011
1012 for (i = 0; i < di->bm->n_chg_out_curr; i++) {
1013 if (curr < di->bm->chg_output_curr[i])
1014 return i - 1;
1015 }
1016
1017
1018 i = di->bm->n_chg_out_curr - 1;
1019 if (curr == di->bm->chg_output_curr[i])
1020 return i;
1021 else
1022 return -1;
1023}
1024
1025static int ab8500_vbus_in_curr_to_regval(struct ab8500_charger *di, int curr)
1026{
1027 int i;
1028
1029 if (curr < di->bm->chg_input_curr[0])
1030 return 0;
1031
1032 for (i = 0; i < di->bm->n_chg_in_curr; i++) {
1033 if (curr < di->bm->chg_input_curr[i])
1034 return i - 1;
1035 }
1036
1037
1038 i = di->bm->n_chg_in_curr - 1;
1039 if (curr == di->bm->chg_input_curr[i])
1040 return i;
1041 else
1042 return -1;
1043}
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054static int ab8500_charger_get_usb_cur(struct ab8500_charger *di)
1055{
1056 int ret = 0;
1057 switch (di->usb_state.usb_current) {
1058 case 100:
1059 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P09;
1060 break;
1061 case 200:
1062 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P19;
1063 break;
1064 case 300:
1065 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P29;
1066 break;
1067 case 400:
1068 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P38;
1069 break;
1070 case 500:
1071 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
1072 break;
1073 default:
1074 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P05;
1075 ret = -EPERM;
1076 break;
1077 };
1078 di->max_usb_in_curr.set_max = di->max_usb_in_curr.usb_type_max;
1079 return ret;
1080}
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091static bool ab8500_charger_check_continue_stepping(struct ab8500_charger *di,
1092 int reg)
1093{
1094 if (reg == AB8500_USBCH_IPT_CRNTLVL_REG)
1095 return !di->flags.vbus_drop_end;
1096 else
1097 return true;
1098}
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113static int ab8500_charger_set_current(struct ab8500_charger *di,
1114 int ich, int reg)
1115{
1116 int ret = 0;
1117 int curr_index, prev_curr_index, shift_value, i;
1118 u8 reg_value;
1119 u32 step_udelay;
1120 bool no_stepping = false;
1121
1122 atomic_inc(&di->current_stepping_sessions);
1123
1124 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1125 reg, ®_value);
1126 if (ret < 0) {
1127 dev_err(di->dev, "%s read failed\n", __func__);
1128 goto exit_set_current;
1129 }
1130
1131 switch (reg) {
1132 case AB8500_MCH_IPT_CURLVL_REG:
1133 shift_value = MAIN_CH_INPUT_CURR_SHIFT;
1134 prev_curr_index = (reg_value >> shift_value);
1135 curr_index = ab8500_current_to_regval(di, ich);
1136 step_udelay = STEP_UDELAY;
1137 if (!di->ac.charger_connected)
1138 no_stepping = true;
1139 break;
1140 case AB8500_USBCH_IPT_CRNTLVL_REG:
1141 if (is_ab8540(di->parent))
1142 shift_value = AB8540_VBUS_IN_CURR_LIM_SHIFT;
1143 else
1144 shift_value = VBUS_IN_CURR_LIM_SHIFT;
1145 prev_curr_index = (reg_value >> shift_value);
1146 curr_index = ab8500_vbus_in_curr_to_regval(di, ich);
1147 step_udelay = STEP_UDELAY * 100;
1148
1149 if (!di->usb.charger_connected)
1150 no_stepping = true;
1151 break;
1152 case AB8500_CH_OPT_CRNTLVL_REG:
1153 shift_value = 0;
1154 prev_curr_index = (reg_value >> shift_value);
1155 curr_index = ab8500_current_to_regval(di, ich);
1156 step_udelay = STEP_UDELAY;
1157 if (curr_index && (curr_index - prev_curr_index) > 1)
1158 step_udelay *= 100;
1159
1160 if (!di->usb.charger_connected && !di->ac.charger_connected)
1161 no_stepping = true;
1162
1163 break;
1164 default:
1165 dev_err(di->dev, "%s current register not valid\n", __func__);
1166 ret = -ENXIO;
1167 goto exit_set_current;
1168 }
1169
1170 if (curr_index < 0) {
1171 dev_err(di->dev, "requested current limit out-of-range\n");
1172 ret = -ENXIO;
1173 goto exit_set_current;
1174 }
1175
1176
1177 if (prev_curr_index == curr_index) {
1178 dev_dbg(di->dev, "%s current not changed for reg: 0x%02x\n",
1179 __func__, reg);
1180 ret = 0;
1181 goto exit_set_current;
1182 }
1183
1184 dev_dbg(di->dev, "%s set charger current: %d mA for reg: 0x%02x\n",
1185 __func__, ich, reg);
1186
1187 if (no_stepping) {
1188 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1189 reg, (u8)curr_index << shift_value);
1190 if (ret)
1191 dev_err(di->dev, "%s write failed\n", __func__);
1192 } else if (prev_curr_index > curr_index) {
1193 for (i = prev_curr_index - 1; i >= curr_index; i--) {
1194 dev_dbg(di->dev, "curr change_1 to: %x for 0x%02x\n",
1195 (u8) i << shift_value, reg);
1196 ret = abx500_set_register_interruptible(di->dev,
1197 AB8500_CHARGER, reg, (u8)i << shift_value);
1198 if (ret) {
1199 dev_err(di->dev, "%s write failed\n", __func__);
1200 goto exit_set_current;
1201 }
1202 if (i != curr_index)
1203 usleep_range(step_udelay, step_udelay * 2);
1204 }
1205 } else {
1206 bool allow = true;
1207 for (i = prev_curr_index + 1; i <= curr_index && allow; i++) {
1208 dev_dbg(di->dev, "curr change_2 to: %x for 0x%02x\n",
1209 (u8)i << shift_value, reg);
1210 ret = abx500_set_register_interruptible(di->dev,
1211 AB8500_CHARGER, reg, (u8)i << shift_value);
1212 if (ret) {
1213 dev_err(di->dev, "%s write failed\n", __func__);
1214 goto exit_set_current;
1215 }
1216 if (i != curr_index)
1217 usleep_range(step_udelay, step_udelay * 2);
1218
1219 allow = ab8500_charger_check_continue_stepping(di, reg);
1220 }
1221 }
1222
1223exit_set_current:
1224 atomic_dec(&di->current_stepping_sessions);
1225
1226 return ret;
1227}
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237static int ab8500_charger_set_vbus_in_curr(struct ab8500_charger *di,
1238 int ich_in)
1239{
1240 int min_value;
1241 int ret;
1242
1243
1244 min_value = min(di->bm->chg_params->usb_curr_max, ich_in);
1245 if (di->max_usb_in_curr.set_max > 0)
1246 min_value = min(di->max_usb_in_curr.set_max, min_value);
1247
1248 if (di->usb_state.usb_current >= 0)
1249 min_value = min(di->usb_state.usb_current, min_value);
1250
1251 switch (min_value) {
1252 case 100:
1253 if (di->vbat < VBAT_TRESH_IP_CUR_RED)
1254 min_value = USB_CH_IP_CUR_LVL_0P05;
1255 break;
1256 case 500:
1257 if (di->vbat < VBAT_TRESH_IP_CUR_RED)
1258 min_value = USB_CH_IP_CUR_LVL_0P45;
1259 break;
1260 default:
1261 break;
1262 }
1263
1264 dev_info(di->dev, "VBUS input current limit set to %d mA\n", min_value);
1265
1266 mutex_lock(&di->usb_ipt_crnt_lock);
1267 ret = ab8500_charger_set_current(di, min_value,
1268 AB8500_USBCH_IPT_CRNTLVL_REG);
1269 mutex_unlock(&di->usb_ipt_crnt_lock);
1270
1271 return ret;
1272}
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282static int ab8500_charger_set_main_in_curr(struct ab8500_charger *di,
1283 int ich_in)
1284{
1285 return ab8500_charger_set_current(di, ich_in,
1286 AB8500_MCH_IPT_CURLVL_REG);
1287}
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297static int ab8500_charger_set_output_curr(struct ab8500_charger *di,
1298 int ich_out)
1299{
1300 return ab8500_charger_set_current(di, ich_out,
1301 AB8500_CH_OPT_CRNTLVL_REG);
1302}
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312static int ab8500_charger_led_en(struct ab8500_charger *di, int on)
1313{
1314 int ret;
1315
1316 if (on) {
1317
1318 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1319 AB8500_LED_INDICATOR_PWM_CTRL,
1320 (LED_IND_CUR_5MA | LED_INDICATOR_PWM_ENA));
1321 if (ret) {
1322 dev_err(di->dev, "Power ON LED failed\n");
1323 return ret;
1324 }
1325
1326 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1327 AB8500_LED_INDICATOR_PWM_DUTY,
1328 LED_INDICATOR_PWM_DUTY_252_256);
1329 if (ret) {
1330 dev_err(di->dev, "Set LED PWM duty cycle failed\n");
1331 return ret;
1332 }
1333 } else {
1334
1335 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1336 AB8500_LED_INDICATOR_PWM_CTRL,
1337 LED_INDICATOR_PWM_DIS);
1338 if (ret) {
1339 dev_err(di->dev, "Power-off LED failed\n");
1340 return ret;
1341 }
1342 }
1343
1344 return ret;
1345}
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357static int ab8500_charger_ac_en(struct ux500_charger *charger,
1358 int enable, int vset, int iset)
1359{
1360 int ret;
1361 int volt_index;
1362 int curr_index;
1363 int input_curr_index;
1364 u8 overshoot = 0;
1365
1366 struct ab8500_charger *di = to_ab8500_charger_ac_device_info(charger);
1367
1368 if (enable) {
1369
1370 if (!di->ac.charger_connected) {
1371 dev_err(di->dev, "AC charger not connected\n");
1372 return -ENXIO;
1373 }
1374
1375
1376 dev_dbg(di->dev, "Enable AC: %dmV %dmA\n", vset, iset);
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388 if (!di->vddadc_en_ac) {
1389 ret = regulator_enable(di->regu);
1390 if (ret)
1391 dev_warn(di->dev,
1392 "Failed to enable regulator\n");
1393 else
1394 di->vddadc_en_ac = true;
1395 }
1396
1397
1398 volt_index = ab8500_voltage_to_regval(vset);
1399 curr_index = ab8500_current_to_regval(di, iset);
1400 input_curr_index = ab8500_current_to_regval(di,
1401 di->bm->chg_params->ac_curr_max);
1402 if (volt_index < 0 || curr_index < 0 || input_curr_index < 0) {
1403 dev_err(di->dev,
1404 "Charger voltage or current too high, "
1405 "charging not started\n");
1406 return -ENXIO;
1407 }
1408
1409
1410 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1411 AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1412 if (ret) {
1413 dev_err(di->dev, "%s write failed\n", __func__);
1414 return ret;
1415 }
1416
1417 ret = ab8500_charger_set_main_in_curr(di,
1418 di->bm->chg_params->ac_curr_max);
1419 if (ret) {
1420 dev_err(di->dev, "%s Failed to set MainChInputCurr\n",
1421 __func__);
1422 return ret;
1423 }
1424
1425 ret = ab8500_charger_set_output_curr(di, iset);
1426 if (ret) {
1427 dev_err(di->dev, "%s "
1428 "Failed to set ChOutputCurentLevel\n",
1429 __func__);
1430 return ret;
1431 }
1432
1433
1434 if (!di->bm->enable_overshoot)
1435 overshoot = MAIN_CH_NO_OVERSHOOT_ENA_N;
1436
1437
1438 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1439 AB8500_MCH_CTRL1, MAIN_CH_ENA | overshoot);
1440 if (ret) {
1441 dev_err(di->dev, "%s write failed\n", __func__);
1442 return ret;
1443 }
1444
1445
1446 ret = ab8500_charger_led_en(di, true);
1447 if (ret < 0)
1448 dev_err(di->dev, "failed to enable LED\n");
1449
1450 di->ac.charger_online = 1;
1451 } else {
1452
1453 if (is_ab8500_1p1_or_earlier(di->parent)) {
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464 if (di->ac_conn) {
1465 queue_delayed_work(di->charger_wq,
1466 &di->kick_wd_work,
1467 round_jiffies(WD_KICK_INTERVAL));
1468 }
1469
1470
1471
1472
1473
1474
1475
1476
1477 ret = abx500_set_register_interruptible(di->dev,
1478 AB8500_CHARGER,
1479 AB8500_CH_VOLT_LVL_REG, CH_VOL_LVL_3P5);
1480 if (ret) {
1481 dev_err(di->dev,
1482 "%s write failed\n", __func__);
1483 return ret;
1484 }
1485
1486 ret = ab8500_charger_set_output_curr(di, 0);
1487 if (ret) {
1488 dev_err(di->dev, "%s "
1489 "Failed to set ChOutputCurentLevel\n",
1490 __func__);
1491 return ret;
1492 }
1493 } else {
1494 ret = abx500_set_register_interruptible(di->dev,
1495 AB8500_CHARGER,
1496 AB8500_MCH_CTRL1, 0);
1497 if (ret) {
1498 dev_err(di->dev,
1499 "%s write failed\n", __func__);
1500 return ret;
1501 }
1502 }
1503
1504 ret = ab8500_charger_led_en(di, false);
1505 if (ret < 0)
1506 dev_err(di->dev, "failed to disable LED\n");
1507
1508 di->ac.charger_online = 0;
1509 di->ac.wd_expired = false;
1510
1511
1512 if (di->vddadc_en_ac) {
1513 regulator_disable(di->regu);
1514 di->vddadc_en_ac = false;
1515 }
1516
1517 dev_dbg(di->dev, "%s Disabled AC charging\n", __func__);
1518 }
1519 ab8500_power_supply_changed(di, &di->ac_chg.psy);
1520
1521 return ret;
1522}
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534static int ab8500_charger_usb_en(struct ux500_charger *charger,
1535 int enable, int vset, int ich_out)
1536{
1537 int ret;
1538 int volt_index;
1539 int curr_index;
1540 u8 overshoot = 0;
1541
1542 struct ab8500_charger *di = to_ab8500_charger_usb_device_info(charger);
1543
1544 if (enable) {
1545
1546 if (!di->usb.charger_connected) {
1547 dev_err(di->dev, "USB charger not connected\n");
1548 return -ENXIO;
1549 }
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561 if (!di->vddadc_en_usb) {
1562 ret = regulator_enable(di->regu);
1563 if (ret)
1564 dev_warn(di->dev,
1565 "Failed to enable regulator\n");
1566 else
1567 di->vddadc_en_usb = true;
1568 }
1569
1570
1571 dev_dbg(di->dev, "Enable USB: %dmV %dmA\n", vset, ich_out);
1572
1573
1574 volt_index = ab8500_voltage_to_regval(vset);
1575 curr_index = ab8500_current_to_regval(di, ich_out);
1576 if (volt_index < 0 || curr_index < 0) {
1577 dev_err(di->dev,
1578 "Charger voltage or current too high, "
1579 "charging not started\n");
1580 return -ENXIO;
1581 }
1582
1583
1584 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1585 AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1586 if (ret) {
1587 dev_err(di->dev, "%s write failed\n", __func__);
1588 return ret;
1589 }
1590
1591 if (!di->bm->enable_overshoot)
1592 overshoot = USB_CHG_NO_OVERSHOOT_ENA_N;
1593
1594
1595 dev_dbg(di->dev,
1596 "Enabling USB with write to AB8500_USBCH_CTRL1_REG\n");
1597 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1598 AB8500_USBCH_CTRL1_REG, USB_CH_ENA | overshoot);
1599 if (ret) {
1600 dev_err(di->dev, "%s write failed\n", __func__);
1601 return ret;
1602 }
1603
1604
1605 ret = ab8500_charger_led_en(di, true);
1606 if (ret < 0)
1607 dev_err(di->dev, "failed to enable LED\n");
1608
1609 di->usb.charger_online = 1;
1610
1611
1612 ret = ab8500_charger_set_vbus_in_curr(di,
1613 di->max_usb_in_curr.usb_type_max);
1614 if (ret) {
1615 dev_err(di->dev, "setting USBChInputCurr failed\n");
1616 return ret;
1617 }
1618
1619
1620 ret = ab8500_charger_set_output_curr(di, ich_out);
1621 if (ret) {
1622 dev_err(di->dev, "%s "
1623 "Failed to set ChOutputCurentLevel\n",
1624 __func__);
1625 return ret;
1626 }
1627
1628 queue_delayed_work(di->charger_wq, &di->check_vbat_work, HZ);
1629
1630 } else {
1631
1632 dev_dbg(di->dev, "%s Disabled USB charging\n", __func__);
1633 ret = abx500_set_register_interruptible(di->dev,
1634 AB8500_CHARGER,
1635 AB8500_USBCH_CTRL1_REG, 0);
1636 if (ret) {
1637 dev_err(di->dev,
1638 "%s write failed\n", __func__);
1639 return ret;
1640 }
1641
1642 ret = ab8500_charger_led_en(di, false);
1643 if (ret < 0)
1644 dev_err(di->dev, "failed to disable LED\n");
1645
1646 ret = ab8500_charger_set_vbus_in_curr(di, 0);
1647 if (ret) {
1648 dev_err(di->dev, "setting USBChInputCurr failed\n");
1649 return ret;
1650 }
1651
1652
1653 ret = ab8500_charger_set_output_curr(di, 0);
1654 if (ret) {
1655 dev_err(di->dev, "%s "
1656 "Failed to reset ChOutputCurentLevel\n",
1657 __func__);
1658 return ret;
1659 }
1660 di->usb.charger_online = 0;
1661 di->usb.wd_expired = false;
1662
1663
1664 if (di->vddadc_en_usb) {
1665 regulator_disable(di->regu);
1666 di->vddadc_en_usb = false;
1667 }
1668
1669 dev_dbg(di->dev, "%s Disabled USB charging\n", __func__);
1670
1671
1672 cancel_delayed_work(&di->check_vbat_work);
1673
1674 }
1675 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1676
1677 return ret;
1678}
1679
1680static int ab8500_external_charger_prepare(struct notifier_block *charger_nb,
1681 unsigned long event, void *data)
1682{
1683 int ret;
1684 struct device *dev = data;
1685
1686 ret = abx500_set_register_interruptible(dev, AB8500_SYS_CTRL1_BLOCK,
1687 AB8500_SYS_CHARGER_CONTROL_REG,
1688 EXTERNAL_CHARGER_DISABLE_REG_VAL);
1689 if (ret < 0) {
1690 dev_err(dev, "write reg failed %d\n", ret);
1691 goto out;
1692 }
1693 ret = abx500_set_register_interruptible(dev, AB8500_SYS_CTRL1_BLOCK,
1694 AB8500_SYS_CHARGER_CONTROL_REG,
1695 EXTERNAL_CHARGER_ENABLE_REG_VAL);
1696 if (ret < 0)
1697 dev_err(dev, "Write reg failed %d\n", ret);
1698
1699out:
1700 return ret;
1701}
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712static int ab8500_charger_usb_check_enable(struct ux500_charger *charger,
1713 int vset, int iset)
1714{
1715 u8 usbch_ctrl1 = 0;
1716 int ret = 0;
1717
1718 struct ab8500_charger *di = to_ab8500_charger_usb_device_info(charger);
1719
1720 if (!di->usb.charger_connected)
1721 return ret;
1722
1723 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1724 AB8500_USBCH_CTRL1_REG, &usbch_ctrl1);
1725 if (ret < 0) {
1726 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
1727 return ret;
1728 }
1729 dev_dbg(di->dev, "USB charger ctrl: 0x%02x\n", usbch_ctrl1);
1730
1731 if (!(usbch_ctrl1 & USB_CH_ENA)) {
1732 dev_info(di->dev, "Charging has been disabled abnormally and will be re-enabled\n");
1733
1734 ret = abx500_mask_and_set_register_interruptible(di->dev,
1735 AB8500_CHARGER, AB8500_CHARGER_CTRL,
1736 DROP_COUNT_RESET, DROP_COUNT_RESET);
1737 if (ret < 0) {
1738 dev_err(di->dev, "ab8500 write failed %d\n", __LINE__);
1739 return ret;
1740 }
1741
1742 ret = ab8500_charger_usb_en(&di->usb_chg, true, vset, iset);
1743 if (ret < 0) {
1744 dev_err(di->dev, "Failed to enable VBUS charger %d\n",
1745 __LINE__);
1746 return ret;
1747 }
1748 }
1749 return ret;
1750}
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761static int ab8500_charger_ac_check_enable(struct ux500_charger *charger,
1762 int vset, int iset)
1763{
1764 u8 mainch_ctrl1 = 0;
1765 int ret = 0;
1766
1767 struct ab8500_charger *di = to_ab8500_charger_ac_device_info(charger);
1768
1769 if (!di->ac.charger_connected)
1770 return ret;
1771
1772 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1773 AB8500_MCH_CTRL1, &mainch_ctrl1);
1774 if (ret < 0) {
1775 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
1776 return ret;
1777 }
1778 dev_dbg(di->dev, "AC charger ctrl: 0x%02x\n", mainch_ctrl1);
1779
1780 if (!(mainch_ctrl1 & MAIN_CH_ENA)) {
1781 dev_info(di->dev, "Charging has been disabled abnormally and will be re-enabled\n");
1782
1783 ret = abx500_mask_and_set_register_interruptible(di->dev,
1784 AB8500_CHARGER, AB8500_CHARGER_CTRL,
1785 DROP_COUNT_RESET, DROP_COUNT_RESET);
1786
1787 if (ret < 0) {
1788 dev_err(di->dev, "ab8500 write failed %d\n", __LINE__);
1789 return ret;
1790 }
1791
1792 ret = ab8500_charger_ac_en(&di->usb_chg, true, vset, iset);
1793 if (ret < 0) {
1794 dev_err(di->dev, "failed to enable AC charger %d\n",
1795 __LINE__);
1796 return ret;
1797 }
1798 }
1799 return ret;
1800}
1801
1802
1803
1804
1805
1806
1807
1808
1809static int ab8500_charger_watchdog_kick(struct ux500_charger *charger)
1810{
1811 int ret;
1812 struct ab8500_charger *di;
1813
1814 if (charger->psy.type == POWER_SUPPLY_TYPE_MAINS)
1815 di = to_ab8500_charger_ac_device_info(charger);
1816 else if (charger->psy.type == POWER_SUPPLY_TYPE_USB)
1817 di = to_ab8500_charger_usb_device_info(charger);
1818 else
1819 return -ENXIO;
1820
1821 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1822 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
1823 if (ret)
1824 dev_err(di->dev, "Failed to kick WD!\n");
1825
1826 return ret;
1827}
1828
1829
1830
1831
1832
1833
1834
1835
1836static int ab8500_charger_update_charger_current(struct ux500_charger *charger,
1837 int ich_out)
1838{
1839 int ret;
1840 struct ab8500_charger *di;
1841
1842 if (charger->psy.type == POWER_SUPPLY_TYPE_MAINS)
1843 di = to_ab8500_charger_ac_device_info(charger);
1844 else if (charger->psy.type == POWER_SUPPLY_TYPE_USB)
1845 di = to_ab8500_charger_usb_device_info(charger);
1846 else
1847 return -ENXIO;
1848
1849 ret = ab8500_charger_set_output_curr(di, ich_out);
1850 if (ret) {
1851 dev_err(di->dev, "%s "
1852 "Failed to set ChOutputCurentLevel\n",
1853 __func__);
1854 return ret;
1855 }
1856
1857
1858 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1859 AB8500_CHARGER_CTRL, DROP_COUNT_RESET);
1860 if (ret) {
1861 dev_err(di->dev, "%s write failed\n", __func__);
1862 return ret;
1863 }
1864
1865 return ret;
1866}
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876static int ab8540_charger_power_path_enable(struct ux500_charger *charger,
1877 bool enable)
1878{
1879 int ret;
1880 struct ab8500_charger *di;
1881
1882 if (charger->psy.type == POWER_SUPPLY_TYPE_USB)
1883 di = to_ab8500_charger_usb_device_info(charger);
1884 else
1885 return -ENXIO;
1886
1887 ret = abx500_mask_and_set_register_interruptible(di->dev,
1888 AB8500_CHARGER, AB8540_USB_PP_MODE_REG,
1889 BUS_POWER_PATH_MODE_ENA, enable);
1890 if (ret) {
1891 dev_err(di->dev, "%s write failed\n", __func__);
1892 return ret;
1893 }
1894
1895 return ret;
1896}
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907static int ab8540_charger_usb_pre_chg_enable(struct ux500_charger *charger,
1908 bool enable)
1909{
1910 int ret;
1911 struct ab8500_charger *di;
1912
1913 if (charger->psy.type == POWER_SUPPLY_TYPE_USB)
1914 di = to_ab8500_charger_usb_device_info(charger);
1915 else
1916 return -ENXIO;
1917
1918 ret = abx500_mask_and_set_register_interruptible(di->dev,
1919 AB8500_CHARGER, AB8540_USB_PP_CHR_REG,
1920 BUS_POWER_PATH_PRECHG_ENA, enable);
1921 if (ret) {
1922 dev_err(di->dev, "%s write failed\n", __func__);
1923 return ret;
1924 }
1925
1926 return ret;
1927}
1928
1929static int ab8500_charger_get_ext_psy_data(struct device *dev, void *data)
1930{
1931 struct power_supply *psy;
1932 struct power_supply *ext;
1933 struct ab8500_charger *di;
1934 union power_supply_propval ret;
1935 int i, j;
1936 bool psy_found = false;
1937 struct ux500_charger *usb_chg;
1938
1939 usb_chg = (struct ux500_charger *)data;
1940 psy = &usb_chg->psy;
1941
1942 di = to_ab8500_charger_usb_device_info(usb_chg);
1943
1944 ext = dev_get_drvdata(dev);
1945
1946
1947 for (i = 0; i < ext->num_supplicants; i++) {
1948 if (!strcmp(ext->supplied_to[i], psy->name))
1949 psy_found = true;
1950 }
1951
1952 if (!psy_found)
1953 return 0;
1954
1955
1956 for (j = 0; j < ext->num_properties; j++) {
1957 enum power_supply_property prop;
1958 prop = ext->properties[j];
1959
1960 if (ext->get_property(ext, prop, &ret))
1961 continue;
1962
1963 switch (prop) {
1964 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1965 switch (ext->type) {
1966 case POWER_SUPPLY_TYPE_BATTERY:
1967 di->vbat = ret.intval / 1000;
1968 break;
1969 default:
1970 break;
1971 }
1972 break;
1973 default:
1974 break;
1975 }
1976 }
1977 return 0;
1978}
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989static void ab8500_charger_check_vbat_work(struct work_struct *work)
1990{
1991 int t = 10;
1992 struct ab8500_charger *di = container_of(work,
1993 struct ab8500_charger, check_vbat_work.work);
1994
1995 class_for_each_device(power_supply_class, NULL,
1996 &di->usb_chg.psy, ab8500_charger_get_ext_psy_data);
1997
1998
1999 if (di->old_vbat == 0)
2000 di->old_vbat = di->vbat;
2001
2002 if (!((di->old_vbat <= VBAT_TRESH_IP_CUR_RED &&
2003 di->vbat <= VBAT_TRESH_IP_CUR_RED) ||
2004 (di->old_vbat > VBAT_TRESH_IP_CUR_RED &&
2005 di->vbat > VBAT_TRESH_IP_CUR_RED))) {
2006
2007 dev_dbg(di->dev, "Vbat did cross threshold, curr: %d, new: %d,"
2008 " old: %d\n", di->max_usb_in_curr.usb_type_max,
2009 di->vbat, di->old_vbat);
2010 ab8500_charger_set_vbus_in_curr(di,
2011 di->max_usb_in_curr.usb_type_max);
2012 power_supply_changed(&di->usb_chg.psy);
2013 }
2014
2015 di->old_vbat = di->vbat;
2016
2017
2018
2019
2020
2021 if (di->vbat < (VBAT_TRESH_IP_CUR_RED + 100) &&
2022 (di->vbat > (VBAT_TRESH_IP_CUR_RED - 100)))
2023 t = 1;
2024
2025 queue_delayed_work(di->charger_wq, &di->check_vbat_work, t * HZ);
2026}
2027
2028
2029
2030
2031
2032
2033
2034static void ab8500_charger_check_hw_failure_work(struct work_struct *work)
2035{
2036 int ret;
2037 u8 reg_value;
2038
2039 struct ab8500_charger *di = container_of(work,
2040 struct ab8500_charger, check_hw_failure_work.work);
2041
2042
2043 if (di->flags.mainextchnotok) {
2044 ret = abx500_get_register_interruptible(di->dev,
2045 AB8500_CHARGER, AB8500_CH_STATUS2_REG, ®_value);
2046 if (ret < 0) {
2047 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2048 return;
2049 }
2050 if (!(reg_value & MAIN_CH_NOK)) {
2051 di->flags.mainextchnotok = false;
2052 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2053 }
2054 }
2055 if (di->flags.vbus_ovv) {
2056 ret = abx500_get_register_interruptible(di->dev,
2057 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG,
2058 ®_value);
2059 if (ret < 0) {
2060 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2061 return;
2062 }
2063 if (!(reg_value & VBUS_OVV_TH)) {
2064 di->flags.vbus_ovv = false;
2065 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2066 }
2067 }
2068
2069 if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
2070 queue_delayed_work(di->charger_wq,
2071 &di->check_hw_failure_work, round_jiffies(HZ));
2072 }
2073}
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089static void ab8500_charger_kick_watchdog_work(struct work_struct *work)
2090{
2091 int ret;
2092
2093 struct ab8500_charger *di = container_of(work,
2094 struct ab8500_charger, kick_wd_work.work);
2095
2096 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
2097 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
2098 if (ret)
2099 dev_err(di->dev, "Failed to kick WD!\n");
2100
2101
2102 queue_delayed_work(di->charger_wq,
2103 &di->kick_wd_work, round_jiffies(WD_KICK_INTERVAL));
2104}
2105
2106
2107
2108
2109
2110
2111
2112static void ab8500_charger_ac_work(struct work_struct *work)
2113{
2114 int ret;
2115
2116 struct ab8500_charger *di = container_of(work,
2117 struct ab8500_charger, ac_work);
2118
2119
2120
2121
2122
2123
2124 ret = ab8500_charger_detect_chargers(di, false);
2125 if (ret < 0)
2126 return;
2127
2128 if (ret & AC_PW_CONN) {
2129 di->ac.charger_connected = 1;
2130 di->ac_conn = true;
2131 } else {
2132 di->ac.charger_connected = 0;
2133 }
2134
2135 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2136 sysfs_notify(&di->ac_chg.psy.dev->kobj, NULL, "present");
2137}
2138
2139static void ab8500_charger_usb_attached_work(struct work_struct *work)
2140{
2141 struct ab8500_charger *di = container_of(work,
2142 struct ab8500_charger,
2143 usb_charger_attached_work.work);
2144 int usbch = (USB_CH_VBUSDROP | USB_CH_VBUSDETDBNC);
2145 int ret, i;
2146 u8 statval;
2147
2148 for (i = 0; i < 10; i++) {
2149 ret = abx500_get_register_interruptible(di->dev,
2150 AB8500_CHARGER,
2151 AB8500_CH_USBCH_STAT1_REG,
2152 &statval);
2153 if (ret < 0) {
2154 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
2155 goto reschedule;
2156 }
2157 if ((statval & usbch) != usbch)
2158 goto reschedule;
2159
2160 msleep(CHARGER_STATUS_POLL);
2161 }
2162
2163 ab8500_charger_usb_en(&di->usb_chg, 0, 0, 0);
2164
2165 mutex_lock(&di->charger_attached_mutex);
2166 mutex_unlock(&di->charger_attached_mutex);
2167
2168 return;
2169
2170reschedule:
2171 queue_delayed_work(di->charger_wq,
2172 &di->usb_charger_attached_work,
2173 HZ);
2174}
2175
2176static void ab8500_charger_ac_attached_work(struct work_struct *work)
2177{
2178
2179 struct ab8500_charger *di = container_of(work,
2180 struct ab8500_charger,
2181 ac_charger_attached_work.work);
2182 int mainch = (MAIN_CH_STATUS2_MAINCHGDROP |
2183 MAIN_CH_STATUS2_MAINCHARGERDETDBNC);
2184 int ret, i;
2185 u8 statval;
2186
2187 for (i = 0; i < 10; i++) {
2188 ret = abx500_get_register_interruptible(di->dev,
2189 AB8500_CHARGER,
2190 AB8500_CH_STATUS2_REG,
2191 &statval);
2192 if (ret < 0) {
2193 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
2194 goto reschedule;
2195 }
2196
2197 if ((statval & mainch) != mainch)
2198 goto reschedule;
2199
2200 msleep(CHARGER_STATUS_POLL);
2201 }
2202
2203 ab8500_charger_ac_en(&di->ac_chg, 0, 0, 0);
2204 queue_work(di->charger_wq, &di->ac_work);
2205
2206 mutex_lock(&di->charger_attached_mutex);
2207 mutex_unlock(&di->charger_attached_mutex);
2208
2209 return;
2210
2211reschedule:
2212 queue_delayed_work(di->charger_wq,
2213 &di->ac_charger_attached_work,
2214 HZ);
2215}
2216
2217
2218
2219
2220
2221
2222
2223static void ab8500_charger_detect_usb_type_work(struct work_struct *work)
2224{
2225 int ret;
2226
2227 struct ab8500_charger *di = container_of(work,
2228 struct ab8500_charger, detect_usb_type_work);
2229
2230
2231
2232
2233
2234
2235 ret = ab8500_charger_detect_chargers(di, false);
2236 if (ret < 0)
2237 return;
2238
2239 if (!(ret & USB_PW_CONN)) {
2240 dev_dbg(di->dev, "%s di->vbus_detected = false\n", __func__);
2241 di->vbus_detected = false;
2242 ab8500_charger_set_usb_connected(di, false);
2243 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2244 } else {
2245 dev_dbg(di->dev, "%s di->vbus_detected = true\n", __func__);
2246 di->vbus_detected = true;
2247
2248 if (is_ab8500_1p1_or_earlier(di->parent)) {
2249 ret = ab8500_charger_detect_usb_type(di);
2250 if (!ret) {
2251 ab8500_charger_set_usb_connected(di, true);
2252 ab8500_power_supply_changed(di,
2253 &di->usb_chg.psy);
2254 }
2255 } else {
2256
2257
2258
2259
2260
2261
2262
2263 if (di->vbus_detected_start) {
2264 di->vbus_detected_start = false;
2265 ret = ab8500_charger_detect_usb_type(di);
2266 if (!ret) {
2267 ab8500_charger_set_usb_connected(di,
2268 true);
2269 ab8500_power_supply_changed(di,
2270 &di->usb_chg.psy);
2271 }
2272 }
2273 }
2274 }
2275}
2276
2277
2278
2279
2280
2281
2282
2283static void ab8500_charger_usb_link_attach_work(struct work_struct *work)
2284{
2285 struct ab8500_charger *di =
2286 container_of(work, struct ab8500_charger, attach_work.work);
2287 int ret;
2288
2289
2290 if (!di->usb.charger_online) {
2291 ret = ab8500_charger_set_vbus_in_curr(di,
2292 di->max_usb_in_curr.usb_type_max);
2293 if (ret)
2294 return;
2295 }
2296
2297 ab8500_charger_set_usb_connected(di, true);
2298 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2299}
2300
2301
2302
2303
2304
2305
2306
2307static void ab8500_charger_usb_link_status_work(struct work_struct *work)
2308{
2309 int detected_chargers;
2310 int ret;
2311 u8 val;
2312 u8 link_status;
2313
2314 struct ab8500_charger *di = container_of(work,
2315 struct ab8500_charger, usb_link_status_work);
2316
2317
2318
2319
2320
2321
2322 detected_chargers = ab8500_charger_detect_chargers(di, false);
2323 if (detected_chargers < 0)
2324 return;
2325
2326
2327
2328
2329
2330
2331
2332 if (is_ab8500(di->parent))
2333 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
2334 AB8500_USB_LINE_STAT_REG, &val);
2335 else
2336 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
2337 AB8500_USB_LINK1_STAT_REG, &val);
2338
2339 if (ret >= 0)
2340 dev_dbg(di->dev, "UsbLineStatus register = 0x%02x\n", val);
2341 else
2342 dev_dbg(di->dev, "Error reading USB link status\n");
2343
2344 if (is_ab8500(di->parent))
2345 link_status = AB8500_USB_LINK_STATUS;
2346 else
2347 link_status = AB8505_USB_LINK_STATUS;
2348
2349 if (detected_chargers & USB_PW_CONN) {
2350 if (((val & link_status) >> USB_LINK_STATUS_SHIFT) ==
2351 USB_STAT_NOT_VALID_LINK &&
2352 di->invalid_charger_detect_state == 0) {
2353 dev_dbg(di->dev,
2354 "Invalid charger detected, state= 0\n");
2355
2356 abx500_mask_and_set_register_interruptible(di->dev,
2357 AB8500_CHARGER, AB8500_USBCH_CTRL1_REG,
2358 USB_CH_ENA, USB_CH_ENA);
2359
2360 abx500_mask_and_set_register_interruptible(di->dev,
2361 AB8500_USB, AB8500_USB_LINE_CTRL2_REG,
2362 USB_CH_DET, USB_CH_DET);
2363 di->invalid_charger_detect_state = 1;
2364
2365 return;
2366
2367 }
2368 if (di->invalid_charger_detect_state == 1) {
2369 dev_dbg(di->dev,
2370 "Invalid charger detected, state= 1\n");
2371
2372 abx500_mask_and_set_register_interruptible(di->dev,
2373 AB8500_USB, AB8500_USB_LINE_CTRL2_REG,
2374 USB_CH_DET, 0x00);
2375
2376 if (is_ab8500(di->parent))
2377 ret = abx500_get_register_interruptible(di->dev,
2378 AB8500_USB, AB8500_USB_LINE_STAT_REG,
2379 &val);
2380 else
2381 ret = abx500_get_register_interruptible(di->dev,
2382 AB8500_USB, AB8500_USB_LINK1_STAT_REG,
2383 &val);
2384
2385 dev_dbg(di->dev, "USB link status= 0x%02x\n",
2386 (val & link_status) >> USB_LINK_STATUS_SHIFT);
2387 di->invalid_charger_detect_state = 2;
2388 }
2389 } else {
2390 di->invalid_charger_detect_state = 0;
2391 }
2392
2393 if (!(detected_chargers & USB_PW_CONN)) {
2394 di->vbus_detected = false;
2395 ab8500_charger_set_usb_connected(di, false);
2396 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2397 return;
2398 }
2399
2400 dev_dbg(di->dev,"%s di->vbus_detected = true\n",__func__);
2401 di->vbus_detected = true;
2402 ret = ab8500_charger_read_usb_type(di);
2403 if (ret) {
2404 if (ret == -ENXIO) {
2405
2406 ab8500_charger_set_usb_connected(di, false);
2407 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2408 }
2409 return;
2410 }
2411
2412 if (di->usb_device_is_unrecognised) {
2413 dev_dbg(di->dev,
2414 "Potential Legacy Charger device. "
2415 "Delay work for %d msec for USB enum "
2416 "to finish",
2417 WAIT_ACA_RID_ENUMERATION);
2418 queue_delayed_work(di->charger_wq,
2419 &di->attach_work,
2420 msecs_to_jiffies(WAIT_ACA_RID_ENUMERATION));
2421 } else if (di->is_aca_rid == 1) {
2422
2423 di->is_aca_rid++;
2424 dev_dbg(di->dev,
2425 "%s Wait %d msec for USB enum to finish",
2426 __func__, WAIT_ACA_RID_ENUMERATION);
2427 queue_delayed_work(di->charger_wq,
2428 &di->attach_work,
2429 msecs_to_jiffies(WAIT_ACA_RID_ENUMERATION));
2430 } else {
2431 queue_delayed_work(di->charger_wq,
2432 &di->attach_work,
2433 0);
2434 }
2435}
2436
2437static void ab8500_charger_usb_state_changed_work(struct work_struct *work)
2438{
2439 int ret;
2440 unsigned long flags;
2441
2442 struct ab8500_charger *di = container_of(work,
2443 struct ab8500_charger, usb_state_changed_work.work);
2444
2445 if (!di->vbus_detected) {
2446 dev_dbg(di->dev,
2447 "%s !di->vbus_detected\n",
2448 __func__);
2449 return;
2450 }
2451
2452 spin_lock_irqsave(&di->usb_state.usb_lock, flags);
2453 di->usb_state.state = di->usb_state.state_tmp;
2454 di->usb_state.usb_current = di->usb_state.usb_current_tmp;
2455 spin_unlock_irqrestore(&di->usb_state.usb_lock, flags);
2456
2457 dev_dbg(di->dev, "%s USB state: 0x%02x mA: %d\n",
2458 __func__, di->usb_state.state, di->usb_state.usb_current);
2459
2460 switch (di->usb_state.state) {
2461 case AB8500_BM_USB_STATE_RESET_HS:
2462 case AB8500_BM_USB_STATE_RESET_FS:
2463 case AB8500_BM_USB_STATE_SUSPEND:
2464 case AB8500_BM_USB_STATE_MAX:
2465 ab8500_charger_set_usb_connected(di, false);
2466 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2467 break;
2468
2469 case AB8500_BM_USB_STATE_RESUME:
2470
2471
2472
2473
2474 msleep(1000);
2475
2476 case AB8500_BM_USB_STATE_CONFIGURED:
2477
2478
2479
2480
2481 if (!ab8500_charger_get_usb_cur(di)) {
2482
2483 ret = ab8500_charger_set_vbus_in_curr(di,
2484 di->max_usb_in_curr.usb_type_max);
2485 if (ret)
2486 return;
2487
2488 ab8500_charger_set_usb_connected(di, true);
2489 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2490 }
2491 break;
2492
2493 default:
2494 break;
2495 };
2496}
2497
2498
2499
2500
2501
2502
2503
2504static void ab8500_charger_check_usbchargernotok_work(struct work_struct *work)
2505{
2506 int ret;
2507 u8 reg_value;
2508 bool prev_status;
2509
2510 struct ab8500_charger *di = container_of(work,
2511 struct ab8500_charger, check_usbchgnotok_work.work);
2512
2513
2514 ret = abx500_get_register_interruptible(di->dev,
2515 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, ®_value);
2516 if (ret < 0) {
2517 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2518 return;
2519 }
2520 prev_status = di->flags.usbchargernotok;
2521
2522 if (reg_value & VBUS_CH_NOK) {
2523 di->flags.usbchargernotok = true;
2524
2525 queue_delayed_work(di->charger_wq,
2526 &di->check_usbchgnotok_work, HZ);
2527 } else {
2528 di->flags.usbchargernotok = false;
2529 di->flags.vbus_collapse = false;
2530 }
2531
2532 if (prev_status != di->flags.usbchargernotok)
2533 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2534}
2535
2536
2537
2538
2539
2540
2541
2542static void ab8500_charger_check_main_thermal_prot_work(
2543 struct work_struct *work)
2544{
2545 int ret;
2546 u8 reg_value;
2547
2548 struct ab8500_charger *di = container_of(work,
2549 struct ab8500_charger, check_main_thermal_prot_work);
2550
2551
2552 ret = abx500_get_register_interruptible(di->dev,
2553 AB8500_CHARGER, AB8500_CH_STATUS2_REG, ®_value);
2554 if (ret < 0) {
2555 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2556 return;
2557 }
2558 if (reg_value & MAIN_CH_TH_PROT)
2559 di->flags.main_thermal_prot = true;
2560 else
2561 di->flags.main_thermal_prot = false;
2562
2563 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2564}
2565
2566
2567
2568
2569
2570
2571
2572static void ab8500_charger_check_usb_thermal_prot_work(
2573 struct work_struct *work)
2574{
2575 int ret;
2576 u8 reg_value;
2577
2578 struct ab8500_charger *di = container_of(work,
2579 struct ab8500_charger, check_usb_thermal_prot_work);
2580
2581
2582 ret = abx500_get_register_interruptible(di->dev,
2583 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, ®_value);
2584 if (ret < 0) {
2585 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2586 return;
2587 }
2588 if (reg_value & USB_CH_TH_PROT)
2589 di->flags.usb_thermal_prot = true;
2590 else
2591 di->flags.usb_thermal_prot = false;
2592
2593 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2594}
2595
2596
2597
2598
2599
2600
2601
2602
2603static irqreturn_t ab8500_charger_mainchunplugdet_handler(int irq, void *_di)
2604{
2605 struct ab8500_charger *di = _di;
2606
2607 dev_dbg(di->dev, "Main charger unplugged\n");
2608 queue_work(di->charger_wq, &di->ac_work);
2609
2610 cancel_delayed_work_sync(&di->ac_charger_attached_work);
2611 mutex_lock(&di->charger_attached_mutex);
2612 mutex_unlock(&di->charger_attached_mutex);
2613
2614 return IRQ_HANDLED;
2615}
2616
2617
2618
2619
2620
2621
2622
2623
2624static irqreturn_t ab8500_charger_mainchplugdet_handler(int irq, void *_di)
2625{
2626 struct ab8500_charger *di = _di;
2627
2628 dev_dbg(di->dev, "Main charger plugged\n");
2629 queue_work(di->charger_wq, &di->ac_work);
2630
2631 mutex_lock(&di->charger_attached_mutex);
2632 mutex_unlock(&di->charger_attached_mutex);
2633
2634 if (is_ab8500(di->parent))
2635 queue_delayed_work(di->charger_wq,
2636 &di->ac_charger_attached_work,
2637 HZ);
2638 return IRQ_HANDLED;
2639}
2640
2641
2642
2643
2644
2645
2646
2647
2648static irqreturn_t ab8500_charger_mainextchnotok_handler(int irq, void *_di)
2649{
2650 struct ab8500_charger *di = _di;
2651
2652 dev_dbg(di->dev, "Main charger not ok\n");
2653 di->flags.mainextchnotok = true;
2654 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2655
2656
2657 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
2658
2659 return IRQ_HANDLED;
2660}
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670static irqreturn_t ab8500_charger_mainchthprotr_handler(int irq, void *_di)
2671{
2672 struct ab8500_charger *di = _di;
2673
2674 dev_dbg(di->dev,
2675 "Die temp above Main charger thermal protection threshold\n");
2676 queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
2677
2678 return IRQ_HANDLED;
2679}
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689static irqreturn_t ab8500_charger_mainchthprotf_handler(int irq, void *_di)
2690{
2691 struct ab8500_charger *di = _di;
2692
2693 dev_dbg(di->dev,
2694 "Die temp ok for Main charger thermal protection threshold\n");
2695 queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
2696
2697 return IRQ_HANDLED;
2698}
2699
2700static void ab8500_charger_vbus_drop_end_work(struct work_struct *work)
2701{
2702 struct ab8500_charger *di = container_of(work,
2703 struct ab8500_charger, vbus_drop_end_work.work);
2704 int ret, curr;
2705 u8 reg_value;
2706
2707 di->flags.vbus_drop_end = false;
2708
2709
2710 abx500_set_register_interruptible(di->dev,
2711 AB8500_CHARGER, AB8500_CHARGER_CTRL, 0x01);
2712
2713 if (is_ab8540(di->parent))
2714 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
2715 AB8540_CH_USBCH_STAT3_REG, ®_value);
2716 else
2717 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
2718 AB8500_CH_USBCH_STAT2_REG, ®_value);
2719 if (ret < 0) {
2720 dev_err(di->dev, "%s read failed\n", __func__);
2721 return;
2722 }
2723
2724 if (is_ab8540(di->parent))
2725 curr = di->bm->chg_input_curr[
2726 reg_value & AB8540_AUTO_VBUS_IN_CURR_MASK];
2727 else
2728 curr = di->bm->chg_input_curr[
2729 reg_value >> AUTO_VBUS_IN_CURR_LIM_SHIFT];
2730
2731 if (di->max_usb_in_curr.calculated_max != curr) {
2732
2733 di->max_usb_in_curr.calculated_max = curr;
2734 dev_dbg(di->dev,
2735 "VBUS input current limiting to %d mA\n",
2736 di->max_usb_in_curr.calculated_max);
2737 } else {
2738
2739
2740
2741
2742 di->max_usb_in_curr.set_max =
2743 di->max_usb_in_curr.calculated_max;
2744 dev_dbg(di->dev,
2745 "VBUS input current limited to %d mA\n",
2746 di->max_usb_in_curr.set_max);
2747 }
2748
2749 if (di->usb.charger_connected)
2750 ab8500_charger_set_vbus_in_curr(di,
2751 di->max_usb_in_curr.usb_type_max);
2752}
2753
2754
2755
2756
2757
2758
2759
2760
2761static irqreturn_t ab8500_charger_vbusdetf_handler(int irq, void *_di)
2762{
2763 struct ab8500_charger *di = _di;
2764
2765 di->vbus_detected = false;
2766 dev_dbg(di->dev, "VBUS falling detected\n");
2767 queue_work(di->charger_wq, &di->detect_usb_type_work);
2768
2769 return IRQ_HANDLED;
2770}
2771
2772
2773
2774
2775
2776
2777
2778
2779static irqreturn_t ab8500_charger_vbusdetr_handler(int irq, void *_di)
2780{
2781 struct ab8500_charger *di = _di;
2782
2783 di->vbus_detected = true;
2784 dev_dbg(di->dev, "VBUS rising detected\n");
2785
2786 queue_work(di->charger_wq, &di->detect_usb_type_work);
2787
2788 return IRQ_HANDLED;
2789}
2790
2791
2792
2793
2794
2795
2796
2797
2798static irqreturn_t ab8500_charger_usblinkstatus_handler(int irq, void *_di)
2799{
2800 struct ab8500_charger *di = _di;
2801
2802 dev_dbg(di->dev, "USB link status changed\n");
2803
2804 queue_work(di->charger_wq, &di->usb_link_status_work);
2805
2806 return IRQ_HANDLED;
2807}
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817static irqreturn_t ab8500_charger_usbchthprotr_handler(int irq, void *_di)
2818{
2819 struct ab8500_charger *di = _di;
2820
2821 dev_dbg(di->dev,
2822 "Die temp above USB charger thermal protection threshold\n");
2823 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2824
2825 return IRQ_HANDLED;
2826}
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836static irqreturn_t ab8500_charger_usbchthprotf_handler(int irq, void *_di)
2837{
2838 struct ab8500_charger *di = _di;
2839
2840 dev_dbg(di->dev,
2841 "Die temp ok for USB charger thermal protection threshold\n");
2842 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2843
2844 return IRQ_HANDLED;
2845}
2846
2847
2848
2849
2850
2851
2852
2853
2854static irqreturn_t ab8500_charger_usbchargernotokr_handler(int irq, void *_di)
2855{
2856 struct ab8500_charger *di = _di;
2857
2858 dev_dbg(di->dev, "Not allowed USB charger detected\n");
2859 queue_delayed_work(di->charger_wq, &di->check_usbchgnotok_work, 0);
2860
2861 return IRQ_HANDLED;
2862}
2863
2864
2865
2866
2867
2868
2869
2870
2871static irqreturn_t ab8500_charger_chwdexp_handler(int irq, void *_di)
2872{
2873 struct ab8500_charger *di = _di;
2874
2875 dev_dbg(di->dev, "Charger watchdog expired\n");
2876
2877
2878
2879
2880
2881 if (di->ac.charger_online) {
2882 di->ac.wd_expired = true;
2883 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2884 }
2885 if (di->usb.charger_online) {
2886 di->usb.wd_expired = true;
2887 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2888 }
2889
2890 return IRQ_HANDLED;
2891}
2892
2893
2894
2895
2896
2897
2898
2899
2900static irqreturn_t ab8500_charger_vbuschdropend_handler(int irq, void *_di)
2901{
2902 struct ab8500_charger *di = _di;
2903
2904 dev_dbg(di->dev, "VBUS charger drop ended\n");
2905 di->flags.vbus_drop_end = true;
2906
2907
2908
2909
2910
2911 queue_delayed_work(di->charger_wq, &di->vbus_drop_end_work,
2912 round_jiffies(VBUS_IN_CURR_LIM_RETRY_SET_TIME * HZ));
2913
2914 return IRQ_HANDLED;
2915}
2916
2917
2918
2919
2920
2921
2922
2923
2924static irqreturn_t ab8500_charger_vbusovv_handler(int irq, void *_di)
2925{
2926 struct ab8500_charger *di = _di;
2927
2928 dev_dbg(di->dev, "VBUS overvoltage detected\n");
2929 di->flags.vbus_ovv = true;
2930 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2931
2932
2933 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
2934
2935 return IRQ_HANDLED;
2936}
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952static int ab8500_charger_ac_get_property(struct power_supply *psy,
2953 enum power_supply_property psp,
2954 union power_supply_propval *val)
2955{
2956 struct ab8500_charger *di;
2957 int ret;
2958
2959 di = to_ab8500_charger_ac_device_info(psy_to_ux500_charger(psy));
2960
2961 switch (psp) {
2962 case POWER_SUPPLY_PROP_HEALTH:
2963 if (di->flags.mainextchnotok)
2964 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
2965 else if (di->ac.wd_expired || di->usb.wd_expired)
2966 val->intval = POWER_SUPPLY_HEALTH_DEAD;
2967 else if (di->flags.main_thermal_prot)
2968 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
2969 else
2970 val->intval = POWER_SUPPLY_HEALTH_GOOD;
2971 break;
2972 case POWER_SUPPLY_PROP_ONLINE:
2973 val->intval = di->ac.charger_online;
2974 break;
2975 case POWER_SUPPLY_PROP_PRESENT:
2976 val->intval = di->ac.charger_connected;
2977 break;
2978 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
2979 ret = ab8500_charger_get_ac_voltage(di);
2980 if (ret >= 0)
2981 di->ac.charger_voltage = ret;
2982
2983 val->intval = di->ac.charger_voltage * 1000;
2984 break;
2985 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
2986
2987
2988
2989
2990 di->ac.cv_active = ab8500_charger_ac_cv(di);
2991 val->intval = di->ac.cv_active;
2992 break;
2993 case POWER_SUPPLY_PROP_CURRENT_NOW:
2994 ret = ab8500_charger_get_ac_current(di);
2995 if (ret >= 0)
2996 di->ac.charger_current = ret;
2997 val->intval = di->ac.charger_current * 1000;
2998 break;
2999 default:
3000 return -EINVAL;
3001 }
3002 return 0;
3003}
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019static int ab8500_charger_usb_get_property(struct power_supply *psy,
3020 enum power_supply_property psp,
3021 union power_supply_propval *val)
3022{
3023 struct ab8500_charger *di;
3024 int ret;
3025
3026 di = to_ab8500_charger_usb_device_info(psy_to_ux500_charger(psy));
3027
3028 switch (psp) {
3029 case POWER_SUPPLY_PROP_HEALTH:
3030 if (di->flags.usbchargernotok)
3031 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
3032 else if (di->ac.wd_expired || di->usb.wd_expired)
3033 val->intval = POWER_SUPPLY_HEALTH_DEAD;
3034 else if (di->flags.usb_thermal_prot)
3035 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
3036 else if (di->flags.vbus_ovv)
3037 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
3038 else
3039 val->intval = POWER_SUPPLY_HEALTH_GOOD;
3040 break;
3041 case POWER_SUPPLY_PROP_ONLINE:
3042 val->intval = di->usb.charger_online;
3043 break;
3044 case POWER_SUPPLY_PROP_PRESENT:
3045 val->intval = di->usb.charger_connected;
3046 break;
3047 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
3048 ret = ab8500_charger_get_vbus_voltage(di);
3049 if (ret >= 0)
3050 di->usb.charger_voltage = ret;
3051 val->intval = di->usb.charger_voltage * 1000;
3052 break;
3053 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
3054
3055
3056
3057
3058 di->usb.cv_active = ab8500_charger_usb_cv(di);
3059 val->intval = di->usb.cv_active;
3060 break;
3061 case POWER_SUPPLY_PROP_CURRENT_NOW:
3062 ret = ab8500_charger_get_usb_current(di);
3063 if (ret >= 0)
3064 di->usb.charger_current = ret;
3065 val->intval = di->usb.charger_current * 1000;
3066 break;
3067 case POWER_SUPPLY_PROP_CURRENT_AVG:
3068
3069
3070
3071
3072 if (di->flags.vbus_collapse)
3073 val->intval = 1;
3074 else
3075 val->intval = 0;
3076 break;
3077 default:
3078 return -EINVAL;
3079 }
3080 return 0;
3081}
3082
3083
3084
3085
3086
3087
3088
3089
3090static int ab8500_charger_init_hw_registers(struct ab8500_charger *di)
3091{
3092 int ret = 0;
3093 u8 bup_vch_range = 0, vbup33_vrtcn = 0;
3094
3095
3096 if (!is_ab8500_1p1_or_earlier(di->parent)) {
3097 ret = abx500_set_register_interruptible(di->dev,
3098 AB8500_CHARGER,
3099 AB8500_CH_VOLT_LVL_MAX_REG, CH_VOL_LVL_4P6);
3100 if (ret) {
3101 dev_err(di->dev,
3102 "failed to set CH_VOLT_LVL_MAX_REG\n");
3103 goto out;
3104 }
3105
3106 if (is_ab8540(di->parent))
3107 ret = abx500_set_register_interruptible(di->dev,
3108 AB8500_CHARGER, AB8500_CH_OPT_CRNTLVL_MAX_REG,
3109 CH_OP_CUR_LVL_2P);
3110 else
3111 ret = abx500_set_register_interruptible(di->dev,
3112 AB8500_CHARGER, AB8500_CH_OPT_CRNTLVL_MAX_REG,
3113 CH_OP_CUR_LVL_1P6);
3114 if (ret) {
3115 dev_err(di->dev,
3116 "failed to set CH_OPT_CRNTLVL_MAX_REG\n");
3117 goto out;
3118 }
3119 }
3120
3121 if (is_ab9540_2p0(di->parent) || is_ab9540_3p0(di->parent)
3122 || is_ab8505_2p0(di->parent) || is_ab8540(di->parent))
3123 ret = abx500_mask_and_set_register_interruptible(di->dev,
3124 AB8500_CHARGER,
3125 AB8500_USBCH_CTRL2_REG,
3126 VBUS_AUTO_IN_CURR_LIM_ENA,
3127 VBUS_AUTO_IN_CURR_LIM_ENA);
3128 else
3129
3130
3131
3132 ret = abx500_set_register_interruptible(di->dev,
3133 AB8500_CHARGER,
3134 AB8500_USBCH_CTRL2_REG,
3135 VBUS_OVV_SELECT_6P3V | VBUS_AUTO_IN_CURR_LIM_ENA);
3136 if (ret) {
3137 dev_err(di->dev,
3138 "failed to set automatic current limitation\n");
3139 goto out;
3140 }
3141
3142
3143 ret = abx500_set_register_interruptible(di->dev,
3144 AB8500_OTP_EMUL, AB8500_OTP_CONF_15, OTP_ENABLE_WD);
3145 if (ret) {
3146 dev_err(di->dev, "failed to enable main WD in OTP\n");
3147 goto out;
3148 }
3149
3150
3151 ret = abx500_set_register_interruptible(di->dev,
3152 AB8500_SYS_CTRL2_BLOCK,
3153 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_ENA);
3154 if (ret) {
3155 dev_err(di->dev, "faile to enable main watchdog\n");
3156 goto out;
3157 }
3158
3159
3160
3161
3162
3163
3164
3165 udelay(63);
3166
3167
3168 ret = abx500_set_register_interruptible(di->dev,
3169 AB8500_SYS_CTRL2_BLOCK,
3170 AB8500_MAIN_WDOG_CTRL_REG,
3171 (MAIN_WDOG_ENA | MAIN_WDOG_KICK));
3172 if (ret) {
3173 dev_err(di->dev, "failed to kick main watchdog\n");
3174 goto out;
3175 }
3176
3177
3178 ret = abx500_set_register_interruptible(di->dev,
3179 AB8500_SYS_CTRL2_BLOCK,
3180 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_DIS);
3181 if (ret) {
3182 dev_err(di->dev, "failed to disable main watchdog\n");
3183 goto out;
3184 }
3185
3186
3187 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
3188 AB8500_CH_WD_TIMER_REG, WD_TIMER);
3189 if (ret) {
3190 dev_err(di->dev, "failed to set charger watchdog timeout\n");
3191 goto out;
3192 }
3193
3194 ret = ab8500_charger_led_en(di, false);
3195 if (ret < 0) {
3196 dev_err(di->dev, "failed to disable LED\n");
3197 goto out;
3198 }
3199
3200
3201 if (di->bm->bkup_bat_v > BUP_VCH_SEL_3P1V)
3202 bup_vch_range = BUP_VCH_RANGE;
3203 if (di->bm->bkup_bat_v == BUP_VCH_SEL_3P3V)
3204 vbup33_vrtcn = VBUP33_VRTCN;
3205
3206 ret = abx500_set_register_interruptible(di->dev,
3207 AB8500_RTC,
3208 AB8500_RTC_BACKUP_CHG_REG,
3209 (di->bm->bkup_bat_v & 0x3) | di->bm->bkup_bat_i);
3210 if (ret) {
3211 dev_err(di->dev, "failed to setup backup battery charging\n");
3212 goto out;
3213 }
3214 if (is_ab8540(di->parent)) {
3215 ret = abx500_set_register_interruptible(di->dev,
3216 AB8500_RTC,
3217 AB8500_RTC_CTRL1_REG,
3218 bup_vch_range | vbup33_vrtcn);
3219 if (ret) {
3220 dev_err(di->dev,
3221 "failed to setup backup battery charging\n");
3222 goto out;
3223 }
3224 }
3225
3226
3227 abx500_mask_and_set_register_interruptible(di->dev,
3228 AB8500_RTC, AB8500_RTC_CTRL_REG,
3229 RTC_BUP_CH_ENA, RTC_BUP_CH_ENA);
3230 if (ret < 0)
3231 dev_err(di->dev, "%s mask and set failed\n", __func__);
3232
3233 if (is_ab8540(di->parent)) {
3234 ret = abx500_mask_and_set_register_interruptible(di->dev,
3235 AB8500_CHARGER, AB8540_USB_PP_MODE_REG,
3236 BUS_VSYS_VOL_SELECT_MASK, BUS_VSYS_VOL_SELECT_3P6V);
3237 if (ret) {
3238 dev_err(di->dev,
3239 "failed to setup usb power path vsys voltage\n");
3240 goto out;
3241 }
3242 ret = abx500_mask_and_set_register_interruptible(di->dev,
3243 AB8500_CHARGER, AB8540_USB_PP_CHR_REG,
3244 BUS_PP_PRECHG_CURRENT_MASK, 0);
3245 if (ret) {
3246 dev_err(di->dev,
3247 "failed to setup usb power path prechage current\n");
3248 goto out;
3249 }
3250 }
3251
3252out:
3253 return ret;
3254}
3255
3256
3257
3258
3259static struct ab8500_charger_interrupts ab8500_charger_irq[] = {
3260 {"MAIN_CH_UNPLUG_DET", ab8500_charger_mainchunplugdet_handler},
3261 {"MAIN_CHARGE_PLUG_DET", ab8500_charger_mainchplugdet_handler},
3262 {"MAIN_EXT_CH_NOT_OK", ab8500_charger_mainextchnotok_handler},
3263 {"MAIN_CH_TH_PROT_R", ab8500_charger_mainchthprotr_handler},
3264 {"MAIN_CH_TH_PROT_F", ab8500_charger_mainchthprotf_handler},
3265 {"VBUS_DET_F", ab8500_charger_vbusdetf_handler},
3266 {"VBUS_DET_R", ab8500_charger_vbusdetr_handler},
3267 {"USB_LINK_STATUS", ab8500_charger_usblinkstatus_handler},
3268 {"USB_CH_TH_PROT_R", ab8500_charger_usbchthprotr_handler},
3269 {"USB_CH_TH_PROT_F", ab8500_charger_usbchthprotf_handler},
3270 {"USB_CHARGER_NOT_OKR", ab8500_charger_usbchargernotokr_handler},
3271 {"VBUS_OVV", ab8500_charger_vbusovv_handler},
3272 {"CH_WD_EXP", ab8500_charger_chwdexp_handler},
3273 {"VBUS_CH_DROP_END", ab8500_charger_vbuschdropend_handler},
3274};
3275
3276static int ab8500_charger_usb_notifier_call(struct notifier_block *nb,
3277 unsigned long event, void *power)
3278{
3279 struct ab8500_charger *di =
3280 container_of(nb, struct ab8500_charger, nb);
3281 enum ab8500_usb_state bm_usb_state;
3282 unsigned mA = *((unsigned *)power);
3283
3284 if (!di)
3285 return NOTIFY_DONE;
3286
3287 if (event != USB_EVENT_VBUS) {
3288 dev_dbg(di->dev, "not a standard host, returning\n");
3289 return NOTIFY_DONE;
3290 }
3291
3292
3293
3294
3295 if ((di->usb_state.usb_current == 2) && (mA > 2))
3296 bm_usb_state = AB8500_BM_USB_STATE_RESUME;
3297 else if (mA == 0)
3298 bm_usb_state = AB8500_BM_USB_STATE_RESET_HS;
3299 else if (mA == 2)
3300 bm_usb_state = AB8500_BM_USB_STATE_SUSPEND;
3301 else if (mA >= 8)
3302 bm_usb_state = AB8500_BM_USB_STATE_CONFIGURED;
3303 else
3304 bm_usb_state = AB8500_BM_USB_STATE_RESET_FS;
3305
3306 dev_dbg(di->dev, "%s usb_state: 0x%02x mA: %d\n",
3307 __func__, bm_usb_state, mA);
3308
3309 spin_lock(&di->usb_state.usb_lock);
3310 di->usb_state.state_tmp = bm_usb_state;
3311 di->usb_state.usb_current_tmp = mA;
3312 spin_unlock(&di->usb_state.usb_lock);
3313
3314
3315
3316
3317
3318 queue_delayed_work(di->charger_wq, &di->usb_state_changed_work, HZ/2);
3319
3320 return NOTIFY_OK;
3321}
3322
3323#if defined(CONFIG_PM)
3324static int ab8500_charger_resume(struct platform_device *pdev)
3325{
3326 int ret;
3327 struct ab8500_charger *di = platform_get_drvdata(pdev);
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338 if (di->ac_conn && is_ab8500_1p1_or_earlier(di->parent)) {
3339 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
3340 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
3341 if (ret)
3342 dev_err(di->dev, "Failed to kick WD!\n");
3343
3344
3345 queue_delayed_work(di->charger_wq, &di->kick_wd_work,
3346 round_jiffies(WD_KICK_INTERVAL));
3347 }
3348
3349
3350 if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
3351 queue_delayed_work(di->charger_wq,
3352 &di->check_hw_failure_work, 0);
3353 }
3354
3355 if (di->flags.vbus_drop_end)
3356 queue_delayed_work(di->charger_wq, &di->vbus_drop_end_work, 0);
3357
3358 return 0;
3359}
3360
3361static int ab8500_charger_suspend(struct platform_device *pdev,
3362 pm_message_t state)
3363{
3364 struct ab8500_charger *di = platform_get_drvdata(pdev);
3365
3366
3367 cancel_delayed_work(&di->check_hw_failure_work);
3368 cancel_delayed_work(&di->vbus_drop_end_work);
3369
3370 flush_delayed_work(&di->attach_work);
3371 flush_delayed_work(&di->usb_charger_attached_work);
3372 flush_delayed_work(&di->ac_charger_attached_work);
3373 flush_delayed_work(&di->check_usbchgnotok_work);
3374 flush_delayed_work(&di->check_vbat_work);
3375 flush_delayed_work(&di->kick_wd_work);
3376
3377 flush_work(&di->usb_link_status_work);
3378 flush_work(&di->ac_work);
3379 flush_work(&di->detect_usb_type_work);
3380
3381 if (atomic_read(&di->current_stepping_sessions))
3382 return -EAGAIN;
3383
3384 return 0;
3385}
3386#else
3387#define ab8500_charger_suspend NULL
3388#define ab8500_charger_resume NULL
3389#endif
3390
3391static struct notifier_block charger_nb = {
3392 .notifier_call = ab8500_external_charger_prepare,
3393};
3394
3395static int ab8500_charger_remove(struct platform_device *pdev)
3396{
3397 struct ab8500_charger *di = platform_get_drvdata(pdev);
3398 int i, irq, ret;
3399
3400
3401 ab8500_charger_ac_en(&di->ac_chg, false, 0, 0);
3402
3403
3404 ab8500_charger_usb_en(&di->usb_chg, false, 0, 0);
3405
3406
3407 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
3408 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3409 free_irq(irq, di);
3410 }
3411
3412
3413 ret = abx500_mask_and_set_register_interruptible(di->dev,
3414 AB8500_RTC, AB8500_RTC_CTRL_REG, RTC_BUP_CH_ENA, 0);
3415 if (ret < 0)
3416 dev_err(di->dev, "%s mask and set failed\n", __func__);
3417
3418 usb_unregister_notifier(di->usb_phy, &di->nb);
3419 usb_put_phy(di->usb_phy);
3420
3421
3422 destroy_workqueue(di->charger_wq);
3423
3424
3425 if (!di->ac_chg.enabled)
3426 blocking_notifier_chain_unregister(
3427 &charger_notifier_list, &charger_nb);
3428
3429 flush_scheduled_work();
3430 if (di->usb_chg.enabled)
3431 power_supply_unregister(&di->usb_chg.psy);
3432
3433 if (di->ac_chg.enabled && !di->ac_chg.external)
3434 power_supply_unregister(&di->ac_chg.psy);
3435
3436 return 0;
3437}
3438
3439static char *supply_interface[] = {
3440 "ab8500_chargalg",
3441 "ab8500_fg",
3442 "ab8500_btemp",
3443};
3444
3445static int ab8500_charger_probe(struct platform_device *pdev)
3446{
3447 struct device_node *np = pdev->dev.of_node;
3448 struct abx500_bm_data *plat = pdev->dev.platform_data;
3449 struct ab8500_charger *di;
3450 int irq, i, charger_status, ret = 0, ch_stat;
3451
3452 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
3453 if (!di) {
3454 dev_err(&pdev->dev, "%s no mem for ab8500_charger\n", __func__);
3455 return -ENOMEM;
3456 }
3457
3458 if (!plat) {
3459 dev_err(&pdev->dev, "no battery management data supplied\n");
3460 return -EINVAL;
3461 }
3462 di->bm = plat;
3463
3464 if (np) {
3465 ret = ab8500_bm_of_probe(&pdev->dev, np, di->bm);
3466 if (ret) {
3467 dev_err(&pdev->dev, "failed to get battery information\n");
3468 return ret;
3469 }
3470 di->autopower_cfg = of_property_read_bool(np, "autopower_cfg");
3471 } else
3472 di->autopower_cfg = false;
3473
3474
3475 di->dev = &pdev->dev;
3476 di->parent = dev_get_drvdata(pdev->dev.parent);
3477 di->gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
3478
3479
3480 spin_lock_init(&di->usb_state.usb_lock);
3481 mutex_init(&di->usb_ipt_crnt_lock);
3482
3483 di->autopower = false;
3484 di->invalid_charger_detect_state = 0;
3485
3486
3487
3488 di->ac_chg.psy.name = "ab8500_ac";
3489 di->ac_chg.psy.type = POWER_SUPPLY_TYPE_MAINS;
3490 di->ac_chg.psy.properties = ab8500_charger_ac_props;
3491 di->ac_chg.psy.num_properties = ARRAY_SIZE(ab8500_charger_ac_props);
3492 di->ac_chg.psy.get_property = ab8500_charger_ac_get_property;
3493 di->ac_chg.psy.supplied_to = supply_interface;
3494 di->ac_chg.psy.num_supplicants = ARRAY_SIZE(supply_interface),
3495
3496 di->ac_chg.ops.enable = &ab8500_charger_ac_en;
3497 di->ac_chg.ops.check_enable = &ab8500_charger_ac_check_enable;
3498 di->ac_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
3499 di->ac_chg.ops.update_curr = &ab8500_charger_update_charger_current;
3500 di->ac_chg.max_out_volt = ab8500_charger_voltage_map[
3501 ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
3502 di->ac_chg.max_out_curr =
3503 di->bm->chg_output_curr[di->bm->n_chg_out_curr - 1];
3504 di->ac_chg.wdt_refresh = CHG_WD_INTERVAL;
3505 di->ac_chg.enabled = di->bm->ac_enabled;
3506 di->ac_chg.external = false;
3507
3508
3509 if (!di->ac_chg.enabled)
3510 blocking_notifier_chain_register(
3511 &charger_notifier_list, &charger_nb);
3512
3513
3514
3515 di->usb_chg.psy.name = "ab8500_usb";
3516 di->usb_chg.psy.type = POWER_SUPPLY_TYPE_USB;
3517 di->usb_chg.psy.properties = ab8500_charger_usb_props;
3518 di->usb_chg.psy.num_properties = ARRAY_SIZE(ab8500_charger_usb_props);
3519 di->usb_chg.psy.get_property = ab8500_charger_usb_get_property;
3520 di->usb_chg.psy.supplied_to = supply_interface;
3521 di->usb_chg.psy.num_supplicants = ARRAY_SIZE(supply_interface),
3522
3523 di->usb_chg.ops.enable = &ab8500_charger_usb_en;
3524 di->usb_chg.ops.check_enable = &ab8500_charger_usb_check_enable;
3525 di->usb_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
3526 di->usb_chg.ops.update_curr = &ab8500_charger_update_charger_current;
3527 di->usb_chg.ops.pp_enable = &ab8540_charger_power_path_enable;
3528 di->usb_chg.ops.pre_chg_enable = &ab8540_charger_usb_pre_chg_enable;
3529 di->usb_chg.max_out_volt = ab8500_charger_voltage_map[
3530 ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
3531 di->usb_chg.max_out_curr =
3532 di->bm->chg_output_curr[di->bm->n_chg_out_curr - 1];
3533 di->usb_chg.wdt_refresh = CHG_WD_INTERVAL;
3534 di->usb_chg.enabled = di->bm->usb_enabled;
3535 di->usb_chg.external = false;
3536 di->usb_chg.power_path = di->bm->usb_power_path;
3537 di->usb_state.usb_current = -1;
3538
3539
3540 di->charger_wq =
3541 create_singlethread_workqueue("ab8500_charger_wq");
3542 if (di->charger_wq == NULL) {
3543 dev_err(di->dev, "failed to create work queue\n");
3544 return -ENOMEM;
3545 }
3546
3547 mutex_init(&di->charger_attached_mutex);
3548
3549
3550 INIT_DEFERRABLE_WORK(&di->check_hw_failure_work,
3551 ab8500_charger_check_hw_failure_work);
3552 INIT_DEFERRABLE_WORK(&di->check_usbchgnotok_work,
3553 ab8500_charger_check_usbchargernotok_work);
3554
3555 INIT_DELAYED_WORK(&di->ac_charger_attached_work,
3556 ab8500_charger_ac_attached_work);
3557 INIT_DELAYED_WORK(&di->usb_charger_attached_work,
3558 ab8500_charger_usb_attached_work);
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569 INIT_DEFERRABLE_WORK(&di->kick_wd_work,
3570 ab8500_charger_kick_watchdog_work);
3571
3572 INIT_DEFERRABLE_WORK(&di->check_vbat_work,
3573 ab8500_charger_check_vbat_work);
3574
3575 INIT_DELAYED_WORK(&di->attach_work,
3576 ab8500_charger_usb_link_attach_work);
3577
3578 INIT_DELAYED_WORK(&di->usb_state_changed_work,
3579 ab8500_charger_usb_state_changed_work);
3580
3581 INIT_DELAYED_WORK(&di->vbus_drop_end_work,
3582 ab8500_charger_vbus_drop_end_work);
3583
3584
3585 INIT_WORK(&di->usb_link_status_work,
3586 ab8500_charger_usb_link_status_work);
3587 INIT_WORK(&di->ac_work, ab8500_charger_ac_work);
3588 INIT_WORK(&di->detect_usb_type_work,
3589 ab8500_charger_detect_usb_type_work);
3590
3591
3592 INIT_WORK(&di->check_main_thermal_prot_work,
3593 ab8500_charger_check_main_thermal_prot_work);
3594 INIT_WORK(&di->check_usb_thermal_prot_work,
3595 ab8500_charger_check_usb_thermal_prot_work);
3596
3597
3598
3599
3600
3601
3602 di->regu = devm_regulator_get(di->dev, "vddadc");
3603 if (IS_ERR(di->regu)) {
3604 ret = PTR_ERR(di->regu);
3605 dev_err(di->dev, "failed to get vddadc regulator\n");
3606 goto free_charger_wq;
3607 }
3608
3609
3610
3611 ret = ab8500_charger_init_hw_registers(di);
3612 if (ret) {
3613 dev_err(di->dev, "failed to initialize ABB registers\n");
3614 goto free_charger_wq;
3615 }
3616
3617
3618 if (di->ac_chg.enabled) {
3619 ret = power_supply_register(di->dev, &di->ac_chg.psy);
3620 if (ret) {
3621 dev_err(di->dev, "failed to register AC charger\n");
3622 goto free_charger_wq;
3623 }
3624 }
3625
3626
3627 if (di->usb_chg.enabled) {
3628 ret = power_supply_register(di->dev, &di->usb_chg.psy);
3629 if (ret) {
3630 dev_err(di->dev, "failed to register USB charger\n");
3631 goto free_ac;
3632 }
3633 }
3634
3635 di->usb_phy = usb_get_phy(USB_PHY_TYPE_USB2);
3636 if (IS_ERR_OR_NULL(di->usb_phy)) {
3637 dev_err(di->dev, "failed to get usb transceiver\n");
3638 ret = -EINVAL;
3639 goto free_usb;
3640 }
3641 di->nb.notifier_call = ab8500_charger_usb_notifier_call;
3642 ret = usb_register_notifier(di->usb_phy, &di->nb);
3643 if (ret) {
3644 dev_err(di->dev, "failed to register usb notifier\n");
3645 goto put_usb_phy;
3646 }
3647
3648
3649 charger_status = ab8500_charger_detect_chargers(di, true);
3650 if (charger_status & AC_PW_CONN) {
3651 di->ac.charger_connected = 1;
3652 di->ac_conn = true;
3653 ab8500_power_supply_changed(di, &di->ac_chg.psy);
3654 sysfs_notify(&di->ac_chg.psy.dev->kobj, NULL, "present");
3655 }
3656
3657 if (charger_status & USB_PW_CONN) {
3658 di->vbus_detected = true;
3659 di->vbus_detected_start = true;
3660 queue_work(di->charger_wq,
3661 &di->detect_usb_type_work);
3662 }
3663
3664
3665 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
3666 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3667 ret = request_threaded_irq(irq, NULL, ab8500_charger_irq[i].isr,
3668 IRQF_SHARED | IRQF_NO_SUSPEND,
3669 ab8500_charger_irq[i].name, di);
3670
3671 if (ret != 0) {
3672 dev_err(di->dev, "failed to request %s IRQ %d: %d\n"
3673 , ab8500_charger_irq[i].name, irq, ret);
3674 goto free_irq;
3675 }
3676 dev_dbg(di->dev, "Requested %s IRQ %d: %d\n",
3677 ab8500_charger_irq[i].name, irq, ret);
3678 }
3679
3680 platform_set_drvdata(pdev, di);
3681
3682 mutex_lock(&di->charger_attached_mutex);
3683
3684 ch_stat = ab8500_charger_detect_chargers(di, false);
3685
3686 if ((ch_stat & AC_PW_CONN) == AC_PW_CONN) {
3687 if (is_ab8500(di->parent))
3688 queue_delayed_work(di->charger_wq,
3689 &di->ac_charger_attached_work,
3690 HZ);
3691 }
3692 if ((ch_stat & USB_PW_CONN) == USB_PW_CONN) {
3693 if (is_ab8500(di->parent))
3694 queue_delayed_work(di->charger_wq,
3695 &di->usb_charger_attached_work,
3696 HZ);
3697 }
3698
3699 mutex_unlock(&di->charger_attached_mutex);
3700
3701 return ret;
3702
3703free_irq:
3704 usb_unregister_notifier(di->usb_phy, &di->nb);
3705
3706
3707 for (i = i - 1; i >= 0; i--) {
3708 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3709 free_irq(irq, di);
3710 }
3711put_usb_phy:
3712 usb_put_phy(di->usb_phy);
3713free_usb:
3714 if (di->usb_chg.enabled)
3715 power_supply_unregister(&di->usb_chg.psy);
3716free_ac:
3717 if (di->ac_chg.enabled)
3718 power_supply_unregister(&di->ac_chg.psy);
3719free_charger_wq:
3720 destroy_workqueue(di->charger_wq);
3721 return ret;
3722}
3723
3724static const struct of_device_id ab8500_charger_match[] = {
3725 { .compatible = "stericsson,ab8500-charger", },
3726 { },
3727};
3728
3729static struct platform_driver ab8500_charger_driver = {
3730 .probe = ab8500_charger_probe,
3731 .remove = ab8500_charger_remove,
3732 .suspend = ab8500_charger_suspend,
3733 .resume = ab8500_charger_resume,
3734 .driver = {
3735 .name = "ab8500-charger",
3736 .of_match_table = ab8500_charger_match,
3737 },
3738};
3739
3740static int __init ab8500_charger_init(void)
3741{
3742 return platform_driver_register(&ab8500_charger_driver);
3743}
3744
3745static void __exit ab8500_charger_exit(void)
3746{
3747 platform_driver_unregister(&ab8500_charger_driver);
3748}
3749
3750subsys_initcall_sync(ab8500_charger_init);
3751module_exit(ab8500_charger_exit);
3752
3753MODULE_LICENSE("GPL v2");
3754MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy");
3755MODULE_ALIAS("platform:ab8500-charger");
3756MODULE_DESCRIPTION("AB8500 charger management driver");
3757