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;
81 unsigned no_temp_support
:1;
82 unsigned no_insert_detect
:1;
84 int (*set_charger
) (int);
87 static int __set_charger(struct max8925_power_info
*info
, int enable
)
89 struct max8925_chip
*chip
= info
->chip
;
91 /* enable charger in platform */
92 if (info
->set_charger
)
95 max8925_set_bits(info
->gpm
, MAX8925_CHG_CNTL1
, 1 << 7, 0);
98 max8925_set_bits(info
->gpm
, MAX8925_CHG_CNTL1
, 1 << 7, 1 << 7);
99 if (info
->set_charger
)
100 info
->set_charger(0);
102 dev_dbg(chip
->dev
, "%s\n", (enable
) ? "Enable charger"
103 : "Disable charger");
107 static irqreturn_t
max8925_charger_handler(int irq
, void *data
)
109 struct max8925_power_info
*info
= (struct max8925_power_info
*)data
;
110 struct max8925_chip
*chip
= info
->chip
;
112 switch (irq
- chip
->irq_base
) {
113 case MAX8925_IRQ_VCHG_DC_R
:
115 __set_charger(info
, 1);
116 dev_dbg(chip
->dev
, "Adapter inserted\n");
118 case MAX8925_IRQ_VCHG_DC_F
:
120 __set_charger(info
, 0);
121 dev_dbg(chip
->dev
, "Adapter removed\n");
123 case MAX8925_IRQ_VCHG_THM_OK_F
:
124 /* Battery is not ready yet */
125 dev_dbg(chip
->dev
, "Battery temperature is out of range\n");
126 case MAX8925_IRQ_VCHG_DC_OVP
:
127 dev_dbg(chip
->dev
, "Error detection\n");
128 __set_charger(info
, 0);
130 case MAX8925_IRQ_VCHG_THM_OK_R
:
131 /* Battery is ready now */
132 dev_dbg(chip
->dev
, "Battery temperature is in range\n");
134 case MAX8925_IRQ_VCHG_SYSLOW_R
:
136 dev_info(chip
->dev
, "Sys power is too low\n");
138 case MAX8925_IRQ_VCHG_SYSLOW_F
:
139 dev_dbg(chip
->dev
, "Sys power is above low threshold\n");
141 case MAX8925_IRQ_VCHG_DONE
:
142 __set_charger(info
, 0);
143 dev_dbg(chip
->dev
, "Charging is done\n");
145 case MAX8925_IRQ_VCHG_TOPOFF
:
146 dev_dbg(chip
->dev
, "Charging in top-off mode\n");
148 case MAX8925_IRQ_VCHG_TMR_FAULT
:
149 __set_charger(info
, 0);
150 dev_dbg(chip
->dev
, "Safe timer is expired\n");
152 case MAX8925_IRQ_VCHG_RST
:
153 __set_charger(info
, 0);
154 dev_dbg(chip
->dev
, "Charger is reset\n");
160 static int start_measure(struct max8925_power_info
*info
, int type
)
162 unsigned char buf
[2] = {0, 0};
164 int meas_reg
= 0, ret
;
168 meas_cmd
= MAX8925_CMD_VCHG
;
169 meas_reg
= MAX8925_ADC_VCHG
;
172 meas_cmd
= MAX8925_CMD_VBBATT
;
173 meas_reg
= MAX8925_ADC_VBBATT
;
176 meas_cmd
= MAX8925_CMD_VMBATT
;
177 meas_reg
= MAX8925_ADC_VMBATT
;
180 meas_cmd
= MAX8925_CMD_ISNS
;
181 meas_reg
= MAX8925_ADC_ISNS
;
187 max8925_reg_write(info
->adc
, meas_cmd
, 0);
188 max8925_bulk_read(info
->adc
, meas_reg
, 2, buf
);
189 ret
= ((buf
[0]<<8) | buf
[1]) >> 4;
194 static int max8925_ac_get_prop(struct power_supply
*psy
,
195 enum power_supply_property psp
,
196 union power_supply_propval
*val
)
198 struct max8925_power_info
*info
= dev_get_drvdata(psy
->dev
->parent
);
202 case POWER_SUPPLY_PROP_ONLINE
:
203 val
->intval
= info
->ac_online
;
205 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
206 if (info
->ac_online
) {
207 ret
= start_measure(info
, MEASURE_VCHG
);
209 val
->intval
= ret
* 2000; /* unit is uV */
223 static enum power_supply_property max8925_ac_props
[] = {
224 POWER_SUPPLY_PROP_ONLINE
,
225 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
228 static int max8925_usb_get_prop(struct power_supply
*psy
,
229 enum power_supply_property psp
,
230 union power_supply_propval
*val
)
232 struct max8925_power_info
*info
= dev_get_drvdata(psy
->dev
->parent
);
236 case POWER_SUPPLY_PROP_ONLINE
:
237 val
->intval
= info
->usb_online
;
239 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
240 if (info
->usb_online
) {
241 ret
= start_measure(info
, MEASURE_VCHG
);
243 val
->intval
= ret
* 2000; /* unit is uV */
257 static enum power_supply_property max8925_usb_props
[] = {
258 POWER_SUPPLY_PROP_ONLINE
,
259 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
262 static int max8925_bat_get_prop(struct power_supply
*psy
,
263 enum power_supply_property psp
,
264 union power_supply_propval
*val
)
266 struct max8925_power_info
*info
= dev_get_drvdata(psy
->dev
->parent
);
270 case POWER_SUPPLY_PROP_ONLINE
:
271 val
->intval
= info
->bat_online
;
273 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
274 if (info
->bat_online
) {
275 ret
= start_measure(info
, MEASURE_VMBATT
);
277 val
->intval
= ret
* 2000; /* unit is uV */
284 case POWER_SUPPLY_PROP_CURRENT_NOW
:
285 if (info
->bat_online
) {
286 ret
= start_measure(info
, MEASURE_ISNS
);
288 /* assume r_sns is 0.02 */
289 ret
= ((ret
* 6250) - 3125) /* uA */;
292 val
->intval
= ret
; /* unit is mA */
299 case POWER_SUPPLY_PROP_CHARGE_TYPE
:
300 if (!info
->bat_online
) {
304 ret
= max8925_reg_read(info
->gpm
, MAX8925_CHG_STATUS
);
305 ret
= (ret
& MAX8925_CHG_STAT_MODE_MASK
) >> 2;
308 val
->intval
= POWER_SUPPLY_CHARGE_TYPE_FAST
;
312 val
->intval
= POWER_SUPPLY_CHARGE_TYPE_TRICKLE
;
315 val
->intval
= POWER_SUPPLY_CHARGE_TYPE_NONE
;
320 case POWER_SUPPLY_PROP_STATUS
:
321 if (!info
->bat_online
) {
325 ret
= max8925_reg_read(info
->gpm
, MAX8925_CHG_STATUS
);
326 if (info
->usb_online
|| info
->ac_online
) {
327 val
->intval
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
328 if (ret
& MAX8925_CHG_STAT_EN_MASK
)
329 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
331 val
->intval
= POWER_SUPPLY_STATUS_DISCHARGING
;
341 static enum power_supply_property max8925_battery_props
[] = {
342 POWER_SUPPLY_PROP_ONLINE
,
343 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
344 POWER_SUPPLY_PROP_CURRENT_NOW
,
345 POWER_SUPPLY_PROP_CHARGE_TYPE
,
346 POWER_SUPPLY_PROP_STATUS
,
349 #define REQUEST_IRQ(_irq, _name) \
351 ret = request_threaded_irq(chip->irq_base + _irq, NULL, \
352 max8925_charger_handler, \
353 IRQF_ONESHOT, _name, info); \
355 dev_err(chip->dev, "Failed to request IRQ #%d: %d\n", \
359 static __devinit
int max8925_init_charger(struct max8925_chip
*chip
,
360 struct max8925_power_info
*info
)
364 REQUEST_IRQ(MAX8925_IRQ_VCHG_DC_OVP
, "ac-ovp");
365 if (!info
->no_insert_detect
) {
366 REQUEST_IRQ(MAX8925_IRQ_VCHG_DC_F
, "ac-remove");
367 REQUEST_IRQ(MAX8925_IRQ_VCHG_DC_R
, "ac-insert");
369 if (!info
->no_temp_support
) {
370 REQUEST_IRQ(MAX8925_IRQ_VCHG_THM_OK_R
, "batt-temp-in-range");
371 REQUEST_IRQ(MAX8925_IRQ_VCHG_THM_OK_F
, "batt-temp-out-range");
373 REQUEST_IRQ(MAX8925_IRQ_VCHG_SYSLOW_F
, "vsys-high");
374 REQUEST_IRQ(MAX8925_IRQ_VCHG_SYSLOW_R
, "vsys-low");
375 REQUEST_IRQ(MAX8925_IRQ_VCHG_RST
, "charger-reset");
376 REQUEST_IRQ(MAX8925_IRQ_VCHG_DONE
, "charger-done");
377 REQUEST_IRQ(MAX8925_IRQ_VCHG_TOPOFF
, "charger-topoff");
378 REQUEST_IRQ(MAX8925_IRQ_VCHG_TMR_FAULT
, "charger-timer-expire");
380 info
->usb_online
= 0;
381 info
->bat_online
= 0;
383 /* check for power - can miss interrupt at boot time */
384 if (start_measure(info
, MEASURE_VCHG
) * 2000 > 500000)
389 ret
= max8925_reg_read(info
->gpm
, MAX8925_CHG_STATUS
);
392 * If battery detection is enabled, ID pin of battery is
393 * connected to MBDET pin of MAX8925. It could be used to
394 * detect battery presence.
395 * Otherwise, we have to assume that battery is always on.
397 if (info
->batt_detect
)
398 info
->bat_online
= (ret
& MAX8925_CHG_MBDET
) ? 0 : 1;
400 info
->bat_online
= 1;
401 if (ret
& MAX8925_CHG_AC_RANGE_MASK
)
407 max8925_set_bits(info
->gpm
, MAX8925_CHG_CNTL1
, 1 << 7, 1 << 7);
408 /* set charging current in charge topoff mode */
409 max8925_set_bits(info
->gpm
, MAX8925_CHG_CNTL1
, 3 << 5,
410 info
->topoff_threshold
<< 5);
411 /* set charing current in fast charge mode */
412 max8925_set_bits(info
->gpm
, MAX8925_CHG_CNTL1
, 7, info
->fast_charge
);
417 static __devexit
int max8925_deinit_charger(struct max8925_power_info
*info
)
419 struct max8925_chip
*chip
= info
->chip
;
422 irq
= chip
->irq_base
+ MAX8925_IRQ_VCHG_DC_OVP
;
423 for (; irq
<= chip
->irq_base
+ MAX8925_IRQ_VCHG_TMR_FAULT
; irq
++)
429 static __devinit
int max8925_power_probe(struct platform_device
*pdev
)
431 struct max8925_chip
*chip
= dev_get_drvdata(pdev
->dev
.parent
);
432 struct max8925_power_pdata
*pdata
= NULL
;
433 struct max8925_power_info
*info
;
436 pdata
= pdev
->dev
.platform_data
;
438 dev_err(&pdev
->dev
, "platform data isn't assigned to "
443 info
= kzalloc(sizeof(struct max8925_power_info
), GFP_KERNEL
);
447 info
->gpm
= chip
->i2c
;
448 info
->adc
= chip
->adc
;
449 platform_set_drvdata(pdev
, info
);
451 info
->ac
.name
= "max8925-ac";
452 info
->ac
.type
= POWER_SUPPLY_TYPE_MAINS
;
453 info
->ac
.properties
= max8925_ac_props
;
454 info
->ac
.num_properties
= ARRAY_SIZE(max8925_ac_props
);
455 info
->ac
.get_property
= max8925_ac_get_prop
;
456 info
->ac
.supplied_to
= pdata
->supplied_to
;
457 info
->ac
.num_supplicants
= pdata
->num_supplicants
;
458 ret
= power_supply_register(&pdev
->dev
, &info
->ac
);
461 info
->ac
.dev
->parent
= &pdev
->dev
;
463 info
->usb
.name
= "max8925-usb";
464 info
->usb
.type
= POWER_SUPPLY_TYPE_USB
;
465 info
->usb
.properties
= max8925_usb_props
;
466 info
->usb
.num_properties
= ARRAY_SIZE(max8925_usb_props
);
467 info
->usb
.get_property
= max8925_usb_get_prop
;
468 info
->usb
.supplied_to
= pdata
->supplied_to
;
469 info
->usb
.num_supplicants
= pdata
->num_supplicants
;
471 ret
= power_supply_register(&pdev
->dev
, &info
->usb
);
474 info
->usb
.dev
->parent
= &pdev
->dev
;
476 info
->battery
.name
= "max8925-battery";
477 info
->battery
.type
= POWER_SUPPLY_TYPE_BATTERY
;
478 info
->battery
.properties
= max8925_battery_props
;
479 info
->battery
.num_properties
= ARRAY_SIZE(max8925_battery_props
);
480 info
->battery
.get_property
= max8925_bat_get_prop
;
481 ret
= power_supply_register(&pdev
->dev
, &info
->battery
);
484 info
->battery
.dev
->parent
= &pdev
->dev
;
486 info
->batt_detect
= pdata
->batt_detect
;
487 info
->topoff_threshold
= pdata
->topoff_threshold
;
488 info
->fast_charge
= pdata
->fast_charge
;
489 info
->set_charger
= pdata
->set_charger
;
490 info
->no_temp_support
= pdata
->no_temp_support
;
491 info
->no_insert_detect
= pdata
->no_insert_detect
;
493 max8925_init_charger(chip
, info
);
496 power_supply_unregister(&info
->battery
);
498 power_supply_unregister(&info
->ac
);
504 static __devexit
int max8925_power_remove(struct platform_device
*pdev
)
506 struct max8925_power_info
*info
= platform_get_drvdata(pdev
);
509 power_supply_unregister(&info
->ac
);
510 power_supply_unregister(&info
->usb
);
511 power_supply_unregister(&info
->battery
);
512 max8925_deinit_charger(info
);
518 static struct platform_driver max8925_power_driver
= {
519 .probe
= max8925_power_probe
,
520 .remove
= __devexit_p(max8925_power_remove
),
522 .name
= "max8925-power",
526 module_platform_driver(max8925_power_driver
);
528 MODULE_LICENSE("GPL");
529 MODULE_DESCRIPTION("Power supply driver for MAX8925");
530 MODULE_ALIAS("platform:max8925-power");