blob: 3eb23cf9ff476b63d3eb74c22918e6c029fc4969 [file] [log] [blame]
Arun Murthy84edbee2012-02-29 21:54:26 +05301/*
2 * Copyright (C) ST-Ericsson SA 2012
3 *
4 * Charger driver for AB8500
5 *
6 * License Terms: GNU General Public License v2
7 * Author:
8 * Johan Palsson <johan.palsson@stericsson.com>
9 * Karl Komierowski <karl.komierowski@stericsson.com>
10 * Arun R Murthy <arun.murthy@stericsson.com>
11 */
12
13#include <linux/init.h>
14#include <linux/module.h>
15#include <linux/device.h>
16#include <linux/interrupt.h>
17#include <linux/delay.h>
18#include <linux/slab.h>
19#include <linux/platform_device.h>
20#include <linux/power_supply.h>
21#include <linux/completion.h>
22#include <linux/regulator/consumer.h>
23#include <linux/err.h>
24#include <linux/workqueue.h>
25#include <linux/kobject.h>
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -080026#include <linux/of.h>
27#include <linux/mfd/core.h>
Arun Murthy84edbee2012-02-29 21:54:26 +053028#include <linux/mfd/abx500/ab8500.h>
29#include <linux/mfd/abx500.h>
30#include <linux/mfd/abx500/ab8500-bm.h>
31#include <linux/mfd/abx500/ab8500-gpadc.h>
32#include <linux/mfd/abx500/ux500_chargalg.h>
33#include <linux/usb/otg.h>
Lee Jonesb269fff2013-01-11 13:12:51 +000034#include <linux/mutex.h>
Arun Murthy84edbee2012-02-29 21:54:26 +053035
36/* Charger constants */
37#define NO_PW_CONN 0
38#define AC_PW_CONN 1
39#define USB_PW_CONN 2
40
41#define MAIN_WDOG_ENA 0x01
42#define MAIN_WDOG_KICK 0x02
43#define MAIN_WDOG_DIS 0x00
44#define CHARG_WD_KICK 0x01
45#define MAIN_CH_ENA 0x01
46#define MAIN_CH_NO_OVERSHOOT_ENA_N 0x02
47#define USB_CH_ENA 0x01
48#define USB_CHG_NO_OVERSHOOT_ENA_N 0x02
49#define MAIN_CH_DET 0x01
50#define MAIN_CH_CV_ON 0x04
51#define USB_CH_CV_ON 0x08
52#define VBUS_DET_DBNC100 0x02
53#define VBUS_DET_DBNC1 0x01
54#define OTP_ENABLE_WD 0x01
Lee Jones4dcdf572013-02-14 09:24:10 +000055#define DROP_COUNT_RESET 0x01
Arun Murthy84edbee2012-02-29 21:54:26 +053056
57#define MAIN_CH_INPUT_CURR_SHIFT 4
58#define VBUS_IN_CURR_LIM_SHIFT 4
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +010059#define AUTO_VBUS_IN_CURR_LIM_SHIFT 4
Arun Murthy84edbee2012-02-29 21:54:26 +053060
61#define LED_INDICATOR_PWM_ENA 0x01
62#define LED_INDICATOR_PWM_DIS 0x00
63#define LED_IND_CUR_5MA 0x04
64#define LED_INDICATOR_PWM_DUTY_252_256 0xBF
65
66/* HW failure constants */
67#define MAIN_CH_TH_PROT 0x02
68#define VBUS_CH_NOK 0x08
69#define USB_CH_TH_PROT 0x02
70#define VBUS_OVV_TH 0x01
71#define MAIN_CH_NOK 0x01
72#define VBUS_DET 0x80
73
Lee Jonesb269fff2013-01-11 13:12:51 +000074#define MAIN_CH_STATUS2_MAINCHGDROP 0x80
75#define MAIN_CH_STATUS2_MAINCHARGERDETDBNC 0x40
76#define USB_CH_VBUSDROP 0x40
77#define USB_CH_VBUSDETDBNC 0x01
78
Arun Murthy84edbee2012-02-29 21:54:26 +053079/* UsbLineStatus register bit masks */
80#define AB8500_USB_LINK_STATUS 0x78
81#define AB8500_STD_HOST_SUSP 0x18
82
83/* Watchdog timeout constant */
84#define WD_TIMER 0x30 /* 4min */
85#define WD_KICK_INTERVAL (60 * HZ)
86
87/* Lowest charger voltage is 3.39V -> 0x4E */
88#define LOW_VOLT_REG 0x4E
89
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +010090/* Step up/down delay in us */
91#define STEP_UDELAY 1000
92
Lee Jonesb269fff2013-01-11 13:12:51 +000093#define CHARGER_STATUS_POLL 10 /* in ms */
94
Loic Pallardye07a5642012-05-10 15:33:56 +020095#define CHG_WD_INTERVAL (60 * HZ)
96
Nicolas Guionb0163222012-05-23 15:59:43 +020097#define AB8500_SW_CONTROL_FALLBACK 0x03
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +010098/* Wait for enumeration before charing in us */
99#define WAIT_ACA_RID_ENUMERATION (5 * 1000)
Nicolas Guionb0163222012-05-23 15:59:43 +0200100
Arun Murthy84edbee2012-02-29 21:54:26 +0530101/* UsbLineStatus register - usb types */
102enum ab8500_charger_link_status {
103 USB_STAT_NOT_CONFIGURED,
104 USB_STAT_STD_HOST_NC,
105 USB_STAT_STD_HOST_C_NS,
106 USB_STAT_STD_HOST_C_S,
107 USB_STAT_HOST_CHG_NM,
108 USB_STAT_HOST_CHG_HS,
109 USB_STAT_HOST_CHG_HS_CHIRP,
110 USB_STAT_DEDICATED_CHG,
111 USB_STAT_ACA_RID_A,
112 USB_STAT_ACA_RID_B,
113 USB_STAT_ACA_RID_C_NM,
114 USB_STAT_ACA_RID_C_HS,
115 USB_STAT_ACA_RID_C_HS_CHIRP,
116 USB_STAT_HM_IDGND,
117 USB_STAT_RESERVED,
118 USB_STAT_NOT_VALID_LINK,
Hakan Berg74a8e342013-01-11 13:12:58 +0000119 USB_STAT_PHY_EN,
120 USB_STAT_SUP_NO_IDGND_VBUS,
121 USB_STAT_SUP_IDGND_VBUS,
122 USB_STAT_CHARGER_LINE_1,
123 USB_STAT_CARKIT_1,
124 USB_STAT_CARKIT_2,
125 USB_STAT_ACA_DOCK_CHARGER,
Arun Murthy84edbee2012-02-29 21:54:26 +0530126};
127
128enum ab8500_usb_state {
129 AB8500_BM_USB_STATE_RESET_HS, /* HighSpeed Reset */
130 AB8500_BM_USB_STATE_RESET_FS, /* FullSpeed/LowSpeed Reset */
131 AB8500_BM_USB_STATE_CONFIGURED,
132 AB8500_BM_USB_STATE_SUSPEND,
133 AB8500_BM_USB_STATE_RESUME,
134 AB8500_BM_USB_STATE_MAX,
135};
136
137/* VBUS input current limits supported in AB8500 in mA */
138#define USB_CH_IP_CUR_LVL_0P05 50
139#define USB_CH_IP_CUR_LVL_0P09 98
140#define USB_CH_IP_CUR_LVL_0P19 193
141#define USB_CH_IP_CUR_LVL_0P29 290
142#define USB_CH_IP_CUR_LVL_0P38 380
143#define USB_CH_IP_CUR_LVL_0P45 450
144#define USB_CH_IP_CUR_LVL_0P5 500
145#define USB_CH_IP_CUR_LVL_0P6 600
146#define USB_CH_IP_CUR_LVL_0P7 700
147#define USB_CH_IP_CUR_LVL_0P8 800
148#define USB_CH_IP_CUR_LVL_0P9 900
149#define USB_CH_IP_CUR_LVL_1P0 1000
150#define USB_CH_IP_CUR_LVL_1P1 1100
151#define USB_CH_IP_CUR_LVL_1P3 1300
152#define USB_CH_IP_CUR_LVL_1P4 1400
153#define USB_CH_IP_CUR_LVL_1P5 1500
154
155#define VBAT_TRESH_IP_CUR_RED 3800
156
157#define to_ab8500_charger_usb_device_info(x) container_of((x), \
158 struct ab8500_charger, usb_chg)
159#define to_ab8500_charger_ac_device_info(x) container_of((x), \
160 struct ab8500_charger, ac_chg)
161
162/**
163 * struct ab8500_charger_interrupts - ab8500 interupts
164 * @name: name of the interrupt
165 * @isr function pointer to the isr
166 */
167struct ab8500_charger_interrupts {
168 char *name;
169 irqreturn_t (*isr)(int irq, void *data);
170};
171
172struct ab8500_charger_info {
173 int charger_connected;
174 int charger_online;
175 int charger_voltage;
176 int cv_active;
177 bool wd_expired;
Jonas Aaberga864c5a2013-01-11 13:12:53 +0000178 int charger_current;
Arun Murthy84edbee2012-02-29 21:54:26 +0530179};
180
181struct ab8500_charger_event_flags {
182 bool mainextchnotok;
183 bool main_thermal_prot;
184 bool usb_thermal_prot;
185 bool vbus_ovv;
186 bool usbchargernotok;
187 bool chgwdexp;
188 bool vbus_collapse;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100189 bool vbus_drop_end;
Arun Murthy84edbee2012-02-29 21:54:26 +0530190};
191
192struct ab8500_charger_usb_state {
Arun Murthy84edbee2012-02-29 21:54:26 +0530193 int usb_current;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100194 int usb_current_tmp;
Arun Murthy84edbee2012-02-29 21:54:26 +0530195 enum ab8500_usb_state state;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100196 enum ab8500_usb_state state_tmp;
Arun Murthy84edbee2012-02-29 21:54:26 +0530197 spinlock_t usb_lock;
198};
199
200/**
201 * struct ab8500_charger - ab8500 Charger device information
202 * @dev: Pointer to the structure device
203 * @max_usb_in_curr: Max USB charger input current
204 * @vbus_detected: VBUS detected
205 * @vbus_detected_start:
206 * VBUS detected during startup
207 * @ac_conn: This will be true when the AC charger has been plugged
208 * @vddadc_en_ac: Indicate if VDD ADC supply is enabled because AC
209 * charger is enabled
210 * @vddadc_en_usb: Indicate if VDD ADC supply is enabled because USB
211 * charger is enabled
212 * @vbat Battery voltage
213 * @old_vbat Previously measured battery voltage
Marcus Cooper4b45f4a2013-01-11 13:13:04 +0000214 * @usb_device_is_unrecognised USB device is unrecognised by the hardware
Arun Murthy84edbee2012-02-29 21:54:26 +0530215 * @autopower Indicate if we should have automatic pwron after pwrloss
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -0800216 * @autopower_cfg platform specific power config support for "pwron after pwrloss"
Henrik Sölverff380902012-04-17 15:51:01 +0200217 * @invalid_charger_detect_state State when forcing AB to use invalid charger
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100218 * @is_usb_host: Indicate if last detected USB type is host
219 * @is_aca_rid: Incicate if accessory is ACA type
220 * @current_stepping_sessions:
221 * Counter for current stepping sessions
Arun Murthy84edbee2012-02-29 21:54:26 +0530222 * @parent: Pointer to the struct ab8500
223 * @gpadc: Pointer to the struct gpadc
Lee Jonesb0284de2012-11-30 10:09:42 +0000224 * @bm: Platform specific battery management information
Arun Murthy84edbee2012-02-29 21:54:26 +0530225 * @flags: Structure for information about events triggered
226 * @usb_state: Structure for usb stack information
227 * @ac_chg: AC charger power supply
228 * @usb_chg: USB charger power supply
229 * @ac: Structure that holds the AC charger properties
230 * @usb: Structure that holds the USB charger properties
231 * @regu: Pointer to the struct regulator
232 * @charger_wq: Work queue for the IRQs and checking HW state
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100233 * @usb_ipt_crnt_lock: Lock to protect VBUS input current setting from mutuals
234 * @pm_lock: Lock to prevent system to suspend
Arun Murthy84edbee2012-02-29 21:54:26 +0530235 * @check_vbat_work Work for checking vbat threshold to adjust vbus current
236 * @check_hw_failure_work: Work for checking HW state
237 * @check_usbchgnotok_work: Work for checking USB charger not ok status
238 * @kick_wd_work: Work for kicking the charger watchdog in case
239 * of ABB rev 1.* due to the watchog logic bug
Lee Jonesb269fff2013-01-11 13:12:51 +0000240 * @ac_charger_attached_work: Work for checking if AC charger is still
241 * connected
242 * @usb_charger_attached_work: Work for checking if USB charger is still
243 * connected
Arun Murthy84edbee2012-02-29 21:54:26 +0530244 * @ac_work: Work for checking AC charger connection
245 * @detect_usb_type_work: Work for detecting the USB type connected
246 * @usb_link_status_work: Work for checking the new USB link status
247 * @usb_state_changed_work: Work for checking USB state
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100248 * @attach_work: Work for detecting USB type
249 * @vbus_drop_end_work: Work for detecting VBUS drop end
Arun Murthy84edbee2012-02-29 21:54:26 +0530250 * @check_main_thermal_prot_work:
251 * Work for checking Main thermal status
252 * @check_usb_thermal_prot_work:
253 * Work for checking USB thermal status
Lee Jonesb269fff2013-01-11 13:12:51 +0000254 * @charger_attached_mutex: For controlling the wakelock
Arun Murthy84edbee2012-02-29 21:54:26 +0530255 */
256struct ab8500_charger {
257 struct device *dev;
258 int max_usb_in_curr;
259 bool vbus_detected;
260 bool vbus_detected_start;
261 bool ac_conn;
262 bool vddadc_en_ac;
263 bool vddadc_en_usb;
264 int vbat;
265 int old_vbat;
Marcus Cooper4b45f4a2013-01-11 13:13:04 +0000266 bool usb_device_is_unrecognised;
Arun Murthy84edbee2012-02-29 21:54:26 +0530267 bool autopower;
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -0800268 bool autopower_cfg;
Henrik Sölverff380902012-04-17 15:51:01 +0200269 int invalid_charger_detect_state;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100270 bool is_usb_host;
271 int is_aca_rid;
272 atomic_t current_stepping_sessions;
Arun Murthy84edbee2012-02-29 21:54:26 +0530273 struct ab8500 *parent;
274 struct ab8500_gpadc *gpadc;
Lee Jonesb0284de2012-11-30 10:09:42 +0000275 struct abx500_bm_data *bm;
Arun Murthy84edbee2012-02-29 21:54:26 +0530276 struct ab8500_charger_event_flags flags;
277 struct ab8500_charger_usb_state usb_state;
278 struct ux500_charger ac_chg;
279 struct ux500_charger usb_chg;
280 struct ab8500_charger_info ac;
281 struct ab8500_charger_info usb;
282 struct regulator *regu;
283 struct workqueue_struct *charger_wq;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100284 struct mutex usb_ipt_crnt_lock;
Arun Murthy84edbee2012-02-29 21:54:26 +0530285 struct delayed_work check_vbat_work;
286 struct delayed_work check_hw_failure_work;
287 struct delayed_work check_usbchgnotok_work;
288 struct delayed_work kick_wd_work;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100289 struct delayed_work usb_state_changed_work;
Marcus Cooper4b45f4a2013-01-11 13:13:04 +0000290 struct delayed_work attach_work;
Lee Jonesb269fff2013-01-11 13:12:51 +0000291 struct delayed_work ac_charger_attached_work;
292 struct delayed_work usb_charger_attached_work;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100293 struct delayed_work vbus_drop_end_work;
Arun Murthy84edbee2012-02-29 21:54:26 +0530294 struct work_struct ac_work;
295 struct work_struct detect_usb_type_work;
296 struct work_struct usb_link_status_work;
Arun Murthy84edbee2012-02-29 21:54:26 +0530297 struct work_struct check_main_thermal_prot_work;
298 struct work_struct check_usb_thermal_prot_work;
Anton Vorontsovefd71c82012-03-14 04:22:17 +0400299 struct usb_phy *usb_phy;
Arun Murthy84edbee2012-02-29 21:54:26 +0530300 struct notifier_block nb;
Lee Jonesb269fff2013-01-11 13:12:51 +0000301 struct mutex charger_attached_mutex;
Arun Murthy84edbee2012-02-29 21:54:26 +0530302};
303
304/* AC properties */
305static enum power_supply_property ab8500_charger_ac_props[] = {
306 POWER_SUPPLY_PROP_HEALTH,
307 POWER_SUPPLY_PROP_PRESENT,
308 POWER_SUPPLY_PROP_ONLINE,
309 POWER_SUPPLY_PROP_VOLTAGE_NOW,
310 POWER_SUPPLY_PROP_VOLTAGE_AVG,
311 POWER_SUPPLY_PROP_CURRENT_NOW,
312};
313
314/* USB properties */
315static enum power_supply_property ab8500_charger_usb_props[] = {
316 POWER_SUPPLY_PROP_HEALTH,
317 POWER_SUPPLY_PROP_CURRENT_AVG,
318 POWER_SUPPLY_PROP_PRESENT,
319 POWER_SUPPLY_PROP_ONLINE,
320 POWER_SUPPLY_PROP_VOLTAGE_NOW,
321 POWER_SUPPLY_PROP_VOLTAGE_AVG,
322 POWER_SUPPLY_PROP_CURRENT_NOW,
323};
324
Paer-Olof Haakanssondefbfa92013-01-11 13:12:49 +0000325/*
326 * Function for enabling and disabling sw fallback mode
327 * should always be disabled when no charger is connected.
Arun Murthy84edbee2012-02-29 21:54:26 +0530328 */
Paer-Olof Haakanssondefbfa92013-01-11 13:12:49 +0000329static void ab8500_enable_disable_sw_fallback(struct ab8500_charger *di,
330 bool fallback)
Arun Murthy84edbee2012-02-29 21:54:26 +0530331{
Nicolas Guionb0163222012-05-23 15:59:43 +0200332 u8 val;
Arun Murthy84edbee2012-02-29 21:54:26 +0530333 u8 reg;
Nicolas Guionb0163222012-05-23 15:59:43 +0200334 u8 bank;
335 u8 bit;
Arun Murthy84edbee2012-02-29 21:54:26 +0530336 int ret;
337
Paer-Olof Haakanssondefbfa92013-01-11 13:12:49 +0000338 dev_dbg(di->dev, "SW Fallback: %d\n", fallback);
Arun Murthy84edbee2012-02-29 21:54:26 +0530339
Nicolas Guionb0163222012-05-23 15:59:43 +0200340 if (is_ab8500(di->parent)) {
341 bank = 0x15;
342 reg = 0x0;
343 bit = 3;
344 } else {
345 bank = AB8500_SYS_CTRL1_BLOCK;
346 reg = AB8500_SW_CONTROL_FALLBACK;
347 bit = 0;
348 }
349
Paer-Olof Haakanssondefbfa92013-01-11 13:12:49 +0000350 /* read the register containing fallback bit */
Nicolas Guionb0163222012-05-23 15:59:43 +0200351 ret = abx500_get_register_interruptible(di->dev, bank, reg, &val);
352 if (ret < 0) {
353 dev_err(di->dev, "%d read failed\n", __LINE__);
Arun Murthy84edbee2012-02-29 21:54:26 +0530354 return;
355 }
356
Nicolas Guionb0163222012-05-23 15:59:43 +0200357 if (is_ab8500(di->parent)) {
358 /* enable the OPT emulation registers */
359 ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x2);
360 if (ret) {
361 dev_err(di->dev, "%d write failed\n", __LINE__);
362 goto disable_otp;
363 }
Arun Murthy84edbee2012-02-29 21:54:26 +0530364 }
365
Paer-Olof Haakanssondefbfa92013-01-11 13:12:49 +0000366 if (fallback)
Nicolas Guionb0163222012-05-23 15:59:43 +0200367 val |= (1 << bit);
Arun Murthy84edbee2012-02-29 21:54:26 +0530368 else
Nicolas Guionb0163222012-05-23 15:59:43 +0200369 val &= ~(1 << bit);
Arun Murthy84edbee2012-02-29 21:54:26 +0530370
Paer-Olof Haakanssondefbfa92013-01-11 13:12:49 +0000371 /* write back the changed fallback bit value to register */
Nicolas Guionb0163222012-05-23 15:59:43 +0200372 ret = abx500_set_register_interruptible(di->dev, bank, reg, val);
Arun Murthy84edbee2012-02-29 21:54:26 +0530373 if (ret) {
374 dev_err(di->dev, "%d write failed\n", __LINE__);
Arun Murthy84edbee2012-02-29 21:54:26 +0530375 }
376
Nicolas Guionb0163222012-05-23 15:59:43 +0200377disable_otp:
378 if (is_ab8500(di->parent)) {
379 /* disable the set OTP registers again */
380 ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x0);
381 if (ret) {
382 dev_err(di->dev, "%d write failed\n", __LINE__);
383 }
Arun Murthy84edbee2012-02-29 21:54:26 +0530384 }
385}
386
387/**
388 * ab8500_power_supply_changed - a wrapper with local extentions for
389 * power_supply_changed
390 * @di: pointer to the ab8500_charger structure
391 * @psy: pointer to power_supply_that have changed.
392 *
393 */
394static void ab8500_power_supply_changed(struct ab8500_charger *di,
395 struct power_supply *psy)
396{
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -0800397 if (di->autopower_cfg) {
Arun Murthy84edbee2012-02-29 21:54:26 +0530398 if (!di->usb.charger_connected &&
399 !di->ac.charger_connected &&
400 di->autopower) {
401 di->autopower = false;
Paer-Olof Haakanssondefbfa92013-01-11 13:12:49 +0000402 ab8500_enable_disable_sw_fallback(di, false);
Arun Murthy84edbee2012-02-29 21:54:26 +0530403 } else if (!di->autopower &&
404 (di->ac.charger_connected ||
405 di->usb.charger_connected)) {
406 di->autopower = true;
Paer-Olof Haakanssondefbfa92013-01-11 13:12:49 +0000407 ab8500_enable_disable_sw_fallback(di, true);
Arun Murthy84edbee2012-02-29 21:54:26 +0530408 }
409 }
410 power_supply_changed(psy);
411}
412
413static void ab8500_charger_set_usb_connected(struct ab8500_charger *di,
414 bool connected)
415{
416 if (connected != di->usb.charger_connected) {
417 dev_dbg(di->dev, "USB connected:%i\n", connected);
418 di->usb.charger_connected = connected;
419 sysfs_notify(&di->usb_chg.psy.dev->kobj, NULL, "present");
Lee Jonesb269fff2013-01-11 13:12:51 +0000420
421 if (connected) {
422 mutex_lock(&di->charger_attached_mutex);
423 mutex_unlock(&di->charger_attached_mutex);
424
425 queue_delayed_work(di->charger_wq,
426 &di->usb_charger_attached_work,
427 HZ);
428 } else {
429 cancel_delayed_work_sync(&di->usb_charger_attached_work);
430 mutex_lock(&di->charger_attached_mutex);
431 mutex_unlock(&di->charger_attached_mutex);
432 }
Arun Murthy84edbee2012-02-29 21:54:26 +0530433 }
434}
435
436/**
437 * ab8500_charger_get_ac_voltage() - get ac charger voltage
438 * @di: pointer to the ab8500_charger structure
439 *
440 * Returns ac charger voltage (on success)
441 */
442static int ab8500_charger_get_ac_voltage(struct ab8500_charger *di)
443{
444 int vch;
445
446 /* Only measure voltage if the charger is connected */
447 if (di->ac.charger_connected) {
448 vch = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_V);
449 if (vch < 0)
450 dev_err(di->dev, "%s gpadc conv failed,\n", __func__);
451 } else {
452 vch = 0;
453 }
454 return vch;
455}
456
457/**
458 * ab8500_charger_ac_cv() - check if the main charger is in CV mode
459 * @di: pointer to the ab8500_charger structure
460 *
461 * Returns ac charger CV mode (on success) else error code
462 */
463static int ab8500_charger_ac_cv(struct ab8500_charger *di)
464{
465 u8 val;
466 int ret = 0;
467
468 /* Only check CV mode if the charger is online */
469 if (di->ac.charger_online) {
470 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
471 AB8500_CH_STATUS1_REG, &val);
472 if (ret < 0) {
473 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
474 return 0;
475 }
476
477 if (val & MAIN_CH_CV_ON)
478 ret = 1;
479 else
480 ret = 0;
481 }
482
483 return ret;
484}
485
486/**
487 * ab8500_charger_get_vbus_voltage() - get vbus voltage
488 * @di: pointer to the ab8500_charger structure
489 *
490 * This function returns the vbus voltage.
491 * Returns vbus voltage (on success)
492 */
493static int ab8500_charger_get_vbus_voltage(struct ab8500_charger *di)
494{
495 int vch;
496
497 /* Only measure voltage if the charger is connected */
498 if (di->usb.charger_connected) {
499 vch = ab8500_gpadc_convert(di->gpadc, VBUS_V);
500 if (vch < 0)
501 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
502 } else {
503 vch = 0;
504 }
505 return vch;
506}
507
508/**
509 * ab8500_charger_get_usb_current() - get usb charger current
510 * @di: pointer to the ab8500_charger structure
511 *
512 * This function returns the usb charger current.
513 * Returns usb current (on success) and error code on failure
514 */
515static int ab8500_charger_get_usb_current(struct ab8500_charger *di)
516{
517 int ich;
518
519 /* Only measure current if the charger is online */
520 if (di->usb.charger_online) {
521 ich = ab8500_gpadc_convert(di->gpadc, USB_CHARGER_C);
522 if (ich < 0)
523 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
524 } else {
525 ich = 0;
526 }
527 return ich;
528}
529
530/**
531 * ab8500_charger_get_ac_current() - get ac charger current
532 * @di: pointer to the ab8500_charger structure
533 *
534 * This function returns the ac charger current.
535 * Returns ac current (on success) and error code on failure.
536 */
537static int ab8500_charger_get_ac_current(struct ab8500_charger *di)
538{
539 int ich;
540
541 /* Only measure current if the charger is online */
542 if (di->ac.charger_online) {
543 ich = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_C);
544 if (ich < 0)
545 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
546 } else {
547 ich = 0;
548 }
549 return ich;
550}
551
552/**
553 * ab8500_charger_usb_cv() - check if the usb charger is in CV mode
554 * @di: pointer to the ab8500_charger structure
555 *
556 * Returns ac charger CV mode (on success) else error code
557 */
558static int ab8500_charger_usb_cv(struct ab8500_charger *di)
559{
560 int ret;
561 u8 val;
562
563 /* Only check CV mode if the charger is online */
564 if (di->usb.charger_online) {
565 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
566 AB8500_CH_USBCH_STAT1_REG, &val);
567 if (ret < 0) {
568 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
569 return 0;
570 }
571
572 if (val & USB_CH_CV_ON)
573 ret = 1;
574 else
575 ret = 0;
576 } else {
577 ret = 0;
578 }
579
580 return ret;
581}
582
583/**
584 * ab8500_charger_detect_chargers() - Detect the connected chargers
585 * @di: pointer to the ab8500_charger structure
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100586 * @probe: if probe, don't delay and wait for HW
Arun Murthy84edbee2012-02-29 21:54:26 +0530587 *
588 * Returns the type of charger connected.
589 * For USB it will not mean we can actually charge from it
590 * but that there is a USB cable connected that we have to
591 * identify. This is used during startup when we don't get
592 * interrupts of the charger detection
593 *
594 * Returns an integer value, that means,
595 * NO_PW_CONN no power supply is connected
596 * AC_PW_CONN if the AC power supply is connected
597 * USB_PW_CONN if the USB power supply is connected
598 * AC_PW_CONN + USB_PW_CONN if USB and AC power supplies are both connected
599 */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100600static int ab8500_charger_detect_chargers(struct ab8500_charger *di, bool probe)
Arun Murthy84edbee2012-02-29 21:54:26 +0530601{
602 int result = NO_PW_CONN;
603 int ret;
604 u8 val;
605
606 /* Check for AC charger */
607 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
608 AB8500_CH_STATUS1_REG, &val);
609 if (ret < 0) {
610 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
611 return ret;
612 }
613
614 if (val & MAIN_CH_DET)
615 result = AC_PW_CONN;
616
617 /* Check for USB charger */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100618
619 if (!probe) {
620 /*
621 * AB8500 says VBUS_DET_DBNC1 & VBUS_DET_DBNC100
622 * when disconnecting ACA even though no
623 * charger was connected. Try waiting a little
624 * longer than the 100 ms of VBUS_DET_DBNC100...
625 */
626 msleep(110);
627 }
Arun Murthy84edbee2012-02-29 21:54:26 +0530628 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
629 AB8500_CH_USBCH_STAT1_REG, &val);
630 if (ret < 0) {
631 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
632 return ret;
633 }
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100634 dev_dbg(di->dev,
635 "%s AB8500_CH_USBCH_STAT1_REG %x\n", __func__,
636 val);
Arun Murthy84edbee2012-02-29 21:54:26 +0530637 if ((val & VBUS_DET_DBNC1) && (val & VBUS_DET_DBNC100))
638 result |= USB_PW_CONN;
639
640 return result;
641}
642
643/**
644 * ab8500_charger_max_usb_curr() - get the max curr for the USB type
645 * @di: pointer to the ab8500_charger structure
646 * @link_status: the identified USB type
647 *
648 * Get the maximum current that is allowed to be drawn from the host
649 * based on the USB type.
650 * Returns error code in case of failure else 0 on success
651 */
652static int ab8500_charger_max_usb_curr(struct ab8500_charger *di,
Hakan Berg74a8e342013-01-11 13:12:58 +0000653 enum ab8500_charger_link_status link_status)
Arun Murthy84edbee2012-02-29 21:54:26 +0530654{
655 int ret = 0;
656
Marcus Cooper4b45f4a2013-01-11 13:13:04 +0000657 di->usb_device_is_unrecognised = false;
658
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100659 /*
660 * Platform only supports USB 2.0.
661 * This means that charging current from USB source
662 * is maximum 500 mA. Every occurence of USB_STAT_*_HOST_*
663 * should set USB_CH_IP_CUR_LVL_0P5.
664 */
665
Arun Murthy84edbee2012-02-29 21:54:26 +0530666 switch (link_status) {
667 case USB_STAT_STD_HOST_NC:
668 case USB_STAT_STD_HOST_C_NS:
669 case USB_STAT_STD_HOST_C_S:
670 dev_dbg(di->dev, "USB Type - Standard host is "
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100671 "detected through USB driver\n");
672 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5;
673 di->is_usb_host = true;
674 di->is_aca_rid = 0;
Arun Murthy84edbee2012-02-29 21:54:26 +0530675 break;
676 case USB_STAT_HOST_CHG_HS_CHIRP:
677 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100678 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:
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100682 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5;
683 di->is_usb_host = true;
684 di->is_aca_rid = 0;
685 break;
Arun Murthy84edbee2012-02-29 21:54:26 +0530686 case USB_STAT_ACA_RID_C_HS:
687 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P9;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100688 di->is_usb_host = false;
689 di->is_aca_rid = 0;
Arun Murthy84edbee2012-02-29 21:54:26 +0530690 break;
691 case USB_STAT_ACA_RID_A:
692 /*
693 * Dedicated charger level minus maximum current accessory
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100694 * can consume (900mA). Closest level is 500mA
Arun Murthy84edbee2012-02-29 21:54:26 +0530695 */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100696 dev_dbg(di->dev, "USB_STAT_ACA_RID_A detected\n");
697 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5;
698 di->is_usb_host = false;
699 di->is_aca_rid = 1;
Arun Murthy84edbee2012-02-29 21:54:26 +0530700 break;
701 case USB_STAT_ACA_RID_B:
702 /*
703 * Dedicated charger level minus 120mA (20mA for ACA and
704 * 100mA for potential accessory). Closest level is 1300mA
705 */
706 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P3;
Hakan Berg74a8e342013-01-11 13:12:58 +0000707 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
708 di->max_usb_in_curr);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100709 di->is_usb_host = false;
710 di->is_aca_rid = 1;
Arun Murthy84edbee2012-02-29 21:54:26 +0530711 break;
Arun Murthy84edbee2012-02-29 21:54:26 +0530712 case USB_STAT_HOST_CHG_NM:
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100713 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5;
714 di->is_usb_host = true;
715 di->is_aca_rid = 0;
716 break;
Hakan Berg74a8e342013-01-11 13:12:58 +0000717 case USB_STAT_DEDICATED_CHG:
Arun Murthy84edbee2012-02-29 21:54:26 +0530718 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P5;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100719 di->is_usb_host = false;
720 di->is_aca_rid = 0;
721 break;
722 case USB_STAT_ACA_RID_C_HS_CHIRP:
723 case USB_STAT_ACA_RID_C_NM:
724 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P5;
725 di->is_usb_host = false;
726 di->is_aca_rid = 1;
Arun Murthy84edbee2012-02-29 21:54:26 +0530727 break;
Marcus Cooper4b45f4a2013-01-11 13:13:04 +0000728 case USB_STAT_NOT_CONFIGURED:
729 if (di->vbus_detected) {
730 di->usb_device_is_unrecognised = true;
731 dev_dbg(di->dev, "USB Type - Legacy charger.\n");
732 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P5;
733 break;
734 }
Arun Murthy84edbee2012-02-29 21:54:26 +0530735 case USB_STAT_HM_IDGND:
Arun Murthy84edbee2012-02-29 21:54:26 +0530736 dev_err(di->dev, "USB Type - Charging not allowed\n");
737 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05;
738 ret = -ENXIO;
739 break;
Hakan Berg74a8e342013-01-11 13:12:58 +0000740 case USB_STAT_RESERVED:
741 if (is_ab8500(di->parent)) {
742 di->flags.vbus_collapse = true;
743 dev_err(di->dev, "USB Type - USB_STAT_RESERVED "
744 "VBUS has collapsed\n");
745 ret = -ENXIO;
746 break;
747 }
748 if (is_ab9540(di->parent) || is_ab8505(di->parent)) {
749 dev_dbg(di->dev, "USB Type - Charging not allowed\n");
750 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05;
751 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
752 link_status, di->max_usb_in_curr);
753 ret = -ENXIO;
754 break;
755 }
756 break;
757 case USB_STAT_CARKIT_1:
758 case USB_STAT_CARKIT_2:
759 case USB_STAT_ACA_DOCK_CHARGER:
760 case USB_STAT_CHARGER_LINE_1:
761 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5;
762 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
763 di->max_usb_in_curr);
Henrik Sölverff380902012-04-17 15:51:01 +0200764 case USB_STAT_NOT_VALID_LINK:
765 dev_err(di->dev, "USB Type invalid - try charging anyway\n");
766 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5;
Hakan Berg74a8e342013-01-11 13:12:58 +0000767 break;
768
Arun Murthy84edbee2012-02-29 21:54:26 +0530769 default:
770 dev_err(di->dev, "USB Type - Unknown\n");
771 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05;
772 ret = -ENXIO;
773 break;
774 };
775
776 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
777 link_status, di->max_usb_in_curr);
778
779 return ret;
780}
781
782/**
783 * ab8500_charger_read_usb_type() - read the type of usb connected
784 * @di: pointer to the ab8500_charger structure
785 *
786 * Detect the type of the plugged USB
787 * Returns error code in case of failure else 0 on success
788 */
789static int ab8500_charger_read_usb_type(struct ab8500_charger *di)
790{
791 int ret;
792 u8 val;
793
794 ret = abx500_get_register_interruptible(di->dev,
795 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG, &val);
796 if (ret < 0) {
797 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
798 return ret;
799 }
Hakan Berg74a8e342013-01-11 13:12:58 +0000800 if (is_ab8500(di->parent)) {
801 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
802 AB8500_USB_LINE_STAT_REG, &val);
803 } else {
804 if (is_ab9540(di->parent) || is_ab8505(di->parent))
805 ret = abx500_get_register_interruptible(di->dev,
806 AB8500_USB, AB8500_USB_LINK1_STAT_REG, &val);
807 }
Arun Murthy84edbee2012-02-29 21:54:26 +0530808 if (ret < 0) {
809 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
810 return ret;
811 }
812
813 /* get the USB type */
814 val = (val & AB8500_USB_LINK_STATUS) >> 3;
815 ret = ab8500_charger_max_usb_curr(di,
816 (enum ab8500_charger_link_status) val);
817
818 return ret;
819}
820
821/**
822 * ab8500_charger_detect_usb_type() - get the type of usb connected
823 * @di: pointer to the ab8500_charger structure
824 *
825 * Detect the type of the plugged USB
826 * Returns error code in case of failure else 0 on success
827 */
828static int ab8500_charger_detect_usb_type(struct ab8500_charger *di)
829{
830 int i, ret;
831 u8 val;
832
833 /*
834 * On getting the VBUS rising edge detect interrupt there
835 * is a 250ms delay after which the register UsbLineStatus
836 * is filled with valid data.
837 */
838 for (i = 0; i < 10; i++) {
839 msleep(250);
840 ret = abx500_get_register_interruptible(di->dev,
841 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG,
842 &val);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100843 dev_dbg(di->dev, "%s AB8500_IT_SOURCE21_REG %x\n",
844 __func__, val);
Arun Murthy84edbee2012-02-29 21:54:26 +0530845 if (ret < 0) {
846 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
847 return ret;
848 }
Hakan Berg74a8e342013-01-11 13:12:58 +0000849
850 if (is_ab8500(di->parent))
851 ret = abx500_get_register_interruptible(di->dev,
852 AB8500_USB, AB8500_USB_LINE_STAT_REG, &val);
853 else
854 ret = abx500_get_register_interruptible(di->dev,
855 AB8500_USB, AB8500_USB_LINK1_STAT_REG, &val);
Arun Murthy84edbee2012-02-29 21:54:26 +0530856 if (ret < 0) {
857 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
858 return ret;
859 }
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100860 dev_dbg(di->dev, "%s AB8500_USB_LINE_STAT_REG %x\n", __func__,
861 val);
Arun Murthy84edbee2012-02-29 21:54:26 +0530862 /*
863 * Until the IT source register is read the UsbLineStatus
864 * register is not updated, hence doing the same
865 * Revisit this:
866 */
867
868 /* get the USB type */
869 val = (val & AB8500_USB_LINK_STATUS) >> 3;
870 if (val)
871 break;
872 }
873 ret = ab8500_charger_max_usb_curr(di,
874 (enum ab8500_charger_link_status) val);
875
876 return ret;
877}
878
879/*
880 * This array maps the raw hex value to charger voltage used by the AB8500
881 * Values taken from the UM0836
882 */
883static int ab8500_charger_voltage_map[] = {
884 3500 ,
885 3525 ,
886 3550 ,
887 3575 ,
888 3600 ,
889 3625 ,
890 3650 ,
891 3675 ,
892 3700 ,
893 3725 ,
894 3750 ,
895 3775 ,
896 3800 ,
897 3825 ,
898 3850 ,
899 3875 ,
900 3900 ,
901 3925 ,
902 3950 ,
903 3975 ,
904 4000 ,
905 4025 ,
906 4050 ,
907 4060 ,
908 4070 ,
909 4080 ,
910 4090 ,
911 4100 ,
912 4110 ,
913 4120 ,
914 4130 ,
915 4140 ,
916 4150 ,
917 4160 ,
918 4170 ,
919 4180 ,
920 4190 ,
921 4200 ,
922 4210 ,
923 4220 ,
924 4230 ,
925 4240 ,
926 4250 ,
927 4260 ,
928 4270 ,
929 4280 ,
930 4290 ,
931 4300 ,
932 4310 ,
933 4320 ,
934 4330 ,
935 4340 ,
936 4350 ,
937 4360 ,
938 4370 ,
939 4380 ,
940 4390 ,
941 4400 ,
942 4410 ,
943 4420 ,
944 4430 ,
945 4440 ,
946 4450 ,
947 4460 ,
948 4470 ,
949 4480 ,
950 4490 ,
951 4500 ,
952 4510 ,
953 4520 ,
954 4530 ,
955 4540 ,
956 4550 ,
957 4560 ,
958 4570 ,
959 4580 ,
960 4590 ,
961 4600 ,
962};
963
964/*
965 * This array maps the raw hex value to charger current used by the AB8500
966 * Values taken from the UM0836
967 */
968static int ab8500_charger_current_map[] = {
969 100 ,
970 200 ,
971 300 ,
972 400 ,
973 500 ,
974 600 ,
975 700 ,
976 800 ,
977 900 ,
978 1000 ,
979 1100 ,
980 1200 ,
981 1300 ,
982 1400 ,
983 1500 ,
984};
985
986/*
987 * This array maps the raw hex value to VBUS input current used by the AB8500
988 * Values taken from the UM0836
989 */
990static int ab8500_charger_vbus_in_curr_map[] = {
991 USB_CH_IP_CUR_LVL_0P05,
992 USB_CH_IP_CUR_LVL_0P09,
993 USB_CH_IP_CUR_LVL_0P19,
994 USB_CH_IP_CUR_LVL_0P29,
995 USB_CH_IP_CUR_LVL_0P38,
996 USB_CH_IP_CUR_LVL_0P45,
997 USB_CH_IP_CUR_LVL_0P5,
998 USB_CH_IP_CUR_LVL_0P6,
999 USB_CH_IP_CUR_LVL_0P7,
1000 USB_CH_IP_CUR_LVL_0P8,
1001 USB_CH_IP_CUR_LVL_0P9,
1002 USB_CH_IP_CUR_LVL_1P0,
1003 USB_CH_IP_CUR_LVL_1P1,
1004 USB_CH_IP_CUR_LVL_1P3,
1005 USB_CH_IP_CUR_LVL_1P4,
1006 USB_CH_IP_CUR_LVL_1P5,
1007};
1008
1009static int ab8500_voltage_to_regval(int voltage)
1010{
1011 int i;
1012
1013 /* Special case for voltage below 3.5V */
1014 if (voltage < ab8500_charger_voltage_map[0])
1015 return LOW_VOLT_REG;
1016
1017 for (i = 1; i < ARRAY_SIZE(ab8500_charger_voltage_map); i++) {
1018 if (voltage < ab8500_charger_voltage_map[i])
1019 return i - 1;
1020 }
1021
1022 /* If not last element, return error */
1023 i = ARRAY_SIZE(ab8500_charger_voltage_map) - 1;
1024 if (voltage == ab8500_charger_voltage_map[i])
1025 return i;
1026 else
1027 return -1;
1028}
1029
1030static int ab8500_current_to_regval(int curr)
1031{
1032 int i;
1033
1034 if (curr < ab8500_charger_current_map[0])
1035 return 0;
1036
1037 for (i = 0; i < ARRAY_SIZE(ab8500_charger_current_map); i++) {
1038 if (curr < ab8500_charger_current_map[i])
1039 return i - 1;
1040 }
1041
1042 /* If not last element, return error */
1043 i = ARRAY_SIZE(ab8500_charger_current_map) - 1;
1044 if (curr == ab8500_charger_current_map[i])
1045 return i;
1046 else
1047 return -1;
1048}
1049
1050static int ab8500_vbus_in_curr_to_regval(int curr)
1051{
1052 int i;
1053
1054 if (curr < ab8500_charger_vbus_in_curr_map[0])
1055 return 0;
1056
1057 for (i = 0; i < ARRAY_SIZE(ab8500_charger_vbus_in_curr_map); i++) {
1058 if (curr < ab8500_charger_vbus_in_curr_map[i])
1059 return i - 1;
1060 }
1061
1062 /* If not last element, return error */
1063 i = ARRAY_SIZE(ab8500_charger_vbus_in_curr_map) - 1;
1064 if (curr == ab8500_charger_vbus_in_curr_map[i])
1065 return i;
1066 else
1067 return -1;
1068}
1069
1070/**
1071 * ab8500_charger_get_usb_cur() - get usb current
1072 * @di: pointer to the ab8500_charger structre
1073 *
1074 * The usb stack provides the maximum current that can be drawn from
1075 * the standard usb host. This will be in mA.
1076 * This function converts current in mA to a value that can be written
1077 * to the register. Returns -1 if charging is not allowed
1078 */
1079static int ab8500_charger_get_usb_cur(struct ab8500_charger *di)
1080{
1081 switch (di->usb_state.usb_current) {
1082 case 100:
1083 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P09;
1084 break;
1085 case 200:
1086 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P19;
1087 break;
1088 case 300:
1089 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P29;
1090 break;
1091 case 400:
1092 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P38;
1093 break;
1094 case 500:
1095 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5;
1096 break;
1097 default:
1098 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05;
1099 return -1;
1100 break;
1101 };
1102 return 0;
1103}
1104
1105/**
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001106 * ab8500_charger_set_current() - set charger current
1107 * @di: pointer to the ab8500_charger structure
1108 * @ich: charger current, in mA
1109 * @reg: select what charger register to set
1110 *
1111 * Set charger current.
1112 * There is no state machine in the AB to step up/down the charger
1113 * current to avoid dips and spikes on MAIN, VBUS and VBAT when
1114 * charging is started. Instead we need to implement
1115 * this charger current step-up/down here.
1116 * Returns error code in case of failure else 0(on success)
1117 */
1118static int ab8500_charger_set_current(struct ab8500_charger *di,
1119 int ich, int reg)
1120{
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001121 int ret = 0;
1122 int auto_curr_index, curr_index, prev_curr_index, shift_value, i;
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001123 u8 reg_value;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001124 u32 step_udelay;
1125 bool no_stepping = false;
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001126
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001127 atomic_inc(&di->current_stepping_sessions);
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001128
1129 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1130 reg, &reg_value);
1131 if (ret < 0) {
1132 dev_err(di->dev, "%s read failed\n", __func__);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001133 goto exit_set_current;
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001134 }
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001135
1136 switch (reg) {
1137 case AB8500_MCH_IPT_CURLVL_REG:
1138 shift_value = MAIN_CH_INPUT_CURR_SHIFT;
1139 prev_curr_index = (reg_value >> shift_value);
1140 curr_index = ab8500_current_to_regval(ich);
1141 step_udelay = STEP_UDELAY;
1142 if (!di->ac.charger_connected)
1143 no_stepping = true;
1144 break;
1145 case AB8500_USBCH_IPT_CRNTLVL_REG:
1146 shift_value = VBUS_IN_CURR_LIM_SHIFT;
1147 prev_curr_index = (reg_value >> shift_value);
1148 curr_index = ab8500_vbus_in_curr_to_regval(ich);
1149 step_udelay = STEP_UDELAY * 100;
1150
1151 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1152 AB8500_CH_USBCH_STAT2_REG, &reg_value);
1153 if (ret < 0) {
1154 dev_err(di->dev, "%s read failed\n", __func__);
1155 goto exit_set_current;
1156 }
1157 auto_curr_index =
1158 reg_value >> AUTO_VBUS_IN_CURR_LIM_SHIFT;
1159
1160 dev_dbg(di->dev, "%s Auto VBUS curr is %d mA\n",
1161 __func__,
1162 ab8500_charger_vbus_in_curr_map[auto_curr_index]);
1163
1164 prev_curr_index = min(prev_curr_index, auto_curr_index);
1165
1166 if (!di->usb.charger_connected)
1167 no_stepping = true;
1168 break;
1169 case AB8500_CH_OPT_CRNTLVL_REG:
1170 shift_value = 0;
1171 prev_curr_index = (reg_value >> shift_value);
1172 curr_index = ab8500_current_to_regval(ich);
1173 step_udelay = STEP_UDELAY;
1174 if (curr_index && (curr_index - prev_curr_index) > 1)
1175 step_udelay *= 100;
1176
1177 if (!di->usb.charger_connected && !di->ac.charger_connected)
1178 no_stepping = true;
1179
1180 break;
1181 default:
1182 dev_err(di->dev, "%s current register not valid\n", __func__);
1183 ret = -ENXIO;
1184 goto exit_set_current;
1185 }
1186
1187 if (curr_index < 0) {
1188 dev_err(di->dev, "requested current limit out-of-range\n");
1189 ret = -ENXIO;
1190 goto exit_set_current;
1191 }
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001192
1193 /* only update current if it's been changed */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001194 if (prev_curr_index == curr_index) {
1195 dev_dbg(di->dev, "%s current not changed for reg: 0x%02x\n",
1196 __func__, reg);
1197 ret = 0;
1198 goto exit_set_current;
1199 }
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001200
1201 dev_dbg(di->dev, "%s set charger current: %d mA for reg: 0x%02x\n",
1202 __func__, ich, reg);
1203
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001204 if (no_stepping) {
1205 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1206 reg, (u8)curr_index << shift_value);
1207 if (ret)
1208 dev_err(di->dev, "%s write failed\n", __func__);
1209 } else if (prev_curr_index > curr_index) {
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001210 for (i = prev_curr_index - 1; i >= curr_index; i--) {
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001211 dev_dbg(di->dev, "curr change_1 to: %x for 0x%02x\n",
1212 (u8) i << shift_value, reg);
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001213 ret = abx500_set_register_interruptible(di->dev,
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001214 AB8500_CHARGER, reg, (u8)i << shift_value);
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001215 if (ret) {
1216 dev_err(di->dev, "%s write failed\n", __func__);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001217 goto exit_set_current;
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001218 }
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001219 if (i != curr_index)
1220 usleep_range(step_udelay, step_udelay * 2);
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001221 }
1222 } else {
1223 for (i = prev_curr_index + 1; i <= curr_index; i++) {
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001224 dev_dbg(di->dev, "curr change_2 to: %x for 0x%02x\n",
1225 (u8)i << shift_value, reg);
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001226 ret = abx500_set_register_interruptible(di->dev,
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001227 AB8500_CHARGER, reg, (u8)i << shift_value);
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001228 if (ret) {
1229 dev_err(di->dev, "%s write failed\n", __func__);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001230 goto exit_set_current;
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001231 }
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001232 if (i != curr_index)
1233 usleep_range(step_udelay, step_udelay * 2);
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001234 }
1235 }
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001236
1237exit_set_current:
1238 atomic_dec(&di->current_stepping_sessions);
1239
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001240 return ret;
1241}
1242
1243/**
Arun Murthy84edbee2012-02-29 21:54:26 +05301244 * ab8500_charger_set_vbus_in_curr() - set VBUS input current limit
1245 * @di: pointer to the ab8500_charger structure
1246 * @ich_in: charger input current limit
1247 *
1248 * Sets the current that can be drawn from the USB host
1249 * Returns error code in case of failure else 0(on success)
1250 */
1251static int ab8500_charger_set_vbus_in_curr(struct ab8500_charger *di,
1252 int ich_in)
1253{
Arun Murthy84edbee2012-02-29 21:54:26 +05301254 int min_value;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001255 int ret;
Arun Murthy84edbee2012-02-29 21:54:26 +05301256
1257 /* We should always use to lowest current limit */
Lee Jonesb0284de2012-11-30 10:09:42 +00001258 min_value = min(di->bm->chg_params->usb_curr_max, ich_in);
Arun Murthy84edbee2012-02-29 21:54:26 +05301259
1260 switch (min_value) {
1261 case 100:
1262 if (di->vbat < VBAT_TRESH_IP_CUR_RED)
1263 min_value = USB_CH_IP_CUR_LVL_0P05;
1264 break;
1265 case 500:
1266 if (di->vbat < VBAT_TRESH_IP_CUR_RED)
1267 min_value = USB_CH_IP_CUR_LVL_0P45;
1268 break;
1269 default:
1270 break;
1271 }
1272
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001273 dev_info(di->dev, "VBUS input current limit set to %d mA\n", min_value);
1274
1275 mutex_lock(&di->usb_ipt_crnt_lock);
1276 ret = ab8500_charger_set_current(di, min_value,
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001277 AB8500_USBCH_IPT_CRNTLVL_REG);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001278 mutex_unlock(&di->usb_ipt_crnt_lock);
1279
1280 return ret;
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001281}
Arun Murthy84edbee2012-02-29 21:54:26 +05301282
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001283/**
1284 * ab8500_charger_set_main_in_curr() - set main charger input current
1285 * @di: pointer to the ab8500_charger structure
1286 * @ich_in: input charger current, in mA
1287 *
1288 * Set main charger input current.
1289 * Returns error code in case of failure else 0(on success)
1290 */
1291static int ab8500_charger_set_main_in_curr(struct ab8500_charger *di,
1292 int ich_in)
1293{
1294 return ab8500_charger_set_current(di, ich_in,
1295 AB8500_MCH_IPT_CURLVL_REG);
1296}
Arun Murthy84edbee2012-02-29 21:54:26 +05301297
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001298/**
1299 * ab8500_charger_set_output_curr() - set charger output current
1300 * @di: pointer to the ab8500_charger structure
1301 * @ich_out: output charger current, in mA
1302 *
1303 * Set charger output current.
1304 * Returns error code in case of failure else 0(on success)
1305 */
1306static int ab8500_charger_set_output_curr(struct ab8500_charger *di,
1307 int ich_out)
1308{
1309 return ab8500_charger_set_current(di, ich_out,
1310 AB8500_CH_OPT_CRNTLVL_REG);
Arun Murthy84edbee2012-02-29 21:54:26 +05301311}
1312
1313/**
1314 * ab8500_charger_led_en() - turn on/off chargign led
1315 * @di: pointer to the ab8500_charger structure
1316 * @on: flag to turn on/off the chargign led
1317 *
1318 * Power ON/OFF charging LED indication
1319 * Returns error code in case of failure else 0(on success)
1320 */
1321static int ab8500_charger_led_en(struct ab8500_charger *di, int on)
1322{
1323 int ret;
1324
1325 if (on) {
1326 /* Power ON charging LED indicator, set LED current to 5mA */
1327 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1328 AB8500_LED_INDICATOR_PWM_CTRL,
1329 (LED_IND_CUR_5MA | LED_INDICATOR_PWM_ENA));
1330 if (ret) {
1331 dev_err(di->dev, "Power ON LED failed\n");
1332 return ret;
1333 }
1334 /* LED indicator PWM duty cycle 252/256 */
1335 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1336 AB8500_LED_INDICATOR_PWM_DUTY,
1337 LED_INDICATOR_PWM_DUTY_252_256);
1338 if (ret) {
1339 dev_err(di->dev, "Set LED PWM duty cycle failed\n");
1340 return ret;
1341 }
1342 } else {
1343 /* Power off charging LED indicator */
1344 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1345 AB8500_LED_INDICATOR_PWM_CTRL,
1346 LED_INDICATOR_PWM_DIS);
1347 if (ret) {
1348 dev_err(di->dev, "Power-off LED failed\n");
1349 return ret;
1350 }
1351 }
1352
1353 return ret;
1354}
1355
1356/**
1357 * ab8500_charger_ac_en() - enable or disable ac charging
1358 * @di: pointer to the ab8500_charger structure
1359 * @enable: enable/disable flag
1360 * @vset: charging voltage
1361 * @iset: charging current
1362 *
1363 * Enable/Disable AC/Mains charging and turns on/off the charging led
1364 * respectively.
1365 **/
1366static int ab8500_charger_ac_en(struct ux500_charger *charger,
1367 int enable, int vset, int iset)
1368{
1369 int ret;
1370 int volt_index;
1371 int curr_index;
1372 int input_curr_index;
1373 u8 overshoot = 0;
1374
1375 struct ab8500_charger *di = to_ab8500_charger_ac_device_info(charger);
1376
1377 if (enable) {
1378 /* Check if AC is connected */
1379 if (!di->ac.charger_connected) {
1380 dev_err(di->dev, "AC charger not connected\n");
1381 return -ENXIO;
1382 }
1383
1384 /* Enable AC charging */
1385 dev_dbg(di->dev, "Enable AC: %dmV %dmA\n", vset, iset);
1386
1387 /*
1388 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1389 * will be triggered everytime we enable the VDD ADC supply.
1390 * This will turn off charging for a short while.
1391 * It can be avoided by having the supply on when
1392 * there is a charger enabled. Normally the VDD ADC supply
1393 * is enabled everytime a GPADC conversion is triggered. We will
1394 * force it to be enabled from this driver to have
1395 * the GPADC module independant of the AB8500 chargers
1396 */
1397 if (!di->vddadc_en_ac) {
1398 regulator_enable(di->regu);
1399 di->vddadc_en_ac = true;
1400 }
1401
1402 /* Check if the requested voltage or current is valid */
1403 volt_index = ab8500_voltage_to_regval(vset);
1404 curr_index = ab8500_current_to_regval(iset);
1405 input_curr_index = ab8500_current_to_regval(
Lee Jonesb0284de2012-11-30 10:09:42 +00001406 di->bm->chg_params->ac_curr_max);
Arun Murthy84edbee2012-02-29 21:54:26 +05301407 if (volt_index < 0 || curr_index < 0 || input_curr_index < 0) {
1408 dev_err(di->dev,
1409 "Charger voltage or current too high, "
1410 "charging not started\n");
1411 return -ENXIO;
1412 }
1413
1414 /* ChVoltLevel: maximum battery charging voltage */
1415 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1416 AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1417 if (ret) {
1418 dev_err(di->dev, "%s write failed\n", __func__);
1419 return ret;
1420 }
1421 /* MainChInputCurr: current that can be drawn from the charger*/
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001422 ret = ab8500_charger_set_main_in_curr(di,
Lee Jonesb0284de2012-11-30 10:09:42 +00001423 di->bm->chg_params->ac_curr_max);
Arun Murthy84edbee2012-02-29 21:54:26 +05301424 if (ret) {
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001425 dev_err(di->dev, "%s Failed to set MainChInputCurr\n",
1426 __func__);
Arun Murthy84edbee2012-02-29 21:54:26 +05301427 return ret;
1428 }
1429 /* ChOutputCurentLevel: protected output current */
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001430 ret = ab8500_charger_set_output_curr(di, iset);
Arun Murthy84edbee2012-02-29 21:54:26 +05301431 if (ret) {
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001432 dev_err(di->dev, "%s "
1433 "Failed to set ChOutputCurentLevel\n",
1434 __func__);
Arun Murthy84edbee2012-02-29 21:54:26 +05301435 return ret;
1436 }
1437
1438 /* Check if VBAT overshoot control should be enabled */
Lee Jonesb0284de2012-11-30 10:09:42 +00001439 if (!di->bm->enable_overshoot)
Arun Murthy84edbee2012-02-29 21:54:26 +05301440 overshoot = MAIN_CH_NO_OVERSHOOT_ENA_N;
1441
1442 /* Enable Main Charger */
1443 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1444 AB8500_MCH_CTRL1, MAIN_CH_ENA | overshoot);
1445 if (ret) {
1446 dev_err(di->dev, "%s write failed\n", __func__);
1447 return ret;
1448 }
1449
1450 /* Power on charging LED indication */
1451 ret = ab8500_charger_led_en(di, true);
1452 if (ret < 0)
1453 dev_err(di->dev, "failed to enable LED\n");
1454
1455 di->ac.charger_online = 1;
1456 } else {
1457 /* Disable AC charging */
1458 if (is_ab8500_1p1_or_earlier(di->parent)) {
1459 /*
1460 * For ABB revision 1.0 and 1.1 there is a bug in the
1461 * watchdog logic. That means we have to continously
1462 * kick the charger watchdog even when no charger is
1463 * connected. This is only valid once the AC charger
1464 * has been enabled. This is a bug that is not handled
1465 * by the algorithm and the watchdog have to be kicked
1466 * by the charger driver when the AC charger
1467 * is disabled
1468 */
1469 if (di->ac_conn) {
1470 queue_delayed_work(di->charger_wq,
1471 &di->kick_wd_work,
1472 round_jiffies(WD_KICK_INTERVAL));
1473 }
1474
1475 /*
1476 * We can't turn off charging completely
1477 * due to a bug in AB8500 cut1.
1478 * If we do, charging will not start again.
1479 * That is why we set the lowest voltage
1480 * and current possible
1481 */
1482 ret = abx500_set_register_interruptible(di->dev,
1483 AB8500_CHARGER,
1484 AB8500_CH_VOLT_LVL_REG, CH_VOL_LVL_3P5);
1485 if (ret) {
1486 dev_err(di->dev,
1487 "%s write failed\n", __func__);
1488 return ret;
1489 }
1490
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001491 ret = ab8500_charger_set_output_curr(di, 0);
Arun Murthy84edbee2012-02-29 21:54:26 +05301492 if (ret) {
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001493 dev_err(di->dev, "%s "
1494 "Failed to set ChOutputCurentLevel\n",
1495 __func__);
Arun Murthy84edbee2012-02-29 21:54:26 +05301496 return ret;
1497 }
1498 } else {
1499 ret = abx500_set_register_interruptible(di->dev,
1500 AB8500_CHARGER,
1501 AB8500_MCH_CTRL1, 0);
1502 if (ret) {
1503 dev_err(di->dev,
1504 "%s write failed\n", __func__);
1505 return ret;
1506 }
1507 }
1508
1509 ret = ab8500_charger_led_en(di, false);
1510 if (ret < 0)
1511 dev_err(di->dev, "failed to disable LED\n");
1512
1513 di->ac.charger_online = 0;
1514 di->ac.wd_expired = false;
1515
1516 /* Disable regulator if enabled */
1517 if (di->vddadc_en_ac) {
1518 regulator_disable(di->regu);
1519 di->vddadc_en_ac = false;
1520 }
1521
1522 dev_dbg(di->dev, "%s Disabled AC charging\n", __func__);
1523 }
1524 ab8500_power_supply_changed(di, &di->ac_chg.psy);
1525
1526 return ret;
1527}
1528
1529/**
1530 * ab8500_charger_usb_en() - enable usb charging
1531 * @di: pointer to the ab8500_charger structure
1532 * @enable: enable/disable flag
1533 * @vset: charging voltage
1534 * @ich_out: charger output current
1535 *
1536 * Enable/Disable USB charging and turns on/off the charging led respectively.
1537 * Returns error code in case of failure else 0(on success)
1538 */
1539static int ab8500_charger_usb_en(struct ux500_charger *charger,
1540 int enable, int vset, int ich_out)
1541{
1542 int ret;
1543 int volt_index;
1544 int curr_index;
1545 u8 overshoot = 0;
1546
1547 struct ab8500_charger *di = to_ab8500_charger_usb_device_info(charger);
1548
1549 if (enable) {
1550 /* Check if USB is connected */
1551 if (!di->usb.charger_connected) {
1552 dev_err(di->dev, "USB charger not connected\n");
1553 return -ENXIO;
1554 }
1555
1556 /*
1557 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1558 * will be triggered everytime we enable the VDD ADC supply.
1559 * This will turn off charging for a short while.
1560 * It can be avoided by having the supply on when
1561 * there is a charger enabled. Normally the VDD ADC supply
1562 * is enabled everytime a GPADC conversion is triggered. We will
1563 * force it to be enabled from this driver to have
1564 * the GPADC module independant of the AB8500 chargers
1565 */
1566 if (!di->vddadc_en_usb) {
1567 regulator_enable(di->regu);
1568 di->vddadc_en_usb = true;
1569 }
1570
1571 /* Enable USB charging */
1572 dev_dbg(di->dev, "Enable USB: %dmV %dmA\n", vset, ich_out);
1573
1574 /* Check if the requested voltage or current is valid */
1575 volt_index = ab8500_voltage_to_regval(vset);
1576 curr_index = ab8500_current_to_regval(ich_out);
1577 if (volt_index < 0 || curr_index < 0) {
1578 dev_err(di->dev,
1579 "Charger voltage or current too high, "
1580 "charging not started\n");
1581 return -ENXIO;
1582 }
1583
1584 /* ChVoltLevel: max voltage upto which battery can be charged */
1585 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1586 AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1587 if (ret) {
1588 dev_err(di->dev, "%s write failed\n", __func__);
1589 return ret;
1590 }
Arun Murthy84edbee2012-02-29 21:54:26 +05301591 /* Check if VBAT overshoot control should be enabled */
Lee Jonesb0284de2012-11-30 10:09:42 +00001592 if (!di->bm->enable_overshoot)
Arun Murthy84edbee2012-02-29 21:54:26 +05301593 overshoot = USB_CHG_NO_OVERSHOOT_ENA_N;
1594
1595 /* Enable USB Charger */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001596 dev_dbg(di->dev,
1597 "Enabling USB with write to AB8500_USBCH_CTRL1_REG\n");
Arun Murthy84edbee2012-02-29 21:54:26 +05301598 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1599 AB8500_USBCH_CTRL1_REG, USB_CH_ENA | overshoot);
1600 if (ret) {
1601 dev_err(di->dev, "%s write failed\n", __func__);
1602 return ret;
1603 }
1604
1605 /* If success power on charging LED indication */
1606 ret = ab8500_charger_led_en(di, true);
1607 if (ret < 0)
1608 dev_err(di->dev, "failed to enable LED\n");
1609
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001610 di->usb.charger_online = 1;
1611
1612 /* USBChInputCurr: current that can be drawn from the usb */
1613 ret = ab8500_charger_set_vbus_in_curr(di, di->max_usb_in_curr);
1614 if (ret) {
1615 dev_err(di->dev, "setting USBChInputCurr failed\n");
1616 return ret;
1617 }
1618
1619 /* ChOutputCurentLevel: protected output current */
1620 ret = ab8500_charger_set_output_curr(di, ich_out);
1621 if (ret) {
1622 dev_err(di->dev, "%s "
1623 "Failed to set ChOutputCurentLevel\n",
1624 __func__);
1625 return ret;
1626 }
1627
Arun Murthy84edbee2012-02-29 21:54:26 +05301628 queue_delayed_work(di->charger_wq, &di->check_vbat_work, HZ);
1629
Arun Murthy84edbee2012-02-29 21:54:26 +05301630 } else {
1631 /* Disable USB charging */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001632 dev_dbg(di->dev, "%s Disabled USB charging\n", __func__);
Arun Murthy84edbee2012-02-29 21:54:26 +05301633 ret = abx500_set_register_interruptible(di->dev,
1634 AB8500_CHARGER,
1635 AB8500_USBCH_CTRL1_REG, 0);
1636 if (ret) {
1637 dev_err(di->dev,
1638 "%s write failed\n", __func__);
1639 return ret;
1640 }
1641
1642 ret = ab8500_charger_led_en(di, false);
1643 if (ret < 0)
1644 dev_err(di->dev, "failed to disable LED\n");
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001645 /* USBChInputCurr: current that can be drawn from the usb */
1646 ret = ab8500_charger_set_vbus_in_curr(di, 0);
1647 if (ret) {
1648 dev_err(di->dev, "setting USBChInputCurr failed\n");
1649 return ret;
1650 }
Arun Murthy84edbee2012-02-29 21:54:26 +05301651
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001652 /* ChOutputCurentLevel: protected output current */
1653 ret = ab8500_charger_set_output_curr(di, 0);
1654 if (ret) {
1655 dev_err(di->dev, "%s "
1656 "Failed to reset ChOutputCurentLevel\n",
1657 __func__);
1658 return ret;
1659 }
Arun Murthy84edbee2012-02-29 21:54:26 +05301660 di->usb.charger_online = 0;
1661 di->usb.wd_expired = false;
1662
1663 /* Disable regulator if enabled */
1664 if (di->vddadc_en_usb) {
1665 regulator_disable(di->regu);
1666 di->vddadc_en_usb = false;
1667 }
1668
1669 dev_dbg(di->dev, "%s Disabled USB charging\n", __func__);
1670
1671 /* Cancel any pending Vbat check work */
1672 if (delayed_work_pending(&di->check_vbat_work))
1673 cancel_delayed_work(&di->check_vbat_work);
1674
1675 }
1676 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1677
1678 return ret;
1679}
1680
1681/**
Lee Jones4dcdf572013-02-14 09:24:10 +00001682 * ab8500_charger_usb_check_enable() - enable usb charging
1683 * @charger: pointer to the ux500_charger structure
1684 * @vset: charging voltage
1685 * @iset: charger output current
1686 *
1687 * Check if the VBUS charger has been disconnected and reconnected without
1688 * AB8500 rising an interrupt. Returns 0 on success.
1689 */
1690static int ab8500_charger_usb_check_enable(struct ux500_charger *charger,
1691 int vset, int iset)
1692{
1693 u8 usbch_ctrl1 = 0;
1694 int ret = 0;
1695
1696 struct ab8500_charger *di = to_ab8500_charger_usb_device_info(charger);
1697
1698 if (!di->usb.charger_connected)
1699 return ret;
1700
1701 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1702 AB8500_USBCH_CTRL1_REG, &usbch_ctrl1);
1703 if (ret < 0) {
1704 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
1705 return ret;
1706 }
1707 dev_dbg(di->dev, "USB charger ctrl: 0x%02x\n", usbch_ctrl1);
1708
1709 if (!(usbch_ctrl1 & USB_CH_ENA)) {
1710 dev_info(di->dev, "Charging has been disabled abnormally and will be re-enabled\n");
1711
1712 ret = abx500_mask_and_set_register_interruptible(di->dev,
1713 AB8500_CHARGER, AB8500_CHARGER_CTRL,
1714 DROP_COUNT_RESET, DROP_COUNT_RESET);
1715 if (ret < 0) {
1716 dev_err(di->dev, "ab8500 write failed %d\n", __LINE__);
1717 return ret;
1718 }
1719
1720 ret = ab8500_charger_usb_en(&di->usb_chg, true, vset, iset);
1721 if (ret < 0) {
1722 dev_err(di->dev, "Failed to enable VBUS charger %d\n",
1723 __LINE__);
1724 return ret;
1725 }
1726 }
1727 return ret;
1728}
1729
1730/**
1731 * ab8500_charger_ac_check_enable() - enable usb charging
1732 * @charger: pointer to the ux500_charger structure
1733 * @vset: charging voltage
1734 * @iset: charger output current
1735 *
1736 * Check if the AC charger has been disconnected and reconnected without
1737 * AB8500 rising an interrupt. Returns 0 on success.
1738 */
1739static int ab8500_charger_ac_check_enable(struct ux500_charger *charger,
1740 int vset, int iset)
1741{
1742 u8 mainch_ctrl1 = 0;
1743 int ret = 0;
1744
1745 struct ab8500_charger *di = to_ab8500_charger_ac_device_info(charger);
1746
1747 if (!di->ac.charger_connected)
1748 return ret;
1749
1750 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1751 AB8500_MCH_CTRL1, &mainch_ctrl1);
1752 if (ret < 0) {
1753 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
1754 return ret;
1755 }
1756 dev_dbg(di->dev, "AC charger ctrl: 0x%02x\n", mainch_ctrl1);
1757
1758 if (!(mainch_ctrl1 & MAIN_CH_ENA)) {
1759 dev_info(di->dev, "Charging has been disabled abnormally and will be re-enabled\n");
1760
1761 ret = abx500_mask_and_set_register_interruptible(di->dev,
1762 AB8500_CHARGER, AB8500_CHARGER_CTRL,
1763 DROP_COUNT_RESET, DROP_COUNT_RESET);
1764
1765 if (ret < 0) {
1766 dev_err(di->dev, "ab8500 write failed %d\n", __LINE__);
1767 return ret;
1768 }
1769
1770 ret = ab8500_charger_ac_en(&di->usb_chg, true, vset, iset);
1771 if (ret < 0) {
1772 dev_err(di->dev, "failed to enable AC charger %d\n",
1773 __LINE__);
1774 return ret;
1775 }
1776 }
1777 return ret;
1778}
1779
1780/**
Arun Murthy84edbee2012-02-29 21:54:26 +05301781 * ab8500_charger_watchdog_kick() - kick charger watchdog
1782 * @di: pointer to the ab8500_charger structure
1783 *
1784 * Kick charger watchdog
1785 * Returns error code in case of failure else 0(on success)
1786 */
1787static int ab8500_charger_watchdog_kick(struct ux500_charger *charger)
1788{
1789 int ret;
1790 struct ab8500_charger *di;
1791
1792 if (charger->psy.type == POWER_SUPPLY_TYPE_MAINS)
1793 di = to_ab8500_charger_ac_device_info(charger);
1794 else if (charger->psy.type == POWER_SUPPLY_TYPE_USB)
1795 di = to_ab8500_charger_usb_device_info(charger);
1796 else
1797 return -ENXIO;
1798
1799 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1800 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
1801 if (ret)
1802 dev_err(di->dev, "Failed to kick WD!\n");
1803
1804 return ret;
1805}
1806
1807/**
1808 * ab8500_charger_update_charger_current() - update charger current
1809 * @di: pointer to the ab8500_charger structure
1810 *
1811 * Update the charger output current for the specified charger
1812 * Returns error code in case of failure else 0(on success)
1813 */
1814static int ab8500_charger_update_charger_current(struct ux500_charger *charger,
1815 int ich_out)
1816{
1817 int ret;
Arun Murthy84edbee2012-02-29 21:54:26 +05301818 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
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001827 ret = ab8500_charger_set_output_curr(di, ich_out);
Arun Murthy84edbee2012-02-29 21:54:26 +05301828 if (ret) {
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001829 dev_err(di->dev, "%s "
1830 "Failed to set ChOutputCurentLevel\n",
1831 __func__);
Arun Murthy84edbee2012-02-29 21:54:26 +05301832 return ret;
1833 }
1834
1835 /* Reset the main and usb drop input current measurement counter */
1836 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
Lee Jones4dcdf572013-02-14 09:24:10 +00001837 AB8500_CHARGER_CTRL, DROP_COUNT_RESET);
Arun Murthy84edbee2012-02-29 21:54:26 +05301838 if (ret) {
1839 dev_err(di->dev, "%s write failed\n", __func__);
1840 return ret;
1841 }
1842
1843 return ret;
1844}
1845
1846static int ab8500_charger_get_ext_psy_data(struct device *dev, void *data)
1847{
1848 struct power_supply *psy;
1849 struct power_supply *ext;
1850 struct ab8500_charger *di;
1851 union power_supply_propval ret;
1852 int i, j;
1853 bool psy_found = false;
1854 struct ux500_charger *usb_chg;
1855
1856 usb_chg = (struct ux500_charger *)data;
1857 psy = &usb_chg->psy;
1858
1859 di = to_ab8500_charger_usb_device_info(usb_chg);
1860
1861 ext = dev_get_drvdata(dev);
1862
1863 /* For all psy where the driver name appears in any supplied_to */
1864 for (i = 0; i < ext->num_supplicants; i++) {
1865 if (!strcmp(ext->supplied_to[i], psy->name))
1866 psy_found = true;
1867 }
1868
1869 if (!psy_found)
1870 return 0;
1871
1872 /* Go through all properties for the psy */
1873 for (j = 0; j < ext->num_properties; j++) {
1874 enum power_supply_property prop;
1875 prop = ext->properties[j];
1876
1877 if (ext->get_property(ext, prop, &ret))
1878 continue;
1879
1880 switch (prop) {
1881 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1882 switch (ext->type) {
1883 case POWER_SUPPLY_TYPE_BATTERY:
1884 di->vbat = ret.intval / 1000;
1885 break;
1886 default:
1887 break;
1888 }
1889 break;
1890 default:
1891 break;
1892 }
1893 }
1894 return 0;
1895}
1896
1897/**
1898 * ab8500_charger_check_vbat_work() - keep vbus current within spec
1899 * @work pointer to the work_struct structure
1900 *
1901 * Due to a asic bug it is necessary to lower the input current to the vbus
1902 * charger when charging with at some specific levels. This issue is only valid
1903 * for below a certain battery voltage. This function makes sure that the
1904 * the allowed current limit isn't exceeded.
1905 */
1906static void ab8500_charger_check_vbat_work(struct work_struct *work)
1907{
1908 int t = 10;
1909 struct ab8500_charger *di = container_of(work,
1910 struct ab8500_charger, check_vbat_work.work);
1911
1912 class_for_each_device(power_supply_class, NULL,
1913 &di->usb_chg.psy, ab8500_charger_get_ext_psy_data);
1914
1915 /* First run old_vbat is 0. */
1916 if (di->old_vbat == 0)
1917 di->old_vbat = di->vbat;
1918
1919 if (!((di->old_vbat <= VBAT_TRESH_IP_CUR_RED &&
1920 di->vbat <= VBAT_TRESH_IP_CUR_RED) ||
1921 (di->old_vbat > VBAT_TRESH_IP_CUR_RED &&
1922 di->vbat > VBAT_TRESH_IP_CUR_RED))) {
1923
1924 dev_dbg(di->dev, "Vbat did cross threshold, curr: %d, new: %d,"
1925 " old: %d\n", di->max_usb_in_curr, di->vbat,
1926 di->old_vbat);
1927 ab8500_charger_set_vbus_in_curr(di, di->max_usb_in_curr);
1928 power_supply_changed(&di->usb_chg.psy);
1929 }
1930
1931 di->old_vbat = di->vbat;
1932
1933 /*
1934 * No need to check the battery voltage every second when not close to
1935 * the threshold.
1936 */
1937 if (di->vbat < (VBAT_TRESH_IP_CUR_RED + 100) &&
1938 (di->vbat > (VBAT_TRESH_IP_CUR_RED - 100)))
1939 t = 1;
1940
1941 queue_delayed_work(di->charger_wq, &di->check_vbat_work, t * HZ);
1942}
1943
1944/**
1945 * ab8500_charger_check_hw_failure_work() - check main charger failure
1946 * @work: pointer to the work_struct structure
1947 *
1948 * Work queue function for checking the main charger status
1949 */
1950static void ab8500_charger_check_hw_failure_work(struct work_struct *work)
1951{
1952 int ret;
1953 u8 reg_value;
1954
1955 struct ab8500_charger *di = container_of(work,
1956 struct ab8500_charger, check_hw_failure_work.work);
1957
1958 /* Check if the status bits for HW failure is still active */
1959 if (di->flags.mainextchnotok) {
1960 ret = abx500_get_register_interruptible(di->dev,
1961 AB8500_CHARGER, AB8500_CH_STATUS2_REG, &reg_value);
1962 if (ret < 0) {
1963 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1964 return;
1965 }
1966 if (!(reg_value & MAIN_CH_NOK)) {
1967 di->flags.mainextchnotok = false;
1968 ab8500_power_supply_changed(di, &di->ac_chg.psy);
1969 }
1970 }
1971 if (di->flags.vbus_ovv) {
1972 ret = abx500_get_register_interruptible(di->dev,
1973 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG,
1974 &reg_value);
1975 if (ret < 0) {
1976 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1977 return;
1978 }
1979 if (!(reg_value & VBUS_OVV_TH)) {
1980 di->flags.vbus_ovv = false;
1981 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1982 }
1983 }
1984 /* If we still have a failure, schedule a new check */
1985 if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
1986 queue_delayed_work(di->charger_wq,
1987 &di->check_hw_failure_work, round_jiffies(HZ));
1988 }
1989}
1990
1991/**
1992 * ab8500_charger_kick_watchdog_work() - kick the watchdog
1993 * @work: pointer to the work_struct structure
1994 *
1995 * Work queue function for kicking the charger watchdog.
1996 *
1997 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
1998 * logic. That means we have to continously kick the charger
1999 * watchdog even when no charger is connected. This is only
2000 * valid once the AC charger has been enabled. This is
2001 * a bug that is not handled by the algorithm and the
2002 * watchdog have to be kicked by the charger driver
2003 * when the AC charger is disabled
2004 */
2005static void ab8500_charger_kick_watchdog_work(struct work_struct *work)
2006{
2007 int ret;
2008
2009 struct ab8500_charger *di = container_of(work,
2010 struct ab8500_charger, kick_wd_work.work);
2011
2012 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
2013 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
2014 if (ret)
2015 dev_err(di->dev, "Failed to kick WD!\n");
2016
2017 /* Schedule a new watchdog kick */
2018 queue_delayed_work(di->charger_wq,
2019 &di->kick_wd_work, round_jiffies(WD_KICK_INTERVAL));
2020}
2021
2022/**
2023 * ab8500_charger_ac_work() - work to get and set main charger status
2024 * @work: pointer to the work_struct structure
2025 *
2026 * Work queue function for checking the main charger status
2027 */
2028static void ab8500_charger_ac_work(struct work_struct *work)
2029{
2030 int ret;
2031
2032 struct ab8500_charger *di = container_of(work,
2033 struct ab8500_charger, ac_work);
2034
2035 /*
2036 * Since we can't be sure that the events are received
2037 * synchronously, we have the check if the main charger is
2038 * connected by reading the status register
2039 */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002040 ret = ab8500_charger_detect_chargers(di, false);
Arun Murthy84edbee2012-02-29 21:54:26 +05302041 if (ret < 0)
2042 return;
2043
2044 if (ret & AC_PW_CONN) {
2045 di->ac.charger_connected = 1;
2046 di->ac_conn = true;
2047 } else {
2048 di->ac.charger_connected = 0;
2049 }
2050
2051 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2052 sysfs_notify(&di->ac_chg.psy.dev->kobj, NULL, "present");
2053}
2054
Lee Jonesb269fff2013-01-11 13:12:51 +00002055static void ab8500_charger_usb_attached_work(struct work_struct *work)
2056{
2057 struct ab8500_charger *di = container_of(work,
2058 struct ab8500_charger,
2059 usb_charger_attached_work.work);
2060 int usbch = (USB_CH_VBUSDROP | USB_CH_VBUSDETDBNC);
2061 int ret, i;
2062 u8 statval;
2063
2064 for (i = 0; i < 10; i++) {
2065 ret = abx500_get_register_interruptible(di->dev,
2066 AB8500_CHARGER,
2067 AB8500_CH_USBCH_STAT1_REG,
2068 &statval);
2069 if (ret < 0) {
2070 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
2071 goto reschedule;
2072 }
2073 if ((statval & usbch) != usbch)
2074 goto reschedule;
2075
2076 msleep(CHARGER_STATUS_POLL);
2077 }
2078
2079 ab8500_charger_usb_en(&di->usb_chg, 0, 0, 0);
2080
2081 mutex_lock(&di->charger_attached_mutex);
2082 mutex_unlock(&di->charger_attached_mutex);
2083
2084 return;
2085
2086reschedule:
2087 queue_delayed_work(di->charger_wq,
2088 &di->usb_charger_attached_work,
2089 HZ);
2090}
2091
2092static void ab8500_charger_ac_attached_work(struct work_struct *work)
2093{
2094
2095 struct ab8500_charger *di = container_of(work,
2096 struct ab8500_charger,
2097 ac_charger_attached_work.work);
2098 int mainch = (MAIN_CH_STATUS2_MAINCHGDROP |
2099 MAIN_CH_STATUS2_MAINCHARGERDETDBNC);
2100 int ret, i;
2101 u8 statval;
2102
2103 for (i = 0; i < 10; i++) {
2104 ret = abx500_get_register_interruptible(di->dev,
2105 AB8500_CHARGER,
2106 AB8500_CH_STATUS2_REG,
2107 &statval);
2108 if (ret < 0) {
2109 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
2110 goto reschedule;
2111 }
2112
2113 if ((statval & mainch) != mainch)
2114 goto reschedule;
2115
2116 msleep(CHARGER_STATUS_POLL);
2117 }
2118
2119 ab8500_charger_ac_en(&di->ac_chg, 0, 0, 0);
2120 queue_work(di->charger_wq, &di->ac_work);
2121
2122 mutex_lock(&di->charger_attached_mutex);
2123 mutex_unlock(&di->charger_attached_mutex);
2124
2125 return;
2126
2127reschedule:
2128 queue_delayed_work(di->charger_wq,
2129 &di->ac_charger_attached_work,
2130 HZ);
2131}
2132
Arun Murthy84edbee2012-02-29 21:54:26 +05302133/**
2134 * ab8500_charger_detect_usb_type_work() - work to detect USB type
2135 * @work: Pointer to the work_struct structure
2136 *
2137 * Detect the type of USB plugged
2138 */
Anton Vorontsov64eb9b02012-03-14 04:43:11 +04002139static void ab8500_charger_detect_usb_type_work(struct work_struct *work)
Arun Murthy84edbee2012-02-29 21:54:26 +05302140{
2141 int ret;
2142
2143 struct ab8500_charger *di = container_of(work,
2144 struct ab8500_charger, detect_usb_type_work);
2145
2146 /*
2147 * Since we can't be sure that the events are received
2148 * synchronously, we have the check if is
2149 * connected by reading the status register
2150 */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002151 ret = ab8500_charger_detect_chargers(di, false);
Arun Murthy84edbee2012-02-29 21:54:26 +05302152 if (ret < 0)
2153 return;
2154
2155 if (!(ret & USB_PW_CONN)) {
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002156 dev_dbg(di->dev, "%s di->vbus_detected = false\n", __func__);
2157 di->vbus_detected = false;
Arun Murthy84edbee2012-02-29 21:54:26 +05302158 ab8500_charger_set_usb_connected(di, false);
2159 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2160 } else {
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002161 dev_dbg(di->dev, "%s di->vbus_detected = true\n", __func__);
2162 di->vbus_detected = true;
Arun Murthy84edbee2012-02-29 21:54:26 +05302163
2164 if (is_ab8500_1p1_or_earlier(di->parent)) {
2165 ret = ab8500_charger_detect_usb_type(di);
2166 if (!ret) {
2167 ab8500_charger_set_usb_connected(di, true);
2168 ab8500_power_supply_changed(di,
2169 &di->usb_chg.psy);
2170 }
2171 } else {
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002172 /*
2173 * For ABB cut2.0 and onwards we have an IRQ,
Arun Murthy84edbee2012-02-29 21:54:26 +05302174 * USB_LINK_STATUS that will be triggered when the USB
2175 * link status changes. The exception is USB connected
2176 * during startup. Then we don't get a
2177 * USB_LINK_STATUS IRQ
2178 */
2179 if (di->vbus_detected_start) {
2180 di->vbus_detected_start = false;
2181 ret = ab8500_charger_detect_usb_type(di);
2182 if (!ret) {
2183 ab8500_charger_set_usb_connected(di,
2184 true);
2185 ab8500_power_supply_changed(di,
2186 &di->usb_chg.psy);
2187 }
2188 }
2189 }
2190 }
2191}
2192
2193/**
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002194 * ab8500_charger_usb_link_attach_work() - work to detect USB type
Marcus Cooper4b45f4a2013-01-11 13:13:04 +00002195 * @work: pointer to the work_struct structure
2196 *
2197 * Detect the type of USB plugged
2198 */
2199static void ab8500_charger_usb_link_attach_work(struct work_struct *work)
2200{
2201 struct ab8500_charger *di =
2202 container_of(work, struct ab8500_charger, attach_work.work);
2203 int ret;
2204
2205 /* Update maximum input current if USB enumeration is not detected */
2206 if (!di->usb.charger_online) {
2207 ret = ab8500_charger_set_vbus_in_curr(di, di->max_usb_in_curr);
2208 if (ret)
2209 return;
2210 }
2211
2212 ab8500_charger_set_usb_connected(di, true);
2213 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2214}
2215
2216/**
Arun Murthy84edbee2012-02-29 21:54:26 +05302217 * ab8500_charger_usb_link_status_work() - work to detect USB type
2218 * @work: pointer to the work_struct structure
2219 *
2220 * Detect the type of USB plugged
2221 */
2222static void ab8500_charger_usb_link_status_work(struct work_struct *work)
2223{
Henrik Sölverff380902012-04-17 15:51:01 +02002224 int detected_chargers;
Arun Murthy84edbee2012-02-29 21:54:26 +05302225 int ret;
Henrik Sölverff380902012-04-17 15:51:01 +02002226 u8 val;
Arun Murthy84edbee2012-02-29 21:54:26 +05302227
2228 struct ab8500_charger *di = container_of(work,
2229 struct ab8500_charger, usb_link_status_work);
2230
2231 /*
2232 * Since we can't be sure that the events are received
2233 * synchronously, we have the check if is
2234 * connected by reading the status register
2235 */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002236 detected_chargers = ab8500_charger_detect_chargers(di, false);
Henrik Sölverff380902012-04-17 15:51:01 +02002237 if (detected_chargers < 0)
Arun Murthy84edbee2012-02-29 21:54:26 +05302238 return;
2239
Henrik Sölverff380902012-04-17 15:51:01 +02002240 /*
2241 * Some chargers that breaks the USB spec is
2242 * identified as invalid by AB8500 and it refuse
2243 * to start the charging process. but by jumping
2244 * thru a few hoops it can be forced to start.
2245 */
2246 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
2247 AB8500_USB_LINE_STAT_REG, &val);
2248 if (ret >= 0)
2249 dev_dbg(di->dev, "UsbLineStatus register = 0x%02x\n", val);
2250 else
2251 dev_dbg(di->dev, "Error reading USB link status\n");
2252
2253 if (detected_chargers & USB_PW_CONN) {
2254 if (((val & AB8500_USB_LINK_STATUS) >> 3) == USB_STAT_NOT_VALID_LINK &&
2255 di->invalid_charger_detect_state == 0) {
2256 dev_dbg(di->dev, "Invalid charger detected, state= 0\n");
2257 /*Enable charger*/
2258 abx500_mask_and_set_register_interruptible(di->dev,
2259 AB8500_CHARGER, AB8500_USBCH_CTRL1_REG, 0x01, 0x01);
2260 /*Enable charger detection*/
2261 abx500_mask_and_set_register_interruptible(di->dev, AB8500_USB,
2262 AB8500_MCH_IPT_CURLVL_REG, 0x01, 0x01);
2263 di->invalid_charger_detect_state = 1;
2264 /*exit and wait for new link status interrupt.*/
2265 return;
2266
2267 }
2268 if (di->invalid_charger_detect_state == 1) {
2269 dev_dbg(di->dev, "Invalid charger detected, state= 1\n");
2270 /*Stop charger detection*/
2271 abx500_mask_and_set_register_interruptible(di->dev, AB8500_USB,
2272 AB8500_MCH_IPT_CURLVL_REG, 0x01, 0x00);
2273 /*Check link status*/
2274 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
2275 AB8500_USB_LINE_STAT_REG, &val);
2276 dev_dbg(di->dev, "USB link status= 0x%02x\n",
2277 (val & AB8500_USB_LINK_STATUS) >> 3);
2278 di->invalid_charger_detect_state = 2;
2279 }
2280 } else {
2281 di->invalid_charger_detect_state = 0;
2282 }
2283
2284 if (!(detected_chargers & USB_PW_CONN)) {
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002285 di->vbus_detected = false;
Arun Murthy84edbee2012-02-29 21:54:26 +05302286 ab8500_charger_set_usb_connected(di, false);
2287 ab8500_power_supply_changed(di, &di->usb_chg.psy);
Marcus Cooper4b45f4a2013-01-11 13:13:04 +00002288 return;
2289 }
Arun Murthy84edbee2012-02-29 21:54:26 +05302290
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002291 dev_dbg(di->dev,"%s di->vbus_detected = true\n",__func__);
2292 di->vbus_detected = true;
Marcus Cooper4b45f4a2013-01-11 13:13:04 +00002293 ret = ab8500_charger_read_usb_type(di);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002294 if (ret) {
2295 if (ret == -ENXIO) {
2296 /* No valid charger type detected */
2297 ab8500_charger_set_usb_connected(di, false);
2298 ab8500_power_supply_changed(di, &di->usb_chg.psy);
Arun Murthy84edbee2012-02-29 21:54:26 +05302299 }
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002300 return;
2301 }
2302
2303 if (di->usb_device_is_unrecognised) {
2304 dev_dbg(di->dev,
2305 "Potential Legacy Charger device. "
2306 "Delay work for %d msec for USB enum "
2307 "to finish",
2308 WAIT_ACA_RID_ENUMERATION);
2309 queue_delayed_work(di->charger_wq,
2310 &di->attach_work,
2311 msecs_to_jiffies(WAIT_ACA_RID_ENUMERATION));
2312 } else if (di->is_aca_rid == 1) {
2313 /* Only wait once */
2314 di->is_aca_rid++;
2315 dev_dbg(di->dev,
2316 "%s Wait %d msec for USB enum to finish",
2317 __func__, WAIT_ACA_RID_ENUMERATION);
2318 queue_delayed_work(di->charger_wq,
2319 &di->attach_work,
2320 msecs_to_jiffies(WAIT_ACA_RID_ENUMERATION));
2321 } else {
2322 queue_delayed_work(di->charger_wq,
2323 &di->attach_work,
2324 0);
Arun Murthy84edbee2012-02-29 21:54:26 +05302325 }
2326}
2327
2328static void ab8500_charger_usb_state_changed_work(struct work_struct *work)
2329{
2330 int ret;
2331 unsigned long flags;
2332
2333 struct ab8500_charger *di = container_of(work,
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002334 struct ab8500_charger, usb_state_changed_work.work);
Arun Murthy84edbee2012-02-29 21:54:26 +05302335
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002336 if (!di->vbus_detected) {
2337 dev_dbg(di->dev,
2338 "%s !di->vbus_detected\n",
2339 __func__);
Arun Murthy84edbee2012-02-29 21:54:26 +05302340 return;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002341 }
Arun Murthy84edbee2012-02-29 21:54:26 +05302342
2343 spin_lock_irqsave(&di->usb_state.usb_lock, flags);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002344 di->usb_state.state = di->usb_state.state_tmp;
2345 di->usb_state.usb_current = di->usb_state.usb_current_tmp;
Arun Murthy84edbee2012-02-29 21:54:26 +05302346 spin_unlock_irqrestore(&di->usb_state.usb_lock, flags);
2347
Arun Murthy84edbee2012-02-29 21:54:26 +05302348 dev_dbg(di->dev, "%s USB state: 0x%02x mA: %d\n",
2349 __func__, di->usb_state.state, di->usb_state.usb_current);
2350
2351 switch (di->usb_state.state) {
2352 case AB8500_BM_USB_STATE_RESET_HS:
2353 case AB8500_BM_USB_STATE_RESET_FS:
2354 case AB8500_BM_USB_STATE_SUSPEND:
2355 case AB8500_BM_USB_STATE_MAX:
2356 ab8500_charger_set_usb_connected(di, false);
2357 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2358 break;
2359
2360 case AB8500_BM_USB_STATE_RESUME:
2361 /*
2362 * when suspend->resume there should be delay
2363 * of 1sec for enabling charging
2364 */
2365 msleep(1000);
2366 /* Intentional fall through */
2367 case AB8500_BM_USB_STATE_CONFIGURED:
2368 /*
2369 * USB is configured, enable charging with the charging
2370 * input current obtained from USB driver
2371 */
2372 if (!ab8500_charger_get_usb_cur(di)) {
2373 /* Update maximum input current */
2374 ret = ab8500_charger_set_vbus_in_curr(di,
2375 di->max_usb_in_curr);
2376 if (ret)
2377 return;
2378
2379 ab8500_charger_set_usb_connected(di, true);
2380 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2381 }
2382 break;
2383
2384 default:
2385 break;
2386 };
2387}
2388
2389/**
2390 * ab8500_charger_check_usbchargernotok_work() - check USB chg not ok status
2391 * @work: pointer to the work_struct structure
2392 *
2393 * Work queue function for checking the USB charger Not OK status
2394 */
2395static void ab8500_charger_check_usbchargernotok_work(struct work_struct *work)
2396{
2397 int ret;
2398 u8 reg_value;
2399 bool prev_status;
2400
2401 struct ab8500_charger *di = container_of(work,
2402 struct ab8500_charger, check_usbchgnotok_work.work);
2403
2404 /* Check if the status bit for usbchargernotok is still active */
2405 ret = abx500_get_register_interruptible(di->dev,
2406 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, &reg_value);
2407 if (ret < 0) {
2408 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2409 return;
2410 }
2411 prev_status = di->flags.usbchargernotok;
2412
2413 if (reg_value & VBUS_CH_NOK) {
2414 di->flags.usbchargernotok = true;
2415 /* Check again in 1sec */
2416 queue_delayed_work(di->charger_wq,
2417 &di->check_usbchgnotok_work, HZ);
2418 } else {
2419 di->flags.usbchargernotok = false;
2420 di->flags.vbus_collapse = false;
2421 }
2422
2423 if (prev_status != di->flags.usbchargernotok)
2424 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2425}
2426
2427/**
2428 * ab8500_charger_check_main_thermal_prot_work() - check main thermal status
2429 * @work: pointer to the work_struct structure
2430 *
2431 * Work queue function for checking the Main thermal prot status
2432 */
2433static void ab8500_charger_check_main_thermal_prot_work(
2434 struct work_struct *work)
2435{
2436 int ret;
2437 u8 reg_value;
2438
2439 struct ab8500_charger *di = container_of(work,
2440 struct ab8500_charger, check_main_thermal_prot_work);
2441
2442 /* Check if the status bit for main_thermal_prot is still active */
2443 ret = abx500_get_register_interruptible(di->dev,
2444 AB8500_CHARGER, AB8500_CH_STATUS2_REG, &reg_value);
2445 if (ret < 0) {
2446 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2447 return;
2448 }
2449 if (reg_value & MAIN_CH_TH_PROT)
2450 di->flags.main_thermal_prot = true;
2451 else
2452 di->flags.main_thermal_prot = false;
2453
2454 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2455}
2456
2457/**
2458 * ab8500_charger_check_usb_thermal_prot_work() - check usb thermal status
2459 * @work: pointer to the work_struct structure
2460 *
2461 * Work queue function for checking the USB thermal prot status
2462 */
2463static void ab8500_charger_check_usb_thermal_prot_work(
2464 struct work_struct *work)
2465{
2466 int ret;
2467 u8 reg_value;
2468
2469 struct ab8500_charger *di = container_of(work,
2470 struct ab8500_charger, check_usb_thermal_prot_work);
2471
2472 /* Check if the status bit for usb_thermal_prot is still active */
2473 ret = abx500_get_register_interruptible(di->dev,
2474 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, &reg_value);
2475 if (ret < 0) {
2476 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2477 return;
2478 }
2479 if (reg_value & USB_CH_TH_PROT)
2480 di->flags.usb_thermal_prot = true;
2481 else
2482 di->flags.usb_thermal_prot = false;
2483
2484 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2485}
2486
2487/**
2488 * ab8500_charger_mainchunplugdet_handler() - main charger unplugged
2489 * @irq: interrupt number
2490 * @_di: pointer to the ab8500_charger structure
2491 *
2492 * Returns IRQ status(IRQ_HANDLED)
2493 */
2494static irqreturn_t ab8500_charger_mainchunplugdet_handler(int irq, void *_di)
2495{
2496 struct ab8500_charger *di = _di;
2497
2498 dev_dbg(di->dev, "Main charger unplugged\n");
2499 queue_work(di->charger_wq, &di->ac_work);
2500
Lee Jonesb269fff2013-01-11 13:12:51 +00002501 cancel_delayed_work_sync(&di->ac_charger_attached_work);
2502 mutex_lock(&di->charger_attached_mutex);
2503 mutex_unlock(&di->charger_attached_mutex);
2504
Arun Murthy84edbee2012-02-29 21:54:26 +05302505 return IRQ_HANDLED;
2506}
2507
2508/**
2509 * ab8500_charger_mainchplugdet_handler() - main charger plugged
2510 * @irq: interrupt number
2511 * @_di: pointer to the ab8500_charger structure
2512 *
2513 * Returns IRQ status(IRQ_HANDLED)
2514 */
2515static irqreturn_t ab8500_charger_mainchplugdet_handler(int irq, void *_di)
2516{
2517 struct ab8500_charger *di = _di;
2518
2519 dev_dbg(di->dev, "Main charger plugged\n");
2520 queue_work(di->charger_wq, &di->ac_work);
2521
Lee Jonesb269fff2013-01-11 13:12:51 +00002522 mutex_lock(&di->charger_attached_mutex);
2523 mutex_unlock(&di->charger_attached_mutex);
2524 queue_delayed_work(di->charger_wq,
2525 &di->ac_charger_attached_work,
2526 HZ);
Arun Murthy84edbee2012-02-29 21:54:26 +05302527 return IRQ_HANDLED;
2528}
2529
2530/**
2531 * ab8500_charger_mainextchnotok_handler() - main charger not ok
2532 * @irq: interrupt number
2533 * @_di: pointer to the ab8500_charger structure
2534 *
2535 * Returns IRQ status(IRQ_HANDLED)
2536 */
2537static irqreturn_t ab8500_charger_mainextchnotok_handler(int irq, void *_di)
2538{
2539 struct ab8500_charger *di = _di;
2540
2541 dev_dbg(di->dev, "Main charger not ok\n");
2542 di->flags.mainextchnotok = true;
2543 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2544
2545 /* Schedule a new HW failure check */
2546 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
2547
2548 return IRQ_HANDLED;
2549}
2550
2551/**
2552 * ab8500_charger_mainchthprotr_handler() - Die temp is above main charger
2553 * thermal protection threshold
2554 * @irq: interrupt number
2555 * @_di: pointer to the ab8500_charger structure
2556 *
2557 * Returns IRQ status(IRQ_HANDLED)
2558 */
2559static irqreturn_t ab8500_charger_mainchthprotr_handler(int irq, void *_di)
2560{
2561 struct ab8500_charger *di = _di;
2562
2563 dev_dbg(di->dev,
2564 "Die temp above Main charger thermal protection threshold\n");
2565 queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
2566
2567 return IRQ_HANDLED;
2568}
2569
2570/**
2571 * ab8500_charger_mainchthprotf_handler() - Die temp is below main charger
2572 * thermal protection threshold
2573 * @irq: interrupt number
2574 * @_di: pointer to the ab8500_charger structure
2575 *
2576 * Returns IRQ status(IRQ_HANDLED)
2577 */
2578static irqreturn_t ab8500_charger_mainchthprotf_handler(int irq, void *_di)
2579{
2580 struct ab8500_charger *di = _di;
2581
2582 dev_dbg(di->dev,
2583 "Die temp ok for Main charger thermal protection threshold\n");
2584 queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
2585
2586 return IRQ_HANDLED;
2587}
2588
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002589static void ab8500_charger_vbus_drop_end_work(struct work_struct *work)
2590{
2591 struct ab8500_charger *di = container_of(work,
2592 struct ab8500_charger, vbus_drop_end_work.work);
2593
2594 di->flags.vbus_drop_end = false;
2595
2596 /* Reset the drop counter */
2597 abx500_set_register_interruptible(di->dev,
2598 AB8500_CHARGER, AB8500_CHARGER_CTRL, 0x01);
2599
2600 if (di->usb.charger_connected)
2601 ab8500_charger_set_vbus_in_curr(di, di->max_usb_in_curr);
2602}
2603
Arun Murthy84edbee2012-02-29 21:54:26 +05302604/**
2605 * ab8500_charger_vbusdetf_handler() - VBUS falling detected
2606 * @irq: interrupt number
2607 * @_di: pointer to the ab8500_charger structure
2608 *
2609 * Returns IRQ status(IRQ_HANDLED)
2610 */
2611static irqreturn_t ab8500_charger_vbusdetf_handler(int irq, void *_di)
2612{
2613 struct ab8500_charger *di = _di;
2614
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002615 di->vbus_detected = false;
Arun Murthy84edbee2012-02-29 21:54:26 +05302616 dev_dbg(di->dev, "VBUS falling detected\n");
2617 queue_work(di->charger_wq, &di->detect_usb_type_work);
2618
2619 return IRQ_HANDLED;
2620}
2621
2622/**
2623 * ab8500_charger_vbusdetr_handler() - VBUS rising detected
2624 * @irq: interrupt number
2625 * @_di: pointer to the ab8500_charger structure
2626 *
2627 * Returns IRQ status(IRQ_HANDLED)
2628 */
2629static irqreturn_t ab8500_charger_vbusdetr_handler(int irq, void *_di)
2630{
2631 struct ab8500_charger *di = _di;
2632
2633 di->vbus_detected = true;
2634 dev_dbg(di->dev, "VBUS rising detected\n");
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002635
Arun Murthy84edbee2012-02-29 21:54:26 +05302636 queue_work(di->charger_wq, &di->detect_usb_type_work);
2637
2638 return IRQ_HANDLED;
2639}
2640
2641/**
2642 * ab8500_charger_usblinkstatus_handler() - USB link status has changed
2643 * @irq: interrupt number
2644 * @_di: pointer to the ab8500_charger structure
2645 *
2646 * Returns IRQ status(IRQ_HANDLED)
2647 */
2648static irqreturn_t ab8500_charger_usblinkstatus_handler(int irq, void *_di)
2649{
2650 struct ab8500_charger *di = _di;
2651
2652 dev_dbg(di->dev, "USB link status changed\n");
2653
2654 queue_work(di->charger_wq, &di->usb_link_status_work);
2655
2656 return IRQ_HANDLED;
2657}
2658
2659/**
2660 * ab8500_charger_usbchthprotr_handler() - Die temp is above usb charger
2661 * thermal protection threshold
2662 * @irq: interrupt number
2663 * @_di: pointer to the ab8500_charger structure
2664 *
2665 * Returns IRQ status(IRQ_HANDLED)
2666 */
2667static irqreturn_t ab8500_charger_usbchthprotr_handler(int irq, void *_di)
2668{
2669 struct ab8500_charger *di = _di;
2670
2671 dev_dbg(di->dev,
2672 "Die temp above USB charger thermal protection threshold\n");
2673 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2674
2675 return IRQ_HANDLED;
2676}
2677
2678/**
2679 * ab8500_charger_usbchthprotf_handler() - Die temp is below usb charger
2680 * thermal protection threshold
2681 * @irq: interrupt number
2682 * @_di: pointer to the ab8500_charger structure
2683 *
2684 * Returns IRQ status(IRQ_HANDLED)
2685 */
2686static irqreturn_t ab8500_charger_usbchthprotf_handler(int irq, void *_di)
2687{
2688 struct ab8500_charger *di = _di;
2689
2690 dev_dbg(di->dev,
2691 "Die temp ok for USB charger thermal protection threshold\n");
2692 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2693
2694 return IRQ_HANDLED;
2695}
2696
2697/**
2698 * ab8500_charger_usbchargernotokr_handler() - USB charger not ok detected
2699 * @irq: interrupt number
2700 * @_di: pointer to the ab8500_charger structure
2701 *
2702 * Returns IRQ status(IRQ_HANDLED)
2703 */
2704static irqreturn_t ab8500_charger_usbchargernotokr_handler(int irq, void *_di)
2705{
2706 struct ab8500_charger *di = _di;
2707
2708 dev_dbg(di->dev, "Not allowed USB charger detected\n");
2709 queue_delayed_work(di->charger_wq, &di->check_usbchgnotok_work, 0);
2710
2711 return IRQ_HANDLED;
2712}
2713
2714/**
2715 * ab8500_charger_chwdexp_handler() - Charger watchdog expired
2716 * @irq: interrupt number
2717 * @_di: pointer to the ab8500_charger structure
2718 *
2719 * Returns IRQ status(IRQ_HANDLED)
2720 */
2721static irqreturn_t ab8500_charger_chwdexp_handler(int irq, void *_di)
2722{
2723 struct ab8500_charger *di = _di;
2724
2725 dev_dbg(di->dev, "Charger watchdog expired\n");
2726
2727 /*
2728 * The charger that was online when the watchdog expired
2729 * needs to be restarted for charging to start again
2730 */
2731 if (di->ac.charger_online) {
2732 di->ac.wd_expired = true;
2733 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2734 }
2735 if (di->usb.charger_online) {
2736 di->usb.wd_expired = true;
2737 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2738 }
2739
2740 return IRQ_HANDLED;
2741}
2742
2743/**
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002744 * ab8500_charger_vbuschdropend_handler() - VBUS drop removed
2745 * @irq: interrupt number
2746 * @_di: pointer to the ab8500_charger structure
2747 *
2748 * Returns IRQ status(IRQ_HANDLED)
2749 */
2750static irqreturn_t ab8500_charger_vbuschdropend_handler(int irq, void *_di)
2751{
2752 struct ab8500_charger *di = _di;
2753
2754 dev_dbg(di->dev, "VBUS charger drop ended\n");
2755 di->flags.vbus_drop_end = true;
2756 queue_delayed_work(di->charger_wq, &di->vbus_drop_end_work,
2757 round_jiffies(30 * HZ));
2758
2759 return IRQ_HANDLED;
2760}
2761
2762/**
Arun Murthy84edbee2012-02-29 21:54:26 +05302763 * ab8500_charger_vbusovv_handler() - VBUS overvoltage detected
2764 * @irq: interrupt number
2765 * @_di: pointer to the ab8500_charger structure
2766 *
2767 * Returns IRQ status(IRQ_HANDLED)
2768 */
2769static irqreturn_t ab8500_charger_vbusovv_handler(int irq, void *_di)
2770{
2771 struct ab8500_charger *di = _di;
2772
2773 dev_dbg(di->dev, "VBUS overvoltage detected\n");
2774 di->flags.vbus_ovv = true;
2775 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2776
2777 /* Schedule a new HW failure check */
2778 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
2779
2780 return IRQ_HANDLED;
2781}
2782
2783/**
2784 * ab8500_charger_ac_get_property() - get the ac/mains properties
2785 * @psy: pointer to the power_supply structure
2786 * @psp: pointer to the power_supply_property structure
2787 * @val: pointer to the power_supply_propval union
2788 *
2789 * This function gets called when an application tries to get the ac/mains
2790 * properties by reading the sysfs files.
2791 * AC/Mains properties are online, present and voltage.
2792 * online: ac/mains charging is in progress or not
2793 * present: presence of the ac/mains
2794 * voltage: AC/Mains voltage
2795 * Returns error code in case of failure else 0(on success)
2796 */
2797static int ab8500_charger_ac_get_property(struct power_supply *psy,
2798 enum power_supply_property psp,
2799 union power_supply_propval *val)
2800{
2801 struct ab8500_charger *di;
Jonas Aaberga864c5a2013-01-11 13:12:53 +00002802 int ret;
Arun Murthy84edbee2012-02-29 21:54:26 +05302803
2804 di = to_ab8500_charger_ac_device_info(psy_to_ux500_charger(psy));
2805
2806 switch (psp) {
2807 case POWER_SUPPLY_PROP_HEALTH:
2808 if (di->flags.mainextchnotok)
2809 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
2810 else if (di->ac.wd_expired || di->usb.wd_expired)
2811 val->intval = POWER_SUPPLY_HEALTH_DEAD;
2812 else if (di->flags.main_thermal_prot)
2813 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
2814 else
2815 val->intval = POWER_SUPPLY_HEALTH_GOOD;
2816 break;
2817 case POWER_SUPPLY_PROP_ONLINE:
2818 val->intval = di->ac.charger_online;
2819 break;
2820 case POWER_SUPPLY_PROP_PRESENT:
2821 val->intval = di->ac.charger_connected;
2822 break;
2823 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Jonas Aaberga864c5a2013-01-11 13:12:53 +00002824 ret = ab8500_charger_get_ac_voltage(di);
2825 if (ret >= 0)
2826 di->ac.charger_voltage = ret;
2827 /* On error, use previous value */
Arun Murthy84edbee2012-02-29 21:54:26 +05302828 val->intval = di->ac.charger_voltage * 1000;
2829 break;
2830 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
2831 /*
2832 * This property is used to indicate when CV mode is entered
2833 * for the AC charger
2834 */
2835 di->ac.cv_active = ab8500_charger_ac_cv(di);
2836 val->intval = di->ac.cv_active;
2837 break;
2838 case POWER_SUPPLY_PROP_CURRENT_NOW:
Jonas Aaberga864c5a2013-01-11 13:12:53 +00002839 ret = ab8500_charger_get_ac_current(di);
2840 if (ret >= 0)
2841 di->ac.charger_current = ret;
2842 val->intval = di->ac.charger_current * 1000;
Arun Murthy84edbee2012-02-29 21:54:26 +05302843 break;
2844 default:
2845 return -EINVAL;
2846 }
2847 return 0;
2848}
2849
2850/**
2851 * ab8500_charger_usb_get_property() - get the usb properties
2852 * @psy: pointer to the power_supply structure
2853 * @psp: pointer to the power_supply_property structure
2854 * @val: pointer to the power_supply_propval union
2855 *
2856 * This function gets called when an application tries to get the usb
2857 * properties by reading the sysfs files.
2858 * USB properties are online, present and voltage.
2859 * online: usb charging is in progress or not
2860 * present: presence of the usb
2861 * voltage: vbus voltage
2862 * Returns error code in case of failure else 0(on success)
2863 */
2864static int ab8500_charger_usb_get_property(struct power_supply *psy,
2865 enum power_supply_property psp,
2866 union power_supply_propval *val)
2867{
2868 struct ab8500_charger *di;
Jonas Aaberga864c5a2013-01-11 13:12:53 +00002869 int ret;
Arun Murthy84edbee2012-02-29 21:54:26 +05302870
2871 di = to_ab8500_charger_usb_device_info(psy_to_ux500_charger(psy));
2872
2873 switch (psp) {
2874 case POWER_SUPPLY_PROP_HEALTH:
2875 if (di->flags.usbchargernotok)
2876 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
2877 else if (di->ac.wd_expired || di->usb.wd_expired)
2878 val->intval = POWER_SUPPLY_HEALTH_DEAD;
2879 else if (di->flags.usb_thermal_prot)
2880 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
2881 else if (di->flags.vbus_ovv)
2882 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
2883 else
2884 val->intval = POWER_SUPPLY_HEALTH_GOOD;
2885 break;
2886 case POWER_SUPPLY_PROP_ONLINE:
2887 val->intval = di->usb.charger_online;
2888 break;
2889 case POWER_SUPPLY_PROP_PRESENT:
2890 val->intval = di->usb.charger_connected;
2891 break;
2892 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Jonas Aaberga864c5a2013-01-11 13:12:53 +00002893 ret = ab8500_charger_get_vbus_voltage(di);
2894 if (ret >= 0)
2895 di->usb.charger_voltage = ret;
Arun Murthy84edbee2012-02-29 21:54:26 +05302896 val->intval = di->usb.charger_voltage * 1000;
2897 break;
2898 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
2899 /*
2900 * This property is used to indicate when CV mode is entered
2901 * for the USB charger
2902 */
2903 di->usb.cv_active = ab8500_charger_usb_cv(di);
2904 val->intval = di->usb.cv_active;
2905 break;
2906 case POWER_SUPPLY_PROP_CURRENT_NOW:
Jonas Aaberga864c5a2013-01-11 13:12:53 +00002907 ret = ab8500_charger_get_usb_current(di);
2908 if (ret >= 0)
2909 di->usb.charger_current = ret;
2910 val->intval = di->usb.charger_current * 1000;
Arun Murthy84edbee2012-02-29 21:54:26 +05302911 break;
2912 case POWER_SUPPLY_PROP_CURRENT_AVG:
2913 /*
2914 * This property is used to indicate when VBUS has collapsed
2915 * due to too high output current from the USB charger
2916 */
2917 if (di->flags.vbus_collapse)
2918 val->intval = 1;
2919 else
2920 val->intval = 0;
2921 break;
2922 default:
2923 return -EINVAL;
2924 }
2925 return 0;
2926}
2927
2928/**
2929 * ab8500_charger_init_hw_registers() - Set up charger related registers
2930 * @di: pointer to the ab8500_charger structure
2931 *
2932 * Set up charger OVV, watchdog and maximum voltage registers as well as
2933 * charging of the backup battery
2934 */
2935static int ab8500_charger_init_hw_registers(struct ab8500_charger *di)
2936{
2937 int ret = 0;
Yang QU0f4aa402012-06-26 19:25:52 +08002938 u8 bup_vch_range = 0, vbup33_vrtcn = 0;
Arun Murthy84edbee2012-02-29 21:54:26 +05302939
2940 /* Setup maximum charger current and voltage for ABB cut2.0 */
2941 if (!is_ab8500_1p1_or_earlier(di->parent)) {
2942 ret = abx500_set_register_interruptible(di->dev,
2943 AB8500_CHARGER,
2944 AB8500_CH_VOLT_LVL_MAX_REG, CH_VOL_LVL_4P6);
2945 if (ret) {
2946 dev_err(di->dev,
2947 "failed to set CH_VOLT_LVL_MAX_REG\n");
2948 goto out;
2949 }
2950
2951 ret = abx500_set_register_interruptible(di->dev,
2952 AB8500_CHARGER,
2953 AB8500_CH_OPT_CRNTLVL_MAX_REG, CH_OP_CUR_LVL_1P6);
2954 if (ret) {
2955 dev_err(di->dev,
2956 "failed to set CH_OPT_CRNTLVL_MAX_REG\n");
2957 goto out;
2958 }
2959 }
2960
Jonas Aaberg0ed51072012-05-11 12:42:25 +02002961 if (is_ab9540_2p0(di->parent) || is_ab8505_2p0(di->parent))
2962 ret = abx500_mask_and_set_register_interruptible(di->dev,
2963 AB8500_CHARGER,
2964 AB8500_USBCH_CTRL2_REG,
2965 VBUS_AUTO_IN_CURR_LIM_ENA,
2966 VBUS_AUTO_IN_CURR_LIM_ENA);
2967 else
2968 /*
2969 * VBUS OVV set to 6.3V and enable automatic current limitation
2970 */
2971 ret = abx500_set_register_interruptible(di->dev,
2972 AB8500_CHARGER,
2973 AB8500_USBCH_CTRL2_REG,
2974 VBUS_OVV_SELECT_6P3V | VBUS_AUTO_IN_CURR_LIM_ENA);
Arun Murthy84edbee2012-02-29 21:54:26 +05302975 if (ret) {
Jonas Aaberg0ed51072012-05-11 12:42:25 +02002976 dev_err(di->dev,
2977 "failed to set automatic current limitation\n");
Arun Murthy84edbee2012-02-29 21:54:26 +05302978 goto out;
2979 }
2980
2981 /* Enable main watchdog in OTP */
2982 ret = abx500_set_register_interruptible(di->dev,
2983 AB8500_OTP_EMUL, AB8500_OTP_CONF_15, OTP_ENABLE_WD);
2984 if (ret) {
2985 dev_err(di->dev, "failed to enable main WD in OTP\n");
2986 goto out;
2987 }
2988
2989 /* Enable main watchdog */
2990 ret = abx500_set_register_interruptible(di->dev,
2991 AB8500_SYS_CTRL2_BLOCK,
2992 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_ENA);
2993 if (ret) {
2994 dev_err(di->dev, "faile to enable main watchdog\n");
2995 goto out;
2996 }
2997
2998 /*
2999 * Due to internal synchronisation, Enable and Kick watchdog bits
3000 * cannot be enabled in a single write.
3001 * A minimum delay of 2*32 kHz period (62.5µs) must be inserted
3002 * between writing Enable then Kick bits.
3003 */
3004 udelay(63);
3005
3006 /* Kick main watchdog */
3007 ret = abx500_set_register_interruptible(di->dev,
3008 AB8500_SYS_CTRL2_BLOCK,
3009 AB8500_MAIN_WDOG_CTRL_REG,
3010 (MAIN_WDOG_ENA | MAIN_WDOG_KICK));
3011 if (ret) {
3012 dev_err(di->dev, "failed to kick main watchdog\n");
3013 goto out;
3014 }
3015
3016 /* Disable main watchdog */
3017 ret = abx500_set_register_interruptible(di->dev,
3018 AB8500_SYS_CTRL2_BLOCK,
3019 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_DIS);
3020 if (ret) {
3021 dev_err(di->dev, "failed to disable main watchdog\n");
3022 goto out;
3023 }
3024
3025 /* Set watchdog timeout */
3026 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
3027 AB8500_CH_WD_TIMER_REG, WD_TIMER);
3028 if (ret) {
3029 dev_err(di->dev, "failed to set charger watchdog timeout\n");
3030 goto out;
3031 }
3032
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003033 /* Set charger watchdog timeout */
3034 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
3035 AB8500_CH_WD_TIMER_REG, WD_TIMER);
3036 if (ret) {
3037 dev_err(di->dev, "failed to set charger watchdog timeout\n");
3038 goto out;
3039 }
3040
3041 ret = ab8500_charger_led_en(di, false);
3042 if (ret < 0) {
3043 dev_err(di->dev, "failed to disable LED\n");
3044 goto out;
3045 }
3046
Arun Murthy84edbee2012-02-29 21:54:26 +05303047 /* Backup battery voltage and current */
Yang QU0f4aa402012-06-26 19:25:52 +08003048 if (di->bm->bkup_bat_v > BUP_VCH_SEL_3P1V)
3049 bup_vch_range = BUP_VCH_RANGE;
3050 if (di->bm->bkup_bat_v == BUP_VCH_SEL_3P3V)
3051 vbup33_vrtcn = VBUP33_VRTCN;
3052
Arun Murthy84edbee2012-02-29 21:54:26 +05303053 ret = abx500_set_register_interruptible(di->dev,
3054 AB8500_RTC,
3055 AB8500_RTC_BACKUP_CHG_REG,
Yang QU0f4aa402012-06-26 19:25:52 +08003056 (di->bm->bkup_bat_v & 0x3) | di->bm->bkup_bat_i);
Arun Murthy84edbee2012-02-29 21:54:26 +05303057 if (ret) {
3058 dev_err(di->dev, "failed to setup backup battery charging\n");
3059 goto out;
3060 }
Yang QU0f4aa402012-06-26 19:25:52 +08003061 if (is_ab8540(di->parent)) {
3062 ret = abx500_set_register_interruptible(di->dev,
3063 AB8500_RTC,
3064 AB8500_RTC_CTRL1_REG,
3065 bup_vch_range | vbup33_vrtcn);
3066 if (ret) {
3067 dev_err(di->dev, "failed to setup backup battery charging\n");
3068 goto out;
3069 }
3070 }
Arun Murthy84edbee2012-02-29 21:54:26 +05303071
3072 /* Enable backup battery charging */
3073 abx500_mask_and_set_register_interruptible(di->dev,
3074 AB8500_RTC, AB8500_RTC_CTRL_REG,
3075 RTC_BUP_CH_ENA, RTC_BUP_CH_ENA);
3076 if (ret < 0)
3077 dev_err(di->dev, "%s mask and set failed\n", __func__);
3078
3079out:
3080 return ret;
3081}
3082
3083/*
3084 * ab8500 charger driver interrupts and their respective isr
3085 */
3086static struct ab8500_charger_interrupts ab8500_charger_irq[] = {
3087 {"MAIN_CH_UNPLUG_DET", ab8500_charger_mainchunplugdet_handler},
3088 {"MAIN_CHARGE_PLUG_DET", ab8500_charger_mainchplugdet_handler},
3089 {"MAIN_EXT_CH_NOT_OK", ab8500_charger_mainextchnotok_handler},
3090 {"MAIN_CH_TH_PROT_R", ab8500_charger_mainchthprotr_handler},
3091 {"MAIN_CH_TH_PROT_F", ab8500_charger_mainchthprotf_handler},
3092 {"VBUS_DET_F", ab8500_charger_vbusdetf_handler},
3093 {"VBUS_DET_R", ab8500_charger_vbusdetr_handler},
3094 {"USB_LINK_STATUS", ab8500_charger_usblinkstatus_handler},
3095 {"USB_CH_TH_PROT_R", ab8500_charger_usbchthprotr_handler},
3096 {"USB_CH_TH_PROT_F", ab8500_charger_usbchthprotf_handler},
3097 {"USB_CHARGER_NOT_OKR", ab8500_charger_usbchargernotokr_handler},
3098 {"VBUS_OVV", ab8500_charger_vbusovv_handler},
3099 {"CH_WD_EXP", ab8500_charger_chwdexp_handler},
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003100 {"VBUS_CH_DROP_END", ab8500_charger_vbuschdropend_handler},
Arun Murthy84edbee2012-02-29 21:54:26 +05303101};
3102
3103static int ab8500_charger_usb_notifier_call(struct notifier_block *nb,
3104 unsigned long event, void *power)
3105{
3106 struct ab8500_charger *di =
3107 container_of(nb, struct ab8500_charger, nb);
3108 enum ab8500_usb_state bm_usb_state;
3109 unsigned mA = *((unsigned *)power);
3110
Lee Jonesc9ade0f2013-01-21 11:22:56 +00003111 if (!di)
3112 return NOTIFY_DONE;
3113
Arun Murthy84edbee2012-02-29 21:54:26 +05303114 if (event != USB_EVENT_VBUS) {
3115 dev_dbg(di->dev, "not a standard host, returning\n");
3116 return NOTIFY_DONE;
3117 }
3118
3119 /* TODO: State is fabricate here. See if charger really needs USB
3120 * state or if mA is enough
3121 */
3122 if ((di->usb_state.usb_current == 2) && (mA > 2))
3123 bm_usb_state = AB8500_BM_USB_STATE_RESUME;
3124 else if (mA == 0)
3125 bm_usb_state = AB8500_BM_USB_STATE_RESET_HS;
3126 else if (mA == 2)
3127 bm_usb_state = AB8500_BM_USB_STATE_SUSPEND;
3128 else if (mA >= 8) /* 8, 100, 500 */
3129 bm_usb_state = AB8500_BM_USB_STATE_CONFIGURED;
3130 else /* Should never occur */
3131 bm_usb_state = AB8500_BM_USB_STATE_RESET_FS;
3132
3133 dev_dbg(di->dev, "%s usb_state: 0x%02x mA: %d\n",
3134 __func__, bm_usb_state, mA);
3135
3136 spin_lock(&di->usb_state.usb_lock);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003137 di->usb_state.state_tmp = bm_usb_state;
3138 di->usb_state.usb_current_tmp = mA;
Arun Murthy84edbee2012-02-29 21:54:26 +05303139 spin_unlock(&di->usb_state.usb_lock);
3140
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003141 /*
3142 * wait for some time until you get updates from the usb stack
3143 * and negotiations are completed
3144 */
3145 queue_delayed_work(di->charger_wq, &di->usb_state_changed_work, HZ/2);
Arun Murthy84edbee2012-02-29 21:54:26 +05303146
3147 return NOTIFY_OK;
3148}
3149
3150#if defined(CONFIG_PM)
3151static int ab8500_charger_resume(struct platform_device *pdev)
3152{
3153 int ret;
3154 struct ab8500_charger *di = platform_get_drvdata(pdev);
3155
3156 /*
3157 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
3158 * logic. That means we have to continously kick the charger
3159 * watchdog even when no charger is connected. This is only
3160 * valid once the AC charger has been enabled. This is
3161 * a bug that is not handled by the algorithm and the
3162 * watchdog have to be kicked by the charger driver
3163 * when the AC charger is disabled
3164 */
3165 if (di->ac_conn && is_ab8500_1p1_or_earlier(di->parent)) {
3166 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
3167 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
3168 if (ret)
3169 dev_err(di->dev, "Failed to kick WD!\n");
3170
3171 /* If not already pending start a new timer */
3172 if (!delayed_work_pending(
3173 &di->kick_wd_work)) {
3174 queue_delayed_work(di->charger_wq, &di->kick_wd_work,
3175 round_jiffies(WD_KICK_INTERVAL));
3176 }
3177 }
3178
3179 /* If we still have a HW failure, schedule a new check */
3180 if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
3181 queue_delayed_work(di->charger_wq,
3182 &di->check_hw_failure_work, 0);
3183 }
3184
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003185 if (di->flags.vbus_drop_end)
3186 queue_delayed_work(di->charger_wq, &di->vbus_drop_end_work, 0);
3187
Arun Murthy84edbee2012-02-29 21:54:26 +05303188 return 0;
3189}
3190
3191static int ab8500_charger_suspend(struct platform_device *pdev,
3192 pm_message_t state)
3193{
3194 struct ab8500_charger *di = platform_get_drvdata(pdev);
3195
3196 /* Cancel any pending HW failure check */
3197 if (delayed_work_pending(&di->check_hw_failure_work))
3198 cancel_delayed_work(&di->check_hw_failure_work);
3199
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003200 if (delayed_work_pending(&di->vbus_drop_end_work))
3201 cancel_delayed_work(&di->vbus_drop_end_work);
3202
Jonas Aaberg53ef1f52012-05-21 16:05:01 +02003203 flush_delayed_work(&di->attach_work);
3204 flush_delayed_work(&di->usb_charger_attached_work);
3205 flush_delayed_work(&di->ac_charger_attached_work);
3206 flush_delayed_work(&di->check_usbchgnotok_work);
3207 flush_delayed_work(&di->check_vbat_work);
3208 flush_delayed_work(&di->kick_wd_work);
3209
3210 flush_work(&di->usb_link_status_work);
3211 flush_work(&di->ac_work);
3212 flush_work(&di->detect_usb_type_work);
3213
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003214 if (atomic_read(&di->current_stepping_sessions))
3215 return -EAGAIN;
3216
Arun Murthy84edbee2012-02-29 21:54:26 +05303217 return 0;
3218}
3219#else
3220#define ab8500_charger_suspend NULL
3221#define ab8500_charger_resume NULL
3222#endif
3223
Bill Pemberton415ec692012-11-19 13:26:07 -05003224static int ab8500_charger_remove(struct platform_device *pdev)
Arun Murthy84edbee2012-02-29 21:54:26 +05303225{
3226 struct ab8500_charger *di = platform_get_drvdata(pdev);
3227 int i, irq, ret;
3228
3229 /* Disable AC charging */
3230 ab8500_charger_ac_en(&di->ac_chg, false, 0, 0);
3231
3232 /* Disable USB charging */
3233 ab8500_charger_usb_en(&di->usb_chg, false, 0, 0);
3234
3235 /* Disable interrupts */
3236 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
3237 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3238 free_irq(irq, di);
3239 }
3240
Arun Murthy84edbee2012-02-29 21:54:26 +05303241 /* Backup battery voltage and current disable */
3242 ret = abx500_mask_and_set_register_interruptible(di->dev,
3243 AB8500_RTC, AB8500_RTC_CTRL_REG, RTC_BUP_CH_ENA, 0);
3244 if (ret < 0)
3245 dev_err(di->dev, "%s mask and set failed\n", __func__);
3246
Anton Vorontsovefd71c82012-03-14 04:22:17 +04003247 usb_unregister_notifier(di->usb_phy, &di->nb);
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +05303248 usb_put_phy(di->usb_phy);
Arun Murthy84edbee2012-02-29 21:54:26 +05303249
3250 /* Delete the work queue */
3251 destroy_workqueue(di->charger_wq);
3252
3253 flush_scheduled_work();
Lee Jones72a90dd2013-02-14 09:22:48 +00003254 if (di->usb_chg.enabled)
Michel JAOUEN01ec8c52012-04-26 10:00:04 +02003255 power_supply_unregister(&di->usb_chg.psy);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003256#if !defined(CONFIG_CHARGER_PM2301)
Lee Jones72a90dd2013-02-14 09:22:48 +00003257 if (di->ac_chg.enabled)
Michel JAOUEN01ec8c52012-04-26 10:00:04 +02003258 power_supply_unregister(&di->ac_chg.psy);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003259#endif
Arun Murthy84edbee2012-02-29 21:54:26 +05303260 platform_set_drvdata(pdev, NULL);
Arun Murthy84edbee2012-02-29 21:54:26 +05303261
3262 return 0;
3263}
3264
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08003265static char *supply_interface[] = {
3266 "ab8500_chargalg",
3267 "ab8500_fg",
3268 "ab8500_btemp",
3269};
3270
Bill Pembertonc8afa642012-11-19 13:22:23 -05003271static int ab8500_charger_probe(struct platform_device *pdev)
Arun Murthy84edbee2012-02-29 21:54:26 +05303272{
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08003273 struct device_node *np = pdev->dev.of_node;
Lee Jones7722b792012-11-30 10:56:28 +00003274 struct abx500_bm_data *plat = pdev->dev.platform_data;
Lee Jones2aac3de2012-05-05 04:38:19 -07003275 struct ab8500_charger *di;
Lee Jonesb269fff2013-01-11 13:12:51 +00003276 int irq, i, charger_status, ret = 0, ch_stat;
Arun Murthy84edbee2012-02-29 21:54:26 +05303277
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08003278 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
3279 if (!di) {
3280 dev_err(&pdev->dev, "%s no mem for ab8500_charger\n", __func__);
Arun Murthy84edbee2012-02-29 21:54:26 +05303281 return -ENOMEM;
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08003282 }
Lee Jones7722b792012-11-30 10:56:28 +00003283
3284 if (!plat) {
3285 dev_err(&pdev->dev, "no battery management data supplied\n");
3286 return -EINVAL;
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08003287 }
Lee Jones7722b792012-11-30 10:56:28 +00003288 di->bm = plat;
3289
3290 if (np) {
3291 ret = ab8500_bm_of_probe(&pdev->dev, np, di->bm);
3292 if (ret) {
3293 dev_err(&pdev->dev, "failed to get battery information\n");
3294 return ret;
3295 }
3296 di->autopower_cfg = of_property_read_bool(np, "autopower_cfg");
3297 } else
3298 di->autopower_cfg = false;
Arun Murthy84edbee2012-02-29 21:54:26 +05303299
3300 /* get parent data */
3301 di->dev = &pdev->dev;
3302 di->parent = dev_get_drvdata(pdev->dev.parent);
3303 di->gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
3304
3305 /* initialize lock */
3306 spin_lock_init(&di->usb_state.usb_lock);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003307 mutex_init(&di->usb_ipt_crnt_lock);
Arun Murthy84edbee2012-02-29 21:54:26 +05303308
Arun Murthy84edbee2012-02-29 21:54:26 +05303309 di->autopower = false;
Henrik Sölverff380902012-04-17 15:51:01 +02003310 di->invalid_charger_detect_state = 0;
Arun Murthy84edbee2012-02-29 21:54:26 +05303311
3312 /* AC supply */
3313 /* power_supply base class */
3314 di->ac_chg.psy.name = "ab8500_ac";
3315 di->ac_chg.psy.type = POWER_SUPPLY_TYPE_MAINS;
3316 di->ac_chg.psy.properties = ab8500_charger_ac_props;
3317 di->ac_chg.psy.num_properties = ARRAY_SIZE(ab8500_charger_ac_props);
3318 di->ac_chg.psy.get_property = ab8500_charger_ac_get_property;
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08003319 di->ac_chg.psy.supplied_to = supply_interface;
3320 di->ac_chg.psy.num_supplicants = ARRAY_SIZE(supply_interface),
Arun Murthy84edbee2012-02-29 21:54:26 +05303321 /* ux500_charger sub-class */
3322 di->ac_chg.ops.enable = &ab8500_charger_ac_en;
Lee Jones4dcdf572013-02-14 09:24:10 +00003323 di->ac_chg.ops.check_enable = &ab8500_charger_ac_check_enable;
Arun Murthy84edbee2012-02-29 21:54:26 +05303324 di->ac_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
3325 di->ac_chg.ops.update_curr = &ab8500_charger_update_charger_current;
3326 di->ac_chg.max_out_volt = ab8500_charger_voltage_map[
3327 ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
3328 di->ac_chg.max_out_curr = ab8500_charger_current_map[
3329 ARRAY_SIZE(ab8500_charger_current_map) - 1];
Loic Pallardye07a5642012-05-10 15:33:56 +02003330 di->ac_chg.wdt_refresh = CHG_WD_INTERVAL;
Lee Jones97034a12013-01-17 16:08:42 +00003331 di->ac_chg.enabled = di->bm->ac_enabled;
Loic Pallardye07a5642012-05-10 15:33:56 +02003332 di->ac_chg.external = false;
Arun Murthy84edbee2012-02-29 21:54:26 +05303333
3334 /* USB supply */
3335 /* power_supply base class */
3336 di->usb_chg.psy.name = "ab8500_usb";
3337 di->usb_chg.psy.type = POWER_SUPPLY_TYPE_USB;
3338 di->usb_chg.psy.properties = ab8500_charger_usb_props;
3339 di->usb_chg.psy.num_properties = ARRAY_SIZE(ab8500_charger_usb_props);
3340 di->usb_chg.psy.get_property = ab8500_charger_usb_get_property;
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08003341 di->usb_chg.psy.supplied_to = supply_interface;
3342 di->usb_chg.psy.num_supplicants = ARRAY_SIZE(supply_interface),
Arun Murthy84edbee2012-02-29 21:54:26 +05303343 /* ux500_charger sub-class */
3344 di->usb_chg.ops.enable = &ab8500_charger_usb_en;
Lee Jones4dcdf572013-02-14 09:24:10 +00003345 di->usb_chg.ops.check_enable = &ab8500_charger_usb_check_enable;
Arun Murthy84edbee2012-02-29 21:54:26 +05303346 di->usb_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
3347 di->usb_chg.ops.update_curr = &ab8500_charger_update_charger_current;
3348 di->usb_chg.max_out_volt = ab8500_charger_voltage_map[
3349 ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
3350 di->usb_chg.max_out_curr = ab8500_charger_current_map[
3351 ARRAY_SIZE(ab8500_charger_current_map) - 1];
Loic Pallardye07a5642012-05-10 15:33:56 +02003352 di->usb_chg.wdt_refresh = CHG_WD_INTERVAL;
Lee Jones97034a12013-01-17 16:08:42 +00003353 di->usb_chg.enabled = di->bm->usb_enabled;
Loic Pallardye07a5642012-05-10 15:33:56 +02003354 di->usb_chg.external = false;
Arun Murthy84edbee2012-02-29 21:54:26 +05303355
3356 /* Create a work queue for the charger */
3357 di->charger_wq =
3358 create_singlethread_workqueue("ab8500_charger_wq");
3359 if (di->charger_wq == NULL) {
3360 dev_err(di->dev, "failed to create work queue\n");
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08003361 return -ENOMEM;
Arun Murthy84edbee2012-02-29 21:54:26 +05303362 }
3363
Lee Jonesb269fff2013-01-11 13:12:51 +00003364 mutex_init(&di->charger_attached_mutex);
3365
Arun Murthy84edbee2012-02-29 21:54:26 +05303366 /* Init work for HW failure check */
Tejun Heo203b42f2012-08-21 13:18:23 -07003367 INIT_DEFERRABLE_WORK(&di->check_hw_failure_work,
Arun Murthy84edbee2012-02-29 21:54:26 +05303368 ab8500_charger_check_hw_failure_work);
Tejun Heo203b42f2012-08-21 13:18:23 -07003369 INIT_DEFERRABLE_WORK(&di->check_usbchgnotok_work,
Arun Murthy84edbee2012-02-29 21:54:26 +05303370 ab8500_charger_check_usbchargernotok_work);
3371
Lee Jonesb269fff2013-01-11 13:12:51 +00003372 INIT_DELAYED_WORK(&di->ac_charger_attached_work,
3373 ab8500_charger_ac_attached_work);
3374 INIT_DELAYED_WORK(&di->usb_charger_attached_work,
3375 ab8500_charger_usb_attached_work);
3376
Arun Murthy84edbee2012-02-29 21:54:26 +05303377 /*
3378 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
3379 * logic. That means we have to continously kick the charger
3380 * watchdog even when no charger is connected. This is only
3381 * valid once the AC charger has been enabled. This is
3382 * a bug that is not handled by the algorithm and the
3383 * watchdog have to be kicked by the charger driver
3384 * when the AC charger is disabled
3385 */
Tejun Heo203b42f2012-08-21 13:18:23 -07003386 INIT_DEFERRABLE_WORK(&di->kick_wd_work,
Arun Murthy84edbee2012-02-29 21:54:26 +05303387 ab8500_charger_kick_watchdog_work);
3388
Tejun Heo203b42f2012-08-21 13:18:23 -07003389 INIT_DEFERRABLE_WORK(&di->check_vbat_work,
Arun Murthy84edbee2012-02-29 21:54:26 +05303390 ab8500_charger_check_vbat_work);
3391
Marcus Cooper4b45f4a2013-01-11 13:13:04 +00003392 INIT_DELAYED_WORK(&di->attach_work,
3393 ab8500_charger_usb_link_attach_work);
3394
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003395 INIT_DELAYED_WORK(&di->usb_state_changed_work,
3396 ab8500_charger_usb_state_changed_work);
3397
3398 INIT_DELAYED_WORK(&di->vbus_drop_end_work,
3399 ab8500_charger_vbus_drop_end_work);
3400
Arun Murthy84edbee2012-02-29 21:54:26 +05303401 /* Init work for charger detection */
3402 INIT_WORK(&di->usb_link_status_work,
3403 ab8500_charger_usb_link_status_work);
3404 INIT_WORK(&di->ac_work, ab8500_charger_ac_work);
3405 INIT_WORK(&di->detect_usb_type_work,
3406 ab8500_charger_detect_usb_type_work);
3407
Arun Murthy84edbee2012-02-29 21:54:26 +05303408 /* Init work for checking HW status */
3409 INIT_WORK(&di->check_main_thermal_prot_work,
3410 ab8500_charger_check_main_thermal_prot_work);
3411 INIT_WORK(&di->check_usb_thermal_prot_work,
3412 ab8500_charger_check_usb_thermal_prot_work);
3413
3414 /*
3415 * VDD ADC supply needs to be enabled from this driver when there
3416 * is a charger connected to avoid erroneous BTEMP_HIGH/LOW
3417 * interrupts during charging
3418 */
Sachin Kamat8feffd12012-12-07 17:23:28 +05303419 di->regu = devm_regulator_get(di->dev, "vddadc");
Arun Murthy84edbee2012-02-29 21:54:26 +05303420 if (IS_ERR(di->regu)) {
3421 ret = PTR_ERR(di->regu);
3422 dev_err(di->dev, "failed to get vddadc regulator\n");
3423 goto free_charger_wq;
3424 }
3425
3426
3427 /* Initialize OVV, and other registers */
3428 ret = ab8500_charger_init_hw_registers(di);
3429 if (ret) {
3430 dev_err(di->dev, "failed to initialize ABB registers\n");
Sachin Kamat8feffd12012-12-07 17:23:28 +05303431 goto free_charger_wq;
Arun Murthy84edbee2012-02-29 21:54:26 +05303432 }
3433
3434 /* Register AC charger class */
Lee Jones72a90dd2013-02-14 09:22:48 +00003435 if (di->ac_chg.enabled) {
Michel JAOUEN01ec8c52012-04-26 10:00:04 +02003436 ret = power_supply_register(di->dev, &di->ac_chg.psy);
3437 if (ret) {
3438 dev_err(di->dev, "failed to register AC charger\n");
3439 goto free_charger_wq;
3440 }
Arun Murthy84edbee2012-02-29 21:54:26 +05303441 }
3442
3443 /* Register USB charger class */
Lee Jones72a90dd2013-02-14 09:22:48 +00003444 if (di->usb_chg.enabled) {
Michel JAOUEN01ec8c52012-04-26 10:00:04 +02003445 ret = power_supply_register(di->dev, &di->usb_chg.psy);
3446 if (ret) {
3447 dev_err(di->dev, "failed to register USB charger\n");
3448 goto free_ac;
3449 }
Arun Murthy84edbee2012-02-29 21:54:26 +05303450 }
3451
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +05303452 di->usb_phy = usb_get_phy(USB_PHY_TYPE_USB2);
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +05303453 if (IS_ERR_OR_NULL(di->usb_phy)) {
Anton Vorontsovefd71c82012-03-14 04:22:17 +04003454 dev_err(di->dev, "failed to get usb transceiver\n");
Arun Murthy84edbee2012-02-29 21:54:26 +05303455 ret = -EINVAL;
3456 goto free_usb;
3457 }
3458 di->nb.notifier_call = ab8500_charger_usb_notifier_call;
Anton Vorontsovefd71c82012-03-14 04:22:17 +04003459 ret = usb_register_notifier(di->usb_phy, &di->nb);
Arun Murthy84edbee2012-02-29 21:54:26 +05303460 if (ret) {
Anton Vorontsovefd71c82012-03-14 04:22:17 +04003461 dev_err(di->dev, "failed to register usb notifier\n");
3462 goto put_usb_phy;
Arun Murthy84edbee2012-02-29 21:54:26 +05303463 }
3464
3465 /* Identify the connected charger types during startup */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003466 charger_status = ab8500_charger_detect_chargers(di, true);
Arun Murthy84edbee2012-02-29 21:54:26 +05303467 if (charger_status & AC_PW_CONN) {
3468 di->ac.charger_connected = 1;
3469 di->ac_conn = true;
3470 ab8500_power_supply_changed(di, &di->ac_chg.psy);
3471 sysfs_notify(&di->ac_chg.psy.dev->kobj, NULL, "present");
3472 }
3473
3474 if (charger_status & USB_PW_CONN) {
Arun Murthy84edbee2012-02-29 21:54:26 +05303475 di->vbus_detected = true;
3476 di->vbus_detected_start = true;
3477 queue_work(di->charger_wq,
3478 &di->detect_usb_type_work);
3479 }
3480
3481 /* Register interrupts */
3482 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
3483 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3484 ret = request_threaded_irq(irq, NULL, ab8500_charger_irq[i].isr,
3485 IRQF_SHARED | IRQF_NO_SUSPEND,
3486 ab8500_charger_irq[i].name, di);
3487
3488 if (ret != 0) {
3489 dev_err(di->dev, "failed to request %s IRQ %d: %d\n"
3490 , ab8500_charger_irq[i].name, irq, ret);
3491 goto free_irq;
3492 }
3493 dev_dbg(di->dev, "Requested %s IRQ %d: %d\n",
3494 ab8500_charger_irq[i].name, irq, ret);
3495 }
3496
3497 platform_set_drvdata(pdev, di);
3498
Lee Jonesb269fff2013-01-11 13:12:51 +00003499 mutex_lock(&di->charger_attached_mutex);
3500
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003501 ch_stat = ab8500_charger_detect_chargers(di, false);
Lee Jonesb269fff2013-01-11 13:12:51 +00003502
3503 if ((ch_stat & AC_PW_CONN) == AC_PW_CONN) {
3504 queue_delayed_work(di->charger_wq,
3505 &di->ac_charger_attached_work,
3506 HZ);
3507 }
3508 if ((ch_stat & USB_PW_CONN) == USB_PW_CONN) {
3509 queue_delayed_work(di->charger_wq,
3510 &di->usb_charger_attached_work,
3511 HZ);
3512 }
3513
3514 mutex_unlock(&di->charger_attached_mutex);
3515
Arun Murthy84edbee2012-02-29 21:54:26 +05303516 return ret;
3517
3518free_irq:
Anton Vorontsovefd71c82012-03-14 04:22:17 +04003519 usb_unregister_notifier(di->usb_phy, &di->nb);
Arun Murthy84edbee2012-02-29 21:54:26 +05303520
3521 /* We also have to free all successfully registered irqs */
3522 for (i = i - 1; i >= 0; i--) {
3523 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3524 free_irq(irq, di);
3525 }
Anton Vorontsovefd71c82012-03-14 04:22:17 +04003526put_usb_phy:
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +05303527 usb_put_phy(di->usb_phy);
Arun Murthy84edbee2012-02-29 21:54:26 +05303528free_usb:
Lee Jones72a90dd2013-02-14 09:22:48 +00003529 if (di->usb_chg.enabled)
Michel JAOUEN01ec8c52012-04-26 10:00:04 +02003530 power_supply_unregister(&di->usb_chg.psy);
Arun Murthy84edbee2012-02-29 21:54:26 +05303531free_ac:
Lee Jones72a90dd2013-02-14 09:22:48 +00003532 if (di->ac_chg.enabled)
Michel JAOUEN01ec8c52012-04-26 10:00:04 +02003533 power_supply_unregister(&di->ac_chg.psy);
Arun Murthy84edbee2012-02-29 21:54:26 +05303534free_charger_wq:
3535 destroy_workqueue(di->charger_wq);
Arun Murthy84edbee2012-02-29 21:54:26 +05303536 return ret;
3537}
3538
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08003539static const struct of_device_id ab8500_charger_match[] = {
3540 { .compatible = "stericsson,ab8500-charger", },
3541 { },
3542};
3543
Arun Murthy84edbee2012-02-29 21:54:26 +05303544static struct platform_driver ab8500_charger_driver = {
3545 .probe = ab8500_charger_probe,
Bill Pemberton28ea73f2012-11-19 13:20:40 -05003546 .remove = ab8500_charger_remove,
Arun Murthy84edbee2012-02-29 21:54:26 +05303547 .suspend = ab8500_charger_suspend,
3548 .resume = ab8500_charger_resume,
3549 .driver = {
3550 .name = "ab8500-charger",
3551 .owner = THIS_MODULE,
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08003552 .of_match_table = ab8500_charger_match,
Arun Murthy84edbee2012-02-29 21:54:26 +05303553 },
3554};
3555
3556static int __init ab8500_charger_init(void)
3557{
3558 return platform_driver_register(&ab8500_charger_driver);
3559}
3560
3561static void __exit ab8500_charger_exit(void)
3562{
3563 platform_driver_unregister(&ab8500_charger_driver);
3564}
3565
3566subsys_initcall_sync(ab8500_charger_init);
3567module_exit(ab8500_charger_exit);
3568
3569MODULE_LICENSE("GPL v2");
3570MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy");
3571MODULE_ALIAS("platform:ab8500-charger");
3572MODULE_DESCRIPTION("AB8500 charger management driver");