2 * Battery driver for Maxim MAX8925
4 * Copyright (c) 2009-2010 Marvell International Ltd.
5 * Haojian Zhuang <haojian.zhuang@marvell.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/module.h>
13 #include <linux/err.h>
14 #include <linux/slab.h>
15 #include <linux/i2c.h>
16 #include <linux/interrupt.h>
17 #include <linux/platform_device.h>
18 #include <linux/power_supply.h>
19 #include <linux/mfd/max8925.h>
21 /* registers in GPM */
22 #define MAX8925_OUT5VEN 0x54
23 #define MAX8925_OUT3VEN 0x58
24 #define MAX8925_CHG_CNTL1 0x7c
27 #define MAX8925_CHG_STAT_VSYSLOW (1 << 0)
28 #define MAX8925_CHG_STAT_MODE_MASK (3 << 2)
29 #define MAX8925_CHG_STAT_EN_MASK (1 << 4)
30 #define MAX8925_CHG_MBDET (1 << 1)
31 #define MAX8925_CHG_AC_RANGE_MASK (3 << 6)
33 /* registers in ADC */
34 #define MAX8925_ADC_RES_CNFG1 0x06
35 #define MAX8925_ADC_AVG_CNFG1 0x07
36 #define MAX8925_ADC_ACQ_CNFG1 0x08
37 #define MAX8925_ADC_ACQ_CNFG2 0x09
38 /* 2 bytes registers in below. MSB is 1st, LSB is 2nd. */
39 #define MAX8925_ADC_AUX2 0x62
40 #define MAX8925_ADC_VCHG 0x64
41 #define MAX8925_ADC_VBBATT 0x66
42 #define MAX8925_ADC_VMBATT 0x68
43 #define MAX8925_ADC_ISNS 0x6a
44 #define MAX8925_ADC_THM 0x6c
45 #define MAX8925_ADC_TDIE 0x6e
46 #define MAX8925_CMD_AUX2 0xc8
47 #define MAX8925_CMD_VCHG 0xd0
48 #define MAX8925_CMD_VBBATT 0xd8
49 #define MAX8925_CMD_VMBATT 0xe0
50 #define MAX8925_CMD_ISNS 0xe8
51 #define MAX8925_CMD_THM 0xf0
52 #define MAX8925_CMD_TDIE 0xf8
65 struct max8925_power_info
{
66 struct max8925_chip
*chip
;
67 struct i2c_client
*gpm
;
68 struct i2c_client
*adc
;
70 struct power_supply ac
;
71 struct power_supply usb
;
72 struct power_supply battery
;
75 unsigned usb_online
:1;
76 unsigned bat_online
:1;
78 unsigned batt_detect
:1; /* detecing MB by ID pin */
79 unsigned topoff_threshold
:2;
80 unsigned fast_charge
:3;
82 int (*set_charger
) (int);
85 static int __set_charger(struct max8925_power_info
*info
, int enable
)
87 struct max8925_chip
*chip
= info
->chip
;
89 /* enable charger in platform */
90 if (info
->set_charger
)
93 max8925_set_bits(info
->gpm
, MAX8925_CHG_CNTL1
, 1 << 7, 0);
96 max8925_set_bits(info
->gpm
, MAX8925_CHG_CNTL1
, 1 << 7, 1 << 7);
97 if (info
->set_charger
)
100 dev_dbg(chip
->dev
, "%s\n", (enable
) ? "Enable charger"
101 : "Disable charger");
105 static irqreturn_t
max8925_charger_handler(int irq
, void *data
)
107 struct max8925_power_info
*info
= (struct max8925_power_info
*)data
;
108 struct max8925_chip
*chip
= info
->chip
;
110 switch (irq
- chip
->irq_base
) {
111 case MAX8925_IRQ_VCHG_DC_R
:
113 __set_charger(info
, 1);
114 dev_dbg(chip
->dev
, "Adapter inserted\n");
116 case MAX8925_IRQ_VCHG_DC_F
:
118 __set_charger(info
, 0);
119 dev_dbg(chip
->dev
, "Adapter is removal\n");
121 case MAX8925_IRQ_VCHG_USB_R
:
122 info
->usb_online
= 1;
123 __set_charger(info
, 1);
124 dev_dbg(chip
->dev
, "USB inserted\n");
126 case MAX8925_IRQ_VCHG_USB_F
:
127 info
->usb_online
= 0;
128 __set_charger(info
, 0);
129 dev_dbg(chip
->dev
, "USB is removal\n");
131 case MAX8925_IRQ_VCHG_THM_OK_F
:
132 /* Battery is not ready yet */
133 dev_dbg(chip
->dev
, "Battery temperature is out of range\n");
134 case MAX8925_IRQ_VCHG_DC_OVP
:
135 dev_dbg(chip
->dev
, "Error detection\n");
136 __set_charger(info
, 0);
138 case MAX8925_IRQ_VCHG_THM_OK_R
:
139 /* Battery is ready now */
140 dev_dbg(chip
->dev
, "Battery temperature is in range\n");
142 case MAX8925_IRQ_VCHG_SYSLOW_R
:
144 dev_info(chip
->dev
, "Sys power is too low\n");
146 case MAX8925_IRQ_VCHG_SYSLOW_F
:
147 dev_dbg(chip
->dev
, "Sys power is above low threshold\n");
149 case MAX8925_IRQ_VCHG_DONE
:
150 __set_charger(info
, 0);
151 dev_dbg(chip
->dev
, "Charging is done\n");
153 case MAX8925_IRQ_VCHG_TOPOFF
:
154 dev_dbg(chip
->dev
, "Charging in top-off mode\n");
156 case MAX8925_IRQ_VCHG_TMR_FAULT
:
157 __set_charger(info
, 0);
158 dev_dbg(chip
->dev
, "Safe timer is expired\n");
160 case MAX8925_IRQ_VCHG_RST
:
161 __set_charger(info
, 0);
162 dev_dbg(chip
->dev
, "Charger is reset\n");
168 static int start_measure(struct max8925_power_info
*info
, int type
)
170 unsigned char buf
[2] = {0, 0};
171 int meas_reg
= 0, ret
;
175 meas_reg
= MAX8925_ADC_VCHG
;
178 meas_reg
= MAX8925_ADC_VBBATT
;
181 meas_reg
= MAX8925_ADC_VMBATT
;
184 meas_reg
= MAX8925_ADC_ISNS
;
190 max8925_bulk_read(info
->adc
, meas_reg
, 2, buf
);
191 ret
= (buf
[0] << 4) | (buf
[1] >> 4);
196 static int max8925_ac_get_prop(struct power_supply
*psy
,
197 enum power_supply_property psp
,
198 union power_supply_propval
*val
)
200 struct max8925_power_info
*info
= dev_get_drvdata(psy
->dev
->parent
);
204 case POWER_SUPPLY_PROP_ONLINE
:
205 val
->intval
= info
->ac_online
;
207 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
208 if (info
->ac_online
) {
209 ret
= start_measure(info
, MEASURE_VCHG
);
211 val
->intval
= ret
<< 1; /* unit is mV */
225 static enum power_supply_property max8925_ac_props
[] = {
226 POWER_SUPPLY_PROP_ONLINE
,
227 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
230 static int max8925_usb_get_prop(struct power_supply
*psy
,
231 enum power_supply_property psp
,
232 union power_supply_propval
*val
)
234 struct max8925_power_info
*info
= dev_get_drvdata(psy
->dev
->parent
);
238 case POWER_SUPPLY_PROP_ONLINE
:
239 val
->intval
= info
->usb_online
;
241 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
242 if (info
->usb_online
) {
243 ret
= start_measure(info
, MEASURE_VCHG
);
245 val
->intval
= ret
<< 1; /* unit is mV */
259 static enum power_supply_property max8925_usb_props
[] = {
260 POWER_SUPPLY_PROP_ONLINE
,
261 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
264 static int max8925_bat_get_prop(struct power_supply
*psy
,
265 enum power_supply_property psp
,
266 union power_supply_propval
*val
)
268 struct max8925_power_info
*info
= dev_get_drvdata(psy
->dev
->parent
);
269 long long int tmp
= 0;
273 case POWER_SUPPLY_PROP_ONLINE
:
274 val
->intval
= info
->bat_online
;
276 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
277 if (info
->bat_online
) {
278 ret
= start_measure(info
, MEASURE_VMBATT
);
280 val
->intval
= ret
<< 1; /* unit is mV */
287 case POWER_SUPPLY_PROP_CURRENT_NOW
:
288 if (info
->bat_online
) {
289 ret
= start_measure(info
, MEASURE_ISNS
);
291 tmp
= (long long int)ret
* 6250 / 4096 - 3125;
295 val
->intval
= ret
; /* unit is mA */
302 case POWER_SUPPLY_PROP_CHARGE_TYPE
:
303 if (!info
->bat_online
) {
307 ret
= max8925_reg_read(info
->gpm
, MAX8925_CHG_STATUS
);
308 ret
= (ret
& MAX8925_CHG_STAT_MODE_MASK
) >> 2;
311 val
->intval
= POWER_SUPPLY_CHARGE_TYPE_FAST
;
315 val
->intval
= POWER_SUPPLY_CHARGE_TYPE_TRICKLE
;
318 val
->intval
= POWER_SUPPLY_CHARGE_TYPE_NONE
;
323 case POWER_SUPPLY_PROP_STATUS
:
324 if (!info
->bat_online
) {
328 ret
= max8925_reg_read(info
->gpm
, MAX8925_CHG_STATUS
);
329 if (info
->usb_online
|| info
->ac_online
) {
330 val
->intval
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
331 if (ret
& MAX8925_CHG_STAT_EN_MASK
)
332 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
334 val
->intval
= POWER_SUPPLY_STATUS_DISCHARGING
;
344 static enum power_supply_property max8925_battery_props
[] = {
345 POWER_SUPPLY_PROP_ONLINE
,
346 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
347 POWER_SUPPLY_PROP_CURRENT_NOW
,
348 POWER_SUPPLY_PROP_CHARGE_TYPE
,
349 POWER_SUPPLY_PROP_STATUS
,
352 #define REQUEST_IRQ(_irq, _name) \
354 ret = request_threaded_irq(chip->irq_base + _irq, NULL, \
355 max8925_charger_handler, \
356 IRQF_ONESHOT, _name, info); \
358 dev_err(chip->dev, "Failed to request IRQ #%d: %d\n", \
362 static __devinit
int max8925_init_charger(struct max8925_chip
*chip
,
363 struct max8925_power_info
*info
)
367 REQUEST_IRQ(MAX8925_IRQ_VCHG_DC_OVP
, "ac-ovp");
368 REQUEST_IRQ(MAX8925_IRQ_VCHG_DC_F
, "ac-remove");
369 REQUEST_IRQ(MAX8925_IRQ_VCHG_DC_R
, "ac-insert");
370 REQUEST_IRQ(MAX8925_IRQ_VCHG_USB_OVP
, "usb-ovp");
371 REQUEST_IRQ(MAX8925_IRQ_VCHG_USB_F
, "usb-remove");
372 REQUEST_IRQ(MAX8925_IRQ_VCHG_USB_R
, "usb-insert");
373 REQUEST_IRQ(MAX8925_IRQ_VCHG_THM_OK_R
, "batt-temp-in-range");
374 REQUEST_IRQ(MAX8925_IRQ_VCHG_THM_OK_F
, "batt-temp-out-range");
375 REQUEST_IRQ(MAX8925_IRQ_VCHG_SYSLOW_F
, "vsys-high");
376 REQUEST_IRQ(MAX8925_IRQ_VCHG_SYSLOW_R
, "vsys-low");
377 REQUEST_IRQ(MAX8925_IRQ_VCHG_RST
, "charger-reset");
378 REQUEST_IRQ(MAX8925_IRQ_VCHG_DONE
, "charger-done");
379 REQUEST_IRQ(MAX8925_IRQ_VCHG_TOPOFF
, "charger-topoff");
380 REQUEST_IRQ(MAX8925_IRQ_VCHG_TMR_FAULT
, "charger-timer-expire");
383 info
->usb_online
= 0;
384 info
->bat_online
= 0;
385 ret
= max8925_reg_read(info
->gpm
, MAX8925_CHG_STATUS
);
388 * If battery detection is enabled, ID pin of battery is
389 * connected to MBDET pin of MAX8925. It could be used to
390 * detect battery presence.
391 * Otherwise, we have to assume that battery is always on.
393 if (info
->batt_detect
)
394 info
->bat_online
= (ret
& MAX8925_CHG_MBDET
) ? 0 : 1;
396 info
->bat_online
= 1;
397 if (ret
& MAX8925_CHG_AC_RANGE_MASK
)
403 max8925_set_bits(info
->gpm
, MAX8925_CHG_CNTL1
, 1 << 7, 1 << 7);
404 /* set charging current in charge topoff mode */
405 max8925_set_bits(info
->gpm
, MAX8925_CHG_CNTL1
, 3 << 5,
406 info
->topoff_threshold
<< 5);
407 /* set charing current in fast charge mode */
408 max8925_set_bits(info
->gpm
, MAX8925_CHG_CNTL1
, 7, info
->fast_charge
);
413 static __devexit
int max8925_deinit_charger(struct max8925_power_info
*info
)
415 struct max8925_chip
*chip
= info
->chip
;
418 irq
= chip
->irq_base
+ MAX8925_IRQ_VCHG_DC_OVP
;
419 for (; irq
<= chip
->irq_base
+ MAX8925_IRQ_VCHG_TMR_FAULT
; irq
++)
425 static __devinit
int max8925_power_probe(struct platform_device
*pdev
)
427 struct max8925_chip
*chip
= dev_get_drvdata(pdev
->dev
.parent
);
428 struct max8925_power_pdata
*pdata
= NULL
;
429 struct max8925_power_info
*info
;
432 pdata
= pdev
->dev
.platform_data
;
434 dev_err(&pdev
->dev
, "platform data isn't assigned to "
439 info
= kzalloc(sizeof(struct max8925_power_info
), GFP_KERNEL
);
443 info
->gpm
= chip
->i2c
;
444 info
->adc
= chip
->adc
;
445 platform_set_drvdata(pdev
, info
);
447 info
->ac
.name
= "max8925-ac";
448 info
->ac
.type
= POWER_SUPPLY_TYPE_MAINS
;
449 info
->ac
.properties
= max8925_ac_props
;
450 info
->ac
.num_properties
= ARRAY_SIZE(max8925_ac_props
);
451 info
->ac
.get_property
= max8925_ac_get_prop
;
452 ret
= power_supply_register(&pdev
->dev
, &info
->ac
);
455 info
->ac
.dev
->parent
= &pdev
->dev
;
457 info
->usb
.name
= "max8925-usb";
458 info
->usb
.type
= POWER_SUPPLY_TYPE_USB
;
459 info
->usb
.properties
= max8925_usb_props
;
460 info
->usb
.num_properties
= ARRAY_SIZE(max8925_usb_props
);
461 info
->usb
.get_property
= max8925_usb_get_prop
;
462 ret
= power_supply_register(&pdev
->dev
, &info
->usb
);
465 info
->usb
.dev
->parent
= &pdev
->dev
;
467 info
->battery
.name
= "max8925-battery";
468 info
->battery
.type
= POWER_SUPPLY_TYPE_BATTERY
;
469 info
->battery
.properties
= max8925_battery_props
;
470 info
->battery
.num_properties
= ARRAY_SIZE(max8925_battery_props
);
471 info
->battery
.get_property
= max8925_bat_get_prop
;
472 ret
= power_supply_register(&pdev
->dev
, &info
->battery
);
475 info
->battery
.dev
->parent
= &pdev
->dev
;
477 info
->batt_detect
= pdata
->batt_detect
;
478 info
->topoff_threshold
= pdata
->topoff_threshold
;
479 info
->fast_charge
= pdata
->fast_charge
;
480 info
->set_charger
= pdata
->set_charger
;
482 max8925_init_charger(chip
, info
);
485 power_supply_unregister(&info
->battery
);
487 power_supply_unregister(&info
->ac
);
493 static __devexit
int max8925_power_remove(struct platform_device
*pdev
)
495 struct max8925_power_info
*info
= platform_get_drvdata(pdev
);
498 power_supply_unregister(&info
->ac
);
499 power_supply_unregister(&info
->usb
);
500 power_supply_unregister(&info
->battery
);
501 max8925_deinit_charger(info
);
507 static struct platform_driver max8925_power_driver
= {
508 .probe
= max8925_power_probe
,
509 .remove
= __devexit_p(max8925_power_remove
),
511 .name
= "max8925-power",
515 static int __init
max8925_power_init(void)
517 return platform_driver_register(&max8925_power_driver
);
519 module_init(max8925_power_init
);
521 static void __exit
max8925_power_exit(void)
523 platform_driver_unregister(&max8925_power_driver
);
525 module_exit(max8925_power_exit
);
527 MODULE_LICENSE("GPL");
528 MODULE_DESCRIPTION("Power supply driver for MAX8925");
529 MODULE_ALIAS("platform:max8925-power");