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