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; |
Sean McNeil | 7677f33 | 2009-11-05 00:24:54 +0300 | [diff] [blame] | 39 | struct power_supply ac; |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | int pcf50633_mbc_usb_curlim_set(struct pcf50633 *pcf, int ma) |
| 43 | { |
| 44 | struct pcf50633_mbc *mbc = platform_get_drvdata(pcf->mbc_pdev); |
| 45 | int ret = 0; |
| 46 | u8 bits; |
Balaji Rao | 9705ecc | 2009-01-27 19:23:12 +0530 | [diff] [blame] | 47 | int charging_start = 1; |
| 48 | u8 mbcs2, chgmod; |
Balaji Rao | 31b4ff0 | 2009-11-05 00:24:55 +0300 | [diff] [blame] | 49 | unsigned int mbcc5; |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 50 | |
Balaji Rao | 31b4ff0 | 2009-11-05 00:24:55 +0300 | [diff] [blame] | 51 | if (ma >= 1000) { |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 52 | bits = PCF50633_MBCC7_USB_1000mA; |
Balaji Rao | 31b4ff0 | 2009-11-05 00:24:55 +0300 | [diff] [blame] | 53 | ma = 1000; |
| 54 | } else if (ma >= 500) { |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 55 | bits = PCF50633_MBCC7_USB_500mA; |
Balaji Rao | 31b4ff0 | 2009-11-05 00:24:55 +0300 | [diff] [blame] | 56 | ma = 500; |
| 57 | } else if (ma >= 100) { |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 58 | bits = PCF50633_MBCC7_USB_100mA; |
Balaji Rao | 31b4ff0 | 2009-11-05 00:24:55 +0300 | [diff] [blame] | 59 | ma = 100; |
| 60 | } else { |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 61 | bits = PCF50633_MBCC7_USB_SUSPEND; |
Balaji Rao | 9705ecc | 2009-01-27 19:23:12 +0530 | [diff] [blame] | 62 | charging_start = 0; |
Balaji Rao | 31b4ff0 | 2009-11-05 00:24:55 +0300 | [diff] [blame] | 63 | ma = 0; |
Balaji Rao | 9705ecc | 2009-01-27 19:23:12 +0530 | [diff] [blame] | 64 | } |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 65 | |
| 66 | ret = pcf50633_reg_set_bit_mask(pcf, PCF50633_REG_MBCC7, |
| 67 | PCF50633_MBCC7_USB_MASK, bits); |
| 68 | if (ret) |
| 69 | dev_err(pcf->dev, "error setting usb curlim to %d mA\n", ma); |
| 70 | else |
| 71 | dev_info(pcf->dev, "usb curlim to %d mA\n", ma); |
| 72 | |
Balaji Rao | 31b4ff0 | 2009-11-05 00:24:55 +0300 | [diff] [blame] | 73 | /* |
| 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 |
| 82 | */ |
| 83 | |
| 84 | if (mbc->pcf->pdata->charger_reference_current_ma) { |
| 85 | mbcc5 = (ma << 8) / mbc->pcf->pdata->charger_reference_current_ma; |
| 86 | if (mbcc5 > 255) |
| 87 | mbcc5 = 255; |
| 88 | pcf50633_reg_write(mbc->pcf, PCF50633_REG_MBCC5, mbcc5); |
| 89 | } |
| 90 | |
Balaji Rao | 9705ecc | 2009-01-27 19:23:12 +0530 | [diff] [blame] | 91 | mbcs2 = pcf50633_reg_read(pcf, PCF50633_REG_MBCS2); |
| 92 | chgmod = (mbcs2 & PCF50633_MBCS2_MBC_MASK); |
| 93 | |
| 94 | /* If chgmod == BATFULL, setting chgena has no effect. |
Paul Fertser | 1282b35 | 2009-11-05 00:24:58 +0300 | [diff] [blame^] | 95 | * Datasheet says we need to set resume instead but when autoresume is |
| 96 | * used resume doesn't work. Clear and set chgena instead. |
Balaji Rao | 9705ecc | 2009-01-27 19:23:12 +0530 | [diff] [blame] | 97 | */ |
| 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); |
Paul Fertser | 1282b35 | 2009-11-05 00:24:58 +0300 | [diff] [blame^] | 101 | else { |
| 102 | pcf50633_reg_clear_bits(pcf, PCF50633_REG_MBCC1, |
| 103 | PCF50633_MBCC1_CHGENA); |
Balaji Rao | 9705ecc | 2009-01-27 19:23:12 +0530 | [diff] [blame] | 104 | pcf50633_reg_set_bit_mask(pcf, PCF50633_REG_MBCC1, |
Paul Fertser | 1282b35 | 2009-11-05 00:24:58 +0300 | [diff] [blame^] | 105 | PCF50633_MBCC1_CHGENA, PCF50633_MBCC1_CHGENA); |
| 106 | } |
Balaji Rao | 9705ecc | 2009-01-27 19:23:12 +0530 | [diff] [blame] | 107 | |
| 108 | mbc->usb_active = charging_start; |
| 109 | |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 110 | power_supply_changed(&mbc->usb); |
| 111 | |
| 112 | return ret; |
| 113 | } |
| 114 | EXPORT_SYMBOL_GPL(pcf50633_mbc_usb_curlim_set); |
| 115 | |
| 116 | int pcf50633_mbc_get_status(struct pcf50633 *pcf) |
| 117 | { |
| 118 | struct pcf50633_mbc *mbc = platform_get_drvdata(pcf->mbc_pdev); |
| 119 | int status = 0; |
| 120 | |
| 121 | if (mbc->usb_online) |
| 122 | status |= PCF50633_MBC_USB_ONLINE; |
| 123 | if (mbc->usb_active) |
| 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; |
| 129 | |
| 130 | return status; |
| 131 | } |
| 132 | EXPORT_SYMBOL_GPL(pcf50633_mbc_get_status); |
| 133 | |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 134 | static ssize_t |
| 135 | show_chgmode(struct device *dev, struct device_attribute *attr, char *buf) |
| 136 | { |
| 137 | struct pcf50633_mbc *mbc = dev_get_drvdata(dev); |
| 138 | |
| 139 | u8 mbcs2 = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCS2); |
| 140 | u8 chgmod = (mbcs2 & PCF50633_MBCS2_MBC_MASK); |
| 141 | |
| 142 | return sprintf(buf, "%d\n", chgmod); |
| 143 | } |
| 144 | static DEVICE_ATTR(chgmode, S_IRUGO, show_chgmode, NULL); |
| 145 | |
| 146 | static ssize_t |
| 147 | show_usblim(struct device *dev, struct device_attribute *attr, char *buf) |
| 148 | { |
| 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; |
| 152 | unsigned int ma; |
| 153 | |
| 154 | if (usblim == PCF50633_MBCC7_USB_1000mA) |
| 155 | ma = 1000; |
| 156 | else if (usblim == PCF50633_MBCC7_USB_500mA) |
| 157 | ma = 500; |
| 158 | else if (usblim == PCF50633_MBCC7_USB_100mA) |
| 159 | ma = 100; |
| 160 | else |
| 161 | ma = 0; |
| 162 | |
| 163 | return sprintf(buf, "%u\n", ma); |
| 164 | } |
| 165 | |
| 166 | static ssize_t set_usblim(struct device *dev, |
| 167 | struct device_attribute *attr, const char *buf, size_t count) |
| 168 | { |
| 169 | struct pcf50633_mbc *mbc = dev_get_drvdata(dev); |
| 170 | unsigned long ma; |
| 171 | int ret; |
| 172 | |
| 173 | ret = strict_strtoul(buf, 10, &ma); |
| 174 | if (ret) |
| 175 | return -EINVAL; |
| 176 | |
| 177 | pcf50633_mbc_usb_curlim_set(mbc->pcf, ma); |
| 178 | |
| 179 | return count; |
| 180 | } |
| 181 | |
| 182 | static DEVICE_ATTR(usb_curlim, S_IRUGO | S_IWUSR, show_usblim, set_usblim); |
| 183 | |
Balaji Rao | 31b4ff0 | 2009-11-05 00:24:55 +0300 | [diff] [blame] | 184 | static ssize_t |
| 185 | show_chglim(struct device *dev, struct device_attribute *attr, char *buf) |
| 186 | { |
| 187 | struct pcf50633_mbc *mbc = dev_get_drvdata(dev); |
| 188 | u8 mbcc5 = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCC5); |
| 189 | unsigned int ma; |
| 190 | |
| 191 | if (!mbc->pcf->pdata->charger_reference_current_ma) |
| 192 | return -ENODEV; |
| 193 | |
| 194 | ma = (mbc->pcf->pdata->charger_reference_current_ma * mbcc5) >> 8; |
| 195 | |
| 196 | return sprintf(buf, "%u\n", ma); |
| 197 | } |
| 198 | |
| 199 | static ssize_t set_chglim(struct device *dev, |
| 200 | struct device_attribute *attr, const char *buf, size_t count) |
| 201 | { |
| 202 | struct pcf50633_mbc *mbc = dev_get_drvdata(dev); |
| 203 | unsigned long ma; |
| 204 | unsigned int mbcc5; |
| 205 | int ret; |
| 206 | |
| 207 | if (!mbc->pcf->pdata->charger_reference_current_ma) |
| 208 | return -ENODEV; |
| 209 | |
| 210 | ret = strict_strtoul(buf, 10, &ma); |
| 211 | if (ret) |
| 212 | return -EINVAL; |
| 213 | |
| 214 | mbcc5 = (ma << 8) / mbc->pcf->pdata->charger_reference_current_ma; |
| 215 | if (mbcc5 > 255) |
| 216 | mbcc5 = 255; |
| 217 | pcf50633_reg_write(mbc->pcf, PCF50633_REG_MBCC5, mbcc5); |
| 218 | |
| 219 | return count; |
| 220 | } |
| 221 | |
| 222 | /* |
| 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. |
| 226 | */ |
| 227 | static DEVICE_ATTR(chg_curlim, S_IRUGO | S_IWUSR, show_chglim, set_chglim); |
| 228 | |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 229 | static struct attribute *pcf50633_mbc_sysfs_entries[] = { |
| 230 | &dev_attr_chgmode.attr, |
| 231 | &dev_attr_usb_curlim.attr, |
Balaji Rao | 31b4ff0 | 2009-11-05 00:24:55 +0300 | [diff] [blame] | 232 | &dev_attr_chg_curlim.attr, |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 233 | NULL, |
| 234 | }; |
| 235 | |
| 236 | static struct attribute_group mbc_attr_group = { |
| 237 | .name = NULL, /* put in device directory */ |
| 238 | .attrs = pcf50633_mbc_sysfs_entries, |
| 239 | }; |
| 240 | |
| 241 | static void |
| 242 | pcf50633_mbc_irq_handler(int irq, void *data) |
| 243 | { |
| 244 | struct pcf50633_mbc *mbc = data; |
| 245 | |
| 246 | /* USB */ |
| 247 | if (irq == PCF50633_IRQ_USBINS) { |
| 248 | mbc->usb_online = 1; |
| 249 | } else if (irq == PCF50633_IRQ_USBREM) { |
| 250 | mbc->usb_online = 0; |
| 251 | mbc->usb_active = 0; |
| 252 | pcf50633_mbc_usb_curlim_set(mbc->pcf, 0); |
| 253 | } |
| 254 | |
| 255 | /* Adapter */ |
| 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; |
| 262 | } |
| 263 | |
| 264 | if (irq == PCF50633_IRQ_BATFULL) { |
| 265 | mbc->usb_active = 0; |
| 266 | mbc->adapter_active = 0; |
Balaji Rao | 9705ecc | 2009-01-27 19:23:12 +0530 | [diff] [blame] | 267 | } else if (irq == PCF50633_IRQ_USBLIMON) |
| 268 | mbc->usb_active = 0; |
| 269 | else if (irq == PCF50633_IRQ_USBLIMOFF) |
| 270 | mbc->usb_active = 1; |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 271 | |
Sean McNeil | 7677f33 | 2009-11-05 00:24:54 +0300 | [diff] [blame] | 272 | power_supply_changed(&mbc->ac); |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 273 | power_supply_changed(&mbc->usb); |
| 274 | power_supply_changed(&mbc->adapter); |
| 275 | |
| 276 | if (mbc->pcf->pdata->mbc_event_callback) |
| 277 | mbc->pcf->pdata->mbc_event_callback(mbc->pcf, irq); |
| 278 | } |
| 279 | |
| 280 | static int adapter_get_property(struct power_supply *psy, |
| 281 | enum power_supply_property psp, |
| 282 | union power_supply_propval *val) |
| 283 | { |
Balaji Rao | ccc9c8b | 2009-01-27 19:22:38 +0530 | [diff] [blame] | 284 | struct pcf50633_mbc *mbc = container_of(psy, |
| 285 | struct pcf50633_mbc, adapter); |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 286 | int ret = 0; |
| 287 | |
| 288 | switch (psp) { |
| 289 | case POWER_SUPPLY_PROP_ONLINE: |
| 290 | val->intval = mbc->adapter_online; |
| 291 | break; |
| 292 | default: |
| 293 | ret = -EINVAL; |
| 294 | break; |
| 295 | } |
| 296 | return ret; |
| 297 | } |
| 298 | |
| 299 | static int usb_get_property(struct power_supply *psy, |
| 300 | enum power_supply_property psp, |
| 301 | union power_supply_propval *val) |
| 302 | { |
| 303 | struct pcf50633_mbc *mbc = container_of(psy, struct pcf50633_mbc, usb); |
| 304 | int ret = 0; |
Sean McNeil | 7677f33 | 2009-11-05 00:24:54 +0300 | [diff] [blame] | 305 | u8 usblim = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCC7) & |
| 306 | PCF50633_MBCC7_USB_MASK; |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 307 | |
| 308 | switch (psp) { |
| 309 | case POWER_SUPPLY_PROP_ONLINE: |
Sean McNeil | 7677f33 | 2009-11-05 00:24:54 +0300 | [diff] [blame] | 310 | val->intval = mbc->usb_online && |
| 311 | (usblim <= PCF50633_MBCC7_USB_500mA); |
| 312 | break; |
| 313 | default: |
| 314 | ret = -EINVAL; |
| 315 | break; |
| 316 | } |
| 317 | return ret; |
| 318 | } |
| 319 | |
| 320 | static int ac_get_property(struct power_supply *psy, |
| 321 | enum power_supply_property psp, |
| 322 | union power_supply_propval *val) |
| 323 | { |
| 324 | struct pcf50633_mbc *mbc = container_of(psy, struct pcf50633_mbc, ac); |
| 325 | int ret = 0; |
| 326 | u8 usblim = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCC7) & |
| 327 | PCF50633_MBCC7_USB_MASK; |
| 328 | |
| 329 | switch (psp) { |
| 330 | case POWER_SUPPLY_PROP_ONLINE: |
| 331 | val->intval = mbc->usb_online && |
| 332 | (usblim == PCF50633_MBCC7_USB_1000mA); |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 333 | break; |
| 334 | default: |
| 335 | ret = -EINVAL; |
| 336 | break; |
| 337 | } |
| 338 | return ret; |
| 339 | } |
| 340 | |
| 341 | static enum power_supply_property power_props[] = { |
| 342 | POWER_SUPPLY_PROP_ONLINE, |
| 343 | }; |
| 344 | |
| 345 | static const u8 mbc_irq_handlers[] = { |
| 346 | PCF50633_IRQ_ADPINS, |
| 347 | PCF50633_IRQ_ADPREM, |
| 348 | PCF50633_IRQ_USBINS, |
| 349 | PCF50633_IRQ_USBREM, |
| 350 | PCF50633_IRQ_BATFULL, |
| 351 | PCF50633_IRQ_CHGHALT, |
| 352 | PCF50633_IRQ_THLIMON, |
| 353 | PCF50633_IRQ_THLIMOFF, |
| 354 | PCF50633_IRQ_USBLIMON, |
| 355 | PCF50633_IRQ_USBLIMOFF, |
| 356 | PCF50633_IRQ_LOWSYS, |
| 357 | PCF50633_IRQ_LOWBAT, |
| 358 | }; |
| 359 | |
| 360 | static int __devinit pcf50633_mbc_probe(struct platform_device *pdev) |
| 361 | { |
| 362 | struct pcf50633_mbc *mbc; |
| 363 | struct pcf50633_subdev_pdata *pdata = pdev->dev.platform_data; |
| 364 | int ret; |
| 365 | int i; |
| 366 | u8 mbcs1; |
| 367 | |
| 368 | mbc = kzalloc(sizeof(*mbc), GFP_KERNEL); |
| 369 | if (!mbc) |
| 370 | return -ENOMEM; |
| 371 | |
| 372 | platform_set_drvdata(pdev, mbc); |
| 373 | mbc->pcf = pdata->pcf; |
| 374 | |
| 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); |
| 379 | |
| 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; |
| 388 | |
| 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; |
| 396 | |
Sean McNeil | 7677f33 | 2009-11-05 00:24:54 +0300 | [diff] [blame] | 397 | mbc->ac.name = "ac"; |
| 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; |
| 404 | |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 405 | ret = power_supply_register(&pdev->dev, &mbc->adapter); |
| 406 | if (ret) { |
| 407 | dev_err(mbc->pcf->dev, "failed to register adapter\n"); |
| 408 | kfree(mbc); |
| 409 | return ret; |
| 410 | } |
| 411 | |
| 412 | ret = power_supply_register(&pdev->dev, &mbc->usb); |
| 413 | if (ret) { |
| 414 | dev_err(mbc->pcf->dev, "failed to register usb\n"); |
| 415 | power_supply_unregister(&mbc->adapter); |
| 416 | kfree(mbc); |
| 417 | return ret; |
| 418 | } |
| 419 | |
Sean McNeil | 7677f33 | 2009-11-05 00:24:54 +0300 | [diff] [blame] | 420 | ret = power_supply_register(&pdev->dev, &mbc->ac); |
| 421 | if (ret) { |
| 422 | dev_err(mbc->pcf->dev, "failed to register ac\n"); |
| 423 | power_supply_unregister(&mbc->adapter); |
| 424 | power_supply_unregister(&mbc->usb); |
| 425 | kfree(mbc); |
| 426 | return ret; |
| 427 | } |
| 428 | |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 429 | ret = sysfs_create_group(&pdev->dev.kobj, &mbc_attr_group); |
| 430 | if (ret) |
| 431 | dev_err(mbc->pcf->dev, "failed to create sysfs entries\n"); |
| 432 | |
| 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); |
| 438 | |
| 439 | return 0; |
| 440 | } |
| 441 | |
| 442 | static int __devexit pcf50633_mbc_remove(struct platform_device *pdev) |
| 443 | { |
| 444 | struct pcf50633_mbc *mbc = platform_get_drvdata(pdev); |
| 445 | int i; |
| 446 | |
| 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]); |
| 450 | |
| 451 | power_supply_unregister(&mbc->usb); |
| 452 | power_supply_unregister(&mbc->adapter); |
Sean McNeil | 7677f33 | 2009-11-05 00:24:54 +0300 | [diff] [blame] | 453 | power_supply_unregister(&mbc->ac); |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 454 | |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 455 | kfree(mbc); |
| 456 | |
| 457 | return 0; |
| 458 | } |
| 459 | |
| 460 | static struct platform_driver pcf50633_mbc_driver = { |
| 461 | .driver = { |
| 462 | .name = "pcf50633-mbc", |
| 463 | }, |
| 464 | .probe = pcf50633_mbc_probe, |
| 465 | .remove = __devexit_p(pcf50633_mbc_remove), |
| 466 | }; |
| 467 | |
| 468 | static int __init pcf50633_mbc_init(void) |
| 469 | { |
| 470 | return platform_driver_register(&pcf50633_mbc_driver); |
| 471 | } |
| 472 | module_init(pcf50633_mbc_init); |
| 473 | |
| 474 | static void __exit pcf50633_mbc_exit(void) |
| 475 | { |
| 476 | platform_driver_unregister(&pcf50633_mbc_driver); |
| 477 | } |
| 478 | module_exit(pcf50633_mbc_exit); |
| 479 | |
| 480 | MODULE_AUTHOR("Balaji Rao <balajirrao@openmoko.org>"); |
| 481 | MODULE_DESCRIPTION("PCF50633 mbc driver"); |
| 482 | MODULE_LICENSE("GPL"); |
| 483 | MODULE_ALIAS("platform:pcf50633-mbc"); |