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