Woogyom Kim | 2165c8a | 2012-01-04 08:27:43 +0400 | [diff] [blame] | 1 | /* |
Kim, Milo | f7bae49 | 2012-01-26 22:59:08 -0800 | [diff] [blame] | 2 | * Driver for LP8727 Micro/Mini USB IC with integrated charger |
Woogyom Kim | 2165c8a | 2012-01-04 08:27:43 +0400 | [diff] [blame] | 3 | * |
Kim, Milo | e39b828 | 2012-01-29 17:28:18 -0800 | [diff] [blame] | 4 | * Copyright (C) 2011 Texas Instruments |
Woogyom Kim | 2165c8a | 2012-01-04 08:27:43 +0400 | [diff] [blame] | 5 | * Copyright (C) 2011 National Semiconductor |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/slab.h> |
| 15 | #include <linux/interrupt.h> |
| 16 | #include <linux/i2c.h> |
| 17 | #include <linux/power_supply.h> |
Kim, Milo | 1aebb09 | 2012-07-03 01:19:03 +0000 | [diff] [blame] | 18 | #include <linux/platform_data/lp8727.h> |
Woogyom Kim | 2165c8a | 2012-01-04 08:27:43 +0400 | [diff] [blame] | 19 | |
Kim, Milo | 60fd57e | 2012-08-31 09:23:25 +0000 | [diff] [blame] | 20 | #define DEFAULT_DEBOUNCE_MSEC 270 |
Woogyom Kim | 2165c8a | 2012-01-04 08:27:43 +0400 | [diff] [blame] | 21 | |
| 22 | /* Registers */ |
| 23 | #define CTRL1 0x1 |
| 24 | #define CTRL2 0x2 |
| 25 | #define SWCTRL 0x3 |
| 26 | #define INT1 0x4 |
| 27 | #define INT2 0x5 |
| 28 | #define STATUS1 0x6 |
Kim, Milo | 7336880 | 2012-01-26 22:58:51 -0800 | [diff] [blame] | 29 | #define STATUS2 0x7 |
Woogyom Kim | 2165c8a | 2012-01-04 08:27:43 +0400 | [diff] [blame] | 30 | #define CHGCTRL2 0x9 |
| 31 | |
| 32 | /* CTRL1 register */ |
| 33 | #define CP_EN (1 << 0) |
| 34 | #define ADC_EN (1 << 1) |
| 35 | #define ID200_EN (1 << 4) |
| 36 | |
| 37 | /* CTRL2 register */ |
| 38 | #define CHGDET_EN (1 << 1) |
| 39 | #define INT_EN (1 << 6) |
| 40 | |
| 41 | /* SWCTRL register */ |
| 42 | #define SW_DM1_DM (0x0 << 0) |
| 43 | #define SW_DM1_U1 (0x1 << 0) |
| 44 | #define SW_DM1_HiZ (0x7 << 0) |
| 45 | #define SW_DP2_DP (0x0 << 3) |
| 46 | #define SW_DP2_U2 (0x1 << 3) |
| 47 | #define SW_DP2_HiZ (0x7 << 3) |
| 48 | |
| 49 | /* INT1 register */ |
| 50 | #define IDNO (0xF << 0) |
| 51 | #define VBUS (1 << 4) |
| 52 | |
| 53 | /* STATUS1 register */ |
| 54 | #define CHGSTAT (3 << 4) |
| 55 | #define CHPORT (1 << 6) |
| 56 | #define DCPORT (1 << 7) |
| 57 | |
| 58 | /* STATUS2 register */ |
| 59 | #define TEMP_STAT (3 << 5) |
| 60 | |
| 61 | enum lp8727_dev_id { |
| 62 | ID_NONE, |
| 63 | ID_TA, |
| 64 | ID_DEDICATED_CHG, |
| 65 | ID_USB_CHG, |
| 66 | ID_USB_DS, |
| 67 | ID_MAX, |
| 68 | }; |
| 69 | |
| 70 | enum lp8727_chg_stat { |
| 71 | PRECHG, |
| 72 | CC, |
| 73 | CV, |
| 74 | EOC, |
| 75 | }; |
| 76 | |
| 77 | struct lp8727_psy { |
| 78 | struct power_supply ac; |
| 79 | struct power_supply usb; |
| 80 | struct power_supply batt; |
| 81 | }; |
| 82 | |
| 83 | struct lp8727_chg { |
| 84 | struct device *dev; |
| 85 | struct i2c_client *client; |
| 86 | struct mutex xfer_lock; |
| 87 | struct delayed_work work; |
Woogyom Kim | 2165c8a | 2012-01-04 08:27:43 +0400 | [diff] [blame] | 88 | struct lp8727_platform_data *pdata; |
| 89 | struct lp8727_psy *psy; |
| 90 | struct lp8727_chg_param *chg_parm; |
| 91 | enum lp8727_dev_id devid; |
Kim, Milo | 60fd57e | 2012-08-31 09:23:25 +0000 | [diff] [blame] | 92 | unsigned long debounce_jiffies; |
Kim, Milo | d71fda0 | 2012-08-31 09:23:57 +0000 | [diff] [blame^] | 93 | int irq; |
Woogyom Kim | 2165c8a | 2012-01-04 08:27:43 +0400 | [diff] [blame] | 94 | }; |
| 95 | |
Kim, Milo | 27aefa3 | 2012-01-26 22:58:39 -0800 | [diff] [blame] | 96 | static int lp8727_read_bytes(struct lp8727_chg *pchg, u8 reg, u8 *data, u8 len) |
Woogyom Kim | 2165c8a | 2012-01-04 08:27:43 +0400 | [diff] [blame] | 97 | { |
| 98 | s32 ret; |
| 99 | |
| 100 | mutex_lock(&pchg->xfer_lock); |
| 101 | ret = i2c_smbus_read_i2c_block_data(pchg->client, reg, len, data); |
| 102 | mutex_unlock(&pchg->xfer_lock); |
| 103 | |
| 104 | return (ret != len) ? -EIO : 0; |
| 105 | } |
| 106 | |
Kim, Milo | 27aefa3 | 2012-01-26 22:58:39 -0800 | [diff] [blame] | 107 | static inline int lp8727_read_byte(struct lp8727_chg *pchg, u8 reg, u8 *data) |
Woogyom Kim | 2165c8a | 2012-01-04 08:27:43 +0400 | [diff] [blame] | 108 | { |
Kim, Milo | 27aefa3 | 2012-01-26 22:58:39 -0800 | [diff] [blame] | 109 | return lp8727_read_bytes(pchg, reg, data, 1); |
| 110 | } |
| 111 | |
| 112 | static int lp8727_write_byte(struct lp8727_chg *pchg, u8 reg, u8 data) |
| 113 | { |
| 114 | int ret; |
Woogyom Kim | 2165c8a | 2012-01-04 08:27:43 +0400 | [diff] [blame] | 115 | |
| 116 | mutex_lock(&pchg->xfer_lock); |
Kim, Milo | 27aefa3 | 2012-01-26 22:58:39 -0800 | [diff] [blame] | 117 | ret = i2c_smbus_write_byte_data(pchg->client, reg, data); |
Woogyom Kim | 2165c8a | 2012-01-04 08:27:43 +0400 | [diff] [blame] | 118 | mutex_unlock(&pchg->xfer_lock); |
| 119 | |
| 120 | return ret; |
| 121 | } |
| 122 | |
Woogyom Kim | 2165c8a | 2012-01-04 08:27:43 +0400 | [diff] [blame] | 123 | static int lp8727_is_charger_attached(const char *name, int id) |
| 124 | { |
| 125 | if (name) { |
| 126 | if (!strcmp(name, "ac")) |
| 127 | return (id == ID_TA || id == ID_DEDICATED_CHG) ? 1 : 0; |
| 128 | else if (!strcmp(name, "usb")) |
| 129 | return (id == ID_USB_CHG) ? 1 : 0; |
| 130 | } |
| 131 | |
| 132 | return (id >= ID_TA && id <= ID_USB_CHG) ? 1 : 0; |
| 133 | } |
| 134 | |
Kim, Milo | 7da6334 | 2012-01-26 22:58:30 -0800 | [diff] [blame] | 135 | static int lp8727_init_device(struct lp8727_chg *pchg) |
Woogyom Kim | 2165c8a | 2012-01-04 08:27:43 +0400 | [diff] [blame] | 136 | { |
| 137 | u8 val; |
Kim, Milo | 7da6334 | 2012-01-26 22:58:30 -0800 | [diff] [blame] | 138 | int ret; |
Woogyom Kim | 2165c8a | 2012-01-04 08:27:43 +0400 | [diff] [blame] | 139 | |
| 140 | val = ID200_EN | ADC_EN | CP_EN; |
Kim, Milo | 27aefa3 | 2012-01-26 22:58:39 -0800 | [diff] [blame] | 141 | ret = lp8727_write_byte(pchg, CTRL1, val); |
Kim, Milo | 7da6334 | 2012-01-26 22:58:30 -0800 | [diff] [blame] | 142 | if (ret) |
| 143 | return ret; |
Woogyom Kim | 2165c8a | 2012-01-04 08:27:43 +0400 | [diff] [blame] | 144 | |
| 145 | val = INT_EN | CHGDET_EN; |
Kim, Milo | 27aefa3 | 2012-01-26 22:58:39 -0800 | [diff] [blame] | 146 | ret = lp8727_write_byte(pchg, CTRL2, val); |
Kim, Milo | 7da6334 | 2012-01-26 22:58:30 -0800 | [diff] [blame] | 147 | if (ret) |
| 148 | return ret; |
| 149 | |
| 150 | return 0; |
Woogyom Kim | 2165c8a | 2012-01-04 08:27:43 +0400 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | static int lp8727_is_dedicated_charger(struct lp8727_chg *pchg) |
| 154 | { |
| 155 | u8 val; |
Kim, Milo | 27aefa3 | 2012-01-26 22:58:39 -0800 | [diff] [blame] | 156 | lp8727_read_byte(pchg, STATUS1, &val); |
Kim, Milo | 7336880 | 2012-01-26 22:58:51 -0800 | [diff] [blame] | 157 | return val & DCPORT; |
Woogyom Kim | 2165c8a | 2012-01-04 08:27:43 +0400 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | static int lp8727_is_usb_charger(struct lp8727_chg *pchg) |
| 161 | { |
| 162 | u8 val; |
Kim, Milo | 27aefa3 | 2012-01-26 22:58:39 -0800 | [diff] [blame] | 163 | lp8727_read_byte(pchg, STATUS1, &val); |
Kim, Milo | 7336880 | 2012-01-26 22:58:51 -0800 | [diff] [blame] | 164 | return val & CHPORT; |
Woogyom Kim | 2165c8a | 2012-01-04 08:27:43 +0400 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | static void lp8727_ctrl_switch(struct lp8727_chg *pchg, u8 sw) |
| 168 | { |
Kim, Milo | 27aefa3 | 2012-01-26 22:58:39 -0800 | [diff] [blame] | 169 | lp8727_write_byte(pchg, SWCTRL, sw); |
Woogyom Kim | 2165c8a | 2012-01-04 08:27:43 +0400 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | static void lp8727_id_detection(struct lp8727_chg *pchg, u8 id, int vbusin) |
| 173 | { |
Kim, Milo | 318cb38 | 2012-08-31 09:23:12 +0000 | [diff] [blame] | 174 | struct lp8727_platform_data *pdata = pchg->pdata; |
Woogyom Kim | 2165c8a | 2012-01-04 08:27:43 +0400 | [diff] [blame] | 175 | u8 devid = ID_NONE; |
| 176 | u8 swctrl = SW_DM1_HiZ | SW_DP2_HiZ; |
| 177 | |
| 178 | switch (id) { |
| 179 | case 0x5: |
| 180 | devid = ID_TA; |
Kim, Milo | 318cb38 | 2012-08-31 09:23:12 +0000 | [diff] [blame] | 181 | pchg->chg_parm = pdata ? pdata->ac : NULL; |
Woogyom Kim | 2165c8a | 2012-01-04 08:27:43 +0400 | [diff] [blame] | 182 | break; |
| 183 | case 0xB: |
| 184 | if (lp8727_is_dedicated_charger(pchg)) { |
Kim, Milo | 318cb38 | 2012-08-31 09:23:12 +0000 | [diff] [blame] | 185 | pchg->chg_parm = pdata ? pdata->ac : NULL; |
Woogyom Kim | 2165c8a | 2012-01-04 08:27:43 +0400 | [diff] [blame] | 186 | devid = ID_DEDICATED_CHG; |
| 187 | } else if (lp8727_is_usb_charger(pchg)) { |
Kim, Milo | 318cb38 | 2012-08-31 09:23:12 +0000 | [diff] [blame] | 188 | pchg->chg_parm = pdata ? pdata->usb : NULL; |
Woogyom Kim | 2165c8a | 2012-01-04 08:27:43 +0400 | [diff] [blame] | 189 | devid = ID_USB_CHG; |
| 190 | swctrl = SW_DM1_DM | SW_DP2_DP; |
| 191 | } else if (vbusin) { |
| 192 | devid = ID_USB_DS; |
| 193 | swctrl = SW_DM1_DM | SW_DP2_DP; |
| 194 | } |
| 195 | break; |
| 196 | default: |
| 197 | devid = ID_NONE; |
| 198 | pchg->chg_parm = NULL; |
| 199 | break; |
| 200 | } |
| 201 | |
| 202 | pchg->devid = devid; |
| 203 | lp8727_ctrl_switch(pchg, swctrl); |
| 204 | } |
| 205 | |
| 206 | static void lp8727_enable_chgdet(struct lp8727_chg *pchg) |
| 207 | { |
| 208 | u8 val; |
| 209 | |
Kim, Milo | 27aefa3 | 2012-01-26 22:58:39 -0800 | [diff] [blame] | 210 | lp8727_read_byte(pchg, CTRL2, &val); |
Woogyom Kim | 2165c8a | 2012-01-04 08:27:43 +0400 | [diff] [blame] | 211 | val |= CHGDET_EN; |
Kim, Milo | 27aefa3 | 2012-01-26 22:58:39 -0800 | [diff] [blame] | 212 | lp8727_write_byte(pchg, CTRL2, val); |
Woogyom Kim | 2165c8a | 2012-01-04 08:27:43 +0400 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | static void lp8727_delayed_func(struct work_struct *_work) |
| 216 | { |
| 217 | u8 intstat[2], idno, vbus; |
| 218 | struct lp8727_chg *pchg = |
| 219 | container_of(_work, struct lp8727_chg, work.work); |
| 220 | |
Kim, Milo | 27aefa3 | 2012-01-26 22:58:39 -0800 | [diff] [blame] | 221 | if (lp8727_read_bytes(pchg, INT1, intstat, 2)) { |
Woogyom Kim | 2165c8a | 2012-01-04 08:27:43 +0400 | [diff] [blame] | 222 | dev_err(pchg->dev, "can not read INT registers\n"); |
| 223 | return; |
| 224 | } |
| 225 | |
| 226 | idno = intstat[0] & IDNO; |
| 227 | vbus = intstat[0] & VBUS; |
| 228 | |
| 229 | lp8727_id_detection(pchg, idno, vbus); |
| 230 | lp8727_enable_chgdet(pchg); |
| 231 | |
| 232 | power_supply_changed(&pchg->psy->ac); |
| 233 | power_supply_changed(&pchg->psy->usb); |
| 234 | power_supply_changed(&pchg->psy->batt); |
| 235 | } |
| 236 | |
| 237 | static irqreturn_t lp8727_isr_func(int irq, void *ptr) |
| 238 | { |
| 239 | struct lp8727_chg *pchg = ptr; |
Woogyom Kim | 2165c8a | 2012-01-04 08:27:43 +0400 | [diff] [blame] | 240 | |
Kim, Milo | 2a09258 | 2012-08-31 09:23:41 +0000 | [diff] [blame] | 241 | schedule_delayed_work(&pchg->work, pchg->debounce_jiffies); |
Woogyom Kim | 2165c8a | 2012-01-04 08:27:43 +0400 | [diff] [blame] | 242 | return IRQ_HANDLED; |
| 243 | } |
| 244 | |
Kim, Milo | d71fda0 | 2012-08-31 09:23:57 +0000 | [diff] [blame^] | 245 | static int lp8727_setup_irq(struct lp8727_chg *pchg) |
Woogyom Kim | 2165c8a | 2012-01-04 08:27:43 +0400 | [diff] [blame] | 246 | { |
Kim, Milo | d71fda0 | 2012-08-31 09:23:57 +0000 | [diff] [blame^] | 247 | int ret; |
| 248 | int irq = pchg->client->irq; |
Kim, Milo | 60fd57e | 2012-08-31 09:23:25 +0000 | [diff] [blame] | 249 | unsigned delay_msec = pchg->pdata ? pchg->pdata->debounce_msec : |
| 250 | DEFAULT_DEBOUNCE_MSEC; |
| 251 | |
Woogyom Kim | 2165c8a | 2012-01-04 08:27:43 +0400 | [diff] [blame] | 252 | INIT_DELAYED_WORK(&pchg->work, lp8727_delayed_func); |
| 253 | |
Kim, Milo | d71fda0 | 2012-08-31 09:23:57 +0000 | [diff] [blame^] | 254 | if (irq <= 0) { |
| 255 | dev_warn(pchg->dev, "invalid irq number: %d\n", irq); |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | ret = request_threaded_irq(irq, NULL, lp8727_isr_func, |
| 260 | IRQF_TRIGGER_FALLING | IRQF_ONESHOT, |
| 261 | "lp8727_irq", pchg); |
| 262 | |
| 263 | if (ret) |
| 264 | return ret; |
| 265 | |
| 266 | pchg->irq = irq; |
Kim, Milo | 60fd57e | 2012-08-31 09:23:25 +0000 | [diff] [blame] | 267 | pchg->debounce_jiffies = msecs_to_jiffies(delay_msec); |
| 268 | |
Kim, Milo | d71fda0 | 2012-08-31 09:23:57 +0000 | [diff] [blame^] | 269 | return 0; |
| 270 | } |
| 271 | |
| 272 | static void lp8727_release_irq(struct lp8727_chg *pchg) |
| 273 | { |
| 274 | cancel_delayed_work_sync(&pchg->work); |
| 275 | |
| 276 | if (pchg->irq) |
| 277 | free_irq(pchg->irq, pchg); |
Woogyom Kim | 2165c8a | 2012-01-04 08:27:43 +0400 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | static enum power_supply_property lp8727_charger_prop[] = { |
| 281 | POWER_SUPPLY_PROP_ONLINE, |
| 282 | }; |
| 283 | |
| 284 | static enum power_supply_property lp8727_battery_prop[] = { |
| 285 | POWER_SUPPLY_PROP_STATUS, |
| 286 | POWER_SUPPLY_PROP_HEALTH, |
| 287 | POWER_SUPPLY_PROP_PRESENT, |
| 288 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 289 | POWER_SUPPLY_PROP_CAPACITY, |
| 290 | POWER_SUPPLY_PROP_TEMP, |
| 291 | }; |
| 292 | |
| 293 | static char *battery_supplied_to[] = { |
| 294 | "main_batt", |
| 295 | }; |
| 296 | |
| 297 | static int lp8727_charger_get_property(struct power_supply *psy, |
| 298 | enum power_supply_property psp, |
| 299 | union power_supply_propval *val) |
| 300 | { |
| 301 | struct lp8727_chg *pchg = dev_get_drvdata(psy->dev->parent); |
| 302 | |
Kim, Milo | ce09aff | 2012-01-04 09:03:18 +0400 | [diff] [blame] | 303 | if (psp == POWER_SUPPLY_PROP_ONLINE) |
Woogyom Kim | 2165c8a | 2012-01-04 08:27:43 +0400 | [diff] [blame] | 304 | val->intval = lp8727_is_charger_attached(psy->name, |
| 305 | pchg->devid); |
Woogyom Kim | 2165c8a | 2012-01-04 08:27:43 +0400 | [diff] [blame] | 306 | |
| 307 | return 0; |
| 308 | } |
| 309 | |
| 310 | static int lp8727_battery_get_property(struct power_supply *psy, |
| 311 | enum power_supply_property psp, |
| 312 | union power_supply_propval *val) |
| 313 | { |
| 314 | struct lp8727_chg *pchg = dev_get_drvdata(psy->dev->parent); |
Kim, Milo | 318cb38 | 2012-08-31 09:23:12 +0000 | [diff] [blame] | 315 | struct lp8727_platform_data *pdata = pchg->pdata; |
Woogyom Kim | 2165c8a | 2012-01-04 08:27:43 +0400 | [diff] [blame] | 316 | u8 read; |
| 317 | |
| 318 | switch (psp) { |
| 319 | case POWER_SUPPLY_PROP_STATUS: |
| 320 | if (lp8727_is_charger_attached(psy->name, pchg->devid)) { |
Kim, Milo | 27aefa3 | 2012-01-26 22:58:39 -0800 | [diff] [blame] | 321 | lp8727_read_byte(pchg, STATUS1, &read); |
Woogyom Kim | 2165c8a | 2012-01-04 08:27:43 +0400 | [diff] [blame] | 322 | if (((read & CHGSTAT) >> 4) == EOC) |
| 323 | val->intval = POWER_SUPPLY_STATUS_FULL; |
| 324 | else |
| 325 | val->intval = POWER_SUPPLY_STATUS_CHARGING; |
| 326 | } else { |
| 327 | val->intval = POWER_SUPPLY_STATUS_DISCHARGING; |
| 328 | } |
| 329 | break; |
| 330 | case POWER_SUPPLY_PROP_HEALTH: |
Kim, Milo | 27aefa3 | 2012-01-26 22:58:39 -0800 | [diff] [blame] | 331 | lp8727_read_byte(pchg, STATUS2, &read); |
Woogyom Kim | 2165c8a | 2012-01-04 08:27:43 +0400 | [diff] [blame] | 332 | read = (read & TEMP_STAT) >> 5; |
| 333 | if (read >= 0x1 && read <= 0x3) |
| 334 | val->intval = POWER_SUPPLY_HEALTH_OVERHEAT; |
| 335 | else |
| 336 | val->intval = POWER_SUPPLY_HEALTH_GOOD; |
| 337 | break; |
| 338 | case POWER_SUPPLY_PROP_PRESENT: |
Kim, Milo | 318cb38 | 2012-08-31 09:23:12 +0000 | [diff] [blame] | 339 | if (!pdata) |
| 340 | return -EINVAL; |
| 341 | |
| 342 | if (pdata->get_batt_present) |
Woogyom Kim | 2165c8a | 2012-01-04 08:27:43 +0400 | [diff] [blame] | 343 | val->intval = pchg->pdata->get_batt_present(); |
| 344 | break; |
| 345 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
Kim, Milo | 318cb38 | 2012-08-31 09:23:12 +0000 | [diff] [blame] | 346 | if (!pdata) |
| 347 | return -EINVAL; |
| 348 | |
| 349 | if (pdata->get_batt_level) |
Woogyom Kim | 2165c8a | 2012-01-04 08:27:43 +0400 | [diff] [blame] | 350 | val->intval = pchg->pdata->get_batt_level(); |
| 351 | break; |
| 352 | case POWER_SUPPLY_PROP_CAPACITY: |
Kim, Milo | 318cb38 | 2012-08-31 09:23:12 +0000 | [diff] [blame] | 353 | if (!pdata) |
| 354 | return -EINVAL; |
| 355 | |
| 356 | if (pdata->get_batt_capacity) |
Woogyom Kim | 2165c8a | 2012-01-04 08:27:43 +0400 | [diff] [blame] | 357 | val->intval = pchg->pdata->get_batt_capacity(); |
| 358 | break; |
| 359 | case POWER_SUPPLY_PROP_TEMP: |
Kim, Milo | 318cb38 | 2012-08-31 09:23:12 +0000 | [diff] [blame] | 360 | if (!pdata) |
| 361 | return -EINVAL; |
| 362 | |
| 363 | if (pdata->get_batt_temp) |
Woogyom Kim | 2165c8a | 2012-01-04 08:27:43 +0400 | [diff] [blame] | 364 | val->intval = pchg->pdata->get_batt_temp(); |
| 365 | break; |
| 366 | default: |
| 367 | break; |
| 368 | } |
| 369 | |
| 370 | return 0; |
| 371 | } |
| 372 | |
| 373 | static void lp8727_charger_changed(struct power_supply *psy) |
| 374 | { |
| 375 | struct lp8727_chg *pchg = dev_get_drvdata(psy->dev->parent); |
| 376 | u8 val; |
| 377 | u8 eoc_level, ichg; |
| 378 | |
| 379 | if (lp8727_is_charger_attached(psy->name, pchg->devid)) { |
| 380 | if (pchg->chg_parm) { |
| 381 | eoc_level = pchg->chg_parm->eoc_level; |
| 382 | ichg = pchg->chg_parm->ichg; |
| 383 | val = (ichg << 4) | eoc_level; |
Kim, Milo | 27aefa3 | 2012-01-26 22:58:39 -0800 | [diff] [blame] | 384 | lp8727_write_byte(pchg, CHGCTRL2, val); |
Woogyom Kim | 2165c8a | 2012-01-04 08:27:43 +0400 | [diff] [blame] | 385 | } |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | static int lp8727_register_psy(struct lp8727_chg *pchg) |
| 390 | { |
| 391 | struct lp8727_psy *psy; |
| 392 | |
Kim, Milo | 74727c5 | 2012-08-31 09:22:46 +0000 | [diff] [blame] | 393 | psy = devm_kzalloc(pchg->dev, sizeof(*psy), GFP_KERNEL); |
Woogyom Kim | 2165c8a | 2012-01-04 08:27:43 +0400 | [diff] [blame] | 394 | if (!psy) |
Devendra Naga | 6297b5e | 2012-07-29 23:31:55 +0545 | [diff] [blame] | 395 | return -ENOMEM; |
Woogyom Kim | 2165c8a | 2012-01-04 08:27:43 +0400 | [diff] [blame] | 396 | |
| 397 | pchg->psy = psy; |
| 398 | |
| 399 | psy->ac.name = "ac"; |
| 400 | psy->ac.type = POWER_SUPPLY_TYPE_MAINS; |
| 401 | psy->ac.properties = lp8727_charger_prop; |
| 402 | psy->ac.num_properties = ARRAY_SIZE(lp8727_charger_prop); |
| 403 | psy->ac.get_property = lp8727_charger_get_property; |
| 404 | psy->ac.supplied_to = battery_supplied_to; |
| 405 | psy->ac.num_supplicants = ARRAY_SIZE(battery_supplied_to); |
| 406 | |
| 407 | if (power_supply_register(pchg->dev, &psy->ac)) |
Devendra Naga | 6297b5e | 2012-07-29 23:31:55 +0545 | [diff] [blame] | 408 | goto err_psy_ac; |
Woogyom Kim | 2165c8a | 2012-01-04 08:27:43 +0400 | [diff] [blame] | 409 | |
| 410 | psy->usb.name = "usb"; |
| 411 | psy->usb.type = POWER_SUPPLY_TYPE_USB; |
| 412 | psy->usb.properties = lp8727_charger_prop; |
| 413 | psy->usb.num_properties = ARRAY_SIZE(lp8727_charger_prop); |
| 414 | psy->usb.get_property = lp8727_charger_get_property; |
| 415 | psy->usb.supplied_to = battery_supplied_to; |
| 416 | psy->usb.num_supplicants = ARRAY_SIZE(battery_supplied_to); |
| 417 | |
| 418 | if (power_supply_register(pchg->dev, &psy->usb)) |
Devendra Naga | 6297b5e | 2012-07-29 23:31:55 +0545 | [diff] [blame] | 419 | goto err_psy_usb; |
Woogyom Kim | 2165c8a | 2012-01-04 08:27:43 +0400 | [diff] [blame] | 420 | |
| 421 | psy->batt.name = "main_batt"; |
| 422 | psy->batt.type = POWER_SUPPLY_TYPE_BATTERY; |
| 423 | psy->batt.properties = lp8727_battery_prop; |
| 424 | psy->batt.num_properties = ARRAY_SIZE(lp8727_battery_prop); |
| 425 | psy->batt.get_property = lp8727_battery_get_property; |
| 426 | psy->batt.external_power_changed = lp8727_charger_changed; |
| 427 | |
| 428 | if (power_supply_register(pchg->dev, &psy->batt)) |
Devendra Naga | 6297b5e | 2012-07-29 23:31:55 +0545 | [diff] [blame] | 429 | goto err_psy_batt; |
Woogyom Kim | 2165c8a | 2012-01-04 08:27:43 +0400 | [diff] [blame] | 430 | |
| 431 | return 0; |
| 432 | |
Devendra Naga | 6297b5e | 2012-07-29 23:31:55 +0545 | [diff] [blame] | 433 | err_psy_batt: |
| 434 | power_supply_unregister(&psy->usb); |
| 435 | err_psy_usb: |
| 436 | power_supply_unregister(&psy->ac); |
| 437 | err_psy_ac: |
Woogyom Kim | 2165c8a | 2012-01-04 08:27:43 +0400 | [diff] [blame] | 438 | return -EPERM; |
| 439 | } |
| 440 | |
| 441 | static void lp8727_unregister_psy(struct lp8727_chg *pchg) |
| 442 | { |
| 443 | struct lp8727_psy *psy = pchg->psy; |
| 444 | |
Kim, Milo | ce09aff | 2012-01-04 09:03:18 +0400 | [diff] [blame] | 445 | if (!psy) |
| 446 | return; |
| 447 | |
| 448 | power_supply_unregister(&psy->ac); |
| 449 | power_supply_unregister(&psy->usb); |
| 450 | power_supply_unregister(&psy->batt); |
Woogyom Kim | 2165c8a | 2012-01-04 08:27:43 +0400 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | static int lp8727_probe(struct i2c_client *cl, const struct i2c_device_id *id) |
| 454 | { |
| 455 | struct lp8727_chg *pchg; |
| 456 | int ret; |
| 457 | |
Kim, Milo | 998a8e7 | 2011-11-17 21:43:06 -0800 | [diff] [blame] | 458 | if (!i2c_check_functionality(cl->adapter, I2C_FUNC_SMBUS_I2C_BLOCK)) |
| 459 | return -EIO; |
| 460 | |
Kim, Milo | 74727c5 | 2012-08-31 09:22:46 +0000 | [diff] [blame] | 461 | pchg = devm_kzalloc(&cl->dev, sizeof(*pchg), GFP_KERNEL); |
Woogyom Kim | 2165c8a | 2012-01-04 08:27:43 +0400 | [diff] [blame] | 462 | if (!pchg) |
| 463 | return -ENOMEM; |
| 464 | |
| 465 | pchg->client = cl; |
| 466 | pchg->dev = &cl->dev; |
| 467 | pchg->pdata = cl->dev.platform_data; |
| 468 | i2c_set_clientdata(cl, pchg); |
| 469 | |
| 470 | mutex_init(&pchg->xfer_lock); |
| 471 | |
Kim, Milo | 7da6334 | 2012-01-26 22:58:30 -0800 | [diff] [blame] | 472 | ret = lp8727_init_device(pchg); |
| 473 | if (ret) { |
| 474 | dev_err(pchg->dev, "i2c communication err: %d", ret); |
Kim, Milo | fb9adc5 | 2012-08-31 09:23:03 +0000 | [diff] [blame] | 475 | return ret; |
Kim, Milo | 7da6334 | 2012-01-26 22:58:30 -0800 | [diff] [blame] | 476 | } |
Woogyom Kim | 2165c8a | 2012-01-04 08:27:43 +0400 | [diff] [blame] | 477 | |
| 478 | ret = lp8727_register_psy(pchg); |
Kim, Milo | 7da6334 | 2012-01-26 22:58:30 -0800 | [diff] [blame] | 479 | if (ret) { |
| 480 | dev_err(pchg->dev, "power supplies register err: %d", ret); |
Kim, Milo | fb9adc5 | 2012-08-31 09:23:03 +0000 | [diff] [blame] | 481 | return ret; |
| 482 | } |
| 483 | |
Kim, Milo | d71fda0 | 2012-08-31 09:23:57 +0000 | [diff] [blame^] | 484 | ret = lp8727_setup_irq(pchg); |
Kim, Milo | fb9adc5 | 2012-08-31 09:23:03 +0000 | [diff] [blame] | 485 | if (ret) { |
| 486 | dev_err(pchg->dev, "irq handler err: %d", ret); |
| 487 | lp8727_unregister_psy(pchg); |
| 488 | return ret; |
Kim, Milo | 7da6334 | 2012-01-26 22:58:30 -0800 | [diff] [blame] | 489 | } |
Woogyom Kim | 2165c8a | 2012-01-04 08:27:43 +0400 | [diff] [blame] | 490 | |
| 491 | return 0; |
| 492 | } |
| 493 | |
| 494 | static int __devexit lp8727_remove(struct i2c_client *cl) |
| 495 | { |
| 496 | struct lp8727_chg *pchg = i2c_get_clientdata(cl); |
| 497 | |
Kim, Milo | d71fda0 | 2012-08-31 09:23:57 +0000 | [diff] [blame^] | 498 | lp8727_release_irq(pchg); |
Kim, Milo | fb9adc5 | 2012-08-31 09:23:03 +0000 | [diff] [blame] | 499 | lp8727_unregister_psy(pchg); |
Woogyom Kim | 2165c8a | 2012-01-04 08:27:43 +0400 | [diff] [blame] | 500 | return 0; |
| 501 | } |
| 502 | |
| 503 | static const struct i2c_device_id lp8727_ids[] = { |
| 504 | {"lp8727", 0}, |
Axel Lin | 455a0e2 | 2012-01-16 13:48:20 +0800 | [diff] [blame] | 505 | { } |
Woogyom Kim | 2165c8a | 2012-01-04 08:27:43 +0400 | [diff] [blame] | 506 | }; |
Axel Lin | e2c5f7d | 2012-01-12 20:45:02 +0800 | [diff] [blame] | 507 | MODULE_DEVICE_TABLE(i2c, lp8727_ids); |
Woogyom Kim | 2165c8a | 2012-01-04 08:27:43 +0400 | [diff] [blame] | 508 | |
| 509 | static struct i2c_driver lp8727_driver = { |
| 510 | .driver = { |
| 511 | .name = "lp8727", |
| 512 | }, |
| 513 | .probe = lp8727_probe, |
| 514 | .remove = __devexit_p(lp8727_remove), |
| 515 | .id_table = lp8727_ids, |
| 516 | }; |
Axel Lin | 5ff92e7 | 2012-01-21 14:42:54 +0800 | [diff] [blame] | 517 | module_i2c_driver(lp8727_driver); |
Woogyom Kim | 2165c8a | 2012-01-04 08:27:43 +0400 | [diff] [blame] | 518 | |
Kim, Milo | e39b828 | 2012-01-29 17:28:18 -0800 | [diff] [blame] | 519 | MODULE_DESCRIPTION("TI/National Semiconductor LP8727 charger driver"); |
Kim, Milo | 7336880 | 2012-01-26 22:58:51 -0800 | [diff] [blame] | 520 | MODULE_AUTHOR("Woogyom Kim <milo.kim@ti.com>, " |
| 521 | "Daniel Jeong <daniel.jeong@ti.com>"); |
Woogyom Kim | 2165c8a | 2012-01-04 08:27:43 +0400 | [diff] [blame] | 522 | MODULE_LICENSE("GPL"); |