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