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