blob: 254bbe07c269ba78e50411ec5b4c64d1d0fb123b [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
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400127static int lp8727_is_charger_attached(const char *name, int id)
128{
129 if (name) {
130 if (!strcmp(name, "ac"))
Kim, Milo60297192012-08-31 09:24:51 +0000131 return (id == LP8727_ID_TA || id == LP8727_ID_DEDICATED_CHG) ? 1 : 0;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400132 else if (!strcmp(name, "usb"))
Kim, Milo60297192012-08-31 09:24:51 +0000133 return (id == LP8727_ID_USB_CHG) ? 1 : 0;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400134 }
135
Kim, Milo60297192012-08-31 09:24:51 +0000136 return (id >= LP8727_ID_TA && id <= LP8727_ID_USB_CHG) ? 1 : 0;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400137}
138
Kim, Milo7da63342012-01-26 22:58:30 -0800139static int lp8727_init_device(struct lp8727_chg *pchg)
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400140{
141 u8 val;
Kim, Milo7da63342012-01-26 22:58:30 -0800142 int ret;
Kim, Milo638555d2012-08-31 09:24:09 +0000143 u8 intstat[LP8788_NUM_INTREGS];
144
145 /* clear interrupts */
Kim, Milo60297192012-08-31 09:24:51 +0000146 ret = lp8727_read_bytes(pchg, LP8727_INT1, intstat, LP8788_NUM_INTREGS);
Kim, Milo638555d2012-08-31 09:24:09 +0000147 if (ret)
148 return ret;
149
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400150
Kim, Milo60297192012-08-31 09:24:51 +0000151 val = LP8727_ID200_EN | LP8727_ADC_EN | LP8727_CP_EN;
152 ret = lp8727_write_byte(pchg, LP8727_CTRL1, val);
Kim, Milo7da63342012-01-26 22:58:30 -0800153 if (ret)
154 return ret;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400155
Kim, Milo60297192012-08-31 09:24:51 +0000156 val = LP8727_INT_EN | LP8727_CHGDET_EN;
157 ret = lp8727_write_byte(pchg, LP8727_CTRL2, val);
Kim, Milo7da63342012-01-26 22:58:30 -0800158 if (ret)
159 return ret;
160
161 return 0;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400162}
163
164static int lp8727_is_dedicated_charger(struct lp8727_chg *pchg)
165{
166 u8 val;
Kim, Milo60297192012-08-31 09:24:51 +0000167 lp8727_read_byte(pchg, LP8727_STATUS1, &val);
168 return val & LP8727_DCPORT;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400169}
170
171static int lp8727_is_usb_charger(struct lp8727_chg *pchg)
172{
173 u8 val;
Kim, Milo60297192012-08-31 09:24:51 +0000174 lp8727_read_byte(pchg, LP8727_STATUS1, &val);
175 return val & LP8727_CHPORT;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400176}
177
178static void lp8727_ctrl_switch(struct lp8727_chg *pchg, u8 sw)
179{
Kim, Milo60297192012-08-31 09:24:51 +0000180 lp8727_write_byte(pchg, LP8727_SWCTRL, sw);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400181}
182
183static void lp8727_id_detection(struct lp8727_chg *pchg, u8 id, int vbusin)
184{
Kim, Milo318cb382012-08-31 09:23:12 +0000185 struct lp8727_platform_data *pdata = pchg->pdata;
Kim, Milo60297192012-08-31 09:24:51 +0000186 u8 devid = LP8727_ID_NONE;
187 u8 swctrl = LP8727_SW_DM1_HiZ | LP8727_SW_DP2_HiZ;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400188
189 switch (id) {
190 case 0x5:
Kim, Milo60297192012-08-31 09:24:51 +0000191 devid = LP8727_ID_TA;
Kim, Milo318cb382012-08-31 09:23:12 +0000192 pchg->chg_parm = pdata ? pdata->ac : NULL;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400193 break;
194 case 0xB:
195 if (lp8727_is_dedicated_charger(pchg)) {
Kim, Milo318cb382012-08-31 09:23:12 +0000196 pchg->chg_parm = pdata ? pdata->ac : NULL;
Kim, Milo60297192012-08-31 09:24:51 +0000197 devid = LP8727_ID_DEDICATED_CHG;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400198 } else if (lp8727_is_usb_charger(pchg)) {
Kim, Milo318cb382012-08-31 09:23:12 +0000199 pchg->chg_parm = pdata ? pdata->usb : NULL;
Kim, Milo60297192012-08-31 09:24:51 +0000200 devid = LP8727_ID_USB_CHG;
201 swctrl = LP8727_SW_DM1_DM | LP8727_SW_DP2_DP;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400202 } else if (vbusin) {
Kim, Milo60297192012-08-31 09:24:51 +0000203 devid = LP8727_ID_USB_DS;
204 swctrl = LP8727_SW_DM1_DM | LP8727_SW_DP2_DP;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400205 }
206 break;
207 default:
Kim, Milo60297192012-08-31 09:24:51 +0000208 devid = LP8727_ID_NONE;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400209 pchg->chg_parm = NULL;
210 break;
211 }
212
213 pchg->devid = devid;
214 lp8727_ctrl_switch(pchg, swctrl);
215}
216
217static void lp8727_enable_chgdet(struct lp8727_chg *pchg)
218{
219 u8 val;
220
Kim, Milo60297192012-08-31 09:24:51 +0000221 lp8727_read_byte(pchg, LP8727_CTRL2, &val);
222 val |= LP8727_CHGDET_EN;
223 lp8727_write_byte(pchg, LP8727_CTRL2, val);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400224}
225
226static void lp8727_delayed_func(struct work_struct *_work)
227{
Kim, Milo18763a32012-08-31 09:25:07 +0000228 u8 intstat[LP8788_NUM_INTREGS], idno, vbus;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400229 struct lp8727_chg *pchg =
230 container_of(_work, struct lp8727_chg, work.work);
231
Kim, Milo18763a32012-08-31 09:25:07 +0000232 if (lp8727_read_bytes(pchg, LP8727_INT1, intstat, LP8788_NUM_INTREGS)) {
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400233 dev_err(pchg->dev, "can not read INT registers\n");
234 return;
235 }
236
Kim, Milo60297192012-08-31 09:24:51 +0000237 idno = intstat[0] & LP8727_IDNO;
238 vbus = intstat[0] & LP8727_VBUS;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400239
240 lp8727_id_detection(pchg, idno, vbus);
241 lp8727_enable_chgdet(pchg);
242
243 power_supply_changed(&pchg->psy->ac);
244 power_supply_changed(&pchg->psy->usb);
245 power_supply_changed(&pchg->psy->batt);
246}
247
248static irqreturn_t lp8727_isr_func(int irq, void *ptr)
249{
250 struct lp8727_chg *pchg = ptr;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400251
Kim, Milo2a092582012-08-31 09:23:41 +0000252 schedule_delayed_work(&pchg->work, pchg->debounce_jiffies);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400253 return IRQ_HANDLED;
254}
255
Kim, Milod71fda02012-08-31 09:23:57 +0000256static int lp8727_setup_irq(struct lp8727_chg *pchg)
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400257{
Kim, Milod71fda02012-08-31 09:23:57 +0000258 int ret;
259 int irq = pchg->client->irq;
Kim, Milo60fd57e2012-08-31 09:23:25 +0000260 unsigned delay_msec = pchg->pdata ? pchg->pdata->debounce_msec :
261 DEFAULT_DEBOUNCE_MSEC;
262
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400263 INIT_DELAYED_WORK(&pchg->work, lp8727_delayed_func);
264
Kim, Milod71fda02012-08-31 09:23:57 +0000265 if (irq <= 0) {
266 dev_warn(pchg->dev, "invalid irq number: %d\n", irq);
267 return 0;
268 }
269
270 ret = request_threaded_irq(irq, NULL, lp8727_isr_func,
271 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
272 "lp8727_irq", pchg);
273
274 if (ret)
275 return ret;
276
277 pchg->irq = irq;
Kim, Milo60fd57e2012-08-31 09:23:25 +0000278 pchg->debounce_jiffies = msecs_to_jiffies(delay_msec);
279
Kim, Milod71fda02012-08-31 09:23:57 +0000280 return 0;
281}
282
283static void lp8727_release_irq(struct lp8727_chg *pchg)
284{
285 cancel_delayed_work_sync(&pchg->work);
286
287 if (pchg->irq)
288 free_irq(pchg->irq, pchg);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400289}
290
291static enum power_supply_property lp8727_charger_prop[] = {
292 POWER_SUPPLY_PROP_ONLINE,
293};
294
295static enum power_supply_property lp8727_battery_prop[] = {
296 POWER_SUPPLY_PROP_STATUS,
297 POWER_SUPPLY_PROP_HEALTH,
298 POWER_SUPPLY_PROP_PRESENT,
299 POWER_SUPPLY_PROP_VOLTAGE_NOW,
300 POWER_SUPPLY_PROP_CAPACITY,
301 POWER_SUPPLY_PROP_TEMP,
302};
303
304static char *battery_supplied_to[] = {
305 "main_batt",
306};
307
308static int lp8727_charger_get_property(struct power_supply *psy,
309 enum power_supply_property psp,
310 union power_supply_propval *val)
311{
312 struct lp8727_chg *pchg = dev_get_drvdata(psy->dev->parent);
313
Kim, Miloce09aff2012-01-04 09:03:18 +0400314 if (psp == POWER_SUPPLY_PROP_ONLINE)
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400315 val->intval = lp8727_is_charger_attached(psy->name,
316 pchg->devid);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400317
318 return 0;
319}
320
Kim, Milob1ad0792012-08-31 09:24:28 +0000321static bool lp8727_is_high_temperature(enum lp8727_die_temp temp)
322{
323 switch (temp) {
324 case LP8788_TEMP_95C:
325 case LP8788_TEMP_115C:
326 case LP8788_TEMP_135C:
327 return true;
328 default:
329 return false;
330 }
331}
332
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400333static int lp8727_battery_get_property(struct power_supply *psy,
334 enum power_supply_property psp,
335 union power_supply_propval *val)
336{
337 struct lp8727_chg *pchg = dev_get_drvdata(psy->dev->parent);
Kim, Milo318cb382012-08-31 09:23:12 +0000338 struct lp8727_platform_data *pdata = pchg->pdata;
Kim, Milob1ad0792012-08-31 09:24:28 +0000339 enum lp8727_die_temp temp;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400340 u8 read;
341
342 switch (psp) {
343 case POWER_SUPPLY_PROP_STATUS:
344 if (lp8727_is_charger_attached(psy->name, pchg->devid)) {
Kim, Milo60297192012-08-31 09:24:51 +0000345 lp8727_read_byte(pchg, LP8727_STATUS1, &read);
Kim, Milofaaae9b2012-08-31 09:24:37 +0000346
Kim, Milo60297192012-08-31 09:24:51 +0000347 val->intval = (read & LP8727_CHGSTAT) == LP8727_STAT_EOC ?
Kim, Milofaaae9b2012-08-31 09:24:37 +0000348 POWER_SUPPLY_STATUS_FULL :
349 POWER_SUPPLY_STATUS_CHARGING;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400350 } else {
351 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
352 }
353 break;
354 case POWER_SUPPLY_PROP_HEALTH:
Kim, Milo60297192012-08-31 09:24:51 +0000355 lp8727_read_byte(pchg, LP8727_STATUS2, &read);
356 temp = (read & LP8727_TEMP_STAT) >> LP8727_TEMP_SHIFT;
Kim, Milob1ad0792012-08-31 09:24:28 +0000357
358 val->intval = lp8727_is_high_temperature(temp) ?
359 POWER_SUPPLY_HEALTH_OVERHEAT :
360 POWER_SUPPLY_HEALTH_GOOD;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400361 break;
362 case POWER_SUPPLY_PROP_PRESENT:
Kim, Milo318cb382012-08-31 09:23:12 +0000363 if (!pdata)
364 return -EINVAL;
365
366 if (pdata->get_batt_present)
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400367 val->intval = pchg->pdata->get_batt_present();
368 break;
369 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Kim, Milo318cb382012-08-31 09:23:12 +0000370 if (!pdata)
371 return -EINVAL;
372
373 if (pdata->get_batt_level)
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400374 val->intval = pchg->pdata->get_batt_level();
375 break;
376 case POWER_SUPPLY_PROP_CAPACITY:
Kim, Milo318cb382012-08-31 09:23:12 +0000377 if (!pdata)
378 return -EINVAL;
379
380 if (pdata->get_batt_capacity)
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400381 val->intval = pchg->pdata->get_batt_capacity();
382 break;
383 case POWER_SUPPLY_PROP_TEMP:
Kim, Milo318cb382012-08-31 09:23:12 +0000384 if (!pdata)
385 return -EINVAL;
386
387 if (pdata->get_batt_temp)
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400388 val->intval = pchg->pdata->get_batt_temp();
389 break;
390 default:
391 break;
392 }
393
394 return 0;
395}
396
397static void lp8727_charger_changed(struct power_supply *psy)
398{
399 struct lp8727_chg *pchg = dev_get_drvdata(psy->dev->parent);
400 u8 val;
401 u8 eoc_level, ichg;
402
403 if (lp8727_is_charger_attached(psy->name, pchg->devid)) {
404 if (pchg->chg_parm) {
405 eoc_level = pchg->chg_parm->eoc_level;
406 ichg = pchg->chg_parm->ichg;
Kim, Milo18763a32012-08-31 09:25:07 +0000407 val = (ichg << LP8727_ICHG_SHIFT) | eoc_level;
Kim, Milo60297192012-08-31 09:24:51 +0000408 lp8727_write_byte(pchg, LP8727_CHGCTRL2, val);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400409 }
410 }
411}
412
413static int lp8727_register_psy(struct lp8727_chg *pchg)
414{
415 struct lp8727_psy *psy;
416
Kim, Milo74727c52012-08-31 09:22:46 +0000417 psy = devm_kzalloc(pchg->dev, sizeof(*psy), GFP_KERNEL);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400418 if (!psy)
Devendra Naga6297b5e2012-07-29 23:31:55 +0545419 return -ENOMEM;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400420
421 pchg->psy = psy;
422
423 psy->ac.name = "ac";
424 psy->ac.type = POWER_SUPPLY_TYPE_MAINS;
425 psy->ac.properties = lp8727_charger_prop;
426 psy->ac.num_properties = ARRAY_SIZE(lp8727_charger_prop);
427 psy->ac.get_property = lp8727_charger_get_property;
428 psy->ac.supplied_to = battery_supplied_to;
429 psy->ac.num_supplicants = ARRAY_SIZE(battery_supplied_to);
430
431 if (power_supply_register(pchg->dev, &psy->ac))
Devendra Naga6297b5e2012-07-29 23:31:55 +0545432 goto err_psy_ac;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400433
434 psy->usb.name = "usb";
435 psy->usb.type = POWER_SUPPLY_TYPE_USB;
436 psy->usb.properties = lp8727_charger_prop;
437 psy->usb.num_properties = ARRAY_SIZE(lp8727_charger_prop);
438 psy->usb.get_property = lp8727_charger_get_property;
439 psy->usb.supplied_to = battery_supplied_to;
440 psy->usb.num_supplicants = ARRAY_SIZE(battery_supplied_to);
441
442 if (power_supply_register(pchg->dev, &psy->usb))
Devendra Naga6297b5e2012-07-29 23:31:55 +0545443 goto err_psy_usb;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400444
445 psy->batt.name = "main_batt";
446 psy->batt.type = POWER_SUPPLY_TYPE_BATTERY;
447 psy->batt.properties = lp8727_battery_prop;
448 psy->batt.num_properties = ARRAY_SIZE(lp8727_battery_prop);
449 psy->batt.get_property = lp8727_battery_get_property;
450 psy->batt.external_power_changed = lp8727_charger_changed;
451
452 if (power_supply_register(pchg->dev, &psy->batt))
Devendra Naga6297b5e2012-07-29 23:31:55 +0545453 goto err_psy_batt;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400454
455 return 0;
456
Devendra Naga6297b5e2012-07-29 23:31:55 +0545457err_psy_batt:
458 power_supply_unregister(&psy->usb);
459err_psy_usb:
460 power_supply_unregister(&psy->ac);
461err_psy_ac:
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400462 return -EPERM;
463}
464
465static void lp8727_unregister_psy(struct lp8727_chg *pchg)
466{
467 struct lp8727_psy *psy = pchg->psy;
468
Kim, Miloce09aff2012-01-04 09:03:18 +0400469 if (!psy)
470 return;
471
472 power_supply_unregister(&psy->ac);
473 power_supply_unregister(&psy->usb);
474 power_supply_unregister(&psy->batt);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400475}
476
477static int lp8727_probe(struct i2c_client *cl, const struct i2c_device_id *id)
478{
479 struct lp8727_chg *pchg;
480 int ret;
481
Kim, Milo998a8e72011-11-17 21:43:06 -0800482 if (!i2c_check_functionality(cl->adapter, I2C_FUNC_SMBUS_I2C_BLOCK))
483 return -EIO;
484
Kim, Milo74727c52012-08-31 09:22:46 +0000485 pchg = devm_kzalloc(&cl->dev, sizeof(*pchg), GFP_KERNEL);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400486 if (!pchg)
487 return -ENOMEM;
488
489 pchg->client = cl;
490 pchg->dev = &cl->dev;
491 pchg->pdata = cl->dev.platform_data;
492 i2c_set_clientdata(cl, pchg);
493
494 mutex_init(&pchg->xfer_lock);
495
Kim, Milo7da63342012-01-26 22:58:30 -0800496 ret = lp8727_init_device(pchg);
497 if (ret) {
498 dev_err(pchg->dev, "i2c communication err: %d", ret);
Kim, Milofb9adc52012-08-31 09:23:03 +0000499 return ret;
Kim, Milo7da63342012-01-26 22:58:30 -0800500 }
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400501
502 ret = lp8727_register_psy(pchg);
Kim, Milo7da63342012-01-26 22:58:30 -0800503 if (ret) {
504 dev_err(pchg->dev, "power supplies register err: %d", ret);
Kim, Milofb9adc52012-08-31 09:23:03 +0000505 return ret;
506 }
507
Kim, Milod71fda02012-08-31 09:23:57 +0000508 ret = lp8727_setup_irq(pchg);
Kim, Milofb9adc52012-08-31 09:23:03 +0000509 if (ret) {
510 dev_err(pchg->dev, "irq handler err: %d", ret);
511 lp8727_unregister_psy(pchg);
512 return ret;
Kim, Milo7da63342012-01-26 22:58:30 -0800513 }
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400514
515 return 0;
516}
517
518static int __devexit lp8727_remove(struct i2c_client *cl)
519{
520 struct lp8727_chg *pchg = i2c_get_clientdata(cl);
521
Kim, Milod71fda02012-08-31 09:23:57 +0000522 lp8727_release_irq(pchg);
Kim, Milofb9adc52012-08-31 09:23:03 +0000523 lp8727_unregister_psy(pchg);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400524 return 0;
525}
526
527static const struct i2c_device_id lp8727_ids[] = {
528 {"lp8727", 0},
Axel Lin455a0e22012-01-16 13:48:20 +0800529 { }
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400530};
Axel Line2c5f7d2012-01-12 20:45:02 +0800531MODULE_DEVICE_TABLE(i2c, lp8727_ids);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400532
533static struct i2c_driver lp8727_driver = {
534 .driver = {
535 .name = "lp8727",
536 },
537 .probe = lp8727_probe,
538 .remove = __devexit_p(lp8727_remove),
539 .id_table = lp8727_ids,
540};
Axel Lin5ff92e72012-01-21 14:42:54 +0800541module_i2c_driver(lp8727_driver);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400542
Kim, Miloe39b8282012-01-29 17:28:18 -0800543MODULE_DESCRIPTION("TI/National Semiconductor LP8727 charger driver");
Kim, Milo73368802012-01-26 22:58:51 -0800544MODULE_AUTHOR("Woogyom Kim <milo.kim@ti.com>, "
545 "Daniel Jeong <daniel.jeong@ti.com>");
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400546MODULE_LICENSE("GPL");