blob: f249a65b02e1f45d11f731ffbef229105363b5be [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>
Lee Jones88917162013-02-13 11:39:19 +000018#include <linux/notifier.h>
Arun Murthy84edbee2012-02-29 21:54:26 +053019#include <linux/slab.h>
20#include <linux/platform_device.h>
21#include <linux/power_supply.h>
22#include <linux/completion.h>
23#include <linux/regulator/consumer.h>
24#include <linux/err.h>
25#include <linux/workqueue.h>
26#include <linux/kobject.h>
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -080027#include <linux/of.h>
28#include <linux/mfd/core.h>
Arun Murthy84edbee2012-02-29 21:54:26 +053029#include <linux/mfd/abx500/ab8500.h>
30#include <linux/mfd/abx500.h>
31#include <linux/mfd/abx500/ab8500-bm.h>
32#include <linux/mfd/abx500/ab8500-gpadc.h>
33#include <linux/mfd/abx500/ux500_chargalg.h>
34#include <linux/usb/otg.h>
Lee Jonesb269fff2013-01-11 13:12:51 +000035#include <linux/mutex.h>
Arun Murthy84edbee2012-02-29 21:54:26 +053036
37/* Charger constants */
38#define NO_PW_CONN 0
39#define AC_PW_CONN 1
40#define USB_PW_CONN 2
41
42#define MAIN_WDOG_ENA 0x01
43#define MAIN_WDOG_KICK 0x02
44#define MAIN_WDOG_DIS 0x00
45#define CHARG_WD_KICK 0x01
46#define MAIN_CH_ENA 0x01
47#define MAIN_CH_NO_OVERSHOOT_ENA_N 0x02
48#define USB_CH_ENA 0x01
49#define USB_CHG_NO_OVERSHOOT_ENA_N 0x02
50#define MAIN_CH_DET 0x01
51#define MAIN_CH_CV_ON 0x04
52#define USB_CH_CV_ON 0x08
53#define VBUS_DET_DBNC100 0x02
54#define VBUS_DET_DBNC1 0x01
55#define OTP_ENABLE_WD 0x01
Lee Jones4dcdf572013-02-14 09:24:10 +000056#define DROP_COUNT_RESET 0x01
Arun Murthy84edbee2012-02-29 21:54:26 +053057
58#define MAIN_CH_INPUT_CURR_SHIFT 4
59#define VBUS_IN_CURR_LIM_SHIFT 4
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +010060#define AUTO_VBUS_IN_CURR_LIM_SHIFT 4
Lee Jonesf7470b52013-02-14 12:37:29 +000061#define VBUS_IN_CURR_LIM_RETRY_SET_TIME 30 /* seconds */
Arun Murthy84edbee2012-02-29 21:54:26 +053062
63#define LED_INDICATOR_PWM_ENA 0x01
64#define LED_INDICATOR_PWM_DIS 0x00
65#define LED_IND_CUR_5MA 0x04
66#define LED_INDICATOR_PWM_DUTY_252_256 0xBF
67
68/* HW failure constants */
69#define MAIN_CH_TH_PROT 0x02
70#define VBUS_CH_NOK 0x08
71#define USB_CH_TH_PROT 0x02
72#define VBUS_OVV_TH 0x01
73#define MAIN_CH_NOK 0x01
74#define VBUS_DET 0x80
75
Lee Jonesb269fff2013-01-11 13:12:51 +000076#define MAIN_CH_STATUS2_MAINCHGDROP 0x80
77#define MAIN_CH_STATUS2_MAINCHARGERDETDBNC 0x40
78#define USB_CH_VBUSDROP 0x40
79#define USB_CH_VBUSDETDBNC 0x01
80
Arun Murthy84edbee2012-02-29 21:54:26 +053081/* UsbLineStatus register bit masks */
82#define AB8500_USB_LINK_STATUS 0x78
Hakan Bergd4337662012-07-26 11:10:35 +020083#define AB8505_USB_LINK_STATUS 0xF8
Arun Murthy84edbee2012-02-29 21:54:26 +053084#define AB8500_STD_HOST_SUSP 0x18
85
86/* Watchdog timeout constant */
87#define WD_TIMER 0x30 /* 4min */
88#define WD_KICK_INTERVAL (60 * HZ)
89
90/* Lowest charger voltage is 3.39V -> 0x4E */
91#define LOW_VOLT_REG 0x4E
92
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +010093/* Step up/down delay in us */
94#define STEP_UDELAY 1000
95
Lee Jonesb269fff2013-01-11 13:12:51 +000096#define CHARGER_STATUS_POLL 10 /* in ms */
97
Loic Pallardye07a5642012-05-10 15:33:56 +020098#define CHG_WD_INTERVAL (60 * HZ)
99
Nicolas Guionb0163222012-05-23 15:59:43 +0200100#define AB8500_SW_CONTROL_FALLBACK 0x03
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100101/* Wait for enumeration before charing in us */
102#define WAIT_ACA_RID_ENUMERATION (5 * 1000)
Lee Jones88917162013-02-13 11:39:19 +0000103/*External charger control*/
104#define AB8500_SYS_CHARGER_CONTROL_REG 0x52
105#define EXTERNAL_CHARGER_DISABLE_REG_VAL 0x03
106#define EXTERNAL_CHARGER_ENABLE_REG_VAL 0x07
Nicolas Guionb0163222012-05-23 15:59:43 +0200107
Arun Murthy84edbee2012-02-29 21:54:26 +0530108/* UsbLineStatus register - usb types */
109enum ab8500_charger_link_status {
110 USB_STAT_NOT_CONFIGURED,
111 USB_STAT_STD_HOST_NC,
112 USB_STAT_STD_HOST_C_NS,
113 USB_STAT_STD_HOST_C_S,
114 USB_STAT_HOST_CHG_NM,
115 USB_STAT_HOST_CHG_HS,
116 USB_STAT_HOST_CHG_HS_CHIRP,
117 USB_STAT_DEDICATED_CHG,
118 USB_STAT_ACA_RID_A,
119 USB_STAT_ACA_RID_B,
120 USB_STAT_ACA_RID_C_NM,
121 USB_STAT_ACA_RID_C_HS,
122 USB_STAT_ACA_RID_C_HS_CHIRP,
123 USB_STAT_HM_IDGND,
124 USB_STAT_RESERVED,
125 USB_STAT_NOT_VALID_LINK,
Hakan Berg74a8e342013-01-11 13:12:58 +0000126 USB_STAT_PHY_EN,
127 USB_STAT_SUP_NO_IDGND_VBUS,
128 USB_STAT_SUP_IDGND_VBUS,
129 USB_STAT_CHARGER_LINE_1,
130 USB_STAT_CARKIT_1,
131 USB_STAT_CARKIT_2,
132 USB_STAT_ACA_DOCK_CHARGER,
Arun Murthy84edbee2012-02-29 21:54:26 +0530133};
134
135enum ab8500_usb_state {
136 AB8500_BM_USB_STATE_RESET_HS, /* HighSpeed Reset */
137 AB8500_BM_USB_STATE_RESET_FS, /* FullSpeed/LowSpeed Reset */
138 AB8500_BM_USB_STATE_CONFIGURED,
139 AB8500_BM_USB_STATE_SUSPEND,
140 AB8500_BM_USB_STATE_RESUME,
141 AB8500_BM_USB_STATE_MAX,
142};
143
144/* VBUS input current limits supported in AB8500 in mA */
145#define USB_CH_IP_CUR_LVL_0P05 50
146#define USB_CH_IP_CUR_LVL_0P09 98
147#define USB_CH_IP_CUR_LVL_0P19 193
148#define USB_CH_IP_CUR_LVL_0P29 290
149#define USB_CH_IP_CUR_LVL_0P38 380
150#define USB_CH_IP_CUR_LVL_0P45 450
151#define USB_CH_IP_CUR_LVL_0P5 500
152#define USB_CH_IP_CUR_LVL_0P6 600
153#define USB_CH_IP_CUR_LVL_0P7 700
154#define USB_CH_IP_CUR_LVL_0P8 800
155#define USB_CH_IP_CUR_LVL_0P9 900
156#define USB_CH_IP_CUR_LVL_1P0 1000
157#define USB_CH_IP_CUR_LVL_1P1 1100
158#define USB_CH_IP_CUR_LVL_1P3 1300
159#define USB_CH_IP_CUR_LVL_1P4 1400
160#define USB_CH_IP_CUR_LVL_1P5 1500
161
162#define VBAT_TRESH_IP_CUR_RED 3800
163
164#define to_ab8500_charger_usb_device_info(x) container_of((x), \
165 struct ab8500_charger, usb_chg)
166#define to_ab8500_charger_ac_device_info(x) container_of((x), \
167 struct ab8500_charger, ac_chg)
168
169/**
170 * struct ab8500_charger_interrupts - ab8500 interupts
171 * @name: name of the interrupt
172 * @isr function pointer to the isr
173 */
174struct ab8500_charger_interrupts {
175 char *name;
176 irqreturn_t (*isr)(int irq, void *data);
177};
178
179struct ab8500_charger_info {
180 int charger_connected;
181 int charger_online;
182 int charger_voltage;
183 int cv_active;
184 bool wd_expired;
Jonas Aaberga864c5a2013-01-11 13:12:53 +0000185 int charger_current;
Arun Murthy84edbee2012-02-29 21:54:26 +0530186};
187
188struct ab8500_charger_event_flags {
189 bool mainextchnotok;
190 bool main_thermal_prot;
191 bool usb_thermal_prot;
192 bool vbus_ovv;
193 bool usbchargernotok;
194 bool chgwdexp;
195 bool vbus_collapse;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100196 bool vbus_drop_end;
Arun Murthy84edbee2012-02-29 21:54:26 +0530197};
198
199struct ab8500_charger_usb_state {
Arun Murthy84edbee2012-02-29 21:54:26 +0530200 int usb_current;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100201 int usb_current_tmp;
Arun Murthy84edbee2012-02-29 21:54:26 +0530202 enum ab8500_usb_state state;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100203 enum ab8500_usb_state state_tmp;
Arun Murthy84edbee2012-02-29 21:54:26 +0530204 spinlock_t usb_lock;
205};
206
Lee Jonesf7470b52013-02-14 12:37:29 +0000207struct ab8500_charger_max_usb_in_curr {
208 int usb_type_max;
209 int set_max;
210 int calculated_max;
211};
212
Arun Murthy84edbee2012-02-29 21:54:26 +0530213/**
214 * struct ab8500_charger - ab8500 Charger device information
215 * @dev: Pointer to the structure device
Arun Murthy84edbee2012-02-29 21:54:26 +0530216 * @vbus_detected: VBUS detected
217 * @vbus_detected_start:
218 * VBUS detected during startup
219 * @ac_conn: This will be true when the AC charger has been plugged
220 * @vddadc_en_ac: Indicate if VDD ADC supply is enabled because AC
221 * charger is enabled
222 * @vddadc_en_usb: Indicate if VDD ADC supply is enabled because USB
223 * charger is enabled
224 * @vbat Battery voltage
225 * @old_vbat Previously measured battery voltage
Marcus Cooper4b45f4a2013-01-11 13:13:04 +0000226 * @usb_device_is_unrecognised USB device is unrecognised by the hardware
Arun Murthy84edbee2012-02-29 21:54:26 +0530227 * @autopower Indicate if we should have automatic pwron after pwrloss
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -0800228 * @autopower_cfg platform specific power config support for "pwron after pwrloss"
Henrik Sölverff380902012-04-17 15:51:01 +0200229 * @invalid_charger_detect_state State when forcing AB to use invalid charger
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100230 * @is_aca_rid: Incicate if accessory is ACA type
231 * @current_stepping_sessions:
232 * Counter for current stepping sessions
Arun Murthy84edbee2012-02-29 21:54:26 +0530233 * @parent: Pointer to the struct ab8500
234 * @gpadc: Pointer to the struct gpadc
Lee Jonesb0284de2012-11-30 10:09:42 +0000235 * @bm: Platform specific battery management information
Arun Murthy84edbee2012-02-29 21:54:26 +0530236 * @flags: Structure for information about events triggered
237 * @usb_state: Structure for usb stack information
Lee Jonesf7470b52013-02-14 12:37:29 +0000238 * @max_usb_in_curr: Max USB charger input current
Arun Murthy84edbee2012-02-29 21:54:26 +0530239 * @ac_chg: AC charger power supply
240 * @usb_chg: USB charger power supply
241 * @ac: Structure that holds the AC charger properties
242 * @usb: Structure that holds the USB charger properties
243 * @regu: Pointer to the struct regulator
244 * @charger_wq: Work queue for the IRQs and checking HW state
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100245 * @usb_ipt_crnt_lock: Lock to protect VBUS input current setting from mutuals
246 * @pm_lock: Lock to prevent system to suspend
Arun Murthy84edbee2012-02-29 21:54:26 +0530247 * @check_vbat_work Work for checking vbat threshold to adjust vbus current
248 * @check_hw_failure_work: Work for checking HW state
249 * @check_usbchgnotok_work: Work for checking USB charger not ok status
250 * @kick_wd_work: Work for kicking the charger watchdog in case
251 * of ABB rev 1.* due to the watchog logic bug
Lee Jonesb269fff2013-01-11 13:12:51 +0000252 * @ac_charger_attached_work: Work for checking if AC charger is still
253 * connected
254 * @usb_charger_attached_work: Work for checking if USB charger is still
255 * connected
Arun Murthy84edbee2012-02-29 21:54:26 +0530256 * @ac_work: Work for checking AC charger connection
257 * @detect_usb_type_work: Work for detecting the USB type connected
258 * @usb_link_status_work: Work for checking the new USB link status
259 * @usb_state_changed_work: Work for checking USB state
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100260 * @attach_work: Work for detecting USB type
261 * @vbus_drop_end_work: Work for detecting VBUS drop end
Arun Murthy84edbee2012-02-29 21:54:26 +0530262 * @check_main_thermal_prot_work:
263 * Work for checking Main thermal status
264 * @check_usb_thermal_prot_work:
265 * Work for checking USB thermal status
Lee Jonesb269fff2013-01-11 13:12:51 +0000266 * @charger_attached_mutex: For controlling the wakelock
Arun Murthy84edbee2012-02-29 21:54:26 +0530267 */
268struct ab8500_charger {
269 struct device *dev;
Arun Murthy84edbee2012-02-29 21:54:26 +0530270 bool vbus_detected;
271 bool vbus_detected_start;
272 bool ac_conn;
273 bool vddadc_en_ac;
274 bool vddadc_en_usb;
275 int vbat;
276 int old_vbat;
Marcus Cooper4b45f4a2013-01-11 13:13:04 +0000277 bool usb_device_is_unrecognised;
Arun Murthy84edbee2012-02-29 21:54:26 +0530278 bool autopower;
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -0800279 bool autopower_cfg;
Henrik Sölverff380902012-04-17 15:51:01 +0200280 int invalid_charger_detect_state;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100281 int is_aca_rid;
282 atomic_t current_stepping_sessions;
Arun Murthy84edbee2012-02-29 21:54:26 +0530283 struct ab8500 *parent;
284 struct ab8500_gpadc *gpadc;
Lee Jonesb0284de2012-11-30 10:09:42 +0000285 struct abx500_bm_data *bm;
Arun Murthy84edbee2012-02-29 21:54:26 +0530286 struct ab8500_charger_event_flags flags;
287 struct ab8500_charger_usb_state usb_state;
Lee Jonesf7470b52013-02-14 12:37:29 +0000288 struct ab8500_charger_max_usb_in_curr max_usb_in_curr;
Arun Murthy84edbee2012-02-29 21:54:26 +0530289 struct ux500_charger ac_chg;
290 struct ux500_charger usb_chg;
291 struct ab8500_charger_info ac;
292 struct ab8500_charger_info usb;
293 struct regulator *regu;
294 struct workqueue_struct *charger_wq;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100295 struct mutex usb_ipt_crnt_lock;
Arun Murthy84edbee2012-02-29 21:54:26 +0530296 struct delayed_work check_vbat_work;
297 struct delayed_work check_hw_failure_work;
298 struct delayed_work check_usbchgnotok_work;
299 struct delayed_work kick_wd_work;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100300 struct delayed_work usb_state_changed_work;
Marcus Cooper4b45f4a2013-01-11 13:13:04 +0000301 struct delayed_work attach_work;
Lee Jonesb269fff2013-01-11 13:12:51 +0000302 struct delayed_work ac_charger_attached_work;
303 struct delayed_work usb_charger_attached_work;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100304 struct delayed_work vbus_drop_end_work;
Arun Murthy84edbee2012-02-29 21:54:26 +0530305 struct work_struct ac_work;
306 struct work_struct detect_usb_type_work;
307 struct work_struct usb_link_status_work;
Arun Murthy84edbee2012-02-29 21:54:26 +0530308 struct work_struct check_main_thermal_prot_work;
309 struct work_struct check_usb_thermal_prot_work;
Anton Vorontsovefd71c82012-03-14 04:22:17 +0400310 struct usb_phy *usb_phy;
Arun Murthy84edbee2012-02-29 21:54:26 +0530311 struct notifier_block nb;
Lee Jonesb269fff2013-01-11 13:12:51 +0000312 struct mutex charger_attached_mutex;
Arun Murthy84edbee2012-02-29 21:54:26 +0530313};
314
315/* AC properties */
316static enum power_supply_property ab8500_charger_ac_props[] = {
317 POWER_SUPPLY_PROP_HEALTH,
318 POWER_SUPPLY_PROP_PRESENT,
319 POWER_SUPPLY_PROP_ONLINE,
320 POWER_SUPPLY_PROP_VOLTAGE_NOW,
321 POWER_SUPPLY_PROP_VOLTAGE_AVG,
322 POWER_SUPPLY_PROP_CURRENT_NOW,
323};
324
325/* USB properties */
326static enum power_supply_property ab8500_charger_usb_props[] = {
327 POWER_SUPPLY_PROP_HEALTH,
328 POWER_SUPPLY_PROP_CURRENT_AVG,
329 POWER_SUPPLY_PROP_PRESENT,
330 POWER_SUPPLY_PROP_ONLINE,
331 POWER_SUPPLY_PROP_VOLTAGE_NOW,
332 POWER_SUPPLY_PROP_VOLTAGE_AVG,
333 POWER_SUPPLY_PROP_CURRENT_NOW,
334};
335
Paer-Olof Haakanssondefbfa92013-01-11 13:12:49 +0000336/*
337 * Function for enabling and disabling sw fallback mode
338 * should always be disabled when no charger is connected.
Arun Murthy84edbee2012-02-29 21:54:26 +0530339 */
Paer-Olof Haakanssondefbfa92013-01-11 13:12:49 +0000340static void ab8500_enable_disable_sw_fallback(struct ab8500_charger *di,
341 bool fallback)
Arun Murthy84edbee2012-02-29 21:54:26 +0530342{
Nicolas Guionb0163222012-05-23 15:59:43 +0200343 u8 val;
Arun Murthy84edbee2012-02-29 21:54:26 +0530344 u8 reg;
Nicolas Guionb0163222012-05-23 15:59:43 +0200345 u8 bank;
346 u8 bit;
Arun Murthy84edbee2012-02-29 21:54:26 +0530347 int ret;
348
Paer-Olof Haakanssondefbfa92013-01-11 13:12:49 +0000349 dev_dbg(di->dev, "SW Fallback: %d\n", fallback);
Arun Murthy84edbee2012-02-29 21:54:26 +0530350
Nicolas Guionb0163222012-05-23 15:59:43 +0200351 if (is_ab8500(di->parent)) {
352 bank = 0x15;
353 reg = 0x0;
354 bit = 3;
355 } else {
356 bank = AB8500_SYS_CTRL1_BLOCK;
357 reg = AB8500_SW_CONTROL_FALLBACK;
358 bit = 0;
359 }
360
Paer-Olof Haakanssondefbfa92013-01-11 13:12:49 +0000361 /* read the register containing fallback bit */
Nicolas Guionb0163222012-05-23 15:59:43 +0200362 ret = abx500_get_register_interruptible(di->dev, bank, reg, &val);
363 if (ret < 0) {
364 dev_err(di->dev, "%d read failed\n", __LINE__);
Arun Murthy84edbee2012-02-29 21:54:26 +0530365 return;
366 }
367
Nicolas Guionb0163222012-05-23 15:59:43 +0200368 if (is_ab8500(di->parent)) {
369 /* enable the OPT emulation registers */
370 ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x2);
371 if (ret) {
372 dev_err(di->dev, "%d write failed\n", __LINE__);
373 goto disable_otp;
374 }
Arun Murthy84edbee2012-02-29 21:54:26 +0530375 }
376
Paer-Olof Haakanssondefbfa92013-01-11 13:12:49 +0000377 if (fallback)
Nicolas Guionb0163222012-05-23 15:59:43 +0200378 val |= (1 << bit);
Arun Murthy84edbee2012-02-29 21:54:26 +0530379 else
Nicolas Guionb0163222012-05-23 15:59:43 +0200380 val &= ~(1 << bit);
Arun Murthy84edbee2012-02-29 21:54:26 +0530381
Paer-Olof Haakanssondefbfa92013-01-11 13:12:49 +0000382 /* write back the changed fallback bit value to register */
Nicolas Guionb0163222012-05-23 15:59:43 +0200383 ret = abx500_set_register_interruptible(di->dev, bank, reg, val);
Arun Murthy84edbee2012-02-29 21:54:26 +0530384 if (ret) {
385 dev_err(di->dev, "%d write failed\n", __LINE__);
Arun Murthy84edbee2012-02-29 21:54:26 +0530386 }
387
Nicolas Guionb0163222012-05-23 15:59:43 +0200388disable_otp:
389 if (is_ab8500(di->parent)) {
390 /* disable the set OTP registers again */
391 ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x0);
392 if (ret) {
393 dev_err(di->dev, "%d write failed\n", __LINE__);
394 }
Arun Murthy84edbee2012-02-29 21:54:26 +0530395 }
396}
397
398/**
399 * ab8500_power_supply_changed - a wrapper with local extentions for
400 * power_supply_changed
401 * @di: pointer to the ab8500_charger structure
402 * @psy: pointer to power_supply_that have changed.
403 *
404 */
405static void ab8500_power_supply_changed(struct ab8500_charger *di,
406 struct power_supply *psy)
407{
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -0800408 if (di->autopower_cfg) {
Arun Murthy84edbee2012-02-29 21:54:26 +0530409 if (!di->usb.charger_connected &&
410 !di->ac.charger_connected &&
411 di->autopower) {
412 di->autopower = false;
Paer-Olof Haakanssondefbfa92013-01-11 13:12:49 +0000413 ab8500_enable_disable_sw_fallback(di, false);
Arun Murthy84edbee2012-02-29 21:54:26 +0530414 } else if (!di->autopower &&
415 (di->ac.charger_connected ||
416 di->usb.charger_connected)) {
417 di->autopower = true;
Paer-Olof Haakanssondefbfa92013-01-11 13:12:49 +0000418 ab8500_enable_disable_sw_fallback(di, true);
Arun Murthy84edbee2012-02-29 21:54:26 +0530419 }
420 }
421 power_supply_changed(psy);
422}
423
424static void ab8500_charger_set_usb_connected(struct ab8500_charger *di,
425 bool connected)
426{
427 if (connected != di->usb.charger_connected) {
428 dev_dbg(di->dev, "USB connected:%i\n", connected);
429 di->usb.charger_connected = connected;
Lee Jonesf7470b52013-02-14 12:37:29 +0000430
431 if (!connected)
432 di->flags.vbus_drop_end = false;
433
Arun Murthy84edbee2012-02-29 21:54:26 +0530434 sysfs_notify(&di->usb_chg.psy.dev->kobj, NULL, "present");
Lee Jonesb269fff2013-01-11 13:12:51 +0000435
436 if (connected) {
437 mutex_lock(&di->charger_attached_mutex);
438 mutex_unlock(&di->charger_attached_mutex);
439
440 queue_delayed_work(di->charger_wq,
441 &di->usb_charger_attached_work,
442 HZ);
443 } else {
444 cancel_delayed_work_sync(&di->usb_charger_attached_work);
445 mutex_lock(&di->charger_attached_mutex);
446 mutex_unlock(&di->charger_attached_mutex);
447 }
Arun Murthy84edbee2012-02-29 21:54:26 +0530448 }
449}
450
451/**
452 * ab8500_charger_get_ac_voltage() - get ac charger voltage
453 * @di: pointer to the ab8500_charger structure
454 *
455 * Returns ac charger voltage (on success)
456 */
457static int ab8500_charger_get_ac_voltage(struct ab8500_charger *di)
458{
459 int vch;
460
461 /* Only measure voltage if the charger is connected */
462 if (di->ac.charger_connected) {
463 vch = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_V);
464 if (vch < 0)
465 dev_err(di->dev, "%s gpadc conv failed,\n", __func__);
466 } else {
467 vch = 0;
468 }
469 return vch;
470}
471
472/**
473 * ab8500_charger_ac_cv() - check if the main charger is in CV mode
474 * @di: pointer to the ab8500_charger structure
475 *
476 * Returns ac charger CV mode (on success) else error code
477 */
478static int ab8500_charger_ac_cv(struct ab8500_charger *di)
479{
480 u8 val;
481 int ret = 0;
482
483 /* Only check CV mode if the charger is online */
484 if (di->ac.charger_online) {
485 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
486 AB8500_CH_STATUS1_REG, &val);
487 if (ret < 0) {
488 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
489 return 0;
490 }
491
492 if (val & MAIN_CH_CV_ON)
493 ret = 1;
494 else
495 ret = 0;
496 }
497
498 return ret;
499}
500
501/**
502 * ab8500_charger_get_vbus_voltage() - get vbus voltage
503 * @di: pointer to the ab8500_charger structure
504 *
505 * This function returns the vbus voltage.
506 * Returns vbus voltage (on success)
507 */
508static int ab8500_charger_get_vbus_voltage(struct ab8500_charger *di)
509{
510 int vch;
511
512 /* Only measure voltage if the charger is connected */
513 if (di->usb.charger_connected) {
514 vch = ab8500_gpadc_convert(di->gpadc, VBUS_V);
515 if (vch < 0)
516 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
517 } else {
518 vch = 0;
519 }
520 return vch;
521}
522
523/**
524 * ab8500_charger_get_usb_current() - get usb charger current
525 * @di: pointer to the ab8500_charger structure
526 *
527 * This function returns the usb charger current.
528 * Returns usb current (on success) and error code on failure
529 */
530static int ab8500_charger_get_usb_current(struct ab8500_charger *di)
531{
532 int ich;
533
534 /* Only measure current if the charger is online */
535 if (di->usb.charger_online) {
536 ich = ab8500_gpadc_convert(di->gpadc, USB_CHARGER_C);
537 if (ich < 0)
538 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
539 } else {
540 ich = 0;
541 }
542 return ich;
543}
544
545/**
546 * ab8500_charger_get_ac_current() - get ac charger current
547 * @di: pointer to the ab8500_charger structure
548 *
549 * This function returns the ac charger current.
550 * Returns ac current (on success) and error code on failure.
551 */
552static int ab8500_charger_get_ac_current(struct ab8500_charger *di)
553{
554 int ich;
555
556 /* Only measure current if the charger is online */
557 if (di->ac.charger_online) {
558 ich = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_C);
559 if (ich < 0)
560 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
561 } else {
562 ich = 0;
563 }
564 return ich;
565}
566
567/**
568 * ab8500_charger_usb_cv() - check if the usb charger is in CV mode
569 * @di: pointer to the ab8500_charger structure
570 *
571 * Returns ac charger CV mode (on success) else error code
572 */
573static int ab8500_charger_usb_cv(struct ab8500_charger *di)
574{
575 int ret;
576 u8 val;
577
578 /* Only check CV mode if the charger is online */
579 if (di->usb.charger_online) {
580 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
581 AB8500_CH_USBCH_STAT1_REG, &val);
582 if (ret < 0) {
583 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
584 return 0;
585 }
586
587 if (val & USB_CH_CV_ON)
588 ret = 1;
589 else
590 ret = 0;
591 } else {
592 ret = 0;
593 }
594
595 return ret;
596}
597
598/**
599 * ab8500_charger_detect_chargers() - Detect the connected chargers
600 * @di: pointer to the ab8500_charger structure
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100601 * @probe: if probe, don't delay and wait for HW
Arun Murthy84edbee2012-02-29 21:54:26 +0530602 *
603 * Returns the type of charger connected.
604 * For USB it will not mean we can actually charge from it
605 * but that there is a USB cable connected that we have to
606 * identify. This is used during startup when we don't get
607 * interrupts of the charger detection
608 *
609 * Returns an integer value, that means,
610 * NO_PW_CONN no power supply is connected
611 * AC_PW_CONN if the AC power supply is connected
612 * USB_PW_CONN if the USB power supply is connected
613 * AC_PW_CONN + USB_PW_CONN if USB and AC power supplies are both connected
614 */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100615static int ab8500_charger_detect_chargers(struct ab8500_charger *di, bool probe)
Arun Murthy84edbee2012-02-29 21:54:26 +0530616{
617 int result = NO_PW_CONN;
618 int ret;
619 u8 val;
620
621 /* Check for AC charger */
622 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
623 AB8500_CH_STATUS1_REG, &val);
624 if (ret < 0) {
625 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
626 return ret;
627 }
628
629 if (val & MAIN_CH_DET)
630 result = AC_PW_CONN;
631
632 /* Check for USB charger */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100633
634 if (!probe) {
635 /*
636 * AB8500 says VBUS_DET_DBNC1 & VBUS_DET_DBNC100
637 * when disconnecting ACA even though no
638 * charger was connected. Try waiting a little
639 * longer than the 100 ms of VBUS_DET_DBNC100...
640 */
641 msleep(110);
642 }
Arun Murthy84edbee2012-02-29 21:54:26 +0530643 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
644 AB8500_CH_USBCH_STAT1_REG, &val);
645 if (ret < 0) {
646 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
647 return ret;
648 }
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100649 dev_dbg(di->dev,
650 "%s AB8500_CH_USBCH_STAT1_REG %x\n", __func__,
651 val);
Arun Murthy84edbee2012-02-29 21:54:26 +0530652 if ((val & VBUS_DET_DBNC1) && (val & VBUS_DET_DBNC100))
653 result |= USB_PW_CONN;
654
655 return result;
656}
657
658/**
659 * ab8500_charger_max_usb_curr() - get the max curr for the USB type
660 * @di: pointer to the ab8500_charger structure
661 * @link_status: the identified USB type
662 *
663 * Get the maximum current that is allowed to be drawn from the host
664 * based on the USB type.
665 * Returns error code in case of failure else 0 on success
666 */
667static int ab8500_charger_max_usb_curr(struct ab8500_charger *di,
Hakan Berg74a8e342013-01-11 13:12:58 +0000668 enum ab8500_charger_link_status link_status)
Arun Murthy84edbee2012-02-29 21:54:26 +0530669{
670 int ret = 0;
671
Marcus Cooper4b45f4a2013-01-11 13:13:04 +0000672 di->usb_device_is_unrecognised = false;
673
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100674 /*
675 * Platform only supports USB 2.0.
676 * This means that charging current from USB source
677 * is maximum 500 mA. Every occurence of USB_STAT_*_HOST_*
678 * should set USB_CH_IP_CUR_LVL_0P5.
679 */
680
Arun Murthy84edbee2012-02-29 21:54:26 +0530681 switch (link_status) {
682 case USB_STAT_STD_HOST_NC:
683 case USB_STAT_STD_HOST_C_NS:
684 case USB_STAT_STD_HOST_C_S:
685 dev_dbg(di->dev, "USB Type - Standard host is "
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100686 "detected through USB driver\n");
Lee Jonesf7470b52013-02-14 12:37:29 +0000687 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100688 di->is_aca_rid = 0;
Arun Murthy84edbee2012-02-29 21:54:26 +0530689 break;
690 case USB_STAT_HOST_CHG_HS_CHIRP:
Lee Jonesf7470b52013-02-14 12:37:29 +0000691 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100692 di->is_aca_rid = 0;
Arun Murthy84edbee2012-02-29 21:54:26 +0530693 break;
694 case USB_STAT_HOST_CHG_HS:
Lee Jonesf7470b52013-02-14 12:37:29 +0000695 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100696 di->is_aca_rid = 0;
697 break;
Arun Murthy84edbee2012-02-29 21:54:26 +0530698 case USB_STAT_ACA_RID_C_HS:
Lee Jonesf7470b52013-02-14 12:37:29 +0000699 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P9;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100700 di->is_aca_rid = 0;
Arun Murthy84edbee2012-02-29 21:54:26 +0530701 break;
702 case USB_STAT_ACA_RID_A:
703 /*
704 * Dedicated charger level minus maximum current accessory
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100705 * can consume (900mA). Closest level is 500mA
Arun Murthy84edbee2012-02-29 21:54:26 +0530706 */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100707 dev_dbg(di->dev, "USB_STAT_ACA_RID_A detected\n");
Lee Jonesf7470b52013-02-14 12:37:29 +0000708 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100709 di->is_aca_rid = 1;
Arun Murthy84edbee2012-02-29 21:54:26 +0530710 break;
711 case USB_STAT_ACA_RID_B:
712 /*
713 * Dedicated charger level minus 120mA (20mA for ACA and
714 * 100mA for potential accessory). Closest level is 1300mA
715 */
Lee Jonesf7470b52013-02-14 12:37:29 +0000716 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_1P3;
Hakan Berg74a8e342013-01-11 13:12:58 +0000717 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
Lee Jonesf7470b52013-02-14 12:37:29 +0000718 di->max_usb_in_curr.usb_type_max);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100719 di->is_aca_rid = 1;
Arun Murthy84edbee2012-02-29 21:54:26 +0530720 break;
Arun Murthy84edbee2012-02-29 21:54:26 +0530721 case USB_STAT_HOST_CHG_NM:
Lee Jonesf7470b52013-02-14 12:37:29 +0000722 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100723 di->is_aca_rid = 0;
724 break;
Hakan Berg74a8e342013-01-11 13:12:58 +0000725 case USB_STAT_DEDICATED_CHG:
Lee Jonesf7470b52013-02-14 12:37:29 +0000726 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_1P5;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100727 di->is_aca_rid = 0;
728 break;
729 case USB_STAT_ACA_RID_C_HS_CHIRP:
730 case USB_STAT_ACA_RID_C_NM:
Lee Jonesf7470b52013-02-14 12:37:29 +0000731 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_1P5;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100732 di->is_aca_rid = 1;
Arun Murthy84edbee2012-02-29 21:54:26 +0530733 break;
Marcus Cooper4b45f4a2013-01-11 13:13:04 +0000734 case USB_STAT_NOT_CONFIGURED:
735 if (di->vbus_detected) {
736 di->usb_device_is_unrecognised = true;
737 dev_dbg(di->dev, "USB Type - Legacy charger.\n");
Lee Jonesf7470b52013-02-14 12:37:29 +0000738 di->max_usb_in_curr.usb_type_max =
739 USB_CH_IP_CUR_LVL_1P5;
Marcus Cooper4b45f4a2013-01-11 13:13:04 +0000740 break;
741 }
Arun Murthy84edbee2012-02-29 21:54:26 +0530742 case USB_STAT_HM_IDGND:
Arun Murthy84edbee2012-02-29 21:54:26 +0530743 dev_err(di->dev, "USB Type - Charging not allowed\n");
Lee Jonesf7470b52013-02-14 12:37:29 +0000744 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P05;
Arun Murthy84edbee2012-02-29 21:54:26 +0530745 ret = -ENXIO;
746 break;
Hakan Berg74a8e342013-01-11 13:12:58 +0000747 case USB_STAT_RESERVED:
748 if (is_ab8500(di->parent)) {
749 di->flags.vbus_collapse = true;
750 dev_err(di->dev, "USB Type - USB_STAT_RESERVED "
751 "VBUS has collapsed\n");
752 ret = -ENXIO;
753 break;
754 }
755 if (is_ab9540(di->parent) || is_ab8505(di->parent)) {
756 dev_dbg(di->dev, "USB Type - Charging not allowed\n");
Lee Jonesf7470b52013-02-14 12:37:29 +0000757 di->max_usb_in_curr.usb_type_max =
758 USB_CH_IP_CUR_LVL_0P05;
Hakan Berg74a8e342013-01-11 13:12:58 +0000759 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
Lee Jonesf7470b52013-02-14 12:37:29 +0000760 link_status,
761 di->max_usb_in_curr.usb_type_max);
Hakan Berg74a8e342013-01-11 13:12:58 +0000762 ret = -ENXIO;
763 break;
764 }
765 break;
766 case USB_STAT_CARKIT_1:
767 case USB_STAT_CARKIT_2:
768 case USB_STAT_ACA_DOCK_CHARGER:
769 case USB_STAT_CHARGER_LINE_1:
Lee Jonesf7470b52013-02-14 12:37:29 +0000770 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
Hakan Berg74a8e342013-01-11 13:12:58 +0000771 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
Lee Jonesf7470b52013-02-14 12:37:29 +0000772 di->max_usb_in_curr.usb_type_max);
Henrik Sölverff380902012-04-17 15:51:01 +0200773 case USB_STAT_NOT_VALID_LINK:
774 dev_err(di->dev, "USB Type invalid - try charging anyway\n");
Lee Jonesf7470b52013-02-14 12:37:29 +0000775 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
Hakan Berg74a8e342013-01-11 13:12:58 +0000776 break;
777
Arun Murthy84edbee2012-02-29 21:54:26 +0530778 default:
779 dev_err(di->dev, "USB Type - Unknown\n");
Lee Jonesf7470b52013-02-14 12:37:29 +0000780 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P05;
Arun Murthy84edbee2012-02-29 21:54:26 +0530781 ret = -ENXIO;
782 break;
783 };
784
Lee Jonesf7470b52013-02-14 12:37:29 +0000785 di->max_usb_in_curr.set_max = di->max_usb_in_curr.usb_type_max;
Arun Murthy84edbee2012-02-29 21:54:26 +0530786 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
Lee Jonesf7470b52013-02-14 12:37:29 +0000787 link_status, di->max_usb_in_curr.set_max);
Arun Murthy84edbee2012-02-29 21:54:26 +0530788
789 return ret;
790}
791
792/**
793 * ab8500_charger_read_usb_type() - read the type of usb connected
794 * @di: pointer to the ab8500_charger structure
795 *
796 * Detect the type of the plugged USB
797 * Returns error code in case of failure else 0 on success
798 */
799static int ab8500_charger_read_usb_type(struct ab8500_charger *di)
800{
801 int ret;
802 u8 val;
803
804 ret = abx500_get_register_interruptible(di->dev,
805 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG, &val);
806 if (ret < 0) {
807 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
808 return ret;
809 }
Hakan Berg74a8e342013-01-11 13:12:58 +0000810 if (is_ab8500(di->parent)) {
811 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
812 AB8500_USB_LINE_STAT_REG, &val);
Hakan Bergd4337662012-07-26 11:10:35 +0200813 } else if (is_ab9540(di->parent) || is_ab8505(di->parent)) {
Hakan Berg74a8e342013-01-11 13:12:58 +0000814 ret = abx500_get_register_interruptible(di->dev,
815 AB8500_USB, AB8500_USB_LINK1_STAT_REG, &val);
Hakan Bergd4337662012-07-26 11:10:35 +0200816 } else {
817 dev_err(di->dev, "%s unsupported analog baseband\n", __func__);
818 return -ENXIO;
Hakan Berg74a8e342013-01-11 13:12:58 +0000819 }
Arun Murthy84edbee2012-02-29 21:54:26 +0530820 if (ret < 0) {
821 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
822 return ret;
823 }
824
825 /* get the USB type */
Hakan Bergd4337662012-07-26 11:10:35 +0200826 if (is_ab8500(di->parent)) {
827 val = (val & AB8500_USB_LINK_STATUS) >> 3;
828 } else if (is_ab9540(di->parent) || is_ab8505(di->parent)) {
829 val = (val & AB8505_USB_LINK_STATUS) >> 3;
830 } else {
831 dev_err(di->dev, "%s unsupported analog baseband\n", __func__);
832 return -ENXIO;
833 }
Arun Murthy84edbee2012-02-29 21:54:26 +0530834 ret = ab8500_charger_max_usb_curr(di,
835 (enum ab8500_charger_link_status) val);
836
837 return ret;
838}
839
840/**
841 * ab8500_charger_detect_usb_type() - get the type of usb connected
842 * @di: pointer to the ab8500_charger structure
843 *
844 * Detect the type of the plugged USB
845 * Returns error code in case of failure else 0 on success
846 */
847static int ab8500_charger_detect_usb_type(struct ab8500_charger *di)
848{
849 int i, ret;
850 u8 val;
851
852 /*
853 * On getting the VBUS rising edge detect interrupt there
854 * is a 250ms delay after which the register UsbLineStatus
855 * is filled with valid data.
856 */
857 for (i = 0; i < 10; i++) {
858 msleep(250);
859 ret = abx500_get_register_interruptible(di->dev,
860 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG,
861 &val);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100862 dev_dbg(di->dev, "%s AB8500_IT_SOURCE21_REG %x\n",
863 __func__, val);
Arun Murthy84edbee2012-02-29 21:54:26 +0530864 if (ret < 0) {
865 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
866 return ret;
867 }
Hakan Berg74a8e342013-01-11 13:12:58 +0000868
Hakan Bergd4337662012-07-26 11:10:35 +0200869 if (is_ab8500(di->parent)) {
Hakan Berg74a8e342013-01-11 13:12:58 +0000870 ret = abx500_get_register_interruptible(di->dev,
871 AB8500_USB, AB8500_USB_LINE_STAT_REG, &val);
Hakan Bergd4337662012-07-26 11:10:35 +0200872 } else if (is_ab9540(di->parent) || is_ab8505(di->parent)) {
Hakan Berg74a8e342013-01-11 13:12:58 +0000873 ret = abx500_get_register_interruptible(di->dev,
874 AB8500_USB, AB8500_USB_LINK1_STAT_REG, &val);
Hakan Bergd4337662012-07-26 11:10:35 +0200875 } else {
876 dev_err(di->dev, "%s unsupported analog baseband\n", __func__);
877 return -ENXIO;
878 }
Arun Murthy84edbee2012-02-29 21:54:26 +0530879 if (ret < 0) {
880 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
881 return ret;
882 }
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100883 dev_dbg(di->dev, "%s AB8500_USB_LINE_STAT_REG %x\n", __func__,
884 val);
Arun Murthy84edbee2012-02-29 21:54:26 +0530885 /*
886 * Until the IT source register is read the UsbLineStatus
887 * register is not updated, hence doing the same
888 * Revisit this:
889 */
890
891 /* get the USB type */
Hakan Bergd4337662012-07-26 11:10:35 +0200892 if (is_ab8500(di->parent)) {
893 val = (val & AB8500_USB_LINK_STATUS) >> 3;
894 } else if (is_ab9540(di->parent) || is_ab8505(di->parent)) {
895 val = (val & AB8505_USB_LINK_STATUS) >> 3;
896 } else {
897 dev_err(di->dev, "%s unsupported analog baseband\n", __func__);
898 return -ENXIO;
899 }
Arun Murthy84edbee2012-02-29 21:54:26 +0530900 if (val)
901 break;
902 }
903 ret = ab8500_charger_max_usb_curr(di,
904 (enum ab8500_charger_link_status) val);
905
906 return ret;
907}
908
909/*
910 * This array maps the raw hex value to charger voltage used by the AB8500
911 * Values taken from the UM0836
912 */
913static int ab8500_charger_voltage_map[] = {
914 3500 ,
915 3525 ,
916 3550 ,
917 3575 ,
918 3600 ,
919 3625 ,
920 3650 ,
921 3675 ,
922 3700 ,
923 3725 ,
924 3750 ,
925 3775 ,
926 3800 ,
927 3825 ,
928 3850 ,
929 3875 ,
930 3900 ,
931 3925 ,
932 3950 ,
933 3975 ,
934 4000 ,
935 4025 ,
936 4050 ,
937 4060 ,
938 4070 ,
939 4080 ,
940 4090 ,
941 4100 ,
942 4110 ,
943 4120 ,
944 4130 ,
945 4140 ,
946 4150 ,
947 4160 ,
948 4170 ,
949 4180 ,
950 4190 ,
951 4200 ,
952 4210 ,
953 4220 ,
954 4230 ,
955 4240 ,
956 4250 ,
957 4260 ,
958 4270 ,
959 4280 ,
960 4290 ,
961 4300 ,
962 4310 ,
963 4320 ,
964 4330 ,
965 4340 ,
966 4350 ,
967 4360 ,
968 4370 ,
969 4380 ,
970 4390 ,
971 4400 ,
972 4410 ,
973 4420 ,
974 4430 ,
975 4440 ,
976 4450 ,
977 4460 ,
978 4470 ,
979 4480 ,
980 4490 ,
981 4500 ,
982 4510 ,
983 4520 ,
984 4530 ,
985 4540 ,
986 4550 ,
987 4560 ,
988 4570 ,
989 4580 ,
990 4590 ,
991 4600 ,
992};
993
994/*
995 * This array maps the raw hex value to charger current used by the AB8500
996 * Values taken from the UM0836
997 */
998static int ab8500_charger_current_map[] = {
999 100 ,
1000 200 ,
1001 300 ,
1002 400 ,
1003 500 ,
1004 600 ,
1005 700 ,
1006 800 ,
1007 900 ,
1008 1000 ,
1009 1100 ,
1010 1200 ,
1011 1300 ,
1012 1400 ,
1013 1500 ,
1014};
1015
1016/*
1017 * This array maps the raw hex value to VBUS input current used by the AB8500
1018 * Values taken from the UM0836
1019 */
1020static int ab8500_charger_vbus_in_curr_map[] = {
1021 USB_CH_IP_CUR_LVL_0P05,
1022 USB_CH_IP_CUR_LVL_0P09,
1023 USB_CH_IP_CUR_LVL_0P19,
1024 USB_CH_IP_CUR_LVL_0P29,
1025 USB_CH_IP_CUR_LVL_0P38,
1026 USB_CH_IP_CUR_LVL_0P45,
1027 USB_CH_IP_CUR_LVL_0P5,
1028 USB_CH_IP_CUR_LVL_0P6,
1029 USB_CH_IP_CUR_LVL_0P7,
1030 USB_CH_IP_CUR_LVL_0P8,
1031 USB_CH_IP_CUR_LVL_0P9,
1032 USB_CH_IP_CUR_LVL_1P0,
1033 USB_CH_IP_CUR_LVL_1P1,
1034 USB_CH_IP_CUR_LVL_1P3,
1035 USB_CH_IP_CUR_LVL_1P4,
1036 USB_CH_IP_CUR_LVL_1P5,
1037};
1038
1039static int ab8500_voltage_to_regval(int voltage)
1040{
1041 int i;
1042
1043 /* Special case for voltage below 3.5V */
1044 if (voltage < ab8500_charger_voltage_map[0])
1045 return LOW_VOLT_REG;
1046
1047 for (i = 1; i < ARRAY_SIZE(ab8500_charger_voltage_map); i++) {
1048 if (voltage < ab8500_charger_voltage_map[i])
1049 return i - 1;
1050 }
1051
1052 /* If not last element, return error */
1053 i = ARRAY_SIZE(ab8500_charger_voltage_map) - 1;
1054 if (voltage == ab8500_charger_voltage_map[i])
1055 return i;
1056 else
1057 return -1;
1058}
1059
1060static int ab8500_current_to_regval(int curr)
1061{
1062 int i;
1063
1064 if (curr < ab8500_charger_current_map[0])
1065 return 0;
1066
1067 for (i = 0; i < ARRAY_SIZE(ab8500_charger_current_map); i++) {
1068 if (curr < ab8500_charger_current_map[i])
1069 return i - 1;
1070 }
1071
1072 /* If not last element, return error */
1073 i = ARRAY_SIZE(ab8500_charger_current_map) - 1;
1074 if (curr == ab8500_charger_current_map[i])
1075 return i;
1076 else
1077 return -1;
1078}
1079
1080static int ab8500_vbus_in_curr_to_regval(int curr)
1081{
1082 int i;
1083
1084 if (curr < ab8500_charger_vbus_in_curr_map[0])
1085 return 0;
1086
1087 for (i = 0; i < ARRAY_SIZE(ab8500_charger_vbus_in_curr_map); i++) {
1088 if (curr < ab8500_charger_vbus_in_curr_map[i])
1089 return i - 1;
1090 }
1091
1092 /* If not last element, return error */
1093 i = ARRAY_SIZE(ab8500_charger_vbus_in_curr_map) - 1;
1094 if (curr == ab8500_charger_vbus_in_curr_map[i])
1095 return i;
1096 else
1097 return -1;
1098}
1099
1100/**
1101 * ab8500_charger_get_usb_cur() - get usb current
1102 * @di: pointer to the ab8500_charger structre
1103 *
1104 * The usb stack provides the maximum current that can be drawn from
1105 * the standard usb host. This will be in mA.
1106 * This function converts current in mA to a value that can be written
1107 * to the register. Returns -1 if charging is not allowed
1108 */
1109static int ab8500_charger_get_usb_cur(struct ab8500_charger *di)
1110{
Lee Jonesf7470b52013-02-14 12:37:29 +00001111 int ret = 0;
Arun Murthy84edbee2012-02-29 21:54:26 +05301112 switch (di->usb_state.usb_current) {
1113 case 100:
Lee Jonesf7470b52013-02-14 12:37:29 +00001114 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P09;
Arun Murthy84edbee2012-02-29 21:54:26 +05301115 break;
1116 case 200:
Lee Jonesf7470b52013-02-14 12:37:29 +00001117 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P19;
Arun Murthy84edbee2012-02-29 21:54:26 +05301118 break;
1119 case 300:
Lee Jonesf7470b52013-02-14 12:37:29 +00001120 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P29;
Arun Murthy84edbee2012-02-29 21:54:26 +05301121 break;
1122 case 400:
Lee Jonesf7470b52013-02-14 12:37:29 +00001123 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P38;
Arun Murthy84edbee2012-02-29 21:54:26 +05301124 break;
1125 case 500:
Lee Jonesf7470b52013-02-14 12:37:29 +00001126 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
Arun Murthy84edbee2012-02-29 21:54:26 +05301127 break;
1128 default:
Lee Jonesf7470b52013-02-14 12:37:29 +00001129 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P05;
1130 ret = -EPERM;
Arun Murthy84edbee2012-02-29 21:54:26 +05301131 break;
1132 };
Lee Jonesf7470b52013-02-14 12:37:29 +00001133 di->max_usb_in_curr.set_max = di->max_usb_in_curr.usb_type_max;
1134 return ret;
1135}
1136
1137/**
1138 * ab8500_charger_check_continue_stepping() - Check to allow stepping
1139 * @di: pointer to the ab8500_charger structure
1140 * @reg: select what charger register to check
1141 *
1142 * Check if current stepping should be allowed to continue.
1143 * Checks if charger source has not collapsed. If it has, further stepping
1144 * is not allowed.
1145 */
1146static bool ab8500_charger_check_continue_stepping(struct ab8500_charger *di,
1147 int reg)
1148{
1149 if (reg == AB8500_USBCH_IPT_CRNTLVL_REG)
1150 return !di->flags.vbus_drop_end;
1151 else
1152 return true;
Arun Murthy84edbee2012-02-29 21:54:26 +05301153}
1154
1155/**
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001156 * ab8500_charger_set_current() - set charger current
1157 * @di: pointer to the ab8500_charger structure
1158 * @ich: charger current, in mA
1159 * @reg: select what charger register to set
1160 *
1161 * Set charger current.
1162 * There is no state machine in the AB to step up/down the charger
1163 * current to avoid dips and spikes on MAIN, VBUS and VBAT when
1164 * charging is started. Instead we need to implement
1165 * this charger current step-up/down here.
1166 * Returns error code in case of failure else 0(on success)
1167 */
1168static int ab8500_charger_set_current(struct ab8500_charger *di,
1169 int ich, int reg)
1170{
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001171 int ret = 0;
1172 int auto_curr_index, curr_index, prev_curr_index, shift_value, i;
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001173 u8 reg_value;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001174 u32 step_udelay;
1175 bool no_stepping = false;
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001176
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001177 atomic_inc(&di->current_stepping_sessions);
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001178
1179 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1180 reg, &reg_value);
1181 if (ret < 0) {
1182 dev_err(di->dev, "%s read failed\n", __func__);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001183 goto exit_set_current;
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001184 }
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001185
1186 switch (reg) {
1187 case AB8500_MCH_IPT_CURLVL_REG:
1188 shift_value = MAIN_CH_INPUT_CURR_SHIFT;
1189 prev_curr_index = (reg_value >> shift_value);
1190 curr_index = ab8500_current_to_regval(ich);
1191 step_udelay = STEP_UDELAY;
1192 if (!di->ac.charger_connected)
1193 no_stepping = true;
1194 break;
1195 case AB8500_USBCH_IPT_CRNTLVL_REG:
1196 shift_value = VBUS_IN_CURR_LIM_SHIFT;
1197 prev_curr_index = (reg_value >> shift_value);
1198 curr_index = ab8500_vbus_in_curr_to_regval(ich);
1199 step_udelay = STEP_UDELAY * 100;
1200
1201 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1202 AB8500_CH_USBCH_STAT2_REG, &reg_value);
1203 if (ret < 0) {
1204 dev_err(di->dev, "%s read failed\n", __func__);
1205 goto exit_set_current;
1206 }
1207 auto_curr_index =
1208 reg_value >> AUTO_VBUS_IN_CURR_LIM_SHIFT;
1209
1210 dev_dbg(di->dev, "%s Auto VBUS curr is %d mA\n",
1211 __func__,
1212 ab8500_charger_vbus_in_curr_map[auto_curr_index]);
1213
1214 prev_curr_index = min(prev_curr_index, auto_curr_index);
1215
1216 if (!di->usb.charger_connected)
1217 no_stepping = true;
1218 break;
1219 case AB8500_CH_OPT_CRNTLVL_REG:
1220 shift_value = 0;
1221 prev_curr_index = (reg_value >> shift_value);
1222 curr_index = ab8500_current_to_regval(ich);
1223 step_udelay = STEP_UDELAY;
1224 if (curr_index && (curr_index - prev_curr_index) > 1)
1225 step_udelay *= 100;
1226
1227 if (!di->usb.charger_connected && !di->ac.charger_connected)
1228 no_stepping = true;
1229
1230 break;
1231 default:
1232 dev_err(di->dev, "%s current register not valid\n", __func__);
1233 ret = -ENXIO;
1234 goto exit_set_current;
1235 }
1236
1237 if (curr_index < 0) {
1238 dev_err(di->dev, "requested current limit out-of-range\n");
1239 ret = -ENXIO;
1240 goto exit_set_current;
1241 }
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001242
1243 /* only update current if it's been changed */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001244 if (prev_curr_index == curr_index) {
1245 dev_dbg(di->dev, "%s current not changed for reg: 0x%02x\n",
1246 __func__, reg);
1247 ret = 0;
1248 goto exit_set_current;
1249 }
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001250
1251 dev_dbg(di->dev, "%s set charger current: %d mA for reg: 0x%02x\n",
1252 __func__, ich, reg);
1253
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001254 if (no_stepping) {
1255 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1256 reg, (u8)curr_index << shift_value);
1257 if (ret)
1258 dev_err(di->dev, "%s write failed\n", __func__);
1259 } else if (prev_curr_index > curr_index) {
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001260 for (i = prev_curr_index - 1; i >= curr_index; i--) {
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001261 dev_dbg(di->dev, "curr change_1 to: %x for 0x%02x\n",
1262 (u8) i << shift_value, reg);
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001263 ret = abx500_set_register_interruptible(di->dev,
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001264 AB8500_CHARGER, reg, (u8)i << shift_value);
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001265 if (ret) {
1266 dev_err(di->dev, "%s write failed\n", __func__);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001267 goto exit_set_current;
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001268 }
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001269 if (i != curr_index)
1270 usleep_range(step_udelay, step_udelay * 2);
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001271 }
1272 } else {
Lee Jonesf7470b52013-02-14 12:37:29 +00001273 bool allow = true;
1274 for (i = prev_curr_index + 1; i <= curr_index && allow; i++) {
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001275 dev_dbg(di->dev, "curr change_2 to: %x for 0x%02x\n",
1276 (u8)i << shift_value, reg);
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001277 ret = abx500_set_register_interruptible(di->dev,
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001278 AB8500_CHARGER, reg, (u8)i << shift_value);
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001279 if (ret) {
1280 dev_err(di->dev, "%s write failed\n", __func__);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001281 goto exit_set_current;
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001282 }
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001283 if (i != curr_index)
1284 usleep_range(step_udelay, step_udelay * 2);
Lee Jonesf7470b52013-02-14 12:37:29 +00001285
1286 allow = ab8500_charger_check_continue_stepping(di, reg);
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001287 }
1288 }
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001289
1290exit_set_current:
1291 atomic_dec(&di->current_stepping_sessions);
1292
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001293 return ret;
1294}
1295
1296/**
Arun Murthy84edbee2012-02-29 21:54:26 +05301297 * ab8500_charger_set_vbus_in_curr() - set VBUS input current limit
1298 * @di: pointer to the ab8500_charger structure
1299 * @ich_in: charger input current limit
1300 *
1301 * Sets the current that can be drawn from the USB host
1302 * Returns error code in case of failure else 0(on success)
1303 */
1304static int ab8500_charger_set_vbus_in_curr(struct ab8500_charger *di,
1305 int ich_in)
1306{
Arun Murthy84edbee2012-02-29 21:54:26 +05301307 int min_value;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001308 int ret;
Arun Murthy84edbee2012-02-29 21:54:26 +05301309
1310 /* We should always use to lowest current limit */
Lee Jonesb0284de2012-11-30 10:09:42 +00001311 min_value = min(di->bm->chg_params->usb_curr_max, ich_in);
Lee Jonesf7470b52013-02-14 12:37:29 +00001312 if (di->max_usb_in_curr.set_max > 0)
1313 min_value = min(di->max_usb_in_curr.set_max, min_value);
1314
1315 if (di->usb_state.usb_current >= 0)
1316 min_value = min(di->usb_state.usb_current, min_value);
Arun Murthy84edbee2012-02-29 21:54:26 +05301317
1318 switch (min_value) {
1319 case 100:
1320 if (di->vbat < VBAT_TRESH_IP_CUR_RED)
1321 min_value = USB_CH_IP_CUR_LVL_0P05;
1322 break;
1323 case 500:
1324 if (di->vbat < VBAT_TRESH_IP_CUR_RED)
1325 min_value = USB_CH_IP_CUR_LVL_0P45;
1326 break;
1327 default:
1328 break;
1329 }
1330
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001331 dev_info(di->dev, "VBUS input current limit set to %d mA\n", min_value);
1332
1333 mutex_lock(&di->usb_ipt_crnt_lock);
1334 ret = ab8500_charger_set_current(di, min_value,
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001335 AB8500_USBCH_IPT_CRNTLVL_REG);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001336 mutex_unlock(&di->usb_ipt_crnt_lock);
1337
1338 return ret;
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001339}
Arun Murthy84edbee2012-02-29 21:54:26 +05301340
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001341/**
1342 * ab8500_charger_set_main_in_curr() - set main charger input current
1343 * @di: pointer to the ab8500_charger structure
1344 * @ich_in: input charger current, in mA
1345 *
1346 * Set main charger input current.
1347 * Returns error code in case of failure else 0(on success)
1348 */
1349static int ab8500_charger_set_main_in_curr(struct ab8500_charger *di,
1350 int ich_in)
1351{
1352 return ab8500_charger_set_current(di, ich_in,
1353 AB8500_MCH_IPT_CURLVL_REG);
1354}
Arun Murthy84edbee2012-02-29 21:54:26 +05301355
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001356/**
1357 * ab8500_charger_set_output_curr() - set charger output current
1358 * @di: pointer to the ab8500_charger structure
1359 * @ich_out: output charger current, in mA
1360 *
1361 * Set charger output current.
1362 * Returns error code in case of failure else 0(on success)
1363 */
1364static int ab8500_charger_set_output_curr(struct ab8500_charger *di,
1365 int ich_out)
1366{
1367 return ab8500_charger_set_current(di, ich_out,
1368 AB8500_CH_OPT_CRNTLVL_REG);
Arun Murthy84edbee2012-02-29 21:54:26 +05301369}
1370
1371/**
1372 * ab8500_charger_led_en() - turn on/off chargign led
1373 * @di: pointer to the ab8500_charger structure
1374 * @on: flag to turn on/off the chargign led
1375 *
1376 * Power ON/OFF charging LED indication
1377 * Returns error code in case of failure else 0(on success)
1378 */
1379static int ab8500_charger_led_en(struct ab8500_charger *di, int on)
1380{
1381 int ret;
1382
1383 if (on) {
1384 /* Power ON charging LED indicator, set LED current to 5mA */
1385 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1386 AB8500_LED_INDICATOR_PWM_CTRL,
1387 (LED_IND_CUR_5MA | LED_INDICATOR_PWM_ENA));
1388 if (ret) {
1389 dev_err(di->dev, "Power ON LED failed\n");
1390 return ret;
1391 }
1392 /* LED indicator PWM duty cycle 252/256 */
1393 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1394 AB8500_LED_INDICATOR_PWM_DUTY,
1395 LED_INDICATOR_PWM_DUTY_252_256);
1396 if (ret) {
1397 dev_err(di->dev, "Set LED PWM duty cycle failed\n");
1398 return ret;
1399 }
1400 } else {
1401 /* Power off charging LED indicator */
1402 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1403 AB8500_LED_INDICATOR_PWM_CTRL,
1404 LED_INDICATOR_PWM_DIS);
1405 if (ret) {
1406 dev_err(di->dev, "Power-off LED failed\n");
1407 return ret;
1408 }
1409 }
1410
1411 return ret;
1412}
1413
1414/**
1415 * ab8500_charger_ac_en() - enable or disable ac charging
1416 * @di: pointer to the ab8500_charger structure
1417 * @enable: enable/disable flag
1418 * @vset: charging voltage
1419 * @iset: charging current
1420 *
1421 * Enable/Disable AC/Mains charging and turns on/off the charging led
1422 * respectively.
1423 **/
1424static int ab8500_charger_ac_en(struct ux500_charger *charger,
1425 int enable, int vset, int iset)
1426{
1427 int ret;
1428 int volt_index;
1429 int curr_index;
1430 int input_curr_index;
1431 u8 overshoot = 0;
1432
1433 struct ab8500_charger *di = to_ab8500_charger_ac_device_info(charger);
1434
1435 if (enable) {
1436 /* Check if AC is connected */
1437 if (!di->ac.charger_connected) {
1438 dev_err(di->dev, "AC charger not connected\n");
1439 return -ENXIO;
1440 }
1441
1442 /* Enable AC charging */
1443 dev_dbg(di->dev, "Enable AC: %dmV %dmA\n", vset, iset);
1444
1445 /*
1446 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1447 * will be triggered everytime we enable the VDD ADC supply.
1448 * This will turn off charging for a short while.
1449 * It can be avoided by having the supply on when
1450 * there is a charger enabled. Normally the VDD ADC supply
1451 * is enabled everytime a GPADC conversion is triggered. We will
1452 * force it to be enabled from this driver to have
1453 * the GPADC module independant of the AB8500 chargers
1454 */
1455 if (!di->vddadc_en_ac) {
1456 regulator_enable(di->regu);
1457 di->vddadc_en_ac = true;
1458 }
1459
1460 /* Check if the requested voltage or current is valid */
1461 volt_index = ab8500_voltage_to_regval(vset);
1462 curr_index = ab8500_current_to_regval(iset);
1463 input_curr_index = ab8500_current_to_regval(
Lee Jonesb0284de2012-11-30 10:09:42 +00001464 di->bm->chg_params->ac_curr_max);
Arun Murthy84edbee2012-02-29 21:54:26 +05301465 if (volt_index < 0 || curr_index < 0 || input_curr_index < 0) {
1466 dev_err(di->dev,
1467 "Charger voltage or current too high, "
1468 "charging not started\n");
1469 return -ENXIO;
1470 }
1471
1472 /* ChVoltLevel: maximum battery charging voltage */
1473 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1474 AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1475 if (ret) {
1476 dev_err(di->dev, "%s write failed\n", __func__);
1477 return ret;
1478 }
1479 /* MainChInputCurr: current that can be drawn from the charger*/
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001480 ret = ab8500_charger_set_main_in_curr(di,
Lee Jonesb0284de2012-11-30 10:09:42 +00001481 di->bm->chg_params->ac_curr_max);
Arun Murthy84edbee2012-02-29 21:54:26 +05301482 if (ret) {
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001483 dev_err(di->dev, "%s Failed to set MainChInputCurr\n",
1484 __func__);
Arun Murthy84edbee2012-02-29 21:54:26 +05301485 return ret;
1486 }
1487 /* ChOutputCurentLevel: protected output current */
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001488 ret = ab8500_charger_set_output_curr(di, iset);
Arun Murthy84edbee2012-02-29 21:54:26 +05301489 if (ret) {
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001490 dev_err(di->dev, "%s "
1491 "Failed to set ChOutputCurentLevel\n",
1492 __func__);
Arun Murthy84edbee2012-02-29 21:54:26 +05301493 return ret;
1494 }
1495
1496 /* Check if VBAT overshoot control should be enabled */
Lee Jonesb0284de2012-11-30 10:09:42 +00001497 if (!di->bm->enable_overshoot)
Arun Murthy84edbee2012-02-29 21:54:26 +05301498 overshoot = MAIN_CH_NO_OVERSHOOT_ENA_N;
1499
1500 /* Enable Main Charger */
1501 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1502 AB8500_MCH_CTRL1, MAIN_CH_ENA | overshoot);
1503 if (ret) {
1504 dev_err(di->dev, "%s write failed\n", __func__);
1505 return ret;
1506 }
1507
1508 /* Power on charging LED indication */
1509 ret = ab8500_charger_led_en(di, true);
1510 if (ret < 0)
1511 dev_err(di->dev, "failed to enable LED\n");
1512
1513 di->ac.charger_online = 1;
1514 } else {
1515 /* Disable AC charging */
1516 if (is_ab8500_1p1_or_earlier(di->parent)) {
1517 /*
1518 * For ABB revision 1.0 and 1.1 there is a bug in the
1519 * watchdog logic. That means we have to continously
1520 * kick the charger watchdog even when no charger is
1521 * connected. This is only valid once the AC charger
1522 * has been enabled. This is a bug that is not handled
1523 * by the algorithm and the watchdog have to be kicked
1524 * by the charger driver when the AC charger
1525 * is disabled
1526 */
1527 if (di->ac_conn) {
1528 queue_delayed_work(di->charger_wq,
1529 &di->kick_wd_work,
1530 round_jiffies(WD_KICK_INTERVAL));
1531 }
1532
1533 /*
1534 * We can't turn off charging completely
1535 * due to a bug in AB8500 cut1.
1536 * If we do, charging will not start again.
1537 * That is why we set the lowest voltage
1538 * and current possible
1539 */
1540 ret = abx500_set_register_interruptible(di->dev,
1541 AB8500_CHARGER,
1542 AB8500_CH_VOLT_LVL_REG, CH_VOL_LVL_3P5);
1543 if (ret) {
1544 dev_err(di->dev,
1545 "%s write failed\n", __func__);
1546 return ret;
1547 }
1548
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001549 ret = ab8500_charger_set_output_curr(di, 0);
Arun Murthy84edbee2012-02-29 21:54:26 +05301550 if (ret) {
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001551 dev_err(di->dev, "%s "
1552 "Failed to set ChOutputCurentLevel\n",
1553 __func__);
Arun Murthy84edbee2012-02-29 21:54:26 +05301554 return ret;
1555 }
1556 } else {
1557 ret = abx500_set_register_interruptible(di->dev,
1558 AB8500_CHARGER,
1559 AB8500_MCH_CTRL1, 0);
1560 if (ret) {
1561 dev_err(di->dev,
1562 "%s write failed\n", __func__);
1563 return ret;
1564 }
1565 }
1566
1567 ret = ab8500_charger_led_en(di, false);
1568 if (ret < 0)
1569 dev_err(di->dev, "failed to disable LED\n");
1570
1571 di->ac.charger_online = 0;
1572 di->ac.wd_expired = false;
1573
1574 /* Disable regulator if enabled */
1575 if (di->vddadc_en_ac) {
1576 regulator_disable(di->regu);
1577 di->vddadc_en_ac = false;
1578 }
1579
1580 dev_dbg(di->dev, "%s Disabled AC charging\n", __func__);
1581 }
1582 ab8500_power_supply_changed(di, &di->ac_chg.psy);
1583
1584 return ret;
1585}
1586
1587/**
1588 * ab8500_charger_usb_en() - enable usb charging
1589 * @di: pointer to the ab8500_charger structure
1590 * @enable: enable/disable flag
1591 * @vset: charging voltage
1592 * @ich_out: charger output current
1593 *
1594 * Enable/Disable USB charging and turns on/off the charging led respectively.
1595 * Returns error code in case of failure else 0(on success)
1596 */
1597static int ab8500_charger_usb_en(struct ux500_charger *charger,
1598 int enable, int vset, int ich_out)
1599{
1600 int ret;
1601 int volt_index;
1602 int curr_index;
1603 u8 overshoot = 0;
1604
1605 struct ab8500_charger *di = to_ab8500_charger_usb_device_info(charger);
1606
1607 if (enable) {
1608 /* Check if USB is connected */
1609 if (!di->usb.charger_connected) {
1610 dev_err(di->dev, "USB charger not connected\n");
1611 return -ENXIO;
1612 }
1613
1614 /*
1615 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1616 * will be triggered everytime we enable the VDD ADC supply.
1617 * This will turn off charging for a short while.
1618 * It can be avoided by having the supply on when
1619 * there is a charger enabled. Normally the VDD ADC supply
1620 * is enabled everytime a GPADC conversion is triggered. We will
1621 * force it to be enabled from this driver to have
1622 * the GPADC module independant of the AB8500 chargers
1623 */
1624 if (!di->vddadc_en_usb) {
1625 regulator_enable(di->regu);
1626 di->vddadc_en_usb = true;
1627 }
1628
1629 /* Enable USB charging */
1630 dev_dbg(di->dev, "Enable USB: %dmV %dmA\n", vset, ich_out);
1631
1632 /* Check if the requested voltage or current is valid */
1633 volt_index = ab8500_voltage_to_regval(vset);
1634 curr_index = ab8500_current_to_regval(ich_out);
1635 if (volt_index < 0 || curr_index < 0) {
1636 dev_err(di->dev,
1637 "Charger voltage or current too high, "
1638 "charging not started\n");
1639 return -ENXIO;
1640 }
1641
1642 /* ChVoltLevel: max voltage upto which battery can be charged */
1643 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1644 AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1645 if (ret) {
1646 dev_err(di->dev, "%s write failed\n", __func__);
1647 return ret;
1648 }
Arun Murthy84edbee2012-02-29 21:54:26 +05301649 /* Check if VBAT overshoot control should be enabled */
Lee Jonesb0284de2012-11-30 10:09:42 +00001650 if (!di->bm->enable_overshoot)
Arun Murthy84edbee2012-02-29 21:54:26 +05301651 overshoot = USB_CHG_NO_OVERSHOOT_ENA_N;
1652
1653 /* Enable USB Charger */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001654 dev_dbg(di->dev,
1655 "Enabling USB with write to AB8500_USBCH_CTRL1_REG\n");
Arun Murthy84edbee2012-02-29 21:54:26 +05301656 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1657 AB8500_USBCH_CTRL1_REG, USB_CH_ENA | overshoot);
1658 if (ret) {
1659 dev_err(di->dev, "%s write failed\n", __func__);
1660 return ret;
1661 }
1662
1663 /* If success power on charging LED indication */
1664 ret = ab8500_charger_led_en(di, true);
1665 if (ret < 0)
1666 dev_err(di->dev, "failed to enable LED\n");
1667
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001668 di->usb.charger_online = 1;
1669
1670 /* USBChInputCurr: current that can be drawn from the usb */
Lee Jonesf7470b52013-02-14 12:37:29 +00001671 ret = ab8500_charger_set_vbus_in_curr(di,
1672 di->max_usb_in_curr.usb_type_max);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001673 if (ret) {
1674 dev_err(di->dev, "setting USBChInputCurr failed\n");
1675 return ret;
1676 }
1677
1678 /* ChOutputCurentLevel: protected output current */
1679 ret = ab8500_charger_set_output_curr(di, ich_out);
1680 if (ret) {
1681 dev_err(di->dev, "%s "
1682 "Failed to set ChOutputCurentLevel\n",
1683 __func__);
1684 return ret;
1685 }
1686
Arun Murthy84edbee2012-02-29 21:54:26 +05301687 queue_delayed_work(di->charger_wq, &di->check_vbat_work, HZ);
1688
Arun Murthy84edbee2012-02-29 21:54:26 +05301689 } else {
1690 /* Disable USB charging */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001691 dev_dbg(di->dev, "%s Disabled USB charging\n", __func__);
Arun Murthy84edbee2012-02-29 21:54:26 +05301692 ret = abx500_set_register_interruptible(di->dev,
1693 AB8500_CHARGER,
1694 AB8500_USBCH_CTRL1_REG, 0);
1695 if (ret) {
1696 dev_err(di->dev,
1697 "%s write failed\n", __func__);
1698 return ret;
1699 }
1700
1701 ret = ab8500_charger_led_en(di, false);
1702 if (ret < 0)
1703 dev_err(di->dev, "failed to disable LED\n");
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001704 /* USBChInputCurr: current that can be drawn from the usb */
1705 ret = ab8500_charger_set_vbus_in_curr(di, 0);
1706 if (ret) {
1707 dev_err(di->dev, "setting USBChInputCurr failed\n");
1708 return ret;
1709 }
Arun Murthy84edbee2012-02-29 21:54:26 +05301710
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001711 /* ChOutputCurentLevel: protected output current */
1712 ret = ab8500_charger_set_output_curr(di, 0);
1713 if (ret) {
1714 dev_err(di->dev, "%s "
1715 "Failed to reset ChOutputCurentLevel\n",
1716 __func__);
1717 return ret;
1718 }
Arun Murthy84edbee2012-02-29 21:54:26 +05301719 di->usb.charger_online = 0;
1720 di->usb.wd_expired = false;
1721
1722 /* Disable regulator if enabled */
1723 if (di->vddadc_en_usb) {
1724 regulator_disable(di->regu);
1725 di->vddadc_en_usb = false;
1726 }
1727
1728 dev_dbg(di->dev, "%s Disabled USB charging\n", __func__);
1729
1730 /* Cancel any pending Vbat check work */
1731 if (delayed_work_pending(&di->check_vbat_work))
1732 cancel_delayed_work(&di->check_vbat_work);
1733
1734 }
1735 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1736
1737 return ret;
1738}
1739
Lee Jones88917162013-02-13 11:39:19 +00001740static int ab8500_external_charger_prepare(struct notifier_block *charger_nb,
1741 unsigned long event, void *data)
1742{
1743 int ret;
1744 struct device *dev = data;
1745 /*Toggle External charger control pin*/
1746 ret = abx500_set_register_interruptible(dev, AB8500_SYS_CTRL1_BLOCK,
1747 AB8500_SYS_CHARGER_CONTROL_REG,
1748 EXTERNAL_CHARGER_DISABLE_REG_VAL);
1749 if (ret < 0) {
1750 dev_err(dev, "write reg failed %d\n", ret);
1751 goto out;
1752 }
1753 ret = abx500_set_register_interruptible(dev, AB8500_SYS_CTRL1_BLOCK,
1754 AB8500_SYS_CHARGER_CONTROL_REG,
1755 EXTERNAL_CHARGER_ENABLE_REG_VAL);
1756 if (ret < 0)
1757 dev_err(dev, "Write reg failed %d\n", ret);
1758
1759out:
1760 return ret;
1761}
1762
Arun Murthy84edbee2012-02-29 21:54:26 +05301763/**
Lee Jones4dcdf572013-02-14 09:24:10 +00001764 * ab8500_charger_usb_check_enable() - enable usb charging
1765 * @charger: pointer to the ux500_charger structure
1766 * @vset: charging voltage
1767 * @iset: charger output current
1768 *
1769 * Check if the VBUS charger has been disconnected and reconnected without
1770 * AB8500 rising an interrupt. Returns 0 on success.
1771 */
1772static int ab8500_charger_usb_check_enable(struct ux500_charger *charger,
1773 int vset, int iset)
1774{
1775 u8 usbch_ctrl1 = 0;
1776 int ret = 0;
1777
1778 struct ab8500_charger *di = to_ab8500_charger_usb_device_info(charger);
1779
1780 if (!di->usb.charger_connected)
1781 return ret;
1782
1783 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1784 AB8500_USBCH_CTRL1_REG, &usbch_ctrl1);
1785 if (ret < 0) {
1786 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
1787 return ret;
1788 }
1789 dev_dbg(di->dev, "USB charger ctrl: 0x%02x\n", usbch_ctrl1);
1790
1791 if (!(usbch_ctrl1 & USB_CH_ENA)) {
1792 dev_info(di->dev, "Charging has been disabled abnormally and will be re-enabled\n");
1793
1794 ret = abx500_mask_and_set_register_interruptible(di->dev,
1795 AB8500_CHARGER, AB8500_CHARGER_CTRL,
1796 DROP_COUNT_RESET, DROP_COUNT_RESET);
1797 if (ret < 0) {
1798 dev_err(di->dev, "ab8500 write failed %d\n", __LINE__);
1799 return ret;
1800 }
1801
1802 ret = ab8500_charger_usb_en(&di->usb_chg, true, vset, iset);
1803 if (ret < 0) {
1804 dev_err(di->dev, "Failed to enable VBUS charger %d\n",
1805 __LINE__);
1806 return ret;
1807 }
1808 }
1809 return ret;
1810}
1811
1812/**
1813 * ab8500_charger_ac_check_enable() - enable usb charging
1814 * @charger: pointer to the ux500_charger structure
1815 * @vset: charging voltage
1816 * @iset: charger output current
1817 *
1818 * Check if the AC charger has been disconnected and reconnected without
1819 * AB8500 rising an interrupt. Returns 0 on success.
1820 */
1821static int ab8500_charger_ac_check_enable(struct ux500_charger *charger,
1822 int vset, int iset)
1823{
1824 u8 mainch_ctrl1 = 0;
1825 int ret = 0;
1826
1827 struct ab8500_charger *di = to_ab8500_charger_ac_device_info(charger);
1828
1829 if (!di->ac.charger_connected)
1830 return ret;
1831
1832 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1833 AB8500_MCH_CTRL1, &mainch_ctrl1);
1834 if (ret < 0) {
1835 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
1836 return ret;
1837 }
1838 dev_dbg(di->dev, "AC charger ctrl: 0x%02x\n", mainch_ctrl1);
1839
1840 if (!(mainch_ctrl1 & MAIN_CH_ENA)) {
1841 dev_info(di->dev, "Charging has been disabled abnormally and will be re-enabled\n");
1842
1843 ret = abx500_mask_and_set_register_interruptible(di->dev,
1844 AB8500_CHARGER, AB8500_CHARGER_CTRL,
1845 DROP_COUNT_RESET, DROP_COUNT_RESET);
1846
1847 if (ret < 0) {
1848 dev_err(di->dev, "ab8500 write failed %d\n", __LINE__);
1849 return ret;
1850 }
1851
1852 ret = ab8500_charger_ac_en(&di->usb_chg, true, vset, iset);
1853 if (ret < 0) {
1854 dev_err(di->dev, "failed to enable AC charger %d\n",
1855 __LINE__);
1856 return ret;
1857 }
1858 }
1859 return ret;
1860}
1861
1862/**
Arun Murthy84edbee2012-02-29 21:54:26 +05301863 * ab8500_charger_watchdog_kick() - kick charger watchdog
1864 * @di: pointer to the ab8500_charger structure
1865 *
1866 * Kick charger watchdog
1867 * Returns error code in case of failure else 0(on success)
1868 */
1869static int ab8500_charger_watchdog_kick(struct ux500_charger *charger)
1870{
1871 int ret;
1872 struct ab8500_charger *di;
1873
1874 if (charger->psy.type == POWER_SUPPLY_TYPE_MAINS)
1875 di = to_ab8500_charger_ac_device_info(charger);
1876 else if (charger->psy.type == POWER_SUPPLY_TYPE_USB)
1877 di = to_ab8500_charger_usb_device_info(charger);
1878 else
1879 return -ENXIO;
1880
1881 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1882 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
1883 if (ret)
1884 dev_err(di->dev, "Failed to kick WD!\n");
1885
1886 return ret;
1887}
1888
1889/**
1890 * ab8500_charger_update_charger_current() - update charger current
1891 * @di: pointer to the ab8500_charger structure
1892 *
1893 * Update the charger output current for the specified charger
1894 * Returns error code in case of failure else 0(on success)
1895 */
1896static int ab8500_charger_update_charger_current(struct ux500_charger *charger,
1897 int ich_out)
1898{
1899 int ret;
Arun Murthy84edbee2012-02-29 21:54:26 +05301900 struct ab8500_charger *di;
1901
1902 if (charger->psy.type == POWER_SUPPLY_TYPE_MAINS)
1903 di = to_ab8500_charger_ac_device_info(charger);
1904 else if (charger->psy.type == POWER_SUPPLY_TYPE_USB)
1905 di = to_ab8500_charger_usb_device_info(charger);
1906 else
1907 return -ENXIO;
1908
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001909 ret = ab8500_charger_set_output_curr(di, ich_out);
Arun Murthy84edbee2012-02-29 21:54:26 +05301910 if (ret) {
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001911 dev_err(di->dev, "%s "
1912 "Failed to set ChOutputCurentLevel\n",
1913 __func__);
Arun Murthy84edbee2012-02-29 21:54:26 +05301914 return ret;
1915 }
1916
1917 /* Reset the main and usb drop input current measurement counter */
1918 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
Lee Jones4dcdf572013-02-14 09:24:10 +00001919 AB8500_CHARGER_CTRL, DROP_COUNT_RESET);
Arun Murthy84edbee2012-02-29 21:54:26 +05301920 if (ret) {
1921 dev_err(di->dev, "%s write failed\n", __func__);
1922 return ret;
1923 }
1924
1925 return ret;
1926}
1927
Lee Jonesdb43e6c2013-02-14 12:39:15 +00001928/**
1929 * ab8540_charger_power_path_enable() - enable usb power path mode
1930 * @charger: pointer to the ux500_charger structure
1931 * @enable: enable/disable flag
1932 *
1933 * Enable or disable the power path for usb mode
1934 * Returns error code in case of failure else 0(on success)
1935 */
1936static int ab8540_charger_power_path_enable(struct ux500_charger *charger,
1937 bool enable)
1938{
1939 int ret;
1940 struct ab8500_charger *di;
1941
1942 if (charger->psy.type == POWER_SUPPLY_TYPE_USB)
1943 di = to_ab8500_charger_usb_device_info(charger);
1944 else
1945 return -ENXIO;
1946
1947 ret = abx500_mask_and_set_register_interruptible(di->dev,
1948 AB8500_CHARGER, AB8540_USB_PP_MODE_REG,
1949 BUS_POWER_PATH_MODE_ENA, enable);
1950 if (ret) {
1951 dev_err(di->dev, "%s write failed\n", __func__);
1952 return ret;
1953 }
1954
1955 return ret;
1956}
1957
1958
1959/**
1960 * ab8540_charger_usb_pre_chg_enable() - enable usb pre change
1961 * @charger: pointer to the ux500_charger structure
1962 * @enable: enable/disable flag
1963 *
1964 * Enable or disable the pre-chage for usb mode
1965 * Returns error code in case of failure else 0(on success)
1966 */
1967static int ab8540_charger_usb_pre_chg_enable(struct ux500_charger *charger,
1968 bool enable)
1969{
1970 int ret;
1971 struct ab8500_charger *di;
1972
1973 if (charger->psy.type == POWER_SUPPLY_TYPE_USB)
1974 di = to_ab8500_charger_usb_device_info(charger);
1975 else
1976 return -ENXIO;
1977
1978 ret = abx500_mask_and_set_register_interruptible(di->dev,
1979 AB8500_CHARGER, AB8540_USB_PP_CHR_REG,
1980 BUS_POWER_PATH_PRECHG_ENA, enable);
1981 if (ret) {
1982 dev_err(di->dev, "%s write failed\n", __func__);
1983 return ret;
1984 }
1985
1986 return ret;
1987}
1988
Arun Murthy84edbee2012-02-29 21:54:26 +05301989static int ab8500_charger_get_ext_psy_data(struct device *dev, void *data)
1990{
1991 struct power_supply *psy;
1992 struct power_supply *ext;
1993 struct ab8500_charger *di;
1994 union power_supply_propval ret;
1995 int i, j;
1996 bool psy_found = false;
1997 struct ux500_charger *usb_chg;
1998
1999 usb_chg = (struct ux500_charger *)data;
2000 psy = &usb_chg->psy;
2001
2002 di = to_ab8500_charger_usb_device_info(usb_chg);
2003
2004 ext = dev_get_drvdata(dev);
2005
2006 /* For all psy where the driver name appears in any supplied_to */
2007 for (i = 0; i < ext->num_supplicants; i++) {
2008 if (!strcmp(ext->supplied_to[i], psy->name))
2009 psy_found = true;
2010 }
2011
2012 if (!psy_found)
2013 return 0;
2014
2015 /* Go through all properties for the psy */
2016 for (j = 0; j < ext->num_properties; j++) {
2017 enum power_supply_property prop;
2018 prop = ext->properties[j];
2019
2020 if (ext->get_property(ext, prop, &ret))
2021 continue;
2022
2023 switch (prop) {
2024 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
2025 switch (ext->type) {
2026 case POWER_SUPPLY_TYPE_BATTERY:
2027 di->vbat = ret.intval / 1000;
2028 break;
2029 default:
2030 break;
2031 }
2032 break;
2033 default:
2034 break;
2035 }
2036 }
2037 return 0;
2038}
2039
2040/**
2041 * ab8500_charger_check_vbat_work() - keep vbus current within spec
2042 * @work pointer to the work_struct structure
2043 *
2044 * Due to a asic bug it is necessary to lower the input current to the vbus
2045 * charger when charging with at some specific levels. This issue is only valid
2046 * for below a certain battery voltage. This function makes sure that the
2047 * the allowed current limit isn't exceeded.
2048 */
2049static void ab8500_charger_check_vbat_work(struct work_struct *work)
2050{
2051 int t = 10;
2052 struct ab8500_charger *di = container_of(work,
2053 struct ab8500_charger, check_vbat_work.work);
2054
2055 class_for_each_device(power_supply_class, NULL,
2056 &di->usb_chg.psy, ab8500_charger_get_ext_psy_data);
2057
2058 /* First run old_vbat is 0. */
2059 if (di->old_vbat == 0)
2060 di->old_vbat = di->vbat;
2061
2062 if (!((di->old_vbat <= VBAT_TRESH_IP_CUR_RED &&
2063 di->vbat <= VBAT_TRESH_IP_CUR_RED) ||
2064 (di->old_vbat > VBAT_TRESH_IP_CUR_RED &&
2065 di->vbat > VBAT_TRESH_IP_CUR_RED))) {
2066
2067 dev_dbg(di->dev, "Vbat did cross threshold, curr: %d, new: %d,"
Lee Jonesf7470b52013-02-14 12:37:29 +00002068 " old: %d\n", di->max_usb_in_curr.usb_type_max,
2069 di->vbat, di->old_vbat);
2070 ab8500_charger_set_vbus_in_curr(di,
2071 di->max_usb_in_curr.usb_type_max);
Arun Murthy84edbee2012-02-29 21:54:26 +05302072 power_supply_changed(&di->usb_chg.psy);
2073 }
2074
2075 di->old_vbat = di->vbat;
2076
2077 /*
2078 * No need to check the battery voltage every second when not close to
2079 * the threshold.
2080 */
2081 if (di->vbat < (VBAT_TRESH_IP_CUR_RED + 100) &&
2082 (di->vbat > (VBAT_TRESH_IP_CUR_RED - 100)))
2083 t = 1;
2084
2085 queue_delayed_work(di->charger_wq, &di->check_vbat_work, t * HZ);
2086}
2087
2088/**
2089 * ab8500_charger_check_hw_failure_work() - check main charger failure
2090 * @work: pointer to the work_struct structure
2091 *
2092 * Work queue function for checking the main charger status
2093 */
2094static void ab8500_charger_check_hw_failure_work(struct work_struct *work)
2095{
2096 int ret;
2097 u8 reg_value;
2098
2099 struct ab8500_charger *di = container_of(work,
2100 struct ab8500_charger, check_hw_failure_work.work);
2101
2102 /* Check if the status bits for HW failure is still active */
2103 if (di->flags.mainextchnotok) {
2104 ret = abx500_get_register_interruptible(di->dev,
2105 AB8500_CHARGER, AB8500_CH_STATUS2_REG, &reg_value);
2106 if (ret < 0) {
2107 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2108 return;
2109 }
2110 if (!(reg_value & MAIN_CH_NOK)) {
2111 di->flags.mainextchnotok = false;
2112 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2113 }
2114 }
2115 if (di->flags.vbus_ovv) {
2116 ret = abx500_get_register_interruptible(di->dev,
2117 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG,
2118 &reg_value);
2119 if (ret < 0) {
2120 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2121 return;
2122 }
2123 if (!(reg_value & VBUS_OVV_TH)) {
2124 di->flags.vbus_ovv = false;
2125 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2126 }
2127 }
2128 /* If we still have a failure, schedule a new check */
2129 if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
2130 queue_delayed_work(di->charger_wq,
2131 &di->check_hw_failure_work, round_jiffies(HZ));
2132 }
2133}
2134
2135/**
2136 * ab8500_charger_kick_watchdog_work() - kick the watchdog
2137 * @work: pointer to the work_struct structure
2138 *
2139 * Work queue function for kicking the charger watchdog.
2140 *
2141 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
2142 * logic. That means we have to continously kick the charger
2143 * watchdog even when no charger is connected. This is only
2144 * valid once the AC charger has been enabled. This is
2145 * a bug that is not handled by the algorithm and the
2146 * watchdog have to be kicked by the charger driver
2147 * when the AC charger is disabled
2148 */
2149static void ab8500_charger_kick_watchdog_work(struct work_struct *work)
2150{
2151 int ret;
2152
2153 struct ab8500_charger *di = container_of(work,
2154 struct ab8500_charger, kick_wd_work.work);
2155
2156 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
2157 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
2158 if (ret)
2159 dev_err(di->dev, "Failed to kick WD!\n");
2160
2161 /* Schedule a new watchdog kick */
2162 queue_delayed_work(di->charger_wq,
2163 &di->kick_wd_work, round_jiffies(WD_KICK_INTERVAL));
2164}
2165
2166/**
2167 * ab8500_charger_ac_work() - work to get and set main charger status
2168 * @work: pointer to the work_struct structure
2169 *
2170 * Work queue function for checking the main charger status
2171 */
2172static void ab8500_charger_ac_work(struct work_struct *work)
2173{
2174 int ret;
2175
2176 struct ab8500_charger *di = container_of(work,
2177 struct ab8500_charger, ac_work);
2178
2179 /*
2180 * Since we can't be sure that the events are received
2181 * synchronously, we have the check if the main charger is
2182 * connected by reading the status register
2183 */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002184 ret = ab8500_charger_detect_chargers(di, false);
Arun Murthy84edbee2012-02-29 21:54:26 +05302185 if (ret < 0)
2186 return;
2187
2188 if (ret & AC_PW_CONN) {
2189 di->ac.charger_connected = 1;
2190 di->ac_conn = true;
2191 } else {
2192 di->ac.charger_connected = 0;
2193 }
2194
2195 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2196 sysfs_notify(&di->ac_chg.psy.dev->kobj, NULL, "present");
2197}
2198
Lee Jonesb269fff2013-01-11 13:12:51 +00002199static void ab8500_charger_usb_attached_work(struct work_struct *work)
2200{
2201 struct ab8500_charger *di = container_of(work,
2202 struct ab8500_charger,
2203 usb_charger_attached_work.work);
2204 int usbch = (USB_CH_VBUSDROP | USB_CH_VBUSDETDBNC);
2205 int ret, i;
2206 u8 statval;
2207
2208 for (i = 0; i < 10; i++) {
2209 ret = abx500_get_register_interruptible(di->dev,
2210 AB8500_CHARGER,
2211 AB8500_CH_USBCH_STAT1_REG,
2212 &statval);
2213 if (ret < 0) {
2214 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
2215 goto reschedule;
2216 }
2217 if ((statval & usbch) != usbch)
2218 goto reschedule;
2219
2220 msleep(CHARGER_STATUS_POLL);
2221 }
2222
2223 ab8500_charger_usb_en(&di->usb_chg, 0, 0, 0);
2224
2225 mutex_lock(&di->charger_attached_mutex);
2226 mutex_unlock(&di->charger_attached_mutex);
2227
2228 return;
2229
2230reschedule:
2231 queue_delayed_work(di->charger_wq,
2232 &di->usb_charger_attached_work,
2233 HZ);
2234}
2235
2236static void ab8500_charger_ac_attached_work(struct work_struct *work)
2237{
2238
2239 struct ab8500_charger *di = container_of(work,
2240 struct ab8500_charger,
2241 ac_charger_attached_work.work);
2242 int mainch = (MAIN_CH_STATUS2_MAINCHGDROP |
2243 MAIN_CH_STATUS2_MAINCHARGERDETDBNC);
2244 int ret, i;
2245 u8 statval;
2246
2247 for (i = 0; i < 10; i++) {
2248 ret = abx500_get_register_interruptible(di->dev,
2249 AB8500_CHARGER,
2250 AB8500_CH_STATUS2_REG,
2251 &statval);
2252 if (ret < 0) {
2253 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
2254 goto reschedule;
2255 }
2256
2257 if ((statval & mainch) != mainch)
2258 goto reschedule;
2259
2260 msleep(CHARGER_STATUS_POLL);
2261 }
2262
2263 ab8500_charger_ac_en(&di->ac_chg, 0, 0, 0);
2264 queue_work(di->charger_wq, &di->ac_work);
2265
2266 mutex_lock(&di->charger_attached_mutex);
2267 mutex_unlock(&di->charger_attached_mutex);
2268
2269 return;
2270
2271reschedule:
2272 queue_delayed_work(di->charger_wq,
2273 &di->ac_charger_attached_work,
2274 HZ);
2275}
2276
Arun Murthy84edbee2012-02-29 21:54:26 +05302277/**
2278 * ab8500_charger_detect_usb_type_work() - work to detect USB type
2279 * @work: Pointer to the work_struct structure
2280 *
2281 * Detect the type of USB plugged
2282 */
Anton Vorontsov64eb9b02012-03-14 04:43:11 +04002283static void ab8500_charger_detect_usb_type_work(struct work_struct *work)
Arun Murthy84edbee2012-02-29 21:54:26 +05302284{
2285 int ret;
2286
2287 struct ab8500_charger *di = container_of(work,
2288 struct ab8500_charger, detect_usb_type_work);
2289
2290 /*
2291 * Since we can't be sure that the events are received
2292 * synchronously, we have the check if is
2293 * connected by reading the status register
2294 */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002295 ret = ab8500_charger_detect_chargers(di, false);
Arun Murthy84edbee2012-02-29 21:54:26 +05302296 if (ret < 0)
2297 return;
2298
2299 if (!(ret & USB_PW_CONN)) {
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002300 dev_dbg(di->dev, "%s di->vbus_detected = false\n", __func__);
2301 di->vbus_detected = false;
Arun Murthy84edbee2012-02-29 21:54:26 +05302302 ab8500_charger_set_usb_connected(di, false);
2303 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2304 } else {
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002305 dev_dbg(di->dev, "%s di->vbus_detected = true\n", __func__);
2306 di->vbus_detected = true;
Arun Murthy84edbee2012-02-29 21:54:26 +05302307
2308 if (is_ab8500_1p1_or_earlier(di->parent)) {
2309 ret = ab8500_charger_detect_usb_type(di);
2310 if (!ret) {
2311 ab8500_charger_set_usb_connected(di, true);
2312 ab8500_power_supply_changed(di,
2313 &di->usb_chg.psy);
2314 }
2315 } else {
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002316 /*
2317 * For ABB cut2.0 and onwards we have an IRQ,
Arun Murthy84edbee2012-02-29 21:54:26 +05302318 * USB_LINK_STATUS that will be triggered when the USB
2319 * link status changes. The exception is USB connected
2320 * during startup. Then we don't get a
2321 * USB_LINK_STATUS IRQ
2322 */
2323 if (di->vbus_detected_start) {
2324 di->vbus_detected_start = false;
2325 ret = ab8500_charger_detect_usb_type(di);
2326 if (!ret) {
2327 ab8500_charger_set_usb_connected(di,
2328 true);
2329 ab8500_power_supply_changed(di,
2330 &di->usb_chg.psy);
2331 }
2332 }
2333 }
2334 }
2335}
2336
2337/**
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002338 * ab8500_charger_usb_link_attach_work() - work to detect USB type
Marcus Cooper4b45f4a2013-01-11 13:13:04 +00002339 * @work: pointer to the work_struct structure
2340 *
2341 * Detect the type of USB plugged
2342 */
2343static void ab8500_charger_usb_link_attach_work(struct work_struct *work)
2344{
2345 struct ab8500_charger *di =
2346 container_of(work, struct ab8500_charger, attach_work.work);
2347 int ret;
2348
2349 /* Update maximum input current if USB enumeration is not detected */
2350 if (!di->usb.charger_online) {
Lee Jonesf7470b52013-02-14 12:37:29 +00002351 ret = ab8500_charger_set_vbus_in_curr(di,
2352 di->max_usb_in_curr.usb_type_max);
Marcus Cooper4b45f4a2013-01-11 13:13:04 +00002353 if (ret)
2354 return;
2355 }
2356
2357 ab8500_charger_set_usb_connected(di, true);
2358 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2359}
2360
2361/**
Arun Murthy84edbee2012-02-29 21:54:26 +05302362 * ab8500_charger_usb_link_status_work() - work to detect USB type
2363 * @work: pointer to the work_struct structure
2364 *
2365 * Detect the type of USB plugged
2366 */
2367static void ab8500_charger_usb_link_status_work(struct work_struct *work)
2368{
Henrik Sölverff380902012-04-17 15:51:01 +02002369 int detected_chargers;
Arun Murthy84edbee2012-02-29 21:54:26 +05302370 int ret;
Henrik Sölverff380902012-04-17 15:51:01 +02002371 u8 val;
Hakan Bergd4337662012-07-26 11:10:35 +02002372 u8 link_status;
Arun Murthy84edbee2012-02-29 21:54:26 +05302373
2374 struct ab8500_charger *di = container_of(work,
2375 struct ab8500_charger, usb_link_status_work);
2376
2377 /*
2378 * Since we can't be sure that the events are received
2379 * synchronously, we have the check if is
2380 * connected by reading the status register
2381 */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002382 detected_chargers = ab8500_charger_detect_chargers(di, false);
Henrik Sölverff380902012-04-17 15:51:01 +02002383 if (detected_chargers < 0)
Arun Murthy84edbee2012-02-29 21:54:26 +05302384 return;
2385
Henrik Sölverff380902012-04-17 15:51:01 +02002386 /*
2387 * Some chargers that breaks the USB spec is
2388 * identified as invalid by AB8500 and it refuse
2389 * to start the charging process. but by jumping
2390 * thru a few hoops it can be forced to start.
2391 */
2392 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
2393 AB8500_USB_LINE_STAT_REG, &val);
2394 if (ret >= 0)
2395 dev_dbg(di->dev, "UsbLineStatus register = 0x%02x\n", val);
2396 else
2397 dev_dbg(di->dev, "Error reading USB link status\n");
2398
Hakan Bergd4337662012-07-26 11:10:35 +02002399 if (is_ab9540(di->parent) || is_ab8505(di->parent))
2400 link_status = AB8505_USB_LINK_STATUS;
2401 else
2402 link_status = AB8500_USB_LINK_STATUS;
2403
Henrik Sölverff380902012-04-17 15:51:01 +02002404 if (detected_chargers & USB_PW_CONN) {
Hakan Bergd4337662012-07-26 11:10:35 +02002405 if (((val & link_status) >> 3) == USB_STAT_NOT_VALID_LINK &&
Henrik Sölverff380902012-04-17 15:51:01 +02002406 di->invalid_charger_detect_state == 0) {
2407 dev_dbg(di->dev, "Invalid charger detected, state= 0\n");
2408 /*Enable charger*/
2409 abx500_mask_and_set_register_interruptible(di->dev,
2410 AB8500_CHARGER, AB8500_USBCH_CTRL1_REG, 0x01, 0x01);
2411 /*Enable charger detection*/
2412 abx500_mask_and_set_register_interruptible(di->dev, AB8500_USB,
2413 AB8500_MCH_IPT_CURLVL_REG, 0x01, 0x01);
2414 di->invalid_charger_detect_state = 1;
2415 /*exit and wait for new link status interrupt.*/
2416 return;
2417
2418 }
2419 if (di->invalid_charger_detect_state == 1) {
2420 dev_dbg(di->dev, "Invalid charger detected, state= 1\n");
2421 /*Stop charger detection*/
2422 abx500_mask_and_set_register_interruptible(di->dev, AB8500_USB,
2423 AB8500_MCH_IPT_CURLVL_REG, 0x01, 0x00);
2424 /*Check link status*/
2425 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
2426 AB8500_USB_LINE_STAT_REG, &val);
2427 dev_dbg(di->dev, "USB link status= 0x%02x\n",
Hakan Bergd4337662012-07-26 11:10:35 +02002428 (val & link_status) >> 3);
Henrik Sölverff380902012-04-17 15:51:01 +02002429 di->invalid_charger_detect_state = 2;
2430 }
2431 } else {
2432 di->invalid_charger_detect_state = 0;
2433 }
2434
2435 if (!(detected_chargers & USB_PW_CONN)) {
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002436 di->vbus_detected = false;
Arun Murthy84edbee2012-02-29 21:54:26 +05302437 ab8500_charger_set_usb_connected(di, false);
2438 ab8500_power_supply_changed(di, &di->usb_chg.psy);
Marcus Cooper4b45f4a2013-01-11 13:13:04 +00002439 return;
2440 }
Arun Murthy84edbee2012-02-29 21:54:26 +05302441
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002442 dev_dbg(di->dev,"%s di->vbus_detected = true\n",__func__);
2443 di->vbus_detected = true;
Marcus Cooper4b45f4a2013-01-11 13:13:04 +00002444 ret = ab8500_charger_read_usb_type(di);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002445 if (ret) {
2446 if (ret == -ENXIO) {
2447 /* No valid charger type detected */
2448 ab8500_charger_set_usb_connected(di, false);
2449 ab8500_power_supply_changed(di, &di->usb_chg.psy);
Arun Murthy84edbee2012-02-29 21:54:26 +05302450 }
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002451 return;
2452 }
2453
2454 if (di->usb_device_is_unrecognised) {
2455 dev_dbg(di->dev,
2456 "Potential Legacy Charger device. "
2457 "Delay work for %d msec for USB enum "
2458 "to finish",
2459 WAIT_ACA_RID_ENUMERATION);
2460 queue_delayed_work(di->charger_wq,
2461 &di->attach_work,
2462 msecs_to_jiffies(WAIT_ACA_RID_ENUMERATION));
2463 } else if (di->is_aca_rid == 1) {
2464 /* Only wait once */
2465 di->is_aca_rid++;
2466 dev_dbg(di->dev,
2467 "%s Wait %d msec for USB enum to finish",
2468 __func__, WAIT_ACA_RID_ENUMERATION);
2469 queue_delayed_work(di->charger_wq,
2470 &di->attach_work,
2471 msecs_to_jiffies(WAIT_ACA_RID_ENUMERATION));
2472 } else {
2473 queue_delayed_work(di->charger_wq,
2474 &di->attach_work,
2475 0);
Arun Murthy84edbee2012-02-29 21:54:26 +05302476 }
2477}
2478
2479static void ab8500_charger_usb_state_changed_work(struct work_struct *work)
2480{
2481 int ret;
2482 unsigned long flags;
2483
2484 struct ab8500_charger *di = container_of(work,
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002485 struct ab8500_charger, usb_state_changed_work.work);
Arun Murthy84edbee2012-02-29 21:54:26 +05302486
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002487 if (!di->vbus_detected) {
2488 dev_dbg(di->dev,
2489 "%s !di->vbus_detected\n",
2490 __func__);
Arun Murthy84edbee2012-02-29 21:54:26 +05302491 return;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002492 }
Arun Murthy84edbee2012-02-29 21:54:26 +05302493
2494 spin_lock_irqsave(&di->usb_state.usb_lock, flags);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002495 di->usb_state.state = di->usb_state.state_tmp;
2496 di->usb_state.usb_current = di->usb_state.usb_current_tmp;
Arun Murthy84edbee2012-02-29 21:54:26 +05302497 spin_unlock_irqrestore(&di->usb_state.usb_lock, flags);
2498
Arun Murthy84edbee2012-02-29 21:54:26 +05302499 dev_dbg(di->dev, "%s USB state: 0x%02x mA: %d\n",
2500 __func__, di->usb_state.state, di->usb_state.usb_current);
2501
2502 switch (di->usb_state.state) {
2503 case AB8500_BM_USB_STATE_RESET_HS:
2504 case AB8500_BM_USB_STATE_RESET_FS:
2505 case AB8500_BM_USB_STATE_SUSPEND:
2506 case AB8500_BM_USB_STATE_MAX:
2507 ab8500_charger_set_usb_connected(di, false);
2508 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2509 break;
2510
2511 case AB8500_BM_USB_STATE_RESUME:
2512 /*
2513 * when suspend->resume there should be delay
2514 * of 1sec for enabling charging
2515 */
2516 msleep(1000);
2517 /* Intentional fall through */
2518 case AB8500_BM_USB_STATE_CONFIGURED:
2519 /*
2520 * USB is configured, enable charging with the charging
2521 * input current obtained from USB driver
2522 */
2523 if (!ab8500_charger_get_usb_cur(di)) {
2524 /* Update maximum input current */
2525 ret = ab8500_charger_set_vbus_in_curr(di,
Lee Jonesf7470b52013-02-14 12:37:29 +00002526 di->max_usb_in_curr.usb_type_max);
Arun Murthy84edbee2012-02-29 21:54:26 +05302527 if (ret)
2528 return;
2529
2530 ab8500_charger_set_usb_connected(di, true);
2531 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2532 }
2533 break;
2534
2535 default:
2536 break;
2537 };
2538}
2539
2540/**
2541 * ab8500_charger_check_usbchargernotok_work() - check USB chg not ok status
2542 * @work: pointer to the work_struct structure
2543 *
2544 * Work queue function for checking the USB charger Not OK status
2545 */
2546static void ab8500_charger_check_usbchargernotok_work(struct work_struct *work)
2547{
2548 int ret;
2549 u8 reg_value;
2550 bool prev_status;
2551
2552 struct ab8500_charger *di = container_of(work,
2553 struct ab8500_charger, check_usbchgnotok_work.work);
2554
2555 /* Check if the status bit for usbchargernotok is still active */
2556 ret = abx500_get_register_interruptible(di->dev,
2557 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, &reg_value);
2558 if (ret < 0) {
2559 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2560 return;
2561 }
2562 prev_status = di->flags.usbchargernotok;
2563
2564 if (reg_value & VBUS_CH_NOK) {
2565 di->flags.usbchargernotok = true;
2566 /* Check again in 1sec */
2567 queue_delayed_work(di->charger_wq,
2568 &di->check_usbchgnotok_work, HZ);
2569 } else {
2570 di->flags.usbchargernotok = false;
2571 di->flags.vbus_collapse = false;
2572 }
2573
2574 if (prev_status != di->flags.usbchargernotok)
2575 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2576}
2577
2578/**
2579 * ab8500_charger_check_main_thermal_prot_work() - check main thermal status
2580 * @work: pointer to the work_struct structure
2581 *
2582 * Work queue function for checking the Main thermal prot status
2583 */
2584static void ab8500_charger_check_main_thermal_prot_work(
2585 struct work_struct *work)
2586{
2587 int ret;
2588 u8 reg_value;
2589
2590 struct ab8500_charger *di = container_of(work,
2591 struct ab8500_charger, check_main_thermal_prot_work);
2592
2593 /* Check if the status bit for main_thermal_prot is still active */
2594 ret = abx500_get_register_interruptible(di->dev,
2595 AB8500_CHARGER, AB8500_CH_STATUS2_REG, &reg_value);
2596 if (ret < 0) {
2597 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2598 return;
2599 }
2600 if (reg_value & MAIN_CH_TH_PROT)
2601 di->flags.main_thermal_prot = true;
2602 else
2603 di->flags.main_thermal_prot = false;
2604
2605 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2606}
2607
2608/**
2609 * ab8500_charger_check_usb_thermal_prot_work() - check usb thermal status
2610 * @work: pointer to the work_struct structure
2611 *
2612 * Work queue function for checking the USB thermal prot status
2613 */
2614static void ab8500_charger_check_usb_thermal_prot_work(
2615 struct work_struct *work)
2616{
2617 int ret;
2618 u8 reg_value;
2619
2620 struct ab8500_charger *di = container_of(work,
2621 struct ab8500_charger, check_usb_thermal_prot_work);
2622
2623 /* Check if the status bit for usb_thermal_prot is still active */
2624 ret = abx500_get_register_interruptible(di->dev,
2625 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, &reg_value);
2626 if (ret < 0) {
2627 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2628 return;
2629 }
2630 if (reg_value & USB_CH_TH_PROT)
2631 di->flags.usb_thermal_prot = true;
2632 else
2633 di->flags.usb_thermal_prot = false;
2634
2635 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2636}
2637
2638/**
2639 * ab8500_charger_mainchunplugdet_handler() - main charger unplugged
2640 * @irq: interrupt number
2641 * @_di: pointer to the ab8500_charger structure
2642 *
2643 * Returns IRQ status(IRQ_HANDLED)
2644 */
2645static irqreturn_t ab8500_charger_mainchunplugdet_handler(int irq, void *_di)
2646{
2647 struct ab8500_charger *di = _di;
2648
2649 dev_dbg(di->dev, "Main charger unplugged\n");
2650 queue_work(di->charger_wq, &di->ac_work);
2651
Lee Jonesb269fff2013-01-11 13:12:51 +00002652 cancel_delayed_work_sync(&di->ac_charger_attached_work);
2653 mutex_lock(&di->charger_attached_mutex);
2654 mutex_unlock(&di->charger_attached_mutex);
2655
Arun Murthy84edbee2012-02-29 21:54:26 +05302656 return IRQ_HANDLED;
2657}
2658
2659/**
2660 * ab8500_charger_mainchplugdet_handler() - main charger plugged
2661 * @irq: interrupt number
2662 * @_di: pointer to the ab8500_charger structure
2663 *
2664 * Returns IRQ status(IRQ_HANDLED)
2665 */
2666static irqreturn_t ab8500_charger_mainchplugdet_handler(int irq, void *_di)
2667{
2668 struct ab8500_charger *di = _di;
2669
2670 dev_dbg(di->dev, "Main charger plugged\n");
2671 queue_work(di->charger_wq, &di->ac_work);
2672
Lee Jonesb269fff2013-01-11 13:12:51 +00002673 mutex_lock(&di->charger_attached_mutex);
2674 mutex_unlock(&di->charger_attached_mutex);
2675 queue_delayed_work(di->charger_wq,
2676 &di->ac_charger_attached_work,
2677 HZ);
Arun Murthy84edbee2012-02-29 21:54:26 +05302678 return IRQ_HANDLED;
2679}
2680
2681/**
2682 * ab8500_charger_mainextchnotok_handler() - main charger not ok
2683 * @irq: interrupt number
2684 * @_di: pointer to the ab8500_charger structure
2685 *
2686 * Returns IRQ status(IRQ_HANDLED)
2687 */
2688static irqreturn_t ab8500_charger_mainextchnotok_handler(int irq, void *_di)
2689{
2690 struct ab8500_charger *di = _di;
2691
2692 dev_dbg(di->dev, "Main charger not ok\n");
2693 di->flags.mainextchnotok = true;
2694 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2695
2696 /* Schedule a new HW failure check */
2697 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
2698
2699 return IRQ_HANDLED;
2700}
2701
2702/**
2703 * ab8500_charger_mainchthprotr_handler() - Die temp is above main charger
2704 * thermal protection threshold
2705 * @irq: interrupt number
2706 * @_di: pointer to the ab8500_charger structure
2707 *
2708 * Returns IRQ status(IRQ_HANDLED)
2709 */
2710static irqreturn_t ab8500_charger_mainchthprotr_handler(int irq, void *_di)
2711{
2712 struct ab8500_charger *di = _di;
2713
2714 dev_dbg(di->dev,
2715 "Die temp above Main charger thermal protection threshold\n");
2716 queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
2717
2718 return IRQ_HANDLED;
2719}
2720
2721/**
2722 * ab8500_charger_mainchthprotf_handler() - Die temp is below main charger
2723 * thermal protection threshold
2724 * @irq: interrupt number
2725 * @_di: pointer to the ab8500_charger structure
2726 *
2727 * Returns IRQ status(IRQ_HANDLED)
2728 */
2729static irqreturn_t ab8500_charger_mainchthprotf_handler(int irq, void *_di)
2730{
2731 struct ab8500_charger *di = _di;
2732
2733 dev_dbg(di->dev,
2734 "Die temp ok for Main charger thermal protection threshold\n");
2735 queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
2736
2737 return IRQ_HANDLED;
2738}
2739
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002740static void ab8500_charger_vbus_drop_end_work(struct work_struct *work)
2741{
2742 struct ab8500_charger *di = container_of(work,
2743 struct ab8500_charger, vbus_drop_end_work.work);
Lee Jonesf7470b52013-02-14 12:37:29 +00002744 int ret;
2745 u8 reg_value;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002746
2747 di->flags.vbus_drop_end = false;
2748
2749 /* Reset the drop counter */
2750 abx500_set_register_interruptible(di->dev,
2751 AB8500_CHARGER, AB8500_CHARGER_CTRL, 0x01);
Lee Jonesf7470b52013-02-14 12:37:29 +00002752 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
2753 AB8500_CH_USBCH_STAT2_REG,
2754 &reg_value);
2755 if (ret < 0) {
2756 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2757 } else {
2758 int curr = ab8500_charger_vbus_in_curr_map[
2759 reg_value >> AUTO_VBUS_IN_CURR_LIM_SHIFT];
2760 if (di->max_usb_in_curr.calculated_max != curr) {
2761 /* USB source is collapsing */
2762 di->max_usb_in_curr.calculated_max = curr;
2763 dev_dbg(di->dev,
2764 "VBUS input current limiting to %d mA\n",
2765 di->max_usb_in_curr.calculated_max);
2766 } else {
2767 /*
2768 * USB source can not give more than this amount.
2769 * Taking more will collapse the source.
2770 */
2771 di->max_usb_in_curr.set_max =
2772 di->max_usb_in_curr.calculated_max;
2773 dev_dbg(di->dev,
2774 "VBUS input current limited to %d mA\n",
2775 di->max_usb_in_curr.set_max);
2776 return;
2777 }
2778 }
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002779
2780 if (di->usb.charger_connected)
Lee Jonesf7470b52013-02-14 12:37:29 +00002781 ab8500_charger_set_vbus_in_curr(di,
2782 di->max_usb_in_curr.usb_type_max);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002783}
2784
Arun Murthy84edbee2012-02-29 21:54:26 +05302785/**
2786 * ab8500_charger_vbusdetf_handler() - VBUS falling detected
2787 * @irq: interrupt number
2788 * @_di: pointer to the ab8500_charger structure
2789 *
2790 * Returns IRQ status(IRQ_HANDLED)
2791 */
2792static irqreturn_t ab8500_charger_vbusdetf_handler(int irq, void *_di)
2793{
2794 struct ab8500_charger *di = _di;
2795
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002796 di->vbus_detected = false;
Arun Murthy84edbee2012-02-29 21:54:26 +05302797 dev_dbg(di->dev, "VBUS falling detected\n");
2798 queue_work(di->charger_wq, &di->detect_usb_type_work);
2799
2800 return IRQ_HANDLED;
2801}
2802
2803/**
2804 * ab8500_charger_vbusdetr_handler() - VBUS rising detected
2805 * @irq: interrupt number
2806 * @_di: pointer to the ab8500_charger structure
2807 *
2808 * Returns IRQ status(IRQ_HANDLED)
2809 */
2810static irqreturn_t ab8500_charger_vbusdetr_handler(int irq, void *_di)
2811{
2812 struct ab8500_charger *di = _di;
2813
2814 di->vbus_detected = true;
2815 dev_dbg(di->dev, "VBUS rising detected\n");
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002816
Arun Murthy84edbee2012-02-29 21:54:26 +05302817 queue_work(di->charger_wq, &di->detect_usb_type_work);
2818
2819 return IRQ_HANDLED;
2820}
2821
2822/**
2823 * ab8500_charger_usblinkstatus_handler() - USB link status has changed
2824 * @irq: interrupt number
2825 * @_di: pointer to the ab8500_charger structure
2826 *
2827 * Returns IRQ status(IRQ_HANDLED)
2828 */
2829static irqreturn_t ab8500_charger_usblinkstatus_handler(int irq, void *_di)
2830{
2831 struct ab8500_charger *di = _di;
2832
2833 dev_dbg(di->dev, "USB link status changed\n");
2834
2835 queue_work(di->charger_wq, &di->usb_link_status_work);
2836
2837 return IRQ_HANDLED;
2838}
2839
2840/**
2841 * ab8500_charger_usbchthprotr_handler() - Die temp is above usb charger
2842 * thermal protection threshold
2843 * @irq: interrupt number
2844 * @_di: pointer to the ab8500_charger structure
2845 *
2846 * Returns IRQ status(IRQ_HANDLED)
2847 */
2848static irqreturn_t ab8500_charger_usbchthprotr_handler(int irq, void *_di)
2849{
2850 struct ab8500_charger *di = _di;
2851
2852 dev_dbg(di->dev,
2853 "Die temp above USB charger thermal protection threshold\n");
2854 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2855
2856 return IRQ_HANDLED;
2857}
2858
2859/**
2860 * ab8500_charger_usbchthprotf_handler() - Die temp is below usb charger
2861 * thermal protection threshold
2862 * @irq: interrupt number
2863 * @_di: pointer to the ab8500_charger structure
2864 *
2865 * Returns IRQ status(IRQ_HANDLED)
2866 */
2867static irqreturn_t ab8500_charger_usbchthprotf_handler(int irq, void *_di)
2868{
2869 struct ab8500_charger *di = _di;
2870
2871 dev_dbg(di->dev,
2872 "Die temp ok for USB charger thermal protection threshold\n");
2873 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2874
2875 return IRQ_HANDLED;
2876}
2877
2878/**
2879 * ab8500_charger_usbchargernotokr_handler() - USB charger not ok detected
2880 * @irq: interrupt number
2881 * @_di: pointer to the ab8500_charger structure
2882 *
2883 * Returns IRQ status(IRQ_HANDLED)
2884 */
2885static irqreturn_t ab8500_charger_usbchargernotokr_handler(int irq, void *_di)
2886{
2887 struct ab8500_charger *di = _di;
2888
2889 dev_dbg(di->dev, "Not allowed USB charger detected\n");
2890 queue_delayed_work(di->charger_wq, &di->check_usbchgnotok_work, 0);
2891
2892 return IRQ_HANDLED;
2893}
2894
2895/**
2896 * ab8500_charger_chwdexp_handler() - Charger watchdog expired
2897 * @irq: interrupt number
2898 * @_di: pointer to the ab8500_charger structure
2899 *
2900 * Returns IRQ status(IRQ_HANDLED)
2901 */
2902static irqreturn_t ab8500_charger_chwdexp_handler(int irq, void *_di)
2903{
2904 struct ab8500_charger *di = _di;
2905
2906 dev_dbg(di->dev, "Charger watchdog expired\n");
2907
2908 /*
2909 * The charger that was online when the watchdog expired
2910 * needs to be restarted for charging to start again
2911 */
2912 if (di->ac.charger_online) {
2913 di->ac.wd_expired = true;
2914 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2915 }
2916 if (di->usb.charger_online) {
2917 di->usb.wd_expired = true;
2918 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2919 }
2920
2921 return IRQ_HANDLED;
2922}
2923
2924/**
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002925 * ab8500_charger_vbuschdropend_handler() - VBUS drop removed
2926 * @irq: interrupt number
2927 * @_di: pointer to the ab8500_charger structure
2928 *
2929 * Returns IRQ status(IRQ_HANDLED)
2930 */
2931static irqreturn_t ab8500_charger_vbuschdropend_handler(int irq, void *_di)
2932{
2933 struct ab8500_charger *di = _di;
2934
2935 dev_dbg(di->dev, "VBUS charger drop ended\n");
2936 di->flags.vbus_drop_end = true;
Lee Jonesf7470b52013-02-14 12:37:29 +00002937
2938 /*
2939 * VBUS might have dropped due to bad connection.
2940 * Schedule a new input limit set to the value SW requests.
2941 */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002942 queue_delayed_work(di->charger_wq, &di->vbus_drop_end_work,
Lee Jonesf7470b52013-02-14 12:37:29 +00002943 round_jiffies(VBUS_IN_CURR_LIM_RETRY_SET_TIME * HZ));
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002944
2945 return IRQ_HANDLED;
2946}
2947
2948/**
Arun Murthy84edbee2012-02-29 21:54:26 +05302949 * ab8500_charger_vbusovv_handler() - VBUS overvoltage detected
2950 * @irq: interrupt number
2951 * @_di: pointer to the ab8500_charger structure
2952 *
2953 * Returns IRQ status(IRQ_HANDLED)
2954 */
2955static irqreturn_t ab8500_charger_vbusovv_handler(int irq, void *_di)
2956{
2957 struct ab8500_charger *di = _di;
2958
2959 dev_dbg(di->dev, "VBUS overvoltage detected\n");
2960 di->flags.vbus_ovv = true;
2961 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2962
2963 /* Schedule a new HW failure check */
2964 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
2965
2966 return IRQ_HANDLED;
2967}
2968
2969/**
2970 * ab8500_charger_ac_get_property() - get the ac/mains properties
2971 * @psy: pointer to the power_supply structure
2972 * @psp: pointer to the power_supply_property structure
2973 * @val: pointer to the power_supply_propval union
2974 *
2975 * This function gets called when an application tries to get the ac/mains
2976 * properties by reading the sysfs files.
2977 * AC/Mains properties are online, present and voltage.
2978 * online: ac/mains charging is in progress or not
2979 * present: presence of the ac/mains
2980 * voltage: AC/Mains voltage
2981 * Returns error code in case of failure else 0(on success)
2982 */
2983static int ab8500_charger_ac_get_property(struct power_supply *psy,
2984 enum power_supply_property psp,
2985 union power_supply_propval *val)
2986{
2987 struct ab8500_charger *di;
Jonas Aaberga864c5a2013-01-11 13:12:53 +00002988 int ret;
Arun Murthy84edbee2012-02-29 21:54:26 +05302989
2990 di = to_ab8500_charger_ac_device_info(psy_to_ux500_charger(psy));
2991
2992 switch (psp) {
2993 case POWER_SUPPLY_PROP_HEALTH:
2994 if (di->flags.mainextchnotok)
2995 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
2996 else if (di->ac.wd_expired || di->usb.wd_expired)
2997 val->intval = POWER_SUPPLY_HEALTH_DEAD;
2998 else if (di->flags.main_thermal_prot)
2999 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
3000 else
3001 val->intval = POWER_SUPPLY_HEALTH_GOOD;
3002 break;
3003 case POWER_SUPPLY_PROP_ONLINE:
3004 val->intval = di->ac.charger_online;
3005 break;
3006 case POWER_SUPPLY_PROP_PRESENT:
3007 val->intval = di->ac.charger_connected;
3008 break;
3009 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Jonas Aaberga864c5a2013-01-11 13:12:53 +00003010 ret = ab8500_charger_get_ac_voltage(di);
3011 if (ret >= 0)
3012 di->ac.charger_voltage = ret;
3013 /* On error, use previous value */
Arun Murthy84edbee2012-02-29 21:54:26 +05303014 val->intval = di->ac.charger_voltage * 1000;
3015 break;
3016 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
3017 /*
3018 * This property is used to indicate when CV mode is entered
3019 * for the AC charger
3020 */
3021 di->ac.cv_active = ab8500_charger_ac_cv(di);
3022 val->intval = di->ac.cv_active;
3023 break;
3024 case POWER_SUPPLY_PROP_CURRENT_NOW:
Jonas Aaberga864c5a2013-01-11 13:12:53 +00003025 ret = ab8500_charger_get_ac_current(di);
3026 if (ret >= 0)
3027 di->ac.charger_current = ret;
3028 val->intval = di->ac.charger_current * 1000;
Arun Murthy84edbee2012-02-29 21:54:26 +05303029 break;
3030 default:
3031 return -EINVAL;
3032 }
3033 return 0;
3034}
3035
3036/**
3037 * ab8500_charger_usb_get_property() - get the usb properties
3038 * @psy: pointer to the power_supply structure
3039 * @psp: pointer to the power_supply_property structure
3040 * @val: pointer to the power_supply_propval union
3041 *
3042 * This function gets called when an application tries to get the usb
3043 * properties by reading the sysfs files.
3044 * USB properties are online, present and voltage.
3045 * online: usb charging is in progress or not
3046 * present: presence of the usb
3047 * voltage: vbus voltage
3048 * Returns error code in case of failure else 0(on success)
3049 */
3050static int ab8500_charger_usb_get_property(struct power_supply *psy,
3051 enum power_supply_property psp,
3052 union power_supply_propval *val)
3053{
3054 struct ab8500_charger *di;
Jonas Aaberga864c5a2013-01-11 13:12:53 +00003055 int ret;
Arun Murthy84edbee2012-02-29 21:54:26 +05303056
3057 di = to_ab8500_charger_usb_device_info(psy_to_ux500_charger(psy));
3058
3059 switch (psp) {
3060 case POWER_SUPPLY_PROP_HEALTH:
3061 if (di->flags.usbchargernotok)
3062 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
3063 else if (di->ac.wd_expired || di->usb.wd_expired)
3064 val->intval = POWER_SUPPLY_HEALTH_DEAD;
3065 else if (di->flags.usb_thermal_prot)
3066 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
3067 else if (di->flags.vbus_ovv)
3068 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
3069 else
3070 val->intval = POWER_SUPPLY_HEALTH_GOOD;
3071 break;
3072 case POWER_SUPPLY_PROP_ONLINE:
3073 val->intval = di->usb.charger_online;
3074 break;
3075 case POWER_SUPPLY_PROP_PRESENT:
3076 val->intval = di->usb.charger_connected;
3077 break;
3078 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Jonas Aaberga864c5a2013-01-11 13:12:53 +00003079 ret = ab8500_charger_get_vbus_voltage(di);
3080 if (ret >= 0)
3081 di->usb.charger_voltage = ret;
Arun Murthy84edbee2012-02-29 21:54:26 +05303082 val->intval = di->usb.charger_voltage * 1000;
3083 break;
3084 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
3085 /*
3086 * This property is used to indicate when CV mode is entered
3087 * for the USB charger
3088 */
3089 di->usb.cv_active = ab8500_charger_usb_cv(di);
3090 val->intval = di->usb.cv_active;
3091 break;
3092 case POWER_SUPPLY_PROP_CURRENT_NOW:
Jonas Aaberga864c5a2013-01-11 13:12:53 +00003093 ret = ab8500_charger_get_usb_current(di);
3094 if (ret >= 0)
3095 di->usb.charger_current = ret;
3096 val->intval = di->usb.charger_current * 1000;
Arun Murthy84edbee2012-02-29 21:54:26 +05303097 break;
3098 case POWER_SUPPLY_PROP_CURRENT_AVG:
3099 /*
3100 * This property is used to indicate when VBUS has collapsed
3101 * due to too high output current from the USB charger
3102 */
3103 if (di->flags.vbus_collapse)
3104 val->intval = 1;
3105 else
3106 val->intval = 0;
3107 break;
3108 default:
3109 return -EINVAL;
3110 }
3111 return 0;
3112}
3113
3114/**
3115 * ab8500_charger_init_hw_registers() - Set up charger related registers
3116 * @di: pointer to the ab8500_charger structure
3117 *
3118 * Set up charger OVV, watchdog and maximum voltage registers as well as
3119 * charging of the backup battery
3120 */
3121static int ab8500_charger_init_hw_registers(struct ab8500_charger *di)
3122{
3123 int ret = 0;
Yang QU0f4aa402012-06-26 19:25:52 +08003124 u8 bup_vch_range = 0, vbup33_vrtcn = 0;
Arun Murthy84edbee2012-02-29 21:54:26 +05303125
3126 /* Setup maximum charger current and voltage for ABB cut2.0 */
3127 if (!is_ab8500_1p1_or_earlier(di->parent)) {
3128 ret = abx500_set_register_interruptible(di->dev,
3129 AB8500_CHARGER,
3130 AB8500_CH_VOLT_LVL_MAX_REG, CH_VOL_LVL_4P6);
3131 if (ret) {
3132 dev_err(di->dev,
3133 "failed to set CH_VOLT_LVL_MAX_REG\n");
3134 goto out;
3135 }
3136
3137 ret = abx500_set_register_interruptible(di->dev,
3138 AB8500_CHARGER,
3139 AB8500_CH_OPT_CRNTLVL_MAX_REG, CH_OP_CUR_LVL_1P6);
3140 if (ret) {
3141 dev_err(di->dev,
3142 "failed to set CH_OPT_CRNTLVL_MAX_REG\n");
3143 goto out;
3144 }
3145 }
3146
Jonas Aaberg0ed51072012-05-11 12:42:25 +02003147 if (is_ab9540_2p0(di->parent) || is_ab8505_2p0(di->parent))
3148 ret = abx500_mask_and_set_register_interruptible(di->dev,
3149 AB8500_CHARGER,
3150 AB8500_USBCH_CTRL2_REG,
3151 VBUS_AUTO_IN_CURR_LIM_ENA,
3152 VBUS_AUTO_IN_CURR_LIM_ENA);
3153 else
3154 /*
3155 * VBUS OVV set to 6.3V and enable automatic current limitation
3156 */
3157 ret = abx500_set_register_interruptible(di->dev,
3158 AB8500_CHARGER,
3159 AB8500_USBCH_CTRL2_REG,
3160 VBUS_OVV_SELECT_6P3V | VBUS_AUTO_IN_CURR_LIM_ENA);
Arun Murthy84edbee2012-02-29 21:54:26 +05303161 if (ret) {
Jonas Aaberg0ed51072012-05-11 12:42:25 +02003162 dev_err(di->dev,
3163 "failed to set automatic current limitation\n");
Arun Murthy84edbee2012-02-29 21:54:26 +05303164 goto out;
3165 }
3166
3167 /* Enable main watchdog in OTP */
3168 ret = abx500_set_register_interruptible(di->dev,
3169 AB8500_OTP_EMUL, AB8500_OTP_CONF_15, OTP_ENABLE_WD);
3170 if (ret) {
3171 dev_err(di->dev, "failed to enable main WD in OTP\n");
3172 goto out;
3173 }
3174
3175 /* Enable main watchdog */
3176 ret = abx500_set_register_interruptible(di->dev,
3177 AB8500_SYS_CTRL2_BLOCK,
3178 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_ENA);
3179 if (ret) {
3180 dev_err(di->dev, "faile to enable main watchdog\n");
3181 goto out;
3182 }
3183
3184 /*
3185 * Due to internal synchronisation, Enable and Kick watchdog bits
3186 * cannot be enabled in a single write.
3187 * A minimum delay of 2*32 kHz period (62.5µs) must be inserted
3188 * between writing Enable then Kick bits.
3189 */
3190 udelay(63);
3191
3192 /* Kick main watchdog */
3193 ret = abx500_set_register_interruptible(di->dev,
3194 AB8500_SYS_CTRL2_BLOCK,
3195 AB8500_MAIN_WDOG_CTRL_REG,
3196 (MAIN_WDOG_ENA | MAIN_WDOG_KICK));
3197 if (ret) {
3198 dev_err(di->dev, "failed to kick main watchdog\n");
3199 goto out;
3200 }
3201
3202 /* Disable main watchdog */
3203 ret = abx500_set_register_interruptible(di->dev,
3204 AB8500_SYS_CTRL2_BLOCK,
3205 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_DIS);
3206 if (ret) {
3207 dev_err(di->dev, "failed to disable main watchdog\n");
3208 goto out;
3209 }
3210
3211 /* Set watchdog timeout */
3212 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
3213 AB8500_CH_WD_TIMER_REG, WD_TIMER);
3214 if (ret) {
3215 dev_err(di->dev, "failed to set charger watchdog timeout\n");
3216 goto out;
3217 }
3218
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003219 /* Set charger watchdog timeout */
3220 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
3221 AB8500_CH_WD_TIMER_REG, WD_TIMER);
3222 if (ret) {
3223 dev_err(di->dev, "failed to set charger watchdog timeout\n");
3224 goto out;
3225 }
3226
3227 ret = ab8500_charger_led_en(di, false);
3228 if (ret < 0) {
3229 dev_err(di->dev, "failed to disable LED\n");
3230 goto out;
3231 }
3232
Arun Murthy84edbee2012-02-29 21:54:26 +05303233 /* Backup battery voltage and current */
Yang QU0f4aa402012-06-26 19:25:52 +08003234 if (di->bm->bkup_bat_v > BUP_VCH_SEL_3P1V)
3235 bup_vch_range = BUP_VCH_RANGE;
3236 if (di->bm->bkup_bat_v == BUP_VCH_SEL_3P3V)
3237 vbup33_vrtcn = VBUP33_VRTCN;
3238
Arun Murthy84edbee2012-02-29 21:54:26 +05303239 ret = abx500_set_register_interruptible(di->dev,
3240 AB8500_RTC,
3241 AB8500_RTC_BACKUP_CHG_REG,
Yang QU0f4aa402012-06-26 19:25:52 +08003242 (di->bm->bkup_bat_v & 0x3) | di->bm->bkup_bat_i);
Arun Murthy84edbee2012-02-29 21:54:26 +05303243 if (ret) {
3244 dev_err(di->dev, "failed to setup backup battery charging\n");
3245 goto out;
3246 }
Yang QU0f4aa402012-06-26 19:25:52 +08003247 if (is_ab8540(di->parent)) {
3248 ret = abx500_set_register_interruptible(di->dev,
3249 AB8500_RTC,
3250 AB8500_RTC_CTRL1_REG,
3251 bup_vch_range | vbup33_vrtcn);
3252 if (ret) {
3253 dev_err(di->dev, "failed to setup backup battery charging\n");
3254 goto out;
3255 }
3256 }
Arun Murthy84edbee2012-02-29 21:54:26 +05303257
3258 /* Enable backup battery charging */
3259 abx500_mask_and_set_register_interruptible(di->dev,
3260 AB8500_RTC, AB8500_RTC_CTRL_REG,
3261 RTC_BUP_CH_ENA, RTC_BUP_CH_ENA);
3262 if (ret < 0)
3263 dev_err(di->dev, "%s mask and set failed\n", __func__);
3264
Lee Jonesdb43e6c2013-02-14 12:39:15 +00003265 if (is_ab8540(di->parent)) {
3266 ret = abx500_mask_and_set_register_interruptible(di->dev,
3267 AB8500_CHARGER, AB8540_USB_PP_MODE_REG,
3268 BUS_VSYS_VOL_SELECT_MASK, BUS_VSYS_VOL_SELECT_3P6V);
3269 if (ret) {
3270 dev_err(di->dev, "failed to setup usb power path vsys voltage\n");
3271 goto out;
3272 }
3273 ret = abx500_mask_and_set_register_interruptible(di->dev,
3274 AB8500_CHARGER, AB8540_USB_PP_CHR_REG,
3275 BUS_PP_PRECHG_CURRENT_MASK, 0);
3276 if (ret) {
3277 dev_err(di->dev, "failed to setup usb power path prechage current\n");
3278 goto out;
3279 }
3280 }
3281
Arun Murthy84edbee2012-02-29 21:54:26 +05303282out:
3283 return ret;
3284}
3285
3286/*
3287 * ab8500 charger driver interrupts and their respective isr
3288 */
3289static struct ab8500_charger_interrupts ab8500_charger_irq[] = {
3290 {"MAIN_CH_UNPLUG_DET", ab8500_charger_mainchunplugdet_handler},
3291 {"MAIN_CHARGE_PLUG_DET", ab8500_charger_mainchplugdet_handler},
3292 {"MAIN_EXT_CH_NOT_OK", ab8500_charger_mainextchnotok_handler},
3293 {"MAIN_CH_TH_PROT_R", ab8500_charger_mainchthprotr_handler},
3294 {"MAIN_CH_TH_PROT_F", ab8500_charger_mainchthprotf_handler},
3295 {"VBUS_DET_F", ab8500_charger_vbusdetf_handler},
3296 {"VBUS_DET_R", ab8500_charger_vbusdetr_handler},
3297 {"USB_LINK_STATUS", ab8500_charger_usblinkstatus_handler},
3298 {"USB_CH_TH_PROT_R", ab8500_charger_usbchthprotr_handler},
3299 {"USB_CH_TH_PROT_F", ab8500_charger_usbchthprotf_handler},
3300 {"USB_CHARGER_NOT_OKR", ab8500_charger_usbchargernotokr_handler},
3301 {"VBUS_OVV", ab8500_charger_vbusovv_handler},
3302 {"CH_WD_EXP", ab8500_charger_chwdexp_handler},
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003303 {"VBUS_CH_DROP_END", ab8500_charger_vbuschdropend_handler},
Arun Murthy84edbee2012-02-29 21:54:26 +05303304};
3305
3306static int ab8500_charger_usb_notifier_call(struct notifier_block *nb,
3307 unsigned long event, void *power)
3308{
3309 struct ab8500_charger *di =
3310 container_of(nb, struct ab8500_charger, nb);
3311 enum ab8500_usb_state bm_usb_state;
3312 unsigned mA = *((unsigned *)power);
3313
Lee Jonesc9ade0f2013-01-21 11:22:56 +00003314 if (!di)
3315 return NOTIFY_DONE;
3316
Arun Murthy84edbee2012-02-29 21:54:26 +05303317 if (event != USB_EVENT_VBUS) {
3318 dev_dbg(di->dev, "not a standard host, returning\n");
3319 return NOTIFY_DONE;
3320 }
3321
3322 /* TODO: State is fabricate here. See if charger really needs USB
3323 * state or if mA is enough
3324 */
3325 if ((di->usb_state.usb_current == 2) && (mA > 2))
3326 bm_usb_state = AB8500_BM_USB_STATE_RESUME;
3327 else if (mA == 0)
3328 bm_usb_state = AB8500_BM_USB_STATE_RESET_HS;
3329 else if (mA == 2)
3330 bm_usb_state = AB8500_BM_USB_STATE_SUSPEND;
3331 else if (mA >= 8) /* 8, 100, 500 */
3332 bm_usb_state = AB8500_BM_USB_STATE_CONFIGURED;
3333 else /* Should never occur */
3334 bm_usb_state = AB8500_BM_USB_STATE_RESET_FS;
3335
3336 dev_dbg(di->dev, "%s usb_state: 0x%02x mA: %d\n",
3337 __func__, bm_usb_state, mA);
3338
3339 spin_lock(&di->usb_state.usb_lock);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003340 di->usb_state.state_tmp = bm_usb_state;
3341 di->usb_state.usb_current_tmp = mA;
Arun Murthy84edbee2012-02-29 21:54:26 +05303342 spin_unlock(&di->usb_state.usb_lock);
3343
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003344 /*
3345 * wait for some time until you get updates from the usb stack
3346 * and negotiations are completed
3347 */
3348 queue_delayed_work(di->charger_wq, &di->usb_state_changed_work, HZ/2);
Arun Murthy84edbee2012-02-29 21:54:26 +05303349
3350 return NOTIFY_OK;
3351}
3352
3353#if defined(CONFIG_PM)
3354static int ab8500_charger_resume(struct platform_device *pdev)
3355{
3356 int ret;
3357 struct ab8500_charger *di = platform_get_drvdata(pdev);
3358
3359 /*
3360 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
3361 * logic. That means we have to continously kick the charger
3362 * watchdog even when no charger is connected. This is only
3363 * valid once the AC charger has been enabled. This is
3364 * a bug that is not handled by the algorithm and the
3365 * watchdog have to be kicked by the charger driver
3366 * when the AC charger is disabled
3367 */
3368 if (di->ac_conn && is_ab8500_1p1_or_earlier(di->parent)) {
3369 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
3370 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
3371 if (ret)
3372 dev_err(di->dev, "Failed to kick WD!\n");
3373
3374 /* If not already pending start a new timer */
3375 if (!delayed_work_pending(
3376 &di->kick_wd_work)) {
3377 queue_delayed_work(di->charger_wq, &di->kick_wd_work,
3378 round_jiffies(WD_KICK_INTERVAL));
3379 }
3380 }
3381
3382 /* If we still have a HW failure, schedule a new check */
3383 if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
3384 queue_delayed_work(di->charger_wq,
3385 &di->check_hw_failure_work, 0);
3386 }
3387
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003388 if (di->flags.vbus_drop_end)
3389 queue_delayed_work(di->charger_wq, &di->vbus_drop_end_work, 0);
3390
Arun Murthy84edbee2012-02-29 21:54:26 +05303391 return 0;
3392}
3393
3394static int ab8500_charger_suspend(struct platform_device *pdev,
3395 pm_message_t state)
3396{
3397 struct ab8500_charger *di = platform_get_drvdata(pdev);
3398
3399 /* Cancel any pending HW failure check */
3400 if (delayed_work_pending(&di->check_hw_failure_work))
3401 cancel_delayed_work(&di->check_hw_failure_work);
3402
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003403 if (delayed_work_pending(&di->vbus_drop_end_work))
3404 cancel_delayed_work(&di->vbus_drop_end_work);
3405
Jonas Aaberg53ef1f52012-05-21 16:05:01 +02003406 flush_delayed_work(&di->attach_work);
3407 flush_delayed_work(&di->usb_charger_attached_work);
3408 flush_delayed_work(&di->ac_charger_attached_work);
3409 flush_delayed_work(&di->check_usbchgnotok_work);
3410 flush_delayed_work(&di->check_vbat_work);
3411 flush_delayed_work(&di->kick_wd_work);
3412
3413 flush_work(&di->usb_link_status_work);
3414 flush_work(&di->ac_work);
3415 flush_work(&di->detect_usb_type_work);
3416
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003417 if (atomic_read(&di->current_stepping_sessions))
3418 return -EAGAIN;
3419
Arun Murthy84edbee2012-02-29 21:54:26 +05303420 return 0;
3421}
3422#else
3423#define ab8500_charger_suspend NULL
3424#define ab8500_charger_resume NULL
3425#endif
3426
Lee Jones88917162013-02-13 11:39:19 +00003427static struct notifier_block charger_nb = {
3428 .notifier_call = ab8500_external_charger_prepare,
3429};
3430
Bill Pemberton415ec692012-11-19 13:26:07 -05003431static int ab8500_charger_remove(struct platform_device *pdev)
Arun Murthy84edbee2012-02-29 21:54:26 +05303432{
3433 struct ab8500_charger *di = platform_get_drvdata(pdev);
3434 int i, irq, ret;
3435
3436 /* Disable AC charging */
3437 ab8500_charger_ac_en(&di->ac_chg, false, 0, 0);
3438
3439 /* Disable USB charging */
3440 ab8500_charger_usb_en(&di->usb_chg, false, 0, 0);
3441
3442 /* Disable interrupts */
3443 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
3444 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3445 free_irq(irq, di);
3446 }
3447
Arun Murthy84edbee2012-02-29 21:54:26 +05303448 /* Backup battery voltage and current disable */
3449 ret = abx500_mask_and_set_register_interruptible(di->dev,
3450 AB8500_RTC, AB8500_RTC_CTRL_REG, RTC_BUP_CH_ENA, 0);
3451 if (ret < 0)
3452 dev_err(di->dev, "%s mask and set failed\n", __func__);
3453
Anton Vorontsovefd71c82012-03-14 04:22:17 +04003454 usb_unregister_notifier(di->usb_phy, &di->nb);
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +05303455 usb_put_phy(di->usb_phy);
Arun Murthy84edbee2012-02-29 21:54:26 +05303456
3457 /* Delete the work queue */
3458 destroy_workqueue(di->charger_wq);
3459
Lee Jones88917162013-02-13 11:39:19 +00003460 /* Unregister external charger enable notifier */
3461 if (!di->ac_chg.enabled)
3462 blocking_notifier_chain_unregister(
3463 &charger_notifier_list, &charger_nb);
3464
Arun Murthy84edbee2012-02-29 21:54:26 +05303465 flush_scheduled_work();
Lee Jones72a90dd2013-02-14 09:22:48 +00003466 if (di->usb_chg.enabled)
Michel JAOUEN01ec8c52012-04-26 10:00:04 +02003467 power_supply_unregister(&di->usb_chg.psy);
Marcus Cooper405fea12012-08-10 12:59:06 +02003468
3469 if (di->ac_chg.enabled && !di->ac_chg.external)
Michel JAOUEN01ec8c52012-04-26 10:00:04 +02003470 power_supply_unregister(&di->ac_chg.psy);
Marcus Cooper405fea12012-08-10 12:59:06 +02003471
Arun Murthy84edbee2012-02-29 21:54:26 +05303472 platform_set_drvdata(pdev, NULL);
Arun Murthy84edbee2012-02-29 21:54:26 +05303473
3474 return 0;
3475}
3476
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08003477static char *supply_interface[] = {
3478 "ab8500_chargalg",
3479 "ab8500_fg",
3480 "ab8500_btemp",
3481};
3482
Bill Pembertonc8afa642012-11-19 13:22:23 -05003483static int ab8500_charger_probe(struct platform_device *pdev)
Arun Murthy84edbee2012-02-29 21:54:26 +05303484{
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08003485 struct device_node *np = pdev->dev.of_node;
Lee Jones7722b792012-11-30 10:56:28 +00003486 struct abx500_bm_data *plat = pdev->dev.platform_data;
Lee Jones2aac3de2012-05-05 04:38:19 -07003487 struct ab8500_charger *di;
Lee Jonesb269fff2013-01-11 13:12:51 +00003488 int irq, i, charger_status, ret = 0, ch_stat;
Arun Murthy84edbee2012-02-29 21:54:26 +05303489
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08003490 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
3491 if (!di) {
3492 dev_err(&pdev->dev, "%s no mem for ab8500_charger\n", __func__);
Arun Murthy84edbee2012-02-29 21:54:26 +05303493 return -ENOMEM;
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08003494 }
Lee Jones7722b792012-11-30 10:56:28 +00003495
3496 if (!plat) {
3497 dev_err(&pdev->dev, "no battery management data supplied\n");
3498 return -EINVAL;
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08003499 }
Lee Jones7722b792012-11-30 10:56:28 +00003500 di->bm = plat;
3501
3502 if (np) {
3503 ret = ab8500_bm_of_probe(&pdev->dev, np, di->bm);
3504 if (ret) {
3505 dev_err(&pdev->dev, "failed to get battery information\n");
3506 return ret;
3507 }
3508 di->autopower_cfg = of_property_read_bool(np, "autopower_cfg");
3509 } else
3510 di->autopower_cfg = false;
Arun Murthy84edbee2012-02-29 21:54:26 +05303511
3512 /* get parent data */
3513 di->dev = &pdev->dev;
3514 di->parent = dev_get_drvdata(pdev->dev.parent);
3515 di->gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
3516
3517 /* initialize lock */
3518 spin_lock_init(&di->usb_state.usb_lock);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003519 mutex_init(&di->usb_ipt_crnt_lock);
Arun Murthy84edbee2012-02-29 21:54:26 +05303520
Arun Murthy84edbee2012-02-29 21:54:26 +05303521 di->autopower = false;
Henrik Sölverff380902012-04-17 15:51:01 +02003522 di->invalid_charger_detect_state = 0;
Arun Murthy84edbee2012-02-29 21:54:26 +05303523
3524 /* AC supply */
3525 /* power_supply base class */
3526 di->ac_chg.psy.name = "ab8500_ac";
3527 di->ac_chg.psy.type = POWER_SUPPLY_TYPE_MAINS;
3528 di->ac_chg.psy.properties = ab8500_charger_ac_props;
3529 di->ac_chg.psy.num_properties = ARRAY_SIZE(ab8500_charger_ac_props);
3530 di->ac_chg.psy.get_property = ab8500_charger_ac_get_property;
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08003531 di->ac_chg.psy.supplied_to = supply_interface;
3532 di->ac_chg.psy.num_supplicants = ARRAY_SIZE(supply_interface),
Arun Murthy84edbee2012-02-29 21:54:26 +05303533 /* ux500_charger sub-class */
3534 di->ac_chg.ops.enable = &ab8500_charger_ac_en;
Lee Jones4dcdf572013-02-14 09:24:10 +00003535 di->ac_chg.ops.check_enable = &ab8500_charger_ac_check_enable;
Arun Murthy84edbee2012-02-29 21:54:26 +05303536 di->ac_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
3537 di->ac_chg.ops.update_curr = &ab8500_charger_update_charger_current;
3538 di->ac_chg.max_out_volt = ab8500_charger_voltage_map[
3539 ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
3540 di->ac_chg.max_out_curr = ab8500_charger_current_map[
3541 ARRAY_SIZE(ab8500_charger_current_map) - 1];
Loic Pallardye07a5642012-05-10 15:33:56 +02003542 di->ac_chg.wdt_refresh = CHG_WD_INTERVAL;
Lee Jones97034a12013-01-17 16:08:42 +00003543 di->ac_chg.enabled = di->bm->ac_enabled;
Loic Pallardye07a5642012-05-10 15:33:56 +02003544 di->ac_chg.external = false;
Arun Murthy84edbee2012-02-29 21:54:26 +05303545
Lee Jones88917162013-02-13 11:39:19 +00003546 /*notifier for external charger enabling*/
3547 if (!di->ac_chg.enabled)
3548 blocking_notifier_chain_register(
3549 &charger_notifier_list, &charger_nb);
3550
Arun Murthy84edbee2012-02-29 21:54:26 +05303551 /* USB supply */
3552 /* power_supply base class */
3553 di->usb_chg.psy.name = "ab8500_usb";
3554 di->usb_chg.psy.type = POWER_SUPPLY_TYPE_USB;
3555 di->usb_chg.psy.properties = ab8500_charger_usb_props;
3556 di->usb_chg.psy.num_properties = ARRAY_SIZE(ab8500_charger_usb_props);
3557 di->usb_chg.psy.get_property = ab8500_charger_usb_get_property;
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08003558 di->usb_chg.psy.supplied_to = supply_interface;
3559 di->usb_chg.psy.num_supplicants = ARRAY_SIZE(supply_interface),
Arun Murthy84edbee2012-02-29 21:54:26 +05303560 /* ux500_charger sub-class */
3561 di->usb_chg.ops.enable = &ab8500_charger_usb_en;
Lee Jones4dcdf572013-02-14 09:24:10 +00003562 di->usb_chg.ops.check_enable = &ab8500_charger_usb_check_enable;
Arun Murthy84edbee2012-02-29 21:54:26 +05303563 di->usb_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
3564 di->usb_chg.ops.update_curr = &ab8500_charger_update_charger_current;
Lee Jonesdb43e6c2013-02-14 12:39:15 +00003565 di->usb_chg.ops.pp_enable = &ab8540_charger_power_path_enable;
3566 di->usb_chg.ops.pre_chg_enable = &ab8540_charger_usb_pre_chg_enable;
Arun Murthy84edbee2012-02-29 21:54:26 +05303567 di->usb_chg.max_out_volt = ab8500_charger_voltage_map[
3568 ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
3569 di->usb_chg.max_out_curr = ab8500_charger_current_map[
3570 ARRAY_SIZE(ab8500_charger_current_map) - 1];
Loic Pallardye07a5642012-05-10 15:33:56 +02003571 di->usb_chg.wdt_refresh = CHG_WD_INTERVAL;
Lee Jones97034a12013-01-17 16:08:42 +00003572 di->usb_chg.enabled = di->bm->usb_enabled;
Loic Pallardye07a5642012-05-10 15:33:56 +02003573 di->usb_chg.external = false;
Lee Jonesdb43e6c2013-02-14 12:39:15 +00003574 di->usb_chg.power_path = di->bm->usb_power_path;
Lee Jonesf7470b52013-02-14 12:37:29 +00003575 di->usb_state.usb_current = -1;
Arun Murthy84edbee2012-02-29 21:54:26 +05303576
3577 /* Create a work queue for the charger */
3578 di->charger_wq =
3579 create_singlethread_workqueue("ab8500_charger_wq");
3580 if (di->charger_wq == NULL) {
3581 dev_err(di->dev, "failed to create work queue\n");
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08003582 return -ENOMEM;
Arun Murthy84edbee2012-02-29 21:54:26 +05303583 }
3584
Lee Jonesb269fff2013-01-11 13:12:51 +00003585 mutex_init(&di->charger_attached_mutex);
3586
Arun Murthy84edbee2012-02-29 21:54:26 +05303587 /* Init work for HW failure check */
Tejun Heo203b42f2012-08-21 13:18:23 -07003588 INIT_DEFERRABLE_WORK(&di->check_hw_failure_work,
Arun Murthy84edbee2012-02-29 21:54:26 +05303589 ab8500_charger_check_hw_failure_work);
Tejun Heo203b42f2012-08-21 13:18:23 -07003590 INIT_DEFERRABLE_WORK(&di->check_usbchgnotok_work,
Arun Murthy84edbee2012-02-29 21:54:26 +05303591 ab8500_charger_check_usbchargernotok_work);
3592
Lee Jonesb269fff2013-01-11 13:12:51 +00003593 INIT_DELAYED_WORK(&di->ac_charger_attached_work,
3594 ab8500_charger_ac_attached_work);
3595 INIT_DELAYED_WORK(&di->usb_charger_attached_work,
3596 ab8500_charger_usb_attached_work);
3597
Arun Murthy84edbee2012-02-29 21:54:26 +05303598 /*
3599 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
3600 * logic. That means we have to continously kick the charger
3601 * watchdog even when no charger is connected. This is only
3602 * valid once the AC charger has been enabled. This is
3603 * a bug that is not handled by the algorithm and the
3604 * watchdog have to be kicked by the charger driver
3605 * when the AC charger is disabled
3606 */
Tejun Heo203b42f2012-08-21 13:18:23 -07003607 INIT_DEFERRABLE_WORK(&di->kick_wd_work,
Arun Murthy84edbee2012-02-29 21:54:26 +05303608 ab8500_charger_kick_watchdog_work);
3609
Tejun Heo203b42f2012-08-21 13:18:23 -07003610 INIT_DEFERRABLE_WORK(&di->check_vbat_work,
Arun Murthy84edbee2012-02-29 21:54:26 +05303611 ab8500_charger_check_vbat_work);
3612
Marcus Cooper4b45f4a2013-01-11 13:13:04 +00003613 INIT_DELAYED_WORK(&di->attach_work,
3614 ab8500_charger_usb_link_attach_work);
3615
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003616 INIT_DELAYED_WORK(&di->usb_state_changed_work,
3617 ab8500_charger_usb_state_changed_work);
3618
3619 INIT_DELAYED_WORK(&di->vbus_drop_end_work,
3620 ab8500_charger_vbus_drop_end_work);
3621
Arun Murthy84edbee2012-02-29 21:54:26 +05303622 /* Init work for charger detection */
3623 INIT_WORK(&di->usb_link_status_work,
3624 ab8500_charger_usb_link_status_work);
3625 INIT_WORK(&di->ac_work, ab8500_charger_ac_work);
3626 INIT_WORK(&di->detect_usb_type_work,
3627 ab8500_charger_detect_usb_type_work);
3628
Arun Murthy84edbee2012-02-29 21:54:26 +05303629 /* Init work for checking HW status */
3630 INIT_WORK(&di->check_main_thermal_prot_work,
3631 ab8500_charger_check_main_thermal_prot_work);
3632 INIT_WORK(&di->check_usb_thermal_prot_work,
3633 ab8500_charger_check_usb_thermal_prot_work);
3634
3635 /*
3636 * VDD ADC supply needs to be enabled from this driver when there
3637 * is a charger connected to avoid erroneous BTEMP_HIGH/LOW
3638 * interrupts during charging
3639 */
Sachin Kamat8feffd12012-12-07 17:23:28 +05303640 di->regu = devm_regulator_get(di->dev, "vddadc");
Arun Murthy84edbee2012-02-29 21:54:26 +05303641 if (IS_ERR(di->regu)) {
3642 ret = PTR_ERR(di->regu);
3643 dev_err(di->dev, "failed to get vddadc regulator\n");
3644 goto free_charger_wq;
3645 }
3646
3647
3648 /* Initialize OVV, and other registers */
3649 ret = ab8500_charger_init_hw_registers(di);
3650 if (ret) {
3651 dev_err(di->dev, "failed to initialize ABB registers\n");
Sachin Kamat8feffd12012-12-07 17:23:28 +05303652 goto free_charger_wq;
Arun Murthy84edbee2012-02-29 21:54:26 +05303653 }
3654
3655 /* Register AC charger class */
Lee Jones72a90dd2013-02-14 09:22:48 +00003656 if (di->ac_chg.enabled) {
Michel JAOUEN01ec8c52012-04-26 10:00:04 +02003657 ret = power_supply_register(di->dev, &di->ac_chg.psy);
3658 if (ret) {
3659 dev_err(di->dev, "failed to register AC charger\n");
3660 goto free_charger_wq;
3661 }
Arun Murthy84edbee2012-02-29 21:54:26 +05303662 }
3663
3664 /* Register USB charger class */
Lee Jones72a90dd2013-02-14 09:22:48 +00003665 if (di->usb_chg.enabled) {
Michel JAOUEN01ec8c52012-04-26 10:00:04 +02003666 ret = power_supply_register(di->dev, &di->usb_chg.psy);
3667 if (ret) {
3668 dev_err(di->dev, "failed to register USB charger\n");
3669 goto free_ac;
3670 }
Arun Murthy84edbee2012-02-29 21:54:26 +05303671 }
3672
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +05303673 di->usb_phy = usb_get_phy(USB_PHY_TYPE_USB2);
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +05303674 if (IS_ERR_OR_NULL(di->usb_phy)) {
Anton Vorontsovefd71c82012-03-14 04:22:17 +04003675 dev_err(di->dev, "failed to get usb transceiver\n");
Arun Murthy84edbee2012-02-29 21:54:26 +05303676 ret = -EINVAL;
3677 goto free_usb;
3678 }
3679 di->nb.notifier_call = ab8500_charger_usb_notifier_call;
Anton Vorontsovefd71c82012-03-14 04:22:17 +04003680 ret = usb_register_notifier(di->usb_phy, &di->nb);
Arun Murthy84edbee2012-02-29 21:54:26 +05303681 if (ret) {
Anton Vorontsovefd71c82012-03-14 04:22:17 +04003682 dev_err(di->dev, "failed to register usb notifier\n");
3683 goto put_usb_phy;
Arun Murthy84edbee2012-02-29 21:54:26 +05303684 }
3685
3686 /* Identify the connected charger types during startup */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003687 charger_status = ab8500_charger_detect_chargers(di, true);
Arun Murthy84edbee2012-02-29 21:54:26 +05303688 if (charger_status & AC_PW_CONN) {
3689 di->ac.charger_connected = 1;
3690 di->ac_conn = true;
3691 ab8500_power_supply_changed(di, &di->ac_chg.psy);
3692 sysfs_notify(&di->ac_chg.psy.dev->kobj, NULL, "present");
3693 }
3694
3695 if (charger_status & USB_PW_CONN) {
Arun Murthy84edbee2012-02-29 21:54:26 +05303696 di->vbus_detected = true;
3697 di->vbus_detected_start = true;
3698 queue_work(di->charger_wq,
3699 &di->detect_usb_type_work);
3700 }
3701
3702 /* Register interrupts */
3703 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
3704 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3705 ret = request_threaded_irq(irq, NULL, ab8500_charger_irq[i].isr,
3706 IRQF_SHARED | IRQF_NO_SUSPEND,
3707 ab8500_charger_irq[i].name, di);
3708
3709 if (ret != 0) {
3710 dev_err(di->dev, "failed to request %s IRQ %d: %d\n"
3711 , ab8500_charger_irq[i].name, irq, ret);
3712 goto free_irq;
3713 }
3714 dev_dbg(di->dev, "Requested %s IRQ %d: %d\n",
3715 ab8500_charger_irq[i].name, irq, ret);
3716 }
3717
3718 platform_set_drvdata(pdev, di);
3719
Lee Jonesb269fff2013-01-11 13:12:51 +00003720 mutex_lock(&di->charger_attached_mutex);
3721
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003722 ch_stat = ab8500_charger_detect_chargers(di, false);
Lee Jonesb269fff2013-01-11 13:12:51 +00003723
3724 if ((ch_stat & AC_PW_CONN) == AC_PW_CONN) {
3725 queue_delayed_work(di->charger_wq,
3726 &di->ac_charger_attached_work,
3727 HZ);
3728 }
3729 if ((ch_stat & USB_PW_CONN) == USB_PW_CONN) {
3730 queue_delayed_work(di->charger_wq,
3731 &di->usb_charger_attached_work,
3732 HZ);
3733 }
3734
3735 mutex_unlock(&di->charger_attached_mutex);
3736
Arun Murthy84edbee2012-02-29 21:54:26 +05303737 return ret;
3738
3739free_irq:
Anton Vorontsovefd71c82012-03-14 04:22:17 +04003740 usb_unregister_notifier(di->usb_phy, &di->nb);
Arun Murthy84edbee2012-02-29 21:54:26 +05303741
3742 /* We also have to free all successfully registered irqs */
3743 for (i = i - 1; i >= 0; i--) {
3744 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3745 free_irq(irq, di);
3746 }
Anton Vorontsovefd71c82012-03-14 04:22:17 +04003747put_usb_phy:
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +05303748 usb_put_phy(di->usb_phy);
Arun Murthy84edbee2012-02-29 21:54:26 +05303749free_usb:
Lee Jones72a90dd2013-02-14 09:22:48 +00003750 if (di->usb_chg.enabled)
Michel JAOUEN01ec8c52012-04-26 10:00:04 +02003751 power_supply_unregister(&di->usb_chg.psy);
Arun Murthy84edbee2012-02-29 21:54:26 +05303752free_ac:
Lee Jones72a90dd2013-02-14 09:22:48 +00003753 if (di->ac_chg.enabled)
Michel JAOUEN01ec8c52012-04-26 10:00:04 +02003754 power_supply_unregister(&di->ac_chg.psy);
Arun Murthy84edbee2012-02-29 21:54:26 +05303755free_charger_wq:
3756 destroy_workqueue(di->charger_wq);
Arun Murthy84edbee2012-02-29 21:54:26 +05303757 return ret;
3758}
3759
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08003760static const struct of_device_id ab8500_charger_match[] = {
3761 { .compatible = "stericsson,ab8500-charger", },
3762 { },
3763};
3764
Arun Murthy84edbee2012-02-29 21:54:26 +05303765static struct platform_driver ab8500_charger_driver = {
3766 .probe = ab8500_charger_probe,
Bill Pemberton28ea73f2012-11-19 13:20:40 -05003767 .remove = ab8500_charger_remove,
Arun Murthy84edbee2012-02-29 21:54:26 +05303768 .suspend = ab8500_charger_suspend,
3769 .resume = ab8500_charger_resume,
3770 .driver = {
3771 .name = "ab8500-charger",
3772 .owner = THIS_MODULE,
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08003773 .of_match_table = ab8500_charger_match,
Arun Murthy84edbee2012-02-29 21:54:26 +05303774 },
3775};
3776
3777static int __init ab8500_charger_init(void)
3778{
3779 return platform_driver_register(&ab8500_charger_driver);
3780}
3781
3782static void __exit ab8500_charger_exit(void)
3783{
3784 platform_driver_unregister(&ab8500_charger_driver);
3785}
3786
3787subsys_initcall_sync(ab8500_charger_init);
3788module_exit(ab8500_charger_exit);
3789
3790MODULE_LICENSE("GPL v2");
3791MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy");
3792MODULE_ALIAS("platform:ab8500-charger");
3793MODULE_DESCRIPTION("AB8500 charger management driver");