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