blob: 64accb235d2ccdbc8a1167ef2e3383bffc17dbd7 [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
444 queue_delayed_work(di->charger_wq,
445 &di->usb_charger_attached_work,
446 HZ);
447 } else {
448 cancel_delayed_work_sync(&di->usb_charger_attached_work);
449 mutex_lock(&di->charger_attached_mutex);
450 mutex_unlock(&di->charger_attached_mutex);
451 }
Arun Murthy84edbee2012-02-29 21:54:26 +0530452 }
453}
454
455/**
456 * ab8500_charger_get_ac_voltage() - get ac charger voltage
457 * @di: pointer to the ab8500_charger structure
458 *
459 * Returns ac charger voltage (on success)
460 */
461static int ab8500_charger_get_ac_voltage(struct ab8500_charger *di)
462{
463 int vch;
464
465 /* Only measure voltage if the charger is connected */
466 if (di->ac.charger_connected) {
467 vch = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_V);
468 if (vch < 0)
469 dev_err(di->dev, "%s gpadc conv failed,\n", __func__);
470 } else {
471 vch = 0;
472 }
473 return vch;
474}
475
476/**
477 * ab8500_charger_ac_cv() - check if the main charger is in CV mode
478 * @di: pointer to the ab8500_charger structure
479 *
480 * Returns ac charger CV mode (on success) else error code
481 */
482static int ab8500_charger_ac_cv(struct ab8500_charger *di)
483{
484 u8 val;
485 int ret = 0;
486
487 /* Only check CV mode if the charger is online */
488 if (di->ac.charger_online) {
489 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
490 AB8500_CH_STATUS1_REG, &val);
491 if (ret < 0) {
492 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
493 return 0;
494 }
495
496 if (val & MAIN_CH_CV_ON)
497 ret = 1;
498 else
499 ret = 0;
500 }
501
502 return ret;
503}
504
505/**
506 * ab8500_charger_get_vbus_voltage() - get vbus voltage
507 * @di: pointer to the ab8500_charger structure
508 *
509 * This function returns the vbus voltage.
510 * Returns vbus voltage (on success)
511 */
512static int ab8500_charger_get_vbus_voltage(struct ab8500_charger *di)
513{
514 int vch;
515
516 /* Only measure voltage if the charger is connected */
517 if (di->usb.charger_connected) {
518 vch = ab8500_gpadc_convert(di->gpadc, VBUS_V);
519 if (vch < 0)
520 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
521 } else {
522 vch = 0;
523 }
524 return vch;
525}
526
527/**
528 * ab8500_charger_get_usb_current() - get usb charger current
529 * @di: pointer to the ab8500_charger structure
530 *
531 * This function returns the usb charger current.
532 * Returns usb current (on success) and error code on failure
533 */
534static int ab8500_charger_get_usb_current(struct ab8500_charger *di)
535{
536 int ich;
537
538 /* Only measure current if the charger is online */
539 if (di->usb.charger_online) {
540 ich = ab8500_gpadc_convert(di->gpadc, USB_CHARGER_C);
541 if (ich < 0)
542 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
543 } else {
544 ich = 0;
545 }
546 return ich;
547}
548
549/**
550 * ab8500_charger_get_ac_current() - get ac charger current
551 * @di: pointer to the ab8500_charger structure
552 *
553 * This function returns the ac charger current.
554 * Returns ac current (on success) and error code on failure.
555 */
556static int ab8500_charger_get_ac_current(struct ab8500_charger *di)
557{
558 int ich;
559
560 /* Only measure current if the charger is online */
561 if (di->ac.charger_online) {
562 ich = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_C);
563 if (ich < 0)
564 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
565 } else {
566 ich = 0;
567 }
568 return ich;
569}
570
571/**
572 * ab8500_charger_usb_cv() - check if the usb charger is in CV mode
573 * @di: pointer to the ab8500_charger structure
574 *
575 * Returns ac charger CV mode (on success) else error code
576 */
577static int ab8500_charger_usb_cv(struct ab8500_charger *di)
578{
579 int ret;
580 u8 val;
581
582 /* Only check CV mode if the charger is online */
583 if (di->usb.charger_online) {
584 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
585 AB8500_CH_USBCH_STAT1_REG, &val);
586 if (ret < 0) {
587 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
588 return 0;
589 }
590
591 if (val & USB_CH_CV_ON)
592 ret = 1;
593 else
594 ret = 0;
595 } else {
596 ret = 0;
597 }
598
599 return ret;
600}
601
602/**
603 * ab8500_charger_detect_chargers() - Detect the connected chargers
604 * @di: pointer to the ab8500_charger structure
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100605 * @probe: if probe, don't delay and wait for HW
Arun Murthy84edbee2012-02-29 21:54:26 +0530606 *
607 * Returns the type of charger connected.
608 * For USB it will not mean we can actually charge from it
609 * but that there is a USB cable connected that we have to
610 * identify. This is used during startup when we don't get
611 * interrupts of the charger detection
612 *
613 * Returns an integer value, that means,
614 * NO_PW_CONN no power supply is connected
615 * AC_PW_CONN if the AC power supply is connected
616 * USB_PW_CONN if the USB power supply is connected
617 * AC_PW_CONN + USB_PW_CONN if USB and AC power supplies are both connected
618 */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100619static int ab8500_charger_detect_chargers(struct ab8500_charger *di, bool probe)
Arun Murthy84edbee2012-02-29 21:54:26 +0530620{
621 int result = NO_PW_CONN;
622 int ret;
623 u8 val;
624
625 /* Check for AC charger */
626 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
627 AB8500_CH_STATUS1_REG, &val);
628 if (ret < 0) {
629 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
630 return ret;
631 }
632
633 if (val & MAIN_CH_DET)
634 result = AC_PW_CONN;
635
636 /* Check for USB charger */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100637
638 if (!probe) {
639 /*
640 * AB8500 says VBUS_DET_DBNC1 & VBUS_DET_DBNC100
641 * when disconnecting ACA even though no
642 * charger was connected. Try waiting a little
643 * longer than the 100 ms of VBUS_DET_DBNC100...
644 */
645 msleep(110);
646 }
Arun Murthy84edbee2012-02-29 21:54:26 +0530647 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
648 AB8500_CH_USBCH_STAT1_REG, &val);
649 if (ret < 0) {
650 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
651 return ret;
652 }
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100653 dev_dbg(di->dev,
654 "%s AB8500_CH_USBCH_STAT1_REG %x\n", __func__,
655 val);
Arun Murthy84edbee2012-02-29 21:54:26 +0530656 if ((val & VBUS_DET_DBNC1) && (val & VBUS_DET_DBNC100))
657 result |= USB_PW_CONN;
658
659 return result;
660}
661
662/**
663 * ab8500_charger_max_usb_curr() - get the max curr for the USB type
664 * @di: pointer to the ab8500_charger structure
665 * @link_status: the identified USB type
666 *
667 * Get the maximum current that is allowed to be drawn from the host
668 * based on the USB type.
669 * Returns error code in case of failure else 0 on success
670 */
671static int ab8500_charger_max_usb_curr(struct ab8500_charger *di,
Hakan Berg74a8e342013-01-11 13:12:58 +0000672 enum ab8500_charger_link_status link_status)
Arun Murthy84edbee2012-02-29 21:54:26 +0530673{
674 int ret = 0;
675
Marcus Cooper4b45f4a2013-01-11 13:13:04 +0000676 di->usb_device_is_unrecognised = false;
677
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100678 /*
679 * Platform only supports USB 2.0.
680 * This means that charging current from USB source
681 * is maximum 500 mA. Every occurence of USB_STAT_*_HOST_*
682 * should set USB_CH_IP_CUR_LVL_0P5.
683 */
684
Arun Murthy84edbee2012-02-29 21:54:26 +0530685 switch (link_status) {
686 case USB_STAT_STD_HOST_NC:
687 case USB_STAT_STD_HOST_C_NS:
688 case USB_STAT_STD_HOST_C_S:
689 dev_dbg(di->dev, "USB Type - Standard host is "
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100690 "detected through USB driver\n");
Lee Jonesf7470b52013-02-14 12:37:29 +0000691 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100692 di->is_aca_rid = 0;
Arun Murthy84edbee2012-02-29 21:54:26 +0530693 break;
694 case USB_STAT_HOST_CHG_HS_CHIRP:
Lee Jonesf7470b52013-02-14 12:37:29 +0000695 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100696 di->is_aca_rid = 0;
Arun Murthy84edbee2012-02-29 21:54:26 +0530697 break;
698 case USB_STAT_HOST_CHG_HS:
Lee Jonesf7470b52013-02-14 12:37:29 +0000699 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100700 di->is_aca_rid = 0;
701 break;
Arun Murthy84edbee2012-02-29 21:54:26 +0530702 case USB_STAT_ACA_RID_C_HS:
Lee Jonesf7470b52013-02-14 12:37:29 +0000703 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P9;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100704 di->is_aca_rid = 0;
Arun Murthy84edbee2012-02-29 21:54:26 +0530705 break;
706 case USB_STAT_ACA_RID_A:
707 /*
708 * Dedicated charger level minus maximum current accessory
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100709 * can consume (900mA). Closest level is 500mA
Arun Murthy84edbee2012-02-29 21:54:26 +0530710 */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100711 dev_dbg(di->dev, "USB_STAT_ACA_RID_A detected\n");
Lee Jonesf7470b52013-02-14 12:37:29 +0000712 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100713 di->is_aca_rid = 1;
Arun Murthy84edbee2012-02-29 21:54:26 +0530714 break;
715 case USB_STAT_ACA_RID_B:
716 /*
717 * Dedicated charger level minus 120mA (20mA for ACA and
718 * 100mA for potential accessory). Closest level is 1300mA
719 */
Lee Jonesf7470b52013-02-14 12:37:29 +0000720 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_1P3;
Hakan Berg74a8e342013-01-11 13:12:58 +0000721 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
Lee Jonesf7470b52013-02-14 12:37:29 +0000722 di->max_usb_in_curr.usb_type_max);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100723 di->is_aca_rid = 1;
Arun Murthy84edbee2012-02-29 21:54:26 +0530724 break;
Arun Murthy84edbee2012-02-29 21:54:26 +0530725 case USB_STAT_HOST_CHG_NM:
Lee Jonesf7470b52013-02-14 12:37:29 +0000726 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100727 di->is_aca_rid = 0;
728 break;
Hakan Berg74a8e342013-01-11 13:12:58 +0000729 case USB_STAT_DEDICATED_CHG:
Lee Jonesf7470b52013-02-14 12:37:29 +0000730 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_1P5;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100731 di->is_aca_rid = 0;
732 break;
733 case USB_STAT_ACA_RID_C_HS_CHIRP:
734 case USB_STAT_ACA_RID_C_NM:
Lee Jonesf7470b52013-02-14 12:37:29 +0000735 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_1P5;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100736 di->is_aca_rid = 1;
Arun Murthy84edbee2012-02-29 21:54:26 +0530737 break;
Marcus Cooper4b45f4a2013-01-11 13:13:04 +0000738 case USB_STAT_NOT_CONFIGURED:
739 if (di->vbus_detected) {
740 di->usb_device_is_unrecognised = true;
741 dev_dbg(di->dev, "USB Type - Legacy charger.\n");
Lee Jonesf7470b52013-02-14 12:37:29 +0000742 di->max_usb_in_curr.usb_type_max =
743 USB_CH_IP_CUR_LVL_1P5;
Marcus Cooper4b45f4a2013-01-11 13:13:04 +0000744 break;
745 }
Arun Murthy84edbee2012-02-29 21:54:26 +0530746 case USB_STAT_HM_IDGND:
Arun Murthy84edbee2012-02-29 21:54:26 +0530747 dev_err(di->dev, "USB Type - Charging not allowed\n");
Lee Jonesf7470b52013-02-14 12:37:29 +0000748 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P05;
Arun Murthy84edbee2012-02-29 21:54:26 +0530749 ret = -ENXIO;
750 break;
Hakan Berg74a8e342013-01-11 13:12:58 +0000751 case USB_STAT_RESERVED:
752 if (is_ab8500(di->parent)) {
753 di->flags.vbus_collapse = true;
754 dev_err(di->dev, "USB Type - USB_STAT_RESERVED "
755 "VBUS has collapsed\n");
756 ret = -ENXIO;
757 break;
Lee Jones861a30d2012-08-29 20:36:51 +0800758 } else {
Hakan Berg74a8e342013-01-11 13:12:58 +0000759 dev_dbg(di->dev, "USB Type - Charging not allowed\n");
Lee Jonesf7470b52013-02-14 12:37:29 +0000760 di->max_usb_in_curr.usb_type_max =
761 USB_CH_IP_CUR_LVL_0P05;
Hakan Berg74a8e342013-01-11 13:12:58 +0000762 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
Lee Jonesf7470b52013-02-14 12:37:29 +0000763 link_status,
764 di->max_usb_in_curr.usb_type_max);
Hakan Berg74a8e342013-01-11 13:12:58 +0000765 ret = -ENXIO;
766 break;
767 }
768 break;
769 case USB_STAT_CARKIT_1:
770 case USB_STAT_CARKIT_2:
771 case USB_STAT_ACA_DOCK_CHARGER:
772 case USB_STAT_CHARGER_LINE_1:
Lee Jonesf7470b52013-02-14 12:37:29 +0000773 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
Hakan Berg74a8e342013-01-11 13:12:58 +0000774 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
Lee Jonesf7470b52013-02-14 12:37:29 +0000775 di->max_usb_in_curr.usb_type_max);
Henrik Sölverff380902012-04-17 15:51:01 +0200776 case USB_STAT_NOT_VALID_LINK:
777 dev_err(di->dev, "USB Type invalid - try charging anyway\n");
Lee Jonesf7470b52013-02-14 12:37:29 +0000778 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
Hakan Berg74a8e342013-01-11 13:12:58 +0000779 break;
780
Arun Murthy84edbee2012-02-29 21:54:26 +0530781 default:
782 dev_err(di->dev, "USB Type - Unknown\n");
Lee Jonesf7470b52013-02-14 12:37:29 +0000783 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P05;
Arun Murthy84edbee2012-02-29 21:54:26 +0530784 ret = -ENXIO;
785 break;
786 };
787
Lee Jonesf7470b52013-02-14 12:37:29 +0000788 di->max_usb_in_curr.set_max = di->max_usb_in_curr.usb_type_max;
Arun Murthy84edbee2012-02-29 21:54:26 +0530789 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
Lee Jonesf7470b52013-02-14 12:37:29 +0000790 link_status, di->max_usb_in_curr.set_max);
Arun Murthy84edbee2012-02-29 21:54:26 +0530791
792 return ret;
793}
794
795/**
796 * ab8500_charger_read_usb_type() - read the type of usb connected
797 * @di: pointer to the ab8500_charger structure
798 *
799 * Detect the type of the plugged USB
800 * Returns error code in case of failure else 0 on success
801 */
802static int ab8500_charger_read_usb_type(struct ab8500_charger *di)
803{
804 int ret;
805 u8 val;
806
807 ret = abx500_get_register_interruptible(di->dev,
808 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG, &val);
809 if (ret < 0) {
810 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
811 return ret;
812 }
Lee Jones861a30d2012-08-29 20:36:51 +0800813 if (is_ab8500(di->parent))
Hakan Berg74a8e342013-01-11 13:12:58 +0000814 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
Lee Jones861a30d2012-08-29 20:36:51 +0800815 AB8500_USB_LINE_STAT_REG, &val);
816 else
817 ret = abx500_get_register_interruptible(di->dev,
818 AB8500_USB, AB8500_USB_LINK1_STAT_REG, &val);
Arun Murthy84edbee2012-02-29 21:54:26 +0530819 if (ret < 0) {
820 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
821 return ret;
822 }
823
824 /* get the USB type */
Lee Jones861a30d2012-08-29 20:36:51 +0800825 if (is_ab8500(di->parent))
826 val = (val & AB8500_USB_LINK_STATUS) >> USB_LINK_STATUS_SHIFT;
827 else
828 val = (val & AB8505_USB_LINK_STATUS) >> USB_LINK_STATUS_SHIFT;
Arun Murthy84edbee2012-02-29 21:54:26 +0530829 ret = ab8500_charger_max_usb_curr(di,
830 (enum ab8500_charger_link_status) val);
831
832 return ret;
833}
834
835/**
836 * ab8500_charger_detect_usb_type() - get the type of usb connected
837 * @di: pointer to the ab8500_charger structure
838 *
839 * Detect the type of the plugged USB
840 * Returns error code in case of failure else 0 on success
841 */
842static int ab8500_charger_detect_usb_type(struct ab8500_charger *di)
843{
844 int i, ret;
845 u8 val;
846
847 /*
848 * On getting the VBUS rising edge detect interrupt there
849 * is a 250ms delay after which the register UsbLineStatus
850 * is filled with valid data.
851 */
852 for (i = 0; i < 10; i++) {
853 msleep(250);
854 ret = abx500_get_register_interruptible(di->dev,
855 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG,
856 &val);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100857 dev_dbg(di->dev, "%s AB8500_IT_SOURCE21_REG %x\n",
858 __func__, val);
Arun Murthy84edbee2012-02-29 21:54:26 +0530859 if (ret < 0) {
860 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
861 return ret;
862 }
Hakan Berg74a8e342013-01-11 13:12:58 +0000863
Lee Jones861a30d2012-08-29 20:36:51 +0800864 if (is_ab8500(di->parent))
Hakan Berg74a8e342013-01-11 13:12:58 +0000865 ret = abx500_get_register_interruptible(di->dev,
866 AB8500_USB, AB8500_USB_LINE_STAT_REG, &val);
Lee Jones861a30d2012-08-29 20:36:51 +0800867 else
Hakan Berg74a8e342013-01-11 13:12:58 +0000868 ret = abx500_get_register_interruptible(di->dev,
869 AB8500_USB, AB8500_USB_LINK1_STAT_REG, &val);
Arun Murthy84edbee2012-02-29 21:54:26 +0530870 if (ret < 0) {
871 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
872 return ret;
873 }
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100874 dev_dbg(di->dev, "%s AB8500_USB_LINE_STAT_REG %x\n", __func__,
875 val);
Arun Murthy84edbee2012-02-29 21:54:26 +0530876 /*
877 * Until the IT source register is read the UsbLineStatus
878 * register is not updated, hence doing the same
879 * Revisit this:
880 */
881
882 /* get the USB type */
Lee Jones861a30d2012-08-29 20:36:51 +0800883 if (is_ab8500(di->parent))
884 val = (val & AB8500_USB_LINK_STATUS) >>
885 USB_LINK_STATUS_SHIFT;
886 else
887 val = (val & AB8505_USB_LINK_STATUS) >>
888 USB_LINK_STATUS_SHIFT;
Arun Murthy84edbee2012-02-29 21:54:26 +0530889 if (val)
890 break;
891 }
892 ret = ab8500_charger_max_usb_curr(di,
893 (enum ab8500_charger_link_status) val);
894
895 return ret;
896}
897
898/*
899 * This array maps the raw hex value to charger voltage used by the AB8500
900 * Values taken from the UM0836
901 */
902static int ab8500_charger_voltage_map[] = {
903 3500 ,
904 3525 ,
905 3550 ,
906 3575 ,
907 3600 ,
908 3625 ,
909 3650 ,
910 3675 ,
911 3700 ,
912 3725 ,
913 3750 ,
914 3775 ,
915 3800 ,
916 3825 ,
917 3850 ,
918 3875 ,
919 3900 ,
920 3925 ,
921 3950 ,
922 3975 ,
923 4000 ,
924 4025 ,
925 4050 ,
926 4060 ,
927 4070 ,
928 4080 ,
929 4090 ,
930 4100 ,
931 4110 ,
932 4120 ,
933 4130 ,
934 4140 ,
935 4150 ,
936 4160 ,
937 4170 ,
938 4180 ,
939 4190 ,
940 4200 ,
941 4210 ,
942 4220 ,
943 4230 ,
944 4240 ,
945 4250 ,
946 4260 ,
947 4270 ,
948 4280 ,
949 4290 ,
950 4300 ,
951 4310 ,
952 4320 ,
953 4330 ,
954 4340 ,
955 4350 ,
956 4360 ,
957 4370 ,
958 4380 ,
959 4390 ,
960 4400 ,
961 4410 ,
962 4420 ,
963 4430 ,
964 4440 ,
965 4450 ,
966 4460 ,
967 4470 ,
968 4480 ,
969 4490 ,
970 4500 ,
971 4510 ,
972 4520 ,
973 4530 ,
974 4540 ,
975 4550 ,
976 4560 ,
977 4570 ,
978 4580 ,
979 4590 ,
980 4600 ,
981};
982
Arun Murthy84edbee2012-02-29 21:54:26 +0530983static int ab8500_voltage_to_regval(int voltage)
984{
985 int i;
986
987 /* Special case for voltage below 3.5V */
988 if (voltage < ab8500_charger_voltage_map[0])
989 return LOW_VOLT_REG;
990
991 for (i = 1; i < ARRAY_SIZE(ab8500_charger_voltage_map); i++) {
992 if (voltage < ab8500_charger_voltage_map[i])
993 return i - 1;
994 }
995
996 /* If not last element, return error */
997 i = ARRAY_SIZE(ab8500_charger_voltage_map) - 1;
998 if (voltage == ab8500_charger_voltage_map[i])
999 return i;
1000 else
1001 return -1;
1002}
1003
Lee Jones861a30d2012-08-29 20:36:51 +08001004static int ab8500_current_to_regval(struct ab8500_charger *di, int curr)
Arun Murthy84edbee2012-02-29 21:54:26 +05301005{
1006 int i;
1007
Lee Jones861a30d2012-08-29 20:36:51 +08001008 if (curr < di->bm->chg_output_curr[0])
Arun Murthy84edbee2012-02-29 21:54:26 +05301009 return 0;
1010
Lee Jones861a30d2012-08-29 20:36:51 +08001011 for (i = 0; i < di->bm->n_chg_out_curr; i++) {
1012 if (curr < di->bm->chg_output_curr[i])
Arun Murthy84edbee2012-02-29 21:54:26 +05301013 return i - 1;
1014 }
1015
1016 /* If not last element, return error */
Lee Jones861a30d2012-08-29 20:36:51 +08001017 i = di->bm->n_chg_out_curr - 1;
1018 if (curr == di->bm->chg_output_curr[i])
Arun Murthy84edbee2012-02-29 21:54:26 +05301019 return i;
1020 else
1021 return -1;
1022}
1023
Lee Jones861a30d2012-08-29 20:36:51 +08001024static int ab8500_vbus_in_curr_to_regval(struct ab8500_charger *di, int curr)
Arun Murthy84edbee2012-02-29 21:54:26 +05301025{
1026 int i;
1027
Lee Jones861a30d2012-08-29 20:36:51 +08001028 if (curr < di->bm->chg_input_curr[0])
Arun Murthy84edbee2012-02-29 21:54:26 +05301029 return 0;
1030
Lee Jones861a30d2012-08-29 20:36:51 +08001031 for (i = 0; i < di->bm->n_chg_in_curr; i++) {
1032 if (curr < di->bm->chg_input_curr[i])
Arun Murthy84edbee2012-02-29 21:54:26 +05301033 return i - 1;
1034 }
1035
1036 /* If not last element, return error */
Lee Jones861a30d2012-08-29 20:36:51 +08001037 i = di->bm->n_chg_in_curr - 1;
1038 if (curr == di->bm->chg_input_curr[i])
Arun Murthy84edbee2012-02-29 21:54:26 +05301039 return i;
1040 else
1041 return -1;
1042}
1043
1044/**
1045 * ab8500_charger_get_usb_cur() - get usb current
1046 * @di: pointer to the ab8500_charger structre
1047 *
1048 * The usb stack provides the maximum current that can be drawn from
1049 * the standard usb host. This will be in mA.
1050 * This function converts current in mA to a value that can be written
1051 * to the register. Returns -1 if charging is not allowed
1052 */
1053static int ab8500_charger_get_usb_cur(struct ab8500_charger *di)
1054{
Lee Jonesf7470b52013-02-14 12:37:29 +00001055 int ret = 0;
Arun Murthy84edbee2012-02-29 21:54:26 +05301056 switch (di->usb_state.usb_current) {
1057 case 100:
Lee Jonesf7470b52013-02-14 12:37:29 +00001058 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P09;
Arun Murthy84edbee2012-02-29 21:54:26 +05301059 break;
1060 case 200:
Lee Jonesf7470b52013-02-14 12:37:29 +00001061 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P19;
Arun Murthy84edbee2012-02-29 21:54:26 +05301062 break;
1063 case 300:
Lee Jonesf7470b52013-02-14 12:37:29 +00001064 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P29;
Arun Murthy84edbee2012-02-29 21:54:26 +05301065 break;
1066 case 400:
Lee Jonesf7470b52013-02-14 12:37:29 +00001067 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P38;
Arun Murthy84edbee2012-02-29 21:54:26 +05301068 break;
1069 case 500:
Lee Jonesf7470b52013-02-14 12:37:29 +00001070 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
Arun Murthy84edbee2012-02-29 21:54:26 +05301071 break;
1072 default:
Lee Jonesf7470b52013-02-14 12:37:29 +00001073 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P05;
1074 ret = -EPERM;
Arun Murthy84edbee2012-02-29 21:54:26 +05301075 break;
1076 };
Lee Jonesf7470b52013-02-14 12:37:29 +00001077 di->max_usb_in_curr.set_max = di->max_usb_in_curr.usb_type_max;
1078 return ret;
1079}
1080
1081/**
1082 * ab8500_charger_check_continue_stepping() - Check to allow stepping
1083 * @di: pointer to the ab8500_charger structure
1084 * @reg: select what charger register to check
1085 *
1086 * Check if current stepping should be allowed to continue.
1087 * Checks if charger source has not collapsed. If it has, further stepping
1088 * is not allowed.
1089 */
1090static bool ab8500_charger_check_continue_stepping(struct ab8500_charger *di,
1091 int reg)
1092{
1093 if (reg == AB8500_USBCH_IPT_CRNTLVL_REG)
1094 return !di->flags.vbus_drop_end;
1095 else
1096 return true;
Arun Murthy84edbee2012-02-29 21:54:26 +05301097}
1098
1099/**
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001100 * ab8500_charger_set_current() - set charger current
1101 * @di: pointer to the ab8500_charger structure
1102 * @ich: charger current, in mA
1103 * @reg: select what charger register to set
1104 *
1105 * Set charger current.
1106 * There is no state machine in the AB to step up/down the charger
1107 * current to avoid dips and spikes on MAIN, VBUS and VBAT when
1108 * charging is started. Instead we need to implement
1109 * this charger current step-up/down here.
1110 * Returns error code in case of failure else 0(on success)
1111 */
1112static int ab8500_charger_set_current(struct ab8500_charger *di,
1113 int ich, int reg)
1114{
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001115 int ret = 0;
Lee Jones861a30d2012-08-29 20:36:51 +08001116 int curr_index, prev_curr_index, shift_value, i;
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001117 u8 reg_value;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001118 u32 step_udelay;
1119 bool no_stepping = false;
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001120
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001121 atomic_inc(&di->current_stepping_sessions);
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001122
1123 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1124 reg, &reg_value);
1125 if (ret < 0) {
1126 dev_err(di->dev, "%s read failed\n", __func__);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001127 goto exit_set_current;
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001128 }
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001129
1130 switch (reg) {
1131 case AB8500_MCH_IPT_CURLVL_REG:
1132 shift_value = MAIN_CH_INPUT_CURR_SHIFT;
1133 prev_curr_index = (reg_value >> shift_value);
Lee Jones861a30d2012-08-29 20:36:51 +08001134 curr_index = ab8500_current_to_regval(di, ich);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001135 step_udelay = STEP_UDELAY;
1136 if (!di->ac.charger_connected)
1137 no_stepping = true;
1138 break;
1139 case AB8500_USBCH_IPT_CRNTLVL_REG:
Lee Jones861a30d2012-08-29 20:36:51 +08001140 if (is_ab8540(di->parent))
1141 shift_value = AB8540_VBUS_IN_CURR_LIM_SHIFT;
1142 else
1143 shift_value = VBUS_IN_CURR_LIM_SHIFT;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001144 prev_curr_index = (reg_value >> shift_value);
Lee Jones861a30d2012-08-29 20:36:51 +08001145 curr_index = ab8500_vbus_in_curr_to_regval(di, ich);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001146 step_udelay = STEP_UDELAY * 100;
1147
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001148 if (!di->usb.charger_connected)
1149 no_stepping = true;
1150 break;
1151 case AB8500_CH_OPT_CRNTLVL_REG:
1152 shift_value = 0;
1153 prev_curr_index = (reg_value >> shift_value);
Lee Jones861a30d2012-08-29 20:36:51 +08001154 curr_index = ab8500_current_to_regval(di, ich);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001155 step_udelay = STEP_UDELAY;
1156 if (curr_index && (curr_index - prev_curr_index) > 1)
1157 step_udelay *= 100;
1158
1159 if (!di->usb.charger_connected && !di->ac.charger_connected)
1160 no_stepping = true;
1161
1162 break;
1163 default:
1164 dev_err(di->dev, "%s current register not valid\n", __func__);
1165 ret = -ENXIO;
1166 goto exit_set_current;
1167 }
1168
1169 if (curr_index < 0) {
1170 dev_err(di->dev, "requested current limit out-of-range\n");
1171 ret = -ENXIO;
1172 goto exit_set_current;
1173 }
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001174
1175 /* only update current if it's been changed */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001176 if (prev_curr_index == curr_index) {
1177 dev_dbg(di->dev, "%s current not changed for reg: 0x%02x\n",
1178 __func__, reg);
1179 ret = 0;
1180 goto exit_set_current;
1181 }
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001182
1183 dev_dbg(di->dev, "%s set charger current: %d mA for reg: 0x%02x\n",
1184 __func__, ich, reg);
1185
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001186 if (no_stepping) {
1187 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1188 reg, (u8)curr_index << shift_value);
1189 if (ret)
1190 dev_err(di->dev, "%s write failed\n", __func__);
1191 } else if (prev_curr_index > curr_index) {
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001192 for (i = prev_curr_index - 1; i >= curr_index; i--) {
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001193 dev_dbg(di->dev, "curr change_1 to: %x for 0x%02x\n",
1194 (u8) i << shift_value, reg);
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001195 ret = abx500_set_register_interruptible(di->dev,
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001196 AB8500_CHARGER, reg, (u8)i << shift_value);
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001197 if (ret) {
1198 dev_err(di->dev, "%s write failed\n", __func__);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001199 goto exit_set_current;
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001200 }
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001201 if (i != curr_index)
1202 usleep_range(step_udelay, step_udelay * 2);
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001203 }
1204 } else {
Lee Jonesf7470b52013-02-14 12:37:29 +00001205 bool allow = true;
1206 for (i = prev_curr_index + 1; i <= curr_index && allow; i++) {
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001207 dev_dbg(di->dev, "curr change_2 to: %x for 0x%02x\n",
1208 (u8)i << shift_value, reg);
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001209 ret = abx500_set_register_interruptible(di->dev,
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001210 AB8500_CHARGER, reg, (u8)i << shift_value);
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001211 if (ret) {
1212 dev_err(di->dev, "%s write failed\n", __func__);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001213 goto exit_set_current;
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001214 }
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001215 if (i != curr_index)
1216 usleep_range(step_udelay, step_udelay * 2);
Lee Jonesf7470b52013-02-14 12:37:29 +00001217
1218 allow = ab8500_charger_check_continue_stepping(di, reg);
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001219 }
1220 }
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001221
1222exit_set_current:
1223 atomic_dec(&di->current_stepping_sessions);
1224
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001225 return ret;
1226}
1227
1228/**
Arun Murthy84edbee2012-02-29 21:54:26 +05301229 * ab8500_charger_set_vbus_in_curr() - set VBUS input current limit
1230 * @di: pointer to the ab8500_charger structure
1231 * @ich_in: charger input current limit
1232 *
1233 * Sets the current that can be drawn from the USB host
1234 * Returns error code in case of failure else 0(on success)
1235 */
1236static int ab8500_charger_set_vbus_in_curr(struct ab8500_charger *di,
1237 int ich_in)
1238{
Arun Murthy84edbee2012-02-29 21:54:26 +05301239 int min_value;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001240 int ret;
Arun Murthy84edbee2012-02-29 21:54:26 +05301241
1242 /* We should always use to lowest current limit */
Lee Jonesb0284de2012-11-30 10:09:42 +00001243 min_value = min(di->bm->chg_params->usb_curr_max, ich_in);
Lee Jonesf7470b52013-02-14 12:37:29 +00001244 if (di->max_usb_in_curr.set_max > 0)
1245 min_value = min(di->max_usb_in_curr.set_max, min_value);
1246
1247 if (di->usb_state.usb_current >= 0)
1248 min_value = min(di->usb_state.usb_current, min_value);
Arun Murthy84edbee2012-02-29 21:54:26 +05301249
1250 switch (min_value) {
1251 case 100:
1252 if (di->vbat < VBAT_TRESH_IP_CUR_RED)
1253 min_value = USB_CH_IP_CUR_LVL_0P05;
1254 break;
1255 case 500:
1256 if (di->vbat < VBAT_TRESH_IP_CUR_RED)
1257 min_value = USB_CH_IP_CUR_LVL_0P45;
1258 break;
1259 default:
1260 break;
1261 }
1262
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001263 dev_info(di->dev, "VBUS input current limit set to %d mA\n", min_value);
1264
1265 mutex_lock(&di->usb_ipt_crnt_lock);
1266 ret = ab8500_charger_set_current(di, min_value,
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001267 AB8500_USBCH_IPT_CRNTLVL_REG);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001268 mutex_unlock(&di->usb_ipt_crnt_lock);
1269
1270 return ret;
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001271}
Arun Murthy84edbee2012-02-29 21:54:26 +05301272
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001273/**
1274 * ab8500_charger_set_main_in_curr() - set main charger input current
1275 * @di: pointer to the ab8500_charger structure
1276 * @ich_in: input charger current, in mA
1277 *
1278 * Set main charger input current.
1279 * Returns error code in case of failure else 0(on success)
1280 */
1281static int ab8500_charger_set_main_in_curr(struct ab8500_charger *di,
1282 int ich_in)
1283{
1284 return ab8500_charger_set_current(di, ich_in,
1285 AB8500_MCH_IPT_CURLVL_REG);
1286}
Arun Murthy84edbee2012-02-29 21:54:26 +05301287
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001288/**
1289 * ab8500_charger_set_output_curr() - set charger output current
1290 * @di: pointer to the ab8500_charger structure
1291 * @ich_out: output charger current, in mA
1292 *
1293 * Set charger output current.
1294 * Returns error code in case of failure else 0(on success)
1295 */
1296static int ab8500_charger_set_output_curr(struct ab8500_charger *di,
1297 int ich_out)
1298{
1299 return ab8500_charger_set_current(di, ich_out,
1300 AB8500_CH_OPT_CRNTLVL_REG);
Arun Murthy84edbee2012-02-29 21:54:26 +05301301}
1302
1303/**
1304 * ab8500_charger_led_en() - turn on/off chargign led
1305 * @di: pointer to the ab8500_charger structure
1306 * @on: flag to turn on/off the chargign led
1307 *
1308 * Power ON/OFF charging LED indication
1309 * Returns error code in case of failure else 0(on success)
1310 */
1311static int ab8500_charger_led_en(struct ab8500_charger *di, int on)
1312{
1313 int ret;
1314
1315 if (on) {
1316 /* Power ON charging LED indicator, set LED current to 5mA */
1317 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1318 AB8500_LED_INDICATOR_PWM_CTRL,
1319 (LED_IND_CUR_5MA | LED_INDICATOR_PWM_ENA));
1320 if (ret) {
1321 dev_err(di->dev, "Power ON LED failed\n");
1322 return ret;
1323 }
1324 /* LED indicator PWM duty cycle 252/256 */
1325 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1326 AB8500_LED_INDICATOR_PWM_DUTY,
1327 LED_INDICATOR_PWM_DUTY_252_256);
1328 if (ret) {
1329 dev_err(di->dev, "Set LED PWM duty cycle failed\n");
1330 return ret;
1331 }
1332 } else {
1333 /* Power off charging LED indicator */
1334 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1335 AB8500_LED_INDICATOR_PWM_CTRL,
1336 LED_INDICATOR_PWM_DIS);
1337 if (ret) {
1338 dev_err(di->dev, "Power-off LED failed\n");
1339 return ret;
1340 }
1341 }
1342
1343 return ret;
1344}
1345
1346/**
1347 * ab8500_charger_ac_en() - enable or disable ac charging
1348 * @di: pointer to the ab8500_charger structure
1349 * @enable: enable/disable flag
1350 * @vset: charging voltage
1351 * @iset: charging current
1352 *
1353 * Enable/Disable AC/Mains charging and turns on/off the charging led
1354 * respectively.
1355 **/
1356static int ab8500_charger_ac_en(struct ux500_charger *charger,
1357 int enable, int vset, int iset)
1358{
1359 int ret;
1360 int volt_index;
1361 int curr_index;
1362 int input_curr_index;
1363 u8 overshoot = 0;
1364
1365 struct ab8500_charger *di = to_ab8500_charger_ac_device_info(charger);
1366
1367 if (enable) {
1368 /* Check if AC is connected */
1369 if (!di->ac.charger_connected) {
1370 dev_err(di->dev, "AC charger not connected\n");
1371 return -ENXIO;
1372 }
1373
1374 /* Enable AC charging */
1375 dev_dbg(di->dev, "Enable AC: %dmV %dmA\n", vset, iset);
1376
1377 /*
1378 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1379 * will be triggered everytime we enable the VDD ADC supply.
1380 * This will turn off charging for a short while.
1381 * It can be avoided by having the supply on when
1382 * there is a charger enabled. Normally the VDD ADC supply
1383 * is enabled everytime a GPADC conversion is triggered. We will
1384 * force it to be enabled from this driver to have
1385 * the GPADC module independant of the AB8500 chargers
1386 */
1387 if (!di->vddadc_en_ac) {
1388 regulator_enable(di->regu);
1389 di->vddadc_en_ac = true;
1390 }
1391
1392 /* Check if the requested voltage or current is valid */
1393 volt_index = ab8500_voltage_to_regval(vset);
Lee Jones861a30d2012-08-29 20:36:51 +08001394 curr_index = ab8500_current_to_regval(di, iset);
1395 input_curr_index = ab8500_current_to_regval(di,
Lee Jonesb0284de2012-11-30 10:09:42 +00001396 di->bm->chg_params->ac_curr_max);
Arun Murthy84edbee2012-02-29 21:54:26 +05301397 if (volt_index < 0 || curr_index < 0 || input_curr_index < 0) {
1398 dev_err(di->dev,
1399 "Charger voltage or current too high, "
1400 "charging not started\n");
1401 return -ENXIO;
1402 }
1403
1404 /* ChVoltLevel: maximum battery charging voltage */
1405 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1406 AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1407 if (ret) {
1408 dev_err(di->dev, "%s write failed\n", __func__);
1409 return ret;
1410 }
1411 /* MainChInputCurr: current that can be drawn from the charger*/
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001412 ret = ab8500_charger_set_main_in_curr(di,
Lee Jonesb0284de2012-11-30 10:09:42 +00001413 di->bm->chg_params->ac_curr_max);
Arun Murthy84edbee2012-02-29 21:54:26 +05301414 if (ret) {
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001415 dev_err(di->dev, "%s Failed to set MainChInputCurr\n",
1416 __func__);
Arun Murthy84edbee2012-02-29 21:54:26 +05301417 return ret;
1418 }
1419 /* ChOutputCurentLevel: protected output current */
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001420 ret = ab8500_charger_set_output_curr(di, iset);
Arun Murthy84edbee2012-02-29 21:54:26 +05301421 if (ret) {
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001422 dev_err(di->dev, "%s "
1423 "Failed to set ChOutputCurentLevel\n",
1424 __func__);
Arun Murthy84edbee2012-02-29 21:54:26 +05301425 return ret;
1426 }
1427
1428 /* Check if VBAT overshoot control should be enabled */
Lee Jonesb0284de2012-11-30 10:09:42 +00001429 if (!di->bm->enable_overshoot)
Arun Murthy84edbee2012-02-29 21:54:26 +05301430 overshoot = MAIN_CH_NO_OVERSHOOT_ENA_N;
1431
1432 /* Enable Main Charger */
1433 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1434 AB8500_MCH_CTRL1, MAIN_CH_ENA | overshoot);
1435 if (ret) {
1436 dev_err(di->dev, "%s write failed\n", __func__);
1437 return ret;
1438 }
1439
1440 /* Power on charging LED indication */
1441 ret = ab8500_charger_led_en(di, true);
1442 if (ret < 0)
1443 dev_err(di->dev, "failed to enable LED\n");
1444
1445 di->ac.charger_online = 1;
1446 } else {
1447 /* Disable AC charging */
1448 if (is_ab8500_1p1_or_earlier(di->parent)) {
1449 /*
1450 * For ABB revision 1.0 and 1.1 there is a bug in the
1451 * watchdog logic. That means we have to continously
1452 * kick the charger watchdog even when no charger is
1453 * connected. This is only valid once the AC charger
1454 * has been enabled. This is a bug that is not handled
1455 * by the algorithm and the watchdog have to be kicked
1456 * by the charger driver when the AC charger
1457 * is disabled
1458 */
1459 if (di->ac_conn) {
1460 queue_delayed_work(di->charger_wq,
1461 &di->kick_wd_work,
1462 round_jiffies(WD_KICK_INTERVAL));
1463 }
1464
1465 /*
1466 * We can't turn off charging completely
1467 * due to a bug in AB8500 cut1.
1468 * If we do, charging will not start again.
1469 * That is why we set the lowest voltage
1470 * and current possible
1471 */
1472 ret = abx500_set_register_interruptible(di->dev,
1473 AB8500_CHARGER,
1474 AB8500_CH_VOLT_LVL_REG, CH_VOL_LVL_3P5);
1475 if (ret) {
1476 dev_err(di->dev,
1477 "%s write failed\n", __func__);
1478 return ret;
1479 }
1480
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001481 ret = ab8500_charger_set_output_curr(di, 0);
Arun Murthy84edbee2012-02-29 21:54:26 +05301482 if (ret) {
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001483 dev_err(di->dev, "%s "
1484 "Failed to set ChOutputCurentLevel\n",
1485 __func__);
Arun Murthy84edbee2012-02-29 21:54:26 +05301486 return ret;
1487 }
1488 } else {
1489 ret = abx500_set_register_interruptible(di->dev,
1490 AB8500_CHARGER,
1491 AB8500_MCH_CTRL1, 0);
1492 if (ret) {
1493 dev_err(di->dev,
1494 "%s write failed\n", __func__);
1495 return ret;
1496 }
1497 }
1498
1499 ret = ab8500_charger_led_en(di, false);
1500 if (ret < 0)
1501 dev_err(di->dev, "failed to disable LED\n");
1502
1503 di->ac.charger_online = 0;
1504 di->ac.wd_expired = false;
1505
1506 /* Disable regulator if enabled */
1507 if (di->vddadc_en_ac) {
1508 regulator_disable(di->regu);
1509 di->vddadc_en_ac = false;
1510 }
1511
1512 dev_dbg(di->dev, "%s Disabled AC charging\n", __func__);
1513 }
1514 ab8500_power_supply_changed(di, &di->ac_chg.psy);
1515
1516 return ret;
1517}
1518
1519/**
1520 * ab8500_charger_usb_en() - enable usb charging
1521 * @di: pointer to the ab8500_charger structure
1522 * @enable: enable/disable flag
1523 * @vset: charging voltage
1524 * @ich_out: charger output current
1525 *
1526 * Enable/Disable USB charging and turns on/off the charging led respectively.
1527 * Returns error code in case of failure else 0(on success)
1528 */
1529static int ab8500_charger_usb_en(struct ux500_charger *charger,
1530 int enable, int vset, int ich_out)
1531{
1532 int ret;
1533 int volt_index;
1534 int curr_index;
1535 u8 overshoot = 0;
1536
1537 struct ab8500_charger *di = to_ab8500_charger_usb_device_info(charger);
1538
1539 if (enable) {
1540 /* Check if USB is connected */
1541 if (!di->usb.charger_connected) {
1542 dev_err(di->dev, "USB charger not connected\n");
1543 return -ENXIO;
1544 }
1545
1546 /*
1547 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1548 * will be triggered everytime we enable the VDD ADC supply.
1549 * This will turn off charging for a short while.
1550 * It can be avoided by having the supply on when
1551 * there is a charger enabled. Normally the VDD ADC supply
1552 * is enabled everytime a GPADC conversion is triggered. We will
1553 * force it to be enabled from this driver to have
1554 * the GPADC module independant of the AB8500 chargers
1555 */
1556 if (!di->vddadc_en_usb) {
1557 regulator_enable(di->regu);
1558 di->vddadc_en_usb = true;
1559 }
1560
1561 /* Enable USB charging */
1562 dev_dbg(di->dev, "Enable USB: %dmV %dmA\n", vset, ich_out);
1563
1564 /* Check if the requested voltage or current is valid */
1565 volt_index = ab8500_voltage_to_regval(vset);
Lee Jones861a30d2012-08-29 20:36:51 +08001566 curr_index = ab8500_current_to_regval(di, ich_out);
Arun Murthy84edbee2012-02-29 21:54:26 +05301567 if (volt_index < 0 || curr_index < 0) {
1568 dev_err(di->dev,
1569 "Charger voltage or current too high, "
1570 "charging not started\n");
1571 return -ENXIO;
1572 }
1573
1574 /* ChVoltLevel: max voltage upto which battery can be charged */
1575 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1576 AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1577 if (ret) {
1578 dev_err(di->dev, "%s write failed\n", __func__);
1579 return ret;
1580 }
Arun Murthy84edbee2012-02-29 21:54:26 +05301581 /* Check if VBAT overshoot control should be enabled */
Lee Jonesb0284de2012-11-30 10:09:42 +00001582 if (!di->bm->enable_overshoot)
Arun Murthy84edbee2012-02-29 21:54:26 +05301583 overshoot = USB_CHG_NO_OVERSHOOT_ENA_N;
1584
1585 /* Enable USB Charger */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001586 dev_dbg(di->dev,
1587 "Enabling USB with write to AB8500_USBCH_CTRL1_REG\n");
Arun Murthy84edbee2012-02-29 21:54:26 +05301588 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1589 AB8500_USBCH_CTRL1_REG, USB_CH_ENA | overshoot);
1590 if (ret) {
1591 dev_err(di->dev, "%s write failed\n", __func__);
1592 return ret;
1593 }
1594
1595 /* If success power on charging LED indication */
1596 ret = ab8500_charger_led_en(di, true);
1597 if (ret < 0)
1598 dev_err(di->dev, "failed to enable LED\n");
1599
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001600 di->usb.charger_online = 1;
1601
1602 /* USBChInputCurr: current that can be drawn from the usb */
Lee Jonesf7470b52013-02-14 12:37:29 +00001603 ret = ab8500_charger_set_vbus_in_curr(di,
1604 di->max_usb_in_curr.usb_type_max);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001605 if (ret) {
1606 dev_err(di->dev, "setting USBChInputCurr failed\n");
1607 return ret;
1608 }
1609
1610 /* ChOutputCurentLevel: protected output current */
1611 ret = ab8500_charger_set_output_curr(di, ich_out);
1612 if (ret) {
1613 dev_err(di->dev, "%s "
1614 "Failed to set ChOutputCurentLevel\n",
1615 __func__);
1616 return ret;
1617 }
1618
Arun Murthy84edbee2012-02-29 21:54:26 +05301619 queue_delayed_work(di->charger_wq, &di->check_vbat_work, HZ);
1620
Arun Murthy84edbee2012-02-29 21:54:26 +05301621 } else {
1622 /* Disable USB charging */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001623 dev_dbg(di->dev, "%s Disabled USB charging\n", __func__);
Arun Murthy84edbee2012-02-29 21:54:26 +05301624 ret = abx500_set_register_interruptible(di->dev,
1625 AB8500_CHARGER,
1626 AB8500_USBCH_CTRL1_REG, 0);
1627 if (ret) {
1628 dev_err(di->dev,
1629 "%s write failed\n", __func__);
1630 return ret;
1631 }
1632
1633 ret = ab8500_charger_led_en(di, false);
1634 if (ret < 0)
1635 dev_err(di->dev, "failed to disable LED\n");
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001636 /* USBChInputCurr: current that can be drawn from the usb */
1637 ret = ab8500_charger_set_vbus_in_curr(di, 0);
1638 if (ret) {
1639 dev_err(di->dev, "setting USBChInputCurr failed\n");
1640 return ret;
1641 }
Arun Murthy84edbee2012-02-29 21:54:26 +05301642
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001643 /* ChOutputCurentLevel: protected output current */
1644 ret = ab8500_charger_set_output_curr(di, 0);
1645 if (ret) {
1646 dev_err(di->dev, "%s "
1647 "Failed to reset ChOutputCurentLevel\n",
1648 __func__);
1649 return ret;
1650 }
Arun Murthy84edbee2012-02-29 21:54:26 +05301651 di->usb.charger_online = 0;
1652 di->usb.wd_expired = false;
1653
1654 /* Disable regulator if enabled */
1655 if (di->vddadc_en_usb) {
1656 regulator_disable(di->regu);
1657 di->vddadc_en_usb = false;
1658 }
1659
1660 dev_dbg(di->dev, "%s Disabled USB charging\n", __func__);
1661
1662 /* Cancel any pending Vbat check work */
1663 if (delayed_work_pending(&di->check_vbat_work))
1664 cancel_delayed_work(&di->check_vbat_work);
1665
1666 }
1667 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1668
1669 return ret;
1670}
1671
Lee Jones88917162013-02-13 11:39:19 +00001672static int ab8500_external_charger_prepare(struct notifier_block *charger_nb,
1673 unsigned long event, void *data)
1674{
1675 int ret;
1676 struct device *dev = data;
1677 /*Toggle External charger control pin*/
1678 ret = abx500_set_register_interruptible(dev, AB8500_SYS_CTRL1_BLOCK,
1679 AB8500_SYS_CHARGER_CONTROL_REG,
1680 EXTERNAL_CHARGER_DISABLE_REG_VAL);
1681 if (ret < 0) {
1682 dev_err(dev, "write reg failed %d\n", ret);
1683 goto out;
1684 }
1685 ret = abx500_set_register_interruptible(dev, AB8500_SYS_CTRL1_BLOCK,
1686 AB8500_SYS_CHARGER_CONTROL_REG,
1687 EXTERNAL_CHARGER_ENABLE_REG_VAL);
1688 if (ret < 0)
1689 dev_err(dev, "Write reg failed %d\n", ret);
1690
1691out:
1692 return ret;
1693}
1694
Arun Murthy84edbee2012-02-29 21:54:26 +05301695/**
Lee Jones4dcdf572013-02-14 09:24:10 +00001696 * ab8500_charger_usb_check_enable() - enable usb charging
1697 * @charger: pointer to the ux500_charger structure
1698 * @vset: charging voltage
1699 * @iset: charger output current
1700 *
1701 * Check if the VBUS charger has been disconnected and reconnected without
1702 * AB8500 rising an interrupt. Returns 0 on success.
1703 */
1704static int ab8500_charger_usb_check_enable(struct ux500_charger *charger,
1705 int vset, int iset)
1706{
1707 u8 usbch_ctrl1 = 0;
1708 int ret = 0;
1709
1710 struct ab8500_charger *di = to_ab8500_charger_usb_device_info(charger);
1711
1712 if (!di->usb.charger_connected)
1713 return ret;
1714
1715 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1716 AB8500_USBCH_CTRL1_REG, &usbch_ctrl1);
1717 if (ret < 0) {
1718 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
1719 return ret;
1720 }
1721 dev_dbg(di->dev, "USB charger ctrl: 0x%02x\n", usbch_ctrl1);
1722
1723 if (!(usbch_ctrl1 & USB_CH_ENA)) {
1724 dev_info(di->dev, "Charging has been disabled abnormally and will be re-enabled\n");
1725
1726 ret = abx500_mask_and_set_register_interruptible(di->dev,
1727 AB8500_CHARGER, AB8500_CHARGER_CTRL,
1728 DROP_COUNT_RESET, DROP_COUNT_RESET);
1729 if (ret < 0) {
1730 dev_err(di->dev, "ab8500 write failed %d\n", __LINE__);
1731 return ret;
1732 }
1733
1734 ret = ab8500_charger_usb_en(&di->usb_chg, true, vset, iset);
1735 if (ret < 0) {
1736 dev_err(di->dev, "Failed to enable VBUS charger %d\n",
1737 __LINE__);
1738 return ret;
1739 }
1740 }
1741 return ret;
1742}
1743
1744/**
1745 * ab8500_charger_ac_check_enable() - enable usb charging
1746 * @charger: pointer to the ux500_charger structure
1747 * @vset: charging voltage
1748 * @iset: charger output current
1749 *
1750 * Check if the AC charger has been disconnected and reconnected without
1751 * AB8500 rising an interrupt. Returns 0 on success.
1752 */
1753static int ab8500_charger_ac_check_enable(struct ux500_charger *charger,
1754 int vset, int iset)
1755{
1756 u8 mainch_ctrl1 = 0;
1757 int ret = 0;
1758
1759 struct ab8500_charger *di = to_ab8500_charger_ac_device_info(charger);
1760
1761 if (!di->ac.charger_connected)
1762 return ret;
1763
1764 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1765 AB8500_MCH_CTRL1, &mainch_ctrl1);
1766 if (ret < 0) {
1767 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
1768 return ret;
1769 }
1770 dev_dbg(di->dev, "AC charger ctrl: 0x%02x\n", mainch_ctrl1);
1771
1772 if (!(mainch_ctrl1 & MAIN_CH_ENA)) {
1773 dev_info(di->dev, "Charging has been disabled abnormally and will be re-enabled\n");
1774
1775 ret = abx500_mask_and_set_register_interruptible(di->dev,
1776 AB8500_CHARGER, AB8500_CHARGER_CTRL,
1777 DROP_COUNT_RESET, DROP_COUNT_RESET);
1778
1779 if (ret < 0) {
1780 dev_err(di->dev, "ab8500 write failed %d\n", __LINE__);
1781 return ret;
1782 }
1783
1784 ret = ab8500_charger_ac_en(&di->usb_chg, true, vset, iset);
1785 if (ret < 0) {
1786 dev_err(di->dev, "failed to enable AC charger %d\n",
1787 __LINE__);
1788 return ret;
1789 }
1790 }
1791 return ret;
1792}
1793
1794/**
Arun Murthy84edbee2012-02-29 21:54:26 +05301795 * ab8500_charger_watchdog_kick() - kick charger watchdog
1796 * @di: pointer to the ab8500_charger structure
1797 *
1798 * Kick charger watchdog
1799 * Returns error code in case of failure else 0(on success)
1800 */
1801static int ab8500_charger_watchdog_kick(struct ux500_charger *charger)
1802{
1803 int ret;
1804 struct ab8500_charger *di;
1805
1806 if (charger->psy.type == POWER_SUPPLY_TYPE_MAINS)
1807 di = to_ab8500_charger_ac_device_info(charger);
1808 else if (charger->psy.type == POWER_SUPPLY_TYPE_USB)
1809 di = to_ab8500_charger_usb_device_info(charger);
1810 else
1811 return -ENXIO;
1812
1813 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1814 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
1815 if (ret)
1816 dev_err(di->dev, "Failed to kick WD!\n");
1817
1818 return ret;
1819}
1820
1821/**
1822 * ab8500_charger_update_charger_current() - update charger current
1823 * @di: pointer to the ab8500_charger structure
1824 *
1825 * Update the charger output current for the specified charger
1826 * Returns error code in case of failure else 0(on success)
1827 */
1828static int ab8500_charger_update_charger_current(struct ux500_charger *charger,
1829 int ich_out)
1830{
1831 int ret;
Arun Murthy84edbee2012-02-29 21:54:26 +05301832 struct ab8500_charger *di;
1833
1834 if (charger->psy.type == POWER_SUPPLY_TYPE_MAINS)
1835 di = to_ab8500_charger_ac_device_info(charger);
1836 else if (charger->psy.type == POWER_SUPPLY_TYPE_USB)
1837 di = to_ab8500_charger_usb_device_info(charger);
1838 else
1839 return -ENXIO;
1840
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001841 ret = ab8500_charger_set_output_curr(di, ich_out);
Arun Murthy84edbee2012-02-29 21:54:26 +05301842 if (ret) {
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001843 dev_err(di->dev, "%s "
1844 "Failed to set ChOutputCurentLevel\n",
1845 __func__);
Arun Murthy84edbee2012-02-29 21:54:26 +05301846 return ret;
1847 }
1848
1849 /* Reset the main and usb drop input current measurement counter */
1850 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
Lee Jones4dcdf572013-02-14 09:24:10 +00001851 AB8500_CHARGER_CTRL, DROP_COUNT_RESET);
Arun Murthy84edbee2012-02-29 21:54:26 +05301852 if (ret) {
1853 dev_err(di->dev, "%s write failed\n", __func__);
1854 return ret;
1855 }
1856
1857 return ret;
1858}
1859
Lee Jonesdb43e6c2013-02-14 12:39:15 +00001860/**
1861 * ab8540_charger_power_path_enable() - enable usb power path mode
1862 * @charger: pointer to the ux500_charger structure
1863 * @enable: enable/disable flag
1864 *
1865 * Enable or disable the power path for usb mode
1866 * Returns error code in case of failure else 0(on success)
1867 */
1868static int ab8540_charger_power_path_enable(struct ux500_charger *charger,
1869 bool enable)
1870{
1871 int ret;
1872 struct ab8500_charger *di;
1873
1874 if (charger->psy.type == POWER_SUPPLY_TYPE_USB)
1875 di = to_ab8500_charger_usb_device_info(charger);
1876 else
1877 return -ENXIO;
1878
1879 ret = abx500_mask_and_set_register_interruptible(di->dev,
1880 AB8500_CHARGER, AB8540_USB_PP_MODE_REG,
1881 BUS_POWER_PATH_MODE_ENA, enable);
1882 if (ret) {
1883 dev_err(di->dev, "%s write failed\n", __func__);
1884 return ret;
1885 }
1886
1887 return ret;
1888}
1889
1890
1891/**
1892 * ab8540_charger_usb_pre_chg_enable() - enable usb pre change
1893 * @charger: pointer to the ux500_charger structure
1894 * @enable: enable/disable flag
1895 *
1896 * Enable or disable the pre-chage for usb mode
1897 * Returns error code in case of failure else 0(on success)
1898 */
1899static int ab8540_charger_usb_pre_chg_enable(struct ux500_charger *charger,
1900 bool enable)
1901{
1902 int ret;
1903 struct ab8500_charger *di;
1904
1905 if (charger->psy.type == POWER_SUPPLY_TYPE_USB)
1906 di = to_ab8500_charger_usb_device_info(charger);
1907 else
1908 return -ENXIO;
1909
1910 ret = abx500_mask_and_set_register_interruptible(di->dev,
1911 AB8500_CHARGER, AB8540_USB_PP_CHR_REG,
1912 BUS_POWER_PATH_PRECHG_ENA, enable);
1913 if (ret) {
1914 dev_err(di->dev, "%s write failed\n", __func__);
1915 return ret;
1916 }
1917
1918 return ret;
1919}
1920
Arun Murthy84edbee2012-02-29 21:54:26 +05301921static int ab8500_charger_get_ext_psy_data(struct device *dev, void *data)
1922{
1923 struct power_supply *psy;
1924 struct power_supply *ext;
1925 struct ab8500_charger *di;
1926 union power_supply_propval ret;
1927 int i, j;
1928 bool psy_found = false;
1929 struct ux500_charger *usb_chg;
1930
1931 usb_chg = (struct ux500_charger *)data;
1932 psy = &usb_chg->psy;
1933
1934 di = to_ab8500_charger_usb_device_info(usb_chg);
1935
1936 ext = dev_get_drvdata(dev);
1937
1938 /* For all psy where the driver name appears in any supplied_to */
1939 for (i = 0; i < ext->num_supplicants; i++) {
1940 if (!strcmp(ext->supplied_to[i], psy->name))
1941 psy_found = true;
1942 }
1943
1944 if (!psy_found)
1945 return 0;
1946
1947 /* Go through all properties for the psy */
1948 for (j = 0; j < ext->num_properties; j++) {
1949 enum power_supply_property prop;
1950 prop = ext->properties[j];
1951
1952 if (ext->get_property(ext, prop, &ret))
1953 continue;
1954
1955 switch (prop) {
1956 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1957 switch (ext->type) {
1958 case POWER_SUPPLY_TYPE_BATTERY:
1959 di->vbat = ret.intval / 1000;
1960 break;
1961 default:
1962 break;
1963 }
1964 break;
1965 default:
1966 break;
1967 }
1968 }
1969 return 0;
1970}
1971
1972/**
1973 * ab8500_charger_check_vbat_work() - keep vbus current within spec
1974 * @work pointer to the work_struct structure
1975 *
1976 * Due to a asic bug it is necessary to lower the input current to the vbus
1977 * charger when charging with at some specific levels. This issue is only valid
1978 * for below a certain battery voltage. This function makes sure that the
1979 * the allowed current limit isn't exceeded.
1980 */
1981static void ab8500_charger_check_vbat_work(struct work_struct *work)
1982{
1983 int t = 10;
1984 struct ab8500_charger *di = container_of(work,
1985 struct ab8500_charger, check_vbat_work.work);
1986
1987 class_for_each_device(power_supply_class, NULL,
1988 &di->usb_chg.psy, ab8500_charger_get_ext_psy_data);
1989
1990 /* First run old_vbat is 0. */
1991 if (di->old_vbat == 0)
1992 di->old_vbat = di->vbat;
1993
1994 if (!((di->old_vbat <= VBAT_TRESH_IP_CUR_RED &&
1995 di->vbat <= VBAT_TRESH_IP_CUR_RED) ||
1996 (di->old_vbat > VBAT_TRESH_IP_CUR_RED &&
1997 di->vbat > VBAT_TRESH_IP_CUR_RED))) {
1998
1999 dev_dbg(di->dev, "Vbat did cross threshold, curr: %d, new: %d,"
Lee Jonesf7470b52013-02-14 12:37:29 +00002000 " old: %d\n", di->max_usb_in_curr.usb_type_max,
2001 di->vbat, di->old_vbat);
2002 ab8500_charger_set_vbus_in_curr(di,
2003 di->max_usb_in_curr.usb_type_max);
Arun Murthy84edbee2012-02-29 21:54:26 +05302004 power_supply_changed(&di->usb_chg.psy);
2005 }
2006
2007 di->old_vbat = di->vbat;
2008
2009 /*
2010 * No need to check the battery voltage every second when not close to
2011 * the threshold.
2012 */
2013 if (di->vbat < (VBAT_TRESH_IP_CUR_RED + 100) &&
2014 (di->vbat > (VBAT_TRESH_IP_CUR_RED - 100)))
2015 t = 1;
2016
2017 queue_delayed_work(di->charger_wq, &di->check_vbat_work, t * HZ);
2018}
2019
2020/**
2021 * ab8500_charger_check_hw_failure_work() - check main charger failure
2022 * @work: pointer to the work_struct structure
2023 *
2024 * Work queue function for checking the main charger status
2025 */
2026static void ab8500_charger_check_hw_failure_work(struct work_struct *work)
2027{
2028 int ret;
2029 u8 reg_value;
2030
2031 struct ab8500_charger *di = container_of(work,
2032 struct ab8500_charger, check_hw_failure_work.work);
2033
2034 /* Check if the status bits for HW failure is still active */
2035 if (di->flags.mainextchnotok) {
2036 ret = abx500_get_register_interruptible(di->dev,
2037 AB8500_CHARGER, AB8500_CH_STATUS2_REG, &reg_value);
2038 if (ret < 0) {
2039 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2040 return;
2041 }
2042 if (!(reg_value & MAIN_CH_NOK)) {
2043 di->flags.mainextchnotok = false;
2044 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2045 }
2046 }
2047 if (di->flags.vbus_ovv) {
2048 ret = abx500_get_register_interruptible(di->dev,
2049 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG,
2050 &reg_value);
2051 if (ret < 0) {
2052 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2053 return;
2054 }
2055 if (!(reg_value & VBUS_OVV_TH)) {
2056 di->flags.vbus_ovv = false;
2057 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2058 }
2059 }
2060 /* If we still have a failure, schedule a new check */
2061 if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
2062 queue_delayed_work(di->charger_wq,
2063 &di->check_hw_failure_work, round_jiffies(HZ));
2064 }
2065}
2066
2067/**
2068 * ab8500_charger_kick_watchdog_work() - kick the watchdog
2069 * @work: pointer to the work_struct structure
2070 *
2071 * Work queue function for kicking the charger watchdog.
2072 *
2073 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
2074 * logic. That means we have to continously kick the charger
2075 * watchdog even when no charger is connected. This is only
2076 * valid once the AC charger has been enabled. This is
2077 * a bug that is not handled by the algorithm and the
2078 * watchdog have to be kicked by the charger driver
2079 * when the AC charger is disabled
2080 */
2081static void ab8500_charger_kick_watchdog_work(struct work_struct *work)
2082{
2083 int ret;
2084
2085 struct ab8500_charger *di = container_of(work,
2086 struct ab8500_charger, kick_wd_work.work);
2087
2088 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
2089 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
2090 if (ret)
2091 dev_err(di->dev, "Failed to kick WD!\n");
2092
2093 /* Schedule a new watchdog kick */
2094 queue_delayed_work(di->charger_wq,
2095 &di->kick_wd_work, round_jiffies(WD_KICK_INTERVAL));
2096}
2097
2098/**
2099 * ab8500_charger_ac_work() - work to get and set main charger status
2100 * @work: pointer to the work_struct structure
2101 *
2102 * Work queue function for checking the main charger status
2103 */
2104static void ab8500_charger_ac_work(struct work_struct *work)
2105{
2106 int ret;
2107
2108 struct ab8500_charger *di = container_of(work,
2109 struct ab8500_charger, ac_work);
2110
2111 /*
2112 * Since we can't be sure that the events are received
2113 * synchronously, we have the check if the main charger is
2114 * connected by reading the status register
2115 */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002116 ret = ab8500_charger_detect_chargers(di, false);
Arun Murthy84edbee2012-02-29 21:54:26 +05302117 if (ret < 0)
2118 return;
2119
2120 if (ret & AC_PW_CONN) {
2121 di->ac.charger_connected = 1;
2122 di->ac_conn = true;
2123 } else {
2124 di->ac.charger_connected = 0;
2125 }
2126
2127 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2128 sysfs_notify(&di->ac_chg.psy.dev->kobj, NULL, "present");
2129}
2130
Lee Jonesb269fff2013-01-11 13:12:51 +00002131static void ab8500_charger_usb_attached_work(struct work_struct *work)
2132{
2133 struct ab8500_charger *di = container_of(work,
2134 struct ab8500_charger,
2135 usb_charger_attached_work.work);
2136 int usbch = (USB_CH_VBUSDROP | USB_CH_VBUSDETDBNC);
2137 int ret, i;
2138 u8 statval;
2139
2140 for (i = 0; i < 10; i++) {
2141 ret = abx500_get_register_interruptible(di->dev,
2142 AB8500_CHARGER,
2143 AB8500_CH_USBCH_STAT1_REG,
2144 &statval);
2145 if (ret < 0) {
2146 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
2147 goto reschedule;
2148 }
2149 if ((statval & usbch) != usbch)
2150 goto reschedule;
2151
2152 msleep(CHARGER_STATUS_POLL);
2153 }
2154
2155 ab8500_charger_usb_en(&di->usb_chg, 0, 0, 0);
2156
2157 mutex_lock(&di->charger_attached_mutex);
2158 mutex_unlock(&di->charger_attached_mutex);
2159
2160 return;
2161
2162reschedule:
2163 queue_delayed_work(di->charger_wq,
2164 &di->usb_charger_attached_work,
2165 HZ);
2166}
2167
2168static void ab8500_charger_ac_attached_work(struct work_struct *work)
2169{
2170
2171 struct ab8500_charger *di = container_of(work,
2172 struct ab8500_charger,
2173 ac_charger_attached_work.work);
2174 int mainch = (MAIN_CH_STATUS2_MAINCHGDROP |
2175 MAIN_CH_STATUS2_MAINCHARGERDETDBNC);
2176 int ret, i;
2177 u8 statval;
2178
2179 for (i = 0; i < 10; i++) {
2180 ret = abx500_get_register_interruptible(di->dev,
2181 AB8500_CHARGER,
2182 AB8500_CH_STATUS2_REG,
2183 &statval);
2184 if (ret < 0) {
2185 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
2186 goto reschedule;
2187 }
2188
2189 if ((statval & mainch) != mainch)
2190 goto reschedule;
2191
2192 msleep(CHARGER_STATUS_POLL);
2193 }
2194
2195 ab8500_charger_ac_en(&di->ac_chg, 0, 0, 0);
2196 queue_work(di->charger_wq, &di->ac_work);
2197
2198 mutex_lock(&di->charger_attached_mutex);
2199 mutex_unlock(&di->charger_attached_mutex);
2200
2201 return;
2202
2203reschedule:
2204 queue_delayed_work(di->charger_wq,
2205 &di->ac_charger_attached_work,
2206 HZ);
2207}
2208
Arun Murthy84edbee2012-02-29 21:54:26 +05302209/**
2210 * ab8500_charger_detect_usb_type_work() - work to detect USB type
2211 * @work: Pointer to the work_struct structure
2212 *
2213 * Detect the type of USB plugged
2214 */
Anton Vorontsov64eb9b02012-03-14 04:43:11 +04002215static void ab8500_charger_detect_usb_type_work(struct work_struct *work)
Arun Murthy84edbee2012-02-29 21:54:26 +05302216{
2217 int ret;
2218
2219 struct ab8500_charger *di = container_of(work,
2220 struct ab8500_charger, detect_usb_type_work);
2221
2222 /*
2223 * Since we can't be sure that the events are received
2224 * synchronously, we have the check if is
2225 * connected by reading the status register
2226 */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002227 ret = ab8500_charger_detect_chargers(di, false);
Arun Murthy84edbee2012-02-29 21:54:26 +05302228 if (ret < 0)
2229 return;
2230
2231 if (!(ret & USB_PW_CONN)) {
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002232 dev_dbg(di->dev, "%s di->vbus_detected = false\n", __func__);
2233 di->vbus_detected = false;
Arun Murthy84edbee2012-02-29 21:54:26 +05302234 ab8500_charger_set_usb_connected(di, false);
2235 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2236 } else {
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002237 dev_dbg(di->dev, "%s di->vbus_detected = true\n", __func__);
2238 di->vbus_detected = true;
Arun Murthy84edbee2012-02-29 21:54:26 +05302239
2240 if (is_ab8500_1p1_or_earlier(di->parent)) {
2241 ret = ab8500_charger_detect_usb_type(di);
2242 if (!ret) {
2243 ab8500_charger_set_usb_connected(di, true);
2244 ab8500_power_supply_changed(di,
2245 &di->usb_chg.psy);
2246 }
2247 } else {
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002248 /*
2249 * For ABB cut2.0 and onwards we have an IRQ,
Arun Murthy84edbee2012-02-29 21:54:26 +05302250 * USB_LINK_STATUS that will be triggered when the USB
2251 * link status changes. The exception is USB connected
2252 * during startup. Then we don't get a
2253 * USB_LINK_STATUS IRQ
2254 */
2255 if (di->vbus_detected_start) {
2256 di->vbus_detected_start = false;
2257 ret = ab8500_charger_detect_usb_type(di);
2258 if (!ret) {
2259 ab8500_charger_set_usb_connected(di,
2260 true);
2261 ab8500_power_supply_changed(di,
2262 &di->usb_chg.psy);
2263 }
2264 }
2265 }
2266 }
2267}
2268
2269/**
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002270 * ab8500_charger_usb_link_attach_work() - work to detect USB type
Marcus Cooper4b45f4a2013-01-11 13:13:04 +00002271 * @work: pointer to the work_struct structure
2272 *
2273 * Detect the type of USB plugged
2274 */
2275static void ab8500_charger_usb_link_attach_work(struct work_struct *work)
2276{
2277 struct ab8500_charger *di =
2278 container_of(work, struct ab8500_charger, attach_work.work);
2279 int ret;
2280
2281 /* Update maximum input current if USB enumeration is not detected */
2282 if (!di->usb.charger_online) {
Lee Jonesf7470b52013-02-14 12:37:29 +00002283 ret = ab8500_charger_set_vbus_in_curr(di,
2284 di->max_usb_in_curr.usb_type_max);
Marcus Cooper4b45f4a2013-01-11 13:13:04 +00002285 if (ret)
2286 return;
2287 }
2288
2289 ab8500_charger_set_usb_connected(di, true);
2290 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2291}
2292
2293/**
Arun Murthy84edbee2012-02-29 21:54:26 +05302294 * ab8500_charger_usb_link_status_work() - work to detect USB type
2295 * @work: pointer to the work_struct structure
2296 *
2297 * Detect the type of USB plugged
2298 */
2299static void ab8500_charger_usb_link_status_work(struct work_struct *work)
2300{
Henrik Sölverff380902012-04-17 15:51:01 +02002301 int detected_chargers;
Arun Murthy84edbee2012-02-29 21:54:26 +05302302 int ret;
Henrik Sölverff380902012-04-17 15:51:01 +02002303 u8 val;
Hakan Bergd4337662012-07-26 11:10:35 +02002304 u8 link_status;
Arun Murthy84edbee2012-02-29 21:54:26 +05302305
2306 struct ab8500_charger *di = container_of(work,
2307 struct ab8500_charger, usb_link_status_work);
2308
2309 /*
2310 * Since we can't be sure that the events are received
2311 * synchronously, we have the check if is
2312 * connected by reading the status register
2313 */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002314 detected_chargers = ab8500_charger_detect_chargers(di, false);
Henrik Sölverff380902012-04-17 15:51:01 +02002315 if (detected_chargers < 0)
Arun Murthy84edbee2012-02-29 21:54:26 +05302316 return;
2317
Henrik Sölverff380902012-04-17 15:51:01 +02002318 /*
2319 * Some chargers that breaks the USB spec is
2320 * identified as invalid by AB8500 and it refuse
2321 * to start the charging process. but by jumping
2322 * thru a few hoops it can be forced to start.
2323 */
Marcus Cooper88efdb82012-08-30 14:12:53 +02002324 if (is_ab8500(di->parent))
2325 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
2326 AB8500_USB_LINE_STAT_REG, &val);
2327 else
2328 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
2329 AB8500_USB_LINK1_STAT_REG, &val);
2330
Henrik Sölverff380902012-04-17 15:51:01 +02002331 if (ret >= 0)
2332 dev_dbg(di->dev, "UsbLineStatus register = 0x%02x\n", val);
2333 else
2334 dev_dbg(di->dev, "Error reading USB link status\n");
2335
Lee Jones861a30d2012-08-29 20:36:51 +08002336 if (is_ab8500(di->parent))
Hakan Bergd4337662012-07-26 11:10:35 +02002337 link_status = AB8500_USB_LINK_STATUS;
Lee Jones861a30d2012-08-29 20:36:51 +08002338 else
2339 link_status = AB8505_USB_LINK_STATUS;
Hakan Bergd4337662012-07-26 11:10:35 +02002340
Henrik Sölverff380902012-04-17 15:51:01 +02002341 if (detected_chargers & USB_PW_CONN) {
Lee Jones861a30d2012-08-29 20:36:51 +08002342 if (((val & link_status) >> USB_LINK_STATUS_SHIFT) ==
2343 USB_STAT_NOT_VALID_LINK &&
Henrik Sölverff380902012-04-17 15:51:01 +02002344 di->invalid_charger_detect_state == 0) {
Lee Jones861a30d2012-08-29 20:36:51 +08002345 dev_dbg(di->dev,
2346 "Invalid charger detected, state= 0\n");
Henrik Sölverff380902012-04-17 15:51:01 +02002347 /*Enable charger*/
2348 abx500_mask_and_set_register_interruptible(di->dev,
Lee Jones861a30d2012-08-29 20:36:51 +08002349 AB8500_CHARGER, AB8500_USBCH_CTRL1_REG,
2350 USB_CH_ENA, USB_CH_ENA);
Henrik Sölverff380902012-04-17 15:51:01 +02002351 /*Enable charger detection*/
Marcus Cooperb3ea5f42012-08-29 17:56:19 +02002352 abx500_mask_and_set_register_interruptible(di->dev,
2353 AB8500_USB, AB8500_USB_LINE_CTRL2_REG,
2354 USB_CH_DET, USB_CH_DET);
Henrik Sölverff380902012-04-17 15:51:01 +02002355 di->invalid_charger_detect_state = 1;
2356 /*exit and wait for new link status interrupt.*/
2357 return;
2358
2359 }
2360 if (di->invalid_charger_detect_state == 1) {
Lee Jones861a30d2012-08-29 20:36:51 +08002361 dev_dbg(di->dev,
2362 "Invalid charger detected, state= 1\n");
Henrik Sölverff380902012-04-17 15:51:01 +02002363 /*Stop charger detection*/
Marcus Cooperb3ea5f42012-08-29 17:56:19 +02002364 abx500_mask_and_set_register_interruptible(di->dev,
2365 AB8500_USB, AB8500_USB_LINE_CTRL2_REG,
2366 USB_CH_DET, 0x00);
Henrik Sölverff380902012-04-17 15:51:01 +02002367 /*Check link status*/
Marcus Cooper88efdb82012-08-30 14:12:53 +02002368 if (is_ab8500(di->parent))
2369 ret = abx500_get_register_interruptible(di->dev,
2370 AB8500_USB, AB8500_USB_LINE_STAT_REG,
2371 &val);
2372 else
2373 ret = abx500_get_register_interruptible(di->dev,
2374 AB8500_USB, AB8500_USB_LINK1_STAT_REG,
2375 &val);
2376
Henrik Sölverff380902012-04-17 15:51:01 +02002377 dev_dbg(di->dev, "USB link status= 0x%02x\n",
Lee Jones861a30d2012-08-29 20:36:51 +08002378 (val & link_status) >> USB_LINK_STATUS_SHIFT);
Henrik Sölverff380902012-04-17 15:51:01 +02002379 di->invalid_charger_detect_state = 2;
2380 }
2381 } else {
2382 di->invalid_charger_detect_state = 0;
2383 }
2384
2385 if (!(detected_chargers & USB_PW_CONN)) {
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002386 di->vbus_detected = false;
Arun Murthy84edbee2012-02-29 21:54:26 +05302387 ab8500_charger_set_usb_connected(di, false);
2388 ab8500_power_supply_changed(di, &di->usb_chg.psy);
Marcus Cooper4b45f4a2013-01-11 13:13:04 +00002389 return;
2390 }
Arun Murthy84edbee2012-02-29 21:54:26 +05302391
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002392 dev_dbg(di->dev,"%s di->vbus_detected = true\n",__func__);
2393 di->vbus_detected = true;
Marcus Cooper4b45f4a2013-01-11 13:13:04 +00002394 ret = ab8500_charger_read_usb_type(di);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002395 if (ret) {
2396 if (ret == -ENXIO) {
2397 /* No valid charger type detected */
2398 ab8500_charger_set_usb_connected(di, false);
2399 ab8500_power_supply_changed(di, &di->usb_chg.psy);
Arun Murthy84edbee2012-02-29 21:54:26 +05302400 }
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002401 return;
2402 }
2403
2404 if (di->usb_device_is_unrecognised) {
2405 dev_dbg(di->dev,
2406 "Potential Legacy Charger device. "
2407 "Delay work for %d msec for USB enum "
2408 "to finish",
2409 WAIT_ACA_RID_ENUMERATION);
2410 queue_delayed_work(di->charger_wq,
2411 &di->attach_work,
2412 msecs_to_jiffies(WAIT_ACA_RID_ENUMERATION));
2413 } else if (di->is_aca_rid == 1) {
2414 /* Only wait once */
2415 di->is_aca_rid++;
2416 dev_dbg(di->dev,
2417 "%s Wait %d msec for USB enum to finish",
2418 __func__, 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 {
2423 queue_delayed_work(di->charger_wq,
2424 &di->attach_work,
2425 0);
Arun Murthy84edbee2012-02-29 21:54:26 +05302426 }
2427}
2428
2429static void ab8500_charger_usb_state_changed_work(struct work_struct *work)
2430{
2431 int ret;
2432 unsigned long flags;
2433
2434 struct ab8500_charger *di = container_of(work,
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002435 struct ab8500_charger, usb_state_changed_work.work);
Arun Murthy84edbee2012-02-29 21:54:26 +05302436
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002437 if (!di->vbus_detected) {
2438 dev_dbg(di->dev,
2439 "%s !di->vbus_detected\n",
2440 __func__);
Arun Murthy84edbee2012-02-29 21:54:26 +05302441 return;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002442 }
Arun Murthy84edbee2012-02-29 21:54:26 +05302443
2444 spin_lock_irqsave(&di->usb_state.usb_lock, flags);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002445 di->usb_state.state = di->usb_state.state_tmp;
2446 di->usb_state.usb_current = di->usb_state.usb_current_tmp;
Arun Murthy84edbee2012-02-29 21:54:26 +05302447 spin_unlock_irqrestore(&di->usb_state.usb_lock, flags);
2448
Arun Murthy84edbee2012-02-29 21:54:26 +05302449 dev_dbg(di->dev, "%s USB state: 0x%02x mA: %d\n",
2450 __func__, di->usb_state.state, di->usb_state.usb_current);
2451
2452 switch (di->usb_state.state) {
2453 case AB8500_BM_USB_STATE_RESET_HS:
2454 case AB8500_BM_USB_STATE_RESET_FS:
2455 case AB8500_BM_USB_STATE_SUSPEND:
2456 case AB8500_BM_USB_STATE_MAX:
2457 ab8500_charger_set_usb_connected(di, false);
2458 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2459 break;
2460
2461 case AB8500_BM_USB_STATE_RESUME:
2462 /*
2463 * when suspend->resume there should be delay
2464 * of 1sec for enabling charging
2465 */
2466 msleep(1000);
2467 /* Intentional fall through */
2468 case AB8500_BM_USB_STATE_CONFIGURED:
2469 /*
2470 * USB is configured, enable charging with the charging
2471 * input current obtained from USB driver
2472 */
2473 if (!ab8500_charger_get_usb_cur(di)) {
2474 /* Update maximum input current */
2475 ret = ab8500_charger_set_vbus_in_curr(di,
Lee Jonesf7470b52013-02-14 12:37:29 +00002476 di->max_usb_in_curr.usb_type_max);
Arun Murthy84edbee2012-02-29 21:54:26 +05302477 if (ret)
2478 return;
2479
2480 ab8500_charger_set_usb_connected(di, true);
2481 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2482 }
2483 break;
2484
2485 default:
2486 break;
2487 };
2488}
2489
2490/**
2491 * ab8500_charger_check_usbchargernotok_work() - check USB chg not ok status
2492 * @work: pointer to the work_struct structure
2493 *
2494 * Work queue function for checking the USB charger Not OK status
2495 */
2496static void ab8500_charger_check_usbchargernotok_work(struct work_struct *work)
2497{
2498 int ret;
2499 u8 reg_value;
2500 bool prev_status;
2501
2502 struct ab8500_charger *di = container_of(work,
2503 struct ab8500_charger, check_usbchgnotok_work.work);
2504
2505 /* Check if the status bit for usbchargernotok is still active */
2506 ret = abx500_get_register_interruptible(di->dev,
2507 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, &reg_value);
2508 if (ret < 0) {
2509 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2510 return;
2511 }
2512 prev_status = di->flags.usbchargernotok;
2513
2514 if (reg_value & VBUS_CH_NOK) {
2515 di->flags.usbchargernotok = true;
2516 /* Check again in 1sec */
2517 queue_delayed_work(di->charger_wq,
2518 &di->check_usbchgnotok_work, HZ);
2519 } else {
2520 di->flags.usbchargernotok = false;
2521 di->flags.vbus_collapse = false;
2522 }
2523
2524 if (prev_status != di->flags.usbchargernotok)
2525 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2526}
2527
2528/**
2529 * ab8500_charger_check_main_thermal_prot_work() - check main thermal status
2530 * @work: pointer to the work_struct structure
2531 *
2532 * Work queue function for checking the Main thermal prot status
2533 */
2534static void ab8500_charger_check_main_thermal_prot_work(
2535 struct work_struct *work)
2536{
2537 int ret;
2538 u8 reg_value;
2539
2540 struct ab8500_charger *di = container_of(work,
2541 struct ab8500_charger, check_main_thermal_prot_work);
2542
2543 /* Check if the status bit for main_thermal_prot is still active */
2544 ret = abx500_get_register_interruptible(di->dev,
2545 AB8500_CHARGER, AB8500_CH_STATUS2_REG, &reg_value);
2546 if (ret < 0) {
2547 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2548 return;
2549 }
2550 if (reg_value & MAIN_CH_TH_PROT)
2551 di->flags.main_thermal_prot = true;
2552 else
2553 di->flags.main_thermal_prot = false;
2554
2555 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2556}
2557
2558/**
2559 * ab8500_charger_check_usb_thermal_prot_work() - check usb thermal status
2560 * @work: pointer to the work_struct structure
2561 *
2562 * Work queue function for checking the USB thermal prot status
2563 */
2564static void ab8500_charger_check_usb_thermal_prot_work(
2565 struct work_struct *work)
2566{
2567 int ret;
2568 u8 reg_value;
2569
2570 struct ab8500_charger *di = container_of(work,
2571 struct ab8500_charger, check_usb_thermal_prot_work);
2572
2573 /* Check if the status bit for usb_thermal_prot is still active */
2574 ret = abx500_get_register_interruptible(di->dev,
2575 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, &reg_value);
2576 if (ret < 0) {
2577 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2578 return;
2579 }
2580 if (reg_value & USB_CH_TH_PROT)
2581 di->flags.usb_thermal_prot = true;
2582 else
2583 di->flags.usb_thermal_prot = false;
2584
2585 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2586}
2587
2588/**
2589 * ab8500_charger_mainchunplugdet_handler() - main charger unplugged
2590 * @irq: interrupt number
2591 * @_di: pointer to the ab8500_charger structure
2592 *
2593 * Returns IRQ status(IRQ_HANDLED)
2594 */
2595static irqreturn_t ab8500_charger_mainchunplugdet_handler(int irq, void *_di)
2596{
2597 struct ab8500_charger *di = _di;
2598
2599 dev_dbg(di->dev, "Main charger unplugged\n");
2600 queue_work(di->charger_wq, &di->ac_work);
2601
Lee Jonesb269fff2013-01-11 13:12:51 +00002602 cancel_delayed_work_sync(&di->ac_charger_attached_work);
2603 mutex_lock(&di->charger_attached_mutex);
2604 mutex_unlock(&di->charger_attached_mutex);
2605
Arun Murthy84edbee2012-02-29 21:54:26 +05302606 return IRQ_HANDLED;
2607}
2608
2609/**
2610 * ab8500_charger_mainchplugdet_handler() - main charger plugged
2611 * @irq: interrupt number
2612 * @_di: pointer to the ab8500_charger structure
2613 *
2614 * Returns IRQ status(IRQ_HANDLED)
2615 */
2616static irqreturn_t ab8500_charger_mainchplugdet_handler(int irq, void *_di)
2617{
2618 struct ab8500_charger *di = _di;
2619
2620 dev_dbg(di->dev, "Main charger plugged\n");
2621 queue_work(di->charger_wq, &di->ac_work);
2622
Lee Jonesb269fff2013-01-11 13:12:51 +00002623 mutex_lock(&di->charger_attached_mutex);
2624 mutex_unlock(&di->charger_attached_mutex);
2625 queue_delayed_work(di->charger_wq,
2626 &di->ac_charger_attached_work,
2627 HZ);
Arun Murthy84edbee2012-02-29 21:54:26 +05302628 return IRQ_HANDLED;
2629}
2630
2631/**
2632 * ab8500_charger_mainextchnotok_handler() - main charger not ok
2633 * @irq: interrupt number
2634 * @_di: pointer to the ab8500_charger structure
2635 *
2636 * Returns IRQ status(IRQ_HANDLED)
2637 */
2638static irqreturn_t ab8500_charger_mainextchnotok_handler(int irq, void *_di)
2639{
2640 struct ab8500_charger *di = _di;
2641
2642 dev_dbg(di->dev, "Main charger not ok\n");
2643 di->flags.mainextchnotok = true;
2644 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2645
2646 /* Schedule a new HW failure check */
2647 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
2648
2649 return IRQ_HANDLED;
2650}
2651
2652/**
2653 * ab8500_charger_mainchthprotr_handler() - Die temp is above main charger
2654 * thermal protection threshold
2655 * @irq: interrupt number
2656 * @_di: pointer to the ab8500_charger structure
2657 *
2658 * Returns IRQ status(IRQ_HANDLED)
2659 */
2660static irqreturn_t ab8500_charger_mainchthprotr_handler(int irq, void *_di)
2661{
2662 struct ab8500_charger *di = _di;
2663
2664 dev_dbg(di->dev,
2665 "Die temp above Main charger thermal protection threshold\n");
2666 queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
2667
2668 return IRQ_HANDLED;
2669}
2670
2671/**
2672 * ab8500_charger_mainchthprotf_handler() - Die temp is below main charger
2673 * thermal protection threshold
2674 * @irq: interrupt number
2675 * @_di: pointer to the ab8500_charger structure
2676 *
2677 * Returns IRQ status(IRQ_HANDLED)
2678 */
2679static irqreturn_t ab8500_charger_mainchthprotf_handler(int irq, void *_di)
2680{
2681 struct ab8500_charger *di = _di;
2682
2683 dev_dbg(di->dev,
2684 "Die temp ok for Main charger thermal protection threshold\n");
2685 queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
2686
2687 return IRQ_HANDLED;
2688}
2689
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002690static void ab8500_charger_vbus_drop_end_work(struct work_struct *work)
2691{
2692 struct ab8500_charger *di = container_of(work,
2693 struct ab8500_charger, vbus_drop_end_work.work);
Lee Jones861a30d2012-08-29 20:36:51 +08002694 int ret, curr;
Lee Jonesf7470b52013-02-14 12:37:29 +00002695 u8 reg_value;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002696
2697 di->flags.vbus_drop_end = false;
2698
2699 /* Reset the drop counter */
2700 abx500_set_register_interruptible(di->dev,
2701 AB8500_CHARGER, AB8500_CHARGER_CTRL, 0x01);
Lee Jones861a30d2012-08-29 20:36:51 +08002702
2703 if (is_ab8540(di->parent))
2704 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
2705 AB8540_CH_USBCH_STAT3_REG, &reg_value);
2706 else
2707 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
2708 AB8500_CH_USBCH_STAT2_REG, &reg_value);
Lee Jonesf7470b52013-02-14 12:37:29 +00002709 if (ret < 0) {
Lee Jones861a30d2012-08-29 20:36:51 +08002710 dev_err(di->dev, "%s read failed\n", __func__);
2711 return;
2712 }
2713
2714 if (is_ab8540(di->parent))
2715 curr = di->bm->chg_input_curr[
2716 reg_value & AB8540_AUTO_VBUS_IN_CURR_MASK];
2717 else
2718 curr = di->bm->chg_input_curr[
Lee Jonesf7470b52013-02-14 12:37:29 +00002719 reg_value >> AUTO_VBUS_IN_CURR_LIM_SHIFT];
Lee Jones861a30d2012-08-29 20:36:51 +08002720
2721 if (di->max_usb_in_curr.calculated_max != curr) {
2722 /* USB source is collapsing */
2723 di->max_usb_in_curr.calculated_max = curr;
2724 dev_dbg(di->dev,
2725 "VBUS input current limiting to %d mA\n",
2726 di->max_usb_in_curr.calculated_max);
2727 } else {
2728 /*
2729 * USB source can not give more than this amount.
2730 * Taking more will collapse the source.
2731 */
2732 di->max_usb_in_curr.set_max =
2733 di->max_usb_in_curr.calculated_max;
2734 dev_dbg(di->dev,
2735 "VBUS input current limited to %d mA\n",
2736 di->max_usb_in_curr.set_max);
Lee Jonesf7470b52013-02-14 12:37:29 +00002737 }
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002738
2739 if (di->usb.charger_connected)
Lee Jonesf7470b52013-02-14 12:37:29 +00002740 ab8500_charger_set_vbus_in_curr(di,
2741 di->max_usb_in_curr.usb_type_max);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002742}
2743
Arun Murthy84edbee2012-02-29 21:54:26 +05302744/**
2745 * ab8500_charger_vbusdetf_handler() - VBUS falling detected
2746 * @irq: interrupt number
2747 * @_di: pointer to the ab8500_charger structure
2748 *
2749 * Returns IRQ status(IRQ_HANDLED)
2750 */
2751static irqreturn_t ab8500_charger_vbusdetf_handler(int irq, void *_di)
2752{
2753 struct ab8500_charger *di = _di;
2754
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002755 di->vbus_detected = false;
Arun Murthy84edbee2012-02-29 21:54:26 +05302756 dev_dbg(di->dev, "VBUS falling detected\n");
2757 queue_work(di->charger_wq, &di->detect_usb_type_work);
2758
2759 return IRQ_HANDLED;
2760}
2761
2762/**
2763 * ab8500_charger_vbusdetr_handler() - VBUS rising detected
2764 * @irq: interrupt number
2765 * @_di: pointer to the ab8500_charger structure
2766 *
2767 * Returns IRQ status(IRQ_HANDLED)
2768 */
2769static irqreturn_t ab8500_charger_vbusdetr_handler(int irq, void *_di)
2770{
2771 struct ab8500_charger *di = _di;
2772
2773 di->vbus_detected = true;
2774 dev_dbg(di->dev, "VBUS rising detected\n");
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002775
Arun Murthy84edbee2012-02-29 21:54:26 +05302776 queue_work(di->charger_wq, &di->detect_usb_type_work);
2777
2778 return IRQ_HANDLED;
2779}
2780
2781/**
2782 * ab8500_charger_usblinkstatus_handler() - USB link status has changed
2783 * @irq: interrupt number
2784 * @_di: pointer to the ab8500_charger structure
2785 *
2786 * Returns IRQ status(IRQ_HANDLED)
2787 */
2788static irqreturn_t ab8500_charger_usblinkstatus_handler(int irq, void *_di)
2789{
2790 struct ab8500_charger *di = _di;
2791
2792 dev_dbg(di->dev, "USB link status changed\n");
2793
2794 queue_work(di->charger_wq, &di->usb_link_status_work);
2795
2796 return IRQ_HANDLED;
2797}
2798
2799/**
2800 * ab8500_charger_usbchthprotr_handler() - Die temp is above usb charger
2801 * thermal protection threshold
2802 * @irq: interrupt number
2803 * @_di: pointer to the ab8500_charger structure
2804 *
2805 * Returns IRQ status(IRQ_HANDLED)
2806 */
2807static irqreturn_t ab8500_charger_usbchthprotr_handler(int irq, void *_di)
2808{
2809 struct ab8500_charger *di = _di;
2810
2811 dev_dbg(di->dev,
2812 "Die temp above USB charger thermal protection threshold\n");
2813 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2814
2815 return IRQ_HANDLED;
2816}
2817
2818/**
2819 * ab8500_charger_usbchthprotf_handler() - Die temp is below usb charger
2820 * thermal protection threshold
2821 * @irq: interrupt number
2822 * @_di: pointer to the ab8500_charger structure
2823 *
2824 * Returns IRQ status(IRQ_HANDLED)
2825 */
2826static irqreturn_t ab8500_charger_usbchthprotf_handler(int irq, void *_di)
2827{
2828 struct ab8500_charger *di = _di;
2829
2830 dev_dbg(di->dev,
2831 "Die temp ok for USB charger thermal protection threshold\n");
2832 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2833
2834 return IRQ_HANDLED;
2835}
2836
2837/**
2838 * ab8500_charger_usbchargernotokr_handler() - USB charger not ok detected
2839 * @irq: interrupt number
2840 * @_di: pointer to the ab8500_charger structure
2841 *
2842 * Returns IRQ status(IRQ_HANDLED)
2843 */
2844static irqreturn_t ab8500_charger_usbchargernotokr_handler(int irq, void *_di)
2845{
2846 struct ab8500_charger *di = _di;
2847
2848 dev_dbg(di->dev, "Not allowed USB charger detected\n");
2849 queue_delayed_work(di->charger_wq, &di->check_usbchgnotok_work, 0);
2850
2851 return IRQ_HANDLED;
2852}
2853
2854/**
2855 * ab8500_charger_chwdexp_handler() - Charger watchdog expired
2856 * @irq: interrupt number
2857 * @_di: pointer to the ab8500_charger structure
2858 *
2859 * Returns IRQ status(IRQ_HANDLED)
2860 */
2861static irqreturn_t ab8500_charger_chwdexp_handler(int irq, void *_di)
2862{
2863 struct ab8500_charger *di = _di;
2864
2865 dev_dbg(di->dev, "Charger watchdog expired\n");
2866
2867 /*
2868 * The charger that was online when the watchdog expired
2869 * needs to be restarted for charging to start again
2870 */
2871 if (di->ac.charger_online) {
2872 di->ac.wd_expired = true;
2873 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2874 }
2875 if (di->usb.charger_online) {
2876 di->usb.wd_expired = true;
2877 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2878 }
2879
2880 return IRQ_HANDLED;
2881}
2882
2883/**
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002884 * ab8500_charger_vbuschdropend_handler() - VBUS drop removed
2885 * @irq: interrupt number
2886 * @_di: pointer to the ab8500_charger structure
2887 *
2888 * Returns IRQ status(IRQ_HANDLED)
2889 */
2890static irqreturn_t ab8500_charger_vbuschdropend_handler(int irq, void *_di)
2891{
2892 struct ab8500_charger *di = _di;
2893
2894 dev_dbg(di->dev, "VBUS charger drop ended\n");
2895 di->flags.vbus_drop_end = true;
Lee Jonesf7470b52013-02-14 12:37:29 +00002896
2897 /*
2898 * VBUS might have dropped due to bad connection.
2899 * Schedule a new input limit set to the value SW requests.
2900 */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002901 queue_delayed_work(di->charger_wq, &di->vbus_drop_end_work,
Lee Jonesf7470b52013-02-14 12:37:29 +00002902 round_jiffies(VBUS_IN_CURR_LIM_RETRY_SET_TIME * HZ));
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002903
2904 return IRQ_HANDLED;
2905}
2906
2907/**
Arun Murthy84edbee2012-02-29 21:54:26 +05302908 * ab8500_charger_vbusovv_handler() - VBUS overvoltage detected
2909 * @irq: interrupt number
2910 * @_di: pointer to the ab8500_charger structure
2911 *
2912 * Returns IRQ status(IRQ_HANDLED)
2913 */
2914static irqreturn_t ab8500_charger_vbusovv_handler(int irq, void *_di)
2915{
2916 struct ab8500_charger *di = _di;
2917
2918 dev_dbg(di->dev, "VBUS overvoltage detected\n");
2919 di->flags.vbus_ovv = true;
2920 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2921
2922 /* Schedule a new HW failure check */
2923 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
2924
2925 return IRQ_HANDLED;
2926}
2927
2928/**
2929 * ab8500_charger_ac_get_property() - get the ac/mains properties
2930 * @psy: pointer to the power_supply structure
2931 * @psp: pointer to the power_supply_property structure
2932 * @val: pointer to the power_supply_propval union
2933 *
2934 * This function gets called when an application tries to get the ac/mains
2935 * properties by reading the sysfs files.
2936 * AC/Mains properties are online, present and voltage.
2937 * online: ac/mains charging is in progress or not
2938 * present: presence of the ac/mains
2939 * voltage: AC/Mains voltage
2940 * Returns error code in case of failure else 0(on success)
2941 */
2942static int ab8500_charger_ac_get_property(struct power_supply *psy,
2943 enum power_supply_property psp,
2944 union power_supply_propval *val)
2945{
2946 struct ab8500_charger *di;
Jonas Aaberga864c5a2013-01-11 13:12:53 +00002947 int ret;
Arun Murthy84edbee2012-02-29 21:54:26 +05302948
2949 di = to_ab8500_charger_ac_device_info(psy_to_ux500_charger(psy));
2950
2951 switch (psp) {
2952 case POWER_SUPPLY_PROP_HEALTH:
2953 if (di->flags.mainextchnotok)
2954 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
2955 else if (di->ac.wd_expired || di->usb.wd_expired)
2956 val->intval = POWER_SUPPLY_HEALTH_DEAD;
2957 else if (di->flags.main_thermal_prot)
2958 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
2959 else
2960 val->intval = POWER_SUPPLY_HEALTH_GOOD;
2961 break;
2962 case POWER_SUPPLY_PROP_ONLINE:
2963 val->intval = di->ac.charger_online;
2964 break;
2965 case POWER_SUPPLY_PROP_PRESENT:
2966 val->intval = di->ac.charger_connected;
2967 break;
2968 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Jonas Aaberga864c5a2013-01-11 13:12:53 +00002969 ret = ab8500_charger_get_ac_voltage(di);
2970 if (ret >= 0)
2971 di->ac.charger_voltage = ret;
2972 /* On error, use previous value */
Arun Murthy84edbee2012-02-29 21:54:26 +05302973 val->intval = di->ac.charger_voltage * 1000;
2974 break;
2975 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
2976 /*
2977 * This property is used to indicate when CV mode is entered
2978 * for the AC charger
2979 */
2980 di->ac.cv_active = ab8500_charger_ac_cv(di);
2981 val->intval = di->ac.cv_active;
2982 break;
2983 case POWER_SUPPLY_PROP_CURRENT_NOW:
Jonas Aaberga864c5a2013-01-11 13:12:53 +00002984 ret = ab8500_charger_get_ac_current(di);
2985 if (ret >= 0)
2986 di->ac.charger_current = ret;
2987 val->intval = di->ac.charger_current * 1000;
Arun Murthy84edbee2012-02-29 21:54:26 +05302988 break;
2989 default:
2990 return -EINVAL;
2991 }
2992 return 0;
2993}
2994
2995/**
2996 * ab8500_charger_usb_get_property() - get the usb properties
2997 * @psy: pointer to the power_supply structure
2998 * @psp: pointer to the power_supply_property structure
2999 * @val: pointer to the power_supply_propval union
3000 *
3001 * This function gets called when an application tries to get the usb
3002 * properties by reading the sysfs files.
3003 * USB properties are online, present and voltage.
3004 * online: usb charging is in progress or not
3005 * present: presence of the usb
3006 * voltage: vbus voltage
3007 * Returns error code in case of failure else 0(on success)
3008 */
3009static int ab8500_charger_usb_get_property(struct power_supply *psy,
3010 enum power_supply_property psp,
3011 union power_supply_propval *val)
3012{
3013 struct ab8500_charger *di;
Jonas Aaberga864c5a2013-01-11 13:12:53 +00003014 int ret;
Arun Murthy84edbee2012-02-29 21:54:26 +05303015
3016 di = to_ab8500_charger_usb_device_info(psy_to_ux500_charger(psy));
3017
3018 switch (psp) {
3019 case POWER_SUPPLY_PROP_HEALTH:
3020 if (di->flags.usbchargernotok)
3021 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
3022 else if (di->ac.wd_expired || di->usb.wd_expired)
3023 val->intval = POWER_SUPPLY_HEALTH_DEAD;
3024 else if (di->flags.usb_thermal_prot)
3025 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
3026 else if (di->flags.vbus_ovv)
3027 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
3028 else
3029 val->intval = POWER_SUPPLY_HEALTH_GOOD;
3030 break;
3031 case POWER_SUPPLY_PROP_ONLINE:
3032 val->intval = di->usb.charger_online;
3033 break;
3034 case POWER_SUPPLY_PROP_PRESENT:
3035 val->intval = di->usb.charger_connected;
3036 break;
3037 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Jonas Aaberga864c5a2013-01-11 13:12:53 +00003038 ret = ab8500_charger_get_vbus_voltage(di);
3039 if (ret >= 0)
3040 di->usb.charger_voltage = ret;
Arun Murthy84edbee2012-02-29 21:54:26 +05303041 val->intval = di->usb.charger_voltage * 1000;
3042 break;
3043 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
3044 /*
3045 * This property is used to indicate when CV mode is entered
3046 * for the USB charger
3047 */
3048 di->usb.cv_active = ab8500_charger_usb_cv(di);
3049 val->intval = di->usb.cv_active;
3050 break;
3051 case POWER_SUPPLY_PROP_CURRENT_NOW:
Jonas Aaberga864c5a2013-01-11 13:12:53 +00003052 ret = ab8500_charger_get_usb_current(di);
3053 if (ret >= 0)
3054 di->usb.charger_current = ret;
3055 val->intval = di->usb.charger_current * 1000;
Arun Murthy84edbee2012-02-29 21:54:26 +05303056 break;
3057 case POWER_SUPPLY_PROP_CURRENT_AVG:
3058 /*
3059 * This property is used to indicate when VBUS has collapsed
3060 * due to too high output current from the USB charger
3061 */
3062 if (di->flags.vbus_collapse)
3063 val->intval = 1;
3064 else
3065 val->intval = 0;
3066 break;
3067 default:
3068 return -EINVAL;
3069 }
3070 return 0;
3071}
3072
3073/**
3074 * ab8500_charger_init_hw_registers() - Set up charger related registers
3075 * @di: pointer to the ab8500_charger structure
3076 *
3077 * Set up charger OVV, watchdog and maximum voltage registers as well as
3078 * charging of the backup battery
3079 */
3080static int ab8500_charger_init_hw_registers(struct ab8500_charger *di)
3081{
3082 int ret = 0;
Yang QU0f4aa402012-06-26 19:25:52 +08003083 u8 bup_vch_range = 0, vbup33_vrtcn = 0;
Arun Murthy84edbee2012-02-29 21:54:26 +05303084
3085 /* Setup maximum charger current and voltage for ABB cut2.0 */
3086 if (!is_ab8500_1p1_or_earlier(di->parent)) {
3087 ret = abx500_set_register_interruptible(di->dev,
3088 AB8500_CHARGER,
3089 AB8500_CH_VOLT_LVL_MAX_REG, CH_VOL_LVL_4P6);
3090 if (ret) {
3091 dev_err(di->dev,
3092 "failed to set CH_VOLT_LVL_MAX_REG\n");
3093 goto out;
3094 }
3095
Lee Jones861a30d2012-08-29 20:36:51 +08003096 if (is_ab8540(di->parent))
3097 ret = abx500_set_register_interruptible(di->dev,
3098 AB8500_CHARGER, AB8500_CH_OPT_CRNTLVL_MAX_REG,
3099 CH_OP_CUR_LVL_2P);
3100 else
3101 ret = abx500_set_register_interruptible(di->dev,
3102 AB8500_CHARGER, AB8500_CH_OPT_CRNTLVL_MAX_REG,
3103 CH_OP_CUR_LVL_1P6);
Arun Murthy84edbee2012-02-29 21:54:26 +05303104 if (ret) {
3105 dev_err(di->dev,
3106 "failed to set CH_OPT_CRNTLVL_MAX_REG\n");
3107 goto out;
3108 }
3109 }
3110
Lee Jones861a30d2012-08-29 20:36:51 +08003111 if (is_ab9540_2p0(di->parent) || is_ab9540_3p0(di->parent)
3112 || is_ab8505_2p0(di->parent) || is_ab8540(di->parent))
Jonas Aaberg0ed51072012-05-11 12:42:25 +02003113 ret = abx500_mask_and_set_register_interruptible(di->dev,
3114 AB8500_CHARGER,
3115 AB8500_USBCH_CTRL2_REG,
3116 VBUS_AUTO_IN_CURR_LIM_ENA,
3117 VBUS_AUTO_IN_CURR_LIM_ENA);
3118 else
3119 /*
3120 * VBUS OVV set to 6.3V and enable automatic current limitation
3121 */
3122 ret = abx500_set_register_interruptible(di->dev,
3123 AB8500_CHARGER,
3124 AB8500_USBCH_CTRL2_REG,
3125 VBUS_OVV_SELECT_6P3V | VBUS_AUTO_IN_CURR_LIM_ENA);
Arun Murthy84edbee2012-02-29 21:54:26 +05303126 if (ret) {
Jonas Aaberg0ed51072012-05-11 12:42:25 +02003127 dev_err(di->dev,
3128 "failed to set automatic current limitation\n");
Arun Murthy84edbee2012-02-29 21:54:26 +05303129 goto out;
3130 }
3131
3132 /* Enable main watchdog in OTP */
3133 ret = abx500_set_register_interruptible(di->dev,
3134 AB8500_OTP_EMUL, AB8500_OTP_CONF_15, OTP_ENABLE_WD);
3135 if (ret) {
3136 dev_err(di->dev, "failed to enable main WD in OTP\n");
3137 goto out;
3138 }
3139
3140 /* Enable main watchdog */
3141 ret = abx500_set_register_interruptible(di->dev,
3142 AB8500_SYS_CTRL2_BLOCK,
3143 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_ENA);
3144 if (ret) {
3145 dev_err(di->dev, "faile to enable main watchdog\n");
3146 goto out;
3147 }
3148
3149 /*
3150 * Due to internal synchronisation, Enable and Kick watchdog bits
3151 * cannot be enabled in a single write.
3152 * A minimum delay of 2*32 kHz period (62.5µs) must be inserted
3153 * between writing Enable then Kick bits.
3154 */
3155 udelay(63);
3156
3157 /* Kick main watchdog */
3158 ret = abx500_set_register_interruptible(di->dev,
3159 AB8500_SYS_CTRL2_BLOCK,
3160 AB8500_MAIN_WDOG_CTRL_REG,
3161 (MAIN_WDOG_ENA | MAIN_WDOG_KICK));
3162 if (ret) {
3163 dev_err(di->dev, "failed to kick main watchdog\n");
3164 goto out;
3165 }
3166
3167 /* Disable main watchdog */
3168 ret = abx500_set_register_interruptible(di->dev,
3169 AB8500_SYS_CTRL2_BLOCK,
3170 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_DIS);
3171 if (ret) {
3172 dev_err(di->dev, "failed to disable main watchdog\n");
3173 goto out;
3174 }
3175
3176 /* Set watchdog timeout */
3177 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
3178 AB8500_CH_WD_TIMER_REG, WD_TIMER);
3179 if (ret) {
3180 dev_err(di->dev, "failed to set charger watchdog timeout\n");
3181 goto out;
3182 }
3183
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003184 /* Set charger watchdog timeout */
3185 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
3186 AB8500_CH_WD_TIMER_REG, WD_TIMER);
3187 if (ret) {
3188 dev_err(di->dev, "failed to set charger watchdog timeout\n");
3189 goto out;
3190 }
3191
3192 ret = ab8500_charger_led_en(di, false);
3193 if (ret < 0) {
3194 dev_err(di->dev, "failed to disable LED\n");
3195 goto out;
3196 }
3197
Arun Murthy84edbee2012-02-29 21:54:26 +05303198 /* Backup battery voltage and current */
Yang QU0f4aa402012-06-26 19:25:52 +08003199 if (di->bm->bkup_bat_v > BUP_VCH_SEL_3P1V)
3200 bup_vch_range = BUP_VCH_RANGE;
3201 if (di->bm->bkup_bat_v == BUP_VCH_SEL_3P3V)
3202 vbup33_vrtcn = VBUP33_VRTCN;
3203
Arun Murthy84edbee2012-02-29 21:54:26 +05303204 ret = abx500_set_register_interruptible(di->dev,
3205 AB8500_RTC,
3206 AB8500_RTC_BACKUP_CHG_REG,
Yang QU0f4aa402012-06-26 19:25:52 +08003207 (di->bm->bkup_bat_v & 0x3) | di->bm->bkup_bat_i);
Arun Murthy84edbee2012-02-29 21:54:26 +05303208 if (ret) {
3209 dev_err(di->dev, "failed to setup backup battery charging\n");
3210 goto out;
3211 }
Yang QU0f4aa402012-06-26 19:25:52 +08003212 if (is_ab8540(di->parent)) {
3213 ret = abx500_set_register_interruptible(di->dev,
3214 AB8500_RTC,
3215 AB8500_RTC_CTRL1_REG,
3216 bup_vch_range | vbup33_vrtcn);
3217 if (ret) {
Lee Jones861a30d2012-08-29 20:36:51 +08003218 dev_err(di->dev,
3219 "failed to setup backup battery charging\n");
Yang QU0f4aa402012-06-26 19:25:52 +08003220 goto out;
3221 }
3222 }
Arun Murthy84edbee2012-02-29 21:54:26 +05303223
3224 /* Enable backup battery charging */
3225 abx500_mask_and_set_register_interruptible(di->dev,
3226 AB8500_RTC, AB8500_RTC_CTRL_REG,
3227 RTC_BUP_CH_ENA, RTC_BUP_CH_ENA);
3228 if (ret < 0)
3229 dev_err(di->dev, "%s mask and set failed\n", __func__);
3230
Lee Jonesdb43e6c2013-02-14 12:39:15 +00003231 if (is_ab8540(di->parent)) {
3232 ret = abx500_mask_and_set_register_interruptible(di->dev,
3233 AB8500_CHARGER, AB8540_USB_PP_MODE_REG,
3234 BUS_VSYS_VOL_SELECT_MASK, BUS_VSYS_VOL_SELECT_3P6V);
3235 if (ret) {
Lee Jones861a30d2012-08-29 20:36:51 +08003236 dev_err(di->dev,
3237 "failed to setup usb power path vsys voltage\n");
Lee Jonesdb43e6c2013-02-14 12:39:15 +00003238 goto out;
3239 }
3240 ret = abx500_mask_and_set_register_interruptible(di->dev,
3241 AB8500_CHARGER, AB8540_USB_PP_CHR_REG,
3242 BUS_PP_PRECHG_CURRENT_MASK, 0);
3243 if (ret) {
Lee Jones861a30d2012-08-29 20:36:51 +08003244 dev_err(di->dev,
3245 "failed to setup usb power path prechage current\n");
Lee Jonesdb43e6c2013-02-14 12:39:15 +00003246 goto out;
3247 }
3248 }
3249
Arun Murthy84edbee2012-02-29 21:54:26 +05303250out:
3251 return ret;
3252}
3253
3254/*
3255 * ab8500 charger driver interrupts and their respective isr
3256 */
3257static struct ab8500_charger_interrupts ab8500_charger_irq[] = {
3258 {"MAIN_CH_UNPLUG_DET", ab8500_charger_mainchunplugdet_handler},
3259 {"MAIN_CHARGE_PLUG_DET", ab8500_charger_mainchplugdet_handler},
3260 {"MAIN_EXT_CH_NOT_OK", ab8500_charger_mainextchnotok_handler},
3261 {"MAIN_CH_TH_PROT_R", ab8500_charger_mainchthprotr_handler},
3262 {"MAIN_CH_TH_PROT_F", ab8500_charger_mainchthprotf_handler},
3263 {"VBUS_DET_F", ab8500_charger_vbusdetf_handler},
3264 {"VBUS_DET_R", ab8500_charger_vbusdetr_handler},
3265 {"USB_LINK_STATUS", ab8500_charger_usblinkstatus_handler},
3266 {"USB_CH_TH_PROT_R", ab8500_charger_usbchthprotr_handler},
3267 {"USB_CH_TH_PROT_F", ab8500_charger_usbchthprotf_handler},
3268 {"USB_CHARGER_NOT_OKR", ab8500_charger_usbchargernotokr_handler},
3269 {"VBUS_OVV", ab8500_charger_vbusovv_handler},
3270 {"CH_WD_EXP", ab8500_charger_chwdexp_handler},
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003271 {"VBUS_CH_DROP_END", ab8500_charger_vbuschdropend_handler},
Arun Murthy84edbee2012-02-29 21:54:26 +05303272};
3273
3274static int ab8500_charger_usb_notifier_call(struct notifier_block *nb,
3275 unsigned long event, void *power)
3276{
3277 struct ab8500_charger *di =
3278 container_of(nb, struct ab8500_charger, nb);
3279 enum ab8500_usb_state bm_usb_state;
3280 unsigned mA = *((unsigned *)power);
3281
Lee Jonesc9ade0f2013-01-21 11:22:56 +00003282 if (!di)
3283 return NOTIFY_DONE;
3284
Arun Murthy84edbee2012-02-29 21:54:26 +05303285 if (event != USB_EVENT_VBUS) {
3286 dev_dbg(di->dev, "not a standard host, returning\n");
3287 return NOTIFY_DONE;
3288 }
3289
3290 /* TODO: State is fabricate here. See if charger really needs USB
3291 * state or if mA is enough
3292 */
3293 if ((di->usb_state.usb_current == 2) && (mA > 2))
3294 bm_usb_state = AB8500_BM_USB_STATE_RESUME;
3295 else if (mA == 0)
3296 bm_usb_state = AB8500_BM_USB_STATE_RESET_HS;
3297 else if (mA == 2)
3298 bm_usb_state = AB8500_BM_USB_STATE_SUSPEND;
3299 else if (mA >= 8) /* 8, 100, 500 */
3300 bm_usb_state = AB8500_BM_USB_STATE_CONFIGURED;
3301 else /* Should never occur */
3302 bm_usb_state = AB8500_BM_USB_STATE_RESET_FS;
3303
3304 dev_dbg(di->dev, "%s usb_state: 0x%02x mA: %d\n",
3305 __func__, bm_usb_state, mA);
3306
3307 spin_lock(&di->usb_state.usb_lock);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003308 di->usb_state.state_tmp = bm_usb_state;
3309 di->usb_state.usb_current_tmp = mA;
Arun Murthy84edbee2012-02-29 21:54:26 +05303310 spin_unlock(&di->usb_state.usb_lock);
3311
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003312 /*
3313 * wait for some time until you get updates from the usb stack
3314 * and negotiations are completed
3315 */
3316 queue_delayed_work(di->charger_wq, &di->usb_state_changed_work, HZ/2);
Arun Murthy84edbee2012-02-29 21:54:26 +05303317
3318 return NOTIFY_OK;
3319}
3320
3321#if defined(CONFIG_PM)
3322static int ab8500_charger_resume(struct platform_device *pdev)
3323{
3324 int ret;
3325 struct ab8500_charger *di = platform_get_drvdata(pdev);
3326
3327 /*
3328 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
3329 * logic. That means we have to continously kick the charger
3330 * watchdog even when no charger is connected. This is only
3331 * valid once the AC charger has been enabled. This is
3332 * a bug that is not handled by the algorithm and the
3333 * watchdog have to be kicked by the charger driver
3334 * when the AC charger is disabled
3335 */
3336 if (di->ac_conn && is_ab8500_1p1_or_earlier(di->parent)) {
3337 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
3338 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
3339 if (ret)
3340 dev_err(di->dev, "Failed to kick WD!\n");
3341
3342 /* If not already pending start a new timer */
3343 if (!delayed_work_pending(
3344 &di->kick_wd_work)) {
3345 queue_delayed_work(di->charger_wq, &di->kick_wd_work,
3346 round_jiffies(WD_KICK_INTERVAL));
3347 }
3348 }
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
3367 /* Cancel any pending HW failure check */
3368 if (delayed_work_pending(&di->check_hw_failure_work))
3369 cancel_delayed_work(&di->check_hw_failure_work);
3370
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003371 if (delayed_work_pending(&di->vbus_drop_end_work))
3372 cancel_delayed_work(&di->vbus_drop_end_work);
3373
Jonas Aaberg53ef1f52012-05-21 16:05:01 +02003374 flush_delayed_work(&di->attach_work);
3375 flush_delayed_work(&di->usb_charger_attached_work);
3376 flush_delayed_work(&di->ac_charger_attached_work);
3377 flush_delayed_work(&di->check_usbchgnotok_work);
3378 flush_delayed_work(&di->check_vbat_work);
3379 flush_delayed_work(&di->kick_wd_work);
3380
3381 flush_work(&di->usb_link_status_work);
3382 flush_work(&di->ac_work);
3383 flush_work(&di->detect_usb_type_work);
3384
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003385 if (atomic_read(&di->current_stepping_sessions))
3386 return -EAGAIN;
3387
Arun Murthy84edbee2012-02-29 21:54:26 +05303388 return 0;
3389}
3390#else
3391#define ab8500_charger_suspend NULL
3392#define ab8500_charger_resume NULL
3393#endif
3394
Lee Jones88917162013-02-13 11:39:19 +00003395static struct notifier_block charger_nb = {
3396 .notifier_call = ab8500_external_charger_prepare,
3397};
3398
Bill Pemberton415ec692012-11-19 13:26:07 -05003399static int ab8500_charger_remove(struct platform_device *pdev)
Arun Murthy84edbee2012-02-29 21:54:26 +05303400{
3401 struct ab8500_charger *di = platform_get_drvdata(pdev);
3402 int i, irq, ret;
3403
3404 /* Disable AC charging */
3405 ab8500_charger_ac_en(&di->ac_chg, false, 0, 0);
3406
3407 /* Disable USB charging */
3408 ab8500_charger_usb_en(&di->usb_chg, false, 0, 0);
3409
3410 /* Disable interrupts */
3411 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
3412 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3413 free_irq(irq, di);
3414 }
3415
Arun Murthy84edbee2012-02-29 21:54:26 +05303416 /* Backup battery voltage and current disable */
3417 ret = abx500_mask_and_set_register_interruptible(di->dev,
3418 AB8500_RTC, AB8500_RTC_CTRL_REG, RTC_BUP_CH_ENA, 0);
3419 if (ret < 0)
3420 dev_err(di->dev, "%s mask and set failed\n", __func__);
3421
Anton Vorontsovefd71c82012-03-14 04:22:17 +04003422 usb_unregister_notifier(di->usb_phy, &di->nb);
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +05303423 usb_put_phy(di->usb_phy);
Arun Murthy84edbee2012-02-29 21:54:26 +05303424
3425 /* Delete the work queue */
3426 destroy_workqueue(di->charger_wq);
3427
Lee Jones88917162013-02-13 11:39:19 +00003428 /* Unregister external charger enable notifier */
3429 if (!di->ac_chg.enabled)
3430 blocking_notifier_chain_unregister(
3431 &charger_notifier_list, &charger_nb);
3432
Arun Murthy84edbee2012-02-29 21:54:26 +05303433 flush_scheduled_work();
Lee Jones72a90dd2013-02-14 09:22:48 +00003434 if (di->usb_chg.enabled)
Michel JAOUEN01ec8c52012-04-26 10:00:04 +02003435 power_supply_unregister(&di->usb_chg.psy);
Marcus Cooper405fea12012-08-10 12:59:06 +02003436
3437 if (di->ac_chg.enabled && !di->ac_chg.external)
Michel JAOUEN01ec8c52012-04-26 10:00:04 +02003438 power_supply_unregister(&di->ac_chg.psy);
Marcus Cooper405fea12012-08-10 12:59:06 +02003439
Arun Murthy84edbee2012-02-29 21:54:26 +05303440 platform_set_drvdata(pdev, NULL);
Arun Murthy84edbee2012-02-29 21:54:26 +05303441
3442 return 0;
3443}
3444
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08003445static char *supply_interface[] = {
3446 "ab8500_chargalg",
3447 "ab8500_fg",
3448 "ab8500_btemp",
3449};
3450
Bill Pembertonc8afa642012-11-19 13:22:23 -05003451static int ab8500_charger_probe(struct platform_device *pdev)
Arun Murthy84edbee2012-02-29 21:54:26 +05303452{
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08003453 struct device_node *np = pdev->dev.of_node;
Lee Jones7722b792012-11-30 10:56:28 +00003454 struct abx500_bm_data *plat = pdev->dev.platform_data;
Lee Jones2aac3de2012-05-05 04:38:19 -07003455 struct ab8500_charger *di;
Lee Jonesb269fff2013-01-11 13:12:51 +00003456 int irq, i, charger_status, ret = 0, ch_stat;
Arun Murthy84edbee2012-02-29 21:54:26 +05303457
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08003458 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
3459 if (!di) {
3460 dev_err(&pdev->dev, "%s no mem for ab8500_charger\n", __func__);
Arun Murthy84edbee2012-02-29 21:54:26 +05303461 return -ENOMEM;
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08003462 }
Lee Jones7722b792012-11-30 10:56:28 +00003463
3464 if (!plat) {
3465 dev_err(&pdev->dev, "no battery management data supplied\n");
3466 return -EINVAL;
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08003467 }
Lee Jones7722b792012-11-30 10:56:28 +00003468 di->bm = plat;
3469
3470 if (np) {
3471 ret = ab8500_bm_of_probe(&pdev->dev, np, di->bm);
3472 if (ret) {
3473 dev_err(&pdev->dev, "failed to get battery information\n");
3474 return ret;
3475 }
3476 di->autopower_cfg = of_property_read_bool(np, "autopower_cfg");
3477 } else
3478 di->autopower_cfg = false;
Arun Murthy84edbee2012-02-29 21:54:26 +05303479
3480 /* get parent data */
3481 di->dev = &pdev->dev;
3482 di->parent = dev_get_drvdata(pdev->dev.parent);
3483 di->gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
3484
3485 /* initialize lock */
3486 spin_lock_init(&di->usb_state.usb_lock);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003487 mutex_init(&di->usb_ipt_crnt_lock);
Arun Murthy84edbee2012-02-29 21:54:26 +05303488
Arun Murthy84edbee2012-02-29 21:54:26 +05303489 di->autopower = false;
Henrik Sölverff380902012-04-17 15:51:01 +02003490 di->invalid_charger_detect_state = 0;
Arun Murthy84edbee2012-02-29 21:54:26 +05303491
3492 /* AC supply */
3493 /* power_supply base class */
3494 di->ac_chg.psy.name = "ab8500_ac";
3495 di->ac_chg.psy.type = POWER_SUPPLY_TYPE_MAINS;
3496 di->ac_chg.psy.properties = ab8500_charger_ac_props;
3497 di->ac_chg.psy.num_properties = ARRAY_SIZE(ab8500_charger_ac_props);
3498 di->ac_chg.psy.get_property = ab8500_charger_ac_get_property;
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08003499 di->ac_chg.psy.supplied_to = supply_interface;
3500 di->ac_chg.psy.num_supplicants = ARRAY_SIZE(supply_interface),
Arun Murthy84edbee2012-02-29 21:54:26 +05303501 /* ux500_charger sub-class */
3502 di->ac_chg.ops.enable = &ab8500_charger_ac_en;
Lee Jones4dcdf572013-02-14 09:24:10 +00003503 di->ac_chg.ops.check_enable = &ab8500_charger_ac_check_enable;
Arun Murthy84edbee2012-02-29 21:54:26 +05303504 di->ac_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
3505 di->ac_chg.ops.update_curr = &ab8500_charger_update_charger_current;
3506 di->ac_chg.max_out_volt = ab8500_charger_voltage_map[
3507 ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
Lee Jones861a30d2012-08-29 20:36:51 +08003508 di->ac_chg.max_out_curr =
3509 di->bm->chg_output_curr[di->bm->n_chg_out_curr - 1];
Loic Pallardye07a5642012-05-10 15:33:56 +02003510 di->ac_chg.wdt_refresh = CHG_WD_INTERVAL;
Lee Jones97034a12013-01-17 16:08:42 +00003511 di->ac_chg.enabled = di->bm->ac_enabled;
Loic Pallardye07a5642012-05-10 15:33:56 +02003512 di->ac_chg.external = false;
Arun Murthy84edbee2012-02-29 21:54:26 +05303513
Lee Jones88917162013-02-13 11:39:19 +00003514 /*notifier for external charger enabling*/
3515 if (!di->ac_chg.enabled)
3516 blocking_notifier_chain_register(
3517 &charger_notifier_list, &charger_nb);
3518
Arun Murthy84edbee2012-02-29 21:54:26 +05303519 /* USB supply */
3520 /* power_supply base class */
3521 di->usb_chg.psy.name = "ab8500_usb";
3522 di->usb_chg.psy.type = POWER_SUPPLY_TYPE_USB;
3523 di->usb_chg.psy.properties = ab8500_charger_usb_props;
3524 di->usb_chg.psy.num_properties = ARRAY_SIZE(ab8500_charger_usb_props);
3525 di->usb_chg.psy.get_property = ab8500_charger_usb_get_property;
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08003526 di->usb_chg.psy.supplied_to = supply_interface;
3527 di->usb_chg.psy.num_supplicants = ARRAY_SIZE(supply_interface),
Arun Murthy84edbee2012-02-29 21:54:26 +05303528 /* ux500_charger sub-class */
3529 di->usb_chg.ops.enable = &ab8500_charger_usb_en;
Lee Jones4dcdf572013-02-14 09:24:10 +00003530 di->usb_chg.ops.check_enable = &ab8500_charger_usb_check_enable;
Arun Murthy84edbee2012-02-29 21:54:26 +05303531 di->usb_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
3532 di->usb_chg.ops.update_curr = &ab8500_charger_update_charger_current;
Lee Jonesdb43e6c2013-02-14 12:39:15 +00003533 di->usb_chg.ops.pp_enable = &ab8540_charger_power_path_enable;
3534 di->usb_chg.ops.pre_chg_enable = &ab8540_charger_usb_pre_chg_enable;
Arun Murthy84edbee2012-02-29 21:54:26 +05303535 di->usb_chg.max_out_volt = ab8500_charger_voltage_map[
3536 ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
Lee Jones861a30d2012-08-29 20:36:51 +08003537 di->usb_chg.max_out_curr =
3538 di->bm->chg_output_curr[di->bm->n_chg_out_curr - 1];
Loic Pallardye07a5642012-05-10 15:33:56 +02003539 di->usb_chg.wdt_refresh = CHG_WD_INTERVAL;
Lee Jones97034a12013-01-17 16:08:42 +00003540 di->usb_chg.enabled = di->bm->usb_enabled;
Loic Pallardye07a5642012-05-10 15:33:56 +02003541 di->usb_chg.external = false;
Lee Jonesdb43e6c2013-02-14 12:39:15 +00003542 di->usb_chg.power_path = di->bm->usb_power_path;
Lee Jonesf7470b52013-02-14 12:37:29 +00003543 di->usb_state.usb_current = -1;
Arun Murthy84edbee2012-02-29 21:54:26 +05303544
3545 /* Create a work queue for the charger */
3546 di->charger_wq =
3547 create_singlethread_workqueue("ab8500_charger_wq");
3548 if (di->charger_wq == NULL) {
3549 dev_err(di->dev, "failed to create work queue\n");
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08003550 return -ENOMEM;
Arun Murthy84edbee2012-02-29 21:54:26 +05303551 }
3552
Lee Jonesb269fff2013-01-11 13:12:51 +00003553 mutex_init(&di->charger_attached_mutex);
3554
Arun Murthy84edbee2012-02-29 21:54:26 +05303555 /* Init work for HW failure check */
Tejun Heo203b42f2012-08-21 13:18:23 -07003556 INIT_DEFERRABLE_WORK(&di->check_hw_failure_work,
Arun Murthy84edbee2012-02-29 21:54:26 +05303557 ab8500_charger_check_hw_failure_work);
Tejun Heo203b42f2012-08-21 13:18:23 -07003558 INIT_DEFERRABLE_WORK(&di->check_usbchgnotok_work,
Arun Murthy84edbee2012-02-29 21:54:26 +05303559 ab8500_charger_check_usbchargernotok_work);
3560
Lee Jonesb269fff2013-01-11 13:12:51 +00003561 INIT_DELAYED_WORK(&di->ac_charger_attached_work,
3562 ab8500_charger_ac_attached_work);
3563 INIT_DELAYED_WORK(&di->usb_charger_attached_work,
3564 ab8500_charger_usb_attached_work);
3565
Arun Murthy84edbee2012-02-29 21:54:26 +05303566 /*
3567 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
3568 * logic. That means we have to continously kick the charger
3569 * watchdog even when no charger is connected. This is only
3570 * valid once the AC charger has been enabled. This is
3571 * a bug that is not handled by the algorithm and the
3572 * watchdog have to be kicked by the charger driver
3573 * when the AC charger is disabled
3574 */
Tejun Heo203b42f2012-08-21 13:18:23 -07003575 INIT_DEFERRABLE_WORK(&di->kick_wd_work,
Arun Murthy84edbee2012-02-29 21:54:26 +05303576 ab8500_charger_kick_watchdog_work);
3577
Tejun Heo203b42f2012-08-21 13:18:23 -07003578 INIT_DEFERRABLE_WORK(&di->check_vbat_work,
Arun Murthy84edbee2012-02-29 21:54:26 +05303579 ab8500_charger_check_vbat_work);
3580
Marcus Cooper4b45f4a2013-01-11 13:13:04 +00003581 INIT_DELAYED_WORK(&di->attach_work,
3582 ab8500_charger_usb_link_attach_work);
3583
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003584 INIT_DELAYED_WORK(&di->usb_state_changed_work,
3585 ab8500_charger_usb_state_changed_work);
3586
3587 INIT_DELAYED_WORK(&di->vbus_drop_end_work,
3588 ab8500_charger_vbus_drop_end_work);
3589
Arun Murthy84edbee2012-02-29 21:54:26 +05303590 /* Init work for charger detection */
3591 INIT_WORK(&di->usb_link_status_work,
3592 ab8500_charger_usb_link_status_work);
3593 INIT_WORK(&di->ac_work, ab8500_charger_ac_work);
3594 INIT_WORK(&di->detect_usb_type_work,
3595 ab8500_charger_detect_usb_type_work);
3596
Arun Murthy84edbee2012-02-29 21:54:26 +05303597 /* Init work for checking HW status */
3598 INIT_WORK(&di->check_main_thermal_prot_work,
3599 ab8500_charger_check_main_thermal_prot_work);
3600 INIT_WORK(&di->check_usb_thermal_prot_work,
3601 ab8500_charger_check_usb_thermal_prot_work);
3602
3603 /*
3604 * VDD ADC supply needs to be enabled from this driver when there
3605 * is a charger connected to avoid erroneous BTEMP_HIGH/LOW
3606 * interrupts during charging
3607 */
Sachin Kamat8feffd12012-12-07 17:23:28 +05303608 di->regu = devm_regulator_get(di->dev, "vddadc");
Arun Murthy84edbee2012-02-29 21:54:26 +05303609 if (IS_ERR(di->regu)) {
3610 ret = PTR_ERR(di->regu);
3611 dev_err(di->dev, "failed to get vddadc regulator\n");
3612 goto free_charger_wq;
3613 }
3614
3615
3616 /* Initialize OVV, and other registers */
3617 ret = ab8500_charger_init_hw_registers(di);
3618 if (ret) {
3619 dev_err(di->dev, "failed to initialize ABB registers\n");
Sachin Kamat8feffd12012-12-07 17:23:28 +05303620 goto free_charger_wq;
Arun Murthy84edbee2012-02-29 21:54:26 +05303621 }
3622
3623 /* Register AC charger class */
Lee Jones72a90dd2013-02-14 09:22:48 +00003624 if (di->ac_chg.enabled) {
Michel JAOUEN01ec8c52012-04-26 10:00:04 +02003625 ret = power_supply_register(di->dev, &di->ac_chg.psy);
3626 if (ret) {
3627 dev_err(di->dev, "failed to register AC charger\n");
3628 goto free_charger_wq;
3629 }
Arun Murthy84edbee2012-02-29 21:54:26 +05303630 }
3631
3632 /* Register USB charger class */
Lee Jones72a90dd2013-02-14 09:22:48 +00003633 if (di->usb_chg.enabled) {
Michel JAOUEN01ec8c52012-04-26 10:00:04 +02003634 ret = power_supply_register(di->dev, &di->usb_chg.psy);
3635 if (ret) {
3636 dev_err(di->dev, "failed to register USB charger\n");
3637 goto free_ac;
3638 }
Arun Murthy84edbee2012-02-29 21:54:26 +05303639 }
3640
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +05303641 di->usb_phy = usb_get_phy(USB_PHY_TYPE_USB2);
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +05303642 if (IS_ERR_OR_NULL(di->usb_phy)) {
Anton Vorontsovefd71c82012-03-14 04:22:17 +04003643 dev_err(di->dev, "failed to get usb transceiver\n");
Arun Murthy84edbee2012-02-29 21:54:26 +05303644 ret = -EINVAL;
3645 goto free_usb;
3646 }
3647 di->nb.notifier_call = ab8500_charger_usb_notifier_call;
Anton Vorontsovefd71c82012-03-14 04:22:17 +04003648 ret = usb_register_notifier(di->usb_phy, &di->nb);
Arun Murthy84edbee2012-02-29 21:54:26 +05303649 if (ret) {
Anton Vorontsovefd71c82012-03-14 04:22:17 +04003650 dev_err(di->dev, "failed to register usb notifier\n");
3651 goto put_usb_phy;
Arun Murthy84edbee2012-02-29 21:54:26 +05303652 }
3653
3654 /* Identify the connected charger types during startup */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003655 charger_status = ab8500_charger_detect_chargers(di, true);
Arun Murthy84edbee2012-02-29 21:54:26 +05303656 if (charger_status & AC_PW_CONN) {
3657 di->ac.charger_connected = 1;
3658 di->ac_conn = true;
3659 ab8500_power_supply_changed(di, &di->ac_chg.psy);
3660 sysfs_notify(&di->ac_chg.psy.dev->kobj, NULL, "present");
3661 }
3662
3663 if (charger_status & USB_PW_CONN) {
Arun Murthy84edbee2012-02-29 21:54:26 +05303664 di->vbus_detected = true;
3665 di->vbus_detected_start = true;
3666 queue_work(di->charger_wq,
3667 &di->detect_usb_type_work);
3668 }
3669
3670 /* Register interrupts */
3671 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
3672 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3673 ret = request_threaded_irq(irq, NULL, ab8500_charger_irq[i].isr,
3674 IRQF_SHARED | IRQF_NO_SUSPEND,
3675 ab8500_charger_irq[i].name, di);
3676
3677 if (ret != 0) {
3678 dev_err(di->dev, "failed to request %s IRQ %d: %d\n"
3679 , ab8500_charger_irq[i].name, irq, ret);
3680 goto free_irq;
3681 }
3682 dev_dbg(di->dev, "Requested %s IRQ %d: %d\n",
3683 ab8500_charger_irq[i].name, irq, ret);
3684 }
3685
3686 platform_set_drvdata(pdev, di);
3687
Lee Jonesb269fff2013-01-11 13:12:51 +00003688 mutex_lock(&di->charger_attached_mutex);
3689
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003690 ch_stat = ab8500_charger_detect_chargers(di, false);
Lee Jonesb269fff2013-01-11 13:12:51 +00003691
3692 if ((ch_stat & AC_PW_CONN) == AC_PW_CONN) {
3693 queue_delayed_work(di->charger_wq,
3694 &di->ac_charger_attached_work,
3695 HZ);
3696 }
3697 if ((ch_stat & USB_PW_CONN) == USB_PW_CONN) {
3698 queue_delayed_work(di->charger_wq,
3699 &di->usb_charger_attached_work,
3700 HZ);
3701 }
3702
3703 mutex_unlock(&di->charger_attached_mutex);
3704
Arun Murthy84edbee2012-02-29 21:54:26 +05303705 return ret;
3706
3707free_irq:
Anton Vorontsovefd71c82012-03-14 04:22:17 +04003708 usb_unregister_notifier(di->usb_phy, &di->nb);
Arun Murthy84edbee2012-02-29 21:54:26 +05303709
3710 /* We also have to free all successfully registered irqs */
3711 for (i = i - 1; i >= 0; i--) {
3712 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3713 free_irq(irq, di);
3714 }
Anton Vorontsovefd71c82012-03-14 04:22:17 +04003715put_usb_phy:
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +05303716 usb_put_phy(di->usb_phy);
Arun Murthy84edbee2012-02-29 21:54:26 +05303717free_usb:
Lee Jones72a90dd2013-02-14 09:22:48 +00003718 if (di->usb_chg.enabled)
Michel JAOUEN01ec8c52012-04-26 10:00:04 +02003719 power_supply_unregister(&di->usb_chg.psy);
Arun Murthy84edbee2012-02-29 21:54:26 +05303720free_ac:
Lee Jones72a90dd2013-02-14 09:22:48 +00003721 if (di->ac_chg.enabled)
Michel JAOUEN01ec8c52012-04-26 10:00:04 +02003722 power_supply_unregister(&di->ac_chg.psy);
Arun Murthy84edbee2012-02-29 21:54:26 +05303723free_charger_wq:
3724 destroy_workqueue(di->charger_wq);
Arun Murthy84edbee2012-02-29 21:54:26 +05303725 return ret;
3726}
3727
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08003728static const struct of_device_id ab8500_charger_match[] = {
3729 { .compatible = "stericsson,ab8500-charger", },
3730 { },
3731};
3732
Arun Murthy84edbee2012-02-29 21:54:26 +05303733static struct platform_driver ab8500_charger_driver = {
3734 .probe = ab8500_charger_probe,
Bill Pemberton28ea73f2012-11-19 13:20:40 -05003735 .remove = ab8500_charger_remove,
Arun Murthy84edbee2012-02-29 21:54:26 +05303736 .suspend = ab8500_charger_suspend,
3737 .resume = ab8500_charger_resume,
3738 .driver = {
3739 .name = "ab8500-charger",
3740 .owner = THIS_MODULE,
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08003741 .of_match_table = ab8500_charger_match,
Arun Murthy84edbee2012-02-29 21:54:26 +05303742 },
3743};
3744
3745static int __init ab8500_charger_init(void)
3746{
3747 return platform_driver_register(&ab8500_charger_driver);
3748}
3749
3750static void __exit ab8500_charger_exit(void)
3751{
3752 platform_driver_unregister(&ab8500_charger_driver);
3753}
3754
3755subsys_initcall_sync(ab8500_charger_init);
3756module_exit(ab8500_charger_exit);
3757
3758MODULE_LICENSE("GPL v2");
3759MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy");
3760MODULE_ALIAS("platform:ab8500-charger");
3761MODULE_DESCRIPTION("AB8500 charger management driver");