Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 1 | /* 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 | |
| 29 | struct 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; |
Balaji Rao | 9705ecc | 2009-01-27 19:23:12 +0530 | [diff] [blame^] | 39 | |
| 40 | struct delayed_work charging_restart_work; |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | int pcf50633_mbc_usb_curlim_set(struct pcf50633 *pcf, int ma) |
| 44 | { |
| 45 | struct pcf50633_mbc *mbc = platform_get_drvdata(pcf->mbc_pdev); |
| 46 | int ret = 0; |
| 47 | u8 bits; |
Balaji Rao | 9705ecc | 2009-01-27 19:23:12 +0530 | [diff] [blame^] | 48 | int charging_start = 1; |
| 49 | u8 mbcs2, chgmod; |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 50 | |
| 51 | if (ma >= 1000) |
| 52 | bits = PCF50633_MBCC7_USB_1000mA; |
| 53 | else if (ma >= 500) |
| 54 | bits = PCF50633_MBCC7_USB_500mA; |
| 55 | else if (ma >= 100) |
| 56 | bits = PCF50633_MBCC7_USB_100mA; |
Balaji Rao | 9705ecc | 2009-01-27 19:23:12 +0530 | [diff] [blame^] | 57 | else { |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 58 | bits = PCF50633_MBCC7_USB_SUSPEND; |
Balaji Rao | 9705ecc | 2009-01-27 19:23:12 +0530 | [diff] [blame^] | 59 | charging_start = 0; |
| 60 | } |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 61 | |
| 62 | ret = pcf50633_reg_set_bit_mask(pcf, PCF50633_REG_MBCC7, |
| 63 | PCF50633_MBCC7_USB_MASK, bits); |
| 64 | if (ret) |
| 65 | dev_err(pcf->dev, "error setting usb curlim to %d mA\n", ma); |
| 66 | else |
| 67 | dev_info(pcf->dev, "usb curlim to %d mA\n", ma); |
| 68 | |
Balaji Rao | 9705ecc | 2009-01-27 19:23:12 +0530 | [diff] [blame^] | 69 | /* Manual charging start */ |
| 70 | mbcs2 = pcf50633_reg_read(pcf, PCF50633_REG_MBCS2); |
| 71 | chgmod = (mbcs2 & PCF50633_MBCS2_MBC_MASK); |
| 72 | |
| 73 | /* If chgmod == BATFULL, setting chgena has no effect. |
| 74 | * We need to set resume instead. |
| 75 | */ |
| 76 | if (chgmod != PCF50633_MBCS2_MBC_BAT_FULL) |
| 77 | pcf50633_reg_set_bit_mask(pcf, PCF50633_REG_MBCC1, |
| 78 | PCF50633_MBCC1_CHGENA, PCF50633_MBCC1_CHGENA); |
| 79 | else |
| 80 | pcf50633_reg_set_bit_mask(pcf, PCF50633_REG_MBCC1, |
| 81 | PCF50633_MBCC1_RESUME, PCF50633_MBCC1_RESUME); |
| 82 | |
| 83 | mbc->usb_active = charging_start; |
| 84 | |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 85 | power_supply_changed(&mbc->usb); |
| 86 | |
| 87 | return ret; |
| 88 | } |
| 89 | EXPORT_SYMBOL_GPL(pcf50633_mbc_usb_curlim_set); |
| 90 | |
| 91 | int pcf50633_mbc_get_status(struct pcf50633 *pcf) |
| 92 | { |
| 93 | struct pcf50633_mbc *mbc = platform_get_drvdata(pcf->mbc_pdev); |
| 94 | int status = 0; |
| 95 | |
| 96 | if (mbc->usb_online) |
| 97 | status |= PCF50633_MBC_USB_ONLINE; |
| 98 | if (mbc->usb_active) |
| 99 | status |= PCF50633_MBC_USB_ACTIVE; |
| 100 | if (mbc->adapter_online) |
| 101 | status |= PCF50633_MBC_ADAPTER_ONLINE; |
| 102 | if (mbc->adapter_active) |
| 103 | status |= PCF50633_MBC_ADAPTER_ACTIVE; |
| 104 | |
| 105 | return status; |
| 106 | } |
| 107 | EXPORT_SYMBOL_GPL(pcf50633_mbc_get_status); |
| 108 | |
| 109 | void pcf50633_mbc_set_status(struct pcf50633 *pcf, int what, int status) |
| 110 | { |
| 111 | struct pcf50633_mbc *mbc = platform_get_drvdata(pcf->mbc_pdev); |
| 112 | |
| 113 | if (what & PCF50633_MBC_USB_ONLINE) |
| 114 | mbc->usb_online = !!status; |
| 115 | if (what & PCF50633_MBC_USB_ACTIVE) |
| 116 | mbc->usb_active = !!status; |
| 117 | if (what & PCF50633_MBC_ADAPTER_ONLINE) |
| 118 | mbc->adapter_online = !!status; |
| 119 | if (what & PCF50633_MBC_ADAPTER_ACTIVE) |
| 120 | mbc->adapter_active = !!status; |
| 121 | } |
| 122 | EXPORT_SYMBOL_GPL(pcf50633_mbc_set_status); |
| 123 | |
| 124 | static ssize_t |
| 125 | show_chgmode(struct device *dev, struct device_attribute *attr, char *buf) |
| 126 | { |
| 127 | struct pcf50633_mbc *mbc = dev_get_drvdata(dev); |
| 128 | |
| 129 | u8 mbcs2 = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCS2); |
| 130 | u8 chgmod = (mbcs2 & PCF50633_MBCS2_MBC_MASK); |
| 131 | |
| 132 | return sprintf(buf, "%d\n", chgmod); |
| 133 | } |
| 134 | static DEVICE_ATTR(chgmode, S_IRUGO, show_chgmode, NULL); |
| 135 | |
| 136 | static ssize_t |
| 137 | show_usblim(struct device *dev, struct device_attribute *attr, char *buf) |
| 138 | { |
| 139 | struct pcf50633_mbc *mbc = dev_get_drvdata(dev); |
| 140 | u8 usblim = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCC7) & |
| 141 | PCF50633_MBCC7_USB_MASK; |
| 142 | unsigned int ma; |
| 143 | |
| 144 | if (usblim == PCF50633_MBCC7_USB_1000mA) |
| 145 | ma = 1000; |
| 146 | else if (usblim == PCF50633_MBCC7_USB_500mA) |
| 147 | ma = 500; |
| 148 | else if (usblim == PCF50633_MBCC7_USB_100mA) |
| 149 | ma = 100; |
| 150 | else |
| 151 | ma = 0; |
| 152 | |
| 153 | return sprintf(buf, "%u\n", ma); |
| 154 | } |
| 155 | |
| 156 | static ssize_t set_usblim(struct device *dev, |
| 157 | struct device_attribute *attr, const char *buf, size_t count) |
| 158 | { |
| 159 | struct pcf50633_mbc *mbc = dev_get_drvdata(dev); |
| 160 | unsigned long ma; |
| 161 | int ret; |
| 162 | |
| 163 | ret = strict_strtoul(buf, 10, &ma); |
| 164 | if (ret) |
| 165 | return -EINVAL; |
| 166 | |
| 167 | pcf50633_mbc_usb_curlim_set(mbc->pcf, ma); |
| 168 | |
| 169 | return count; |
| 170 | } |
| 171 | |
| 172 | static DEVICE_ATTR(usb_curlim, S_IRUGO | S_IWUSR, show_usblim, set_usblim); |
| 173 | |
| 174 | static struct attribute *pcf50633_mbc_sysfs_entries[] = { |
| 175 | &dev_attr_chgmode.attr, |
| 176 | &dev_attr_usb_curlim.attr, |
| 177 | NULL, |
| 178 | }; |
| 179 | |
| 180 | static struct attribute_group mbc_attr_group = { |
| 181 | .name = NULL, /* put in device directory */ |
| 182 | .attrs = pcf50633_mbc_sysfs_entries, |
| 183 | }; |
| 184 | |
Balaji Rao | 9705ecc | 2009-01-27 19:23:12 +0530 | [diff] [blame^] | 185 | /* MBC state machine switches into charging mode when the battery voltage |
| 186 | * falls below 96% of a battery float voltage. But the voltage drop in Li-ion |
| 187 | * batteries is marginal(1~2 %) till about 80% of its capacity - which means, |
| 188 | * after a BATFULL, charging won't be restarted until 80%. |
| 189 | * |
| 190 | * This work_struct function restarts charging at regular intervals to make |
| 191 | * sure we don't discharge too much |
| 192 | */ |
| 193 | |
| 194 | static void pcf50633_mbc_charging_restart(struct work_struct *work) |
| 195 | { |
| 196 | struct pcf50633_mbc *mbc; |
| 197 | u8 mbcs2, chgmod; |
| 198 | |
| 199 | mbc = container_of(work, struct pcf50633_mbc, |
| 200 | charging_restart_work.work); |
| 201 | |
| 202 | mbcs2 = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCS2); |
| 203 | chgmod = (mbcs2 & PCF50633_MBCS2_MBC_MASK); |
| 204 | |
| 205 | if (chgmod != PCF50633_MBCS2_MBC_BAT_FULL) |
| 206 | return; |
| 207 | |
| 208 | /* Restart charging */ |
| 209 | pcf50633_reg_set_bit_mask(mbc->pcf, PCF50633_REG_MBCC1, |
| 210 | PCF50633_MBCC1_RESUME, PCF50633_MBCC1_RESUME); |
| 211 | mbc->usb_active = 1; |
| 212 | power_supply_changed(&mbc->usb); |
| 213 | |
| 214 | dev_info(mbc->pcf->dev, "Charging restarted\n"); |
| 215 | } |
| 216 | |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 217 | static void |
| 218 | pcf50633_mbc_irq_handler(int irq, void *data) |
| 219 | { |
| 220 | struct pcf50633_mbc *mbc = data; |
Balaji Rao | 9705ecc | 2009-01-27 19:23:12 +0530 | [diff] [blame^] | 221 | int chg_restart_interval = |
| 222 | mbc->pcf->pdata->charging_restart_interval; |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 223 | |
| 224 | /* USB */ |
| 225 | if (irq == PCF50633_IRQ_USBINS) { |
| 226 | mbc->usb_online = 1; |
| 227 | } else if (irq == PCF50633_IRQ_USBREM) { |
| 228 | mbc->usb_online = 0; |
| 229 | mbc->usb_active = 0; |
| 230 | pcf50633_mbc_usb_curlim_set(mbc->pcf, 0); |
Balaji Rao | 9705ecc | 2009-01-27 19:23:12 +0530 | [diff] [blame^] | 231 | cancel_delayed_work_sync(&mbc->charging_restart_work); |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | /* Adapter */ |
| 235 | if (irq == PCF50633_IRQ_ADPINS) { |
| 236 | mbc->adapter_online = 1; |
| 237 | mbc->adapter_active = 1; |
| 238 | } else if (irq == PCF50633_IRQ_ADPREM) { |
| 239 | mbc->adapter_online = 0; |
| 240 | mbc->adapter_active = 0; |
| 241 | } |
| 242 | |
| 243 | if (irq == PCF50633_IRQ_BATFULL) { |
| 244 | mbc->usb_active = 0; |
| 245 | mbc->adapter_active = 0; |
Balaji Rao | 9705ecc | 2009-01-27 19:23:12 +0530 | [diff] [blame^] | 246 | |
| 247 | if (chg_restart_interval > 0) |
| 248 | schedule_delayed_work(&mbc->charging_restart_work, |
| 249 | chg_restart_interval); |
| 250 | } else if (irq == PCF50633_IRQ_USBLIMON) |
| 251 | mbc->usb_active = 0; |
| 252 | else if (irq == PCF50633_IRQ_USBLIMOFF) |
| 253 | mbc->usb_active = 1; |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 254 | |
| 255 | power_supply_changed(&mbc->usb); |
| 256 | power_supply_changed(&mbc->adapter); |
| 257 | |
| 258 | if (mbc->pcf->pdata->mbc_event_callback) |
| 259 | mbc->pcf->pdata->mbc_event_callback(mbc->pcf, irq); |
| 260 | } |
| 261 | |
| 262 | static int adapter_get_property(struct power_supply *psy, |
| 263 | enum power_supply_property psp, |
| 264 | union power_supply_propval *val) |
| 265 | { |
Balaji Rao | ccc9c8b | 2009-01-27 19:22:38 +0530 | [diff] [blame] | 266 | struct pcf50633_mbc *mbc = container_of(psy, |
| 267 | struct pcf50633_mbc, adapter); |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 268 | int ret = 0; |
| 269 | |
| 270 | switch (psp) { |
| 271 | case POWER_SUPPLY_PROP_ONLINE: |
| 272 | val->intval = mbc->adapter_online; |
| 273 | break; |
| 274 | default: |
| 275 | ret = -EINVAL; |
| 276 | break; |
| 277 | } |
| 278 | return ret; |
| 279 | } |
| 280 | |
| 281 | static int usb_get_property(struct power_supply *psy, |
| 282 | enum power_supply_property psp, |
| 283 | union power_supply_propval *val) |
| 284 | { |
| 285 | struct pcf50633_mbc *mbc = container_of(psy, struct pcf50633_mbc, usb); |
| 286 | int ret = 0; |
| 287 | |
| 288 | switch (psp) { |
| 289 | case POWER_SUPPLY_PROP_ONLINE: |
| 290 | val->intval = mbc->usb_online; |
| 291 | break; |
| 292 | default: |
| 293 | ret = -EINVAL; |
| 294 | break; |
| 295 | } |
| 296 | return ret; |
| 297 | } |
| 298 | |
| 299 | static enum power_supply_property power_props[] = { |
| 300 | POWER_SUPPLY_PROP_ONLINE, |
| 301 | }; |
| 302 | |
| 303 | static const u8 mbc_irq_handlers[] = { |
| 304 | PCF50633_IRQ_ADPINS, |
| 305 | PCF50633_IRQ_ADPREM, |
| 306 | PCF50633_IRQ_USBINS, |
| 307 | PCF50633_IRQ_USBREM, |
| 308 | PCF50633_IRQ_BATFULL, |
| 309 | PCF50633_IRQ_CHGHALT, |
| 310 | PCF50633_IRQ_THLIMON, |
| 311 | PCF50633_IRQ_THLIMOFF, |
| 312 | PCF50633_IRQ_USBLIMON, |
| 313 | PCF50633_IRQ_USBLIMOFF, |
| 314 | PCF50633_IRQ_LOWSYS, |
| 315 | PCF50633_IRQ_LOWBAT, |
| 316 | }; |
| 317 | |
| 318 | static int __devinit pcf50633_mbc_probe(struct platform_device *pdev) |
| 319 | { |
| 320 | struct pcf50633_mbc *mbc; |
| 321 | struct pcf50633_subdev_pdata *pdata = pdev->dev.platform_data; |
| 322 | int ret; |
| 323 | int i; |
| 324 | u8 mbcs1; |
| 325 | |
| 326 | mbc = kzalloc(sizeof(*mbc), GFP_KERNEL); |
| 327 | if (!mbc) |
| 328 | return -ENOMEM; |
| 329 | |
| 330 | platform_set_drvdata(pdev, mbc); |
| 331 | mbc->pcf = pdata->pcf; |
| 332 | |
| 333 | /* Set up IRQ handlers */ |
| 334 | for (i = 0; i < ARRAY_SIZE(mbc_irq_handlers); i++) |
| 335 | pcf50633_register_irq(mbc->pcf, mbc_irq_handlers[i], |
| 336 | pcf50633_mbc_irq_handler, mbc); |
| 337 | |
| 338 | /* Create power supplies */ |
| 339 | mbc->adapter.name = "adapter"; |
| 340 | mbc->adapter.type = POWER_SUPPLY_TYPE_MAINS; |
| 341 | mbc->adapter.properties = power_props; |
| 342 | mbc->adapter.num_properties = ARRAY_SIZE(power_props); |
| 343 | mbc->adapter.get_property = &adapter_get_property; |
| 344 | mbc->adapter.supplied_to = mbc->pcf->pdata->batteries; |
| 345 | mbc->adapter.num_supplicants = mbc->pcf->pdata->num_batteries; |
| 346 | |
| 347 | mbc->usb.name = "usb"; |
| 348 | mbc->usb.type = POWER_SUPPLY_TYPE_USB; |
| 349 | mbc->usb.properties = power_props; |
| 350 | mbc->usb.num_properties = ARRAY_SIZE(power_props); |
| 351 | mbc->usb.get_property = usb_get_property; |
| 352 | mbc->usb.supplied_to = mbc->pcf->pdata->batteries; |
| 353 | mbc->usb.num_supplicants = mbc->pcf->pdata->num_batteries; |
| 354 | |
| 355 | ret = power_supply_register(&pdev->dev, &mbc->adapter); |
| 356 | if (ret) { |
| 357 | dev_err(mbc->pcf->dev, "failed to register adapter\n"); |
| 358 | kfree(mbc); |
| 359 | return ret; |
| 360 | } |
| 361 | |
| 362 | ret = power_supply_register(&pdev->dev, &mbc->usb); |
| 363 | if (ret) { |
| 364 | dev_err(mbc->pcf->dev, "failed to register usb\n"); |
| 365 | power_supply_unregister(&mbc->adapter); |
| 366 | kfree(mbc); |
| 367 | return ret; |
| 368 | } |
| 369 | |
Balaji Rao | 9705ecc | 2009-01-27 19:23:12 +0530 | [diff] [blame^] | 370 | INIT_DELAYED_WORK(&mbc->charging_restart_work, |
| 371 | pcf50633_mbc_charging_restart); |
| 372 | |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 373 | ret = sysfs_create_group(&pdev->dev.kobj, &mbc_attr_group); |
| 374 | if (ret) |
| 375 | dev_err(mbc->pcf->dev, "failed to create sysfs entries\n"); |
| 376 | |
| 377 | mbcs1 = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCS1); |
| 378 | if (mbcs1 & PCF50633_MBCS1_USBPRES) |
| 379 | pcf50633_mbc_irq_handler(PCF50633_IRQ_USBINS, mbc); |
| 380 | if (mbcs1 & PCF50633_MBCS1_ADAPTPRES) |
| 381 | pcf50633_mbc_irq_handler(PCF50633_IRQ_ADPINS, mbc); |
| 382 | |
| 383 | return 0; |
| 384 | } |
| 385 | |
| 386 | static int __devexit pcf50633_mbc_remove(struct platform_device *pdev) |
| 387 | { |
| 388 | struct pcf50633_mbc *mbc = platform_get_drvdata(pdev); |
| 389 | int i; |
| 390 | |
| 391 | /* Remove IRQ handlers */ |
| 392 | for (i = 0; i < ARRAY_SIZE(mbc_irq_handlers); i++) |
| 393 | pcf50633_free_irq(mbc->pcf, mbc_irq_handlers[i]); |
| 394 | |
| 395 | power_supply_unregister(&mbc->usb); |
| 396 | power_supply_unregister(&mbc->adapter); |
| 397 | |
Balaji Rao | 9705ecc | 2009-01-27 19:23:12 +0530 | [diff] [blame^] | 398 | cancel_delayed_work_sync(&mbc->charging_restart_work); |
| 399 | |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 400 | kfree(mbc); |
| 401 | |
| 402 | return 0; |
| 403 | } |
| 404 | |
| 405 | static struct platform_driver pcf50633_mbc_driver = { |
| 406 | .driver = { |
| 407 | .name = "pcf50633-mbc", |
| 408 | }, |
| 409 | .probe = pcf50633_mbc_probe, |
| 410 | .remove = __devexit_p(pcf50633_mbc_remove), |
| 411 | }; |
| 412 | |
| 413 | static int __init pcf50633_mbc_init(void) |
| 414 | { |
| 415 | return platform_driver_register(&pcf50633_mbc_driver); |
| 416 | } |
| 417 | module_init(pcf50633_mbc_init); |
| 418 | |
| 419 | static void __exit pcf50633_mbc_exit(void) |
| 420 | { |
| 421 | platform_driver_unregister(&pcf50633_mbc_driver); |
| 422 | } |
| 423 | module_exit(pcf50633_mbc_exit); |
| 424 | |
| 425 | MODULE_AUTHOR("Balaji Rao <balajirrao@openmoko.org>"); |
| 426 | MODULE_DESCRIPTION("PCF50633 mbc driver"); |
| 427 | MODULE_LICENSE("GPL"); |
| 428 | MODULE_ALIAS("platform:pcf50633-mbc"); |