1 /* NXP PCF50633 Main Battery Charger Driver
3 * (C) 2006-2008 by Openmoko, Inc.
4 * Author: Balaji Rao <balajirrao@openmoko.org>
7 * Broken down from monstrous PCF50633 driver mainly by
8 * Harald Welte, Andy Green and Werner Almesberger
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.
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>
26 #include <linux/mfd/pcf50633/core.h>
27 #include <linux/mfd/pcf50633/mbc.h>
37 struct power_supply usb;
38 struct power_supply adapter;
39 struct power_supply ac;
42 int pcf50633_mbc_usb_curlim_set(struct pcf50633 *pcf, int ma)
44 struct pcf50633_mbc *mbc = platform_get_drvdata(pcf->mbc_pdev);
47 int charging_start = 1;
52 bits = PCF50633_MBCC7_USB_1000mA;
54 } else if (ma >= 500) {
55 bits = PCF50633_MBCC7_USB_500mA;
57 } else if (ma >= 100) {
58 bits = PCF50633_MBCC7_USB_100mA;
61 bits = PCF50633_MBCC7_USB_SUSPEND;
66 ret = pcf50633_reg_set_bit_mask(pcf, PCF50633_REG_MBCC7,
67 PCF50633_MBCC7_USB_MASK, bits);
69 dev_err(pcf->dev, "error setting usb curlim to %d mA\n", ma);
71 dev_info(pcf->dev, "usb curlim to %d mA\n", ma);
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
84 if (mbc->pcf->pdata->charger_reference_current_ma) {
85 mbcc5 = (ma << 8) / mbc->pcf->pdata->charger_reference_current_ma;
88 pcf50633_reg_write(mbc->pcf, PCF50633_REG_MBCC5, mbcc5);
91 mbcs2 = pcf50633_reg_read(pcf, PCF50633_REG_MBCS2);
92 chgmod = (mbcs2 & PCF50633_MBCS2_MBC_MASK);
94 /* If chgmod == BATFULL, setting chgena has no effect.
95 * Datasheet says we need to set resume instead but when autoresume is
96 * used resume doesn't work. Clear and set chgena instead.
98 if (chgmod != PCF50633_MBCS2_MBC_BAT_FULL)
99 pcf50633_reg_set_bit_mask(pcf, PCF50633_REG_MBCC1,
100 PCF50633_MBCC1_CHGENA, PCF50633_MBCC1_CHGENA);
102 pcf50633_reg_clear_bits(pcf, PCF50633_REG_MBCC1,
103 PCF50633_MBCC1_CHGENA);
104 pcf50633_reg_set_bit_mask(pcf, PCF50633_REG_MBCC1,
105 PCF50633_MBCC1_CHGENA, PCF50633_MBCC1_CHGENA);
108 mbc->usb_active = charging_start;
110 power_supply_changed(&mbc->usb);
114 EXPORT_SYMBOL_GPL(pcf50633_mbc_usb_curlim_set);
116 int pcf50633_mbc_get_status(struct pcf50633 *pcf)
118 struct pcf50633_mbc *mbc = platform_get_drvdata(pcf->mbc_pdev);
122 status |= PCF50633_MBC_USB_ONLINE;
124 status |= PCF50633_MBC_USB_ACTIVE;
125 if (mbc->adapter_online)
126 status |= PCF50633_MBC_ADAPTER_ONLINE;
127 if (mbc->adapter_active)
128 status |= PCF50633_MBC_ADAPTER_ACTIVE;
132 EXPORT_SYMBOL_GPL(pcf50633_mbc_get_status);
135 show_chgmode(struct device *dev, struct device_attribute *attr, char *buf)
137 struct pcf50633_mbc *mbc = dev_get_drvdata(dev);
139 u8 mbcs2 = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCS2);
140 u8 chgmod = (mbcs2 & PCF50633_MBCS2_MBC_MASK);
142 return sprintf(buf, "%d\n", chgmod);
144 static DEVICE_ATTR(chgmode, S_IRUGO, show_chgmode, NULL);
147 show_usblim(struct device *dev, struct device_attribute *attr, char *buf)
149 struct pcf50633_mbc *mbc = dev_get_drvdata(dev);
150 u8 usblim = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCC7) &
151 PCF50633_MBCC7_USB_MASK;
154 if (usblim == PCF50633_MBCC7_USB_1000mA)
156 else if (usblim == PCF50633_MBCC7_USB_500mA)
158 else if (usblim == PCF50633_MBCC7_USB_100mA)
163 return sprintf(buf, "%u\n", ma);
166 static ssize_t set_usblim(struct device *dev,
167 struct device_attribute *attr, const char *buf, size_t count)
169 struct pcf50633_mbc *mbc = dev_get_drvdata(dev);
173 ret = strict_strtoul(buf, 10, &ma);
177 pcf50633_mbc_usb_curlim_set(mbc->pcf, ma);
182 static DEVICE_ATTR(usb_curlim, S_IRUGO | S_IWUSR, show_usblim, set_usblim);
185 show_chglim(struct device *dev, struct device_attribute *attr, char *buf)
187 struct pcf50633_mbc *mbc = dev_get_drvdata(dev);
188 u8 mbcc5 = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCC5);
191 if (!mbc->pcf->pdata->charger_reference_current_ma)
194 ma = (mbc->pcf->pdata->charger_reference_current_ma * mbcc5) >> 8;
196 return sprintf(buf, "%u\n", ma);
199 static ssize_t set_chglim(struct device *dev,
200 struct device_attribute *attr, const char *buf, size_t count)
202 struct pcf50633_mbc *mbc = dev_get_drvdata(dev);
207 if (!mbc->pcf->pdata->charger_reference_current_ma)
210 ret = strict_strtoul(buf, 10, &ma);
214 mbcc5 = (ma << 8) / mbc->pcf->pdata->charger_reference_current_ma;
217 pcf50633_reg_write(mbc->pcf, PCF50633_REG_MBCC5, mbcc5);
223 * This attribute allows to change MBC charging limit on the fly
224 * independently of usb current limit. It also gets set automatically every
225 * time usb current limit is changed.
227 static DEVICE_ATTR(chg_curlim, S_IRUGO | S_IWUSR, show_chglim, set_chglim);
229 static struct attribute *pcf50633_mbc_sysfs_entries[] = {
230 &dev_attr_chgmode.attr,
231 &dev_attr_usb_curlim.attr,
232 &dev_attr_chg_curlim.attr,
236 static struct attribute_group mbc_attr_group = {
237 .name = NULL, /* put in device directory */
238 .attrs = pcf50633_mbc_sysfs_entries,
242 pcf50633_mbc_irq_handler(int irq, void *data)
244 struct pcf50633_mbc *mbc = data;
247 if (irq == PCF50633_IRQ_USBINS) {
249 } else if (irq == PCF50633_IRQ_USBREM) {
252 pcf50633_mbc_usb_curlim_set(mbc->pcf, 0);
256 if (irq == PCF50633_IRQ_ADPINS) {
257 mbc->adapter_online = 1;
258 mbc->adapter_active = 1;
259 } else if (irq == PCF50633_IRQ_ADPREM) {
260 mbc->adapter_online = 0;
261 mbc->adapter_active = 0;
264 if (irq == PCF50633_IRQ_BATFULL) {
266 mbc->adapter_active = 0;
267 } else if (irq == PCF50633_IRQ_USBLIMON)
269 else if (irq == PCF50633_IRQ_USBLIMOFF)
272 power_supply_changed(&mbc->ac);
273 power_supply_changed(&mbc->usb);
274 power_supply_changed(&mbc->adapter);
276 if (mbc->pcf->pdata->mbc_event_callback)
277 mbc->pcf->pdata->mbc_event_callback(mbc->pcf, irq);
280 static int adapter_get_property(struct power_supply *psy,
281 enum power_supply_property psp,
282 union power_supply_propval *val)
284 struct pcf50633_mbc *mbc = container_of(psy,
285 struct pcf50633_mbc, adapter);
289 case POWER_SUPPLY_PROP_ONLINE:
290 val->intval = mbc->adapter_online;
299 static int usb_get_property(struct power_supply *psy,
300 enum power_supply_property psp,
301 union power_supply_propval *val)
303 struct pcf50633_mbc *mbc = container_of(psy, struct pcf50633_mbc, usb);
305 u8 usblim = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCC7) &
306 PCF50633_MBCC7_USB_MASK;
309 case POWER_SUPPLY_PROP_ONLINE:
310 val->intval = mbc->usb_online &&
311 (usblim <= PCF50633_MBCC7_USB_500mA);
320 static int ac_get_property(struct power_supply *psy,
321 enum power_supply_property psp,
322 union power_supply_propval *val)
324 struct pcf50633_mbc *mbc = container_of(psy, struct pcf50633_mbc, ac);
326 u8 usblim = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCC7) &
327 PCF50633_MBCC7_USB_MASK;
330 case POWER_SUPPLY_PROP_ONLINE:
331 val->intval = mbc->usb_online &&
332 (usblim == PCF50633_MBCC7_USB_1000mA);
341 static enum power_supply_property power_props[] = {
342 POWER_SUPPLY_PROP_ONLINE,
345 static const u8 mbc_irq_handlers[] = {
350 PCF50633_IRQ_BATFULL,
351 PCF50633_IRQ_CHGHALT,
352 PCF50633_IRQ_THLIMON,
353 PCF50633_IRQ_THLIMOFF,
354 PCF50633_IRQ_USBLIMON,
355 PCF50633_IRQ_USBLIMOFF,
360 static int __devinit pcf50633_mbc_probe(struct platform_device *pdev)
362 struct pcf50633_mbc *mbc;
363 struct pcf50633_subdev_pdata *pdata = pdev->dev.platform_data;
368 mbc = kzalloc(sizeof(*mbc), GFP_KERNEL);
372 platform_set_drvdata(pdev, mbc);
373 mbc->pcf = pdata->pcf;
375 /* Set up IRQ handlers */
376 for (i = 0; i < ARRAY_SIZE(mbc_irq_handlers); i++)
377 pcf50633_register_irq(mbc->pcf, mbc_irq_handlers[i],
378 pcf50633_mbc_irq_handler, mbc);
380 /* Create power supplies */
381 mbc->adapter.name = "adapter";
382 mbc->adapter.type = POWER_SUPPLY_TYPE_MAINS;
383 mbc->adapter.properties = power_props;
384 mbc->adapter.num_properties = ARRAY_SIZE(power_props);
385 mbc->adapter.get_property = &adapter_get_property;
386 mbc->adapter.supplied_to = mbc->pcf->pdata->batteries;
387 mbc->adapter.num_supplicants = mbc->pcf->pdata->num_batteries;
389 mbc->usb.name = "usb";
390 mbc->usb.type = POWER_SUPPLY_TYPE_USB;
391 mbc->usb.properties = power_props;
392 mbc->usb.num_properties = ARRAY_SIZE(power_props);
393 mbc->usb.get_property = usb_get_property;
394 mbc->usb.supplied_to = mbc->pcf->pdata->batteries;
395 mbc->usb.num_supplicants = mbc->pcf->pdata->num_batteries;
398 mbc->ac.type = POWER_SUPPLY_TYPE_MAINS;
399 mbc->ac.properties = power_props;
400 mbc->ac.num_properties = ARRAY_SIZE(power_props);
401 mbc->ac.get_property = ac_get_property;
402 mbc->ac.supplied_to = mbc->pcf->pdata->batteries;
403 mbc->ac.num_supplicants = mbc->pcf->pdata->num_batteries;
405 ret = power_supply_register(&pdev->dev, &mbc->adapter);
407 dev_err(mbc->pcf->dev, "failed to register adapter\n");
412 ret = power_supply_register(&pdev->dev, &mbc->usb);
414 dev_err(mbc->pcf->dev, "failed to register usb\n");
415 power_supply_unregister(&mbc->adapter);
420 ret = power_supply_register(&pdev->dev, &mbc->ac);
422 dev_err(mbc->pcf->dev, "failed to register ac\n");
423 power_supply_unregister(&mbc->adapter);
424 power_supply_unregister(&mbc->usb);
429 ret = sysfs_create_group(&pdev->dev.kobj, &mbc_attr_group);
431 dev_err(mbc->pcf->dev, "failed to create sysfs entries\n");
433 mbcs1 = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCS1);
434 if (mbcs1 & PCF50633_MBCS1_USBPRES)
435 pcf50633_mbc_irq_handler(PCF50633_IRQ_USBINS, mbc);
436 if (mbcs1 & PCF50633_MBCS1_ADAPTPRES)
437 pcf50633_mbc_irq_handler(PCF50633_IRQ_ADPINS, mbc);
442 static int __devexit pcf50633_mbc_remove(struct platform_device *pdev)
444 struct pcf50633_mbc *mbc = platform_get_drvdata(pdev);
447 /* Remove IRQ handlers */
448 for (i = 0; i < ARRAY_SIZE(mbc_irq_handlers); i++)
449 pcf50633_free_irq(mbc->pcf, mbc_irq_handlers[i]);
451 power_supply_unregister(&mbc->usb);
452 power_supply_unregister(&mbc->adapter);
453 power_supply_unregister(&mbc->ac);
460 static struct platform_driver pcf50633_mbc_driver = {
462 .name = "pcf50633-mbc",
464 .probe = pcf50633_mbc_probe,
465 .remove = __devexit_p(pcf50633_mbc_remove),
468 static int __init pcf50633_mbc_init(void)
470 return platform_driver_register(&pcf50633_mbc_driver);
472 module_init(pcf50633_mbc_init);
474 static void __exit pcf50633_mbc_exit(void)
476 platform_driver_unregister(&pcf50633_mbc_driver);
478 module_exit(pcf50633_mbc_exit);
480 MODULE_AUTHOR("Balaji Rao <balajirrao@openmoko.org>");
481 MODULE_DESCRIPTION("PCF50633 mbc driver");
482 MODULE_LICENSE("GPL");
483 MODULE_ALIAS("platform:pcf50633-mbc");