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/slab.h>
20 #include <linux/init.h>
21 #include <linux/types.h>
22 #include <linux/device.h>
23 #include <linux/sysfs.h>
24 #include <linux/platform_device.h>
25 #include <linux/power_supply.h>
27 #include <linux/mfd/pcf50633/core.h>
28 #include <linux/mfd/pcf50633/mbc.h>
36 struct power_supply
*usb
;
37 struct power_supply
*adapter
;
38 struct power_supply
*ac
;
41 int pcf50633_mbc_usb_curlim_set(struct pcf50633
*pcf
, int ma
)
43 struct pcf50633_mbc
*mbc
= platform_get_drvdata(pcf
->mbc_pdev
);
50 bits
= PCF50633_MBCC7_USB_1000mA
;
52 } else if (ma
>= 500) {
53 bits
= PCF50633_MBCC7_USB_500mA
;
55 } else if (ma
>= 100) {
56 bits
= PCF50633_MBCC7_USB_100mA
;
59 bits
= PCF50633_MBCC7_USB_SUSPEND
;
63 ret
= pcf50633_reg_set_bit_mask(pcf
, PCF50633_REG_MBCC7
,
64 PCF50633_MBCC7_USB_MASK
, bits
);
66 dev_err(pcf
->dev
, "error setting usb curlim to %d mA\n", ma
);
68 dev_info(pcf
->dev
, "usb curlim to %d mA\n", ma
);
71 * We limit the charging current to be the USB current limit.
72 * The reason is that on pcf50633, when it enters PMU Standby mode,
73 * which it does when the device goes "off", the USB current limit
74 * reverts to the variant default. In at least one common case, that
75 * default is 500mA. By setting the charging current to be the same
76 * as the USB limit we set here before PMU standby, we enforce it only
77 * using the correct amount of current even when the USB current limit
78 * gets reset to the wrong thing
81 if (mbc
->pcf
->pdata
->charger_reference_current_ma
) {
82 mbcc5
= (ma
<< 8) / mbc
->pcf
->pdata
->charger_reference_current_ma
;
85 pcf50633_reg_write(mbc
->pcf
, PCF50633_REG_MBCC5
, mbcc5
);
88 mbcs2
= pcf50633_reg_read(mbc
->pcf
, PCF50633_REG_MBCS2
);
89 chgmod
= (mbcs2
& PCF50633_MBCS2_MBC_MASK
);
91 /* If chgmod == BATFULL, setting chgena has no effect.
92 * Datasheet says we need to set resume instead but when autoresume is
93 * used resume doesn't work. Clear and set chgena instead.
95 if (chgmod
!= PCF50633_MBCS2_MBC_BAT_FULL
)
96 pcf50633_reg_set_bit_mask(pcf
, PCF50633_REG_MBCC1
,
97 PCF50633_MBCC1_CHGENA
, PCF50633_MBCC1_CHGENA
);
99 pcf50633_reg_clear_bits(pcf
, PCF50633_REG_MBCC1
,
100 PCF50633_MBCC1_CHGENA
);
101 pcf50633_reg_set_bit_mask(pcf
, PCF50633_REG_MBCC1
,
102 PCF50633_MBCC1_CHGENA
, PCF50633_MBCC1_CHGENA
);
105 power_supply_changed(mbc
->usb
);
109 EXPORT_SYMBOL_GPL(pcf50633_mbc_usb_curlim_set
);
111 int pcf50633_mbc_get_status(struct pcf50633
*pcf
)
113 struct pcf50633_mbc
*mbc
= platform_get_drvdata(pcf
->mbc_pdev
);
120 chgmod
= pcf50633_reg_read(mbc
->pcf
, PCF50633_REG_MBCS2
)
121 & PCF50633_MBCS2_MBC_MASK
;
124 status
|= PCF50633_MBC_USB_ONLINE
;
125 if (chgmod
== PCF50633_MBCS2_MBC_USB_PRE
||
126 chgmod
== PCF50633_MBCS2_MBC_USB_PRE_WAIT
||
127 chgmod
== PCF50633_MBCS2_MBC_USB_FAST
||
128 chgmod
== PCF50633_MBCS2_MBC_USB_FAST_WAIT
)
129 status
|= PCF50633_MBC_USB_ACTIVE
;
130 if (mbc
->adapter_online
)
131 status
|= PCF50633_MBC_ADAPTER_ONLINE
;
132 if (chgmod
== PCF50633_MBCS2_MBC_ADP_PRE
||
133 chgmod
== PCF50633_MBCS2_MBC_ADP_PRE_WAIT
||
134 chgmod
== PCF50633_MBCS2_MBC_ADP_FAST
||
135 chgmod
== PCF50633_MBCS2_MBC_ADP_FAST_WAIT
)
136 status
|= PCF50633_MBC_ADAPTER_ACTIVE
;
140 EXPORT_SYMBOL_GPL(pcf50633_mbc_get_status
);
142 int pcf50633_mbc_get_usb_online_status(struct pcf50633
*pcf
)
144 struct pcf50633_mbc
*mbc
= platform_get_drvdata(pcf
->mbc_pdev
);
149 return mbc
->usb_online
;
151 EXPORT_SYMBOL_GPL(pcf50633_mbc_get_usb_online_status
);
154 show_chgmode(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
156 struct pcf50633_mbc
*mbc
= dev_get_drvdata(dev
);
158 u8 mbcs2
= pcf50633_reg_read(mbc
->pcf
, PCF50633_REG_MBCS2
);
159 u8 chgmod
= (mbcs2
& PCF50633_MBCS2_MBC_MASK
);
161 return sprintf(buf
, "%d\n", chgmod
);
163 static DEVICE_ATTR(chgmode
, S_IRUGO
, show_chgmode
, NULL
);
166 show_usblim(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
168 struct pcf50633_mbc
*mbc
= dev_get_drvdata(dev
);
169 u8 usblim
= pcf50633_reg_read(mbc
->pcf
, PCF50633_REG_MBCC7
) &
170 PCF50633_MBCC7_USB_MASK
;
173 if (usblim
== PCF50633_MBCC7_USB_1000mA
)
175 else if (usblim
== PCF50633_MBCC7_USB_500mA
)
177 else if (usblim
== PCF50633_MBCC7_USB_100mA
)
182 return sprintf(buf
, "%u\n", ma
);
185 static ssize_t
set_usblim(struct device
*dev
,
186 struct device_attribute
*attr
, const char *buf
, size_t count
)
188 struct pcf50633_mbc
*mbc
= dev_get_drvdata(dev
);
192 ret
= kstrtoul(buf
, 10, &ma
);
196 pcf50633_mbc_usb_curlim_set(mbc
->pcf
, ma
);
201 static DEVICE_ATTR(usb_curlim
, S_IRUGO
| S_IWUSR
, show_usblim
, set_usblim
);
204 show_chglim(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
206 struct pcf50633_mbc
*mbc
= dev_get_drvdata(dev
);
207 u8 mbcc5
= pcf50633_reg_read(mbc
->pcf
, PCF50633_REG_MBCC5
);
210 if (!mbc
->pcf
->pdata
->charger_reference_current_ma
)
213 ma
= (mbc
->pcf
->pdata
->charger_reference_current_ma
* mbcc5
) >> 8;
215 return sprintf(buf
, "%u\n", ma
);
218 static ssize_t
set_chglim(struct device
*dev
,
219 struct device_attribute
*attr
, const char *buf
, size_t count
)
221 struct pcf50633_mbc
*mbc
= dev_get_drvdata(dev
);
226 if (!mbc
->pcf
->pdata
->charger_reference_current_ma
)
229 ret
= kstrtoul(buf
, 10, &ma
);
233 mbcc5
= (ma
<< 8) / mbc
->pcf
->pdata
->charger_reference_current_ma
;
236 pcf50633_reg_write(mbc
->pcf
, PCF50633_REG_MBCC5
, mbcc5
);
242 * This attribute allows to change MBC charging limit on the fly
243 * independently of usb current limit. It also gets set automatically every
244 * time usb current limit is changed.
246 static DEVICE_ATTR(chg_curlim
, S_IRUGO
| S_IWUSR
, show_chglim
, set_chglim
);
248 static struct attribute
*pcf50633_mbc_sysfs_entries
[] = {
249 &dev_attr_chgmode
.attr
,
250 &dev_attr_usb_curlim
.attr
,
251 &dev_attr_chg_curlim
.attr
,
255 static const struct attribute_group mbc_attr_group
= {
256 .name
= NULL
, /* put in device directory */
257 .attrs
= pcf50633_mbc_sysfs_entries
,
261 pcf50633_mbc_irq_handler(int irq
, void *data
)
263 struct pcf50633_mbc
*mbc
= data
;
266 if (irq
== PCF50633_IRQ_USBINS
) {
268 } else if (irq
== PCF50633_IRQ_USBREM
) {
270 pcf50633_mbc_usb_curlim_set(mbc
->pcf
, 0);
274 if (irq
== PCF50633_IRQ_ADPINS
)
275 mbc
->adapter_online
= 1;
276 else if (irq
== PCF50633_IRQ_ADPREM
)
277 mbc
->adapter_online
= 0;
279 power_supply_changed(mbc
->ac
);
280 power_supply_changed(mbc
->usb
);
281 power_supply_changed(mbc
->adapter
);
283 if (mbc
->pcf
->pdata
->mbc_event_callback
)
284 mbc
->pcf
->pdata
->mbc_event_callback(mbc
->pcf
, irq
);
287 static int adapter_get_property(struct power_supply
*psy
,
288 enum power_supply_property psp
,
289 union power_supply_propval
*val
)
291 struct pcf50633_mbc
*mbc
= power_supply_get_drvdata(psy
);
295 case POWER_SUPPLY_PROP_ONLINE
:
296 val
->intval
= mbc
->adapter_online
;
305 static int usb_get_property(struct power_supply
*psy
,
306 enum power_supply_property psp
,
307 union power_supply_propval
*val
)
309 struct pcf50633_mbc
*mbc
= power_supply_get_drvdata(psy
);
311 u8 usblim
= pcf50633_reg_read(mbc
->pcf
, PCF50633_REG_MBCC7
) &
312 PCF50633_MBCC7_USB_MASK
;
315 case POWER_SUPPLY_PROP_ONLINE
:
316 val
->intval
= mbc
->usb_online
&&
317 (usblim
<= PCF50633_MBCC7_USB_500mA
);
326 static int ac_get_property(struct power_supply
*psy
,
327 enum power_supply_property psp
,
328 union power_supply_propval
*val
)
330 struct pcf50633_mbc
*mbc
= power_supply_get_drvdata(psy
);
332 u8 usblim
= pcf50633_reg_read(mbc
->pcf
, PCF50633_REG_MBCC7
) &
333 PCF50633_MBCC7_USB_MASK
;
336 case POWER_SUPPLY_PROP_ONLINE
:
337 val
->intval
= mbc
->usb_online
&&
338 (usblim
== PCF50633_MBCC7_USB_1000mA
);
347 static enum power_supply_property power_props
[] = {
348 POWER_SUPPLY_PROP_ONLINE
,
351 static const u8 mbc_irq_handlers
[] = {
356 PCF50633_IRQ_BATFULL
,
357 PCF50633_IRQ_CHGHALT
,
358 PCF50633_IRQ_THLIMON
,
359 PCF50633_IRQ_THLIMOFF
,
360 PCF50633_IRQ_USBLIMON
,
361 PCF50633_IRQ_USBLIMOFF
,
366 static const struct power_supply_desc pcf50633_mbc_adapter_desc
= {
368 .type
= POWER_SUPPLY_TYPE_MAINS
,
369 .properties
= power_props
,
370 .num_properties
= ARRAY_SIZE(power_props
),
371 .get_property
= &adapter_get_property
,
374 static const struct power_supply_desc pcf50633_mbc_usb_desc
= {
376 .type
= POWER_SUPPLY_TYPE_USB
,
377 .properties
= power_props
,
378 .num_properties
= ARRAY_SIZE(power_props
),
379 .get_property
= usb_get_property
,
382 static const struct power_supply_desc pcf50633_mbc_ac_desc
= {
384 .type
= POWER_SUPPLY_TYPE_MAINS
,
385 .properties
= power_props
,
386 .num_properties
= ARRAY_SIZE(power_props
),
387 .get_property
= ac_get_property
,
390 static int pcf50633_mbc_probe(struct platform_device
*pdev
)
392 struct power_supply_config psy_cfg
= {};
393 struct pcf50633_mbc
*mbc
;
397 mbc
= devm_kzalloc(&pdev
->dev
, sizeof(*mbc
), GFP_KERNEL
);
401 platform_set_drvdata(pdev
, mbc
);
402 mbc
->pcf
= dev_to_pcf50633(pdev
->dev
.parent
);
404 /* Set up IRQ handlers */
405 for (i
= 0; i
< ARRAY_SIZE(mbc_irq_handlers
); i
++)
406 pcf50633_register_irq(mbc
->pcf
, mbc_irq_handlers
[i
],
407 pcf50633_mbc_irq_handler
, mbc
);
409 psy_cfg
.supplied_to
= mbc
->pcf
->pdata
->batteries
;
410 psy_cfg
.num_supplicants
= mbc
->pcf
->pdata
->num_batteries
;
411 psy_cfg
.drv_data
= mbc
;
413 /* Create power supplies */
414 mbc
->adapter
= power_supply_register(&pdev
->dev
,
415 &pcf50633_mbc_adapter_desc
,
417 if (IS_ERR(mbc
->adapter
)) {
418 dev_err(mbc
->pcf
->dev
, "failed to register adapter\n");
419 return PTR_ERR(mbc
->adapter
);
422 mbc
->usb
= power_supply_register(&pdev
->dev
, &pcf50633_mbc_usb_desc
,
424 if (IS_ERR(mbc
->usb
)) {
425 dev_err(mbc
->pcf
->dev
, "failed to register usb\n");
426 power_supply_unregister(mbc
->adapter
);
427 return PTR_ERR(mbc
->usb
);
430 mbc
->ac
= power_supply_register(&pdev
->dev
, &pcf50633_mbc_ac_desc
,
432 if (IS_ERR(mbc
->ac
)) {
433 dev_err(mbc
->pcf
->dev
, "failed to register ac\n");
434 power_supply_unregister(mbc
->adapter
);
435 power_supply_unregister(mbc
->usb
);
436 return PTR_ERR(mbc
->ac
);
439 if (sysfs_create_group(&pdev
->dev
.kobj
, &mbc_attr_group
))
440 dev_err(mbc
->pcf
->dev
, "failed to create sysfs entries\n");
442 mbcs1
= pcf50633_reg_read(mbc
->pcf
, PCF50633_REG_MBCS1
);
443 if (mbcs1
& PCF50633_MBCS1_USBPRES
)
444 pcf50633_mbc_irq_handler(PCF50633_IRQ_USBINS
, mbc
);
445 if (mbcs1
& PCF50633_MBCS1_ADAPTPRES
)
446 pcf50633_mbc_irq_handler(PCF50633_IRQ_ADPINS
, mbc
);
451 static int pcf50633_mbc_remove(struct platform_device
*pdev
)
453 struct pcf50633_mbc
*mbc
= platform_get_drvdata(pdev
);
456 /* Remove IRQ handlers */
457 for (i
= 0; i
< ARRAY_SIZE(mbc_irq_handlers
); i
++)
458 pcf50633_free_irq(mbc
->pcf
, mbc_irq_handlers
[i
]);
460 sysfs_remove_group(&pdev
->dev
.kobj
, &mbc_attr_group
);
461 power_supply_unregister(mbc
->usb
);
462 power_supply_unregister(mbc
->adapter
);
463 power_supply_unregister(mbc
->ac
);
468 static struct platform_driver pcf50633_mbc_driver
= {
470 .name
= "pcf50633-mbc",
472 .probe
= pcf50633_mbc_probe
,
473 .remove
= pcf50633_mbc_remove
,
476 module_platform_driver(pcf50633_mbc_driver
);
478 MODULE_AUTHOR("Balaji Rao <balajirrao@openmoko.org>");
479 MODULE_DESCRIPTION("PCF50633 mbc driver");
480 MODULE_LICENSE("GPL");
481 MODULE_ALIAS("platform:pcf50633-mbc");