upload tizen1.0 source
[kernel/linux-2.6.36.git] / drivers / power / max8998.c
1 /*
2  * max8998.c - Power supply consumer driver for the Maxim 8998
3  *
4  *  Copyright (C) 2009-2010 Samsung Electronics
5  *  MyungJoo Ham <myungjoo.ham@samsung.com>
6  *
7  * This program is not provided / owned by Maxim Integrated Products.
8  *
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.
13  *
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.
18  *
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
22  */
23
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>
30
31 struct max8998_battery_data {
32         struct device *dev;
33         struct max8998_dev *iodev;
34         unsigned int eoc_in_mA;
35         struct power_supply battery;
36         enum max8998_type device_id;
37 };
38
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 */
43 };
44
45 static const char * const manufacturers[] = {
46         [TYPE_MAX8998] = "Maxim",
47         [TYPE_LP3974] = "National Semiconductor",
48         [TYPE_UNKNOWN] = "Unknown",
49 };
50
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)
55 {
56         struct max8998_battery_data *max8998 = container_of(psy,
57                         struct max8998_battery_data, battery);
58         struct i2c_client *i2c = max8998->iodev->i2c;
59         int ret;
60         u8 reg;
61
62         switch (psp) {
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];
68                 else
69                         val->strval = manufacturers[TYPE_UNKNOWN];
70                 break;
71         case POWER_SUPPLY_PROP_STATUS:
72                 ret = max8998_read_reg(i2c, MAX8998_REG_STATUS2, &reg);
73                 if (ret)
74                         return ret;
75                 if (reg & (1 << 0))
76                         val->intval = POWER_SUPPLY_STATUS_FULL;
77                 else
78                         val->intval = 0;
79                 break;
80         case POWER_SUPPLY_PROP_PRESENT:
81                 ret = max8998_read_reg(i2c, MAX8998_REG_STATUS2, &reg);
82                 if (ret)
83                         return ret;
84                 if (reg & (1 << 4))
85                         val->intval = 0;
86                 else
87                         val->intval = 1;
88                 break;
89         case POWER_SUPPLY_PROP_ONLINE:
90                 ret = max8998_read_reg(i2c, MAX8998_REG_STATUS2, &reg);
91                 if (ret)
92                         return ret;
93
94                 val->intval = 0;
95                 /* Charger is in prequal or fast charge and not done */
96                 if ((reg & (1 << 1)) || (reg & (1 << 2)))
97                         if (!(reg & (1 << 6)))
98                                 val->intval = 1;
99                 break;
100         default:
101                 return -EINVAL;
102         }
103
104         return 0;
105 }
106
107 static void _max8998_update_eoc(struct platform_device *pdev)
108 {
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;
113         u8 val;
114
115         /* Nothing to do. User set EOC with % */
116         if (max8998->eoc_in_mA == 0)
117                 return;
118
119         /* Not initialized. */
120         if (!charger_current_map_desc)
121                 return;
122
123         if (max8998_read_reg(i2c, MAX8998_REG_CHGR1, &val))
124                 return;
125
126         charge_current = charger_current_map_desc[val & 0x07];
127
128         target_eoc_ratio = max8998->eoc_in_mA / charge_current * 100;
129
130         if (target_eoc_ratio < 10)
131                 target_eoc_ratio = 10;
132         if (target_eoc_ratio > 45)
133                 target_eoc_ratio = 45;
134
135         max8998_update_reg(i2c, MAX8998_REG_CHGR1,
136                         (target_eoc_ratio / 5 - 2) << 5,
137                         0x7 << 5);
138 }
139
140 /*
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%.
144 **/
145 void max8998_update_eoc(struct max8998_dev *mdev)
146 {
147         if (mdev->battery)
148                 _max8998_update_eoc(mdev->battery);
149         else
150                 pr_warn("Device Not Probed, Yet (max8998_battery)\n");
151 }
152
153 static __devinit int max8998_battery_probe(struct platform_device *pdev)
154 {
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;
159         int ret = 0;
160
161         if (!pdata) {
162                 dev_err(pdev->dev.parent, "No platform init data supplied\n");
163                 return -ENODEV;
164         }
165
166         max8998 = kzalloc(sizeof(struct max8998_battery_data), GFP_KERNEL);
167         if (!max8998)
168                 return -ENOMEM;
169
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;
176
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);
184         } else {
185                 dev_info(max8998->dev, "EOC value not set: leave it unchanged.\n");
186         }
187
188         /* Setup Charge Restart Level */
189         switch (pdata->restart) {
190         case 100:
191                 max8998_update_reg(i2c, MAX8998_REG_CHGR1, 0x1 << 3, 0x3 << 3);
192                 break;
193         case 150:
194                 max8998_update_reg(i2c, MAX8998_REG_CHGR1, 0x0 << 3, 0x3 << 3);
195                 break;
196         case 200:
197                 max8998_update_reg(i2c, MAX8998_REG_CHGR1, 0x2 << 3, 0x3 << 3);
198                 break;
199         case -1:
200                 max8998_update_reg(i2c, MAX8998_REG_CHGR1, 0x3 << 3, 0x3 << 3);
201                 break;
202         default:
203                 dev_info(max8998->dev, "Restart Level not set: leave it unchanged.\n");
204         }
205
206         /* Setup Charge Full Timeout */
207         switch (pdata->timeout) {
208         case 5:
209                 max8998_update_reg(i2c, MAX8998_REG_CHGR2, 0x0 << 4, 0x3 << 4);
210                 break;
211         case 6:
212                 max8998_update_reg(i2c, MAX8998_REG_CHGR2, 0x1 << 4, 0x3 << 4);
213                 break;
214         case 7:
215                 max8998_update_reg(i2c, MAX8998_REG_CHGR2, 0x2 << 4, 0x3 << 4);
216                 break;
217         case -1:
218                 max8998_update_reg(i2c, MAX8998_REG_CHGR2, 0x3 << 4, 0x3 << 4);
219                 break;
220         default:
221                 dev_info(max8998->dev, "Full Timeout not set: leave it unchanged.\n");
222         }
223
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);
229
230         ret = power_supply_register(max8998->dev, &max8998->battery);
231         if (ret) {
232                 dev_err(max8998->dev, "failed: power supply register\n");
233                 kfree(max8998);
234                 return ret;
235         }
236
237         max8998->device_id = pdev->id_entry->driver_data;
238
239         return 0;
240 }
241
242 static int __devexit max8998_battery_remove(struct platform_device *pdev)
243 {
244         struct max8998_battery_data *max8998 = platform_get_drvdata(pdev);
245
246         power_supply_unregister(&max8998->battery);
247         kfree(max8998);
248
249         return 0;
250 }
251
252 static const struct platform_device_id max8998_battery_id[] = {
253         { "max8998-battery", TYPE_MAX8998 },
254         { "lp3974-battery", TYPE_LP3974 },
255 };
256
257 static struct platform_driver max8998_battery_driver = {
258         .driver = {
259                 .name = "max8998-battery",
260                 .owner = THIS_MODULE,
261         },
262         .probe = max8998_battery_probe,
263         .remove = __devexit_p(max8998_battery_remove),
264         .id_table = max8998_battery_id,
265 };
266
267 static int __init max8998_battery_init(void)
268 {
269         return platform_driver_register(&max8998_battery_driver);
270 }
271 subsys_initcall(max8998_battery_init);
272
273 static void __exit max8998_battery_cleanup(void)
274 {
275         platform_driver_unregister(&max8998_battery_driver);
276 }
277 module_exit(max8998_battery_cleanup);
278
279 MODULE_DESCRIPTION("MAXIM 8998 battery control driver");
280 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
281 MODULE_LICENSE("GPL");