blob: c628224b7f58788b94df718fb087c1b56e5a03ed [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;
Woogyom Kim2165c8a2012-01-04 08:27:43 +040091 struct lp8727_psy *psy;
Kim, Milo1ae9df72012-08-31 09:27:04 +000092 struct lp8727_platform_data *pdata;
93
94 /* Charger Data */
Woogyom Kim2165c8a2012-01-04 08:27:43 +040095 enum lp8727_dev_id devid;
Kim, Milo1ae9df72012-08-31 09:27:04 +000096 struct lp8727_chg_param *chg_param;
97
98 /* Interrupt Handling */
Kim, Milod71fda02012-08-31 09:23:57 +000099 int irq;
Kim, Milo1ae9df72012-08-31 09:27:04 +0000100 struct delayed_work work;
101 unsigned long debounce_jiffies;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400102};
103
Kim, Milo27aefa32012-01-26 22:58:39 -0800104static int lp8727_read_bytes(struct lp8727_chg *pchg, u8 reg, u8 *data, u8 len)
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400105{
106 s32 ret;
107
108 mutex_lock(&pchg->xfer_lock);
109 ret = i2c_smbus_read_i2c_block_data(pchg->client, reg, len, data);
110 mutex_unlock(&pchg->xfer_lock);
111
112 return (ret != len) ? -EIO : 0;
113}
114
Kim, Milo27aefa32012-01-26 22:58:39 -0800115static inline int lp8727_read_byte(struct lp8727_chg *pchg, u8 reg, u8 *data)
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400116{
Kim, Milo27aefa32012-01-26 22:58:39 -0800117 return lp8727_read_bytes(pchg, reg, data, 1);
118}
119
120static int lp8727_write_byte(struct lp8727_chg *pchg, u8 reg, u8 data)
121{
122 int ret;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400123
124 mutex_lock(&pchg->xfer_lock);
Kim, Milo27aefa32012-01-26 22:58:39 -0800125 ret = i2c_smbus_write_byte_data(pchg->client, reg, data);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400126 mutex_unlock(&pchg->xfer_lock);
127
128 return ret;
129}
130
Kim, Milo65272ba2012-08-31 09:25:22 +0000131static bool lp8727_is_charger_attached(const char *name, int id)
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400132{
Kim, Milo65272ba2012-08-31 09:25:22 +0000133 if (!strcmp(name, "ac"))
134 return id == LP8727_ID_TA || id == LP8727_ID_DEDICATED_CHG;
135 else if (!strcmp(name, "usb"))
136 return id == LP8727_ID_USB_CHG;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400137
Kim, Milo65272ba2012-08-31 09:25:22 +0000138 return id >= LP8727_ID_TA && id <= LP8727_ID_USB_CHG;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400139}
140
Kim, Milo7da63342012-01-26 22:58:30 -0800141static int lp8727_init_device(struct lp8727_chg *pchg)
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400142{
143 u8 val;
Kim, Milo7da63342012-01-26 22:58:30 -0800144 int ret;
Kim, Milo638555d2012-08-31 09:24:09 +0000145 u8 intstat[LP8788_NUM_INTREGS];
146
147 /* clear interrupts */
Kim, Milo60297192012-08-31 09:24:51 +0000148 ret = lp8727_read_bytes(pchg, LP8727_INT1, intstat, LP8788_NUM_INTREGS);
Kim, Milo638555d2012-08-31 09:24:09 +0000149 if (ret)
150 return ret;
151
Kim, Milo60297192012-08-31 09:24:51 +0000152 val = LP8727_ID200_EN | LP8727_ADC_EN | LP8727_CP_EN;
153 ret = lp8727_write_byte(pchg, LP8727_CTRL1, val);
Kim, Milo7da63342012-01-26 22:58:30 -0800154 if (ret)
155 return ret;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400156
Kim, Milo60297192012-08-31 09:24:51 +0000157 val = LP8727_INT_EN | LP8727_CHGDET_EN;
Kim, Milo70be1302012-08-31 09:25:34 +0000158 return lp8727_write_byte(pchg, LP8727_CTRL2, val);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400159}
160
161static int lp8727_is_dedicated_charger(struct lp8727_chg *pchg)
162{
163 u8 val;
Kim, Milob9633ef2012-08-31 09:27:22 +0000164
Kim, Milo60297192012-08-31 09:24:51 +0000165 lp8727_read_byte(pchg, LP8727_STATUS1, &val);
166 return val & LP8727_DCPORT;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400167}
168
169static int lp8727_is_usb_charger(struct lp8727_chg *pchg)
170{
171 u8 val;
Kim, Milob9633ef2012-08-31 09:27:22 +0000172
Kim, Milo60297192012-08-31 09:24:51 +0000173 lp8727_read_byte(pchg, LP8727_STATUS1, &val);
174 return val & LP8727_CHPORT;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400175}
176
Kim, Milo91a69632012-08-31 09:25:45 +0000177static inline void lp8727_ctrl_switch(struct lp8727_chg *pchg, u8 sw)
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400178{
Kim, Milo60297192012-08-31 09:24:51 +0000179 lp8727_write_byte(pchg, LP8727_SWCTRL, sw);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400180}
181
182static void lp8727_id_detection(struct lp8727_chg *pchg, u8 id, int vbusin)
183{
Kim, Milo318cb382012-08-31 09:23:12 +0000184 struct lp8727_platform_data *pdata = pchg->pdata;
Kim, Milo60297192012-08-31 09:24:51 +0000185 u8 devid = LP8727_ID_NONE;
186 u8 swctrl = LP8727_SW_DM1_HiZ | LP8727_SW_DP2_HiZ;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400187
188 switch (id) {
189 case 0x5:
Kim, Milo60297192012-08-31 09:24:51 +0000190 devid = LP8727_ID_TA;
Kim, Milo56d99ed2012-08-31 09:26:49 +0000191 pchg->chg_param = pdata ? pdata->ac : NULL;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400192 break;
193 case 0xB:
194 if (lp8727_is_dedicated_charger(pchg)) {
Kim, Milo56d99ed2012-08-31 09:26:49 +0000195 pchg->chg_param = pdata ? pdata->ac : NULL;
Kim, Milo60297192012-08-31 09:24:51 +0000196 devid = LP8727_ID_DEDICATED_CHG;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400197 } else if (lp8727_is_usb_charger(pchg)) {
Kim, Milo56d99ed2012-08-31 09:26:49 +0000198 pchg->chg_param = pdata ? pdata->usb : NULL;
Kim, Milo60297192012-08-31 09:24:51 +0000199 devid = LP8727_ID_USB_CHG;
200 swctrl = LP8727_SW_DM1_DM | LP8727_SW_DP2_DP;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400201 } else if (vbusin) {
Kim, Milo60297192012-08-31 09:24:51 +0000202 devid = LP8727_ID_USB_DS;
203 swctrl = LP8727_SW_DM1_DM | LP8727_SW_DP2_DP;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400204 }
205 break;
206 default:
Kim, Milo60297192012-08-31 09:24:51 +0000207 devid = LP8727_ID_NONE;
Kim, Milo56d99ed2012-08-31 09:26:49 +0000208 pchg->chg_param = NULL;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400209 break;
210 }
211
212 pchg->devid = devid;
213 lp8727_ctrl_switch(pchg, swctrl);
214}
215
216static void lp8727_enable_chgdet(struct lp8727_chg *pchg)
217{
218 u8 val;
219
Kim, Milo60297192012-08-31 09:24:51 +0000220 lp8727_read_byte(pchg, LP8727_CTRL2, &val);
221 val |= LP8727_CHGDET_EN;
222 lp8727_write_byte(pchg, LP8727_CTRL2, val);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400223}
224
225static void lp8727_delayed_func(struct work_struct *_work)
226{
Kim, Milo5e00b972012-08-31 09:26:33 +0000227 struct lp8727_chg *pchg = container_of(_work, struct lp8727_chg,
228 work.work);
229 u8 intstat[LP8788_NUM_INTREGS];
230 u8 idno;
231 u8 vbus;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400232
Kim, Milo18763a32012-08-31 09:25:07 +0000233 if (lp8727_read_bytes(pchg, LP8727_INT1, intstat, LP8788_NUM_INTREGS)) {
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400234 dev_err(pchg->dev, "can not read INT registers\n");
235 return;
236 }
237
Kim, Milo60297192012-08-31 09:24:51 +0000238 idno = intstat[0] & LP8727_IDNO;
239 vbus = intstat[0] & LP8727_VBUS;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400240
241 lp8727_id_detection(pchg, idno, vbus);
242 lp8727_enable_chgdet(pchg);
243
244 power_supply_changed(&pchg->psy->ac);
245 power_supply_changed(&pchg->psy->usb);
246 power_supply_changed(&pchg->psy->batt);
247}
248
249static irqreturn_t lp8727_isr_func(int irq, void *ptr)
250{
251 struct lp8727_chg *pchg = ptr;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400252
Kim, Milo2a092582012-08-31 09:23:41 +0000253 schedule_delayed_work(&pchg->work, pchg->debounce_jiffies);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400254 return IRQ_HANDLED;
255}
256
Kim, Milod71fda02012-08-31 09:23:57 +0000257static int lp8727_setup_irq(struct lp8727_chg *pchg)
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400258{
Kim, Milod71fda02012-08-31 09:23:57 +0000259 int ret;
260 int irq = pchg->client->irq;
Kim, Milo60fd57e2012-08-31 09:23:25 +0000261 unsigned delay_msec = pchg->pdata ? pchg->pdata->debounce_msec :
262 DEFAULT_DEBOUNCE_MSEC;
263
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400264 INIT_DELAYED_WORK(&pchg->work, lp8727_delayed_func);
265
Kim, Milod71fda02012-08-31 09:23:57 +0000266 if (irq <= 0) {
267 dev_warn(pchg->dev, "invalid irq number: %d\n", irq);
268 return 0;
269 }
270
271 ret = request_threaded_irq(irq, NULL, lp8727_isr_func,
272 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
273 "lp8727_irq", pchg);
274
275 if (ret)
276 return ret;
277
278 pchg->irq = irq;
Kim, Milo60fd57e2012-08-31 09:23:25 +0000279 pchg->debounce_jiffies = msecs_to_jiffies(delay_msec);
280
Kim, Milod71fda02012-08-31 09:23:57 +0000281 return 0;
282}
283
284static void lp8727_release_irq(struct lp8727_chg *pchg)
285{
286 cancel_delayed_work_sync(&pchg->work);
287
288 if (pchg->irq)
289 free_irq(pchg->irq, pchg);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400290}
291
292static enum power_supply_property lp8727_charger_prop[] = {
293 POWER_SUPPLY_PROP_ONLINE,
294};
295
296static enum power_supply_property lp8727_battery_prop[] = {
297 POWER_SUPPLY_PROP_STATUS,
298 POWER_SUPPLY_PROP_HEALTH,
299 POWER_SUPPLY_PROP_PRESENT,
300 POWER_SUPPLY_PROP_VOLTAGE_NOW,
301 POWER_SUPPLY_PROP_CAPACITY,
302 POWER_SUPPLY_PROP_TEMP,
303};
304
305static char *battery_supplied_to[] = {
306 "main_batt",
307};
308
309static int lp8727_charger_get_property(struct power_supply *psy,
310 enum power_supply_property psp,
311 union power_supply_propval *val)
312{
313 struct lp8727_chg *pchg = dev_get_drvdata(psy->dev->parent);
314
Kim, Milob59c93c02012-08-31 09:25:55 +0000315 if (psp != POWER_SUPPLY_PROP_ONLINE)
316 return -EINVAL;
317
318 val->intval = lp8727_is_charger_attached(psy->name, pchg->devid);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400319
320 return 0;
321}
322
Kim, Milob1ad0792012-08-31 09:24:28 +0000323static bool lp8727_is_high_temperature(enum lp8727_die_temp temp)
324{
325 switch (temp) {
326 case LP8788_TEMP_95C:
327 case LP8788_TEMP_115C:
328 case LP8788_TEMP_135C:
329 return true;
330 default:
331 return false;
332 }
333}
334
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400335static int lp8727_battery_get_property(struct power_supply *psy,
336 enum power_supply_property psp,
337 union power_supply_propval *val)
338{
339 struct lp8727_chg *pchg = dev_get_drvdata(psy->dev->parent);
Kim, Milo318cb382012-08-31 09:23:12 +0000340 struct lp8727_platform_data *pdata = pchg->pdata;
Kim, Milob1ad0792012-08-31 09:24:28 +0000341 enum lp8727_die_temp temp;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400342 u8 read;
343
344 switch (psp) {
345 case POWER_SUPPLY_PROP_STATUS:
Kim, Miloe06374b2012-08-31 09:26:06 +0000346 if (!lp8727_is_charger_attached(psy->name, pchg->devid)) {
347 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
348 return 0;
349 }
Kim, Milofaaae9b2012-08-31 09:24:37 +0000350
Kim, Miloe06374b2012-08-31 09:26:06 +0000351 lp8727_read_byte(pchg, LP8727_STATUS1, &read);
352
353 val->intval = (read & LP8727_CHGSTAT) == LP8727_STAT_EOC ?
Kim, Milofaaae9b2012-08-31 09:24:37 +0000354 POWER_SUPPLY_STATUS_FULL :
355 POWER_SUPPLY_STATUS_CHARGING;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400356 break;
357 case POWER_SUPPLY_PROP_HEALTH:
Kim, Milo60297192012-08-31 09:24:51 +0000358 lp8727_read_byte(pchg, LP8727_STATUS2, &read);
359 temp = (read & LP8727_TEMP_STAT) >> LP8727_TEMP_SHIFT;
Kim, Milob1ad0792012-08-31 09:24:28 +0000360
361 val->intval = lp8727_is_high_temperature(temp) ?
362 POWER_SUPPLY_HEALTH_OVERHEAT :
363 POWER_SUPPLY_HEALTH_GOOD;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400364 break;
365 case POWER_SUPPLY_PROP_PRESENT:
Kim, Milo318cb382012-08-31 09:23:12 +0000366 if (!pdata)
367 return -EINVAL;
368
369 if (pdata->get_batt_present)
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400370 val->intval = pchg->pdata->get_batt_present();
371 break;
372 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Kim, Milo318cb382012-08-31 09:23:12 +0000373 if (!pdata)
374 return -EINVAL;
375
376 if (pdata->get_batt_level)
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400377 val->intval = pchg->pdata->get_batt_level();
378 break;
379 case POWER_SUPPLY_PROP_CAPACITY:
Kim, Milo318cb382012-08-31 09:23:12 +0000380 if (!pdata)
381 return -EINVAL;
382
383 if (pdata->get_batt_capacity)
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400384 val->intval = pchg->pdata->get_batt_capacity();
385 break;
386 case POWER_SUPPLY_PROP_TEMP:
Kim, Milo318cb382012-08-31 09:23:12 +0000387 if (!pdata)
388 return -EINVAL;
389
390 if (pdata->get_batt_temp)
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400391 val->intval = pchg->pdata->get_batt_temp();
392 break;
393 default:
394 break;
395 }
396
397 return 0;
398}
399
400static void lp8727_charger_changed(struct power_supply *psy)
401{
402 struct lp8727_chg *pchg = dev_get_drvdata(psy->dev->parent);
Kim, Milo20414e22012-08-31 09:26:22 +0000403 u8 eoc_level;
404 u8 ichg;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400405 u8 val;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400406
Kim, Milo20414e22012-08-31 09:26:22 +0000407 /* skip if no charger exists */
408 if (!lp8727_is_charger_attached(psy->name, pchg->devid))
409 return;
410
411 /* update charging parameters */
Kim, Milo56d99ed2012-08-31 09:26:49 +0000412 if (pchg->chg_param) {
413 eoc_level = pchg->chg_param->eoc_level;
414 ichg = pchg->chg_param->ichg;
Kim, Milo20414e22012-08-31 09:26:22 +0000415 val = (ichg << LP8727_ICHG_SHIFT) | eoc_level;
416 lp8727_write_byte(pchg, LP8727_CHGCTRL2, val);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400417 }
418}
419
420static int lp8727_register_psy(struct lp8727_chg *pchg)
421{
422 struct lp8727_psy *psy;
423
Kim, Milo74727c52012-08-31 09:22:46 +0000424 psy = devm_kzalloc(pchg->dev, sizeof(*psy), GFP_KERNEL);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400425 if (!psy)
Devendra Naga6297b5e2012-07-29 23:31:55 +0545426 return -ENOMEM;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400427
428 pchg->psy = psy;
429
430 psy->ac.name = "ac";
431 psy->ac.type = POWER_SUPPLY_TYPE_MAINS;
432 psy->ac.properties = lp8727_charger_prop;
433 psy->ac.num_properties = ARRAY_SIZE(lp8727_charger_prop);
434 psy->ac.get_property = lp8727_charger_get_property;
435 psy->ac.supplied_to = battery_supplied_to;
436 psy->ac.num_supplicants = ARRAY_SIZE(battery_supplied_to);
437
438 if (power_supply_register(pchg->dev, &psy->ac))
Devendra Naga6297b5e2012-07-29 23:31:55 +0545439 goto err_psy_ac;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400440
441 psy->usb.name = "usb";
442 psy->usb.type = POWER_SUPPLY_TYPE_USB;
443 psy->usb.properties = lp8727_charger_prop;
444 psy->usb.num_properties = ARRAY_SIZE(lp8727_charger_prop);
445 psy->usb.get_property = lp8727_charger_get_property;
446 psy->usb.supplied_to = battery_supplied_to;
447 psy->usb.num_supplicants = ARRAY_SIZE(battery_supplied_to);
448
449 if (power_supply_register(pchg->dev, &psy->usb))
Devendra Naga6297b5e2012-07-29 23:31:55 +0545450 goto err_psy_usb;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400451
452 psy->batt.name = "main_batt";
453 psy->batt.type = POWER_SUPPLY_TYPE_BATTERY;
454 psy->batt.properties = lp8727_battery_prop;
455 psy->batt.num_properties = ARRAY_SIZE(lp8727_battery_prop);
456 psy->batt.get_property = lp8727_battery_get_property;
457 psy->batt.external_power_changed = lp8727_charger_changed;
458
459 if (power_supply_register(pchg->dev, &psy->batt))
Devendra Naga6297b5e2012-07-29 23:31:55 +0545460 goto err_psy_batt;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400461
462 return 0;
463
Devendra Naga6297b5e2012-07-29 23:31:55 +0545464err_psy_batt:
465 power_supply_unregister(&psy->usb);
466err_psy_usb:
467 power_supply_unregister(&psy->ac);
468err_psy_ac:
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400469 return -EPERM;
470}
471
472static void lp8727_unregister_psy(struct lp8727_chg *pchg)
473{
474 struct lp8727_psy *psy = pchg->psy;
475
Kim, Miloce09aff2012-01-04 09:03:18 +0400476 if (!psy)
477 return;
478
479 power_supply_unregister(&psy->ac);
480 power_supply_unregister(&psy->usb);
481 power_supply_unregister(&psy->batt);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400482}
483
484static int lp8727_probe(struct i2c_client *cl, const struct i2c_device_id *id)
485{
486 struct lp8727_chg *pchg;
487 int ret;
488
Kim, Milo998a8e72011-11-17 21:43:06 -0800489 if (!i2c_check_functionality(cl->adapter, I2C_FUNC_SMBUS_I2C_BLOCK))
490 return -EIO;
491
Kim, Milo74727c52012-08-31 09:22:46 +0000492 pchg = devm_kzalloc(&cl->dev, sizeof(*pchg), GFP_KERNEL);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400493 if (!pchg)
494 return -ENOMEM;
495
496 pchg->client = cl;
497 pchg->dev = &cl->dev;
498 pchg->pdata = cl->dev.platform_data;
499 i2c_set_clientdata(cl, pchg);
500
501 mutex_init(&pchg->xfer_lock);
502
Kim, Milo7da63342012-01-26 22:58:30 -0800503 ret = lp8727_init_device(pchg);
504 if (ret) {
505 dev_err(pchg->dev, "i2c communication err: %d", ret);
Kim, Milofb9adc52012-08-31 09:23:03 +0000506 return ret;
Kim, Milo7da63342012-01-26 22:58:30 -0800507 }
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400508
509 ret = lp8727_register_psy(pchg);
Kim, Milo7da63342012-01-26 22:58:30 -0800510 if (ret) {
511 dev_err(pchg->dev, "power supplies register err: %d", ret);
Kim, Milofb9adc52012-08-31 09:23:03 +0000512 return ret;
513 }
514
Kim, Milod71fda02012-08-31 09:23:57 +0000515 ret = lp8727_setup_irq(pchg);
Kim, Milofb9adc52012-08-31 09:23:03 +0000516 if (ret) {
517 dev_err(pchg->dev, "irq handler err: %d", ret);
518 lp8727_unregister_psy(pchg);
519 return ret;
Kim, Milo7da63342012-01-26 22:58:30 -0800520 }
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400521
522 return 0;
523}
524
525static int __devexit lp8727_remove(struct i2c_client *cl)
526{
527 struct lp8727_chg *pchg = i2c_get_clientdata(cl);
528
Kim, Milod71fda02012-08-31 09:23:57 +0000529 lp8727_release_irq(pchg);
Kim, Milofb9adc52012-08-31 09:23:03 +0000530 lp8727_unregister_psy(pchg);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400531 return 0;
532}
533
534static const struct i2c_device_id lp8727_ids[] = {
535 {"lp8727", 0},
Axel Lin455a0e22012-01-16 13:48:20 +0800536 { }
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400537};
Axel Line2c5f7d2012-01-12 20:45:02 +0800538MODULE_DEVICE_TABLE(i2c, lp8727_ids);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400539
540static struct i2c_driver lp8727_driver = {
541 .driver = {
542 .name = "lp8727",
543 },
544 .probe = lp8727_probe,
545 .remove = __devexit_p(lp8727_remove),
546 .id_table = lp8727_ids,
547};
Axel Lin5ff92e72012-01-21 14:42:54 +0800548module_i2c_driver(lp8727_driver);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400549
Kim, Miloe39b8282012-01-29 17:28:18 -0800550MODULE_DESCRIPTION("TI/National Semiconductor LP8727 charger driver");
Kim, Milo18259ca2012-08-31 09:27:12 +0000551MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>, Daniel Jeong <daniel.jeong@ti.com>");
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400552MODULE_LICENSE("GPL");