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