Commit | Line | Data |
---|---|---|
84edbeea AM |
1 | /* |
2 | * Copyright (C) ST-Ericsson SA 2012 | |
3 | * | |
4 | * Charger driver for AB8500 | |
5 | * | |
6 | * License Terms: GNU General Public License v2 | |
7 | * Author: | |
8 | * Johan Palsson <johan.palsson@stericsson.com> | |
9 | * Karl Komierowski <karl.komierowski@stericsson.com> | |
10 | * Arun R Murthy <arun.murthy@stericsson.com> | |
11 | */ | |
12 | ||
13 | #include <linux/init.h> | |
14 | #include <linux/module.h> | |
15 | #include <linux/device.h> | |
16 | #include <linux/interrupt.h> | |
17 | #include <linux/delay.h> | |
18 | #include <linux/slab.h> | |
19 | #include <linux/platform_device.h> | |
20 | #include <linux/power_supply.h> | |
21 | #include <linux/completion.h> | |
22 | #include <linux/regulator/consumer.h> | |
23 | #include <linux/err.h> | |
24 | #include <linux/workqueue.h> | |
25 | #include <linux/kobject.h> | |
4aef72db R |
26 | #include <linux/of.h> |
27 | #include <linux/mfd/core.h> | |
84edbeea AM |
28 | #include <linux/mfd/abx500/ab8500.h> |
29 | #include <linux/mfd/abx500.h> | |
30 | #include <linux/mfd/abx500/ab8500-bm.h> | |
31 | #include <linux/mfd/abx500/ab8500-gpadc.h> | |
32 | #include <linux/mfd/abx500/ux500_chargalg.h> | |
33 | #include <linux/usb/otg.h> | |
34 | ||
35 | /* Charger constants */ | |
36 | #define NO_PW_CONN 0 | |
37 | #define AC_PW_CONN 1 | |
38 | #define USB_PW_CONN 2 | |
39 | ||
40 | #define MAIN_WDOG_ENA 0x01 | |
41 | #define MAIN_WDOG_KICK 0x02 | |
42 | #define MAIN_WDOG_DIS 0x00 | |
43 | #define CHARG_WD_KICK 0x01 | |
44 | #define MAIN_CH_ENA 0x01 | |
45 | #define MAIN_CH_NO_OVERSHOOT_ENA_N 0x02 | |
46 | #define USB_CH_ENA 0x01 | |
47 | #define USB_CHG_NO_OVERSHOOT_ENA_N 0x02 | |
48 | #define MAIN_CH_DET 0x01 | |
49 | #define MAIN_CH_CV_ON 0x04 | |
50 | #define USB_CH_CV_ON 0x08 | |
51 | #define VBUS_DET_DBNC100 0x02 | |
52 | #define VBUS_DET_DBNC1 0x01 | |
53 | #define OTP_ENABLE_WD 0x01 | |
54 | ||
55 | #define MAIN_CH_INPUT_CURR_SHIFT 4 | |
56 | #define VBUS_IN_CURR_LIM_SHIFT 4 | |
57 | ||
58 | #define LED_INDICATOR_PWM_ENA 0x01 | |
59 | #define LED_INDICATOR_PWM_DIS 0x00 | |
60 | #define LED_IND_CUR_5MA 0x04 | |
61 | #define LED_INDICATOR_PWM_DUTY_252_256 0xBF | |
62 | ||
63 | /* HW failure constants */ | |
64 | #define MAIN_CH_TH_PROT 0x02 | |
65 | #define VBUS_CH_NOK 0x08 | |
66 | #define USB_CH_TH_PROT 0x02 | |
67 | #define VBUS_OVV_TH 0x01 | |
68 | #define MAIN_CH_NOK 0x01 | |
69 | #define VBUS_DET 0x80 | |
70 | ||
71 | /* UsbLineStatus register bit masks */ | |
72 | #define AB8500_USB_LINK_STATUS 0x78 | |
73 | #define AB8500_STD_HOST_SUSP 0x18 | |
74 | ||
75 | /* Watchdog timeout constant */ | |
76 | #define WD_TIMER 0x30 /* 4min */ | |
77 | #define WD_KICK_INTERVAL (60 * HZ) | |
78 | ||
79 | /* Lowest charger voltage is 3.39V -> 0x4E */ | |
80 | #define LOW_VOLT_REG 0x4E | |
81 | ||
f8e96dff JB |
82 | /* Step up/down delay in us */ |
83 | #define STEP_UDELAY 1000 | |
84 | ||
84edbeea AM |
85 | /* UsbLineStatus register - usb types */ |
86 | enum ab8500_charger_link_status { | |
87 | USB_STAT_NOT_CONFIGURED, | |
88 | USB_STAT_STD_HOST_NC, | |
89 | USB_STAT_STD_HOST_C_NS, | |
90 | USB_STAT_STD_HOST_C_S, | |
91 | USB_STAT_HOST_CHG_NM, | |
92 | USB_STAT_HOST_CHG_HS, | |
93 | USB_STAT_HOST_CHG_HS_CHIRP, | |
94 | USB_STAT_DEDICATED_CHG, | |
95 | USB_STAT_ACA_RID_A, | |
96 | USB_STAT_ACA_RID_B, | |
97 | USB_STAT_ACA_RID_C_NM, | |
98 | USB_STAT_ACA_RID_C_HS, | |
99 | USB_STAT_ACA_RID_C_HS_CHIRP, | |
100 | USB_STAT_HM_IDGND, | |
101 | USB_STAT_RESERVED, | |
102 | USB_STAT_NOT_VALID_LINK, | |
103 | }; | |
104 | ||
105 | enum ab8500_usb_state { | |
106 | AB8500_BM_USB_STATE_RESET_HS, /* HighSpeed Reset */ | |
107 | AB8500_BM_USB_STATE_RESET_FS, /* FullSpeed/LowSpeed Reset */ | |
108 | AB8500_BM_USB_STATE_CONFIGURED, | |
109 | AB8500_BM_USB_STATE_SUSPEND, | |
110 | AB8500_BM_USB_STATE_RESUME, | |
111 | AB8500_BM_USB_STATE_MAX, | |
112 | }; | |
113 | ||
114 | /* VBUS input current limits supported in AB8500 in mA */ | |
115 | #define USB_CH_IP_CUR_LVL_0P05 50 | |
116 | #define USB_CH_IP_CUR_LVL_0P09 98 | |
117 | #define USB_CH_IP_CUR_LVL_0P19 193 | |
118 | #define USB_CH_IP_CUR_LVL_0P29 290 | |
119 | #define USB_CH_IP_CUR_LVL_0P38 380 | |
120 | #define USB_CH_IP_CUR_LVL_0P45 450 | |
121 | #define USB_CH_IP_CUR_LVL_0P5 500 | |
122 | #define USB_CH_IP_CUR_LVL_0P6 600 | |
123 | #define USB_CH_IP_CUR_LVL_0P7 700 | |
124 | #define USB_CH_IP_CUR_LVL_0P8 800 | |
125 | #define USB_CH_IP_CUR_LVL_0P9 900 | |
126 | #define USB_CH_IP_CUR_LVL_1P0 1000 | |
127 | #define USB_CH_IP_CUR_LVL_1P1 1100 | |
128 | #define USB_CH_IP_CUR_LVL_1P3 1300 | |
129 | #define USB_CH_IP_CUR_LVL_1P4 1400 | |
130 | #define USB_CH_IP_CUR_LVL_1P5 1500 | |
131 | ||
132 | #define VBAT_TRESH_IP_CUR_RED 3800 | |
133 | ||
134 | #define to_ab8500_charger_usb_device_info(x) container_of((x), \ | |
135 | struct ab8500_charger, usb_chg) | |
136 | #define to_ab8500_charger_ac_device_info(x) container_of((x), \ | |
137 | struct ab8500_charger, ac_chg) | |
138 | ||
139 | /** | |
140 | * struct ab8500_charger_interrupts - ab8500 interupts | |
141 | * @name: name of the interrupt | |
142 | * @isr function pointer to the isr | |
143 | */ | |
144 | struct ab8500_charger_interrupts { | |
145 | char *name; | |
146 | irqreturn_t (*isr)(int irq, void *data); | |
147 | }; | |
148 | ||
149 | struct ab8500_charger_info { | |
150 | int charger_connected; | |
151 | int charger_online; | |
152 | int charger_voltage; | |
153 | int cv_active; | |
154 | bool wd_expired; | |
155 | }; | |
156 | ||
157 | struct ab8500_charger_event_flags { | |
158 | bool mainextchnotok; | |
159 | bool main_thermal_prot; | |
160 | bool usb_thermal_prot; | |
161 | bool vbus_ovv; | |
162 | bool usbchargernotok; | |
163 | bool chgwdexp; | |
164 | bool vbus_collapse; | |
165 | }; | |
166 | ||
167 | struct ab8500_charger_usb_state { | |
168 | bool usb_changed; | |
169 | int usb_current; | |
170 | enum ab8500_usb_state state; | |
171 | spinlock_t usb_lock; | |
172 | }; | |
173 | ||
174 | /** | |
175 | * struct ab8500_charger - ab8500 Charger device information | |
176 | * @dev: Pointer to the structure device | |
177 | * @max_usb_in_curr: Max USB charger input current | |
178 | * @vbus_detected: VBUS detected | |
179 | * @vbus_detected_start: | |
180 | * VBUS detected during startup | |
181 | * @ac_conn: This will be true when the AC charger has been plugged | |
182 | * @vddadc_en_ac: Indicate if VDD ADC supply is enabled because AC | |
183 | * charger is enabled | |
184 | * @vddadc_en_usb: Indicate if VDD ADC supply is enabled because USB | |
185 | * charger is enabled | |
186 | * @vbat Battery voltage | |
187 | * @old_vbat Previously measured battery voltage | |
188 | * @autopower Indicate if we should have automatic pwron after pwrloss | |
4aef72db | 189 | * @autopower_cfg platform specific power config support for "pwron after pwrloss" |
84edbeea AM |
190 | * @parent: Pointer to the struct ab8500 |
191 | * @gpadc: Pointer to the struct gpadc | |
b0284de0 | 192 | * @bm: Platform specific battery management information |
84edbeea AM |
193 | * @flags: Structure for information about events triggered |
194 | * @usb_state: Structure for usb stack information | |
195 | * @ac_chg: AC charger power supply | |
196 | * @usb_chg: USB charger power supply | |
197 | * @ac: Structure that holds the AC charger properties | |
198 | * @usb: Structure that holds the USB charger properties | |
199 | * @regu: Pointer to the struct regulator | |
200 | * @charger_wq: Work queue for the IRQs and checking HW state | |
201 | * @check_vbat_work Work for checking vbat threshold to adjust vbus current | |
202 | * @check_hw_failure_work: Work for checking HW state | |
203 | * @check_usbchgnotok_work: Work for checking USB charger not ok status | |
204 | * @kick_wd_work: Work for kicking the charger watchdog in case | |
205 | * of ABB rev 1.* due to the watchog logic bug | |
206 | * @ac_work: Work for checking AC charger connection | |
207 | * @detect_usb_type_work: Work for detecting the USB type connected | |
208 | * @usb_link_status_work: Work for checking the new USB link status | |
209 | * @usb_state_changed_work: Work for checking USB state | |
210 | * @check_main_thermal_prot_work: | |
211 | * Work for checking Main thermal status | |
212 | * @check_usb_thermal_prot_work: | |
213 | * Work for checking USB thermal status | |
214 | */ | |
215 | struct ab8500_charger { | |
216 | struct device *dev; | |
217 | int max_usb_in_curr; | |
218 | bool vbus_detected; | |
219 | bool vbus_detected_start; | |
220 | bool ac_conn; | |
221 | bool vddadc_en_ac; | |
222 | bool vddadc_en_usb; | |
223 | int vbat; | |
224 | int old_vbat; | |
225 | bool autopower; | |
4aef72db | 226 | bool autopower_cfg; |
84edbeea AM |
227 | struct ab8500 *parent; |
228 | struct ab8500_gpadc *gpadc; | |
b0284de0 | 229 | struct abx500_bm_data *bm; |
84edbeea AM |
230 | struct ab8500_charger_event_flags flags; |
231 | struct ab8500_charger_usb_state usb_state; | |
232 | struct ux500_charger ac_chg; | |
233 | struct ux500_charger usb_chg; | |
234 | struct ab8500_charger_info ac; | |
235 | struct ab8500_charger_info usb; | |
236 | struct regulator *regu; | |
237 | struct workqueue_struct *charger_wq; | |
238 | struct delayed_work check_vbat_work; | |
239 | struct delayed_work check_hw_failure_work; | |
240 | struct delayed_work check_usbchgnotok_work; | |
241 | struct delayed_work kick_wd_work; | |
242 | struct work_struct ac_work; | |
243 | struct work_struct detect_usb_type_work; | |
244 | struct work_struct usb_link_status_work; | |
245 | struct work_struct usb_state_changed_work; | |
246 | struct work_struct check_main_thermal_prot_work; | |
247 | struct work_struct check_usb_thermal_prot_work; | |
efd71c89 | 248 | struct usb_phy *usb_phy; |
84edbeea AM |
249 | struct notifier_block nb; |
250 | }; | |
251 | ||
252 | /* AC properties */ | |
253 | static enum power_supply_property ab8500_charger_ac_props[] = { | |
254 | POWER_SUPPLY_PROP_HEALTH, | |
255 | POWER_SUPPLY_PROP_PRESENT, | |
256 | POWER_SUPPLY_PROP_ONLINE, | |
257 | POWER_SUPPLY_PROP_VOLTAGE_NOW, | |
258 | POWER_SUPPLY_PROP_VOLTAGE_AVG, | |
259 | POWER_SUPPLY_PROP_CURRENT_NOW, | |
260 | }; | |
261 | ||
262 | /* USB properties */ | |
263 | static enum power_supply_property ab8500_charger_usb_props[] = { | |
264 | POWER_SUPPLY_PROP_HEALTH, | |
265 | POWER_SUPPLY_PROP_CURRENT_AVG, | |
266 | POWER_SUPPLY_PROP_PRESENT, | |
267 | POWER_SUPPLY_PROP_ONLINE, | |
268 | POWER_SUPPLY_PROP_VOLTAGE_NOW, | |
269 | POWER_SUPPLY_PROP_VOLTAGE_AVG, | |
270 | POWER_SUPPLY_PROP_CURRENT_NOW, | |
271 | }; | |
272 | ||
defbfa9c POH |
273 | /* |
274 | * Function for enabling and disabling sw fallback mode | |
275 | * should always be disabled when no charger is connected. | |
84edbeea | 276 | */ |
defbfa9c POH |
277 | static void ab8500_enable_disable_sw_fallback(struct ab8500_charger *di, |
278 | bool fallback) | |
84edbeea AM |
279 | { |
280 | u8 reg; | |
281 | int ret; | |
282 | ||
defbfa9c | 283 | dev_dbg(di->dev, "SW Fallback: %d\n", fallback); |
84edbeea | 284 | |
defbfa9c | 285 | /* read the register containing fallback bit */ |
84edbeea AM |
286 | ret = abx500_get_register_interruptible(di->dev, 0x15, 0x00, ®); |
287 | if (ret) { | |
288 | dev_err(di->dev, "%d write failed\n", __LINE__); | |
289 | return; | |
290 | } | |
291 | ||
292 | /* enable the OPT emulation registers */ | |
293 | ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x2); | |
294 | if (ret) { | |
295 | dev_err(di->dev, "%d write failed\n", __LINE__); | |
296 | return; | |
297 | } | |
298 | ||
defbfa9c | 299 | if (fallback) |
84edbeea AM |
300 | reg |= 0x8; |
301 | else | |
302 | reg &= ~0x8; | |
303 | ||
defbfa9c | 304 | /* write back the changed fallback bit value to register */ |
84edbeea AM |
305 | ret = abx500_set_register_interruptible(di->dev, 0x15, 0x00, reg); |
306 | if (ret) { | |
307 | dev_err(di->dev, "%d write failed\n", __LINE__); | |
308 | return; | |
309 | } | |
310 | ||
311 | /* disable the set OTP registers again */ | |
312 | ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x0); | |
313 | if (ret) { | |
314 | dev_err(di->dev, "%d write failed\n", __LINE__); | |
315 | return; | |
316 | } | |
317 | } | |
318 | ||
319 | /** | |
320 | * ab8500_power_supply_changed - a wrapper with local extentions for | |
321 | * power_supply_changed | |
322 | * @di: pointer to the ab8500_charger structure | |
323 | * @psy: pointer to power_supply_that have changed. | |
324 | * | |
325 | */ | |
326 | static void ab8500_power_supply_changed(struct ab8500_charger *di, | |
327 | struct power_supply *psy) | |
328 | { | |
4aef72db | 329 | if (di->autopower_cfg) { |
84edbeea AM |
330 | if (!di->usb.charger_connected && |
331 | !di->ac.charger_connected && | |
332 | di->autopower) { | |
333 | di->autopower = false; | |
defbfa9c | 334 | ab8500_enable_disable_sw_fallback(di, false); |
84edbeea AM |
335 | } else if (!di->autopower && |
336 | (di->ac.charger_connected || | |
337 | di->usb.charger_connected)) { | |
338 | di->autopower = true; | |
defbfa9c | 339 | ab8500_enable_disable_sw_fallback(di, true); |
84edbeea AM |
340 | } |
341 | } | |
342 | power_supply_changed(psy); | |
343 | } | |
344 | ||
345 | static void ab8500_charger_set_usb_connected(struct ab8500_charger *di, | |
346 | bool connected) | |
347 | { | |
348 | if (connected != di->usb.charger_connected) { | |
349 | dev_dbg(di->dev, "USB connected:%i\n", connected); | |
350 | di->usb.charger_connected = connected; | |
351 | sysfs_notify(&di->usb_chg.psy.dev->kobj, NULL, "present"); | |
352 | } | |
353 | } | |
354 | ||
355 | /** | |
356 | * ab8500_charger_get_ac_voltage() - get ac charger voltage | |
357 | * @di: pointer to the ab8500_charger structure | |
358 | * | |
359 | * Returns ac charger voltage (on success) | |
360 | */ | |
361 | static int ab8500_charger_get_ac_voltage(struct ab8500_charger *di) | |
362 | { | |
363 | int vch; | |
364 | ||
365 | /* Only measure voltage if the charger is connected */ | |
366 | if (di->ac.charger_connected) { | |
367 | vch = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_V); | |
368 | if (vch < 0) | |
369 | dev_err(di->dev, "%s gpadc conv failed,\n", __func__); | |
370 | } else { | |
371 | vch = 0; | |
372 | } | |
373 | return vch; | |
374 | } | |
375 | ||
376 | /** | |
377 | * ab8500_charger_ac_cv() - check if the main charger is in CV mode | |
378 | * @di: pointer to the ab8500_charger structure | |
379 | * | |
380 | * Returns ac charger CV mode (on success) else error code | |
381 | */ | |
382 | static int ab8500_charger_ac_cv(struct ab8500_charger *di) | |
383 | { | |
384 | u8 val; | |
385 | int ret = 0; | |
386 | ||
387 | /* Only check CV mode if the charger is online */ | |
388 | if (di->ac.charger_online) { | |
389 | ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER, | |
390 | AB8500_CH_STATUS1_REG, &val); | |
391 | if (ret < 0) { | |
392 | dev_err(di->dev, "%s ab8500 read failed\n", __func__); | |
393 | return 0; | |
394 | } | |
395 | ||
396 | if (val & MAIN_CH_CV_ON) | |
397 | ret = 1; | |
398 | else | |
399 | ret = 0; | |
400 | } | |
401 | ||
402 | return ret; | |
403 | } | |
404 | ||
405 | /** | |
406 | * ab8500_charger_get_vbus_voltage() - get vbus voltage | |
407 | * @di: pointer to the ab8500_charger structure | |
408 | * | |
409 | * This function returns the vbus voltage. | |
410 | * Returns vbus voltage (on success) | |
411 | */ | |
412 | static int ab8500_charger_get_vbus_voltage(struct ab8500_charger *di) | |
413 | { | |
414 | int vch; | |
415 | ||
416 | /* Only measure voltage if the charger is connected */ | |
417 | if (di->usb.charger_connected) { | |
418 | vch = ab8500_gpadc_convert(di->gpadc, VBUS_V); | |
419 | if (vch < 0) | |
420 | dev_err(di->dev, "%s gpadc conv failed\n", __func__); | |
421 | } else { | |
422 | vch = 0; | |
423 | } | |
424 | return vch; | |
425 | } | |
426 | ||
427 | /** | |
428 | * ab8500_charger_get_usb_current() - get usb charger current | |
429 | * @di: pointer to the ab8500_charger structure | |
430 | * | |
431 | * This function returns the usb charger current. | |
432 | * Returns usb current (on success) and error code on failure | |
433 | */ | |
434 | static int ab8500_charger_get_usb_current(struct ab8500_charger *di) | |
435 | { | |
436 | int ich; | |
437 | ||
438 | /* Only measure current if the charger is online */ | |
439 | if (di->usb.charger_online) { | |
440 | ich = ab8500_gpadc_convert(di->gpadc, USB_CHARGER_C); | |
441 | if (ich < 0) | |
442 | dev_err(di->dev, "%s gpadc conv failed\n", __func__); | |
443 | } else { | |
444 | ich = 0; | |
445 | } | |
446 | return ich; | |
447 | } | |
448 | ||
449 | /** | |
450 | * ab8500_charger_get_ac_current() - get ac charger current | |
451 | * @di: pointer to the ab8500_charger structure | |
452 | * | |
453 | * This function returns the ac charger current. | |
454 | * Returns ac current (on success) and error code on failure. | |
455 | */ | |
456 | static int ab8500_charger_get_ac_current(struct ab8500_charger *di) | |
457 | { | |
458 | int ich; | |
459 | ||
460 | /* Only measure current if the charger is online */ | |
461 | if (di->ac.charger_online) { | |
462 | ich = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_C); | |
463 | if (ich < 0) | |
464 | dev_err(di->dev, "%s gpadc conv failed\n", __func__); | |
465 | } else { | |
466 | ich = 0; | |
467 | } | |
468 | return ich; | |
469 | } | |
470 | ||
471 | /** | |
472 | * ab8500_charger_usb_cv() - check if the usb charger is in CV mode | |
473 | * @di: pointer to the ab8500_charger structure | |
474 | * | |
475 | * Returns ac charger CV mode (on success) else error code | |
476 | */ | |
477 | static int ab8500_charger_usb_cv(struct ab8500_charger *di) | |
478 | { | |
479 | int ret; | |
480 | u8 val; | |
481 | ||
482 | /* Only check CV mode if the charger is online */ | |
483 | if (di->usb.charger_online) { | |
484 | ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER, | |
485 | AB8500_CH_USBCH_STAT1_REG, &val); | |
486 | if (ret < 0) { | |
487 | dev_err(di->dev, "%s ab8500 read failed\n", __func__); | |
488 | return 0; | |
489 | } | |
490 | ||
491 | if (val & USB_CH_CV_ON) | |
492 | ret = 1; | |
493 | else | |
494 | ret = 0; | |
495 | } else { | |
496 | ret = 0; | |
497 | } | |
498 | ||
499 | return ret; | |
500 | } | |
501 | ||
502 | /** | |
503 | * ab8500_charger_detect_chargers() - Detect the connected chargers | |
504 | * @di: pointer to the ab8500_charger structure | |
505 | * | |
506 | * Returns the type of charger connected. | |
507 | * For USB it will not mean we can actually charge from it | |
508 | * but that there is a USB cable connected that we have to | |
509 | * identify. This is used during startup when we don't get | |
510 | * interrupts of the charger detection | |
511 | * | |
512 | * Returns an integer value, that means, | |
513 | * NO_PW_CONN no power supply is connected | |
514 | * AC_PW_CONN if the AC power supply is connected | |
515 | * USB_PW_CONN if the USB power supply is connected | |
516 | * AC_PW_CONN + USB_PW_CONN if USB and AC power supplies are both connected | |
517 | */ | |
518 | static int ab8500_charger_detect_chargers(struct ab8500_charger *di) | |
519 | { | |
520 | int result = NO_PW_CONN; | |
521 | int ret; | |
522 | u8 val; | |
523 | ||
524 | /* Check for AC charger */ | |
525 | ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER, | |
526 | AB8500_CH_STATUS1_REG, &val); | |
527 | if (ret < 0) { | |
528 | dev_err(di->dev, "%s ab8500 read failed\n", __func__); | |
529 | return ret; | |
530 | } | |
531 | ||
532 | if (val & MAIN_CH_DET) | |
533 | result = AC_PW_CONN; | |
534 | ||
535 | /* Check for USB charger */ | |
536 | ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER, | |
537 | AB8500_CH_USBCH_STAT1_REG, &val); | |
538 | if (ret < 0) { | |
539 | dev_err(di->dev, "%s ab8500 read failed\n", __func__); | |
540 | return ret; | |
541 | } | |
542 | ||
543 | if ((val & VBUS_DET_DBNC1) && (val & VBUS_DET_DBNC100)) | |
544 | result |= USB_PW_CONN; | |
545 | ||
546 | return result; | |
547 | } | |
548 | ||
549 | /** | |
550 | * ab8500_charger_max_usb_curr() - get the max curr for the USB type | |
551 | * @di: pointer to the ab8500_charger structure | |
552 | * @link_status: the identified USB type | |
553 | * | |
554 | * Get the maximum current that is allowed to be drawn from the host | |
555 | * based on the USB type. | |
556 | * Returns error code in case of failure else 0 on success | |
557 | */ | |
558 | static int ab8500_charger_max_usb_curr(struct ab8500_charger *di, | |
559 | enum ab8500_charger_link_status link_status) | |
560 | { | |
561 | int ret = 0; | |
562 | ||
563 | switch (link_status) { | |
564 | case USB_STAT_STD_HOST_NC: | |
565 | case USB_STAT_STD_HOST_C_NS: | |
566 | case USB_STAT_STD_HOST_C_S: | |
567 | dev_dbg(di->dev, "USB Type - Standard host is " | |
568 | "detected through USB driver\n"); | |
569 | di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P09; | |
570 | break; | |
571 | case USB_STAT_HOST_CHG_HS_CHIRP: | |
572 | di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5; | |
573 | break; | |
574 | case USB_STAT_HOST_CHG_HS: | |
575 | case USB_STAT_ACA_RID_C_HS: | |
576 | di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P9; | |
577 | break; | |
578 | case USB_STAT_ACA_RID_A: | |
579 | /* | |
580 | * Dedicated charger level minus maximum current accessory | |
581 | * can consume (300mA). Closest level is 1100mA | |
582 | */ | |
583 | di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P1; | |
584 | break; | |
585 | case USB_STAT_ACA_RID_B: | |
586 | /* | |
587 | * Dedicated charger level minus 120mA (20mA for ACA and | |
588 | * 100mA for potential accessory). Closest level is 1300mA | |
589 | */ | |
590 | di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P3; | |
591 | break; | |
592 | case USB_STAT_DEDICATED_CHG: | |
593 | case USB_STAT_HOST_CHG_NM: | |
594 | case USB_STAT_ACA_RID_C_HS_CHIRP: | |
595 | case USB_STAT_ACA_RID_C_NM: | |
596 | di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P5; | |
597 | break; | |
598 | case USB_STAT_RESERVED: | |
599 | /* | |
600 | * This state is used to indicate that VBUS has dropped below | |
601 | * the detection level 4 times in a row. This is due to the | |
602 | * charger output current is set to high making the charger | |
603 | * voltage collapse. This have to be propagated through to | |
604 | * chargalg. This is done using the property | |
605 | * POWER_SUPPLY_PROP_CURRENT_AVG = 1 | |
606 | */ | |
607 | di->flags.vbus_collapse = true; | |
608 | dev_dbg(di->dev, "USB Type - USB_STAT_RESERVED " | |
609 | "VBUS has collapsed\n"); | |
610 | ret = -1; | |
611 | break; | |
612 | case USB_STAT_HM_IDGND: | |
613 | case USB_STAT_NOT_CONFIGURED: | |
614 | case USB_STAT_NOT_VALID_LINK: | |
615 | dev_err(di->dev, "USB Type - Charging not allowed\n"); | |
616 | di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05; | |
617 | ret = -ENXIO; | |
618 | break; | |
619 | default: | |
620 | dev_err(di->dev, "USB Type - Unknown\n"); | |
621 | di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05; | |
622 | ret = -ENXIO; | |
623 | break; | |
624 | }; | |
625 | ||
626 | dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", | |
627 | link_status, di->max_usb_in_curr); | |
628 | ||
629 | return ret; | |
630 | } | |
631 | ||
632 | /** | |
633 | * ab8500_charger_read_usb_type() - read the type of usb connected | |
634 | * @di: pointer to the ab8500_charger structure | |
635 | * | |
636 | * Detect the type of the plugged USB | |
637 | * Returns error code in case of failure else 0 on success | |
638 | */ | |
639 | static int ab8500_charger_read_usb_type(struct ab8500_charger *di) | |
640 | { | |
641 | int ret; | |
642 | u8 val; | |
643 | ||
644 | ret = abx500_get_register_interruptible(di->dev, | |
645 | AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG, &val); | |
646 | if (ret < 0) { | |
647 | dev_err(di->dev, "%s ab8500 read failed\n", __func__); | |
648 | return ret; | |
649 | } | |
650 | ret = abx500_get_register_interruptible(di->dev, AB8500_USB, | |
651 | AB8500_USB_LINE_STAT_REG, &val); | |
652 | if (ret < 0) { | |
653 | dev_err(di->dev, "%s ab8500 read failed\n", __func__); | |
654 | return ret; | |
655 | } | |
656 | ||
657 | /* get the USB type */ | |
658 | val = (val & AB8500_USB_LINK_STATUS) >> 3; | |
659 | ret = ab8500_charger_max_usb_curr(di, | |
660 | (enum ab8500_charger_link_status) val); | |
661 | ||
662 | return ret; | |
663 | } | |
664 | ||
665 | /** | |
666 | * ab8500_charger_detect_usb_type() - get the type of usb connected | |
667 | * @di: pointer to the ab8500_charger structure | |
668 | * | |
669 | * Detect the type of the plugged USB | |
670 | * Returns error code in case of failure else 0 on success | |
671 | */ | |
672 | static int ab8500_charger_detect_usb_type(struct ab8500_charger *di) | |
673 | { | |
674 | int i, ret; | |
675 | u8 val; | |
676 | ||
677 | /* | |
678 | * On getting the VBUS rising edge detect interrupt there | |
679 | * is a 250ms delay after which the register UsbLineStatus | |
680 | * is filled with valid data. | |
681 | */ | |
682 | for (i = 0; i < 10; i++) { | |
683 | msleep(250); | |
684 | ret = abx500_get_register_interruptible(di->dev, | |
685 | AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG, | |
686 | &val); | |
687 | if (ret < 0) { | |
688 | dev_err(di->dev, "%s ab8500 read failed\n", __func__); | |
689 | return ret; | |
690 | } | |
691 | ret = abx500_get_register_interruptible(di->dev, AB8500_USB, | |
692 | AB8500_USB_LINE_STAT_REG, &val); | |
693 | if (ret < 0) { | |
694 | dev_err(di->dev, "%s ab8500 read failed\n", __func__); | |
695 | return ret; | |
696 | } | |
697 | /* | |
698 | * Until the IT source register is read the UsbLineStatus | |
699 | * register is not updated, hence doing the same | |
700 | * Revisit this: | |
701 | */ | |
702 | ||
703 | /* get the USB type */ | |
704 | val = (val & AB8500_USB_LINK_STATUS) >> 3; | |
705 | if (val) | |
706 | break; | |
707 | } | |
708 | ret = ab8500_charger_max_usb_curr(di, | |
709 | (enum ab8500_charger_link_status) val); | |
710 | ||
711 | return ret; | |
712 | } | |
713 | ||
714 | /* | |
715 | * This array maps the raw hex value to charger voltage used by the AB8500 | |
716 | * Values taken from the UM0836 | |
717 | */ | |
718 | static int ab8500_charger_voltage_map[] = { | |
719 | 3500 , | |
720 | 3525 , | |
721 | 3550 , | |
722 | 3575 , | |
723 | 3600 , | |
724 | 3625 , | |
725 | 3650 , | |
726 | 3675 , | |
727 | 3700 , | |
728 | 3725 , | |
729 | 3750 , | |
730 | 3775 , | |
731 | 3800 , | |
732 | 3825 , | |
733 | 3850 , | |
734 | 3875 , | |
735 | 3900 , | |
736 | 3925 , | |
737 | 3950 , | |
738 | 3975 , | |
739 | 4000 , | |
740 | 4025 , | |
741 | 4050 , | |
742 | 4060 , | |
743 | 4070 , | |
744 | 4080 , | |
745 | 4090 , | |
746 | 4100 , | |
747 | 4110 , | |
748 | 4120 , | |
749 | 4130 , | |
750 | 4140 , | |
751 | 4150 , | |
752 | 4160 , | |
753 | 4170 , | |
754 | 4180 , | |
755 | 4190 , | |
756 | 4200 , | |
757 | 4210 , | |
758 | 4220 , | |
759 | 4230 , | |
760 | 4240 , | |
761 | 4250 , | |
762 | 4260 , | |
763 | 4270 , | |
764 | 4280 , | |
765 | 4290 , | |
766 | 4300 , | |
767 | 4310 , | |
768 | 4320 , | |
769 | 4330 , | |
770 | 4340 , | |
771 | 4350 , | |
772 | 4360 , | |
773 | 4370 , | |
774 | 4380 , | |
775 | 4390 , | |
776 | 4400 , | |
777 | 4410 , | |
778 | 4420 , | |
779 | 4430 , | |
780 | 4440 , | |
781 | 4450 , | |
782 | 4460 , | |
783 | 4470 , | |
784 | 4480 , | |
785 | 4490 , | |
786 | 4500 , | |
787 | 4510 , | |
788 | 4520 , | |
789 | 4530 , | |
790 | 4540 , | |
791 | 4550 , | |
792 | 4560 , | |
793 | 4570 , | |
794 | 4580 , | |
795 | 4590 , | |
796 | 4600 , | |
797 | }; | |
798 | ||
799 | /* | |
800 | * This array maps the raw hex value to charger current used by the AB8500 | |
801 | * Values taken from the UM0836 | |
802 | */ | |
803 | static int ab8500_charger_current_map[] = { | |
804 | 100 , | |
805 | 200 , | |
806 | 300 , | |
807 | 400 , | |
808 | 500 , | |
809 | 600 , | |
810 | 700 , | |
811 | 800 , | |
812 | 900 , | |
813 | 1000 , | |
814 | 1100 , | |
815 | 1200 , | |
816 | 1300 , | |
817 | 1400 , | |
818 | 1500 , | |
819 | }; | |
820 | ||
821 | /* | |
822 | * This array maps the raw hex value to VBUS input current used by the AB8500 | |
823 | * Values taken from the UM0836 | |
824 | */ | |
825 | static int ab8500_charger_vbus_in_curr_map[] = { | |
826 | USB_CH_IP_CUR_LVL_0P05, | |
827 | USB_CH_IP_CUR_LVL_0P09, | |
828 | USB_CH_IP_CUR_LVL_0P19, | |
829 | USB_CH_IP_CUR_LVL_0P29, | |
830 | USB_CH_IP_CUR_LVL_0P38, | |
831 | USB_CH_IP_CUR_LVL_0P45, | |
832 | USB_CH_IP_CUR_LVL_0P5, | |
833 | USB_CH_IP_CUR_LVL_0P6, | |
834 | USB_CH_IP_CUR_LVL_0P7, | |
835 | USB_CH_IP_CUR_LVL_0P8, | |
836 | USB_CH_IP_CUR_LVL_0P9, | |
837 | USB_CH_IP_CUR_LVL_1P0, | |
838 | USB_CH_IP_CUR_LVL_1P1, | |
839 | USB_CH_IP_CUR_LVL_1P3, | |
840 | USB_CH_IP_CUR_LVL_1P4, | |
841 | USB_CH_IP_CUR_LVL_1P5, | |
842 | }; | |
843 | ||
844 | static int ab8500_voltage_to_regval(int voltage) | |
845 | { | |
846 | int i; | |
847 | ||
848 | /* Special case for voltage below 3.5V */ | |
849 | if (voltage < ab8500_charger_voltage_map[0]) | |
850 | return LOW_VOLT_REG; | |
851 | ||
852 | for (i = 1; i < ARRAY_SIZE(ab8500_charger_voltage_map); i++) { | |
853 | if (voltage < ab8500_charger_voltage_map[i]) | |
854 | return i - 1; | |
855 | } | |
856 | ||
857 | /* If not last element, return error */ | |
858 | i = ARRAY_SIZE(ab8500_charger_voltage_map) - 1; | |
859 | if (voltage == ab8500_charger_voltage_map[i]) | |
860 | return i; | |
861 | else | |
862 | return -1; | |
863 | } | |
864 | ||
865 | static int ab8500_current_to_regval(int curr) | |
866 | { | |
867 | int i; | |
868 | ||
869 | if (curr < ab8500_charger_current_map[0]) | |
870 | return 0; | |
871 | ||
872 | for (i = 0; i < ARRAY_SIZE(ab8500_charger_current_map); i++) { | |
873 | if (curr < ab8500_charger_current_map[i]) | |
874 | return i - 1; | |
875 | } | |
876 | ||
877 | /* If not last element, return error */ | |
878 | i = ARRAY_SIZE(ab8500_charger_current_map) - 1; | |
879 | if (curr == ab8500_charger_current_map[i]) | |
880 | return i; | |
881 | else | |
882 | return -1; | |
883 | } | |
884 | ||
885 | static int ab8500_vbus_in_curr_to_regval(int curr) | |
886 | { | |
887 | int i; | |
888 | ||
889 | if (curr < ab8500_charger_vbus_in_curr_map[0]) | |
890 | return 0; | |
891 | ||
892 | for (i = 0; i < ARRAY_SIZE(ab8500_charger_vbus_in_curr_map); i++) { | |
893 | if (curr < ab8500_charger_vbus_in_curr_map[i]) | |
894 | return i - 1; | |
895 | } | |
896 | ||
897 | /* If not last element, return error */ | |
898 | i = ARRAY_SIZE(ab8500_charger_vbus_in_curr_map) - 1; | |
899 | if (curr == ab8500_charger_vbus_in_curr_map[i]) | |
900 | return i; | |
901 | else | |
902 | return -1; | |
903 | } | |
904 | ||
905 | /** | |
906 | * ab8500_charger_get_usb_cur() - get usb current | |
907 | * @di: pointer to the ab8500_charger structre | |
908 | * | |
909 | * The usb stack provides the maximum current that can be drawn from | |
910 | * the standard usb host. This will be in mA. | |
911 | * This function converts current in mA to a value that can be written | |
912 | * to the register. Returns -1 if charging is not allowed | |
913 | */ | |
914 | static int ab8500_charger_get_usb_cur(struct ab8500_charger *di) | |
915 | { | |
916 | switch (di->usb_state.usb_current) { | |
917 | case 100: | |
918 | di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P09; | |
919 | break; | |
920 | case 200: | |
921 | di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P19; | |
922 | break; | |
923 | case 300: | |
924 | di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P29; | |
925 | break; | |
926 | case 400: | |
927 | di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P38; | |
928 | break; | |
929 | case 500: | |
930 | di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5; | |
931 | break; | |
932 | default: | |
933 | di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05; | |
934 | return -1; | |
935 | break; | |
936 | }; | |
937 | return 0; | |
938 | } | |
939 | ||
f8e96dff JB |
940 | /** |
941 | * ab8500_charger_set_current() - set charger current | |
942 | * @di: pointer to the ab8500_charger structure | |
943 | * @ich: charger current, in mA | |
944 | * @reg: select what charger register to set | |
945 | * | |
946 | * Set charger current. | |
947 | * There is no state machine in the AB to step up/down the charger | |
948 | * current to avoid dips and spikes on MAIN, VBUS and VBAT when | |
949 | * charging is started. Instead we need to implement | |
950 | * this charger current step-up/down here. | |
951 | * Returns error code in case of failure else 0(on success) | |
952 | */ | |
953 | static int ab8500_charger_set_current(struct ab8500_charger *di, | |
954 | int ich, int reg) | |
955 | { | |
956 | int ret, i; | |
957 | int curr_index, prev_curr_index, shift_value; | |
958 | u8 reg_value; | |
959 | ||
960 | switch (reg) { | |
961 | case AB8500_MCH_IPT_CURLVL_REG: | |
962 | shift_value = MAIN_CH_INPUT_CURR_SHIFT; | |
963 | curr_index = ab8500_current_to_regval(ich); | |
964 | break; | |
965 | case AB8500_USBCH_IPT_CRNTLVL_REG: | |
966 | shift_value = VBUS_IN_CURR_LIM_SHIFT; | |
967 | curr_index = ab8500_vbus_in_curr_to_regval(ich); | |
968 | break; | |
969 | case AB8500_CH_OPT_CRNTLVL_REG: | |
970 | shift_value = 0; | |
971 | curr_index = ab8500_current_to_regval(ich); | |
972 | break; | |
973 | default: | |
974 | dev_err(di->dev, "%s current register not valid\n", __func__); | |
975 | return -ENXIO; | |
976 | } | |
977 | ||
978 | if (curr_index < 0) { | |
979 | dev_err(di->dev, "requested current limit out-of-range\n"); | |
980 | return -ENXIO; | |
981 | } | |
982 | ||
983 | ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER, | |
984 | reg, ®_value); | |
985 | if (ret < 0) { | |
986 | dev_err(di->dev, "%s read failed\n", __func__); | |
987 | return ret; | |
988 | } | |
989 | prev_curr_index = (reg_value >> shift_value); | |
990 | ||
991 | /* only update current if it's been changed */ | |
992 | if (prev_curr_index == curr_index) | |
993 | return 0; | |
994 | ||
995 | dev_dbg(di->dev, "%s set charger current: %d mA for reg: 0x%02x\n", | |
996 | __func__, ich, reg); | |
997 | ||
998 | if (prev_curr_index > curr_index) { | |
999 | for (i = prev_curr_index - 1; i >= curr_index; i--) { | |
1000 | ret = abx500_set_register_interruptible(di->dev, | |
1001 | AB8500_CHARGER, reg, (u8) i << shift_value); | |
1002 | if (ret) { | |
1003 | dev_err(di->dev, "%s write failed\n", __func__); | |
1004 | return ret; | |
1005 | } | |
1006 | usleep_range(STEP_UDELAY, STEP_UDELAY * 2); | |
1007 | } | |
1008 | } else { | |
1009 | for (i = prev_curr_index + 1; i <= curr_index; i++) { | |
1010 | ret = abx500_set_register_interruptible(di->dev, | |
1011 | AB8500_CHARGER, reg, (u8) i << shift_value); | |
1012 | if (ret) { | |
1013 | dev_err(di->dev, "%s write failed\n", __func__); | |
1014 | return ret; | |
1015 | } | |
1016 | usleep_range(STEP_UDELAY, STEP_UDELAY * 2); | |
1017 | } | |
1018 | } | |
1019 | return ret; | |
1020 | } | |
1021 | ||
84edbeea AM |
1022 | /** |
1023 | * ab8500_charger_set_vbus_in_curr() - set VBUS input current limit | |
1024 | * @di: pointer to the ab8500_charger structure | |
1025 | * @ich_in: charger input current limit | |
1026 | * | |
1027 | * Sets the current that can be drawn from the USB host | |
1028 | * Returns error code in case of failure else 0(on success) | |
1029 | */ | |
1030 | static int ab8500_charger_set_vbus_in_curr(struct ab8500_charger *di, | |
1031 | int ich_in) | |
1032 | { | |
84edbeea AM |
1033 | int min_value; |
1034 | ||
1035 | /* We should always use to lowest current limit */ | |
b0284de0 | 1036 | min_value = min(di->bm->chg_params->usb_curr_max, ich_in); |
84edbeea AM |
1037 | |
1038 | switch (min_value) { | |
1039 | case 100: | |
1040 | if (di->vbat < VBAT_TRESH_IP_CUR_RED) | |
1041 | min_value = USB_CH_IP_CUR_LVL_0P05; | |
1042 | break; | |
1043 | case 500: | |
1044 | if (di->vbat < VBAT_TRESH_IP_CUR_RED) | |
1045 | min_value = USB_CH_IP_CUR_LVL_0P45; | |
1046 | break; | |
1047 | default: | |
1048 | break; | |
1049 | } | |
1050 | ||
f8e96dff JB |
1051 | return ab8500_charger_set_current(di, min_value, |
1052 | AB8500_USBCH_IPT_CRNTLVL_REG); | |
1053 | } | |
84edbeea | 1054 | |
f8e96dff JB |
1055 | /** |
1056 | * ab8500_charger_set_main_in_curr() - set main charger input current | |
1057 | * @di: pointer to the ab8500_charger structure | |
1058 | * @ich_in: input charger current, in mA | |
1059 | * | |
1060 | * Set main charger input current. | |
1061 | * Returns error code in case of failure else 0(on success) | |
1062 | */ | |
1063 | static int ab8500_charger_set_main_in_curr(struct ab8500_charger *di, | |
1064 | int ich_in) | |
1065 | { | |
1066 | return ab8500_charger_set_current(di, ich_in, | |
1067 | AB8500_MCH_IPT_CURLVL_REG); | |
1068 | } | |
84edbeea | 1069 | |
f8e96dff JB |
1070 | /** |
1071 | * ab8500_charger_set_output_curr() - set charger output current | |
1072 | * @di: pointer to the ab8500_charger structure | |
1073 | * @ich_out: output charger current, in mA | |
1074 | * | |
1075 | * Set charger output current. | |
1076 | * Returns error code in case of failure else 0(on success) | |
1077 | */ | |
1078 | static int ab8500_charger_set_output_curr(struct ab8500_charger *di, | |
1079 | int ich_out) | |
1080 | { | |
1081 | return ab8500_charger_set_current(di, ich_out, | |
1082 | AB8500_CH_OPT_CRNTLVL_REG); | |
84edbeea AM |
1083 | } |
1084 | ||
1085 | /** | |
1086 | * ab8500_charger_led_en() - turn on/off chargign led | |
1087 | * @di: pointer to the ab8500_charger structure | |
1088 | * @on: flag to turn on/off the chargign led | |
1089 | * | |
1090 | * Power ON/OFF charging LED indication | |
1091 | * Returns error code in case of failure else 0(on success) | |
1092 | */ | |
1093 | static int ab8500_charger_led_en(struct ab8500_charger *di, int on) | |
1094 | { | |
1095 | int ret; | |
1096 | ||
1097 | if (on) { | |
1098 | /* Power ON charging LED indicator, set LED current to 5mA */ | |
1099 | ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, | |
1100 | AB8500_LED_INDICATOR_PWM_CTRL, | |
1101 | (LED_IND_CUR_5MA | LED_INDICATOR_PWM_ENA)); | |
1102 | if (ret) { | |
1103 | dev_err(di->dev, "Power ON LED failed\n"); | |
1104 | return ret; | |
1105 | } | |
1106 | /* LED indicator PWM duty cycle 252/256 */ | |
1107 | ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, | |
1108 | AB8500_LED_INDICATOR_PWM_DUTY, | |
1109 | LED_INDICATOR_PWM_DUTY_252_256); | |
1110 | if (ret) { | |
1111 | dev_err(di->dev, "Set LED PWM duty cycle failed\n"); | |
1112 | return ret; | |
1113 | } | |
1114 | } else { | |
1115 | /* Power off charging LED indicator */ | |
1116 | ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, | |
1117 | AB8500_LED_INDICATOR_PWM_CTRL, | |
1118 | LED_INDICATOR_PWM_DIS); | |
1119 | if (ret) { | |
1120 | dev_err(di->dev, "Power-off LED failed\n"); | |
1121 | return ret; | |
1122 | } | |
1123 | } | |
1124 | ||
1125 | return ret; | |
1126 | } | |
1127 | ||
1128 | /** | |
1129 | * ab8500_charger_ac_en() - enable or disable ac charging | |
1130 | * @di: pointer to the ab8500_charger structure | |
1131 | * @enable: enable/disable flag | |
1132 | * @vset: charging voltage | |
1133 | * @iset: charging current | |
1134 | * | |
1135 | * Enable/Disable AC/Mains charging and turns on/off the charging led | |
1136 | * respectively. | |
1137 | **/ | |
1138 | static int ab8500_charger_ac_en(struct ux500_charger *charger, | |
1139 | int enable, int vset, int iset) | |
1140 | { | |
1141 | int ret; | |
1142 | int volt_index; | |
1143 | int curr_index; | |
1144 | int input_curr_index; | |
1145 | u8 overshoot = 0; | |
1146 | ||
1147 | struct ab8500_charger *di = to_ab8500_charger_ac_device_info(charger); | |
1148 | ||
1149 | if (enable) { | |
1150 | /* Check if AC is connected */ | |
1151 | if (!di->ac.charger_connected) { | |
1152 | dev_err(di->dev, "AC charger not connected\n"); | |
1153 | return -ENXIO; | |
1154 | } | |
1155 | ||
1156 | /* Enable AC charging */ | |
1157 | dev_dbg(di->dev, "Enable AC: %dmV %dmA\n", vset, iset); | |
1158 | ||
1159 | /* | |
1160 | * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts | |
1161 | * will be triggered everytime we enable the VDD ADC supply. | |
1162 | * This will turn off charging for a short while. | |
1163 | * It can be avoided by having the supply on when | |
1164 | * there is a charger enabled. Normally the VDD ADC supply | |
1165 | * is enabled everytime a GPADC conversion is triggered. We will | |
1166 | * force it to be enabled from this driver to have | |
1167 | * the GPADC module independant of the AB8500 chargers | |
1168 | */ | |
1169 | if (!di->vddadc_en_ac) { | |
1170 | regulator_enable(di->regu); | |
1171 | di->vddadc_en_ac = true; | |
1172 | } | |
1173 | ||
1174 | /* Check if the requested voltage or current is valid */ | |
1175 | volt_index = ab8500_voltage_to_regval(vset); | |
1176 | curr_index = ab8500_current_to_regval(iset); | |
1177 | input_curr_index = ab8500_current_to_regval( | |
b0284de0 | 1178 | di->bm->chg_params->ac_curr_max); |
84edbeea AM |
1179 | if (volt_index < 0 || curr_index < 0 || input_curr_index < 0) { |
1180 | dev_err(di->dev, | |
1181 | "Charger voltage or current too high, " | |
1182 | "charging not started\n"); | |
1183 | return -ENXIO; | |
1184 | } | |
1185 | ||
1186 | /* ChVoltLevel: maximum battery charging voltage */ | |
1187 | ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, | |
1188 | AB8500_CH_VOLT_LVL_REG, (u8) volt_index); | |
1189 | if (ret) { | |
1190 | dev_err(di->dev, "%s write failed\n", __func__); | |
1191 | return ret; | |
1192 | } | |
1193 | /* MainChInputCurr: current that can be drawn from the charger*/ | |
f8e96dff | 1194 | ret = ab8500_charger_set_main_in_curr(di, |
b0284de0 | 1195 | di->bm->chg_params->ac_curr_max); |
84edbeea | 1196 | if (ret) { |
f8e96dff JB |
1197 | dev_err(di->dev, "%s Failed to set MainChInputCurr\n", |
1198 | __func__); | |
84edbeea AM |
1199 | return ret; |
1200 | } | |
1201 | /* ChOutputCurentLevel: protected output current */ | |
f8e96dff | 1202 | ret = ab8500_charger_set_output_curr(di, iset); |
84edbeea | 1203 | if (ret) { |
f8e96dff JB |
1204 | dev_err(di->dev, "%s " |
1205 | "Failed to set ChOutputCurentLevel\n", | |
1206 | __func__); | |
84edbeea AM |
1207 | return ret; |
1208 | } | |
1209 | ||
1210 | /* Check if VBAT overshoot control should be enabled */ | |
b0284de0 | 1211 | if (!di->bm->enable_overshoot) |
84edbeea AM |
1212 | overshoot = MAIN_CH_NO_OVERSHOOT_ENA_N; |
1213 | ||
1214 | /* Enable Main Charger */ | |
1215 | ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, | |
1216 | AB8500_MCH_CTRL1, MAIN_CH_ENA | overshoot); | |
1217 | if (ret) { | |
1218 | dev_err(di->dev, "%s write failed\n", __func__); | |
1219 | return ret; | |
1220 | } | |
1221 | ||
1222 | /* Power on charging LED indication */ | |
1223 | ret = ab8500_charger_led_en(di, true); | |
1224 | if (ret < 0) | |
1225 | dev_err(di->dev, "failed to enable LED\n"); | |
1226 | ||
1227 | di->ac.charger_online = 1; | |
1228 | } else { | |
1229 | /* Disable AC charging */ | |
1230 | if (is_ab8500_1p1_or_earlier(di->parent)) { | |
1231 | /* | |
1232 | * For ABB revision 1.0 and 1.1 there is a bug in the | |
1233 | * watchdog logic. That means we have to continously | |
1234 | * kick the charger watchdog even when no charger is | |
1235 | * connected. This is only valid once the AC charger | |
1236 | * has been enabled. This is a bug that is not handled | |
1237 | * by the algorithm and the watchdog have to be kicked | |
1238 | * by the charger driver when the AC charger | |
1239 | * is disabled | |
1240 | */ | |
1241 | if (di->ac_conn) { | |
1242 | queue_delayed_work(di->charger_wq, | |
1243 | &di->kick_wd_work, | |
1244 | round_jiffies(WD_KICK_INTERVAL)); | |
1245 | } | |
1246 | ||
1247 | /* | |
1248 | * We can't turn off charging completely | |
1249 | * due to a bug in AB8500 cut1. | |
1250 | * If we do, charging will not start again. | |
1251 | * That is why we set the lowest voltage | |
1252 | * and current possible | |
1253 | */ | |
1254 | ret = abx500_set_register_interruptible(di->dev, | |
1255 | AB8500_CHARGER, | |
1256 | AB8500_CH_VOLT_LVL_REG, CH_VOL_LVL_3P5); | |
1257 | if (ret) { | |
1258 | dev_err(di->dev, | |
1259 | "%s write failed\n", __func__); | |
1260 | return ret; | |
1261 | } | |
1262 | ||
f8e96dff | 1263 | ret = ab8500_charger_set_output_curr(di, 0); |
84edbeea | 1264 | if (ret) { |
f8e96dff JB |
1265 | dev_err(di->dev, "%s " |
1266 | "Failed to set ChOutputCurentLevel\n", | |
1267 | __func__); | |
84edbeea AM |
1268 | return ret; |
1269 | } | |
1270 | } else { | |
1271 | ret = abx500_set_register_interruptible(di->dev, | |
1272 | AB8500_CHARGER, | |
1273 | AB8500_MCH_CTRL1, 0); | |
1274 | if (ret) { | |
1275 | dev_err(di->dev, | |
1276 | "%s write failed\n", __func__); | |
1277 | return ret; | |
1278 | } | |
1279 | } | |
1280 | ||
1281 | ret = ab8500_charger_led_en(di, false); | |
1282 | if (ret < 0) | |
1283 | dev_err(di->dev, "failed to disable LED\n"); | |
1284 | ||
1285 | di->ac.charger_online = 0; | |
1286 | di->ac.wd_expired = false; | |
1287 | ||
1288 | /* Disable regulator if enabled */ | |
1289 | if (di->vddadc_en_ac) { | |
1290 | regulator_disable(di->regu); | |
1291 | di->vddadc_en_ac = false; | |
1292 | } | |
1293 | ||
1294 | dev_dbg(di->dev, "%s Disabled AC charging\n", __func__); | |
1295 | } | |
1296 | ab8500_power_supply_changed(di, &di->ac_chg.psy); | |
1297 | ||
1298 | return ret; | |
1299 | } | |
1300 | ||
1301 | /** | |
1302 | * ab8500_charger_usb_en() - enable usb charging | |
1303 | * @di: pointer to the ab8500_charger structure | |
1304 | * @enable: enable/disable flag | |
1305 | * @vset: charging voltage | |
1306 | * @ich_out: charger output current | |
1307 | * | |
1308 | * Enable/Disable USB charging and turns on/off the charging led respectively. | |
1309 | * Returns error code in case of failure else 0(on success) | |
1310 | */ | |
1311 | static int ab8500_charger_usb_en(struct ux500_charger *charger, | |
1312 | int enable, int vset, int ich_out) | |
1313 | { | |
1314 | int ret; | |
1315 | int volt_index; | |
1316 | int curr_index; | |
1317 | u8 overshoot = 0; | |
1318 | ||
1319 | struct ab8500_charger *di = to_ab8500_charger_usb_device_info(charger); | |
1320 | ||
1321 | if (enable) { | |
1322 | /* Check if USB is connected */ | |
1323 | if (!di->usb.charger_connected) { | |
1324 | dev_err(di->dev, "USB charger not connected\n"); | |
1325 | return -ENXIO; | |
1326 | } | |
1327 | ||
1328 | /* | |
1329 | * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts | |
1330 | * will be triggered everytime we enable the VDD ADC supply. | |
1331 | * This will turn off charging for a short while. | |
1332 | * It can be avoided by having the supply on when | |
1333 | * there is a charger enabled. Normally the VDD ADC supply | |
1334 | * is enabled everytime a GPADC conversion is triggered. We will | |
1335 | * force it to be enabled from this driver to have | |
1336 | * the GPADC module independant of the AB8500 chargers | |
1337 | */ | |
1338 | if (!di->vddadc_en_usb) { | |
1339 | regulator_enable(di->regu); | |
1340 | di->vddadc_en_usb = true; | |
1341 | } | |
1342 | ||
1343 | /* Enable USB charging */ | |
1344 | dev_dbg(di->dev, "Enable USB: %dmV %dmA\n", vset, ich_out); | |
1345 | ||
1346 | /* Check if the requested voltage or current is valid */ | |
1347 | volt_index = ab8500_voltage_to_regval(vset); | |
1348 | curr_index = ab8500_current_to_regval(ich_out); | |
1349 | if (volt_index < 0 || curr_index < 0) { | |
1350 | dev_err(di->dev, | |
1351 | "Charger voltage or current too high, " | |
1352 | "charging not started\n"); | |
1353 | return -ENXIO; | |
1354 | } | |
1355 | ||
1356 | /* ChVoltLevel: max voltage upto which battery can be charged */ | |
1357 | ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, | |
1358 | AB8500_CH_VOLT_LVL_REG, (u8) volt_index); | |
1359 | if (ret) { | |
1360 | dev_err(di->dev, "%s write failed\n", __func__); | |
1361 | return ret; | |
1362 | } | |
1363 | /* USBChInputCurr: current that can be drawn from the usb */ | |
1364 | ret = ab8500_charger_set_vbus_in_curr(di, di->max_usb_in_curr); | |
1365 | if (ret) { | |
1366 | dev_err(di->dev, "setting USBChInputCurr failed\n"); | |
1367 | return ret; | |
1368 | } | |
1369 | /* ChOutputCurentLevel: protected output current */ | |
f8e96dff | 1370 | ret = ab8500_charger_set_output_curr(di, ich_out); |
84edbeea | 1371 | if (ret) { |
f8e96dff JB |
1372 | dev_err(di->dev, "%s " |
1373 | "Failed to set ChOutputCurentLevel\n", | |
1374 | __func__); | |
84edbeea AM |
1375 | return ret; |
1376 | } | |
1377 | /* Check if VBAT overshoot control should be enabled */ | |
b0284de0 | 1378 | if (!di->bm->enable_overshoot) |
84edbeea AM |
1379 | overshoot = USB_CHG_NO_OVERSHOOT_ENA_N; |
1380 | ||
1381 | /* Enable USB Charger */ | |
1382 | ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, | |
1383 | AB8500_USBCH_CTRL1_REG, USB_CH_ENA | overshoot); | |
1384 | if (ret) { | |
1385 | dev_err(di->dev, "%s write failed\n", __func__); | |
1386 | return ret; | |
1387 | } | |
1388 | ||
1389 | /* If success power on charging LED indication */ | |
1390 | ret = ab8500_charger_led_en(di, true); | |
1391 | if (ret < 0) | |
1392 | dev_err(di->dev, "failed to enable LED\n"); | |
1393 | ||
1394 | queue_delayed_work(di->charger_wq, &di->check_vbat_work, HZ); | |
1395 | ||
1396 | di->usb.charger_online = 1; | |
1397 | } else { | |
1398 | /* Disable USB charging */ | |
1399 | ret = abx500_set_register_interruptible(di->dev, | |
1400 | AB8500_CHARGER, | |
1401 | AB8500_USBCH_CTRL1_REG, 0); | |
1402 | if (ret) { | |
1403 | dev_err(di->dev, | |
1404 | "%s write failed\n", __func__); | |
1405 | return ret; | |
1406 | } | |
1407 | ||
1408 | ret = ab8500_charger_led_en(di, false); | |
1409 | if (ret < 0) | |
1410 | dev_err(di->dev, "failed to disable LED\n"); | |
1411 | ||
1412 | di->usb.charger_online = 0; | |
1413 | di->usb.wd_expired = false; | |
1414 | ||
1415 | /* Disable regulator if enabled */ | |
1416 | if (di->vddadc_en_usb) { | |
1417 | regulator_disable(di->regu); | |
1418 | di->vddadc_en_usb = false; | |
1419 | } | |
1420 | ||
1421 | dev_dbg(di->dev, "%s Disabled USB charging\n", __func__); | |
1422 | ||
1423 | /* Cancel any pending Vbat check work */ | |
1424 | if (delayed_work_pending(&di->check_vbat_work)) | |
1425 | cancel_delayed_work(&di->check_vbat_work); | |
1426 | ||
1427 | } | |
1428 | ab8500_power_supply_changed(di, &di->usb_chg.psy); | |
1429 | ||
1430 | return ret; | |
1431 | } | |
1432 | ||
1433 | /** | |
1434 | * ab8500_charger_watchdog_kick() - kick charger watchdog | |
1435 | * @di: pointer to the ab8500_charger structure | |
1436 | * | |
1437 | * Kick charger watchdog | |
1438 | * Returns error code in case of failure else 0(on success) | |
1439 | */ | |
1440 | static int ab8500_charger_watchdog_kick(struct ux500_charger *charger) | |
1441 | { | |
1442 | int ret; | |
1443 | struct ab8500_charger *di; | |
1444 | ||
1445 | if (charger->psy.type == POWER_SUPPLY_TYPE_MAINS) | |
1446 | di = to_ab8500_charger_ac_device_info(charger); | |
1447 | else if (charger->psy.type == POWER_SUPPLY_TYPE_USB) | |
1448 | di = to_ab8500_charger_usb_device_info(charger); | |
1449 | else | |
1450 | return -ENXIO; | |
1451 | ||
1452 | ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, | |
1453 | AB8500_CHARG_WD_CTRL, CHARG_WD_KICK); | |
1454 | if (ret) | |
1455 | dev_err(di->dev, "Failed to kick WD!\n"); | |
1456 | ||
1457 | return ret; | |
1458 | } | |
1459 | ||
1460 | /** | |
1461 | * ab8500_charger_update_charger_current() - update charger current | |
1462 | * @di: pointer to the ab8500_charger structure | |
1463 | * | |
1464 | * Update the charger output current for the specified charger | |
1465 | * Returns error code in case of failure else 0(on success) | |
1466 | */ | |
1467 | static int ab8500_charger_update_charger_current(struct ux500_charger *charger, | |
1468 | int ich_out) | |
1469 | { | |
1470 | int ret; | |
84edbeea AM |
1471 | struct ab8500_charger *di; |
1472 | ||
1473 | if (charger->psy.type == POWER_SUPPLY_TYPE_MAINS) | |
1474 | di = to_ab8500_charger_ac_device_info(charger); | |
1475 | else if (charger->psy.type == POWER_SUPPLY_TYPE_USB) | |
1476 | di = to_ab8500_charger_usb_device_info(charger); | |
1477 | else | |
1478 | return -ENXIO; | |
1479 | ||
f8e96dff | 1480 | ret = ab8500_charger_set_output_curr(di, ich_out); |
84edbeea | 1481 | if (ret) { |
f8e96dff JB |
1482 | dev_err(di->dev, "%s " |
1483 | "Failed to set ChOutputCurentLevel\n", | |
1484 | __func__); | |
84edbeea AM |
1485 | return ret; |
1486 | } | |
1487 | ||
1488 | /* Reset the main and usb drop input current measurement counter */ | |
1489 | ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, | |
1490 | AB8500_CHARGER_CTRL, | |
1491 | 0x1); | |
1492 | if (ret) { | |
1493 | dev_err(di->dev, "%s write failed\n", __func__); | |
1494 | return ret; | |
1495 | } | |
1496 | ||
1497 | return ret; | |
1498 | } | |
1499 | ||
1500 | static int ab8500_charger_get_ext_psy_data(struct device *dev, void *data) | |
1501 | { | |
1502 | struct power_supply *psy; | |
1503 | struct power_supply *ext; | |
1504 | struct ab8500_charger *di; | |
1505 | union power_supply_propval ret; | |
1506 | int i, j; | |
1507 | bool psy_found = false; | |
1508 | struct ux500_charger *usb_chg; | |
1509 | ||
1510 | usb_chg = (struct ux500_charger *)data; | |
1511 | psy = &usb_chg->psy; | |
1512 | ||
1513 | di = to_ab8500_charger_usb_device_info(usb_chg); | |
1514 | ||
1515 | ext = dev_get_drvdata(dev); | |
1516 | ||
1517 | /* For all psy where the driver name appears in any supplied_to */ | |
1518 | for (i = 0; i < ext->num_supplicants; i++) { | |
1519 | if (!strcmp(ext->supplied_to[i], psy->name)) | |
1520 | psy_found = true; | |
1521 | } | |
1522 | ||
1523 | if (!psy_found) | |
1524 | return 0; | |
1525 | ||
1526 | /* Go through all properties for the psy */ | |
1527 | for (j = 0; j < ext->num_properties; j++) { | |
1528 | enum power_supply_property prop; | |
1529 | prop = ext->properties[j]; | |
1530 | ||
1531 | if (ext->get_property(ext, prop, &ret)) | |
1532 | continue; | |
1533 | ||
1534 | switch (prop) { | |
1535 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: | |
1536 | switch (ext->type) { | |
1537 | case POWER_SUPPLY_TYPE_BATTERY: | |
1538 | di->vbat = ret.intval / 1000; | |
1539 | break; | |
1540 | default: | |
1541 | break; | |
1542 | } | |
1543 | break; | |
1544 | default: | |
1545 | break; | |
1546 | } | |
1547 | } | |
1548 | return 0; | |
1549 | } | |
1550 | ||
1551 | /** | |
1552 | * ab8500_charger_check_vbat_work() - keep vbus current within spec | |
1553 | * @work pointer to the work_struct structure | |
1554 | * | |
1555 | * Due to a asic bug it is necessary to lower the input current to the vbus | |
1556 | * charger when charging with at some specific levels. This issue is only valid | |
1557 | * for below a certain battery voltage. This function makes sure that the | |
1558 | * the allowed current limit isn't exceeded. | |
1559 | */ | |
1560 | static void ab8500_charger_check_vbat_work(struct work_struct *work) | |
1561 | { | |
1562 | int t = 10; | |
1563 | struct ab8500_charger *di = container_of(work, | |
1564 | struct ab8500_charger, check_vbat_work.work); | |
1565 | ||
1566 | class_for_each_device(power_supply_class, NULL, | |
1567 | &di->usb_chg.psy, ab8500_charger_get_ext_psy_data); | |
1568 | ||
1569 | /* First run old_vbat is 0. */ | |
1570 | if (di->old_vbat == 0) | |
1571 | di->old_vbat = di->vbat; | |
1572 | ||
1573 | if (!((di->old_vbat <= VBAT_TRESH_IP_CUR_RED && | |
1574 | di->vbat <= VBAT_TRESH_IP_CUR_RED) || | |
1575 | (di->old_vbat > VBAT_TRESH_IP_CUR_RED && | |
1576 | di->vbat > VBAT_TRESH_IP_CUR_RED))) { | |
1577 | ||
1578 | dev_dbg(di->dev, "Vbat did cross threshold, curr: %d, new: %d," | |
1579 | " old: %d\n", di->max_usb_in_curr, di->vbat, | |
1580 | di->old_vbat); | |
1581 | ab8500_charger_set_vbus_in_curr(di, di->max_usb_in_curr); | |
1582 | power_supply_changed(&di->usb_chg.psy); | |
1583 | } | |
1584 | ||
1585 | di->old_vbat = di->vbat; | |
1586 | ||
1587 | /* | |
1588 | * No need to check the battery voltage every second when not close to | |
1589 | * the threshold. | |
1590 | */ | |
1591 | if (di->vbat < (VBAT_TRESH_IP_CUR_RED + 100) && | |
1592 | (di->vbat > (VBAT_TRESH_IP_CUR_RED - 100))) | |
1593 | t = 1; | |
1594 | ||
1595 | queue_delayed_work(di->charger_wq, &di->check_vbat_work, t * HZ); | |
1596 | } | |
1597 | ||
1598 | /** | |
1599 | * ab8500_charger_check_hw_failure_work() - check main charger failure | |
1600 | * @work: pointer to the work_struct structure | |
1601 | * | |
1602 | * Work queue function for checking the main charger status | |
1603 | */ | |
1604 | static void ab8500_charger_check_hw_failure_work(struct work_struct *work) | |
1605 | { | |
1606 | int ret; | |
1607 | u8 reg_value; | |
1608 | ||
1609 | struct ab8500_charger *di = container_of(work, | |
1610 | struct ab8500_charger, check_hw_failure_work.work); | |
1611 | ||
1612 | /* Check if the status bits for HW failure is still active */ | |
1613 | if (di->flags.mainextchnotok) { | |
1614 | ret = abx500_get_register_interruptible(di->dev, | |
1615 | AB8500_CHARGER, AB8500_CH_STATUS2_REG, ®_value); | |
1616 | if (ret < 0) { | |
1617 | dev_err(di->dev, "%s ab8500 read failed\n", __func__); | |
1618 | return; | |
1619 | } | |
1620 | if (!(reg_value & MAIN_CH_NOK)) { | |
1621 | di->flags.mainextchnotok = false; | |
1622 | ab8500_power_supply_changed(di, &di->ac_chg.psy); | |
1623 | } | |
1624 | } | |
1625 | if (di->flags.vbus_ovv) { | |
1626 | ret = abx500_get_register_interruptible(di->dev, | |
1627 | AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, | |
1628 | ®_value); | |
1629 | if (ret < 0) { | |
1630 | dev_err(di->dev, "%s ab8500 read failed\n", __func__); | |
1631 | return; | |
1632 | } | |
1633 | if (!(reg_value & VBUS_OVV_TH)) { | |
1634 | di->flags.vbus_ovv = false; | |
1635 | ab8500_power_supply_changed(di, &di->usb_chg.psy); | |
1636 | } | |
1637 | } | |
1638 | /* If we still have a failure, schedule a new check */ | |
1639 | if (di->flags.mainextchnotok || di->flags.vbus_ovv) { | |
1640 | queue_delayed_work(di->charger_wq, | |
1641 | &di->check_hw_failure_work, round_jiffies(HZ)); | |
1642 | } | |
1643 | } | |
1644 | ||
1645 | /** | |
1646 | * ab8500_charger_kick_watchdog_work() - kick the watchdog | |
1647 | * @work: pointer to the work_struct structure | |
1648 | * | |
1649 | * Work queue function for kicking the charger watchdog. | |
1650 | * | |
1651 | * For ABB revision 1.0 and 1.1 there is a bug in the watchdog | |
1652 | * logic. That means we have to continously kick the charger | |
1653 | * watchdog even when no charger is connected. This is only | |
1654 | * valid once the AC charger has been enabled. This is | |
1655 | * a bug that is not handled by the algorithm and the | |
1656 | * watchdog have to be kicked by the charger driver | |
1657 | * when the AC charger is disabled | |
1658 | */ | |
1659 | static void ab8500_charger_kick_watchdog_work(struct work_struct *work) | |
1660 | { | |
1661 | int ret; | |
1662 | ||
1663 | struct ab8500_charger *di = container_of(work, | |
1664 | struct ab8500_charger, kick_wd_work.work); | |
1665 | ||
1666 | ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, | |
1667 | AB8500_CHARG_WD_CTRL, CHARG_WD_KICK); | |
1668 | if (ret) | |
1669 | dev_err(di->dev, "Failed to kick WD!\n"); | |
1670 | ||
1671 | /* Schedule a new watchdog kick */ | |
1672 | queue_delayed_work(di->charger_wq, | |
1673 | &di->kick_wd_work, round_jiffies(WD_KICK_INTERVAL)); | |
1674 | } | |
1675 | ||
1676 | /** | |
1677 | * ab8500_charger_ac_work() - work to get and set main charger status | |
1678 | * @work: pointer to the work_struct structure | |
1679 | * | |
1680 | * Work queue function for checking the main charger status | |
1681 | */ | |
1682 | static void ab8500_charger_ac_work(struct work_struct *work) | |
1683 | { | |
1684 | int ret; | |
1685 | ||
1686 | struct ab8500_charger *di = container_of(work, | |
1687 | struct ab8500_charger, ac_work); | |
1688 | ||
1689 | /* | |
1690 | * Since we can't be sure that the events are received | |
1691 | * synchronously, we have the check if the main charger is | |
1692 | * connected by reading the status register | |
1693 | */ | |
1694 | ret = ab8500_charger_detect_chargers(di); | |
1695 | if (ret < 0) | |
1696 | return; | |
1697 | ||
1698 | if (ret & AC_PW_CONN) { | |
1699 | di->ac.charger_connected = 1; | |
1700 | di->ac_conn = true; | |
1701 | } else { | |
1702 | di->ac.charger_connected = 0; | |
1703 | } | |
1704 | ||
1705 | ab8500_power_supply_changed(di, &di->ac_chg.psy); | |
1706 | sysfs_notify(&di->ac_chg.psy.dev->kobj, NULL, "present"); | |
1707 | } | |
1708 | ||
1709 | /** | |
1710 | * ab8500_charger_detect_usb_type_work() - work to detect USB type | |
1711 | * @work: Pointer to the work_struct structure | |
1712 | * | |
1713 | * Detect the type of USB plugged | |
1714 | */ | |
64eb9b02 | 1715 | static void ab8500_charger_detect_usb_type_work(struct work_struct *work) |
84edbeea AM |
1716 | { |
1717 | int ret; | |
1718 | ||
1719 | struct ab8500_charger *di = container_of(work, | |
1720 | struct ab8500_charger, detect_usb_type_work); | |
1721 | ||
1722 | /* | |
1723 | * Since we can't be sure that the events are received | |
1724 | * synchronously, we have the check if is | |
1725 | * connected by reading the status register | |
1726 | */ | |
1727 | ret = ab8500_charger_detect_chargers(di); | |
1728 | if (ret < 0) | |
1729 | return; | |
1730 | ||
1731 | if (!(ret & USB_PW_CONN)) { | |
1732 | di->vbus_detected = 0; | |
1733 | ab8500_charger_set_usb_connected(di, false); | |
1734 | ab8500_power_supply_changed(di, &di->usb_chg.psy); | |
1735 | } else { | |
1736 | di->vbus_detected = 1; | |
1737 | ||
1738 | if (is_ab8500_1p1_or_earlier(di->parent)) { | |
1739 | ret = ab8500_charger_detect_usb_type(di); | |
1740 | if (!ret) { | |
1741 | ab8500_charger_set_usb_connected(di, true); | |
1742 | ab8500_power_supply_changed(di, | |
1743 | &di->usb_chg.psy); | |
1744 | } | |
1745 | } else { | |
1746 | /* For ABB cut2.0 and onwards we have an IRQ, | |
1747 | * USB_LINK_STATUS that will be triggered when the USB | |
1748 | * link status changes. The exception is USB connected | |
1749 | * during startup. Then we don't get a | |
1750 | * USB_LINK_STATUS IRQ | |
1751 | */ | |
1752 | if (di->vbus_detected_start) { | |
1753 | di->vbus_detected_start = false; | |
1754 | ret = ab8500_charger_detect_usb_type(di); | |
1755 | if (!ret) { | |
1756 | ab8500_charger_set_usb_connected(di, | |
1757 | true); | |
1758 | ab8500_power_supply_changed(di, | |
1759 | &di->usb_chg.psy); | |
1760 | } | |
1761 | } | |
1762 | } | |
1763 | } | |
1764 | } | |
1765 | ||
1766 | /** | |
1767 | * ab8500_charger_usb_link_status_work() - work to detect USB type | |
1768 | * @work: pointer to the work_struct structure | |
1769 | * | |
1770 | * Detect the type of USB plugged | |
1771 | */ | |
1772 | static void ab8500_charger_usb_link_status_work(struct work_struct *work) | |
1773 | { | |
1774 | int ret; | |
1775 | ||
1776 | struct ab8500_charger *di = container_of(work, | |
1777 | struct ab8500_charger, usb_link_status_work); | |
1778 | ||
1779 | /* | |
1780 | * Since we can't be sure that the events are received | |
1781 | * synchronously, we have the check if is | |
1782 | * connected by reading the status register | |
1783 | */ | |
1784 | ret = ab8500_charger_detect_chargers(di); | |
1785 | if (ret < 0) | |
1786 | return; | |
1787 | ||
1788 | if (!(ret & USB_PW_CONN)) { | |
1789 | di->vbus_detected = 0; | |
1790 | ab8500_charger_set_usb_connected(di, false); | |
1791 | ab8500_power_supply_changed(di, &di->usb_chg.psy); | |
1792 | } else { | |
1793 | di->vbus_detected = 1; | |
1794 | ret = ab8500_charger_read_usb_type(di); | |
1795 | if (!ret) { | |
1796 | /* Update maximum input current */ | |
1797 | ret = ab8500_charger_set_vbus_in_curr(di, | |
1798 | di->max_usb_in_curr); | |
1799 | if (ret) | |
1800 | return; | |
1801 | ||
1802 | ab8500_charger_set_usb_connected(di, true); | |
1803 | ab8500_power_supply_changed(di, &di->usb_chg.psy); | |
1804 | } else if (ret == -ENXIO) { | |
1805 | /* No valid charger type detected */ | |
1806 | ab8500_charger_set_usb_connected(di, false); | |
1807 | ab8500_power_supply_changed(di, &di->usb_chg.psy); | |
1808 | } | |
1809 | } | |
1810 | } | |
1811 | ||
1812 | static void ab8500_charger_usb_state_changed_work(struct work_struct *work) | |
1813 | { | |
1814 | int ret; | |
1815 | unsigned long flags; | |
1816 | ||
1817 | struct ab8500_charger *di = container_of(work, | |
1818 | struct ab8500_charger, usb_state_changed_work); | |
1819 | ||
1820 | if (!di->vbus_detected) | |
1821 | return; | |
1822 | ||
1823 | spin_lock_irqsave(&di->usb_state.usb_lock, flags); | |
1824 | di->usb_state.usb_changed = false; | |
1825 | spin_unlock_irqrestore(&di->usb_state.usb_lock, flags); | |
1826 | ||
1827 | /* | |
1828 | * wait for some time until you get updates from the usb stack | |
1829 | * and negotiations are completed | |
1830 | */ | |
1831 | msleep(250); | |
1832 | ||
1833 | if (di->usb_state.usb_changed) | |
1834 | return; | |
1835 | ||
1836 | dev_dbg(di->dev, "%s USB state: 0x%02x mA: %d\n", | |
1837 | __func__, di->usb_state.state, di->usb_state.usb_current); | |
1838 | ||
1839 | switch (di->usb_state.state) { | |
1840 | case AB8500_BM_USB_STATE_RESET_HS: | |
1841 | case AB8500_BM_USB_STATE_RESET_FS: | |
1842 | case AB8500_BM_USB_STATE_SUSPEND: | |
1843 | case AB8500_BM_USB_STATE_MAX: | |
1844 | ab8500_charger_set_usb_connected(di, false); | |
1845 | ab8500_power_supply_changed(di, &di->usb_chg.psy); | |
1846 | break; | |
1847 | ||
1848 | case AB8500_BM_USB_STATE_RESUME: | |
1849 | /* | |
1850 | * when suspend->resume there should be delay | |
1851 | * of 1sec for enabling charging | |
1852 | */ | |
1853 | msleep(1000); | |
1854 | /* Intentional fall through */ | |
1855 | case AB8500_BM_USB_STATE_CONFIGURED: | |
1856 | /* | |
1857 | * USB is configured, enable charging with the charging | |
1858 | * input current obtained from USB driver | |
1859 | */ | |
1860 | if (!ab8500_charger_get_usb_cur(di)) { | |
1861 | /* Update maximum input current */ | |
1862 | ret = ab8500_charger_set_vbus_in_curr(di, | |
1863 | di->max_usb_in_curr); | |
1864 | if (ret) | |
1865 | return; | |
1866 | ||
1867 | ab8500_charger_set_usb_connected(di, true); | |
1868 | ab8500_power_supply_changed(di, &di->usb_chg.psy); | |
1869 | } | |
1870 | break; | |
1871 | ||
1872 | default: | |
1873 | break; | |
1874 | }; | |
1875 | } | |
1876 | ||
1877 | /** | |
1878 | * ab8500_charger_check_usbchargernotok_work() - check USB chg not ok status | |
1879 | * @work: pointer to the work_struct structure | |
1880 | * | |
1881 | * Work queue function for checking the USB charger Not OK status | |
1882 | */ | |
1883 | static void ab8500_charger_check_usbchargernotok_work(struct work_struct *work) | |
1884 | { | |
1885 | int ret; | |
1886 | u8 reg_value; | |
1887 | bool prev_status; | |
1888 | ||
1889 | struct ab8500_charger *di = container_of(work, | |
1890 | struct ab8500_charger, check_usbchgnotok_work.work); | |
1891 | ||
1892 | /* Check if the status bit for usbchargernotok is still active */ | |
1893 | ret = abx500_get_register_interruptible(di->dev, | |
1894 | AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, ®_value); | |
1895 | if (ret < 0) { | |
1896 | dev_err(di->dev, "%s ab8500 read failed\n", __func__); | |
1897 | return; | |
1898 | } | |
1899 | prev_status = di->flags.usbchargernotok; | |
1900 | ||
1901 | if (reg_value & VBUS_CH_NOK) { | |
1902 | di->flags.usbchargernotok = true; | |
1903 | /* Check again in 1sec */ | |
1904 | queue_delayed_work(di->charger_wq, | |
1905 | &di->check_usbchgnotok_work, HZ); | |
1906 | } else { | |
1907 | di->flags.usbchargernotok = false; | |
1908 | di->flags.vbus_collapse = false; | |
1909 | } | |
1910 | ||
1911 | if (prev_status != di->flags.usbchargernotok) | |
1912 | ab8500_power_supply_changed(di, &di->usb_chg.psy); | |
1913 | } | |
1914 | ||
1915 | /** | |
1916 | * ab8500_charger_check_main_thermal_prot_work() - check main thermal status | |
1917 | * @work: pointer to the work_struct structure | |
1918 | * | |
1919 | * Work queue function for checking the Main thermal prot status | |
1920 | */ | |
1921 | static void ab8500_charger_check_main_thermal_prot_work( | |
1922 | struct work_struct *work) | |
1923 | { | |
1924 | int ret; | |
1925 | u8 reg_value; | |
1926 | ||
1927 | struct ab8500_charger *di = container_of(work, | |
1928 | struct ab8500_charger, check_main_thermal_prot_work); | |
1929 | ||
1930 | /* Check if the status bit for main_thermal_prot is still active */ | |
1931 | ret = abx500_get_register_interruptible(di->dev, | |
1932 | AB8500_CHARGER, AB8500_CH_STATUS2_REG, ®_value); | |
1933 | if (ret < 0) { | |
1934 | dev_err(di->dev, "%s ab8500 read failed\n", __func__); | |
1935 | return; | |
1936 | } | |
1937 | if (reg_value & MAIN_CH_TH_PROT) | |
1938 | di->flags.main_thermal_prot = true; | |
1939 | else | |
1940 | di->flags.main_thermal_prot = false; | |
1941 | ||
1942 | ab8500_power_supply_changed(di, &di->ac_chg.psy); | |
1943 | } | |
1944 | ||
1945 | /** | |
1946 | * ab8500_charger_check_usb_thermal_prot_work() - check usb thermal status | |
1947 | * @work: pointer to the work_struct structure | |
1948 | * | |
1949 | * Work queue function for checking the USB thermal prot status | |
1950 | */ | |
1951 | static void ab8500_charger_check_usb_thermal_prot_work( | |
1952 | struct work_struct *work) | |
1953 | { | |
1954 | int ret; | |
1955 | u8 reg_value; | |
1956 | ||
1957 | struct ab8500_charger *di = container_of(work, | |
1958 | struct ab8500_charger, check_usb_thermal_prot_work); | |
1959 | ||
1960 | /* Check if the status bit for usb_thermal_prot is still active */ | |
1961 | ret = abx500_get_register_interruptible(di->dev, | |
1962 | AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, ®_value); | |
1963 | if (ret < 0) { | |
1964 | dev_err(di->dev, "%s ab8500 read failed\n", __func__); | |
1965 | return; | |
1966 | } | |
1967 | if (reg_value & USB_CH_TH_PROT) | |
1968 | di->flags.usb_thermal_prot = true; | |
1969 | else | |
1970 | di->flags.usb_thermal_prot = false; | |
1971 | ||
1972 | ab8500_power_supply_changed(di, &di->usb_chg.psy); | |
1973 | } | |
1974 | ||
1975 | /** | |
1976 | * ab8500_charger_mainchunplugdet_handler() - main charger unplugged | |
1977 | * @irq: interrupt number | |
1978 | * @_di: pointer to the ab8500_charger structure | |
1979 | * | |
1980 | * Returns IRQ status(IRQ_HANDLED) | |
1981 | */ | |
1982 | static irqreturn_t ab8500_charger_mainchunplugdet_handler(int irq, void *_di) | |
1983 | { | |
1984 | struct ab8500_charger *di = _di; | |
1985 | ||
1986 | dev_dbg(di->dev, "Main charger unplugged\n"); | |
1987 | queue_work(di->charger_wq, &di->ac_work); | |
1988 | ||
1989 | return IRQ_HANDLED; | |
1990 | } | |
1991 | ||
1992 | /** | |
1993 | * ab8500_charger_mainchplugdet_handler() - main charger plugged | |
1994 | * @irq: interrupt number | |
1995 | * @_di: pointer to the ab8500_charger structure | |
1996 | * | |
1997 | * Returns IRQ status(IRQ_HANDLED) | |
1998 | */ | |
1999 | static irqreturn_t ab8500_charger_mainchplugdet_handler(int irq, void *_di) | |
2000 | { | |
2001 | struct ab8500_charger *di = _di; | |
2002 | ||
2003 | dev_dbg(di->dev, "Main charger plugged\n"); | |
2004 | queue_work(di->charger_wq, &di->ac_work); | |
2005 | ||
2006 | return IRQ_HANDLED; | |
2007 | } | |
2008 | ||
2009 | /** | |
2010 | * ab8500_charger_mainextchnotok_handler() - main charger not ok | |
2011 | * @irq: interrupt number | |
2012 | * @_di: pointer to the ab8500_charger structure | |
2013 | * | |
2014 | * Returns IRQ status(IRQ_HANDLED) | |
2015 | */ | |
2016 | static irqreturn_t ab8500_charger_mainextchnotok_handler(int irq, void *_di) | |
2017 | { | |
2018 | struct ab8500_charger *di = _di; | |
2019 | ||
2020 | dev_dbg(di->dev, "Main charger not ok\n"); | |
2021 | di->flags.mainextchnotok = true; | |
2022 | ab8500_power_supply_changed(di, &di->ac_chg.psy); | |
2023 | ||
2024 | /* Schedule a new HW failure check */ | |
2025 | queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0); | |
2026 | ||
2027 | return IRQ_HANDLED; | |
2028 | } | |
2029 | ||
2030 | /** | |
2031 | * ab8500_charger_mainchthprotr_handler() - Die temp is above main charger | |
2032 | * thermal protection threshold | |
2033 | * @irq: interrupt number | |
2034 | * @_di: pointer to the ab8500_charger structure | |
2035 | * | |
2036 | * Returns IRQ status(IRQ_HANDLED) | |
2037 | */ | |
2038 | static irqreturn_t ab8500_charger_mainchthprotr_handler(int irq, void *_di) | |
2039 | { | |
2040 | struct ab8500_charger *di = _di; | |
2041 | ||
2042 | dev_dbg(di->dev, | |
2043 | "Die temp above Main charger thermal protection threshold\n"); | |
2044 | queue_work(di->charger_wq, &di->check_main_thermal_prot_work); | |
2045 | ||
2046 | return IRQ_HANDLED; | |
2047 | } | |
2048 | ||
2049 | /** | |
2050 | * ab8500_charger_mainchthprotf_handler() - Die temp is below main charger | |
2051 | * thermal protection threshold | |
2052 | * @irq: interrupt number | |
2053 | * @_di: pointer to the ab8500_charger structure | |
2054 | * | |
2055 | * Returns IRQ status(IRQ_HANDLED) | |
2056 | */ | |
2057 | static irqreturn_t ab8500_charger_mainchthprotf_handler(int irq, void *_di) | |
2058 | { | |
2059 | struct ab8500_charger *di = _di; | |
2060 | ||
2061 | dev_dbg(di->dev, | |
2062 | "Die temp ok for Main charger thermal protection threshold\n"); | |
2063 | queue_work(di->charger_wq, &di->check_main_thermal_prot_work); | |
2064 | ||
2065 | return IRQ_HANDLED; | |
2066 | } | |
2067 | ||
2068 | /** | |
2069 | * ab8500_charger_vbusdetf_handler() - VBUS falling detected | |
2070 | * @irq: interrupt number | |
2071 | * @_di: pointer to the ab8500_charger structure | |
2072 | * | |
2073 | * Returns IRQ status(IRQ_HANDLED) | |
2074 | */ | |
2075 | static irqreturn_t ab8500_charger_vbusdetf_handler(int irq, void *_di) | |
2076 | { | |
2077 | struct ab8500_charger *di = _di; | |
2078 | ||
2079 | dev_dbg(di->dev, "VBUS falling detected\n"); | |
2080 | queue_work(di->charger_wq, &di->detect_usb_type_work); | |
2081 | ||
2082 | return IRQ_HANDLED; | |
2083 | } | |
2084 | ||
2085 | /** | |
2086 | * ab8500_charger_vbusdetr_handler() - VBUS rising detected | |
2087 | * @irq: interrupt number | |
2088 | * @_di: pointer to the ab8500_charger structure | |
2089 | * | |
2090 | * Returns IRQ status(IRQ_HANDLED) | |
2091 | */ | |
2092 | static irqreturn_t ab8500_charger_vbusdetr_handler(int irq, void *_di) | |
2093 | { | |
2094 | struct ab8500_charger *di = _di; | |
2095 | ||
2096 | di->vbus_detected = true; | |
2097 | dev_dbg(di->dev, "VBUS rising detected\n"); | |
2098 | queue_work(di->charger_wq, &di->detect_usb_type_work); | |
2099 | ||
2100 | return IRQ_HANDLED; | |
2101 | } | |
2102 | ||
2103 | /** | |
2104 | * ab8500_charger_usblinkstatus_handler() - USB link status has changed | |
2105 | * @irq: interrupt number | |
2106 | * @_di: pointer to the ab8500_charger structure | |
2107 | * | |
2108 | * Returns IRQ status(IRQ_HANDLED) | |
2109 | */ | |
2110 | static irqreturn_t ab8500_charger_usblinkstatus_handler(int irq, void *_di) | |
2111 | { | |
2112 | struct ab8500_charger *di = _di; | |
2113 | ||
2114 | dev_dbg(di->dev, "USB link status changed\n"); | |
2115 | ||
2116 | queue_work(di->charger_wq, &di->usb_link_status_work); | |
2117 | ||
2118 | return IRQ_HANDLED; | |
2119 | } | |
2120 | ||
2121 | /** | |
2122 | * ab8500_charger_usbchthprotr_handler() - Die temp is above usb charger | |
2123 | * thermal protection threshold | |
2124 | * @irq: interrupt number | |
2125 | * @_di: pointer to the ab8500_charger structure | |
2126 | * | |
2127 | * Returns IRQ status(IRQ_HANDLED) | |
2128 | */ | |
2129 | static irqreturn_t ab8500_charger_usbchthprotr_handler(int irq, void *_di) | |
2130 | { | |
2131 | struct ab8500_charger *di = _di; | |
2132 | ||
2133 | dev_dbg(di->dev, | |
2134 | "Die temp above USB charger thermal protection threshold\n"); | |
2135 | queue_work(di->charger_wq, &di->check_usb_thermal_prot_work); | |
2136 | ||
2137 | return IRQ_HANDLED; | |
2138 | } | |
2139 | ||
2140 | /** | |
2141 | * ab8500_charger_usbchthprotf_handler() - Die temp is below usb charger | |
2142 | * thermal protection threshold | |
2143 | * @irq: interrupt number | |
2144 | * @_di: pointer to the ab8500_charger structure | |
2145 | * | |
2146 | * Returns IRQ status(IRQ_HANDLED) | |
2147 | */ | |
2148 | static irqreturn_t ab8500_charger_usbchthprotf_handler(int irq, void *_di) | |
2149 | { | |
2150 | struct ab8500_charger *di = _di; | |
2151 | ||
2152 | dev_dbg(di->dev, | |
2153 | "Die temp ok for USB charger thermal protection threshold\n"); | |
2154 | queue_work(di->charger_wq, &di->check_usb_thermal_prot_work); | |
2155 | ||
2156 | return IRQ_HANDLED; | |
2157 | } | |
2158 | ||
2159 | /** | |
2160 | * ab8500_charger_usbchargernotokr_handler() - USB charger not ok detected | |
2161 | * @irq: interrupt number | |
2162 | * @_di: pointer to the ab8500_charger structure | |
2163 | * | |
2164 | * Returns IRQ status(IRQ_HANDLED) | |
2165 | */ | |
2166 | static irqreturn_t ab8500_charger_usbchargernotokr_handler(int irq, void *_di) | |
2167 | { | |
2168 | struct ab8500_charger *di = _di; | |
2169 | ||
2170 | dev_dbg(di->dev, "Not allowed USB charger detected\n"); | |
2171 | queue_delayed_work(di->charger_wq, &di->check_usbchgnotok_work, 0); | |
2172 | ||
2173 | return IRQ_HANDLED; | |
2174 | } | |
2175 | ||
2176 | /** | |
2177 | * ab8500_charger_chwdexp_handler() - Charger watchdog expired | |
2178 | * @irq: interrupt number | |
2179 | * @_di: pointer to the ab8500_charger structure | |
2180 | * | |
2181 | * Returns IRQ status(IRQ_HANDLED) | |
2182 | */ | |
2183 | static irqreturn_t ab8500_charger_chwdexp_handler(int irq, void *_di) | |
2184 | { | |
2185 | struct ab8500_charger *di = _di; | |
2186 | ||
2187 | dev_dbg(di->dev, "Charger watchdog expired\n"); | |
2188 | ||
2189 | /* | |
2190 | * The charger that was online when the watchdog expired | |
2191 | * needs to be restarted for charging to start again | |
2192 | */ | |
2193 | if (di->ac.charger_online) { | |
2194 | di->ac.wd_expired = true; | |
2195 | ab8500_power_supply_changed(di, &di->ac_chg.psy); | |
2196 | } | |
2197 | if (di->usb.charger_online) { | |
2198 | di->usb.wd_expired = true; | |
2199 | ab8500_power_supply_changed(di, &di->usb_chg.psy); | |
2200 | } | |
2201 | ||
2202 | return IRQ_HANDLED; | |
2203 | } | |
2204 | ||
2205 | /** | |
2206 | * ab8500_charger_vbusovv_handler() - VBUS overvoltage detected | |
2207 | * @irq: interrupt number | |
2208 | * @_di: pointer to the ab8500_charger structure | |
2209 | * | |
2210 | * Returns IRQ status(IRQ_HANDLED) | |
2211 | */ | |
2212 | static irqreturn_t ab8500_charger_vbusovv_handler(int irq, void *_di) | |
2213 | { | |
2214 | struct ab8500_charger *di = _di; | |
2215 | ||
2216 | dev_dbg(di->dev, "VBUS overvoltage detected\n"); | |
2217 | di->flags.vbus_ovv = true; | |
2218 | ab8500_power_supply_changed(di, &di->usb_chg.psy); | |
2219 | ||
2220 | /* Schedule a new HW failure check */ | |
2221 | queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0); | |
2222 | ||
2223 | return IRQ_HANDLED; | |
2224 | } | |
2225 | ||
2226 | /** | |
2227 | * ab8500_charger_ac_get_property() - get the ac/mains properties | |
2228 | * @psy: pointer to the power_supply structure | |
2229 | * @psp: pointer to the power_supply_property structure | |
2230 | * @val: pointer to the power_supply_propval union | |
2231 | * | |
2232 | * This function gets called when an application tries to get the ac/mains | |
2233 | * properties by reading the sysfs files. | |
2234 | * AC/Mains properties are online, present and voltage. | |
2235 | * online: ac/mains charging is in progress or not | |
2236 | * present: presence of the ac/mains | |
2237 | * voltage: AC/Mains voltage | |
2238 | * Returns error code in case of failure else 0(on success) | |
2239 | */ | |
2240 | static int ab8500_charger_ac_get_property(struct power_supply *psy, | |
2241 | enum power_supply_property psp, | |
2242 | union power_supply_propval *val) | |
2243 | { | |
2244 | struct ab8500_charger *di; | |
2245 | ||
2246 | di = to_ab8500_charger_ac_device_info(psy_to_ux500_charger(psy)); | |
2247 | ||
2248 | switch (psp) { | |
2249 | case POWER_SUPPLY_PROP_HEALTH: | |
2250 | if (di->flags.mainextchnotok) | |
2251 | val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE; | |
2252 | else if (di->ac.wd_expired || di->usb.wd_expired) | |
2253 | val->intval = POWER_SUPPLY_HEALTH_DEAD; | |
2254 | else if (di->flags.main_thermal_prot) | |
2255 | val->intval = POWER_SUPPLY_HEALTH_OVERHEAT; | |
2256 | else | |
2257 | val->intval = POWER_SUPPLY_HEALTH_GOOD; | |
2258 | break; | |
2259 | case POWER_SUPPLY_PROP_ONLINE: | |
2260 | val->intval = di->ac.charger_online; | |
2261 | break; | |
2262 | case POWER_SUPPLY_PROP_PRESENT: | |
2263 | val->intval = di->ac.charger_connected; | |
2264 | break; | |
2265 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: | |
2266 | di->ac.charger_voltage = ab8500_charger_get_ac_voltage(di); | |
2267 | val->intval = di->ac.charger_voltage * 1000; | |
2268 | break; | |
2269 | case POWER_SUPPLY_PROP_VOLTAGE_AVG: | |
2270 | /* | |
2271 | * This property is used to indicate when CV mode is entered | |
2272 | * for the AC charger | |
2273 | */ | |
2274 | di->ac.cv_active = ab8500_charger_ac_cv(di); | |
2275 | val->intval = di->ac.cv_active; | |
2276 | break; | |
2277 | case POWER_SUPPLY_PROP_CURRENT_NOW: | |
2278 | val->intval = ab8500_charger_get_ac_current(di) * 1000; | |
2279 | break; | |
2280 | default: | |
2281 | return -EINVAL; | |
2282 | } | |
2283 | return 0; | |
2284 | } | |
2285 | ||
2286 | /** | |
2287 | * ab8500_charger_usb_get_property() - get the usb properties | |
2288 | * @psy: pointer to the power_supply structure | |
2289 | * @psp: pointer to the power_supply_property structure | |
2290 | * @val: pointer to the power_supply_propval union | |
2291 | * | |
2292 | * This function gets called when an application tries to get the usb | |
2293 | * properties by reading the sysfs files. | |
2294 | * USB properties are online, present and voltage. | |
2295 | * online: usb charging is in progress or not | |
2296 | * present: presence of the usb | |
2297 | * voltage: vbus voltage | |
2298 | * Returns error code in case of failure else 0(on success) | |
2299 | */ | |
2300 | static int ab8500_charger_usb_get_property(struct power_supply *psy, | |
2301 | enum power_supply_property psp, | |
2302 | union power_supply_propval *val) | |
2303 | { | |
2304 | struct ab8500_charger *di; | |
2305 | ||
2306 | di = to_ab8500_charger_usb_device_info(psy_to_ux500_charger(psy)); | |
2307 | ||
2308 | switch (psp) { | |
2309 | case POWER_SUPPLY_PROP_HEALTH: | |
2310 | if (di->flags.usbchargernotok) | |
2311 | val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE; | |
2312 | else if (di->ac.wd_expired || di->usb.wd_expired) | |
2313 | val->intval = POWER_SUPPLY_HEALTH_DEAD; | |
2314 | else if (di->flags.usb_thermal_prot) | |
2315 | val->intval = POWER_SUPPLY_HEALTH_OVERHEAT; | |
2316 | else if (di->flags.vbus_ovv) | |
2317 | val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE; | |
2318 | else | |
2319 | val->intval = POWER_SUPPLY_HEALTH_GOOD; | |
2320 | break; | |
2321 | case POWER_SUPPLY_PROP_ONLINE: | |
2322 | val->intval = di->usb.charger_online; | |
2323 | break; | |
2324 | case POWER_SUPPLY_PROP_PRESENT: | |
2325 | val->intval = di->usb.charger_connected; | |
2326 | break; | |
2327 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: | |
2328 | di->usb.charger_voltage = ab8500_charger_get_vbus_voltage(di); | |
2329 | val->intval = di->usb.charger_voltage * 1000; | |
2330 | break; | |
2331 | case POWER_SUPPLY_PROP_VOLTAGE_AVG: | |
2332 | /* | |
2333 | * This property is used to indicate when CV mode is entered | |
2334 | * for the USB charger | |
2335 | */ | |
2336 | di->usb.cv_active = ab8500_charger_usb_cv(di); | |
2337 | val->intval = di->usb.cv_active; | |
2338 | break; | |
2339 | case POWER_SUPPLY_PROP_CURRENT_NOW: | |
2340 | val->intval = ab8500_charger_get_usb_current(di) * 1000; | |
2341 | break; | |
2342 | case POWER_SUPPLY_PROP_CURRENT_AVG: | |
2343 | /* | |
2344 | * This property is used to indicate when VBUS has collapsed | |
2345 | * due to too high output current from the USB charger | |
2346 | */ | |
2347 | if (di->flags.vbus_collapse) | |
2348 | val->intval = 1; | |
2349 | else | |
2350 | val->intval = 0; | |
2351 | break; | |
2352 | default: | |
2353 | return -EINVAL; | |
2354 | } | |
2355 | return 0; | |
2356 | } | |
2357 | ||
2358 | /** | |
2359 | * ab8500_charger_init_hw_registers() - Set up charger related registers | |
2360 | * @di: pointer to the ab8500_charger structure | |
2361 | * | |
2362 | * Set up charger OVV, watchdog and maximum voltage registers as well as | |
2363 | * charging of the backup battery | |
2364 | */ | |
2365 | static int ab8500_charger_init_hw_registers(struct ab8500_charger *di) | |
2366 | { | |
2367 | int ret = 0; | |
2368 | ||
2369 | /* Setup maximum charger current and voltage for ABB cut2.0 */ | |
2370 | if (!is_ab8500_1p1_or_earlier(di->parent)) { | |
2371 | ret = abx500_set_register_interruptible(di->dev, | |
2372 | AB8500_CHARGER, | |
2373 | AB8500_CH_VOLT_LVL_MAX_REG, CH_VOL_LVL_4P6); | |
2374 | if (ret) { | |
2375 | dev_err(di->dev, | |
2376 | "failed to set CH_VOLT_LVL_MAX_REG\n"); | |
2377 | goto out; | |
2378 | } | |
2379 | ||
2380 | ret = abx500_set_register_interruptible(di->dev, | |
2381 | AB8500_CHARGER, | |
2382 | AB8500_CH_OPT_CRNTLVL_MAX_REG, CH_OP_CUR_LVL_1P6); | |
2383 | if (ret) { | |
2384 | dev_err(di->dev, | |
2385 | "failed to set CH_OPT_CRNTLVL_MAX_REG\n"); | |
2386 | goto out; | |
2387 | } | |
2388 | } | |
2389 | ||
2390 | /* VBUS OVV set to 6.3V and enable automatic current limitiation */ | |
2391 | ret = abx500_set_register_interruptible(di->dev, | |
2392 | AB8500_CHARGER, | |
2393 | AB8500_USBCH_CTRL2_REG, | |
2394 | VBUS_OVV_SELECT_6P3V | VBUS_AUTO_IN_CURR_LIM_ENA); | |
2395 | if (ret) { | |
2396 | dev_err(di->dev, "failed to set VBUS OVV\n"); | |
2397 | goto out; | |
2398 | } | |
2399 | ||
2400 | /* Enable main watchdog in OTP */ | |
2401 | ret = abx500_set_register_interruptible(di->dev, | |
2402 | AB8500_OTP_EMUL, AB8500_OTP_CONF_15, OTP_ENABLE_WD); | |
2403 | if (ret) { | |
2404 | dev_err(di->dev, "failed to enable main WD in OTP\n"); | |
2405 | goto out; | |
2406 | } | |
2407 | ||
2408 | /* Enable main watchdog */ | |
2409 | ret = abx500_set_register_interruptible(di->dev, | |
2410 | AB8500_SYS_CTRL2_BLOCK, | |
2411 | AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_ENA); | |
2412 | if (ret) { | |
2413 | dev_err(di->dev, "faile to enable main watchdog\n"); | |
2414 | goto out; | |
2415 | } | |
2416 | ||
2417 | /* | |
2418 | * Due to internal synchronisation, Enable and Kick watchdog bits | |
2419 | * cannot be enabled in a single write. | |
2420 | * A minimum delay of 2*32 kHz period (62.5µs) must be inserted | |
2421 | * between writing Enable then Kick bits. | |
2422 | */ | |
2423 | udelay(63); | |
2424 | ||
2425 | /* Kick main watchdog */ | |
2426 | ret = abx500_set_register_interruptible(di->dev, | |
2427 | AB8500_SYS_CTRL2_BLOCK, | |
2428 | AB8500_MAIN_WDOG_CTRL_REG, | |
2429 | (MAIN_WDOG_ENA | MAIN_WDOG_KICK)); | |
2430 | if (ret) { | |
2431 | dev_err(di->dev, "failed to kick main watchdog\n"); | |
2432 | goto out; | |
2433 | } | |
2434 | ||
2435 | /* Disable main watchdog */ | |
2436 | ret = abx500_set_register_interruptible(di->dev, | |
2437 | AB8500_SYS_CTRL2_BLOCK, | |
2438 | AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_DIS); | |
2439 | if (ret) { | |
2440 | dev_err(di->dev, "failed to disable main watchdog\n"); | |
2441 | goto out; | |
2442 | } | |
2443 | ||
2444 | /* Set watchdog timeout */ | |
2445 | ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, | |
2446 | AB8500_CH_WD_TIMER_REG, WD_TIMER); | |
2447 | if (ret) { | |
2448 | dev_err(di->dev, "failed to set charger watchdog timeout\n"); | |
2449 | goto out; | |
2450 | } | |
2451 | ||
2452 | /* Backup battery voltage and current */ | |
2453 | ret = abx500_set_register_interruptible(di->dev, | |
2454 | AB8500_RTC, | |
2455 | AB8500_RTC_BACKUP_CHG_REG, | |
b0284de0 LJ |
2456 | di->bm->bkup_bat_v | |
2457 | di->bm->bkup_bat_i); | |
84edbeea AM |
2458 | if (ret) { |
2459 | dev_err(di->dev, "failed to setup backup battery charging\n"); | |
2460 | goto out; | |
2461 | } | |
2462 | ||
2463 | /* Enable backup battery charging */ | |
2464 | abx500_mask_and_set_register_interruptible(di->dev, | |
2465 | AB8500_RTC, AB8500_RTC_CTRL_REG, | |
2466 | RTC_BUP_CH_ENA, RTC_BUP_CH_ENA); | |
2467 | if (ret < 0) | |
2468 | dev_err(di->dev, "%s mask and set failed\n", __func__); | |
2469 | ||
2470 | out: | |
2471 | return ret; | |
2472 | } | |
2473 | ||
2474 | /* | |
2475 | * ab8500 charger driver interrupts and their respective isr | |
2476 | */ | |
2477 | static struct ab8500_charger_interrupts ab8500_charger_irq[] = { | |
2478 | {"MAIN_CH_UNPLUG_DET", ab8500_charger_mainchunplugdet_handler}, | |
2479 | {"MAIN_CHARGE_PLUG_DET", ab8500_charger_mainchplugdet_handler}, | |
2480 | {"MAIN_EXT_CH_NOT_OK", ab8500_charger_mainextchnotok_handler}, | |
2481 | {"MAIN_CH_TH_PROT_R", ab8500_charger_mainchthprotr_handler}, | |
2482 | {"MAIN_CH_TH_PROT_F", ab8500_charger_mainchthprotf_handler}, | |
2483 | {"VBUS_DET_F", ab8500_charger_vbusdetf_handler}, | |
2484 | {"VBUS_DET_R", ab8500_charger_vbusdetr_handler}, | |
2485 | {"USB_LINK_STATUS", ab8500_charger_usblinkstatus_handler}, | |
2486 | {"USB_CH_TH_PROT_R", ab8500_charger_usbchthprotr_handler}, | |
2487 | {"USB_CH_TH_PROT_F", ab8500_charger_usbchthprotf_handler}, | |
2488 | {"USB_CHARGER_NOT_OKR", ab8500_charger_usbchargernotokr_handler}, | |
2489 | {"VBUS_OVV", ab8500_charger_vbusovv_handler}, | |
2490 | {"CH_WD_EXP", ab8500_charger_chwdexp_handler}, | |
2491 | }; | |
2492 | ||
2493 | static int ab8500_charger_usb_notifier_call(struct notifier_block *nb, | |
2494 | unsigned long event, void *power) | |
2495 | { | |
2496 | struct ab8500_charger *di = | |
2497 | container_of(nb, struct ab8500_charger, nb); | |
2498 | enum ab8500_usb_state bm_usb_state; | |
2499 | unsigned mA = *((unsigned *)power); | |
2500 | ||
2501 | if (event != USB_EVENT_VBUS) { | |
2502 | dev_dbg(di->dev, "not a standard host, returning\n"); | |
2503 | return NOTIFY_DONE; | |
2504 | } | |
2505 | ||
2506 | /* TODO: State is fabricate here. See if charger really needs USB | |
2507 | * state or if mA is enough | |
2508 | */ | |
2509 | if ((di->usb_state.usb_current == 2) && (mA > 2)) | |
2510 | bm_usb_state = AB8500_BM_USB_STATE_RESUME; | |
2511 | else if (mA == 0) | |
2512 | bm_usb_state = AB8500_BM_USB_STATE_RESET_HS; | |
2513 | else if (mA == 2) | |
2514 | bm_usb_state = AB8500_BM_USB_STATE_SUSPEND; | |
2515 | else if (mA >= 8) /* 8, 100, 500 */ | |
2516 | bm_usb_state = AB8500_BM_USB_STATE_CONFIGURED; | |
2517 | else /* Should never occur */ | |
2518 | bm_usb_state = AB8500_BM_USB_STATE_RESET_FS; | |
2519 | ||
2520 | dev_dbg(di->dev, "%s usb_state: 0x%02x mA: %d\n", | |
2521 | __func__, bm_usb_state, mA); | |
2522 | ||
2523 | spin_lock(&di->usb_state.usb_lock); | |
2524 | di->usb_state.usb_changed = true; | |
2525 | spin_unlock(&di->usb_state.usb_lock); | |
2526 | ||
2527 | di->usb_state.state = bm_usb_state; | |
2528 | di->usb_state.usb_current = mA; | |
2529 | ||
2530 | queue_work(di->charger_wq, &di->usb_state_changed_work); | |
2531 | ||
2532 | return NOTIFY_OK; | |
2533 | } | |
2534 | ||
2535 | #if defined(CONFIG_PM) | |
2536 | static int ab8500_charger_resume(struct platform_device *pdev) | |
2537 | { | |
2538 | int ret; | |
2539 | struct ab8500_charger *di = platform_get_drvdata(pdev); | |
2540 | ||
2541 | /* | |
2542 | * For ABB revision 1.0 and 1.1 there is a bug in the watchdog | |
2543 | * logic. That means we have to continously kick the charger | |
2544 | * watchdog even when no charger is connected. This is only | |
2545 | * valid once the AC charger has been enabled. This is | |
2546 | * a bug that is not handled by the algorithm and the | |
2547 | * watchdog have to be kicked by the charger driver | |
2548 | * when the AC charger is disabled | |
2549 | */ | |
2550 | if (di->ac_conn && is_ab8500_1p1_or_earlier(di->parent)) { | |
2551 | ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, | |
2552 | AB8500_CHARG_WD_CTRL, CHARG_WD_KICK); | |
2553 | if (ret) | |
2554 | dev_err(di->dev, "Failed to kick WD!\n"); | |
2555 | ||
2556 | /* If not already pending start a new timer */ | |
2557 | if (!delayed_work_pending( | |
2558 | &di->kick_wd_work)) { | |
2559 | queue_delayed_work(di->charger_wq, &di->kick_wd_work, | |
2560 | round_jiffies(WD_KICK_INTERVAL)); | |
2561 | } | |
2562 | } | |
2563 | ||
2564 | /* If we still have a HW failure, schedule a new check */ | |
2565 | if (di->flags.mainextchnotok || di->flags.vbus_ovv) { | |
2566 | queue_delayed_work(di->charger_wq, | |
2567 | &di->check_hw_failure_work, 0); | |
2568 | } | |
2569 | ||
2570 | return 0; | |
2571 | } | |
2572 | ||
2573 | static int ab8500_charger_suspend(struct platform_device *pdev, | |
2574 | pm_message_t state) | |
2575 | { | |
2576 | struct ab8500_charger *di = platform_get_drvdata(pdev); | |
2577 | ||
2578 | /* Cancel any pending HW failure check */ | |
2579 | if (delayed_work_pending(&di->check_hw_failure_work)) | |
2580 | cancel_delayed_work(&di->check_hw_failure_work); | |
2581 | ||
2582 | return 0; | |
2583 | } | |
2584 | #else | |
2585 | #define ab8500_charger_suspend NULL | |
2586 | #define ab8500_charger_resume NULL | |
2587 | #endif | |
2588 | ||
415ec69f | 2589 | static int ab8500_charger_remove(struct platform_device *pdev) |
84edbeea AM |
2590 | { |
2591 | struct ab8500_charger *di = platform_get_drvdata(pdev); | |
2592 | int i, irq, ret; | |
2593 | ||
2594 | /* Disable AC charging */ | |
2595 | ab8500_charger_ac_en(&di->ac_chg, false, 0, 0); | |
2596 | ||
2597 | /* Disable USB charging */ | |
2598 | ab8500_charger_usb_en(&di->usb_chg, false, 0, 0); | |
2599 | ||
2600 | /* Disable interrupts */ | |
2601 | for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) { | |
2602 | irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name); | |
2603 | free_irq(irq, di); | |
2604 | } | |
2605 | ||
84edbeea AM |
2606 | /* Backup battery voltage and current disable */ |
2607 | ret = abx500_mask_and_set_register_interruptible(di->dev, | |
2608 | AB8500_RTC, AB8500_RTC_CTRL_REG, RTC_BUP_CH_ENA, 0); | |
2609 | if (ret < 0) | |
2610 | dev_err(di->dev, "%s mask and set failed\n", __func__); | |
2611 | ||
efd71c89 | 2612 | usb_unregister_notifier(di->usb_phy, &di->nb); |
721002ec | 2613 | usb_put_phy(di->usb_phy); |
84edbeea AM |
2614 | |
2615 | /* Delete the work queue */ | |
2616 | destroy_workqueue(di->charger_wq); | |
2617 | ||
2618 | flush_scheduled_work(); | |
2619 | power_supply_unregister(&di->usb_chg.psy); | |
2620 | power_supply_unregister(&di->ac_chg.psy); | |
2621 | platform_set_drvdata(pdev, NULL); | |
84edbeea AM |
2622 | |
2623 | return 0; | |
2624 | } | |
2625 | ||
4aef72db R |
2626 | static char *supply_interface[] = { |
2627 | "ab8500_chargalg", | |
2628 | "ab8500_fg", | |
2629 | "ab8500_btemp", | |
2630 | }; | |
2631 | ||
c8afa640 | 2632 | static int ab8500_charger_probe(struct platform_device *pdev) |
84edbeea | 2633 | { |
4aef72db | 2634 | struct device_node *np = pdev->dev.of_node; |
7722b799 | 2635 | struct abx500_bm_data *plat = pdev->dev.platform_data; |
2aac3de1 | 2636 | struct ab8500_charger *di; |
e0f1abeb | 2637 | int irq, i, charger_status, ret = 0; |
84edbeea | 2638 | |
4aef72db R |
2639 | di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL); |
2640 | if (!di) { | |
2641 | dev_err(&pdev->dev, "%s no mem for ab8500_charger\n", __func__); | |
84edbeea | 2642 | return -ENOMEM; |
4aef72db | 2643 | } |
7722b799 LJ |
2644 | |
2645 | if (!plat) { | |
2646 | dev_err(&pdev->dev, "no battery management data supplied\n"); | |
2647 | return -EINVAL; | |
2648 | } | |
2649 | di->bm = plat; | |
2650 | ||
2651 | if (np) { | |
2652 | ret = ab8500_bm_of_probe(&pdev->dev, np, di->bm); | |
2653 | if (ret) { | |
2654 | dev_err(&pdev->dev, "failed to get battery information\n"); | |
2655 | return ret; | |
4aef72db | 2656 | } |
7722b799 LJ |
2657 | di->autopower_cfg = of_property_read_bool(np, "autopower_cfg"); |
2658 | } else | |
4aef72db | 2659 | di->autopower_cfg = false; |
84edbeea AM |
2660 | |
2661 | /* get parent data */ | |
2662 | di->dev = &pdev->dev; | |
2663 | di->parent = dev_get_drvdata(pdev->dev.parent); | |
2664 | di->gpadc = ab8500_gpadc_get("ab8500-gpadc.0"); | |
2665 | ||
2666 | /* initialize lock */ | |
2667 | spin_lock_init(&di->usb_state.usb_lock); | |
2668 | ||
84edbeea AM |
2669 | di->autopower = false; |
2670 | ||
2671 | /* AC supply */ | |
2672 | /* power_supply base class */ | |
2673 | di->ac_chg.psy.name = "ab8500_ac"; | |
2674 | di->ac_chg.psy.type = POWER_SUPPLY_TYPE_MAINS; | |
2675 | di->ac_chg.psy.properties = ab8500_charger_ac_props; | |
2676 | di->ac_chg.psy.num_properties = ARRAY_SIZE(ab8500_charger_ac_props); | |
2677 | di->ac_chg.psy.get_property = ab8500_charger_ac_get_property; | |
4aef72db R |
2678 | di->ac_chg.psy.supplied_to = supply_interface; |
2679 | di->ac_chg.psy.num_supplicants = ARRAY_SIZE(supply_interface), | |
84edbeea AM |
2680 | /* ux500_charger sub-class */ |
2681 | di->ac_chg.ops.enable = &ab8500_charger_ac_en; | |
2682 | di->ac_chg.ops.kick_wd = &ab8500_charger_watchdog_kick; | |
2683 | di->ac_chg.ops.update_curr = &ab8500_charger_update_charger_current; | |
2684 | di->ac_chg.max_out_volt = ab8500_charger_voltage_map[ | |
2685 | ARRAY_SIZE(ab8500_charger_voltage_map) - 1]; | |
2686 | di->ac_chg.max_out_curr = ab8500_charger_current_map[ | |
2687 | ARRAY_SIZE(ab8500_charger_current_map) - 1]; | |
2688 | ||
2689 | /* USB supply */ | |
2690 | /* power_supply base class */ | |
2691 | di->usb_chg.psy.name = "ab8500_usb"; | |
2692 | di->usb_chg.psy.type = POWER_SUPPLY_TYPE_USB; | |
2693 | di->usb_chg.psy.properties = ab8500_charger_usb_props; | |
2694 | di->usb_chg.psy.num_properties = ARRAY_SIZE(ab8500_charger_usb_props); | |
2695 | di->usb_chg.psy.get_property = ab8500_charger_usb_get_property; | |
4aef72db R |
2696 | di->usb_chg.psy.supplied_to = supply_interface; |
2697 | di->usb_chg.psy.num_supplicants = ARRAY_SIZE(supply_interface), | |
84edbeea AM |
2698 | /* ux500_charger sub-class */ |
2699 | di->usb_chg.ops.enable = &ab8500_charger_usb_en; | |
2700 | di->usb_chg.ops.kick_wd = &ab8500_charger_watchdog_kick; | |
2701 | di->usb_chg.ops.update_curr = &ab8500_charger_update_charger_current; | |
2702 | di->usb_chg.max_out_volt = ab8500_charger_voltage_map[ | |
2703 | ARRAY_SIZE(ab8500_charger_voltage_map) - 1]; | |
2704 | di->usb_chg.max_out_curr = ab8500_charger_current_map[ | |
2705 | ARRAY_SIZE(ab8500_charger_current_map) - 1]; | |
2706 | ||
2707 | ||
2708 | /* Create a work queue for the charger */ | |
2709 | di->charger_wq = | |
2710 | create_singlethread_workqueue("ab8500_charger_wq"); | |
2711 | if (di->charger_wq == NULL) { | |
2712 | dev_err(di->dev, "failed to create work queue\n"); | |
4aef72db | 2713 | return -ENOMEM; |
84edbeea AM |
2714 | } |
2715 | ||
2716 | /* Init work for HW failure check */ | |
203b42f7 | 2717 | INIT_DEFERRABLE_WORK(&di->check_hw_failure_work, |
84edbeea | 2718 | ab8500_charger_check_hw_failure_work); |
203b42f7 | 2719 | INIT_DEFERRABLE_WORK(&di->check_usbchgnotok_work, |
84edbeea AM |
2720 | ab8500_charger_check_usbchargernotok_work); |
2721 | ||
2722 | /* | |
2723 | * For ABB revision 1.0 and 1.1 there is a bug in the watchdog | |
2724 | * logic. That means we have to continously kick the charger | |
2725 | * watchdog even when no charger is connected. This is only | |
2726 | * valid once the AC charger has been enabled. This is | |
2727 | * a bug that is not handled by the algorithm and the | |
2728 | * watchdog have to be kicked by the charger driver | |
2729 | * when the AC charger is disabled | |
2730 | */ | |
203b42f7 | 2731 | INIT_DEFERRABLE_WORK(&di->kick_wd_work, |
84edbeea AM |
2732 | ab8500_charger_kick_watchdog_work); |
2733 | ||
203b42f7 | 2734 | INIT_DEFERRABLE_WORK(&di->check_vbat_work, |
84edbeea AM |
2735 | ab8500_charger_check_vbat_work); |
2736 | ||
2737 | /* Init work for charger detection */ | |
2738 | INIT_WORK(&di->usb_link_status_work, | |
2739 | ab8500_charger_usb_link_status_work); | |
2740 | INIT_WORK(&di->ac_work, ab8500_charger_ac_work); | |
2741 | INIT_WORK(&di->detect_usb_type_work, | |
2742 | ab8500_charger_detect_usb_type_work); | |
2743 | ||
2744 | INIT_WORK(&di->usb_state_changed_work, | |
2745 | ab8500_charger_usb_state_changed_work); | |
2746 | ||
2747 | /* Init work for checking HW status */ | |
2748 | INIT_WORK(&di->check_main_thermal_prot_work, | |
2749 | ab8500_charger_check_main_thermal_prot_work); | |
2750 | INIT_WORK(&di->check_usb_thermal_prot_work, | |
2751 | ab8500_charger_check_usb_thermal_prot_work); | |
2752 | ||
2753 | /* | |
2754 | * VDD ADC supply needs to be enabled from this driver when there | |
2755 | * is a charger connected to avoid erroneous BTEMP_HIGH/LOW | |
2756 | * interrupts during charging | |
2757 | */ | |
8feffd10 | 2758 | di->regu = devm_regulator_get(di->dev, "vddadc"); |
84edbeea AM |
2759 | if (IS_ERR(di->regu)) { |
2760 | ret = PTR_ERR(di->regu); | |
2761 | dev_err(di->dev, "failed to get vddadc regulator\n"); | |
2762 | goto free_charger_wq; | |
2763 | } | |
2764 | ||
2765 | ||
2766 | /* Initialize OVV, and other registers */ | |
2767 | ret = ab8500_charger_init_hw_registers(di); | |
2768 | if (ret) { | |
2769 | dev_err(di->dev, "failed to initialize ABB registers\n"); | |
8feffd10 | 2770 | goto free_charger_wq; |
84edbeea AM |
2771 | } |
2772 | ||
2773 | /* Register AC charger class */ | |
2774 | ret = power_supply_register(di->dev, &di->ac_chg.psy); | |
2775 | if (ret) { | |
2776 | dev_err(di->dev, "failed to register AC charger\n"); | |
8feffd10 | 2777 | goto free_charger_wq; |
84edbeea AM |
2778 | } |
2779 | ||
2780 | /* Register USB charger class */ | |
2781 | ret = power_supply_register(di->dev, &di->usb_chg.psy); | |
2782 | if (ret) { | |
2783 | dev_err(di->dev, "failed to register USB charger\n"); | |
2784 | goto free_ac; | |
2785 | } | |
2786 | ||
662dca54 | 2787 | di->usb_phy = usb_get_phy(USB_PHY_TYPE_USB2); |
ded017ee | 2788 | if (IS_ERR_OR_NULL(di->usb_phy)) { |
efd71c89 | 2789 | dev_err(di->dev, "failed to get usb transceiver\n"); |
84edbeea AM |
2790 | ret = -EINVAL; |
2791 | goto free_usb; | |
2792 | } | |
2793 | di->nb.notifier_call = ab8500_charger_usb_notifier_call; | |
efd71c89 | 2794 | ret = usb_register_notifier(di->usb_phy, &di->nb); |
84edbeea | 2795 | if (ret) { |
efd71c89 AV |
2796 | dev_err(di->dev, "failed to register usb notifier\n"); |
2797 | goto put_usb_phy; | |
84edbeea AM |
2798 | } |
2799 | ||
2800 | /* Identify the connected charger types during startup */ | |
2801 | charger_status = ab8500_charger_detect_chargers(di); | |
2802 | if (charger_status & AC_PW_CONN) { | |
2803 | di->ac.charger_connected = 1; | |
2804 | di->ac_conn = true; | |
2805 | ab8500_power_supply_changed(di, &di->ac_chg.psy); | |
2806 | sysfs_notify(&di->ac_chg.psy.dev->kobj, NULL, "present"); | |
2807 | } | |
2808 | ||
2809 | if (charger_status & USB_PW_CONN) { | |
2810 | dev_dbg(di->dev, "VBUS Detect during startup\n"); | |
2811 | di->vbus_detected = true; | |
2812 | di->vbus_detected_start = true; | |
2813 | queue_work(di->charger_wq, | |
2814 | &di->detect_usb_type_work); | |
2815 | } | |
2816 | ||
2817 | /* Register interrupts */ | |
2818 | for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) { | |
2819 | irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name); | |
2820 | ret = request_threaded_irq(irq, NULL, ab8500_charger_irq[i].isr, | |
2821 | IRQF_SHARED | IRQF_NO_SUSPEND, | |
2822 | ab8500_charger_irq[i].name, di); | |
2823 | ||
2824 | if (ret != 0) { | |
2825 | dev_err(di->dev, "failed to request %s IRQ %d: %d\n" | |
2826 | , ab8500_charger_irq[i].name, irq, ret); | |
2827 | goto free_irq; | |
2828 | } | |
2829 | dev_dbg(di->dev, "Requested %s IRQ %d: %d\n", | |
2830 | ab8500_charger_irq[i].name, irq, ret); | |
2831 | } | |
2832 | ||
2833 | platform_set_drvdata(pdev, di); | |
2834 | ||
2835 | return ret; | |
2836 | ||
2837 | free_irq: | |
efd71c89 | 2838 | usb_unregister_notifier(di->usb_phy, &di->nb); |
84edbeea AM |
2839 | |
2840 | /* We also have to free all successfully registered irqs */ | |
2841 | for (i = i - 1; i >= 0; i--) { | |
2842 | irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name); | |
2843 | free_irq(irq, di); | |
2844 | } | |
efd71c89 | 2845 | put_usb_phy: |
721002ec | 2846 | usb_put_phy(di->usb_phy); |
84edbeea AM |
2847 | free_usb: |
2848 | power_supply_unregister(&di->usb_chg.psy); | |
2849 | free_ac: | |
2850 | power_supply_unregister(&di->ac_chg.psy); | |
84edbeea AM |
2851 | free_charger_wq: |
2852 | destroy_workqueue(di->charger_wq); | |
84edbeea AM |
2853 | return ret; |
2854 | } | |
2855 | ||
4aef72db R |
2856 | static const struct of_device_id ab8500_charger_match[] = { |
2857 | { .compatible = "stericsson,ab8500-charger", }, | |
2858 | { }, | |
2859 | }; | |
2860 | ||
84edbeea AM |
2861 | static struct platform_driver ab8500_charger_driver = { |
2862 | .probe = ab8500_charger_probe, | |
28ea73f4 | 2863 | .remove = ab8500_charger_remove, |
84edbeea AM |
2864 | .suspend = ab8500_charger_suspend, |
2865 | .resume = ab8500_charger_resume, | |
2866 | .driver = { | |
2867 | .name = "ab8500-charger", | |
2868 | .owner = THIS_MODULE, | |
4aef72db | 2869 | .of_match_table = ab8500_charger_match, |
84edbeea AM |
2870 | }, |
2871 | }; | |
2872 | ||
2873 | static int __init ab8500_charger_init(void) | |
2874 | { | |
2875 | return platform_driver_register(&ab8500_charger_driver); | |
2876 | } | |
2877 | ||
2878 | static void __exit ab8500_charger_exit(void) | |
2879 | { | |
2880 | platform_driver_unregister(&ab8500_charger_driver); | |
2881 | } | |
2882 | ||
2883 | subsys_initcall_sync(ab8500_charger_init); | |
2884 | module_exit(ab8500_charger_exit); | |
2885 | ||
2886 | MODULE_LICENSE("GPL v2"); | |
2887 | MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy"); | |
2888 | MODULE_ALIAS("platform:ab8500-charger"); | |
2889 | MODULE_DESCRIPTION("AB8500 charger management driver"); |