blob: 21b6e64e78053a8a46582fbbe051158777f09923 [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 Rao9705ecc2009-01-27 19:23:12 +053040
41 struct delayed_work charging_restart_work;
Balaji Raof5714dc2009-01-09 01:50:55 +010042};
43
44int pcf50633_mbc_usb_curlim_set(struct pcf50633 *pcf, int ma)
45{
46 struct pcf50633_mbc *mbc = platform_get_drvdata(pcf->mbc_pdev);
47 int ret = 0;
48 u8 bits;
Balaji Rao9705ecc2009-01-27 19:23:12 +053049 int charging_start = 1;
50 u8 mbcs2, chgmod;
Balaji Rao31b4ff02009-11-05 00:24:55 +030051 unsigned int mbcc5;
Balaji Raof5714dc2009-01-09 01:50:55 +010052
Balaji Rao31b4ff02009-11-05 00:24:55 +030053 if (ma >= 1000) {
Balaji Raof5714dc2009-01-09 01:50:55 +010054 bits = PCF50633_MBCC7_USB_1000mA;
Balaji Rao31b4ff02009-11-05 00:24:55 +030055 ma = 1000;
56 } else if (ma >= 500) {
Balaji Raof5714dc2009-01-09 01:50:55 +010057 bits = PCF50633_MBCC7_USB_500mA;
Balaji Rao31b4ff02009-11-05 00:24:55 +030058 ma = 500;
59 } else if (ma >= 100) {
Balaji Raof5714dc2009-01-09 01:50:55 +010060 bits = PCF50633_MBCC7_USB_100mA;
Balaji Rao31b4ff02009-11-05 00:24:55 +030061 ma = 100;
62 } else {
Balaji Raof5714dc2009-01-09 01:50:55 +010063 bits = PCF50633_MBCC7_USB_SUSPEND;
Balaji Rao9705ecc2009-01-27 19:23:12 +053064 charging_start = 0;
Balaji Rao31b4ff02009-11-05 00:24:55 +030065 ma = 0;
Balaji Rao9705ecc2009-01-27 19:23:12 +053066 }
Balaji Raof5714dc2009-01-09 01:50:55 +010067
68 ret = pcf50633_reg_set_bit_mask(pcf, PCF50633_REG_MBCC7,
69 PCF50633_MBCC7_USB_MASK, bits);
70 if (ret)
71 dev_err(pcf->dev, "error setting usb curlim to %d mA\n", ma);
72 else
73 dev_info(pcf->dev, "usb curlim to %d mA\n", ma);
74
Balaji Rao31b4ff02009-11-05 00:24:55 +030075 /*
76 * We limit the charging current to be the USB current limit.
77 * The reason is that on pcf50633, when it enters PMU Standby mode,
78 * which it does when the device goes "off", the USB current limit
79 * reverts to the variant default. In at least one common case, that
80 * default is 500mA. By setting the charging current to be the same
81 * as the USB limit we set here before PMU standby, we enforce it only
82 * using the correct amount of current even when the USB current limit
83 * gets reset to the wrong thing
84 */
85
86 if (mbc->pcf->pdata->charger_reference_current_ma) {
87 mbcc5 = (ma << 8) / mbc->pcf->pdata->charger_reference_current_ma;
88 if (mbcc5 > 255)
89 mbcc5 = 255;
90 pcf50633_reg_write(mbc->pcf, PCF50633_REG_MBCC5, mbcc5);
91 }
92
Balaji Rao9705ecc2009-01-27 19:23:12 +053093 mbcs2 = pcf50633_reg_read(pcf, PCF50633_REG_MBCS2);
94 chgmod = (mbcs2 & PCF50633_MBCS2_MBC_MASK);
95
96 /* If chgmod == BATFULL, setting chgena has no effect.
97 * We need to set resume instead.
98 */
99 if (chgmod != PCF50633_MBCS2_MBC_BAT_FULL)
100 pcf50633_reg_set_bit_mask(pcf, PCF50633_REG_MBCC1,
101 PCF50633_MBCC1_CHGENA, PCF50633_MBCC1_CHGENA);
102 else
103 pcf50633_reg_set_bit_mask(pcf, PCF50633_REG_MBCC1,
104 PCF50633_MBCC1_RESUME, PCF50633_MBCC1_RESUME);
105
106 mbc->usb_active = charging_start;
107
Balaji Raof5714dc2009-01-09 01:50:55 +0100108 power_supply_changed(&mbc->usb);
109
110 return ret;
111}
112EXPORT_SYMBOL_GPL(pcf50633_mbc_usb_curlim_set);
113
114int pcf50633_mbc_get_status(struct pcf50633 *pcf)
115{
116 struct pcf50633_mbc *mbc = platform_get_drvdata(pcf->mbc_pdev);
117 int status = 0;
118
119 if (mbc->usb_online)
120 status |= PCF50633_MBC_USB_ONLINE;
121 if (mbc->usb_active)
122 status |= PCF50633_MBC_USB_ACTIVE;
123 if (mbc->adapter_online)
124 status |= PCF50633_MBC_ADAPTER_ONLINE;
125 if (mbc->adapter_active)
126 status |= PCF50633_MBC_ADAPTER_ACTIVE;
127
128 return status;
129}
130EXPORT_SYMBOL_GPL(pcf50633_mbc_get_status);
131
Balaji Raof5714dc2009-01-09 01:50:55 +0100132static ssize_t
133show_chgmode(struct device *dev, struct device_attribute *attr, char *buf)
134{
135 struct pcf50633_mbc *mbc = dev_get_drvdata(dev);
136
137 u8 mbcs2 = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCS2);
138 u8 chgmod = (mbcs2 & PCF50633_MBCS2_MBC_MASK);
139
140 return sprintf(buf, "%d\n", chgmod);
141}
142static DEVICE_ATTR(chgmode, S_IRUGO, show_chgmode, NULL);
143
144static ssize_t
145show_usblim(struct device *dev, struct device_attribute *attr, char *buf)
146{
147 struct pcf50633_mbc *mbc = dev_get_drvdata(dev);
148 u8 usblim = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCC7) &
149 PCF50633_MBCC7_USB_MASK;
150 unsigned int ma;
151
152 if (usblim == PCF50633_MBCC7_USB_1000mA)
153 ma = 1000;
154 else if (usblim == PCF50633_MBCC7_USB_500mA)
155 ma = 500;
156 else if (usblim == PCF50633_MBCC7_USB_100mA)
157 ma = 100;
158 else
159 ma = 0;
160
161 return sprintf(buf, "%u\n", ma);
162}
163
164static ssize_t set_usblim(struct device *dev,
165 struct device_attribute *attr, const char *buf, size_t count)
166{
167 struct pcf50633_mbc *mbc = dev_get_drvdata(dev);
168 unsigned long ma;
169 int ret;
170
171 ret = strict_strtoul(buf, 10, &ma);
172 if (ret)
173 return -EINVAL;
174
175 pcf50633_mbc_usb_curlim_set(mbc->pcf, ma);
176
177 return count;
178}
179
180static DEVICE_ATTR(usb_curlim, S_IRUGO | S_IWUSR, show_usblim, set_usblim);
181
Balaji Rao31b4ff02009-11-05 00:24:55 +0300182static ssize_t
183show_chglim(struct device *dev, struct device_attribute *attr, char *buf)
184{
185 struct pcf50633_mbc *mbc = dev_get_drvdata(dev);
186 u8 mbcc5 = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCC5);
187 unsigned int ma;
188
189 if (!mbc->pcf->pdata->charger_reference_current_ma)
190 return -ENODEV;
191
192 ma = (mbc->pcf->pdata->charger_reference_current_ma * mbcc5) >> 8;
193
194 return sprintf(buf, "%u\n", ma);
195}
196
197static ssize_t set_chglim(struct device *dev,
198 struct device_attribute *attr, const char *buf, size_t count)
199{
200 struct pcf50633_mbc *mbc = dev_get_drvdata(dev);
201 unsigned long ma;
202 unsigned int mbcc5;
203 int ret;
204
205 if (!mbc->pcf->pdata->charger_reference_current_ma)
206 return -ENODEV;
207
208 ret = strict_strtoul(buf, 10, &ma);
209 if (ret)
210 return -EINVAL;
211
212 mbcc5 = (ma << 8) / mbc->pcf->pdata->charger_reference_current_ma;
213 if (mbcc5 > 255)
214 mbcc5 = 255;
215 pcf50633_reg_write(mbc->pcf, PCF50633_REG_MBCC5, mbcc5);
216
217 return count;
218}
219
220/*
221 * This attribute allows to change MBC charging limit on the fly
222 * independently of usb current limit. It also gets set automatically every
223 * time usb current limit is changed.
224 */
225static DEVICE_ATTR(chg_curlim, S_IRUGO | S_IWUSR, show_chglim, set_chglim);
226
Balaji Raof5714dc2009-01-09 01:50:55 +0100227static struct attribute *pcf50633_mbc_sysfs_entries[] = {
228 &dev_attr_chgmode.attr,
229 &dev_attr_usb_curlim.attr,
Balaji Rao31b4ff02009-11-05 00:24:55 +0300230 &dev_attr_chg_curlim.attr,
Balaji Raof5714dc2009-01-09 01:50:55 +0100231 NULL,
232};
233
234static struct attribute_group mbc_attr_group = {
235 .name = NULL, /* put in device directory */
236 .attrs = pcf50633_mbc_sysfs_entries,
237};
238
Balaji Rao9705ecc2009-01-27 19:23:12 +0530239/* MBC state machine switches into charging mode when the battery voltage
240 * falls below 96% of a battery float voltage. But the voltage drop in Li-ion
241 * batteries is marginal(1~2 %) till about 80% of its capacity - which means,
242 * after a BATFULL, charging won't be restarted until 80%.
243 *
244 * This work_struct function restarts charging at regular intervals to make
245 * sure we don't discharge too much
246 */
247
248static void pcf50633_mbc_charging_restart(struct work_struct *work)
249{
250 struct pcf50633_mbc *mbc;
251 u8 mbcs2, chgmod;
252
253 mbc = container_of(work, struct pcf50633_mbc,
254 charging_restart_work.work);
255
256 mbcs2 = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCS2);
257 chgmod = (mbcs2 & PCF50633_MBCS2_MBC_MASK);
258
259 if (chgmod != PCF50633_MBCS2_MBC_BAT_FULL)
260 return;
261
262 /* Restart charging */
263 pcf50633_reg_set_bit_mask(mbc->pcf, PCF50633_REG_MBCC1,
264 PCF50633_MBCC1_RESUME, PCF50633_MBCC1_RESUME);
265 mbc->usb_active = 1;
266 power_supply_changed(&mbc->usb);
267
268 dev_info(mbc->pcf->dev, "Charging restarted\n");
269}
270
Balaji Raof5714dc2009-01-09 01:50:55 +0100271static void
272pcf50633_mbc_irq_handler(int irq, void *data)
273{
274 struct pcf50633_mbc *mbc = data;
Balaji Rao9705ecc2009-01-27 19:23:12 +0530275 int chg_restart_interval =
276 mbc->pcf->pdata->charging_restart_interval;
Balaji Raof5714dc2009-01-09 01:50:55 +0100277
278 /* USB */
279 if (irq == PCF50633_IRQ_USBINS) {
280 mbc->usb_online = 1;
281 } else if (irq == PCF50633_IRQ_USBREM) {
282 mbc->usb_online = 0;
283 mbc->usb_active = 0;
284 pcf50633_mbc_usb_curlim_set(mbc->pcf, 0);
Balaji Rao9705ecc2009-01-27 19:23:12 +0530285 cancel_delayed_work_sync(&mbc->charging_restart_work);
Balaji Raof5714dc2009-01-09 01:50:55 +0100286 }
287
288 /* Adapter */
289 if (irq == PCF50633_IRQ_ADPINS) {
290 mbc->adapter_online = 1;
291 mbc->adapter_active = 1;
292 } else if (irq == PCF50633_IRQ_ADPREM) {
293 mbc->adapter_online = 0;
294 mbc->adapter_active = 0;
295 }
296
297 if (irq == PCF50633_IRQ_BATFULL) {
298 mbc->usb_active = 0;
299 mbc->adapter_active = 0;
Balaji Rao9705ecc2009-01-27 19:23:12 +0530300
301 if (chg_restart_interval > 0)
302 schedule_delayed_work(&mbc->charging_restart_work,
303 chg_restart_interval);
304 } else if (irq == PCF50633_IRQ_USBLIMON)
305 mbc->usb_active = 0;
306 else if (irq == PCF50633_IRQ_USBLIMOFF)
307 mbc->usb_active = 1;
Balaji Raof5714dc2009-01-09 01:50:55 +0100308
Sean McNeil7677f332009-11-05 00:24:54 +0300309 power_supply_changed(&mbc->ac);
Balaji Raof5714dc2009-01-09 01:50:55 +0100310 power_supply_changed(&mbc->usb);
311 power_supply_changed(&mbc->adapter);
312
313 if (mbc->pcf->pdata->mbc_event_callback)
314 mbc->pcf->pdata->mbc_event_callback(mbc->pcf, irq);
315}
316
317static int adapter_get_property(struct power_supply *psy,
318 enum power_supply_property psp,
319 union power_supply_propval *val)
320{
Balaji Raoccc9c8b2009-01-27 19:22:38 +0530321 struct pcf50633_mbc *mbc = container_of(psy,
322 struct pcf50633_mbc, adapter);
Balaji Raof5714dc2009-01-09 01:50:55 +0100323 int ret = 0;
324
325 switch (psp) {
326 case POWER_SUPPLY_PROP_ONLINE:
327 val->intval = mbc->adapter_online;
328 break;
329 default:
330 ret = -EINVAL;
331 break;
332 }
333 return ret;
334}
335
336static int usb_get_property(struct power_supply *psy,
337 enum power_supply_property psp,
338 union power_supply_propval *val)
339{
340 struct pcf50633_mbc *mbc = container_of(psy, struct pcf50633_mbc, usb);
341 int ret = 0;
Sean McNeil7677f332009-11-05 00:24:54 +0300342 u8 usblim = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCC7) &
343 PCF50633_MBCC7_USB_MASK;
Balaji Raof5714dc2009-01-09 01:50:55 +0100344
345 switch (psp) {
346 case POWER_SUPPLY_PROP_ONLINE:
Sean McNeil7677f332009-11-05 00:24:54 +0300347 val->intval = mbc->usb_online &&
348 (usblim <= PCF50633_MBCC7_USB_500mA);
349 break;
350 default:
351 ret = -EINVAL;
352 break;
353 }
354 return ret;
355}
356
357static int ac_get_property(struct power_supply *psy,
358 enum power_supply_property psp,
359 union power_supply_propval *val)
360{
361 struct pcf50633_mbc *mbc = container_of(psy, struct pcf50633_mbc, ac);
362 int ret = 0;
363 u8 usblim = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCC7) &
364 PCF50633_MBCC7_USB_MASK;
365
366 switch (psp) {
367 case POWER_SUPPLY_PROP_ONLINE:
368 val->intval = mbc->usb_online &&
369 (usblim == PCF50633_MBCC7_USB_1000mA);
Balaji Raof5714dc2009-01-09 01:50:55 +0100370 break;
371 default:
372 ret = -EINVAL;
373 break;
374 }
375 return ret;
376}
377
378static enum power_supply_property power_props[] = {
379 POWER_SUPPLY_PROP_ONLINE,
380};
381
382static const u8 mbc_irq_handlers[] = {
383 PCF50633_IRQ_ADPINS,
384 PCF50633_IRQ_ADPREM,
385 PCF50633_IRQ_USBINS,
386 PCF50633_IRQ_USBREM,
387 PCF50633_IRQ_BATFULL,
388 PCF50633_IRQ_CHGHALT,
389 PCF50633_IRQ_THLIMON,
390 PCF50633_IRQ_THLIMOFF,
391 PCF50633_IRQ_USBLIMON,
392 PCF50633_IRQ_USBLIMOFF,
393 PCF50633_IRQ_LOWSYS,
394 PCF50633_IRQ_LOWBAT,
395};
396
397static int __devinit pcf50633_mbc_probe(struct platform_device *pdev)
398{
399 struct pcf50633_mbc *mbc;
400 struct pcf50633_subdev_pdata *pdata = pdev->dev.platform_data;
401 int ret;
402 int i;
403 u8 mbcs1;
404
405 mbc = kzalloc(sizeof(*mbc), GFP_KERNEL);
406 if (!mbc)
407 return -ENOMEM;
408
409 platform_set_drvdata(pdev, mbc);
410 mbc->pcf = pdata->pcf;
411
412 /* Set up IRQ handlers */
413 for (i = 0; i < ARRAY_SIZE(mbc_irq_handlers); i++)
414 pcf50633_register_irq(mbc->pcf, mbc_irq_handlers[i],
415 pcf50633_mbc_irq_handler, mbc);
416
417 /* Create power supplies */
418 mbc->adapter.name = "adapter";
419 mbc->adapter.type = POWER_SUPPLY_TYPE_MAINS;
420 mbc->adapter.properties = power_props;
421 mbc->adapter.num_properties = ARRAY_SIZE(power_props);
422 mbc->adapter.get_property = &adapter_get_property;
423 mbc->adapter.supplied_to = mbc->pcf->pdata->batteries;
424 mbc->adapter.num_supplicants = mbc->pcf->pdata->num_batteries;
425
426 mbc->usb.name = "usb";
427 mbc->usb.type = POWER_SUPPLY_TYPE_USB;
428 mbc->usb.properties = power_props;
429 mbc->usb.num_properties = ARRAY_SIZE(power_props);
430 mbc->usb.get_property = usb_get_property;
431 mbc->usb.supplied_to = mbc->pcf->pdata->batteries;
432 mbc->usb.num_supplicants = mbc->pcf->pdata->num_batteries;
433
Sean McNeil7677f332009-11-05 00:24:54 +0300434 mbc->ac.name = "ac";
435 mbc->ac.type = POWER_SUPPLY_TYPE_MAINS;
436 mbc->ac.properties = power_props;
437 mbc->ac.num_properties = ARRAY_SIZE(power_props);
438 mbc->ac.get_property = ac_get_property;
439 mbc->ac.supplied_to = mbc->pcf->pdata->batteries;
440 mbc->ac.num_supplicants = mbc->pcf->pdata->num_batteries;
441
Balaji Raof5714dc2009-01-09 01:50:55 +0100442 ret = power_supply_register(&pdev->dev, &mbc->adapter);
443 if (ret) {
444 dev_err(mbc->pcf->dev, "failed to register adapter\n");
445 kfree(mbc);
446 return ret;
447 }
448
449 ret = power_supply_register(&pdev->dev, &mbc->usb);
450 if (ret) {
451 dev_err(mbc->pcf->dev, "failed to register usb\n");
452 power_supply_unregister(&mbc->adapter);
453 kfree(mbc);
454 return ret;
455 }
456
Sean McNeil7677f332009-11-05 00:24:54 +0300457 ret = power_supply_register(&pdev->dev, &mbc->ac);
458 if (ret) {
459 dev_err(mbc->pcf->dev, "failed to register ac\n");
460 power_supply_unregister(&mbc->adapter);
461 power_supply_unregister(&mbc->usb);
462 kfree(mbc);
463 return ret;
464 }
465
Balaji Rao9705ecc2009-01-27 19:23:12 +0530466 INIT_DELAYED_WORK(&mbc->charging_restart_work,
467 pcf50633_mbc_charging_restart);
468
Balaji Raof5714dc2009-01-09 01:50:55 +0100469 ret = sysfs_create_group(&pdev->dev.kobj, &mbc_attr_group);
470 if (ret)
471 dev_err(mbc->pcf->dev, "failed to create sysfs entries\n");
472
473 mbcs1 = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCS1);
474 if (mbcs1 & PCF50633_MBCS1_USBPRES)
475 pcf50633_mbc_irq_handler(PCF50633_IRQ_USBINS, mbc);
476 if (mbcs1 & PCF50633_MBCS1_ADAPTPRES)
477 pcf50633_mbc_irq_handler(PCF50633_IRQ_ADPINS, mbc);
478
479 return 0;
480}
481
482static int __devexit pcf50633_mbc_remove(struct platform_device *pdev)
483{
484 struct pcf50633_mbc *mbc = platform_get_drvdata(pdev);
485 int i;
486
487 /* Remove IRQ handlers */
488 for (i = 0; i < ARRAY_SIZE(mbc_irq_handlers); i++)
489 pcf50633_free_irq(mbc->pcf, mbc_irq_handlers[i]);
490
491 power_supply_unregister(&mbc->usb);
492 power_supply_unregister(&mbc->adapter);
Sean McNeil7677f332009-11-05 00:24:54 +0300493 power_supply_unregister(&mbc->ac);
Balaji Raof5714dc2009-01-09 01:50:55 +0100494
Balaji Rao9705ecc2009-01-27 19:23:12 +0530495 cancel_delayed_work_sync(&mbc->charging_restart_work);
496
Balaji Raof5714dc2009-01-09 01:50:55 +0100497 kfree(mbc);
498
499 return 0;
500}
501
502static struct platform_driver pcf50633_mbc_driver = {
503 .driver = {
504 .name = "pcf50633-mbc",
505 },
506 .probe = pcf50633_mbc_probe,
507 .remove = __devexit_p(pcf50633_mbc_remove),
508};
509
510static int __init pcf50633_mbc_init(void)
511{
512 return platform_driver_register(&pcf50633_mbc_driver);
513}
514module_init(pcf50633_mbc_init);
515
516static void __exit pcf50633_mbc_exit(void)
517{
518 platform_driver_unregister(&pcf50633_mbc_driver);
519}
520module_exit(pcf50633_mbc_exit);
521
522MODULE_AUTHOR("Balaji Rao <balajirrao@openmoko.org>");
523MODULE_DESCRIPTION("PCF50633 mbc driver");
524MODULE_LICENSE("GPL");
525MODULE_ALIAS("platform:pcf50633-mbc");