2 * max8998.c - Power supply consumer driver for the Maxim 8998
4 * Copyright (C) 2009-2010 Samsung Electronics
5 * MyungJoo Ham <myungjoo.ham@samsung.com>
7 * This program is not provided / owned by Maxim Integrated Products.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <linux/err.h>
25 #include <linux/slab.h>
26 #include <linux/platform_device.h>
27 #include <linux/power_supply.h>
28 #include <linux/mfd/max8998.h>
29 #include <linux/mfd/max8998-private.h>
31 struct max8998_battery_data {
33 struct max8998_dev *iodev;
34 unsigned int eoc_in_mA;
35 struct power_supply battery;
36 enum max8998_type device_id;
39 static enum power_supply_property max8998_battery_props[] = {
40 POWER_SUPPLY_PROP_MANUFACTURER,
41 POWER_SUPPLY_PROP_PRESENT, /* the presence of battery */
42 POWER_SUPPLY_PROP_ONLINE, /* charger is active or not */
45 static const char * const manufacturers[] = {
46 [TYPE_MAX8998] = "Maxim",
47 [TYPE_LP3974] = "National Semiconductor",
48 [TYPE_UNKNOWN] = "Unknown",
51 /* Note that the charger control is done by a current regulator "CHARGER" */
52 static int max8998_battery_get_property(struct power_supply *psy,
53 enum power_supply_property psp,
54 union power_supply_propval *val)
56 struct max8998_battery_data *max8998 = container_of(psy,
57 struct max8998_battery_data, battery);
58 struct i2c_client *i2c = max8998->iodev->i2c;
63 case POWER_SUPPLY_PROP_MANUFACTURER:
64 if (max8998->device_id == TYPE_MAX8998)
65 val->strval = manufacturers[TYPE_MAX8998];
66 else if (max8998->device_id == TYPE_LP3974)
67 val->strval = manufacturers[TYPE_LP3974];
69 val->strval = manufacturers[TYPE_UNKNOWN];
71 case POWER_SUPPLY_PROP_STATUS:
72 ret = max8998_read_reg(i2c, MAX8998_REG_STATUS2, ®);
76 val->intval = POWER_SUPPLY_STATUS_FULL;
80 case POWER_SUPPLY_PROP_PRESENT:
81 ret = max8998_read_reg(i2c, MAX8998_REG_STATUS2, ®);
89 case POWER_SUPPLY_PROP_ONLINE:
90 ret = max8998_read_reg(i2c, MAX8998_REG_STATUS2, ®);
95 /* Charger is in prequal or fast charge and not done */
96 if ((reg & (1 << 1)) || (reg & (1 << 2)))
97 if (!(reg & (1 << 6)))
107 static void _max8998_update_eoc(struct platform_device *pdev)
109 struct max8998_battery_data *max8998 = platform_get_drvdata(pdev);
110 struct i2c_client *i2c = max8998->iodev->i2c;
111 int charge_current = 0;
112 int target_eoc_ratio;
115 /* Nothing to do. User set EOC with % */
116 if (max8998->eoc_in_mA == 0)
119 /* Not initialized. */
120 if (!charger_current_map_desc)
123 if (max8998_read_reg(i2c, MAX8998_REG_CHGR1, &val))
126 charge_current = charger_current_map_desc[val & 0x07];
128 target_eoc_ratio = max8998->eoc_in_mA / charge_current * 100;
130 if (target_eoc_ratio < 10)
131 target_eoc_ratio = 10;
132 if (target_eoc_ratio > 45)
133 target_eoc_ratio = 45;
135 max8998_update_reg(i2c, MAX8998_REG_CHGR1,
136 (target_eoc_ratio / 5 - 2) << 5,
141 * max89998_update_eoc() sets EOC ratio. It uses
142 * the greatest EOC ratio that results in equal to or lower than the
143 * specified "eoc_in_mA" value. If no such value exists, it's 10%.
145 void max8998_update_eoc(struct max8998_dev *mdev)
148 _max8998_update_eoc(mdev->battery);
150 pr_warn("Device Not Probed, Yet (max8998_battery)\n");
153 static __devinit int max8998_battery_probe(struct platform_device *pdev)
155 struct max8998_dev *iodev = dev_get_drvdata(pdev->dev.parent);
156 struct max8998_platform_data *pdata = dev_get_platdata(iodev->dev);
157 struct max8998_battery_data *max8998;
158 struct i2c_client *i2c;
162 dev_err(pdev->dev.parent, "No platform init data supplied\n");
166 max8998 = kzalloc(sizeof(struct max8998_battery_data), GFP_KERNEL);
170 max8998->eoc_in_mA = 0;
171 max8998->dev = &pdev->dev;
172 max8998->iodev = iodev;
173 iodev->battery = pdev;
174 platform_set_drvdata(pdev, max8998);
175 i2c = max8998->iodev->i2c;
177 /* Setup "End of Charge" */
178 if (pdata->eoc >= 10 && pdata->eoc <= 45) {
179 max8998_update_reg(i2c, MAX8998_REG_CHGR1,
180 (pdata->eoc / 5 - 2) << 5, 0x7 << 5);
181 } else if (pdata->eoc >= 50) {
182 max8998->eoc_in_mA = pdata->eoc;
183 _max8998_update_eoc(pdev);
185 dev_info(max8998->dev, "EOC value not set: leave it unchanged.\n");
188 /* Setup Charge Restart Level */
189 switch (pdata->restart) {
191 max8998_update_reg(i2c, MAX8998_REG_CHGR1, 0x1 << 3, 0x3 << 3);
194 max8998_update_reg(i2c, MAX8998_REG_CHGR1, 0x0 << 3, 0x3 << 3);
197 max8998_update_reg(i2c, MAX8998_REG_CHGR1, 0x2 << 3, 0x3 << 3);
200 max8998_update_reg(i2c, MAX8998_REG_CHGR1, 0x3 << 3, 0x3 << 3);
203 dev_info(max8998->dev, "Restart Level not set: leave it unchanged.\n");
206 /* Setup Charge Full Timeout */
207 switch (pdata->timeout) {
209 max8998_update_reg(i2c, MAX8998_REG_CHGR2, 0x0 << 4, 0x3 << 4);
212 max8998_update_reg(i2c, MAX8998_REG_CHGR2, 0x1 << 4, 0x3 << 4);
215 max8998_update_reg(i2c, MAX8998_REG_CHGR2, 0x2 << 4, 0x3 << 4);
218 max8998_update_reg(i2c, MAX8998_REG_CHGR2, 0x3 << 4, 0x3 << 4);
221 dev_info(max8998->dev, "Full Timeout not set: leave it unchanged.\n");
224 max8998->battery.name = "max8998_pmic";
225 max8998->battery.type = POWER_SUPPLY_TYPE_BATTERY;
226 max8998->battery.get_property = max8998_battery_get_property;
227 max8998->battery.properties = max8998_battery_props;
228 max8998->battery.num_properties = ARRAY_SIZE(max8998_battery_props);
230 ret = power_supply_register(max8998->dev, &max8998->battery);
232 dev_err(max8998->dev, "failed: power supply register\n");
237 max8998->device_id = pdev->id_entry->driver_data;
242 static int __devexit max8998_battery_remove(struct platform_device *pdev)
244 struct max8998_battery_data *max8998 = platform_get_drvdata(pdev);
246 power_supply_unregister(&max8998->battery);
252 static const struct platform_device_id max8998_battery_id[] = {
253 { "max8998-battery", TYPE_MAX8998 },
254 { "lp3974-battery", TYPE_LP3974 },
257 static struct platform_driver max8998_battery_driver = {
259 .name = "max8998-battery",
260 .owner = THIS_MODULE,
262 .probe = max8998_battery_probe,
263 .remove = __devexit_p(max8998_battery_remove),
264 .id_table = max8998_battery_id,
267 static int __init max8998_battery_init(void)
269 return platform_driver_register(&max8998_battery_driver);
271 subsys_initcall(max8998_battery_init);
273 static void __exit max8998_battery_cleanup(void)
275 platform_driver_unregister(&max8998_battery_driver);
277 module_exit(max8998_battery_cleanup);
279 MODULE_DESCRIPTION("MAXIM 8998 battery control driver");
280 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
281 MODULE_LICENSE("GPL");