blob: c0a6feab79ec6d96b1b99adf6fe9929245855c8e [file] [log] [blame]
Woogyom Kim2165c8a2012-01-04 08:27:43 +04001/*
Kim, Milof7bae492012-01-26 22:59:08 -08002 * Driver for LP8727 Micro/Mini USB IC with integrated charger
Woogyom Kim2165c8a2012-01-04 08:27:43 +04003 *
Kim, Miloe39b8282012-01-29 17:28:18 -08004 * Copyright (C) 2011 Texas Instruments
Woogyom Kim2165c8a2012-01-04 08:27:43 +04005 * 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, Milo1aebb092012-07-03 01:19:03 +000018#include <linux/platform_data/lp8727.h>
Woogyom Kim2165c8a2012-01-04 08:27:43 +040019
Kim, Milo638555d2012-08-31 09:24:09 +000020#define LP8788_NUM_INTREGS 2
Kim, Milo60fd57e2012-08-31 09:23:25 +000021#define DEFAULT_DEBOUNCE_MSEC 270
Woogyom Kim2165c8a2012-01-04 08:27:43 +040022
23/* Registers */
Kim, Milo60297192012-08-31 09:24:51 +000024#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 Kim2165c8a2012-01-04 08:27:43 +040032
33/* CTRL1 register */
Kim, Milo60297192012-08-31 09:24:51 +000034#define LP8727_CP_EN BIT(0)
35#define LP8727_ADC_EN BIT(1)
36#define LP8727_ID200_EN BIT(4)
Woogyom Kim2165c8a2012-01-04 08:27:43 +040037
38/* CTRL2 register */
Kim, Milo60297192012-08-31 09:24:51 +000039#define LP8727_CHGDET_EN BIT(1)
40#define LP8727_INT_EN BIT(6)
Woogyom Kim2165c8a2012-01-04 08:27:43 +040041
42/* SWCTRL register */
Kim, Milo60297192012-08-31 09:24:51 +000043#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 Kim2165c8a2012-01-04 08:27:43 +040047
48/* INT1 register */
Kim, Milo60297192012-08-31 09:24:51 +000049#define LP8727_IDNO (0xF << 0)
50#define LP8727_VBUS BIT(4)
Woogyom Kim2165c8a2012-01-04 08:27:43 +040051
52/* STATUS1 register */
Kim, Milo60297192012-08-31 09:24:51 +000053#define LP8727_CHGSTAT (3 << 4)
54#define LP8727_CHPORT BIT(6)
55#define LP8727_DCPORT BIT(7)
56#define LP8727_STAT_EOC 0x30
Woogyom Kim2165c8a2012-01-04 08:27:43 +040057
58/* STATUS2 register */
Kim, Milo60297192012-08-31 09:24:51 +000059#define LP8727_TEMP_STAT (3 << 5)
60#define LP8727_TEMP_SHIFT 5
Woogyom Kim2165c8a2012-01-04 08:27:43 +040061
Kim, Milo18763a32012-08-31 09:25:07 +000062/* CHGCTRL2 register */
63#define LP8727_ICHG_SHIFT 4
64
Woogyom Kim2165c8a2012-01-04 08:27:43 +040065enum lp8727_dev_id {
Kim, Milo60297192012-08-31 09:24:51 +000066 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 Kim2165c8a2012-01-04 08:27:43 +040072};
73
Kim, Milob1ad0792012-08-31 09:24:28 +000074enum lp8727_die_temp {
75 LP8788_TEMP_75C,
76 LP8788_TEMP_95C,
77 LP8788_TEMP_115C,
78 LP8788_TEMP_135C,
79};
80
Woogyom Kim2165c8a2012-01-04 08:27:43 +040081struct lp8727_psy {
82 struct power_supply ac;
83 struct power_supply usb;
84 struct power_supply batt;
85};
86
87struct lp8727_chg {
88 struct device *dev;
89 struct i2c_client *client;
90 struct mutex xfer_lock;
91 struct delayed_work work;
Woogyom Kim2165c8a2012-01-04 08:27:43 +040092 struct lp8727_platform_data *pdata;
93 struct lp8727_psy *psy;
Kim, Milo56d99ed2012-08-31 09:26:49 +000094 struct lp8727_chg_param *chg_param;
Woogyom Kim2165c8a2012-01-04 08:27:43 +040095 enum lp8727_dev_id devid;
Kim, Milo60fd57e2012-08-31 09:23:25 +000096 unsigned long debounce_jiffies;
Kim, Milod71fda02012-08-31 09:23:57 +000097 int irq;
Woogyom Kim2165c8a2012-01-04 08:27:43 +040098};
99
Kim, Milo27aefa32012-01-26 22:58:39 -0800100static int lp8727_read_bytes(struct lp8727_chg *pchg, u8 reg, u8 *data, u8 len)
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400101{
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, Milo27aefa32012-01-26 22:58:39 -0800111static inline int lp8727_read_byte(struct lp8727_chg *pchg, u8 reg, u8 *data)
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400112{
Kim, Milo27aefa32012-01-26 22:58:39 -0800113 return lp8727_read_bytes(pchg, reg, data, 1);
114}
115
116static int lp8727_write_byte(struct lp8727_chg *pchg, u8 reg, u8 data)
117{
118 int ret;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400119
120 mutex_lock(&pchg->xfer_lock);
Kim, Milo27aefa32012-01-26 22:58:39 -0800121 ret = i2c_smbus_write_byte_data(pchg->client, reg, data);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400122 mutex_unlock(&pchg->xfer_lock);
123
124 return ret;
125}
126
Kim, Milo65272ba2012-08-31 09:25:22 +0000127static bool lp8727_is_charger_attached(const char *name, int id)
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400128{
Kim, Milo65272ba2012-08-31 09:25:22 +0000129 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 Kim2165c8a2012-01-04 08:27:43 +0400133
Kim, Milo65272ba2012-08-31 09:25:22 +0000134 return id >= LP8727_ID_TA && id <= LP8727_ID_USB_CHG;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400135}
136
Kim, Milo7da63342012-01-26 22:58:30 -0800137static int lp8727_init_device(struct lp8727_chg *pchg)
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400138{
139 u8 val;
Kim, Milo7da63342012-01-26 22:58:30 -0800140 int ret;
Kim, Milo638555d2012-08-31 09:24:09 +0000141 u8 intstat[LP8788_NUM_INTREGS];
142
143 /* clear interrupts */
Kim, Milo60297192012-08-31 09:24:51 +0000144 ret = lp8727_read_bytes(pchg, LP8727_INT1, intstat, LP8788_NUM_INTREGS);
Kim, Milo638555d2012-08-31 09:24:09 +0000145 if (ret)
146 return ret;
147
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400148
Kim, Milo60297192012-08-31 09:24:51 +0000149 val = LP8727_ID200_EN | LP8727_ADC_EN | LP8727_CP_EN;
150 ret = lp8727_write_byte(pchg, LP8727_CTRL1, val);
Kim, Milo7da63342012-01-26 22:58:30 -0800151 if (ret)
152 return ret;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400153
Kim, Milo60297192012-08-31 09:24:51 +0000154 val = LP8727_INT_EN | LP8727_CHGDET_EN;
Kim, Milo70be1302012-08-31 09:25:34 +0000155 return lp8727_write_byte(pchg, LP8727_CTRL2, val);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400156}
157
158static int lp8727_is_dedicated_charger(struct lp8727_chg *pchg)
159{
160 u8 val;
Kim, Milo60297192012-08-31 09:24:51 +0000161 lp8727_read_byte(pchg, LP8727_STATUS1, &val);
162 return val & LP8727_DCPORT;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400163}
164
165static int lp8727_is_usb_charger(struct lp8727_chg *pchg)
166{
167 u8 val;
Kim, Milo60297192012-08-31 09:24:51 +0000168 lp8727_read_byte(pchg, LP8727_STATUS1, &val);
169 return val & LP8727_CHPORT;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400170}
171
Kim, Milo91a69632012-08-31 09:25:45 +0000172static inline void lp8727_ctrl_switch(struct lp8727_chg *pchg, u8 sw)
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400173{
Kim, Milo60297192012-08-31 09:24:51 +0000174 lp8727_write_byte(pchg, LP8727_SWCTRL, sw);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400175}
176
177static void lp8727_id_detection(struct lp8727_chg *pchg, u8 id, int vbusin)
178{
Kim, Milo318cb382012-08-31 09:23:12 +0000179 struct lp8727_platform_data *pdata = pchg->pdata;
Kim, Milo60297192012-08-31 09:24:51 +0000180 u8 devid = LP8727_ID_NONE;
181 u8 swctrl = LP8727_SW_DM1_HiZ | LP8727_SW_DP2_HiZ;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400182
183 switch (id) {
184 case 0x5:
Kim, Milo60297192012-08-31 09:24:51 +0000185 devid = LP8727_ID_TA;
Kim, Milo56d99ed2012-08-31 09:26:49 +0000186 pchg->chg_param = pdata ? pdata->ac : NULL;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400187 break;
188 case 0xB:
189 if (lp8727_is_dedicated_charger(pchg)) {
Kim, Milo56d99ed2012-08-31 09:26:49 +0000190 pchg->chg_param = pdata ? pdata->ac : NULL;
Kim, Milo60297192012-08-31 09:24:51 +0000191 devid = LP8727_ID_DEDICATED_CHG;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400192 } else if (lp8727_is_usb_charger(pchg)) {
Kim, Milo56d99ed2012-08-31 09:26:49 +0000193 pchg->chg_param = pdata ? pdata->usb : NULL;
Kim, Milo60297192012-08-31 09:24:51 +0000194 devid = LP8727_ID_USB_CHG;
195 swctrl = LP8727_SW_DM1_DM | LP8727_SW_DP2_DP;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400196 } else if (vbusin) {
Kim, Milo60297192012-08-31 09:24:51 +0000197 devid = LP8727_ID_USB_DS;
198 swctrl = LP8727_SW_DM1_DM | LP8727_SW_DP2_DP;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400199 }
200 break;
201 default:
Kim, Milo60297192012-08-31 09:24:51 +0000202 devid = LP8727_ID_NONE;
Kim, Milo56d99ed2012-08-31 09:26:49 +0000203 pchg->chg_param = NULL;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400204 break;
205 }
206
207 pchg->devid = devid;
208 lp8727_ctrl_switch(pchg, swctrl);
209}
210
211static void lp8727_enable_chgdet(struct lp8727_chg *pchg)
212{
213 u8 val;
214
Kim, Milo60297192012-08-31 09:24:51 +0000215 lp8727_read_byte(pchg, LP8727_CTRL2, &val);
216 val |= LP8727_CHGDET_EN;
217 lp8727_write_byte(pchg, LP8727_CTRL2, val);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400218}
219
220static void lp8727_delayed_func(struct work_struct *_work)
221{
Kim, Milo5e00b972012-08-31 09:26:33 +0000222 struct lp8727_chg *pchg = container_of(_work, struct lp8727_chg,
223 work.work);
224 u8 intstat[LP8788_NUM_INTREGS];
225 u8 idno;
226 u8 vbus;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400227
Kim, Milo18763a32012-08-31 09:25:07 +0000228 if (lp8727_read_bytes(pchg, LP8727_INT1, intstat, LP8788_NUM_INTREGS)) {
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400229 dev_err(pchg->dev, "can not read INT registers\n");
230 return;
231 }
232
Kim, Milo60297192012-08-31 09:24:51 +0000233 idno = intstat[0] & LP8727_IDNO;
234 vbus = intstat[0] & LP8727_VBUS;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400235
236 lp8727_id_detection(pchg, idno, vbus);
237 lp8727_enable_chgdet(pchg);
238
239 power_supply_changed(&pchg->psy->ac);
240 power_supply_changed(&pchg->psy->usb);
241 power_supply_changed(&pchg->psy->batt);
242}
243
244static irqreturn_t lp8727_isr_func(int irq, void *ptr)
245{
246 struct lp8727_chg *pchg = ptr;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400247
Kim, Milo2a092582012-08-31 09:23:41 +0000248 schedule_delayed_work(&pchg->work, pchg->debounce_jiffies);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400249 return IRQ_HANDLED;
250}
251
Kim, Milod71fda02012-08-31 09:23:57 +0000252static int lp8727_setup_irq(struct lp8727_chg *pchg)
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400253{
Kim, Milod71fda02012-08-31 09:23:57 +0000254 int ret;
255 int irq = pchg->client->irq;
Kim, Milo60fd57e2012-08-31 09:23:25 +0000256 unsigned delay_msec = pchg->pdata ? pchg->pdata->debounce_msec :
257 DEFAULT_DEBOUNCE_MSEC;
258
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400259 INIT_DELAYED_WORK(&pchg->work, lp8727_delayed_func);
260
Kim, Milod71fda02012-08-31 09:23:57 +0000261 if (irq <= 0) {
262 dev_warn(pchg->dev, "invalid irq number: %d\n", irq);
263 return 0;
264 }
265
266 ret = request_threaded_irq(irq, NULL, lp8727_isr_func,
267 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
268 "lp8727_irq", pchg);
269
270 if (ret)
271 return ret;
272
273 pchg->irq = irq;
Kim, Milo60fd57e2012-08-31 09:23:25 +0000274 pchg->debounce_jiffies = msecs_to_jiffies(delay_msec);
275
Kim, Milod71fda02012-08-31 09:23:57 +0000276 return 0;
277}
278
279static void lp8727_release_irq(struct lp8727_chg *pchg)
280{
281 cancel_delayed_work_sync(&pchg->work);
282
283 if (pchg->irq)
284 free_irq(pchg->irq, pchg);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400285}
286
287static enum power_supply_property lp8727_charger_prop[] = {
288 POWER_SUPPLY_PROP_ONLINE,
289};
290
291static enum power_supply_property lp8727_battery_prop[] = {
292 POWER_SUPPLY_PROP_STATUS,
293 POWER_SUPPLY_PROP_HEALTH,
294 POWER_SUPPLY_PROP_PRESENT,
295 POWER_SUPPLY_PROP_VOLTAGE_NOW,
296 POWER_SUPPLY_PROP_CAPACITY,
297 POWER_SUPPLY_PROP_TEMP,
298};
299
300static char *battery_supplied_to[] = {
301 "main_batt",
302};
303
304static int lp8727_charger_get_property(struct power_supply *psy,
305 enum power_supply_property psp,
306 union power_supply_propval *val)
307{
308 struct lp8727_chg *pchg = dev_get_drvdata(psy->dev->parent);
309
Kim, Milob59c93c02012-08-31 09:25:55 +0000310 if (psp != POWER_SUPPLY_PROP_ONLINE)
311 return -EINVAL;
312
313 val->intval = lp8727_is_charger_attached(psy->name, pchg->devid);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400314
315 return 0;
316}
317
Kim, Milob1ad0792012-08-31 09:24:28 +0000318static 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 Kim2165c8a2012-01-04 08:27:43 +0400330static 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, Milo318cb382012-08-31 09:23:12 +0000335 struct lp8727_platform_data *pdata = pchg->pdata;
Kim, Milob1ad0792012-08-31 09:24:28 +0000336 enum lp8727_die_temp temp;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400337 u8 read;
338
339 switch (psp) {
340 case POWER_SUPPLY_PROP_STATUS:
Kim, Miloe06374b2012-08-31 09:26:06 +0000341 if (!lp8727_is_charger_attached(psy->name, pchg->devid)) {
342 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
343 return 0;
344 }
Kim, Milofaaae9b2012-08-31 09:24:37 +0000345
Kim, Miloe06374b2012-08-31 09:26:06 +0000346 lp8727_read_byte(pchg, LP8727_STATUS1, &read);
347
348 val->intval = (read & LP8727_CHGSTAT) == LP8727_STAT_EOC ?
Kim, Milofaaae9b2012-08-31 09:24:37 +0000349 POWER_SUPPLY_STATUS_FULL :
350 POWER_SUPPLY_STATUS_CHARGING;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400351 break;
352 case POWER_SUPPLY_PROP_HEALTH:
Kim, Milo60297192012-08-31 09:24:51 +0000353 lp8727_read_byte(pchg, LP8727_STATUS2, &read);
354 temp = (read & LP8727_TEMP_STAT) >> LP8727_TEMP_SHIFT;
Kim, Milob1ad0792012-08-31 09:24:28 +0000355
356 val->intval = lp8727_is_high_temperature(temp) ?
357 POWER_SUPPLY_HEALTH_OVERHEAT :
358 POWER_SUPPLY_HEALTH_GOOD;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400359 break;
360 case POWER_SUPPLY_PROP_PRESENT:
Kim, Milo318cb382012-08-31 09:23:12 +0000361 if (!pdata)
362 return -EINVAL;
363
364 if (pdata->get_batt_present)
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400365 val->intval = pchg->pdata->get_batt_present();
366 break;
367 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Kim, Milo318cb382012-08-31 09:23:12 +0000368 if (!pdata)
369 return -EINVAL;
370
371 if (pdata->get_batt_level)
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400372 val->intval = pchg->pdata->get_batt_level();
373 break;
374 case POWER_SUPPLY_PROP_CAPACITY:
Kim, Milo318cb382012-08-31 09:23:12 +0000375 if (!pdata)
376 return -EINVAL;
377
378 if (pdata->get_batt_capacity)
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400379 val->intval = pchg->pdata->get_batt_capacity();
380 break;
381 case POWER_SUPPLY_PROP_TEMP:
Kim, Milo318cb382012-08-31 09:23:12 +0000382 if (!pdata)
383 return -EINVAL;
384
385 if (pdata->get_batt_temp)
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400386 val->intval = pchg->pdata->get_batt_temp();
387 break;
388 default:
389 break;
390 }
391
392 return 0;
393}
394
395static void lp8727_charger_changed(struct power_supply *psy)
396{
397 struct lp8727_chg *pchg = dev_get_drvdata(psy->dev->parent);
Kim, Milo20414e22012-08-31 09:26:22 +0000398 u8 eoc_level;
399 u8 ichg;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400400 u8 val;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400401
Kim, Milo20414e22012-08-31 09:26:22 +0000402 /* skip if no charger exists */
403 if (!lp8727_is_charger_attached(psy->name, pchg->devid))
404 return;
405
406 /* update charging parameters */
Kim, Milo56d99ed2012-08-31 09:26:49 +0000407 if (pchg->chg_param) {
408 eoc_level = pchg->chg_param->eoc_level;
409 ichg = pchg->chg_param->ichg;
Kim, Milo20414e22012-08-31 09:26:22 +0000410 val = (ichg << LP8727_ICHG_SHIFT) | eoc_level;
411 lp8727_write_byte(pchg, LP8727_CHGCTRL2, val);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400412 }
413}
414
415static int lp8727_register_psy(struct lp8727_chg *pchg)
416{
417 struct lp8727_psy *psy;
418
Kim, Milo74727c52012-08-31 09:22:46 +0000419 psy = devm_kzalloc(pchg->dev, sizeof(*psy), GFP_KERNEL);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400420 if (!psy)
Devendra Naga6297b5e2012-07-29 23:31:55 +0545421 return -ENOMEM;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400422
423 pchg->psy = psy;
424
425 psy->ac.name = "ac";
426 psy->ac.type = POWER_SUPPLY_TYPE_MAINS;
427 psy->ac.properties = lp8727_charger_prop;
428 psy->ac.num_properties = ARRAY_SIZE(lp8727_charger_prop);
429 psy->ac.get_property = lp8727_charger_get_property;
430 psy->ac.supplied_to = battery_supplied_to;
431 psy->ac.num_supplicants = ARRAY_SIZE(battery_supplied_to);
432
433 if (power_supply_register(pchg->dev, &psy->ac))
Devendra Naga6297b5e2012-07-29 23:31:55 +0545434 goto err_psy_ac;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400435
436 psy->usb.name = "usb";
437 psy->usb.type = POWER_SUPPLY_TYPE_USB;
438 psy->usb.properties = lp8727_charger_prop;
439 psy->usb.num_properties = ARRAY_SIZE(lp8727_charger_prop);
440 psy->usb.get_property = lp8727_charger_get_property;
441 psy->usb.supplied_to = battery_supplied_to;
442 psy->usb.num_supplicants = ARRAY_SIZE(battery_supplied_to);
443
444 if (power_supply_register(pchg->dev, &psy->usb))
Devendra Naga6297b5e2012-07-29 23:31:55 +0545445 goto err_psy_usb;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400446
447 psy->batt.name = "main_batt";
448 psy->batt.type = POWER_SUPPLY_TYPE_BATTERY;
449 psy->batt.properties = lp8727_battery_prop;
450 psy->batt.num_properties = ARRAY_SIZE(lp8727_battery_prop);
451 psy->batt.get_property = lp8727_battery_get_property;
452 psy->batt.external_power_changed = lp8727_charger_changed;
453
454 if (power_supply_register(pchg->dev, &psy->batt))
Devendra Naga6297b5e2012-07-29 23:31:55 +0545455 goto err_psy_batt;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400456
457 return 0;
458
Devendra Naga6297b5e2012-07-29 23:31:55 +0545459err_psy_batt:
460 power_supply_unregister(&psy->usb);
461err_psy_usb:
462 power_supply_unregister(&psy->ac);
463err_psy_ac:
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400464 return -EPERM;
465}
466
467static void lp8727_unregister_psy(struct lp8727_chg *pchg)
468{
469 struct lp8727_psy *psy = pchg->psy;
470
Kim, Miloce09aff2012-01-04 09:03:18 +0400471 if (!psy)
472 return;
473
474 power_supply_unregister(&psy->ac);
475 power_supply_unregister(&psy->usb);
476 power_supply_unregister(&psy->batt);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400477}
478
479static int lp8727_probe(struct i2c_client *cl, const struct i2c_device_id *id)
480{
481 struct lp8727_chg *pchg;
482 int ret;
483
Kim, Milo998a8e72011-11-17 21:43:06 -0800484 if (!i2c_check_functionality(cl->adapter, I2C_FUNC_SMBUS_I2C_BLOCK))
485 return -EIO;
486
Kim, Milo74727c52012-08-31 09:22:46 +0000487 pchg = devm_kzalloc(&cl->dev, sizeof(*pchg), GFP_KERNEL);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400488 if (!pchg)
489 return -ENOMEM;
490
491 pchg->client = cl;
492 pchg->dev = &cl->dev;
493 pchg->pdata = cl->dev.platform_data;
494 i2c_set_clientdata(cl, pchg);
495
496 mutex_init(&pchg->xfer_lock);
497
Kim, Milo7da63342012-01-26 22:58:30 -0800498 ret = lp8727_init_device(pchg);
499 if (ret) {
500 dev_err(pchg->dev, "i2c communication err: %d", ret);
Kim, Milofb9adc52012-08-31 09:23:03 +0000501 return ret;
Kim, Milo7da63342012-01-26 22:58:30 -0800502 }
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400503
504 ret = lp8727_register_psy(pchg);
Kim, Milo7da63342012-01-26 22:58:30 -0800505 if (ret) {
506 dev_err(pchg->dev, "power supplies register err: %d", ret);
Kim, Milofb9adc52012-08-31 09:23:03 +0000507 return ret;
508 }
509
Kim, Milod71fda02012-08-31 09:23:57 +0000510 ret = lp8727_setup_irq(pchg);
Kim, Milofb9adc52012-08-31 09:23:03 +0000511 if (ret) {
512 dev_err(pchg->dev, "irq handler err: %d", ret);
513 lp8727_unregister_psy(pchg);
514 return ret;
Kim, Milo7da63342012-01-26 22:58:30 -0800515 }
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400516
517 return 0;
518}
519
520static int __devexit lp8727_remove(struct i2c_client *cl)
521{
522 struct lp8727_chg *pchg = i2c_get_clientdata(cl);
523
Kim, Milod71fda02012-08-31 09:23:57 +0000524 lp8727_release_irq(pchg);
Kim, Milofb9adc52012-08-31 09:23:03 +0000525 lp8727_unregister_psy(pchg);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400526 return 0;
527}
528
529static const struct i2c_device_id lp8727_ids[] = {
530 {"lp8727", 0},
Axel Lin455a0e22012-01-16 13:48:20 +0800531 { }
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400532};
Axel Line2c5f7d2012-01-12 20:45:02 +0800533MODULE_DEVICE_TABLE(i2c, lp8727_ids);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400534
535static struct i2c_driver lp8727_driver = {
536 .driver = {
537 .name = "lp8727",
538 },
539 .probe = lp8727_probe,
540 .remove = __devexit_p(lp8727_remove),
541 .id_table = lp8727_ids,
542};
Axel Lin5ff92e72012-01-21 14:42:54 +0800543module_i2c_driver(lp8727_driver);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400544
Kim, Miloe39b8282012-01-29 17:28:18 -0800545MODULE_DESCRIPTION("TI/National Semiconductor LP8727 charger driver");
Kim, Milo73368802012-01-26 22:58:51 -0800546MODULE_AUTHOR("Woogyom Kim <milo.kim@ti.com>, "
547 "Daniel Jeong <daniel.jeong@ti.com>");
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400548MODULE_LICENSE("GPL");