treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / power / supply / ab8500_charger.c
blobf69550d64f09e0a7734db0fcbed826dd02ed5388
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) ST-Ericsson SA 2012
5 * Charger driver for AB8500
7 * Author:
8 * Johan Palsson <johan.palsson@stericsson.com>
9 * Karl Komierowski <karl.komierowski@stericsson.com>
10 * Arun R Murthy <arun.murthy@stericsson.com>
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>
37 /* Charger constants */
38 #define NO_PW_CONN 0
39 #define AC_PW_CONN 1
40 #define USB_PW_CONN 2
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
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 */
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
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
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
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
88 /* Watchdog timeout constant */
89 #define WD_TIMER 0x30 /* 4min */
90 #define WD_KICK_INTERVAL (60 * HZ)
92 /* Lowest charger voltage is 3.39V -> 0x4E */
93 #define LOW_VOLT_REG 0x4E
95 /* Step up/down delay in us */
96 #define STEP_UDELAY 1000
98 #define CHARGER_STATUS_POLL 10 /* in ms */
100 #define CHG_WD_INTERVAL (60 * HZ)
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
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,
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,
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
164 #define VBAT_TRESH_IP_CUR_RED 3800
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)
172 * struct ab8500_charger_interrupts - ab8500 interupts
173 * @name: name of the interrupt
174 * @isr function pointer to the isr
176 struct ab8500_charger_interrupts {
177 char *name;
178 irqreturn_t (*isr)(int irq, void *data);
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;
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;
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;
209 struct ab8500_charger_max_usb_in_curr {
210 int usb_type_max;
211 int set_max;
212 int calculated_max;
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
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;
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,
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,
345 * Function for enabling and disabling sw fallback mode
346 * should always be disabled when no charger is connected.
348 static void ab8500_enable_disable_sw_fallback(struct ab8500_charger *di,
349 bool fallback)
351 u8 val;
352 u8 reg;
353 u8 bank;
354 u8 bit;
355 int ret;
357 dev_dbg(di->dev, "SW Fallback: %d\n", fallback);
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;
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;
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;
385 if (fallback)
386 val |= (1 << bit);
387 else
388 val &= ~(1 << bit);
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__);
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__);
407 * ab8500_power_supply_changed - a wrapper with local extentions for
408 * power_supply_changed
409 * @di: pointer to the ab8500_charger structure
410 * @psy: pointer to power_supply_that have changed.
413 static void ab8500_power_supply_changed(struct ab8500_charger *di,
414 struct power_supply *psy)
416 if (di->autopower_cfg) {
417 if (!di->usb.charger_connected &&
418 !di->ac.charger_connected &&
419 di->autopower) {
420 di->autopower = false;
421 ab8500_enable_disable_sw_fallback(di, false);
422 } else if (!di->autopower &&
423 (di->ac.charger_connected ||
424 di->usb.charger_connected)) {
425 di->autopower = true;
426 ab8500_enable_disable_sw_fallback(di, true);
429 power_supply_changed(psy);
432 static void ab8500_charger_set_usb_connected(struct ab8500_charger *di,
433 bool connected)
435 if (connected != di->usb.charger_connected) {
436 dev_dbg(di->dev, "USB connected:%i\n", connected);
437 di->usb.charger_connected = connected;
439 if (!connected)
440 di->flags.vbus_drop_end = false;
442 sysfs_notify(&di->usb_chg.psy->dev.kobj, NULL, "present");
444 if (connected) {
445 mutex_lock(&di->charger_attached_mutex);
446 mutex_unlock(&di->charger_attached_mutex);
448 if (is_ab8500(di->parent))
449 queue_delayed_work(di->charger_wq,
450 &di->usb_charger_attached_work,
451 HZ);
452 } else {
453 cancel_delayed_work_sync(&di->usb_charger_attached_work);
454 mutex_lock(&di->charger_attached_mutex);
455 mutex_unlock(&di->charger_attached_mutex);
461 * ab8500_charger_get_ac_voltage() - get ac charger voltage
462 * @di: pointer to the ab8500_charger structure
464 * Returns ac charger voltage (on success)
466 static int ab8500_charger_get_ac_voltage(struct ab8500_charger *di)
468 int vch, ret;
470 /* Only measure voltage if the charger is connected */
471 if (di->ac.charger_connected) {
472 ret = iio_read_channel_processed(di->adc_main_charger_v, &vch);
473 if (ret < 0)
474 dev_err(di->dev, "%s ADC conv failed,\n", __func__);
475 } else {
476 vch = 0;
478 return vch;
482 * ab8500_charger_ac_cv() - check if the main charger is in CV mode
483 * @di: pointer to the ab8500_charger structure
485 * Returns ac charger CV mode (on success) else error code
487 static int ab8500_charger_ac_cv(struct ab8500_charger *di)
489 u8 val;
490 int ret = 0;
492 /* Only check CV mode if the charger is online */
493 if (di->ac.charger_online) {
494 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
495 AB8500_CH_STATUS1_REG, &val);
496 if (ret < 0) {
497 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
498 return 0;
501 if (val & MAIN_CH_CV_ON)
502 ret = 1;
503 else
504 ret = 0;
507 return ret;
511 * ab8500_charger_get_vbus_voltage() - get vbus voltage
512 * @di: pointer to the ab8500_charger structure
514 * This function returns the vbus voltage.
515 * Returns vbus voltage (on success)
517 static int ab8500_charger_get_vbus_voltage(struct ab8500_charger *di)
519 int vch, ret;
521 /* Only measure voltage if the charger is connected */
522 if (di->usb.charger_connected) {
523 ret = iio_read_channel_processed(di->adc_vbus_v, &vch);
524 if (ret < 0)
525 dev_err(di->dev, "%s ADC conv failed,\n", __func__);
526 } else {
527 vch = 0;
529 return vch;
533 * ab8500_charger_get_usb_current() - get usb charger current
534 * @di: pointer to the ab8500_charger structure
536 * This function returns the usb charger current.
537 * Returns usb current (on success) and error code on failure
539 static int ab8500_charger_get_usb_current(struct ab8500_charger *di)
541 int ich, ret;
543 /* Only measure current if the charger is online */
544 if (di->usb.charger_online) {
545 ret = iio_read_channel_processed(di->adc_usb_charger_c, &ich);
546 if (ret < 0)
547 dev_err(di->dev, "%s ADC conv failed,\n", __func__);
548 } else {
549 ich = 0;
551 return ich;
555 * ab8500_charger_get_ac_current() - get ac charger current
556 * @di: pointer to the ab8500_charger structure
558 * This function returns the ac charger current.
559 * Returns ac current (on success) and error code on failure.
561 static int ab8500_charger_get_ac_current(struct ab8500_charger *di)
563 int ich, ret;
565 /* Only measure current if the charger is online */
566 if (di->ac.charger_online) {
567 ret = iio_read_channel_processed(di->adc_main_charger_c, &ich);
568 if (ret < 0)
569 dev_err(di->dev, "%s ADC conv failed,\n", __func__);
570 } else {
571 ich = 0;
573 return ich;
577 * ab8500_charger_usb_cv() - check if the usb charger is in CV mode
578 * @di: pointer to the ab8500_charger structure
580 * Returns ac charger CV mode (on success) else error code
582 static int ab8500_charger_usb_cv(struct ab8500_charger *di)
584 int ret;
585 u8 val;
587 /* Only check CV mode if the charger is online */
588 if (di->usb.charger_online) {
589 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
590 AB8500_CH_USBCH_STAT1_REG, &val);
591 if (ret < 0) {
592 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
593 return 0;
596 if (val & USB_CH_CV_ON)
597 ret = 1;
598 else
599 ret = 0;
600 } else {
601 ret = 0;
604 return ret;
608 * ab8500_charger_detect_chargers() - Detect the connected chargers
609 * @di: pointer to the ab8500_charger structure
610 * @probe: if probe, don't delay and wait for HW
612 * Returns the type of charger connected.
613 * For USB it will not mean we can actually charge from it
614 * but that there is a USB cable connected that we have to
615 * identify. This is used during startup when we don't get
616 * interrupts of the charger detection
618 * Returns an integer value, that means,
619 * NO_PW_CONN no power supply is connected
620 * AC_PW_CONN if the AC power supply is connected
621 * USB_PW_CONN if the USB power supply is connected
622 * AC_PW_CONN + USB_PW_CONN if USB and AC power supplies are both connected
624 static int ab8500_charger_detect_chargers(struct ab8500_charger *di, bool probe)
626 int result = NO_PW_CONN;
627 int ret;
628 u8 val;
630 /* Check for AC charger */
631 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
632 AB8500_CH_STATUS1_REG, &val);
633 if (ret < 0) {
634 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
635 return ret;
638 if (val & MAIN_CH_DET)
639 result = AC_PW_CONN;
641 /* Check for USB charger */
643 if (!probe) {
645 * AB8500 says VBUS_DET_DBNC1 & VBUS_DET_DBNC100
646 * when disconnecting ACA even though no
647 * charger was connected. Try waiting a little
648 * longer than the 100 ms of VBUS_DET_DBNC100...
650 msleep(110);
652 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
653 AB8500_CH_USBCH_STAT1_REG, &val);
654 if (ret < 0) {
655 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
656 return ret;
658 dev_dbg(di->dev,
659 "%s AB8500_CH_USBCH_STAT1_REG %x\n", __func__,
660 val);
661 if ((val & VBUS_DET_DBNC1) && (val & VBUS_DET_DBNC100))
662 result |= USB_PW_CONN;
664 return result;
668 * ab8500_charger_max_usb_curr() - get the max curr for the USB type
669 * @di: pointer to the ab8500_charger structure
670 * @link_status: the identified USB type
672 * Get the maximum current that is allowed to be drawn from the host
673 * based on the USB type.
674 * Returns error code in case of failure else 0 on success
676 static int ab8500_charger_max_usb_curr(struct ab8500_charger *di,
677 enum ab8500_charger_link_status link_status)
679 int ret = 0;
681 di->usb_device_is_unrecognised = false;
684 * Platform only supports USB 2.0.
685 * This means that charging current from USB source
686 * is maximum 500 mA. Every occurence of USB_STAT_*_HOST_*
687 * should set USB_CH_IP_CUR_LVL_0P5.
690 switch (link_status) {
691 case USB_STAT_STD_HOST_NC:
692 case USB_STAT_STD_HOST_C_NS:
693 case USB_STAT_STD_HOST_C_S:
694 dev_dbg(di->dev, "USB Type - Standard host is "
695 "detected through USB driver\n");
696 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
697 di->is_aca_rid = 0;
698 break;
699 case USB_STAT_HOST_CHG_HS_CHIRP:
700 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
701 di->is_aca_rid = 0;
702 break;
703 case USB_STAT_HOST_CHG_HS:
704 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
705 di->is_aca_rid = 0;
706 break;
707 case USB_STAT_ACA_RID_C_HS:
708 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P9;
709 di->is_aca_rid = 0;
710 break;
711 case USB_STAT_ACA_RID_A:
713 * Dedicated charger level minus maximum current accessory
714 * can consume (900mA). Closest level is 500mA
716 dev_dbg(di->dev, "USB_STAT_ACA_RID_A detected\n");
717 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
718 di->is_aca_rid = 1;
719 break;
720 case USB_STAT_ACA_RID_B:
722 * Dedicated charger level minus 120mA (20mA for ACA and
723 * 100mA for potential accessory). Closest level is 1300mA
725 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_1P3;
726 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
727 di->max_usb_in_curr.usb_type_max);
728 di->is_aca_rid = 1;
729 break;
730 case USB_STAT_HOST_CHG_NM:
731 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
732 di->is_aca_rid = 0;
733 break;
734 case USB_STAT_DEDICATED_CHG:
735 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_1P5;
736 di->is_aca_rid = 0;
737 break;
738 case USB_STAT_ACA_RID_C_HS_CHIRP:
739 case USB_STAT_ACA_RID_C_NM:
740 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_1P5;
741 di->is_aca_rid = 1;
742 break;
743 case USB_STAT_NOT_CONFIGURED:
744 if (di->vbus_detected) {
745 di->usb_device_is_unrecognised = true;
746 dev_dbg(di->dev, "USB Type - Legacy charger.\n");
747 di->max_usb_in_curr.usb_type_max =
748 USB_CH_IP_CUR_LVL_1P5;
749 break;
751 /* else, fall through */
752 case USB_STAT_HM_IDGND:
753 dev_err(di->dev, "USB Type - Charging not allowed\n");
754 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P05;
755 ret = -ENXIO;
756 break;
757 case USB_STAT_RESERVED:
758 if (is_ab8500(di->parent)) {
759 di->flags.vbus_collapse = true;
760 dev_err(di->dev, "USB Type - USB_STAT_RESERVED "
761 "VBUS has collapsed\n");
762 ret = -ENXIO;
763 break;
764 } else {
765 dev_dbg(di->dev, "USB Type - Charging not allowed\n");
766 di->max_usb_in_curr.usb_type_max =
767 USB_CH_IP_CUR_LVL_0P05;
768 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
769 link_status,
770 di->max_usb_in_curr.usb_type_max);
771 ret = -ENXIO;
772 break;
774 case USB_STAT_CARKIT_1:
775 case USB_STAT_CARKIT_2:
776 case USB_STAT_ACA_DOCK_CHARGER:
777 case USB_STAT_CHARGER_LINE_1:
778 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
779 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
780 di->max_usb_in_curr.usb_type_max);
781 break;
782 case USB_STAT_NOT_VALID_LINK:
783 dev_err(di->dev, "USB Type invalid - try charging anyway\n");
784 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
785 break;
787 default:
788 dev_err(di->dev, "USB Type - Unknown\n");
789 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P05;
790 ret = -ENXIO;
791 break;
794 di->max_usb_in_curr.set_max = di->max_usb_in_curr.usb_type_max;
795 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
796 link_status, di->max_usb_in_curr.set_max);
798 return ret;
802 * ab8500_charger_read_usb_type() - read the type of usb connected
803 * @di: pointer to the ab8500_charger structure
805 * Detect the type of the plugged USB
806 * Returns error code in case of failure else 0 on success
808 static int ab8500_charger_read_usb_type(struct ab8500_charger *di)
810 int ret;
811 u8 val;
813 ret = abx500_get_register_interruptible(di->dev,
814 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG, &val);
815 if (ret < 0) {
816 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
817 return ret;
819 if (is_ab8500(di->parent))
820 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
821 AB8500_USB_LINE_STAT_REG, &val);
822 else
823 ret = abx500_get_register_interruptible(di->dev,
824 AB8500_USB, AB8500_USB_LINK1_STAT_REG, &val);
825 if (ret < 0) {
826 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
827 return ret;
830 /* get the USB type */
831 if (is_ab8500(di->parent))
832 val = (val & AB8500_USB_LINK_STATUS) >> USB_LINK_STATUS_SHIFT;
833 else
834 val = (val & AB8505_USB_LINK_STATUS) >> USB_LINK_STATUS_SHIFT;
835 ret = ab8500_charger_max_usb_curr(di,
836 (enum ab8500_charger_link_status) val);
838 return ret;
842 * ab8500_charger_detect_usb_type() - get the type of usb connected
843 * @di: pointer to the ab8500_charger structure
845 * Detect the type of the plugged USB
846 * Returns error code in case of failure else 0 on success
848 static int ab8500_charger_detect_usb_type(struct ab8500_charger *di)
850 int i, ret;
851 u8 val;
854 * On getting the VBUS rising edge detect interrupt there
855 * is a 250ms delay after which the register UsbLineStatus
856 * is filled with valid data.
858 for (i = 0; i < 10; i++) {
859 msleep(250);
860 ret = abx500_get_register_interruptible(di->dev,
861 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG,
862 &val);
863 dev_dbg(di->dev, "%s AB8500_IT_SOURCE21_REG %x\n",
864 __func__, val);
865 if (ret < 0) {
866 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
867 return ret;
870 if (is_ab8500(di->parent))
871 ret = abx500_get_register_interruptible(di->dev,
872 AB8500_USB, AB8500_USB_LINE_STAT_REG, &val);
873 else
874 ret = abx500_get_register_interruptible(di->dev,
875 AB8500_USB, AB8500_USB_LINK1_STAT_REG, &val);
876 if (ret < 0) {
877 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
878 return ret;
880 dev_dbg(di->dev, "%s AB8500_USB_LINE_STAT_REG %x\n", __func__,
881 val);
883 * Until the IT source register is read the UsbLineStatus
884 * register is not updated, hence doing the same
885 * Revisit this:
888 /* get the USB type */
889 if (is_ab8500(di->parent))
890 val = (val & AB8500_USB_LINK_STATUS) >>
891 USB_LINK_STATUS_SHIFT;
892 else
893 val = (val & AB8505_USB_LINK_STATUS) >>
894 USB_LINK_STATUS_SHIFT;
895 if (val)
896 break;
898 ret = ab8500_charger_max_usb_curr(di,
899 (enum ab8500_charger_link_status) val);
901 return ret;
905 * This array maps the raw hex value to charger voltage used by the AB8500
906 * Values taken from the UM0836
908 static int ab8500_charger_voltage_map[] = {
909 3500 ,
910 3525 ,
911 3550 ,
912 3575 ,
913 3600 ,
914 3625 ,
915 3650 ,
916 3675 ,
917 3700 ,
918 3725 ,
919 3750 ,
920 3775 ,
921 3800 ,
922 3825 ,
923 3850 ,
924 3875 ,
925 3900 ,
926 3925 ,
927 3950 ,
928 3975 ,
929 4000 ,
930 4025 ,
931 4050 ,
932 4060 ,
933 4070 ,
934 4080 ,
935 4090 ,
936 4100 ,
937 4110 ,
938 4120 ,
939 4130 ,
940 4140 ,
941 4150 ,
942 4160 ,
943 4170 ,
944 4180 ,
945 4190 ,
946 4200 ,
947 4210 ,
948 4220 ,
949 4230 ,
950 4240 ,
951 4250 ,
952 4260 ,
953 4270 ,
954 4280 ,
955 4290 ,
956 4300 ,
957 4310 ,
958 4320 ,
959 4330 ,
960 4340 ,
961 4350 ,
962 4360 ,
963 4370 ,
964 4380 ,
965 4390 ,
966 4400 ,
967 4410 ,
968 4420 ,
969 4430 ,
970 4440 ,
971 4450 ,
972 4460 ,
973 4470 ,
974 4480 ,
975 4490 ,
976 4500 ,
977 4510 ,
978 4520 ,
979 4530 ,
980 4540 ,
981 4550 ,
982 4560 ,
983 4570 ,
984 4580 ,
985 4590 ,
986 4600 ,
989 static int ab8500_voltage_to_regval(int voltage)
991 int i;
993 /* Special case for voltage below 3.5V */
994 if (voltage < ab8500_charger_voltage_map[0])
995 return LOW_VOLT_REG;
997 for (i = 1; i < ARRAY_SIZE(ab8500_charger_voltage_map); i++) {
998 if (voltage < ab8500_charger_voltage_map[i])
999 return i - 1;
1002 /* If not last element, return error */
1003 i = ARRAY_SIZE(ab8500_charger_voltage_map) - 1;
1004 if (voltage == ab8500_charger_voltage_map[i])
1005 return i;
1006 else
1007 return -1;
1010 static int ab8500_current_to_regval(struct ab8500_charger *di, int curr)
1012 int i;
1014 if (curr < di->bm->chg_output_curr[0])
1015 return 0;
1017 for (i = 0; i < di->bm->n_chg_out_curr; i++) {
1018 if (curr < di->bm->chg_output_curr[i])
1019 return i - 1;
1022 /* If not last element, return error */
1023 i = di->bm->n_chg_out_curr - 1;
1024 if (curr == di->bm->chg_output_curr[i])
1025 return i;
1026 else
1027 return -1;
1030 static int ab8500_vbus_in_curr_to_regval(struct ab8500_charger *di, int curr)
1032 int i;
1034 if (curr < di->bm->chg_input_curr[0])
1035 return 0;
1037 for (i = 0; i < di->bm->n_chg_in_curr; i++) {
1038 if (curr < di->bm->chg_input_curr[i])
1039 return i - 1;
1042 /* If not last element, return error */
1043 i = di->bm->n_chg_in_curr - 1;
1044 if (curr == di->bm->chg_input_curr[i])
1045 return i;
1046 else
1047 return -1;
1051 * ab8500_charger_get_usb_cur() - get usb current
1052 * @di: pointer to the ab8500_charger structre
1054 * The usb stack provides the maximum current that can be drawn from
1055 * the standard usb host. This will be in mA.
1056 * This function converts current in mA to a value that can be written
1057 * to the register. Returns -1 if charging is not allowed
1059 static int ab8500_charger_get_usb_cur(struct ab8500_charger *di)
1061 int ret = 0;
1062 switch (di->usb_state.usb_current) {
1063 case 100:
1064 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P09;
1065 break;
1066 case 200:
1067 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P19;
1068 break;
1069 case 300:
1070 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P29;
1071 break;
1072 case 400:
1073 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P38;
1074 break;
1075 case 500:
1076 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
1077 break;
1078 default:
1079 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P05;
1080 ret = -EPERM;
1081 break;
1083 di->max_usb_in_curr.set_max = di->max_usb_in_curr.usb_type_max;
1084 return ret;
1088 * ab8500_charger_check_continue_stepping() - Check to allow stepping
1089 * @di: pointer to the ab8500_charger structure
1090 * @reg: select what charger register to check
1092 * Check if current stepping should be allowed to continue.
1093 * Checks if charger source has not collapsed. If it has, further stepping
1094 * is not allowed.
1096 static bool ab8500_charger_check_continue_stepping(struct ab8500_charger *di,
1097 int reg)
1099 if (reg == AB8500_USBCH_IPT_CRNTLVL_REG)
1100 return !di->flags.vbus_drop_end;
1101 else
1102 return true;
1106 * ab8500_charger_set_current() - set charger current
1107 * @di: pointer to the ab8500_charger structure
1108 * @ich: charger current, in mA
1109 * @reg: select what charger register to set
1111 * Set charger current.
1112 * There is no state machine in the AB to step up/down the charger
1113 * current to avoid dips and spikes on MAIN, VBUS and VBAT when
1114 * charging is started. Instead we need to implement
1115 * this charger current step-up/down here.
1116 * Returns error code in case of failure else 0(on success)
1118 static int ab8500_charger_set_current(struct ab8500_charger *di,
1119 int ich, int reg)
1121 int ret = 0;
1122 int curr_index, prev_curr_index, shift_value, i;
1123 u8 reg_value;
1124 u32 step_udelay;
1125 bool no_stepping = false;
1127 atomic_inc(&di->current_stepping_sessions);
1129 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1130 reg, &reg_value);
1131 if (ret < 0) {
1132 dev_err(di->dev, "%s read failed\n", __func__);
1133 goto exit_set_current;
1136 switch (reg) {
1137 case AB8500_MCH_IPT_CURLVL_REG:
1138 shift_value = MAIN_CH_INPUT_CURR_SHIFT;
1139 prev_curr_index = (reg_value >> shift_value);
1140 curr_index = ab8500_current_to_regval(di, ich);
1141 step_udelay = STEP_UDELAY;
1142 if (!di->ac.charger_connected)
1143 no_stepping = true;
1144 break;
1145 case AB8500_USBCH_IPT_CRNTLVL_REG:
1146 shift_value = VBUS_IN_CURR_LIM_SHIFT;
1147 prev_curr_index = (reg_value >> shift_value);
1148 curr_index = ab8500_vbus_in_curr_to_regval(di, ich);
1149 step_udelay = STEP_UDELAY * 100;
1151 if (!di->usb.charger_connected)
1152 no_stepping = true;
1153 break;
1154 case AB8500_CH_OPT_CRNTLVL_REG:
1155 shift_value = 0;
1156 prev_curr_index = (reg_value >> shift_value);
1157 curr_index = ab8500_current_to_regval(di, ich);
1158 step_udelay = STEP_UDELAY;
1159 if (curr_index && (curr_index - prev_curr_index) > 1)
1160 step_udelay *= 100;
1162 if (!di->usb.charger_connected && !di->ac.charger_connected)
1163 no_stepping = true;
1165 break;
1166 default:
1167 dev_err(di->dev, "%s current register not valid\n", __func__);
1168 ret = -ENXIO;
1169 goto exit_set_current;
1172 if (curr_index < 0) {
1173 dev_err(di->dev, "requested current limit out-of-range\n");
1174 ret = -ENXIO;
1175 goto exit_set_current;
1178 /* only update current if it's been changed */
1179 if (prev_curr_index == curr_index) {
1180 dev_dbg(di->dev, "%s current not changed for reg: 0x%02x\n",
1181 __func__, reg);
1182 ret = 0;
1183 goto exit_set_current;
1186 dev_dbg(di->dev, "%s set charger current: %d mA for reg: 0x%02x\n",
1187 __func__, ich, reg);
1189 if (no_stepping) {
1190 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1191 reg, (u8)curr_index << shift_value);
1192 if (ret)
1193 dev_err(di->dev, "%s write failed\n", __func__);
1194 } else if (prev_curr_index > curr_index) {
1195 for (i = prev_curr_index - 1; i >= curr_index; i--) {
1196 dev_dbg(di->dev, "curr change_1 to: %x for 0x%02x\n",
1197 (u8) i << shift_value, reg);
1198 ret = abx500_set_register_interruptible(di->dev,
1199 AB8500_CHARGER, reg, (u8)i << shift_value);
1200 if (ret) {
1201 dev_err(di->dev, "%s write failed\n", __func__);
1202 goto exit_set_current;
1204 if (i != curr_index)
1205 usleep_range(step_udelay, step_udelay * 2);
1207 } else {
1208 bool allow = true;
1209 for (i = prev_curr_index + 1; i <= curr_index && allow; i++) {
1210 dev_dbg(di->dev, "curr change_2 to: %x for 0x%02x\n",
1211 (u8)i << shift_value, reg);
1212 ret = abx500_set_register_interruptible(di->dev,
1213 AB8500_CHARGER, reg, (u8)i << shift_value);
1214 if (ret) {
1215 dev_err(di->dev, "%s write failed\n", __func__);
1216 goto exit_set_current;
1218 if (i != curr_index)
1219 usleep_range(step_udelay, step_udelay * 2);
1221 allow = ab8500_charger_check_continue_stepping(di, reg);
1225 exit_set_current:
1226 atomic_dec(&di->current_stepping_sessions);
1228 return ret;
1232 * ab8500_charger_set_vbus_in_curr() - set VBUS input current limit
1233 * @di: pointer to the ab8500_charger structure
1234 * @ich_in: charger input current limit
1236 * Sets the current that can be drawn from the USB host
1237 * Returns error code in case of failure else 0(on success)
1239 static int ab8500_charger_set_vbus_in_curr(struct ab8500_charger *di,
1240 int ich_in)
1242 int min_value;
1243 int ret;
1245 /* We should always use to lowest current limit */
1246 min_value = min(di->bm->chg_params->usb_curr_max, ich_in);
1247 if (di->max_usb_in_curr.set_max > 0)
1248 min_value = min(di->max_usb_in_curr.set_max, min_value);
1250 if (di->usb_state.usb_current >= 0)
1251 min_value = min(di->usb_state.usb_current, min_value);
1253 switch (min_value) {
1254 case 100:
1255 if (di->vbat < VBAT_TRESH_IP_CUR_RED)
1256 min_value = USB_CH_IP_CUR_LVL_0P05;
1257 break;
1258 case 500:
1259 if (di->vbat < VBAT_TRESH_IP_CUR_RED)
1260 min_value = USB_CH_IP_CUR_LVL_0P45;
1261 break;
1262 default:
1263 break;
1266 dev_info(di->dev, "VBUS input current limit set to %d mA\n", min_value);
1268 mutex_lock(&di->usb_ipt_crnt_lock);
1269 ret = ab8500_charger_set_current(di, min_value,
1270 AB8500_USBCH_IPT_CRNTLVL_REG);
1271 mutex_unlock(&di->usb_ipt_crnt_lock);
1273 return ret;
1277 * ab8500_charger_set_main_in_curr() - set main charger input current
1278 * @di: pointer to the ab8500_charger structure
1279 * @ich_in: input charger current, in mA
1281 * Set main charger input current.
1282 * Returns error code in case of failure else 0(on success)
1284 static int ab8500_charger_set_main_in_curr(struct ab8500_charger *di,
1285 int ich_in)
1287 return ab8500_charger_set_current(di, ich_in,
1288 AB8500_MCH_IPT_CURLVL_REG);
1292 * ab8500_charger_set_output_curr() - set charger output current
1293 * @di: pointer to the ab8500_charger structure
1294 * @ich_out: output charger current, in mA
1296 * Set charger output current.
1297 * Returns error code in case of failure else 0(on success)
1299 static int ab8500_charger_set_output_curr(struct ab8500_charger *di,
1300 int ich_out)
1302 return ab8500_charger_set_current(di, ich_out,
1303 AB8500_CH_OPT_CRNTLVL_REG);
1307 * ab8500_charger_led_en() - turn on/off chargign led
1308 * @di: pointer to the ab8500_charger structure
1309 * @on: flag to turn on/off the chargign led
1311 * Power ON/OFF charging LED indication
1312 * Returns error code in case of failure else 0(on success)
1314 static int ab8500_charger_led_en(struct ab8500_charger *di, int on)
1316 int ret;
1318 if (on) {
1319 /* Power ON charging LED indicator, set LED current to 5mA */
1320 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1321 AB8500_LED_INDICATOR_PWM_CTRL,
1322 (LED_IND_CUR_5MA | LED_INDICATOR_PWM_ENA));
1323 if (ret) {
1324 dev_err(di->dev, "Power ON LED failed\n");
1325 return ret;
1327 /* LED indicator PWM duty cycle 252/256 */
1328 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1329 AB8500_LED_INDICATOR_PWM_DUTY,
1330 LED_INDICATOR_PWM_DUTY_252_256);
1331 if (ret) {
1332 dev_err(di->dev, "Set LED PWM duty cycle failed\n");
1333 return ret;
1335 } else {
1336 /* Power off charging LED indicator */
1337 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1338 AB8500_LED_INDICATOR_PWM_CTRL,
1339 LED_INDICATOR_PWM_DIS);
1340 if (ret) {
1341 dev_err(di->dev, "Power-off LED failed\n");
1342 return ret;
1346 return ret;
1350 * ab8500_charger_ac_en() - enable or disable ac charging
1351 * @di: pointer to the ab8500_charger structure
1352 * @enable: enable/disable flag
1353 * @vset: charging voltage
1354 * @iset: charging current
1356 * Enable/Disable AC/Mains charging and turns on/off the charging led
1357 * respectively.
1359 static int ab8500_charger_ac_en(struct ux500_charger *charger,
1360 int enable, int vset, int iset)
1362 int ret;
1363 int volt_index;
1364 int curr_index;
1365 int input_curr_index;
1366 u8 overshoot = 0;
1368 struct ab8500_charger *di = to_ab8500_charger_ac_device_info(charger);
1370 if (enable) {
1371 /* Check if AC is connected */
1372 if (!di->ac.charger_connected) {
1373 dev_err(di->dev, "AC charger not connected\n");
1374 return -ENXIO;
1377 /* Enable AC charging */
1378 dev_dbg(di->dev, "Enable AC: %dmV %dmA\n", vset, iset);
1381 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1382 * will be triggered everytime we enable the VDD ADC supply.
1383 * This will turn off charging for a short while.
1384 * It can be avoided by having the supply on when
1385 * there is a charger enabled. Normally the VDD ADC supply
1386 * is enabled everytime a GPADC conversion is triggered. We will
1387 * force it to be enabled from this driver to have
1388 * the GPADC module independant of the AB8500 chargers
1390 if (!di->vddadc_en_ac) {
1391 ret = regulator_enable(di->regu);
1392 if (ret)
1393 dev_warn(di->dev,
1394 "Failed to enable regulator\n");
1395 else
1396 di->vddadc_en_ac = true;
1399 /* Check if the requested voltage or current is valid */
1400 volt_index = ab8500_voltage_to_regval(vset);
1401 curr_index = ab8500_current_to_regval(di, iset);
1402 input_curr_index = ab8500_current_to_regval(di,
1403 di->bm->chg_params->ac_curr_max);
1404 if (volt_index < 0 || curr_index < 0 || input_curr_index < 0) {
1405 dev_err(di->dev,
1406 "Charger voltage or current too high, "
1407 "charging not started\n");
1408 return -ENXIO;
1411 /* ChVoltLevel: maximum battery charging voltage */
1412 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1413 AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1414 if (ret) {
1415 dev_err(di->dev, "%s write failed\n", __func__);
1416 return ret;
1418 /* MainChInputCurr: current that can be drawn from the charger*/
1419 ret = ab8500_charger_set_main_in_curr(di,
1420 di->bm->chg_params->ac_curr_max);
1421 if (ret) {
1422 dev_err(di->dev, "%s Failed to set MainChInputCurr\n",
1423 __func__);
1424 return ret;
1426 /* ChOutputCurentLevel: protected output current */
1427 ret = ab8500_charger_set_output_curr(di, iset);
1428 if (ret) {
1429 dev_err(di->dev, "%s "
1430 "Failed to set ChOutputCurentLevel\n",
1431 __func__);
1432 return ret;
1435 /* Check if VBAT overshoot control should be enabled */
1436 if (!di->bm->enable_overshoot)
1437 overshoot = MAIN_CH_NO_OVERSHOOT_ENA_N;
1439 /* Enable Main Charger */
1440 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1441 AB8500_MCH_CTRL1, MAIN_CH_ENA | overshoot);
1442 if (ret) {
1443 dev_err(di->dev, "%s write failed\n", __func__);
1444 return ret;
1447 /* Power on charging LED indication */
1448 ret = ab8500_charger_led_en(di, true);
1449 if (ret < 0)
1450 dev_err(di->dev, "failed to enable LED\n");
1452 di->ac.charger_online = 1;
1453 } else {
1454 /* Disable AC charging */
1455 if (is_ab8500_1p1_or_earlier(di->parent)) {
1457 * For ABB revision 1.0 and 1.1 there is a bug in the
1458 * watchdog logic. That means we have to continously
1459 * kick the charger watchdog even when no charger is
1460 * connected. This is only valid once the AC charger
1461 * has been enabled. This is a bug that is not handled
1462 * by the algorithm and the watchdog have to be kicked
1463 * by the charger driver when the AC charger
1464 * is disabled
1466 if (di->ac_conn) {
1467 queue_delayed_work(di->charger_wq,
1468 &di->kick_wd_work,
1469 round_jiffies(WD_KICK_INTERVAL));
1473 * We can't turn off charging completely
1474 * due to a bug in AB8500 cut1.
1475 * If we do, charging will not start again.
1476 * That is why we set the lowest voltage
1477 * and current possible
1479 ret = abx500_set_register_interruptible(di->dev,
1480 AB8500_CHARGER,
1481 AB8500_CH_VOLT_LVL_REG, CH_VOL_LVL_3P5);
1482 if (ret) {
1483 dev_err(di->dev,
1484 "%s write failed\n", __func__);
1485 return ret;
1488 ret = ab8500_charger_set_output_curr(di, 0);
1489 if (ret) {
1490 dev_err(di->dev, "%s "
1491 "Failed to set ChOutputCurentLevel\n",
1492 __func__);
1493 return ret;
1495 } else {
1496 ret = abx500_set_register_interruptible(di->dev,
1497 AB8500_CHARGER,
1498 AB8500_MCH_CTRL1, 0);
1499 if (ret) {
1500 dev_err(di->dev,
1501 "%s write failed\n", __func__);
1502 return ret;
1506 ret = ab8500_charger_led_en(di, false);
1507 if (ret < 0)
1508 dev_err(di->dev, "failed to disable LED\n");
1510 di->ac.charger_online = 0;
1511 di->ac.wd_expired = false;
1513 /* Disable regulator if enabled */
1514 if (di->vddadc_en_ac) {
1515 regulator_disable(di->regu);
1516 di->vddadc_en_ac = false;
1519 dev_dbg(di->dev, "%s Disabled AC charging\n", __func__);
1521 ab8500_power_supply_changed(di, di->ac_chg.psy);
1523 return ret;
1527 * ab8500_charger_usb_en() - enable usb charging
1528 * @di: pointer to the ab8500_charger structure
1529 * @enable: enable/disable flag
1530 * @vset: charging voltage
1531 * @ich_out: charger output current
1533 * Enable/Disable USB charging and turns on/off the charging led respectively.
1534 * Returns error code in case of failure else 0(on success)
1536 static int ab8500_charger_usb_en(struct ux500_charger *charger,
1537 int enable, int vset, int ich_out)
1539 int ret;
1540 int volt_index;
1541 int curr_index;
1542 u8 overshoot = 0;
1544 struct ab8500_charger *di = to_ab8500_charger_usb_device_info(charger);
1546 if (enable) {
1547 /* Check if USB is connected */
1548 if (!di->usb.charger_connected) {
1549 dev_err(di->dev, "USB charger not connected\n");
1550 return -ENXIO;
1554 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1555 * will be triggered everytime we enable the VDD ADC supply.
1556 * This will turn off charging for a short while.
1557 * It can be avoided by having the supply on when
1558 * there is a charger enabled. Normally the VDD ADC supply
1559 * is enabled everytime a GPADC conversion is triggered. We will
1560 * force it to be enabled from this driver to have
1561 * the GPADC module independant of the AB8500 chargers
1563 if (!di->vddadc_en_usb) {
1564 ret = regulator_enable(di->regu);
1565 if (ret)
1566 dev_warn(di->dev,
1567 "Failed to enable regulator\n");
1568 else
1569 di->vddadc_en_usb = true;
1572 /* Enable USB charging */
1573 dev_dbg(di->dev, "Enable USB: %dmV %dmA\n", vset, ich_out);
1575 /* Check if the requested voltage or current is valid */
1576 volt_index = ab8500_voltage_to_regval(vset);
1577 curr_index = ab8500_current_to_regval(di, ich_out);
1578 if (volt_index < 0 || curr_index < 0) {
1579 dev_err(di->dev,
1580 "Charger voltage or current too high, "
1581 "charging not started\n");
1582 return -ENXIO;
1585 /* ChVoltLevel: max voltage upto which battery can be charged */
1586 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1587 AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1588 if (ret) {
1589 dev_err(di->dev, "%s write failed\n", __func__);
1590 return ret;
1592 /* Check if VBAT overshoot control should be enabled */
1593 if (!di->bm->enable_overshoot)
1594 overshoot = USB_CHG_NO_OVERSHOOT_ENA_N;
1596 /* Enable USB Charger */
1597 dev_dbg(di->dev,
1598 "Enabling USB with write to AB8500_USBCH_CTRL1_REG\n");
1599 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1600 AB8500_USBCH_CTRL1_REG, USB_CH_ENA | overshoot);
1601 if (ret) {
1602 dev_err(di->dev, "%s write failed\n", __func__);
1603 return ret;
1606 /* If success power on charging LED indication */
1607 ret = ab8500_charger_led_en(di, true);
1608 if (ret < 0)
1609 dev_err(di->dev, "failed to enable LED\n");
1611 di->usb.charger_online = 1;
1613 /* USBChInputCurr: current that can be drawn from the usb */
1614 ret = ab8500_charger_set_vbus_in_curr(di,
1615 di->max_usb_in_curr.usb_type_max);
1616 if (ret) {
1617 dev_err(di->dev, "setting USBChInputCurr failed\n");
1618 return ret;
1621 /* ChOutputCurentLevel: protected output current */
1622 ret = ab8500_charger_set_output_curr(di, ich_out);
1623 if (ret) {
1624 dev_err(di->dev, "%s "
1625 "Failed to set ChOutputCurentLevel\n",
1626 __func__);
1627 return ret;
1630 queue_delayed_work(di->charger_wq, &di->check_vbat_work, HZ);
1632 } else {
1633 /* Disable USB charging */
1634 dev_dbg(di->dev, "%s Disabled USB charging\n", __func__);
1635 ret = abx500_set_register_interruptible(di->dev,
1636 AB8500_CHARGER,
1637 AB8500_USBCH_CTRL1_REG, 0);
1638 if (ret) {
1639 dev_err(di->dev,
1640 "%s write failed\n", __func__);
1641 return ret;
1644 ret = ab8500_charger_led_en(di, false);
1645 if (ret < 0)
1646 dev_err(di->dev, "failed to disable LED\n");
1647 /* USBChInputCurr: current that can be drawn from the usb */
1648 ret = ab8500_charger_set_vbus_in_curr(di, 0);
1649 if (ret) {
1650 dev_err(di->dev, "setting USBChInputCurr failed\n");
1651 return ret;
1654 /* ChOutputCurentLevel: protected output current */
1655 ret = ab8500_charger_set_output_curr(di, 0);
1656 if (ret) {
1657 dev_err(di->dev, "%s "
1658 "Failed to reset ChOutputCurentLevel\n",
1659 __func__);
1660 return ret;
1662 di->usb.charger_online = 0;
1663 di->usb.wd_expired = false;
1665 /* Disable regulator if enabled */
1666 if (di->vddadc_en_usb) {
1667 regulator_disable(di->regu);
1668 di->vddadc_en_usb = false;
1671 dev_dbg(di->dev, "%s Disabled USB charging\n", __func__);
1673 /* Cancel any pending Vbat check work */
1674 cancel_delayed_work(&di->check_vbat_work);
1677 ab8500_power_supply_changed(di, di->usb_chg.psy);
1679 return ret;
1682 static int ab8500_external_charger_prepare(struct notifier_block *charger_nb,
1683 unsigned long event, void *data)
1685 int ret;
1686 struct device *dev = data;
1687 /*Toggle External charger control pin*/
1688 ret = abx500_set_register_interruptible(dev, AB8500_SYS_CTRL1_BLOCK,
1689 AB8500_SYS_CHARGER_CONTROL_REG,
1690 EXTERNAL_CHARGER_DISABLE_REG_VAL);
1691 if (ret < 0) {
1692 dev_err(dev, "write reg failed %d\n", ret);
1693 goto out;
1695 ret = abx500_set_register_interruptible(dev, AB8500_SYS_CTRL1_BLOCK,
1696 AB8500_SYS_CHARGER_CONTROL_REG,
1697 EXTERNAL_CHARGER_ENABLE_REG_VAL);
1698 if (ret < 0)
1699 dev_err(dev, "Write reg failed %d\n", ret);
1701 out:
1702 return ret;
1706 * ab8500_charger_usb_check_enable() - enable usb charging
1707 * @charger: pointer to the ux500_charger structure
1708 * @vset: charging voltage
1709 * @iset: charger output current
1711 * Check if the VBUS charger has been disconnected and reconnected without
1712 * AB8500 rising an interrupt. Returns 0 on success.
1714 static int ab8500_charger_usb_check_enable(struct ux500_charger *charger,
1715 int vset, int iset)
1717 u8 usbch_ctrl1 = 0;
1718 int ret = 0;
1720 struct ab8500_charger *di = to_ab8500_charger_usb_device_info(charger);
1722 if (!di->usb.charger_connected)
1723 return ret;
1725 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1726 AB8500_USBCH_CTRL1_REG, &usbch_ctrl1);
1727 if (ret < 0) {
1728 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
1729 return ret;
1731 dev_dbg(di->dev, "USB charger ctrl: 0x%02x\n", usbch_ctrl1);
1733 if (!(usbch_ctrl1 & USB_CH_ENA)) {
1734 dev_info(di->dev, "Charging has been disabled abnormally and will be re-enabled\n");
1736 ret = abx500_mask_and_set_register_interruptible(di->dev,
1737 AB8500_CHARGER, AB8500_CHARGER_CTRL,
1738 DROP_COUNT_RESET, DROP_COUNT_RESET);
1739 if (ret < 0) {
1740 dev_err(di->dev, "ab8500 write failed %d\n", __LINE__);
1741 return ret;
1744 ret = ab8500_charger_usb_en(&di->usb_chg, true, vset, iset);
1745 if (ret < 0) {
1746 dev_err(di->dev, "Failed to enable VBUS charger %d\n",
1747 __LINE__);
1748 return ret;
1751 return ret;
1755 * ab8500_charger_ac_check_enable() - enable usb charging
1756 * @charger: pointer to the ux500_charger structure
1757 * @vset: charging voltage
1758 * @iset: charger output current
1760 * Check if the AC charger has been disconnected and reconnected without
1761 * AB8500 rising an interrupt. Returns 0 on success.
1763 static int ab8500_charger_ac_check_enable(struct ux500_charger *charger,
1764 int vset, int iset)
1766 u8 mainch_ctrl1 = 0;
1767 int ret = 0;
1769 struct ab8500_charger *di = to_ab8500_charger_ac_device_info(charger);
1771 if (!di->ac.charger_connected)
1772 return ret;
1774 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1775 AB8500_MCH_CTRL1, &mainch_ctrl1);
1776 if (ret < 0) {
1777 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
1778 return ret;
1780 dev_dbg(di->dev, "AC charger ctrl: 0x%02x\n", mainch_ctrl1);
1782 if (!(mainch_ctrl1 & MAIN_CH_ENA)) {
1783 dev_info(di->dev, "Charging has been disabled abnormally and will be re-enabled\n");
1785 ret = abx500_mask_and_set_register_interruptible(di->dev,
1786 AB8500_CHARGER, AB8500_CHARGER_CTRL,
1787 DROP_COUNT_RESET, DROP_COUNT_RESET);
1789 if (ret < 0) {
1790 dev_err(di->dev, "ab8500 write failed %d\n", __LINE__);
1791 return ret;
1794 ret = ab8500_charger_ac_en(&di->usb_chg, true, vset, iset);
1795 if (ret < 0) {
1796 dev_err(di->dev, "failed to enable AC charger %d\n",
1797 __LINE__);
1798 return ret;
1801 return ret;
1805 * ab8500_charger_watchdog_kick() - kick charger watchdog
1806 * @di: pointer to the ab8500_charger structure
1808 * Kick charger watchdog
1809 * Returns error code in case of failure else 0(on success)
1811 static int ab8500_charger_watchdog_kick(struct ux500_charger *charger)
1813 int ret;
1814 struct ab8500_charger *di;
1816 if (charger->psy->desc->type == POWER_SUPPLY_TYPE_MAINS)
1817 di = to_ab8500_charger_ac_device_info(charger);
1818 else if (charger->psy->desc->type == POWER_SUPPLY_TYPE_USB)
1819 di = to_ab8500_charger_usb_device_info(charger);
1820 else
1821 return -ENXIO;
1823 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1824 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
1825 if (ret)
1826 dev_err(di->dev, "Failed to kick WD!\n");
1828 return ret;
1832 * ab8500_charger_update_charger_current() - update charger current
1833 * @di: pointer to the ab8500_charger structure
1835 * Update the charger output current for the specified charger
1836 * Returns error code in case of failure else 0(on success)
1838 static int ab8500_charger_update_charger_current(struct ux500_charger *charger,
1839 int ich_out)
1841 int ret;
1842 struct ab8500_charger *di;
1844 if (charger->psy->desc->type == POWER_SUPPLY_TYPE_MAINS)
1845 di = to_ab8500_charger_ac_device_info(charger);
1846 else if (charger->psy->desc->type == POWER_SUPPLY_TYPE_USB)
1847 di = to_ab8500_charger_usb_device_info(charger);
1848 else
1849 return -ENXIO;
1851 ret = ab8500_charger_set_output_curr(di, ich_out);
1852 if (ret) {
1853 dev_err(di->dev, "%s "
1854 "Failed to set ChOutputCurentLevel\n",
1855 __func__);
1856 return ret;
1859 /* Reset the main and usb drop input current measurement counter */
1860 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1861 AB8500_CHARGER_CTRL, DROP_COUNT_RESET);
1862 if (ret) {
1863 dev_err(di->dev, "%s write failed\n", __func__);
1864 return ret;
1867 return ret;
1870 static int ab8500_charger_get_ext_psy_data(struct device *dev, void *data)
1872 struct power_supply *psy;
1873 struct power_supply *ext = dev_get_drvdata(dev);
1874 const char **supplicants = (const char **)ext->supplied_to;
1875 struct ab8500_charger *di;
1876 union power_supply_propval ret;
1877 int j;
1878 struct ux500_charger *usb_chg;
1880 usb_chg = (struct ux500_charger *)data;
1881 psy = usb_chg->psy;
1883 di = to_ab8500_charger_usb_device_info(usb_chg);
1885 /* For all psy where the driver name appears in any supplied_to */
1886 j = match_string(supplicants, ext->num_supplicants, psy->desc->name);
1887 if (j < 0)
1888 return 0;
1890 /* Go through all properties for the psy */
1891 for (j = 0; j < ext->desc->num_properties; j++) {
1892 enum power_supply_property prop;
1893 prop = ext->desc->properties[j];
1895 if (power_supply_get_property(ext, prop, &ret))
1896 continue;
1898 switch (prop) {
1899 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1900 switch (ext->desc->type) {
1901 case POWER_SUPPLY_TYPE_BATTERY:
1902 di->vbat = ret.intval / 1000;
1903 break;
1904 default:
1905 break;
1907 break;
1908 default:
1909 break;
1912 return 0;
1916 * ab8500_charger_check_vbat_work() - keep vbus current within spec
1917 * @work pointer to the work_struct structure
1919 * Due to a asic bug it is necessary to lower the input current to the vbus
1920 * charger when charging with at some specific levels. This issue is only valid
1921 * for below a certain battery voltage. This function makes sure that the
1922 * the allowed current limit isn't exceeded.
1924 static void ab8500_charger_check_vbat_work(struct work_struct *work)
1926 int t = 10;
1927 struct ab8500_charger *di = container_of(work,
1928 struct ab8500_charger, check_vbat_work.work);
1930 class_for_each_device(power_supply_class, NULL,
1931 di->usb_chg.psy, ab8500_charger_get_ext_psy_data);
1933 /* First run old_vbat is 0. */
1934 if (di->old_vbat == 0)
1935 di->old_vbat = di->vbat;
1937 if (!((di->old_vbat <= VBAT_TRESH_IP_CUR_RED &&
1938 di->vbat <= VBAT_TRESH_IP_CUR_RED) ||
1939 (di->old_vbat > VBAT_TRESH_IP_CUR_RED &&
1940 di->vbat > VBAT_TRESH_IP_CUR_RED))) {
1942 dev_dbg(di->dev, "Vbat did cross threshold, curr: %d, new: %d,"
1943 " old: %d\n", di->max_usb_in_curr.usb_type_max,
1944 di->vbat, di->old_vbat);
1945 ab8500_charger_set_vbus_in_curr(di,
1946 di->max_usb_in_curr.usb_type_max);
1947 power_supply_changed(di->usb_chg.psy);
1950 di->old_vbat = di->vbat;
1953 * No need to check the battery voltage every second when not close to
1954 * the threshold.
1956 if (di->vbat < (VBAT_TRESH_IP_CUR_RED + 100) &&
1957 (di->vbat > (VBAT_TRESH_IP_CUR_RED - 100)))
1958 t = 1;
1960 queue_delayed_work(di->charger_wq, &di->check_vbat_work, t * HZ);
1964 * ab8500_charger_check_hw_failure_work() - check main charger failure
1965 * @work: pointer to the work_struct structure
1967 * Work queue function for checking the main charger status
1969 static void ab8500_charger_check_hw_failure_work(struct work_struct *work)
1971 int ret;
1972 u8 reg_value;
1974 struct ab8500_charger *di = container_of(work,
1975 struct ab8500_charger, check_hw_failure_work.work);
1977 /* Check if the status bits for HW failure is still active */
1978 if (di->flags.mainextchnotok) {
1979 ret = abx500_get_register_interruptible(di->dev,
1980 AB8500_CHARGER, AB8500_CH_STATUS2_REG, &reg_value);
1981 if (ret < 0) {
1982 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1983 return;
1985 if (!(reg_value & MAIN_CH_NOK)) {
1986 di->flags.mainextchnotok = false;
1987 ab8500_power_supply_changed(di, di->ac_chg.psy);
1990 if (di->flags.vbus_ovv) {
1991 ret = abx500_get_register_interruptible(di->dev,
1992 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG,
1993 &reg_value);
1994 if (ret < 0) {
1995 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1996 return;
1998 if (!(reg_value & VBUS_OVV_TH)) {
1999 di->flags.vbus_ovv = false;
2000 ab8500_power_supply_changed(di, di->usb_chg.psy);
2003 /* If we still have a failure, schedule a new check */
2004 if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
2005 queue_delayed_work(di->charger_wq,
2006 &di->check_hw_failure_work, round_jiffies(HZ));
2011 * ab8500_charger_kick_watchdog_work() - kick the watchdog
2012 * @work: pointer to the work_struct structure
2014 * Work queue function for kicking the charger watchdog.
2016 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
2017 * logic. That means we have to continously kick the charger
2018 * watchdog even when no charger is connected. This is only
2019 * valid once the AC charger has been enabled. This is
2020 * a bug that is not handled by the algorithm and the
2021 * watchdog have to be kicked by the charger driver
2022 * when the AC charger is disabled
2024 static void ab8500_charger_kick_watchdog_work(struct work_struct *work)
2026 int ret;
2028 struct ab8500_charger *di = container_of(work,
2029 struct ab8500_charger, kick_wd_work.work);
2031 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
2032 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
2033 if (ret)
2034 dev_err(di->dev, "Failed to kick WD!\n");
2036 /* Schedule a new watchdog kick */
2037 queue_delayed_work(di->charger_wq,
2038 &di->kick_wd_work, round_jiffies(WD_KICK_INTERVAL));
2042 * ab8500_charger_ac_work() - work to get and set main charger status
2043 * @work: pointer to the work_struct structure
2045 * Work queue function for checking the main charger status
2047 static void ab8500_charger_ac_work(struct work_struct *work)
2049 int ret;
2051 struct ab8500_charger *di = container_of(work,
2052 struct ab8500_charger, ac_work);
2055 * Since we can't be sure that the events are received
2056 * synchronously, we have the check if the main charger is
2057 * connected by reading the status register
2059 ret = ab8500_charger_detect_chargers(di, false);
2060 if (ret < 0)
2061 return;
2063 if (ret & AC_PW_CONN) {
2064 di->ac.charger_connected = 1;
2065 di->ac_conn = true;
2066 } else {
2067 di->ac.charger_connected = 0;
2070 ab8500_power_supply_changed(di, di->ac_chg.psy);
2071 sysfs_notify(&di->ac_chg.psy->dev.kobj, NULL, "present");
2074 static void ab8500_charger_usb_attached_work(struct work_struct *work)
2076 struct ab8500_charger *di = container_of(work,
2077 struct ab8500_charger,
2078 usb_charger_attached_work.work);
2079 int usbch = (USB_CH_VBUSDROP | USB_CH_VBUSDETDBNC);
2080 int ret, i;
2081 u8 statval;
2083 for (i = 0; i < 10; i++) {
2084 ret = abx500_get_register_interruptible(di->dev,
2085 AB8500_CHARGER,
2086 AB8500_CH_USBCH_STAT1_REG,
2087 &statval);
2088 if (ret < 0) {
2089 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
2090 goto reschedule;
2092 if ((statval & usbch) != usbch)
2093 goto reschedule;
2095 msleep(CHARGER_STATUS_POLL);
2098 ab8500_charger_usb_en(&di->usb_chg, 0, 0, 0);
2100 mutex_lock(&di->charger_attached_mutex);
2101 mutex_unlock(&di->charger_attached_mutex);
2103 return;
2105 reschedule:
2106 queue_delayed_work(di->charger_wq,
2107 &di->usb_charger_attached_work,
2108 HZ);
2111 static void ab8500_charger_ac_attached_work(struct work_struct *work)
2114 struct ab8500_charger *di = container_of(work,
2115 struct ab8500_charger,
2116 ac_charger_attached_work.work);
2117 int mainch = (MAIN_CH_STATUS2_MAINCHGDROP |
2118 MAIN_CH_STATUS2_MAINCHARGERDETDBNC);
2119 int ret, i;
2120 u8 statval;
2122 for (i = 0; i < 10; i++) {
2123 ret = abx500_get_register_interruptible(di->dev,
2124 AB8500_CHARGER,
2125 AB8500_CH_STATUS2_REG,
2126 &statval);
2127 if (ret < 0) {
2128 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
2129 goto reschedule;
2132 if ((statval & mainch) != mainch)
2133 goto reschedule;
2135 msleep(CHARGER_STATUS_POLL);
2138 ab8500_charger_ac_en(&di->ac_chg, 0, 0, 0);
2139 queue_work(di->charger_wq, &di->ac_work);
2141 mutex_lock(&di->charger_attached_mutex);
2142 mutex_unlock(&di->charger_attached_mutex);
2144 return;
2146 reschedule:
2147 queue_delayed_work(di->charger_wq,
2148 &di->ac_charger_attached_work,
2149 HZ);
2153 * ab8500_charger_detect_usb_type_work() - work to detect USB type
2154 * @work: Pointer to the work_struct structure
2156 * Detect the type of USB plugged
2158 static void ab8500_charger_detect_usb_type_work(struct work_struct *work)
2160 int ret;
2162 struct ab8500_charger *di = container_of(work,
2163 struct ab8500_charger, detect_usb_type_work);
2166 * Since we can't be sure that the events are received
2167 * synchronously, we have the check if is
2168 * connected by reading the status register
2170 ret = ab8500_charger_detect_chargers(di, false);
2171 if (ret < 0)
2172 return;
2174 if (!(ret & USB_PW_CONN)) {
2175 dev_dbg(di->dev, "%s di->vbus_detected = false\n", __func__);
2176 di->vbus_detected = false;
2177 ab8500_charger_set_usb_connected(di, false);
2178 ab8500_power_supply_changed(di, di->usb_chg.psy);
2179 } else {
2180 dev_dbg(di->dev, "%s di->vbus_detected = true\n", __func__);
2181 di->vbus_detected = true;
2183 if (is_ab8500_1p1_or_earlier(di->parent)) {
2184 ret = ab8500_charger_detect_usb_type(di);
2185 if (!ret) {
2186 ab8500_charger_set_usb_connected(di, true);
2187 ab8500_power_supply_changed(di,
2188 di->usb_chg.psy);
2190 } else {
2192 * For ABB cut2.0 and onwards we have an IRQ,
2193 * USB_LINK_STATUS that will be triggered when the USB
2194 * link status changes. The exception is USB connected
2195 * during startup. Then we don't get a
2196 * USB_LINK_STATUS IRQ
2198 if (di->vbus_detected_start) {
2199 di->vbus_detected_start = false;
2200 ret = ab8500_charger_detect_usb_type(di);
2201 if (!ret) {
2202 ab8500_charger_set_usb_connected(di,
2203 true);
2204 ab8500_power_supply_changed(di,
2205 di->usb_chg.psy);
2213 * ab8500_charger_usb_link_attach_work() - work to detect USB type
2214 * @work: pointer to the work_struct structure
2216 * Detect the type of USB plugged
2218 static void ab8500_charger_usb_link_attach_work(struct work_struct *work)
2220 struct ab8500_charger *di =
2221 container_of(work, struct ab8500_charger, attach_work.work);
2222 int ret;
2224 /* Update maximum input current if USB enumeration is not detected */
2225 if (!di->usb.charger_online) {
2226 ret = ab8500_charger_set_vbus_in_curr(di,
2227 di->max_usb_in_curr.usb_type_max);
2228 if (ret)
2229 return;
2232 ab8500_charger_set_usb_connected(di, true);
2233 ab8500_power_supply_changed(di, di->usb_chg.psy);
2237 * ab8500_charger_usb_link_status_work() - work to detect USB type
2238 * @work: pointer to the work_struct structure
2240 * Detect the type of USB plugged
2242 static void ab8500_charger_usb_link_status_work(struct work_struct *work)
2244 int detected_chargers;
2245 int ret;
2246 u8 val;
2247 u8 link_status;
2249 struct ab8500_charger *di = container_of(work,
2250 struct ab8500_charger, usb_link_status_work);
2253 * Since we can't be sure that the events are received
2254 * synchronously, we have the check if is
2255 * connected by reading the status register
2257 detected_chargers = ab8500_charger_detect_chargers(di, false);
2258 if (detected_chargers < 0)
2259 return;
2262 * Some chargers that breaks the USB spec is
2263 * identified as invalid by AB8500 and it refuse
2264 * to start the charging process. but by jumping
2265 * thru a few hoops it can be forced to start.
2267 if (is_ab8500(di->parent))
2268 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
2269 AB8500_USB_LINE_STAT_REG, &val);
2270 else
2271 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
2272 AB8500_USB_LINK1_STAT_REG, &val);
2274 if (ret >= 0)
2275 dev_dbg(di->dev, "UsbLineStatus register = 0x%02x\n", val);
2276 else
2277 dev_dbg(di->dev, "Error reading USB link status\n");
2279 if (is_ab8500(di->parent))
2280 link_status = AB8500_USB_LINK_STATUS;
2281 else
2282 link_status = AB8505_USB_LINK_STATUS;
2284 if (detected_chargers & USB_PW_CONN) {
2285 if (((val & link_status) >> USB_LINK_STATUS_SHIFT) ==
2286 USB_STAT_NOT_VALID_LINK &&
2287 di->invalid_charger_detect_state == 0) {
2288 dev_dbg(di->dev,
2289 "Invalid charger detected, state= 0\n");
2290 /*Enable charger*/
2291 abx500_mask_and_set_register_interruptible(di->dev,
2292 AB8500_CHARGER, AB8500_USBCH_CTRL1_REG,
2293 USB_CH_ENA, USB_CH_ENA);
2294 /*Enable charger detection*/
2295 abx500_mask_and_set_register_interruptible(di->dev,
2296 AB8500_USB, AB8500_USB_LINE_CTRL2_REG,
2297 USB_CH_DET, USB_CH_DET);
2298 di->invalid_charger_detect_state = 1;
2299 /*exit and wait for new link status interrupt.*/
2300 return;
2303 if (di->invalid_charger_detect_state == 1) {
2304 dev_dbg(di->dev,
2305 "Invalid charger detected, state= 1\n");
2306 /*Stop charger detection*/
2307 abx500_mask_and_set_register_interruptible(di->dev,
2308 AB8500_USB, AB8500_USB_LINE_CTRL2_REG,
2309 USB_CH_DET, 0x00);
2310 /*Check link status*/
2311 if (is_ab8500(di->parent))
2312 ret = abx500_get_register_interruptible(di->dev,
2313 AB8500_USB, AB8500_USB_LINE_STAT_REG,
2314 &val);
2315 else
2316 ret = abx500_get_register_interruptible(di->dev,
2317 AB8500_USB, AB8500_USB_LINK1_STAT_REG,
2318 &val);
2320 dev_dbg(di->dev, "USB link status= 0x%02x\n",
2321 (val & link_status) >> USB_LINK_STATUS_SHIFT);
2322 di->invalid_charger_detect_state = 2;
2324 } else {
2325 di->invalid_charger_detect_state = 0;
2328 if (!(detected_chargers & USB_PW_CONN)) {
2329 di->vbus_detected = false;
2330 ab8500_charger_set_usb_connected(di, false);
2331 ab8500_power_supply_changed(di, di->usb_chg.psy);
2332 return;
2335 dev_dbg(di->dev,"%s di->vbus_detected = true\n",__func__);
2336 di->vbus_detected = true;
2337 ret = ab8500_charger_read_usb_type(di);
2338 if (ret) {
2339 if (ret == -ENXIO) {
2340 /* No valid charger type detected */
2341 ab8500_charger_set_usb_connected(di, false);
2342 ab8500_power_supply_changed(di, di->usb_chg.psy);
2344 return;
2347 if (di->usb_device_is_unrecognised) {
2348 dev_dbg(di->dev,
2349 "Potential Legacy Charger device. "
2350 "Delay work for %d msec for USB enum "
2351 "to finish",
2352 WAIT_ACA_RID_ENUMERATION);
2353 queue_delayed_work(di->charger_wq,
2354 &di->attach_work,
2355 msecs_to_jiffies(WAIT_ACA_RID_ENUMERATION));
2356 } else if (di->is_aca_rid == 1) {
2357 /* Only wait once */
2358 di->is_aca_rid++;
2359 dev_dbg(di->dev,
2360 "%s Wait %d msec for USB enum to finish",
2361 __func__, WAIT_ACA_RID_ENUMERATION);
2362 queue_delayed_work(di->charger_wq,
2363 &di->attach_work,
2364 msecs_to_jiffies(WAIT_ACA_RID_ENUMERATION));
2365 } else {
2366 queue_delayed_work(di->charger_wq,
2367 &di->attach_work,
2372 static void ab8500_charger_usb_state_changed_work(struct work_struct *work)
2374 int ret;
2375 unsigned long flags;
2377 struct ab8500_charger *di = container_of(work,
2378 struct ab8500_charger, usb_state_changed_work.work);
2380 if (!di->vbus_detected) {
2381 dev_dbg(di->dev,
2382 "%s !di->vbus_detected\n",
2383 __func__);
2384 return;
2387 spin_lock_irqsave(&di->usb_state.usb_lock, flags);
2388 di->usb_state.state = di->usb_state.state_tmp;
2389 di->usb_state.usb_current = di->usb_state.usb_current_tmp;
2390 spin_unlock_irqrestore(&di->usb_state.usb_lock, flags);
2392 dev_dbg(di->dev, "%s USB state: 0x%02x mA: %d\n",
2393 __func__, di->usb_state.state, di->usb_state.usb_current);
2395 switch (di->usb_state.state) {
2396 case AB8500_BM_USB_STATE_RESET_HS:
2397 case AB8500_BM_USB_STATE_RESET_FS:
2398 case AB8500_BM_USB_STATE_SUSPEND:
2399 case AB8500_BM_USB_STATE_MAX:
2400 ab8500_charger_set_usb_connected(di, false);
2401 ab8500_power_supply_changed(di, di->usb_chg.psy);
2402 break;
2404 case AB8500_BM_USB_STATE_RESUME:
2406 * when suspend->resume there should be delay
2407 * of 1sec for enabling charging
2409 msleep(1000);
2410 /* Intentional fall through */
2411 case AB8500_BM_USB_STATE_CONFIGURED:
2413 * USB is configured, enable charging with the charging
2414 * input current obtained from USB driver
2416 if (!ab8500_charger_get_usb_cur(di)) {
2417 /* Update maximum input current */
2418 ret = ab8500_charger_set_vbus_in_curr(di,
2419 di->max_usb_in_curr.usb_type_max);
2420 if (ret)
2421 return;
2423 ab8500_charger_set_usb_connected(di, true);
2424 ab8500_power_supply_changed(di, di->usb_chg.psy);
2426 break;
2428 default:
2429 break;
2434 * ab8500_charger_check_usbchargernotok_work() - check USB chg not ok status
2435 * @work: pointer to the work_struct structure
2437 * Work queue function for checking the USB charger Not OK status
2439 static void ab8500_charger_check_usbchargernotok_work(struct work_struct *work)
2441 int ret;
2442 u8 reg_value;
2443 bool prev_status;
2445 struct ab8500_charger *di = container_of(work,
2446 struct ab8500_charger, check_usbchgnotok_work.work);
2448 /* Check if the status bit for usbchargernotok is still active */
2449 ret = abx500_get_register_interruptible(di->dev,
2450 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, &reg_value);
2451 if (ret < 0) {
2452 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2453 return;
2455 prev_status = di->flags.usbchargernotok;
2457 if (reg_value & VBUS_CH_NOK) {
2458 di->flags.usbchargernotok = true;
2459 /* Check again in 1sec */
2460 queue_delayed_work(di->charger_wq,
2461 &di->check_usbchgnotok_work, HZ);
2462 } else {
2463 di->flags.usbchargernotok = false;
2464 di->flags.vbus_collapse = false;
2467 if (prev_status != di->flags.usbchargernotok)
2468 ab8500_power_supply_changed(di, di->usb_chg.psy);
2472 * ab8500_charger_check_main_thermal_prot_work() - check main thermal status
2473 * @work: pointer to the work_struct structure
2475 * Work queue function for checking the Main thermal prot status
2477 static void ab8500_charger_check_main_thermal_prot_work(
2478 struct work_struct *work)
2480 int ret;
2481 u8 reg_value;
2483 struct ab8500_charger *di = container_of(work,
2484 struct ab8500_charger, check_main_thermal_prot_work);
2486 /* Check if the status bit for main_thermal_prot is still active */
2487 ret = abx500_get_register_interruptible(di->dev,
2488 AB8500_CHARGER, AB8500_CH_STATUS2_REG, &reg_value);
2489 if (ret < 0) {
2490 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2491 return;
2493 if (reg_value & MAIN_CH_TH_PROT)
2494 di->flags.main_thermal_prot = true;
2495 else
2496 di->flags.main_thermal_prot = false;
2498 ab8500_power_supply_changed(di, di->ac_chg.psy);
2502 * ab8500_charger_check_usb_thermal_prot_work() - check usb thermal status
2503 * @work: pointer to the work_struct structure
2505 * Work queue function for checking the USB thermal prot status
2507 static void ab8500_charger_check_usb_thermal_prot_work(
2508 struct work_struct *work)
2510 int ret;
2511 u8 reg_value;
2513 struct ab8500_charger *di = container_of(work,
2514 struct ab8500_charger, check_usb_thermal_prot_work);
2516 /* Check if the status bit for usb_thermal_prot is still active */
2517 ret = abx500_get_register_interruptible(di->dev,
2518 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, &reg_value);
2519 if (ret < 0) {
2520 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2521 return;
2523 if (reg_value & USB_CH_TH_PROT)
2524 di->flags.usb_thermal_prot = true;
2525 else
2526 di->flags.usb_thermal_prot = false;
2528 ab8500_power_supply_changed(di, di->usb_chg.psy);
2532 * ab8500_charger_mainchunplugdet_handler() - main charger unplugged
2533 * @irq: interrupt number
2534 * @_di: pointer to the ab8500_charger structure
2536 * Returns IRQ status(IRQ_HANDLED)
2538 static irqreturn_t ab8500_charger_mainchunplugdet_handler(int irq, void *_di)
2540 struct ab8500_charger *di = _di;
2542 dev_dbg(di->dev, "Main charger unplugged\n");
2543 queue_work(di->charger_wq, &di->ac_work);
2545 cancel_delayed_work_sync(&di->ac_charger_attached_work);
2546 mutex_lock(&di->charger_attached_mutex);
2547 mutex_unlock(&di->charger_attached_mutex);
2549 return IRQ_HANDLED;
2553 * ab8500_charger_mainchplugdet_handler() - main charger plugged
2554 * @irq: interrupt number
2555 * @_di: pointer to the ab8500_charger structure
2557 * Returns IRQ status(IRQ_HANDLED)
2559 static irqreturn_t ab8500_charger_mainchplugdet_handler(int irq, void *_di)
2561 struct ab8500_charger *di = _di;
2563 dev_dbg(di->dev, "Main charger plugged\n");
2564 queue_work(di->charger_wq, &di->ac_work);
2566 mutex_lock(&di->charger_attached_mutex);
2567 mutex_unlock(&di->charger_attached_mutex);
2569 if (is_ab8500(di->parent))
2570 queue_delayed_work(di->charger_wq,
2571 &di->ac_charger_attached_work,
2572 HZ);
2573 return IRQ_HANDLED;
2577 * ab8500_charger_mainextchnotok_handler() - main charger not ok
2578 * @irq: interrupt number
2579 * @_di: pointer to the ab8500_charger structure
2581 * Returns IRQ status(IRQ_HANDLED)
2583 static irqreturn_t ab8500_charger_mainextchnotok_handler(int irq, void *_di)
2585 struct ab8500_charger *di = _di;
2587 dev_dbg(di->dev, "Main charger not ok\n");
2588 di->flags.mainextchnotok = true;
2589 ab8500_power_supply_changed(di, di->ac_chg.psy);
2591 /* Schedule a new HW failure check */
2592 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
2594 return IRQ_HANDLED;
2598 * ab8500_charger_mainchthprotr_handler() - Die temp is above main charger
2599 * thermal protection threshold
2600 * @irq: interrupt number
2601 * @_di: pointer to the ab8500_charger structure
2603 * Returns IRQ status(IRQ_HANDLED)
2605 static irqreturn_t ab8500_charger_mainchthprotr_handler(int irq, void *_di)
2607 struct ab8500_charger *di = _di;
2609 dev_dbg(di->dev,
2610 "Die temp above Main charger thermal protection threshold\n");
2611 queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
2613 return IRQ_HANDLED;
2617 * ab8500_charger_mainchthprotf_handler() - Die temp is below main charger
2618 * thermal protection threshold
2619 * @irq: interrupt number
2620 * @_di: pointer to the ab8500_charger structure
2622 * Returns IRQ status(IRQ_HANDLED)
2624 static irqreturn_t ab8500_charger_mainchthprotf_handler(int irq, void *_di)
2626 struct ab8500_charger *di = _di;
2628 dev_dbg(di->dev,
2629 "Die temp ok for Main charger thermal protection threshold\n");
2630 queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
2632 return IRQ_HANDLED;
2635 static void ab8500_charger_vbus_drop_end_work(struct work_struct *work)
2637 struct ab8500_charger *di = container_of(work,
2638 struct ab8500_charger, vbus_drop_end_work.work);
2639 int ret, curr;
2640 u8 reg_value;
2642 di->flags.vbus_drop_end = false;
2644 /* Reset the drop counter */
2645 abx500_set_register_interruptible(di->dev,
2646 AB8500_CHARGER, AB8500_CHARGER_CTRL, 0x01);
2648 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
2649 AB8500_CH_USBCH_STAT2_REG, &reg_value);
2650 if (ret < 0) {
2651 dev_err(di->dev, "%s read failed\n", __func__);
2652 return;
2655 curr = di->bm->chg_input_curr[
2656 reg_value >> AUTO_VBUS_IN_CURR_LIM_SHIFT];
2658 if (di->max_usb_in_curr.calculated_max != curr) {
2659 /* USB source is collapsing */
2660 di->max_usb_in_curr.calculated_max = curr;
2661 dev_dbg(di->dev,
2662 "VBUS input current limiting to %d mA\n",
2663 di->max_usb_in_curr.calculated_max);
2664 } else {
2666 * USB source can not give more than this amount.
2667 * Taking more will collapse the source.
2669 di->max_usb_in_curr.set_max =
2670 di->max_usb_in_curr.calculated_max;
2671 dev_dbg(di->dev,
2672 "VBUS input current limited to %d mA\n",
2673 di->max_usb_in_curr.set_max);
2676 if (di->usb.charger_connected)
2677 ab8500_charger_set_vbus_in_curr(di,
2678 di->max_usb_in_curr.usb_type_max);
2682 * ab8500_charger_vbusdetf_handler() - VBUS falling detected
2683 * @irq: interrupt number
2684 * @_di: pointer to the ab8500_charger structure
2686 * Returns IRQ status(IRQ_HANDLED)
2688 static irqreturn_t ab8500_charger_vbusdetf_handler(int irq, void *_di)
2690 struct ab8500_charger *di = _di;
2692 di->vbus_detected = false;
2693 dev_dbg(di->dev, "VBUS falling detected\n");
2694 queue_work(di->charger_wq, &di->detect_usb_type_work);
2696 return IRQ_HANDLED;
2700 * ab8500_charger_vbusdetr_handler() - VBUS rising detected
2701 * @irq: interrupt number
2702 * @_di: pointer to the ab8500_charger structure
2704 * Returns IRQ status(IRQ_HANDLED)
2706 static irqreturn_t ab8500_charger_vbusdetr_handler(int irq, void *_di)
2708 struct ab8500_charger *di = _di;
2710 di->vbus_detected = true;
2711 dev_dbg(di->dev, "VBUS rising detected\n");
2713 queue_work(di->charger_wq, &di->detect_usb_type_work);
2715 return IRQ_HANDLED;
2719 * ab8500_charger_usblinkstatus_handler() - USB link status has changed
2720 * @irq: interrupt number
2721 * @_di: pointer to the ab8500_charger structure
2723 * Returns IRQ status(IRQ_HANDLED)
2725 static irqreturn_t ab8500_charger_usblinkstatus_handler(int irq, void *_di)
2727 struct ab8500_charger *di = _di;
2729 dev_dbg(di->dev, "USB link status changed\n");
2731 queue_work(di->charger_wq, &di->usb_link_status_work);
2733 return IRQ_HANDLED;
2737 * ab8500_charger_usbchthprotr_handler() - Die temp is above usb charger
2738 * thermal protection threshold
2739 * @irq: interrupt number
2740 * @_di: pointer to the ab8500_charger structure
2742 * Returns IRQ status(IRQ_HANDLED)
2744 static irqreturn_t ab8500_charger_usbchthprotr_handler(int irq, void *_di)
2746 struct ab8500_charger *di = _di;
2748 dev_dbg(di->dev,
2749 "Die temp above USB charger thermal protection threshold\n");
2750 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2752 return IRQ_HANDLED;
2756 * ab8500_charger_usbchthprotf_handler() - Die temp is below usb charger
2757 * thermal protection threshold
2758 * @irq: interrupt number
2759 * @_di: pointer to the ab8500_charger structure
2761 * Returns IRQ status(IRQ_HANDLED)
2763 static irqreturn_t ab8500_charger_usbchthprotf_handler(int irq, void *_di)
2765 struct ab8500_charger *di = _di;
2767 dev_dbg(di->dev,
2768 "Die temp ok for USB charger thermal protection threshold\n");
2769 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2771 return IRQ_HANDLED;
2775 * ab8500_charger_usbchargernotokr_handler() - USB charger not ok detected
2776 * @irq: interrupt number
2777 * @_di: pointer to the ab8500_charger structure
2779 * Returns IRQ status(IRQ_HANDLED)
2781 static irqreturn_t ab8500_charger_usbchargernotokr_handler(int irq, void *_di)
2783 struct ab8500_charger *di = _di;
2785 dev_dbg(di->dev, "Not allowed USB charger detected\n");
2786 queue_delayed_work(di->charger_wq, &di->check_usbchgnotok_work, 0);
2788 return IRQ_HANDLED;
2792 * ab8500_charger_chwdexp_handler() - Charger watchdog expired
2793 * @irq: interrupt number
2794 * @_di: pointer to the ab8500_charger structure
2796 * Returns IRQ status(IRQ_HANDLED)
2798 static irqreturn_t ab8500_charger_chwdexp_handler(int irq, void *_di)
2800 struct ab8500_charger *di = _di;
2802 dev_dbg(di->dev, "Charger watchdog expired\n");
2805 * The charger that was online when the watchdog expired
2806 * needs to be restarted for charging to start again
2808 if (di->ac.charger_online) {
2809 di->ac.wd_expired = true;
2810 ab8500_power_supply_changed(di, di->ac_chg.psy);
2812 if (di->usb.charger_online) {
2813 di->usb.wd_expired = true;
2814 ab8500_power_supply_changed(di, di->usb_chg.psy);
2817 return IRQ_HANDLED;
2821 * ab8500_charger_vbuschdropend_handler() - VBUS drop removed
2822 * @irq: interrupt number
2823 * @_di: pointer to the ab8500_charger structure
2825 * Returns IRQ status(IRQ_HANDLED)
2827 static irqreturn_t ab8500_charger_vbuschdropend_handler(int irq, void *_di)
2829 struct ab8500_charger *di = _di;
2831 dev_dbg(di->dev, "VBUS charger drop ended\n");
2832 di->flags.vbus_drop_end = true;
2835 * VBUS might have dropped due to bad connection.
2836 * Schedule a new input limit set to the value SW requests.
2838 queue_delayed_work(di->charger_wq, &di->vbus_drop_end_work,
2839 round_jiffies(VBUS_IN_CURR_LIM_RETRY_SET_TIME * HZ));
2841 return IRQ_HANDLED;
2845 * ab8500_charger_vbusovv_handler() - VBUS overvoltage detected
2846 * @irq: interrupt number
2847 * @_di: pointer to the ab8500_charger structure
2849 * Returns IRQ status(IRQ_HANDLED)
2851 static irqreturn_t ab8500_charger_vbusovv_handler(int irq, void *_di)
2853 struct ab8500_charger *di = _di;
2855 dev_dbg(di->dev, "VBUS overvoltage detected\n");
2856 di->flags.vbus_ovv = true;
2857 ab8500_power_supply_changed(di, di->usb_chg.psy);
2859 /* Schedule a new HW failure check */
2860 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
2862 return IRQ_HANDLED;
2866 * ab8500_charger_ac_get_property() - get the ac/mains properties
2867 * @psy: pointer to the power_supply structure
2868 * @psp: pointer to the power_supply_property structure
2869 * @val: pointer to the power_supply_propval union
2871 * This function gets called when an application tries to get the ac/mains
2872 * properties by reading the sysfs files.
2873 * AC/Mains properties are online, present and voltage.
2874 * online: ac/mains charging is in progress or not
2875 * present: presence of the ac/mains
2876 * voltage: AC/Mains voltage
2877 * Returns error code in case of failure else 0(on success)
2879 static int ab8500_charger_ac_get_property(struct power_supply *psy,
2880 enum power_supply_property psp,
2881 union power_supply_propval *val)
2883 struct ab8500_charger *di;
2884 int ret;
2886 di = to_ab8500_charger_ac_device_info(psy_to_ux500_charger(psy));
2888 switch (psp) {
2889 case POWER_SUPPLY_PROP_HEALTH:
2890 if (di->flags.mainextchnotok)
2891 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
2892 else if (di->ac.wd_expired || di->usb.wd_expired)
2893 val->intval = POWER_SUPPLY_HEALTH_DEAD;
2894 else if (di->flags.main_thermal_prot)
2895 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
2896 else
2897 val->intval = POWER_SUPPLY_HEALTH_GOOD;
2898 break;
2899 case POWER_SUPPLY_PROP_ONLINE:
2900 val->intval = di->ac.charger_online;
2901 break;
2902 case POWER_SUPPLY_PROP_PRESENT:
2903 val->intval = di->ac.charger_connected;
2904 break;
2905 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
2906 ret = ab8500_charger_get_ac_voltage(di);
2907 if (ret >= 0)
2908 di->ac.charger_voltage = ret;
2909 /* On error, use previous value */
2910 val->intval = di->ac.charger_voltage * 1000;
2911 break;
2912 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
2914 * This property is used to indicate when CV mode is entered
2915 * for the AC charger
2917 di->ac.cv_active = ab8500_charger_ac_cv(di);
2918 val->intval = di->ac.cv_active;
2919 break;
2920 case POWER_SUPPLY_PROP_CURRENT_NOW:
2921 ret = ab8500_charger_get_ac_current(di);
2922 if (ret >= 0)
2923 di->ac.charger_current = ret;
2924 val->intval = di->ac.charger_current * 1000;
2925 break;
2926 default:
2927 return -EINVAL;
2929 return 0;
2933 * ab8500_charger_usb_get_property() - get the usb properties
2934 * @psy: pointer to the power_supply structure
2935 * @psp: pointer to the power_supply_property structure
2936 * @val: pointer to the power_supply_propval union
2938 * This function gets called when an application tries to get the usb
2939 * properties by reading the sysfs files.
2940 * USB properties are online, present and voltage.
2941 * online: usb charging is in progress or not
2942 * present: presence of the usb
2943 * voltage: vbus voltage
2944 * Returns error code in case of failure else 0(on success)
2946 static int ab8500_charger_usb_get_property(struct power_supply *psy,
2947 enum power_supply_property psp,
2948 union power_supply_propval *val)
2950 struct ab8500_charger *di;
2951 int ret;
2953 di = to_ab8500_charger_usb_device_info(psy_to_ux500_charger(psy));
2955 switch (psp) {
2956 case POWER_SUPPLY_PROP_HEALTH:
2957 if (di->flags.usbchargernotok)
2958 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
2959 else if (di->ac.wd_expired || di->usb.wd_expired)
2960 val->intval = POWER_SUPPLY_HEALTH_DEAD;
2961 else if (di->flags.usb_thermal_prot)
2962 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
2963 else if (di->flags.vbus_ovv)
2964 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
2965 else
2966 val->intval = POWER_SUPPLY_HEALTH_GOOD;
2967 break;
2968 case POWER_SUPPLY_PROP_ONLINE:
2969 val->intval = di->usb.charger_online;
2970 break;
2971 case POWER_SUPPLY_PROP_PRESENT:
2972 val->intval = di->usb.charger_connected;
2973 break;
2974 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
2975 ret = ab8500_charger_get_vbus_voltage(di);
2976 if (ret >= 0)
2977 di->usb.charger_voltage = ret;
2978 val->intval = di->usb.charger_voltage * 1000;
2979 break;
2980 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
2982 * This property is used to indicate when CV mode is entered
2983 * for the USB charger
2985 di->usb.cv_active = ab8500_charger_usb_cv(di);
2986 val->intval = di->usb.cv_active;
2987 break;
2988 case POWER_SUPPLY_PROP_CURRENT_NOW:
2989 ret = ab8500_charger_get_usb_current(di);
2990 if (ret >= 0)
2991 di->usb.charger_current = ret;
2992 val->intval = di->usb.charger_current * 1000;
2993 break;
2994 case POWER_SUPPLY_PROP_CURRENT_AVG:
2996 * This property is used to indicate when VBUS has collapsed
2997 * due to too high output current from the USB charger
2999 if (di->flags.vbus_collapse)
3000 val->intval = 1;
3001 else
3002 val->intval = 0;
3003 break;
3004 default:
3005 return -EINVAL;
3007 return 0;
3011 * ab8500_charger_init_hw_registers() - Set up charger related registers
3012 * @di: pointer to the ab8500_charger structure
3014 * Set up charger OVV, watchdog and maximum voltage registers as well as
3015 * charging of the backup battery
3017 static int ab8500_charger_init_hw_registers(struct ab8500_charger *di)
3019 int ret = 0;
3021 /* Setup maximum charger current and voltage for ABB cut2.0 */
3022 if (!is_ab8500_1p1_or_earlier(di->parent)) {
3023 ret = abx500_set_register_interruptible(di->dev,
3024 AB8500_CHARGER,
3025 AB8500_CH_VOLT_LVL_MAX_REG, CH_VOL_LVL_4P6);
3026 if (ret) {
3027 dev_err(di->dev,
3028 "failed to set CH_VOLT_LVL_MAX_REG\n");
3029 goto out;
3032 ret = abx500_set_register_interruptible(di->dev,
3033 AB8500_CHARGER, AB8500_CH_OPT_CRNTLVL_MAX_REG,
3034 CH_OP_CUR_LVL_1P6);
3035 if (ret) {
3036 dev_err(di->dev,
3037 "failed to set CH_OPT_CRNTLVL_MAX_REG\n");
3038 goto out;
3042 if (is_ab8505_2p0(di->parent))
3043 ret = abx500_mask_and_set_register_interruptible(di->dev,
3044 AB8500_CHARGER,
3045 AB8500_USBCH_CTRL2_REG,
3046 VBUS_AUTO_IN_CURR_LIM_ENA,
3047 VBUS_AUTO_IN_CURR_LIM_ENA);
3048 else
3050 * VBUS OVV set to 6.3V and enable automatic current limitation
3052 ret = abx500_set_register_interruptible(di->dev,
3053 AB8500_CHARGER,
3054 AB8500_USBCH_CTRL2_REG,
3055 VBUS_OVV_SELECT_6P3V | VBUS_AUTO_IN_CURR_LIM_ENA);
3056 if (ret) {
3057 dev_err(di->dev,
3058 "failed to set automatic current limitation\n");
3059 goto out;
3062 /* Enable main watchdog in OTP */
3063 ret = abx500_set_register_interruptible(di->dev,
3064 AB8500_OTP_EMUL, AB8500_OTP_CONF_15, OTP_ENABLE_WD);
3065 if (ret) {
3066 dev_err(di->dev, "failed to enable main WD in OTP\n");
3067 goto out;
3070 /* Enable main watchdog */
3071 ret = abx500_set_register_interruptible(di->dev,
3072 AB8500_SYS_CTRL2_BLOCK,
3073 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_ENA);
3074 if (ret) {
3075 dev_err(di->dev, "failed to enable main watchdog\n");
3076 goto out;
3080 * Due to internal synchronisation, Enable and Kick watchdog bits
3081 * cannot be enabled in a single write.
3082 * A minimum delay of 2*32 kHz period (62.5µs) must be inserted
3083 * between writing Enable then Kick bits.
3085 udelay(63);
3087 /* Kick main watchdog */
3088 ret = abx500_set_register_interruptible(di->dev,
3089 AB8500_SYS_CTRL2_BLOCK,
3090 AB8500_MAIN_WDOG_CTRL_REG,
3091 (MAIN_WDOG_ENA | MAIN_WDOG_KICK));
3092 if (ret) {
3093 dev_err(di->dev, "failed to kick main watchdog\n");
3094 goto out;
3097 /* Disable main watchdog */
3098 ret = abx500_set_register_interruptible(di->dev,
3099 AB8500_SYS_CTRL2_BLOCK,
3100 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_DIS);
3101 if (ret) {
3102 dev_err(di->dev, "failed to disable main watchdog\n");
3103 goto out;
3106 /* Set watchdog timeout */
3107 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
3108 AB8500_CH_WD_TIMER_REG, WD_TIMER);
3109 if (ret) {
3110 dev_err(di->dev, "failed to set charger watchdog timeout\n");
3111 goto out;
3114 ret = ab8500_charger_led_en(di, false);
3115 if (ret < 0) {
3116 dev_err(di->dev, "failed to disable LED\n");
3117 goto out;
3120 ret = abx500_set_register_interruptible(di->dev,
3121 AB8500_RTC,
3122 AB8500_RTC_BACKUP_CHG_REG,
3123 (di->bm->bkup_bat_v & 0x3) | di->bm->bkup_bat_i);
3124 if (ret) {
3125 dev_err(di->dev, "failed to setup backup battery charging\n");
3126 goto out;
3129 /* Enable backup battery charging */
3130 ret = abx500_mask_and_set_register_interruptible(di->dev,
3131 AB8500_RTC, AB8500_RTC_CTRL_REG,
3132 RTC_BUP_CH_ENA, RTC_BUP_CH_ENA);
3133 if (ret < 0) {
3134 dev_err(di->dev, "%s mask and set failed\n", __func__);
3135 goto out;
3138 out:
3139 return ret;
3143 * ab8500 charger driver interrupts and their respective isr
3145 static struct ab8500_charger_interrupts ab8500_charger_irq[] = {
3146 {"MAIN_CH_UNPLUG_DET", ab8500_charger_mainchunplugdet_handler},
3147 {"MAIN_CHARGE_PLUG_DET", ab8500_charger_mainchplugdet_handler},
3148 {"MAIN_EXT_CH_NOT_OK", ab8500_charger_mainextchnotok_handler},
3149 {"MAIN_CH_TH_PROT_R", ab8500_charger_mainchthprotr_handler},
3150 {"MAIN_CH_TH_PROT_F", ab8500_charger_mainchthprotf_handler},
3151 {"VBUS_DET_F", ab8500_charger_vbusdetf_handler},
3152 {"VBUS_DET_R", ab8500_charger_vbusdetr_handler},
3153 {"USB_LINK_STATUS", ab8500_charger_usblinkstatus_handler},
3154 {"USB_CH_TH_PROT_R", ab8500_charger_usbchthprotr_handler},
3155 {"USB_CH_TH_PROT_F", ab8500_charger_usbchthprotf_handler},
3156 {"USB_CHARGER_NOT_OKR", ab8500_charger_usbchargernotokr_handler},
3157 {"VBUS_OVV", ab8500_charger_vbusovv_handler},
3158 {"CH_WD_EXP", ab8500_charger_chwdexp_handler},
3159 {"VBUS_CH_DROP_END", ab8500_charger_vbuschdropend_handler},
3162 static int ab8500_charger_usb_notifier_call(struct notifier_block *nb,
3163 unsigned long event, void *power)
3165 struct ab8500_charger *di =
3166 container_of(nb, struct ab8500_charger, nb);
3167 enum ab8500_usb_state bm_usb_state;
3168 unsigned mA = *((unsigned *)power);
3170 if (!di)
3171 return NOTIFY_DONE;
3173 if (event != USB_EVENT_VBUS) {
3174 dev_dbg(di->dev, "not a standard host, returning\n");
3175 return NOTIFY_DONE;
3178 /* TODO: State is fabricate here. See if charger really needs USB
3179 * state or if mA is enough
3181 if ((di->usb_state.usb_current == 2) && (mA > 2))
3182 bm_usb_state = AB8500_BM_USB_STATE_RESUME;
3183 else if (mA == 0)
3184 bm_usb_state = AB8500_BM_USB_STATE_RESET_HS;
3185 else if (mA == 2)
3186 bm_usb_state = AB8500_BM_USB_STATE_SUSPEND;
3187 else if (mA >= 8) /* 8, 100, 500 */
3188 bm_usb_state = AB8500_BM_USB_STATE_CONFIGURED;
3189 else /* Should never occur */
3190 bm_usb_state = AB8500_BM_USB_STATE_RESET_FS;
3192 dev_dbg(di->dev, "%s usb_state: 0x%02x mA: %d\n",
3193 __func__, bm_usb_state, mA);
3195 spin_lock(&di->usb_state.usb_lock);
3196 di->usb_state.state_tmp = bm_usb_state;
3197 di->usb_state.usb_current_tmp = mA;
3198 spin_unlock(&di->usb_state.usb_lock);
3201 * wait for some time until you get updates from the usb stack
3202 * and negotiations are completed
3204 queue_delayed_work(di->charger_wq, &di->usb_state_changed_work, HZ/2);
3206 return NOTIFY_OK;
3209 #if defined(CONFIG_PM)
3210 static int ab8500_charger_resume(struct platform_device *pdev)
3212 int ret;
3213 struct ab8500_charger *di = platform_get_drvdata(pdev);
3216 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
3217 * logic. That means we have to continously kick the charger
3218 * watchdog even when no charger is connected. This is only
3219 * valid once the AC charger has been enabled. This is
3220 * a bug that is not handled by the algorithm and the
3221 * watchdog have to be kicked by the charger driver
3222 * when the AC charger is disabled
3224 if (di->ac_conn && is_ab8500_1p1_or_earlier(di->parent)) {
3225 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
3226 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
3227 if (ret)
3228 dev_err(di->dev, "Failed to kick WD!\n");
3230 /* If not already pending start a new timer */
3231 queue_delayed_work(di->charger_wq, &di->kick_wd_work,
3232 round_jiffies(WD_KICK_INTERVAL));
3235 /* If we still have a HW failure, schedule a new check */
3236 if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
3237 queue_delayed_work(di->charger_wq,
3238 &di->check_hw_failure_work, 0);
3241 if (di->flags.vbus_drop_end)
3242 queue_delayed_work(di->charger_wq, &di->vbus_drop_end_work, 0);
3244 return 0;
3247 static int ab8500_charger_suspend(struct platform_device *pdev,
3248 pm_message_t state)
3250 struct ab8500_charger *di = platform_get_drvdata(pdev);
3252 /* Cancel any pending jobs */
3253 cancel_delayed_work(&di->check_hw_failure_work);
3254 cancel_delayed_work(&di->vbus_drop_end_work);
3256 flush_delayed_work(&di->attach_work);
3257 flush_delayed_work(&di->usb_charger_attached_work);
3258 flush_delayed_work(&di->ac_charger_attached_work);
3259 flush_delayed_work(&di->check_usbchgnotok_work);
3260 flush_delayed_work(&di->check_vbat_work);
3261 flush_delayed_work(&di->kick_wd_work);
3263 flush_work(&di->usb_link_status_work);
3264 flush_work(&di->ac_work);
3265 flush_work(&di->detect_usb_type_work);
3267 if (atomic_read(&di->current_stepping_sessions))
3268 return -EAGAIN;
3270 return 0;
3272 #else
3273 #define ab8500_charger_suspend NULL
3274 #define ab8500_charger_resume NULL
3275 #endif
3277 static struct notifier_block charger_nb = {
3278 .notifier_call = ab8500_external_charger_prepare,
3281 static int ab8500_charger_remove(struct platform_device *pdev)
3283 struct ab8500_charger *di = platform_get_drvdata(pdev);
3284 int i, irq, ret;
3286 /* Disable AC charging */
3287 ab8500_charger_ac_en(&di->ac_chg, false, 0, 0);
3289 /* Disable USB charging */
3290 ab8500_charger_usb_en(&di->usb_chg, false, 0, 0);
3292 /* Disable interrupts */
3293 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
3294 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3295 free_irq(irq, di);
3298 /* Backup battery voltage and current disable */
3299 ret = abx500_mask_and_set_register_interruptible(di->dev,
3300 AB8500_RTC, AB8500_RTC_CTRL_REG, RTC_BUP_CH_ENA, 0);
3301 if (ret < 0)
3302 dev_err(di->dev, "%s mask and set failed\n", __func__);
3304 usb_unregister_notifier(di->usb_phy, &di->nb);
3305 usb_put_phy(di->usb_phy);
3307 /* Delete the work queue */
3308 destroy_workqueue(di->charger_wq);
3310 /* Unregister external charger enable notifier */
3311 if (!di->ac_chg.enabled)
3312 blocking_notifier_chain_unregister(
3313 &charger_notifier_list, &charger_nb);
3315 flush_scheduled_work();
3316 if (di->usb_chg.enabled)
3317 power_supply_unregister(di->usb_chg.psy);
3319 if (di->ac_chg.enabled && !di->ac_chg.external)
3320 power_supply_unregister(di->ac_chg.psy);
3322 return 0;
3325 static char *supply_interface[] = {
3326 "ab8500_chargalg",
3327 "ab8500_fg",
3328 "ab8500_btemp",
3331 static const struct power_supply_desc ab8500_ac_chg_desc = {
3332 .name = "ab8500_ac",
3333 .type = POWER_SUPPLY_TYPE_MAINS,
3334 .properties = ab8500_charger_ac_props,
3335 .num_properties = ARRAY_SIZE(ab8500_charger_ac_props),
3336 .get_property = ab8500_charger_ac_get_property,
3339 static const struct power_supply_desc ab8500_usb_chg_desc = {
3340 .name = "ab8500_usb",
3341 .type = POWER_SUPPLY_TYPE_USB,
3342 .properties = ab8500_charger_usb_props,
3343 .num_properties = ARRAY_SIZE(ab8500_charger_usb_props),
3344 .get_property = ab8500_charger_usb_get_property,
3347 static int ab8500_charger_probe(struct platform_device *pdev)
3349 struct device_node *np = pdev->dev.of_node;
3350 struct abx500_bm_data *plat = pdev->dev.platform_data;
3351 struct power_supply_config ac_psy_cfg = {}, usb_psy_cfg = {};
3352 struct ab8500_charger *di;
3353 int irq, i, charger_status, ret = 0, ch_stat;
3355 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
3356 if (!di) {
3357 dev_err(&pdev->dev, "%s no mem for ab8500_charger\n", __func__);
3358 return -ENOMEM;
3361 if (!plat) {
3362 dev_err(&pdev->dev, "no battery management data supplied\n");
3363 return -EINVAL;
3365 di->bm = plat;
3367 if (np) {
3368 ret = ab8500_bm_of_probe(&pdev->dev, np, di->bm);
3369 if (ret) {
3370 dev_err(&pdev->dev, "failed to get battery information\n");
3371 return ret;
3373 di->autopower_cfg = of_property_read_bool(np, "autopower_cfg");
3374 } else
3375 di->autopower_cfg = false;
3377 /* get parent data */
3378 di->dev = &pdev->dev;
3379 di->parent = dev_get_drvdata(pdev->dev.parent);
3381 /* Get ADC channels */
3382 di->adc_main_charger_v = devm_iio_channel_get(&pdev->dev,
3383 "main_charger_v");
3384 if (IS_ERR(di->adc_main_charger_v)) {
3385 if (PTR_ERR(di->adc_main_charger_v) == -ENODEV)
3386 return -EPROBE_DEFER;
3387 dev_err(&pdev->dev, "failed to get ADC main charger voltage\n");
3388 return PTR_ERR(di->adc_main_charger_v);
3390 di->adc_main_charger_c = devm_iio_channel_get(&pdev->dev,
3391 "main_charger_c");
3392 if (IS_ERR(di->adc_main_charger_c)) {
3393 if (PTR_ERR(di->adc_main_charger_c) == -ENODEV)
3394 return -EPROBE_DEFER;
3395 dev_err(&pdev->dev, "failed to get ADC main charger current\n");
3396 return PTR_ERR(di->adc_main_charger_c);
3398 di->adc_vbus_v = devm_iio_channel_get(&pdev->dev, "vbus_v");
3399 if (IS_ERR(di->adc_vbus_v)) {
3400 if (PTR_ERR(di->adc_vbus_v) == -ENODEV)
3401 return -EPROBE_DEFER;
3402 dev_err(&pdev->dev, "failed to get ADC USB charger voltage\n");
3403 return PTR_ERR(di->adc_vbus_v);
3405 di->adc_usb_charger_c = devm_iio_channel_get(&pdev->dev,
3406 "usb_charger_c");
3407 if (IS_ERR(di->adc_usb_charger_c)) {
3408 if (PTR_ERR(di->adc_usb_charger_c) == -ENODEV)
3409 return -EPROBE_DEFER;
3410 dev_err(&pdev->dev, "failed to get ADC USB charger current\n");
3411 return PTR_ERR(di->adc_usb_charger_c);
3414 /* initialize lock */
3415 spin_lock_init(&di->usb_state.usb_lock);
3416 mutex_init(&di->usb_ipt_crnt_lock);
3418 di->autopower = false;
3419 di->invalid_charger_detect_state = 0;
3421 /* AC and USB supply config */
3422 ac_psy_cfg.supplied_to = supply_interface;
3423 ac_psy_cfg.num_supplicants = ARRAY_SIZE(supply_interface);
3424 ac_psy_cfg.drv_data = &di->ac_chg;
3425 usb_psy_cfg.supplied_to = supply_interface;
3426 usb_psy_cfg.num_supplicants = ARRAY_SIZE(supply_interface);
3427 usb_psy_cfg.drv_data = &di->usb_chg;
3429 /* AC supply */
3430 /* ux500_charger sub-class */
3431 di->ac_chg.ops.enable = &ab8500_charger_ac_en;
3432 di->ac_chg.ops.check_enable = &ab8500_charger_ac_check_enable;
3433 di->ac_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
3434 di->ac_chg.ops.update_curr = &ab8500_charger_update_charger_current;
3435 di->ac_chg.max_out_volt = ab8500_charger_voltage_map[
3436 ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
3437 di->ac_chg.max_out_curr =
3438 di->bm->chg_output_curr[di->bm->n_chg_out_curr - 1];
3439 di->ac_chg.wdt_refresh = CHG_WD_INTERVAL;
3440 di->ac_chg.enabled = di->bm->ac_enabled;
3441 di->ac_chg.external = false;
3443 /*notifier for external charger enabling*/
3444 if (!di->ac_chg.enabled)
3445 blocking_notifier_chain_register(
3446 &charger_notifier_list, &charger_nb);
3448 /* USB supply */
3449 /* ux500_charger sub-class */
3450 di->usb_chg.ops.enable = &ab8500_charger_usb_en;
3451 di->usb_chg.ops.check_enable = &ab8500_charger_usb_check_enable;
3452 di->usb_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
3453 di->usb_chg.ops.update_curr = &ab8500_charger_update_charger_current;
3454 di->usb_chg.max_out_volt = ab8500_charger_voltage_map[
3455 ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
3456 di->usb_chg.max_out_curr =
3457 di->bm->chg_output_curr[di->bm->n_chg_out_curr - 1];
3458 di->usb_chg.wdt_refresh = CHG_WD_INTERVAL;
3459 di->usb_chg.enabled = di->bm->usb_enabled;
3460 di->usb_chg.external = false;
3461 di->usb_state.usb_current = -1;
3463 /* Create a work queue for the charger */
3464 di->charger_wq = alloc_ordered_workqueue("ab8500_charger_wq",
3465 WQ_MEM_RECLAIM);
3466 if (di->charger_wq == NULL) {
3467 dev_err(di->dev, "failed to create work queue\n");
3468 return -ENOMEM;
3471 mutex_init(&di->charger_attached_mutex);
3473 /* Init work for HW failure check */
3474 INIT_DEFERRABLE_WORK(&di->check_hw_failure_work,
3475 ab8500_charger_check_hw_failure_work);
3476 INIT_DEFERRABLE_WORK(&di->check_usbchgnotok_work,
3477 ab8500_charger_check_usbchargernotok_work);
3479 INIT_DELAYED_WORK(&di->ac_charger_attached_work,
3480 ab8500_charger_ac_attached_work);
3481 INIT_DELAYED_WORK(&di->usb_charger_attached_work,
3482 ab8500_charger_usb_attached_work);
3485 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
3486 * logic. That means we have to continously kick the charger
3487 * watchdog even when no charger is connected. This is only
3488 * valid once the AC charger has been enabled. This is
3489 * a bug that is not handled by the algorithm and the
3490 * watchdog have to be kicked by the charger driver
3491 * when the AC charger is disabled
3493 INIT_DEFERRABLE_WORK(&di->kick_wd_work,
3494 ab8500_charger_kick_watchdog_work);
3496 INIT_DEFERRABLE_WORK(&di->check_vbat_work,
3497 ab8500_charger_check_vbat_work);
3499 INIT_DELAYED_WORK(&di->attach_work,
3500 ab8500_charger_usb_link_attach_work);
3502 INIT_DELAYED_WORK(&di->usb_state_changed_work,
3503 ab8500_charger_usb_state_changed_work);
3505 INIT_DELAYED_WORK(&di->vbus_drop_end_work,
3506 ab8500_charger_vbus_drop_end_work);
3508 /* Init work for charger detection */
3509 INIT_WORK(&di->usb_link_status_work,
3510 ab8500_charger_usb_link_status_work);
3511 INIT_WORK(&di->ac_work, ab8500_charger_ac_work);
3512 INIT_WORK(&di->detect_usb_type_work,
3513 ab8500_charger_detect_usb_type_work);
3515 /* Init work for checking HW status */
3516 INIT_WORK(&di->check_main_thermal_prot_work,
3517 ab8500_charger_check_main_thermal_prot_work);
3518 INIT_WORK(&di->check_usb_thermal_prot_work,
3519 ab8500_charger_check_usb_thermal_prot_work);
3522 * VDD ADC supply needs to be enabled from this driver when there
3523 * is a charger connected to avoid erroneous BTEMP_HIGH/LOW
3524 * interrupts during charging
3526 di->regu = devm_regulator_get(di->dev, "vddadc");
3527 if (IS_ERR(di->regu)) {
3528 ret = PTR_ERR(di->regu);
3529 dev_err(di->dev, "failed to get vddadc regulator\n");
3530 goto free_charger_wq;
3534 /* Initialize OVV, and other registers */
3535 ret = ab8500_charger_init_hw_registers(di);
3536 if (ret) {
3537 dev_err(di->dev, "failed to initialize ABB registers\n");
3538 goto free_charger_wq;
3541 /* Register AC charger class */
3542 if (di->ac_chg.enabled) {
3543 di->ac_chg.psy = power_supply_register(di->dev,
3544 &ab8500_ac_chg_desc,
3545 &ac_psy_cfg);
3546 if (IS_ERR(di->ac_chg.psy)) {
3547 dev_err(di->dev, "failed to register AC charger\n");
3548 ret = PTR_ERR(di->ac_chg.psy);
3549 goto free_charger_wq;
3553 /* Register USB charger class */
3554 if (di->usb_chg.enabled) {
3555 di->usb_chg.psy = power_supply_register(di->dev,
3556 &ab8500_usb_chg_desc,
3557 &usb_psy_cfg);
3558 if (IS_ERR(di->usb_chg.psy)) {
3559 dev_err(di->dev, "failed to register USB charger\n");
3560 ret = PTR_ERR(di->usb_chg.psy);
3561 goto free_ac;
3565 di->usb_phy = usb_get_phy(USB_PHY_TYPE_USB2);
3566 if (IS_ERR_OR_NULL(di->usb_phy)) {
3567 dev_err(di->dev, "failed to get usb transceiver\n");
3568 ret = -EINVAL;
3569 goto free_usb;
3571 di->nb.notifier_call = ab8500_charger_usb_notifier_call;
3572 ret = usb_register_notifier(di->usb_phy, &di->nb);
3573 if (ret) {
3574 dev_err(di->dev, "failed to register usb notifier\n");
3575 goto put_usb_phy;
3578 /* Identify the connected charger types during startup */
3579 charger_status = ab8500_charger_detect_chargers(di, true);
3580 if (charger_status & AC_PW_CONN) {
3581 di->ac.charger_connected = 1;
3582 di->ac_conn = true;
3583 ab8500_power_supply_changed(di, di->ac_chg.psy);
3584 sysfs_notify(&di->ac_chg.psy->dev.kobj, NULL, "present");
3587 if (charger_status & USB_PW_CONN) {
3588 di->vbus_detected = true;
3589 di->vbus_detected_start = true;
3590 queue_work(di->charger_wq,
3591 &di->detect_usb_type_work);
3594 /* Register interrupts */
3595 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
3596 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3597 if (irq < 0) {
3598 ret = irq;
3599 goto free_irq;
3602 ret = request_threaded_irq(irq, NULL, ab8500_charger_irq[i].isr,
3603 IRQF_SHARED | IRQF_NO_SUSPEND,
3604 ab8500_charger_irq[i].name, di);
3606 if (ret != 0) {
3607 dev_err(di->dev, "failed to request %s IRQ %d: %d\n"
3608 , ab8500_charger_irq[i].name, irq, ret);
3609 goto free_irq;
3611 dev_dbg(di->dev, "Requested %s IRQ %d: %d\n",
3612 ab8500_charger_irq[i].name, irq, ret);
3615 platform_set_drvdata(pdev, di);
3617 mutex_lock(&di->charger_attached_mutex);
3619 ch_stat = ab8500_charger_detect_chargers(di, false);
3621 if ((ch_stat & AC_PW_CONN) == AC_PW_CONN) {
3622 if (is_ab8500(di->parent))
3623 queue_delayed_work(di->charger_wq,
3624 &di->ac_charger_attached_work,
3625 HZ);
3627 if ((ch_stat & USB_PW_CONN) == USB_PW_CONN) {
3628 if (is_ab8500(di->parent))
3629 queue_delayed_work(di->charger_wq,
3630 &di->usb_charger_attached_work,
3631 HZ);
3634 mutex_unlock(&di->charger_attached_mutex);
3636 return ret;
3638 free_irq:
3639 usb_unregister_notifier(di->usb_phy, &di->nb);
3641 /* We also have to free all successfully registered irqs */
3642 for (i = i - 1; i >= 0; i--) {
3643 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3644 free_irq(irq, di);
3646 put_usb_phy:
3647 usb_put_phy(di->usb_phy);
3648 free_usb:
3649 if (di->usb_chg.enabled)
3650 power_supply_unregister(di->usb_chg.psy);
3651 free_ac:
3652 if (di->ac_chg.enabled)
3653 power_supply_unregister(di->ac_chg.psy);
3654 free_charger_wq:
3655 destroy_workqueue(di->charger_wq);
3656 return ret;
3659 static const struct of_device_id ab8500_charger_match[] = {
3660 { .compatible = "stericsson,ab8500-charger", },
3661 { },
3664 static struct platform_driver ab8500_charger_driver = {
3665 .probe = ab8500_charger_probe,
3666 .remove = ab8500_charger_remove,
3667 .suspend = ab8500_charger_suspend,
3668 .resume = ab8500_charger_resume,
3669 .driver = {
3670 .name = "ab8500-charger",
3671 .of_match_table = ab8500_charger_match,
3675 static int __init ab8500_charger_init(void)
3677 return platform_driver_register(&ab8500_charger_driver);
3680 static void __exit ab8500_charger_exit(void)
3682 platform_driver_unregister(&ab8500_charger_driver);
3685 subsys_initcall_sync(ab8500_charger_init);
3686 module_exit(ab8500_charger_exit);
3688 MODULE_LICENSE("GPL v2");
3689 MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy");
3690 MODULE_ALIAS("platform:ab8500-charger");
3691 MODULE_DESCRIPTION("AB8500 charger management driver");