2 * max8998_charger.c - Power supply consumer driver for the Maxim 8998/LP3974
4 * Copyright (C) 2009-2010 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/max8998.h>
27 #include <linux/mfd/max8998-private.h>
29 struct max8998_battery_data
{
31 struct max8998_dev
*iodev
;
32 struct power_supply battery
;
35 static enum power_supply_property max8998_battery_props
[] = {
36 POWER_SUPPLY_PROP_PRESENT
, /* the presence of battery */
37 POWER_SUPPLY_PROP_ONLINE
, /* charger is active or not */
40 /* Note that the charger control is done by a current regulator "CHARGER" */
41 static int max8998_battery_get_property(struct power_supply
*psy
,
42 enum power_supply_property psp
,
43 union power_supply_propval
*val
)
45 struct max8998_battery_data
*max8998
= container_of(psy
,
46 struct max8998_battery_data
, battery
);
47 struct i2c_client
*i2c
= max8998
->iodev
->i2c
;
52 case POWER_SUPPLY_PROP_PRESENT
:
53 ret
= max8998_read_reg(i2c
, MAX8998_REG_STATUS2
, ®
);
61 case POWER_SUPPLY_PROP_ONLINE
:
62 ret
= max8998_read_reg(i2c
, MAX8998_REG_STATUS2
, ®
);
77 static __devinit
int max8998_battery_probe(struct platform_device
*pdev
)
79 struct max8998_dev
*iodev
= dev_get_drvdata(pdev
->dev
.parent
);
80 struct max8998_platform_data
*pdata
= dev_get_platdata(iodev
->dev
);
81 struct max8998_battery_data
*max8998
;
82 struct i2c_client
*i2c
;
86 dev_err(pdev
->dev
.parent
, "No platform init data supplied\n");
90 max8998
= kzalloc(sizeof(struct max8998_battery_data
), GFP_KERNEL
);
94 max8998
->dev
= &pdev
->dev
;
95 max8998
->iodev
= iodev
;
96 platform_set_drvdata(pdev
, max8998
);
97 i2c
= max8998
->iodev
->i2c
;
99 /* Setup "End of Charge" */
100 /* If EOC value equals 0,
101 * remain value set from bootloader or default value */
102 if (pdata
->eoc
>= 10 && pdata
->eoc
<= 45) {
103 max8998_update_reg(i2c
, MAX8998_REG_CHGR1
,
104 (pdata
->eoc
/ 5 - 2) << 5, 0x7 << 5);
105 } else if (pdata
->eoc
== 0) {
106 dev_dbg(max8998
->dev
,
107 "EOC value not set: leave it unchanged.\n");
109 dev_err(max8998
->dev
, "Invalid EOC value\n");
114 /* Setup Charge Restart Level */
115 switch (pdata
->restart
) {
117 max8998_update_reg(i2c
, MAX8998_REG_CHGR1
, 0x1 << 3, 0x3 << 3);
120 max8998_update_reg(i2c
, MAX8998_REG_CHGR1
, 0x0 << 3, 0x3 << 3);
123 max8998_update_reg(i2c
, MAX8998_REG_CHGR1
, 0x2 << 3, 0x3 << 3);
126 max8998_update_reg(i2c
, MAX8998_REG_CHGR1
, 0x3 << 3, 0x3 << 3);
129 dev_dbg(max8998
->dev
,
130 "Restart Level not set: leave it unchanged.\n");
133 dev_err(max8998
->dev
, "Invalid Restart Level\n");
138 /* Setup Charge Full Timeout */
139 switch (pdata
->timeout
) {
141 max8998_update_reg(i2c
, MAX8998_REG_CHGR2
, 0x0 << 4, 0x3 << 4);
144 max8998_update_reg(i2c
, MAX8998_REG_CHGR2
, 0x1 << 4, 0x3 << 4);
147 max8998_update_reg(i2c
, MAX8998_REG_CHGR2
, 0x2 << 4, 0x3 << 4);
150 max8998_update_reg(i2c
, MAX8998_REG_CHGR2
, 0x3 << 4, 0x3 << 4);
153 dev_dbg(max8998
->dev
,
154 "Full Timeout not set: leave it unchanged.\n");
156 dev_err(max8998
->dev
, "Invalid Full Timeout value\n");
161 max8998
->battery
.name
= "max8998_pmic";
162 max8998
->battery
.type
= POWER_SUPPLY_TYPE_BATTERY
;
163 max8998
->battery
.get_property
= max8998_battery_get_property
;
164 max8998
->battery
.properties
= max8998_battery_props
;
165 max8998
->battery
.num_properties
= ARRAY_SIZE(max8998_battery_props
);
167 ret
= power_supply_register(max8998
->dev
, &max8998
->battery
);
169 dev_err(max8998
->dev
, "failed: power supply register\n");
179 static int __devexit
max8998_battery_remove(struct platform_device
*pdev
)
181 struct max8998_battery_data
*max8998
= platform_get_drvdata(pdev
);
183 power_supply_unregister(&max8998
->battery
);
189 static const struct platform_device_id max8998_battery_id
[] = {
190 { "max8998-battery", TYPE_MAX8998
},
193 static struct platform_driver max8998_battery_driver
= {
195 .name
= "max8998-battery",
196 .owner
= THIS_MODULE
,
198 .probe
= max8998_battery_probe
,
199 .remove
= __devexit_p(max8998_battery_remove
),
200 .id_table
= max8998_battery_id
,
203 static int __init
max8998_battery_init(void)
205 return platform_driver_register(&max8998_battery_driver
);
207 module_init(max8998_battery_init
);
209 static void __exit
max8998_battery_cleanup(void)
211 platform_driver_unregister(&max8998_battery_driver
);
213 module_exit(max8998_battery_cleanup
);
215 MODULE_DESCRIPTION("MAXIM 8998 battery control driver");
216 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
217 MODULE_LICENSE("GPL");
218 MODULE_ALIAS("platform:max8998-battery");