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