blob: 30de5d42b26af95905b1a07f10b6d69cb7bd0994 [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
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100438 sysfs_notify(&di->usb_chg.psy->dev.kobj, NULL, "present");
Lee Jonesb269fff2013-01-11 13:12:51 +0000439
440 if (connected) {
441 mutex_lock(&di->charger_attached_mutex);
442 mutex_unlock(&di->charger_attached_mutex);
443
Rabin Vincent261c5132012-09-26 13:36:38 +0200444 if (is_ab8500(di->parent))
445 queue_delayed_work(di->charger_wq,
Lee Jonesb269fff2013-01-11 13:12:51 +0000446 &di->usb_charger_attached_work,
447 HZ);
448 } else {
449 cancel_delayed_work_sync(&di->usb_charger_attached_work);
450 mutex_lock(&di->charger_attached_mutex);
451 mutex_unlock(&di->charger_attached_mutex);
452 }
Arun Murthy84edbee2012-02-29 21:54:26 +0530453 }
454}
455
456/**
457 * ab8500_charger_get_ac_voltage() - get ac charger voltage
458 * @di: pointer to the ab8500_charger structure
459 *
460 * Returns ac charger voltage (on success)
461 */
462static int ab8500_charger_get_ac_voltage(struct ab8500_charger *di)
463{
464 int vch;
465
466 /* Only measure voltage if the charger is connected */
467 if (di->ac.charger_connected) {
468 vch = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_V);
469 if (vch < 0)
470 dev_err(di->dev, "%s gpadc conv failed,\n", __func__);
471 } else {
472 vch = 0;
473 }
474 return vch;
475}
476
477/**
478 * ab8500_charger_ac_cv() - check if the main charger is in CV mode
479 * @di: pointer to the ab8500_charger structure
480 *
481 * Returns ac charger CV mode (on success) else error code
482 */
483static int ab8500_charger_ac_cv(struct ab8500_charger *di)
484{
485 u8 val;
486 int ret = 0;
487
488 /* Only check CV mode if the charger is online */
489 if (di->ac.charger_online) {
490 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
491 AB8500_CH_STATUS1_REG, &val);
492 if (ret < 0) {
493 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
494 return 0;
495 }
496
497 if (val & MAIN_CH_CV_ON)
498 ret = 1;
499 else
500 ret = 0;
501 }
502
503 return ret;
504}
505
506/**
507 * ab8500_charger_get_vbus_voltage() - get vbus voltage
508 * @di: pointer to the ab8500_charger structure
509 *
510 * This function returns the vbus voltage.
511 * Returns vbus voltage (on success)
512 */
513static int ab8500_charger_get_vbus_voltage(struct ab8500_charger *di)
514{
515 int vch;
516
517 /* Only measure voltage if the charger is connected */
518 if (di->usb.charger_connected) {
519 vch = ab8500_gpadc_convert(di->gpadc, VBUS_V);
520 if (vch < 0)
521 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
522 } else {
523 vch = 0;
524 }
525 return vch;
526}
527
528/**
529 * ab8500_charger_get_usb_current() - get usb charger current
530 * @di: pointer to the ab8500_charger structure
531 *
532 * This function returns the usb charger current.
533 * Returns usb current (on success) and error code on failure
534 */
535static int ab8500_charger_get_usb_current(struct ab8500_charger *di)
536{
537 int ich;
538
539 /* Only measure current if the charger is online */
540 if (di->usb.charger_online) {
541 ich = ab8500_gpadc_convert(di->gpadc, USB_CHARGER_C);
542 if (ich < 0)
543 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
544 } else {
545 ich = 0;
546 }
547 return ich;
548}
549
550/**
551 * ab8500_charger_get_ac_current() - get ac charger current
552 * @di: pointer to the ab8500_charger structure
553 *
554 * This function returns the ac charger current.
555 * Returns ac current (on success) and error code on failure.
556 */
557static int ab8500_charger_get_ac_current(struct ab8500_charger *di)
558{
559 int ich;
560
561 /* Only measure current if the charger is online */
562 if (di->ac.charger_online) {
563 ich = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_C);
564 if (ich < 0)
565 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
566 } else {
567 ich = 0;
568 }
569 return ich;
570}
571
572/**
573 * ab8500_charger_usb_cv() - check if the usb charger is in CV mode
574 * @di: pointer to the ab8500_charger structure
575 *
576 * Returns ac charger CV mode (on success) else error code
577 */
578static int ab8500_charger_usb_cv(struct ab8500_charger *di)
579{
580 int ret;
581 u8 val;
582
583 /* Only check CV mode if the charger is online */
584 if (di->usb.charger_online) {
585 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
586 AB8500_CH_USBCH_STAT1_REG, &val);
587 if (ret < 0) {
588 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
589 return 0;
590 }
591
592 if (val & USB_CH_CV_ON)
593 ret = 1;
594 else
595 ret = 0;
596 } else {
597 ret = 0;
598 }
599
600 return ret;
601}
602
603/**
604 * ab8500_charger_detect_chargers() - Detect the connected chargers
605 * @di: pointer to the ab8500_charger structure
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100606 * @probe: if probe, don't delay and wait for HW
Arun Murthy84edbee2012-02-29 21:54:26 +0530607 *
608 * Returns the type of charger connected.
609 * For USB it will not mean we can actually charge from it
610 * but that there is a USB cable connected that we have to
611 * identify. This is used during startup when we don't get
612 * interrupts of the charger detection
613 *
614 * Returns an integer value, that means,
615 * NO_PW_CONN no power supply is connected
616 * AC_PW_CONN if the AC power supply is connected
617 * USB_PW_CONN if the USB power supply is connected
618 * AC_PW_CONN + USB_PW_CONN if USB and AC power supplies are both connected
619 */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100620static int ab8500_charger_detect_chargers(struct ab8500_charger *di, bool probe)
Arun Murthy84edbee2012-02-29 21:54:26 +0530621{
622 int result = NO_PW_CONN;
623 int ret;
624 u8 val;
625
626 /* Check for AC charger */
627 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
628 AB8500_CH_STATUS1_REG, &val);
629 if (ret < 0) {
630 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
631 return ret;
632 }
633
634 if (val & MAIN_CH_DET)
635 result = AC_PW_CONN;
636
637 /* Check for USB charger */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100638
639 if (!probe) {
640 /*
641 * AB8500 says VBUS_DET_DBNC1 & VBUS_DET_DBNC100
642 * when disconnecting ACA even though no
643 * charger was connected. Try waiting a little
644 * longer than the 100 ms of VBUS_DET_DBNC100...
645 */
646 msleep(110);
647 }
Arun Murthy84edbee2012-02-29 21:54:26 +0530648 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
649 AB8500_CH_USBCH_STAT1_REG, &val);
650 if (ret < 0) {
651 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
652 return ret;
653 }
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100654 dev_dbg(di->dev,
655 "%s AB8500_CH_USBCH_STAT1_REG %x\n", __func__,
656 val);
Arun Murthy84edbee2012-02-29 21:54:26 +0530657 if ((val & VBUS_DET_DBNC1) && (val & VBUS_DET_DBNC100))
658 result |= USB_PW_CONN;
659
660 return result;
661}
662
663/**
664 * ab8500_charger_max_usb_curr() - get the max curr for the USB type
665 * @di: pointer to the ab8500_charger structure
666 * @link_status: the identified USB type
667 *
668 * Get the maximum current that is allowed to be drawn from the host
669 * based on the USB type.
670 * Returns error code in case of failure else 0 on success
671 */
672static int ab8500_charger_max_usb_curr(struct ab8500_charger *di,
Hakan Berg74a8e342013-01-11 13:12:58 +0000673 enum ab8500_charger_link_status link_status)
Arun Murthy84edbee2012-02-29 21:54:26 +0530674{
675 int ret = 0;
676
Marcus Cooper4b45f4a2013-01-11 13:13:04 +0000677 di->usb_device_is_unrecognised = false;
678
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100679 /*
680 * Platform only supports USB 2.0.
681 * This means that charging current from USB source
682 * is maximum 500 mA. Every occurence of USB_STAT_*_HOST_*
683 * should set USB_CH_IP_CUR_LVL_0P5.
684 */
685
Arun Murthy84edbee2012-02-29 21:54:26 +0530686 switch (link_status) {
687 case USB_STAT_STD_HOST_NC:
688 case USB_STAT_STD_HOST_C_NS:
689 case USB_STAT_STD_HOST_C_S:
690 dev_dbg(di->dev, "USB Type - Standard host is "
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100691 "detected through USB driver\n");
Lee Jonesf7470b52013-02-14 12:37:29 +0000692 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100693 di->is_aca_rid = 0;
Arun Murthy84edbee2012-02-29 21:54:26 +0530694 break;
695 case USB_STAT_HOST_CHG_HS_CHIRP:
Lee Jonesf7470b52013-02-14 12:37:29 +0000696 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100697 di->is_aca_rid = 0;
Arun Murthy84edbee2012-02-29 21:54:26 +0530698 break;
699 case USB_STAT_HOST_CHG_HS:
Lee Jonesf7470b52013-02-14 12:37:29 +0000700 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100701 di->is_aca_rid = 0;
702 break;
Arun Murthy84edbee2012-02-29 21:54:26 +0530703 case USB_STAT_ACA_RID_C_HS:
Lee Jonesf7470b52013-02-14 12:37:29 +0000704 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P9;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100705 di->is_aca_rid = 0;
Arun Murthy84edbee2012-02-29 21:54:26 +0530706 break;
707 case USB_STAT_ACA_RID_A:
708 /*
709 * Dedicated charger level minus maximum current accessory
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100710 * can consume (900mA). Closest level is 500mA
Arun Murthy84edbee2012-02-29 21:54:26 +0530711 */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100712 dev_dbg(di->dev, "USB_STAT_ACA_RID_A detected\n");
Lee Jonesf7470b52013-02-14 12:37:29 +0000713 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100714 di->is_aca_rid = 1;
Arun Murthy84edbee2012-02-29 21:54:26 +0530715 break;
716 case USB_STAT_ACA_RID_B:
717 /*
718 * Dedicated charger level minus 120mA (20mA for ACA and
719 * 100mA for potential accessory). Closest level is 1300mA
720 */
Lee Jonesf7470b52013-02-14 12:37:29 +0000721 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_1P3;
Hakan Berg74a8e342013-01-11 13:12:58 +0000722 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
Lee Jonesf7470b52013-02-14 12:37:29 +0000723 di->max_usb_in_curr.usb_type_max);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100724 di->is_aca_rid = 1;
Arun Murthy84edbee2012-02-29 21:54:26 +0530725 break;
Arun Murthy84edbee2012-02-29 21:54:26 +0530726 case USB_STAT_HOST_CHG_NM:
Lee Jonesf7470b52013-02-14 12:37:29 +0000727 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100728 di->is_aca_rid = 0;
729 break;
Hakan Berg74a8e342013-01-11 13:12:58 +0000730 case USB_STAT_DEDICATED_CHG:
Lee Jonesf7470b52013-02-14 12:37:29 +0000731 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_1P5;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100732 di->is_aca_rid = 0;
733 break;
734 case USB_STAT_ACA_RID_C_HS_CHIRP:
735 case USB_STAT_ACA_RID_C_NM:
Lee Jonesf7470b52013-02-14 12:37:29 +0000736 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_1P5;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100737 di->is_aca_rid = 1;
Arun Murthy84edbee2012-02-29 21:54:26 +0530738 break;
Marcus Cooper4b45f4a2013-01-11 13:13:04 +0000739 case USB_STAT_NOT_CONFIGURED:
740 if (di->vbus_detected) {
741 di->usb_device_is_unrecognised = true;
742 dev_dbg(di->dev, "USB Type - Legacy charger.\n");
Lee Jonesf7470b52013-02-14 12:37:29 +0000743 di->max_usb_in_curr.usb_type_max =
744 USB_CH_IP_CUR_LVL_1P5;
Marcus Cooper4b45f4a2013-01-11 13:13:04 +0000745 break;
746 }
Arun Murthy84edbee2012-02-29 21:54:26 +0530747 case USB_STAT_HM_IDGND:
Arun Murthy84edbee2012-02-29 21:54:26 +0530748 dev_err(di->dev, "USB Type - Charging not allowed\n");
Lee Jonesf7470b52013-02-14 12:37:29 +0000749 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P05;
Arun Murthy84edbee2012-02-29 21:54:26 +0530750 ret = -ENXIO;
751 break;
Hakan Berg74a8e342013-01-11 13:12:58 +0000752 case USB_STAT_RESERVED:
753 if (is_ab8500(di->parent)) {
754 di->flags.vbus_collapse = true;
755 dev_err(di->dev, "USB Type - USB_STAT_RESERVED "
756 "VBUS has collapsed\n");
757 ret = -ENXIO;
758 break;
Lee Jones861a30d2012-08-29 20:36:51 +0800759 } else {
Hakan Berg74a8e342013-01-11 13:12:58 +0000760 dev_dbg(di->dev, "USB Type - Charging not allowed\n");
Lee Jonesf7470b52013-02-14 12:37:29 +0000761 di->max_usb_in_curr.usb_type_max =
762 USB_CH_IP_CUR_LVL_0P05;
Hakan Berg74a8e342013-01-11 13:12:58 +0000763 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
Lee Jonesf7470b52013-02-14 12:37:29 +0000764 link_status,
765 di->max_usb_in_curr.usb_type_max);
Hakan Berg74a8e342013-01-11 13:12:58 +0000766 ret = -ENXIO;
767 break;
768 }
Hakan Berg74a8e342013-01-11 13:12:58 +0000769 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);
Dan Carpentered5243f2013-08-27 04:19:42 +0300776 break;
Henrik Sölverff380902012-04-17 15:51:01 +0200777 case USB_STAT_NOT_VALID_LINK:
778 dev_err(di->dev, "USB Type invalid - try charging anyway\n");
Lee Jonesf7470b52013-02-14 12:37:29 +0000779 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
Hakan Berg74a8e342013-01-11 13:12:58 +0000780 break;
781
Arun Murthy84edbee2012-02-29 21:54:26 +0530782 default:
783 dev_err(di->dev, "USB Type - Unknown\n");
Lee Jonesf7470b52013-02-14 12:37:29 +0000784 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P05;
Arun Murthy84edbee2012-02-29 21:54:26 +0530785 ret = -ENXIO;
786 break;
787 };
788
Lee Jonesf7470b52013-02-14 12:37:29 +0000789 di->max_usb_in_curr.set_max = di->max_usb_in_curr.usb_type_max;
Arun Murthy84edbee2012-02-29 21:54:26 +0530790 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
Lee Jonesf7470b52013-02-14 12:37:29 +0000791 link_status, di->max_usb_in_curr.set_max);
Arun Murthy84edbee2012-02-29 21:54:26 +0530792
793 return ret;
794}
795
796/**
797 * ab8500_charger_read_usb_type() - read the type of usb connected
798 * @di: pointer to the ab8500_charger structure
799 *
800 * Detect the type of the plugged USB
801 * Returns error code in case of failure else 0 on success
802 */
803static int ab8500_charger_read_usb_type(struct ab8500_charger *di)
804{
805 int ret;
806 u8 val;
807
808 ret = abx500_get_register_interruptible(di->dev,
809 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG, &val);
810 if (ret < 0) {
811 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
812 return ret;
813 }
Lee Jones861a30d2012-08-29 20:36:51 +0800814 if (is_ab8500(di->parent))
Hakan Berg74a8e342013-01-11 13:12:58 +0000815 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
Lee Jones861a30d2012-08-29 20:36:51 +0800816 AB8500_USB_LINE_STAT_REG, &val);
817 else
818 ret = abx500_get_register_interruptible(di->dev,
819 AB8500_USB, AB8500_USB_LINK1_STAT_REG, &val);
Arun Murthy84edbee2012-02-29 21:54:26 +0530820 if (ret < 0) {
821 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
822 return ret;
823 }
824
825 /* get the USB type */
Lee Jones861a30d2012-08-29 20:36:51 +0800826 if (is_ab8500(di->parent))
827 val = (val & AB8500_USB_LINK_STATUS) >> USB_LINK_STATUS_SHIFT;
828 else
829 val = (val & AB8505_USB_LINK_STATUS) >> USB_LINK_STATUS_SHIFT;
Arun Murthy84edbee2012-02-29 21:54:26 +0530830 ret = ab8500_charger_max_usb_curr(di,
831 (enum ab8500_charger_link_status) val);
832
833 return ret;
834}
835
836/**
837 * ab8500_charger_detect_usb_type() - get the type of usb connected
838 * @di: pointer to the ab8500_charger structure
839 *
840 * Detect the type of the plugged USB
841 * Returns error code in case of failure else 0 on success
842 */
843static int ab8500_charger_detect_usb_type(struct ab8500_charger *di)
844{
845 int i, ret;
846 u8 val;
847
848 /*
849 * On getting the VBUS rising edge detect interrupt there
850 * is a 250ms delay after which the register UsbLineStatus
851 * is filled with valid data.
852 */
853 for (i = 0; i < 10; i++) {
854 msleep(250);
855 ret = abx500_get_register_interruptible(di->dev,
856 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG,
857 &val);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100858 dev_dbg(di->dev, "%s AB8500_IT_SOURCE21_REG %x\n",
859 __func__, val);
Arun Murthy84edbee2012-02-29 21:54:26 +0530860 if (ret < 0) {
861 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
862 return ret;
863 }
Hakan Berg74a8e342013-01-11 13:12:58 +0000864
Lee Jones861a30d2012-08-29 20:36:51 +0800865 if (is_ab8500(di->parent))
Hakan Berg74a8e342013-01-11 13:12:58 +0000866 ret = abx500_get_register_interruptible(di->dev,
867 AB8500_USB, AB8500_USB_LINE_STAT_REG, &val);
Lee Jones861a30d2012-08-29 20:36:51 +0800868 else
Hakan Berg74a8e342013-01-11 13:12:58 +0000869 ret = abx500_get_register_interruptible(di->dev,
870 AB8500_USB, AB8500_USB_LINK1_STAT_REG, &val);
Arun Murthy84edbee2012-02-29 21:54:26 +0530871 if (ret < 0) {
872 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
873 return ret;
874 }
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +0100875 dev_dbg(di->dev, "%s AB8500_USB_LINE_STAT_REG %x\n", __func__,
876 val);
Arun Murthy84edbee2012-02-29 21:54:26 +0530877 /*
878 * Until the IT source register is read the UsbLineStatus
879 * register is not updated, hence doing the same
880 * Revisit this:
881 */
882
883 /* get the USB type */
Lee Jones861a30d2012-08-29 20:36:51 +0800884 if (is_ab8500(di->parent))
885 val = (val & AB8500_USB_LINK_STATUS) >>
886 USB_LINK_STATUS_SHIFT;
887 else
888 val = (val & AB8505_USB_LINK_STATUS) >>
889 USB_LINK_STATUS_SHIFT;
Arun Murthy84edbee2012-02-29 21:54:26 +0530890 if (val)
891 break;
892 }
893 ret = ab8500_charger_max_usb_curr(di,
894 (enum ab8500_charger_link_status) val);
895
896 return ret;
897}
898
899/*
900 * This array maps the raw hex value to charger voltage used by the AB8500
901 * Values taken from the UM0836
902 */
903static int ab8500_charger_voltage_map[] = {
904 3500 ,
905 3525 ,
906 3550 ,
907 3575 ,
908 3600 ,
909 3625 ,
910 3650 ,
911 3675 ,
912 3700 ,
913 3725 ,
914 3750 ,
915 3775 ,
916 3800 ,
917 3825 ,
918 3850 ,
919 3875 ,
920 3900 ,
921 3925 ,
922 3950 ,
923 3975 ,
924 4000 ,
925 4025 ,
926 4050 ,
927 4060 ,
928 4070 ,
929 4080 ,
930 4090 ,
931 4100 ,
932 4110 ,
933 4120 ,
934 4130 ,
935 4140 ,
936 4150 ,
937 4160 ,
938 4170 ,
939 4180 ,
940 4190 ,
941 4200 ,
942 4210 ,
943 4220 ,
944 4230 ,
945 4240 ,
946 4250 ,
947 4260 ,
948 4270 ,
949 4280 ,
950 4290 ,
951 4300 ,
952 4310 ,
953 4320 ,
954 4330 ,
955 4340 ,
956 4350 ,
957 4360 ,
958 4370 ,
959 4380 ,
960 4390 ,
961 4400 ,
962 4410 ,
963 4420 ,
964 4430 ,
965 4440 ,
966 4450 ,
967 4460 ,
968 4470 ,
969 4480 ,
970 4490 ,
971 4500 ,
972 4510 ,
973 4520 ,
974 4530 ,
975 4540 ,
976 4550 ,
977 4560 ,
978 4570 ,
979 4580 ,
980 4590 ,
981 4600 ,
982};
983
Arun Murthy84edbee2012-02-29 21:54:26 +0530984static int ab8500_voltage_to_regval(int voltage)
985{
986 int i;
987
988 /* Special case for voltage below 3.5V */
989 if (voltage < ab8500_charger_voltage_map[0])
990 return LOW_VOLT_REG;
991
992 for (i = 1; i < ARRAY_SIZE(ab8500_charger_voltage_map); i++) {
993 if (voltage < ab8500_charger_voltage_map[i])
994 return i - 1;
995 }
996
997 /* If not last element, return error */
998 i = ARRAY_SIZE(ab8500_charger_voltage_map) - 1;
999 if (voltage == ab8500_charger_voltage_map[i])
1000 return i;
1001 else
1002 return -1;
1003}
1004
Lee Jones861a30d2012-08-29 20:36:51 +08001005static int ab8500_current_to_regval(struct ab8500_charger *di, int curr)
Arun Murthy84edbee2012-02-29 21:54:26 +05301006{
1007 int i;
1008
Lee Jones861a30d2012-08-29 20:36:51 +08001009 if (curr < di->bm->chg_output_curr[0])
Arun Murthy84edbee2012-02-29 21:54:26 +05301010 return 0;
1011
Lee Jones861a30d2012-08-29 20:36:51 +08001012 for (i = 0; i < di->bm->n_chg_out_curr; i++) {
1013 if (curr < di->bm->chg_output_curr[i])
Arun Murthy84edbee2012-02-29 21:54:26 +05301014 return i - 1;
1015 }
1016
1017 /* If not last element, return error */
Lee Jones861a30d2012-08-29 20:36:51 +08001018 i = di->bm->n_chg_out_curr - 1;
1019 if (curr == di->bm->chg_output_curr[i])
Arun Murthy84edbee2012-02-29 21:54:26 +05301020 return i;
1021 else
1022 return -1;
1023}
1024
Lee Jones861a30d2012-08-29 20:36:51 +08001025static int ab8500_vbus_in_curr_to_regval(struct ab8500_charger *di, int curr)
Arun Murthy84edbee2012-02-29 21:54:26 +05301026{
1027 int i;
1028
Lee Jones861a30d2012-08-29 20:36:51 +08001029 if (curr < di->bm->chg_input_curr[0])
Arun Murthy84edbee2012-02-29 21:54:26 +05301030 return 0;
1031
Lee Jones861a30d2012-08-29 20:36:51 +08001032 for (i = 0; i < di->bm->n_chg_in_curr; i++) {
1033 if (curr < di->bm->chg_input_curr[i])
Arun Murthy84edbee2012-02-29 21:54:26 +05301034 return i - 1;
1035 }
1036
1037 /* If not last element, return error */
Lee Jones861a30d2012-08-29 20:36:51 +08001038 i = di->bm->n_chg_in_curr - 1;
1039 if (curr == di->bm->chg_input_curr[i])
Arun Murthy84edbee2012-02-29 21:54:26 +05301040 return i;
1041 else
1042 return -1;
1043}
1044
1045/**
1046 * ab8500_charger_get_usb_cur() - get usb current
1047 * @di: pointer to the ab8500_charger structre
1048 *
1049 * The usb stack provides the maximum current that can be drawn from
1050 * the standard usb host. This will be in mA.
1051 * This function converts current in mA to a value that can be written
1052 * to the register. Returns -1 if charging is not allowed
1053 */
1054static int ab8500_charger_get_usb_cur(struct ab8500_charger *di)
1055{
Lee Jonesf7470b52013-02-14 12:37:29 +00001056 int ret = 0;
Arun Murthy84edbee2012-02-29 21:54:26 +05301057 switch (di->usb_state.usb_current) {
1058 case 100:
Lee Jonesf7470b52013-02-14 12:37:29 +00001059 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P09;
Arun Murthy84edbee2012-02-29 21:54:26 +05301060 break;
1061 case 200:
Lee Jonesf7470b52013-02-14 12:37:29 +00001062 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P19;
Arun Murthy84edbee2012-02-29 21:54:26 +05301063 break;
1064 case 300:
Lee Jonesf7470b52013-02-14 12:37:29 +00001065 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P29;
Arun Murthy84edbee2012-02-29 21:54:26 +05301066 break;
1067 case 400:
Lee Jonesf7470b52013-02-14 12:37:29 +00001068 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P38;
Arun Murthy84edbee2012-02-29 21:54:26 +05301069 break;
1070 case 500:
Lee Jonesf7470b52013-02-14 12:37:29 +00001071 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
Arun Murthy84edbee2012-02-29 21:54:26 +05301072 break;
1073 default:
Lee Jonesf7470b52013-02-14 12:37:29 +00001074 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P05;
1075 ret = -EPERM;
Arun Murthy84edbee2012-02-29 21:54:26 +05301076 break;
1077 };
Lee Jonesf7470b52013-02-14 12:37:29 +00001078 di->max_usb_in_curr.set_max = di->max_usb_in_curr.usb_type_max;
1079 return ret;
1080}
1081
1082/**
1083 * ab8500_charger_check_continue_stepping() - Check to allow stepping
1084 * @di: pointer to the ab8500_charger structure
1085 * @reg: select what charger register to check
1086 *
1087 * Check if current stepping should be allowed to continue.
1088 * Checks if charger source has not collapsed. If it has, further stepping
1089 * is not allowed.
1090 */
1091static bool ab8500_charger_check_continue_stepping(struct ab8500_charger *di,
1092 int reg)
1093{
1094 if (reg == AB8500_USBCH_IPT_CRNTLVL_REG)
1095 return !di->flags.vbus_drop_end;
1096 else
1097 return true;
Arun Murthy84edbee2012-02-29 21:54:26 +05301098}
1099
1100/**
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001101 * ab8500_charger_set_current() - set charger current
1102 * @di: pointer to the ab8500_charger structure
1103 * @ich: charger current, in mA
1104 * @reg: select what charger register to set
1105 *
1106 * Set charger current.
1107 * There is no state machine in the AB to step up/down the charger
1108 * current to avoid dips and spikes on MAIN, VBUS and VBAT when
1109 * charging is started. Instead we need to implement
1110 * this charger current step-up/down here.
1111 * Returns error code in case of failure else 0(on success)
1112 */
1113static int ab8500_charger_set_current(struct ab8500_charger *di,
1114 int ich, int reg)
1115{
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001116 int ret = 0;
Lee Jones861a30d2012-08-29 20:36:51 +08001117 int curr_index, prev_curr_index, shift_value, i;
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001118 u8 reg_value;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001119 u32 step_udelay;
1120 bool no_stepping = false;
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001121
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001122 atomic_inc(&di->current_stepping_sessions);
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001123
1124 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1125 reg, &reg_value);
1126 if (ret < 0) {
1127 dev_err(di->dev, "%s read failed\n", __func__);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001128 goto exit_set_current;
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001129 }
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001130
1131 switch (reg) {
1132 case AB8500_MCH_IPT_CURLVL_REG:
1133 shift_value = MAIN_CH_INPUT_CURR_SHIFT;
1134 prev_curr_index = (reg_value >> shift_value);
Lee Jones861a30d2012-08-29 20:36:51 +08001135 curr_index = ab8500_current_to_regval(di, ich);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001136 step_udelay = STEP_UDELAY;
1137 if (!di->ac.charger_connected)
1138 no_stepping = true;
1139 break;
1140 case AB8500_USBCH_IPT_CRNTLVL_REG:
Lee Jones861a30d2012-08-29 20:36:51 +08001141 if (is_ab8540(di->parent))
1142 shift_value = AB8540_VBUS_IN_CURR_LIM_SHIFT;
1143 else
1144 shift_value = VBUS_IN_CURR_LIM_SHIFT;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001145 prev_curr_index = (reg_value >> shift_value);
Lee Jones861a30d2012-08-29 20:36:51 +08001146 curr_index = ab8500_vbus_in_curr_to_regval(di, ich);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001147 step_udelay = STEP_UDELAY * 100;
1148
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001149 if (!di->usb.charger_connected)
1150 no_stepping = true;
1151 break;
1152 case AB8500_CH_OPT_CRNTLVL_REG:
1153 shift_value = 0;
1154 prev_curr_index = (reg_value >> shift_value);
Lee Jones861a30d2012-08-29 20:36:51 +08001155 curr_index = ab8500_current_to_regval(di, ich);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001156 step_udelay = STEP_UDELAY;
1157 if (curr_index && (curr_index - prev_curr_index) > 1)
1158 step_udelay *= 100;
1159
1160 if (!di->usb.charger_connected && !di->ac.charger_connected)
1161 no_stepping = true;
1162
1163 break;
1164 default:
1165 dev_err(di->dev, "%s current register not valid\n", __func__);
1166 ret = -ENXIO;
1167 goto exit_set_current;
1168 }
1169
1170 if (curr_index < 0) {
1171 dev_err(di->dev, "requested current limit out-of-range\n");
1172 ret = -ENXIO;
1173 goto exit_set_current;
1174 }
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001175
1176 /* only update current if it's been changed */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001177 if (prev_curr_index == curr_index) {
1178 dev_dbg(di->dev, "%s current not changed for reg: 0x%02x\n",
1179 __func__, reg);
1180 ret = 0;
1181 goto exit_set_current;
1182 }
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001183
1184 dev_dbg(di->dev, "%s set charger current: %d mA for reg: 0x%02x\n",
1185 __func__, ich, reg);
1186
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001187 if (no_stepping) {
1188 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1189 reg, (u8)curr_index << shift_value);
1190 if (ret)
1191 dev_err(di->dev, "%s write failed\n", __func__);
1192 } else if (prev_curr_index > curr_index) {
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001193 for (i = prev_curr_index - 1; i >= curr_index; i--) {
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001194 dev_dbg(di->dev, "curr change_1 to: %x for 0x%02x\n",
1195 (u8) i << shift_value, reg);
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001196 ret = abx500_set_register_interruptible(di->dev,
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001197 AB8500_CHARGER, reg, (u8)i << shift_value);
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001198 if (ret) {
1199 dev_err(di->dev, "%s write failed\n", __func__);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001200 goto exit_set_current;
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001201 }
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001202 if (i != curr_index)
1203 usleep_range(step_udelay, step_udelay * 2);
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001204 }
1205 } else {
Lee Jonesf7470b52013-02-14 12:37:29 +00001206 bool allow = true;
1207 for (i = prev_curr_index + 1; i <= curr_index && allow; i++) {
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001208 dev_dbg(di->dev, "curr change_2 to: %x for 0x%02x\n",
1209 (u8)i << shift_value, reg);
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001210 ret = abx500_set_register_interruptible(di->dev,
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001211 AB8500_CHARGER, reg, (u8)i << shift_value);
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001212 if (ret) {
1213 dev_err(di->dev, "%s write failed\n", __func__);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001214 goto exit_set_current;
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001215 }
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001216 if (i != curr_index)
1217 usleep_range(step_udelay, step_udelay * 2);
Lee Jonesf7470b52013-02-14 12:37:29 +00001218
1219 allow = ab8500_charger_check_continue_stepping(di, reg);
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001220 }
1221 }
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001222
1223exit_set_current:
1224 atomic_dec(&di->current_stepping_sessions);
1225
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001226 return ret;
1227}
1228
1229/**
Arun Murthy84edbee2012-02-29 21:54:26 +05301230 * ab8500_charger_set_vbus_in_curr() - set VBUS input current limit
1231 * @di: pointer to the ab8500_charger structure
1232 * @ich_in: charger input current limit
1233 *
1234 * Sets the current that can be drawn from the USB host
1235 * Returns error code in case of failure else 0(on success)
1236 */
1237static int ab8500_charger_set_vbus_in_curr(struct ab8500_charger *di,
1238 int ich_in)
1239{
Arun Murthy84edbee2012-02-29 21:54:26 +05301240 int min_value;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001241 int ret;
Arun Murthy84edbee2012-02-29 21:54:26 +05301242
1243 /* We should always use to lowest current limit */
Lee Jonesb0284de2012-11-30 10:09:42 +00001244 min_value = min(di->bm->chg_params->usb_curr_max, ich_in);
Lee Jonesf7470b52013-02-14 12:37:29 +00001245 if (di->max_usb_in_curr.set_max > 0)
1246 min_value = min(di->max_usb_in_curr.set_max, min_value);
1247
1248 if (di->usb_state.usb_current >= 0)
1249 min_value = min(di->usb_state.usb_current, min_value);
Arun Murthy84edbee2012-02-29 21:54:26 +05301250
1251 switch (min_value) {
1252 case 100:
1253 if (di->vbat < VBAT_TRESH_IP_CUR_RED)
1254 min_value = USB_CH_IP_CUR_LVL_0P05;
1255 break;
1256 case 500:
1257 if (di->vbat < VBAT_TRESH_IP_CUR_RED)
1258 min_value = USB_CH_IP_CUR_LVL_0P45;
1259 break;
1260 default:
1261 break;
1262 }
1263
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001264 dev_info(di->dev, "VBUS input current limit set to %d mA\n", min_value);
1265
1266 mutex_lock(&di->usb_ipt_crnt_lock);
1267 ret = ab8500_charger_set_current(di, min_value,
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001268 AB8500_USBCH_IPT_CRNTLVL_REG);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001269 mutex_unlock(&di->usb_ipt_crnt_lock);
1270
1271 return ret;
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001272}
Arun Murthy84edbee2012-02-29 21:54:26 +05301273
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001274/**
1275 * ab8500_charger_set_main_in_curr() - set main charger input current
1276 * @di: pointer to the ab8500_charger structure
1277 * @ich_in: input charger current, in mA
1278 *
1279 * Set main charger input current.
1280 * Returns error code in case of failure else 0(on success)
1281 */
1282static int ab8500_charger_set_main_in_curr(struct ab8500_charger *di,
1283 int ich_in)
1284{
1285 return ab8500_charger_set_current(di, ich_in,
1286 AB8500_MCH_IPT_CURLVL_REG);
1287}
Arun Murthy84edbee2012-02-29 21:54:26 +05301288
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001289/**
1290 * ab8500_charger_set_output_curr() - set charger output current
1291 * @di: pointer to the ab8500_charger structure
1292 * @ich_out: output charger current, in mA
1293 *
1294 * Set charger output current.
1295 * Returns error code in case of failure else 0(on success)
1296 */
1297static int ab8500_charger_set_output_curr(struct ab8500_charger *di,
1298 int ich_out)
1299{
1300 return ab8500_charger_set_current(di, ich_out,
1301 AB8500_CH_OPT_CRNTLVL_REG);
Arun Murthy84edbee2012-02-29 21:54:26 +05301302}
1303
1304/**
1305 * ab8500_charger_led_en() - turn on/off chargign led
1306 * @di: pointer to the ab8500_charger structure
1307 * @on: flag to turn on/off the chargign led
1308 *
1309 * Power ON/OFF charging LED indication
1310 * Returns error code in case of failure else 0(on success)
1311 */
1312static int ab8500_charger_led_en(struct ab8500_charger *di, int on)
1313{
1314 int ret;
1315
1316 if (on) {
1317 /* Power ON charging LED indicator, set LED current to 5mA */
1318 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1319 AB8500_LED_INDICATOR_PWM_CTRL,
1320 (LED_IND_CUR_5MA | LED_INDICATOR_PWM_ENA));
1321 if (ret) {
1322 dev_err(di->dev, "Power ON LED failed\n");
1323 return ret;
1324 }
1325 /* LED indicator PWM duty cycle 252/256 */
1326 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1327 AB8500_LED_INDICATOR_PWM_DUTY,
1328 LED_INDICATOR_PWM_DUTY_252_256);
1329 if (ret) {
1330 dev_err(di->dev, "Set LED PWM duty cycle failed\n");
1331 return ret;
1332 }
1333 } else {
1334 /* Power off charging LED indicator */
1335 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1336 AB8500_LED_INDICATOR_PWM_CTRL,
1337 LED_INDICATOR_PWM_DIS);
1338 if (ret) {
1339 dev_err(di->dev, "Power-off LED failed\n");
1340 return ret;
1341 }
1342 }
1343
1344 return ret;
1345}
1346
1347/**
1348 * ab8500_charger_ac_en() - enable or disable ac charging
1349 * @di: pointer to the ab8500_charger structure
1350 * @enable: enable/disable flag
1351 * @vset: charging voltage
1352 * @iset: charging current
1353 *
1354 * Enable/Disable AC/Mains charging and turns on/off the charging led
1355 * respectively.
1356 **/
1357static int ab8500_charger_ac_en(struct ux500_charger *charger,
1358 int enable, int vset, int iset)
1359{
1360 int ret;
1361 int volt_index;
1362 int curr_index;
1363 int input_curr_index;
1364 u8 overshoot = 0;
1365
1366 struct ab8500_charger *di = to_ab8500_charger_ac_device_info(charger);
1367
1368 if (enable) {
1369 /* Check if AC is connected */
1370 if (!di->ac.charger_connected) {
1371 dev_err(di->dev, "AC charger not connected\n");
1372 return -ENXIO;
1373 }
1374
1375 /* Enable AC charging */
1376 dev_dbg(di->dev, "Enable AC: %dmV %dmA\n", vset, iset);
1377
1378 /*
1379 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1380 * will be triggered everytime we enable the VDD ADC supply.
1381 * This will turn off charging for a short while.
1382 * It can be avoided by having the supply on when
1383 * there is a charger enabled. Normally the VDD ADC supply
1384 * is enabled everytime a GPADC conversion is triggered. We will
1385 * force it to be enabled from this driver to have
1386 * the GPADC module independant of the AB8500 chargers
1387 */
1388 if (!di->vddadc_en_ac) {
Sachin Kamat6de7ef32013-09-06 13:31:09 +05301389 ret = regulator_enable(di->regu);
1390 if (ret)
1391 dev_warn(di->dev,
1392 "Failed to enable regulator\n");
1393 else
1394 di->vddadc_en_ac = true;
Arun Murthy84edbee2012-02-29 21:54:26 +05301395 }
1396
1397 /* Check if the requested voltage or current is valid */
1398 volt_index = ab8500_voltage_to_regval(vset);
Lee Jones861a30d2012-08-29 20:36:51 +08001399 curr_index = ab8500_current_to_regval(di, iset);
1400 input_curr_index = ab8500_current_to_regval(di,
Lee Jonesb0284de2012-11-30 10:09:42 +00001401 di->bm->chg_params->ac_curr_max);
Arun Murthy84edbee2012-02-29 21:54:26 +05301402 if (volt_index < 0 || curr_index < 0 || input_curr_index < 0) {
1403 dev_err(di->dev,
1404 "Charger voltage or current too high, "
1405 "charging not started\n");
1406 return -ENXIO;
1407 }
1408
1409 /* ChVoltLevel: maximum battery charging voltage */
1410 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1411 AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1412 if (ret) {
1413 dev_err(di->dev, "%s write failed\n", __func__);
1414 return ret;
1415 }
1416 /* MainChInputCurr: current that can be drawn from the charger*/
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001417 ret = ab8500_charger_set_main_in_curr(di,
Lee Jonesb0284de2012-11-30 10:09:42 +00001418 di->bm->chg_params->ac_curr_max);
Arun Murthy84edbee2012-02-29 21:54:26 +05301419 if (ret) {
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001420 dev_err(di->dev, "%s Failed to set MainChInputCurr\n",
1421 __func__);
Arun Murthy84edbee2012-02-29 21:54:26 +05301422 return ret;
1423 }
1424 /* ChOutputCurentLevel: protected output current */
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001425 ret = ab8500_charger_set_output_curr(di, iset);
Arun Murthy84edbee2012-02-29 21:54:26 +05301426 if (ret) {
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001427 dev_err(di->dev, "%s "
1428 "Failed to set ChOutputCurentLevel\n",
1429 __func__);
Arun Murthy84edbee2012-02-29 21:54:26 +05301430 return ret;
1431 }
1432
1433 /* Check if VBAT overshoot control should be enabled */
Lee Jonesb0284de2012-11-30 10:09:42 +00001434 if (!di->bm->enable_overshoot)
Arun Murthy84edbee2012-02-29 21:54:26 +05301435 overshoot = MAIN_CH_NO_OVERSHOOT_ENA_N;
1436
1437 /* Enable Main Charger */
1438 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1439 AB8500_MCH_CTRL1, MAIN_CH_ENA | overshoot);
1440 if (ret) {
1441 dev_err(di->dev, "%s write failed\n", __func__);
1442 return ret;
1443 }
1444
1445 /* Power on charging LED indication */
1446 ret = ab8500_charger_led_en(di, true);
1447 if (ret < 0)
1448 dev_err(di->dev, "failed to enable LED\n");
1449
1450 di->ac.charger_online = 1;
1451 } else {
1452 /* Disable AC charging */
1453 if (is_ab8500_1p1_or_earlier(di->parent)) {
1454 /*
1455 * For ABB revision 1.0 and 1.1 there is a bug in the
1456 * watchdog logic. That means we have to continously
1457 * kick the charger watchdog even when no charger is
1458 * connected. This is only valid once the AC charger
1459 * has been enabled. This is a bug that is not handled
1460 * by the algorithm and the watchdog have to be kicked
1461 * by the charger driver when the AC charger
1462 * is disabled
1463 */
1464 if (di->ac_conn) {
1465 queue_delayed_work(di->charger_wq,
1466 &di->kick_wd_work,
1467 round_jiffies(WD_KICK_INTERVAL));
1468 }
1469
1470 /*
1471 * We can't turn off charging completely
1472 * due to a bug in AB8500 cut1.
1473 * If we do, charging will not start again.
1474 * That is why we set the lowest voltage
1475 * and current possible
1476 */
1477 ret = abx500_set_register_interruptible(di->dev,
1478 AB8500_CHARGER,
1479 AB8500_CH_VOLT_LVL_REG, CH_VOL_LVL_3P5);
1480 if (ret) {
1481 dev_err(di->dev,
1482 "%s write failed\n", __func__);
1483 return ret;
1484 }
1485
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001486 ret = ab8500_charger_set_output_curr(di, 0);
Arun Murthy84edbee2012-02-29 21:54:26 +05301487 if (ret) {
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001488 dev_err(di->dev, "%s "
1489 "Failed to set ChOutputCurentLevel\n",
1490 __func__);
Arun Murthy84edbee2012-02-29 21:54:26 +05301491 return ret;
1492 }
1493 } else {
1494 ret = abx500_set_register_interruptible(di->dev,
1495 AB8500_CHARGER,
1496 AB8500_MCH_CTRL1, 0);
1497 if (ret) {
1498 dev_err(di->dev,
1499 "%s write failed\n", __func__);
1500 return ret;
1501 }
1502 }
1503
1504 ret = ab8500_charger_led_en(di, false);
1505 if (ret < 0)
1506 dev_err(di->dev, "failed to disable LED\n");
1507
1508 di->ac.charger_online = 0;
1509 di->ac.wd_expired = false;
1510
1511 /* Disable regulator if enabled */
1512 if (di->vddadc_en_ac) {
1513 regulator_disable(di->regu);
1514 di->vddadc_en_ac = false;
1515 }
1516
1517 dev_dbg(di->dev, "%s Disabled AC charging\n", __func__);
1518 }
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001519 ab8500_power_supply_changed(di, di->ac_chg.psy);
Arun Murthy84edbee2012-02-29 21:54:26 +05301520
1521 return ret;
1522}
1523
1524/**
1525 * ab8500_charger_usb_en() - enable usb charging
1526 * @di: pointer to the ab8500_charger structure
1527 * @enable: enable/disable flag
1528 * @vset: charging voltage
1529 * @ich_out: charger output current
1530 *
1531 * Enable/Disable USB charging and turns on/off the charging led respectively.
1532 * Returns error code in case of failure else 0(on success)
1533 */
1534static int ab8500_charger_usb_en(struct ux500_charger *charger,
1535 int enable, int vset, int ich_out)
1536{
1537 int ret;
1538 int volt_index;
1539 int curr_index;
1540 u8 overshoot = 0;
1541
1542 struct ab8500_charger *di = to_ab8500_charger_usb_device_info(charger);
1543
1544 if (enable) {
1545 /* Check if USB is connected */
1546 if (!di->usb.charger_connected) {
1547 dev_err(di->dev, "USB charger not connected\n");
1548 return -ENXIO;
1549 }
1550
1551 /*
1552 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1553 * will be triggered everytime we enable the VDD ADC supply.
1554 * This will turn off charging for a short while.
1555 * It can be avoided by having the supply on when
1556 * there is a charger enabled. Normally the VDD ADC supply
1557 * is enabled everytime a GPADC conversion is triggered. We will
1558 * force it to be enabled from this driver to have
1559 * the GPADC module independant of the AB8500 chargers
1560 */
1561 if (!di->vddadc_en_usb) {
Sachin Kamat6de7ef32013-09-06 13:31:09 +05301562 ret = regulator_enable(di->regu);
1563 if (ret)
1564 dev_warn(di->dev,
1565 "Failed to enable regulator\n");
1566 else
1567 di->vddadc_en_usb = true;
Arun Murthy84edbee2012-02-29 21:54:26 +05301568 }
1569
1570 /* Enable USB charging */
1571 dev_dbg(di->dev, "Enable USB: %dmV %dmA\n", vset, ich_out);
1572
1573 /* Check if the requested voltage or current is valid */
1574 volt_index = ab8500_voltage_to_regval(vset);
Lee Jones861a30d2012-08-29 20:36:51 +08001575 curr_index = ab8500_current_to_regval(di, ich_out);
Arun Murthy84edbee2012-02-29 21:54:26 +05301576 if (volt_index < 0 || curr_index < 0) {
1577 dev_err(di->dev,
1578 "Charger voltage or current too high, "
1579 "charging not started\n");
1580 return -ENXIO;
1581 }
1582
1583 /* ChVoltLevel: max voltage upto which battery can be charged */
1584 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1585 AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1586 if (ret) {
1587 dev_err(di->dev, "%s write failed\n", __func__);
1588 return ret;
1589 }
Arun Murthy84edbee2012-02-29 21:54:26 +05301590 /* Check if VBAT overshoot control should be enabled */
Lee Jonesb0284de2012-11-30 10:09:42 +00001591 if (!di->bm->enable_overshoot)
Arun Murthy84edbee2012-02-29 21:54:26 +05301592 overshoot = USB_CHG_NO_OVERSHOOT_ENA_N;
1593
1594 /* Enable USB Charger */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001595 dev_dbg(di->dev,
1596 "Enabling USB with write to AB8500_USBCH_CTRL1_REG\n");
Arun Murthy84edbee2012-02-29 21:54:26 +05301597 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1598 AB8500_USBCH_CTRL1_REG, USB_CH_ENA | overshoot);
1599 if (ret) {
1600 dev_err(di->dev, "%s write failed\n", __func__);
1601 return ret;
1602 }
1603
1604 /* If success power on charging LED indication */
1605 ret = ab8500_charger_led_en(di, true);
1606 if (ret < 0)
1607 dev_err(di->dev, "failed to enable LED\n");
1608
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001609 di->usb.charger_online = 1;
1610
1611 /* USBChInputCurr: current that can be drawn from the usb */
Lee Jonesf7470b52013-02-14 12:37:29 +00001612 ret = ab8500_charger_set_vbus_in_curr(di,
1613 di->max_usb_in_curr.usb_type_max);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001614 if (ret) {
1615 dev_err(di->dev, "setting USBChInputCurr failed\n");
1616 return ret;
1617 }
1618
1619 /* ChOutputCurentLevel: protected output current */
1620 ret = ab8500_charger_set_output_curr(di, ich_out);
1621 if (ret) {
1622 dev_err(di->dev, "%s "
1623 "Failed to set ChOutputCurentLevel\n",
1624 __func__);
1625 return ret;
1626 }
1627
Arun Murthy84edbee2012-02-29 21:54:26 +05301628 queue_delayed_work(di->charger_wq, &di->check_vbat_work, HZ);
1629
Arun Murthy84edbee2012-02-29 21:54:26 +05301630 } else {
1631 /* Disable USB charging */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001632 dev_dbg(di->dev, "%s Disabled USB charging\n", __func__);
Arun Murthy84edbee2012-02-29 21:54:26 +05301633 ret = abx500_set_register_interruptible(di->dev,
1634 AB8500_CHARGER,
1635 AB8500_USBCH_CTRL1_REG, 0);
1636 if (ret) {
1637 dev_err(di->dev,
1638 "%s write failed\n", __func__);
1639 return ret;
1640 }
1641
1642 ret = ab8500_charger_led_en(di, false);
1643 if (ret < 0)
1644 dev_err(di->dev, "failed to disable LED\n");
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001645 /* USBChInputCurr: current that can be drawn from the usb */
1646 ret = ab8500_charger_set_vbus_in_curr(di, 0);
1647 if (ret) {
1648 dev_err(di->dev, "setting USBChInputCurr failed\n");
1649 return ret;
1650 }
Arun Murthy84edbee2012-02-29 21:54:26 +05301651
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01001652 /* ChOutputCurentLevel: protected output current */
1653 ret = ab8500_charger_set_output_curr(di, 0);
1654 if (ret) {
1655 dev_err(di->dev, "%s "
1656 "Failed to reset ChOutputCurentLevel\n",
1657 __func__);
1658 return ret;
1659 }
Arun Murthy84edbee2012-02-29 21:54:26 +05301660 di->usb.charger_online = 0;
1661 di->usb.wd_expired = false;
1662
1663 /* Disable regulator if enabled */
1664 if (di->vddadc_en_usb) {
1665 regulator_disable(di->regu);
1666 di->vddadc_en_usb = false;
1667 }
1668
1669 dev_dbg(di->dev, "%s Disabled USB charging\n", __func__);
1670
1671 /* Cancel any pending Vbat check work */
Lee Jonesb09f86d2012-12-21 17:56:52 -08001672 cancel_delayed_work(&di->check_vbat_work);
Arun Murthy84edbee2012-02-29 21:54:26 +05301673
1674 }
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001675 ab8500_power_supply_changed(di, di->usb_chg.psy);
Arun Murthy84edbee2012-02-29 21:54:26 +05301676
1677 return ret;
1678}
1679
Lee Jones88917162013-02-13 11:39:19 +00001680static int ab8500_external_charger_prepare(struct notifier_block *charger_nb,
1681 unsigned long event, void *data)
1682{
1683 int ret;
1684 struct device *dev = data;
1685 /*Toggle External charger control pin*/
1686 ret = abx500_set_register_interruptible(dev, AB8500_SYS_CTRL1_BLOCK,
1687 AB8500_SYS_CHARGER_CONTROL_REG,
1688 EXTERNAL_CHARGER_DISABLE_REG_VAL);
1689 if (ret < 0) {
1690 dev_err(dev, "write reg failed %d\n", ret);
1691 goto out;
1692 }
1693 ret = abx500_set_register_interruptible(dev, AB8500_SYS_CTRL1_BLOCK,
1694 AB8500_SYS_CHARGER_CONTROL_REG,
1695 EXTERNAL_CHARGER_ENABLE_REG_VAL);
1696 if (ret < 0)
1697 dev_err(dev, "Write reg failed %d\n", ret);
1698
1699out:
1700 return ret;
1701}
1702
Arun Murthy84edbee2012-02-29 21:54:26 +05301703/**
Lee Jones4dcdf572013-02-14 09:24:10 +00001704 * ab8500_charger_usb_check_enable() - enable usb charging
1705 * @charger: pointer to the ux500_charger structure
1706 * @vset: charging voltage
1707 * @iset: charger output current
1708 *
1709 * Check if the VBUS charger has been disconnected and reconnected without
1710 * AB8500 rising an interrupt. Returns 0 on success.
1711 */
1712static int ab8500_charger_usb_check_enable(struct ux500_charger *charger,
1713 int vset, int iset)
1714{
1715 u8 usbch_ctrl1 = 0;
1716 int ret = 0;
1717
1718 struct ab8500_charger *di = to_ab8500_charger_usb_device_info(charger);
1719
1720 if (!di->usb.charger_connected)
1721 return ret;
1722
1723 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1724 AB8500_USBCH_CTRL1_REG, &usbch_ctrl1);
1725 if (ret < 0) {
1726 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
1727 return ret;
1728 }
1729 dev_dbg(di->dev, "USB charger ctrl: 0x%02x\n", usbch_ctrl1);
1730
1731 if (!(usbch_ctrl1 & USB_CH_ENA)) {
1732 dev_info(di->dev, "Charging has been disabled abnormally and will be re-enabled\n");
1733
1734 ret = abx500_mask_and_set_register_interruptible(di->dev,
1735 AB8500_CHARGER, AB8500_CHARGER_CTRL,
1736 DROP_COUNT_RESET, DROP_COUNT_RESET);
1737 if (ret < 0) {
1738 dev_err(di->dev, "ab8500 write failed %d\n", __LINE__);
1739 return ret;
1740 }
1741
1742 ret = ab8500_charger_usb_en(&di->usb_chg, true, vset, iset);
1743 if (ret < 0) {
1744 dev_err(di->dev, "Failed to enable VBUS charger %d\n",
1745 __LINE__);
1746 return ret;
1747 }
1748 }
1749 return ret;
1750}
1751
1752/**
1753 * ab8500_charger_ac_check_enable() - enable usb charging
1754 * @charger: pointer to the ux500_charger structure
1755 * @vset: charging voltage
1756 * @iset: charger output current
1757 *
1758 * Check if the AC charger has been disconnected and reconnected without
1759 * AB8500 rising an interrupt. Returns 0 on success.
1760 */
1761static int ab8500_charger_ac_check_enable(struct ux500_charger *charger,
1762 int vset, int iset)
1763{
1764 u8 mainch_ctrl1 = 0;
1765 int ret = 0;
1766
1767 struct ab8500_charger *di = to_ab8500_charger_ac_device_info(charger);
1768
1769 if (!di->ac.charger_connected)
1770 return ret;
1771
1772 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1773 AB8500_MCH_CTRL1, &mainch_ctrl1);
1774 if (ret < 0) {
1775 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
1776 return ret;
1777 }
1778 dev_dbg(di->dev, "AC charger ctrl: 0x%02x\n", mainch_ctrl1);
1779
1780 if (!(mainch_ctrl1 & MAIN_CH_ENA)) {
1781 dev_info(di->dev, "Charging has been disabled abnormally and will be re-enabled\n");
1782
1783 ret = abx500_mask_and_set_register_interruptible(di->dev,
1784 AB8500_CHARGER, AB8500_CHARGER_CTRL,
1785 DROP_COUNT_RESET, DROP_COUNT_RESET);
1786
1787 if (ret < 0) {
1788 dev_err(di->dev, "ab8500 write failed %d\n", __LINE__);
1789 return ret;
1790 }
1791
1792 ret = ab8500_charger_ac_en(&di->usb_chg, true, vset, iset);
1793 if (ret < 0) {
1794 dev_err(di->dev, "failed to enable AC charger %d\n",
1795 __LINE__);
1796 return ret;
1797 }
1798 }
1799 return ret;
1800}
1801
1802/**
Arun Murthy84edbee2012-02-29 21:54:26 +05301803 * ab8500_charger_watchdog_kick() - kick charger watchdog
1804 * @di: pointer to the ab8500_charger structure
1805 *
1806 * Kick charger watchdog
1807 * Returns error code in case of failure else 0(on success)
1808 */
1809static int ab8500_charger_watchdog_kick(struct ux500_charger *charger)
1810{
1811 int ret;
1812 struct ab8500_charger *di;
1813
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001814 if (charger->psy->desc->type == POWER_SUPPLY_TYPE_MAINS)
Arun Murthy84edbee2012-02-29 21:54:26 +05301815 di = to_ab8500_charger_ac_device_info(charger);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001816 else if (charger->psy->desc->type == POWER_SUPPLY_TYPE_USB)
Arun Murthy84edbee2012-02-29 21:54:26 +05301817 di = to_ab8500_charger_usb_device_info(charger);
1818 else
1819 return -ENXIO;
1820
1821 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1822 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
1823 if (ret)
1824 dev_err(di->dev, "Failed to kick WD!\n");
1825
1826 return ret;
1827}
1828
1829/**
1830 * ab8500_charger_update_charger_current() - update charger current
1831 * @di: pointer to the ab8500_charger structure
1832 *
1833 * Update the charger output current for the specified charger
1834 * Returns error code in case of failure else 0(on success)
1835 */
1836static int ab8500_charger_update_charger_current(struct ux500_charger *charger,
1837 int ich_out)
1838{
1839 int ret;
Arun Murthy84edbee2012-02-29 21:54:26 +05301840 struct ab8500_charger *di;
1841
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001842 if (charger->psy->desc->type == POWER_SUPPLY_TYPE_MAINS)
Arun Murthy84edbee2012-02-29 21:54:26 +05301843 di = to_ab8500_charger_ac_device_info(charger);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001844 else if (charger->psy->desc->type == POWER_SUPPLY_TYPE_USB)
Arun Murthy84edbee2012-02-29 21:54:26 +05301845 di = to_ab8500_charger_usb_device_info(charger);
1846 else
1847 return -ENXIO;
1848
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001849 ret = ab8500_charger_set_output_curr(di, ich_out);
Arun Murthy84edbee2012-02-29 21:54:26 +05301850 if (ret) {
Johan Bjornstedtf8e96df2012-01-18 12:44:55 +01001851 dev_err(di->dev, "%s "
1852 "Failed to set ChOutputCurentLevel\n",
1853 __func__);
Arun Murthy84edbee2012-02-29 21:54:26 +05301854 return ret;
1855 }
1856
1857 /* Reset the main and usb drop input current measurement counter */
1858 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
Lee Jones4dcdf572013-02-14 09:24:10 +00001859 AB8500_CHARGER_CTRL, DROP_COUNT_RESET);
Arun Murthy84edbee2012-02-29 21:54:26 +05301860 if (ret) {
1861 dev_err(di->dev, "%s write failed\n", __func__);
1862 return ret;
1863 }
1864
1865 return ret;
1866}
1867
Lee Jonesdb43e6c2013-02-14 12:39:15 +00001868/**
1869 * ab8540_charger_power_path_enable() - enable usb power path mode
1870 * @charger: pointer to the ux500_charger structure
1871 * @enable: enable/disable flag
1872 *
1873 * Enable or disable the power path for usb mode
1874 * Returns error code in case of failure else 0(on success)
1875 */
1876static int ab8540_charger_power_path_enable(struct ux500_charger *charger,
1877 bool enable)
1878{
1879 int ret;
1880 struct ab8500_charger *di;
1881
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001882 if (charger->psy->desc->type == POWER_SUPPLY_TYPE_USB)
Lee Jonesdb43e6c2013-02-14 12:39:15 +00001883 di = to_ab8500_charger_usb_device_info(charger);
1884 else
1885 return -ENXIO;
1886
1887 ret = abx500_mask_and_set_register_interruptible(di->dev,
1888 AB8500_CHARGER, AB8540_USB_PP_MODE_REG,
1889 BUS_POWER_PATH_MODE_ENA, enable);
1890 if (ret) {
1891 dev_err(di->dev, "%s write failed\n", __func__);
1892 return ret;
1893 }
1894
1895 return ret;
1896}
1897
1898
1899/**
1900 * ab8540_charger_usb_pre_chg_enable() - enable usb pre change
1901 * @charger: pointer to the ux500_charger structure
1902 * @enable: enable/disable flag
1903 *
1904 * Enable or disable the pre-chage for usb mode
1905 * Returns error code in case of failure else 0(on success)
1906 */
1907static int ab8540_charger_usb_pre_chg_enable(struct ux500_charger *charger,
1908 bool enable)
1909{
1910 int ret;
1911 struct ab8500_charger *di;
1912
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001913 if (charger->psy->desc->type == POWER_SUPPLY_TYPE_USB)
Lee Jonesdb43e6c2013-02-14 12:39:15 +00001914 di = to_ab8500_charger_usb_device_info(charger);
1915 else
1916 return -ENXIO;
1917
1918 ret = abx500_mask_and_set_register_interruptible(di->dev,
1919 AB8500_CHARGER, AB8540_USB_PP_CHR_REG,
1920 BUS_POWER_PATH_PRECHG_ENA, enable);
1921 if (ret) {
1922 dev_err(di->dev, "%s write failed\n", __func__);
1923 return ret;
1924 }
1925
1926 return ret;
1927}
1928
Arun Murthy84edbee2012-02-29 21:54:26 +05301929static int ab8500_charger_get_ext_psy_data(struct device *dev, void *data)
1930{
1931 struct power_supply *psy;
Andy Shevchenkoea32cea2016-03-17 14:22:29 -07001932 struct power_supply *ext = dev_get_drvdata(dev);
1933 const char **supplicants = (const char **)ext->supplied_to;
Arun Murthy84edbee2012-02-29 21:54:26 +05301934 struct ab8500_charger *di;
1935 union power_supply_propval ret;
Andy Shevchenkoea32cea2016-03-17 14:22:29 -07001936 int j;
Arun Murthy84edbee2012-02-29 21:54:26 +05301937 struct ux500_charger *usb_chg;
1938
1939 usb_chg = (struct ux500_charger *)data;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001940 psy = usb_chg->psy;
Arun Murthy84edbee2012-02-29 21:54:26 +05301941
1942 di = to_ab8500_charger_usb_device_info(usb_chg);
1943
Arun Murthy84edbee2012-02-29 21:54:26 +05301944 /* For all psy where the driver name appears in any supplied_to */
Andy Shevchenkoea32cea2016-03-17 14:22:29 -07001945 j = match_string(supplicants, ext->num_supplicants, psy->desc->name);
1946 if (j < 0)
Arun Murthy84edbee2012-02-29 21:54:26 +05301947 return 0;
1948
1949 /* Go through all properties for the psy */
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001950 for (j = 0; j < ext->desc->num_properties; j++) {
Arun Murthy84edbee2012-02-29 21:54:26 +05301951 enum power_supply_property prop;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001952 prop = ext->desc->properties[j];
Arun Murthy84edbee2012-02-29 21:54:26 +05301953
Krzysztof Kozlowski15077fc2015-03-12 08:44:06 +01001954 if (power_supply_get_property(ext, prop, &ret))
Arun Murthy84edbee2012-02-29 21:54:26 +05301955 continue;
1956
1957 switch (prop) {
1958 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001959 switch (ext->desc->type) {
Arun Murthy84edbee2012-02-29 21:54:26 +05301960 case POWER_SUPPLY_TYPE_BATTERY:
1961 di->vbat = ret.intval / 1000;
1962 break;
1963 default:
1964 break;
1965 }
1966 break;
1967 default:
1968 break;
1969 }
1970 }
1971 return 0;
1972}
1973
1974/**
1975 * ab8500_charger_check_vbat_work() - keep vbus current within spec
1976 * @work pointer to the work_struct structure
1977 *
1978 * Due to a asic bug it is necessary to lower the input current to the vbus
1979 * charger when charging with at some specific levels. This issue is only valid
1980 * for below a certain battery voltage. This function makes sure that the
1981 * the allowed current limit isn't exceeded.
1982 */
1983static void ab8500_charger_check_vbat_work(struct work_struct *work)
1984{
1985 int t = 10;
1986 struct ab8500_charger *di = container_of(work,
1987 struct ab8500_charger, check_vbat_work.work);
1988
1989 class_for_each_device(power_supply_class, NULL,
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001990 di->usb_chg.psy, ab8500_charger_get_ext_psy_data);
Arun Murthy84edbee2012-02-29 21:54:26 +05301991
1992 /* First run old_vbat is 0. */
1993 if (di->old_vbat == 0)
1994 di->old_vbat = di->vbat;
1995
1996 if (!((di->old_vbat <= VBAT_TRESH_IP_CUR_RED &&
1997 di->vbat <= VBAT_TRESH_IP_CUR_RED) ||
1998 (di->old_vbat > VBAT_TRESH_IP_CUR_RED &&
1999 di->vbat > VBAT_TRESH_IP_CUR_RED))) {
2000
2001 dev_dbg(di->dev, "Vbat did cross threshold, curr: %d, new: %d,"
Lee Jonesf7470b52013-02-14 12:37:29 +00002002 " old: %d\n", di->max_usb_in_curr.usb_type_max,
2003 di->vbat, di->old_vbat);
2004 ab8500_charger_set_vbus_in_curr(di,
2005 di->max_usb_in_curr.usb_type_max);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002006 power_supply_changed(di->usb_chg.psy);
Arun Murthy84edbee2012-02-29 21:54:26 +05302007 }
2008
2009 di->old_vbat = di->vbat;
2010
2011 /*
2012 * No need to check the battery voltage every second when not close to
2013 * the threshold.
2014 */
2015 if (di->vbat < (VBAT_TRESH_IP_CUR_RED + 100) &&
2016 (di->vbat > (VBAT_TRESH_IP_CUR_RED - 100)))
2017 t = 1;
2018
2019 queue_delayed_work(di->charger_wq, &di->check_vbat_work, t * HZ);
2020}
2021
2022/**
2023 * ab8500_charger_check_hw_failure_work() - check main charger failure
2024 * @work: pointer to the work_struct structure
2025 *
2026 * Work queue function for checking the main charger status
2027 */
2028static void ab8500_charger_check_hw_failure_work(struct work_struct *work)
2029{
2030 int ret;
2031 u8 reg_value;
2032
2033 struct ab8500_charger *di = container_of(work,
2034 struct ab8500_charger, check_hw_failure_work.work);
2035
2036 /* Check if the status bits for HW failure is still active */
2037 if (di->flags.mainextchnotok) {
2038 ret = abx500_get_register_interruptible(di->dev,
2039 AB8500_CHARGER, AB8500_CH_STATUS2_REG, &reg_value);
2040 if (ret < 0) {
2041 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2042 return;
2043 }
2044 if (!(reg_value & MAIN_CH_NOK)) {
2045 di->flags.mainextchnotok = false;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002046 ab8500_power_supply_changed(di, di->ac_chg.psy);
Arun Murthy84edbee2012-02-29 21:54:26 +05302047 }
2048 }
2049 if (di->flags.vbus_ovv) {
2050 ret = abx500_get_register_interruptible(di->dev,
2051 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG,
2052 &reg_value);
2053 if (ret < 0) {
2054 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2055 return;
2056 }
2057 if (!(reg_value & VBUS_OVV_TH)) {
2058 di->flags.vbus_ovv = false;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002059 ab8500_power_supply_changed(di, di->usb_chg.psy);
Arun Murthy84edbee2012-02-29 21:54:26 +05302060 }
2061 }
2062 /* If we still have a failure, schedule a new check */
2063 if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
2064 queue_delayed_work(di->charger_wq,
2065 &di->check_hw_failure_work, round_jiffies(HZ));
2066 }
2067}
2068
2069/**
2070 * ab8500_charger_kick_watchdog_work() - kick the watchdog
2071 * @work: pointer to the work_struct structure
2072 *
2073 * Work queue function for kicking the charger watchdog.
2074 *
2075 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
2076 * logic. That means we have to continously kick the charger
2077 * watchdog even when no charger is connected. This is only
2078 * valid once the AC charger has been enabled. This is
2079 * a bug that is not handled by the algorithm and the
2080 * watchdog have to be kicked by the charger driver
2081 * when the AC charger is disabled
2082 */
2083static void ab8500_charger_kick_watchdog_work(struct work_struct *work)
2084{
2085 int ret;
2086
2087 struct ab8500_charger *di = container_of(work,
2088 struct ab8500_charger, kick_wd_work.work);
2089
2090 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
2091 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
2092 if (ret)
2093 dev_err(di->dev, "Failed to kick WD!\n");
2094
2095 /* Schedule a new watchdog kick */
2096 queue_delayed_work(di->charger_wq,
2097 &di->kick_wd_work, round_jiffies(WD_KICK_INTERVAL));
2098}
2099
2100/**
2101 * ab8500_charger_ac_work() - work to get and set main charger status
2102 * @work: pointer to the work_struct structure
2103 *
2104 * Work queue function for checking the main charger status
2105 */
2106static void ab8500_charger_ac_work(struct work_struct *work)
2107{
2108 int ret;
2109
2110 struct ab8500_charger *di = container_of(work,
2111 struct ab8500_charger, ac_work);
2112
2113 /*
2114 * Since we can't be sure that the events are received
2115 * synchronously, we have the check if the main charger is
2116 * connected by reading the status register
2117 */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002118 ret = ab8500_charger_detect_chargers(di, false);
Arun Murthy84edbee2012-02-29 21:54:26 +05302119 if (ret < 0)
2120 return;
2121
2122 if (ret & AC_PW_CONN) {
2123 di->ac.charger_connected = 1;
2124 di->ac_conn = true;
2125 } else {
2126 di->ac.charger_connected = 0;
2127 }
2128
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002129 ab8500_power_supply_changed(di, di->ac_chg.psy);
2130 sysfs_notify(&di->ac_chg.psy->dev.kobj, NULL, "present");
Arun Murthy84edbee2012-02-29 21:54:26 +05302131}
2132
Lee Jonesb269fff2013-01-11 13:12:51 +00002133static void ab8500_charger_usb_attached_work(struct work_struct *work)
2134{
2135 struct ab8500_charger *di = container_of(work,
2136 struct ab8500_charger,
2137 usb_charger_attached_work.work);
2138 int usbch = (USB_CH_VBUSDROP | USB_CH_VBUSDETDBNC);
2139 int ret, i;
2140 u8 statval;
2141
2142 for (i = 0; i < 10; i++) {
2143 ret = abx500_get_register_interruptible(di->dev,
2144 AB8500_CHARGER,
2145 AB8500_CH_USBCH_STAT1_REG,
2146 &statval);
2147 if (ret < 0) {
2148 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
2149 goto reschedule;
2150 }
2151 if ((statval & usbch) != usbch)
2152 goto reschedule;
2153
2154 msleep(CHARGER_STATUS_POLL);
2155 }
2156
2157 ab8500_charger_usb_en(&di->usb_chg, 0, 0, 0);
2158
2159 mutex_lock(&di->charger_attached_mutex);
2160 mutex_unlock(&di->charger_attached_mutex);
2161
2162 return;
2163
2164reschedule:
2165 queue_delayed_work(di->charger_wq,
2166 &di->usb_charger_attached_work,
2167 HZ);
2168}
2169
2170static void ab8500_charger_ac_attached_work(struct work_struct *work)
2171{
2172
2173 struct ab8500_charger *di = container_of(work,
2174 struct ab8500_charger,
2175 ac_charger_attached_work.work);
2176 int mainch = (MAIN_CH_STATUS2_MAINCHGDROP |
2177 MAIN_CH_STATUS2_MAINCHARGERDETDBNC);
2178 int ret, i;
2179 u8 statval;
2180
2181 for (i = 0; i < 10; i++) {
2182 ret = abx500_get_register_interruptible(di->dev,
2183 AB8500_CHARGER,
2184 AB8500_CH_STATUS2_REG,
2185 &statval);
2186 if (ret < 0) {
2187 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
2188 goto reschedule;
2189 }
2190
2191 if ((statval & mainch) != mainch)
2192 goto reschedule;
2193
2194 msleep(CHARGER_STATUS_POLL);
2195 }
2196
2197 ab8500_charger_ac_en(&di->ac_chg, 0, 0, 0);
2198 queue_work(di->charger_wq, &di->ac_work);
2199
2200 mutex_lock(&di->charger_attached_mutex);
2201 mutex_unlock(&di->charger_attached_mutex);
2202
2203 return;
2204
2205reschedule:
2206 queue_delayed_work(di->charger_wq,
2207 &di->ac_charger_attached_work,
2208 HZ);
2209}
2210
Arun Murthy84edbee2012-02-29 21:54:26 +05302211/**
2212 * ab8500_charger_detect_usb_type_work() - work to detect USB type
2213 * @work: Pointer to the work_struct structure
2214 *
2215 * Detect the type of USB plugged
2216 */
Anton Vorontsov64eb9b02012-03-14 04:43:11 +04002217static void ab8500_charger_detect_usb_type_work(struct work_struct *work)
Arun Murthy84edbee2012-02-29 21:54:26 +05302218{
2219 int ret;
2220
2221 struct ab8500_charger *di = container_of(work,
2222 struct ab8500_charger, detect_usb_type_work);
2223
2224 /*
2225 * Since we can't be sure that the events are received
2226 * synchronously, we have the check if is
2227 * connected by reading the status register
2228 */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002229 ret = ab8500_charger_detect_chargers(di, false);
Arun Murthy84edbee2012-02-29 21:54:26 +05302230 if (ret < 0)
2231 return;
2232
2233 if (!(ret & USB_PW_CONN)) {
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002234 dev_dbg(di->dev, "%s di->vbus_detected = false\n", __func__);
2235 di->vbus_detected = false;
Arun Murthy84edbee2012-02-29 21:54:26 +05302236 ab8500_charger_set_usb_connected(di, false);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002237 ab8500_power_supply_changed(di, di->usb_chg.psy);
Arun Murthy84edbee2012-02-29 21:54:26 +05302238 } else {
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002239 dev_dbg(di->dev, "%s di->vbus_detected = true\n", __func__);
2240 di->vbus_detected = true;
Arun Murthy84edbee2012-02-29 21:54:26 +05302241
2242 if (is_ab8500_1p1_or_earlier(di->parent)) {
2243 ret = ab8500_charger_detect_usb_type(di);
2244 if (!ret) {
2245 ab8500_charger_set_usb_connected(di, true);
2246 ab8500_power_supply_changed(di,
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002247 di->usb_chg.psy);
Arun Murthy84edbee2012-02-29 21:54:26 +05302248 }
2249 } else {
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002250 /*
2251 * For ABB cut2.0 and onwards we have an IRQ,
Arun Murthy84edbee2012-02-29 21:54:26 +05302252 * USB_LINK_STATUS that will be triggered when the USB
2253 * link status changes. The exception is USB connected
2254 * during startup. Then we don't get a
2255 * USB_LINK_STATUS IRQ
2256 */
2257 if (di->vbus_detected_start) {
2258 di->vbus_detected_start = false;
2259 ret = ab8500_charger_detect_usb_type(di);
2260 if (!ret) {
2261 ab8500_charger_set_usb_connected(di,
2262 true);
2263 ab8500_power_supply_changed(di,
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002264 di->usb_chg.psy);
Arun Murthy84edbee2012-02-29 21:54:26 +05302265 }
2266 }
2267 }
2268 }
2269}
2270
2271/**
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002272 * ab8500_charger_usb_link_attach_work() - work to detect USB type
Marcus Cooper4b45f4a2013-01-11 13:13:04 +00002273 * @work: pointer to the work_struct structure
2274 *
2275 * Detect the type of USB plugged
2276 */
2277static void ab8500_charger_usb_link_attach_work(struct work_struct *work)
2278{
2279 struct ab8500_charger *di =
2280 container_of(work, struct ab8500_charger, attach_work.work);
2281 int ret;
2282
2283 /* Update maximum input current if USB enumeration is not detected */
2284 if (!di->usb.charger_online) {
Lee Jonesf7470b52013-02-14 12:37:29 +00002285 ret = ab8500_charger_set_vbus_in_curr(di,
2286 di->max_usb_in_curr.usb_type_max);
Marcus Cooper4b45f4a2013-01-11 13:13:04 +00002287 if (ret)
2288 return;
2289 }
2290
2291 ab8500_charger_set_usb_connected(di, true);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002292 ab8500_power_supply_changed(di, di->usb_chg.psy);
Marcus Cooper4b45f4a2013-01-11 13:13:04 +00002293}
2294
2295/**
Arun Murthy84edbee2012-02-29 21:54:26 +05302296 * ab8500_charger_usb_link_status_work() - work to detect USB type
2297 * @work: pointer to the work_struct structure
2298 *
2299 * Detect the type of USB plugged
2300 */
2301static void ab8500_charger_usb_link_status_work(struct work_struct *work)
2302{
Henrik Sölverff380902012-04-17 15:51:01 +02002303 int detected_chargers;
Arun Murthy84edbee2012-02-29 21:54:26 +05302304 int ret;
Henrik Sölverff380902012-04-17 15:51:01 +02002305 u8 val;
Hakan Bergd4337662012-07-26 11:10:35 +02002306 u8 link_status;
Arun Murthy84edbee2012-02-29 21:54:26 +05302307
2308 struct ab8500_charger *di = container_of(work,
2309 struct ab8500_charger, usb_link_status_work);
2310
2311 /*
2312 * Since we can't be sure that the events are received
2313 * synchronously, we have the check if is
2314 * connected by reading the status register
2315 */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002316 detected_chargers = ab8500_charger_detect_chargers(di, false);
Henrik Sölverff380902012-04-17 15:51:01 +02002317 if (detected_chargers < 0)
Arun Murthy84edbee2012-02-29 21:54:26 +05302318 return;
2319
Henrik Sölverff380902012-04-17 15:51:01 +02002320 /*
2321 * Some chargers that breaks the USB spec is
2322 * identified as invalid by AB8500 and it refuse
2323 * to start the charging process. but by jumping
2324 * thru a few hoops it can be forced to start.
2325 */
Marcus Cooper88efdb82012-08-30 14:12:53 +02002326 if (is_ab8500(di->parent))
2327 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
2328 AB8500_USB_LINE_STAT_REG, &val);
2329 else
2330 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
2331 AB8500_USB_LINK1_STAT_REG, &val);
2332
Henrik Sölverff380902012-04-17 15:51:01 +02002333 if (ret >= 0)
2334 dev_dbg(di->dev, "UsbLineStatus register = 0x%02x\n", val);
2335 else
2336 dev_dbg(di->dev, "Error reading USB link status\n");
2337
Lee Jones861a30d2012-08-29 20:36:51 +08002338 if (is_ab8500(di->parent))
Hakan Bergd4337662012-07-26 11:10:35 +02002339 link_status = AB8500_USB_LINK_STATUS;
Lee Jones861a30d2012-08-29 20:36:51 +08002340 else
2341 link_status = AB8505_USB_LINK_STATUS;
Hakan Bergd4337662012-07-26 11:10:35 +02002342
Henrik Sölverff380902012-04-17 15:51:01 +02002343 if (detected_chargers & USB_PW_CONN) {
Lee Jones861a30d2012-08-29 20:36:51 +08002344 if (((val & link_status) >> USB_LINK_STATUS_SHIFT) ==
2345 USB_STAT_NOT_VALID_LINK &&
Henrik Sölverff380902012-04-17 15:51:01 +02002346 di->invalid_charger_detect_state == 0) {
Lee Jones861a30d2012-08-29 20:36:51 +08002347 dev_dbg(di->dev,
2348 "Invalid charger detected, state= 0\n");
Henrik Sölverff380902012-04-17 15:51:01 +02002349 /*Enable charger*/
2350 abx500_mask_and_set_register_interruptible(di->dev,
Lee Jones861a30d2012-08-29 20:36:51 +08002351 AB8500_CHARGER, AB8500_USBCH_CTRL1_REG,
2352 USB_CH_ENA, USB_CH_ENA);
Henrik Sölverff380902012-04-17 15:51:01 +02002353 /*Enable charger detection*/
Marcus Cooperb3ea5f42012-08-29 17:56:19 +02002354 abx500_mask_and_set_register_interruptible(di->dev,
2355 AB8500_USB, AB8500_USB_LINE_CTRL2_REG,
2356 USB_CH_DET, USB_CH_DET);
Henrik Sölverff380902012-04-17 15:51:01 +02002357 di->invalid_charger_detect_state = 1;
2358 /*exit and wait for new link status interrupt.*/
2359 return;
2360
2361 }
2362 if (di->invalid_charger_detect_state == 1) {
Lee Jones861a30d2012-08-29 20:36:51 +08002363 dev_dbg(di->dev,
2364 "Invalid charger detected, state= 1\n");
Henrik Sölverff380902012-04-17 15:51:01 +02002365 /*Stop charger detection*/
Marcus Cooperb3ea5f42012-08-29 17:56:19 +02002366 abx500_mask_and_set_register_interruptible(di->dev,
2367 AB8500_USB, AB8500_USB_LINE_CTRL2_REG,
2368 USB_CH_DET, 0x00);
Henrik Sölverff380902012-04-17 15:51:01 +02002369 /*Check link status*/
Marcus Cooper88efdb82012-08-30 14:12:53 +02002370 if (is_ab8500(di->parent))
2371 ret = abx500_get_register_interruptible(di->dev,
2372 AB8500_USB, AB8500_USB_LINE_STAT_REG,
2373 &val);
2374 else
2375 ret = abx500_get_register_interruptible(di->dev,
2376 AB8500_USB, AB8500_USB_LINK1_STAT_REG,
2377 &val);
2378
Henrik Sölverff380902012-04-17 15:51:01 +02002379 dev_dbg(di->dev, "USB link status= 0x%02x\n",
Lee Jones861a30d2012-08-29 20:36:51 +08002380 (val & link_status) >> USB_LINK_STATUS_SHIFT);
Henrik Sölverff380902012-04-17 15:51:01 +02002381 di->invalid_charger_detect_state = 2;
2382 }
2383 } else {
2384 di->invalid_charger_detect_state = 0;
2385 }
2386
2387 if (!(detected_chargers & USB_PW_CONN)) {
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002388 di->vbus_detected = false;
Arun Murthy84edbee2012-02-29 21:54:26 +05302389 ab8500_charger_set_usb_connected(di, false);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002390 ab8500_power_supply_changed(di, di->usb_chg.psy);
Marcus Cooper4b45f4a2013-01-11 13:13:04 +00002391 return;
2392 }
Arun Murthy84edbee2012-02-29 21:54:26 +05302393
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002394 dev_dbg(di->dev,"%s di->vbus_detected = true\n",__func__);
2395 di->vbus_detected = true;
Marcus Cooper4b45f4a2013-01-11 13:13:04 +00002396 ret = ab8500_charger_read_usb_type(di);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002397 if (ret) {
2398 if (ret == -ENXIO) {
2399 /* No valid charger type detected */
2400 ab8500_charger_set_usb_connected(di, false);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002401 ab8500_power_supply_changed(di, di->usb_chg.psy);
Arun Murthy84edbee2012-02-29 21:54:26 +05302402 }
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002403 return;
2404 }
2405
2406 if (di->usb_device_is_unrecognised) {
2407 dev_dbg(di->dev,
2408 "Potential Legacy Charger device. "
2409 "Delay work for %d msec for USB enum "
2410 "to finish",
2411 WAIT_ACA_RID_ENUMERATION);
2412 queue_delayed_work(di->charger_wq,
2413 &di->attach_work,
2414 msecs_to_jiffies(WAIT_ACA_RID_ENUMERATION));
2415 } else if (di->is_aca_rid == 1) {
2416 /* Only wait once */
2417 di->is_aca_rid++;
2418 dev_dbg(di->dev,
2419 "%s Wait %d msec for USB enum to finish",
2420 __func__, WAIT_ACA_RID_ENUMERATION);
2421 queue_delayed_work(di->charger_wq,
2422 &di->attach_work,
2423 msecs_to_jiffies(WAIT_ACA_RID_ENUMERATION));
2424 } else {
2425 queue_delayed_work(di->charger_wq,
2426 &di->attach_work,
2427 0);
Arun Murthy84edbee2012-02-29 21:54:26 +05302428 }
2429}
2430
2431static void ab8500_charger_usb_state_changed_work(struct work_struct *work)
2432{
2433 int ret;
2434 unsigned long flags;
2435
2436 struct ab8500_charger *di = container_of(work,
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002437 struct ab8500_charger, usb_state_changed_work.work);
Arun Murthy84edbee2012-02-29 21:54:26 +05302438
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002439 if (!di->vbus_detected) {
2440 dev_dbg(di->dev,
2441 "%s !di->vbus_detected\n",
2442 __func__);
Arun Murthy84edbee2012-02-29 21:54:26 +05302443 return;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002444 }
Arun Murthy84edbee2012-02-29 21:54:26 +05302445
2446 spin_lock_irqsave(&di->usb_state.usb_lock, flags);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002447 di->usb_state.state = di->usb_state.state_tmp;
2448 di->usb_state.usb_current = di->usb_state.usb_current_tmp;
Arun Murthy84edbee2012-02-29 21:54:26 +05302449 spin_unlock_irqrestore(&di->usb_state.usb_lock, flags);
2450
Arun Murthy84edbee2012-02-29 21:54:26 +05302451 dev_dbg(di->dev, "%s USB state: 0x%02x mA: %d\n",
2452 __func__, di->usb_state.state, di->usb_state.usb_current);
2453
2454 switch (di->usb_state.state) {
2455 case AB8500_BM_USB_STATE_RESET_HS:
2456 case AB8500_BM_USB_STATE_RESET_FS:
2457 case AB8500_BM_USB_STATE_SUSPEND:
2458 case AB8500_BM_USB_STATE_MAX:
2459 ab8500_charger_set_usb_connected(di, false);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002460 ab8500_power_supply_changed(di, di->usb_chg.psy);
Arun Murthy84edbee2012-02-29 21:54:26 +05302461 break;
2462
2463 case AB8500_BM_USB_STATE_RESUME:
2464 /*
2465 * when suspend->resume there should be delay
2466 * of 1sec for enabling charging
2467 */
2468 msleep(1000);
2469 /* Intentional fall through */
2470 case AB8500_BM_USB_STATE_CONFIGURED:
2471 /*
2472 * USB is configured, enable charging with the charging
2473 * input current obtained from USB driver
2474 */
2475 if (!ab8500_charger_get_usb_cur(di)) {
2476 /* Update maximum input current */
2477 ret = ab8500_charger_set_vbus_in_curr(di,
Lee Jonesf7470b52013-02-14 12:37:29 +00002478 di->max_usb_in_curr.usb_type_max);
Arun Murthy84edbee2012-02-29 21:54:26 +05302479 if (ret)
2480 return;
2481
2482 ab8500_charger_set_usb_connected(di, true);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002483 ab8500_power_supply_changed(di, di->usb_chg.psy);
Arun Murthy84edbee2012-02-29 21:54:26 +05302484 }
2485 break;
2486
2487 default:
2488 break;
2489 };
2490}
2491
2492/**
2493 * ab8500_charger_check_usbchargernotok_work() - check USB chg not ok status
2494 * @work: pointer to the work_struct structure
2495 *
2496 * Work queue function for checking the USB charger Not OK status
2497 */
2498static void ab8500_charger_check_usbchargernotok_work(struct work_struct *work)
2499{
2500 int ret;
2501 u8 reg_value;
2502 bool prev_status;
2503
2504 struct ab8500_charger *di = container_of(work,
2505 struct ab8500_charger, check_usbchgnotok_work.work);
2506
2507 /* Check if the status bit for usbchargernotok is still active */
2508 ret = abx500_get_register_interruptible(di->dev,
2509 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, &reg_value);
2510 if (ret < 0) {
2511 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2512 return;
2513 }
2514 prev_status = di->flags.usbchargernotok;
2515
2516 if (reg_value & VBUS_CH_NOK) {
2517 di->flags.usbchargernotok = true;
2518 /* Check again in 1sec */
2519 queue_delayed_work(di->charger_wq,
2520 &di->check_usbchgnotok_work, HZ);
2521 } else {
2522 di->flags.usbchargernotok = false;
2523 di->flags.vbus_collapse = false;
2524 }
2525
2526 if (prev_status != di->flags.usbchargernotok)
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002527 ab8500_power_supply_changed(di, di->usb_chg.psy);
Arun Murthy84edbee2012-02-29 21:54:26 +05302528}
2529
2530/**
2531 * ab8500_charger_check_main_thermal_prot_work() - check main thermal status
2532 * @work: pointer to the work_struct structure
2533 *
2534 * Work queue function for checking the Main thermal prot status
2535 */
2536static void ab8500_charger_check_main_thermal_prot_work(
2537 struct work_struct *work)
2538{
2539 int ret;
2540 u8 reg_value;
2541
2542 struct ab8500_charger *di = container_of(work,
2543 struct ab8500_charger, check_main_thermal_prot_work);
2544
2545 /* Check if the status bit for main_thermal_prot is still active */
2546 ret = abx500_get_register_interruptible(di->dev,
2547 AB8500_CHARGER, AB8500_CH_STATUS2_REG, &reg_value);
2548 if (ret < 0) {
2549 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2550 return;
2551 }
2552 if (reg_value & MAIN_CH_TH_PROT)
2553 di->flags.main_thermal_prot = true;
2554 else
2555 di->flags.main_thermal_prot = false;
2556
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002557 ab8500_power_supply_changed(di, di->ac_chg.psy);
Arun Murthy84edbee2012-02-29 21:54:26 +05302558}
2559
2560/**
2561 * ab8500_charger_check_usb_thermal_prot_work() - check usb thermal status
2562 * @work: pointer to the work_struct structure
2563 *
2564 * Work queue function for checking the USB thermal prot status
2565 */
2566static void ab8500_charger_check_usb_thermal_prot_work(
2567 struct work_struct *work)
2568{
2569 int ret;
2570 u8 reg_value;
2571
2572 struct ab8500_charger *di = container_of(work,
2573 struct ab8500_charger, check_usb_thermal_prot_work);
2574
2575 /* Check if the status bit for usb_thermal_prot is still active */
2576 ret = abx500_get_register_interruptible(di->dev,
2577 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, &reg_value);
2578 if (ret < 0) {
2579 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2580 return;
2581 }
2582 if (reg_value & USB_CH_TH_PROT)
2583 di->flags.usb_thermal_prot = true;
2584 else
2585 di->flags.usb_thermal_prot = false;
2586
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002587 ab8500_power_supply_changed(di, di->usb_chg.psy);
Arun Murthy84edbee2012-02-29 21:54:26 +05302588}
2589
2590/**
2591 * ab8500_charger_mainchunplugdet_handler() - main charger unplugged
2592 * @irq: interrupt number
2593 * @_di: pointer to the ab8500_charger structure
2594 *
2595 * Returns IRQ status(IRQ_HANDLED)
2596 */
2597static irqreturn_t ab8500_charger_mainchunplugdet_handler(int irq, void *_di)
2598{
2599 struct ab8500_charger *di = _di;
2600
2601 dev_dbg(di->dev, "Main charger unplugged\n");
2602 queue_work(di->charger_wq, &di->ac_work);
2603
Lee Jonesb269fff2013-01-11 13:12:51 +00002604 cancel_delayed_work_sync(&di->ac_charger_attached_work);
2605 mutex_lock(&di->charger_attached_mutex);
2606 mutex_unlock(&di->charger_attached_mutex);
2607
Arun Murthy84edbee2012-02-29 21:54:26 +05302608 return IRQ_HANDLED;
2609}
2610
2611/**
2612 * ab8500_charger_mainchplugdet_handler() - main charger plugged
2613 * @irq: interrupt number
2614 * @_di: pointer to the ab8500_charger structure
2615 *
2616 * Returns IRQ status(IRQ_HANDLED)
2617 */
2618static irqreturn_t ab8500_charger_mainchplugdet_handler(int irq, void *_di)
2619{
2620 struct ab8500_charger *di = _di;
2621
2622 dev_dbg(di->dev, "Main charger plugged\n");
2623 queue_work(di->charger_wq, &di->ac_work);
2624
Lee Jonesb269fff2013-01-11 13:12:51 +00002625 mutex_lock(&di->charger_attached_mutex);
2626 mutex_unlock(&di->charger_attached_mutex);
Rabin Vincent261c5132012-09-26 13:36:38 +02002627
2628 if (is_ab8500(di->parent))
2629 queue_delayed_work(di->charger_wq,
Lee Jonesb269fff2013-01-11 13:12:51 +00002630 &di->ac_charger_attached_work,
2631 HZ);
Arun Murthy84edbee2012-02-29 21:54:26 +05302632 return IRQ_HANDLED;
2633}
2634
2635/**
2636 * ab8500_charger_mainextchnotok_handler() - main charger not ok
2637 * @irq: interrupt number
2638 * @_di: pointer to the ab8500_charger structure
2639 *
2640 * Returns IRQ status(IRQ_HANDLED)
2641 */
2642static irqreturn_t ab8500_charger_mainextchnotok_handler(int irq, void *_di)
2643{
2644 struct ab8500_charger *di = _di;
2645
2646 dev_dbg(di->dev, "Main charger not ok\n");
2647 di->flags.mainextchnotok = true;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002648 ab8500_power_supply_changed(di, di->ac_chg.psy);
Arun Murthy84edbee2012-02-29 21:54:26 +05302649
2650 /* Schedule a new HW failure check */
2651 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
2652
2653 return IRQ_HANDLED;
2654}
2655
2656/**
2657 * ab8500_charger_mainchthprotr_handler() - Die temp is above main charger
2658 * thermal protection threshold
2659 * @irq: interrupt number
2660 * @_di: pointer to the ab8500_charger structure
2661 *
2662 * Returns IRQ status(IRQ_HANDLED)
2663 */
2664static irqreturn_t ab8500_charger_mainchthprotr_handler(int irq, void *_di)
2665{
2666 struct ab8500_charger *di = _di;
2667
2668 dev_dbg(di->dev,
2669 "Die temp above Main charger thermal protection threshold\n");
2670 queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
2671
2672 return IRQ_HANDLED;
2673}
2674
2675/**
2676 * ab8500_charger_mainchthprotf_handler() - Die temp is below main charger
2677 * thermal protection threshold
2678 * @irq: interrupt number
2679 * @_di: pointer to the ab8500_charger structure
2680 *
2681 * Returns IRQ status(IRQ_HANDLED)
2682 */
2683static irqreturn_t ab8500_charger_mainchthprotf_handler(int irq, void *_di)
2684{
2685 struct ab8500_charger *di = _di;
2686
2687 dev_dbg(di->dev,
2688 "Die temp ok for Main charger thermal protection threshold\n");
2689 queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
2690
2691 return IRQ_HANDLED;
2692}
2693
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002694static void ab8500_charger_vbus_drop_end_work(struct work_struct *work)
2695{
2696 struct ab8500_charger *di = container_of(work,
2697 struct ab8500_charger, vbus_drop_end_work.work);
Lee Jones861a30d2012-08-29 20:36:51 +08002698 int ret, curr;
Lee Jonesf7470b52013-02-14 12:37:29 +00002699 u8 reg_value;
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002700
2701 di->flags.vbus_drop_end = false;
2702
2703 /* Reset the drop counter */
2704 abx500_set_register_interruptible(di->dev,
2705 AB8500_CHARGER, AB8500_CHARGER_CTRL, 0x01);
Lee Jones861a30d2012-08-29 20:36:51 +08002706
2707 if (is_ab8540(di->parent))
2708 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
2709 AB8540_CH_USBCH_STAT3_REG, &reg_value);
2710 else
2711 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
2712 AB8500_CH_USBCH_STAT2_REG, &reg_value);
Lee Jonesf7470b52013-02-14 12:37:29 +00002713 if (ret < 0) {
Lee Jones861a30d2012-08-29 20:36:51 +08002714 dev_err(di->dev, "%s read failed\n", __func__);
2715 return;
2716 }
2717
2718 if (is_ab8540(di->parent))
2719 curr = di->bm->chg_input_curr[
2720 reg_value & AB8540_AUTO_VBUS_IN_CURR_MASK];
2721 else
2722 curr = di->bm->chg_input_curr[
Lee Jonesf7470b52013-02-14 12:37:29 +00002723 reg_value >> AUTO_VBUS_IN_CURR_LIM_SHIFT];
Lee Jones861a30d2012-08-29 20:36:51 +08002724
2725 if (di->max_usb_in_curr.calculated_max != curr) {
2726 /* USB source is collapsing */
2727 di->max_usb_in_curr.calculated_max = curr;
2728 dev_dbg(di->dev,
2729 "VBUS input current limiting to %d mA\n",
2730 di->max_usb_in_curr.calculated_max);
2731 } else {
2732 /*
2733 * USB source can not give more than this amount.
2734 * Taking more will collapse the source.
2735 */
2736 di->max_usb_in_curr.set_max =
2737 di->max_usb_in_curr.calculated_max;
2738 dev_dbg(di->dev,
2739 "VBUS input current limited to %d mA\n",
2740 di->max_usb_in_curr.set_max);
Lee Jonesf7470b52013-02-14 12:37:29 +00002741 }
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002742
2743 if (di->usb.charger_connected)
Lee Jonesf7470b52013-02-14 12:37:29 +00002744 ab8500_charger_set_vbus_in_curr(di,
2745 di->max_usb_in_curr.usb_type_max);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002746}
2747
Arun Murthy84edbee2012-02-29 21:54:26 +05302748/**
2749 * ab8500_charger_vbusdetf_handler() - VBUS falling detected
2750 * @irq: interrupt number
2751 * @_di: pointer to the ab8500_charger structure
2752 *
2753 * Returns IRQ status(IRQ_HANDLED)
2754 */
2755static irqreturn_t ab8500_charger_vbusdetf_handler(int irq, void *_di)
2756{
2757 struct ab8500_charger *di = _di;
2758
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002759 di->vbus_detected = false;
Arun Murthy84edbee2012-02-29 21:54:26 +05302760 dev_dbg(di->dev, "VBUS falling detected\n");
2761 queue_work(di->charger_wq, &di->detect_usb_type_work);
2762
2763 return IRQ_HANDLED;
2764}
2765
2766/**
2767 * ab8500_charger_vbusdetr_handler() - VBUS rising detected
2768 * @irq: interrupt number
2769 * @_di: pointer to the ab8500_charger structure
2770 *
2771 * Returns IRQ status(IRQ_HANDLED)
2772 */
2773static irqreturn_t ab8500_charger_vbusdetr_handler(int irq, void *_di)
2774{
2775 struct ab8500_charger *di = _di;
2776
2777 di->vbus_detected = true;
2778 dev_dbg(di->dev, "VBUS rising detected\n");
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002779
Arun Murthy84edbee2012-02-29 21:54:26 +05302780 queue_work(di->charger_wq, &di->detect_usb_type_work);
2781
2782 return IRQ_HANDLED;
2783}
2784
2785/**
2786 * ab8500_charger_usblinkstatus_handler() - USB link status has changed
2787 * @irq: interrupt number
2788 * @_di: pointer to the ab8500_charger structure
2789 *
2790 * Returns IRQ status(IRQ_HANDLED)
2791 */
2792static irqreturn_t ab8500_charger_usblinkstatus_handler(int irq, void *_di)
2793{
2794 struct ab8500_charger *di = _di;
2795
2796 dev_dbg(di->dev, "USB link status changed\n");
2797
2798 queue_work(di->charger_wq, &di->usb_link_status_work);
2799
2800 return IRQ_HANDLED;
2801}
2802
2803/**
2804 * ab8500_charger_usbchthprotr_handler() - Die temp is above usb charger
2805 * thermal protection threshold
2806 * @irq: interrupt number
2807 * @_di: pointer to the ab8500_charger structure
2808 *
2809 * Returns IRQ status(IRQ_HANDLED)
2810 */
2811static irqreturn_t ab8500_charger_usbchthprotr_handler(int irq, void *_di)
2812{
2813 struct ab8500_charger *di = _di;
2814
2815 dev_dbg(di->dev,
2816 "Die temp above USB charger thermal protection threshold\n");
2817 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2818
2819 return IRQ_HANDLED;
2820}
2821
2822/**
2823 * ab8500_charger_usbchthprotf_handler() - Die temp is below usb charger
2824 * thermal protection threshold
2825 * @irq: interrupt number
2826 * @_di: pointer to the ab8500_charger structure
2827 *
2828 * Returns IRQ status(IRQ_HANDLED)
2829 */
2830static irqreturn_t ab8500_charger_usbchthprotf_handler(int irq, void *_di)
2831{
2832 struct ab8500_charger *di = _di;
2833
2834 dev_dbg(di->dev,
2835 "Die temp ok for USB charger thermal protection threshold\n");
2836 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2837
2838 return IRQ_HANDLED;
2839}
2840
2841/**
2842 * ab8500_charger_usbchargernotokr_handler() - USB charger not ok detected
2843 * @irq: interrupt number
2844 * @_di: pointer to the ab8500_charger structure
2845 *
2846 * Returns IRQ status(IRQ_HANDLED)
2847 */
2848static irqreturn_t ab8500_charger_usbchargernotokr_handler(int irq, void *_di)
2849{
2850 struct ab8500_charger *di = _di;
2851
2852 dev_dbg(di->dev, "Not allowed USB charger detected\n");
2853 queue_delayed_work(di->charger_wq, &di->check_usbchgnotok_work, 0);
2854
2855 return IRQ_HANDLED;
2856}
2857
2858/**
2859 * ab8500_charger_chwdexp_handler() - Charger watchdog expired
2860 * @irq: interrupt number
2861 * @_di: pointer to the ab8500_charger structure
2862 *
2863 * Returns IRQ status(IRQ_HANDLED)
2864 */
2865static irqreturn_t ab8500_charger_chwdexp_handler(int irq, void *_di)
2866{
2867 struct ab8500_charger *di = _di;
2868
2869 dev_dbg(di->dev, "Charger watchdog expired\n");
2870
2871 /*
2872 * The charger that was online when the watchdog expired
2873 * needs to be restarted for charging to start again
2874 */
2875 if (di->ac.charger_online) {
2876 di->ac.wd_expired = true;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002877 ab8500_power_supply_changed(di, di->ac_chg.psy);
Arun Murthy84edbee2012-02-29 21:54:26 +05302878 }
2879 if (di->usb.charger_online) {
2880 di->usb.wd_expired = true;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002881 ab8500_power_supply_changed(di, di->usb_chg.psy);
Arun Murthy84edbee2012-02-29 21:54:26 +05302882 }
2883
2884 return IRQ_HANDLED;
2885}
2886
2887/**
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002888 * ab8500_charger_vbuschdropend_handler() - VBUS drop removed
2889 * @irq: interrupt number
2890 * @_di: pointer to the ab8500_charger structure
2891 *
2892 * Returns IRQ status(IRQ_HANDLED)
2893 */
2894static irqreturn_t ab8500_charger_vbuschdropend_handler(int irq, void *_di)
2895{
2896 struct ab8500_charger *di = _di;
2897
2898 dev_dbg(di->dev, "VBUS charger drop ended\n");
2899 di->flags.vbus_drop_end = true;
Lee Jonesf7470b52013-02-14 12:37:29 +00002900
2901 /*
2902 * VBUS might have dropped due to bad connection.
2903 * Schedule a new input limit set to the value SW requests.
2904 */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002905 queue_delayed_work(di->charger_wq, &di->vbus_drop_end_work,
Lee Jonesf7470b52013-02-14 12:37:29 +00002906 round_jiffies(VBUS_IN_CURR_LIM_RETRY_SET_TIME * HZ));
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01002907
2908 return IRQ_HANDLED;
2909}
2910
2911/**
Arun Murthy84edbee2012-02-29 21:54:26 +05302912 * ab8500_charger_vbusovv_handler() - VBUS overvoltage detected
2913 * @irq: interrupt number
2914 * @_di: pointer to the ab8500_charger structure
2915 *
2916 * Returns IRQ status(IRQ_HANDLED)
2917 */
2918static irqreturn_t ab8500_charger_vbusovv_handler(int irq, void *_di)
2919{
2920 struct ab8500_charger *di = _di;
2921
2922 dev_dbg(di->dev, "VBUS overvoltage detected\n");
2923 di->flags.vbus_ovv = true;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002924 ab8500_power_supply_changed(di, di->usb_chg.psy);
Arun Murthy84edbee2012-02-29 21:54:26 +05302925
2926 /* Schedule a new HW failure check */
2927 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
2928
2929 return IRQ_HANDLED;
2930}
2931
2932/**
2933 * ab8500_charger_ac_get_property() - get the ac/mains properties
2934 * @psy: pointer to the power_supply structure
2935 * @psp: pointer to the power_supply_property structure
2936 * @val: pointer to the power_supply_propval union
2937 *
2938 * This function gets called when an application tries to get the ac/mains
2939 * properties by reading the sysfs files.
2940 * AC/Mains properties are online, present and voltage.
2941 * online: ac/mains charging is in progress or not
2942 * present: presence of the ac/mains
2943 * voltage: AC/Mains voltage
2944 * Returns error code in case of failure else 0(on success)
2945 */
2946static int ab8500_charger_ac_get_property(struct power_supply *psy,
2947 enum power_supply_property psp,
2948 union power_supply_propval *val)
2949{
2950 struct ab8500_charger *di;
Jonas Aaberga864c5a2013-01-11 13:12:53 +00002951 int ret;
Arun Murthy84edbee2012-02-29 21:54:26 +05302952
2953 di = to_ab8500_charger_ac_device_info(psy_to_ux500_charger(psy));
2954
2955 switch (psp) {
2956 case POWER_SUPPLY_PROP_HEALTH:
2957 if (di->flags.mainextchnotok)
2958 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
2959 else if (di->ac.wd_expired || di->usb.wd_expired)
2960 val->intval = POWER_SUPPLY_HEALTH_DEAD;
2961 else if (di->flags.main_thermal_prot)
2962 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
2963 else
2964 val->intval = POWER_SUPPLY_HEALTH_GOOD;
2965 break;
2966 case POWER_SUPPLY_PROP_ONLINE:
2967 val->intval = di->ac.charger_online;
2968 break;
2969 case POWER_SUPPLY_PROP_PRESENT:
2970 val->intval = di->ac.charger_connected;
2971 break;
2972 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Jonas Aaberga864c5a2013-01-11 13:12:53 +00002973 ret = ab8500_charger_get_ac_voltage(di);
2974 if (ret >= 0)
2975 di->ac.charger_voltage = ret;
2976 /* On error, use previous value */
Arun Murthy84edbee2012-02-29 21:54:26 +05302977 val->intval = di->ac.charger_voltage * 1000;
2978 break;
2979 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
2980 /*
2981 * This property is used to indicate when CV mode is entered
2982 * for the AC charger
2983 */
2984 di->ac.cv_active = ab8500_charger_ac_cv(di);
2985 val->intval = di->ac.cv_active;
2986 break;
2987 case POWER_SUPPLY_PROP_CURRENT_NOW:
Jonas Aaberga864c5a2013-01-11 13:12:53 +00002988 ret = ab8500_charger_get_ac_current(di);
2989 if (ret >= 0)
2990 di->ac.charger_current = ret;
2991 val->intval = di->ac.charger_current * 1000;
Arun Murthy84edbee2012-02-29 21:54:26 +05302992 break;
2993 default:
2994 return -EINVAL;
2995 }
2996 return 0;
2997}
2998
2999/**
3000 * ab8500_charger_usb_get_property() - get the usb properties
3001 * @psy: pointer to the power_supply structure
3002 * @psp: pointer to the power_supply_property structure
3003 * @val: pointer to the power_supply_propval union
3004 *
3005 * This function gets called when an application tries to get the usb
3006 * properties by reading the sysfs files.
3007 * USB properties are online, present and voltage.
3008 * online: usb charging is in progress or not
3009 * present: presence of the usb
3010 * voltage: vbus voltage
3011 * Returns error code in case of failure else 0(on success)
3012 */
3013static int ab8500_charger_usb_get_property(struct power_supply *psy,
3014 enum power_supply_property psp,
3015 union power_supply_propval *val)
3016{
3017 struct ab8500_charger *di;
Jonas Aaberga864c5a2013-01-11 13:12:53 +00003018 int ret;
Arun Murthy84edbee2012-02-29 21:54:26 +05303019
3020 di = to_ab8500_charger_usb_device_info(psy_to_ux500_charger(psy));
3021
3022 switch (psp) {
3023 case POWER_SUPPLY_PROP_HEALTH:
3024 if (di->flags.usbchargernotok)
3025 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
3026 else if (di->ac.wd_expired || di->usb.wd_expired)
3027 val->intval = POWER_SUPPLY_HEALTH_DEAD;
3028 else if (di->flags.usb_thermal_prot)
3029 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
3030 else if (di->flags.vbus_ovv)
3031 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
3032 else
3033 val->intval = POWER_SUPPLY_HEALTH_GOOD;
3034 break;
3035 case POWER_SUPPLY_PROP_ONLINE:
3036 val->intval = di->usb.charger_online;
3037 break;
3038 case POWER_SUPPLY_PROP_PRESENT:
3039 val->intval = di->usb.charger_connected;
3040 break;
3041 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Jonas Aaberga864c5a2013-01-11 13:12:53 +00003042 ret = ab8500_charger_get_vbus_voltage(di);
3043 if (ret >= 0)
3044 di->usb.charger_voltage = ret;
Arun Murthy84edbee2012-02-29 21:54:26 +05303045 val->intval = di->usb.charger_voltage * 1000;
3046 break;
3047 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
3048 /*
3049 * This property is used to indicate when CV mode is entered
3050 * for the USB charger
3051 */
3052 di->usb.cv_active = ab8500_charger_usb_cv(di);
3053 val->intval = di->usb.cv_active;
3054 break;
3055 case POWER_SUPPLY_PROP_CURRENT_NOW:
Jonas Aaberga864c5a2013-01-11 13:12:53 +00003056 ret = ab8500_charger_get_usb_current(di);
3057 if (ret >= 0)
3058 di->usb.charger_current = ret;
3059 val->intval = di->usb.charger_current * 1000;
Arun Murthy84edbee2012-02-29 21:54:26 +05303060 break;
3061 case POWER_SUPPLY_PROP_CURRENT_AVG:
3062 /*
3063 * This property is used to indicate when VBUS has collapsed
3064 * due to too high output current from the USB charger
3065 */
3066 if (di->flags.vbus_collapse)
3067 val->intval = 1;
3068 else
3069 val->intval = 0;
3070 break;
3071 default:
3072 return -EINVAL;
3073 }
3074 return 0;
3075}
3076
3077/**
3078 * ab8500_charger_init_hw_registers() - Set up charger related registers
3079 * @di: pointer to the ab8500_charger structure
3080 *
3081 * Set up charger OVV, watchdog and maximum voltage registers as well as
3082 * charging of the backup battery
3083 */
3084static int ab8500_charger_init_hw_registers(struct ab8500_charger *di)
3085{
3086 int ret = 0;
Yang QU0f4aa402012-06-26 19:25:52 +08003087 u8 bup_vch_range = 0, vbup33_vrtcn = 0;
Arun Murthy84edbee2012-02-29 21:54:26 +05303088
3089 /* Setup maximum charger current and voltage for ABB cut2.0 */
3090 if (!is_ab8500_1p1_or_earlier(di->parent)) {
3091 ret = abx500_set_register_interruptible(di->dev,
3092 AB8500_CHARGER,
3093 AB8500_CH_VOLT_LVL_MAX_REG, CH_VOL_LVL_4P6);
3094 if (ret) {
3095 dev_err(di->dev,
3096 "failed to set CH_VOLT_LVL_MAX_REG\n");
3097 goto out;
3098 }
3099
Lee Jones861a30d2012-08-29 20:36:51 +08003100 if (is_ab8540(di->parent))
3101 ret = abx500_set_register_interruptible(di->dev,
3102 AB8500_CHARGER, AB8500_CH_OPT_CRNTLVL_MAX_REG,
3103 CH_OP_CUR_LVL_2P);
3104 else
3105 ret = abx500_set_register_interruptible(di->dev,
3106 AB8500_CHARGER, AB8500_CH_OPT_CRNTLVL_MAX_REG,
3107 CH_OP_CUR_LVL_1P6);
Arun Murthy84edbee2012-02-29 21:54:26 +05303108 if (ret) {
3109 dev_err(di->dev,
3110 "failed to set CH_OPT_CRNTLVL_MAX_REG\n");
3111 goto out;
3112 }
3113 }
3114
Lee Jones861a30d2012-08-29 20:36:51 +08003115 if (is_ab9540_2p0(di->parent) || is_ab9540_3p0(di->parent)
3116 || is_ab8505_2p0(di->parent) || is_ab8540(di->parent))
Jonas Aaberg0ed51072012-05-11 12:42:25 +02003117 ret = abx500_mask_and_set_register_interruptible(di->dev,
3118 AB8500_CHARGER,
3119 AB8500_USBCH_CTRL2_REG,
3120 VBUS_AUTO_IN_CURR_LIM_ENA,
3121 VBUS_AUTO_IN_CURR_LIM_ENA);
3122 else
3123 /*
3124 * VBUS OVV set to 6.3V and enable automatic current limitation
3125 */
3126 ret = abx500_set_register_interruptible(di->dev,
3127 AB8500_CHARGER,
3128 AB8500_USBCH_CTRL2_REG,
3129 VBUS_OVV_SELECT_6P3V | VBUS_AUTO_IN_CURR_LIM_ENA);
Arun Murthy84edbee2012-02-29 21:54:26 +05303130 if (ret) {
Jonas Aaberg0ed51072012-05-11 12:42:25 +02003131 dev_err(di->dev,
3132 "failed to set automatic current limitation\n");
Arun Murthy84edbee2012-02-29 21:54:26 +05303133 goto out;
3134 }
3135
3136 /* Enable main watchdog in OTP */
3137 ret = abx500_set_register_interruptible(di->dev,
3138 AB8500_OTP_EMUL, AB8500_OTP_CONF_15, OTP_ENABLE_WD);
3139 if (ret) {
3140 dev_err(di->dev, "failed to enable main WD in OTP\n");
3141 goto out;
3142 }
3143
3144 /* Enable main watchdog */
3145 ret = abx500_set_register_interruptible(di->dev,
3146 AB8500_SYS_CTRL2_BLOCK,
3147 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_ENA);
3148 if (ret) {
3149 dev_err(di->dev, "faile to enable main watchdog\n");
3150 goto out;
3151 }
3152
3153 /*
3154 * Due to internal synchronisation, Enable and Kick watchdog bits
3155 * cannot be enabled in a single write.
3156 * A minimum delay of 2*32 kHz period (62.5µs) must be inserted
3157 * between writing Enable then Kick bits.
3158 */
3159 udelay(63);
3160
3161 /* Kick main watchdog */
3162 ret = abx500_set_register_interruptible(di->dev,
3163 AB8500_SYS_CTRL2_BLOCK,
3164 AB8500_MAIN_WDOG_CTRL_REG,
3165 (MAIN_WDOG_ENA | MAIN_WDOG_KICK));
3166 if (ret) {
3167 dev_err(di->dev, "failed to kick main watchdog\n");
3168 goto out;
3169 }
3170
3171 /* Disable main watchdog */
3172 ret = abx500_set_register_interruptible(di->dev,
3173 AB8500_SYS_CTRL2_BLOCK,
3174 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_DIS);
3175 if (ret) {
3176 dev_err(di->dev, "failed to disable main watchdog\n");
3177 goto out;
3178 }
3179
3180 /* Set watchdog timeout */
3181 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
3182 AB8500_CH_WD_TIMER_REG, WD_TIMER);
3183 if (ret) {
3184 dev_err(di->dev, "failed to set charger watchdog timeout\n");
3185 goto out;
3186 }
3187
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003188 ret = ab8500_charger_led_en(di, false);
3189 if (ret < 0) {
3190 dev_err(di->dev, "failed to disable LED\n");
3191 goto out;
3192 }
3193
Arun Murthy84edbee2012-02-29 21:54:26 +05303194 /* Backup battery voltage and current */
Yang QU0f4aa402012-06-26 19:25:52 +08003195 if (di->bm->bkup_bat_v > BUP_VCH_SEL_3P1V)
3196 bup_vch_range = BUP_VCH_RANGE;
3197 if (di->bm->bkup_bat_v == BUP_VCH_SEL_3P3V)
3198 vbup33_vrtcn = VBUP33_VRTCN;
3199
Arun Murthy84edbee2012-02-29 21:54:26 +05303200 ret = abx500_set_register_interruptible(di->dev,
3201 AB8500_RTC,
3202 AB8500_RTC_BACKUP_CHG_REG,
Yang QU0f4aa402012-06-26 19:25:52 +08003203 (di->bm->bkup_bat_v & 0x3) | di->bm->bkup_bat_i);
Arun Murthy84edbee2012-02-29 21:54:26 +05303204 if (ret) {
3205 dev_err(di->dev, "failed to setup backup battery charging\n");
3206 goto out;
3207 }
Yang QU0f4aa402012-06-26 19:25:52 +08003208 if (is_ab8540(di->parent)) {
3209 ret = abx500_set_register_interruptible(di->dev,
3210 AB8500_RTC,
3211 AB8500_RTC_CTRL1_REG,
3212 bup_vch_range | vbup33_vrtcn);
3213 if (ret) {
Lee Jones861a30d2012-08-29 20:36:51 +08003214 dev_err(di->dev,
3215 "failed to setup backup battery charging\n");
Yang QU0f4aa402012-06-26 19:25:52 +08003216 goto out;
3217 }
3218 }
Arun Murthy84edbee2012-02-29 21:54:26 +05303219
3220 /* Enable backup battery charging */
3221 abx500_mask_and_set_register_interruptible(di->dev,
3222 AB8500_RTC, AB8500_RTC_CTRL_REG,
3223 RTC_BUP_CH_ENA, RTC_BUP_CH_ENA);
3224 if (ret < 0)
3225 dev_err(di->dev, "%s mask and set failed\n", __func__);
3226
Lee Jonesdb43e6c2013-02-14 12:39:15 +00003227 if (is_ab8540(di->parent)) {
3228 ret = abx500_mask_and_set_register_interruptible(di->dev,
3229 AB8500_CHARGER, AB8540_USB_PP_MODE_REG,
3230 BUS_VSYS_VOL_SELECT_MASK, BUS_VSYS_VOL_SELECT_3P6V);
3231 if (ret) {
Lee Jones861a30d2012-08-29 20:36:51 +08003232 dev_err(di->dev,
3233 "failed to setup usb power path vsys voltage\n");
Lee Jonesdb43e6c2013-02-14 12:39:15 +00003234 goto out;
3235 }
3236 ret = abx500_mask_and_set_register_interruptible(di->dev,
3237 AB8500_CHARGER, AB8540_USB_PP_CHR_REG,
3238 BUS_PP_PRECHG_CURRENT_MASK, 0);
3239 if (ret) {
Lee Jones861a30d2012-08-29 20:36:51 +08003240 dev_err(di->dev,
3241 "failed to setup usb power path prechage current\n");
Lee Jonesdb43e6c2013-02-14 12:39:15 +00003242 goto out;
3243 }
3244 }
3245
Arun Murthy84edbee2012-02-29 21:54:26 +05303246out:
3247 return ret;
3248}
3249
3250/*
3251 * ab8500 charger driver interrupts and their respective isr
3252 */
3253static struct ab8500_charger_interrupts ab8500_charger_irq[] = {
3254 {"MAIN_CH_UNPLUG_DET", ab8500_charger_mainchunplugdet_handler},
3255 {"MAIN_CHARGE_PLUG_DET", ab8500_charger_mainchplugdet_handler},
3256 {"MAIN_EXT_CH_NOT_OK", ab8500_charger_mainextchnotok_handler},
3257 {"MAIN_CH_TH_PROT_R", ab8500_charger_mainchthprotr_handler},
3258 {"MAIN_CH_TH_PROT_F", ab8500_charger_mainchthprotf_handler},
3259 {"VBUS_DET_F", ab8500_charger_vbusdetf_handler},
3260 {"VBUS_DET_R", ab8500_charger_vbusdetr_handler},
3261 {"USB_LINK_STATUS", ab8500_charger_usblinkstatus_handler},
3262 {"USB_CH_TH_PROT_R", ab8500_charger_usbchthprotr_handler},
3263 {"USB_CH_TH_PROT_F", ab8500_charger_usbchthprotf_handler},
3264 {"USB_CHARGER_NOT_OKR", ab8500_charger_usbchargernotokr_handler},
3265 {"VBUS_OVV", ab8500_charger_vbusovv_handler},
3266 {"CH_WD_EXP", ab8500_charger_chwdexp_handler},
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003267 {"VBUS_CH_DROP_END", ab8500_charger_vbuschdropend_handler},
Arun Murthy84edbee2012-02-29 21:54:26 +05303268};
3269
3270static int ab8500_charger_usb_notifier_call(struct notifier_block *nb,
3271 unsigned long event, void *power)
3272{
3273 struct ab8500_charger *di =
3274 container_of(nb, struct ab8500_charger, nb);
3275 enum ab8500_usb_state bm_usb_state;
3276 unsigned mA = *((unsigned *)power);
3277
Lee Jonesc9ade0f2013-01-21 11:22:56 +00003278 if (!di)
3279 return NOTIFY_DONE;
3280
Arun Murthy84edbee2012-02-29 21:54:26 +05303281 if (event != USB_EVENT_VBUS) {
3282 dev_dbg(di->dev, "not a standard host, returning\n");
3283 return NOTIFY_DONE;
3284 }
3285
3286 /* TODO: State is fabricate here. See if charger really needs USB
3287 * state or if mA is enough
3288 */
3289 if ((di->usb_state.usb_current == 2) && (mA > 2))
3290 bm_usb_state = AB8500_BM_USB_STATE_RESUME;
3291 else if (mA == 0)
3292 bm_usb_state = AB8500_BM_USB_STATE_RESET_HS;
3293 else if (mA == 2)
3294 bm_usb_state = AB8500_BM_USB_STATE_SUSPEND;
3295 else if (mA >= 8) /* 8, 100, 500 */
3296 bm_usb_state = AB8500_BM_USB_STATE_CONFIGURED;
3297 else /* Should never occur */
3298 bm_usb_state = AB8500_BM_USB_STATE_RESET_FS;
3299
3300 dev_dbg(di->dev, "%s usb_state: 0x%02x mA: %d\n",
3301 __func__, bm_usb_state, mA);
3302
3303 spin_lock(&di->usb_state.usb_lock);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003304 di->usb_state.state_tmp = bm_usb_state;
3305 di->usb_state.usb_current_tmp = mA;
Arun Murthy84edbee2012-02-29 21:54:26 +05303306 spin_unlock(&di->usb_state.usb_lock);
3307
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003308 /*
3309 * wait for some time until you get updates from the usb stack
3310 * and negotiations are completed
3311 */
3312 queue_delayed_work(di->charger_wq, &di->usb_state_changed_work, HZ/2);
Arun Murthy84edbee2012-02-29 21:54:26 +05303313
3314 return NOTIFY_OK;
3315}
3316
3317#if defined(CONFIG_PM)
3318static int ab8500_charger_resume(struct platform_device *pdev)
3319{
3320 int ret;
3321 struct ab8500_charger *di = platform_get_drvdata(pdev);
3322
3323 /*
3324 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
3325 * logic. That means we have to continously kick the charger
3326 * watchdog even when no charger is connected. This is only
3327 * valid once the AC charger has been enabled. This is
3328 * a bug that is not handled by the algorithm and the
3329 * watchdog have to be kicked by the charger driver
3330 * when the AC charger is disabled
3331 */
3332 if (di->ac_conn && is_ab8500_1p1_or_earlier(di->parent)) {
3333 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
3334 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
3335 if (ret)
3336 dev_err(di->dev, "Failed to kick WD!\n");
3337
3338 /* If not already pending start a new timer */
Lee Jonesb09f86d2012-12-21 17:56:52 -08003339 queue_delayed_work(di->charger_wq, &di->kick_wd_work,
3340 round_jiffies(WD_KICK_INTERVAL));
Arun Murthy84edbee2012-02-29 21:54:26 +05303341 }
3342
3343 /* If we still have a HW failure, schedule a new check */
3344 if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
3345 queue_delayed_work(di->charger_wq,
3346 &di->check_hw_failure_work, 0);
3347 }
3348
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003349 if (di->flags.vbus_drop_end)
3350 queue_delayed_work(di->charger_wq, &di->vbus_drop_end_work, 0);
3351
Arun Murthy84edbee2012-02-29 21:54:26 +05303352 return 0;
3353}
3354
3355static int ab8500_charger_suspend(struct platform_device *pdev,
3356 pm_message_t state)
3357{
3358 struct ab8500_charger *di = platform_get_drvdata(pdev);
3359
Lee Jonesb09f86d2012-12-21 17:56:52 -08003360 /* Cancel any pending jobs */
3361 cancel_delayed_work(&di->check_hw_failure_work);
3362 cancel_delayed_work(&di->vbus_drop_end_work);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003363
Jonas Aaberg53ef1f52012-05-21 16:05:01 +02003364 flush_delayed_work(&di->attach_work);
3365 flush_delayed_work(&di->usb_charger_attached_work);
3366 flush_delayed_work(&di->ac_charger_attached_work);
3367 flush_delayed_work(&di->check_usbchgnotok_work);
3368 flush_delayed_work(&di->check_vbat_work);
3369 flush_delayed_work(&di->kick_wd_work);
3370
3371 flush_work(&di->usb_link_status_work);
3372 flush_work(&di->ac_work);
3373 flush_work(&di->detect_usb_type_work);
3374
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003375 if (atomic_read(&di->current_stepping_sessions))
3376 return -EAGAIN;
3377
Arun Murthy84edbee2012-02-29 21:54:26 +05303378 return 0;
3379}
3380#else
3381#define ab8500_charger_suspend NULL
3382#define ab8500_charger_resume NULL
3383#endif
3384
Lee Jones88917162013-02-13 11:39:19 +00003385static struct notifier_block charger_nb = {
3386 .notifier_call = ab8500_external_charger_prepare,
3387};
3388
Bill Pemberton415ec692012-11-19 13:26:07 -05003389static int ab8500_charger_remove(struct platform_device *pdev)
Arun Murthy84edbee2012-02-29 21:54:26 +05303390{
3391 struct ab8500_charger *di = platform_get_drvdata(pdev);
3392 int i, irq, ret;
3393
3394 /* Disable AC charging */
3395 ab8500_charger_ac_en(&di->ac_chg, false, 0, 0);
3396
3397 /* Disable USB charging */
3398 ab8500_charger_usb_en(&di->usb_chg, false, 0, 0);
3399
3400 /* Disable interrupts */
3401 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
3402 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3403 free_irq(irq, di);
3404 }
3405
Arun Murthy84edbee2012-02-29 21:54:26 +05303406 /* Backup battery voltage and current disable */
3407 ret = abx500_mask_and_set_register_interruptible(di->dev,
3408 AB8500_RTC, AB8500_RTC_CTRL_REG, RTC_BUP_CH_ENA, 0);
3409 if (ret < 0)
3410 dev_err(di->dev, "%s mask and set failed\n", __func__);
3411
Anton Vorontsovefd71c82012-03-14 04:22:17 +04003412 usb_unregister_notifier(di->usb_phy, &di->nb);
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +05303413 usb_put_phy(di->usb_phy);
Arun Murthy84edbee2012-02-29 21:54:26 +05303414
3415 /* Delete the work queue */
3416 destroy_workqueue(di->charger_wq);
3417
Lee Jones88917162013-02-13 11:39:19 +00003418 /* Unregister external charger enable notifier */
3419 if (!di->ac_chg.enabled)
3420 blocking_notifier_chain_unregister(
3421 &charger_notifier_list, &charger_nb);
3422
Arun Murthy84edbee2012-02-29 21:54:26 +05303423 flush_scheduled_work();
Lee Jones72a90dd2013-02-14 09:22:48 +00003424 if (di->usb_chg.enabled)
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01003425 power_supply_unregister(di->usb_chg.psy);
Marcus Cooper405fea12012-08-10 12:59:06 +02003426
3427 if (di->ac_chg.enabled && !di->ac_chg.external)
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01003428 power_supply_unregister(di->ac_chg.psy);
Marcus Cooper405fea12012-08-10 12:59:06 +02003429
Arun Murthy84edbee2012-02-29 21:54:26 +05303430 return 0;
3431}
3432
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08003433static char *supply_interface[] = {
3434 "ab8500_chargalg",
3435 "ab8500_fg",
3436 "ab8500_btemp",
3437};
3438
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01003439static const struct power_supply_desc ab8500_ac_chg_desc = {
3440 .name = "ab8500_ac",
3441 .type = POWER_SUPPLY_TYPE_MAINS,
3442 .properties = ab8500_charger_ac_props,
3443 .num_properties = ARRAY_SIZE(ab8500_charger_ac_props),
3444 .get_property = ab8500_charger_ac_get_property,
3445};
3446
3447static const struct power_supply_desc ab8500_usb_chg_desc = {
3448 .name = "ab8500_usb",
3449 .type = POWER_SUPPLY_TYPE_USB,
3450 .properties = ab8500_charger_usb_props,
3451 .num_properties = ARRAY_SIZE(ab8500_charger_usb_props),
3452 .get_property = ab8500_charger_usb_get_property,
3453};
3454
Bill Pembertonc8afa642012-11-19 13:22:23 -05003455static int ab8500_charger_probe(struct platform_device *pdev)
Arun Murthy84edbee2012-02-29 21:54:26 +05303456{
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08003457 struct device_node *np = pdev->dev.of_node;
Lee Jones7722b792012-11-30 10:56:28 +00003458 struct abx500_bm_data *plat = pdev->dev.platform_data;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01003459 struct power_supply_config ac_psy_cfg = {}, usb_psy_cfg = {};
Lee Jones2aac3de2012-05-05 04:38:19 -07003460 struct ab8500_charger *di;
Lee Jonesb269fff2013-01-11 13:12:51 +00003461 int irq, i, charger_status, ret = 0, ch_stat;
Arun Murthy84edbee2012-02-29 21:54:26 +05303462
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08003463 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
3464 if (!di) {
3465 dev_err(&pdev->dev, "%s no mem for ab8500_charger\n", __func__);
Arun Murthy84edbee2012-02-29 21:54:26 +05303466 return -ENOMEM;
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08003467 }
Lee Jones7722b792012-11-30 10:56:28 +00003468
3469 if (!plat) {
3470 dev_err(&pdev->dev, "no battery management data supplied\n");
3471 return -EINVAL;
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08003472 }
Lee Jones7722b792012-11-30 10:56:28 +00003473 di->bm = plat;
3474
3475 if (np) {
3476 ret = ab8500_bm_of_probe(&pdev->dev, np, di->bm);
3477 if (ret) {
3478 dev_err(&pdev->dev, "failed to get battery information\n");
3479 return ret;
3480 }
3481 di->autopower_cfg = of_property_read_bool(np, "autopower_cfg");
3482 } else
3483 di->autopower_cfg = false;
Arun Murthy84edbee2012-02-29 21:54:26 +05303484
3485 /* get parent data */
3486 di->dev = &pdev->dev;
3487 di->parent = dev_get_drvdata(pdev->dev.parent);
3488 di->gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
3489
3490 /* initialize lock */
3491 spin_lock_init(&di->usb_state.usb_lock);
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003492 mutex_init(&di->usb_ipt_crnt_lock);
Arun Murthy84edbee2012-02-29 21:54:26 +05303493
Arun Murthy84edbee2012-02-29 21:54:26 +05303494 di->autopower = false;
Henrik Sölverff380902012-04-17 15:51:01 +02003495 di->invalid_charger_detect_state = 0;
Arun Murthy84edbee2012-02-29 21:54:26 +05303496
Krzysztof Kozlowski2dc92152015-03-12 08:44:02 +01003497 /* AC and USB supply config */
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01003498 ac_psy_cfg.supplied_to = supply_interface;
3499 ac_psy_cfg.num_supplicants = ARRAY_SIZE(supply_interface);
3500 ac_psy_cfg.drv_data = &di->ac_chg;
3501 usb_psy_cfg.supplied_to = supply_interface;
3502 usb_psy_cfg.num_supplicants = ARRAY_SIZE(supply_interface);
3503 usb_psy_cfg.drv_data = &di->usb_chg;
Krzysztof Kozlowski2dc92152015-03-12 08:44:02 +01003504
Arun Murthy84edbee2012-02-29 21:54:26 +05303505 /* AC supply */
Arun Murthy84edbee2012-02-29 21:54:26 +05303506 /* ux500_charger sub-class */
3507 di->ac_chg.ops.enable = &ab8500_charger_ac_en;
Lee Jones4dcdf572013-02-14 09:24:10 +00003508 di->ac_chg.ops.check_enable = &ab8500_charger_ac_check_enable;
Arun Murthy84edbee2012-02-29 21:54:26 +05303509 di->ac_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
3510 di->ac_chg.ops.update_curr = &ab8500_charger_update_charger_current;
3511 di->ac_chg.max_out_volt = ab8500_charger_voltage_map[
3512 ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
Lee Jones861a30d2012-08-29 20:36:51 +08003513 di->ac_chg.max_out_curr =
3514 di->bm->chg_output_curr[di->bm->n_chg_out_curr - 1];
Loic Pallardye07a5642012-05-10 15:33:56 +02003515 di->ac_chg.wdt_refresh = CHG_WD_INTERVAL;
Lee Jones97034a12013-01-17 16:08:42 +00003516 di->ac_chg.enabled = di->bm->ac_enabled;
Loic Pallardye07a5642012-05-10 15:33:56 +02003517 di->ac_chg.external = false;
Arun Murthy84edbee2012-02-29 21:54:26 +05303518
Lee Jones88917162013-02-13 11:39:19 +00003519 /*notifier for external charger enabling*/
3520 if (!di->ac_chg.enabled)
3521 blocking_notifier_chain_register(
3522 &charger_notifier_list, &charger_nb);
3523
Arun Murthy84edbee2012-02-29 21:54:26 +05303524 /* USB supply */
Arun Murthy84edbee2012-02-29 21:54:26 +05303525 /* ux500_charger sub-class */
3526 di->usb_chg.ops.enable = &ab8500_charger_usb_en;
Lee Jones4dcdf572013-02-14 09:24:10 +00003527 di->usb_chg.ops.check_enable = &ab8500_charger_usb_check_enable;
Arun Murthy84edbee2012-02-29 21:54:26 +05303528 di->usb_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
3529 di->usb_chg.ops.update_curr = &ab8500_charger_update_charger_current;
Lee Jonesdb43e6c2013-02-14 12:39:15 +00003530 di->usb_chg.ops.pp_enable = &ab8540_charger_power_path_enable;
3531 di->usb_chg.ops.pre_chg_enable = &ab8540_charger_usb_pre_chg_enable;
Arun Murthy84edbee2012-02-29 21:54:26 +05303532 di->usb_chg.max_out_volt = ab8500_charger_voltage_map[
3533 ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
Lee Jones861a30d2012-08-29 20:36:51 +08003534 di->usb_chg.max_out_curr =
3535 di->bm->chg_output_curr[di->bm->n_chg_out_curr - 1];
Loic Pallardye07a5642012-05-10 15:33:56 +02003536 di->usb_chg.wdt_refresh = CHG_WD_INTERVAL;
Lee Jones97034a12013-01-17 16:08:42 +00003537 di->usb_chg.enabled = di->bm->usb_enabled;
Loic Pallardye07a5642012-05-10 15:33:56 +02003538 di->usb_chg.external = false;
Lee Jonesdb43e6c2013-02-14 12:39:15 +00003539 di->usb_chg.power_path = di->bm->usb_power_path;
Lee Jonesf7470b52013-02-14 12:37:29 +00003540 di->usb_state.usb_current = -1;
Arun Murthy84edbee2012-02-29 21:54:26 +05303541
3542 /* Create a work queue for the charger */
3543 di->charger_wq =
3544 create_singlethread_workqueue("ab8500_charger_wq");
3545 if (di->charger_wq == NULL) {
3546 dev_err(di->dev, "failed to create work queue\n");
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08003547 return -ENOMEM;
Arun Murthy84edbee2012-02-29 21:54:26 +05303548 }
3549
Lee Jonesb269fff2013-01-11 13:12:51 +00003550 mutex_init(&di->charger_attached_mutex);
3551
Arun Murthy84edbee2012-02-29 21:54:26 +05303552 /* Init work for HW failure check */
Tejun Heo203b42f2012-08-21 13:18:23 -07003553 INIT_DEFERRABLE_WORK(&di->check_hw_failure_work,
Arun Murthy84edbee2012-02-29 21:54:26 +05303554 ab8500_charger_check_hw_failure_work);
Tejun Heo203b42f2012-08-21 13:18:23 -07003555 INIT_DEFERRABLE_WORK(&di->check_usbchgnotok_work,
Arun Murthy84edbee2012-02-29 21:54:26 +05303556 ab8500_charger_check_usbchargernotok_work);
3557
Lee Jonesb269fff2013-01-11 13:12:51 +00003558 INIT_DELAYED_WORK(&di->ac_charger_attached_work,
3559 ab8500_charger_ac_attached_work);
3560 INIT_DELAYED_WORK(&di->usb_charger_attached_work,
3561 ab8500_charger_usb_attached_work);
3562
Arun Murthy84edbee2012-02-29 21:54:26 +05303563 /*
3564 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
3565 * logic. That means we have to continously kick the charger
3566 * watchdog even when no charger is connected. This is only
3567 * valid once the AC charger has been enabled. This is
3568 * a bug that is not handled by the algorithm and the
3569 * watchdog have to be kicked by the charger driver
3570 * when the AC charger is disabled
3571 */
Tejun Heo203b42f2012-08-21 13:18:23 -07003572 INIT_DEFERRABLE_WORK(&di->kick_wd_work,
Arun Murthy84edbee2012-02-29 21:54:26 +05303573 ab8500_charger_kick_watchdog_work);
3574
Tejun Heo203b42f2012-08-21 13:18:23 -07003575 INIT_DEFERRABLE_WORK(&di->check_vbat_work,
Arun Murthy84edbee2012-02-29 21:54:26 +05303576 ab8500_charger_check_vbat_work);
3577
Marcus Cooper4b45f4a2013-01-11 13:13:04 +00003578 INIT_DELAYED_WORK(&di->attach_work,
3579 ab8500_charger_usb_link_attach_work);
3580
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003581 INIT_DELAYED_WORK(&di->usb_state_changed_work,
3582 ab8500_charger_usb_state_changed_work);
3583
3584 INIT_DELAYED_WORK(&di->vbus_drop_end_work,
3585 ab8500_charger_vbus_drop_end_work);
3586
Arun Murthy84edbee2012-02-29 21:54:26 +05303587 /* Init work for charger detection */
3588 INIT_WORK(&di->usb_link_status_work,
3589 ab8500_charger_usb_link_status_work);
3590 INIT_WORK(&di->ac_work, ab8500_charger_ac_work);
3591 INIT_WORK(&di->detect_usb_type_work,
3592 ab8500_charger_detect_usb_type_work);
3593
Arun Murthy84edbee2012-02-29 21:54:26 +05303594 /* Init work for checking HW status */
3595 INIT_WORK(&di->check_main_thermal_prot_work,
3596 ab8500_charger_check_main_thermal_prot_work);
3597 INIT_WORK(&di->check_usb_thermal_prot_work,
3598 ab8500_charger_check_usb_thermal_prot_work);
3599
3600 /*
3601 * VDD ADC supply needs to be enabled from this driver when there
3602 * is a charger connected to avoid erroneous BTEMP_HIGH/LOW
3603 * interrupts during charging
3604 */
Sachin Kamat8feffd12012-12-07 17:23:28 +05303605 di->regu = devm_regulator_get(di->dev, "vddadc");
Arun Murthy84edbee2012-02-29 21:54:26 +05303606 if (IS_ERR(di->regu)) {
3607 ret = PTR_ERR(di->regu);
3608 dev_err(di->dev, "failed to get vddadc regulator\n");
3609 goto free_charger_wq;
3610 }
3611
3612
3613 /* Initialize OVV, and other registers */
3614 ret = ab8500_charger_init_hw_registers(di);
3615 if (ret) {
3616 dev_err(di->dev, "failed to initialize ABB registers\n");
Sachin Kamat8feffd12012-12-07 17:23:28 +05303617 goto free_charger_wq;
Arun Murthy84edbee2012-02-29 21:54:26 +05303618 }
3619
3620 /* Register AC charger class */
Lee Jones72a90dd2013-02-14 09:22:48 +00003621 if (di->ac_chg.enabled) {
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01003622 di->ac_chg.psy = power_supply_register(di->dev,
3623 &ab8500_ac_chg_desc,
3624 &ac_psy_cfg);
3625 if (IS_ERR(di->ac_chg.psy)) {
Michel JAOUEN01ec8c52012-04-26 10:00:04 +02003626 dev_err(di->dev, "failed to register AC charger\n");
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01003627 ret = PTR_ERR(di->ac_chg.psy);
Michel JAOUEN01ec8c52012-04-26 10:00:04 +02003628 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) {
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01003634 di->usb_chg.psy = power_supply_register(di->dev,
3635 &ab8500_usb_chg_desc,
3636 &usb_psy_cfg);
3637 if (IS_ERR(di->usb_chg.psy)) {
Michel JAOUEN01ec8c52012-04-26 10:00:04 +02003638 dev_err(di->dev, "failed to register USB charger\n");
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01003639 ret = PTR_ERR(di->usb_chg.psy);
Michel JAOUEN01ec8c52012-04-26 10:00:04 +02003640 goto free_ac;
3641 }
Arun Murthy84edbee2012-02-29 21:54:26 +05303642 }
3643
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +05303644 di->usb_phy = usb_get_phy(USB_PHY_TYPE_USB2);
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +05303645 if (IS_ERR_OR_NULL(di->usb_phy)) {
Anton Vorontsovefd71c82012-03-14 04:22:17 +04003646 dev_err(di->dev, "failed to get usb transceiver\n");
Arun Murthy84edbee2012-02-29 21:54:26 +05303647 ret = -EINVAL;
3648 goto free_usb;
3649 }
3650 di->nb.notifier_call = ab8500_charger_usb_notifier_call;
Anton Vorontsovefd71c82012-03-14 04:22:17 +04003651 ret = usb_register_notifier(di->usb_phy, &di->nb);
Arun Murthy84edbee2012-02-29 21:54:26 +05303652 if (ret) {
Anton Vorontsovefd71c82012-03-14 04:22:17 +04003653 dev_err(di->dev, "failed to register usb notifier\n");
3654 goto put_usb_phy;
Arun Murthy84edbee2012-02-29 21:54:26 +05303655 }
3656
3657 /* Identify the connected charger types during startup */
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003658 charger_status = ab8500_charger_detect_chargers(di, true);
Arun Murthy84edbee2012-02-29 21:54:26 +05303659 if (charger_status & AC_PW_CONN) {
3660 di->ac.charger_connected = 1;
3661 di->ac_conn = true;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01003662 ab8500_power_supply_changed(di, di->ac_chg.psy);
3663 sysfs_notify(&di->ac_chg.psy->dev.kobj, NULL, "present");
Arun Murthy84edbee2012-02-29 21:54:26 +05303664 }
3665
3666 if (charger_status & USB_PW_CONN) {
Arun Murthy84edbee2012-02-29 21:54:26 +05303667 di->vbus_detected = true;
3668 di->vbus_detected_start = true;
3669 queue_work(di->charger_wq,
3670 &di->detect_usb_type_work);
3671 }
3672
3673 /* Register interrupts */
3674 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
3675 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3676 ret = request_threaded_irq(irq, NULL, ab8500_charger_irq[i].isr,
3677 IRQF_SHARED | IRQF_NO_SUSPEND,
3678 ab8500_charger_irq[i].name, di);
3679
3680 if (ret != 0) {
3681 dev_err(di->dev, "failed to request %s IRQ %d: %d\n"
3682 , ab8500_charger_irq[i].name, irq, ret);
3683 goto free_irq;
3684 }
3685 dev_dbg(di->dev, "Requested %s IRQ %d: %d\n",
3686 ab8500_charger_irq[i].name, irq, ret);
3687 }
3688
3689 platform_set_drvdata(pdev, di);
3690
Lee Jonesb269fff2013-01-11 13:12:51 +00003691 mutex_lock(&di->charger_attached_mutex);
3692
Paer-Olof Haakansson34c11a72012-02-22 19:07:51 +01003693 ch_stat = ab8500_charger_detect_chargers(di, false);
Lee Jonesb269fff2013-01-11 13:12:51 +00003694
3695 if ((ch_stat & AC_PW_CONN) == AC_PW_CONN) {
Rabin Vincent261c5132012-09-26 13:36:38 +02003696 if (is_ab8500(di->parent))
3697 queue_delayed_work(di->charger_wq,
3698 &di->ac_charger_attached_work,
3699 HZ);
Lee Jonesb269fff2013-01-11 13:12:51 +00003700 }
3701 if ((ch_stat & USB_PW_CONN) == USB_PW_CONN) {
Rabin Vincent261c5132012-09-26 13:36:38 +02003702 if (is_ab8500(di->parent))
3703 queue_delayed_work(di->charger_wq,
3704 &di->usb_charger_attached_work,
3705 HZ);
Lee Jonesb269fff2013-01-11 13:12:51 +00003706 }
3707
3708 mutex_unlock(&di->charger_attached_mutex);
3709
Arun Murthy84edbee2012-02-29 21:54:26 +05303710 return ret;
3711
3712free_irq:
Anton Vorontsovefd71c82012-03-14 04:22:17 +04003713 usb_unregister_notifier(di->usb_phy, &di->nb);
Arun Murthy84edbee2012-02-29 21:54:26 +05303714
3715 /* We also have to free all successfully registered irqs */
3716 for (i = i - 1; i >= 0; i--) {
3717 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3718 free_irq(irq, di);
3719 }
Anton Vorontsovefd71c82012-03-14 04:22:17 +04003720put_usb_phy:
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +05303721 usb_put_phy(di->usb_phy);
Arun Murthy84edbee2012-02-29 21:54:26 +05303722free_usb:
Lee Jones72a90dd2013-02-14 09:22:48 +00003723 if (di->usb_chg.enabled)
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01003724 power_supply_unregister(di->usb_chg.psy);
Arun Murthy84edbee2012-02-29 21:54:26 +05303725free_ac:
Lee Jones72a90dd2013-02-14 09:22:48 +00003726 if (di->ac_chg.enabled)
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01003727 power_supply_unregister(di->ac_chg.psy);
Arun Murthy84edbee2012-02-29 21:54:26 +05303728free_charger_wq:
3729 destroy_workqueue(di->charger_wq);
Arun Murthy84edbee2012-02-29 21:54:26 +05303730 return ret;
3731}
3732
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08003733static const struct of_device_id ab8500_charger_match[] = {
3734 { .compatible = "stericsson,ab8500-charger", },
3735 { },
3736};
3737
Arun Murthy84edbee2012-02-29 21:54:26 +05303738static struct platform_driver ab8500_charger_driver = {
3739 .probe = ab8500_charger_probe,
Bill Pemberton28ea73f2012-11-19 13:20:40 -05003740 .remove = ab8500_charger_remove,
Arun Murthy84edbee2012-02-29 21:54:26 +05303741 .suspend = ab8500_charger_suspend,
3742 .resume = ab8500_charger_resume,
3743 .driver = {
3744 .name = "ab8500-charger",
Rajanikanth H.V4aef72d2012-11-18 19:17:47 -08003745 .of_match_table = ab8500_charger_match,
Arun Murthy84edbee2012-02-29 21:54:26 +05303746 },
3747};
3748
3749static int __init ab8500_charger_init(void)
3750{
3751 return platform_driver_register(&ab8500_charger_driver);
3752}
3753
3754static void __exit ab8500_charger_exit(void)
3755{
3756 platform_driver_unregister(&ab8500_charger_driver);
3757}
3758
3759subsys_initcall_sync(ab8500_charger_init);
3760module_exit(ab8500_charger_exit);
3761
3762MODULE_LICENSE("GPL v2");
3763MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy");
3764MODULE_ALIAS("platform:ab8500-charger");
3765MODULE_DESCRIPTION("AB8500 charger management driver");