blob: f1d712308b02762fd82325e660f0b9c6f60e5dd2 [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
Arun Murthy84edbee2012-02-29 21:54:26 +053061
62#define LED_INDICATOR_PWM_ENA 0x01
63#define LED_INDICATOR_PWM_DIS 0x00
64#define LED_IND_CUR_5MA 0x04
65#define LED_INDICATOR_PWM_DUTY_252_256 0xBF
66
67/* HW failure constants */
68#define MAIN_CH_TH_PROT 0x02
69#define VBUS_CH_NOK 0x08
70#define USB_CH_TH_PROT 0x02
71#define VBUS_OVV_TH 0x01
72#define MAIN_CH_NOK 0x01
73#define VBUS_DET 0x80
74
Lee Jonesb269fff2013-01-11 13:12:51 +000075#define MAIN_CH_STATUS2_MAINCHGDROP 0x80
76#define MAIN_CH_STATUS2_MAINCHARGERDETDBNC 0x40
77#define USB_CH_VBUSDROP 0x40
78#define USB_CH_VBUSDETDBNC 0x01
79
Arun Murthy84edbee2012-02-29 21:54:26 +053080/* UsbLineStatus register bit masks */
81#define AB8500_USB_LINK_STATUS 0x78
82#define AB8500_STD_HOST_SUSP 0x18
83
84/* Watchdog timeout constant */
85#define WD_TIMER 0x30 /* 4min */
86#define WD_KICK_INTERVAL (60 * HZ)
87
88/* Lowest charger voltage is 3.39V -> 0x4E */
89#define LOW_VOLT_REG 0x4E
90
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +010091/* Step up/down delay in us */
92#define STEP_UDELAY 1000
93
Lee Jonesb269fff2013-01-11 13:12:51 +000094#define CHARGER_STATUS_POLL 10 /* in ms */
95
Loic Pallardye07a5642012-05-10 15:33:56 +020096#define CHG_WD_INTERVAL (60 * HZ)
97
Nicolas Guionb0163222012-05-23 15:59:43 +020098#define AB8500_SW_CONTROL_FALLBACK 0x03
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +010099/* Wait for enumeration before charing in us */
100#define WAIT_ACA_RID_ENUMERATION (5 * 1000)
Lee Jones88917162013-02-13 11:39:19 +0000101/*External charger control*/
102#define AB8500_SYS_CHARGER_CONTROL_REG 0x52
103#define EXTERNAL_CHARGER_DISABLE_REG_VAL 0x03
104#define EXTERNAL_CHARGER_ENABLE_REG_VAL 0x07
Nicolas Guionb0163222012-05-23 15:59:43 +0200105
Arun Murthy84edbee2012-02-29 21:54:26 +0530106/* UsbLineStatus register - usb types */
107enum ab8500_charger_link_status {
108 USB_STAT_NOT_CONFIGURED,
109 USB_STAT_STD_HOST_NC,
110 USB_STAT_STD_HOST_C_NS,
111 USB_STAT_STD_HOST_C_S,
112 USB_STAT_HOST_CHG_NM,
113 USB_STAT_HOST_CHG_HS,
114 USB_STAT_HOST_CHG_HS_CHIRP,
115 USB_STAT_DEDICATED_CHG,
116 USB_STAT_ACA_RID_A,
117 USB_STAT_ACA_RID_B,
118 USB_STAT_ACA_RID_C_NM,
119 USB_STAT_ACA_RID_C_HS,
120 USB_STAT_ACA_RID_C_HS_CHIRP,
121 USB_STAT_HM_IDGND,
122 USB_STAT_RESERVED,
123 USB_STAT_NOT_VALID_LINK,
Hakan Berg74a8e342013-01-11 13:12:58 +0000124 USB_STAT_PHY_EN,
125 USB_STAT_SUP_NO_IDGND_VBUS,
126 USB_STAT_SUP_IDGND_VBUS,
127 USB_STAT_CHARGER_LINE_1,
128 USB_STAT_CARKIT_1,
129 USB_STAT_CARKIT_2,
130 USB_STAT_ACA_DOCK_CHARGER,
Arun Murthy84edbee2012-02-29 21:54:26 +0530131};
132
133enum ab8500_usb_state {
134 AB8500_BM_USB_STATE_RESET_HS, /* HighSpeed Reset */
135 AB8500_BM_USB_STATE_RESET_FS, /* FullSpeed/LowSpeed Reset */
136 AB8500_BM_USB_STATE_CONFIGURED,
137 AB8500_BM_USB_STATE_SUSPEND,
138 AB8500_BM_USB_STATE_RESUME,
139 AB8500_BM_USB_STATE_MAX,
140};
141
142/* VBUS input current limits supported in AB8500 in mA */
143#define USB_CH_IP_CUR_LVL_0P05 50
144#define USB_CH_IP_CUR_LVL_0P09 98
145#define USB_CH_IP_CUR_LVL_0P19 193
146#define USB_CH_IP_CUR_LVL_0P29 290
147#define USB_CH_IP_CUR_LVL_0P38 380
148#define USB_CH_IP_CUR_LVL_0P45 450
149#define USB_CH_IP_CUR_LVL_0P5 500
150#define USB_CH_IP_CUR_LVL_0P6 600
151#define USB_CH_IP_CUR_LVL_0P7 700
152#define USB_CH_IP_CUR_LVL_0P8 800
153#define USB_CH_IP_CUR_LVL_0P9 900
154#define USB_CH_IP_CUR_LVL_1P0 1000
155#define USB_CH_IP_CUR_LVL_1P1 1100
156#define USB_CH_IP_CUR_LVL_1P3 1300
157#define USB_CH_IP_CUR_LVL_1P4 1400
158#define USB_CH_IP_CUR_LVL_1P5 1500
159
160#define VBAT_TRESH_IP_CUR_RED 3800
161
162#define to_ab8500_charger_usb_device_info(x) container_of((x), \
163 struct ab8500_charger, usb_chg)
164#define to_ab8500_charger_ac_device_info(x) container_of((x), \
165 struct ab8500_charger, ac_chg)
166
167/**
168 * struct ab8500_charger_interrupts - ab8500 interupts
169 * @name: name of the interrupt
170 * @isr function pointer to the isr
171 */
172struct ab8500_charger_interrupts {
173 char *name;
174 irqreturn_t (*isr)(int irq, void *data);
175};
176
177struct ab8500_charger_info {
178 int charger_connected;
179 int charger_online;
180 int charger_voltage;
181 int cv_active;
182 bool wd_expired;
Jonas Aaberga864c5a2013-01-11 13:12:53 +0000183 int charger_current;
Arun Murthy84edbee2012-02-29 21:54:26 +0530184};
185
186struct ab8500_charger_event_flags {
187 bool mainextchnotok;
188 bool main_thermal_prot;
189 bool usb_thermal_prot;
190 bool vbus_ovv;
191 bool usbchargernotok;
192 bool chgwdexp;
193 bool vbus_collapse;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100194 bool vbus_drop_end;
Arun Murthy84edbee2012-02-29 21:54:26 +0530195};
196
197struct ab8500_charger_usb_state {
Arun Murthy84edbee2012-02-29 21:54:26 +0530198 int usb_current;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100199 int usb_current_tmp;
Arun Murthy84edbee2012-02-29 21:54:26 +0530200 enum ab8500_usb_state state;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100201 enum ab8500_usb_state state_tmp;
Arun Murthy84edbee2012-02-29 21:54:26 +0530202 spinlock_t usb_lock;
203};
204
205/**
206 * struct ab8500_charger - ab8500 Charger device information
207 * @dev: Pointer to the structure device
208 * @max_usb_in_curr: Max USB charger input current
209 * @vbus_detected: VBUS detected
210 * @vbus_detected_start:
211 * VBUS detected during startup
212 * @ac_conn: This will be true when the AC charger has been plugged
213 * @vddadc_en_ac: Indicate if VDD ADC supply is enabled because AC
214 * charger is enabled
215 * @vddadc_en_usb: Indicate if VDD ADC supply is enabled because USB
216 * charger is enabled
217 * @vbat Battery voltage
218 * @old_vbat Previously measured battery voltage
Marcus Cooper4b45f4a2013-01-11 13:13:04 +0000219 * @usb_device_is_unrecognised USB device is unrecognised by the hardware
Arun Murthy84edbee2012-02-29 21:54:26 +0530220 * @autopower Indicate if we should have automatic pwron after pwrloss
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -0800221 * @autopower_cfg platform specific power config support for "pwron after pwrloss"
Henrik Sölverff380902012-04-17 15:51:01 +0200222 * @invalid_charger_detect_state State when forcing AB to use invalid charger
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100223 * @is_usb_host: Indicate if last detected USB type is host
224 * @is_aca_rid: Incicate if accessory is ACA type
225 * @current_stepping_sessions:
226 * Counter for current stepping sessions
Arun Murthy84edbee2012-02-29 21:54:26 +0530227 * @parent: Pointer to the struct ab8500
228 * @gpadc: Pointer to the struct gpadc
Lee Jonesb0284de2012-11-30 10:09:42 +0000229 * @bm: Platform specific battery management information
Arun Murthy84edbee2012-02-29 21:54:26 +0530230 * @flags: Structure for information about events triggered
231 * @usb_state: Structure for usb stack information
232 * @ac_chg: AC charger power supply
233 * @usb_chg: USB charger power supply
234 * @ac: Structure that holds the AC charger properties
235 * @usb: Structure that holds the USB charger properties
236 * @regu: Pointer to the struct regulator
237 * @charger_wq: Work queue for the IRQs and checking HW state
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100238 * @usb_ipt_crnt_lock: Lock to protect VBUS input current setting from mutuals
239 * @pm_lock: Lock to prevent system to suspend
Arun Murthy84edbee2012-02-29 21:54:26 +0530240 * @check_vbat_work Work for checking vbat threshold to adjust vbus current
241 * @check_hw_failure_work: Work for checking HW state
242 * @check_usbchgnotok_work: Work for checking USB charger not ok status
243 * @kick_wd_work: Work for kicking the charger watchdog in case
244 * of ABB rev 1.* due to the watchog logic bug
Lee Jonesb269fff2013-01-11 13:12:51 +0000245 * @ac_charger_attached_work: Work for checking if AC charger is still
246 * connected
247 * @usb_charger_attached_work: Work for checking if USB charger is still
248 * connected
Arun Murthy84edbee2012-02-29 21:54:26 +0530249 * @ac_work: Work for checking AC charger connection
250 * @detect_usb_type_work: Work for detecting the USB type connected
251 * @usb_link_status_work: Work for checking the new USB link status
252 * @usb_state_changed_work: Work for checking USB state
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100253 * @attach_work: Work for detecting USB type
254 * @vbus_drop_end_work: Work for detecting VBUS drop end
Arun Murthy84edbee2012-02-29 21:54:26 +0530255 * @check_main_thermal_prot_work:
256 * Work for checking Main thermal status
257 * @check_usb_thermal_prot_work:
258 * Work for checking USB thermal status
Lee Jonesb269fff2013-01-11 13:12:51 +0000259 * @charger_attached_mutex: For controlling the wakelock
Arun Murthy84edbee2012-02-29 21:54:26 +0530260 */
261struct ab8500_charger {
262 struct device *dev;
263 int max_usb_in_curr;
264 bool vbus_detected;
265 bool vbus_detected_start;
266 bool ac_conn;
267 bool vddadc_en_ac;
268 bool vddadc_en_usb;
269 int vbat;
270 int old_vbat;
Marcus Cooper4b45f4a2013-01-11 13:13:04 +0000271 bool usb_device_is_unrecognised;
Arun Murthy84edbee2012-02-29 21:54:26 +0530272 bool autopower;
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -0800273 bool autopower_cfg;
Henrik Sölverff380902012-04-17 15:51:01 +0200274 int invalid_charger_detect_state;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100275 bool is_usb_host;
276 int is_aca_rid;
277 atomic_t current_stepping_sessions;
Arun Murthy84edbee2012-02-29 21:54:26 +0530278 struct ab8500 *parent;
279 struct ab8500_gpadc *gpadc;
Lee Jonesb0284de2012-11-30 10:09:42 +0000280 struct abx500_bm_data *bm;
Arun Murthy84edbee2012-02-29 21:54:26 +0530281 struct ab8500_charger_event_flags flags;
282 struct ab8500_charger_usb_state usb_state;
283 struct ux500_charger ac_chg;
284 struct ux500_charger usb_chg;
285 struct ab8500_charger_info ac;
286 struct ab8500_charger_info usb;
287 struct regulator *regu;
288 struct workqueue_struct *charger_wq;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100289 struct mutex usb_ipt_crnt_lock;
Arun Murthy84edbee2012-02-29 21:54:26 +0530290 struct delayed_work check_vbat_work;
291 struct delayed_work check_hw_failure_work;
292 struct delayed_work check_usbchgnotok_work;
293 struct delayed_work kick_wd_work;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100294 struct delayed_work usb_state_changed_work;
Marcus Cooper4b45f4a2013-01-11 13:13:04 +0000295 struct delayed_work attach_work;
Lee Jonesb269fff2013-01-11 13:12:51 +0000296 struct delayed_work ac_charger_attached_work;
297 struct delayed_work usb_charger_attached_work;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100298 struct delayed_work vbus_drop_end_work;
Arun Murthy84edbee2012-02-29 21:54:26 +0530299 struct work_struct ac_work;
300 struct work_struct detect_usb_type_work;
301 struct work_struct usb_link_status_work;
Arun Murthy84edbee2012-02-29 21:54:26 +0530302 struct work_struct check_main_thermal_prot_work;
303 struct work_struct check_usb_thermal_prot_work;
Anton Vorontsovefd71c82012-03-14 04:22:17 +0400304 struct usb_phy *usb_phy;
Arun Murthy84edbee2012-02-29 21:54:26 +0530305 struct notifier_block nb;
Lee Jonesb269fff2013-01-11 13:12:51 +0000306 struct mutex charger_attached_mutex;
Arun Murthy84edbee2012-02-29 21:54:26 +0530307};
308
309/* AC properties */
310static enum power_supply_property ab8500_charger_ac_props[] = {
311 POWER_SUPPLY_PROP_HEALTH,
312 POWER_SUPPLY_PROP_PRESENT,
313 POWER_SUPPLY_PROP_ONLINE,
314 POWER_SUPPLY_PROP_VOLTAGE_NOW,
315 POWER_SUPPLY_PROP_VOLTAGE_AVG,
316 POWER_SUPPLY_PROP_CURRENT_NOW,
317};
318
319/* USB properties */
320static enum power_supply_property ab8500_charger_usb_props[] = {
321 POWER_SUPPLY_PROP_HEALTH,
322 POWER_SUPPLY_PROP_CURRENT_AVG,
323 POWER_SUPPLY_PROP_PRESENT,
324 POWER_SUPPLY_PROP_ONLINE,
325 POWER_SUPPLY_PROP_VOLTAGE_NOW,
326 POWER_SUPPLY_PROP_VOLTAGE_AVG,
327 POWER_SUPPLY_PROP_CURRENT_NOW,
328};
329
Paer-Olof Haakanssondefbfa92013-01-11 13:12:49 +0000330/*
331 * Function for enabling and disabling sw fallback mode
332 * should always be disabled when no charger is connected.
Arun Murthy84edbee2012-02-29 21:54:26 +0530333 */
Paer-Olof Haakanssondefbfa92013-01-11 13:12:49 +0000334static void ab8500_enable_disable_sw_fallback(struct ab8500_charger *di,
335 bool fallback)
Arun Murthy84edbee2012-02-29 21:54:26 +0530336{
Nicolas Guionb0163222012-05-23 15:59:43 +0200337 u8 val;
Arun Murthy84edbee2012-02-29 21:54:26 +0530338 u8 reg;
Nicolas Guionb0163222012-05-23 15:59:43 +0200339 u8 bank;
340 u8 bit;
Arun Murthy84edbee2012-02-29 21:54:26 +0530341 int ret;
342
Paer-Olof Haakanssondefbfa92013-01-11 13:12:49 +0000343 dev_dbg(di->dev, "SW Fallback: %d\n", fallback);
Arun Murthy84edbee2012-02-29 21:54:26 +0530344
Nicolas Guionb0163222012-05-23 15:59:43 +0200345 if (is_ab8500(di->parent)) {
346 bank = 0x15;
347 reg = 0x0;
348 bit = 3;
349 } else {
350 bank = AB8500_SYS_CTRL1_BLOCK;
351 reg = AB8500_SW_CONTROL_FALLBACK;
352 bit = 0;
353 }
354
Paer-Olof Haakanssondefbfa92013-01-11 13:12:49 +0000355 /* read the register containing fallback bit */
Nicolas Guionb0163222012-05-23 15:59:43 +0200356 ret = abx500_get_register_interruptible(di->dev, bank, reg, &val);
357 if (ret < 0) {
358 dev_err(di->dev, "%d read failed\n", __LINE__);
Arun Murthy84edbee2012-02-29 21:54:26 +0530359 return;
360 }
361
Nicolas Guionb0163222012-05-23 15:59:43 +0200362 if (is_ab8500(di->parent)) {
363 /* enable the OPT emulation registers */
364 ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x2);
365 if (ret) {
366 dev_err(di->dev, "%d write failed\n", __LINE__);
367 goto disable_otp;
368 }
Arun Murthy84edbee2012-02-29 21:54:26 +0530369 }
370
Paer-Olof Haakanssondefbfa92013-01-11 13:12:49 +0000371 if (fallback)
Nicolas Guionb0163222012-05-23 15:59:43 +0200372 val |= (1 << bit);
Arun Murthy84edbee2012-02-29 21:54:26 +0530373 else
Nicolas Guionb0163222012-05-23 15:59:43 +0200374 val &= ~(1 << bit);
Arun Murthy84edbee2012-02-29 21:54:26 +0530375
Paer-Olof Haakanssondefbfa92013-01-11 13:12:49 +0000376 /* write back the changed fallback bit value to register */
Nicolas Guionb0163222012-05-23 15:59:43 +0200377 ret = abx500_set_register_interruptible(di->dev, bank, reg, val);
Arun Murthy84edbee2012-02-29 21:54:26 +0530378 if (ret) {
379 dev_err(di->dev, "%d write failed\n", __LINE__);
Arun Murthy84edbee2012-02-29 21:54:26 +0530380 }
381
Nicolas Guionb0163222012-05-23 15:59:43 +0200382disable_otp:
383 if (is_ab8500(di->parent)) {
384 /* disable the set OTP registers again */
385 ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x0);
386 if (ret) {
387 dev_err(di->dev, "%d write failed\n", __LINE__);
388 }
Arun Murthy84edbee2012-02-29 21:54:26 +0530389 }
390}
391
392/**
393 * ab8500_power_supply_changed - a wrapper with local extentions for
394 * power_supply_changed
395 * @di: pointer to the ab8500_charger structure
396 * @psy: pointer to power_supply_that have changed.
397 *
398 */
399static void ab8500_power_supply_changed(struct ab8500_charger *di,
400 struct power_supply *psy)
401{
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -0800402 if (di->autopower_cfg) {
Arun Murthy84edbee2012-02-29 21:54:26 +0530403 if (!di->usb.charger_connected &&
404 !di->ac.charger_connected &&
405 di->autopower) {
406 di->autopower = false;
Paer-Olof Haakanssondefbfa92013-01-11 13:12:49 +0000407 ab8500_enable_disable_sw_fallback(di, false);
Arun Murthy84edbee2012-02-29 21:54:26 +0530408 } else if (!di->autopower &&
409 (di->ac.charger_connected ||
410 di->usb.charger_connected)) {
411 di->autopower = true;
Paer-Olof Haakanssondefbfa92013-01-11 13:12:49 +0000412 ab8500_enable_disable_sw_fallback(di, true);
Arun Murthy84edbee2012-02-29 21:54:26 +0530413 }
414 }
415 power_supply_changed(psy);
416}
417
418static void ab8500_charger_set_usb_connected(struct ab8500_charger *di,
419 bool connected)
420{
421 if (connected != di->usb.charger_connected) {
422 dev_dbg(di->dev, "USB connected:%i\n", connected);
423 di->usb.charger_connected = connected;
424 sysfs_notify(&di->usb_chg.psy.dev->kobj, NULL, "present");
Lee Jonesb269fff2013-01-11 13:12:51 +0000425
426 if (connected) {
427 mutex_lock(&di->charger_attached_mutex);
428 mutex_unlock(&di->charger_attached_mutex);
429
430 queue_delayed_work(di->charger_wq,
431 &di->usb_charger_attached_work,
432 HZ);
433 } else {
434 cancel_delayed_work_sync(&di->usb_charger_attached_work);
435 mutex_lock(&di->charger_attached_mutex);
436 mutex_unlock(&di->charger_attached_mutex);
437 }
Arun Murthy84edbee2012-02-29 21:54:26 +0530438 }
439}
440
441/**
442 * ab8500_charger_get_ac_voltage() - get ac charger voltage
443 * @di: pointer to the ab8500_charger structure
444 *
445 * Returns ac charger voltage (on success)
446 */
447static int ab8500_charger_get_ac_voltage(struct ab8500_charger *di)
448{
449 int vch;
450
451 /* Only measure voltage if the charger is connected */
452 if (di->ac.charger_connected) {
453 vch = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_V);
454 if (vch < 0)
455 dev_err(di->dev, "%s gpadc conv failed,\n", __func__);
456 } else {
457 vch = 0;
458 }
459 return vch;
460}
461
462/**
463 * ab8500_charger_ac_cv() - check if the main charger is in CV mode
464 * @di: pointer to the ab8500_charger structure
465 *
466 * Returns ac charger CV mode (on success) else error code
467 */
468static int ab8500_charger_ac_cv(struct ab8500_charger *di)
469{
470 u8 val;
471 int ret = 0;
472
473 /* Only check CV mode if the charger is online */
474 if (di->ac.charger_online) {
475 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
476 AB8500_CH_STATUS1_REG, &val);
477 if (ret < 0) {
478 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
479 return 0;
480 }
481
482 if (val & MAIN_CH_CV_ON)
483 ret = 1;
484 else
485 ret = 0;
486 }
487
488 return ret;
489}
490
491/**
492 * ab8500_charger_get_vbus_voltage() - get vbus voltage
493 * @di: pointer to the ab8500_charger structure
494 *
495 * This function returns the vbus voltage.
496 * Returns vbus voltage (on success)
497 */
498static int ab8500_charger_get_vbus_voltage(struct ab8500_charger *di)
499{
500 int vch;
501
502 /* Only measure voltage if the charger is connected */
503 if (di->usb.charger_connected) {
504 vch = ab8500_gpadc_convert(di->gpadc, VBUS_V);
505 if (vch < 0)
506 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
507 } else {
508 vch = 0;
509 }
510 return vch;
511}
512
513/**
514 * ab8500_charger_get_usb_current() - get usb charger current
515 * @di: pointer to the ab8500_charger structure
516 *
517 * This function returns the usb charger current.
518 * Returns usb current (on success) and error code on failure
519 */
520static int ab8500_charger_get_usb_current(struct ab8500_charger *di)
521{
522 int ich;
523
524 /* Only measure current if the charger is online */
525 if (di->usb.charger_online) {
526 ich = ab8500_gpadc_convert(di->gpadc, USB_CHARGER_C);
527 if (ich < 0)
528 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
529 } else {
530 ich = 0;
531 }
532 return ich;
533}
534
535/**
536 * ab8500_charger_get_ac_current() - get ac charger current
537 * @di: pointer to the ab8500_charger structure
538 *
539 * This function returns the ac charger current.
540 * Returns ac current (on success) and error code on failure.
541 */
542static int ab8500_charger_get_ac_current(struct ab8500_charger *di)
543{
544 int ich;
545
546 /* Only measure current if the charger is online */
547 if (di->ac.charger_online) {
548 ich = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_C);
549 if (ich < 0)
550 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
551 } else {
552 ich = 0;
553 }
554 return ich;
555}
556
557/**
558 * ab8500_charger_usb_cv() - check if the usb charger is in CV mode
559 * @di: pointer to the ab8500_charger structure
560 *
561 * Returns ac charger CV mode (on success) else error code
562 */
563static int ab8500_charger_usb_cv(struct ab8500_charger *di)
564{
565 int ret;
566 u8 val;
567
568 /* Only check CV mode if the charger is online */
569 if (di->usb.charger_online) {
570 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
571 AB8500_CH_USBCH_STAT1_REG, &val);
572 if (ret < 0) {
573 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
574 return 0;
575 }
576
577 if (val & USB_CH_CV_ON)
578 ret = 1;
579 else
580 ret = 0;
581 } else {
582 ret = 0;
583 }
584
585 return ret;
586}
587
588/**
589 * ab8500_charger_detect_chargers() - Detect the connected chargers
590 * @di: pointer to the ab8500_charger structure
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100591 * @probe: if probe, don't delay and wait for HW
Arun Murthy84edbee2012-02-29 21:54:26 +0530592 *
593 * Returns the type of charger connected.
594 * For USB it will not mean we can actually charge from it
595 * but that there is a USB cable connected that we have to
596 * identify. This is used during startup when we don't get
597 * interrupts of the charger detection
598 *
599 * Returns an integer value, that means,
600 * NO_PW_CONN no power supply is connected
601 * AC_PW_CONN if the AC power supply is connected
602 * USB_PW_CONN if the USB power supply is connected
603 * AC_PW_CONN + USB_PW_CONN if USB and AC power supplies are both connected
604 */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100605static int ab8500_charger_detect_chargers(struct ab8500_charger *di, bool probe)
Arun Murthy84edbee2012-02-29 21:54:26 +0530606{
607 int result = NO_PW_CONN;
608 int ret;
609 u8 val;
610
611 /* Check for AC charger */
612 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
613 AB8500_CH_STATUS1_REG, &val);
614 if (ret < 0) {
615 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
616 return ret;
617 }
618
619 if (val & MAIN_CH_DET)
620 result = AC_PW_CONN;
621
622 /* Check for USB charger */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100623
624 if (!probe) {
625 /*
626 * AB8500 says VBUS_DET_DBNC1 & VBUS_DET_DBNC100
627 * when disconnecting ACA even though no
628 * charger was connected. Try waiting a little
629 * longer than the 100 ms of VBUS_DET_DBNC100...
630 */
631 msleep(110);
632 }
Arun Murthy84edbee2012-02-29 21:54:26 +0530633 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
634 AB8500_CH_USBCH_STAT1_REG, &val);
635 if (ret < 0) {
636 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
637 return ret;
638 }
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100639 dev_dbg(di->dev,
640 "%s AB8500_CH_USBCH_STAT1_REG %x\n", __func__,
641 val);
Arun Murthy84edbee2012-02-29 21:54:26 +0530642 if ((val & VBUS_DET_DBNC1) && (val & VBUS_DET_DBNC100))
643 result |= USB_PW_CONN;
644
645 return result;
646}
647
648/**
649 * ab8500_charger_max_usb_curr() - get the max curr for the USB type
650 * @di: pointer to the ab8500_charger structure
651 * @link_status: the identified USB type
652 *
653 * Get the maximum current that is allowed to be drawn from the host
654 * based on the USB type.
655 * Returns error code in case of failure else 0 on success
656 */
657static int ab8500_charger_max_usb_curr(struct ab8500_charger *di,
Hakan Berg74a8e342013-01-11 13:12:58 +0000658 enum ab8500_charger_link_status link_status)
Arun Murthy84edbee2012-02-29 21:54:26 +0530659{
660 int ret = 0;
661
Marcus Cooper4b45f4a2013-01-11 13:13:04 +0000662 di->usb_device_is_unrecognised = false;
663
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100664 /*
665 * Platform only supports USB 2.0.
666 * This means that charging current from USB source
667 * is maximum 500 mA. Every occurence of USB_STAT_*_HOST_*
668 * should set USB_CH_IP_CUR_LVL_0P5.
669 */
670
Arun Murthy84edbee2012-02-29 21:54:26 +0530671 switch (link_status) {
672 case USB_STAT_STD_HOST_NC:
673 case USB_STAT_STD_HOST_C_NS:
674 case USB_STAT_STD_HOST_C_S:
675 dev_dbg(di->dev, "USB Type - Standard host is "
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100676 "detected through USB driver\n");
677 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5;
678 di->is_usb_host = true;
679 di->is_aca_rid = 0;
Arun Murthy84edbee2012-02-29 21:54:26 +0530680 break;
681 case USB_STAT_HOST_CHG_HS_CHIRP:
682 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100683 di->is_usb_host = true;
684 di->is_aca_rid = 0;
Arun Murthy84edbee2012-02-29 21:54:26 +0530685 break;
686 case USB_STAT_HOST_CHG_HS:
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100687 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5;
688 di->is_usb_host = true;
689 di->is_aca_rid = 0;
690 break;
Arun Murthy84edbee2012-02-29 21:54:26 +0530691 case USB_STAT_ACA_RID_C_HS:
692 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P9;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100693 di->is_usb_host = false;
694 di->is_aca_rid = 0;
Arun Murthy84edbee2012-02-29 21:54:26 +0530695 break;
696 case USB_STAT_ACA_RID_A:
697 /*
698 * Dedicated charger level minus maximum current accessory
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100699 * can consume (900mA). Closest level is 500mA
Arun Murthy84edbee2012-02-29 21:54:26 +0530700 */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100701 dev_dbg(di->dev, "USB_STAT_ACA_RID_A detected\n");
702 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5;
703 di->is_usb_host = false;
704 di->is_aca_rid = 1;
Arun Murthy84edbee2012-02-29 21:54:26 +0530705 break;
706 case USB_STAT_ACA_RID_B:
707 /*
708 * Dedicated charger level minus 120mA (20mA for ACA and
709 * 100mA for potential accessory). Closest level is 1300mA
710 */
711 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P3;
Hakan Berg74a8e342013-01-11 13:12:58 +0000712 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
713 di->max_usb_in_curr);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100714 di->is_usb_host = false;
715 di->is_aca_rid = 1;
Arun Murthy84edbee2012-02-29 21:54:26 +0530716 break;
Arun Murthy84edbee2012-02-29 21:54:26 +0530717 case USB_STAT_HOST_CHG_NM:
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100718 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5;
719 di->is_usb_host = true;
720 di->is_aca_rid = 0;
721 break;
Hakan Berg74a8e342013-01-11 13:12:58 +0000722 case USB_STAT_DEDICATED_CHG:
Arun Murthy84edbee2012-02-29 21:54:26 +0530723 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P5;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100724 di->is_usb_host = false;
725 di->is_aca_rid = 0;
726 break;
727 case USB_STAT_ACA_RID_C_HS_CHIRP:
728 case USB_STAT_ACA_RID_C_NM:
729 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P5;
730 di->is_usb_host = false;
731 di->is_aca_rid = 1;
Arun Murthy84edbee2012-02-29 21:54:26 +0530732 break;
Marcus Cooper4b45f4a2013-01-11 13:13:04 +0000733 case USB_STAT_NOT_CONFIGURED:
734 if (di->vbus_detected) {
735 di->usb_device_is_unrecognised = true;
736 dev_dbg(di->dev, "USB Type - Legacy charger.\n");
737 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P5;
738 break;
739 }
Arun Murthy84edbee2012-02-29 21:54:26 +0530740 case USB_STAT_HM_IDGND:
Arun Murthy84edbee2012-02-29 21:54:26 +0530741 dev_err(di->dev, "USB Type - Charging not allowed\n");
742 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05;
743 ret = -ENXIO;
744 break;
Hakan Berg74a8e342013-01-11 13:12:58 +0000745 case USB_STAT_RESERVED:
746 if (is_ab8500(di->parent)) {
747 di->flags.vbus_collapse = true;
748 dev_err(di->dev, "USB Type - USB_STAT_RESERVED "
749 "VBUS has collapsed\n");
750 ret = -ENXIO;
751 break;
752 }
753 if (is_ab9540(di->parent) || is_ab8505(di->parent)) {
754 dev_dbg(di->dev, "USB Type - Charging not allowed\n");
755 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05;
756 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
757 link_status, di->max_usb_in_curr);
758 ret = -ENXIO;
759 break;
760 }
761 break;
762 case USB_STAT_CARKIT_1:
763 case USB_STAT_CARKIT_2:
764 case USB_STAT_ACA_DOCK_CHARGER:
765 case USB_STAT_CHARGER_LINE_1:
766 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5;
767 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
768 di->max_usb_in_curr);
Henrik Sölverff380902012-04-17 15:51:01 +0200769 case USB_STAT_NOT_VALID_LINK:
770 dev_err(di->dev, "USB Type invalid - try charging anyway\n");
771 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5;
Hakan Berg74a8e342013-01-11 13:12:58 +0000772 break;
773
Arun Murthy84edbee2012-02-29 21:54:26 +0530774 default:
775 dev_err(di->dev, "USB Type - Unknown\n");
776 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05;
777 ret = -ENXIO;
778 break;
779 };
780
781 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
782 link_status, di->max_usb_in_curr);
783
784 return ret;
785}
786
787/**
788 * ab8500_charger_read_usb_type() - read the type of usb connected
789 * @di: pointer to the ab8500_charger structure
790 *
791 * Detect the type of the plugged USB
792 * Returns error code in case of failure else 0 on success
793 */
794static int ab8500_charger_read_usb_type(struct ab8500_charger *di)
795{
796 int ret;
797 u8 val;
798
799 ret = abx500_get_register_interruptible(di->dev,
800 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG, &val);
801 if (ret < 0) {
802 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
803 return ret;
804 }
Hakan Berg74a8e342013-01-11 13:12:58 +0000805 if (is_ab8500(di->parent)) {
806 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
807 AB8500_USB_LINE_STAT_REG, &val);
808 } else {
809 if (is_ab9540(di->parent) || is_ab8505(di->parent))
810 ret = abx500_get_register_interruptible(di->dev,
811 AB8500_USB, AB8500_USB_LINK1_STAT_REG, &val);
812 }
Arun Murthy84edbee2012-02-29 21:54:26 +0530813 if (ret < 0) {
814 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
815 return ret;
816 }
817
818 /* get the USB type */
819 val = (val & AB8500_USB_LINK_STATUS) >> 3;
820 ret = ab8500_charger_max_usb_curr(di,
821 (enum ab8500_charger_link_status) val);
822
823 return ret;
824}
825
826/**
827 * ab8500_charger_detect_usb_type() - get the type of usb connected
828 * @di: pointer to the ab8500_charger structure
829 *
830 * Detect the type of the plugged USB
831 * Returns error code in case of failure else 0 on success
832 */
833static int ab8500_charger_detect_usb_type(struct ab8500_charger *di)
834{
835 int i, ret;
836 u8 val;
837
838 /*
839 * On getting the VBUS rising edge detect interrupt there
840 * is a 250ms delay after which the register UsbLineStatus
841 * is filled with valid data.
842 */
843 for (i = 0; i < 10; i++) {
844 msleep(250);
845 ret = abx500_get_register_interruptible(di->dev,
846 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG,
847 &val);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100848 dev_dbg(di->dev, "%s AB8500_IT_SOURCE21_REG %x\n",
849 __func__, val);
Arun Murthy84edbee2012-02-29 21:54:26 +0530850 if (ret < 0) {
851 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
852 return ret;
853 }
Hakan Berg74a8e342013-01-11 13:12:58 +0000854
855 if (is_ab8500(di->parent))
856 ret = abx500_get_register_interruptible(di->dev,
857 AB8500_USB, AB8500_USB_LINE_STAT_REG, &val);
858 else
859 ret = abx500_get_register_interruptible(di->dev,
860 AB8500_USB, AB8500_USB_LINK1_STAT_REG, &val);
Arun Murthy84edbee2012-02-29 21:54:26 +0530861 if (ret < 0) {
862 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
863 return ret;
864 }
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100865 dev_dbg(di->dev, "%s AB8500_USB_LINE_STAT_REG %x\n", __func__,
866 val);
Arun Murthy84edbee2012-02-29 21:54:26 +0530867 /*
868 * Until the IT source register is read the UsbLineStatus
869 * register is not updated, hence doing the same
870 * Revisit this:
871 */
872
873 /* get the USB type */
874 val = (val & AB8500_USB_LINK_STATUS) >> 3;
875 if (val)
876 break;
877 }
878 ret = ab8500_charger_max_usb_curr(di,
879 (enum ab8500_charger_link_status) val);
880
881 return ret;
882}
883
884/*
885 * This array maps the raw hex value to charger voltage used by the AB8500
886 * Values taken from the UM0836
887 */
888static int ab8500_charger_voltage_map[] = {
889 3500 ,
890 3525 ,
891 3550 ,
892 3575 ,
893 3600 ,
894 3625 ,
895 3650 ,
896 3675 ,
897 3700 ,
898 3725 ,
899 3750 ,
900 3775 ,
901 3800 ,
902 3825 ,
903 3850 ,
904 3875 ,
905 3900 ,
906 3925 ,
907 3950 ,
908 3975 ,
909 4000 ,
910 4025 ,
911 4050 ,
912 4060 ,
913 4070 ,
914 4080 ,
915 4090 ,
916 4100 ,
917 4110 ,
918 4120 ,
919 4130 ,
920 4140 ,
921 4150 ,
922 4160 ,
923 4170 ,
924 4180 ,
925 4190 ,
926 4200 ,
927 4210 ,
928 4220 ,
929 4230 ,
930 4240 ,
931 4250 ,
932 4260 ,
933 4270 ,
934 4280 ,
935 4290 ,
936 4300 ,
937 4310 ,
938 4320 ,
939 4330 ,
940 4340 ,
941 4350 ,
942 4360 ,
943 4370 ,
944 4380 ,
945 4390 ,
946 4400 ,
947 4410 ,
948 4420 ,
949 4430 ,
950 4440 ,
951 4450 ,
952 4460 ,
953 4470 ,
954 4480 ,
955 4490 ,
956 4500 ,
957 4510 ,
958 4520 ,
959 4530 ,
960 4540 ,
961 4550 ,
962 4560 ,
963 4570 ,
964 4580 ,
965 4590 ,
966 4600 ,
967};
968
969/*
970 * This array maps the raw hex value to charger current used by the AB8500
971 * Values taken from the UM0836
972 */
973static int ab8500_charger_current_map[] = {
974 100 ,
975 200 ,
976 300 ,
977 400 ,
978 500 ,
979 600 ,
980 700 ,
981 800 ,
982 900 ,
983 1000 ,
984 1100 ,
985 1200 ,
986 1300 ,
987 1400 ,
988 1500 ,
989};
990
991/*
992 * This array maps the raw hex value to VBUS input current used by the AB8500
993 * Values taken from the UM0836
994 */
995static int ab8500_charger_vbus_in_curr_map[] = {
996 USB_CH_IP_CUR_LVL_0P05,
997 USB_CH_IP_CUR_LVL_0P09,
998 USB_CH_IP_CUR_LVL_0P19,
999 USB_CH_IP_CUR_LVL_0P29,
1000 USB_CH_IP_CUR_LVL_0P38,
1001 USB_CH_IP_CUR_LVL_0P45,
1002 USB_CH_IP_CUR_LVL_0P5,
1003 USB_CH_IP_CUR_LVL_0P6,
1004 USB_CH_IP_CUR_LVL_0P7,
1005 USB_CH_IP_CUR_LVL_0P8,
1006 USB_CH_IP_CUR_LVL_0P9,
1007 USB_CH_IP_CUR_LVL_1P0,
1008 USB_CH_IP_CUR_LVL_1P1,
1009 USB_CH_IP_CUR_LVL_1P3,
1010 USB_CH_IP_CUR_LVL_1P4,
1011 USB_CH_IP_CUR_LVL_1P5,
1012};
1013
1014static int ab8500_voltage_to_regval(int voltage)
1015{
1016 int i;
1017
1018 /* Special case for voltage below 3.5V */
1019 if (voltage < ab8500_charger_voltage_map[0])
1020 return LOW_VOLT_REG;
1021
1022 for (i = 1; i < ARRAY_SIZE(ab8500_charger_voltage_map); i++) {
1023 if (voltage < ab8500_charger_voltage_map[i])
1024 return i - 1;
1025 }
1026
1027 /* If not last element, return error */
1028 i = ARRAY_SIZE(ab8500_charger_voltage_map) - 1;
1029 if (voltage == ab8500_charger_voltage_map[i])
1030 return i;
1031 else
1032 return -1;
1033}
1034
1035static int ab8500_current_to_regval(int curr)
1036{
1037 int i;
1038
1039 if (curr < ab8500_charger_current_map[0])
1040 return 0;
1041
1042 for (i = 0; i < ARRAY_SIZE(ab8500_charger_current_map); i++) {
1043 if (curr < ab8500_charger_current_map[i])
1044 return i - 1;
1045 }
1046
1047 /* If not last element, return error */
1048 i = ARRAY_SIZE(ab8500_charger_current_map) - 1;
1049 if (curr == ab8500_charger_current_map[i])
1050 return i;
1051 else
1052 return -1;
1053}
1054
1055static int ab8500_vbus_in_curr_to_regval(int curr)
1056{
1057 int i;
1058
1059 if (curr < ab8500_charger_vbus_in_curr_map[0])
1060 return 0;
1061
1062 for (i = 0; i < ARRAY_SIZE(ab8500_charger_vbus_in_curr_map); i++) {
1063 if (curr < ab8500_charger_vbus_in_curr_map[i])
1064 return i - 1;
1065 }
1066
1067 /* If not last element, return error */
1068 i = ARRAY_SIZE(ab8500_charger_vbus_in_curr_map) - 1;
1069 if (curr == ab8500_charger_vbus_in_curr_map[i])
1070 return i;
1071 else
1072 return -1;
1073}
1074
1075/**
1076 * ab8500_charger_get_usb_cur() - get usb current
1077 * @di: pointer to the ab8500_charger structre
1078 *
1079 * The usb stack provides the maximum current that can be drawn from
1080 * the standard usb host. This will be in mA.
1081 * This function converts current in mA to a value that can be written
1082 * to the register. Returns -1 if charging is not allowed
1083 */
1084static int ab8500_charger_get_usb_cur(struct ab8500_charger *di)
1085{
1086 switch (di->usb_state.usb_current) {
1087 case 100:
1088 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P09;
1089 break;
1090 case 200:
1091 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P19;
1092 break;
1093 case 300:
1094 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P29;
1095 break;
1096 case 400:
1097 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P38;
1098 break;
1099 case 500:
1100 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5;
1101 break;
1102 default:
1103 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05;
1104 return -1;
1105 break;
1106 };
1107 return 0;
1108}
1109
1110/**
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001111 * ab8500_charger_set_current() - set charger current
1112 * @di: pointer to the ab8500_charger structure
1113 * @ich: charger current, in mA
1114 * @reg: select what charger register to set
1115 *
1116 * Set charger current.
1117 * There is no state machine in the AB to step up/down the charger
1118 * current to avoid dips and spikes on MAIN, VBUS and VBAT when
1119 * charging is started. Instead we need to implement
1120 * this charger current step-up/down here.
1121 * Returns error code in case of failure else 0(on success)
1122 */
1123static int ab8500_charger_set_current(struct ab8500_charger *di,
1124 int ich, int reg)
1125{
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001126 int ret = 0;
1127 int auto_curr_index, curr_index, prev_curr_index, shift_value, i;
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001128 u8 reg_value;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001129 u32 step_udelay;
1130 bool no_stepping = false;
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001131
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001132 atomic_inc(&di->current_stepping_sessions);
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001133
1134 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1135 reg, &reg_value);
1136 if (ret < 0) {
1137 dev_err(di->dev, "%s read failed\n", __func__);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001138 goto exit_set_current;
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001139 }
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001140
1141 switch (reg) {
1142 case AB8500_MCH_IPT_CURLVL_REG:
1143 shift_value = MAIN_CH_INPUT_CURR_SHIFT;
1144 prev_curr_index = (reg_value >> shift_value);
1145 curr_index = ab8500_current_to_regval(ich);
1146 step_udelay = STEP_UDELAY;
1147 if (!di->ac.charger_connected)
1148 no_stepping = true;
1149 break;
1150 case AB8500_USBCH_IPT_CRNTLVL_REG:
1151 shift_value = VBUS_IN_CURR_LIM_SHIFT;
1152 prev_curr_index = (reg_value >> shift_value);
1153 curr_index = ab8500_vbus_in_curr_to_regval(ich);
1154 step_udelay = STEP_UDELAY * 100;
1155
1156 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1157 AB8500_CH_USBCH_STAT2_REG, &reg_value);
1158 if (ret < 0) {
1159 dev_err(di->dev, "%s read failed\n", __func__);
1160 goto exit_set_current;
1161 }
1162 auto_curr_index =
1163 reg_value >> AUTO_VBUS_IN_CURR_LIM_SHIFT;
1164
1165 dev_dbg(di->dev, "%s Auto VBUS curr is %d mA\n",
1166 __func__,
1167 ab8500_charger_vbus_in_curr_map[auto_curr_index]);
1168
1169 prev_curr_index = min(prev_curr_index, auto_curr_index);
1170
1171 if (!di->usb.charger_connected)
1172 no_stepping = true;
1173 break;
1174 case AB8500_CH_OPT_CRNTLVL_REG:
1175 shift_value = 0;
1176 prev_curr_index = (reg_value >> shift_value);
1177 curr_index = ab8500_current_to_regval(ich);
1178 step_udelay = STEP_UDELAY;
1179 if (curr_index && (curr_index - prev_curr_index) > 1)
1180 step_udelay *= 100;
1181
1182 if (!di->usb.charger_connected && !di->ac.charger_connected)
1183 no_stepping = true;
1184
1185 break;
1186 default:
1187 dev_err(di->dev, "%s current register not valid\n", __func__);
1188 ret = -ENXIO;
1189 goto exit_set_current;
1190 }
1191
1192 if (curr_index < 0) {
1193 dev_err(di->dev, "requested current limit out-of-range\n");
1194 ret = -ENXIO;
1195 goto exit_set_current;
1196 }
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001197
1198 /* only update current if it's been changed */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001199 if (prev_curr_index == curr_index) {
1200 dev_dbg(di->dev, "%s current not changed for reg: 0x%02x\n",
1201 __func__, reg);
1202 ret = 0;
1203 goto exit_set_current;
1204 }
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001205
1206 dev_dbg(di->dev, "%s set charger current: %d mA for reg: 0x%02x\n",
1207 __func__, ich, reg);
1208
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001209 if (no_stepping) {
1210 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1211 reg, (u8)curr_index << shift_value);
1212 if (ret)
1213 dev_err(di->dev, "%s write failed\n", __func__);
1214 } else if (prev_curr_index > curr_index) {
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001215 for (i = prev_curr_index - 1; i >= curr_index; i--) {
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001216 dev_dbg(di->dev, "curr change_1 to: %x for 0x%02x\n",
1217 (u8) i << shift_value, reg);
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001218 ret = abx500_set_register_interruptible(di->dev,
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001219 AB8500_CHARGER, reg, (u8)i << shift_value);
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001220 if (ret) {
1221 dev_err(di->dev, "%s write failed\n", __func__);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001222 goto exit_set_current;
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001223 }
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001224 if (i != curr_index)
1225 usleep_range(step_udelay, step_udelay * 2);
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001226 }
1227 } else {
1228 for (i = prev_curr_index + 1; i <= curr_index; i++) {
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001229 dev_dbg(di->dev, "curr change_2 to: %x for 0x%02x\n",
1230 (u8)i << shift_value, reg);
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001231 ret = abx500_set_register_interruptible(di->dev,
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001232 AB8500_CHARGER, reg, (u8)i << shift_value);
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001233 if (ret) {
1234 dev_err(di->dev, "%s write failed\n", __func__);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001235 goto exit_set_current;
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001236 }
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001237 if (i != curr_index)
1238 usleep_range(step_udelay, step_udelay * 2);
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001239 }
1240 }
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001241
1242exit_set_current:
1243 atomic_dec(&di->current_stepping_sessions);
1244
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001245 return ret;
1246}
1247
1248/**
Arun Murthy84edbee2012-02-29 21:54:26 +05301249 * ab8500_charger_set_vbus_in_curr() - set VBUS input current limit
1250 * @di: pointer to the ab8500_charger structure
1251 * @ich_in: charger input current limit
1252 *
1253 * Sets the current that can be drawn from the USB host
1254 * Returns error code in case of failure else 0(on success)
1255 */
1256static int ab8500_charger_set_vbus_in_curr(struct ab8500_charger *di,
1257 int ich_in)
1258{
Arun Murthy84edbee2012-02-29 21:54:26 +05301259 int min_value;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001260 int ret;
Arun Murthy84edbee2012-02-29 21:54:26 +05301261
1262 /* We should always use to lowest current limit */
Lee Jonesb0284de2012-11-30 10:09:42 +00001263 min_value = min(di->bm->chg_params->usb_curr_max, ich_in);
Arun Murthy84edbee2012-02-29 21:54:26 +05301264
1265 switch (min_value) {
1266 case 100:
1267 if (di->vbat < VBAT_TRESH_IP_CUR_RED)
1268 min_value = USB_CH_IP_CUR_LVL_0P05;
1269 break;
1270 case 500:
1271 if (di->vbat < VBAT_TRESH_IP_CUR_RED)
1272 min_value = USB_CH_IP_CUR_LVL_0P45;
1273 break;
1274 default:
1275 break;
1276 }
1277
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001278 dev_info(di->dev, "VBUS input current limit set to %d mA\n", min_value);
1279
1280 mutex_lock(&di->usb_ipt_crnt_lock);
1281 ret = ab8500_charger_set_current(di, min_value,
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001282 AB8500_USBCH_IPT_CRNTLVL_REG);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001283 mutex_unlock(&di->usb_ipt_crnt_lock);
1284
1285 return ret;
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001286}
Arun Murthy84edbee2012-02-29 21:54:26 +05301287
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001288/**
1289 * ab8500_charger_set_main_in_curr() - set main charger input current
1290 * @di: pointer to the ab8500_charger structure
1291 * @ich_in: input charger current, in mA
1292 *
1293 * Set main charger input current.
1294 * Returns error code in case of failure else 0(on success)
1295 */
1296static int ab8500_charger_set_main_in_curr(struct ab8500_charger *di,
1297 int ich_in)
1298{
1299 return ab8500_charger_set_current(di, ich_in,
1300 AB8500_MCH_IPT_CURLVL_REG);
1301}
Arun Murthy84edbee2012-02-29 21:54:26 +05301302
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001303/**
1304 * ab8500_charger_set_output_curr() - set charger output current
1305 * @di: pointer to the ab8500_charger structure
1306 * @ich_out: output charger current, in mA
1307 *
1308 * Set charger output current.
1309 * Returns error code in case of failure else 0(on success)
1310 */
1311static int ab8500_charger_set_output_curr(struct ab8500_charger *di,
1312 int ich_out)
1313{
1314 return ab8500_charger_set_current(di, ich_out,
1315 AB8500_CH_OPT_CRNTLVL_REG);
Arun Murthy84edbee2012-02-29 21:54:26 +05301316}
1317
1318/**
1319 * ab8500_charger_led_en() - turn on/off chargign led
1320 * @di: pointer to the ab8500_charger structure
1321 * @on: flag to turn on/off the chargign led
1322 *
1323 * Power ON/OFF charging LED indication
1324 * Returns error code in case of failure else 0(on success)
1325 */
1326static int ab8500_charger_led_en(struct ab8500_charger *di, int on)
1327{
1328 int ret;
1329
1330 if (on) {
1331 /* Power ON charging LED indicator, set LED current to 5mA */
1332 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1333 AB8500_LED_INDICATOR_PWM_CTRL,
1334 (LED_IND_CUR_5MA | LED_INDICATOR_PWM_ENA));
1335 if (ret) {
1336 dev_err(di->dev, "Power ON LED failed\n");
1337 return ret;
1338 }
1339 /* LED indicator PWM duty cycle 252/256 */
1340 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1341 AB8500_LED_INDICATOR_PWM_DUTY,
1342 LED_INDICATOR_PWM_DUTY_252_256);
1343 if (ret) {
1344 dev_err(di->dev, "Set LED PWM duty cycle failed\n");
1345 return ret;
1346 }
1347 } else {
1348 /* Power off charging LED indicator */
1349 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1350 AB8500_LED_INDICATOR_PWM_CTRL,
1351 LED_INDICATOR_PWM_DIS);
1352 if (ret) {
1353 dev_err(di->dev, "Power-off LED failed\n");
1354 return ret;
1355 }
1356 }
1357
1358 return ret;
1359}
1360
1361/**
1362 * ab8500_charger_ac_en() - enable or disable ac charging
1363 * @di: pointer to the ab8500_charger structure
1364 * @enable: enable/disable flag
1365 * @vset: charging voltage
1366 * @iset: charging current
1367 *
1368 * Enable/Disable AC/Mains charging and turns on/off the charging led
1369 * respectively.
1370 **/
1371static int ab8500_charger_ac_en(struct ux500_charger *charger,
1372 int enable, int vset, int iset)
1373{
1374 int ret;
1375 int volt_index;
1376 int curr_index;
1377 int input_curr_index;
1378 u8 overshoot = 0;
1379
1380 struct ab8500_charger *di = to_ab8500_charger_ac_device_info(charger);
1381
1382 if (enable) {
1383 /* Check if AC is connected */
1384 if (!di->ac.charger_connected) {
1385 dev_err(di->dev, "AC charger not connected\n");
1386 return -ENXIO;
1387 }
1388
1389 /* Enable AC charging */
1390 dev_dbg(di->dev, "Enable AC: %dmV %dmA\n", vset, iset);
1391
1392 /*
1393 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1394 * will be triggered everytime we enable the VDD ADC supply.
1395 * This will turn off charging for a short while.
1396 * It can be avoided by having the supply on when
1397 * there is a charger enabled. Normally the VDD ADC supply
1398 * is enabled everytime a GPADC conversion is triggered. We will
1399 * force it to be enabled from this driver to have
1400 * the GPADC module independant of the AB8500 chargers
1401 */
1402 if (!di->vddadc_en_ac) {
1403 regulator_enable(di->regu);
1404 di->vddadc_en_ac = true;
1405 }
1406
1407 /* Check if the requested voltage or current is valid */
1408 volt_index = ab8500_voltage_to_regval(vset);
1409 curr_index = ab8500_current_to_regval(iset);
1410 input_curr_index = ab8500_current_to_regval(
Lee Jonesb0284de2012-11-30 10:09:42 +00001411 di->bm->chg_params->ac_curr_max);
Arun Murthy84edbee2012-02-29 21:54:26 +05301412 if (volt_index < 0 || curr_index < 0 || input_curr_index < 0) {
1413 dev_err(di->dev,
1414 "Charger voltage or current too high, "
1415 "charging not started\n");
1416 return -ENXIO;
1417 }
1418
1419 /* ChVoltLevel: maximum battery charging voltage */
1420 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1421 AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1422 if (ret) {
1423 dev_err(di->dev, "%s write failed\n", __func__);
1424 return ret;
1425 }
1426 /* MainChInputCurr: current that can be drawn from the charger*/
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001427 ret = ab8500_charger_set_main_in_curr(di,
Lee Jonesb0284de2012-11-30 10:09:42 +00001428 di->bm->chg_params->ac_curr_max);
Arun Murthy84edbee2012-02-29 21:54:26 +05301429 if (ret) {
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001430 dev_err(di->dev, "%s Failed to set MainChInputCurr\n",
1431 __func__);
Arun Murthy84edbee2012-02-29 21:54:26 +05301432 return ret;
1433 }
1434 /* ChOutputCurentLevel: protected output current */
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001435 ret = ab8500_charger_set_output_curr(di, iset);
Arun Murthy84edbee2012-02-29 21:54:26 +05301436 if (ret) {
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001437 dev_err(di->dev, "%s "
1438 "Failed to set ChOutputCurentLevel\n",
1439 __func__);
Arun Murthy84edbee2012-02-29 21:54:26 +05301440 return ret;
1441 }
1442
1443 /* Check if VBAT overshoot control should be enabled */
Lee Jonesb0284de2012-11-30 10:09:42 +00001444 if (!di->bm->enable_overshoot)
Arun Murthy84edbee2012-02-29 21:54:26 +05301445 overshoot = MAIN_CH_NO_OVERSHOOT_ENA_N;
1446
1447 /* Enable Main Charger */
1448 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1449 AB8500_MCH_CTRL1, MAIN_CH_ENA | overshoot);
1450 if (ret) {
1451 dev_err(di->dev, "%s write failed\n", __func__);
1452 return ret;
1453 }
1454
1455 /* Power on charging LED indication */
1456 ret = ab8500_charger_led_en(di, true);
1457 if (ret < 0)
1458 dev_err(di->dev, "failed to enable LED\n");
1459
1460 di->ac.charger_online = 1;
1461 } else {
1462 /* Disable AC charging */
1463 if (is_ab8500_1p1_or_earlier(di->parent)) {
1464 /*
1465 * For ABB revision 1.0 and 1.1 there is a bug in the
1466 * watchdog logic. That means we have to continously
1467 * kick the charger watchdog even when no charger is
1468 * connected. This is only valid once the AC charger
1469 * has been enabled. This is a bug that is not handled
1470 * by the algorithm and the watchdog have to be kicked
1471 * by the charger driver when the AC charger
1472 * is disabled
1473 */
1474 if (di->ac_conn) {
1475 queue_delayed_work(di->charger_wq,
1476 &di->kick_wd_work,
1477 round_jiffies(WD_KICK_INTERVAL));
1478 }
1479
1480 /*
1481 * We can't turn off charging completely
1482 * due to a bug in AB8500 cut1.
1483 * If we do, charging will not start again.
1484 * That is why we set the lowest voltage
1485 * and current possible
1486 */
1487 ret = abx500_set_register_interruptible(di->dev,
1488 AB8500_CHARGER,
1489 AB8500_CH_VOLT_LVL_REG, CH_VOL_LVL_3P5);
1490 if (ret) {
1491 dev_err(di->dev,
1492 "%s write failed\n", __func__);
1493 return ret;
1494 }
1495
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001496 ret = ab8500_charger_set_output_curr(di, 0);
Arun Murthy84edbee2012-02-29 21:54:26 +05301497 if (ret) {
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001498 dev_err(di->dev, "%s "
1499 "Failed to set ChOutputCurentLevel\n",
1500 __func__);
Arun Murthy84edbee2012-02-29 21:54:26 +05301501 return ret;
1502 }
1503 } else {
1504 ret = abx500_set_register_interruptible(di->dev,
1505 AB8500_CHARGER,
1506 AB8500_MCH_CTRL1, 0);
1507 if (ret) {
1508 dev_err(di->dev,
1509 "%s write failed\n", __func__);
1510 return ret;
1511 }
1512 }
1513
1514 ret = ab8500_charger_led_en(di, false);
1515 if (ret < 0)
1516 dev_err(di->dev, "failed to disable LED\n");
1517
1518 di->ac.charger_online = 0;
1519 di->ac.wd_expired = false;
1520
1521 /* Disable regulator if enabled */
1522 if (di->vddadc_en_ac) {
1523 regulator_disable(di->regu);
1524 di->vddadc_en_ac = false;
1525 }
1526
1527 dev_dbg(di->dev, "%s Disabled AC charging\n", __func__);
1528 }
1529 ab8500_power_supply_changed(di, &di->ac_chg.psy);
1530
1531 return ret;
1532}
1533
1534/**
1535 * ab8500_charger_usb_en() - enable usb charging
1536 * @di: pointer to the ab8500_charger structure
1537 * @enable: enable/disable flag
1538 * @vset: charging voltage
1539 * @ich_out: charger output current
1540 *
1541 * Enable/Disable USB charging and turns on/off the charging led respectively.
1542 * Returns error code in case of failure else 0(on success)
1543 */
1544static int ab8500_charger_usb_en(struct ux500_charger *charger,
1545 int enable, int vset, int ich_out)
1546{
1547 int ret;
1548 int volt_index;
1549 int curr_index;
1550 u8 overshoot = 0;
1551
1552 struct ab8500_charger *di = to_ab8500_charger_usb_device_info(charger);
1553
1554 if (enable) {
1555 /* Check if USB is connected */
1556 if (!di->usb.charger_connected) {
1557 dev_err(di->dev, "USB charger not connected\n");
1558 return -ENXIO;
1559 }
1560
1561 /*
1562 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1563 * will be triggered everytime we enable the VDD ADC supply.
1564 * This will turn off charging for a short while.
1565 * It can be avoided by having the supply on when
1566 * there is a charger enabled. Normally the VDD ADC supply
1567 * is enabled everytime a GPADC conversion is triggered. We will
1568 * force it to be enabled from this driver to have
1569 * the GPADC module independant of the AB8500 chargers
1570 */
1571 if (!di->vddadc_en_usb) {
1572 regulator_enable(di->regu);
1573 di->vddadc_en_usb = true;
1574 }
1575
1576 /* Enable USB charging */
1577 dev_dbg(di->dev, "Enable USB: %dmV %dmA\n", vset, ich_out);
1578
1579 /* Check if the requested voltage or current is valid */
1580 volt_index = ab8500_voltage_to_regval(vset);
1581 curr_index = ab8500_current_to_regval(ich_out);
1582 if (volt_index < 0 || curr_index < 0) {
1583 dev_err(di->dev,
1584 "Charger voltage or current too high, "
1585 "charging not started\n");
1586 return -ENXIO;
1587 }
1588
1589 /* ChVoltLevel: max voltage upto which battery can be charged */
1590 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1591 AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1592 if (ret) {
1593 dev_err(di->dev, "%s write failed\n", __func__);
1594 return ret;
1595 }
Arun Murthy84edbee2012-02-29 21:54:26 +05301596 /* Check if VBAT overshoot control should be enabled */
Lee Jonesb0284de2012-11-30 10:09:42 +00001597 if (!di->bm->enable_overshoot)
Arun Murthy84edbee2012-02-29 21:54:26 +05301598 overshoot = USB_CHG_NO_OVERSHOOT_ENA_N;
1599
1600 /* Enable USB Charger */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001601 dev_dbg(di->dev,
1602 "Enabling USB with write to AB8500_USBCH_CTRL1_REG\n");
Arun Murthy84edbee2012-02-29 21:54:26 +05301603 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1604 AB8500_USBCH_CTRL1_REG, USB_CH_ENA | overshoot);
1605 if (ret) {
1606 dev_err(di->dev, "%s write failed\n", __func__);
1607 return ret;
1608 }
1609
1610 /* If success power on charging LED indication */
1611 ret = ab8500_charger_led_en(di, true);
1612 if (ret < 0)
1613 dev_err(di->dev, "failed to enable LED\n");
1614
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001615 di->usb.charger_online = 1;
1616
1617 /* USBChInputCurr: current that can be drawn from the usb */
1618 ret = ab8500_charger_set_vbus_in_curr(di, di->max_usb_in_curr);
1619 if (ret) {
1620 dev_err(di->dev, "setting USBChInputCurr failed\n");
1621 return ret;
1622 }
1623
1624 /* ChOutputCurentLevel: protected output current */
1625 ret = ab8500_charger_set_output_curr(di, ich_out);
1626 if (ret) {
1627 dev_err(di->dev, "%s "
1628 "Failed to set ChOutputCurentLevel\n",
1629 __func__);
1630 return ret;
1631 }
1632
Arun Murthy84edbee2012-02-29 21:54:26 +05301633 queue_delayed_work(di->charger_wq, &di->check_vbat_work, HZ);
1634
Arun Murthy84edbee2012-02-29 21:54:26 +05301635 } else {
1636 /* Disable USB charging */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001637 dev_dbg(di->dev, "%s Disabled USB charging\n", __func__);
Arun Murthy84edbee2012-02-29 21:54:26 +05301638 ret = abx500_set_register_interruptible(di->dev,
1639 AB8500_CHARGER,
1640 AB8500_USBCH_CTRL1_REG, 0);
1641 if (ret) {
1642 dev_err(di->dev,
1643 "%s write failed\n", __func__);
1644 return ret;
1645 }
1646
1647 ret = ab8500_charger_led_en(di, false);
1648 if (ret < 0)
1649 dev_err(di->dev, "failed to disable LED\n");
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001650 /* USBChInputCurr: current that can be drawn from the usb */
1651 ret = ab8500_charger_set_vbus_in_curr(di, 0);
1652 if (ret) {
1653 dev_err(di->dev, "setting USBChInputCurr failed\n");
1654 return ret;
1655 }
Arun Murthy84edbee2012-02-29 21:54:26 +05301656
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001657 /* ChOutputCurentLevel: protected output current */
1658 ret = ab8500_charger_set_output_curr(di, 0);
1659 if (ret) {
1660 dev_err(di->dev, "%s "
1661 "Failed to reset ChOutputCurentLevel\n",
1662 __func__);
1663 return ret;
1664 }
Arun Murthy84edbee2012-02-29 21:54:26 +05301665 di->usb.charger_online = 0;
1666 di->usb.wd_expired = false;
1667
1668 /* Disable regulator if enabled */
1669 if (di->vddadc_en_usb) {
1670 regulator_disable(di->regu);
1671 di->vddadc_en_usb = false;
1672 }
1673
1674 dev_dbg(di->dev, "%s Disabled USB charging\n", __func__);
1675
1676 /* Cancel any pending Vbat check work */
1677 if (delayed_work_pending(&di->check_vbat_work))
1678 cancel_delayed_work(&di->check_vbat_work);
1679
1680 }
1681 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1682
1683 return ret;
1684}
1685
Lee Jones88917162013-02-13 11:39:19 +00001686static int ab8500_external_charger_prepare(struct notifier_block *charger_nb,
1687 unsigned long event, void *data)
1688{
1689 int ret;
1690 struct device *dev = data;
1691 /*Toggle External charger control pin*/
1692 ret = abx500_set_register_interruptible(dev, AB8500_SYS_CTRL1_BLOCK,
1693 AB8500_SYS_CHARGER_CONTROL_REG,
1694 EXTERNAL_CHARGER_DISABLE_REG_VAL);
1695 if (ret < 0) {
1696 dev_err(dev, "write reg failed %d\n", ret);
1697 goto out;
1698 }
1699 ret = abx500_set_register_interruptible(dev, AB8500_SYS_CTRL1_BLOCK,
1700 AB8500_SYS_CHARGER_CONTROL_REG,
1701 EXTERNAL_CHARGER_ENABLE_REG_VAL);
1702 if (ret < 0)
1703 dev_err(dev, "Write reg failed %d\n", ret);
1704
1705out:
1706 return ret;
1707}
1708
Arun Murthy84edbee2012-02-29 21:54:26 +05301709/**
Lee Jones4dcdf572013-02-14 09:24:10 +00001710 * ab8500_charger_usb_check_enable() - enable usb charging
1711 * @charger: pointer to the ux500_charger structure
1712 * @vset: charging voltage
1713 * @iset: charger output current
1714 *
1715 * Check if the VBUS charger has been disconnected and reconnected without
1716 * AB8500 rising an interrupt. Returns 0 on success.
1717 */
1718static int ab8500_charger_usb_check_enable(struct ux500_charger *charger,
1719 int vset, int iset)
1720{
1721 u8 usbch_ctrl1 = 0;
1722 int ret = 0;
1723
1724 struct ab8500_charger *di = to_ab8500_charger_usb_device_info(charger);
1725
1726 if (!di->usb.charger_connected)
1727 return ret;
1728
1729 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1730 AB8500_USBCH_CTRL1_REG, &usbch_ctrl1);
1731 if (ret < 0) {
1732 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
1733 return ret;
1734 }
1735 dev_dbg(di->dev, "USB charger ctrl: 0x%02x\n", usbch_ctrl1);
1736
1737 if (!(usbch_ctrl1 & USB_CH_ENA)) {
1738 dev_info(di->dev, "Charging has been disabled abnormally and will be re-enabled\n");
1739
1740 ret = abx500_mask_and_set_register_interruptible(di->dev,
1741 AB8500_CHARGER, AB8500_CHARGER_CTRL,
1742 DROP_COUNT_RESET, DROP_COUNT_RESET);
1743 if (ret < 0) {
1744 dev_err(di->dev, "ab8500 write failed %d\n", __LINE__);
1745 return ret;
1746 }
1747
1748 ret = ab8500_charger_usb_en(&di->usb_chg, true, vset, iset);
1749 if (ret < 0) {
1750 dev_err(di->dev, "Failed to enable VBUS charger %d\n",
1751 __LINE__);
1752 return ret;
1753 }
1754 }
1755 return ret;
1756}
1757
1758/**
1759 * ab8500_charger_ac_check_enable() - enable usb charging
1760 * @charger: pointer to the ux500_charger structure
1761 * @vset: charging voltage
1762 * @iset: charger output current
1763 *
1764 * Check if the AC charger has been disconnected and reconnected without
1765 * AB8500 rising an interrupt. Returns 0 on success.
1766 */
1767static int ab8500_charger_ac_check_enable(struct ux500_charger *charger,
1768 int vset, int iset)
1769{
1770 u8 mainch_ctrl1 = 0;
1771 int ret = 0;
1772
1773 struct ab8500_charger *di = to_ab8500_charger_ac_device_info(charger);
1774
1775 if (!di->ac.charger_connected)
1776 return ret;
1777
1778 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1779 AB8500_MCH_CTRL1, &mainch_ctrl1);
1780 if (ret < 0) {
1781 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
1782 return ret;
1783 }
1784 dev_dbg(di->dev, "AC charger ctrl: 0x%02x\n", mainch_ctrl1);
1785
1786 if (!(mainch_ctrl1 & MAIN_CH_ENA)) {
1787 dev_info(di->dev, "Charging has been disabled abnormally and will be re-enabled\n");
1788
1789 ret = abx500_mask_and_set_register_interruptible(di->dev,
1790 AB8500_CHARGER, AB8500_CHARGER_CTRL,
1791 DROP_COUNT_RESET, DROP_COUNT_RESET);
1792
1793 if (ret < 0) {
1794 dev_err(di->dev, "ab8500 write failed %d\n", __LINE__);
1795 return ret;
1796 }
1797
1798 ret = ab8500_charger_ac_en(&di->usb_chg, true, vset, iset);
1799 if (ret < 0) {
1800 dev_err(di->dev, "failed to enable AC charger %d\n",
1801 __LINE__);
1802 return ret;
1803 }
1804 }
1805 return ret;
1806}
1807
1808/**
Arun Murthy84edbee2012-02-29 21:54:26 +05301809 * ab8500_charger_watchdog_kick() - kick charger watchdog
1810 * @di: pointer to the ab8500_charger structure
1811 *
1812 * Kick charger watchdog
1813 * Returns error code in case of failure else 0(on success)
1814 */
1815static int ab8500_charger_watchdog_kick(struct ux500_charger *charger)
1816{
1817 int ret;
1818 struct ab8500_charger *di;
1819
1820 if (charger->psy.type == POWER_SUPPLY_TYPE_MAINS)
1821 di = to_ab8500_charger_ac_device_info(charger);
1822 else if (charger->psy.type == POWER_SUPPLY_TYPE_USB)
1823 di = to_ab8500_charger_usb_device_info(charger);
1824 else
1825 return -ENXIO;
1826
1827 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1828 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
1829 if (ret)
1830 dev_err(di->dev, "Failed to kick WD!\n");
1831
1832 return ret;
1833}
1834
1835/**
1836 * ab8500_charger_update_charger_current() - update charger current
1837 * @di: pointer to the ab8500_charger structure
1838 *
1839 * Update the charger output current for the specified charger
1840 * Returns error code in case of failure else 0(on success)
1841 */
1842static int ab8500_charger_update_charger_current(struct ux500_charger *charger,
1843 int ich_out)
1844{
1845 int ret;
Arun Murthy84edbee2012-02-29 21:54:26 +05301846 struct ab8500_charger *di;
1847
1848 if (charger->psy.type == POWER_SUPPLY_TYPE_MAINS)
1849 di = to_ab8500_charger_ac_device_info(charger);
1850 else if (charger->psy.type == POWER_SUPPLY_TYPE_USB)
1851 di = to_ab8500_charger_usb_device_info(charger);
1852 else
1853 return -ENXIO;
1854
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001855 ret = ab8500_charger_set_output_curr(di, ich_out);
Arun Murthy84edbee2012-02-29 21:54:26 +05301856 if (ret) {
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001857 dev_err(di->dev, "%s "
1858 "Failed to set ChOutputCurentLevel\n",
1859 __func__);
Arun Murthy84edbee2012-02-29 21:54:26 +05301860 return ret;
1861 }
1862
1863 /* Reset the main and usb drop input current measurement counter */
1864 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
Lee Jones4dcdf572013-02-14 09:24:10 +00001865 AB8500_CHARGER_CTRL, DROP_COUNT_RESET);
Arun Murthy84edbee2012-02-29 21:54:26 +05301866 if (ret) {
1867 dev_err(di->dev, "%s write failed\n", __func__);
1868 return ret;
1869 }
1870
1871 return ret;
1872}
1873
1874static int ab8500_charger_get_ext_psy_data(struct device *dev, void *data)
1875{
1876 struct power_supply *psy;
1877 struct power_supply *ext;
1878 struct ab8500_charger *di;
1879 union power_supply_propval ret;
1880 int i, j;
1881 bool psy_found = false;
1882 struct ux500_charger *usb_chg;
1883
1884 usb_chg = (struct ux500_charger *)data;
1885 psy = &usb_chg->psy;
1886
1887 di = to_ab8500_charger_usb_device_info(usb_chg);
1888
1889 ext = dev_get_drvdata(dev);
1890
1891 /* For all psy where the driver name appears in any supplied_to */
1892 for (i = 0; i < ext->num_supplicants; i++) {
1893 if (!strcmp(ext->supplied_to[i], psy->name))
1894 psy_found = true;
1895 }
1896
1897 if (!psy_found)
1898 return 0;
1899
1900 /* Go through all properties for the psy */
1901 for (j = 0; j < ext->num_properties; j++) {
1902 enum power_supply_property prop;
1903 prop = ext->properties[j];
1904
1905 if (ext->get_property(ext, prop, &ret))
1906 continue;
1907
1908 switch (prop) {
1909 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1910 switch (ext->type) {
1911 case POWER_SUPPLY_TYPE_BATTERY:
1912 di->vbat = ret.intval / 1000;
1913 break;
1914 default:
1915 break;
1916 }
1917 break;
1918 default:
1919 break;
1920 }
1921 }
1922 return 0;
1923}
1924
1925/**
1926 * ab8500_charger_check_vbat_work() - keep vbus current within spec
1927 * @work pointer to the work_struct structure
1928 *
1929 * Due to a asic bug it is necessary to lower the input current to the vbus
1930 * charger when charging with at some specific levels. This issue is only valid
1931 * for below a certain battery voltage. This function makes sure that the
1932 * the allowed current limit isn't exceeded.
1933 */
1934static void ab8500_charger_check_vbat_work(struct work_struct *work)
1935{
1936 int t = 10;
1937 struct ab8500_charger *di = container_of(work,
1938 struct ab8500_charger, check_vbat_work.work);
1939
1940 class_for_each_device(power_supply_class, NULL,
1941 &di->usb_chg.psy, ab8500_charger_get_ext_psy_data);
1942
1943 /* First run old_vbat is 0. */
1944 if (di->old_vbat == 0)
1945 di->old_vbat = di->vbat;
1946
1947 if (!((di->old_vbat <= VBAT_TRESH_IP_CUR_RED &&
1948 di->vbat <= VBAT_TRESH_IP_CUR_RED) ||
1949 (di->old_vbat > VBAT_TRESH_IP_CUR_RED &&
1950 di->vbat > VBAT_TRESH_IP_CUR_RED))) {
1951
1952 dev_dbg(di->dev, "Vbat did cross threshold, curr: %d, new: %d,"
1953 " old: %d\n", di->max_usb_in_curr, di->vbat,
1954 di->old_vbat);
1955 ab8500_charger_set_vbus_in_curr(di, di->max_usb_in_curr);
1956 power_supply_changed(&di->usb_chg.psy);
1957 }
1958
1959 di->old_vbat = di->vbat;
1960
1961 /*
1962 * No need to check the battery voltage every second when not close to
1963 * the threshold.
1964 */
1965 if (di->vbat < (VBAT_TRESH_IP_CUR_RED + 100) &&
1966 (di->vbat > (VBAT_TRESH_IP_CUR_RED - 100)))
1967 t = 1;
1968
1969 queue_delayed_work(di->charger_wq, &di->check_vbat_work, t * HZ);
1970}
1971
1972/**
1973 * ab8500_charger_check_hw_failure_work() - check main charger failure
1974 * @work: pointer to the work_struct structure
1975 *
1976 * Work queue function for checking the main charger status
1977 */
1978static void ab8500_charger_check_hw_failure_work(struct work_struct *work)
1979{
1980 int ret;
1981 u8 reg_value;
1982
1983 struct ab8500_charger *di = container_of(work,
1984 struct ab8500_charger, check_hw_failure_work.work);
1985
1986 /* Check if the status bits for HW failure is still active */
1987 if (di->flags.mainextchnotok) {
1988 ret = abx500_get_register_interruptible(di->dev,
1989 AB8500_CHARGER, AB8500_CH_STATUS2_REG, &reg_value);
1990 if (ret < 0) {
1991 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1992 return;
1993 }
1994 if (!(reg_value & MAIN_CH_NOK)) {
1995 di->flags.mainextchnotok = false;
1996 ab8500_power_supply_changed(di, &di->ac_chg.psy);
1997 }
1998 }
1999 if (di->flags.vbus_ovv) {
2000 ret = abx500_get_register_interruptible(di->dev,
2001 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG,
2002 &reg_value);
2003 if (ret < 0) {
2004 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2005 return;
2006 }
2007 if (!(reg_value & VBUS_OVV_TH)) {
2008 di->flags.vbus_ovv = false;
2009 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2010 }
2011 }
2012 /* If we still have a failure, schedule a new check */
2013 if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
2014 queue_delayed_work(di->charger_wq,
2015 &di->check_hw_failure_work, round_jiffies(HZ));
2016 }
2017}
2018
2019/**
2020 * ab8500_charger_kick_watchdog_work() - kick the watchdog
2021 * @work: pointer to the work_struct structure
2022 *
2023 * Work queue function for kicking the charger watchdog.
2024 *
2025 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
2026 * logic. That means we have to continously kick the charger
2027 * watchdog even when no charger is connected. This is only
2028 * valid once the AC charger has been enabled. This is
2029 * a bug that is not handled by the algorithm and the
2030 * watchdog have to be kicked by the charger driver
2031 * when the AC charger is disabled
2032 */
2033static void ab8500_charger_kick_watchdog_work(struct work_struct *work)
2034{
2035 int ret;
2036
2037 struct ab8500_charger *di = container_of(work,
2038 struct ab8500_charger, kick_wd_work.work);
2039
2040 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
2041 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
2042 if (ret)
2043 dev_err(di->dev, "Failed to kick WD!\n");
2044
2045 /* Schedule a new watchdog kick */
2046 queue_delayed_work(di->charger_wq,
2047 &di->kick_wd_work, round_jiffies(WD_KICK_INTERVAL));
2048}
2049
2050/**
2051 * ab8500_charger_ac_work() - work to get and set main charger status
2052 * @work: pointer to the work_struct structure
2053 *
2054 * Work queue function for checking the main charger status
2055 */
2056static void ab8500_charger_ac_work(struct work_struct *work)
2057{
2058 int ret;
2059
2060 struct ab8500_charger *di = container_of(work,
2061 struct ab8500_charger, ac_work);
2062
2063 /*
2064 * Since we can't be sure that the events are received
2065 * synchronously, we have the check if the main charger is
2066 * connected by reading the status register
2067 */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002068 ret = ab8500_charger_detect_chargers(di, false);
Arun Murthy84edbee2012-02-29 21:54:26 +05302069 if (ret < 0)
2070 return;
2071
2072 if (ret & AC_PW_CONN) {
2073 di->ac.charger_connected = 1;
2074 di->ac_conn = true;
2075 } else {
2076 di->ac.charger_connected = 0;
2077 }
2078
2079 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2080 sysfs_notify(&di->ac_chg.psy.dev->kobj, NULL, "present");
2081}
2082
Lee Jonesb269fff2013-01-11 13:12:51 +00002083static void ab8500_charger_usb_attached_work(struct work_struct *work)
2084{
2085 struct ab8500_charger *di = container_of(work,
2086 struct ab8500_charger,
2087 usb_charger_attached_work.work);
2088 int usbch = (USB_CH_VBUSDROP | USB_CH_VBUSDETDBNC);
2089 int ret, i;
2090 u8 statval;
2091
2092 for (i = 0; i < 10; i++) {
2093 ret = abx500_get_register_interruptible(di->dev,
2094 AB8500_CHARGER,
2095 AB8500_CH_USBCH_STAT1_REG,
2096 &statval);
2097 if (ret < 0) {
2098 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
2099 goto reschedule;
2100 }
2101 if ((statval & usbch) != usbch)
2102 goto reschedule;
2103
2104 msleep(CHARGER_STATUS_POLL);
2105 }
2106
2107 ab8500_charger_usb_en(&di->usb_chg, 0, 0, 0);
2108
2109 mutex_lock(&di->charger_attached_mutex);
2110 mutex_unlock(&di->charger_attached_mutex);
2111
2112 return;
2113
2114reschedule:
2115 queue_delayed_work(di->charger_wq,
2116 &di->usb_charger_attached_work,
2117 HZ);
2118}
2119
2120static void ab8500_charger_ac_attached_work(struct work_struct *work)
2121{
2122
2123 struct ab8500_charger *di = container_of(work,
2124 struct ab8500_charger,
2125 ac_charger_attached_work.work);
2126 int mainch = (MAIN_CH_STATUS2_MAINCHGDROP |
2127 MAIN_CH_STATUS2_MAINCHARGERDETDBNC);
2128 int ret, i;
2129 u8 statval;
2130
2131 for (i = 0; i < 10; i++) {
2132 ret = abx500_get_register_interruptible(di->dev,
2133 AB8500_CHARGER,
2134 AB8500_CH_STATUS2_REG,
2135 &statval);
2136 if (ret < 0) {
2137 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
2138 goto reschedule;
2139 }
2140
2141 if ((statval & mainch) != mainch)
2142 goto reschedule;
2143
2144 msleep(CHARGER_STATUS_POLL);
2145 }
2146
2147 ab8500_charger_ac_en(&di->ac_chg, 0, 0, 0);
2148 queue_work(di->charger_wq, &di->ac_work);
2149
2150 mutex_lock(&di->charger_attached_mutex);
2151 mutex_unlock(&di->charger_attached_mutex);
2152
2153 return;
2154
2155reschedule:
2156 queue_delayed_work(di->charger_wq,
2157 &di->ac_charger_attached_work,
2158 HZ);
2159}
2160
Arun Murthy84edbee2012-02-29 21:54:26 +05302161/**
2162 * ab8500_charger_detect_usb_type_work() - work to detect USB type
2163 * @work: Pointer to the work_struct structure
2164 *
2165 * Detect the type of USB plugged
2166 */
Anton Vorontsov64eb9b02012-03-14 04:43:11 +04002167static void ab8500_charger_detect_usb_type_work(struct work_struct *work)
Arun Murthy84edbee2012-02-29 21:54:26 +05302168{
2169 int ret;
2170
2171 struct ab8500_charger *di = container_of(work,
2172 struct ab8500_charger, detect_usb_type_work);
2173
2174 /*
2175 * Since we can't be sure that the events are received
2176 * synchronously, we have the check if is
2177 * connected by reading the status register
2178 */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002179 ret = ab8500_charger_detect_chargers(di, false);
Arun Murthy84edbee2012-02-29 21:54:26 +05302180 if (ret < 0)
2181 return;
2182
2183 if (!(ret & USB_PW_CONN)) {
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002184 dev_dbg(di->dev, "%s di->vbus_detected = false\n", __func__);
2185 di->vbus_detected = false;
Arun Murthy84edbee2012-02-29 21:54:26 +05302186 ab8500_charger_set_usb_connected(di, false);
2187 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2188 } else {
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002189 dev_dbg(di->dev, "%s di->vbus_detected = true\n", __func__);
2190 di->vbus_detected = true;
Arun Murthy84edbee2012-02-29 21:54:26 +05302191
2192 if (is_ab8500_1p1_or_earlier(di->parent)) {
2193 ret = ab8500_charger_detect_usb_type(di);
2194 if (!ret) {
2195 ab8500_charger_set_usb_connected(di, true);
2196 ab8500_power_supply_changed(di,
2197 &di->usb_chg.psy);
2198 }
2199 } else {
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002200 /*
2201 * For ABB cut2.0 and onwards we have an IRQ,
Arun Murthy84edbee2012-02-29 21:54:26 +05302202 * USB_LINK_STATUS that will be triggered when the USB
2203 * link status changes. The exception is USB connected
2204 * during startup. Then we don't get a
2205 * USB_LINK_STATUS IRQ
2206 */
2207 if (di->vbus_detected_start) {
2208 di->vbus_detected_start = false;
2209 ret = ab8500_charger_detect_usb_type(di);
2210 if (!ret) {
2211 ab8500_charger_set_usb_connected(di,
2212 true);
2213 ab8500_power_supply_changed(di,
2214 &di->usb_chg.psy);
2215 }
2216 }
2217 }
2218 }
2219}
2220
2221/**
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002222 * ab8500_charger_usb_link_attach_work() - work to detect USB type
Marcus Cooper4b45f4a2013-01-11 13:13:04 +00002223 * @work: pointer to the work_struct structure
2224 *
2225 * Detect the type of USB plugged
2226 */
2227static void ab8500_charger_usb_link_attach_work(struct work_struct *work)
2228{
2229 struct ab8500_charger *di =
2230 container_of(work, struct ab8500_charger, attach_work.work);
2231 int ret;
2232
2233 /* Update maximum input current if USB enumeration is not detected */
2234 if (!di->usb.charger_online) {
2235 ret = ab8500_charger_set_vbus_in_curr(di, di->max_usb_in_curr);
2236 if (ret)
2237 return;
2238 }
2239
2240 ab8500_charger_set_usb_connected(di, true);
2241 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2242}
2243
2244/**
Arun Murthy84edbee2012-02-29 21:54:26 +05302245 * ab8500_charger_usb_link_status_work() - work to detect USB type
2246 * @work: pointer to the work_struct structure
2247 *
2248 * Detect the type of USB plugged
2249 */
2250static void ab8500_charger_usb_link_status_work(struct work_struct *work)
2251{
Henrik Sölverff380902012-04-17 15:51:01 +02002252 int detected_chargers;
Arun Murthy84edbee2012-02-29 21:54:26 +05302253 int ret;
Henrik Sölverff380902012-04-17 15:51:01 +02002254 u8 val;
Arun Murthy84edbee2012-02-29 21:54:26 +05302255
2256 struct ab8500_charger *di = container_of(work,
2257 struct ab8500_charger, usb_link_status_work);
2258
2259 /*
2260 * Since we can't be sure that the events are received
2261 * synchronously, we have the check if is
2262 * connected by reading the status register
2263 */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002264 detected_chargers = ab8500_charger_detect_chargers(di, false);
Henrik Sölverff380902012-04-17 15:51:01 +02002265 if (detected_chargers < 0)
Arun Murthy84edbee2012-02-29 21:54:26 +05302266 return;
2267
Henrik Sölverff380902012-04-17 15:51:01 +02002268 /*
2269 * Some chargers that breaks the USB spec is
2270 * identified as invalid by AB8500 and it refuse
2271 * to start the charging process. but by jumping
2272 * thru a few hoops it can be forced to start.
2273 */
2274 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
2275 AB8500_USB_LINE_STAT_REG, &val);
2276 if (ret >= 0)
2277 dev_dbg(di->dev, "UsbLineStatus register = 0x%02x\n", val);
2278 else
2279 dev_dbg(di->dev, "Error reading USB link status\n");
2280
2281 if (detected_chargers & USB_PW_CONN) {
2282 if (((val & AB8500_USB_LINK_STATUS) >> 3) == USB_STAT_NOT_VALID_LINK &&
2283 di->invalid_charger_detect_state == 0) {
2284 dev_dbg(di->dev, "Invalid charger detected, state= 0\n");
2285 /*Enable charger*/
2286 abx500_mask_and_set_register_interruptible(di->dev,
2287 AB8500_CHARGER, AB8500_USBCH_CTRL1_REG, 0x01, 0x01);
2288 /*Enable charger detection*/
2289 abx500_mask_and_set_register_interruptible(di->dev, AB8500_USB,
2290 AB8500_MCH_IPT_CURLVL_REG, 0x01, 0x01);
2291 di->invalid_charger_detect_state = 1;
2292 /*exit and wait for new link status interrupt.*/
2293 return;
2294
2295 }
2296 if (di->invalid_charger_detect_state == 1) {
2297 dev_dbg(di->dev, "Invalid charger detected, state= 1\n");
2298 /*Stop charger detection*/
2299 abx500_mask_and_set_register_interruptible(di->dev, AB8500_USB,
2300 AB8500_MCH_IPT_CURLVL_REG, 0x01, 0x00);
2301 /*Check link status*/
2302 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
2303 AB8500_USB_LINE_STAT_REG, &val);
2304 dev_dbg(di->dev, "USB link status= 0x%02x\n",
2305 (val & AB8500_USB_LINK_STATUS) >> 3);
2306 di->invalid_charger_detect_state = 2;
2307 }
2308 } else {
2309 di->invalid_charger_detect_state = 0;
2310 }
2311
2312 if (!(detected_chargers & USB_PW_CONN)) {
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002313 di->vbus_detected = false;
Arun Murthy84edbee2012-02-29 21:54:26 +05302314 ab8500_charger_set_usb_connected(di, false);
2315 ab8500_power_supply_changed(di, &di->usb_chg.psy);
Marcus Cooper4b45f4a2013-01-11 13:13:04 +00002316 return;
2317 }
Arun Murthy84edbee2012-02-29 21:54:26 +05302318
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002319 dev_dbg(di->dev,"%s di->vbus_detected = true\n",__func__);
2320 di->vbus_detected = true;
Marcus Cooper4b45f4a2013-01-11 13:13:04 +00002321 ret = ab8500_charger_read_usb_type(di);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002322 if (ret) {
2323 if (ret == -ENXIO) {
2324 /* No valid charger type detected */
2325 ab8500_charger_set_usb_connected(di, false);
2326 ab8500_power_supply_changed(di, &di->usb_chg.psy);
Arun Murthy84edbee2012-02-29 21:54:26 +05302327 }
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002328 return;
2329 }
2330
2331 if (di->usb_device_is_unrecognised) {
2332 dev_dbg(di->dev,
2333 "Potential Legacy Charger device. "
2334 "Delay work for %d msec for USB enum "
2335 "to finish",
2336 WAIT_ACA_RID_ENUMERATION);
2337 queue_delayed_work(di->charger_wq,
2338 &di->attach_work,
2339 msecs_to_jiffies(WAIT_ACA_RID_ENUMERATION));
2340 } else if (di->is_aca_rid == 1) {
2341 /* Only wait once */
2342 di->is_aca_rid++;
2343 dev_dbg(di->dev,
2344 "%s Wait %d msec for USB enum to finish",
2345 __func__, WAIT_ACA_RID_ENUMERATION);
2346 queue_delayed_work(di->charger_wq,
2347 &di->attach_work,
2348 msecs_to_jiffies(WAIT_ACA_RID_ENUMERATION));
2349 } else {
2350 queue_delayed_work(di->charger_wq,
2351 &di->attach_work,
2352 0);
Arun Murthy84edbee2012-02-29 21:54:26 +05302353 }
2354}
2355
2356static void ab8500_charger_usb_state_changed_work(struct work_struct *work)
2357{
2358 int ret;
2359 unsigned long flags;
2360
2361 struct ab8500_charger *di = container_of(work,
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002362 struct ab8500_charger, usb_state_changed_work.work);
Arun Murthy84edbee2012-02-29 21:54:26 +05302363
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002364 if (!di->vbus_detected) {
2365 dev_dbg(di->dev,
2366 "%s !di->vbus_detected\n",
2367 __func__);
Arun Murthy84edbee2012-02-29 21:54:26 +05302368 return;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002369 }
Arun Murthy84edbee2012-02-29 21:54:26 +05302370
2371 spin_lock_irqsave(&di->usb_state.usb_lock, flags);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002372 di->usb_state.state = di->usb_state.state_tmp;
2373 di->usb_state.usb_current = di->usb_state.usb_current_tmp;
Arun Murthy84edbee2012-02-29 21:54:26 +05302374 spin_unlock_irqrestore(&di->usb_state.usb_lock, flags);
2375
Arun Murthy84edbee2012-02-29 21:54:26 +05302376 dev_dbg(di->dev, "%s USB state: 0x%02x mA: %d\n",
2377 __func__, di->usb_state.state, di->usb_state.usb_current);
2378
2379 switch (di->usb_state.state) {
2380 case AB8500_BM_USB_STATE_RESET_HS:
2381 case AB8500_BM_USB_STATE_RESET_FS:
2382 case AB8500_BM_USB_STATE_SUSPEND:
2383 case AB8500_BM_USB_STATE_MAX:
2384 ab8500_charger_set_usb_connected(di, false);
2385 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2386 break;
2387
2388 case AB8500_BM_USB_STATE_RESUME:
2389 /*
2390 * when suspend->resume there should be delay
2391 * of 1sec for enabling charging
2392 */
2393 msleep(1000);
2394 /* Intentional fall through */
2395 case AB8500_BM_USB_STATE_CONFIGURED:
2396 /*
2397 * USB is configured, enable charging with the charging
2398 * input current obtained from USB driver
2399 */
2400 if (!ab8500_charger_get_usb_cur(di)) {
2401 /* Update maximum input current */
2402 ret = ab8500_charger_set_vbus_in_curr(di,
2403 di->max_usb_in_curr);
2404 if (ret)
2405 return;
2406
2407 ab8500_charger_set_usb_connected(di, true);
2408 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2409 }
2410 break;
2411
2412 default:
2413 break;
2414 };
2415}
2416
2417/**
2418 * ab8500_charger_check_usbchargernotok_work() - check USB chg not ok status
2419 * @work: pointer to the work_struct structure
2420 *
2421 * Work queue function for checking the USB charger Not OK status
2422 */
2423static void ab8500_charger_check_usbchargernotok_work(struct work_struct *work)
2424{
2425 int ret;
2426 u8 reg_value;
2427 bool prev_status;
2428
2429 struct ab8500_charger *di = container_of(work,
2430 struct ab8500_charger, check_usbchgnotok_work.work);
2431
2432 /* Check if the status bit for usbchargernotok is still active */
2433 ret = abx500_get_register_interruptible(di->dev,
2434 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, &reg_value);
2435 if (ret < 0) {
2436 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2437 return;
2438 }
2439 prev_status = di->flags.usbchargernotok;
2440
2441 if (reg_value & VBUS_CH_NOK) {
2442 di->flags.usbchargernotok = true;
2443 /* Check again in 1sec */
2444 queue_delayed_work(di->charger_wq,
2445 &di->check_usbchgnotok_work, HZ);
2446 } else {
2447 di->flags.usbchargernotok = false;
2448 di->flags.vbus_collapse = false;
2449 }
2450
2451 if (prev_status != di->flags.usbchargernotok)
2452 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2453}
2454
2455/**
2456 * ab8500_charger_check_main_thermal_prot_work() - check main thermal status
2457 * @work: pointer to the work_struct structure
2458 *
2459 * Work queue function for checking the Main thermal prot status
2460 */
2461static void ab8500_charger_check_main_thermal_prot_work(
2462 struct work_struct *work)
2463{
2464 int ret;
2465 u8 reg_value;
2466
2467 struct ab8500_charger *di = container_of(work,
2468 struct ab8500_charger, check_main_thermal_prot_work);
2469
2470 /* Check if the status bit for main_thermal_prot is still active */
2471 ret = abx500_get_register_interruptible(di->dev,
2472 AB8500_CHARGER, AB8500_CH_STATUS2_REG, &reg_value);
2473 if (ret < 0) {
2474 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2475 return;
2476 }
2477 if (reg_value & MAIN_CH_TH_PROT)
2478 di->flags.main_thermal_prot = true;
2479 else
2480 di->flags.main_thermal_prot = false;
2481
2482 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2483}
2484
2485/**
2486 * ab8500_charger_check_usb_thermal_prot_work() - check usb thermal status
2487 * @work: pointer to the work_struct structure
2488 *
2489 * Work queue function for checking the USB thermal prot status
2490 */
2491static void ab8500_charger_check_usb_thermal_prot_work(
2492 struct work_struct *work)
2493{
2494 int ret;
2495 u8 reg_value;
2496
2497 struct ab8500_charger *di = container_of(work,
2498 struct ab8500_charger, check_usb_thermal_prot_work);
2499
2500 /* Check if the status bit for usb_thermal_prot is still active */
2501 ret = abx500_get_register_interruptible(di->dev,
2502 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, &reg_value);
2503 if (ret < 0) {
2504 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2505 return;
2506 }
2507 if (reg_value & USB_CH_TH_PROT)
2508 di->flags.usb_thermal_prot = true;
2509 else
2510 di->flags.usb_thermal_prot = false;
2511
2512 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2513}
2514
2515/**
2516 * ab8500_charger_mainchunplugdet_handler() - main charger unplugged
2517 * @irq: interrupt number
2518 * @_di: pointer to the ab8500_charger structure
2519 *
2520 * Returns IRQ status(IRQ_HANDLED)
2521 */
2522static irqreturn_t ab8500_charger_mainchunplugdet_handler(int irq, void *_di)
2523{
2524 struct ab8500_charger *di = _di;
2525
2526 dev_dbg(di->dev, "Main charger unplugged\n");
2527 queue_work(di->charger_wq, &di->ac_work);
2528
Lee Jonesb269fff2013-01-11 13:12:51 +00002529 cancel_delayed_work_sync(&di->ac_charger_attached_work);
2530 mutex_lock(&di->charger_attached_mutex);
2531 mutex_unlock(&di->charger_attached_mutex);
2532
Arun Murthy84edbee2012-02-29 21:54:26 +05302533 return IRQ_HANDLED;
2534}
2535
2536/**
2537 * ab8500_charger_mainchplugdet_handler() - main charger plugged
2538 * @irq: interrupt number
2539 * @_di: pointer to the ab8500_charger structure
2540 *
2541 * Returns IRQ status(IRQ_HANDLED)
2542 */
2543static irqreturn_t ab8500_charger_mainchplugdet_handler(int irq, void *_di)
2544{
2545 struct ab8500_charger *di = _di;
2546
2547 dev_dbg(di->dev, "Main charger plugged\n");
2548 queue_work(di->charger_wq, &di->ac_work);
2549
Lee Jonesb269fff2013-01-11 13:12:51 +00002550 mutex_lock(&di->charger_attached_mutex);
2551 mutex_unlock(&di->charger_attached_mutex);
2552 queue_delayed_work(di->charger_wq,
2553 &di->ac_charger_attached_work,
2554 HZ);
Arun Murthy84edbee2012-02-29 21:54:26 +05302555 return IRQ_HANDLED;
2556}
2557
2558/**
2559 * ab8500_charger_mainextchnotok_handler() - main charger not ok
2560 * @irq: interrupt number
2561 * @_di: pointer to the ab8500_charger structure
2562 *
2563 * Returns IRQ status(IRQ_HANDLED)
2564 */
2565static irqreturn_t ab8500_charger_mainextchnotok_handler(int irq, void *_di)
2566{
2567 struct ab8500_charger *di = _di;
2568
2569 dev_dbg(di->dev, "Main charger not ok\n");
2570 di->flags.mainextchnotok = true;
2571 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2572
2573 /* Schedule a new HW failure check */
2574 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
2575
2576 return IRQ_HANDLED;
2577}
2578
2579/**
2580 * ab8500_charger_mainchthprotr_handler() - Die temp is above main charger
2581 * thermal protection threshold
2582 * @irq: interrupt number
2583 * @_di: pointer to the ab8500_charger structure
2584 *
2585 * Returns IRQ status(IRQ_HANDLED)
2586 */
2587static irqreturn_t ab8500_charger_mainchthprotr_handler(int irq, void *_di)
2588{
2589 struct ab8500_charger *di = _di;
2590
2591 dev_dbg(di->dev,
2592 "Die temp above Main charger thermal protection threshold\n");
2593 queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
2594
2595 return IRQ_HANDLED;
2596}
2597
2598/**
2599 * ab8500_charger_mainchthprotf_handler() - Die temp is below main charger
2600 * thermal protection threshold
2601 * @irq: interrupt number
2602 * @_di: pointer to the ab8500_charger structure
2603 *
2604 * Returns IRQ status(IRQ_HANDLED)
2605 */
2606static irqreturn_t ab8500_charger_mainchthprotf_handler(int irq, void *_di)
2607{
2608 struct ab8500_charger *di = _di;
2609
2610 dev_dbg(di->dev,
2611 "Die temp ok for Main charger thermal protection threshold\n");
2612 queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
2613
2614 return IRQ_HANDLED;
2615}
2616
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002617static void ab8500_charger_vbus_drop_end_work(struct work_struct *work)
2618{
2619 struct ab8500_charger *di = container_of(work,
2620 struct ab8500_charger, vbus_drop_end_work.work);
2621
2622 di->flags.vbus_drop_end = false;
2623
2624 /* Reset the drop counter */
2625 abx500_set_register_interruptible(di->dev,
2626 AB8500_CHARGER, AB8500_CHARGER_CTRL, 0x01);
2627
2628 if (di->usb.charger_connected)
2629 ab8500_charger_set_vbus_in_curr(di, di->max_usb_in_curr);
2630}
2631
Arun Murthy84edbee2012-02-29 21:54:26 +05302632/**
2633 * ab8500_charger_vbusdetf_handler() - VBUS falling detected
2634 * @irq: interrupt number
2635 * @_di: pointer to the ab8500_charger structure
2636 *
2637 * Returns IRQ status(IRQ_HANDLED)
2638 */
2639static irqreturn_t ab8500_charger_vbusdetf_handler(int irq, void *_di)
2640{
2641 struct ab8500_charger *di = _di;
2642
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002643 di->vbus_detected = false;
Arun Murthy84edbee2012-02-29 21:54:26 +05302644 dev_dbg(di->dev, "VBUS falling detected\n");
2645 queue_work(di->charger_wq, &di->detect_usb_type_work);
2646
2647 return IRQ_HANDLED;
2648}
2649
2650/**
2651 * ab8500_charger_vbusdetr_handler() - VBUS rising detected
2652 * @irq: interrupt number
2653 * @_di: pointer to the ab8500_charger structure
2654 *
2655 * Returns IRQ status(IRQ_HANDLED)
2656 */
2657static irqreturn_t ab8500_charger_vbusdetr_handler(int irq, void *_di)
2658{
2659 struct ab8500_charger *di = _di;
2660
2661 di->vbus_detected = true;
2662 dev_dbg(di->dev, "VBUS rising detected\n");
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002663
Arun Murthy84edbee2012-02-29 21:54:26 +05302664 queue_work(di->charger_wq, &di->detect_usb_type_work);
2665
2666 return IRQ_HANDLED;
2667}
2668
2669/**
2670 * ab8500_charger_usblinkstatus_handler() - USB link status has changed
2671 * @irq: interrupt number
2672 * @_di: pointer to the ab8500_charger structure
2673 *
2674 * Returns IRQ status(IRQ_HANDLED)
2675 */
2676static irqreturn_t ab8500_charger_usblinkstatus_handler(int irq, void *_di)
2677{
2678 struct ab8500_charger *di = _di;
2679
2680 dev_dbg(di->dev, "USB link status changed\n");
2681
2682 queue_work(di->charger_wq, &di->usb_link_status_work);
2683
2684 return IRQ_HANDLED;
2685}
2686
2687/**
2688 * ab8500_charger_usbchthprotr_handler() - Die temp is above usb charger
2689 * thermal protection threshold
2690 * @irq: interrupt number
2691 * @_di: pointer to the ab8500_charger structure
2692 *
2693 * Returns IRQ status(IRQ_HANDLED)
2694 */
2695static irqreturn_t ab8500_charger_usbchthprotr_handler(int irq, void *_di)
2696{
2697 struct ab8500_charger *di = _di;
2698
2699 dev_dbg(di->dev,
2700 "Die temp above USB charger thermal protection threshold\n");
2701 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2702
2703 return IRQ_HANDLED;
2704}
2705
2706/**
2707 * ab8500_charger_usbchthprotf_handler() - Die temp is below usb charger
2708 * thermal protection threshold
2709 * @irq: interrupt number
2710 * @_di: pointer to the ab8500_charger structure
2711 *
2712 * Returns IRQ status(IRQ_HANDLED)
2713 */
2714static irqreturn_t ab8500_charger_usbchthprotf_handler(int irq, void *_di)
2715{
2716 struct ab8500_charger *di = _di;
2717
2718 dev_dbg(di->dev,
2719 "Die temp ok for USB charger thermal protection threshold\n");
2720 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2721
2722 return IRQ_HANDLED;
2723}
2724
2725/**
2726 * ab8500_charger_usbchargernotokr_handler() - USB charger not ok detected
2727 * @irq: interrupt number
2728 * @_di: pointer to the ab8500_charger structure
2729 *
2730 * Returns IRQ status(IRQ_HANDLED)
2731 */
2732static irqreturn_t ab8500_charger_usbchargernotokr_handler(int irq, void *_di)
2733{
2734 struct ab8500_charger *di = _di;
2735
2736 dev_dbg(di->dev, "Not allowed USB charger detected\n");
2737 queue_delayed_work(di->charger_wq, &di->check_usbchgnotok_work, 0);
2738
2739 return IRQ_HANDLED;
2740}
2741
2742/**
2743 * ab8500_charger_chwdexp_handler() - Charger watchdog expired
2744 * @irq: interrupt number
2745 * @_di: pointer to the ab8500_charger structure
2746 *
2747 * Returns IRQ status(IRQ_HANDLED)
2748 */
2749static irqreturn_t ab8500_charger_chwdexp_handler(int irq, void *_di)
2750{
2751 struct ab8500_charger *di = _di;
2752
2753 dev_dbg(di->dev, "Charger watchdog expired\n");
2754
2755 /*
2756 * The charger that was online when the watchdog expired
2757 * needs to be restarted for charging to start again
2758 */
2759 if (di->ac.charger_online) {
2760 di->ac.wd_expired = true;
2761 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2762 }
2763 if (di->usb.charger_online) {
2764 di->usb.wd_expired = true;
2765 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2766 }
2767
2768 return IRQ_HANDLED;
2769}
2770
2771/**
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002772 * ab8500_charger_vbuschdropend_handler() - VBUS drop removed
2773 * @irq: interrupt number
2774 * @_di: pointer to the ab8500_charger structure
2775 *
2776 * Returns IRQ status(IRQ_HANDLED)
2777 */
2778static irqreturn_t ab8500_charger_vbuschdropend_handler(int irq, void *_di)
2779{
2780 struct ab8500_charger *di = _di;
2781
2782 dev_dbg(di->dev, "VBUS charger drop ended\n");
2783 di->flags.vbus_drop_end = true;
2784 queue_delayed_work(di->charger_wq, &di->vbus_drop_end_work,
2785 round_jiffies(30 * HZ));
2786
2787 return IRQ_HANDLED;
2788}
2789
2790/**
Arun Murthy84edbee2012-02-29 21:54:26 +05302791 * ab8500_charger_vbusovv_handler() - VBUS overvoltage detected
2792 * @irq: interrupt number
2793 * @_di: pointer to the ab8500_charger structure
2794 *
2795 * Returns IRQ status(IRQ_HANDLED)
2796 */
2797static irqreturn_t ab8500_charger_vbusovv_handler(int irq, void *_di)
2798{
2799 struct ab8500_charger *di = _di;
2800
2801 dev_dbg(di->dev, "VBUS overvoltage detected\n");
2802 di->flags.vbus_ovv = true;
2803 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2804
2805 /* Schedule a new HW failure check */
2806 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
2807
2808 return IRQ_HANDLED;
2809}
2810
2811/**
2812 * ab8500_charger_ac_get_property() - get the ac/mains properties
2813 * @psy: pointer to the power_supply structure
2814 * @psp: pointer to the power_supply_property structure
2815 * @val: pointer to the power_supply_propval union
2816 *
2817 * This function gets called when an application tries to get the ac/mains
2818 * properties by reading the sysfs files.
2819 * AC/Mains properties are online, present and voltage.
2820 * online: ac/mains charging is in progress or not
2821 * present: presence of the ac/mains
2822 * voltage: AC/Mains voltage
2823 * Returns error code in case of failure else 0(on success)
2824 */
2825static int ab8500_charger_ac_get_property(struct power_supply *psy,
2826 enum power_supply_property psp,
2827 union power_supply_propval *val)
2828{
2829 struct ab8500_charger *di;
Jonas Aaberga864c5a2013-01-11 13:12:53 +00002830 int ret;
Arun Murthy84edbee2012-02-29 21:54:26 +05302831
2832 di = to_ab8500_charger_ac_device_info(psy_to_ux500_charger(psy));
2833
2834 switch (psp) {
2835 case POWER_SUPPLY_PROP_HEALTH:
2836 if (di->flags.mainextchnotok)
2837 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
2838 else if (di->ac.wd_expired || di->usb.wd_expired)
2839 val->intval = POWER_SUPPLY_HEALTH_DEAD;
2840 else if (di->flags.main_thermal_prot)
2841 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
2842 else
2843 val->intval = POWER_SUPPLY_HEALTH_GOOD;
2844 break;
2845 case POWER_SUPPLY_PROP_ONLINE:
2846 val->intval = di->ac.charger_online;
2847 break;
2848 case POWER_SUPPLY_PROP_PRESENT:
2849 val->intval = di->ac.charger_connected;
2850 break;
2851 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Jonas Aaberga864c5a2013-01-11 13:12:53 +00002852 ret = ab8500_charger_get_ac_voltage(di);
2853 if (ret >= 0)
2854 di->ac.charger_voltage = ret;
2855 /* On error, use previous value */
Arun Murthy84edbee2012-02-29 21:54:26 +05302856 val->intval = di->ac.charger_voltage * 1000;
2857 break;
2858 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
2859 /*
2860 * This property is used to indicate when CV mode is entered
2861 * for the AC charger
2862 */
2863 di->ac.cv_active = ab8500_charger_ac_cv(di);
2864 val->intval = di->ac.cv_active;
2865 break;
2866 case POWER_SUPPLY_PROP_CURRENT_NOW:
Jonas Aaberga864c5a2013-01-11 13:12:53 +00002867 ret = ab8500_charger_get_ac_current(di);
2868 if (ret >= 0)
2869 di->ac.charger_current = ret;
2870 val->intval = di->ac.charger_current * 1000;
Arun Murthy84edbee2012-02-29 21:54:26 +05302871 break;
2872 default:
2873 return -EINVAL;
2874 }
2875 return 0;
2876}
2877
2878/**
2879 * ab8500_charger_usb_get_property() - get the usb properties
2880 * @psy: pointer to the power_supply structure
2881 * @psp: pointer to the power_supply_property structure
2882 * @val: pointer to the power_supply_propval union
2883 *
2884 * This function gets called when an application tries to get the usb
2885 * properties by reading the sysfs files.
2886 * USB properties are online, present and voltage.
2887 * online: usb charging is in progress or not
2888 * present: presence of the usb
2889 * voltage: vbus voltage
2890 * Returns error code in case of failure else 0(on success)
2891 */
2892static int ab8500_charger_usb_get_property(struct power_supply *psy,
2893 enum power_supply_property psp,
2894 union power_supply_propval *val)
2895{
2896 struct ab8500_charger *di;
Jonas Aaberga864c5a2013-01-11 13:12:53 +00002897 int ret;
Arun Murthy84edbee2012-02-29 21:54:26 +05302898
2899 di = to_ab8500_charger_usb_device_info(psy_to_ux500_charger(psy));
2900
2901 switch (psp) {
2902 case POWER_SUPPLY_PROP_HEALTH:
2903 if (di->flags.usbchargernotok)
2904 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
2905 else if (di->ac.wd_expired || di->usb.wd_expired)
2906 val->intval = POWER_SUPPLY_HEALTH_DEAD;
2907 else if (di->flags.usb_thermal_prot)
2908 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
2909 else if (di->flags.vbus_ovv)
2910 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
2911 else
2912 val->intval = POWER_SUPPLY_HEALTH_GOOD;
2913 break;
2914 case POWER_SUPPLY_PROP_ONLINE:
2915 val->intval = di->usb.charger_online;
2916 break;
2917 case POWER_SUPPLY_PROP_PRESENT:
2918 val->intval = di->usb.charger_connected;
2919 break;
2920 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Jonas Aaberga864c5a2013-01-11 13:12:53 +00002921 ret = ab8500_charger_get_vbus_voltage(di);
2922 if (ret >= 0)
2923 di->usb.charger_voltage = ret;
Arun Murthy84edbee2012-02-29 21:54:26 +05302924 val->intval = di->usb.charger_voltage * 1000;
2925 break;
2926 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
2927 /*
2928 * This property is used to indicate when CV mode is entered
2929 * for the USB charger
2930 */
2931 di->usb.cv_active = ab8500_charger_usb_cv(di);
2932 val->intval = di->usb.cv_active;
2933 break;
2934 case POWER_SUPPLY_PROP_CURRENT_NOW:
Jonas Aaberga864c5a2013-01-11 13:12:53 +00002935 ret = ab8500_charger_get_usb_current(di);
2936 if (ret >= 0)
2937 di->usb.charger_current = ret;
2938 val->intval = di->usb.charger_current * 1000;
Arun Murthy84edbee2012-02-29 21:54:26 +05302939 break;
2940 case POWER_SUPPLY_PROP_CURRENT_AVG:
2941 /*
2942 * This property is used to indicate when VBUS has collapsed
2943 * due to too high output current from the USB charger
2944 */
2945 if (di->flags.vbus_collapse)
2946 val->intval = 1;
2947 else
2948 val->intval = 0;
2949 break;
2950 default:
2951 return -EINVAL;
2952 }
2953 return 0;
2954}
2955
2956/**
2957 * ab8500_charger_init_hw_registers() - Set up charger related registers
2958 * @di: pointer to the ab8500_charger structure
2959 *
2960 * Set up charger OVV, watchdog and maximum voltage registers as well as
2961 * charging of the backup battery
2962 */
2963static int ab8500_charger_init_hw_registers(struct ab8500_charger *di)
2964{
2965 int ret = 0;
Yang QU0f4aa402012-06-26 19:25:52 +08002966 u8 bup_vch_range = 0, vbup33_vrtcn = 0;
Arun Murthy84edbee2012-02-29 21:54:26 +05302967
2968 /* Setup maximum charger current and voltage for ABB cut2.0 */
2969 if (!is_ab8500_1p1_or_earlier(di->parent)) {
2970 ret = abx500_set_register_interruptible(di->dev,
2971 AB8500_CHARGER,
2972 AB8500_CH_VOLT_LVL_MAX_REG, CH_VOL_LVL_4P6);
2973 if (ret) {
2974 dev_err(di->dev,
2975 "failed to set CH_VOLT_LVL_MAX_REG\n");
2976 goto out;
2977 }
2978
2979 ret = abx500_set_register_interruptible(di->dev,
2980 AB8500_CHARGER,
2981 AB8500_CH_OPT_CRNTLVL_MAX_REG, CH_OP_CUR_LVL_1P6);
2982 if (ret) {
2983 dev_err(di->dev,
2984 "failed to set CH_OPT_CRNTLVL_MAX_REG\n");
2985 goto out;
2986 }
2987 }
2988
Jonas Aaberg0ed51072012-05-11 12:42:25 +02002989 if (is_ab9540_2p0(di->parent) || is_ab8505_2p0(di->parent))
2990 ret = abx500_mask_and_set_register_interruptible(di->dev,
2991 AB8500_CHARGER,
2992 AB8500_USBCH_CTRL2_REG,
2993 VBUS_AUTO_IN_CURR_LIM_ENA,
2994 VBUS_AUTO_IN_CURR_LIM_ENA);
2995 else
2996 /*
2997 * VBUS OVV set to 6.3V and enable automatic current limitation
2998 */
2999 ret = abx500_set_register_interruptible(di->dev,
3000 AB8500_CHARGER,
3001 AB8500_USBCH_CTRL2_REG,
3002 VBUS_OVV_SELECT_6P3V | VBUS_AUTO_IN_CURR_LIM_ENA);
Arun Murthy84edbee2012-02-29 21:54:26 +05303003 if (ret) {
Jonas Aaberg0ed51072012-05-11 12:42:25 +02003004 dev_err(di->dev,
3005 "failed to set automatic current limitation\n");
Arun Murthy84edbee2012-02-29 21:54:26 +05303006 goto out;
3007 }
3008
3009 /* Enable main watchdog in OTP */
3010 ret = abx500_set_register_interruptible(di->dev,
3011 AB8500_OTP_EMUL, AB8500_OTP_CONF_15, OTP_ENABLE_WD);
3012 if (ret) {
3013 dev_err(di->dev, "failed to enable main WD in OTP\n");
3014 goto out;
3015 }
3016
3017 /* Enable main watchdog */
3018 ret = abx500_set_register_interruptible(di->dev,
3019 AB8500_SYS_CTRL2_BLOCK,
3020 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_ENA);
3021 if (ret) {
3022 dev_err(di->dev, "faile to enable main watchdog\n");
3023 goto out;
3024 }
3025
3026 /*
3027 * Due to internal synchronisation, Enable and Kick watchdog bits
3028 * cannot be enabled in a single write.
3029 * A minimum delay of 2*32 kHz period (62.5µs) must be inserted
3030 * between writing Enable then Kick bits.
3031 */
3032 udelay(63);
3033
3034 /* Kick main watchdog */
3035 ret = abx500_set_register_interruptible(di->dev,
3036 AB8500_SYS_CTRL2_BLOCK,
3037 AB8500_MAIN_WDOG_CTRL_REG,
3038 (MAIN_WDOG_ENA | MAIN_WDOG_KICK));
3039 if (ret) {
3040 dev_err(di->dev, "failed to kick main watchdog\n");
3041 goto out;
3042 }
3043
3044 /* Disable main watchdog */
3045 ret = abx500_set_register_interruptible(di->dev,
3046 AB8500_SYS_CTRL2_BLOCK,
3047 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_DIS);
3048 if (ret) {
3049 dev_err(di->dev, "failed to disable main watchdog\n");
3050 goto out;
3051 }
3052
3053 /* Set watchdog timeout */
3054 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
3055 AB8500_CH_WD_TIMER_REG, WD_TIMER);
3056 if (ret) {
3057 dev_err(di->dev, "failed to set charger watchdog timeout\n");
3058 goto out;
3059 }
3060
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003061 /* Set charger watchdog timeout */
3062 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
3063 AB8500_CH_WD_TIMER_REG, WD_TIMER);
3064 if (ret) {
3065 dev_err(di->dev, "failed to set charger watchdog timeout\n");
3066 goto out;
3067 }
3068
3069 ret = ab8500_charger_led_en(di, false);
3070 if (ret < 0) {
3071 dev_err(di->dev, "failed to disable LED\n");
3072 goto out;
3073 }
3074
Arun Murthy84edbee2012-02-29 21:54:26 +05303075 /* Backup battery voltage and current */
Yang QU0f4aa402012-06-26 19:25:52 +08003076 if (di->bm->bkup_bat_v > BUP_VCH_SEL_3P1V)
3077 bup_vch_range = BUP_VCH_RANGE;
3078 if (di->bm->bkup_bat_v == BUP_VCH_SEL_3P3V)
3079 vbup33_vrtcn = VBUP33_VRTCN;
3080
Arun Murthy84edbee2012-02-29 21:54:26 +05303081 ret = abx500_set_register_interruptible(di->dev,
3082 AB8500_RTC,
3083 AB8500_RTC_BACKUP_CHG_REG,
Yang QU0f4aa402012-06-26 19:25:52 +08003084 (di->bm->bkup_bat_v & 0x3) | di->bm->bkup_bat_i);
Arun Murthy84edbee2012-02-29 21:54:26 +05303085 if (ret) {
3086 dev_err(di->dev, "failed to setup backup battery charging\n");
3087 goto out;
3088 }
Yang QU0f4aa402012-06-26 19:25:52 +08003089 if (is_ab8540(di->parent)) {
3090 ret = abx500_set_register_interruptible(di->dev,
3091 AB8500_RTC,
3092 AB8500_RTC_CTRL1_REG,
3093 bup_vch_range | vbup33_vrtcn);
3094 if (ret) {
3095 dev_err(di->dev, "failed to setup backup battery charging\n");
3096 goto out;
3097 }
3098 }
Arun Murthy84edbee2012-02-29 21:54:26 +05303099
3100 /* Enable backup battery charging */
3101 abx500_mask_and_set_register_interruptible(di->dev,
3102 AB8500_RTC, AB8500_RTC_CTRL_REG,
3103 RTC_BUP_CH_ENA, RTC_BUP_CH_ENA);
3104 if (ret < 0)
3105 dev_err(di->dev, "%s mask and set failed\n", __func__);
3106
3107out:
3108 return ret;
3109}
3110
3111/*
3112 * ab8500 charger driver interrupts and their respective isr
3113 */
3114static struct ab8500_charger_interrupts ab8500_charger_irq[] = {
3115 {"MAIN_CH_UNPLUG_DET", ab8500_charger_mainchunplugdet_handler},
3116 {"MAIN_CHARGE_PLUG_DET", ab8500_charger_mainchplugdet_handler},
3117 {"MAIN_EXT_CH_NOT_OK", ab8500_charger_mainextchnotok_handler},
3118 {"MAIN_CH_TH_PROT_R", ab8500_charger_mainchthprotr_handler},
3119 {"MAIN_CH_TH_PROT_F", ab8500_charger_mainchthprotf_handler},
3120 {"VBUS_DET_F", ab8500_charger_vbusdetf_handler},
3121 {"VBUS_DET_R", ab8500_charger_vbusdetr_handler},
3122 {"USB_LINK_STATUS", ab8500_charger_usblinkstatus_handler},
3123 {"USB_CH_TH_PROT_R", ab8500_charger_usbchthprotr_handler},
3124 {"USB_CH_TH_PROT_F", ab8500_charger_usbchthprotf_handler},
3125 {"USB_CHARGER_NOT_OKR", ab8500_charger_usbchargernotokr_handler},
3126 {"VBUS_OVV", ab8500_charger_vbusovv_handler},
3127 {"CH_WD_EXP", ab8500_charger_chwdexp_handler},
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003128 {"VBUS_CH_DROP_END", ab8500_charger_vbuschdropend_handler},
Arun Murthy84edbee2012-02-29 21:54:26 +05303129};
3130
3131static int ab8500_charger_usb_notifier_call(struct notifier_block *nb,
3132 unsigned long event, void *power)
3133{
3134 struct ab8500_charger *di =
3135 container_of(nb, struct ab8500_charger, nb);
3136 enum ab8500_usb_state bm_usb_state;
3137 unsigned mA = *((unsigned *)power);
3138
Lee Jonesc9ade0f2013-01-21 11:22:56 +00003139 if (!di)
3140 return NOTIFY_DONE;
3141
Arun Murthy84edbee2012-02-29 21:54:26 +05303142 if (event != USB_EVENT_VBUS) {
3143 dev_dbg(di->dev, "not a standard host, returning\n");
3144 return NOTIFY_DONE;
3145 }
3146
3147 /* TODO: State is fabricate here. See if charger really needs USB
3148 * state or if mA is enough
3149 */
3150 if ((di->usb_state.usb_current == 2) && (mA > 2))
3151 bm_usb_state = AB8500_BM_USB_STATE_RESUME;
3152 else if (mA == 0)
3153 bm_usb_state = AB8500_BM_USB_STATE_RESET_HS;
3154 else if (mA == 2)
3155 bm_usb_state = AB8500_BM_USB_STATE_SUSPEND;
3156 else if (mA >= 8) /* 8, 100, 500 */
3157 bm_usb_state = AB8500_BM_USB_STATE_CONFIGURED;
3158 else /* Should never occur */
3159 bm_usb_state = AB8500_BM_USB_STATE_RESET_FS;
3160
3161 dev_dbg(di->dev, "%s usb_state: 0x%02x mA: %d\n",
3162 __func__, bm_usb_state, mA);
3163
3164 spin_lock(&di->usb_state.usb_lock);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003165 di->usb_state.state_tmp = bm_usb_state;
3166 di->usb_state.usb_current_tmp = mA;
Arun Murthy84edbee2012-02-29 21:54:26 +05303167 spin_unlock(&di->usb_state.usb_lock);
3168
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003169 /*
3170 * wait for some time until you get updates from the usb stack
3171 * and negotiations are completed
3172 */
3173 queue_delayed_work(di->charger_wq, &di->usb_state_changed_work, HZ/2);
Arun Murthy84edbee2012-02-29 21:54:26 +05303174
3175 return NOTIFY_OK;
3176}
3177
3178#if defined(CONFIG_PM)
3179static int ab8500_charger_resume(struct platform_device *pdev)
3180{
3181 int ret;
3182 struct ab8500_charger *di = platform_get_drvdata(pdev);
3183
3184 /*
3185 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
3186 * logic. That means we have to continously kick the charger
3187 * watchdog even when no charger is connected. This is only
3188 * valid once the AC charger has been enabled. This is
3189 * a bug that is not handled by the algorithm and the
3190 * watchdog have to be kicked by the charger driver
3191 * when the AC charger is disabled
3192 */
3193 if (di->ac_conn && is_ab8500_1p1_or_earlier(di->parent)) {
3194 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
3195 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
3196 if (ret)
3197 dev_err(di->dev, "Failed to kick WD!\n");
3198
3199 /* If not already pending start a new timer */
3200 if (!delayed_work_pending(
3201 &di->kick_wd_work)) {
3202 queue_delayed_work(di->charger_wq, &di->kick_wd_work,
3203 round_jiffies(WD_KICK_INTERVAL));
3204 }
3205 }
3206
3207 /* If we still have a HW failure, schedule a new check */
3208 if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
3209 queue_delayed_work(di->charger_wq,
3210 &di->check_hw_failure_work, 0);
3211 }
3212
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003213 if (di->flags.vbus_drop_end)
3214 queue_delayed_work(di->charger_wq, &di->vbus_drop_end_work, 0);
3215
Arun Murthy84edbee2012-02-29 21:54:26 +05303216 return 0;
3217}
3218
3219static int ab8500_charger_suspend(struct platform_device *pdev,
3220 pm_message_t state)
3221{
3222 struct ab8500_charger *di = platform_get_drvdata(pdev);
3223
3224 /* Cancel any pending HW failure check */
3225 if (delayed_work_pending(&di->check_hw_failure_work))
3226 cancel_delayed_work(&di->check_hw_failure_work);
3227
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003228 if (delayed_work_pending(&di->vbus_drop_end_work))
3229 cancel_delayed_work(&di->vbus_drop_end_work);
3230
Jonas Aaberg53ef1f52012-05-21 16:05:01 +02003231 flush_delayed_work(&di->attach_work);
3232 flush_delayed_work(&di->usb_charger_attached_work);
3233 flush_delayed_work(&di->ac_charger_attached_work);
3234 flush_delayed_work(&di->check_usbchgnotok_work);
3235 flush_delayed_work(&di->check_vbat_work);
3236 flush_delayed_work(&di->kick_wd_work);
3237
3238 flush_work(&di->usb_link_status_work);
3239 flush_work(&di->ac_work);
3240 flush_work(&di->detect_usb_type_work);
3241
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003242 if (atomic_read(&di->current_stepping_sessions))
3243 return -EAGAIN;
3244
Arun Murthy84edbee2012-02-29 21:54:26 +05303245 return 0;
3246}
3247#else
3248#define ab8500_charger_suspend NULL
3249#define ab8500_charger_resume NULL
3250#endif
3251
Lee Jones88917162013-02-13 11:39:19 +00003252static struct notifier_block charger_nb = {
3253 .notifier_call = ab8500_external_charger_prepare,
3254};
3255
Bill Pemberton415ec692012-11-19 13:26:07 -05003256static int ab8500_charger_remove(struct platform_device *pdev)
Arun Murthy84edbee2012-02-29 21:54:26 +05303257{
3258 struct ab8500_charger *di = platform_get_drvdata(pdev);
3259 int i, irq, ret;
3260
3261 /* Disable AC charging */
3262 ab8500_charger_ac_en(&di->ac_chg, false, 0, 0);
3263
3264 /* Disable USB charging */
3265 ab8500_charger_usb_en(&di->usb_chg, false, 0, 0);
3266
3267 /* Disable interrupts */
3268 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
3269 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3270 free_irq(irq, di);
3271 }
3272
Arun Murthy84edbee2012-02-29 21:54:26 +05303273 /* Backup battery voltage and current disable */
3274 ret = abx500_mask_and_set_register_interruptible(di->dev,
3275 AB8500_RTC, AB8500_RTC_CTRL_REG, RTC_BUP_CH_ENA, 0);
3276 if (ret < 0)
3277 dev_err(di->dev, "%s mask and set failed\n", __func__);
3278
Anton Vorontsovefd71c82012-03-14 04:22:17 +04003279 usb_unregister_notifier(di->usb_phy, &di->nb);
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +05303280 usb_put_phy(di->usb_phy);
Arun Murthy84edbee2012-02-29 21:54:26 +05303281
3282 /* Delete the work queue */
3283 destroy_workqueue(di->charger_wq);
3284
Lee Jones88917162013-02-13 11:39:19 +00003285 /* Unregister external charger enable notifier */
3286 if (!di->ac_chg.enabled)
3287 blocking_notifier_chain_unregister(
3288 &charger_notifier_list, &charger_nb);
3289
Arun Murthy84edbee2012-02-29 21:54:26 +05303290 flush_scheduled_work();
Lee Jones72a90dd2013-02-14 09:22:48 +00003291 if (di->usb_chg.enabled)
Michel JAOUEN01ec8c52012-04-26 10:00:04 +02003292 power_supply_unregister(&di->usb_chg.psy);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003293#if !defined(CONFIG_CHARGER_PM2301)
Lee Jones72a90dd2013-02-14 09:22:48 +00003294 if (di->ac_chg.enabled)
Michel JAOUEN01ec8c52012-04-26 10:00:04 +02003295 power_supply_unregister(&di->ac_chg.psy);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003296#endif
Arun Murthy84edbee2012-02-29 21:54:26 +05303297 platform_set_drvdata(pdev, NULL);
Arun Murthy84edbee2012-02-29 21:54:26 +05303298
3299 return 0;
3300}
3301
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08003302static char *supply_interface[] = {
3303 "ab8500_chargalg",
3304 "ab8500_fg",
3305 "ab8500_btemp",
3306};
3307
Bill Pembertonc8afa642012-11-19 13:22:23 -05003308static int ab8500_charger_probe(struct platform_device *pdev)
Arun Murthy84edbee2012-02-29 21:54:26 +05303309{
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08003310 struct device_node *np = pdev->dev.of_node;
Lee Jones7722b792012-11-30 10:56:28 +00003311 struct abx500_bm_data *plat = pdev->dev.platform_data;
Lee Jones2aac3de2012-05-05 04:38:19 -07003312 struct ab8500_charger *di;
Lee Jonesb269fff2013-01-11 13:12:51 +00003313 int irq, i, charger_status, ret = 0, ch_stat;
Arun Murthy84edbee2012-02-29 21:54:26 +05303314
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08003315 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
3316 if (!di) {
3317 dev_err(&pdev->dev, "%s no mem for ab8500_charger\n", __func__);
Arun Murthy84edbee2012-02-29 21:54:26 +05303318 return -ENOMEM;
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08003319 }
Lee Jones7722b792012-11-30 10:56:28 +00003320
3321 if (!plat) {
3322 dev_err(&pdev->dev, "no battery management data supplied\n");
3323 return -EINVAL;
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08003324 }
Lee Jones7722b792012-11-30 10:56:28 +00003325 di->bm = plat;
3326
3327 if (np) {
3328 ret = ab8500_bm_of_probe(&pdev->dev, np, di->bm);
3329 if (ret) {
3330 dev_err(&pdev->dev, "failed to get battery information\n");
3331 return ret;
3332 }
3333 di->autopower_cfg = of_property_read_bool(np, "autopower_cfg");
3334 } else
3335 di->autopower_cfg = false;
Arun Murthy84edbee2012-02-29 21:54:26 +05303336
3337 /* get parent data */
3338 di->dev = &pdev->dev;
3339 di->parent = dev_get_drvdata(pdev->dev.parent);
3340 di->gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
3341
3342 /* initialize lock */
3343 spin_lock_init(&di->usb_state.usb_lock);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003344 mutex_init(&di->usb_ipt_crnt_lock);
Arun Murthy84edbee2012-02-29 21:54:26 +05303345
Arun Murthy84edbee2012-02-29 21:54:26 +05303346 di->autopower = false;
Henrik Sölverff380902012-04-17 15:51:01 +02003347 di->invalid_charger_detect_state = 0;
Arun Murthy84edbee2012-02-29 21:54:26 +05303348
3349 /* AC supply */
3350 /* power_supply base class */
3351 di->ac_chg.psy.name = "ab8500_ac";
3352 di->ac_chg.psy.type = POWER_SUPPLY_TYPE_MAINS;
3353 di->ac_chg.psy.properties = ab8500_charger_ac_props;
3354 di->ac_chg.psy.num_properties = ARRAY_SIZE(ab8500_charger_ac_props);
3355 di->ac_chg.psy.get_property = ab8500_charger_ac_get_property;
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08003356 di->ac_chg.psy.supplied_to = supply_interface;
3357 di->ac_chg.psy.num_supplicants = ARRAY_SIZE(supply_interface),
Arun Murthy84edbee2012-02-29 21:54:26 +05303358 /* ux500_charger sub-class */
3359 di->ac_chg.ops.enable = &ab8500_charger_ac_en;
Lee Jones4dcdf572013-02-14 09:24:10 +00003360 di->ac_chg.ops.check_enable = &ab8500_charger_ac_check_enable;
Arun Murthy84edbee2012-02-29 21:54:26 +05303361 di->ac_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
3362 di->ac_chg.ops.update_curr = &ab8500_charger_update_charger_current;
3363 di->ac_chg.max_out_volt = ab8500_charger_voltage_map[
3364 ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
3365 di->ac_chg.max_out_curr = ab8500_charger_current_map[
3366 ARRAY_SIZE(ab8500_charger_current_map) - 1];
Loic Pallardye07a5642012-05-10 15:33:56 +02003367 di->ac_chg.wdt_refresh = CHG_WD_INTERVAL;
Lee Jones97034a12013-01-17 16:08:42 +00003368 di->ac_chg.enabled = di->bm->ac_enabled;
Loic Pallardye07a5642012-05-10 15:33:56 +02003369 di->ac_chg.external = false;
Arun Murthy84edbee2012-02-29 21:54:26 +05303370
Lee Jones88917162013-02-13 11:39:19 +00003371 /*notifier for external charger enabling*/
3372 if (!di->ac_chg.enabled)
3373 blocking_notifier_chain_register(
3374 &charger_notifier_list, &charger_nb);
3375
Arun Murthy84edbee2012-02-29 21:54:26 +05303376 /* USB supply */
3377 /* power_supply base class */
3378 di->usb_chg.psy.name = "ab8500_usb";
3379 di->usb_chg.psy.type = POWER_SUPPLY_TYPE_USB;
3380 di->usb_chg.psy.properties = ab8500_charger_usb_props;
3381 di->usb_chg.psy.num_properties = ARRAY_SIZE(ab8500_charger_usb_props);
3382 di->usb_chg.psy.get_property = ab8500_charger_usb_get_property;
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08003383 di->usb_chg.psy.supplied_to = supply_interface;
3384 di->usb_chg.psy.num_supplicants = ARRAY_SIZE(supply_interface),
Arun Murthy84edbee2012-02-29 21:54:26 +05303385 /* ux500_charger sub-class */
3386 di->usb_chg.ops.enable = &ab8500_charger_usb_en;
Lee Jones4dcdf572013-02-14 09:24:10 +00003387 di->usb_chg.ops.check_enable = &ab8500_charger_usb_check_enable;
Arun Murthy84edbee2012-02-29 21:54:26 +05303388 di->usb_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
3389 di->usb_chg.ops.update_curr = &ab8500_charger_update_charger_current;
3390 di->usb_chg.max_out_volt = ab8500_charger_voltage_map[
3391 ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
3392 di->usb_chg.max_out_curr = ab8500_charger_current_map[
3393 ARRAY_SIZE(ab8500_charger_current_map) - 1];
Loic Pallardye07a5642012-05-10 15:33:56 +02003394 di->usb_chg.wdt_refresh = CHG_WD_INTERVAL;
Lee Jones97034a12013-01-17 16:08:42 +00003395 di->usb_chg.enabled = di->bm->usb_enabled;
Loic Pallardye07a5642012-05-10 15:33:56 +02003396 di->usb_chg.external = false;
Arun Murthy84edbee2012-02-29 21:54:26 +05303397
3398 /* Create a work queue for the charger */
3399 di->charger_wq =
3400 create_singlethread_workqueue("ab8500_charger_wq");
3401 if (di->charger_wq == NULL) {
3402 dev_err(di->dev, "failed to create work queue\n");
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08003403 return -ENOMEM;
Arun Murthy84edbee2012-02-29 21:54:26 +05303404 }
3405
Lee Jonesb269fff2013-01-11 13:12:51 +00003406 mutex_init(&di->charger_attached_mutex);
3407
Arun Murthy84edbee2012-02-29 21:54:26 +05303408 /* Init work for HW failure check */
Tejun Heo203b42f2012-08-21 13:18:23 -07003409 INIT_DEFERRABLE_WORK(&di->check_hw_failure_work,
Arun Murthy84edbee2012-02-29 21:54:26 +05303410 ab8500_charger_check_hw_failure_work);
Tejun Heo203b42f2012-08-21 13:18:23 -07003411 INIT_DEFERRABLE_WORK(&di->check_usbchgnotok_work,
Arun Murthy84edbee2012-02-29 21:54:26 +05303412 ab8500_charger_check_usbchargernotok_work);
3413
Lee Jonesb269fff2013-01-11 13:12:51 +00003414 INIT_DELAYED_WORK(&di->ac_charger_attached_work,
3415 ab8500_charger_ac_attached_work);
3416 INIT_DELAYED_WORK(&di->usb_charger_attached_work,
3417 ab8500_charger_usb_attached_work);
3418
Arun Murthy84edbee2012-02-29 21:54:26 +05303419 /*
3420 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
3421 * logic. That means we have to continously kick the charger
3422 * watchdog even when no charger is connected. This is only
3423 * valid once the AC charger has been enabled. This is
3424 * a bug that is not handled by the algorithm and the
3425 * watchdog have to be kicked by the charger driver
3426 * when the AC charger is disabled
3427 */
Tejun Heo203b42f2012-08-21 13:18:23 -07003428 INIT_DEFERRABLE_WORK(&di->kick_wd_work,
Arun Murthy84edbee2012-02-29 21:54:26 +05303429 ab8500_charger_kick_watchdog_work);
3430
Tejun Heo203b42f2012-08-21 13:18:23 -07003431 INIT_DEFERRABLE_WORK(&di->check_vbat_work,
Arun Murthy84edbee2012-02-29 21:54:26 +05303432 ab8500_charger_check_vbat_work);
3433
Marcus Cooper4b45f4a2013-01-11 13:13:04 +00003434 INIT_DELAYED_WORK(&di->attach_work,
3435 ab8500_charger_usb_link_attach_work);
3436
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003437 INIT_DELAYED_WORK(&di->usb_state_changed_work,
3438 ab8500_charger_usb_state_changed_work);
3439
3440 INIT_DELAYED_WORK(&di->vbus_drop_end_work,
3441 ab8500_charger_vbus_drop_end_work);
3442
Arun Murthy84edbee2012-02-29 21:54:26 +05303443 /* Init work for charger detection */
3444 INIT_WORK(&di->usb_link_status_work,
3445 ab8500_charger_usb_link_status_work);
3446 INIT_WORK(&di->ac_work, ab8500_charger_ac_work);
3447 INIT_WORK(&di->detect_usb_type_work,
3448 ab8500_charger_detect_usb_type_work);
3449
Arun Murthy84edbee2012-02-29 21:54:26 +05303450 /* Init work for checking HW status */
3451 INIT_WORK(&di->check_main_thermal_prot_work,
3452 ab8500_charger_check_main_thermal_prot_work);
3453 INIT_WORK(&di->check_usb_thermal_prot_work,
3454 ab8500_charger_check_usb_thermal_prot_work);
3455
3456 /*
3457 * VDD ADC supply needs to be enabled from this driver when there
3458 * is a charger connected to avoid erroneous BTEMP_HIGH/LOW
3459 * interrupts during charging
3460 */
Sachin Kamat8feffd12012-12-07 17:23:28 +05303461 di->regu = devm_regulator_get(di->dev, "vddadc");
Arun Murthy84edbee2012-02-29 21:54:26 +05303462 if (IS_ERR(di->regu)) {
3463 ret = PTR_ERR(di->regu);
3464 dev_err(di->dev, "failed to get vddadc regulator\n");
3465 goto free_charger_wq;
3466 }
3467
3468
3469 /* Initialize OVV, and other registers */
3470 ret = ab8500_charger_init_hw_registers(di);
3471 if (ret) {
3472 dev_err(di->dev, "failed to initialize ABB registers\n");
Sachin Kamat8feffd12012-12-07 17:23:28 +05303473 goto free_charger_wq;
Arun Murthy84edbee2012-02-29 21:54:26 +05303474 }
3475
3476 /* Register AC charger class */
Lee Jones72a90dd2013-02-14 09:22:48 +00003477 if (di->ac_chg.enabled) {
Michel JAOUEN01ec8c52012-04-26 10:00:04 +02003478 ret = power_supply_register(di->dev, &di->ac_chg.psy);
3479 if (ret) {
3480 dev_err(di->dev, "failed to register AC charger\n");
3481 goto free_charger_wq;
3482 }
Arun Murthy84edbee2012-02-29 21:54:26 +05303483 }
3484
3485 /* Register USB charger class */
Lee Jones72a90dd2013-02-14 09:22:48 +00003486 if (di->usb_chg.enabled) {
Michel JAOUEN01ec8c52012-04-26 10:00:04 +02003487 ret = power_supply_register(di->dev, &di->usb_chg.psy);
3488 if (ret) {
3489 dev_err(di->dev, "failed to register USB charger\n");
3490 goto free_ac;
3491 }
Arun Murthy84edbee2012-02-29 21:54:26 +05303492 }
3493
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +05303494 di->usb_phy = usb_get_phy(USB_PHY_TYPE_USB2);
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +05303495 if (IS_ERR_OR_NULL(di->usb_phy)) {
Anton Vorontsovefd71c82012-03-14 04:22:17 +04003496 dev_err(di->dev, "failed to get usb transceiver\n");
Arun Murthy84edbee2012-02-29 21:54:26 +05303497 ret = -EINVAL;
3498 goto free_usb;
3499 }
3500 di->nb.notifier_call = ab8500_charger_usb_notifier_call;
Anton Vorontsovefd71c82012-03-14 04:22:17 +04003501 ret = usb_register_notifier(di->usb_phy, &di->nb);
Arun Murthy84edbee2012-02-29 21:54:26 +05303502 if (ret) {
Anton Vorontsovefd71c82012-03-14 04:22:17 +04003503 dev_err(di->dev, "failed to register usb notifier\n");
3504 goto put_usb_phy;
Arun Murthy84edbee2012-02-29 21:54:26 +05303505 }
3506
3507 /* Identify the connected charger types during startup */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003508 charger_status = ab8500_charger_detect_chargers(di, true);
Arun Murthy84edbee2012-02-29 21:54:26 +05303509 if (charger_status & AC_PW_CONN) {
3510 di->ac.charger_connected = 1;
3511 di->ac_conn = true;
3512 ab8500_power_supply_changed(di, &di->ac_chg.psy);
3513 sysfs_notify(&di->ac_chg.psy.dev->kobj, NULL, "present");
3514 }
3515
3516 if (charger_status & USB_PW_CONN) {
Arun Murthy84edbee2012-02-29 21:54:26 +05303517 di->vbus_detected = true;
3518 di->vbus_detected_start = true;
3519 queue_work(di->charger_wq,
3520 &di->detect_usb_type_work);
3521 }
3522
3523 /* Register interrupts */
3524 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
3525 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3526 ret = request_threaded_irq(irq, NULL, ab8500_charger_irq[i].isr,
3527 IRQF_SHARED | IRQF_NO_SUSPEND,
3528 ab8500_charger_irq[i].name, di);
3529
3530 if (ret != 0) {
3531 dev_err(di->dev, "failed to request %s IRQ %d: %d\n"
3532 , ab8500_charger_irq[i].name, irq, ret);
3533 goto free_irq;
3534 }
3535 dev_dbg(di->dev, "Requested %s IRQ %d: %d\n",
3536 ab8500_charger_irq[i].name, irq, ret);
3537 }
3538
3539 platform_set_drvdata(pdev, di);
3540
Lee Jonesb269fff2013-01-11 13:12:51 +00003541 mutex_lock(&di->charger_attached_mutex);
3542
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003543 ch_stat = ab8500_charger_detect_chargers(di, false);
Lee Jonesb269fff2013-01-11 13:12:51 +00003544
3545 if ((ch_stat & AC_PW_CONN) == AC_PW_CONN) {
3546 queue_delayed_work(di->charger_wq,
3547 &di->ac_charger_attached_work,
3548 HZ);
3549 }
3550 if ((ch_stat & USB_PW_CONN) == USB_PW_CONN) {
3551 queue_delayed_work(di->charger_wq,
3552 &di->usb_charger_attached_work,
3553 HZ);
3554 }
3555
3556 mutex_unlock(&di->charger_attached_mutex);
3557
Arun Murthy84edbee2012-02-29 21:54:26 +05303558 return ret;
3559
3560free_irq:
Anton Vorontsovefd71c82012-03-14 04:22:17 +04003561 usb_unregister_notifier(di->usb_phy, &di->nb);
Arun Murthy84edbee2012-02-29 21:54:26 +05303562
3563 /* We also have to free all successfully registered irqs */
3564 for (i = i - 1; i >= 0; i--) {
3565 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3566 free_irq(irq, di);
3567 }
Anton Vorontsovefd71c82012-03-14 04:22:17 +04003568put_usb_phy:
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +05303569 usb_put_phy(di->usb_phy);
Arun Murthy84edbee2012-02-29 21:54:26 +05303570free_usb:
Lee Jones72a90dd2013-02-14 09:22:48 +00003571 if (di->usb_chg.enabled)
Michel JAOUEN01ec8c52012-04-26 10:00:04 +02003572 power_supply_unregister(&di->usb_chg.psy);
Arun Murthy84edbee2012-02-29 21:54:26 +05303573free_ac:
Lee Jones72a90dd2013-02-14 09:22:48 +00003574 if (di->ac_chg.enabled)
Michel JAOUEN01ec8c52012-04-26 10:00:04 +02003575 power_supply_unregister(&di->ac_chg.psy);
Arun Murthy84edbee2012-02-29 21:54:26 +05303576free_charger_wq:
3577 destroy_workqueue(di->charger_wq);
Arun Murthy84edbee2012-02-29 21:54:26 +05303578 return ret;
3579}
3580
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08003581static const struct of_device_id ab8500_charger_match[] = {
3582 { .compatible = "stericsson,ab8500-charger", },
3583 { },
3584};
3585
Arun Murthy84edbee2012-02-29 21:54:26 +05303586static struct platform_driver ab8500_charger_driver = {
3587 .probe = ab8500_charger_probe,
Bill Pemberton28ea73f2012-11-19 13:20:40 -05003588 .remove = ab8500_charger_remove,
Arun Murthy84edbee2012-02-29 21:54:26 +05303589 .suspend = ab8500_charger_suspend,
3590 .resume = ab8500_charger_resume,
3591 .driver = {
3592 .name = "ab8500-charger",
3593 .owner = THIS_MODULE,
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08003594 .of_match_table = ab8500_charger_match,
Arun Murthy84edbee2012-02-29 21:54:26 +05303595 },
3596};
3597
3598static int __init ab8500_charger_init(void)
3599{
3600 return platform_driver_register(&ab8500_charger_driver);
3601}
3602
3603static void __exit ab8500_charger_exit(void)
3604{
3605 platform_driver_unregister(&ab8500_charger_driver);
3606}
3607
3608subsys_initcall_sync(ab8500_charger_init);
3609module_exit(ab8500_charger_exit);
3610
3611MODULE_LICENSE("GPL v2");
3612MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy");
3613MODULE_ALIAS("platform:ab8500-charger");
3614MODULE_DESCRIPTION("AB8500 charger management driver");