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 * We need to set resume instead.
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);
101 pcf50633_reg_set_bit_mask(pcf, PCF50633_REG_MBCC1,
102 PCF50633_MBCC1_RESUME, PCF50633_MBCC1_RESUME);
104 mbc->usb_active = charging_start;
106 power_supply_changed(&mbc->usb);
110 EXPORT_SYMBOL_GPL(pcf50633_mbc_usb_curlim_set);
112 int pcf50633_mbc_get_status(struct pcf50633 *pcf)
114 struct pcf50633_mbc *mbc = platform_get_drvdata(pcf->mbc_pdev);
118 status |= PCF50633_MBC_USB_ONLINE;
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;
128 EXPORT_SYMBOL_GPL(pcf50633_mbc_get_status);
131 show_chgmode(struct device *dev, struct device_attribute *attr, char *buf)
133 struct pcf50633_mbc *mbc = dev_get_drvdata(dev);
135 u8 mbcs2 = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCS2);
136 u8 chgmod = (mbcs2 & PCF50633_MBCS2_MBC_MASK);
138 return sprintf(buf, "%d\n", chgmod);
140 static DEVICE_ATTR(chgmode, S_IRUGO, show_chgmode, NULL);
143 show_usblim(struct device *dev, struct device_attribute *attr, char *buf)
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;
150 if (usblim == PCF50633_MBCC7_USB_1000mA)
152 else if (usblim == PCF50633_MBCC7_USB_500mA)
154 else if (usblim == PCF50633_MBCC7_USB_100mA)
159 return sprintf(buf, "%u\n", ma);
162 static ssize_t set_usblim(struct device *dev,
163 struct device_attribute *attr, const char *buf, size_t count)
165 struct pcf50633_mbc *mbc = dev_get_drvdata(dev);
169 ret = strict_strtoul(buf, 10, &ma);
173 pcf50633_mbc_usb_curlim_set(mbc->pcf, ma);
178 static DEVICE_ATTR(usb_curlim, S_IRUGO | S_IWUSR, show_usblim, set_usblim);
181 show_chglim(struct device *dev, struct device_attribute *attr, char *buf)
183 struct pcf50633_mbc *mbc = dev_get_drvdata(dev);
184 u8 mbcc5 = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCC5);
187 if (!mbc->pcf->pdata->charger_reference_current_ma)
190 ma = (mbc->pcf->pdata->charger_reference_current_ma * mbcc5) >> 8;
192 return sprintf(buf, "%u\n", ma);
195 static ssize_t set_chglim(struct device *dev,
196 struct device_attribute *attr, const char *buf, size_t count)
198 struct pcf50633_mbc *mbc = dev_get_drvdata(dev);
203 if (!mbc->pcf->pdata->charger_reference_current_ma)
206 ret = strict_strtoul(buf, 10, &ma);
210 mbcc5 = (ma << 8) / mbc->pcf->pdata->charger_reference_current_ma;
213 pcf50633_reg_write(mbc->pcf, PCF50633_REG_MBCC5, mbcc5);
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.
223 static DEVICE_ATTR(chg_curlim, S_IRUGO | S_IWUSR, show_chglim, set_chglim);
225 static struct attribute *pcf50633_mbc_sysfs_entries[] = {
226 &dev_attr_chgmode.attr,
227 &dev_attr_usb_curlim.attr,
228 &dev_attr_chg_curlim.attr,
232 static struct attribute_group mbc_attr_group = {
233 .name = NULL, /* put in device directory */
234 .attrs = pcf50633_mbc_sysfs_entries,
238 pcf50633_mbc_irq_handler(int irq, void *data)
240 struct pcf50633_mbc *mbc = data;
243 if (irq == PCF50633_IRQ_USBINS) {
245 } else if (irq == PCF50633_IRQ_USBREM) {
248 pcf50633_mbc_usb_curlim_set(mbc->pcf, 0);
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;
260 if (irq == PCF50633_IRQ_BATFULL) {
262 mbc->adapter_active = 0;
263 } else if (irq == PCF50633_IRQ_USBLIMON)
265 else if (irq == PCF50633_IRQ_USBLIMOFF)
268 power_supply_changed(&mbc->ac);
269 power_supply_changed(&mbc->usb);
270 power_supply_changed(&mbc->adapter);
272 if (mbc->pcf->pdata->mbc_event_callback)
273 mbc->pcf->pdata->mbc_event_callback(mbc->pcf, irq);
276 static int adapter_get_property(struct power_supply *psy,
277 enum power_supply_property psp,
278 union power_supply_propval *val)
280 struct pcf50633_mbc *mbc = container_of(psy,
281 struct pcf50633_mbc, adapter);
285 case POWER_SUPPLY_PROP_ONLINE:
286 val->intval = mbc->adapter_online;
295 static int usb_get_property(struct power_supply *psy,
296 enum power_supply_property psp,
297 union power_supply_propval *val)
299 struct pcf50633_mbc *mbc = container_of(psy, struct pcf50633_mbc, usb);
301 u8 usblim = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCC7) &
302 PCF50633_MBCC7_USB_MASK;
305 case POWER_SUPPLY_PROP_ONLINE:
306 val->intval = mbc->usb_online &&
307 (usblim <= PCF50633_MBCC7_USB_500mA);
316 static int ac_get_property(struct power_supply *psy,
317 enum power_supply_property psp,
318 union power_supply_propval *val)
320 struct pcf50633_mbc *mbc = container_of(psy, struct pcf50633_mbc, ac);
322 u8 usblim = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCC7) &
323 PCF50633_MBCC7_USB_MASK;
326 case POWER_SUPPLY_PROP_ONLINE:
327 val->intval = mbc->usb_online &&
328 (usblim == PCF50633_MBCC7_USB_1000mA);
337 static enum power_supply_property power_props[] = {
338 POWER_SUPPLY_PROP_ONLINE,
341 static const u8 mbc_irq_handlers[] = {
346 PCF50633_IRQ_BATFULL,
347 PCF50633_IRQ_CHGHALT,
348 PCF50633_IRQ_THLIMON,
349 PCF50633_IRQ_THLIMOFF,
350 PCF50633_IRQ_USBLIMON,
351 PCF50633_IRQ_USBLIMOFF,
356 static int __devinit pcf50633_mbc_probe(struct platform_device *pdev)
358 struct pcf50633_mbc *mbc;
359 struct pcf50633_subdev_pdata *pdata = pdev->dev.platform_data;
364 mbc = kzalloc(sizeof(*mbc), GFP_KERNEL);
368 platform_set_drvdata(pdev, mbc);
369 mbc->pcf = pdata->pcf;
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);
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;
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;
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;
401 ret = power_supply_register(&pdev->dev, &mbc->adapter);
403 dev_err(mbc->pcf->dev, "failed to register adapter\n");
408 ret = power_supply_register(&pdev->dev, &mbc->usb);
410 dev_err(mbc->pcf->dev, "failed to register usb\n");
411 power_supply_unregister(&mbc->adapter);
416 ret = power_supply_register(&pdev->dev, &mbc->ac);
418 dev_err(mbc->pcf->dev, "failed to register ac\n");
419 power_supply_unregister(&mbc->adapter);
420 power_supply_unregister(&mbc->usb);
425 ret = sysfs_create_group(&pdev->dev.kobj, &mbc_attr_group);
427 dev_err(mbc->pcf->dev, "failed to create sysfs entries\n");
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);
438 static int __devexit pcf50633_mbc_remove(struct platform_device *pdev)
440 struct pcf50633_mbc *mbc = platform_get_drvdata(pdev);
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]);
447 power_supply_unregister(&mbc->usb);
448 power_supply_unregister(&mbc->adapter);
449 power_supply_unregister(&mbc->ac);
456 static struct platform_driver pcf50633_mbc_driver = {
458 .name = "pcf50633-mbc",
460 .probe = pcf50633_mbc_probe,
461 .remove = __devexit_p(pcf50633_mbc_remove),
464 static int __init pcf50633_mbc_init(void)
466 return platform_driver_register(&pcf50633_mbc_driver);
468 module_init(pcf50633_mbc_init);
470 static void __exit pcf50633_mbc_exit(void)
472 platform_driver_unregister(&pcf50633_mbc_driver);
474 module_exit(pcf50633_mbc_exit);
476 MODULE_AUTHOR("Balaji Rao <balajirrao@openmoko.org>");
477 MODULE_DESCRIPTION("PCF50633 mbc driver");
478 MODULE_LICENSE("GPL");
479 MODULE_ALIAS("platform:pcf50633-mbc");