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