blob: fd350189b6329baaf3a6808462eb1b479394c979 [file] [log] [blame]
Arun Murthy84edbee2012-02-29 21:54:26 +05301/*
2 * Copyright (C) ST-Ericsson SA 2012
3 *
4 * Charger driver for AB8500
5 *
6 * License Terms: GNU General Public License v2
7 * Author:
8 * Johan Palsson <johan.palsson@stericsson.com>
9 * Karl Komierowski <karl.komierowski@stericsson.com>
10 * Arun R Murthy <arun.murthy@stericsson.com>
11 */
12
13#include <linux/init.h>
14#include <linux/module.h>
15#include <linux/device.h>
16#include <linux/interrupt.h>
17#include <linux/delay.h>
Lee Jones88917162013-02-13 11:39:19 +000018#include <linux/notifier.h>
Arun Murthy84edbee2012-02-29 21:54:26 +053019#include <linux/slab.h>
20#include <linux/platform_device.h>
21#include <linux/power_supply.h>
22#include <linux/completion.h>
23#include <linux/regulator/consumer.h>
24#include <linux/err.h>
25#include <linux/workqueue.h>
26#include <linux/kobject.h>
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -080027#include <linux/of.h>
28#include <linux/mfd/core.h>
Arun Murthy84edbee2012-02-29 21:54:26 +053029#include <linux/mfd/abx500/ab8500.h>
30#include <linux/mfd/abx500.h>
31#include <linux/mfd/abx500/ab8500-bm.h>
32#include <linux/mfd/abx500/ab8500-gpadc.h>
33#include <linux/mfd/abx500/ux500_chargalg.h>
34#include <linux/usb/otg.h>
Lee Jonesb269fff2013-01-11 13:12:51 +000035#include <linux/mutex.h>
Arun Murthy84edbee2012-02-29 21:54:26 +053036
37/* Charger constants */
38#define NO_PW_CONN 0
39#define AC_PW_CONN 1
40#define USB_PW_CONN 2
41
42#define MAIN_WDOG_ENA 0x01
43#define MAIN_WDOG_KICK 0x02
44#define MAIN_WDOG_DIS 0x00
45#define CHARG_WD_KICK 0x01
46#define MAIN_CH_ENA 0x01
47#define MAIN_CH_NO_OVERSHOOT_ENA_N 0x02
48#define USB_CH_ENA 0x01
49#define USB_CHG_NO_OVERSHOOT_ENA_N 0x02
50#define MAIN_CH_DET 0x01
51#define MAIN_CH_CV_ON 0x04
52#define USB_CH_CV_ON 0x08
53#define VBUS_DET_DBNC100 0x02
54#define VBUS_DET_DBNC1 0x01
55#define OTP_ENABLE_WD 0x01
Lee Jones4dcdf572013-02-14 09:24:10 +000056#define DROP_COUNT_RESET 0x01
Marcus Cooperb3ea5f42012-08-29 17:56:19 +020057#define USB_CH_DET 0x01
Arun Murthy84edbee2012-02-29 21:54:26 +053058
59#define MAIN_CH_INPUT_CURR_SHIFT 4
60#define VBUS_IN_CURR_LIM_SHIFT 4
Lee Jones861a30d2012-08-29 20:36:51 +080061#define AB8540_VBUS_IN_CURR_LIM_SHIFT 2
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +010062#define AUTO_VBUS_IN_CURR_LIM_SHIFT 4
Lee Jones861a30d2012-08-29 20:36:51 +080063#define AB8540_AUTO_VBUS_IN_CURR_MASK 0x3F
Lee Jonesf7470b52013-02-14 12:37:29 +000064#define VBUS_IN_CURR_LIM_RETRY_SET_TIME 30 /* seconds */
Arun Murthy84edbee2012-02-29 21:54:26 +053065
66#define LED_INDICATOR_PWM_ENA 0x01
67#define LED_INDICATOR_PWM_DIS 0x00
68#define LED_IND_CUR_5MA 0x04
69#define LED_INDICATOR_PWM_DUTY_252_256 0xBF
70
71/* HW failure constants */
72#define MAIN_CH_TH_PROT 0x02
73#define VBUS_CH_NOK 0x08
74#define USB_CH_TH_PROT 0x02
75#define VBUS_OVV_TH 0x01
76#define MAIN_CH_NOK 0x01
77#define VBUS_DET 0x80
78
Lee Jonesb269fff2013-01-11 13:12:51 +000079#define MAIN_CH_STATUS2_MAINCHGDROP 0x80
80#define MAIN_CH_STATUS2_MAINCHARGERDETDBNC 0x40
81#define USB_CH_VBUSDROP 0x40
82#define USB_CH_VBUSDETDBNC 0x01
83
Arun Murthy84edbee2012-02-29 21:54:26 +053084/* UsbLineStatus register bit masks */
85#define AB8500_USB_LINK_STATUS 0x78
Hakan Bergd4337662012-07-26 11:10:35 +020086#define AB8505_USB_LINK_STATUS 0xF8
Arun Murthy84edbee2012-02-29 21:54:26 +053087#define AB8500_STD_HOST_SUSP 0x18
Lee Jones861a30d2012-08-29 20:36:51 +080088#define USB_LINK_STATUS_SHIFT 3
Arun Murthy84edbee2012-02-29 21:54:26 +053089
90/* Watchdog timeout constant */
91#define WD_TIMER 0x30 /* 4min */
92#define WD_KICK_INTERVAL (60 * HZ)
93
94/* Lowest charger voltage is 3.39V -> 0x4E */
95#define LOW_VOLT_REG 0x4E
96
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +010097/* Step up/down delay in us */
98#define STEP_UDELAY 1000
99
Lee Jonesb269fff2013-01-11 13:12:51 +0000100#define CHARGER_STATUS_POLL 10 /* in ms */
101
Loic Pallardye07a5642012-05-10 15:33:56 +0200102#define CHG_WD_INTERVAL (60 * HZ)
103
Nicolas Guionb0163222012-05-23 15:59:43 +0200104#define AB8500_SW_CONTROL_FALLBACK 0x03
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100105/* Wait for enumeration before charing in us */
106#define WAIT_ACA_RID_ENUMERATION (5 * 1000)
Lee Jones88917162013-02-13 11:39:19 +0000107/*External charger control*/
108#define AB8500_SYS_CHARGER_CONTROL_REG 0x52
109#define EXTERNAL_CHARGER_DISABLE_REG_VAL 0x03
110#define EXTERNAL_CHARGER_ENABLE_REG_VAL 0x07
Nicolas Guionb0163222012-05-23 15:59:43 +0200111
Arun Murthy84edbee2012-02-29 21:54:26 +0530112/* UsbLineStatus register - usb types */
113enum ab8500_charger_link_status {
114 USB_STAT_NOT_CONFIGURED,
115 USB_STAT_STD_HOST_NC,
116 USB_STAT_STD_HOST_C_NS,
117 USB_STAT_STD_HOST_C_S,
118 USB_STAT_HOST_CHG_NM,
119 USB_STAT_HOST_CHG_HS,
120 USB_STAT_HOST_CHG_HS_CHIRP,
121 USB_STAT_DEDICATED_CHG,
122 USB_STAT_ACA_RID_A,
123 USB_STAT_ACA_RID_B,
124 USB_STAT_ACA_RID_C_NM,
125 USB_STAT_ACA_RID_C_HS,
126 USB_STAT_ACA_RID_C_HS_CHIRP,
127 USB_STAT_HM_IDGND,
128 USB_STAT_RESERVED,
129 USB_STAT_NOT_VALID_LINK,
Hakan Berg74a8e342013-01-11 13:12:58 +0000130 USB_STAT_PHY_EN,
131 USB_STAT_SUP_NO_IDGND_VBUS,
132 USB_STAT_SUP_IDGND_VBUS,
133 USB_STAT_CHARGER_LINE_1,
134 USB_STAT_CARKIT_1,
135 USB_STAT_CARKIT_2,
136 USB_STAT_ACA_DOCK_CHARGER,
Arun Murthy84edbee2012-02-29 21:54:26 +0530137};
138
139enum ab8500_usb_state {
140 AB8500_BM_USB_STATE_RESET_HS, /* HighSpeed Reset */
141 AB8500_BM_USB_STATE_RESET_FS, /* FullSpeed/LowSpeed Reset */
142 AB8500_BM_USB_STATE_CONFIGURED,
143 AB8500_BM_USB_STATE_SUSPEND,
144 AB8500_BM_USB_STATE_RESUME,
145 AB8500_BM_USB_STATE_MAX,
146};
147
148/* VBUS input current limits supported in AB8500 in mA */
149#define USB_CH_IP_CUR_LVL_0P05 50
150#define USB_CH_IP_CUR_LVL_0P09 98
151#define USB_CH_IP_CUR_LVL_0P19 193
152#define USB_CH_IP_CUR_LVL_0P29 290
153#define USB_CH_IP_CUR_LVL_0P38 380
154#define USB_CH_IP_CUR_LVL_0P45 450
155#define USB_CH_IP_CUR_LVL_0P5 500
156#define USB_CH_IP_CUR_LVL_0P6 600
157#define USB_CH_IP_CUR_LVL_0P7 700
158#define USB_CH_IP_CUR_LVL_0P8 800
159#define USB_CH_IP_CUR_LVL_0P9 900
160#define USB_CH_IP_CUR_LVL_1P0 1000
161#define USB_CH_IP_CUR_LVL_1P1 1100
162#define USB_CH_IP_CUR_LVL_1P3 1300
163#define USB_CH_IP_CUR_LVL_1P4 1400
164#define USB_CH_IP_CUR_LVL_1P5 1500
165
166#define VBAT_TRESH_IP_CUR_RED 3800
167
168#define to_ab8500_charger_usb_device_info(x) container_of((x), \
169 struct ab8500_charger, usb_chg)
170#define to_ab8500_charger_ac_device_info(x) container_of((x), \
171 struct ab8500_charger, ac_chg)
172
173/**
174 * struct ab8500_charger_interrupts - ab8500 interupts
175 * @name: name of the interrupt
176 * @isr function pointer to the isr
177 */
178struct ab8500_charger_interrupts {
179 char *name;
180 irqreturn_t (*isr)(int irq, void *data);
181};
182
183struct ab8500_charger_info {
184 int charger_connected;
185 int charger_online;
186 int charger_voltage;
187 int cv_active;
188 bool wd_expired;
Jonas Aaberga864c5a2013-01-11 13:12:53 +0000189 int charger_current;
Arun Murthy84edbee2012-02-29 21:54:26 +0530190};
191
192struct ab8500_charger_event_flags {
193 bool mainextchnotok;
194 bool main_thermal_prot;
195 bool usb_thermal_prot;
196 bool vbus_ovv;
197 bool usbchargernotok;
198 bool chgwdexp;
199 bool vbus_collapse;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100200 bool vbus_drop_end;
Arun Murthy84edbee2012-02-29 21:54:26 +0530201};
202
203struct ab8500_charger_usb_state {
Arun Murthy84edbee2012-02-29 21:54:26 +0530204 int usb_current;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100205 int usb_current_tmp;
Arun Murthy84edbee2012-02-29 21:54:26 +0530206 enum ab8500_usb_state state;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100207 enum ab8500_usb_state state_tmp;
Arun Murthy84edbee2012-02-29 21:54:26 +0530208 spinlock_t usb_lock;
209};
210
Lee Jonesf7470b52013-02-14 12:37:29 +0000211struct ab8500_charger_max_usb_in_curr {
212 int usb_type_max;
213 int set_max;
214 int calculated_max;
215};
216
Arun Murthy84edbee2012-02-29 21:54:26 +0530217/**
218 * struct ab8500_charger - ab8500 Charger device information
219 * @dev: Pointer to the structure device
Arun Murthy84edbee2012-02-29 21:54:26 +0530220 * @vbus_detected: VBUS detected
221 * @vbus_detected_start:
222 * VBUS detected during startup
223 * @ac_conn: This will be true when the AC charger has been plugged
224 * @vddadc_en_ac: Indicate if VDD ADC supply is enabled because AC
225 * charger is enabled
226 * @vddadc_en_usb: Indicate if VDD ADC supply is enabled because USB
227 * charger is enabled
228 * @vbat Battery voltage
229 * @old_vbat Previously measured battery voltage
Marcus Cooper4b45f4a2013-01-11 13:13:04 +0000230 * @usb_device_is_unrecognised USB device is unrecognised by the hardware
Arun Murthy84edbee2012-02-29 21:54:26 +0530231 * @autopower Indicate if we should have automatic pwron after pwrloss
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -0800232 * @autopower_cfg platform specific power config support for "pwron after pwrloss"
Henrik Sölverff380902012-04-17 15:51:01 +0200233 * @invalid_charger_detect_state State when forcing AB to use invalid charger
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100234 * @is_aca_rid: Incicate if accessory is ACA type
235 * @current_stepping_sessions:
236 * Counter for current stepping sessions
Arun Murthy84edbee2012-02-29 21:54:26 +0530237 * @parent: Pointer to the struct ab8500
238 * @gpadc: Pointer to the struct gpadc
Lee Jonesb0284de2012-11-30 10:09:42 +0000239 * @bm: Platform specific battery management information
Arun Murthy84edbee2012-02-29 21:54:26 +0530240 * @flags: Structure for information about events triggered
241 * @usb_state: Structure for usb stack information
Lee Jonesf7470b52013-02-14 12:37:29 +0000242 * @max_usb_in_curr: Max USB charger input current
Arun Murthy84edbee2012-02-29 21:54:26 +0530243 * @ac_chg: AC charger power supply
244 * @usb_chg: USB charger power supply
245 * @ac: Structure that holds the AC charger properties
246 * @usb: Structure that holds the USB charger properties
247 * @regu: Pointer to the struct regulator
248 * @charger_wq: Work queue for the IRQs and checking HW state
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100249 * @usb_ipt_crnt_lock: Lock to protect VBUS input current setting from mutuals
250 * @pm_lock: Lock to prevent system to suspend
Arun Murthy84edbee2012-02-29 21:54:26 +0530251 * @check_vbat_work Work for checking vbat threshold to adjust vbus current
252 * @check_hw_failure_work: Work for checking HW state
253 * @check_usbchgnotok_work: Work for checking USB charger not ok status
254 * @kick_wd_work: Work for kicking the charger watchdog in case
255 * of ABB rev 1.* due to the watchog logic bug
Lee Jonesb269fff2013-01-11 13:12:51 +0000256 * @ac_charger_attached_work: Work for checking if AC charger is still
257 * connected
258 * @usb_charger_attached_work: Work for checking if USB charger is still
259 * connected
Arun Murthy84edbee2012-02-29 21:54:26 +0530260 * @ac_work: Work for checking AC charger connection
261 * @detect_usb_type_work: Work for detecting the USB type connected
262 * @usb_link_status_work: Work for checking the new USB link status
263 * @usb_state_changed_work: Work for checking USB state
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100264 * @attach_work: Work for detecting USB type
265 * @vbus_drop_end_work: Work for detecting VBUS drop end
Arun Murthy84edbee2012-02-29 21:54:26 +0530266 * @check_main_thermal_prot_work:
267 * Work for checking Main thermal status
268 * @check_usb_thermal_prot_work:
269 * Work for checking USB thermal status
Lee Jonesb269fff2013-01-11 13:12:51 +0000270 * @charger_attached_mutex: For controlling the wakelock
Arun Murthy84edbee2012-02-29 21:54:26 +0530271 */
272struct ab8500_charger {
273 struct device *dev;
Arun Murthy84edbee2012-02-29 21:54:26 +0530274 bool vbus_detected;
275 bool vbus_detected_start;
276 bool ac_conn;
277 bool vddadc_en_ac;
278 bool vddadc_en_usb;
279 int vbat;
280 int old_vbat;
Marcus Cooper4b45f4a2013-01-11 13:13:04 +0000281 bool usb_device_is_unrecognised;
Arun Murthy84edbee2012-02-29 21:54:26 +0530282 bool autopower;
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -0800283 bool autopower_cfg;
Henrik Sölverff380902012-04-17 15:51:01 +0200284 int invalid_charger_detect_state;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100285 int is_aca_rid;
286 atomic_t current_stepping_sessions;
Arun Murthy84edbee2012-02-29 21:54:26 +0530287 struct ab8500 *parent;
288 struct ab8500_gpadc *gpadc;
Lee Jonesb0284de2012-11-30 10:09:42 +0000289 struct abx500_bm_data *bm;
Arun Murthy84edbee2012-02-29 21:54:26 +0530290 struct ab8500_charger_event_flags flags;
291 struct ab8500_charger_usb_state usb_state;
Lee Jonesf7470b52013-02-14 12:37:29 +0000292 struct ab8500_charger_max_usb_in_curr max_usb_in_curr;
Arun Murthy84edbee2012-02-29 21:54:26 +0530293 struct ux500_charger ac_chg;
294 struct ux500_charger usb_chg;
295 struct ab8500_charger_info ac;
296 struct ab8500_charger_info usb;
297 struct regulator *regu;
298 struct workqueue_struct *charger_wq;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100299 struct mutex usb_ipt_crnt_lock;
Arun Murthy84edbee2012-02-29 21:54:26 +0530300 struct delayed_work check_vbat_work;
301 struct delayed_work check_hw_failure_work;
302 struct delayed_work check_usbchgnotok_work;
303 struct delayed_work kick_wd_work;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100304 struct delayed_work usb_state_changed_work;
Marcus Cooper4b45f4a2013-01-11 13:13:04 +0000305 struct delayed_work attach_work;
Lee Jonesb269fff2013-01-11 13:12:51 +0000306 struct delayed_work ac_charger_attached_work;
307 struct delayed_work usb_charger_attached_work;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100308 struct delayed_work vbus_drop_end_work;
Arun Murthy84edbee2012-02-29 21:54:26 +0530309 struct work_struct ac_work;
310 struct work_struct detect_usb_type_work;
311 struct work_struct usb_link_status_work;
Arun Murthy84edbee2012-02-29 21:54:26 +0530312 struct work_struct check_main_thermal_prot_work;
313 struct work_struct check_usb_thermal_prot_work;
Anton Vorontsovefd71c82012-03-14 04:22:17 +0400314 struct usb_phy *usb_phy;
Arun Murthy84edbee2012-02-29 21:54:26 +0530315 struct notifier_block nb;
Lee Jonesb269fff2013-01-11 13:12:51 +0000316 struct mutex charger_attached_mutex;
Arun Murthy84edbee2012-02-29 21:54:26 +0530317};
318
319/* AC properties */
320static enum power_supply_property ab8500_charger_ac_props[] = {
321 POWER_SUPPLY_PROP_HEALTH,
322 POWER_SUPPLY_PROP_PRESENT,
323 POWER_SUPPLY_PROP_ONLINE,
324 POWER_SUPPLY_PROP_VOLTAGE_NOW,
325 POWER_SUPPLY_PROP_VOLTAGE_AVG,
326 POWER_SUPPLY_PROP_CURRENT_NOW,
327};
328
329/* USB properties */
330static enum power_supply_property ab8500_charger_usb_props[] = {
331 POWER_SUPPLY_PROP_HEALTH,
332 POWER_SUPPLY_PROP_CURRENT_AVG,
333 POWER_SUPPLY_PROP_PRESENT,
334 POWER_SUPPLY_PROP_ONLINE,
335 POWER_SUPPLY_PROP_VOLTAGE_NOW,
336 POWER_SUPPLY_PROP_VOLTAGE_AVG,
337 POWER_SUPPLY_PROP_CURRENT_NOW,
338};
339
Paer-Olof Haakanssondefbfa92013-01-11 13:12:49 +0000340/*
341 * Function for enabling and disabling sw fallback mode
342 * should always be disabled when no charger is connected.
Arun Murthy84edbee2012-02-29 21:54:26 +0530343 */
Paer-Olof Haakanssondefbfa92013-01-11 13:12:49 +0000344static void ab8500_enable_disable_sw_fallback(struct ab8500_charger *di,
345 bool fallback)
Arun Murthy84edbee2012-02-29 21:54:26 +0530346{
Nicolas Guionb0163222012-05-23 15:59:43 +0200347 u8 val;
Arun Murthy84edbee2012-02-29 21:54:26 +0530348 u8 reg;
Nicolas Guionb0163222012-05-23 15:59:43 +0200349 u8 bank;
350 u8 bit;
Arun Murthy84edbee2012-02-29 21:54:26 +0530351 int ret;
352
Paer-Olof Haakanssondefbfa92013-01-11 13:12:49 +0000353 dev_dbg(di->dev, "SW Fallback: %d\n", fallback);
Arun Murthy84edbee2012-02-29 21:54:26 +0530354
Nicolas Guionb0163222012-05-23 15:59:43 +0200355 if (is_ab8500(di->parent)) {
356 bank = 0x15;
357 reg = 0x0;
358 bit = 3;
359 } else {
360 bank = AB8500_SYS_CTRL1_BLOCK;
361 reg = AB8500_SW_CONTROL_FALLBACK;
362 bit = 0;
363 }
364
Paer-Olof Haakanssondefbfa92013-01-11 13:12:49 +0000365 /* read the register containing fallback bit */
Nicolas Guionb0163222012-05-23 15:59:43 +0200366 ret = abx500_get_register_interruptible(di->dev, bank, reg, &val);
367 if (ret < 0) {
368 dev_err(di->dev, "%d read failed\n", __LINE__);
Arun Murthy84edbee2012-02-29 21:54:26 +0530369 return;
370 }
371
Nicolas Guionb0163222012-05-23 15:59:43 +0200372 if (is_ab8500(di->parent)) {
373 /* enable the OPT emulation registers */
374 ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x2);
375 if (ret) {
376 dev_err(di->dev, "%d write failed\n", __LINE__);
377 goto disable_otp;
378 }
Arun Murthy84edbee2012-02-29 21:54:26 +0530379 }
380
Paer-Olof Haakanssondefbfa92013-01-11 13:12:49 +0000381 if (fallback)
Nicolas Guionb0163222012-05-23 15:59:43 +0200382 val |= (1 << bit);
Arun Murthy84edbee2012-02-29 21:54:26 +0530383 else
Nicolas Guionb0163222012-05-23 15:59:43 +0200384 val &= ~(1 << bit);
Arun Murthy84edbee2012-02-29 21:54:26 +0530385
Paer-Olof Haakanssondefbfa92013-01-11 13:12:49 +0000386 /* write back the changed fallback bit value to register */
Nicolas Guionb0163222012-05-23 15:59:43 +0200387 ret = abx500_set_register_interruptible(di->dev, bank, reg, val);
Arun Murthy84edbee2012-02-29 21:54:26 +0530388 if (ret) {
389 dev_err(di->dev, "%d write failed\n", __LINE__);
Arun Murthy84edbee2012-02-29 21:54:26 +0530390 }
391
Nicolas Guionb0163222012-05-23 15:59:43 +0200392disable_otp:
393 if (is_ab8500(di->parent)) {
394 /* disable the set OTP registers again */
395 ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x0);
396 if (ret) {
397 dev_err(di->dev, "%d write failed\n", __LINE__);
398 }
Arun Murthy84edbee2012-02-29 21:54:26 +0530399 }
400}
401
402/**
403 * ab8500_power_supply_changed - a wrapper with local extentions for
404 * power_supply_changed
405 * @di: pointer to the ab8500_charger structure
406 * @psy: pointer to power_supply_that have changed.
407 *
408 */
409static void ab8500_power_supply_changed(struct ab8500_charger *di,
410 struct power_supply *psy)
411{
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -0800412 if (di->autopower_cfg) {
Arun Murthy84edbee2012-02-29 21:54:26 +0530413 if (!di->usb.charger_connected &&
414 !di->ac.charger_connected &&
415 di->autopower) {
416 di->autopower = false;
Paer-Olof Haakanssondefbfa92013-01-11 13:12:49 +0000417 ab8500_enable_disable_sw_fallback(di, false);
Arun Murthy84edbee2012-02-29 21:54:26 +0530418 } else if (!di->autopower &&
419 (di->ac.charger_connected ||
420 di->usb.charger_connected)) {
421 di->autopower = true;
Paer-Olof Haakanssondefbfa92013-01-11 13:12:49 +0000422 ab8500_enable_disable_sw_fallback(di, true);
Arun Murthy84edbee2012-02-29 21:54:26 +0530423 }
424 }
425 power_supply_changed(psy);
426}
427
428static void ab8500_charger_set_usb_connected(struct ab8500_charger *di,
429 bool connected)
430{
431 if (connected != di->usb.charger_connected) {
432 dev_dbg(di->dev, "USB connected:%i\n", connected);
433 di->usb.charger_connected = connected;
Lee Jonesf7470b52013-02-14 12:37:29 +0000434
435 if (!connected)
436 di->flags.vbus_drop_end = false;
437
Arun Murthy84edbee2012-02-29 21:54:26 +0530438 sysfs_notify(&di->usb_chg.psy.dev->kobj, NULL, "present");
Lee Jonesb269fff2013-01-11 13:12:51 +0000439
440 if (connected) {
441 mutex_lock(&di->charger_attached_mutex);
442 mutex_unlock(&di->charger_attached_mutex);
443
Rabin Vincent261c5132012-09-26 13:36:38 +0200444 if (is_ab8500(di->parent))
445 queue_delayed_work(di->charger_wq,
Lee Jonesb269fff2013-01-11 13:12:51 +0000446 &di->usb_charger_attached_work,
447 HZ);
448 } else {
449 cancel_delayed_work_sync(&di->usb_charger_attached_work);
450 mutex_lock(&di->charger_attached_mutex);
451 mutex_unlock(&di->charger_attached_mutex);
452 }
Arun Murthy84edbee2012-02-29 21:54:26 +0530453 }
454}
455
456/**
457 * ab8500_charger_get_ac_voltage() - get ac charger voltage
458 * @di: pointer to the ab8500_charger structure
459 *
460 * Returns ac charger voltage (on success)
461 */
462static int ab8500_charger_get_ac_voltage(struct ab8500_charger *di)
463{
464 int vch;
465
466 /* Only measure voltage if the charger is connected */
467 if (di->ac.charger_connected) {
468 vch = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_V);
469 if (vch < 0)
470 dev_err(di->dev, "%s gpadc conv failed,\n", __func__);
471 } else {
472 vch = 0;
473 }
474 return vch;
475}
476
477/**
478 * ab8500_charger_ac_cv() - check if the main charger is in CV mode
479 * @di: pointer to the ab8500_charger structure
480 *
481 * Returns ac charger CV mode (on success) else error code
482 */
483static int ab8500_charger_ac_cv(struct ab8500_charger *di)
484{
485 u8 val;
486 int ret = 0;
487
488 /* Only check CV mode if the charger is online */
489 if (di->ac.charger_online) {
490 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
491 AB8500_CH_STATUS1_REG, &val);
492 if (ret < 0) {
493 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
494 return 0;
495 }
496
497 if (val & MAIN_CH_CV_ON)
498 ret = 1;
499 else
500 ret = 0;
501 }
502
503 return ret;
504}
505
506/**
507 * ab8500_charger_get_vbus_voltage() - get vbus voltage
508 * @di: pointer to the ab8500_charger structure
509 *
510 * This function returns the vbus voltage.
511 * Returns vbus voltage (on success)
512 */
513static int ab8500_charger_get_vbus_voltage(struct ab8500_charger *di)
514{
515 int vch;
516
517 /* Only measure voltage if the charger is connected */
518 if (di->usb.charger_connected) {
519 vch = ab8500_gpadc_convert(di->gpadc, VBUS_V);
520 if (vch < 0)
521 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
522 } else {
523 vch = 0;
524 }
525 return vch;
526}
527
528/**
529 * ab8500_charger_get_usb_current() - get usb charger current
530 * @di: pointer to the ab8500_charger structure
531 *
532 * This function returns the usb charger current.
533 * Returns usb current (on success) and error code on failure
534 */
535static int ab8500_charger_get_usb_current(struct ab8500_charger *di)
536{
537 int ich;
538
539 /* Only measure current if the charger is online */
540 if (di->usb.charger_online) {
541 ich = ab8500_gpadc_convert(di->gpadc, USB_CHARGER_C);
542 if (ich < 0)
543 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
544 } else {
545 ich = 0;
546 }
547 return ich;
548}
549
550/**
551 * ab8500_charger_get_ac_current() - get ac charger current
552 * @di: pointer to the ab8500_charger structure
553 *
554 * This function returns the ac charger current.
555 * Returns ac current (on success) and error code on failure.
556 */
557static int ab8500_charger_get_ac_current(struct ab8500_charger *di)
558{
559 int ich;
560
561 /* Only measure current if the charger is online */
562 if (di->ac.charger_online) {
563 ich = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_C);
564 if (ich < 0)
565 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
566 } else {
567 ich = 0;
568 }
569 return ich;
570}
571
572/**
573 * ab8500_charger_usb_cv() - check if the usb charger is in CV mode
574 * @di: pointer to the ab8500_charger structure
575 *
576 * Returns ac charger CV mode (on success) else error code
577 */
578static int ab8500_charger_usb_cv(struct ab8500_charger *di)
579{
580 int ret;
581 u8 val;
582
583 /* Only check CV mode if the charger is online */
584 if (di->usb.charger_online) {
585 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
586 AB8500_CH_USBCH_STAT1_REG, &val);
587 if (ret < 0) {
588 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
589 return 0;
590 }
591
592 if (val & USB_CH_CV_ON)
593 ret = 1;
594 else
595 ret = 0;
596 } else {
597 ret = 0;
598 }
599
600 return ret;
601}
602
603/**
604 * ab8500_charger_detect_chargers() - Detect the connected chargers
605 * @di: pointer to the ab8500_charger structure
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100606 * @probe: if probe, don't delay and wait for HW
Arun Murthy84edbee2012-02-29 21:54:26 +0530607 *
608 * Returns the type of charger connected.
609 * For USB it will not mean we can actually charge from it
610 * but that there is a USB cable connected that we have to
611 * identify. This is used during startup when we don't get
612 * interrupts of the charger detection
613 *
614 * Returns an integer value, that means,
615 * NO_PW_CONN no power supply is connected
616 * AC_PW_CONN if the AC power supply is connected
617 * USB_PW_CONN if the USB power supply is connected
618 * AC_PW_CONN + USB_PW_CONN if USB and AC power supplies are both connected
619 */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100620static int ab8500_charger_detect_chargers(struct ab8500_charger *di, bool probe)
Arun Murthy84edbee2012-02-29 21:54:26 +0530621{
622 int result = NO_PW_CONN;
623 int ret;
624 u8 val;
625
626 /* Check for AC charger */
627 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
628 AB8500_CH_STATUS1_REG, &val);
629 if (ret < 0) {
630 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
631 return ret;
632 }
633
634 if (val & MAIN_CH_DET)
635 result = AC_PW_CONN;
636
637 /* Check for USB charger */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100638
639 if (!probe) {
640 /*
641 * AB8500 says VBUS_DET_DBNC1 & VBUS_DET_DBNC100
642 * when disconnecting ACA even though no
643 * charger was connected. Try waiting a little
644 * longer than the 100 ms of VBUS_DET_DBNC100...
645 */
646 msleep(110);
647 }
Arun Murthy84edbee2012-02-29 21:54:26 +0530648 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
649 AB8500_CH_USBCH_STAT1_REG, &val);
650 if (ret < 0) {
651 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
652 return ret;
653 }
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100654 dev_dbg(di->dev,
655 "%s AB8500_CH_USBCH_STAT1_REG %x\n", __func__,
656 val);
Arun Murthy84edbee2012-02-29 21:54:26 +0530657 if ((val & VBUS_DET_DBNC1) && (val & VBUS_DET_DBNC100))
658 result |= USB_PW_CONN;
659
660 return result;
661}
662
663/**
664 * ab8500_charger_max_usb_curr() - get the max curr for the USB type
665 * @di: pointer to the ab8500_charger structure
666 * @link_status: the identified USB type
667 *
668 * Get the maximum current that is allowed to be drawn from the host
669 * based on the USB type.
670 * Returns error code in case of failure else 0 on success
671 */
672static int ab8500_charger_max_usb_curr(struct ab8500_charger *di,
Hakan Berg74a8e342013-01-11 13:12:58 +0000673 enum ab8500_charger_link_status link_status)
Arun Murthy84edbee2012-02-29 21:54:26 +0530674{
675 int ret = 0;
676
Marcus Cooper4b45f4a2013-01-11 13:13:04 +0000677 di->usb_device_is_unrecognised = false;
678
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100679 /*
680 * Platform only supports USB 2.0.
681 * This means that charging current from USB source
682 * is maximum 500 mA. Every occurence of USB_STAT_*_HOST_*
683 * should set USB_CH_IP_CUR_LVL_0P5.
684 */
685
Arun Murthy84edbee2012-02-29 21:54:26 +0530686 switch (link_status) {
687 case USB_STAT_STD_HOST_NC:
688 case USB_STAT_STD_HOST_C_NS:
689 case USB_STAT_STD_HOST_C_S:
690 dev_dbg(di->dev, "USB Type - Standard host is "
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100691 "detected through USB driver\n");
Lee Jonesf7470b52013-02-14 12:37:29 +0000692 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100693 di->is_aca_rid = 0;
Arun Murthy84edbee2012-02-29 21:54:26 +0530694 break;
695 case USB_STAT_HOST_CHG_HS_CHIRP:
Lee Jonesf7470b52013-02-14 12:37:29 +0000696 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100697 di->is_aca_rid = 0;
Arun Murthy84edbee2012-02-29 21:54:26 +0530698 break;
699 case USB_STAT_HOST_CHG_HS:
Lee Jonesf7470b52013-02-14 12:37:29 +0000700 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100701 di->is_aca_rid = 0;
702 break;
Arun Murthy84edbee2012-02-29 21:54:26 +0530703 case USB_STAT_ACA_RID_C_HS:
Lee Jonesf7470b52013-02-14 12:37:29 +0000704 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P9;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100705 di->is_aca_rid = 0;
Arun Murthy84edbee2012-02-29 21:54:26 +0530706 break;
707 case USB_STAT_ACA_RID_A:
708 /*
709 * Dedicated charger level minus maximum current accessory
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100710 * can consume (900mA). Closest level is 500mA
Arun Murthy84edbee2012-02-29 21:54:26 +0530711 */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100712 dev_dbg(di->dev, "USB_STAT_ACA_RID_A detected\n");
Lee Jonesf7470b52013-02-14 12:37:29 +0000713 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100714 di->is_aca_rid = 1;
Arun Murthy84edbee2012-02-29 21:54:26 +0530715 break;
716 case USB_STAT_ACA_RID_B:
717 /*
718 * Dedicated charger level minus 120mA (20mA for ACA and
719 * 100mA for potential accessory). Closest level is 1300mA
720 */
Lee Jonesf7470b52013-02-14 12:37:29 +0000721 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_1P3;
Hakan Berg74a8e342013-01-11 13:12:58 +0000722 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
Lee Jonesf7470b52013-02-14 12:37:29 +0000723 di->max_usb_in_curr.usb_type_max);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100724 di->is_aca_rid = 1;
Arun Murthy84edbee2012-02-29 21:54:26 +0530725 break;
Arun Murthy84edbee2012-02-29 21:54:26 +0530726 case USB_STAT_HOST_CHG_NM:
Lee Jonesf7470b52013-02-14 12:37:29 +0000727 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100728 di->is_aca_rid = 0;
729 break;
Hakan Berg74a8e342013-01-11 13:12:58 +0000730 case USB_STAT_DEDICATED_CHG:
Lee Jonesf7470b52013-02-14 12:37:29 +0000731 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_1P5;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100732 di->is_aca_rid = 0;
733 break;
734 case USB_STAT_ACA_RID_C_HS_CHIRP:
735 case USB_STAT_ACA_RID_C_NM:
Lee Jonesf7470b52013-02-14 12:37:29 +0000736 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_1P5;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100737 di->is_aca_rid = 1;
Arun Murthy84edbee2012-02-29 21:54:26 +0530738 break;
Marcus Cooper4b45f4a2013-01-11 13:13:04 +0000739 case USB_STAT_NOT_CONFIGURED:
740 if (di->vbus_detected) {
741 di->usb_device_is_unrecognised = true;
742 dev_dbg(di->dev, "USB Type - Legacy charger.\n");
Lee Jonesf7470b52013-02-14 12:37:29 +0000743 di->max_usb_in_curr.usb_type_max =
744 USB_CH_IP_CUR_LVL_1P5;
Marcus Cooper4b45f4a2013-01-11 13:13:04 +0000745 break;
746 }
Arun Murthy84edbee2012-02-29 21:54:26 +0530747 case USB_STAT_HM_IDGND:
Arun Murthy84edbee2012-02-29 21:54:26 +0530748 dev_err(di->dev, "USB Type - Charging not allowed\n");
Lee Jonesf7470b52013-02-14 12:37:29 +0000749 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P05;
Arun Murthy84edbee2012-02-29 21:54:26 +0530750 ret = -ENXIO;
751 break;
Hakan Berg74a8e342013-01-11 13:12:58 +0000752 case USB_STAT_RESERVED:
753 if (is_ab8500(di->parent)) {
754 di->flags.vbus_collapse = true;
755 dev_err(di->dev, "USB Type - USB_STAT_RESERVED "
756 "VBUS has collapsed\n");
757 ret = -ENXIO;
758 break;
Lee Jones861a30d2012-08-29 20:36:51 +0800759 } else {
Hakan Berg74a8e342013-01-11 13:12:58 +0000760 dev_dbg(di->dev, "USB Type - Charging not allowed\n");
Lee Jonesf7470b52013-02-14 12:37:29 +0000761 di->max_usb_in_curr.usb_type_max =
762 USB_CH_IP_CUR_LVL_0P05;
Hakan Berg74a8e342013-01-11 13:12:58 +0000763 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
Lee Jonesf7470b52013-02-14 12:37:29 +0000764 link_status,
765 di->max_usb_in_curr.usb_type_max);
Hakan Berg74a8e342013-01-11 13:12:58 +0000766 ret = -ENXIO;
767 break;
768 }
769 break;
770 case USB_STAT_CARKIT_1:
771 case USB_STAT_CARKIT_2:
772 case USB_STAT_ACA_DOCK_CHARGER:
773 case USB_STAT_CHARGER_LINE_1:
Lee Jonesf7470b52013-02-14 12:37:29 +0000774 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
Hakan Berg74a8e342013-01-11 13:12:58 +0000775 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
Lee Jonesf7470b52013-02-14 12:37:29 +0000776 di->max_usb_in_curr.usb_type_max);
Dan Carpentered5243f2013-08-27 04:19:42 +0300777 break;
Henrik Sölverff380902012-04-17 15:51:01 +0200778 case USB_STAT_NOT_VALID_LINK:
779 dev_err(di->dev, "USB Type invalid - try charging anyway\n");
Lee Jonesf7470b52013-02-14 12:37:29 +0000780 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
Hakan Berg74a8e342013-01-11 13:12:58 +0000781 break;
782
Arun Murthy84edbee2012-02-29 21:54:26 +0530783 default:
784 dev_err(di->dev, "USB Type - Unknown\n");
Lee Jonesf7470b52013-02-14 12:37:29 +0000785 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P05;
Arun Murthy84edbee2012-02-29 21:54:26 +0530786 ret = -ENXIO;
787 break;
788 };
789
Lee Jonesf7470b52013-02-14 12:37:29 +0000790 di->max_usb_in_curr.set_max = di->max_usb_in_curr.usb_type_max;
Arun Murthy84edbee2012-02-29 21:54:26 +0530791 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
Lee Jonesf7470b52013-02-14 12:37:29 +0000792 link_status, di->max_usb_in_curr.set_max);
Arun Murthy84edbee2012-02-29 21:54:26 +0530793
794 return ret;
795}
796
797/**
798 * ab8500_charger_read_usb_type() - read the type of usb connected
799 * @di: pointer to the ab8500_charger structure
800 *
801 * Detect the type of the plugged USB
802 * Returns error code in case of failure else 0 on success
803 */
804static int ab8500_charger_read_usb_type(struct ab8500_charger *di)
805{
806 int ret;
807 u8 val;
808
809 ret = abx500_get_register_interruptible(di->dev,
810 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG, &val);
811 if (ret < 0) {
812 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
813 return ret;
814 }
Lee Jones861a30d2012-08-29 20:36:51 +0800815 if (is_ab8500(di->parent))
Hakan Berg74a8e342013-01-11 13:12:58 +0000816 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
Lee Jones861a30d2012-08-29 20:36:51 +0800817 AB8500_USB_LINE_STAT_REG, &val);
818 else
819 ret = abx500_get_register_interruptible(di->dev,
820 AB8500_USB, AB8500_USB_LINK1_STAT_REG, &val);
Arun Murthy84edbee2012-02-29 21:54:26 +0530821 if (ret < 0) {
822 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
823 return ret;
824 }
825
826 /* get the USB type */
Lee Jones861a30d2012-08-29 20:36:51 +0800827 if (is_ab8500(di->parent))
828 val = (val & AB8500_USB_LINK_STATUS) >> USB_LINK_STATUS_SHIFT;
829 else
830 val = (val & AB8505_USB_LINK_STATUS) >> USB_LINK_STATUS_SHIFT;
Arun Murthy84edbee2012-02-29 21:54:26 +0530831 ret = ab8500_charger_max_usb_curr(di,
832 (enum ab8500_charger_link_status) val);
833
834 return ret;
835}
836
837/**
838 * ab8500_charger_detect_usb_type() - get the type of usb connected
839 * @di: pointer to the ab8500_charger structure
840 *
841 * Detect the type of the plugged USB
842 * Returns error code in case of failure else 0 on success
843 */
844static int ab8500_charger_detect_usb_type(struct ab8500_charger *di)
845{
846 int i, ret;
847 u8 val;
848
849 /*
850 * On getting the VBUS rising edge detect interrupt there
851 * is a 250ms delay after which the register UsbLineStatus
852 * is filled with valid data.
853 */
854 for (i = 0; i < 10; i++) {
855 msleep(250);
856 ret = abx500_get_register_interruptible(di->dev,
857 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG,
858 &val);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100859 dev_dbg(di->dev, "%s AB8500_IT_SOURCE21_REG %x\n",
860 __func__, val);
Arun Murthy84edbee2012-02-29 21:54:26 +0530861 if (ret < 0) {
862 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
863 return ret;
864 }
Hakan Berg74a8e342013-01-11 13:12:58 +0000865
Lee Jones861a30d2012-08-29 20:36:51 +0800866 if (is_ab8500(di->parent))
Hakan Berg74a8e342013-01-11 13:12:58 +0000867 ret = abx500_get_register_interruptible(di->dev,
868 AB8500_USB, AB8500_USB_LINE_STAT_REG, &val);
Lee Jones861a30d2012-08-29 20:36:51 +0800869 else
Hakan Berg74a8e342013-01-11 13:12:58 +0000870 ret = abx500_get_register_interruptible(di->dev,
871 AB8500_USB, AB8500_USB_LINK1_STAT_REG, &val);
Arun Murthy84edbee2012-02-29 21:54:26 +0530872 if (ret < 0) {
873 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
874 return ret;
875 }
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100876 dev_dbg(di->dev, "%s AB8500_USB_LINE_STAT_REG %x\n", __func__,
877 val);
Arun Murthy84edbee2012-02-29 21:54:26 +0530878 /*
879 * Until the IT source register is read the UsbLineStatus
880 * register is not updated, hence doing the same
881 * Revisit this:
882 */
883
884 /* get the USB type */
Lee Jones861a30d2012-08-29 20:36:51 +0800885 if (is_ab8500(di->parent))
886 val = (val & AB8500_USB_LINK_STATUS) >>
887 USB_LINK_STATUS_SHIFT;
888 else
889 val = (val & AB8505_USB_LINK_STATUS) >>
890 USB_LINK_STATUS_SHIFT;
Arun Murthy84edbee2012-02-29 21:54:26 +0530891 if (val)
892 break;
893 }
894 ret = ab8500_charger_max_usb_curr(di,
895 (enum ab8500_charger_link_status) val);
896
897 return ret;
898}
899
900/*
901 * This array maps the raw hex value to charger voltage used by the AB8500
902 * Values taken from the UM0836
903 */
904static int ab8500_charger_voltage_map[] = {
905 3500 ,
906 3525 ,
907 3550 ,
908 3575 ,
909 3600 ,
910 3625 ,
911 3650 ,
912 3675 ,
913 3700 ,
914 3725 ,
915 3750 ,
916 3775 ,
917 3800 ,
918 3825 ,
919 3850 ,
920 3875 ,
921 3900 ,
922 3925 ,
923 3950 ,
924 3975 ,
925 4000 ,
926 4025 ,
927 4050 ,
928 4060 ,
929 4070 ,
930 4080 ,
931 4090 ,
932 4100 ,
933 4110 ,
934 4120 ,
935 4130 ,
936 4140 ,
937 4150 ,
938 4160 ,
939 4170 ,
940 4180 ,
941 4190 ,
942 4200 ,
943 4210 ,
944 4220 ,
945 4230 ,
946 4240 ,
947 4250 ,
948 4260 ,
949 4270 ,
950 4280 ,
951 4290 ,
952 4300 ,
953 4310 ,
954 4320 ,
955 4330 ,
956 4340 ,
957 4350 ,
958 4360 ,
959 4370 ,
960 4380 ,
961 4390 ,
962 4400 ,
963 4410 ,
964 4420 ,
965 4430 ,
966 4440 ,
967 4450 ,
968 4460 ,
969 4470 ,
970 4480 ,
971 4490 ,
972 4500 ,
973 4510 ,
974 4520 ,
975 4530 ,
976 4540 ,
977 4550 ,
978 4560 ,
979 4570 ,
980 4580 ,
981 4590 ,
982 4600 ,
983};
984
Arun Murthy84edbee2012-02-29 21:54:26 +0530985static int ab8500_voltage_to_regval(int voltage)
986{
987 int i;
988
989 /* Special case for voltage below 3.5V */
990 if (voltage < ab8500_charger_voltage_map[0])
991 return LOW_VOLT_REG;
992
993 for (i = 1; i < ARRAY_SIZE(ab8500_charger_voltage_map); i++) {
994 if (voltage < ab8500_charger_voltage_map[i])
995 return i - 1;
996 }
997
998 /* If not last element, return error */
999 i = ARRAY_SIZE(ab8500_charger_voltage_map) - 1;
1000 if (voltage == ab8500_charger_voltage_map[i])
1001 return i;
1002 else
1003 return -1;
1004}
1005
Lee Jones861a30d2012-08-29 20:36:51 +08001006static int ab8500_current_to_regval(struct ab8500_charger *di, int curr)
Arun Murthy84edbee2012-02-29 21:54:26 +05301007{
1008 int i;
1009
Lee Jones861a30d2012-08-29 20:36:51 +08001010 if (curr < di->bm->chg_output_curr[0])
Arun Murthy84edbee2012-02-29 21:54:26 +05301011 return 0;
1012
Lee Jones861a30d2012-08-29 20:36:51 +08001013 for (i = 0; i < di->bm->n_chg_out_curr; i++) {
1014 if (curr < di->bm->chg_output_curr[i])
Arun Murthy84edbee2012-02-29 21:54:26 +05301015 return i - 1;
1016 }
1017
1018 /* If not last element, return error */
Lee Jones861a30d2012-08-29 20:36:51 +08001019 i = di->bm->n_chg_out_curr - 1;
1020 if (curr == di->bm->chg_output_curr[i])
Arun Murthy84edbee2012-02-29 21:54:26 +05301021 return i;
1022 else
1023 return -1;
1024}
1025
Lee Jones861a30d2012-08-29 20:36:51 +08001026static int ab8500_vbus_in_curr_to_regval(struct ab8500_charger *di, int curr)
Arun Murthy84edbee2012-02-29 21:54:26 +05301027{
1028 int i;
1029
Lee Jones861a30d2012-08-29 20:36:51 +08001030 if (curr < di->bm->chg_input_curr[0])
Arun Murthy84edbee2012-02-29 21:54:26 +05301031 return 0;
1032
Lee Jones861a30d2012-08-29 20:36:51 +08001033 for (i = 0; i < di->bm->n_chg_in_curr; i++) {
1034 if (curr < di->bm->chg_input_curr[i])
Arun Murthy84edbee2012-02-29 21:54:26 +05301035 return i - 1;
1036 }
1037
1038 /* If not last element, return error */
Lee Jones861a30d2012-08-29 20:36:51 +08001039 i = di->bm->n_chg_in_curr - 1;
1040 if (curr == di->bm->chg_input_curr[i])
Arun Murthy84edbee2012-02-29 21:54:26 +05301041 return i;
1042 else
1043 return -1;
1044}
1045
1046/**
1047 * ab8500_charger_get_usb_cur() - get usb current
1048 * @di: pointer to the ab8500_charger structre
1049 *
1050 * The usb stack provides the maximum current that can be drawn from
1051 * the standard usb host. This will be in mA.
1052 * This function converts current in mA to a value that can be written
1053 * to the register. Returns -1 if charging is not allowed
1054 */
1055static int ab8500_charger_get_usb_cur(struct ab8500_charger *di)
1056{
Lee Jonesf7470b52013-02-14 12:37:29 +00001057 int ret = 0;
Arun Murthy84edbee2012-02-29 21:54:26 +05301058 switch (di->usb_state.usb_current) {
1059 case 100:
Lee Jonesf7470b52013-02-14 12:37:29 +00001060 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P09;
Arun Murthy84edbee2012-02-29 21:54:26 +05301061 break;
1062 case 200:
Lee Jonesf7470b52013-02-14 12:37:29 +00001063 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P19;
Arun Murthy84edbee2012-02-29 21:54:26 +05301064 break;
1065 case 300:
Lee Jonesf7470b52013-02-14 12:37:29 +00001066 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P29;
Arun Murthy84edbee2012-02-29 21:54:26 +05301067 break;
1068 case 400:
Lee Jonesf7470b52013-02-14 12:37:29 +00001069 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P38;
Arun Murthy84edbee2012-02-29 21:54:26 +05301070 break;
1071 case 500:
Lee Jonesf7470b52013-02-14 12:37:29 +00001072 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
Arun Murthy84edbee2012-02-29 21:54:26 +05301073 break;
1074 default:
Lee Jonesf7470b52013-02-14 12:37:29 +00001075 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P05;
1076 ret = -EPERM;
Arun Murthy84edbee2012-02-29 21:54:26 +05301077 break;
1078 };
Lee Jonesf7470b52013-02-14 12:37:29 +00001079 di->max_usb_in_curr.set_max = di->max_usb_in_curr.usb_type_max;
1080 return ret;
1081}
1082
1083/**
1084 * ab8500_charger_check_continue_stepping() - Check to allow stepping
1085 * @di: pointer to the ab8500_charger structure
1086 * @reg: select what charger register to check
1087 *
1088 * Check if current stepping should be allowed to continue.
1089 * Checks if charger source has not collapsed. If it has, further stepping
1090 * is not allowed.
1091 */
1092static bool ab8500_charger_check_continue_stepping(struct ab8500_charger *di,
1093 int reg)
1094{
1095 if (reg == AB8500_USBCH_IPT_CRNTLVL_REG)
1096 return !di->flags.vbus_drop_end;
1097 else
1098 return true;
Arun Murthy84edbee2012-02-29 21:54:26 +05301099}
1100
1101/**
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001102 * ab8500_charger_set_current() - set charger current
1103 * @di: pointer to the ab8500_charger structure
1104 * @ich: charger current, in mA
1105 * @reg: select what charger register to set
1106 *
1107 * Set charger current.
1108 * There is no state machine in the AB to step up/down the charger
1109 * current to avoid dips and spikes on MAIN, VBUS and VBAT when
1110 * charging is started. Instead we need to implement
1111 * this charger current step-up/down here.
1112 * Returns error code in case of failure else 0(on success)
1113 */
1114static int ab8500_charger_set_current(struct ab8500_charger *di,
1115 int ich, int reg)
1116{
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001117 int ret = 0;
Lee Jones861a30d2012-08-29 20:36:51 +08001118 int curr_index, prev_curr_index, shift_value, i;
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001119 u8 reg_value;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001120 u32 step_udelay;
1121 bool no_stepping = false;
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001122
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001123 atomic_inc(&di->current_stepping_sessions);
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001124
1125 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1126 reg, &reg_value);
1127 if (ret < 0) {
1128 dev_err(di->dev, "%s read failed\n", __func__);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001129 goto exit_set_current;
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001130 }
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001131
1132 switch (reg) {
1133 case AB8500_MCH_IPT_CURLVL_REG:
1134 shift_value = MAIN_CH_INPUT_CURR_SHIFT;
1135 prev_curr_index = (reg_value >> shift_value);
Lee Jones861a30d2012-08-29 20:36:51 +08001136 curr_index = ab8500_current_to_regval(di, ich);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001137 step_udelay = STEP_UDELAY;
1138 if (!di->ac.charger_connected)
1139 no_stepping = true;
1140 break;
1141 case AB8500_USBCH_IPT_CRNTLVL_REG:
Lee Jones861a30d2012-08-29 20:36:51 +08001142 if (is_ab8540(di->parent))
1143 shift_value = AB8540_VBUS_IN_CURR_LIM_SHIFT;
1144 else
1145 shift_value = VBUS_IN_CURR_LIM_SHIFT;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001146 prev_curr_index = (reg_value >> shift_value);
Lee Jones861a30d2012-08-29 20:36:51 +08001147 curr_index = ab8500_vbus_in_curr_to_regval(di, ich);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001148 step_udelay = STEP_UDELAY * 100;
1149
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001150 if (!di->usb.charger_connected)
1151 no_stepping = true;
1152 break;
1153 case AB8500_CH_OPT_CRNTLVL_REG:
1154 shift_value = 0;
1155 prev_curr_index = (reg_value >> shift_value);
Lee Jones861a30d2012-08-29 20:36:51 +08001156 curr_index = ab8500_current_to_regval(di, ich);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001157 step_udelay = STEP_UDELAY;
1158 if (curr_index && (curr_index - prev_curr_index) > 1)
1159 step_udelay *= 100;
1160
1161 if (!di->usb.charger_connected && !di->ac.charger_connected)
1162 no_stepping = true;
1163
1164 break;
1165 default:
1166 dev_err(di->dev, "%s current register not valid\n", __func__);
1167 ret = -ENXIO;
1168 goto exit_set_current;
1169 }
1170
1171 if (curr_index < 0) {
1172 dev_err(di->dev, "requested current limit out-of-range\n");
1173 ret = -ENXIO;
1174 goto exit_set_current;
1175 }
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001176
1177 /* only update current if it's been changed */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001178 if (prev_curr_index == curr_index) {
1179 dev_dbg(di->dev, "%s current not changed for reg: 0x%02x\n",
1180 __func__, reg);
1181 ret = 0;
1182 goto exit_set_current;
1183 }
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001184
1185 dev_dbg(di->dev, "%s set charger current: %d mA for reg: 0x%02x\n",
1186 __func__, ich, reg);
1187
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001188 if (no_stepping) {
1189 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1190 reg, (u8)curr_index << shift_value);
1191 if (ret)
1192 dev_err(di->dev, "%s write failed\n", __func__);
1193 } else if (prev_curr_index > curr_index) {
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001194 for (i = prev_curr_index - 1; i >= curr_index; i--) {
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001195 dev_dbg(di->dev, "curr change_1 to: %x for 0x%02x\n",
1196 (u8) i << shift_value, reg);
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001197 ret = abx500_set_register_interruptible(di->dev,
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001198 AB8500_CHARGER, reg, (u8)i << shift_value);
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001199 if (ret) {
1200 dev_err(di->dev, "%s write failed\n", __func__);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001201 goto exit_set_current;
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001202 }
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001203 if (i != curr_index)
1204 usleep_range(step_udelay, step_udelay * 2);
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001205 }
1206 } else {
Lee Jonesf7470b52013-02-14 12:37:29 +00001207 bool allow = true;
1208 for (i = prev_curr_index + 1; i <= curr_index && allow; i++) {
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001209 dev_dbg(di->dev, "curr change_2 to: %x for 0x%02x\n",
1210 (u8)i << shift_value, reg);
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001211 ret = abx500_set_register_interruptible(di->dev,
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001212 AB8500_CHARGER, reg, (u8)i << shift_value);
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001213 if (ret) {
1214 dev_err(di->dev, "%s write failed\n", __func__);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001215 goto exit_set_current;
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001216 }
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001217 if (i != curr_index)
1218 usleep_range(step_udelay, step_udelay * 2);
Lee Jonesf7470b52013-02-14 12:37:29 +00001219
1220 allow = ab8500_charger_check_continue_stepping(di, reg);
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001221 }
1222 }
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001223
1224exit_set_current:
1225 atomic_dec(&di->current_stepping_sessions);
1226
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001227 return ret;
1228}
1229
1230/**
Arun Murthy84edbee2012-02-29 21:54:26 +05301231 * ab8500_charger_set_vbus_in_curr() - set VBUS input current limit
1232 * @di: pointer to the ab8500_charger structure
1233 * @ich_in: charger input current limit
1234 *
1235 * Sets the current that can be drawn from the USB host
1236 * Returns error code in case of failure else 0(on success)
1237 */
1238static int ab8500_charger_set_vbus_in_curr(struct ab8500_charger *di,
1239 int ich_in)
1240{
Arun Murthy84edbee2012-02-29 21:54:26 +05301241 int min_value;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001242 int ret;
Arun Murthy84edbee2012-02-29 21:54:26 +05301243
1244 /* We should always use to lowest current limit */
Lee Jonesb0284de2012-11-30 10:09:42 +00001245 min_value = min(di->bm->chg_params->usb_curr_max, ich_in);
Lee Jonesf7470b52013-02-14 12:37:29 +00001246 if (di->max_usb_in_curr.set_max > 0)
1247 min_value = min(di->max_usb_in_curr.set_max, min_value);
1248
1249 if (di->usb_state.usb_current >= 0)
1250 min_value = min(di->usb_state.usb_current, min_value);
Arun Murthy84edbee2012-02-29 21:54:26 +05301251
1252 switch (min_value) {
1253 case 100:
1254 if (di->vbat < VBAT_TRESH_IP_CUR_RED)
1255 min_value = USB_CH_IP_CUR_LVL_0P05;
1256 break;
1257 case 500:
1258 if (di->vbat < VBAT_TRESH_IP_CUR_RED)
1259 min_value = USB_CH_IP_CUR_LVL_0P45;
1260 break;
1261 default:
1262 break;
1263 }
1264
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001265 dev_info(di->dev, "VBUS input current limit set to %d mA\n", min_value);
1266
1267 mutex_lock(&di->usb_ipt_crnt_lock);
1268 ret = ab8500_charger_set_current(di, min_value,
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001269 AB8500_USBCH_IPT_CRNTLVL_REG);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001270 mutex_unlock(&di->usb_ipt_crnt_lock);
1271
1272 return ret;
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001273}
Arun Murthy84edbee2012-02-29 21:54:26 +05301274
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001275/**
1276 * ab8500_charger_set_main_in_curr() - set main charger input current
1277 * @di: pointer to the ab8500_charger structure
1278 * @ich_in: input charger current, in mA
1279 *
1280 * Set main charger input current.
1281 * Returns error code in case of failure else 0(on success)
1282 */
1283static int ab8500_charger_set_main_in_curr(struct ab8500_charger *di,
1284 int ich_in)
1285{
1286 return ab8500_charger_set_current(di, ich_in,
1287 AB8500_MCH_IPT_CURLVL_REG);
1288}
Arun Murthy84edbee2012-02-29 21:54:26 +05301289
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001290/**
1291 * ab8500_charger_set_output_curr() - set charger output current
1292 * @di: pointer to the ab8500_charger structure
1293 * @ich_out: output charger current, in mA
1294 *
1295 * Set charger output current.
1296 * Returns error code in case of failure else 0(on success)
1297 */
1298static int ab8500_charger_set_output_curr(struct ab8500_charger *di,
1299 int ich_out)
1300{
1301 return ab8500_charger_set_current(di, ich_out,
1302 AB8500_CH_OPT_CRNTLVL_REG);
Arun Murthy84edbee2012-02-29 21:54:26 +05301303}
1304
1305/**
1306 * ab8500_charger_led_en() - turn on/off chargign led
1307 * @di: pointer to the ab8500_charger structure
1308 * @on: flag to turn on/off the chargign led
1309 *
1310 * Power ON/OFF charging LED indication
1311 * Returns error code in case of failure else 0(on success)
1312 */
1313static int ab8500_charger_led_en(struct ab8500_charger *di, int on)
1314{
1315 int ret;
1316
1317 if (on) {
1318 /* Power ON charging LED indicator, set LED current to 5mA */
1319 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1320 AB8500_LED_INDICATOR_PWM_CTRL,
1321 (LED_IND_CUR_5MA | LED_INDICATOR_PWM_ENA));
1322 if (ret) {
1323 dev_err(di->dev, "Power ON LED failed\n");
1324 return ret;
1325 }
1326 /* LED indicator PWM duty cycle 252/256 */
1327 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1328 AB8500_LED_INDICATOR_PWM_DUTY,
1329 LED_INDICATOR_PWM_DUTY_252_256);
1330 if (ret) {
1331 dev_err(di->dev, "Set LED PWM duty cycle failed\n");
1332 return ret;
1333 }
1334 } else {
1335 /* Power off charging LED indicator */
1336 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1337 AB8500_LED_INDICATOR_PWM_CTRL,
1338 LED_INDICATOR_PWM_DIS);
1339 if (ret) {
1340 dev_err(di->dev, "Power-off LED failed\n");
1341 return ret;
1342 }
1343 }
1344
1345 return ret;
1346}
1347
1348/**
1349 * ab8500_charger_ac_en() - enable or disable ac charging
1350 * @di: pointer to the ab8500_charger structure
1351 * @enable: enable/disable flag
1352 * @vset: charging voltage
1353 * @iset: charging current
1354 *
1355 * Enable/Disable AC/Mains charging and turns on/off the charging led
1356 * respectively.
1357 **/
1358static int ab8500_charger_ac_en(struct ux500_charger *charger,
1359 int enable, int vset, int iset)
1360{
1361 int ret;
1362 int volt_index;
1363 int curr_index;
1364 int input_curr_index;
1365 u8 overshoot = 0;
1366
1367 struct ab8500_charger *di = to_ab8500_charger_ac_device_info(charger);
1368
1369 if (enable) {
1370 /* Check if AC is connected */
1371 if (!di->ac.charger_connected) {
1372 dev_err(di->dev, "AC charger not connected\n");
1373 return -ENXIO;
1374 }
1375
1376 /* Enable AC charging */
1377 dev_dbg(di->dev, "Enable AC: %dmV %dmA\n", vset, iset);
1378
1379 /*
1380 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1381 * will be triggered everytime we enable the VDD ADC supply.
1382 * This will turn off charging for a short while.
1383 * It can be avoided by having the supply on when
1384 * there is a charger enabled. Normally the VDD ADC supply
1385 * is enabled everytime a GPADC conversion is triggered. We will
1386 * force it to be enabled from this driver to have
1387 * the GPADC module independant of the AB8500 chargers
1388 */
1389 if (!di->vddadc_en_ac) {
Sachin Kamat6de7ef32013-09-06 13:31:09 +05301390 ret = regulator_enable(di->regu);
1391 if (ret)
1392 dev_warn(di->dev,
1393 "Failed to enable regulator\n");
1394 else
1395 di->vddadc_en_ac = true;
Arun Murthy84edbee2012-02-29 21:54:26 +05301396 }
1397
1398 /* Check if the requested voltage or current is valid */
1399 volt_index = ab8500_voltage_to_regval(vset);
Lee Jones861a30d2012-08-29 20:36:51 +08001400 curr_index = ab8500_current_to_regval(di, iset);
1401 input_curr_index = ab8500_current_to_regval(di,
Lee Jonesb0284de2012-11-30 10:09:42 +00001402 di->bm->chg_params->ac_curr_max);
Arun Murthy84edbee2012-02-29 21:54:26 +05301403 if (volt_index < 0 || curr_index < 0 || input_curr_index < 0) {
1404 dev_err(di->dev,
1405 "Charger voltage or current too high, "
1406 "charging not started\n");
1407 return -ENXIO;
1408 }
1409
1410 /* ChVoltLevel: maximum battery charging voltage */
1411 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1412 AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1413 if (ret) {
1414 dev_err(di->dev, "%s write failed\n", __func__);
1415 return ret;
1416 }
1417 /* MainChInputCurr: current that can be drawn from the charger*/
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001418 ret = ab8500_charger_set_main_in_curr(di,
Lee Jonesb0284de2012-11-30 10:09:42 +00001419 di->bm->chg_params->ac_curr_max);
Arun Murthy84edbee2012-02-29 21:54:26 +05301420 if (ret) {
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001421 dev_err(di->dev, "%s Failed to set MainChInputCurr\n",
1422 __func__);
Arun Murthy84edbee2012-02-29 21:54:26 +05301423 return ret;
1424 }
1425 /* ChOutputCurentLevel: protected output current */
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001426 ret = ab8500_charger_set_output_curr(di, iset);
Arun Murthy84edbee2012-02-29 21:54:26 +05301427 if (ret) {
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001428 dev_err(di->dev, "%s "
1429 "Failed to set ChOutputCurentLevel\n",
1430 __func__);
Arun Murthy84edbee2012-02-29 21:54:26 +05301431 return ret;
1432 }
1433
1434 /* Check if VBAT overshoot control should be enabled */
Lee Jonesb0284de2012-11-30 10:09:42 +00001435 if (!di->bm->enable_overshoot)
Arun Murthy84edbee2012-02-29 21:54:26 +05301436 overshoot = MAIN_CH_NO_OVERSHOOT_ENA_N;
1437
1438 /* Enable Main Charger */
1439 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1440 AB8500_MCH_CTRL1, MAIN_CH_ENA | overshoot);
1441 if (ret) {
1442 dev_err(di->dev, "%s write failed\n", __func__);
1443 return ret;
1444 }
1445
1446 /* Power on charging LED indication */
1447 ret = ab8500_charger_led_en(di, true);
1448 if (ret < 0)
1449 dev_err(di->dev, "failed to enable LED\n");
1450
1451 di->ac.charger_online = 1;
1452 } else {
1453 /* Disable AC charging */
1454 if (is_ab8500_1p1_or_earlier(di->parent)) {
1455 /*
1456 * For ABB revision 1.0 and 1.1 there is a bug in the
1457 * watchdog logic. That means we have to continously
1458 * kick the charger watchdog even when no charger is
1459 * connected. This is only valid once the AC charger
1460 * has been enabled. This is a bug that is not handled
1461 * by the algorithm and the watchdog have to be kicked
1462 * by the charger driver when the AC charger
1463 * is disabled
1464 */
1465 if (di->ac_conn) {
1466 queue_delayed_work(di->charger_wq,
1467 &di->kick_wd_work,
1468 round_jiffies(WD_KICK_INTERVAL));
1469 }
1470
1471 /*
1472 * We can't turn off charging completely
1473 * due to a bug in AB8500 cut1.
1474 * If we do, charging will not start again.
1475 * That is why we set the lowest voltage
1476 * and current possible
1477 */
1478 ret = abx500_set_register_interruptible(di->dev,
1479 AB8500_CHARGER,
1480 AB8500_CH_VOLT_LVL_REG, CH_VOL_LVL_3P5);
1481 if (ret) {
1482 dev_err(di->dev,
1483 "%s write failed\n", __func__);
1484 return ret;
1485 }
1486
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001487 ret = ab8500_charger_set_output_curr(di, 0);
Arun Murthy84edbee2012-02-29 21:54:26 +05301488 if (ret) {
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001489 dev_err(di->dev, "%s "
1490 "Failed to set ChOutputCurentLevel\n",
1491 __func__);
Arun Murthy84edbee2012-02-29 21:54:26 +05301492 return ret;
1493 }
1494 } else {
1495 ret = abx500_set_register_interruptible(di->dev,
1496 AB8500_CHARGER,
1497 AB8500_MCH_CTRL1, 0);
1498 if (ret) {
1499 dev_err(di->dev,
1500 "%s write failed\n", __func__);
1501 return ret;
1502 }
1503 }
1504
1505 ret = ab8500_charger_led_en(di, false);
1506 if (ret < 0)
1507 dev_err(di->dev, "failed to disable LED\n");
1508
1509 di->ac.charger_online = 0;
1510 di->ac.wd_expired = false;
1511
1512 /* Disable regulator if enabled */
1513 if (di->vddadc_en_ac) {
1514 regulator_disable(di->regu);
1515 di->vddadc_en_ac = false;
1516 }
1517
1518 dev_dbg(di->dev, "%s Disabled AC charging\n", __func__);
1519 }
1520 ab8500_power_supply_changed(di, &di->ac_chg.psy);
1521
1522 return ret;
1523}
1524
1525/**
1526 * ab8500_charger_usb_en() - enable usb charging
1527 * @di: pointer to the ab8500_charger structure
1528 * @enable: enable/disable flag
1529 * @vset: charging voltage
1530 * @ich_out: charger output current
1531 *
1532 * Enable/Disable USB charging and turns on/off the charging led respectively.
1533 * Returns error code in case of failure else 0(on success)
1534 */
1535static int ab8500_charger_usb_en(struct ux500_charger *charger,
1536 int enable, int vset, int ich_out)
1537{
1538 int ret;
1539 int volt_index;
1540 int curr_index;
1541 u8 overshoot = 0;
1542
1543 struct ab8500_charger *di = to_ab8500_charger_usb_device_info(charger);
1544
1545 if (enable) {
1546 /* Check if USB is connected */
1547 if (!di->usb.charger_connected) {
1548 dev_err(di->dev, "USB charger not connected\n");
1549 return -ENXIO;
1550 }
1551
1552 /*
1553 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1554 * will be triggered everytime we enable the VDD ADC supply.
1555 * This will turn off charging for a short while.
1556 * It can be avoided by having the supply on when
1557 * there is a charger enabled. Normally the VDD ADC supply
1558 * is enabled everytime a GPADC conversion is triggered. We will
1559 * force it to be enabled from this driver to have
1560 * the GPADC module independant of the AB8500 chargers
1561 */
1562 if (!di->vddadc_en_usb) {
Sachin Kamat6de7ef32013-09-06 13:31:09 +05301563 ret = regulator_enable(di->regu);
1564 if (ret)
1565 dev_warn(di->dev,
1566 "Failed to enable regulator\n");
1567 else
1568 di->vddadc_en_usb = true;
Arun Murthy84edbee2012-02-29 21:54:26 +05301569 }
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);
Lee Jones861a30d2012-08-29 20:36:51 +08001576 curr_index = ab8500_current_to_regval(di, ich_out);
Arun Murthy84edbee2012-02-29 21:54:26 +05301577 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 */
Lee Jonesf7470b52013-02-14 12:37:29 +00001613 ret = ab8500_charger_set_vbus_in_curr(di,
1614 di->max_usb_in_curr.usb_type_max);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001615 if (ret) {
1616 dev_err(di->dev, "setting USBChInputCurr failed\n");
1617 return ret;
1618 }
1619
1620 /* ChOutputCurentLevel: protected output current */
1621 ret = ab8500_charger_set_output_curr(di, ich_out);
1622 if (ret) {
1623 dev_err(di->dev, "%s "
1624 "Failed to set ChOutputCurentLevel\n",
1625 __func__);
1626 return ret;
1627 }
1628
Arun Murthy84edbee2012-02-29 21:54:26 +05301629 queue_delayed_work(di->charger_wq, &di->check_vbat_work, HZ);
1630
Arun Murthy84edbee2012-02-29 21:54:26 +05301631 } else {
1632 /* Disable USB charging */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001633 dev_dbg(di->dev, "%s Disabled USB charging\n", __func__);
Arun Murthy84edbee2012-02-29 21:54:26 +05301634 ret = abx500_set_register_interruptible(di->dev,
1635 AB8500_CHARGER,
1636 AB8500_USBCH_CTRL1_REG, 0);
1637 if (ret) {
1638 dev_err(di->dev,
1639 "%s write failed\n", __func__);
1640 return ret;
1641 }
1642
1643 ret = ab8500_charger_led_en(di, false);
1644 if (ret < 0)
1645 dev_err(di->dev, "failed to disable LED\n");
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001646 /* USBChInputCurr: current that can be drawn from the usb */
1647 ret = ab8500_charger_set_vbus_in_curr(di, 0);
1648 if (ret) {
1649 dev_err(di->dev, "setting USBChInputCurr failed\n");
1650 return ret;
1651 }
Arun Murthy84edbee2012-02-29 21:54:26 +05301652
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001653 /* ChOutputCurentLevel: protected output current */
1654 ret = ab8500_charger_set_output_curr(di, 0);
1655 if (ret) {
1656 dev_err(di->dev, "%s "
1657 "Failed to reset ChOutputCurentLevel\n",
1658 __func__);
1659 return ret;
1660 }
Arun Murthy84edbee2012-02-29 21:54:26 +05301661 di->usb.charger_online = 0;
1662 di->usb.wd_expired = false;
1663
1664 /* Disable regulator if enabled */
1665 if (di->vddadc_en_usb) {
1666 regulator_disable(di->regu);
1667 di->vddadc_en_usb = false;
1668 }
1669
1670 dev_dbg(di->dev, "%s Disabled USB charging\n", __func__);
1671
1672 /* Cancel any pending Vbat check work */
Lee Jonesb09f86d2012-12-21 17:56:52 -08001673 cancel_delayed_work(&di->check_vbat_work);
Arun Murthy84edbee2012-02-29 21:54:26 +05301674
1675 }
1676 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1677
1678 return ret;
1679}
1680
Lee Jones88917162013-02-13 11:39:19 +00001681static int ab8500_external_charger_prepare(struct notifier_block *charger_nb,
1682 unsigned long event, void *data)
1683{
1684 int ret;
1685 struct device *dev = data;
1686 /*Toggle External charger control pin*/
1687 ret = abx500_set_register_interruptible(dev, AB8500_SYS_CTRL1_BLOCK,
1688 AB8500_SYS_CHARGER_CONTROL_REG,
1689 EXTERNAL_CHARGER_DISABLE_REG_VAL);
1690 if (ret < 0) {
1691 dev_err(dev, "write reg failed %d\n", ret);
1692 goto out;
1693 }
1694 ret = abx500_set_register_interruptible(dev, AB8500_SYS_CTRL1_BLOCK,
1695 AB8500_SYS_CHARGER_CONTROL_REG,
1696 EXTERNAL_CHARGER_ENABLE_REG_VAL);
1697 if (ret < 0)
1698 dev_err(dev, "Write reg failed %d\n", ret);
1699
1700out:
1701 return ret;
1702}
1703
Arun Murthy84edbee2012-02-29 21:54:26 +05301704/**
Lee Jones4dcdf572013-02-14 09:24:10 +00001705 * ab8500_charger_usb_check_enable() - enable usb charging
1706 * @charger: pointer to the ux500_charger structure
1707 * @vset: charging voltage
1708 * @iset: charger output current
1709 *
1710 * Check if the VBUS charger has been disconnected and reconnected without
1711 * AB8500 rising an interrupt. Returns 0 on success.
1712 */
1713static int ab8500_charger_usb_check_enable(struct ux500_charger *charger,
1714 int vset, int iset)
1715{
1716 u8 usbch_ctrl1 = 0;
1717 int ret = 0;
1718
1719 struct ab8500_charger *di = to_ab8500_charger_usb_device_info(charger);
1720
1721 if (!di->usb.charger_connected)
1722 return ret;
1723
1724 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1725 AB8500_USBCH_CTRL1_REG, &usbch_ctrl1);
1726 if (ret < 0) {
1727 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
1728 return ret;
1729 }
1730 dev_dbg(di->dev, "USB charger ctrl: 0x%02x\n", usbch_ctrl1);
1731
1732 if (!(usbch_ctrl1 & USB_CH_ENA)) {
1733 dev_info(di->dev, "Charging has been disabled abnormally and will be re-enabled\n");
1734
1735 ret = abx500_mask_and_set_register_interruptible(di->dev,
1736 AB8500_CHARGER, AB8500_CHARGER_CTRL,
1737 DROP_COUNT_RESET, DROP_COUNT_RESET);
1738 if (ret < 0) {
1739 dev_err(di->dev, "ab8500 write failed %d\n", __LINE__);
1740 return ret;
1741 }
1742
1743 ret = ab8500_charger_usb_en(&di->usb_chg, true, vset, iset);
1744 if (ret < 0) {
1745 dev_err(di->dev, "Failed to enable VBUS charger %d\n",
1746 __LINE__);
1747 return ret;
1748 }
1749 }
1750 return ret;
1751}
1752
1753/**
1754 * ab8500_charger_ac_check_enable() - enable usb charging
1755 * @charger: pointer to the ux500_charger structure
1756 * @vset: charging voltage
1757 * @iset: charger output current
1758 *
1759 * Check if the AC charger has been disconnected and reconnected without
1760 * AB8500 rising an interrupt. Returns 0 on success.
1761 */
1762static int ab8500_charger_ac_check_enable(struct ux500_charger *charger,
1763 int vset, int iset)
1764{
1765 u8 mainch_ctrl1 = 0;
1766 int ret = 0;
1767
1768 struct ab8500_charger *di = to_ab8500_charger_ac_device_info(charger);
1769
1770 if (!di->ac.charger_connected)
1771 return ret;
1772
1773 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1774 AB8500_MCH_CTRL1, &mainch_ctrl1);
1775 if (ret < 0) {
1776 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
1777 return ret;
1778 }
1779 dev_dbg(di->dev, "AC charger ctrl: 0x%02x\n", mainch_ctrl1);
1780
1781 if (!(mainch_ctrl1 & MAIN_CH_ENA)) {
1782 dev_info(di->dev, "Charging has been disabled abnormally and will be re-enabled\n");
1783
1784 ret = abx500_mask_and_set_register_interruptible(di->dev,
1785 AB8500_CHARGER, AB8500_CHARGER_CTRL,
1786 DROP_COUNT_RESET, DROP_COUNT_RESET);
1787
1788 if (ret < 0) {
1789 dev_err(di->dev, "ab8500 write failed %d\n", __LINE__);
1790 return ret;
1791 }
1792
1793 ret = ab8500_charger_ac_en(&di->usb_chg, true, vset, iset);
1794 if (ret < 0) {
1795 dev_err(di->dev, "failed to enable AC charger %d\n",
1796 __LINE__);
1797 return ret;
1798 }
1799 }
1800 return ret;
1801}
1802
1803/**
Arun Murthy84edbee2012-02-29 21:54:26 +05301804 * ab8500_charger_watchdog_kick() - kick charger watchdog
1805 * @di: pointer to the ab8500_charger structure
1806 *
1807 * Kick charger watchdog
1808 * Returns error code in case of failure else 0(on success)
1809 */
1810static int ab8500_charger_watchdog_kick(struct ux500_charger *charger)
1811{
1812 int ret;
1813 struct ab8500_charger *di;
1814
1815 if (charger->psy.type == POWER_SUPPLY_TYPE_MAINS)
1816 di = to_ab8500_charger_ac_device_info(charger);
1817 else if (charger->psy.type == POWER_SUPPLY_TYPE_USB)
1818 di = to_ab8500_charger_usb_device_info(charger);
1819 else
1820 return -ENXIO;
1821
1822 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1823 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
1824 if (ret)
1825 dev_err(di->dev, "Failed to kick WD!\n");
1826
1827 return ret;
1828}
1829
1830/**
1831 * ab8500_charger_update_charger_current() - update charger current
1832 * @di: pointer to the ab8500_charger structure
1833 *
1834 * Update the charger output current for the specified charger
1835 * Returns error code in case of failure else 0(on success)
1836 */
1837static int ab8500_charger_update_charger_current(struct ux500_charger *charger,
1838 int ich_out)
1839{
1840 int ret;
Arun Murthy84edbee2012-02-29 21:54:26 +05301841 struct ab8500_charger *di;
1842
1843 if (charger->psy.type == POWER_SUPPLY_TYPE_MAINS)
1844 di = to_ab8500_charger_ac_device_info(charger);
1845 else if (charger->psy.type == POWER_SUPPLY_TYPE_USB)
1846 di = to_ab8500_charger_usb_device_info(charger);
1847 else
1848 return -ENXIO;
1849
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001850 ret = ab8500_charger_set_output_curr(di, ich_out);
Arun Murthy84edbee2012-02-29 21:54:26 +05301851 if (ret) {
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001852 dev_err(di->dev, "%s "
1853 "Failed to set ChOutputCurentLevel\n",
1854 __func__);
Arun Murthy84edbee2012-02-29 21:54:26 +05301855 return ret;
1856 }
1857
1858 /* Reset the main and usb drop input current measurement counter */
1859 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
Lee Jones4dcdf572013-02-14 09:24:10 +00001860 AB8500_CHARGER_CTRL, DROP_COUNT_RESET);
Arun Murthy84edbee2012-02-29 21:54:26 +05301861 if (ret) {
1862 dev_err(di->dev, "%s write failed\n", __func__);
1863 return ret;
1864 }
1865
1866 return ret;
1867}
1868
Lee Jonesdb43e6c2013-02-14 12:39:15 +00001869/**
1870 * ab8540_charger_power_path_enable() - enable usb power path mode
1871 * @charger: pointer to the ux500_charger structure
1872 * @enable: enable/disable flag
1873 *
1874 * Enable or disable the power path for usb mode
1875 * Returns error code in case of failure else 0(on success)
1876 */
1877static int ab8540_charger_power_path_enable(struct ux500_charger *charger,
1878 bool enable)
1879{
1880 int ret;
1881 struct ab8500_charger *di;
1882
1883 if (charger->psy.type == POWER_SUPPLY_TYPE_USB)
1884 di = to_ab8500_charger_usb_device_info(charger);
1885 else
1886 return -ENXIO;
1887
1888 ret = abx500_mask_and_set_register_interruptible(di->dev,
1889 AB8500_CHARGER, AB8540_USB_PP_MODE_REG,
1890 BUS_POWER_PATH_MODE_ENA, enable);
1891 if (ret) {
1892 dev_err(di->dev, "%s write failed\n", __func__);
1893 return ret;
1894 }
1895
1896 return ret;
1897}
1898
1899
1900/**
1901 * ab8540_charger_usb_pre_chg_enable() - enable usb pre change
1902 * @charger: pointer to the ux500_charger structure
1903 * @enable: enable/disable flag
1904 *
1905 * Enable or disable the pre-chage for usb mode
1906 * Returns error code in case of failure else 0(on success)
1907 */
1908static int ab8540_charger_usb_pre_chg_enable(struct ux500_charger *charger,
1909 bool enable)
1910{
1911 int ret;
1912 struct ab8500_charger *di;
1913
1914 if (charger->psy.type == POWER_SUPPLY_TYPE_USB)
1915 di = to_ab8500_charger_usb_device_info(charger);
1916 else
1917 return -ENXIO;
1918
1919 ret = abx500_mask_and_set_register_interruptible(di->dev,
1920 AB8500_CHARGER, AB8540_USB_PP_CHR_REG,
1921 BUS_POWER_PATH_PRECHG_ENA, enable);
1922 if (ret) {
1923 dev_err(di->dev, "%s write failed\n", __func__);
1924 return ret;
1925 }
1926
1927 return ret;
1928}
1929
Arun Murthy84edbee2012-02-29 21:54:26 +05301930static int ab8500_charger_get_ext_psy_data(struct device *dev, void *data)
1931{
1932 struct power_supply *psy;
1933 struct power_supply *ext;
1934 struct ab8500_charger *di;
1935 union power_supply_propval ret;
1936 int i, j;
1937 bool psy_found = false;
1938 struct ux500_charger *usb_chg;
1939
1940 usb_chg = (struct ux500_charger *)data;
1941 psy = &usb_chg->psy;
1942
1943 di = to_ab8500_charger_usb_device_info(usb_chg);
1944
1945 ext = dev_get_drvdata(dev);
1946
1947 /* For all psy where the driver name appears in any supplied_to */
1948 for (i = 0; i < ext->num_supplicants; i++) {
1949 if (!strcmp(ext->supplied_to[i], psy->name))
1950 psy_found = true;
1951 }
1952
1953 if (!psy_found)
1954 return 0;
1955
1956 /* Go through all properties for the psy */
1957 for (j = 0; j < ext->num_properties; j++) {
1958 enum power_supply_property prop;
1959 prop = ext->properties[j];
1960
1961 if (ext->get_property(ext, prop, &ret))
1962 continue;
1963
1964 switch (prop) {
1965 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1966 switch (ext->type) {
1967 case POWER_SUPPLY_TYPE_BATTERY:
1968 di->vbat = ret.intval / 1000;
1969 break;
1970 default:
1971 break;
1972 }
1973 break;
1974 default:
1975 break;
1976 }
1977 }
1978 return 0;
1979}
1980
1981/**
1982 * ab8500_charger_check_vbat_work() - keep vbus current within spec
1983 * @work pointer to the work_struct structure
1984 *
1985 * Due to a asic bug it is necessary to lower the input current to the vbus
1986 * charger when charging with at some specific levels. This issue is only valid
1987 * for below a certain battery voltage. This function makes sure that the
1988 * the allowed current limit isn't exceeded.
1989 */
1990static void ab8500_charger_check_vbat_work(struct work_struct *work)
1991{
1992 int t = 10;
1993 struct ab8500_charger *di = container_of(work,
1994 struct ab8500_charger, check_vbat_work.work);
1995
1996 class_for_each_device(power_supply_class, NULL,
1997 &di->usb_chg.psy, ab8500_charger_get_ext_psy_data);
1998
1999 /* First run old_vbat is 0. */
2000 if (di->old_vbat == 0)
2001 di->old_vbat = di->vbat;
2002
2003 if (!((di->old_vbat <= VBAT_TRESH_IP_CUR_RED &&
2004 di->vbat <= VBAT_TRESH_IP_CUR_RED) ||
2005 (di->old_vbat > VBAT_TRESH_IP_CUR_RED &&
2006 di->vbat > VBAT_TRESH_IP_CUR_RED))) {
2007
2008 dev_dbg(di->dev, "Vbat did cross threshold, curr: %d, new: %d,"
Lee Jonesf7470b52013-02-14 12:37:29 +00002009 " old: %d\n", di->max_usb_in_curr.usb_type_max,
2010 di->vbat, di->old_vbat);
2011 ab8500_charger_set_vbus_in_curr(di,
2012 di->max_usb_in_curr.usb_type_max);
Arun Murthy84edbee2012-02-29 21:54:26 +05302013 power_supply_changed(&di->usb_chg.psy);
2014 }
2015
2016 di->old_vbat = di->vbat;
2017
2018 /*
2019 * No need to check the battery voltage every second when not close to
2020 * the threshold.
2021 */
2022 if (di->vbat < (VBAT_TRESH_IP_CUR_RED + 100) &&
2023 (di->vbat > (VBAT_TRESH_IP_CUR_RED - 100)))
2024 t = 1;
2025
2026 queue_delayed_work(di->charger_wq, &di->check_vbat_work, t * HZ);
2027}
2028
2029/**
2030 * ab8500_charger_check_hw_failure_work() - check main charger failure
2031 * @work: pointer to the work_struct structure
2032 *
2033 * Work queue function for checking the main charger status
2034 */
2035static void ab8500_charger_check_hw_failure_work(struct work_struct *work)
2036{
2037 int ret;
2038 u8 reg_value;
2039
2040 struct ab8500_charger *di = container_of(work,
2041 struct ab8500_charger, check_hw_failure_work.work);
2042
2043 /* Check if the status bits for HW failure is still active */
2044 if (di->flags.mainextchnotok) {
2045 ret = abx500_get_register_interruptible(di->dev,
2046 AB8500_CHARGER, AB8500_CH_STATUS2_REG, &reg_value);
2047 if (ret < 0) {
2048 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2049 return;
2050 }
2051 if (!(reg_value & MAIN_CH_NOK)) {
2052 di->flags.mainextchnotok = false;
2053 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2054 }
2055 }
2056 if (di->flags.vbus_ovv) {
2057 ret = abx500_get_register_interruptible(di->dev,
2058 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG,
2059 &reg_value);
2060 if (ret < 0) {
2061 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2062 return;
2063 }
2064 if (!(reg_value & VBUS_OVV_TH)) {
2065 di->flags.vbus_ovv = false;
2066 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2067 }
2068 }
2069 /* If we still have a failure, schedule a new check */
2070 if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
2071 queue_delayed_work(di->charger_wq,
2072 &di->check_hw_failure_work, round_jiffies(HZ));
2073 }
2074}
2075
2076/**
2077 * ab8500_charger_kick_watchdog_work() - kick the watchdog
2078 * @work: pointer to the work_struct structure
2079 *
2080 * Work queue function for kicking the charger watchdog.
2081 *
2082 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
2083 * logic. That means we have to continously kick the charger
2084 * watchdog even when no charger is connected. This is only
2085 * valid once the AC charger has been enabled. This is
2086 * a bug that is not handled by the algorithm and the
2087 * watchdog have to be kicked by the charger driver
2088 * when the AC charger is disabled
2089 */
2090static void ab8500_charger_kick_watchdog_work(struct work_struct *work)
2091{
2092 int ret;
2093
2094 struct ab8500_charger *di = container_of(work,
2095 struct ab8500_charger, kick_wd_work.work);
2096
2097 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
2098 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
2099 if (ret)
2100 dev_err(di->dev, "Failed to kick WD!\n");
2101
2102 /* Schedule a new watchdog kick */
2103 queue_delayed_work(di->charger_wq,
2104 &di->kick_wd_work, round_jiffies(WD_KICK_INTERVAL));
2105}
2106
2107/**
2108 * ab8500_charger_ac_work() - work to get and set main charger status
2109 * @work: pointer to the work_struct structure
2110 *
2111 * Work queue function for checking the main charger status
2112 */
2113static void ab8500_charger_ac_work(struct work_struct *work)
2114{
2115 int ret;
2116
2117 struct ab8500_charger *di = container_of(work,
2118 struct ab8500_charger, ac_work);
2119
2120 /*
2121 * Since we can't be sure that the events are received
2122 * synchronously, we have the check if the main charger is
2123 * connected by reading the status register
2124 */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002125 ret = ab8500_charger_detect_chargers(di, false);
Arun Murthy84edbee2012-02-29 21:54:26 +05302126 if (ret < 0)
2127 return;
2128
2129 if (ret & AC_PW_CONN) {
2130 di->ac.charger_connected = 1;
2131 di->ac_conn = true;
2132 } else {
2133 di->ac.charger_connected = 0;
2134 }
2135
2136 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2137 sysfs_notify(&di->ac_chg.psy.dev->kobj, NULL, "present");
2138}
2139
Lee Jonesb269fff2013-01-11 13:12:51 +00002140static void ab8500_charger_usb_attached_work(struct work_struct *work)
2141{
2142 struct ab8500_charger *di = container_of(work,
2143 struct ab8500_charger,
2144 usb_charger_attached_work.work);
2145 int usbch = (USB_CH_VBUSDROP | USB_CH_VBUSDETDBNC);
2146 int ret, i;
2147 u8 statval;
2148
2149 for (i = 0; i < 10; i++) {
2150 ret = abx500_get_register_interruptible(di->dev,
2151 AB8500_CHARGER,
2152 AB8500_CH_USBCH_STAT1_REG,
2153 &statval);
2154 if (ret < 0) {
2155 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
2156 goto reschedule;
2157 }
2158 if ((statval & usbch) != usbch)
2159 goto reschedule;
2160
2161 msleep(CHARGER_STATUS_POLL);
2162 }
2163
2164 ab8500_charger_usb_en(&di->usb_chg, 0, 0, 0);
2165
2166 mutex_lock(&di->charger_attached_mutex);
2167 mutex_unlock(&di->charger_attached_mutex);
2168
2169 return;
2170
2171reschedule:
2172 queue_delayed_work(di->charger_wq,
2173 &di->usb_charger_attached_work,
2174 HZ);
2175}
2176
2177static void ab8500_charger_ac_attached_work(struct work_struct *work)
2178{
2179
2180 struct ab8500_charger *di = container_of(work,
2181 struct ab8500_charger,
2182 ac_charger_attached_work.work);
2183 int mainch = (MAIN_CH_STATUS2_MAINCHGDROP |
2184 MAIN_CH_STATUS2_MAINCHARGERDETDBNC);
2185 int ret, i;
2186 u8 statval;
2187
2188 for (i = 0; i < 10; i++) {
2189 ret = abx500_get_register_interruptible(di->dev,
2190 AB8500_CHARGER,
2191 AB8500_CH_STATUS2_REG,
2192 &statval);
2193 if (ret < 0) {
2194 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
2195 goto reschedule;
2196 }
2197
2198 if ((statval & mainch) != mainch)
2199 goto reschedule;
2200
2201 msleep(CHARGER_STATUS_POLL);
2202 }
2203
2204 ab8500_charger_ac_en(&di->ac_chg, 0, 0, 0);
2205 queue_work(di->charger_wq, &di->ac_work);
2206
2207 mutex_lock(&di->charger_attached_mutex);
2208 mutex_unlock(&di->charger_attached_mutex);
2209
2210 return;
2211
2212reschedule:
2213 queue_delayed_work(di->charger_wq,
2214 &di->ac_charger_attached_work,
2215 HZ);
2216}
2217
Arun Murthy84edbee2012-02-29 21:54:26 +05302218/**
2219 * ab8500_charger_detect_usb_type_work() - work to detect USB type
2220 * @work: Pointer to the work_struct structure
2221 *
2222 * Detect the type of USB plugged
2223 */
Anton Vorontsov64eb9b02012-03-14 04:43:11 +04002224static void ab8500_charger_detect_usb_type_work(struct work_struct *work)
Arun Murthy84edbee2012-02-29 21:54:26 +05302225{
2226 int ret;
2227
2228 struct ab8500_charger *di = container_of(work,
2229 struct ab8500_charger, detect_usb_type_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 ret = ab8500_charger_detect_chargers(di, false);
Arun Murthy84edbee2012-02-29 21:54:26 +05302237 if (ret < 0)
2238 return;
2239
2240 if (!(ret & USB_PW_CONN)) {
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002241 dev_dbg(di->dev, "%s di->vbus_detected = false\n", __func__);
2242 di->vbus_detected = false;
Arun Murthy84edbee2012-02-29 21:54:26 +05302243 ab8500_charger_set_usb_connected(di, false);
2244 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2245 } else {
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002246 dev_dbg(di->dev, "%s di->vbus_detected = true\n", __func__);
2247 di->vbus_detected = true;
Arun Murthy84edbee2012-02-29 21:54:26 +05302248
2249 if (is_ab8500_1p1_or_earlier(di->parent)) {
2250 ret = ab8500_charger_detect_usb_type(di);
2251 if (!ret) {
2252 ab8500_charger_set_usb_connected(di, true);
2253 ab8500_power_supply_changed(di,
2254 &di->usb_chg.psy);
2255 }
2256 } else {
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002257 /*
2258 * For ABB cut2.0 and onwards we have an IRQ,
Arun Murthy84edbee2012-02-29 21:54:26 +05302259 * USB_LINK_STATUS that will be triggered when the USB
2260 * link status changes. The exception is USB connected
2261 * during startup. Then we don't get a
2262 * USB_LINK_STATUS IRQ
2263 */
2264 if (di->vbus_detected_start) {
2265 di->vbus_detected_start = false;
2266 ret = ab8500_charger_detect_usb_type(di);
2267 if (!ret) {
2268 ab8500_charger_set_usb_connected(di,
2269 true);
2270 ab8500_power_supply_changed(di,
2271 &di->usb_chg.psy);
2272 }
2273 }
2274 }
2275 }
2276}
2277
2278/**
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002279 * ab8500_charger_usb_link_attach_work() - work to detect USB type
Marcus Cooper4b45f4a2013-01-11 13:13:04 +00002280 * @work: pointer to the work_struct structure
2281 *
2282 * Detect the type of USB plugged
2283 */
2284static void ab8500_charger_usb_link_attach_work(struct work_struct *work)
2285{
2286 struct ab8500_charger *di =
2287 container_of(work, struct ab8500_charger, attach_work.work);
2288 int ret;
2289
2290 /* Update maximum input current if USB enumeration is not detected */
2291 if (!di->usb.charger_online) {
Lee Jonesf7470b52013-02-14 12:37:29 +00002292 ret = ab8500_charger_set_vbus_in_curr(di,
2293 di->max_usb_in_curr.usb_type_max);
Marcus Cooper4b45f4a2013-01-11 13:13:04 +00002294 if (ret)
2295 return;
2296 }
2297
2298 ab8500_charger_set_usb_connected(di, true);
2299 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2300}
2301
2302/**
Arun Murthy84edbee2012-02-29 21:54:26 +05302303 * ab8500_charger_usb_link_status_work() - work to detect USB type
2304 * @work: pointer to the work_struct structure
2305 *
2306 * Detect the type of USB plugged
2307 */
2308static void ab8500_charger_usb_link_status_work(struct work_struct *work)
2309{
Henrik Sölverff380902012-04-17 15:51:01 +02002310 int detected_chargers;
Arun Murthy84edbee2012-02-29 21:54:26 +05302311 int ret;
Henrik Sölverff380902012-04-17 15:51:01 +02002312 u8 val;
Hakan Bergd4337662012-07-26 11:10:35 +02002313 u8 link_status;
Arun Murthy84edbee2012-02-29 21:54:26 +05302314
2315 struct ab8500_charger *di = container_of(work,
2316 struct ab8500_charger, usb_link_status_work);
2317
2318 /*
2319 * Since we can't be sure that the events are received
2320 * synchronously, we have the check if is
2321 * connected by reading the status register
2322 */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002323 detected_chargers = ab8500_charger_detect_chargers(di, false);
Henrik Sölverff380902012-04-17 15:51:01 +02002324 if (detected_chargers < 0)
Arun Murthy84edbee2012-02-29 21:54:26 +05302325 return;
2326
Henrik Sölverff380902012-04-17 15:51:01 +02002327 /*
2328 * Some chargers that breaks the USB spec is
2329 * identified as invalid by AB8500 and it refuse
2330 * to start the charging process. but by jumping
2331 * thru a few hoops it can be forced to start.
2332 */
Marcus Cooper88efdb82012-08-30 14:12:53 +02002333 if (is_ab8500(di->parent))
2334 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
2335 AB8500_USB_LINE_STAT_REG, &val);
2336 else
2337 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
2338 AB8500_USB_LINK1_STAT_REG, &val);
2339
Henrik Sölverff380902012-04-17 15:51:01 +02002340 if (ret >= 0)
2341 dev_dbg(di->dev, "UsbLineStatus register = 0x%02x\n", val);
2342 else
2343 dev_dbg(di->dev, "Error reading USB link status\n");
2344
Lee Jones861a30d2012-08-29 20:36:51 +08002345 if (is_ab8500(di->parent))
Hakan Bergd4337662012-07-26 11:10:35 +02002346 link_status = AB8500_USB_LINK_STATUS;
Lee Jones861a30d2012-08-29 20:36:51 +08002347 else
2348 link_status = AB8505_USB_LINK_STATUS;
Hakan Bergd4337662012-07-26 11:10:35 +02002349
Henrik Sölverff380902012-04-17 15:51:01 +02002350 if (detected_chargers & USB_PW_CONN) {
Lee Jones861a30d2012-08-29 20:36:51 +08002351 if (((val & link_status) >> USB_LINK_STATUS_SHIFT) ==
2352 USB_STAT_NOT_VALID_LINK &&
Henrik Sölverff380902012-04-17 15:51:01 +02002353 di->invalid_charger_detect_state == 0) {
Lee Jones861a30d2012-08-29 20:36:51 +08002354 dev_dbg(di->dev,
2355 "Invalid charger detected, state= 0\n");
Henrik Sölverff380902012-04-17 15:51:01 +02002356 /*Enable charger*/
2357 abx500_mask_and_set_register_interruptible(di->dev,
Lee Jones861a30d2012-08-29 20:36:51 +08002358 AB8500_CHARGER, AB8500_USBCH_CTRL1_REG,
2359 USB_CH_ENA, USB_CH_ENA);
Henrik Sölverff380902012-04-17 15:51:01 +02002360 /*Enable charger detection*/
Marcus Cooperb3ea5f42012-08-29 17:56:19 +02002361 abx500_mask_and_set_register_interruptible(di->dev,
2362 AB8500_USB, AB8500_USB_LINE_CTRL2_REG,
2363 USB_CH_DET, USB_CH_DET);
Henrik Sölverff380902012-04-17 15:51:01 +02002364 di->invalid_charger_detect_state = 1;
2365 /*exit and wait for new link status interrupt.*/
2366 return;
2367
2368 }
2369 if (di->invalid_charger_detect_state == 1) {
Lee Jones861a30d2012-08-29 20:36:51 +08002370 dev_dbg(di->dev,
2371 "Invalid charger detected, state= 1\n");
Henrik Sölverff380902012-04-17 15:51:01 +02002372 /*Stop charger detection*/
Marcus Cooperb3ea5f42012-08-29 17:56:19 +02002373 abx500_mask_and_set_register_interruptible(di->dev,
2374 AB8500_USB, AB8500_USB_LINE_CTRL2_REG,
2375 USB_CH_DET, 0x00);
Henrik Sölverff380902012-04-17 15:51:01 +02002376 /*Check link status*/
Marcus Cooper88efdb82012-08-30 14:12:53 +02002377 if (is_ab8500(di->parent))
2378 ret = abx500_get_register_interruptible(di->dev,
2379 AB8500_USB, AB8500_USB_LINE_STAT_REG,
2380 &val);
2381 else
2382 ret = abx500_get_register_interruptible(di->dev,
2383 AB8500_USB, AB8500_USB_LINK1_STAT_REG,
2384 &val);
2385
Henrik Sölverff380902012-04-17 15:51:01 +02002386 dev_dbg(di->dev, "USB link status= 0x%02x\n",
Lee Jones861a30d2012-08-29 20:36:51 +08002387 (val & link_status) >> USB_LINK_STATUS_SHIFT);
Henrik Sölverff380902012-04-17 15:51:01 +02002388 di->invalid_charger_detect_state = 2;
2389 }
2390 } else {
2391 di->invalid_charger_detect_state = 0;
2392 }
2393
2394 if (!(detected_chargers & USB_PW_CONN)) {
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002395 di->vbus_detected = false;
Arun Murthy84edbee2012-02-29 21:54:26 +05302396 ab8500_charger_set_usb_connected(di, false);
2397 ab8500_power_supply_changed(di, &di->usb_chg.psy);
Marcus Cooper4b45f4a2013-01-11 13:13:04 +00002398 return;
2399 }
Arun Murthy84edbee2012-02-29 21:54:26 +05302400
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002401 dev_dbg(di->dev,"%s di->vbus_detected = true\n",__func__);
2402 di->vbus_detected = true;
Marcus Cooper4b45f4a2013-01-11 13:13:04 +00002403 ret = ab8500_charger_read_usb_type(di);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002404 if (ret) {
2405 if (ret == -ENXIO) {
2406 /* No valid charger type detected */
2407 ab8500_charger_set_usb_connected(di, false);
2408 ab8500_power_supply_changed(di, &di->usb_chg.psy);
Arun Murthy84edbee2012-02-29 21:54:26 +05302409 }
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002410 return;
2411 }
2412
2413 if (di->usb_device_is_unrecognised) {
2414 dev_dbg(di->dev,
2415 "Potential Legacy Charger device. "
2416 "Delay work for %d msec for USB enum "
2417 "to finish",
2418 WAIT_ACA_RID_ENUMERATION);
2419 queue_delayed_work(di->charger_wq,
2420 &di->attach_work,
2421 msecs_to_jiffies(WAIT_ACA_RID_ENUMERATION));
2422 } else if (di->is_aca_rid == 1) {
2423 /* Only wait once */
2424 di->is_aca_rid++;
2425 dev_dbg(di->dev,
2426 "%s Wait %d msec for USB enum to finish",
2427 __func__, WAIT_ACA_RID_ENUMERATION);
2428 queue_delayed_work(di->charger_wq,
2429 &di->attach_work,
2430 msecs_to_jiffies(WAIT_ACA_RID_ENUMERATION));
2431 } else {
2432 queue_delayed_work(di->charger_wq,
2433 &di->attach_work,
2434 0);
Arun Murthy84edbee2012-02-29 21:54:26 +05302435 }
2436}
2437
2438static void ab8500_charger_usb_state_changed_work(struct work_struct *work)
2439{
2440 int ret;
2441 unsigned long flags;
2442
2443 struct ab8500_charger *di = container_of(work,
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002444 struct ab8500_charger, usb_state_changed_work.work);
Arun Murthy84edbee2012-02-29 21:54:26 +05302445
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002446 if (!di->vbus_detected) {
2447 dev_dbg(di->dev,
2448 "%s !di->vbus_detected\n",
2449 __func__);
Arun Murthy84edbee2012-02-29 21:54:26 +05302450 return;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002451 }
Arun Murthy84edbee2012-02-29 21:54:26 +05302452
2453 spin_lock_irqsave(&di->usb_state.usb_lock, flags);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002454 di->usb_state.state = di->usb_state.state_tmp;
2455 di->usb_state.usb_current = di->usb_state.usb_current_tmp;
Arun Murthy84edbee2012-02-29 21:54:26 +05302456 spin_unlock_irqrestore(&di->usb_state.usb_lock, flags);
2457
Arun Murthy84edbee2012-02-29 21:54:26 +05302458 dev_dbg(di->dev, "%s USB state: 0x%02x mA: %d\n",
2459 __func__, di->usb_state.state, di->usb_state.usb_current);
2460
2461 switch (di->usb_state.state) {
2462 case AB8500_BM_USB_STATE_RESET_HS:
2463 case AB8500_BM_USB_STATE_RESET_FS:
2464 case AB8500_BM_USB_STATE_SUSPEND:
2465 case AB8500_BM_USB_STATE_MAX:
2466 ab8500_charger_set_usb_connected(di, false);
2467 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2468 break;
2469
2470 case AB8500_BM_USB_STATE_RESUME:
2471 /*
2472 * when suspend->resume there should be delay
2473 * of 1sec for enabling charging
2474 */
2475 msleep(1000);
2476 /* Intentional fall through */
2477 case AB8500_BM_USB_STATE_CONFIGURED:
2478 /*
2479 * USB is configured, enable charging with the charging
2480 * input current obtained from USB driver
2481 */
2482 if (!ab8500_charger_get_usb_cur(di)) {
2483 /* Update maximum input current */
2484 ret = ab8500_charger_set_vbus_in_curr(di,
Lee Jonesf7470b52013-02-14 12:37:29 +00002485 di->max_usb_in_curr.usb_type_max);
Arun Murthy84edbee2012-02-29 21:54:26 +05302486 if (ret)
2487 return;
2488
2489 ab8500_charger_set_usb_connected(di, true);
2490 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2491 }
2492 break;
2493
2494 default:
2495 break;
2496 };
2497}
2498
2499/**
2500 * ab8500_charger_check_usbchargernotok_work() - check USB chg not ok status
2501 * @work: pointer to the work_struct structure
2502 *
2503 * Work queue function for checking the USB charger Not OK status
2504 */
2505static void ab8500_charger_check_usbchargernotok_work(struct work_struct *work)
2506{
2507 int ret;
2508 u8 reg_value;
2509 bool prev_status;
2510
2511 struct ab8500_charger *di = container_of(work,
2512 struct ab8500_charger, check_usbchgnotok_work.work);
2513
2514 /* Check if the status bit for usbchargernotok is still active */
2515 ret = abx500_get_register_interruptible(di->dev,
2516 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, &reg_value);
2517 if (ret < 0) {
2518 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2519 return;
2520 }
2521 prev_status = di->flags.usbchargernotok;
2522
2523 if (reg_value & VBUS_CH_NOK) {
2524 di->flags.usbchargernotok = true;
2525 /* Check again in 1sec */
2526 queue_delayed_work(di->charger_wq,
2527 &di->check_usbchgnotok_work, HZ);
2528 } else {
2529 di->flags.usbchargernotok = false;
2530 di->flags.vbus_collapse = false;
2531 }
2532
2533 if (prev_status != di->flags.usbchargernotok)
2534 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2535}
2536
2537/**
2538 * ab8500_charger_check_main_thermal_prot_work() - check main thermal status
2539 * @work: pointer to the work_struct structure
2540 *
2541 * Work queue function for checking the Main thermal prot status
2542 */
2543static void ab8500_charger_check_main_thermal_prot_work(
2544 struct work_struct *work)
2545{
2546 int ret;
2547 u8 reg_value;
2548
2549 struct ab8500_charger *di = container_of(work,
2550 struct ab8500_charger, check_main_thermal_prot_work);
2551
2552 /* Check if the status bit for main_thermal_prot is still active */
2553 ret = abx500_get_register_interruptible(di->dev,
2554 AB8500_CHARGER, AB8500_CH_STATUS2_REG, &reg_value);
2555 if (ret < 0) {
2556 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2557 return;
2558 }
2559 if (reg_value & MAIN_CH_TH_PROT)
2560 di->flags.main_thermal_prot = true;
2561 else
2562 di->flags.main_thermal_prot = false;
2563
2564 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2565}
2566
2567/**
2568 * ab8500_charger_check_usb_thermal_prot_work() - check usb thermal status
2569 * @work: pointer to the work_struct structure
2570 *
2571 * Work queue function for checking the USB thermal prot status
2572 */
2573static void ab8500_charger_check_usb_thermal_prot_work(
2574 struct work_struct *work)
2575{
2576 int ret;
2577 u8 reg_value;
2578
2579 struct ab8500_charger *di = container_of(work,
2580 struct ab8500_charger, check_usb_thermal_prot_work);
2581
2582 /* Check if the status bit for usb_thermal_prot is still active */
2583 ret = abx500_get_register_interruptible(di->dev,
2584 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, &reg_value);
2585 if (ret < 0) {
2586 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2587 return;
2588 }
2589 if (reg_value & USB_CH_TH_PROT)
2590 di->flags.usb_thermal_prot = true;
2591 else
2592 di->flags.usb_thermal_prot = false;
2593
2594 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2595}
2596
2597/**
2598 * ab8500_charger_mainchunplugdet_handler() - main charger unplugged
2599 * @irq: interrupt number
2600 * @_di: pointer to the ab8500_charger structure
2601 *
2602 * Returns IRQ status(IRQ_HANDLED)
2603 */
2604static irqreturn_t ab8500_charger_mainchunplugdet_handler(int irq, void *_di)
2605{
2606 struct ab8500_charger *di = _di;
2607
2608 dev_dbg(di->dev, "Main charger unplugged\n");
2609 queue_work(di->charger_wq, &di->ac_work);
2610
Lee Jonesb269fff2013-01-11 13:12:51 +00002611 cancel_delayed_work_sync(&di->ac_charger_attached_work);
2612 mutex_lock(&di->charger_attached_mutex);
2613 mutex_unlock(&di->charger_attached_mutex);
2614
Arun Murthy84edbee2012-02-29 21:54:26 +05302615 return IRQ_HANDLED;
2616}
2617
2618/**
2619 * ab8500_charger_mainchplugdet_handler() - main charger plugged
2620 * @irq: interrupt number
2621 * @_di: pointer to the ab8500_charger structure
2622 *
2623 * Returns IRQ status(IRQ_HANDLED)
2624 */
2625static irqreturn_t ab8500_charger_mainchplugdet_handler(int irq, void *_di)
2626{
2627 struct ab8500_charger *di = _di;
2628
2629 dev_dbg(di->dev, "Main charger plugged\n");
2630 queue_work(di->charger_wq, &di->ac_work);
2631
Lee Jonesb269fff2013-01-11 13:12:51 +00002632 mutex_lock(&di->charger_attached_mutex);
2633 mutex_unlock(&di->charger_attached_mutex);
Rabin Vincent261c5132012-09-26 13:36:38 +02002634
2635 if (is_ab8500(di->parent))
2636 queue_delayed_work(di->charger_wq,
Lee Jonesb269fff2013-01-11 13:12:51 +00002637 &di->ac_charger_attached_work,
2638 HZ);
Arun Murthy84edbee2012-02-29 21:54:26 +05302639 return IRQ_HANDLED;
2640}
2641
2642/**
2643 * ab8500_charger_mainextchnotok_handler() - main charger not ok
2644 * @irq: interrupt number
2645 * @_di: pointer to the ab8500_charger structure
2646 *
2647 * Returns IRQ status(IRQ_HANDLED)
2648 */
2649static irqreturn_t ab8500_charger_mainextchnotok_handler(int irq, void *_di)
2650{
2651 struct ab8500_charger *di = _di;
2652
2653 dev_dbg(di->dev, "Main charger not ok\n");
2654 di->flags.mainextchnotok = true;
2655 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2656
2657 /* Schedule a new HW failure check */
2658 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
2659
2660 return IRQ_HANDLED;
2661}
2662
2663/**
2664 * ab8500_charger_mainchthprotr_handler() - Die temp is above main charger
2665 * thermal protection threshold
2666 * @irq: interrupt number
2667 * @_di: pointer to the ab8500_charger structure
2668 *
2669 * Returns IRQ status(IRQ_HANDLED)
2670 */
2671static irqreturn_t ab8500_charger_mainchthprotr_handler(int irq, void *_di)
2672{
2673 struct ab8500_charger *di = _di;
2674
2675 dev_dbg(di->dev,
2676 "Die temp above Main charger thermal protection threshold\n");
2677 queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
2678
2679 return IRQ_HANDLED;
2680}
2681
2682/**
2683 * ab8500_charger_mainchthprotf_handler() - Die temp is below main charger
2684 * thermal protection threshold
2685 * @irq: interrupt number
2686 * @_di: pointer to the ab8500_charger structure
2687 *
2688 * Returns IRQ status(IRQ_HANDLED)
2689 */
2690static irqreturn_t ab8500_charger_mainchthprotf_handler(int irq, void *_di)
2691{
2692 struct ab8500_charger *di = _di;
2693
2694 dev_dbg(di->dev,
2695 "Die temp ok for Main charger thermal protection threshold\n");
2696 queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
2697
2698 return IRQ_HANDLED;
2699}
2700
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002701static void ab8500_charger_vbus_drop_end_work(struct work_struct *work)
2702{
2703 struct ab8500_charger *di = container_of(work,
2704 struct ab8500_charger, vbus_drop_end_work.work);
Lee Jones861a30d2012-08-29 20:36:51 +08002705 int ret, curr;
Lee Jonesf7470b52013-02-14 12:37:29 +00002706 u8 reg_value;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002707
2708 di->flags.vbus_drop_end = false;
2709
2710 /* Reset the drop counter */
2711 abx500_set_register_interruptible(di->dev,
2712 AB8500_CHARGER, AB8500_CHARGER_CTRL, 0x01);
Lee Jones861a30d2012-08-29 20:36:51 +08002713
2714 if (is_ab8540(di->parent))
2715 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
2716 AB8540_CH_USBCH_STAT3_REG, &reg_value);
2717 else
2718 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
2719 AB8500_CH_USBCH_STAT2_REG, &reg_value);
Lee Jonesf7470b52013-02-14 12:37:29 +00002720 if (ret < 0) {
Lee Jones861a30d2012-08-29 20:36:51 +08002721 dev_err(di->dev, "%s read failed\n", __func__);
2722 return;
2723 }
2724
2725 if (is_ab8540(di->parent))
2726 curr = di->bm->chg_input_curr[
2727 reg_value & AB8540_AUTO_VBUS_IN_CURR_MASK];
2728 else
2729 curr = di->bm->chg_input_curr[
Lee Jonesf7470b52013-02-14 12:37:29 +00002730 reg_value >> AUTO_VBUS_IN_CURR_LIM_SHIFT];
Lee Jones861a30d2012-08-29 20:36:51 +08002731
2732 if (di->max_usb_in_curr.calculated_max != curr) {
2733 /* USB source is collapsing */
2734 di->max_usb_in_curr.calculated_max = curr;
2735 dev_dbg(di->dev,
2736 "VBUS input current limiting to %d mA\n",
2737 di->max_usb_in_curr.calculated_max);
2738 } else {
2739 /*
2740 * USB source can not give more than this amount.
2741 * Taking more will collapse the source.
2742 */
2743 di->max_usb_in_curr.set_max =
2744 di->max_usb_in_curr.calculated_max;
2745 dev_dbg(di->dev,
2746 "VBUS input current limited to %d mA\n",
2747 di->max_usb_in_curr.set_max);
Lee Jonesf7470b52013-02-14 12:37:29 +00002748 }
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002749
2750 if (di->usb.charger_connected)
Lee Jonesf7470b52013-02-14 12:37:29 +00002751 ab8500_charger_set_vbus_in_curr(di,
2752 di->max_usb_in_curr.usb_type_max);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002753}
2754
Arun Murthy84edbee2012-02-29 21:54:26 +05302755/**
2756 * ab8500_charger_vbusdetf_handler() - VBUS falling detected
2757 * @irq: interrupt number
2758 * @_di: pointer to the ab8500_charger structure
2759 *
2760 * Returns IRQ status(IRQ_HANDLED)
2761 */
2762static irqreturn_t ab8500_charger_vbusdetf_handler(int irq, void *_di)
2763{
2764 struct ab8500_charger *di = _di;
2765
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002766 di->vbus_detected = false;
Arun Murthy84edbee2012-02-29 21:54:26 +05302767 dev_dbg(di->dev, "VBUS falling detected\n");
2768 queue_work(di->charger_wq, &di->detect_usb_type_work);
2769
2770 return IRQ_HANDLED;
2771}
2772
2773/**
2774 * ab8500_charger_vbusdetr_handler() - VBUS rising detected
2775 * @irq: interrupt number
2776 * @_di: pointer to the ab8500_charger structure
2777 *
2778 * Returns IRQ status(IRQ_HANDLED)
2779 */
2780static irqreturn_t ab8500_charger_vbusdetr_handler(int irq, void *_di)
2781{
2782 struct ab8500_charger *di = _di;
2783
2784 di->vbus_detected = true;
2785 dev_dbg(di->dev, "VBUS rising detected\n");
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002786
Arun Murthy84edbee2012-02-29 21:54:26 +05302787 queue_work(di->charger_wq, &di->detect_usb_type_work);
2788
2789 return IRQ_HANDLED;
2790}
2791
2792/**
2793 * ab8500_charger_usblinkstatus_handler() - USB link status has changed
2794 * @irq: interrupt number
2795 * @_di: pointer to the ab8500_charger structure
2796 *
2797 * Returns IRQ status(IRQ_HANDLED)
2798 */
2799static irqreturn_t ab8500_charger_usblinkstatus_handler(int irq, void *_di)
2800{
2801 struct ab8500_charger *di = _di;
2802
2803 dev_dbg(di->dev, "USB link status changed\n");
2804
2805 queue_work(di->charger_wq, &di->usb_link_status_work);
2806
2807 return IRQ_HANDLED;
2808}
2809
2810/**
2811 * ab8500_charger_usbchthprotr_handler() - Die temp is above usb charger
2812 * thermal protection threshold
2813 * @irq: interrupt number
2814 * @_di: pointer to the ab8500_charger structure
2815 *
2816 * Returns IRQ status(IRQ_HANDLED)
2817 */
2818static irqreturn_t ab8500_charger_usbchthprotr_handler(int irq, void *_di)
2819{
2820 struct ab8500_charger *di = _di;
2821
2822 dev_dbg(di->dev,
2823 "Die temp above USB charger thermal protection threshold\n");
2824 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2825
2826 return IRQ_HANDLED;
2827}
2828
2829/**
2830 * ab8500_charger_usbchthprotf_handler() - Die temp is below usb charger
2831 * thermal protection threshold
2832 * @irq: interrupt number
2833 * @_di: pointer to the ab8500_charger structure
2834 *
2835 * Returns IRQ status(IRQ_HANDLED)
2836 */
2837static irqreturn_t ab8500_charger_usbchthprotf_handler(int irq, void *_di)
2838{
2839 struct ab8500_charger *di = _di;
2840
2841 dev_dbg(di->dev,
2842 "Die temp ok for USB charger thermal protection threshold\n");
2843 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2844
2845 return IRQ_HANDLED;
2846}
2847
2848/**
2849 * ab8500_charger_usbchargernotokr_handler() - USB charger not ok detected
2850 * @irq: interrupt number
2851 * @_di: pointer to the ab8500_charger structure
2852 *
2853 * Returns IRQ status(IRQ_HANDLED)
2854 */
2855static irqreturn_t ab8500_charger_usbchargernotokr_handler(int irq, void *_di)
2856{
2857 struct ab8500_charger *di = _di;
2858
2859 dev_dbg(di->dev, "Not allowed USB charger detected\n");
2860 queue_delayed_work(di->charger_wq, &di->check_usbchgnotok_work, 0);
2861
2862 return IRQ_HANDLED;
2863}
2864
2865/**
2866 * ab8500_charger_chwdexp_handler() - Charger watchdog expired
2867 * @irq: interrupt number
2868 * @_di: pointer to the ab8500_charger structure
2869 *
2870 * Returns IRQ status(IRQ_HANDLED)
2871 */
2872static irqreturn_t ab8500_charger_chwdexp_handler(int irq, void *_di)
2873{
2874 struct ab8500_charger *di = _di;
2875
2876 dev_dbg(di->dev, "Charger watchdog expired\n");
2877
2878 /*
2879 * The charger that was online when the watchdog expired
2880 * needs to be restarted for charging to start again
2881 */
2882 if (di->ac.charger_online) {
2883 di->ac.wd_expired = true;
2884 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2885 }
2886 if (di->usb.charger_online) {
2887 di->usb.wd_expired = true;
2888 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2889 }
2890
2891 return IRQ_HANDLED;
2892}
2893
2894/**
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002895 * ab8500_charger_vbuschdropend_handler() - VBUS drop removed
2896 * @irq: interrupt number
2897 * @_di: pointer to the ab8500_charger structure
2898 *
2899 * Returns IRQ status(IRQ_HANDLED)
2900 */
2901static irqreturn_t ab8500_charger_vbuschdropend_handler(int irq, void *_di)
2902{
2903 struct ab8500_charger *di = _di;
2904
2905 dev_dbg(di->dev, "VBUS charger drop ended\n");
2906 di->flags.vbus_drop_end = true;
Lee Jonesf7470b52013-02-14 12:37:29 +00002907
2908 /*
2909 * VBUS might have dropped due to bad connection.
2910 * Schedule a new input limit set to the value SW requests.
2911 */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002912 queue_delayed_work(di->charger_wq, &di->vbus_drop_end_work,
Lee Jonesf7470b52013-02-14 12:37:29 +00002913 round_jiffies(VBUS_IN_CURR_LIM_RETRY_SET_TIME * HZ));
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002914
2915 return IRQ_HANDLED;
2916}
2917
2918/**
Arun Murthy84edbee2012-02-29 21:54:26 +05302919 * ab8500_charger_vbusovv_handler() - VBUS overvoltage detected
2920 * @irq: interrupt number
2921 * @_di: pointer to the ab8500_charger structure
2922 *
2923 * Returns IRQ status(IRQ_HANDLED)
2924 */
2925static irqreturn_t ab8500_charger_vbusovv_handler(int irq, void *_di)
2926{
2927 struct ab8500_charger *di = _di;
2928
2929 dev_dbg(di->dev, "VBUS overvoltage detected\n");
2930 di->flags.vbus_ovv = true;
2931 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2932
2933 /* Schedule a new HW failure check */
2934 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
2935
2936 return IRQ_HANDLED;
2937}
2938
2939/**
2940 * ab8500_charger_ac_get_property() - get the ac/mains properties
2941 * @psy: pointer to the power_supply structure
2942 * @psp: pointer to the power_supply_property structure
2943 * @val: pointer to the power_supply_propval union
2944 *
2945 * This function gets called when an application tries to get the ac/mains
2946 * properties by reading the sysfs files.
2947 * AC/Mains properties are online, present and voltage.
2948 * online: ac/mains charging is in progress or not
2949 * present: presence of the ac/mains
2950 * voltage: AC/Mains voltage
2951 * Returns error code in case of failure else 0(on success)
2952 */
2953static int ab8500_charger_ac_get_property(struct power_supply *psy,
2954 enum power_supply_property psp,
2955 union power_supply_propval *val)
2956{
2957 struct ab8500_charger *di;
Jonas Aaberga864c5a2013-01-11 13:12:53 +00002958 int ret;
Arun Murthy84edbee2012-02-29 21:54:26 +05302959
2960 di = to_ab8500_charger_ac_device_info(psy_to_ux500_charger(psy));
2961
2962 switch (psp) {
2963 case POWER_SUPPLY_PROP_HEALTH:
2964 if (di->flags.mainextchnotok)
2965 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
2966 else if (di->ac.wd_expired || di->usb.wd_expired)
2967 val->intval = POWER_SUPPLY_HEALTH_DEAD;
2968 else if (di->flags.main_thermal_prot)
2969 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
2970 else
2971 val->intval = POWER_SUPPLY_HEALTH_GOOD;
2972 break;
2973 case POWER_SUPPLY_PROP_ONLINE:
2974 val->intval = di->ac.charger_online;
2975 break;
2976 case POWER_SUPPLY_PROP_PRESENT:
2977 val->intval = di->ac.charger_connected;
2978 break;
2979 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Jonas Aaberga864c5a2013-01-11 13:12:53 +00002980 ret = ab8500_charger_get_ac_voltage(di);
2981 if (ret >= 0)
2982 di->ac.charger_voltage = ret;
2983 /* On error, use previous value */
Arun Murthy84edbee2012-02-29 21:54:26 +05302984 val->intval = di->ac.charger_voltage * 1000;
2985 break;
2986 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
2987 /*
2988 * This property is used to indicate when CV mode is entered
2989 * for the AC charger
2990 */
2991 di->ac.cv_active = ab8500_charger_ac_cv(di);
2992 val->intval = di->ac.cv_active;
2993 break;
2994 case POWER_SUPPLY_PROP_CURRENT_NOW:
Jonas Aaberga864c5a2013-01-11 13:12:53 +00002995 ret = ab8500_charger_get_ac_current(di);
2996 if (ret >= 0)
2997 di->ac.charger_current = ret;
2998 val->intval = di->ac.charger_current * 1000;
Arun Murthy84edbee2012-02-29 21:54:26 +05302999 break;
3000 default:
3001 return -EINVAL;
3002 }
3003 return 0;
3004}
3005
3006/**
3007 * ab8500_charger_usb_get_property() - get the usb properties
3008 * @psy: pointer to the power_supply structure
3009 * @psp: pointer to the power_supply_property structure
3010 * @val: pointer to the power_supply_propval union
3011 *
3012 * This function gets called when an application tries to get the usb
3013 * properties by reading the sysfs files.
3014 * USB properties are online, present and voltage.
3015 * online: usb charging is in progress or not
3016 * present: presence of the usb
3017 * voltage: vbus voltage
3018 * Returns error code in case of failure else 0(on success)
3019 */
3020static int ab8500_charger_usb_get_property(struct power_supply *psy,
3021 enum power_supply_property psp,
3022 union power_supply_propval *val)
3023{
3024 struct ab8500_charger *di;
Jonas Aaberga864c5a2013-01-11 13:12:53 +00003025 int ret;
Arun Murthy84edbee2012-02-29 21:54:26 +05303026
3027 di = to_ab8500_charger_usb_device_info(psy_to_ux500_charger(psy));
3028
3029 switch (psp) {
3030 case POWER_SUPPLY_PROP_HEALTH:
3031 if (di->flags.usbchargernotok)
3032 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
3033 else if (di->ac.wd_expired || di->usb.wd_expired)
3034 val->intval = POWER_SUPPLY_HEALTH_DEAD;
3035 else if (di->flags.usb_thermal_prot)
3036 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
3037 else if (di->flags.vbus_ovv)
3038 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
3039 else
3040 val->intval = POWER_SUPPLY_HEALTH_GOOD;
3041 break;
3042 case POWER_SUPPLY_PROP_ONLINE:
3043 val->intval = di->usb.charger_online;
3044 break;
3045 case POWER_SUPPLY_PROP_PRESENT:
3046 val->intval = di->usb.charger_connected;
3047 break;
3048 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Jonas Aaberga864c5a2013-01-11 13:12:53 +00003049 ret = ab8500_charger_get_vbus_voltage(di);
3050 if (ret >= 0)
3051 di->usb.charger_voltage = ret;
Arun Murthy84edbee2012-02-29 21:54:26 +05303052 val->intval = di->usb.charger_voltage * 1000;
3053 break;
3054 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
3055 /*
3056 * This property is used to indicate when CV mode is entered
3057 * for the USB charger
3058 */
3059 di->usb.cv_active = ab8500_charger_usb_cv(di);
3060 val->intval = di->usb.cv_active;
3061 break;
3062 case POWER_SUPPLY_PROP_CURRENT_NOW:
Jonas Aaberga864c5a2013-01-11 13:12:53 +00003063 ret = ab8500_charger_get_usb_current(di);
3064 if (ret >= 0)
3065 di->usb.charger_current = ret;
3066 val->intval = di->usb.charger_current * 1000;
Arun Murthy84edbee2012-02-29 21:54:26 +05303067 break;
3068 case POWER_SUPPLY_PROP_CURRENT_AVG:
3069 /*
3070 * This property is used to indicate when VBUS has collapsed
3071 * due to too high output current from the USB charger
3072 */
3073 if (di->flags.vbus_collapse)
3074 val->intval = 1;
3075 else
3076 val->intval = 0;
3077 break;
3078 default:
3079 return -EINVAL;
3080 }
3081 return 0;
3082}
3083
3084/**
3085 * ab8500_charger_init_hw_registers() - Set up charger related registers
3086 * @di: pointer to the ab8500_charger structure
3087 *
3088 * Set up charger OVV, watchdog and maximum voltage registers as well as
3089 * charging of the backup battery
3090 */
3091static int ab8500_charger_init_hw_registers(struct ab8500_charger *di)
3092{
3093 int ret = 0;
Yang QU0f4aa402012-06-26 19:25:52 +08003094 u8 bup_vch_range = 0, vbup33_vrtcn = 0;
Arun Murthy84edbee2012-02-29 21:54:26 +05303095
3096 /* Setup maximum charger current and voltage for ABB cut2.0 */
3097 if (!is_ab8500_1p1_or_earlier(di->parent)) {
3098 ret = abx500_set_register_interruptible(di->dev,
3099 AB8500_CHARGER,
3100 AB8500_CH_VOLT_LVL_MAX_REG, CH_VOL_LVL_4P6);
3101 if (ret) {
3102 dev_err(di->dev,
3103 "failed to set CH_VOLT_LVL_MAX_REG\n");
3104 goto out;
3105 }
3106
Lee Jones861a30d2012-08-29 20:36:51 +08003107 if (is_ab8540(di->parent))
3108 ret = abx500_set_register_interruptible(di->dev,
3109 AB8500_CHARGER, AB8500_CH_OPT_CRNTLVL_MAX_REG,
3110 CH_OP_CUR_LVL_2P);
3111 else
3112 ret = abx500_set_register_interruptible(di->dev,
3113 AB8500_CHARGER, AB8500_CH_OPT_CRNTLVL_MAX_REG,
3114 CH_OP_CUR_LVL_1P6);
Arun Murthy84edbee2012-02-29 21:54:26 +05303115 if (ret) {
3116 dev_err(di->dev,
3117 "failed to set CH_OPT_CRNTLVL_MAX_REG\n");
3118 goto out;
3119 }
3120 }
3121
Lee Jones861a30d2012-08-29 20:36:51 +08003122 if (is_ab9540_2p0(di->parent) || is_ab9540_3p0(di->parent)
3123 || is_ab8505_2p0(di->parent) || is_ab8540(di->parent))
Jonas Aaberg0ed51072012-05-11 12:42:25 +02003124 ret = abx500_mask_and_set_register_interruptible(di->dev,
3125 AB8500_CHARGER,
3126 AB8500_USBCH_CTRL2_REG,
3127 VBUS_AUTO_IN_CURR_LIM_ENA,
3128 VBUS_AUTO_IN_CURR_LIM_ENA);
3129 else
3130 /*
3131 * VBUS OVV set to 6.3V and enable automatic current limitation
3132 */
3133 ret = abx500_set_register_interruptible(di->dev,
3134 AB8500_CHARGER,
3135 AB8500_USBCH_CTRL2_REG,
3136 VBUS_OVV_SELECT_6P3V | VBUS_AUTO_IN_CURR_LIM_ENA);
Arun Murthy84edbee2012-02-29 21:54:26 +05303137 if (ret) {
Jonas Aaberg0ed51072012-05-11 12:42:25 +02003138 dev_err(di->dev,
3139 "failed to set automatic current limitation\n");
Arun Murthy84edbee2012-02-29 21:54:26 +05303140 goto out;
3141 }
3142
3143 /* Enable main watchdog in OTP */
3144 ret = abx500_set_register_interruptible(di->dev,
3145 AB8500_OTP_EMUL, AB8500_OTP_CONF_15, OTP_ENABLE_WD);
3146 if (ret) {
3147 dev_err(di->dev, "failed to enable main WD in OTP\n");
3148 goto out;
3149 }
3150
3151 /* Enable main watchdog */
3152 ret = abx500_set_register_interruptible(di->dev,
3153 AB8500_SYS_CTRL2_BLOCK,
3154 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_ENA);
3155 if (ret) {
3156 dev_err(di->dev, "faile to enable main watchdog\n");
3157 goto out;
3158 }
3159
3160 /*
3161 * Due to internal synchronisation, Enable and Kick watchdog bits
3162 * cannot be enabled in a single write.
3163 * A minimum delay of 2*32 kHz period (62.5µs) must be inserted
3164 * between writing Enable then Kick bits.
3165 */
3166 udelay(63);
3167
3168 /* Kick main watchdog */
3169 ret = abx500_set_register_interruptible(di->dev,
3170 AB8500_SYS_CTRL2_BLOCK,
3171 AB8500_MAIN_WDOG_CTRL_REG,
3172 (MAIN_WDOG_ENA | MAIN_WDOG_KICK));
3173 if (ret) {
3174 dev_err(di->dev, "failed to kick main watchdog\n");
3175 goto out;
3176 }
3177
3178 /* Disable main watchdog */
3179 ret = abx500_set_register_interruptible(di->dev,
3180 AB8500_SYS_CTRL2_BLOCK,
3181 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_DIS);
3182 if (ret) {
3183 dev_err(di->dev, "failed to disable main watchdog\n");
3184 goto out;
3185 }
3186
3187 /* Set watchdog timeout */
3188 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
3189 AB8500_CH_WD_TIMER_REG, WD_TIMER);
3190 if (ret) {
3191 dev_err(di->dev, "failed to set charger watchdog timeout\n");
3192 goto out;
3193 }
3194
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003195 ret = ab8500_charger_led_en(di, false);
3196 if (ret < 0) {
3197 dev_err(di->dev, "failed to disable LED\n");
3198 goto out;
3199 }
3200
Arun Murthy84edbee2012-02-29 21:54:26 +05303201 /* Backup battery voltage and current */
Yang QU0f4aa402012-06-26 19:25:52 +08003202 if (di->bm->bkup_bat_v > BUP_VCH_SEL_3P1V)
3203 bup_vch_range = BUP_VCH_RANGE;
3204 if (di->bm->bkup_bat_v == BUP_VCH_SEL_3P3V)
3205 vbup33_vrtcn = VBUP33_VRTCN;
3206
Arun Murthy84edbee2012-02-29 21:54:26 +05303207 ret = abx500_set_register_interruptible(di->dev,
3208 AB8500_RTC,
3209 AB8500_RTC_BACKUP_CHG_REG,
Yang QU0f4aa402012-06-26 19:25:52 +08003210 (di->bm->bkup_bat_v & 0x3) | di->bm->bkup_bat_i);
Arun Murthy84edbee2012-02-29 21:54:26 +05303211 if (ret) {
3212 dev_err(di->dev, "failed to setup backup battery charging\n");
3213 goto out;
3214 }
Yang QU0f4aa402012-06-26 19:25:52 +08003215 if (is_ab8540(di->parent)) {
3216 ret = abx500_set_register_interruptible(di->dev,
3217 AB8500_RTC,
3218 AB8500_RTC_CTRL1_REG,
3219 bup_vch_range | vbup33_vrtcn);
3220 if (ret) {
Lee Jones861a30d2012-08-29 20:36:51 +08003221 dev_err(di->dev,
3222 "failed to setup backup battery charging\n");
Yang QU0f4aa402012-06-26 19:25:52 +08003223 goto out;
3224 }
3225 }
Arun Murthy84edbee2012-02-29 21:54:26 +05303226
3227 /* Enable backup battery charging */
3228 abx500_mask_and_set_register_interruptible(di->dev,
3229 AB8500_RTC, AB8500_RTC_CTRL_REG,
3230 RTC_BUP_CH_ENA, RTC_BUP_CH_ENA);
3231 if (ret < 0)
3232 dev_err(di->dev, "%s mask and set failed\n", __func__);
3233
Lee Jonesdb43e6c2013-02-14 12:39:15 +00003234 if (is_ab8540(di->parent)) {
3235 ret = abx500_mask_and_set_register_interruptible(di->dev,
3236 AB8500_CHARGER, AB8540_USB_PP_MODE_REG,
3237 BUS_VSYS_VOL_SELECT_MASK, BUS_VSYS_VOL_SELECT_3P6V);
3238 if (ret) {
Lee Jones861a30d2012-08-29 20:36:51 +08003239 dev_err(di->dev,
3240 "failed to setup usb power path vsys voltage\n");
Lee Jonesdb43e6c2013-02-14 12:39:15 +00003241 goto out;
3242 }
3243 ret = abx500_mask_and_set_register_interruptible(di->dev,
3244 AB8500_CHARGER, AB8540_USB_PP_CHR_REG,
3245 BUS_PP_PRECHG_CURRENT_MASK, 0);
3246 if (ret) {
Lee Jones861a30d2012-08-29 20:36:51 +08003247 dev_err(di->dev,
3248 "failed to setup usb power path prechage current\n");
Lee Jonesdb43e6c2013-02-14 12:39:15 +00003249 goto out;
3250 }
3251 }
3252
Arun Murthy84edbee2012-02-29 21:54:26 +05303253out:
3254 return ret;
3255}
3256
3257/*
3258 * ab8500 charger driver interrupts and their respective isr
3259 */
3260static struct ab8500_charger_interrupts ab8500_charger_irq[] = {
3261 {"MAIN_CH_UNPLUG_DET", ab8500_charger_mainchunplugdet_handler},
3262 {"MAIN_CHARGE_PLUG_DET", ab8500_charger_mainchplugdet_handler},
3263 {"MAIN_EXT_CH_NOT_OK", ab8500_charger_mainextchnotok_handler},
3264 {"MAIN_CH_TH_PROT_R", ab8500_charger_mainchthprotr_handler},
3265 {"MAIN_CH_TH_PROT_F", ab8500_charger_mainchthprotf_handler},
3266 {"VBUS_DET_F", ab8500_charger_vbusdetf_handler},
3267 {"VBUS_DET_R", ab8500_charger_vbusdetr_handler},
3268 {"USB_LINK_STATUS", ab8500_charger_usblinkstatus_handler},
3269 {"USB_CH_TH_PROT_R", ab8500_charger_usbchthprotr_handler},
3270 {"USB_CH_TH_PROT_F", ab8500_charger_usbchthprotf_handler},
3271 {"USB_CHARGER_NOT_OKR", ab8500_charger_usbchargernotokr_handler},
3272 {"VBUS_OVV", ab8500_charger_vbusovv_handler},
3273 {"CH_WD_EXP", ab8500_charger_chwdexp_handler},
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003274 {"VBUS_CH_DROP_END", ab8500_charger_vbuschdropend_handler},
Arun Murthy84edbee2012-02-29 21:54:26 +05303275};
3276
3277static int ab8500_charger_usb_notifier_call(struct notifier_block *nb,
3278 unsigned long event, void *power)
3279{
3280 struct ab8500_charger *di =
3281 container_of(nb, struct ab8500_charger, nb);
3282 enum ab8500_usb_state bm_usb_state;
3283 unsigned mA = *((unsigned *)power);
3284
Lee Jonesc9ade0f2013-01-21 11:22:56 +00003285 if (!di)
3286 return NOTIFY_DONE;
3287
Arun Murthy84edbee2012-02-29 21:54:26 +05303288 if (event != USB_EVENT_VBUS) {
3289 dev_dbg(di->dev, "not a standard host, returning\n");
3290 return NOTIFY_DONE;
3291 }
3292
3293 /* TODO: State is fabricate here. See if charger really needs USB
3294 * state or if mA is enough
3295 */
3296 if ((di->usb_state.usb_current == 2) && (mA > 2))
3297 bm_usb_state = AB8500_BM_USB_STATE_RESUME;
3298 else if (mA == 0)
3299 bm_usb_state = AB8500_BM_USB_STATE_RESET_HS;
3300 else if (mA == 2)
3301 bm_usb_state = AB8500_BM_USB_STATE_SUSPEND;
3302 else if (mA >= 8) /* 8, 100, 500 */
3303 bm_usb_state = AB8500_BM_USB_STATE_CONFIGURED;
3304 else /* Should never occur */
3305 bm_usb_state = AB8500_BM_USB_STATE_RESET_FS;
3306
3307 dev_dbg(di->dev, "%s usb_state: 0x%02x mA: %d\n",
3308 __func__, bm_usb_state, mA);
3309
3310 spin_lock(&di->usb_state.usb_lock);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003311 di->usb_state.state_tmp = bm_usb_state;
3312 di->usb_state.usb_current_tmp = mA;
Arun Murthy84edbee2012-02-29 21:54:26 +05303313 spin_unlock(&di->usb_state.usb_lock);
3314
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003315 /*
3316 * wait for some time until you get updates from the usb stack
3317 * and negotiations are completed
3318 */
3319 queue_delayed_work(di->charger_wq, &di->usb_state_changed_work, HZ/2);
Arun Murthy84edbee2012-02-29 21:54:26 +05303320
3321 return NOTIFY_OK;
3322}
3323
3324#if defined(CONFIG_PM)
3325static int ab8500_charger_resume(struct platform_device *pdev)
3326{
3327 int ret;
3328 struct ab8500_charger *di = platform_get_drvdata(pdev);
3329
3330 /*
3331 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
3332 * logic. That means we have to continously kick the charger
3333 * watchdog even when no charger is connected. This is only
3334 * valid once the AC charger has been enabled. This is
3335 * a bug that is not handled by the algorithm and the
3336 * watchdog have to be kicked by the charger driver
3337 * when the AC charger is disabled
3338 */
3339 if (di->ac_conn && is_ab8500_1p1_or_earlier(di->parent)) {
3340 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
3341 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
3342 if (ret)
3343 dev_err(di->dev, "Failed to kick WD!\n");
3344
3345 /* If not already pending start a new timer */
Lee Jonesb09f86d2012-12-21 17:56:52 -08003346 queue_delayed_work(di->charger_wq, &di->kick_wd_work,
3347 round_jiffies(WD_KICK_INTERVAL));
Arun Murthy84edbee2012-02-29 21:54:26 +05303348 }
3349
3350 /* If we still have a HW failure, schedule a new check */
3351 if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
3352 queue_delayed_work(di->charger_wq,
3353 &di->check_hw_failure_work, 0);
3354 }
3355
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003356 if (di->flags.vbus_drop_end)
3357 queue_delayed_work(di->charger_wq, &di->vbus_drop_end_work, 0);
3358
Arun Murthy84edbee2012-02-29 21:54:26 +05303359 return 0;
3360}
3361
3362static int ab8500_charger_suspend(struct platform_device *pdev,
3363 pm_message_t state)
3364{
3365 struct ab8500_charger *di = platform_get_drvdata(pdev);
3366
Lee Jonesb09f86d2012-12-21 17:56:52 -08003367 /* Cancel any pending jobs */
3368 cancel_delayed_work(&di->check_hw_failure_work);
3369 cancel_delayed_work(&di->vbus_drop_end_work);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003370
Jonas Aaberg53ef1f52012-05-21 16:05:01 +02003371 flush_delayed_work(&di->attach_work);
3372 flush_delayed_work(&di->usb_charger_attached_work);
3373 flush_delayed_work(&di->ac_charger_attached_work);
3374 flush_delayed_work(&di->check_usbchgnotok_work);
3375 flush_delayed_work(&di->check_vbat_work);
3376 flush_delayed_work(&di->kick_wd_work);
3377
3378 flush_work(&di->usb_link_status_work);
3379 flush_work(&di->ac_work);
3380 flush_work(&di->detect_usb_type_work);
3381
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003382 if (atomic_read(&di->current_stepping_sessions))
3383 return -EAGAIN;
3384
Arun Murthy84edbee2012-02-29 21:54:26 +05303385 return 0;
3386}
3387#else
3388#define ab8500_charger_suspend NULL
3389#define ab8500_charger_resume NULL
3390#endif
3391
Lee Jones88917162013-02-13 11:39:19 +00003392static struct notifier_block charger_nb = {
3393 .notifier_call = ab8500_external_charger_prepare,
3394};
3395
Bill Pemberton415ec692012-11-19 13:26:07 -05003396static int ab8500_charger_remove(struct platform_device *pdev)
Arun Murthy84edbee2012-02-29 21:54:26 +05303397{
3398 struct ab8500_charger *di = platform_get_drvdata(pdev);
3399 int i, irq, ret;
3400
3401 /* Disable AC charging */
3402 ab8500_charger_ac_en(&di->ac_chg, false, 0, 0);
3403
3404 /* Disable USB charging */
3405 ab8500_charger_usb_en(&di->usb_chg, false, 0, 0);
3406
3407 /* Disable interrupts */
3408 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
3409 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3410 free_irq(irq, di);
3411 }
3412
Arun Murthy84edbee2012-02-29 21:54:26 +05303413 /* Backup battery voltage and current disable */
3414 ret = abx500_mask_and_set_register_interruptible(di->dev,
3415 AB8500_RTC, AB8500_RTC_CTRL_REG, RTC_BUP_CH_ENA, 0);
3416 if (ret < 0)
3417 dev_err(di->dev, "%s mask and set failed\n", __func__);
3418
Anton Vorontsovefd71c82012-03-14 04:22:17 +04003419 usb_unregister_notifier(di->usb_phy, &di->nb);
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +05303420 usb_put_phy(di->usb_phy);
Arun Murthy84edbee2012-02-29 21:54:26 +05303421
3422 /* Delete the work queue */
3423 destroy_workqueue(di->charger_wq);
3424
Lee Jones88917162013-02-13 11:39:19 +00003425 /* Unregister external charger enable notifier */
3426 if (!di->ac_chg.enabled)
3427 blocking_notifier_chain_unregister(
3428 &charger_notifier_list, &charger_nb);
3429
Arun Murthy84edbee2012-02-29 21:54:26 +05303430 flush_scheduled_work();
Lee Jones72a90dd2013-02-14 09:22:48 +00003431 if (di->usb_chg.enabled)
Michel JAOUEN01ec8c52012-04-26 10:00:04 +02003432 power_supply_unregister(&di->usb_chg.psy);
Marcus Cooper405fea12012-08-10 12:59:06 +02003433
3434 if (di->ac_chg.enabled && !di->ac_chg.external)
Michel JAOUEN01ec8c52012-04-26 10:00:04 +02003435 power_supply_unregister(&di->ac_chg.psy);
Marcus Cooper405fea12012-08-10 12:59:06 +02003436
Arun Murthy84edbee2012-02-29 21:54:26 +05303437 return 0;
3438}
3439
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08003440static char *supply_interface[] = {
3441 "ab8500_chargalg",
3442 "ab8500_fg",
3443 "ab8500_btemp",
3444};
3445
Bill Pembertonc8afa642012-11-19 13:22:23 -05003446static int ab8500_charger_probe(struct platform_device *pdev)
Arun Murthy84edbee2012-02-29 21:54:26 +05303447{
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08003448 struct device_node *np = pdev->dev.of_node;
Lee Jones7722b792012-11-30 10:56:28 +00003449 struct abx500_bm_data *plat = pdev->dev.platform_data;
Lee Jones2aac3de2012-05-05 04:38:19 -07003450 struct ab8500_charger *di;
Lee Jonesb269fff2013-01-11 13:12:51 +00003451 int irq, i, charger_status, ret = 0, ch_stat;
Arun Murthy84edbee2012-02-29 21:54:26 +05303452
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08003453 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
3454 if (!di) {
3455 dev_err(&pdev->dev, "%s no mem for ab8500_charger\n", __func__);
Arun Murthy84edbee2012-02-29 21:54:26 +05303456 return -ENOMEM;
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08003457 }
Lee Jones7722b792012-11-30 10:56:28 +00003458
3459 if (!plat) {
3460 dev_err(&pdev->dev, "no battery management data supplied\n");
3461 return -EINVAL;
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08003462 }
Lee Jones7722b792012-11-30 10:56:28 +00003463 di->bm = plat;
3464
3465 if (np) {
3466 ret = ab8500_bm_of_probe(&pdev->dev, np, di->bm);
3467 if (ret) {
3468 dev_err(&pdev->dev, "failed to get battery information\n");
3469 return ret;
3470 }
3471 di->autopower_cfg = of_property_read_bool(np, "autopower_cfg");
3472 } else
3473 di->autopower_cfg = false;
Arun Murthy84edbee2012-02-29 21:54:26 +05303474
3475 /* get parent data */
3476 di->dev = &pdev->dev;
3477 di->parent = dev_get_drvdata(pdev->dev.parent);
3478 di->gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
3479
3480 /* initialize lock */
3481 spin_lock_init(&di->usb_state.usb_lock);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003482 mutex_init(&di->usb_ipt_crnt_lock);
Arun Murthy84edbee2012-02-29 21:54:26 +05303483
Arun Murthy84edbee2012-02-29 21:54:26 +05303484 di->autopower = false;
Henrik Sölverff380902012-04-17 15:51:01 +02003485 di->invalid_charger_detect_state = 0;
Arun Murthy84edbee2012-02-29 21:54:26 +05303486
3487 /* AC supply */
3488 /* power_supply base class */
3489 di->ac_chg.psy.name = "ab8500_ac";
3490 di->ac_chg.psy.type = POWER_SUPPLY_TYPE_MAINS;
3491 di->ac_chg.psy.properties = ab8500_charger_ac_props;
3492 di->ac_chg.psy.num_properties = ARRAY_SIZE(ab8500_charger_ac_props);
3493 di->ac_chg.psy.get_property = ab8500_charger_ac_get_property;
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08003494 di->ac_chg.psy.supplied_to = supply_interface;
3495 di->ac_chg.psy.num_supplicants = ARRAY_SIZE(supply_interface),
Arun Murthy84edbee2012-02-29 21:54:26 +05303496 /* ux500_charger sub-class */
3497 di->ac_chg.ops.enable = &ab8500_charger_ac_en;
Lee Jones4dcdf572013-02-14 09:24:10 +00003498 di->ac_chg.ops.check_enable = &ab8500_charger_ac_check_enable;
Arun Murthy84edbee2012-02-29 21:54:26 +05303499 di->ac_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
3500 di->ac_chg.ops.update_curr = &ab8500_charger_update_charger_current;
3501 di->ac_chg.max_out_volt = ab8500_charger_voltage_map[
3502 ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
Lee Jones861a30d2012-08-29 20:36:51 +08003503 di->ac_chg.max_out_curr =
3504 di->bm->chg_output_curr[di->bm->n_chg_out_curr - 1];
Loic Pallardye07a5642012-05-10 15:33:56 +02003505 di->ac_chg.wdt_refresh = CHG_WD_INTERVAL;
Lee Jones97034a12013-01-17 16:08:42 +00003506 di->ac_chg.enabled = di->bm->ac_enabled;
Loic Pallardye07a5642012-05-10 15:33:56 +02003507 di->ac_chg.external = false;
Arun Murthy84edbee2012-02-29 21:54:26 +05303508
Lee Jones88917162013-02-13 11:39:19 +00003509 /*notifier for external charger enabling*/
3510 if (!di->ac_chg.enabled)
3511 blocking_notifier_chain_register(
3512 &charger_notifier_list, &charger_nb);
3513
Arun Murthy84edbee2012-02-29 21:54:26 +05303514 /* USB supply */
3515 /* power_supply base class */
3516 di->usb_chg.psy.name = "ab8500_usb";
3517 di->usb_chg.psy.type = POWER_SUPPLY_TYPE_USB;
3518 di->usb_chg.psy.properties = ab8500_charger_usb_props;
3519 di->usb_chg.psy.num_properties = ARRAY_SIZE(ab8500_charger_usb_props);
3520 di->usb_chg.psy.get_property = ab8500_charger_usb_get_property;
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08003521 di->usb_chg.psy.supplied_to = supply_interface;
3522 di->usb_chg.psy.num_supplicants = ARRAY_SIZE(supply_interface),
Arun Murthy84edbee2012-02-29 21:54:26 +05303523 /* ux500_charger sub-class */
3524 di->usb_chg.ops.enable = &ab8500_charger_usb_en;
Lee Jones4dcdf572013-02-14 09:24:10 +00003525 di->usb_chg.ops.check_enable = &ab8500_charger_usb_check_enable;
Arun Murthy84edbee2012-02-29 21:54:26 +05303526 di->usb_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
3527 di->usb_chg.ops.update_curr = &ab8500_charger_update_charger_current;
Lee Jonesdb43e6c2013-02-14 12:39:15 +00003528 di->usb_chg.ops.pp_enable = &ab8540_charger_power_path_enable;
3529 di->usb_chg.ops.pre_chg_enable = &ab8540_charger_usb_pre_chg_enable;
Arun Murthy84edbee2012-02-29 21:54:26 +05303530 di->usb_chg.max_out_volt = ab8500_charger_voltage_map[
3531 ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
Lee Jones861a30d2012-08-29 20:36:51 +08003532 di->usb_chg.max_out_curr =
3533 di->bm->chg_output_curr[di->bm->n_chg_out_curr - 1];
Loic Pallardye07a5642012-05-10 15:33:56 +02003534 di->usb_chg.wdt_refresh = CHG_WD_INTERVAL;
Lee Jones97034a12013-01-17 16:08:42 +00003535 di->usb_chg.enabled = di->bm->usb_enabled;
Loic Pallardye07a5642012-05-10 15:33:56 +02003536 di->usb_chg.external = false;
Lee Jonesdb43e6c2013-02-14 12:39:15 +00003537 di->usb_chg.power_path = di->bm->usb_power_path;
Lee Jonesf7470b52013-02-14 12:37:29 +00003538 di->usb_state.usb_current = -1;
Arun Murthy84edbee2012-02-29 21:54:26 +05303539
3540 /* Create a work queue for the charger */
3541 di->charger_wq =
3542 create_singlethread_workqueue("ab8500_charger_wq");
3543 if (di->charger_wq == NULL) {
3544 dev_err(di->dev, "failed to create work queue\n");
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08003545 return -ENOMEM;
Arun Murthy84edbee2012-02-29 21:54:26 +05303546 }
3547
Lee Jonesb269fff2013-01-11 13:12:51 +00003548 mutex_init(&di->charger_attached_mutex);
3549
Arun Murthy84edbee2012-02-29 21:54:26 +05303550 /* Init work for HW failure check */
Tejun Heo203b42f2012-08-21 13:18:23 -07003551 INIT_DEFERRABLE_WORK(&di->check_hw_failure_work,
Arun Murthy84edbee2012-02-29 21:54:26 +05303552 ab8500_charger_check_hw_failure_work);
Tejun Heo203b42f2012-08-21 13:18:23 -07003553 INIT_DEFERRABLE_WORK(&di->check_usbchgnotok_work,
Arun Murthy84edbee2012-02-29 21:54:26 +05303554 ab8500_charger_check_usbchargernotok_work);
3555
Lee Jonesb269fff2013-01-11 13:12:51 +00003556 INIT_DELAYED_WORK(&di->ac_charger_attached_work,
3557 ab8500_charger_ac_attached_work);
3558 INIT_DELAYED_WORK(&di->usb_charger_attached_work,
3559 ab8500_charger_usb_attached_work);
3560
Arun Murthy84edbee2012-02-29 21:54:26 +05303561 /*
3562 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
3563 * logic. That means we have to continously kick the charger
3564 * watchdog even when no charger is connected. This is only
3565 * valid once the AC charger has been enabled. This is
3566 * a bug that is not handled by the algorithm and the
3567 * watchdog have to be kicked by the charger driver
3568 * when the AC charger is disabled
3569 */
Tejun Heo203b42f2012-08-21 13:18:23 -07003570 INIT_DEFERRABLE_WORK(&di->kick_wd_work,
Arun Murthy84edbee2012-02-29 21:54:26 +05303571 ab8500_charger_kick_watchdog_work);
3572
Tejun Heo203b42f2012-08-21 13:18:23 -07003573 INIT_DEFERRABLE_WORK(&di->check_vbat_work,
Arun Murthy84edbee2012-02-29 21:54:26 +05303574 ab8500_charger_check_vbat_work);
3575
Marcus Cooper4b45f4a2013-01-11 13:13:04 +00003576 INIT_DELAYED_WORK(&di->attach_work,
3577 ab8500_charger_usb_link_attach_work);
3578
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003579 INIT_DELAYED_WORK(&di->usb_state_changed_work,
3580 ab8500_charger_usb_state_changed_work);
3581
3582 INIT_DELAYED_WORK(&di->vbus_drop_end_work,
3583 ab8500_charger_vbus_drop_end_work);
3584
Arun Murthy84edbee2012-02-29 21:54:26 +05303585 /* Init work for charger detection */
3586 INIT_WORK(&di->usb_link_status_work,
3587 ab8500_charger_usb_link_status_work);
3588 INIT_WORK(&di->ac_work, ab8500_charger_ac_work);
3589 INIT_WORK(&di->detect_usb_type_work,
3590 ab8500_charger_detect_usb_type_work);
3591
Arun Murthy84edbee2012-02-29 21:54:26 +05303592 /* Init work for checking HW status */
3593 INIT_WORK(&di->check_main_thermal_prot_work,
3594 ab8500_charger_check_main_thermal_prot_work);
3595 INIT_WORK(&di->check_usb_thermal_prot_work,
3596 ab8500_charger_check_usb_thermal_prot_work);
3597
3598 /*
3599 * VDD ADC supply needs to be enabled from this driver when there
3600 * is a charger connected to avoid erroneous BTEMP_HIGH/LOW
3601 * interrupts during charging
3602 */
Sachin Kamat8feffd12012-12-07 17:23:28 +05303603 di->regu = devm_regulator_get(di->dev, "vddadc");
Arun Murthy84edbee2012-02-29 21:54:26 +05303604 if (IS_ERR(di->regu)) {
3605 ret = PTR_ERR(di->regu);
3606 dev_err(di->dev, "failed to get vddadc regulator\n");
3607 goto free_charger_wq;
3608 }
3609
3610
3611 /* Initialize OVV, and other registers */
3612 ret = ab8500_charger_init_hw_registers(di);
3613 if (ret) {
3614 dev_err(di->dev, "failed to initialize ABB registers\n");
Sachin Kamat8feffd12012-12-07 17:23:28 +05303615 goto free_charger_wq;
Arun Murthy84edbee2012-02-29 21:54:26 +05303616 }
3617
3618 /* Register AC charger class */
Lee Jones72a90dd2013-02-14 09:22:48 +00003619 if (di->ac_chg.enabled) {
Michel JAOUEN01ec8c52012-04-26 10:00:04 +02003620 ret = power_supply_register(di->dev, &di->ac_chg.psy);
3621 if (ret) {
3622 dev_err(di->dev, "failed to register AC charger\n");
3623 goto free_charger_wq;
3624 }
Arun Murthy84edbee2012-02-29 21:54:26 +05303625 }
3626
3627 /* Register USB charger class */
Lee Jones72a90dd2013-02-14 09:22:48 +00003628 if (di->usb_chg.enabled) {
Michel JAOUEN01ec8c52012-04-26 10:00:04 +02003629 ret = power_supply_register(di->dev, &di->usb_chg.psy);
3630 if (ret) {
3631 dev_err(di->dev, "failed to register USB charger\n");
3632 goto free_ac;
3633 }
Arun Murthy84edbee2012-02-29 21:54:26 +05303634 }
3635
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +05303636 di->usb_phy = usb_get_phy(USB_PHY_TYPE_USB2);
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +05303637 if (IS_ERR_OR_NULL(di->usb_phy)) {
Anton Vorontsovefd71c82012-03-14 04:22:17 +04003638 dev_err(di->dev, "failed to get usb transceiver\n");
Arun Murthy84edbee2012-02-29 21:54:26 +05303639 ret = -EINVAL;
3640 goto free_usb;
3641 }
3642 di->nb.notifier_call = ab8500_charger_usb_notifier_call;
Anton Vorontsovefd71c82012-03-14 04:22:17 +04003643 ret = usb_register_notifier(di->usb_phy, &di->nb);
Arun Murthy84edbee2012-02-29 21:54:26 +05303644 if (ret) {
Anton Vorontsovefd71c82012-03-14 04:22:17 +04003645 dev_err(di->dev, "failed to register usb notifier\n");
3646 goto put_usb_phy;
Arun Murthy84edbee2012-02-29 21:54:26 +05303647 }
3648
3649 /* Identify the connected charger types during startup */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003650 charger_status = ab8500_charger_detect_chargers(di, true);
Arun Murthy84edbee2012-02-29 21:54:26 +05303651 if (charger_status & AC_PW_CONN) {
3652 di->ac.charger_connected = 1;
3653 di->ac_conn = true;
3654 ab8500_power_supply_changed(di, &di->ac_chg.psy);
3655 sysfs_notify(&di->ac_chg.psy.dev->kobj, NULL, "present");
3656 }
3657
3658 if (charger_status & USB_PW_CONN) {
Arun Murthy84edbee2012-02-29 21:54:26 +05303659 di->vbus_detected = true;
3660 di->vbus_detected_start = true;
3661 queue_work(di->charger_wq,
3662 &di->detect_usb_type_work);
3663 }
3664
3665 /* Register interrupts */
3666 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
3667 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3668 ret = request_threaded_irq(irq, NULL, ab8500_charger_irq[i].isr,
3669 IRQF_SHARED | IRQF_NO_SUSPEND,
3670 ab8500_charger_irq[i].name, di);
3671
3672 if (ret != 0) {
3673 dev_err(di->dev, "failed to request %s IRQ %d: %d\n"
3674 , ab8500_charger_irq[i].name, irq, ret);
3675 goto free_irq;
3676 }
3677 dev_dbg(di->dev, "Requested %s IRQ %d: %d\n",
3678 ab8500_charger_irq[i].name, irq, ret);
3679 }
3680
3681 platform_set_drvdata(pdev, di);
3682
Lee Jonesb269fff2013-01-11 13:12:51 +00003683 mutex_lock(&di->charger_attached_mutex);
3684
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003685 ch_stat = ab8500_charger_detect_chargers(di, false);
Lee Jonesb269fff2013-01-11 13:12:51 +00003686
3687 if ((ch_stat & AC_PW_CONN) == AC_PW_CONN) {
Rabin Vincent261c5132012-09-26 13:36:38 +02003688 if (is_ab8500(di->parent))
3689 queue_delayed_work(di->charger_wq,
3690 &di->ac_charger_attached_work,
3691 HZ);
Lee Jonesb269fff2013-01-11 13:12:51 +00003692 }
3693 if ((ch_stat & USB_PW_CONN) == USB_PW_CONN) {
Rabin Vincent261c5132012-09-26 13:36:38 +02003694 if (is_ab8500(di->parent))
3695 queue_delayed_work(di->charger_wq,
3696 &di->usb_charger_attached_work,
3697 HZ);
Lee Jonesb269fff2013-01-11 13:12:51 +00003698 }
3699
3700 mutex_unlock(&di->charger_attached_mutex);
3701
Arun Murthy84edbee2012-02-29 21:54:26 +05303702 return ret;
3703
3704free_irq:
Anton Vorontsovefd71c82012-03-14 04:22:17 +04003705 usb_unregister_notifier(di->usb_phy, &di->nb);
Arun Murthy84edbee2012-02-29 21:54:26 +05303706
3707 /* We also have to free all successfully registered irqs */
3708 for (i = i - 1; i >= 0; i--) {
3709 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3710 free_irq(irq, di);
3711 }
Anton Vorontsovefd71c82012-03-14 04:22:17 +04003712put_usb_phy:
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +05303713 usb_put_phy(di->usb_phy);
Arun Murthy84edbee2012-02-29 21:54:26 +05303714free_usb:
Lee Jones72a90dd2013-02-14 09:22:48 +00003715 if (di->usb_chg.enabled)
Michel JAOUEN01ec8c52012-04-26 10:00:04 +02003716 power_supply_unregister(&di->usb_chg.psy);
Arun Murthy84edbee2012-02-29 21:54:26 +05303717free_ac:
Lee Jones72a90dd2013-02-14 09:22:48 +00003718 if (di->ac_chg.enabled)
Michel JAOUEN01ec8c52012-04-26 10:00:04 +02003719 power_supply_unregister(&di->ac_chg.psy);
Arun Murthy84edbee2012-02-29 21:54:26 +05303720free_charger_wq:
3721 destroy_workqueue(di->charger_wq);
Arun Murthy84edbee2012-02-29 21:54:26 +05303722 return ret;
3723}
3724
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08003725static const struct of_device_id ab8500_charger_match[] = {
3726 { .compatible = "stericsson,ab8500-charger", },
3727 { },
3728};
3729
Arun Murthy84edbee2012-02-29 21:54:26 +05303730static struct platform_driver ab8500_charger_driver = {
3731 .probe = ab8500_charger_probe,
Bill Pemberton28ea73f2012-11-19 13:20:40 -05003732 .remove = ab8500_charger_remove,
Arun Murthy84edbee2012-02-29 21:54:26 +05303733 .suspend = ab8500_charger_suspend,
3734 .resume = ab8500_charger_resume,
3735 .driver = {
3736 .name = "ab8500-charger",
3737 .owner = THIS_MODULE,
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08003738 .of_match_table = ab8500_charger_match,
Arun Murthy84edbee2012-02-29 21:54:26 +05303739 },
3740};
3741
3742static int __init ab8500_charger_init(void)
3743{
3744 return platform_driver_register(&ab8500_charger_driver);
3745}
3746
3747static void __exit ab8500_charger_exit(void)
3748{
3749 platform_driver_unregister(&ab8500_charger_driver);
3750}
3751
3752subsys_initcall_sync(ab8500_charger_init);
3753module_exit(ab8500_charger_exit);
3754
3755MODULE_LICENSE("GPL v2");
3756MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy");
3757MODULE_ALIAS("platform:ab8500-charger");
3758MODULE_DESCRIPTION("AB8500 charger management driver");