blob: 880bb7b982336030055b400a9423c130e2f8ff77 [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;
94 struct lp8727_chg_param *chg_parm;
95 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, Milo318cb382012-08-31 09:23:12 +0000186 pchg->chg_parm = pdata ? pdata->ac : NULL;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400187 break;
188 case 0xB:
189 if (lp8727_is_dedicated_charger(pchg)) {
Kim, Milo318cb382012-08-31 09:23:12 +0000190 pchg->chg_parm = 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, Milo318cb382012-08-31 09:23:12 +0000193 pchg->chg_parm = 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;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400203 pchg->chg_parm = NULL;
204 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, Milo18763a32012-08-31 09:25:07 +0000222 u8 intstat[LP8788_NUM_INTREGS], idno, vbus;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400223 struct lp8727_chg *pchg =
224 container_of(_work, struct lp8727_chg, work.work);
225
Kim, Milo18763a32012-08-31 09:25:07 +0000226 if (lp8727_read_bytes(pchg, LP8727_INT1, intstat, LP8788_NUM_INTREGS)) {
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400227 dev_err(pchg->dev, "can not read INT registers\n");
228 return;
229 }
230
Kim, Milo60297192012-08-31 09:24:51 +0000231 idno = intstat[0] & LP8727_IDNO;
232 vbus = intstat[0] & LP8727_VBUS;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400233
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
242static irqreturn_t lp8727_isr_func(int irq, void *ptr)
243{
244 struct lp8727_chg *pchg = ptr;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400245
Kim, Milo2a092582012-08-31 09:23:41 +0000246 schedule_delayed_work(&pchg->work, pchg->debounce_jiffies);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400247 return IRQ_HANDLED;
248}
249
Kim, Milod71fda02012-08-31 09:23:57 +0000250static int lp8727_setup_irq(struct lp8727_chg *pchg)
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400251{
Kim, Milod71fda02012-08-31 09:23:57 +0000252 int ret;
253 int irq = pchg->client->irq;
Kim, Milo60fd57e2012-08-31 09:23:25 +0000254 unsigned delay_msec = pchg->pdata ? pchg->pdata->debounce_msec :
255 DEFAULT_DEBOUNCE_MSEC;
256
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400257 INIT_DELAYED_WORK(&pchg->work, lp8727_delayed_func);
258
Kim, Milod71fda02012-08-31 09:23:57 +0000259 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, Milo60fd57e2012-08-31 09:23:25 +0000272 pchg->debounce_jiffies = msecs_to_jiffies(delay_msec);
273
Kim, Milod71fda02012-08-31 09:23:57 +0000274 return 0;
275}
276
277static 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 Kim2165c8a2012-01-04 08:27:43 +0400283}
284
285static enum power_supply_property lp8727_charger_prop[] = {
286 POWER_SUPPLY_PROP_ONLINE,
287};
288
289static 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
298static char *battery_supplied_to[] = {
299 "main_batt",
300};
301
302static 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, Miloce09aff2012-01-04 09:03:18 +0400308 if (psp == POWER_SUPPLY_PROP_ONLINE)
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400309 val->intval = lp8727_is_charger_attached(psy->name,
310 pchg->devid);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400311
312 return 0;
313}
314
Kim, Milob1ad0792012-08-31 09:24:28 +0000315static 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 Kim2165c8a2012-01-04 08:27:43 +0400327static 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, Milo318cb382012-08-31 09:23:12 +0000332 struct lp8727_platform_data *pdata = pchg->pdata;
Kim, Milob1ad0792012-08-31 09:24:28 +0000333 enum lp8727_die_temp temp;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400334 u8 read;
335
336 switch (psp) {
337 case POWER_SUPPLY_PROP_STATUS:
338 if (lp8727_is_charger_attached(psy->name, pchg->devid)) {
Kim, Milo60297192012-08-31 09:24:51 +0000339 lp8727_read_byte(pchg, LP8727_STATUS1, &read);
Kim, Milofaaae9b2012-08-31 09:24:37 +0000340
Kim, Milo60297192012-08-31 09:24:51 +0000341 val->intval = (read & LP8727_CHGSTAT) == LP8727_STAT_EOC ?
Kim, Milofaaae9b2012-08-31 09:24:37 +0000342 POWER_SUPPLY_STATUS_FULL :
343 POWER_SUPPLY_STATUS_CHARGING;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400344 } else {
345 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
346 }
347 break;
348 case POWER_SUPPLY_PROP_HEALTH:
Kim, Milo60297192012-08-31 09:24:51 +0000349 lp8727_read_byte(pchg, LP8727_STATUS2, &read);
350 temp = (read & LP8727_TEMP_STAT) >> LP8727_TEMP_SHIFT;
Kim, Milob1ad0792012-08-31 09:24:28 +0000351
352 val->intval = lp8727_is_high_temperature(temp) ?
353 POWER_SUPPLY_HEALTH_OVERHEAT :
354 POWER_SUPPLY_HEALTH_GOOD;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400355 break;
356 case POWER_SUPPLY_PROP_PRESENT:
Kim, Milo318cb382012-08-31 09:23:12 +0000357 if (!pdata)
358 return -EINVAL;
359
360 if (pdata->get_batt_present)
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400361 val->intval = pchg->pdata->get_batt_present();
362 break;
363 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Kim, Milo318cb382012-08-31 09:23:12 +0000364 if (!pdata)
365 return -EINVAL;
366
367 if (pdata->get_batt_level)
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400368 val->intval = pchg->pdata->get_batt_level();
369 break;
370 case POWER_SUPPLY_PROP_CAPACITY:
Kim, Milo318cb382012-08-31 09:23:12 +0000371 if (!pdata)
372 return -EINVAL;
373
374 if (pdata->get_batt_capacity)
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400375 val->intval = pchg->pdata->get_batt_capacity();
376 break;
377 case POWER_SUPPLY_PROP_TEMP:
Kim, Milo318cb382012-08-31 09:23:12 +0000378 if (!pdata)
379 return -EINVAL;
380
381 if (pdata->get_batt_temp)
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400382 val->intval = pchg->pdata->get_batt_temp();
383 break;
384 default:
385 break;
386 }
387
388 return 0;
389}
390
391static 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, Milo18763a32012-08-31 09:25:07 +0000401 val = (ichg << LP8727_ICHG_SHIFT) | eoc_level;
Kim, Milo60297192012-08-31 09:24:51 +0000402 lp8727_write_byte(pchg, LP8727_CHGCTRL2, val);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400403 }
404 }
405}
406
407static int lp8727_register_psy(struct lp8727_chg *pchg)
408{
409 struct lp8727_psy *psy;
410
Kim, Milo74727c52012-08-31 09:22:46 +0000411 psy = devm_kzalloc(pchg->dev, sizeof(*psy), GFP_KERNEL);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400412 if (!psy)
Devendra Naga6297b5e2012-07-29 23:31:55 +0545413 return -ENOMEM;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400414
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 Naga6297b5e2012-07-29 23:31:55 +0545426 goto err_psy_ac;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400427
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 Naga6297b5e2012-07-29 23:31:55 +0545437 goto err_psy_usb;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400438
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 Naga6297b5e2012-07-29 23:31:55 +0545447 goto err_psy_batt;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400448
449 return 0;
450
Devendra Naga6297b5e2012-07-29 23:31:55 +0545451err_psy_batt:
452 power_supply_unregister(&psy->usb);
453err_psy_usb:
454 power_supply_unregister(&psy->ac);
455err_psy_ac:
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400456 return -EPERM;
457}
458
459static void lp8727_unregister_psy(struct lp8727_chg *pchg)
460{
461 struct lp8727_psy *psy = pchg->psy;
462
Kim, Miloce09aff2012-01-04 09:03:18 +0400463 if (!psy)
464 return;
465
466 power_supply_unregister(&psy->ac);
467 power_supply_unregister(&psy->usb);
468 power_supply_unregister(&psy->batt);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400469}
470
471static int lp8727_probe(struct i2c_client *cl, const struct i2c_device_id *id)
472{
473 struct lp8727_chg *pchg;
474 int ret;
475
Kim, Milo998a8e72011-11-17 21:43:06 -0800476 if (!i2c_check_functionality(cl->adapter, I2C_FUNC_SMBUS_I2C_BLOCK))
477 return -EIO;
478
Kim, Milo74727c52012-08-31 09:22:46 +0000479 pchg = devm_kzalloc(&cl->dev, sizeof(*pchg), GFP_KERNEL);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400480 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, Milo7da63342012-01-26 22:58:30 -0800490 ret = lp8727_init_device(pchg);
491 if (ret) {
492 dev_err(pchg->dev, "i2c communication err: %d", ret);
Kim, Milofb9adc52012-08-31 09:23:03 +0000493 return ret;
Kim, Milo7da63342012-01-26 22:58:30 -0800494 }
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400495
496 ret = lp8727_register_psy(pchg);
Kim, Milo7da63342012-01-26 22:58:30 -0800497 if (ret) {
498 dev_err(pchg->dev, "power supplies register err: %d", ret);
Kim, Milofb9adc52012-08-31 09:23:03 +0000499 return ret;
500 }
501
Kim, Milod71fda02012-08-31 09:23:57 +0000502 ret = lp8727_setup_irq(pchg);
Kim, Milofb9adc52012-08-31 09:23:03 +0000503 if (ret) {
504 dev_err(pchg->dev, "irq handler err: %d", ret);
505 lp8727_unregister_psy(pchg);
506 return ret;
Kim, Milo7da63342012-01-26 22:58:30 -0800507 }
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400508
509 return 0;
510}
511
512static int __devexit lp8727_remove(struct i2c_client *cl)
513{
514 struct lp8727_chg *pchg = i2c_get_clientdata(cl);
515
Kim, Milod71fda02012-08-31 09:23:57 +0000516 lp8727_release_irq(pchg);
Kim, Milofb9adc52012-08-31 09:23:03 +0000517 lp8727_unregister_psy(pchg);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400518 return 0;
519}
520
521static const struct i2c_device_id lp8727_ids[] = {
522 {"lp8727", 0},
Axel Lin455a0e22012-01-16 13:48:20 +0800523 { }
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400524};
Axel Line2c5f7d2012-01-12 20:45:02 +0800525MODULE_DEVICE_TABLE(i2c, lp8727_ids);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400526
527static 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 Lin5ff92e72012-01-21 14:42:54 +0800535module_i2c_driver(lp8727_driver);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400536
Kim, Miloe39b8282012-01-29 17:28:18 -0800537MODULE_DESCRIPTION("TI/National Semiconductor LP8727 charger driver");
Kim, Milo73368802012-01-26 22:58:51 -0800538MODULE_AUTHOR("Woogyom Kim <milo.kim@ti.com>, "
539 "Daniel Jeong <daniel.jeong@ti.com>");
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400540MODULE_LICENSE("GPL");