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