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