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