2 * max8997_charger.c - Power supply consumer driver for the Maxim 8997/8966
4 * Copyright (C) 2011 Samsung Electronics
5 * MyungJoo Ham <myungjoo.ham@samsung.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 as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/err.h>
23 #include <linux/slab.h>
24 #include <linux/platform_device.h>
25 #include <linux/power_supply.h>
26 #include <linux/mfd/max8997.h>
27 #include <linux/mfd/max8997-private.h>
31 struct max8997_dev
*iodev
;
32 struct power_supply battery
;
35 static enum power_supply_property max8997_battery_props
[] = {
36 POWER_SUPPLY_PROP_STATUS
, /* "FULL" or "NOT FULL" only. */
37 POWER_SUPPLY_PROP_PRESENT
, /* the presence of battery */
38 POWER_SUPPLY_PROP_ONLINE
, /* charger is active or not */
41 /* Note that the charger control is done by a current regulator "CHARGER" */
42 static int max8997_battery_get_property(struct power_supply
*psy
,
43 enum power_supply_property psp
,
44 union power_supply_propval
*val
)
46 struct charger_data
*charger
= container_of(psy
,
47 struct charger_data
, battery
);
48 struct i2c_client
*i2c
= charger
->iodev
->i2c
;
53 case POWER_SUPPLY_PROP_STATUS
:
55 ret
= max8997_read_reg(i2c
, MAX8997_REG_STATUS4
, ®
);
58 if ((reg
& (1 << 0)) == 0x1)
59 val
->intval
= POWER_SUPPLY_STATUS_FULL
;
62 case POWER_SUPPLY_PROP_PRESENT
:
64 ret
= max8997_read_reg(i2c
, MAX8997_REG_STATUS4
, ®
);
67 if ((reg
& (1 << 2)) == 0x0)
71 case POWER_SUPPLY_PROP_ONLINE
:
73 ret
= max8997_read_reg(i2c
, MAX8997_REG_STATUS4
, ®
);
88 static __devinit
int max8997_battery_probe(struct platform_device
*pdev
)
91 struct charger_data
*charger
;
92 struct max8997_dev
*iodev
= dev_get_drvdata(pdev
->dev
.parent
);
93 struct max8997_platform_data
*pdata
= dev_get_platdata(iodev
->dev
);
99 u8 val
= (pdata
->eoc_mA
- 50) / 10;
105 ret
= max8997_update_reg(iodev
->i2c
,
106 MAX8997_REG_MBCCTRL5
, val
, 0xf);
108 dev_err(&pdev
->dev
, "Cannot use i2c bus.\n");
113 switch (pdata
->timeout
) {
115 ret
= max8997_update_reg(iodev
->i2c
, MAX8997_REG_MBCCTRL1
,
119 ret
= max8997_update_reg(iodev
->i2c
, MAX8997_REG_MBCCTRL1
,
123 ret
= max8997_update_reg(iodev
->i2c
, MAX8997_REG_MBCCTRL1
,
127 ret
= max8997_update_reg(iodev
->i2c
, MAX8997_REG_MBCCTRL1
,
131 dev_err(&pdev
->dev
, "incorrect timeout value (%d)\n",
136 dev_err(&pdev
->dev
, "Cannot use i2c bus.\n");
140 charger
= kzalloc(sizeof(struct charger_data
), GFP_KERNEL
);
141 if (charger
== NULL
) {
142 dev_err(&pdev
->dev
, "Cannot allocate memory.\n");
146 platform_set_drvdata(pdev
, charger
);
148 charger
->battery
.name
= "max8997_pmic";
149 charger
->battery
.type
= POWER_SUPPLY_TYPE_BATTERY
;
150 charger
->battery
.get_property
= max8997_battery_get_property
;
151 charger
->battery
.properties
= max8997_battery_props
;
152 charger
->battery
.num_properties
= ARRAY_SIZE(max8997_battery_props
);
154 charger
->dev
= &pdev
->dev
;
155 charger
->iodev
= iodev
;
157 ret
= power_supply_register(&pdev
->dev
, &charger
->battery
);
159 dev_err(&pdev
->dev
, "failed: power supply register\n");
169 static int __devexit
max8997_battery_remove(struct platform_device
*pdev
)
171 struct charger_data
*charger
= platform_get_drvdata(pdev
);
173 power_supply_unregister(&charger
->battery
);
178 static const struct platform_device_id max8997_battery_id
[] = {
179 { "max8997-battery", 0 },
182 static struct platform_driver max8997_battery_driver
= {
184 .name
= "max8997-battery",
185 .owner
= THIS_MODULE
,
187 .probe
= max8997_battery_probe
,
188 .remove
= __devexit_p(max8997_battery_remove
),
189 .id_table
= max8997_battery_id
,
192 static int __init
max8997_battery_init(void)
194 return platform_driver_register(&max8997_battery_driver
);
196 subsys_initcall(max8997_battery_init
);
198 static void __exit
max8997_battery_cleanup(void)
200 platform_driver_unregister(&max8997_battery_driver
);
202 module_exit(max8997_battery_cleanup
);
204 MODULE_DESCRIPTION("MAXIM 8997/8966 battery control driver");
205 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
206 MODULE_LICENSE("GPL");