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/module.h>
24 #include <linux/slab.h>
25 #include <linux/platform_device.h>
26 #include <linux/power_supply.h>
27 #include <linux/mfd/max8998.h>
28 #include <linux/mfd/max8998-private.h>
30 struct max8998_battery_data
{
32 struct max8998_dev
*iodev
;
33 struct power_supply
*battery
;
36 static enum power_supply_property max8998_battery_props
[] = {
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 max8998_battery_get_property(struct power_supply
*psy
,
43 enum power_supply_property psp
,
44 union power_supply_propval
*val
)
46 struct max8998_battery_data
*max8998
= power_supply_get_drvdata(psy
);
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 const struct power_supply_desc max8998_battery_desc
= {
78 .name
= "max8998_pmic",
79 .type
= POWER_SUPPLY_TYPE_BATTERY
,
80 .get_property
= max8998_battery_get_property
,
81 .properties
= max8998_battery_props
,
82 .num_properties
= ARRAY_SIZE(max8998_battery_props
),
85 static int max8998_battery_probe(struct platform_device
*pdev
)
87 struct max8998_dev
*iodev
= dev_get_drvdata(pdev
->dev
.parent
);
88 struct max8998_platform_data
*pdata
= dev_get_platdata(iodev
->dev
);
89 struct power_supply_config psy_cfg
= {};
90 struct max8998_battery_data
*max8998
;
91 struct i2c_client
*i2c
;
95 dev_err(pdev
->dev
.parent
, "No platform init data supplied\n");
99 max8998
= devm_kzalloc(&pdev
->dev
, sizeof(struct max8998_battery_data
),
104 max8998
->dev
= &pdev
->dev
;
105 max8998
->iodev
= iodev
;
106 platform_set_drvdata(pdev
, max8998
);
107 i2c
= max8998
->iodev
->i2c
;
109 /* Setup "End of Charge" */
110 /* If EOC value equals 0,
111 * remain value set from bootloader or default value */
112 if (pdata
->eoc
>= 10 && pdata
->eoc
<= 45) {
113 max8998_update_reg(i2c
, MAX8998_REG_CHGR1
,
114 (pdata
->eoc
/ 5 - 2) << 5, 0x7 << 5);
115 } else if (pdata
->eoc
== 0) {
116 dev_dbg(max8998
->dev
,
117 "EOC value not set: leave it unchanged.\n");
119 dev_err(max8998
->dev
, "Invalid EOC value\n");
123 /* Setup Charge Restart Level */
124 switch (pdata
->restart
) {
126 max8998_update_reg(i2c
, MAX8998_REG_CHGR1
, 0x1 << 3, 0x3 << 3);
129 max8998_update_reg(i2c
, MAX8998_REG_CHGR1
, 0x0 << 3, 0x3 << 3);
132 max8998_update_reg(i2c
, MAX8998_REG_CHGR1
, 0x2 << 3, 0x3 << 3);
135 max8998_update_reg(i2c
, MAX8998_REG_CHGR1
, 0x3 << 3, 0x3 << 3);
138 dev_dbg(max8998
->dev
,
139 "Restart Level not set: leave it unchanged.\n");
142 dev_err(max8998
->dev
, "Invalid Restart Level\n");
146 /* Setup Charge Full Timeout */
147 switch (pdata
->timeout
) {
149 max8998_update_reg(i2c
, MAX8998_REG_CHGR2
, 0x0 << 4, 0x3 << 4);
152 max8998_update_reg(i2c
, MAX8998_REG_CHGR2
, 0x1 << 4, 0x3 << 4);
155 max8998_update_reg(i2c
, MAX8998_REG_CHGR2
, 0x2 << 4, 0x3 << 4);
158 max8998_update_reg(i2c
, MAX8998_REG_CHGR2
, 0x3 << 4, 0x3 << 4);
161 dev_dbg(max8998
->dev
,
162 "Full Timeout not set: leave it unchanged.\n");
165 dev_err(max8998
->dev
, "Invalid Full Timeout value\n");
169 psy_cfg
.drv_data
= max8998
;
171 max8998
->battery
= devm_power_supply_register(max8998
->dev
,
172 &max8998_battery_desc
,
174 if (IS_ERR(max8998
->battery
)) {
175 ret
= PTR_ERR(max8998
->battery
);
176 dev_err(max8998
->dev
, "failed: power supply register: %d\n",
184 static const struct platform_device_id max8998_battery_id
[] = {
185 { "max8998-battery", TYPE_MAX8998
},
189 static struct platform_driver max8998_battery_driver
= {
191 .name
= "max8998-battery",
193 .probe
= max8998_battery_probe
,
194 .id_table
= max8998_battery_id
,
197 module_platform_driver(max8998_battery_driver
);
199 MODULE_DESCRIPTION("MAXIM 8998 battery control driver");
200 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
201 MODULE_LICENSE("GPL");
202 MODULE_ALIAS("platform:max8998-battery");