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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 19 | #include <linux/slab.h> |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 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> |
| 26 | |
| 27 | #include <linux/mfd/pcf50633/core.h> |
| 28 | #include <linux/mfd/pcf50633/mbc.h> |
| 29 | |
| 30 | struct pcf50633_mbc { |
| 31 | struct pcf50633 *pcf; |
| 32 | |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 33 | int adapter_online; |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 34 | int usb_online; |
| 35 | |
| 36 | struct power_supply usb; |
| 37 | struct power_supply adapter; |
Sean McNeil | 7677f33 | 2009-11-05 00:24:54 +0300 | [diff] [blame] | 38 | struct power_supply ac; |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | int pcf50633_mbc_usb_curlim_set(struct pcf50633 *pcf, int ma) |
| 42 | { |
| 43 | struct pcf50633_mbc *mbc = platform_get_drvdata(pcf->mbc_pdev); |
| 44 | int ret = 0; |
| 45 | u8 bits; |
Balaji Rao | 9705ecc | 2009-01-27 19:23:12 +0530 | [diff] [blame] | 46 | int charging_start = 1; |
| 47 | u8 mbcs2, chgmod; |
Balaji Rao | 31b4ff0 | 2009-11-05 00:24:55 +0300 | [diff] [blame] | 48 | unsigned int mbcc5; |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 49 | |
Balaji Rao | 31b4ff0 | 2009-11-05 00:24:55 +0300 | [diff] [blame] | 50 | if (ma >= 1000) { |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 51 | bits = PCF50633_MBCC7_USB_1000mA; |
Balaji Rao | 31b4ff0 | 2009-11-05 00:24:55 +0300 | [diff] [blame] | 52 | ma = 1000; |
| 53 | } else if (ma >= 500) { |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 54 | bits = PCF50633_MBCC7_USB_500mA; |
Balaji Rao | 31b4ff0 | 2009-11-05 00:24:55 +0300 | [diff] [blame] | 55 | ma = 500; |
| 56 | } else if (ma >= 100) { |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 57 | bits = PCF50633_MBCC7_USB_100mA; |
Balaji Rao | 31b4ff0 | 2009-11-05 00:24:55 +0300 | [diff] [blame] | 58 | ma = 100; |
| 59 | } else { |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 60 | bits = PCF50633_MBCC7_USB_SUSPEND; |
Balaji Rao | 9705ecc | 2009-01-27 19:23:12 +0530 | [diff] [blame] | 61 | charging_start = 0; |
Balaji Rao | 31b4ff0 | 2009-11-05 00:24:55 +0300 | [diff] [blame] | 62 | ma = 0; |
Balaji Rao | 9705ecc | 2009-01-27 19:23:12 +0530 | [diff] [blame] | 63 | } |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 64 | |
| 65 | ret = pcf50633_reg_set_bit_mask(pcf, PCF50633_REG_MBCC7, |
| 66 | PCF50633_MBCC7_USB_MASK, bits); |
| 67 | if (ret) |
| 68 | dev_err(pcf->dev, "error setting usb curlim to %d mA\n", ma); |
| 69 | else |
| 70 | dev_info(pcf->dev, "usb curlim to %d mA\n", ma); |
| 71 | |
Balaji Rao | 31b4ff0 | 2009-11-05 00:24:55 +0300 | [diff] [blame] | 72 | /* |
| 73 | * We limit the charging current to be the USB current limit. |
| 74 | * The reason is that on pcf50633, when it enters PMU Standby mode, |
| 75 | * which it does when the device goes "off", the USB current limit |
| 76 | * reverts to the variant default. In at least one common case, that |
| 77 | * default is 500mA. By setting the charging current to be the same |
| 78 | * as the USB limit we set here before PMU standby, we enforce it only |
| 79 | * using the correct amount of current even when the USB current limit |
| 80 | * gets reset to the wrong thing |
| 81 | */ |
| 82 | |
| 83 | if (mbc->pcf->pdata->charger_reference_current_ma) { |
| 84 | mbcc5 = (ma << 8) / mbc->pcf->pdata->charger_reference_current_ma; |
| 85 | if (mbcc5 > 255) |
| 86 | mbcc5 = 255; |
| 87 | pcf50633_reg_write(mbc->pcf, PCF50633_REG_MBCC5, mbcc5); |
| 88 | } |
| 89 | |
Paul Fertser | c329795 | 2009-11-05 00:24:59 +0300 | [diff] [blame] | 90 | mbcs2 = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCS2); |
Balaji Rao | 9705ecc | 2009-01-27 19:23:12 +0530 | [diff] [blame] | 91 | chgmod = (mbcs2 & PCF50633_MBCS2_MBC_MASK); |
| 92 | |
| 93 | /* If chgmod == BATFULL, setting chgena has no effect. |
Paul Fertser | 1282b35 | 2009-11-05 00:24:58 +0300 | [diff] [blame] | 94 | * Datasheet says we need to set resume instead but when autoresume is |
| 95 | * used resume doesn't work. Clear and set chgena instead. |
Balaji Rao | 9705ecc | 2009-01-27 19:23:12 +0530 | [diff] [blame] | 96 | */ |
| 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); |
Paul Fertser | 1282b35 | 2009-11-05 00:24:58 +0300 | [diff] [blame] | 100 | else { |
| 101 | pcf50633_reg_clear_bits(pcf, PCF50633_REG_MBCC1, |
| 102 | PCF50633_MBCC1_CHGENA); |
Balaji Rao | 9705ecc | 2009-01-27 19:23:12 +0530 | [diff] [blame] | 103 | pcf50633_reg_set_bit_mask(pcf, PCF50633_REG_MBCC1, |
Paul Fertser | 1282b35 | 2009-11-05 00:24:58 +0300 | [diff] [blame] | 104 | PCF50633_MBCC1_CHGENA, PCF50633_MBCC1_CHGENA); |
| 105 | } |
Balaji Rao | 9705ecc | 2009-01-27 19:23:12 +0530 | [diff] [blame] | 106 | |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 107 | power_supply_changed(&mbc->usb); |
| 108 | |
| 109 | return ret; |
| 110 | } |
| 111 | EXPORT_SYMBOL_GPL(pcf50633_mbc_usb_curlim_set); |
| 112 | |
| 113 | int pcf50633_mbc_get_status(struct pcf50633 *pcf) |
| 114 | { |
| 115 | struct pcf50633_mbc *mbc = platform_get_drvdata(pcf->mbc_pdev); |
| 116 | int status = 0; |
Paul Fertser | c329795 | 2009-11-05 00:24:59 +0300 | [diff] [blame] | 117 | u8 chgmod; |
| 118 | |
| 119 | if (!mbc) |
| 120 | return 0; |
| 121 | |
| 122 | chgmod = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCS2) |
| 123 | & PCF50633_MBCS2_MBC_MASK; |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 124 | |
| 125 | if (mbc->usb_online) |
| 126 | status |= PCF50633_MBC_USB_ONLINE; |
Paul Fertser | c329795 | 2009-11-05 00:24:59 +0300 | [diff] [blame] | 127 | if (chgmod == PCF50633_MBCS2_MBC_USB_PRE || |
| 128 | chgmod == PCF50633_MBCS2_MBC_USB_PRE_WAIT || |
| 129 | chgmod == PCF50633_MBCS2_MBC_USB_FAST || |
| 130 | chgmod == PCF50633_MBCS2_MBC_USB_FAST_WAIT) |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 131 | status |= PCF50633_MBC_USB_ACTIVE; |
| 132 | if (mbc->adapter_online) |
| 133 | status |= PCF50633_MBC_ADAPTER_ONLINE; |
Paul Fertser | c329795 | 2009-11-05 00:24:59 +0300 | [diff] [blame] | 134 | if (chgmod == PCF50633_MBCS2_MBC_ADP_PRE || |
| 135 | chgmod == PCF50633_MBCS2_MBC_ADP_PRE_WAIT || |
| 136 | chgmod == PCF50633_MBCS2_MBC_ADP_FAST || |
| 137 | chgmod == PCF50633_MBCS2_MBC_ADP_FAST_WAIT) |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 138 | status |= PCF50633_MBC_ADAPTER_ACTIVE; |
| 139 | |
| 140 | return status; |
| 141 | } |
| 142 | EXPORT_SYMBOL_GPL(pcf50633_mbc_get_status); |
| 143 | |
Paul Fertser | c329795 | 2009-11-05 00:24:59 +0300 | [diff] [blame] | 144 | int pcf50633_mbc_get_usb_online_status(struct pcf50633 *pcf) |
| 145 | { |
| 146 | struct pcf50633_mbc *mbc = platform_get_drvdata(pcf->mbc_pdev); |
| 147 | |
| 148 | if (!mbc) |
| 149 | return 0; |
| 150 | |
| 151 | return mbc->usb_online; |
| 152 | } |
| 153 | EXPORT_SYMBOL_GPL(pcf50633_mbc_get_usb_online_status); |
| 154 | |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 155 | static ssize_t |
| 156 | show_chgmode(struct device *dev, struct device_attribute *attr, char *buf) |
| 157 | { |
| 158 | struct pcf50633_mbc *mbc = dev_get_drvdata(dev); |
| 159 | |
| 160 | u8 mbcs2 = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCS2); |
| 161 | u8 chgmod = (mbcs2 & PCF50633_MBCS2_MBC_MASK); |
| 162 | |
| 163 | return sprintf(buf, "%d\n", chgmod); |
| 164 | } |
| 165 | static DEVICE_ATTR(chgmode, S_IRUGO, show_chgmode, NULL); |
| 166 | |
| 167 | static ssize_t |
| 168 | show_usblim(struct device *dev, struct device_attribute *attr, char *buf) |
| 169 | { |
| 170 | struct pcf50633_mbc *mbc = dev_get_drvdata(dev); |
| 171 | u8 usblim = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCC7) & |
| 172 | PCF50633_MBCC7_USB_MASK; |
| 173 | unsigned int ma; |
| 174 | |
| 175 | if (usblim == PCF50633_MBCC7_USB_1000mA) |
| 176 | ma = 1000; |
| 177 | else if (usblim == PCF50633_MBCC7_USB_500mA) |
| 178 | ma = 500; |
| 179 | else if (usblim == PCF50633_MBCC7_USB_100mA) |
| 180 | ma = 100; |
| 181 | else |
| 182 | ma = 0; |
| 183 | |
| 184 | return sprintf(buf, "%u\n", ma); |
| 185 | } |
| 186 | |
| 187 | static ssize_t set_usblim(struct device *dev, |
| 188 | struct device_attribute *attr, const char *buf, size_t count) |
| 189 | { |
| 190 | struct pcf50633_mbc *mbc = dev_get_drvdata(dev); |
| 191 | unsigned long ma; |
| 192 | int ret; |
| 193 | |
| 194 | ret = strict_strtoul(buf, 10, &ma); |
| 195 | if (ret) |
| 196 | return -EINVAL; |
| 197 | |
| 198 | pcf50633_mbc_usb_curlim_set(mbc->pcf, ma); |
| 199 | |
| 200 | return count; |
| 201 | } |
| 202 | |
| 203 | static DEVICE_ATTR(usb_curlim, S_IRUGO | S_IWUSR, show_usblim, set_usblim); |
| 204 | |
Balaji Rao | 31b4ff0 | 2009-11-05 00:24:55 +0300 | [diff] [blame] | 205 | static ssize_t |
| 206 | show_chglim(struct device *dev, struct device_attribute *attr, char *buf) |
| 207 | { |
| 208 | struct pcf50633_mbc *mbc = dev_get_drvdata(dev); |
| 209 | u8 mbcc5 = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCC5); |
| 210 | unsigned int ma; |
| 211 | |
| 212 | if (!mbc->pcf->pdata->charger_reference_current_ma) |
| 213 | return -ENODEV; |
| 214 | |
| 215 | ma = (mbc->pcf->pdata->charger_reference_current_ma * mbcc5) >> 8; |
| 216 | |
| 217 | return sprintf(buf, "%u\n", ma); |
| 218 | } |
| 219 | |
| 220 | static ssize_t set_chglim(struct device *dev, |
| 221 | struct device_attribute *attr, const char *buf, size_t count) |
| 222 | { |
| 223 | struct pcf50633_mbc *mbc = dev_get_drvdata(dev); |
| 224 | unsigned long ma; |
| 225 | unsigned int mbcc5; |
| 226 | int ret; |
| 227 | |
| 228 | if (!mbc->pcf->pdata->charger_reference_current_ma) |
| 229 | return -ENODEV; |
| 230 | |
| 231 | ret = strict_strtoul(buf, 10, &ma); |
| 232 | if (ret) |
| 233 | return -EINVAL; |
| 234 | |
| 235 | mbcc5 = (ma << 8) / mbc->pcf->pdata->charger_reference_current_ma; |
| 236 | if (mbcc5 > 255) |
| 237 | mbcc5 = 255; |
| 238 | pcf50633_reg_write(mbc->pcf, PCF50633_REG_MBCC5, mbcc5); |
| 239 | |
| 240 | return count; |
| 241 | } |
| 242 | |
| 243 | /* |
| 244 | * This attribute allows to change MBC charging limit on the fly |
| 245 | * independently of usb current limit. It also gets set automatically every |
| 246 | * time usb current limit is changed. |
| 247 | */ |
| 248 | static DEVICE_ATTR(chg_curlim, S_IRUGO | S_IWUSR, show_chglim, set_chglim); |
| 249 | |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 250 | static struct attribute *pcf50633_mbc_sysfs_entries[] = { |
| 251 | &dev_attr_chgmode.attr, |
| 252 | &dev_attr_usb_curlim.attr, |
Balaji Rao | 31b4ff0 | 2009-11-05 00:24:55 +0300 | [diff] [blame] | 253 | &dev_attr_chg_curlim.attr, |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 254 | NULL, |
| 255 | }; |
| 256 | |
| 257 | static struct attribute_group mbc_attr_group = { |
| 258 | .name = NULL, /* put in device directory */ |
| 259 | .attrs = pcf50633_mbc_sysfs_entries, |
| 260 | }; |
| 261 | |
| 262 | static void |
| 263 | pcf50633_mbc_irq_handler(int irq, void *data) |
| 264 | { |
| 265 | struct pcf50633_mbc *mbc = data; |
| 266 | |
| 267 | /* USB */ |
| 268 | if (irq == PCF50633_IRQ_USBINS) { |
| 269 | mbc->usb_online = 1; |
| 270 | } else if (irq == PCF50633_IRQ_USBREM) { |
| 271 | mbc->usb_online = 0; |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 272 | pcf50633_mbc_usb_curlim_set(mbc->pcf, 0); |
| 273 | } |
| 274 | |
| 275 | /* Adapter */ |
Paul Fertser | c329795 | 2009-11-05 00:24:59 +0300 | [diff] [blame] | 276 | if (irq == PCF50633_IRQ_ADPINS) |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 277 | mbc->adapter_online = 1; |
Paul Fertser | c329795 | 2009-11-05 00:24:59 +0300 | [diff] [blame] | 278 | else if (irq == PCF50633_IRQ_ADPREM) |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 279 | mbc->adapter_online = 0; |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 280 | |
Sean McNeil | 7677f33 | 2009-11-05 00:24:54 +0300 | [diff] [blame] | 281 | power_supply_changed(&mbc->ac); |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 282 | power_supply_changed(&mbc->usb); |
| 283 | power_supply_changed(&mbc->adapter); |
| 284 | |
| 285 | if (mbc->pcf->pdata->mbc_event_callback) |
| 286 | mbc->pcf->pdata->mbc_event_callback(mbc->pcf, irq); |
| 287 | } |
| 288 | |
| 289 | static int adapter_get_property(struct power_supply *psy, |
| 290 | enum power_supply_property psp, |
| 291 | union power_supply_propval *val) |
| 292 | { |
Balaji Rao | ccc9c8b | 2009-01-27 19:22:38 +0530 | [diff] [blame] | 293 | struct pcf50633_mbc *mbc = container_of(psy, |
| 294 | struct pcf50633_mbc, adapter); |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 295 | int ret = 0; |
| 296 | |
| 297 | switch (psp) { |
| 298 | case POWER_SUPPLY_PROP_ONLINE: |
| 299 | val->intval = mbc->adapter_online; |
| 300 | break; |
| 301 | default: |
| 302 | ret = -EINVAL; |
| 303 | break; |
| 304 | } |
| 305 | return ret; |
| 306 | } |
| 307 | |
| 308 | static int usb_get_property(struct power_supply *psy, |
| 309 | enum power_supply_property psp, |
| 310 | union power_supply_propval *val) |
| 311 | { |
| 312 | struct pcf50633_mbc *mbc = container_of(psy, struct pcf50633_mbc, usb); |
| 313 | int ret = 0; |
Sean McNeil | 7677f33 | 2009-11-05 00:24:54 +0300 | [diff] [blame] | 314 | u8 usblim = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCC7) & |
| 315 | PCF50633_MBCC7_USB_MASK; |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 316 | |
| 317 | switch (psp) { |
| 318 | case POWER_SUPPLY_PROP_ONLINE: |
Sean McNeil | 7677f33 | 2009-11-05 00:24:54 +0300 | [diff] [blame] | 319 | val->intval = mbc->usb_online && |
| 320 | (usblim <= PCF50633_MBCC7_USB_500mA); |
| 321 | break; |
| 322 | default: |
| 323 | ret = -EINVAL; |
| 324 | break; |
| 325 | } |
| 326 | return ret; |
| 327 | } |
| 328 | |
| 329 | static int ac_get_property(struct power_supply *psy, |
| 330 | enum power_supply_property psp, |
| 331 | union power_supply_propval *val) |
| 332 | { |
| 333 | struct pcf50633_mbc *mbc = container_of(psy, struct pcf50633_mbc, ac); |
| 334 | int ret = 0; |
| 335 | u8 usblim = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCC7) & |
| 336 | PCF50633_MBCC7_USB_MASK; |
| 337 | |
| 338 | switch (psp) { |
| 339 | case POWER_SUPPLY_PROP_ONLINE: |
| 340 | val->intval = mbc->usb_online && |
| 341 | (usblim == PCF50633_MBCC7_USB_1000mA); |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 342 | break; |
| 343 | default: |
| 344 | ret = -EINVAL; |
| 345 | break; |
| 346 | } |
| 347 | return ret; |
| 348 | } |
| 349 | |
| 350 | static enum power_supply_property power_props[] = { |
| 351 | POWER_SUPPLY_PROP_ONLINE, |
| 352 | }; |
| 353 | |
| 354 | static const u8 mbc_irq_handlers[] = { |
| 355 | PCF50633_IRQ_ADPINS, |
| 356 | PCF50633_IRQ_ADPREM, |
| 357 | PCF50633_IRQ_USBINS, |
| 358 | PCF50633_IRQ_USBREM, |
| 359 | PCF50633_IRQ_BATFULL, |
| 360 | PCF50633_IRQ_CHGHALT, |
| 361 | PCF50633_IRQ_THLIMON, |
| 362 | PCF50633_IRQ_THLIMOFF, |
| 363 | PCF50633_IRQ_USBLIMON, |
| 364 | PCF50633_IRQ_USBLIMOFF, |
| 365 | PCF50633_IRQ_LOWSYS, |
| 366 | PCF50633_IRQ_LOWBAT, |
| 367 | }; |
| 368 | |
Bill Pemberton | c8afa64 | 2012-11-19 13:22:23 -0500 | [diff] [blame] | 369 | static int pcf50633_mbc_probe(struct platform_device *pdev) |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 370 | { |
| 371 | struct pcf50633_mbc *mbc; |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 372 | int ret; |
| 373 | int i; |
| 374 | u8 mbcs1; |
| 375 | |
Jingoo Han | c09dd7d | 2013-03-11 15:35:38 +0900 | [diff] [blame] | 376 | mbc = devm_kzalloc(&pdev->dev, sizeof(*mbc), GFP_KERNEL); |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 377 | if (!mbc) |
| 378 | return -ENOMEM; |
| 379 | |
| 380 | platform_set_drvdata(pdev, mbc); |
Lars-Peter Clausen | 68d641e | 2009-10-14 02:12:33 +0400 | [diff] [blame] | 381 | mbc->pcf = dev_to_pcf50633(pdev->dev.parent); |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 382 | |
| 383 | /* Set up IRQ handlers */ |
| 384 | for (i = 0; i < ARRAY_SIZE(mbc_irq_handlers); i++) |
| 385 | pcf50633_register_irq(mbc->pcf, mbc_irq_handlers[i], |
| 386 | pcf50633_mbc_irq_handler, mbc); |
| 387 | |
| 388 | /* Create power supplies */ |
| 389 | mbc->adapter.name = "adapter"; |
| 390 | mbc->adapter.type = POWER_SUPPLY_TYPE_MAINS; |
| 391 | mbc->adapter.properties = power_props; |
| 392 | mbc->adapter.num_properties = ARRAY_SIZE(power_props); |
| 393 | mbc->adapter.get_property = &adapter_get_property; |
| 394 | mbc->adapter.supplied_to = mbc->pcf->pdata->batteries; |
| 395 | mbc->adapter.num_supplicants = mbc->pcf->pdata->num_batteries; |
| 396 | |
| 397 | mbc->usb.name = "usb"; |
| 398 | mbc->usb.type = POWER_SUPPLY_TYPE_USB; |
| 399 | mbc->usb.properties = power_props; |
| 400 | mbc->usb.num_properties = ARRAY_SIZE(power_props); |
| 401 | mbc->usb.get_property = usb_get_property; |
| 402 | mbc->usb.supplied_to = mbc->pcf->pdata->batteries; |
| 403 | mbc->usb.num_supplicants = mbc->pcf->pdata->num_batteries; |
| 404 | |
Sean McNeil | 7677f33 | 2009-11-05 00:24:54 +0300 | [diff] [blame] | 405 | mbc->ac.name = "ac"; |
| 406 | mbc->ac.type = POWER_SUPPLY_TYPE_MAINS; |
| 407 | mbc->ac.properties = power_props; |
| 408 | mbc->ac.num_properties = ARRAY_SIZE(power_props); |
| 409 | mbc->ac.get_property = ac_get_property; |
| 410 | mbc->ac.supplied_to = mbc->pcf->pdata->batteries; |
| 411 | mbc->ac.num_supplicants = mbc->pcf->pdata->num_batteries; |
| 412 | |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 413 | ret = power_supply_register(&pdev->dev, &mbc->adapter); |
| 414 | if (ret) { |
| 415 | dev_err(mbc->pcf->dev, "failed to register adapter\n"); |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 416 | return ret; |
| 417 | } |
| 418 | |
| 419 | ret = power_supply_register(&pdev->dev, &mbc->usb); |
| 420 | if (ret) { |
| 421 | dev_err(mbc->pcf->dev, "failed to register usb\n"); |
| 422 | power_supply_unregister(&mbc->adapter); |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 423 | return ret; |
| 424 | } |
| 425 | |
Sean McNeil | 7677f33 | 2009-11-05 00:24:54 +0300 | [diff] [blame] | 426 | ret = power_supply_register(&pdev->dev, &mbc->ac); |
| 427 | if (ret) { |
| 428 | dev_err(mbc->pcf->dev, "failed to register ac\n"); |
| 429 | power_supply_unregister(&mbc->adapter); |
| 430 | power_supply_unregister(&mbc->usb); |
Sean McNeil | 7677f33 | 2009-11-05 00:24:54 +0300 | [diff] [blame] | 431 | return ret; |
| 432 | } |
Balaji Rao | 9705ecc | 2009-01-27 19:23:12 +0530 | [diff] [blame] | 433 | |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 434 | ret = sysfs_create_group(&pdev->dev.kobj, &mbc_attr_group); |
| 435 | if (ret) |
| 436 | dev_err(mbc->pcf->dev, "failed to create sysfs entries\n"); |
| 437 | |
| 438 | mbcs1 = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCS1); |
| 439 | if (mbcs1 & PCF50633_MBCS1_USBPRES) |
| 440 | pcf50633_mbc_irq_handler(PCF50633_IRQ_USBINS, mbc); |
| 441 | if (mbcs1 & PCF50633_MBCS1_ADAPTPRES) |
| 442 | pcf50633_mbc_irq_handler(PCF50633_IRQ_ADPINS, mbc); |
| 443 | |
| 444 | return 0; |
| 445 | } |
| 446 | |
Bill Pemberton | 415ec69 | 2012-11-19 13:26:07 -0500 | [diff] [blame] | 447 | static int pcf50633_mbc_remove(struct platform_device *pdev) |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 448 | { |
| 449 | struct pcf50633_mbc *mbc = platform_get_drvdata(pdev); |
| 450 | int i; |
| 451 | |
| 452 | /* Remove IRQ handlers */ |
| 453 | for (i = 0; i < ARRAY_SIZE(mbc_irq_handlers); i++) |
| 454 | pcf50633_free_irq(mbc->pcf, mbc_irq_handlers[i]); |
| 455 | |
Axel Lin | 305da8f | 2010-09-21 16:35:57 +0800 | [diff] [blame] | 456 | sysfs_remove_group(&pdev->dev.kobj, &mbc_attr_group); |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 457 | power_supply_unregister(&mbc->usb); |
| 458 | power_supply_unregister(&mbc->adapter); |
Sean McNeil | 7677f33 | 2009-11-05 00:24:54 +0300 | [diff] [blame] | 459 | power_supply_unregister(&mbc->ac); |
Balaji Rao | 9705ecc | 2009-01-27 19:23:12 +0530 | [diff] [blame] | 460 | |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 461 | return 0; |
| 462 | } |
| 463 | |
| 464 | static struct platform_driver pcf50633_mbc_driver = { |
| 465 | .driver = { |
| 466 | .name = "pcf50633-mbc", |
| 467 | }, |
| 468 | .probe = pcf50633_mbc_probe, |
Bill Pemberton | 28ea73f | 2012-11-19 13:20:40 -0500 | [diff] [blame] | 469 | .remove = pcf50633_mbc_remove, |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 470 | }; |
| 471 | |
Axel Lin | 300bac7 | 2011-11-26 12:01:10 +0800 | [diff] [blame] | 472 | module_platform_driver(pcf50633_mbc_driver); |
Balaji Rao | f5714dc | 2009-01-09 01:50:55 +0100 | [diff] [blame] | 473 | |
| 474 | MODULE_AUTHOR("Balaji Rao <balajirrao@openmoko.org>"); |
| 475 | MODULE_DESCRIPTION("PCF50633 mbc driver"); |
| 476 | MODULE_LICENSE("GPL"); |
| 477 | MODULE_ALIAS("platform:pcf50633-mbc"); |