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