blob: 338311996eeacc1b111771d9f7f29dd14b0e0be9 [file] [log] [blame]
Balaji Raof5714dc2009-01-09 01:50:55 +01001/* NXP PCF50633 Main Battery Charger Driver
2 *
3 * (C) 2006-2008 by Openmoko, Inc.
4 * Author: Balaji Rao <balajirrao@openmoko.org>
5 * All rights reserved.
6 *
7 * Broken down from monstrous PCF50633 driver mainly by
8 * Harald Welte, Andy Green and Werner Almesberger
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
14 *
15 */
16
17#include <linux/kernel.h>
18#include <linux/module.h>
19#include <linux/init.h>
20#include <linux/types.h>
21#include <linux/device.h>
22#include <linux/sysfs.h>
23#include <linux/platform_device.h>
24#include <linux/power_supply.h>
25
26#include <linux/mfd/pcf50633/core.h>
27#include <linux/mfd/pcf50633/mbc.h>
28
29struct pcf50633_mbc {
30 struct pcf50633 *pcf;
31
32 int adapter_active;
33 int adapter_online;
34 int usb_active;
35 int usb_online;
36
37 struct power_supply usb;
38 struct power_supply adapter;
Sean McNeil7677f332009-11-05 00:24:54 +030039 struct power_supply ac;
Balaji Raof5714dc2009-01-09 01:50:55 +010040};
41
42int pcf50633_mbc_usb_curlim_set(struct pcf50633 *pcf, int ma)
43{
44 struct pcf50633_mbc *mbc = platform_get_drvdata(pcf->mbc_pdev);
45 int ret = 0;
46 u8 bits;
Balaji Rao9705ecc2009-01-27 19:23:12 +053047 int charging_start = 1;
48 u8 mbcs2, chgmod;
Balaji Rao31b4ff02009-11-05 00:24:55 +030049 unsigned int mbcc5;
Balaji Raof5714dc2009-01-09 01:50:55 +010050
Balaji Rao31b4ff02009-11-05 00:24:55 +030051 if (ma >= 1000) {
Balaji Raof5714dc2009-01-09 01:50:55 +010052 bits = PCF50633_MBCC7_USB_1000mA;
Balaji Rao31b4ff02009-11-05 00:24:55 +030053 ma = 1000;
54 } else if (ma >= 500) {
Balaji Raof5714dc2009-01-09 01:50:55 +010055 bits = PCF50633_MBCC7_USB_500mA;
Balaji Rao31b4ff02009-11-05 00:24:55 +030056 ma = 500;
57 } else if (ma >= 100) {
Balaji Raof5714dc2009-01-09 01:50:55 +010058 bits = PCF50633_MBCC7_USB_100mA;
Balaji Rao31b4ff02009-11-05 00:24:55 +030059 ma = 100;
60 } else {
Balaji Raof5714dc2009-01-09 01:50:55 +010061 bits = PCF50633_MBCC7_USB_SUSPEND;
Balaji Rao9705ecc2009-01-27 19:23:12 +053062 charging_start = 0;
Balaji Rao31b4ff02009-11-05 00:24:55 +030063 ma = 0;
Balaji Rao9705ecc2009-01-27 19:23:12 +053064 }
Balaji Raof5714dc2009-01-09 01:50:55 +010065
66 ret = pcf50633_reg_set_bit_mask(pcf, PCF50633_REG_MBCC7,
67 PCF50633_MBCC7_USB_MASK, bits);
68 if (ret)
69 dev_err(pcf->dev, "error setting usb curlim to %d mA\n", ma);
70 else
71 dev_info(pcf->dev, "usb curlim to %d mA\n", ma);
72
Balaji Rao31b4ff02009-11-05 00:24:55 +030073 /*
74 * We limit the charging current to be the USB current limit.
75 * The reason is that on pcf50633, when it enters PMU Standby mode,
76 * which it does when the device goes "off", the USB current limit
77 * reverts to the variant default. In at least one common case, that
78 * default is 500mA. By setting the charging current to be the same
79 * as the USB limit we set here before PMU standby, we enforce it only
80 * using the correct amount of current even when the USB current limit
81 * gets reset to the wrong thing
82 */
83
84 if (mbc->pcf->pdata->charger_reference_current_ma) {
85 mbcc5 = (ma << 8) / mbc->pcf->pdata->charger_reference_current_ma;
86 if (mbcc5 > 255)
87 mbcc5 = 255;
88 pcf50633_reg_write(mbc->pcf, PCF50633_REG_MBCC5, mbcc5);
89 }
90
Balaji Rao9705ecc2009-01-27 19:23:12 +053091 mbcs2 = pcf50633_reg_read(pcf, PCF50633_REG_MBCS2);
92 chgmod = (mbcs2 & PCF50633_MBCS2_MBC_MASK);
93
94 /* If chgmod == BATFULL, setting chgena has no effect.
95 * We need to set resume instead.
96 */
97 if (chgmod != PCF50633_MBCS2_MBC_BAT_FULL)
98 pcf50633_reg_set_bit_mask(pcf, PCF50633_REG_MBCC1,
99 PCF50633_MBCC1_CHGENA, PCF50633_MBCC1_CHGENA);
100 else
101 pcf50633_reg_set_bit_mask(pcf, PCF50633_REG_MBCC1,
102 PCF50633_MBCC1_RESUME, PCF50633_MBCC1_RESUME);
103
104 mbc->usb_active = charging_start;
105
Balaji Raof5714dc2009-01-09 01:50:55 +0100106 power_supply_changed(&mbc->usb);
107
108 return ret;
109}
110EXPORT_SYMBOL_GPL(pcf50633_mbc_usb_curlim_set);
111
112int pcf50633_mbc_get_status(struct pcf50633 *pcf)
113{
114 struct pcf50633_mbc *mbc = platform_get_drvdata(pcf->mbc_pdev);
115 int status = 0;
116
117 if (mbc->usb_online)
118 status |= PCF50633_MBC_USB_ONLINE;
119 if (mbc->usb_active)
120 status |= PCF50633_MBC_USB_ACTIVE;
121 if (mbc->adapter_online)
122 status |= PCF50633_MBC_ADAPTER_ONLINE;
123 if (mbc->adapter_active)
124 status |= PCF50633_MBC_ADAPTER_ACTIVE;
125
126 return status;
127}
128EXPORT_SYMBOL_GPL(pcf50633_mbc_get_status);
129
Balaji Raof5714dc2009-01-09 01:50:55 +0100130static ssize_t
131show_chgmode(struct device *dev, struct device_attribute *attr, char *buf)
132{
133 struct pcf50633_mbc *mbc = dev_get_drvdata(dev);
134
135 u8 mbcs2 = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCS2);
136 u8 chgmod = (mbcs2 & PCF50633_MBCS2_MBC_MASK);
137
138 return sprintf(buf, "%d\n", chgmod);
139}
140static DEVICE_ATTR(chgmode, S_IRUGO, show_chgmode, NULL);
141
142static ssize_t
143show_usblim(struct device *dev, struct device_attribute *attr, char *buf)
144{
145 struct pcf50633_mbc *mbc = dev_get_drvdata(dev);
146 u8 usblim = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCC7) &
147 PCF50633_MBCC7_USB_MASK;
148 unsigned int ma;
149
150 if (usblim == PCF50633_MBCC7_USB_1000mA)
151 ma = 1000;
152 else if (usblim == PCF50633_MBCC7_USB_500mA)
153 ma = 500;
154 else if (usblim == PCF50633_MBCC7_USB_100mA)
155 ma = 100;
156 else
157 ma = 0;
158
159 return sprintf(buf, "%u\n", ma);
160}
161
162static ssize_t set_usblim(struct device *dev,
163 struct device_attribute *attr, const char *buf, size_t count)
164{
165 struct pcf50633_mbc *mbc = dev_get_drvdata(dev);
166 unsigned long ma;
167 int ret;
168
169 ret = strict_strtoul(buf, 10, &ma);
170 if (ret)
171 return -EINVAL;
172
173 pcf50633_mbc_usb_curlim_set(mbc->pcf, ma);
174
175 return count;
176}
177
178static DEVICE_ATTR(usb_curlim, S_IRUGO | S_IWUSR, show_usblim, set_usblim);
179
Balaji Rao31b4ff02009-11-05 00:24:55 +0300180static ssize_t
181show_chglim(struct device *dev, struct device_attribute *attr, char *buf)
182{
183 struct pcf50633_mbc *mbc = dev_get_drvdata(dev);
184 u8 mbcc5 = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCC5);
185 unsigned int ma;
186
187 if (!mbc->pcf->pdata->charger_reference_current_ma)
188 return -ENODEV;
189
190 ma = (mbc->pcf->pdata->charger_reference_current_ma * mbcc5) >> 8;
191
192 return sprintf(buf, "%u\n", ma);
193}
194
195static ssize_t set_chglim(struct device *dev,
196 struct device_attribute *attr, const char *buf, size_t count)
197{
198 struct pcf50633_mbc *mbc = dev_get_drvdata(dev);
199 unsigned long ma;
200 unsigned int mbcc5;
201 int ret;
202
203 if (!mbc->pcf->pdata->charger_reference_current_ma)
204 return -ENODEV;
205
206 ret = strict_strtoul(buf, 10, &ma);
207 if (ret)
208 return -EINVAL;
209
210 mbcc5 = (ma << 8) / mbc->pcf->pdata->charger_reference_current_ma;
211 if (mbcc5 > 255)
212 mbcc5 = 255;
213 pcf50633_reg_write(mbc->pcf, PCF50633_REG_MBCC5, mbcc5);
214
215 return count;
216}
217
218/*
219 * This attribute allows to change MBC charging limit on the fly
220 * independently of usb current limit. It also gets set automatically every
221 * time usb current limit is changed.
222 */
223static DEVICE_ATTR(chg_curlim, S_IRUGO | S_IWUSR, show_chglim, set_chglim);
224
Balaji Raof5714dc2009-01-09 01:50:55 +0100225static struct attribute *pcf50633_mbc_sysfs_entries[] = {
226 &dev_attr_chgmode.attr,
227 &dev_attr_usb_curlim.attr,
Balaji Rao31b4ff02009-11-05 00:24:55 +0300228 &dev_attr_chg_curlim.attr,
Balaji Raof5714dc2009-01-09 01:50:55 +0100229 NULL,
230};
231
232static struct attribute_group mbc_attr_group = {
233 .name = NULL, /* put in device directory */
234 .attrs = pcf50633_mbc_sysfs_entries,
235};
236
237static void
238pcf50633_mbc_irq_handler(int irq, void *data)
239{
240 struct pcf50633_mbc *mbc = data;
241
242 /* USB */
243 if (irq == PCF50633_IRQ_USBINS) {
244 mbc->usb_online = 1;
245 } else if (irq == PCF50633_IRQ_USBREM) {
246 mbc->usb_online = 0;
247 mbc->usb_active = 0;
248 pcf50633_mbc_usb_curlim_set(mbc->pcf, 0);
249 }
250
251 /* Adapter */
252 if (irq == PCF50633_IRQ_ADPINS) {
253 mbc->adapter_online = 1;
254 mbc->adapter_active = 1;
255 } else if (irq == PCF50633_IRQ_ADPREM) {
256 mbc->adapter_online = 0;
257 mbc->adapter_active = 0;
258 }
259
260 if (irq == PCF50633_IRQ_BATFULL) {
261 mbc->usb_active = 0;
262 mbc->adapter_active = 0;
Balaji Rao9705ecc2009-01-27 19:23:12 +0530263 } else if (irq == PCF50633_IRQ_USBLIMON)
264 mbc->usb_active = 0;
265 else if (irq == PCF50633_IRQ_USBLIMOFF)
266 mbc->usb_active = 1;
Balaji Raof5714dc2009-01-09 01:50:55 +0100267
Sean McNeil7677f332009-11-05 00:24:54 +0300268 power_supply_changed(&mbc->ac);
Balaji Raof5714dc2009-01-09 01:50:55 +0100269 power_supply_changed(&mbc->usb);
270 power_supply_changed(&mbc->adapter);
271
272 if (mbc->pcf->pdata->mbc_event_callback)
273 mbc->pcf->pdata->mbc_event_callback(mbc->pcf, irq);
274}
275
276static int adapter_get_property(struct power_supply *psy,
277 enum power_supply_property psp,
278 union power_supply_propval *val)
279{
Balaji Raoccc9c8b2009-01-27 19:22:38 +0530280 struct pcf50633_mbc *mbc = container_of(psy,
281 struct pcf50633_mbc, adapter);
Balaji Raof5714dc2009-01-09 01:50:55 +0100282 int ret = 0;
283
284 switch (psp) {
285 case POWER_SUPPLY_PROP_ONLINE:
286 val->intval = mbc->adapter_online;
287 break;
288 default:
289 ret = -EINVAL;
290 break;
291 }
292 return ret;
293}
294
295static int usb_get_property(struct power_supply *psy,
296 enum power_supply_property psp,
297 union power_supply_propval *val)
298{
299 struct pcf50633_mbc *mbc = container_of(psy, struct pcf50633_mbc, usb);
300 int ret = 0;
Sean McNeil7677f332009-11-05 00:24:54 +0300301 u8 usblim = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCC7) &
302 PCF50633_MBCC7_USB_MASK;
Balaji Raof5714dc2009-01-09 01:50:55 +0100303
304 switch (psp) {
305 case POWER_SUPPLY_PROP_ONLINE:
Sean McNeil7677f332009-11-05 00:24:54 +0300306 val->intval = mbc->usb_online &&
307 (usblim <= PCF50633_MBCC7_USB_500mA);
308 break;
309 default:
310 ret = -EINVAL;
311 break;
312 }
313 return ret;
314}
315
316static int ac_get_property(struct power_supply *psy,
317 enum power_supply_property psp,
318 union power_supply_propval *val)
319{
320 struct pcf50633_mbc *mbc = container_of(psy, struct pcf50633_mbc, ac);
321 int ret = 0;
322 u8 usblim = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCC7) &
323 PCF50633_MBCC7_USB_MASK;
324
325 switch (psp) {
326 case POWER_SUPPLY_PROP_ONLINE:
327 val->intval = mbc->usb_online &&
328 (usblim == PCF50633_MBCC7_USB_1000mA);
Balaji Raof5714dc2009-01-09 01:50:55 +0100329 break;
330 default:
331 ret = -EINVAL;
332 break;
333 }
334 return ret;
335}
336
337static enum power_supply_property power_props[] = {
338 POWER_SUPPLY_PROP_ONLINE,
339};
340
341static const u8 mbc_irq_handlers[] = {
342 PCF50633_IRQ_ADPINS,
343 PCF50633_IRQ_ADPREM,
344 PCF50633_IRQ_USBINS,
345 PCF50633_IRQ_USBREM,
346 PCF50633_IRQ_BATFULL,
347 PCF50633_IRQ_CHGHALT,
348 PCF50633_IRQ_THLIMON,
349 PCF50633_IRQ_THLIMOFF,
350 PCF50633_IRQ_USBLIMON,
351 PCF50633_IRQ_USBLIMOFF,
352 PCF50633_IRQ_LOWSYS,
353 PCF50633_IRQ_LOWBAT,
354};
355
356static int __devinit pcf50633_mbc_probe(struct platform_device *pdev)
357{
358 struct pcf50633_mbc *mbc;
359 struct pcf50633_subdev_pdata *pdata = pdev->dev.platform_data;
360 int ret;
361 int i;
362 u8 mbcs1;
363
364 mbc = kzalloc(sizeof(*mbc), GFP_KERNEL);
365 if (!mbc)
366 return -ENOMEM;
367
368 platform_set_drvdata(pdev, mbc);
369 mbc->pcf = pdata->pcf;
370
371 /* Set up IRQ handlers */
372 for (i = 0; i < ARRAY_SIZE(mbc_irq_handlers); i++)
373 pcf50633_register_irq(mbc->pcf, mbc_irq_handlers[i],
374 pcf50633_mbc_irq_handler, mbc);
375
376 /* Create power supplies */
377 mbc->adapter.name = "adapter";
378 mbc->adapter.type = POWER_SUPPLY_TYPE_MAINS;
379 mbc->adapter.properties = power_props;
380 mbc->adapter.num_properties = ARRAY_SIZE(power_props);
381 mbc->adapter.get_property = &adapter_get_property;
382 mbc->adapter.supplied_to = mbc->pcf->pdata->batteries;
383 mbc->adapter.num_supplicants = mbc->pcf->pdata->num_batteries;
384
385 mbc->usb.name = "usb";
386 mbc->usb.type = POWER_SUPPLY_TYPE_USB;
387 mbc->usb.properties = power_props;
388 mbc->usb.num_properties = ARRAY_SIZE(power_props);
389 mbc->usb.get_property = usb_get_property;
390 mbc->usb.supplied_to = mbc->pcf->pdata->batteries;
391 mbc->usb.num_supplicants = mbc->pcf->pdata->num_batteries;
392
Sean McNeil7677f332009-11-05 00:24:54 +0300393 mbc->ac.name = "ac";
394 mbc->ac.type = POWER_SUPPLY_TYPE_MAINS;
395 mbc->ac.properties = power_props;
396 mbc->ac.num_properties = ARRAY_SIZE(power_props);
397 mbc->ac.get_property = ac_get_property;
398 mbc->ac.supplied_to = mbc->pcf->pdata->batteries;
399 mbc->ac.num_supplicants = mbc->pcf->pdata->num_batteries;
400
Balaji Raof5714dc2009-01-09 01:50:55 +0100401 ret = power_supply_register(&pdev->dev, &mbc->adapter);
402 if (ret) {
403 dev_err(mbc->pcf->dev, "failed to register adapter\n");
404 kfree(mbc);
405 return ret;
406 }
407
408 ret = power_supply_register(&pdev->dev, &mbc->usb);
409 if (ret) {
410 dev_err(mbc->pcf->dev, "failed to register usb\n");
411 power_supply_unregister(&mbc->adapter);
412 kfree(mbc);
413 return ret;
414 }
415
Sean McNeil7677f332009-11-05 00:24:54 +0300416 ret = power_supply_register(&pdev->dev, &mbc->ac);
417 if (ret) {
418 dev_err(mbc->pcf->dev, "failed to register ac\n");
419 power_supply_unregister(&mbc->adapter);
420 power_supply_unregister(&mbc->usb);
421 kfree(mbc);
422 return ret;
423 }
424
Balaji Raof5714dc2009-01-09 01:50:55 +0100425 ret = sysfs_create_group(&pdev->dev.kobj, &mbc_attr_group);
426 if (ret)
427 dev_err(mbc->pcf->dev, "failed to create sysfs entries\n");
428
429 mbcs1 = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCS1);
430 if (mbcs1 & PCF50633_MBCS1_USBPRES)
431 pcf50633_mbc_irq_handler(PCF50633_IRQ_USBINS, mbc);
432 if (mbcs1 & PCF50633_MBCS1_ADAPTPRES)
433 pcf50633_mbc_irq_handler(PCF50633_IRQ_ADPINS, mbc);
434
435 return 0;
436}
437
438static int __devexit pcf50633_mbc_remove(struct platform_device *pdev)
439{
440 struct pcf50633_mbc *mbc = platform_get_drvdata(pdev);
441 int i;
442
443 /* Remove IRQ handlers */
444 for (i = 0; i < ARRAY_SIZE(mbc_irq_handlers); i++)
445 pcf50633_free_irq(mbc->pcf, mbc_irq_handlers[i]);
446
447 power_supply_unregister(&mbc->usb);
448 power_supply_unregister(&mbc->adapter);
Sean McNeil7677f332009-11-05 00:24:54 +0300449 power_supply_unregister(&mbc->ac);
Balaji Raof5714dc2009-01-09 01:50:55 +0100450
Balaji Raof5714dc2009-01-09 01:50:55 +0100451 kfree(mbc);
452
453 return 0;
454}
455
456static struct platform_driver pcf50633_mbc_driver = {
457 .driver = {
458 .name = "pcf50633-mbc",
459 },
460 .probe = pcf50633_mbc_probe,
461 .remove = __devexit_p(pcf50633_mbc_remove),
462};
463
464static int __init pcf50633_mbc_init(void)
465{
466 return platform_driver_register(&pcf50633_mbc_driver);
467}
468module_init(pcf50633_mbc_init);
469
470static void __exit pcf50633_mbc_exit(void)
471{
472 platform_driver_unregister(&pcf50633_mbc_driver);
473}
474module_exit(pcf50633_mbc_exit);
475
476MODULE_AUTHOR("Balaji Rao <balajirrao@openmoko.org>");
477MODULE_DESCRIPTION("PCF50633 mbc driver");
478MODULE_LICENSE("GPL");
479MODULE_ALIAS("platform:pcf50633-mbc");