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