ab8500-chargalg: Only root should have write permission on sysfs file
[deliverable/linux.git] / drivers / power / ab8500_charger.c
CommitLineData
84edbeea
AM
1/*
2 * Copyright (C) ST-Ericsson SA 2012
3 *
4 * Charger driver for AB8500
5 *
6 * License Terms: GNU General Public License v2
7 * Author:
8 * Johan Palsson <johan.palsson@stericsson.com>
9 * Karl Komierowski <karl.komierowski@stericsson.com>
10 * Arun R Murthy <arun.murthy@stericsson.com>
11 */
12
13#include <linux/init.h>
14#include <linux/module.h>
15#include <linux/device.h>
16#include <linux/interrupt.h>
17#include <linux/delay.h>
18#include <linux/slab.h>
19#include <linux/platform_device.h>
20#include <linux/power_supply.h>
21#include <linux/completion.h>
22#include <linux/regulator/consumer.h>
23#include <linux/err.h>
24#include <linux/workqueue.h>
25#include <linux/kobject.h>
4aef72db
R
26#include <linux/of.h>
27#include <linux/mfd/core.h>
84edbeea
AM
28#include <linux/mfd/abx500/ab8500.h>
29#include <linux/mfd/abx500.h>
30#include <linux/mfd/abx500/ab8500-bm.h>
31#include <linux/mfd/abx500/ab8500-gpadc.h>
32#include <linux/mfd/abx500/ux500_chargalg.h>
33#include <linux/usb/otg.h>
b269fff4 34#include <linux/mutex.h>
84edbeea
AM
35
36/* Charger constants */
37#define NO_PW_CONN 0
38#define AC_PW_CONN 1
39#define USB_PW_CONN 2
40
41#define MAIN_WDOG_ENA 0x01
42#define MAIN_WDOG_KICK 0x02
43#define MAIN_WDOG_DIS 0x00
44#define CHARG_WD_KICK 0x01
45#define MAIN_CH_ENA 0x01
46#define MAIN_CH_NO_OVERSHOOT_ENA_N 0x02
47#define USB_CH_ENA 0x01
48#define USB_CHG_NO_OVERSHOOT_ENA_N 0x02
49#define MAIN_CH_DET 0x01
50#define MAIN_CH_CV_ON 0x04
51#define USB_CH_CV_ON 0x08
52#define VBUS_DET_DBNC100 0x02
53#define VBUS_DET_DBNC1 0x01
54#define OTP_ENABLE_WD 0x01
55
56#define MAIN_CH_INPUT_CURR_SHIFT 4
57#define VBUS_IN_CURR_LIM_SHIFT 4
58
59#define LED_INDICATOR_PWM_ENA 0x01
60#define LED_INDICATOR_PWM_DIS 0x00
61#define LED_IND_CUR_5MA 0x04
62#define LED_INDICATOR_PWM_DUTY_252_256 0xBF
63
64/* HW failure constants */
65#define MAIN_CH_TH_PROT 0x02
66#define VBUS_CH_NOK 0x08
67#define USB_CH_TH_PROT 0x02
68#define VBUS_OVV_TH 0x01
69#define MAIN_CH_NOK 0x01
70#define VBUS_DET 0x80
71
b269fff4
LJ
72#define MAIN_CH_STATUS2_MAINCHGDROP 0x80
73#define MAIN_CH_STATUS2_MAINCHARGERDETDBNC 0x40
74#define USB_CH_VBUSDROP 0x40
75#define USB_CH_VBUSDETDBNC 0x01
76
84edbeea
AM
77/* UsbLineStatus register bit masks */
78#define AB8500_USB_LINK_STATUS 0x78
79#define AB8500_STD_HOST_SUSP 0x18
80
81/* Watchdog timeout constant */
82#define WD_TIMER 0x30 /* 4min */
83#define WD_KICK_INTERVAL (60 * HZ)
84
85/* Lowest charger voltage is 3.39V -> 0x4E */
86#define LOW_VOLT_REG 0x4E
87
f8e96dff
JB
88/* Step up/down delay in us */
89#define STEP_UDELAY 1000
90
4b45f4a9
MC
91/* Wait for enumeration before charging in ms */
92#define WAIT_FOR_USB_ENUMERATION 5 * 1000
93
b269fff4
LJ
94#define CHARGER_STATUS_POLL 10 /* in ms */
95
84edbeea
AM
96/* UsbLineStatus register - usb types */
97enum ab8500_charger_link_status {
98 USB_STAT_NOT_CONFIGURED,
99 USB_STAT_STD_HOST_NC,
100 USB_STAT_STD_HOST_C_NS,
101 USB_STAT_STD_HOST_C_S,
102 USB_STAT_HOST_CHG_NM,
103 USB_STAT_HOST_CHG_HS,
104 USB_STAT_HOST_CHG_HS_CHIRP,
105 USB_STAT_DEDICATED_CHG,
106 USB_STAT_ACA_RID_A,
107 USB_STAT_ACA_RID_B,
108 USB_STAT_ACA_RID_C_NM,
109 USB_STAT_ACA_RID_C_HS,
110 USB_STAT_ACA_RID_C_HS_CHIRP,
111 USB_STAT_HM_IDGND,
112 USB_STAT_RESERVED,
113 USB_STAT_NOT_VALID_LINK,
74a8e349
HB
114 USB_STAT_PHY_EN,
115 USB_STAT_SUP_NO_IDGND_VBUS,
116 USB_STAT_SUP_IDGND_VBUS,
117 USB_STAT_CHARGER_LINE_1,
118 USB_STAT_CARKIT_1,
119 USB_STAT_CARKIT_2,
120 USB_STAT_ACA_DOCK_CHARGER,
84edbeea
AM
121};
122
123enum ab8500_usb_state {
124 AB8500_BM_USB_STATE_RESET_HS, /* HighSpeed Reset */
125 AB8500_BM_USB_STATE_RESET_FS, /* FullSpeed/LowSpeed Reset */
126 AB8500_BM_USB_STATE_CONFIGURED,
127 AB8500_BM_USB_STATE_SUSPEND,
128 AB8500_BM_USB_STATE_RESUME,
129 AB8500_BM_USB_STATE_MAX,
130};
131
132/* VBUS input current limits supported in AB8500 in mA */
133#define USB_CH_IP_CUR_LVL_0P05 50
134#define USB_CH_IP_CUR_LVL_0P09 98
135#define USB_CH_IP_CUR_LVL_0P19 193
136#define USB_CH_IP_CUR_LVL_0P29 290
137#define USB_CH_IP_CUR_LVL_0P38 380
138#define USB_CH_IP_CUR_LVL_0P45 450
139#define USB_CH_IP_CUR_LVL_0P5 500
140#define USB_CH_IP_CUR_LVL_0P6 600
141#define USB_CH_IP_CUR_LVL_0P7 700
142#define USB_CH_IP_CUR_LVL_0P8 800
143#define USB_CH_IP_CUR_LVL_0P9 900
144#define USB_CH_IP_CUR_LVL_1P0 1000
145#define USB_CH_IP_CUR_LVL_1P1 1100
146#define USB_CH_IP_CUR_LVL_1P3 1300
147#define USB_CH_IP_CUR_LVL_1P4 1400
148#define USB_CH_IP_CUR_LVL_1P5 1500
149
150#define VBAT_TRESH_IP_CUR_RED 3800
151
152#define to_ab8500_charger_usb_device_info(x) container_of((x), \
153 struct ab8500_charger, usb_chg)
154#define to_ab8500_charger_ac_device_info(x) container_of((x), \
155 struct ab8500_charger, ac_chg)
156
157/**
158 * struct ab8500_charger_interrupts - ab8500 interupts
159 * @name: name of the interrupt
160 * @isr function pointer to the isr
161 */
162struct ab8500_charger_interrupts {
163 char *name;
164 irqreturn_t (*isr)(int irq, void *data);
165};
166
167struct ab8500_charger_info {
168 int charger_connected;
169 int charger_online;
170 int charger_voltage;
171 int cv_active;
172 bool wd_expired;
a864c5a8 173 int charger_current;
84edbeea
AM
174};
175
176struct ab8500_charger_event_flags {
177 bool mainextchnotok;
178 bool main_thermal_prot;
179 bool usb_thermal_prot;
180 bool vbus_ovv;
181 bool usbchargernotok;
182 bool chgwdexp;
183 bool vbus_collapse;
184};
185
186struct ab8500_charger_usb_state {
187 bool usb_changed;
188 int usb_current;
189 enum ab8500_usb_state state;
190 spinlock_t usb_lock;
191};
192
193/**
194 * struct ab8500_charger - ab8500 Charger device information
195 * @dev: Pointer to the structure device
196 * @max_usb_in_curr: Max USB charger input current
197 * @vbus_detected: VBUS detected
198 * @vbus_detected_start:
199 * VBUS detected during startup
200 * @ac_conn: This will be true when the AC charger has been plugged
201 * @vddadc_en_ac: Indicate if VDD ADC supply is enabled because AC
202 * charger is enabled
203 * @vddadc_en_usb: Indicate if VDD ADC supply is enabled because USB
204 * charger is enabled
205 * @vbat Battery voltage
206 * @old_vbat Previously measured battery voltage
4b45f4a9 207 * @usb_device_is_unrecognised USB device is unrecognised by the hardware
84edbeea 208 * @autopower Indicate if we should have automatic pwron after pwrloss
4aef72db 209 * @autopower_cfg platform specific power config support for "pwron after pwrloss"
ff38090a 210 * @invalid_charger_detect_state State when forcing AB to use invalid charger
84edbeea
AM
211 * @parent: Pointer to the struct ab8500
212 * @gpadc: Pointer to the struct gpadc
b0284de0 213 * @bm: Platform specific battery management information
84edbeea
AM
214 * @flags: Structure for information about events triggered
215 * @usb_state: Structure for usb stack information
216 * @ac_chg: AC charger power supply
217 * @usb_chg: USB charger power supply
218 * @ac: Structure that holds the AC charger properties
219 * @usb: Structure that holds the USB charger properties
220 * @regu: Pointer to the struct regulator
221 * @charger_wq: Work queue for the IRQs and checking HW state
222 * @check_vbat_work Work for checking vbat threshold to adjust vbus current
223 * @check_hw_failure_work: Work for checking HW state
224 * @check_usbchgnotok_work: Work for checking USB charger not ok status
225 * @kick_wd_work: Work for kicking the charger watchdog in case
226 * of ABB rev 1.* due to the watchog logic bug
4b45f4a9 227 * @attach_work: Work for checking the usb enumeration
b269fff4
LJ
228 * @ac_charger_attached_work: Work for checking if AC charger is still
229 * connected
230 * @usb_charger_attached_work: Work for checking if USB charger is still
231 * connected
84edbeea
AM
232 * @ac_work: Work for checking AC charger connection
233 * @detect_usb_type_work: Work for detecting the USB type connected
234 * @usb_link_status_work: Work for checking the new USB link status
235 * @usb_state_changed_work: Work for checking USB state
236 * @check_main_thermal_prot_work:
237 * Work for checking Main thermal status
238 * @check_usb_thermal_prot_work:
239 * Work for checking USB thermal status
b269fff4 240 * @charger_attached_mutex: For controlling the wakelock
84edbeea
AM
241 */
242struct ab8500_charger {
243 struct device *dev;
244 int max_usb_in_curr;
245 bool vbus_detected;
246 bool vbus_detected_start;
247 bool ac_conn;
248 bool vddadc_en_ac;
249 bool vddadc_en_usb;
250 int vbat;
251 int old_vbat;
4b45f4a9 252 bool usb_device_is_unrecognised;
84edbeea 253 bool autopower;
4aef72db 254 bool autopower_cfg;
ff38090a 255 int invalid_charger_detect_state;
84edbeea
AM
256 struct ab8500 *parent;
257 struct ab8500_gpadc *gpadc;
b0284de0 258 struct abx500_bm_data *bm;
84edbeea
AM
259 struct ab8500_charger_event_flags flags;
260 struct ab8500_charger_usb_state usb_state;
261 struct ux500_charger ac_chg;
262 struct ux500_charger usb_chg;
263 struct ab8500_charger_info ac;
264 struct ab8500_charger_info usb;
265 struct regulator *regu;
266 struct workqueue_struct *charger_wq;
267 struct delayed_work check_vbat_work;
268 struct delayed_work check_hw_failure_work;
269 struct delayed_work check_usbchgnotok_work;
270 struct delayed_work kick_wd_work;
4b45f4a9 271 struct delayed_work attach_work;
b269fff4
LJ
272 struct delayed_work ac_charger_attached_work;
273 struct delayed_work usb_charger_attached_work;
84edbeea
AM
274 struct work_struct ac_work;
275 struct work_struct detect_usb_type_work;
276 struct work_struct usb_link_status_work;
277 struct work_struct usb_state_changed_work;
278 struct work_struct check_main_thermal_prot_work;
279 struct work_struct check_usb_thermal_prot_work;
efd71c89 280 struct usb_phy *usb_phy;
84edbeea 281 struct notifier_block nb;
b269fff4 282 struct mutex charger_attached_mutex;
84edbeea
AM
283};
284
285/* AC properties */
286static enum power_supply_property ab8500_charger_ac_props[] = {
287 POWER_SUPPLY_PROP_HEALTH,
288 POWER_SUPPLY_PROP_PRESENT,
289 POWER_SUPPLY_PROP_ONLINE,
290 POWER_SUPPLY_PROP_VOLTAGE_NOW,
291 POWER_SUPPLY_PROP_VOLTAGE_AVG,
292 POWER_SUPPLY_PROP_CURRENT_NOW,
293};
294
295/* USB properties */
296static enum power_supply_property ab8500_charger_usb_props[] = {
297 POWER_SUPPLY_PROP_HEALTH,
298 POWER_SUPPLY_PROP_CURRENT_AVG,
299 POWER_SUPPLY_PROP_PRESENT,
300 POWER_SUPPLY_PROP_ONLINE,
301 POWER_SUPPLY_PROP_VOLTAGE_NOW,
302 POWER_SUPPLY_PROP_VOLTAGE_AVG,
303 POWER_SUPPLY_PROP_CURRENT_NOW,
304};
305
defbfa9c
POH
306/*
307 * Function for enabling and disabling sw fallback mode
308 * should always be disabled when no charger is connected.
84edbeea 309 */
defbfa9c
POH
310static void ab8500_enable_disable_sw_fallback(struct ab8500_charger *di,
311 bool fallback)
84edbeea
AM
312{
313 u8 reg;
314 int ret;
315
defbfa9c 316 dev_dbg(di->dev, "SW Fallback: %d\n", fallback);
84edbeea 317
defbfa9c 318 /* read the register containing fallback bit */
84edbeea
AM
319 ret = abx500_get_register_interruptible(di->dev, 0x15, 0x00, &reg);
320 if (ret) {
321 dev_err(di->dev, "%d write failed\n", __LINE__);
322 return;
323 }
324
325 /* enable the OPT emulation registers */
326 ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x2);
327 if (ret) {
328 dev_err(di->dev, "%d write failed\n", __LINE__);
329 return;
330 }
331
defbfa9c 332 if (fallback)
84edbeea
AM
333 reg |= 0x8;
334 else
335 reg &= ~0x8;
336
defbfa9c 337 /* write back the changed fallback bit value to register */
84edbeea
AM
338 ret = abx500_set_register_interruptible(di->dev, 0x15, 0x00, reg);
339 if (ret) {
340 dev_err(di->dev, "%d write failed\n", __LINE__);
341 return;
342 }
343
344 /* disable the set OTP registers again */
345 ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x0);
346 if (ret) {
347 dev_err(di->dev, "%d write failed\n", __LINE__);
348 return;
349 }
350}
351
352/**
353 * ab8500_power_supply_changed - a wrapper with local extentions for
354 * power_supply_changed
355 * @di: pointer to the ab8500_charger structure
356 * @psy: pointer to power_supply_that have changed.
357 *
358 */
359static void ab8500_power_supply_changed(struct ab8500_charger *di,
360 struct power_supply *psy)
361{
4aef72db 362 if (di->autopower_cfg) {
84edbeea
AM
363 if (!di->usb.charger_connected &&
364 !di->ac.charger_connected &&
365 di->autopower) {
366 di->autopower = false;
defbfa9c 367 ab8500_enable_disable_sw_fallback(di, false);
84edbeea
AM
368 } else if (!di->autopower &&
369 (di->ac.charger_connected ||
370 di->usb.charger_connected)) {
371 di->autopower = true;
defbfa9c 372 ab8500_enable_disable_sw_fallback(di, true);
84edbeea
AM
373 }
374 }
375 power_supply_changed(psy);
376}
377
378static void ab8500_charger_set_usb_connected(struct ab8500_charger *di,
379 bool connected)
380{
381 if (connected != di->usb.charger_connected) {
382 dev_dbg(di->dev, "USB connected:%i\n", connected);
383 di->usb.charger_connected = connected;
384 sysfs_notify(&di->usb_chg.psy.dev->kobj, NULL, "present");
b269fff4
LJ
385
386 if (connected) {
387 mutex_lock(&di->charger_attached_mutex);
388 mutex_unlock(&di->charger_attached_mutex);
389
390 queue_delayed_work(di->charger_wq,
391 &di->usb_charger_attached_work,
392 HZ);
393 } else {
394 cancel_delayed_work_sync(&di->usb_charger_attached_work);
395 mutex_lock(&di->charger_attached_mutex);
396 mutex_unlock(&di->charger_attached_mutex);
397 }
84edbeea
AM
398 }
399}
400
401/**
402 * ab8500_charger_get_ac_voltage() - get ac charger voltage
403 * @di: pointer to the ab8500_charger structure
404 *
405 * Returns ac charger voltage (on success)
406 */
407static int ab8500_charger_get_ac_voltage(struct ab8500_charger *di)
408{
409 int vch;
410
411 /* Only measure voltage if the charger is connected */
412 if (di->ac.charger_connected) {
413 vch = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_V);
414 if (vch < 0)
415 dev_err(di->dev, "%s gpadc conv failed,\n", __func__);
416 } else {
417 vch = 0;
418 }
419 return vch;
420}
421
422/**
423 * ab8500_charger_ac_cv() - check if the main charger is in CV mode
424 * @di: pointer to the ab8500_charger structure
425 *
426 * Returns ac charger CV mode (on success) else error code
427 */
428static int ab8500_charger_ac_cv(struct ab8500_charger *di)
429{
430 u8 val;
431 int ret = 0;
432
433 /* Only check CV mode if the charger is online */
434 if (di->ac.charger_online) {
435 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
436 AB8500_CH_STATUS1_REG, &val);
437 if (ret < 0) {
438 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
439 return 0;
440 }
441
442 if (val & MAIN_CH_CV_ON)
443 ret = 1;
444 else
445 ret = 0;
446 }
447
448 return ret;
449}
450
451/**
452 * ab8500_charger_get_vbus_voltage() - get vbus voltage
453 * @di: pointer to the ab8500_charger structure
454 *
455 * This function returns the vbus voltage.
456 * Returns vbus voltage (on success)
457 */
458static int ab8500_charger_get_vbus_voltage(struct ab8500_charger *di)
459{
460 int vch;
461
462 /* Only measure voltage if the charger is connected */
463 if (di->usb.charger_connected) {
464 vch = ab8500_gpadc_convert(di->gpadc, VBUS_V);
465 if (vch < 0)
466 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
467 } else {
468 vch = 0;
469 }
470 return vch;
471}
472
473/**
474 * ab8500_charger_get_usb_current() - get usb charger current
475 * @di: pointer to the ab8500_charger structure
476 *
477 * This function returns the usb charger current.
478 * Returns usb current (on success) and error code on failure
479 */
480static int ab8500_charger_get_usb_current(struct ab8500_charger *di)
481{
482 int ich;
483
484 /* Only measure current if the charger is online */
485 if (di->usb.charger_online) {
486 ich = ab8500_gpadc_convert(di->gpadc, USB_CHARGER_C);
487 if (ich < 0)
488 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
489 } else {
490 ich = 0;
491 }
492 return ich;
493}
494
495/**
496 * ab8500_charger_get_ac_current() - get ac charger current
497 * @di: pointer to the ab8500_charger structure
498 *
499 * This function returns the ac charger current.
500 * Returns ac current (on success) and error code on failure.
501 */
502static int ab8500_charger_get_ac_current(struct ab8500_charger *di)
503{
504 int ich;
505
506 /* Only measure current if the charger is online */
507 if (di->ac.charger_online) {
508 ich = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_C);
509 if (ich < 0)
510 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
511 } else {
512 ich = 0;
513 }
514 return ich;
515}
516
517/**
518 * ab8500_charger_usb_cv() - check if the usb charger is in CV mode
519 * @di: pointer to the ab8500_charger structure
520 *
521 * Returns ac charger CV mode (on success) else error code
522 */
523static int ab8500_charger_usb_cv(struct ab8500_charger *di)
524{
525 int ret;
526 u8 val;
527
528 /* Only check CV mode if the charger is online */
529 if (di->usb.charger_online) {
530 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
531 AB8500_CH_USBCH_STAT1_REG, &val);
532 if (ret < 0) {
533 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
534 return 0;
535 }
536
537 if (val & USB_CH_CV_ON)
538 ret = 1;
539 else
540 ret = 0;
541 } else {
542 ret = 0;
543 }
544
545 return ret;
546}
547
548/**
549 * ab8500_charger_detect_chargers() - Detect the connected chargers
550 * @di: pointer to the ab8500_charger structure
551 *
552 * Returns the type of charger connected.
553 * For USB it will not mean we can actually charge from it
554 * but that there is a USB cable connected that we have to
555 * identify. This is used during startup when we don't get
556 * interrupts of the charger detection
557 *
558 * Returns an integer value, that means,
559 * NO_PW_CONN no power supply is connected
560 * AC_PW_CONN if the AC power supply is connected
561 * USB_PW_CONN if the USB power supply is connected
562 * AC_PW_CONN + USB_PW_CONN if USB and AC power supplies are both connected
563 */
564static int ab8500_charger_detect_chargers(struct ab8500_charger *di)
565{
566 int result = NO_PW_CONN;
567 int ret;
568 u8 val;
569
570 /* Check for AC charger */
571 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
572 AB8500_CH_STATUS1_REG, &val);
573 if (ret < 0) {
574 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
575 return ret;
576 }
577
578 if (val & MAIN_CH_DET)
579 result = AC_PW_CONN;
580
581 /* Check for USB charger */
582 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
583 AB8500_CH_USBCH_STAT1_REG, &val);
584 if (ret < 0) {
585 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
586 return ret;
587 }
588
589 if ((val & VBUS_DET_DBNC1) && (val & VBUS_DET_DBNC100))
590 result |= USB_PW_CONN;
591
592 return result;
593}
594
595/**
596 * ab8500_charger_max_usb_curr() - get the max curr for the USB type
597 * @di: pointer to the ab8500_charger structure
598 * @link_status: the identified USB type
599 *
600 * Get the maximum current that is allowed to be drawn from the host
601 * based on the USB type.
602 * Returns error code in case of failure else 0 on success
603 */
604static int ab8500_charger_max_usb_curr(struct ab8500_charger *di,
74a8e349 605 enum ab8500_charger_link_status link_status)
84edbeea
AM
606{
607 int ret = 0;
608
4b45f4a9
MC
609 di->usb_device_is_unrecognised = false;
610
84edbeea
AM
611 switch (link_status) {
612 case USB_STAT_STD_HOST_NC:
613 case USB_STAT_STD_HOST_C_NS:
614 case USB_STAT_STD_HOST_C_S:
615 dev_dbg(di->dev, "USB Type - Standard host is "
74a8e349 616 "detected through USB driver\n");
84edbeea
AM
617 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P09;
618 break;
619 case USB_STAT_HOST_CHG_HS_CHIRP:
620 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5;
74a8e349
HB
621 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
622 di->max_usb_in_curr);
84edbeea
AM
623 break;
624 case USB_STAT_HOST_CHG_HS:
625 case USB_STAT_ACA_RID_C_HS:
626 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P9;
74a8e349
HB
627 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
628 di->max_usb_in_curr);
84edbeea
AM
629 break;
630 case USB_STAT_ACA_RID_A:
631 /*
632 * Dedicated charger level minus maximum current accessory
633 * can consume (300mA). Closest level is 1100mA
634 */
635 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P1;
74a8e349
HB
636 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
637 di->max_usb_in_curr);
84edbeea
AM
638 break;
639 case USB_STAT_ACA_RID_B:
640 /*
641 * Dedicated charger level minus 120mA (20mA for ACA and
642 * 100mA for potential accessory). Closest level is 1300mA
643 */
644 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P3;
74a8e349
HB
645 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
646 di->max_usb_in_curr);
84edbeea 647 break;
84edbeea 648 case USB_STAT_HOST_CHG_NM:
74a8e349 649 case USB_STAT_DEDICATED_CHG:
84edbeea 650 case USB_STAT_ACA_RID_C_NM:
74a8e349 651 case USB_STAT_ACA_RID_C_HS_CHIRP:
84edbeea 652 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P5;
74a8e349
HB
653 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
654 di->max_usb_in_curr);
84edbeea 655 break;
4b45f4a9
MC
656 case USB_STAT_NOT_CONFIGURED:
657 if (di->vbus_detected) {
658 di->usb_device_is_unrecognised = true;
659 dev_dbg(di->dev, "USB Type - Legacy charger.\n");
660 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P5;
661 break;
662 }
84edbeea 663 case USB_STAT_HM_IDGND:
84edbeea
AM
664 dev_err(di->dev, "USB Type - Charging not allowed\n");
665 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05;
666 ret = -ENXIO;
667 break;
74a8e349
HB
668 case USB_STAT_RESERVED:
669 if (is_ab8500(di->parent)) {
670 di->flags.vbus_collapse = true;
671 dev_err(di->dev, "USB Type - USB_STAT_RESERVED "
672 "VBUS has collapsed\n");
673 ret = -ENXIO;
674 break;
675 }
676 if (is_ab9540(di->parent) || is_ab8505(di->parent)) {
677 dev_dbg(di->dev, "USB Type - Charging not allowed\n");
678 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05;
679 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
680 link_status, di->max_usb_in_curr);
681 ret = -ENXIO;
682 break;
683 }
684 break;
685 case USB_STAT_CARKIT_1:
686 case USB_STAT_CARKIT_2:
687 case USB_STAT_ACA_DOCK_CHARGER:
688 case USB_STAT_CHARGER_LINE_1:
689 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5;
690 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
691 di->max_usb_in_curr);
ff38090a
HS
692 case USB_STAT_NOT_VALID_LINK:
693 dev_err(di->dev, "USB Type invalid - try charging anyway\n");
694 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5;
74a8e349
HB
695 break;
696
84edbeea
AM
697 default:
698 dev_err(di->dev, "USB Type - Unknown\n");
699 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05;
700 ret = -ENXIO;
701 break;
702 };
703
704 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
705 link_status, di->max_usb_in_curr);
706
707 return ret;
708}
709
710/**
711 * ab8500_charger_read_usb_type() - read the type of usb connected
712 * @di: pointer to the ab8500_charger structure
713 *
714 * Detect the type of the plugged USB
715 * Returns error code in case of failure else 0 on success
716 */
717static int ab8500_charger_read_usb_type(struct ab8500_charger *di)
718{
719 int ret;
720 u8 val;
721
722 ret = abx500_get_register_interruptible(di->dev,
723 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG, &val);
724 if (ret < 0) {
725 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
726 return ret;
727 }
74a8e349
HB
728 if (is_ab8500(di->parent)) {
729 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
730 AB8500_USB_LINE_STAT_REG, &val);
731 } else {
732 if (is_ab9540(di->parent) || is_ab8505(di->parent))
733 ret = abx500_get_register_interruptible(di->dev,
734 AB8500_USB, AB8500_USB_LINK1_STAT_REG, &val);
735 }
84edbeea
AM
736 if (ret < 0) {
737 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
738 return ret;
739 }
740
741 /* get the USB type */
742 val = (val & AB8500_USB_LINK_STATUS) >> 3;
743 ret = ab8500_charger_max_usb_curr(di,
744 (enum ab8500_charger_link_status) val);
745
746 return ret;
747}
748
749/**
750 * ab8500_charger_detect_usb_type() - get the type of usb connected
751 * @di: pointer to the ab8500_charger structure
752 *
753 * Detect the type of the plugged USB
754 * Returns error code in case of failure else 0 on success
755 */
756static int ab8500_charger_detect_usb_type(struct ab8500_charger *di)
757{
758 int i, ret;
759 u8 val;
760
761 /*
762 * On getting the VBUS rising edge detect interrupt there
763 * is a 250ms delay after which the register UsbLineStatus
764 * is filled with valid data.
765 */
766 for (i = 0; i < 10; i++) {
767 msleep(250);
768 ret = abx500_get_register_interruptible(di->dev,
769 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG,
770 &val);
771 if (ret < 0) {
772 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
773 return ret;
774 }
74a8e349
HB
775
776 if (is_ab8500(di->parent))
777 ret = abx500_get_register_interruptible(di->dev,
778 AB8500_USB, AB8500_USB_LINE_STAT_REG, &val);
779 else
780 ret = abx500_get_register_interruptible(di->dev,
781 AB8500_USB, AB8500_USB_LINK1_STAT_REG, &val);
84edbeea
AM
782 if (ret < 0) {
783 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
784 return ret;
785 }
786 /*
787 * Until the IT source register is read the UsbLineStatus
788 * register is not updated, hence doing the same
789 * Revisit this:
790 */
791
792 /* get the USB type */
793 val = (val & AB8500_USB_LINK_STATUS) >> 3;
794 if (val)
795 break;
796 }
797 ret = ab8500_charger_max_usb_curr(di,
798 (enum ab8500_charger_link_status) val);
799
800 return ret;
801}
802
803/*
804 * This array maps the raw hex value to charger voltage used by the AB8500
805 * Values taken from the UM0836
806 */
807static int ab8500_charger_voltage_map[] = {
808 3500 ,
809 3525 ,
810 3550 ,
811 3575 ,
812 3600 ,
813 3625 ,
814 3650 ,
815 3675 ,
816 3700 ,
817 3725 ,
818 3750 ,
819 3775 ,
820 3800 ,
821 3825 ,
822 3850 ,
823 3875 ,
824 3900 ,
825 3925 ,
826 3950 ,
827 3975 ,
828 4000 ,
829 4025 ,
830 4050 ,
831 4060 ,
832 4070 ,
833 4080 ,
834 4090 ,
835 4100 ,
836 4110 ,
837 4120 ,
838 4130 ,
839 4140 ,
840 4150 ,
841 4160 ,
842 4170 ,
843 4180 ,
844 4190 ,
845 4200 ,
846 4210 ,
847 4220 ,
848 4230 ,
849 4240 ,
850 4250 ,
851 4260 ,
852 4270 ,
853 4280 ,
854 4290 ,
855 4300 ,
856 4310 ,
857 4320 ,
858 4330 ,
859 4340 ,
860 4350 ,
861 4360 ,
862 4370 ,
863 4380 ,
864 4390 ,
865 4400 ,
866 4410 ,
867 4420 ,
868 4430 ,
869 4440 ,
870 4450 ,
871 4460 ,
872 4470 ,
873 4480 ,
874 4490 ,
875 4500 ,
876 4510 ,
877 4520 ,
878 4530 ,
879 4540 ,
880 4550 ,
881 4560 ,
882 4570 ,
883 4580 ,
884 4590 ,
885 4600 ,
886};
887
888/*
889 * This array maps the raw hex value to charger current used by the AB8500
890 * Values taken from the UM0836
891 */
892static int ab8500_charger_current_map[] = {
893 100 ,
894 200 ,
895 300 ,
896 400 ,
897 500 ,
898 600 ,
899 700 ,
900 800 ,
901 900 ,
902 1000 ,
903 1100 ,
904 1200 ,
905 1300 ,
906 1400 ,
907 1500 ,
908};
909
910/*
911 * This array maps the raw hex value to VBUS input current used by the AB8500
912 * Values taken from the UM0836
913 */
914static int ab8500_charger_vbus_in_curr_map[] = {
915 USB_CH_IP_CUR_LVL_0P05,
916 USB_CH_IP_CUR_LVL_0P09,
917 USB_CH_IP_CUR_LVL_0P19,
918 USB_CH_IP_CUR_LVL_0P29,
919 USB_CH_IP_CUR_LVL_0P38,
920 USB_CH_IP_CUR_LVL_0P45,
921 USB_CH_IP_CUR_LVL_0P5,
922 USB_CH_IP_CUR_LVL_0P6,
923 USB_CH_IP_CUR_LVL_0P7,
924 USB_CH_IP_CUR_LVL_0P8,
925 USB_CH_IP_CUR_LVL_0P9,
926 USB_CH_IP_CUR_LVL_1P0,
927 USB_CH_IP_CUR_LVL_1P1,
928 USB_CH_IP_CUR_LVL_1P3,
929 USB_CH_IP_CUR_LVL_1P4,
930 USB_CH_IP_CUR_LVL_1P5,
931};
932
933static int ab8500_voltage_to_regval(int voltage)
934{
935 int i;
936
937 /* Special case for voltage below 3.5V */
938 if (voltage < ab8500_charger_voltage_map[0])
939 return LOW_VOLT_REG;
940
941 for (i = 1; i < ARRAY_SIZE(ab8500_charger_voltage_map); i++) {
942 if (voltage < ab8500_charger_voltage_map[i])
943 return i - 1;
944 }
945
946 /* If not last element, return error */
947 i = ARRAY_SIZE(ab8500_charger_voltage_map) - 1;
948 if (voltage == ab8500_charger_voltage_map[i])
949 return i;
950 else
951 return -1;
952}
953
954static int ab8500_current_to_regval(int curr)
955{
956 int i;
957
958 if (curr < ab8500_charger_current_map[0])
959 return 0;
960
961 for (i = 0; i < ARRAY_SIZE(ab8500_charger_current_map); i++) {
962 if (curr < ab8500_charger_current_map[i])
963 return i - 1;
964 }
965
966 /* If not last element, return error */
967 i = ARRAY_SIZE(ab8500_charger_current_map) - 1;
968 if (curr == ab8500_charger_current_map[i])
969 return i;
970 else
971 return -1;
972}
973
974static int ab8500_vbus_in_curr_to_regval(int curr)
975{
976 int i;
977
978 if (curr < ab8500_charger_vbus_in_curr_map[0])
979 return 0;
980
981 for (i = 0; i < ARRAY_SIZE(ab8500_charger_vbus_in_curr_map); i++) {
982 if (curr < ab8500_charger_vbus_in_curr_map[i])
983 return i - 1;
984 }
985
986 /* If not last element, return error */
987 i = ARRAY_SIZE(ab8500_charger_vbus_in_curr_map) - 1;
988 if (curr == ab8500_charger_vbus_in_curr_map[i])
989 return i;
990 else
991 return -1;
992}
993
994/**
995 * ab8500_charger_get_usb_cur() - get usb current
996 * @di: pointer to the ab8500_charger structre
997 *
998 * The usb stack provides the maximum current that can be drawn from
999 * the standard usb host. This will be in mA.
1000 * This function converts current in mA to a value that can be written
1001 * to the register. Returns -1 if charging is not allowed
1002 */
1003static int ab8500_charger_get_usb_cur(struct ab8500_charger *di)
1004{
1005 switch (di->usb_state.usb_current) {
1006 case 100:
1007 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P09;
1008 break;
1009 case 200:
1010 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P19;
1011 break;
1012 case 300:
1013 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P29;
1014 break;
1015 case 400:
1016 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P38;
1017 break;
1018 case 500:
1019 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5;
1020 break;
1021 default:
1022 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05;
1023 return -1;
1024 break;
1025 };
1026 return 0;
1027}
1028
f8e96dff
JB
1029/**
1030 * ab8500_charger_set_current() - set charger current
1031 * @di: pointer to the ab8500_charger structure
1032 * @ich: charger current, in mA
1033 * @reg: select what charger register to set
1034 *
1035 * Set charger current.
1036 * There is no state machine in the AB to step up/down the charger
1037 * current to avoid dips and spikes on MAIN, VBUS and VBAT when
1038 * charging is started. Instead we need to implement
1039 * this charger current step-up/down here.
1040 * Returns error code in case of failure else 0(on success)
1041 */
1042static int ab8500_charger_set_current(struct ab8500_charger *di,
1043 int ich, int reg)
1044{
1045 int ret, i;
1046 int curr_index, prev_curr_index, shift_value;
1047 u8 reg_value;
1048
1049 switch (reg) {
1050 case AB8500_MCH_IPT_CURLVL_REG:
1051 shift_value = MAIN_CH_INPUT_CURR_SHIFT;
1052 curr_index = ab8500_current_to_regval(ich);
1053 break;
1054 case AB8500_USBCH_IPT_CRNTLVL_REG:
1055 shift_value = VBUS_IN_CURR_LIM_SHIFT;
1056 curr_index = ab8500_vbus_in_curr_to_regval(ich);
1057 break;
1058 case AB8500_CH_OPT_CRNTLVL_REG:
1059 shift_value = 0;
1060 curr_index = ab8500_current_to_regval(ich);
1061 break;
1062 default:
1063 dev_err(di->dev, "%s current register not valid\n", __func__);
1064 return -ENXIO;
1065 }
1066
1067 if (curr_index < 0) {
1068 dev_err(di->dev, "requested current limit out-of-range\n");
1069 return -ENXIO;
1070 }
1071
1072 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1073 reg, &reg_value);
1074 if (ret < 0) {
1075 dev_err(di->dev, "%s read failed\n", __func__);
1076 return ret;
1077 }
1078 prev_curr_index = (reg_value >> shift_value);
1079
1080 /* only update current if it's been changed */
1081 if (prev_curr_index == curr_index)
1082 return 0;
1083
1084 dev_dbg(di->dev, "%s set charger current: %d mA for reg: 0x%02x\n",
1085 __func__, ich, reg);
1086
1087 if (prev_curr_index > curr_index) {
1088 for (i = prev_curr_index - 1; i >= curr_index; i--) {
1089 ret = abx500_set_register_interruptible(di->dev,
1090 AB8500_CHARGER, reg, (u8) i << shift_value);
1091 if (ret) {
1092 dev_err(di->dev, "%s write failed\n", __func__);
1093 return ret;
1094 }
1095 usleep_range(STEP_UDELAY, STEP_UDELAY * 2);
1096 }
1097 } else {
1098 for (i = prev_curr_index + 1; i <= curr_index; i++) {
1099 ret = abx500_set_register_interruptible(di->dev,
1100 AB8500_CHARGER, reg, (u8) i << shift_value);
1101 if (ret) {
1102 dev_err(di->dev, "%s write failed\n", __func__);
1103 return ret;
1104 }
1105 usleep_range(STEP_UDELAY, STEP_UDELAY * 2);
1106 }
1107 }
1108 return ret;
1109}
1110
84edbeea
AM
1111/**
1112 * ab8500_charger_set_vbus_in_curr() - set VBUS input current limit
1113 * @di: pointer to the ab8500_charger structure
1114 * @ich_in: charger input current limit
1115 *
1116 * Sets the current that can be drawn from the USB host
1117 * Returns error code in case of failure else 0(on success)
1118 */
1119static int ab8500_charger_set_vbus_in_curr(struct ab8500_charger *di,
1120 int ich_in)
1121{
84edbeea
AM
1122 int min_value;
1123
1124 /* We should always use to lowest current limit */
b0284de0 1125 min_value = min(di->bm->chg_params->usb_curr_max, ich_in);
84edbeea
AM
1126
1127 switch (min_value) {
1128 case 100:
1129 if (di->vbat < VBAT_TRESH_IP_CUR_RED)
1130 min_value = USB_CH_IP_CUR_LVL_0P05;
1131 break;
1132 case 500:
1133 if (di->vbat < VBAT_TRESH_IP_CUR_RED)
1134 min_value = USB_CH_IP_CUR_LVL_0P45;
1135 break;
1136 default:
1137 break;
1138 }
1139
f8e96dff
JB
1140 return ab8500_charger_set_current(di, min_value,
1141 AB8500_USBCH_IPT_CRNTLVL_REG);
1142}
84edbeea 1143
f8e96dff
JB
1144/**
1145 * ab8500_charger_set_main_in_curr() - set main charger input current
1146 * @di: pointer to the ab8500_charger structure
1147 * @ich_in: input charger current, in mA
1148 *
1149 * Set main charger input current.
1150 * Returns error code in case of failure else 0(on success)
1151 */
1152static int ab8500_charger_set_main_in_curr(struct ab8500_charger *di,
1153 int ich_in)
1154{
1155 return ab8500_charger_set_current(di, ich_in,
1156 AB8500_MCH_IPT_CURLVL_REG);
1157}
84edbeea 1158
f8e96dff
JB
1159/**
1160 * ab8500_charger_set_output_curr() - set charger output current
1161 * @di: pointer to the ab8500_charger structure
1162 * @ich_out: output charger current, in mA
1163 *
1164 * Set charger output current.
1165 * Returns error code in case of failure else 0(on success)
1166 */
1167static int ab8500_charger_set_output_curr(struct ab8500_charger *di,
1168 int ich_out)
1169{
1170 return ab8500_charger_set_current(di, ich_out,
1171 AB8500_CH_OPT_CRNTLVL_REG);
84edbeea
AM
1172}
1173
1174/**
1175 * ab8500_charger_led_en() - turn on/off chargign led
1176 * @di: pointer to the ab8500_charger structure
1177 * @on: flag to turn on/off the chargign led
1178 *
1179 * Power ON/OFF charging LED indication
1180 * Returns error code in case of failure else 0(on success)
1181 */
1182static int ab8500_charger_led_en(struct ab8500_charger *di, int on)
1183{
1184 int ret;
1185
1186 if (on) {
1187 /* Power ON charging LED indicator, set LED current to 5mA */
1188 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1189 AB8500_LED_INDICATOR_PWM_CTRL,
1190 (LED_IND_CUR_5MA | LED_INDICATOR_PWM_ENA));
1191 if (ret) {
1192 dev_err(di->dev, "Power ON LED failed\n");
1193 return ret;
1194 }
1195 /* LED indicator PWM duty cycle 252/256 */
1196 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1197 AB8500_LED_INDICATOR_PWM_DUTY,
1198 LED_INDICATOR_PWM_DUTY_252_256);
1199 if (ret) {
1200 dev_err(di->dev, "Set LED PWM duty cycle failed\n");
1201 return ret;
1202 }
1203 } else {
1204 /* Power off charging LED indicator */
1205 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1206 AB8500_LED_INDICATOR_PWM_CTRL,
1207 LED_INDICATOR_PWM_DIS);
1208 if (ret) {
1209 dev_err(di->dev, "Power-off LED failed\n");
1210 return ret;
1211 }
1212 }
1213
1214 return ret;
1215}
1216
1217/**
1218 * ab8500_charger_ac_en() - enable or disable ac charging
1219 * @di: pointer to the ab8500_charger structure
1220 * @enable: enable/disable flag
1221 * @vset: charging voltage
1222 * @iset: charging current
1223 *
1224 * Enable/Disable AC/Mains charging and turns on/off the charging led
1225 * respectively.
1226 **/
1227static int ab8500_charger_ac_en(struct ux500_charger *charger,
1228 int enable, int vset, int iset)
1229{
1230 int ret;
1231 int volt_index;
1232 int curr_index;
1233 int input_curr_index;
1234 u8 overshoot = 0;
1235
1236 struct ab8500_charger *di = to_ab8500_charger_ac_device_info(charger);
1237
1238 if (enable) {
1239 /* Check if AC is connected */
1240 if (!di->ac.charger_connected) {
1241 dev_err(di->dev, "AC charger not connected\n");
1242 return -ENXIO;
1243 }
1244
1245 /* Enable AC charging */
1246 dev_dbg(di->dev, "Enable AC: %dmV %dmA\n", vset, iset);
1247
1248 /*
1249 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1250 * will be triggered everytime we enable the VDD ADC supply.
1251 * This will turn off charging for a short while.
1252 * It can be avoided by having the supply on when
1253 * there is a charger enabled. Normally the VDD ADC supply
1254 * is enabled everytime a GPADC conversion is triggered. We will
1255 * force it to be enabled from this driver to have
1256 * the GPADC module independant of the AB8500 chargers
1257 */
1258 if (!di->vddadc_en_ac) {
1259 regulator_enable(di->regu);
1260 di->vddadc_en_ac = true;
1261 }
1262
1263 /* Check if the requested voltage or current is valid */
1264 volt_index = ab8500_voltage_to_regval(vset);
1265 curr_index = ab8500_current_to_regval(iset);
1266 input_curr_index = ab8500_current_to_regval(
b0284de0 1267 di->bm->chg_params->ac_curr_max);
84edbeea
AM
1268 if (volt_index < 0 || curr_index < 0 || input_curr_index < 0) {
1269 dev_err(di->dev,
1270 "Charger voltage or current too high, "
1271 "charging not started\n");
1272 return -ENXIO;
1273 }
1274
1275 /* ChVoltLevel: maximum battery charging voltage */
1276 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1277 AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1278 if (ret) {
1279 dev_err(di->dev, "%s write failed\n", __func__);
1280 return ret;
1281 }
1282 /* MainChInputCurr: current that can be drawn from the charger*/
f8e96dff 1283 ret = ab8500_charger_set_main_in_curr(di,
b0284de0 1284 di->bm->chg_params->ac_curr_max);
84edbeea 1285 if (ret) {
f8e96dff
JB
1286 dev_err(di->dev, "%s Failed to set MainChInputCurr\n",
1287 __func__);
84edbeea
AM
1288 return ret;
1289 }
1290 /* ChOutputCurentLevel: protected output current */
f8e96dff 1291 ret = ab8500_charger_set_output_curr(di, iset);
84edbeea 1292 if (ret) {
f8e96dff
JB
1293 dev_err(di->dev, "%s "
1294 "Failed to set ChOutputCurentLevel\n",
1295 __func__);
84edbeea
AM
1296 return ret;
1297 }
1298
1299 /* Check if VBAT overshoot control should be enabled */
b0284de0 1300 if (!di->bm->enable_overshoot)
84edbeea
AM
1301 overshoot = MAIN_CH_NO_OVERSHOOT_ENA_N;
1302
1303 /* Enable Main Charger */
1304 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1305 AB8500_MCH_CTRL1, MAIN_CH_ENA | overshoot);
1306 if (ret) {
1307 dev_err(di->dev, "%s write failed\n", __func__);
1308 return ret;
1309 }
1310
1311 /* Power on charging LED indication */
1312 ret = ab8500_charger_led_en(di, true);
1313 if (ret < 0)
1314 dev_err(di->dev, "failed to enable LED\n");
1315
1316 di->ac.charger_online = 1;
1317 } else {
1318 /* Disable AC charging */
1319 if (is_ab8500_1p1_or_earlier(di->parent)) {
1320 /*
1321 * For ABB revision 1.0 and 1.1 there is a bug in the
1322 * watchdog logic. That means we have to continously
1323 * kick the charger watchdog even when no charger is
1324 * connected. This is only valid once the AC charger
1325 * has been enabled. This is a bug that is not handled
1326 * by the algorithm and the watchdog have to be kicked
1327 * by the charger driver when the AC charger
1328 * is disabled
1329 */
1330 if (di->ac_conn) {
1331 queue_delayed_work(di->charger_wq,
1332 &di->kick_wd_work,
1333 round_jiffies(WD_KICK_INTERVAL));
1334 }
1335
1336 /*
1337 * We can't turn off charging completely
1338 * due to a bug in AB8500 cut1.
1339 * If we do, charging will not start again.
1340 * That is why we set the lowest voltage
1341 * and current possible
1342 */
1343 ret = abx500_set_register_interruptible(di->dev,
1344 AB8500_CHARGER,
1345 AB8500_CH_VOLT_LVL_REG, CH_VOL_LVL_3P5);
1346 if (ret) {
1347 dev_err(di->dev,
1348 "%s write failed\n", __func__);
1349 return ret;
1350 }
1351
f8e96dff 1352 ret = ab8500_charger_set_output_curr(di, 0);
84edbeea 1353 if (ret) {
f8e96dff
JB
1354 dev_err(di->dev, "%s "
1355 "Failed to set ChOutputCurentLevel\n",
1356 __func__);
84edbeea
AM
1357 return ret;
1358 }
1359 } else {
1360 ret = abx500_set_register_interruptible(di->dev,
1361 AB8500_CHARGER,
1362 AB8500_MCH_CTRL1, 0);
1363 if (ret) {
1364 dev_err(di->dev,
1365 "%s write failed\n", __func__);
1366 return ret;
1367 }
1368 }
1369
1370 ret = ab8500_charger_led_en(di, false);
1371 if (ret < 0)
1372 dev_err(di->dev, "failed to disable LED\n");
1373
1374 di->ac.charger_online = 0;
1375 di->ac.wd_expired = false;
1376
1377 /* Disable regulator if enabled */
1378 if (di->vddadc_en_ac) {
1379 regulator_disable(di->regu);
1380 di->vddadc_en_ac = false;
1381 }
1382
1383 dev_dbg(di->dev, "%s Disabled AC charging\n", __func__);
1384 }
1385 ab8500_power_supply_changed(di, &di->ac_chg.psy);
1386
1387 return ret;
1388}
1389
1390/**
1391 * ab8500_charger_usb_en() - enable usb charging
1392 * @di: pointer to the ab8500_charger structure
1393 * @enable: enable/disable flag
1394 * @vset: charging voltage
1395 * @ich_out: charger output current
1396 *
1397 * Enable/Disable USB charging and turns on/off the charging led respectively.
1398 * Returns error code in case of failure else 0(on success)
1399 */
1400static int ab8500_charger_usb_en(struct ux500_charger *charger,
1401 int enable, int vset, int ich_out)
1402{
1403 int ret;
1404 int volt_index;
1405 int curr_index;
1406 u8 overshoot = 0;
1407
1408 struct ab8500_charger *di = to_ab8500_charger_usb_device_info(charger);
1409
1410 if (enable) {
1411 /* Check if USB is connected */
1412 if (!di->usb.charger_connected) {
1413 dev_err(di->dev, "USB charger not connected\n");
1414 return -ENXIO;
1415 }
1416
1417 /*
1418 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1419 * will be triggered everytime we enable the VDD ADC supply.
1420 * This will turn off charging for a short while.
1421 * It can be avoided by having the supply on when
1422 * there is a charger enabled. Normally the VDD ADC supply
1423 * is enabled everytime a GPADC conversion is triggered. We will
1424 * force it to be enabled from this driver to have
1425 * the GPADC module independant of the AB8500 chargers
1426 */
1427 if (!di->vddadc_en_usb) {
1428 regulator_enable(di->regu);
1429 di->vddadc_en_usb = true;
1430 }
1431
1432 /* Enable USB charging */
1433 dev_dbg(di->dev, "Enable USB: %dmV %dmA\n", vset, ich_out);
1434
1435 /* Check if the requested voltage or current is valid */
1436 volt_index = ab8500_voltage_to_regval(vset);
1437 curr_index = ab8500_current_to_regval(ich_out);
1438 if (volt_index < 0 || curr_index < 0) {
1439 dev_err(di->dev,
1440 "Charger voltage or current too high, "
1441 "charging not started\n");
1442 return -ENXIO;
1443 }
1444
1445 /* ChVoltLevel: max voltage upto which battery can be charged */
1446 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1447 AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1448 if (ret) {
1449 dev_err(di->dev, "%s write failed\n", __func__);
1450 return ret;
1451 }
1452 /* USBChInputCurr: current that can be drawn from the usb */
1453 ret = ab8500_charger_set_vbus_in_curr(di, di->max_usb_in_curr);
1454 if (ret) {
1455 dev_err(di->dev, "setting USBChInputCurr failed\n");
1456 return ret;
1457 }
1458 /* ChOutputCurentLevel: protected output current */
f8e96dff 1459 ret = ab8500_charger_set_output_curr(di, ich_out);
84edbeea 1460 if (ret) {
f8e96dff
JB
1461 dev_err(di->dev, "%s "
1462 "Failed to set ChOutputCurentLevel\n",
1463 __func__);
84edbeea
AM
1464 return ret;
1465 }
1466 /* Check if VBAT overshoot control should be enabled */
b0284de0 1467 if (!di->bm->enable_overshoot)
84edbeea
AM
1468 overshoot = USB_CHG_NO_OVERSHOOT_ENA_N;
1469
1470 /* Enable USB Charger */
1471 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1472 AB8500_USBCH_CTRL1_REG, USB_CH_ENA | overshoot);
1473 if (ret) {
1474 dev_err(di->dev, "%s write failed\n", __func__);
1475 return ret;
1476 }
1477
1478 /* If success power on charging LED indication */
1479 ret = ab8500_charger_led_en(di, true);
1480 if (ret < 0)
1481 dev_err(di->dev, "failed to enable LED\n");
1482
1483 queue_delayed_work(di->charger_wq, &di->check_vbat_work, HZ);
1484
1485 di->usb.charger_online = 1;
1486 } else {
1487 /* Disable USB charging */
1488 ret = abx500_set_register_interruptible(di->dev,
1489 AB8500_CHARGER,
1490 AB8500_USBCH_CTRL1_REG, 0);
1491 if (ret) {
1492 dev_err(di->dev,
1493 "%s write failed\n", __func__);
1494 return ret;
1495 }
1496
1497 ret = ab8500_charger_led_en(di, false);
1498 if (ret < 0)
1499 dev_err(di->dev, "failed to disable LED\n");
1500
1501 di->usb.charger_online = 0;
1502 di->usb.wd_expired = false;
1503
1504 /* Disable regulator if enabled */
1505 if (di->vddadc_en_usb) {
1506 regulator_disable(di->regu);
1507 di->vddadc_en_usb = false;
1508 }
1509
1510 dev_dbg(di->dev, "%s Disabled USB charging\n", __func__);
1511
1512 /* Cancel any pending Vbat check work */
1513 if (delayed_work_pending(&di->check_vbat_work))
1514 cancel_delayed_work(&di->check_vbat_work);
1515
1516 }
1517 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1518
1519 return ret;
1520}
1521
1522/**
1523 * ab8500_charger_watchdog_kick() - kick charger watchdog
1524 * @di: pointer to the ab8500_charger structure
1525 *
1526 * Kick charger watchdog
1527 * Returns error code in case of failure else 0(on success)
1528 */
1529static int ab8500_charger_watchdog_kick(struct ux500_charger *charger)
1530{
1531 int ret;
1532 struct ab8500_charger *di;
1533
1534 if (charger->psy.type == POWER_SUPPLY_TYPE_MAINS)
1535 di = to_ab8500_charger_ac_device_info(charger);
1536 else if (charger->psy.type == POWER_SUPPLY_TYPE_USB)
1537 di = to_ab8500_charger_usb_device_info(charger);
1538 else
1539 return -ENXIO;
1540
1541 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1542 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
1543 if (ret)
1544 dev_err(di->dev, "Failed to kick WD!\n");
1545
1546 return ret;
1547}
1548
1549/**
1550 * ab8500_charger_update_charger_current() - update charger current
1551 * @di: pointer to the ab8500_charger structure
1552 *
1553 * Update the charger output current for the specified charger
1554 * Returns error code in case of failure else 0(on success)
1555 */
1556static int ab8500_charger_update_charger_current(struct ux500_charger *charger,
1557 int ich_out)
1558{
1559 int ret;
84edbeea
AM
1560 struct ab8500_charger *di;
1561
1562 if (charger->psy.type == POWER_SUPPLY_TYPE_MAINS)
1563 di = to_ab8500_charger_ac_device_info(charger);
1564 else if (charger->psy.type == POWER_SUPPLY_TYPE_USB)
1565 di = to_ab8500_charger_usb_device_info(charger);
1566 else
1567 return -ENXIO;
1568
f8e96dff 1569 ret = ab8500_charger_set_output_curr(di, ich_out);
84edbeea 1570 if (ret) {
f8e96dff
JB
1571 dev_err(di->dev, "%s "
1572 "Failed to set ChOutputCurentLevel\n",
1573 __func__);
84edbeea
AM
1574 return ret;
1575 }
1576
1577 /* Reset the main and usb drop input current measurement counter */
1578 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1579 AB8500_CHARGER_CTRL,
1580 0x1);
1581 if (ret) {
1582 dev_err(di->dev, "%s write failed\n", __func__);
1583 return ret;
1584 }
1585
1586 return ret;
1587}
1588
1589static int ab8500_charger_get_ext_psy_data(struct device *dev, void *data)
1590{
1591 struct power_supply *psy;
1592 struct power_supply *ext;
1593 struct ab8500_charger *di;
1594 union power_supply_propval ret;
1595 int i, j;
1596 bool psy_found = false;
1597 struct ux500_charger *usb_chg;
1598
1599 usb_chg = (struct ux500_charger *)data;
1600 psy = &usb_chg->psy;
1601
1602 di = to_ab8500_charger_usb_device_info(usb_chg);
1603
1604 ext = dev_get_drvdata(dev);
1605
1606 /* For all psy where the driver name appears in any supplied_to */
1607 for (i = 0; i < ext->num_supplicants; i++) {
1608 if (!strcmp(ext->supplied_to[i], psy->name))
1609 psy_found = true;
1610 }
1611
1612 if (!psy_found)
1613 return 0;
1614
1615 /* Go through all properties for the psy */
1616 for (j = 0; j < ext->num_properties; j++) {
1617 enum power_supply_property prop;
1618 prop = ext->properties[j];
1619
1620 if (ext->get_property(ext, prop, &ret))
1621 continue;
1622
1623 switch (prop) {
1624 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1625 switch (ext->type) {
1626 case POWER_SUPPLY_TYPE_BATTERY:
1627 di->vbat = ret.intval / 1000;
1628 break;
1629 default:
1630 break;
1631 }
1632 break;
1633 default:
1634 break;
1635 }
1636 }
1637 return 0;
1638}
1639
1640/**
1641 * ab8500_charger_check_vbat_work() - keep vbus current within spec
1642 * @work pointer to the work_struct structure
1643 *
1644 * Due to a asic bug it is necessary to lower the input current to the vbus
1645 * charger when charging with at some specific levels. This issue is only valid
1646 * for below a certain battery voltage. This function makes sure that the
1647 * the allowed current limit isn't exceeded.
1648 */
1649static void ab8500_charger_check_vbat_work(struct work_struct *work)
1650{
1651 int t = 10;
1652 struct ab8500_charger *di = container_of(work,
1653 struct ab8500_charger, check_vbat_work.work);
1654
1655 class_for_each_device(power_supply_class, NULL,
1656 &di->usb_chg.psy, ab8500_charger_get_ext_psy_data);
1657
1658 /* First run old_vbat is 0. */
1659 if (di->old_vbat == 0)
1660 di->old_vbat = di->vbat;
1661
1662 if (!((di->old_vbat <= VBAT_TRESH_IP_CUR_RED &&
1663 di->vbat <= VBAT_TRESH_IP_CUR_RED) ||
1664 (di->old_vbat > VBAT_TRESH_IP_CUR_RED &&
1665 di->vbat > VBAT_TRESH_IP_CUR_RED))) {
1666
1667 dev_dbg(di->dev, "Vbat did cross threshold, curr: %d, new: %d,"
1668 " old: %d\n", di->max_usb_in_curr, di->vbat,
1669 di->old_vbat);
1670 ab8500_charger_set_vbus_in_curr(di, di->max_usb_in_curr);
1671 power_supply_changed(&di->usb_chg.psy);
1672 }
1673
1674 di->old_vbat = di->vbat;
1675
1676 /*
1677 * No need to check the battery voltage every second when not close to
1678 * the threshold.
1679 */
1680 if (di->vbat < (VBAT_TRESH_IP_CUR_RED + 100) &&
1681 (di->vbat > (VBAT_TRESH_IP_CUR_RED - 100)))
1682 t = 1;
1683
1684 queue_delayed_work(di->charger_wq, &di->check_vbat_work, t * HZ);
1685}
1686
1687/**
1688 * ab8500_charger_check_hw_failure_work() - check main charger failure
1689 * @work: pointer to the work_struct structure
1690 *
1691 * Work queue function for checking the main charger status
1692 */
1693static void ab8500_charger_check_hw_failure_work(struct work_struct *work)
1694{
1695 int ret;
1696 u8 reg_value;
1697
1698 struct ab8500_charger *di = container_of(work,
1699 struct ab8500_charger, check_hw_failure_work.work);
1700
1701 /* Check if the status bits for HW failure is still active */
1702 if (di->flags.mainextchnotok) {
1703 ret = abx500_get_register_interruptible(di->dev,
1704 AB8500_CHARGER, AB8500_CH_STATUS2_REG, &reg_value);
1705 if (ret < 0) {
1706 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1707 return;
1708 }
1709 if (!(reg_value & MAIN_CH_NOK)) {
1710 di->flags.mainextchnotok = false;
1711 ab8500_power_supply_changed(di, &di->ac_chg.psy);
1712 }
1713 }
1714 if (di->flags.vbus_ovv) {
1715 ret = abx500_get_register_interruptible(di->dev,
1716 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG,
1717 &reg_value);
1718 if (ret < 0) {
1719 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1720 return;
1721 }
1722 if (!(reg_value & VBUS_OVV_TH)) {
1723 di->flags.vbus_ovv = false;
1724 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1725 }
1726 }
1727 /* If we still have a failure, schedule a new check */
1728 if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
1729 queue_delayed_work(di->charger_wq,
1730 &di->check_hw_failure_work, round_jiffies(HZ));
1731 }
1732}
1733
1734/**
1735 * ab8500_charger_kick_watchdog_work() - kick the watchdog
1736 * @work: pointer to the work_struct structure
1737 *
1738 * Work queue function for kicking the charger watchdog.
1739 *
1740 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
1741 * logic. That means we have to continously kick the charger
1742 * watchdog even when no charger is connected. This is only
1743 * valid once the AC charger has been enabled. This is
1744 * a bug that is not handled by the algorithm and the
1745 * watchdog have to be kicked by the charger driver
1746 * when the AC charger is disabled
1747 */
1748static void ab8500_charger_kick_watchdog_work(struct work_struct *work)
1749{
1750 int ret;
1751
1752 struct ab8500_charger *di = container_of(work,
1753 struct ab8500_charger, kick_wd_work.work);
1754
1755 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1756 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
1757 if (ret)
1758 dev_err(di->dev, "Failed to kick WD!\n");
1759
1760 /* Schedule a new watchdog kick */
1761 queue_delayed_work(di->charger_wq,
1762 &di->kick_wd_work, round_jiffies(WD_KICK_INTERVAL));
1763}
1764
1765/**
1766 * ab8500_charger_ac_work() - work to get and set main charger status
1767 * @work: pointer to the work_struct structure
1768 *
1769 * Work queue function for checking the main charger status
1770 */
1771static void ab8500_charger_ac_work(struct work_struct *work)
1772{
1773 int ret;
1774
1775 struct ab8500_charger *di = container_of(work,
1776 struct ab8500_charger, ac_work);
1777
1778 /*
1779 * Since we can't be sure that the events are received
1780 * synchronously, we have the check if the main charger is
1781 * connected by reading the status register
1782 */
1783 ret = ab8500_charger_detect_chargers(di);
1784 if (ret < 0)
1785 return;
1786
1787 if (ret & AC_PW_CONN) {
1788 di->ac.charger_connected = 1;
1789 di->ac_conn = true;
1790 } else {
1791 di->ac.charger_connected = 0;
1792 }
1793
1794 ab8500_power_supply_changed(di, &di->ac_chg.psy);
1795 sysfs_notify(&di->ac_chg.psy.dev->kobj, NULL, "present");
1796}
1797
b269fff4
LJ
1798static void ab8500_charger_usb_attached_work(struct work_struct *work)
1799{
1800 struct ab8500_charger *di = container_of(work,
1801 struct ab8500_charger,
1802 usb_charger_attached_work.work);
1803 int usbch = (USB_CH_VBUSDROP | USB_CH_VBUSDETDBNC);
1804 int ret, i;
1805 u8 statval;
1806
1807 for (i = 0; i < 10; i++) {
1808 ret = abx500_get_register_interruptible(di->dev,
1809 AB8500_CHARGER,
1810 AB8500_CH_USBCH_STAT1_REG,
1811 &statval);
1812 if (ret < 0) {
1813 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
1814 goto reschedule;
1815 }
1816 if ((statval & usbch) != usbch)
1817 goto reschedule;
1818
1819 msleep(CHARGER_STATUS_POLL);
1820 }
1821
1822 ab8500_charger_usb_en(&di->usb_chg, 0, 0, 0);
1823
1824 mutex_lock(&di->charger_attached_mutex);
1825 mutex_unlock(&di->charger_attached_mutex);
1826
1827 return;
1828
1829reschedule:
1830 queue_delayed_work(di->charger_wq,
1831 &di->usb_charger_attached_work,
1832 HZ);
1833}
1834
1835static void ab8500_charger_ac_attached_work(struct work_struct *work)
1836{
1837
1838 struct ab8500_charger *di = container_of(work,
1839 struct ab8500_charger,
1840 ac_charger_attached_work.work);
1841 int mainch = (MAIN_CH_STATUS2_MAINCHGDROP |
1842 MAIN_CH_STATUS2_MAINCHARGERDETDBNC);
1843 int ret, i;
1844 u8 statval;
1845
1846 for (i = 0; i < 10; i++) {
1847 ret = abx500_get_register_interruptible(di->dev,
1848 AB8500_CHARGER,
1849 AB8500_CH_STATUS2_REG,
1850 &statval);
1851 if (ret < 0) {
1852 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
1853 goto reschedule;
1854 }
1855
1856 if ((statval & mainch) != mainch)
1857 goto reschedule;
1858
1859 msleep(CHARGER_STATUS_POLL);
1860 }
1861
1862 ab8500_charger_ac_en(&di->ac_chg, 0, 0, 0);
1863 queue_work(di->charger_wq, &di->ac_work);
1864
1865 mutex_lock(&di->charger_attached_mutex);
1866 mutex_unlock(&di->charger_attached_mutex);
1867
1868 return;
1869
1870reschedule:
1871 queue_delayed_work(di->charger_wq,
1872 &di->ac_charger_attached_work,
1873 HZ);
1874}
1875
84edbeea
AM
1876/**
1877 * ab8500_charger_detect_usb_type_work() - work to detect USB type
1878 * @work: Pointer to the work_struct structure
1879 *
1880 * Detect the type of USB plugged
1881 */
64eb9b02 1882static void ab8500_charger_detect_usb_type_work(struct work_struct *work)
84edbeea
AM
1883{
1884 int ret;
1885
1886 struct ab8500_charger *di = container_of(work,
1887 struct ab8500_charger, detect_usb_type_work);
1888
1889 /*
1890 * Since we can't be sure that the events are received
1891 * synchronously, we have the check if is
1892 * connected by reading the status register
1893 */
1894 ret = ab8500_charger_detect_chargers(di);
1895 if (ret < 0)
1896 return;
1897
1898 if (!(ret & USB_PW_CONN)) {
1899 di->vbus_detected = 0;
1900 ab8500_charger_set_usb_connected(di, false);
1901 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1902 } else {
1903 di->vbus_detected = 1;
1904
1905 if (is_ab8500_1p1_or_earlier(di->parent)) {
1906 ret = ab8500_charger_detect_usb_type(di);
1907 if (!ret) {
1908 ab8500_charger_set_usb_connected(di, true);
1909 ab8500_power_supply_changed(di,
1910 &di->usb_chg.psy);
1911 }
1912 } else {
1913 /* For ABB cut2.0 and onwards we have an IRQ,
1914 * USB_LINK_STATUS that will be triggered when the USB
1915 * link status changes. The exception is USB connected
1916 * during startup. Then we don't get a
1917 * USB_LINK_STATUS IRQ
1918 */
1919 if (di->vbus_detected_start) {
1920 di->vbus_detected_start = false;
1921 ret = ab8500_charger_detect_usb_type(di);
1922 if (!ret) {
1923 ab8500_charger_set_usb_connected(di,
1924 true);
1925 ab8500_power_supply_changed(di,
1926 &di->usb_chg.psy);
1927 }
1928 }
1929 }
1930 }
1931}
1932
4b45f4a9
MC
1933/**
1934 * ab8500_charger_usb_link_attach_work() - delayd work to detect USB type
1935 * @work: pointer to the work_struct structure
1936 *
1937 * Detect the type of USB plugged
1938 */
1939static void ab8500_charger_usb_link_attach_work(struct work_struct *work)
1940{
1941 struct ab8500_charger *di =
1942 container_of(work, struct ab8500_charger, attach_work.work);
1943 int ret;
1944
1945 /* Update maximum input current if USB enumeration is not detected */
1946 if (!di->usb.charger_online) {
1947 ret = ab8500_charger_set_vbus_in_curr(di, di->max_usb_in_curr);
1948 if (ret)
1949 return;
1950 }
1951
1952 ab8500_charger_set_usb_connected(di, true);
1953 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1954}
1955
84edbeea
AM
1956/**
1957 * ab8500_charger_usb_link_status_work() - work to detect USB type
1958 * @work: pointer to the work_struct structure
1959 *
1960 * Detect the type of USB plugged
1961 */
1962static void ab8500_charger_usb_link_status_work(struct work_struct *work)
1963{
ff38090a 1964 int detected_chargers;
84edbeea 1965 int ret;
ff38090a 1966 u8 val;
84edbeea
AM
1967
1968 struct ab8500_charger *di = container_of(work,
1969 struct ab8500_charger, usb_link_status_work);
1970
1971 /*
1972 * Since we can't be sure that the events are received
1973 * synchronously, we have the check if is
1974 * connected by reading the status register
1975 */
ff38090a
HS
1976 detected_chargers = ab8500_charger_detect_chargers(di);
1977 if (detected_chargers < 0)
84edbeea
AM
1978 return;
1979
ff38090a
HS
1980 /*
1981 * Some chargers that breaks the USB spec is
1982 * identified as invalid by AB8500 and it refuse
1983 * to start the charging process. but by jumping
1984 * thru a few hoops it can be forced to start.
1985 */
1986 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
1987 AB8500_USB_LINE_STAT_REG, &val);
1988 if (ret >= 0)
1989 dev_dbg(di->dev, "UsbLineStatus register = 0x%02x\n", val);
1990 else
1991 dev_dbg(di->dev, "Error reading USB link status\n");
1992
1993 if (detected_chargers & USB_PW_CONN) {
1994 if (((val & AB8500_USB_LINK_STATUS) >> 3) == USB_STAT_NOT_VALID_LINK &&
1995 di->invalid_charger_detect_state == 0) {
1996 dev_dbg(di->dev, "Invalid charger detected, state= 0\n");
1997 /*Enable charger*/
1998 abx500_mask_and_set_register_interruptible(di->dev,
1999 AB8500_CHARGER, AB8500_USBCH_CTRL1_REG, 0x01, 0x01);
2000 /*Enable charger detection*/
2001 abx500_mask_and_set_register_interruptible(di->dev, AB8500_USB,
2002 AB8500_MCH_IPT_CURLVL_REG, 0x01, 0x01);
2003 di->invalid_charger_detect_state = 1;
2004 /*exit and wait for new link status interrupt.*/
2005 return;
2006
2007 }
2008 if (di->invalid_charger_detect_state == 1) {
2009 dev_dbg(di->dev, "Invalid charger detected, state= 1\n");
2010 /*Stop charger detection*/
2011 abx500_mask_and_set_register_interruptible(di->dev, AB8500_USB,
2012 AB8500_MCH_IPT_CURLVL_REG, 0x01, 0x00);
2013 /*Check link status*/
2014 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
2015 AB8500_USB_LINE_STAT_REG, &val);
2016 dev_dbg(di->dev, "USB link status= 0x%02x\n",
2017 (val & AB8500_USB_LINK_STATUS) >> 3);
2018 di->invalid_charger_detect_state = 2;
2019 }
2020 } else {
2021 di->invalid_charger_detect_state = 0;
2022 }
2023
2024 if (!(detected_chargers & USB_PW_CONN)) {
84edbeea
AM
2025 di->vbus_detected = 0;
2026 ab8500_charger_set_usb_connected(di, false);
2027 ab8500_power_supply_changed(di, &di->usb_chg.psy);
4b45f4a9
MC
2028 return;
2029 }
84edbeea 2030
4b45f4a9
MC
2031 di->vbus_detected = 1;
2032 ret = ab8500_charger_read_usb_type(di);
2033 if (!ret) {
2034 if (di->usb_device_is_unrecognised) {
2035 dev_dbg(di->dev,
2036 "Potential Legacy Charger device. "
2037 "Delay work for %d msec for USB enum "
2038 "to finish",
2039 WAIT_FOR_USB_ENUMERATION);
2040 queue_delayed_work(di->charger_wq,
2041 &di->attach_work,
2042 msecs_to_jiffies(WAIT_FOR_USB_ENUMERATION));
2043 } else {
2044 queue_delayed_work(di->charger_wq,
2045 &di->attach_work, 0);
84edbeea 2046 }
4b45f4a9
MC
2047 } else if (ret == -ENXIO) {
2048 /* No valid charger type detected */
2049 ab8500_charger_set_usb_connected(di, false);
2050 ab8500_power_supply_changed(di, &di->usb_chg.psy);
84edbeea
AM
2051 }
2052}
2053
2054static void ab8500_charger_usb_state_changed_work(struct work_struct *work)
2055{
2056 int ret;
2057 unsigned long flags;
2058
2059 struct ab8500_charger *di = container_of(work,
2060 struct ab8500_charger, usb_state_changed_work);
2061
2062 if (!di->vbus_detected)
2063 return;
2064
2065 spin_lock_irqsave(&di->usb_state.usb_lock, flags);
2066 di->usb_state.usb_changed = false;
2067 spin_unlock_irqrestore(&di->usb_state.usb_lock, flags);
2068
2069 /*
2070 * wait for some time until you get updates from the usb stack
2071 * and negotiations are completed
2072 */
2073 msleep(250);
2074
2075 if (di->usb_state.usb_changed)
2076 return;
2077
2078 dev_dbg(di->dev, "%s USB state: 0x%02x mA: %d\n",
2079 __func__, di->usb_state.state, di->usb_state.usb_current);
2080
2081 switch (di->usb_state.state) {
2082 case AB8500_BM_USB_STATE_RESET_HS:
2083 case AB8500_BM_USB_STATE_RESET_FS:
2084 case AB8500_BM_USB_STATE_SUSPEND:
2085 case AB8500_BM_USB_STATE_MAX:
2086 ab8500_charger_set_usb_connected(di, false);
2087 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2088 break;
2089
2090 case AB8500_BM_USB_STATE_RESUME:
2091 /*
2092 * when suspend->resume there should be delay
2093 * of 1sec for enabling charging
2094 */
2095 msleep(1000);
2096 /* Intentional fall through */
2097 case AB8500_BM_USB_STATE_CONFIGURED:
2098 /*
2099 * USB is configured, enable charging with the charging
2100 * input current obtained from USB driver
2101 */
2102 if (!ab8500_charger_get_usb_cur(di)) {
2103 /* Update maximum input current */
2104 ret = ab8500_charger_set_vbus_in_curr(di,
2105 di->max_usb_in_curr);
2106 if (ret)
2107 return;
2108
2109 ab8500_charger_set_usb_connected(di, true);
2110 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2111 }
2112 break;
2113
2114 default:
2115 break;
2116 };
2117}
2118
2119/**
2120 * ab8500_charger_check_usbchargernotok_work() - check USB chg not ok status
2121 * @work: pointer to the work_struct structure
2122 *
2123 * Work queue function for checking the USB charger Not OK status
2124 */
2125static void ab8500_charger_check_usbchargernotok_work(struct work_struct *work)
2126{
2127 int ret;
2128 u8 reg_value;
2129 bool prev_status;
2130
2131 struct ab8500_charger *di = container_of(work,
2132 struct ab8500_charger, check_usbchgnotok_work.work);
2133
2134 /* Check if the status bit for usbchargernotok is still active */
2135 ret = abx500_get_register_interruptible(di->dev,
2136 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, &reg_value);
2137 if (ret < 0) {
2138 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2139 return;
2140 }
2141 prev_status = di->flags.usbchargernotok;
2142
2143 if (reg_value & VBUS_CH_NOK) {
2144 di->flags.usbchargernotok = true;
2145 /* Check again in 1sec */
2146 queue_delayed_work(di->charger_wq,
2147 &di->check_usbchgnotok_work, HZ);
2148 } else {
2149 di->flags.usbchargernotok = false;
2150 di->flags.vbus_collapse = false;
2151 }
2152
2153 if (prev_status != di->flags.usbchargernotok)
2154 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2155}
2156
2157/**
2158 * ab8500_charger_check_main_thermal_prot_work() - check main thermal status
2159 * @work: pointer to the work_struct structure
2160 *
2161 * Work queue function for checking the Main thermal prot status
2162 */
2163static void ab8500_charger_check_main_thermal_prot_work(
2164 struct work_struct *work)
2165{
2166 int ret;
2167 u8 reg_value;
2168
2169 struct ab8500_charger *di = container_of(work,
2170 struct ab8500_charger, check_main_thermal_prot_work);
2171
2172 /* Check if the status bit for main_thermal_prot is still active */
2173 ret = abx500_get_register_interruptible(di->dev,
2174 AB8500_CHARGER, AB8500_CH_STATUS2_REG, &reg_value);
2175 if (ret < 0) {
2176 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2177 return;
2178 }
2179 if (reg_value & MAIN_CH_TH_PROT)
2180 di->flags.main_thermal_prot = true;
2181 else
2182 di->flags.main_thermal_prot = false;
2183
2184 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2185}
2186
2187/**
2188 * ab8500_charger_check_usb_thermal_prot_work() - check usb thermal status
2189 * @work: pointer to the work_struct structure
2190 *
2191 * Work queue function for checking the USB thermal prot status
2192 */
2193static void ab8500_charger_check_usb_thermal_prot_work(
2194 struct work_struct *work)
2195{
2196 int ret;
2197 u8 reg_value;
2198
2199 struct ab8500_charger *di = container_of(work,
2200 struct ab8500_charger, check_usb_thermal_prot_work);
2201
2202 /* Check if the status bit for usb_thermal_prot is still active */
2203 ret = abx500_get_register_interruptible(di->dev,
2204 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, &reg_value);
2205 if (ret < 0) {
2206 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2207 return;
2208 }
2209 if (reg_value & USB_CH_TH_PROT)
2210 di->flags.usb_thermal_prot = true;
2211 else
2212 di->flags.usb_thermal_prot = false;
2213
2214 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2215}
2216
2217/**
2218 * ab8500_charger_mainchunplugdet_handler() - main charger unplugged
2219 * @irq: interrupt number
2220 * @_di: pointer to the ab8500_charger structure
2221 *
2222 * Returns IRQ status(IRQ_HANDLED)
2223 */
2224static irqreturn_t ab8500_charger_mainchunplugdet_handler(int irq, void *_di)
2225{
2226 struct ab8500_charger *di = _di;
2227
2228 dev_dbg(di->dev, "Main charger unplugged\n");
2229 queue_work(di->charger_wq, &di->ac_work);
2230
b269fff4
LJ
2231 cancel_delayed_work_sync(&di->ac_charger_attached_work);
2232 mutex_lock(&di->charger_attached_mutex);
2233 mutex_unlock(&di->charger_attached_mutex);
2234
84edbeea
AM
2235 return IRQ_HANDLED;
2236}
2237
2238/**
2239 * ab8500_charger_mainchplugdet_handler() - main charger plugged
2240 * @irq: interrupt number
2241 * @_di: pointer to the ab8500_charger structure
2242 *
2243 * Returns IRQ status(IRQ_HANDLED)
2244 */
2245static irqreturn_t ab8500_charger_mainchplugdet_handler(int irq, void *_di)
2246{
2247 struct ab8500_charger *di = _di;
2248
2249 dev_dbg(di->dev, "Main charger plugged\n");
2250 queue_work(di->charger_wq, &di->ac_work);
2251
b269fff4
LJ
2252 mutex_lock(&di->charger_attached_mutex);
2253 mutex_unlock(&di->charger_attached_mutex);
2254 queue_delayed_work(di->charger_wq,
2255 &di->ac_charger_attached_work,
2256 HZ);
84edbeea
AM
2257 return IRQ_HANDLED;
2258}
2259
2260/**
2261 * ab8500_charger_mainextchnotok_handler() - main charger not ok
2262 * @irq: interrupt number
2263 * @_di: pointer to the ab8500_charger structure
2264 *
2265 * Returns IRQ status(IRQ_HANDLED)
2266 */
2267static irqreturn_t ab8500_charger_mainextchnotok_handler(int irq, void *_di)
2268{
2269 struct ab8500_charger *di = _di;
2270
2271 dev_dbg(di->dev, "Main charger not ok\n");
2272 di->flags.mainextchnotok = true;
2273 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2274
2275 /* Schedule a new HW failure check */
2276 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
2277
2278 return IRQ_HANDLED;
2279}
2280
2281/**
2282 * ab8500_charger_mainchthprotr_handler() - Die temp is above main charger
2283 * thermal protection threshold
2284 * @irq: interrupt number
2285 * @_di: pointer to the ab8500_charger structure
2286 *
2287 * Returns IRQ status(IRQ_HANDLED)
2288 */
2289static irqreturn_t ab8500_charger_mainchthprotr_handler(int irq, void *_di)
2290{
2291 struct ab8500_charger *di = _di;
2292
2293 dev_dbg(di->dev,
2294 "Die temp above Main charger thermal protection threshold\n");
2295 queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
2296
2297 return IRQ_HANDLED;
2298}
2299
2300/**
2301 * ab8500_charger_mainchthprotf_handler() - Die temp is below main charger
2302 * thermal protection threshold
2303 * @irq: interrupt number
2304 * @_di: pointer to the ab8500_charger structure
2305 *
2306 * Returns IRQ status(IRQ_HANDLED)
2307 */
2308static irqreturn_t ab8500_charger_mainchthprotf_handler(int irq, void *_di)
2309{
2310 struct ab8500_charger *di = _di;
2311
2312 dev_dbg(di->dev,
2313 "Die temp ok for Main charger thermal protection threshold\n");
2314 queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
2315
2316 return IRQ_HANDLED;
2317}
2318
2319/**
2320 * ab8500_charger_vbusdetf_handler() - VBUS falling detected
2321 * @irq: interrupt number
2322 * @_di: pointer to the ab8500_charger structure
2323 *
2324 * Returns IRQ status(IRQ_HANDLED)
2325 */
2326static irqreturn_t ab8500_charger_vbusdetf_handler(int irq, void *_di)
2327{
2328 struct ab8500_charger *di = _di;
2329
2330 dev_dbg(di->dev, "VBUS falling detected\n");
2331 queue_work(di->charger_wq, &di->detect_usb_type_work);
2332
2333 return IRQ_HANDLED;
2334}
2335
2336/**
2337 * ab8500_charger_vbusdetr_handler() - VBUS rising detected
2338 * @irq: interrupt number
2339 * @_di: pointer to the ab8500_charger structure
2340 *
2341 * Returns IRQ status(IRQ_HANDLED)
2342 */
2343static irqreturn_t ab8500_charger_vbusdetr_handler(int irq, void *_di)
2344{
2345 struct ab8500_charger *di = _di;
2346
2347 di->vbus_detected = true;
2348 dev_dbg(di->dev, "VBUS rising detected\n");
2349 queue_work(di->charger_wq, &di->detect_usb_type_work);
2350
2351 return IRQ_HANDLED;
2352}
2353
2354/**
2355 * ab8500_charger_usblinkstatus_handler() - USB link status has changed
2356 * @irq: interrupt number
2357 * @_di: pointer to the ab8500_charger structure
2358 *
2359 * Returns IRQ status(IRQ_HANDLED)
2360 */
2361static irqreturn_t ab8500_charger_usblinkstatus_handler(int irq, void *_di)
2362{
2363 struct ab8500_charger *di = _di;
2364
2365 dev_dbg(di->dev, "USB link status changed\n");
2366
2367 queue_work(di->charger_wq, &di->usb_link_status_work);
2368
2369 return IRQ_HANDLED;
2370}
2371
2372/**
2373 * ab8500_charger_usbchthprotr_handler() - Die temp is above usb charger
2374 * thermal protection threshold
2375 * @irq: interrupt number
2376 * @_di: pointer to the ab8500_charger structure
2377 *
2378 * Returns IRQ status(IRQ_HANDLED)
2379 */
2380static irqreturn_t ab8500_charger_usbchthprotr_handler(int irq, void *_di)
2381{
2382 struct ab8500_charger *di = _di;
2383
2384 dev_dbg(di->dev,
2385 "Die temp above USB charger thermal protection threshold\n");
2386 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2387
2388 return IRQ_HANDLED;
2389}
2390
2391/**
2392 * ab8500_charger_usbchthprotf_handler() - Die temp is below usb charger
2393 * thermal protection threshold
2394 * @irq: interrupt number
2395 * @_di: pointer to the ab8500_charger structure
2396 *
2397 * Returns IRQ status(IRQ_HANDLED)
2398 */
2399static irqreturn_t ab8500_charger_usbchthprotf_handler(int irq, void *_di)
2400{
2401 struct ab8500_charger *di = _di;
2402
2403 dev_dbg(di->dev,
2404 "Die temp ok for USB charger thermal protection threshold\n");
2405 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2406
2407 return IRQ_HANDLED;
2408}
2409
2410/**
2411 * ab8500_charger_usbchargernotokr_handler() - USB charger not ok detected
2412 * @irq: interrupt number
2413 * @_di: pointer to the ab8500_charger structure
2414 *
2415 * Returns IRQ status(IRQ_HANDLED)
2416 */
2417static irqreturn_t ab8500_charger_usbchargernotokr_handler(int irq, void *_di)
2418{
2419 struct ab8500_charger *di = _di;
2420
2421 dev_dbg(di->dev, "Not allowed USB charger detected\n");
2422 queue_delayed_work(di->charger_wq, &di->check_usbchgnotok_work, 0);
2423
2424 return IRQ_HANDLED;
2425}
2426
2427/**
2428 * ab8500_charger_chwdexp_handler() - Charger watchdog expired
2429 * @irq: interrupt number
2430 * @_di: pointer to the ab8500_charger structure
2431 *
2432 * Returns IRQ status(IRQ_HANDLED)
2433 */
2434static irqreturn_t ab8500_charger_chwdexp_handler(int irq, void *_di)
2435{
2436 struct ab8500_charger *di = _di;
2437
2438 dev_dbg(di->dev, "Charger watchdog expired\n");
2439
2440 /*
2441 * The charger that was online when the watchdog expired
2442 * needs to be restarted for charging to start again
2443 */
2444 if (di->ac.charger_online) {
2445 di->ac.wd_expired = true;
2446 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2447 }
2448 if (di->usb.charger_online) {
2449 di->usb.wd_expired = true;
2450 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2451 }
2452
2453 return IRQ_HANDLED;
2454}
2455
2456/**
2457 * ab8500_charger_vbusovv_handler() - VBUS overvoltage detected
2458 * @irq: interrupt number
2459 * @_di: pointer to the ab8500_charger structure
2460 *
2461 * Returns IRQ status(IRQ_HANDLED)
2462 */
2463static irqreturn_t ab8500_charger_vbusovv_handler(int irq, void *_di)
2464{
2465 struct ab8500_charger *di = _di;
2466
2467 dev_dbg(di->dev, "VBUS overvoltage detected\n");
2468 di->flags.vbus_ovv = true;
2469 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2470
2471 /* Schedule a new HW failure check */
2472 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
2473
2474 return IRQ_HANDLED;
2475}
2476
2477/**
2478 * ab8500_charger_ac_get_property() - get the ac/mains properties
2479 * @psy: pointer to the power_supply structure
2480 * @psp: pointer to the power_supply_property structure
2481 * @val: pointer to the power_supply_propval union
2482 *
2483 * This function gets called when an application tries to get the ac/mains
2484 * properties by reading the sysfs files.
2485 * AC/Mains properties are online, present and voltage.
2486 * online: ac/mains charging is in progress or not
2487 * present: presence of the ac/mains
2488 * voltage: AC/Mains voltage
2489 * Returns error code in case of failure else 0(on success)
2490 */
2491static int ab8500_charger_ac_get_property(struct power_supply *psy,
2492 enum power_supply_property psp,
2493 union power_supply_propval *val)
2494{
2495 struct ab8500_charger *di;
a864c5a8 2496 int ret;
84edbeea
AM
2497
2498 di = to_ab8500_charger_ac_device_info(psy_to_ux500_charger(psy));
2499
2500 switch (psp) {
2501 case POWER_SUPPLY_PROP_HEALTH:
2502 if (di->flags.mainextchnotok)
2503 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
2504 else if (di->ac.wd_expired || di->usb.wd_expired)
2505 val->intval = POWER_SUPPLY_HEALTH_DEAD;
2506 else if (di->flags.main_thermal_prot)
2507 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
2508 else
2509 val->intval = POWER_SUPPLY_HEALTH_GOOD;
2510 break;
2511 case POWER_SUPPLY_PROP_ONLINE:
2512 val->intval = di->ac.charger_online;
2513 break;
2514 case POWER_SUPPLY_PROP_PRESENT:
2515 val->intval = di->ac.charger_connected;
2516 break;
2517 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
a864c5a8
JA
2518 ret = ab8500_charger_get_ac_voltage(di);
2519 if (ret >= 0)
2520 di->ac.charger_voltage = ret;
2521 /* On error, use previous value */
84edbeea
AM
2522 val->intval = di->ac.charger_voltage * 1000;
2523 break;
2524 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
2525 /*
2526 * This property is used to indicate when CV mode is entered
2527 * for the AC charger
2528 */
2529 di->ac.cv_active = ab8500_charger_ac_cv(di);
2530 val->intval = di->ac.cv_active;
2531 break;
2532 case POWER_SUPPLY_PROP_CURRENT_NOW:
a864c5a8
JA
2533 ret = ab8500_charger_get_ac_current(di);
2534 if (ret >= 0)
2535 di->ac.charger_current = ret;
2536 val->intval = di->ac.charger_current * 1000;
84edbeea
AM
2537 break;
2538 default:
2539 return -EINVAL;
2540 }
2541 return 0;
2542}
2543
2544/**
2545 * ab8500_charger_usb_get_property() - get the usb properties
2546 * @psy: pointer to the power_supply structure
2547 * @psp: pointer to the power_supply_property structure
2548 * @val: pointer to the power_supply_propval union
2549 *
2550 * This function gets called when an application tries to get the usb
2551 * properties by reading the sysfs files.
2552 * USB properties are online, present and voltage.
2553 * online: usb charging is in progress or not
2554 * present: presence of the usb
2555 * voltage: vbus voltage
2556 * Returns error code in case of failure else 0(on success)
2557 */
2558static int ab8500_charger_usb_get_property(struct power_supply *psy,
2559 enum power_supply_property psp,
2560 union power_supply_propval *val)
2561{
2562 struct ab8500_charger *di;
a864c5a8 2563 int ret;
84edbeea
AM
2564
2565 di = to_ab8500_charger_usb_device_info(psy_to_ux500_charger(psy));
2566
2567 switch (psp) {
2568 case POWER_SUPPLY_PROP_HEALTH:
2569 if (di->flags.usbchargernotok)
2570 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
2571 else if (di->ac.wd_expired || di->usb.wd_expired)
2572 val->intval = POWER_SUPPLY_HEALTH_DEAD;
2573 else if (di->flags.usb_thermal_prot)
2574 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
2575 else if (di->flags.vbus_ovv)
2576 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
2577 else
2578 val->intval = POWER_SUPPLY_HEALTH_GOOD;
2579 break;
2580 case POWER_SUPPLY_PROP_ONLINE:
2581 val->intval = di->usb.charger_online;
2582 break;
2583 case POWER_SUPPLY_PROP_PRESENT:
2584 val->intval = di->usb.charger_connected;
2585 break;
2586 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
a864c5a8
JA
2587 ret = ab8500_charger_get_vbus_voltage(di);
2588 if (ret >= 0)
2589 di->usb.charger_voltage = ret;
84edbeea
AM
2590 val->intval = di->usb.charger_voltage * 1000;
2591 break;
2592 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
2593 /*
2594 * This property is used to indicate when CV mode is entered
2595 * for the USB charger
2596 */
2597 di->usb.cv_active = ab8500_charger_usb_cv(di);
2598 val->intval = di->usb.cv_active;
2599 break;
2600 case POWER_SUPPLY_PROP_CURRENT_NOW:
a864c5a8
JA
2601 ret = ab8500_charger_get_usb_current(di);
2602 if (ret >= 0)
2603 di->usb.charger_current = ret;
2604 val->intval = di->usb.charger_current * 1000;
84edbeea
AM
2605 break;
2606 case POWER_SUPPLY_PROP_CURRENT_AVG:
2607 /*
2608 * This property is used to indicate when VBUS has collapsed
2609 * due to too high output current from the USB charger
2610 */
2611 if (di->flags.vbus_collapse)
2612 val->intval = 1;
2613 else
2614 val->intval = 0;
2615 break;
2616 default:
2617 return -EINVAL;
2618 }
2619 return 0;
2620}
2621
2622/**
2623 * ab8500_charger_init_hw_registers() - Set up charger related registers
2624 * @di: pointer to the ab8500_charger structure
2625 *
2626 * Set up charger OVV, watchdog and maximum voltage registers as well as
2627 * charging of the backup battery
2628 */
2629static int ab8500_charger_init_hw_registers(struct ab8500_charger *di)
2630{
2631 int ret = 0;
2632
2633 /* Setup maximum charger current and voltage for ABB cut2.0 */
2634 if (!is_ab8500_1p1_or_earlier(di->parent)) {
2635 ret = abx500_set_register_interruptible(di->dev,
2636 AB8500_CHARGER,
2637 AB8500_CH_VOLT_LVL_MAX_REG, CH_VOL_LVL_4P6);
2638 if (ret) {
2639 dev_err(di->dev,
2640 "failed to set CH_VOLT_LVL_MAX_REG\n");
2641 goto out;
2642 }
2643
2644 ret = abx500_set_register_interruptible(di->dev,
2645 AB8500_CHARGER,
2646 AB8500_CH_OPT_CRNTLVL_MAX_REG, CH_OP_CUR_LVL_1P6);
2647 if (ret) {
2648 dev_err(di->dev,
2649 "failed to set CH_OPT_CRNTLVL_MAX_REG\n");
2650 goto out;
2651 }
2652 }
2653
2654 /* VBUS OVV set to 6.3V and enable automatic current limitiation */
2655 ret = abx500_set_register_interruptible(di->dev,
2656 AB8500_CHARGER,
2657 AB8500_USBCH_CTRL2_REG,
2658 VBUS_OVV_SELECT_6P3V | VBUS_AUTO_IN_CURR_LIM_ENA);
2659 if (ret) {
2660 dev_err(di->dev, "failed to set VBUS OVV\n");
2661 goto out;
2662 }
2663
2664 /* Enable main watchdog in OTP */
2665 ret = abx500_set_register_interruptible(di->dev,
2666 AB8500_OTP_EMUL, AB8500_OTP_CONF_15, OTP_ENABLE_WD);
2667 if (ret) {
2668 dev_err(di->dev, "failed to enable main WD in OTP\n");
2669 goto out;
2670 }
2671
2672 /* Enable main watchdog */
2673 ret = abx500_set_register_interruptible(di->dev,
2674 AB8500_SYS_CTRL2_BLOCK,
2675 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_ENA);
2676 if (ret) {
2677 dev_err(di->dev, "faile to enable main watchdog\n");
2678 goto out;
2679 }
2680
2681 /*
2682 * Due to internal synchronisation, Enable and Kick watchdog bits
2683 * cannot be enabled in a single write.
2684 * A minimum delay of 2*32 kHz period (62.5µs) must be inserted
2685 * between writing Enable then Kick bits.
2686 */
2687 udelay(63);
2688
2689 /* Kick main watchdog */
2690 ret = abx500_set_register_interruptible(di->dev,
2691 AB8500_SYS_CTRL2_BLOCK,
2692 AB8500_MAIN_WDOG_CTRL_REG,
2693 (MAIN_WDOG_ENA | MAIN_WDOG_KICK));
2694 if (ret) {
2695 dev_err(di->dev, "failed to kick main watchdog\n");
2696 goto out;
2697 }
2698
2699 /* Disable main watchdog */
2700 ret = abx500_set_register_interruptible(di->dev,
2701 AB8500_SYS_CTRL2_BLOCK,
2702 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_DIS);
2703 if (ret) {
2704 dev_err(di->dev, "failed to disable main watchdog\n");
2705 goto out;
2706 }
2707
2708 /* Set watchdog timeout */
2709 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
2710 AB8500_CH_WD_TIMER_REG, WD_TIMER);
2711 if (ret) {
2712 dev_err(di->dev, "failed to set charger watchdog timeout\n");
2713 goto out;
2714 }
2715
2716 /* Backup battery voltage and current */
2717 ret = abx500_set_register_interruptible(di->dev,
2718 AB8500_RTC,
2719 AB8500_RTC_BACKUP_CHG_REG,
b0284de0
LJ
2720 di->bm->bkup_bat_v |
2721 di->bm->bkup_bat_i);
84edbeea
AM
2722 if (ret) {
2723 dev_err(di->dev, "failed to setup backup battery charging\n");
2724 goto out;
2725 }
2726
2727 /* Enable backup battery charging */
2728 abx500_mask_and_set_register_interruptible(di->dev,
2729 AB8500_RTC, AB8500_RTC_CTRL_REG,
2730 RTC_BUP_CH_ENA, RTC_BUP_CH_ENA);
2731 if (ret < 0)
2732 dev_err(di->dev, "%s mask and set failed\n", __func__);
2733
2734out:
2735 return ret;
2736}
2737
2738/*
2739 * ab8500 charger driver interrupts and their respective isr
2740 */
2741static struct ab8500_charger_interrupts ab8500_charger_irq[] = {
2742 {"MAIN_CH_UNPLUG_DET", ab8500_charger_mainchunplugdet_handler},
2743 {"MAIN_CHARGE_PLUG_DET", ab8500_charger_mainchplugdet_handler},
2744 {"MAIN_EXT_CH_NOT_OK", ab8500_charger_mainextchnotok_handler},
2745 {"MAIN_CH_TH_PROT_R", ab8500_charger_mainchthprotr_handler},
2746 {"MAIN_CH_TH_PROT_F", ab8500_charger_mainchthprotf_handler},
2747 {"VBUS_DET_F", ab8500_charger_vbusdetf_handler},
2748 {"VBUS_DET_R", ab8500_charger_vbusdetr_handler},
2749 {"USB_LINK_STATUS", ab8500_charger_usblinkstatus_handler},
2750 {"USB_CH_TH_PROT_R", ab8500_charger_usbchthprotr_handler},
2751 {"USB_CH_TH_PROT_F", ab8500_charger_usbchthprotf_handler},
2752 {"USB_CHARGER_NOT_OKR", ab8500_charger_usbchargernotokr_handler},
2753 {"VBUS_OVV", ab8500_charger_vbusovv_handler},
2754 {"CH_WD_EXP", ab8500_charger_chwdexp_handler},
2755};
2756
2757static int ab8500_charger_usb_notifier_call(struct notifier_block *nb,
2758 unsigned long event, void *power)
2759{
2760 struct ab8500_charger *di =
2761 container_of(nb, struct ab8500_charger, nb);
2762 enum ab8500_usb_state bm_usb_state;
2763 unsigned mA = *((unsigned *)power);
2764
2765 if (event != USB_EVENT_VBUS) {
2766 dev_dbg(di->dev, "not a standard host, returning\n");
2767 return NOTIFY_DONE;
2768 }
2769
2770 /* TODO: State is fabricate here. See if charger really needs USB
2771 * state or if mA is enough
2772 */
2773 if ((di->usb_state.usb_current == 2) && (mA > 2))
2774 bm_usb_state = AB8500_BM_USB_STATE_RESUME;
2775 else if (mA == 0)
2776 bm_usb_state = AB8500_BM_USB_STATE_RESET_HS;
2777 else if (mA == 2)
2778 bm_usb_state = AB8500_BM_USB_STATE_SUSPEND;
2779 else if (mA >= 8) /* 8, 100, 500 */
2780 bm_usb_state = AB8500_BM_USB_STATE_CONFIGURED;
2781 else /* Should never occur */
2782 bm_usb_state = AB8500_BM_USB_STATE_RESET_FS;
2783
2784 dev_dbg(di->dev, "%s usb_state: 0x%02x mA: %d\n",
2785 __func__, bm_usb_state, mA);
2786
2787 spin_lock(&di->usb_state.usb_lock);
2788 di->usb_state.usb_changed = true;
2789 spin_unlock(&di->usb_state.usb_lock);
2790
2791 di->usb_state.state = bm_usb_state;
2792 di->usb_state.usb_current = mA;
2793
2794 queue_work(di->charger_wq, &di->usb_state_changed_work);
2795
2796 return NOTIFY_OK;
2797}
2798
2799#if defined(CONFIG_PM)
2800static int ab8500_charger_resume(struct platform_device *pdev)
2801{
2802 int ret;
2803 struct ab8500_charger *di = platform_get_drvdata(pdev);
2804
2805 /*
2806 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
2807 * logic. That means we have to continously kick the charger
2808 * watchdog even when no charger is connected. This is only
2809 * valid once the AC charger has been enabled. This is
2810 * a bug that is not handled by the algorithm and the
2811 * watchdog have to be kicked by the charger driver
2812 * when the AC charger is disabled
2813 */
2814 if (di->ac_conn && is_ab8500_1p1_or_earlier(di->parent)) {
2815 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
2816 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
2817 if (ret)
2818 dev_err(di->dev, "Failed to kick WD!\n");
2819
2820 /* If not already pending start a new timer */
2821 if (!delayed_work_pending(
2822 &di->kick_wd_work)) {
2823 queue_delayed_work(di->charger_wq, &di->kick_wd_work,
2824 round_jiffies(WD_KICK_INTERVAL));
2825 }
2826 }
2827
2828 /* If we still have a HW failure, schedule a new check */
2829 if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
2830 queue_delayed_work(di->charger_wq,
2831 &di->check_hw_failure_work, 0);
2832 }
2833
2834 return 0;
2835}
2836
2837static int ab8500_charger_suspend(struct platform_device *pdev,
2838 pm_message_t state)
2839{
2840 struct ab8500_charger *di = platform_get_drvdata(pdev);
2841
2842 /* Cancel any pending HW failure check */
2843 if (delayed_work_pending(&di->check_hw_failure_work))
2844 cancel_delayed_work(&di->check_hw_failure_work);
2845
2846 return 0;
2847}
2848#else
2849#define ab8500_charger_suspend NULL
2850#define ab8500_charger_resume NULL
2851#endif
2852
415ec69f 2853static int ab8500_charger_remove(struct platform_device *pdev)
84edbeea
AM
2854{
2855 struct ab8500_charger *di = platform_get_drvdata(pdev);
2856 int i, irq, ret;
2857
2858 /* Disable AC charging */
2859 ab8500_charger_ac_en(&di->ac_chg, false, 0, 0);
2860
2861 /* Disable USB charging */
2862 ab8500_charger_usb_en(&di->usb_chg, false, 0, 0);
2863
2864 /* Disable interrupts */
2865 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
2866 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
2867 free_irq(irq, di);
2868 }
2869
84edbeea
AM
2870 /* Backup battery voltage and current disable */
2871 ret = abx500_mask_and_set_register_interruptible(di->dev,
2872 AB8500_RTC, AB8500_RTC_CTRL_REG, RTC_BUP_CH_ENA, 0);
2873 if (ret < 0)
2874 dev_err(di->dev, "%s mask and set failed\n", __func__);
2875
efd71c89 2876 usb_unregister_notifier(di->usb_phy, &di->nb);
721002ec 2877 usb_put_phy(di->usb_phy);
84edbeea
AM
2878
2879 /* Delete the work queue */
2880 destroy_workqueue(di->charger_wq);
2881
2882 flush_scheduled_work();
01ec8c54
MJ
2883 if(di->usb_chg.enabled)
2884 power_supply_unregister(&di->usb_chg.psy);
2885 if(di->ac_chg.enabled)
2886 power_supply_unregister(&di->ac_chg.psy);
2887
84edbeea 2888 platform_set_drvdata(pdev, NULL);
84edbeea
AM
2889
2890 return 0;
2891}
2892
4aef72db
R
2893static char *supply_interface[] = {
2894 "ab8500_chargalg",
2895 "ab8500_fg",
2896 "ab8500_btemp",
2897};
2898
c8afa640 2899static int ab8500_charger_probe(struct platform_device *pdev)
84edbeea 2900{
4aef72db 2901 struct device_node *np = pdev->dev.of_node;
7722b799 2902 struct abx500_bm_data *plat = pdev->dev.platform_data;
2aac3de1 2903 struct ab8500_charger *di;
b269fff4 2904 int irq, i, charger_status, ret = 0, ch_stat;
84edbeea 2905
4aef72db
R
2906 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
2907 if (!di) {
2908 dev_err(&pdev->dev, "%s no mem for ab8500_charger\n", __func__);
84edbeea 2909 return -ENOMEM;
4aef72db 2910 }
7722b799
LJ
2911
2912 if (!plat) {
2913 dev_err(&pdev->dev, "no battery management data supplied\n");
2914 return -EINVAL;
2915 }
2916 di->bm = plat;
2917
2918 if (np) {
2919 ret = ab8500_bm_of_probe(&pdev->dev, np, di->bm);
2920 if (ret) {
2921 dev_err(&pdev->dev, "failed to get battery information\n");
2922 return ret;
4aef72db 2923 }
7722b799
LJ
2924 di->autopower_cfg = of_property_read_bool(np, "autopower_cfg");
2925 } else
4aef72db 2926 di->autopower_cfg = false;
84edbeea
AM
2927
2928 /* get parent data */
2929 di->dev = &pdev->dev;
2930 di->parent = dev_get_drvdata(pdev->dev.parent);
2931 di->gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
2932
2933 /* initialize lock */
2934 spin_lock_init(&di->usb_state.usb_lock);
2935
84edbeea 2936 di->autopower = false;
ff38090a 2937 di->invalid_charger_detect_state = 0;
84edbeea
AM
2938
2939 /* AC supply */
2940 /* power_supply base class */
2941 di->ac_chg.psy.name = "ab8500_ac";
2942 di->ac_chg.psy.type = POWER_SUPPLY_TYPE_MAINS;
2943 di->ac_chg.psy.properties = ab8500_charger_ac_props;
2944 di->ac_chg.psy.num_properties = ARRAY_SIZE(ab8500_charger_ac_props);
2945 di->ac_chg.psy.get_property = ab8500_charger_ac_get_property;
4aef72db
R
2946 di->ac_chg.psy.supplied_to = supply_interface;
2947 di->ac_chg.psy.num_supplicants = ARRAY_SIZE(supply_interface),
84edbeea
AM
2948 /* ux500_charger sub-class */
2949 di->ac_chg.ops.enable = &ab8500_charger_ac_en;
2950 di->ac_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
2951 di->ac_chg.ops.update_curr = &ab8500_charger_update_charger_current;
2952 di->ac_chg.max_out_volt = ab8500_charger_voltage_map[
2953 ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
2954 di->ac_chg.max_out_curr = ab8500_charger_current_map[
2955 ARRAY_SIZE(ab8500_charger_current_map) - 1];
01ec8c54 2956 di->ac_chg.enabled = di->pdata->ac_enabled;
84edbeea
AM
2957
2958 /* USB supply */
2959 /* power_supply base class */
2960 di->usb_chg.psy.name = "ab8500_usb";
2961 di->usb_chg.psy.type = POWER_SUPPLY_TYPE_USB;
2962 di->usb_chg.psy.properties = ab8500_charger_usb_props;
2963 di->usb_chg.psy.num_properties = ARRAY_SIZE(ab8500_charger_usb_props);
2964 di->usb_chg.psy.get_property = ab8500_charger_usb_get_property;
4aef72db
R
2965 di->usb_chg.psy.supplied_to = supply_interface;
2966 di->usb_chg.psy.num_supplicants = ARRAY_SIZE(supply_interface),
84edbeea
AM
2967 /* ux500_charger sub-class */
2968 di->usb_chg.ops.enable = &ab8500_charger_usb_en;
2969 di->usb_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
2970 di->usb_chg.ops.update_curr = &ab8500_charger_update_charger_current;
2971 di->usb_chg.max_out_volt = ab8500_charger_voltage_map[
2972 ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
2973 di->usb_chg.max_out_curr = ab8500_charger_current_map[
2974 ARRAY_SIZE(ab8500_charger_current_map) - 1];
01ec8c54 2975 di->usb_chg.enabled = di->pdata->usb_enabled;
84edbeea
AM
2976
2977 /* Create a work queue for the charger */
2978 di->charger_wq =
2979 create_singlethread_workqueue("ab8500_charger_wq");
2980 if (di->charger_wq == NULL) {
2981 dev_err(di->dev, "failed to create work queue\n");
4aef72db 2982 return -ENOMEM;
84edbeea
AM
2983 }
2984
b269fff4
LJ
2985 mutex_init(&di->charger_attached_mutex);
2986
84edbeea 2987 /* Init work for HW failure check */
203b42f7 2988 INIT_DEFERRABLE_WORK(&di->check_hw_failure_work,
84edbeea 2989 ab8500_charger_check_hw_failure_work);
203b42f7 2990 INIT_DEFERRABLE_WORK(&di->check_usbchgnotok_work,
84edbeea
AM
2991 ab8500_charger_check_usbchargernotok_work);
2992
b269fff4
LJ
2993 INIT_DELAYED_WORK(&di->ac_charger_attached_work,
2994 ab8500_charger_ac_attached_work);
2995 INIT_DELAYED_WORK(&di->usb_charger_attached_work,
2996 ab8500_charger_usb_attached_work);
2997
84edbeea
AM
2998 /*
2999 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
3000 * logic. That means we have to continously kick the charger
3001 * watchdog even when no charger is connected. This is only
3002 * valid once the AC charger has been enabled. This is
3003 * a bug that is not handled by the algorithm and the
3004 * watchdog have to be kicked by the charger driver
3005 * when the AC charger is disabled
3006 */
203b42f7 3007 INIT_DEFERRABLE_WORK(&di->kick_wd_work,
84edbeea
AM
3008 ab8500_charger_kick_watchdog_work);
3009
203b42f7 3010 INIT_DEFERRABLE_WORK(&di->check_vbat_work,
84edbeea
AM
3011 ab8500_charger_check_vbat_work);
3012
4b45f4a9
MC
3013 INIT_DELAYED_WORK(&di->attach_work,
3014 ab8500_charger_usb_link_attach_work);
3015
84edbeea
AM
3016 /* Init work for charger detection */
3017 INIT_WORK(&di->usb_link_status_work,
3018 ab8500_charger_usb_link_status_work);
3019 INIT_WORK(&di->ac_work, ab8500_charger_ac_work);
3020 INIT_WORK(&di->detect_usb_type_work,
3021 ab8500_charger_detect_usb_type_work);
3022
3023 INIT_WORK(&di->usb_state_changed_work,
3024 ab8500_charger_usb_state_changed_work);
3025
3026 /* Init work for checking HW status */
3027 INIT_WORK(&di->check_main_thermal_prot_work,
3028 ab8500_charger_check_main_thermal_prot_work);
3029 INIT_WORK(&di->check_usb_thermal_prot_work,
3030 ab8500_charger_check_usb_thermal_prot_work);
3031
3032 /*
3033 * VDD ADC supply needs to be enabled from this driver when there
3034 * is a charger connected to avoid erroneous BTEMP_HIGH/LOW
3035 * interrupts during charging
3036 */
8feffd10 3037 di->regu = devm_regulator_get(di->dev, "vddadc");
84edbeea
AM
3038 if (IS_ERR(di->regu)) {
3039 ret = PTR_ERR(di->regu);
3040 dev_err(di->dev, "failed to get vddadc regulator\n");
3041 goto free_charger_wq;
3042 }
3043
3044
3045 /* Initialize OVV, and other registers */
3046 ret = ab8500_charger_init_hw_registers(di);
3047 if (ret) {
3048 dev_err(di->dev, "failed to initialize ABB registers\n");
8feffd10 3049 goto free_charger_wq;
84edbeea
AM
3050 }
3051
3052 /* Register AC charger class */
01ec8c54
MJ
3053 if(di->ac_chg.enabled) {
3054 ret = power_supply_register(di->dev, &di->ac_chg.psy);
3055 if (ret) {
3056 dev_err(di->dev, "failed to register AC charger\n");
3057 goto free_charger_wq;
3058 }
84edbeea
AM
3059 }
3060
3061 /* Register USB charger class */
01ec8c54
MJ
3062 if(di->usb_chg.enabled) {
3063 ret = power_supply_register(di->dev, &di->usb_chg.psy);
3064 if (ret) {
3065 dev_err(di->dev, "failed to register USB charger\n");
3066 goto free_ac;
3067 }
84edbeea
AM
3068 }
3069
662dca54 3070 di->usb_phy = usb_get_phy(USB_PHY_TYPE_USB2);
ded017ee 3071 if (IS_ERR_OR_NULL(di->usb_phy)) {
efd71c89 3072 dev_err(di->dev, "failed to get usb transceiver\n");
84edbeea
AM
3073 ret = -EINVAL;
3074 goto free_usb;
3075 }
3076 di->nb.notifier_call = ab8500_charger_usb_notifier_call;
efd71c89 3077 ret = usb_register_notifier(di->usb_phy, &di->nb);
84edbeea 3078 if (ret) {
efd71c89
AV
3079 dev_err(di->dev, "failed to register usb notifier\n");
3080 goto put_usb_phy;
84edbeea
AM
3081 }
3082
3083 /* Identify the connected charger types during startup */
3084 charger_status = ab8500_charger_detect_chargers(di);
3085 if (charger_status & AC_PW_CONN) {
3086 di->ac.charger_connected = 1;
3087 di->ac_conn = true;
3088 ab8500_power_supply_changed(di, &di->ac_chg.psy);
3089 sysfs_notify(&di->ac_chg.psy.dev->kobj, NULL, "present");
3090 }
3091
3092 if (charger_status & USB_PW_CONN) {
84edbeea
AM
3093 di->vbus_detected = true;
3094 di->vbus_detected_start = true;
3095 queue_work(di->charger_wq,
3096 &di->detect_usb_type_work);
3097 }
3098
3099 /* Register interrupts */
3100 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
3101 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3102 ret = request_threaded_irq(irq, NULL, ab8500_charger_irq[i].isr,
3103 IRQF_SHARED | IRQF_NO_SUSPEND,
3104 ab8500_charger_irq[i].name, di);
3105
3106 if (ret != 0) {
3107 dev_err(di->dev, "failed to request %s IRQ %d: %d\n"
3108 , ab8500_charger_irq[i].name, irq, ret);
3109 goto free_irq;
3110 }
3111 dev_dbg(di->dev, "Requested %s IRQ %d: %d\n",
3112 ab8500_charger_irq[i].name, irq, ret);
3113 }
3114
3115 platform_set_drvdata(pdev, di);
3116
b269fff4
LJ
3117 mutex_lock(&di->charger_attached_mutex);
3118
3119 ch_stat = ab8500_charger_detect_chargers(di);
3120
3121 if ((ch_stat & AC_PW_CONN) == AC_PW_CONN) {
3122 queue_delayed_work(di->charger_wq,
3123 &di->ac_charger_attached_work,
3124 HZ);
3125 }
3126 if ((ch_stat & USB_PW_CONN) == USB_PW_CONN) {
3127 queue_delayed_work(di->charger_wq,
3128 &di->usb_charger_attached_work,
3129 HZ);
3130 }
3131
3132 mutex_unlock(&di->charger_attached_mutex);
3133
84edbeea
AM
3134 return ret;
3135
3136free_irq:
efd71c89 3137 usb_unregister_notifier(di->usb_phy, &di->nb);
84edbeea
AM
3138
3139 /* We also have to free all successfully registered irqs */
3140 for (i = i - 1; i >= 0; i--) {
3141 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3142 free_irq(irq, di);
3143 }
efd71c89 3144put_usb_phy:
721002ec 3145 usb_put_phy(di->usb_phy);
84edbeea 3146free_usb:
01ec8c54
MJ
3147 if(di->usb_chg.enabled)
3148 power_supply_unregister(&di->usb_chg.psy);
84edbeea 3149free_ac:
01ec8c54
MJ
3150 if(di->ac_chg.enabled)
3151 power_supply_unregister(&di->ac_chg.psy);
84edbeea
AM
3152free_charger_wq:
3153 destroy_workqueue(di->charger_wq);
84edbeea
AM
3154 return ret;
3155}
3156
4aef72db
R
3157static const struct of_device_id ab8500_charger_match[] = {
3158 { .compatible = "stericsson,ab8500-charger", },
3159 { },
3160};
3161
84edbeea
AM
3162static struct platform_driver ab8500_charger_driver = {
3163 .probe = ab8500_charger_probe,
28ea73f4 3164 .remove = ab8500_charger_remove,
84edbeea
AM
3165 .suspend = ab8500_charger_suspend,
3166 .resume = ab8500_charger_resume,
3167 .driver = {
3168 .name = "ab8500-charger",
3169 .owner = THIS_MODULE,
4aef72db 3170 .of_match_table = ab8500_charger_match,
84edbeea
AM
3171 },
3172};
3173
3174static int __init ab8500_charger_init(void)
3175{
3176 return platform_driver_register(&ab8500_charger_driver);
3177}
3178
3179static void __exit ab8500_charger_exit(void)
3180{
3181 platform_driver_unregister(&ab8500_charger_driver);
3182}
3183
3184subsys_initcall_sync(ab8500_charger_init);
3185module_exit(ab8500_charger_exit);
3186
3187MODULE_LICENSE("GPL v2");
3188MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy");
3189MODULE_ALIAS("platform:ab8500-charger");
3190MODULE_DESCRIPTION("AB8500 charger management driver");
This page took 0.251239 seconds and 5 git commands to generate.