blob: 32de636dcd732d4f392cff5fd2c8e08bb1125f4a [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>
Kim, Milo17b45652013-05-10 07:17:51 +000019#include <linux/of.h>
Woogyom Kim2165c8a2012-01-04 08:27:43 +040020
Kim, Milo638555d2012-08-31 09:24:09 +000021#define LP8788_NUM_INTREGS 2
Kim, Milo60fd57e2012-08-31 09:23:25 +000022#define DEFAULT_DEBOUNCE_MSEC 270
Woogyom Kim2165c8a2012-01-04 08:27:43 +040023
24/* Registers */
Kim, Milo60297192012-08-31 09:24:51 +000025#define LP8727_CTRL1 0x1
26#define LP8727_CTRL2 0x2
27#define LP8727_SWCTRL 0x3
28#define LP8727_INT1 0x4
29#define LP8727_INT2 0x5
30#define LP8727_STATUS1 0x6
31#define LP8727_STATUS2 0x7
32#define LP8727_CHGCTRL2 0x9
Woogyom Kim2165c8a2012-01-04 08:27:43 +040033
34/* CTRL1 register */
Kim, Milo60297192012-08-31 09:24:51 +000035#define LP8727_CP_EN BIT(0)
36#define LP8727_ADC_EN BIT(1)
37#define LP8727_ID200_EN BIT(4)
Woogyom Kim2165c8a2012-01-04 08:27:43 +040038
39/* CTRL2 register */
Kim, Milo60297192012-08-31 09:24:51 +000040#define LP8727_CHGDET_EN BIT(1)
41#define LP8727_INT_EN BIT(6)
Woogyom Kim2165c8a2012-01-04 08:27:43 +040042
43/* SWCTRL register */
Kim, Milo60297192012-08-31 09:24:51 +000044#define LP8727_SW_DM1_DM (0x0 << 0)
45#define LP8727_SW_DM1_HiZ (0x7 << 0)
46#define LP8727_SW_DP2_DP (0x0 << 3)
47#define LP8727_SW_DP2_HiZ (0x7 << 3)
Woogyom Kim2165c8a2012-01-04 08:27:43 +040048
49/* INT1 register */
Kim, Milo60297192012-08-31 09:24:51 +000050#define LP8727_IDNO (0xF << 0)
51#define LP8727_VBUS BIT(4)
Woogyom Kim2165c8a2012-01-04 08:27:43 +040052
53/* STATUS1 register */
Kim, Milo60297192012-08-31 09:24:51 +000054#define LP8727_CHGSTAT (3 << 4)
55#define LP8727_CHPORT BIT(6)
56#define LP8727_DCPORT BIT(7)
57#define LP8727_STAT_EOC 0x30
Woogyom Kim2165c8a2012-01-04 08:27:43 +040058
59/* STATUS2 register */
Kim, Milo60297192012-08-31 09:24:51 +000060#define LP8727_TEMP_STAT (3 << 5)
61#define LP8727_TEMP_SHIFT 5
Woogyom Kim2165c8a2012-01-04 08:27:43 +040062
Kim, Milo18763a32012-08-31 09:25:07 +000063/* CHGCTRL2 register */
64#define LP8727_ICHG_SHIFT 4
65
Woogyom Kim2165c8a2012-01-04 08:27:43 +040066enum lp8727_dev_id {
Kim, Milo60297192012-08-31 09:24:51 +000067 LP8727_ID_NONE,
68 LP8727_ID_TA,
69 LP8727_ID_DEDICATED_CHG,
70 LP8727_ID_USB_CHG,
71 LP8727_ID_USB_DS,
72 LP8727_ID_MAX,
Woogyom Kim2165c8a2012-01-04 08:27:43 +040073};
74
Kim, Milob1ad0792012-08-31 09:24:28 +000075enum lp8727_die_temp {
76 LP8788_TEMP_75C,
77 LP8788_TEMP_95C,
78 LP8788_TEMP_115C,
79 LP8788_TEMP_135C,
80};
81
Woogyom Kim2165c8a2012-01-04 08:27:43 +040082struct lp8727_psy {
83 struct power_supply ac;
84 struct power_supply usb;
85 struct power_supply batt;
86};
87
88struct lp8727_chg {
89 struct device *dev;
90 struct i2c_client *client;
91 struct mutex xfer_lock;
Woogyom Kim2165c8a2012-01-04 08:27:43 +040092 struct lp8727_psy *psy;
Kim, Milo1ae9df72012-08-31 09:27:04 +000093 struct lp8727_platform_data *pdata;
94
95 /* Charger Data */
Woogyom Kim2165c8a2012-01-04 08:27:43 +040096 enum lp8727_dev_id devid;
Kim, Milo1ae9df72012-08-31 09:27:04 +000097 struct lp8727_chg_param *chg_param;
98
99 /* Interrupt Handling */
Kim, Milod71fda02012-08-31 09:23:57 +0000100 int irq;
Kim, Milo1ae9df72012-08-31 09:27:04 +0000101 struct delayed_work work;
102 unsigned long debounce_jiffies;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400103};
104
Kim, Milo27aefa32012-01-26 22:58:39 -0800105static int lp8727_read_bytes(struct lp8727_chg *pchg, u8 reg, u8 *data, u8 len)
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400106{
107 s32 ret;
108
109 mutex_lock(&pchg->xfer_lock);
110 ret = i2c_smbus_read_i2c_block_data(pchg->client, reg, len, data);
111 mutex_unlock(&pchg->xfer_lock);
112
113 return (ret != len) ? -EIO : 0;
114}
115
Kim, Milo27aefa32012-01-26 22:58:39 -0800116static inline int lp8727_read_byte(struct lp8727_chg *pchg, u8 reg, u8 *data)
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400117{
Kim, Milo27aefa32012-01-26 22:58:39 -0800118 return lp8727_read_bytes(pchg, reg, data, 1);
119}
120
121static int lp8727_write_byte(struct lp8727_chg *pchg, u8 reg, u8 data)
122{
123 int ret;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400124
125 mutex_lock(&pchg->xfer_lock);
Kim, Milo27aefa32012-01-26 22:58:39 -0800126 ret = i2c_smbus_write_byte_data(pchg->client, reg, data);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400127 mutex_unlock(&pchg->xfer_lock);
128
129 return ret;
130}
131
Kim, Milo65272ba2012-08-31 09:25:22 +0000132static bool lp8727_is_charger_attached(const char *name, int id)
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400133{
Kim, Milo65272ba2012-08-31 09:25:22 +0000134 if (!strcmp(name, "ac"))
135 return id == LP8727_ID_TA || id == LP8727_ID_DEDICATED_CHG;
136 else if (!strcmp(name, "usb"))
137 return id == LP8727_ID_USB_CHG;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400138
Kim, Milo65272ba2012-08-31 09:25:22 +0000139 return id >= LP8727_ID_TA && id <= LP8727_ID_USB_CHG;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400140}
141
Kim, Milo7da63342012-01-26 22:58:30 -0800142static int lp8727_init_device(struct lp8727_chg *pchg)
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400143{
144 u8 val;
Kim, Milo7da63342012-01-26 22:58:30 -0800145 int ret;
Kim, Milo638555d2012-08-31 09:24:09 +0000146 u8 intstat[LP8788_NUM_INTREGS];
147
148 /* clear interrupts */
Kim, Milo60297192012-08-31 09:24:51 +0000149 ret = lp8727_read_bytes(pchg, LP8727_INT1, intstat, LP8788_NUM_INTREGS);
Kim, Milo638555d2012-08-31 09:24:09 +0000150 if (ret)
151 return ret;
152
Kim, Milo60297192012-08-31 09:24:51 +0000153 val = LP8727_ID200_EN | LP8727_ADC_EN | LP8727_CP_EN;
154 ret = lp8727_write_byte(pchg, LP8727_CTRL1, val);
Kim, Milo7da63342012-01-26 22:58:30 -0800155 if (ret)
156 return ret;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400157
Kim, Milo60297192012-08-31 09:24:51 +0000158 val = LP8727_INT_EN | LP8727_CHGDET_EN;
Kim, Milo70be1302012-08-31 09:25:34 +0000159 return lp8727_write_byte(pchg, LP8727_CTRL2, val);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400160}
161
162static int lp8727_is_dedicated_charger(struct lp8727_chg *pchg)
163{
164 u8 val;
Kim, Milob9633ef2012-08-31 09:27:22 +0000165
Kim, Milo60297192012-08-31 09:24:51 +0000166 lp8727_read_byte(pchg, LP8727_STATUS1, &val);
167 return val & LP8727_DCPORT;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400168}
169
170static int lp8727_is_usb_charger(struct lp8727_chg *pchg)
171{
172 u8 val;
Kim, Milob9633ef2012-08-31 09:27:22 +0000173
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
Kim, Milo91a69632012-08-31 09:25:45 +0000178static inline void lp8727_ctrl_switch(struct lp8727_chg *pchg, u8 sw)
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400179{
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, Milo56d99ed2012-08-31 09:26:49 +0000192 pchg->chg_param = pdata ? pdata->ac : NULL;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400193 break;
194 case 0xB:
195 if (lp8727_is_dedicated_charger(pchg)) {
Kim, Milo56d99ed2012-08-31 09:26:49 +0000196 pchg->chg_param = 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, Milo56d99ed2012-08-31 09:26:49 +0000199 pchg->chg_param = 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;
Kim, Milo56d99ed2012-08-31 09:26:49 +0000209 pchg->chg_param = NULL;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400210 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, Milo5e00b972012-08-31 09:26:33 +0000228 struct lp8727_chg *pchg = container_of(_work, struct lp8727_chg,
229 work.work);
230 u8 intstat[LP8788_NUM_INTREGS];
231 u8 idno;
232 u8 vbus;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400233
Kim, Milo18763a32012-08-31 09:25:07 +0000234 if (lp8727_read_bytes(pchg, LP8727_INT1, intstat, LP8788_NUM_INTREGS)) {
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400235 dev_err(pchg->dev, "can not read INT registers\n");
236 return;
237 }
238
Kim, Milo60297192012-08-31 09:24:51 +0000239 idno = intstat[0] & LP8727_IDNO;
240 vbus = intstat[0] & LP8727_VBUS;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400241
242 lp8727_id_detection(pchg, idno, vbus);
243 lp8727_enable_chgdet(pchg);
244
245 power_supply_changed(&pchg->psy->ac);
246 power_supply_changed(&pchg->psy->usb);
247 power_supply_changed(&pchg->psy->batt);
248}
249
250static irqreturn_t lp8727_isr_func(int irq, void *ptr)
251{
252 struct lp8727_chg *pchg = ptr;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400253
Kim, Milo2a092582012-08-31 09:23:41 +0000254 schedule_delayed_work(&pchg->work, pchg->debounce_jiffies);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400255 return IRQ_HANDLED;
256}
257
Kim, Milod71fda02012-08-31 09:23:57 +0000258static int lp8727_setup_irq(struct lp8727_chg *pchg)
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400259{
Kim, Milod71fda02012-08-31 09:23:57 +0000260 int ret;
261 int irq = pchg->client->irq;
Kim, Milo60fd57e2012-08-31 09:23:25 +0000262 unsigned delay_msec = pchg->pdata ? pchg->pdata->debounce_msec :
263 DEFAULT_DEBOUNCE_MSEC;
264
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400265 INIT_DELAYED_WORK(&pchg->work, lp8727_delayed_func);
266
Kim, Milod71fda02012-08-31 09:23:57 +0000267 if (irq <= 0) {
268 dev_warn(pchg->dev, "invalid irq number: %d\n", irq);
269 return 0;
270 }
271
272 ret = request_threaded_irq(irq, NULL, lp8727_isr_func,
273 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
274 "lp8727_irq", pchg);
275
276 if (ret)
277 return ret;
278
279 pchg->irq = irq;
Kim, Milo60fd57e2012-08-31 09:23:25 +0000280 pchg->debounce_jiffies = msecs_to_jiffies(delay_msec);
281
Kim, Milod71fda02012-08-31 09:23:57 +0000282 return 0;
283}
284
285static void lp8727_release_irq(struct lp8727_chg *pchg)
286{
287 cancel_delayed_work_sync(&pchg->work);
288
289 if (pchg->irq)
290 free_irq(pchg->irq, pchg);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400291}
292
293static enum power_supply_property lp8727_charger_prop[] = {
294 POWER_SUPPLY_PROP_ONLINE,
295};
296
297static enum power_supply_property lp8727_battery_prop[] = {
298 POWER_SUPPLY_PROP_STATUS,
299 POWER_SUPPLY_PROP_HEALTH,
300 POWER_SUPPLY_PROP_PRESENT,
301 POWER_SUPPLY_PROP_VOLTAGE_NOW,
302 POWER_SUPPLY_PROP_CAPACITY,
303 POWER_SUPPLY_PROP_TEMP,
304};
305
306static char *battery_supplied_to[] = {
307 "main_batt",
308};
309
310static int lp8727_charger_get_property(struct power_supply *psy,
311 enum power_supply_property psp,
312 union power_supply_propval *val)
313{
314 struct lp8727_chg *pchg = dev_get_drvdata(psy->dev->parent);
315
Kim, Milob59c93c02012-08-31 09:25:55 +0000316 if (psp != POWER_SUPPLY_PROP_ONLINE)
317 return -EINVAL;
318
319 val->intval = lp8727_is_charger_attached(psy->name, pchg->devid);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400320
321 return 0;
322}
323
Kim, Milob1ad0792012-08-31 09:24:28 +0000324static bool lp8727_is_high_temperature(enum lp8727_die_temp temp)
325{
326 switch (temp) {
327 case LP8788_TEMP_95C:
328 case LP8788_TEMP_115C:
329 case LP8788_TEMP_135C:
330 return true;
331 default:
332 return false;
333 }
334}
335
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400336static int lp8727_battery_get_property(struct power_supply *psy,
337 enum power_supply_property psp,
338 union power_supply_propval *val)
339{
340 struct lp8727_chg *pchg = dev_get_drvdata(psy->dev->parent);
Kim, Milo318cb382012-08-31 09:23:12 +0000341 struct lp8727_platform_data *pdata = pchg->pdata;
Kim, Milob1ad0792012-08-31 09:24:28 +0000342 enum lp8727_die_temp temp;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400343 u8 read;
344
345 switch (psp) {
346 case POWER_SUPPLY_PROP_STATUS:
Kim, Miloe06374b2012-08-31 09:26:06 +0000347 if (!lp8727_is_charger_attached(psy->name, pchg->devid)) {
348 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
349 return 0;
350 }
Kim, Milofaaae9b2012-08-31 09:24:37 +0000351
Kim, Miloe06374b2012-08-31 09:26:06 +0000352 lp8727_read_byte(pchg, LP8727_STATUS1, &read);
353
354 val->intval = (read & LP8727_CHGSTAT) == LP8727_STAT_EOC ?
Kim, Milofaaae9b2012-08-31 09:24:37 +0000355 POWER_SUPPLY_STATUS_FULL :
356 POWER_SUPPLY_STATUS_CHARGING;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400357 break;
358 case POWER_SUPPLY_PROP_HEALTH:
Kim, Milo60297192012-08-31 09:24:51 +0000359 lp8727_read_byte(pchg, LP8727_STATUS2, &read);
360 temp = (read & LP8727_TEMP_STAT) >> LP8727_TEMP_SHIFT;
Kim, Milob1ad0792012-08-31 09:24:28 +0000361
362 val->intval = lp8727_is_high_temperature(temp) ?
363 POWER_SUPPLY_HEALTH_OVERHEAT :
364 POWER_SUPPLY_HEALTH_GOOD;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400365 break;
366 case POWER_SUPPLY_PROP_PRESENT:
Kim, Milo318cb382012-08-31 09:23:12 +0000367 if (!pdata)
368 return -EINVAL;
369
370 if (pdata->get_batt_present)
Dan Carpenterf96b3072013-01-25 18:43:50 +0300371 val->intval = pdata->get_batt_present();
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400372 break;
373 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Kim, Milo318cb382012-08-31 09:23:12 +0000374 if (!pdata)
375 return -EINVAL;
376
377 if (pdata->get_batt_level)
Dan Carpenterf96b3072013-01-25 18:43:50 +0300378 val->intval = pdata->get_batt_level();
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400379 break;
380 case POWER_SUPPLY_PROP_CAPACITY:
Kim, Milo318cb382012-08-31 09:23:12 +0000381 if (!pdata)
382 return -EINVAL;
383
384 if (pdata->get_batt_capacity)
Dan Carpenterf96b3072013-01-25 18:43:50 +0300385 val->intval = pdata->get_batt_capacity();
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400386 break;
387 case POWER_SUPPLY_PROP_TEMP:
Kim, Milo318cb382012-08-31 09:23:12 +0000388 if (!pdata)
389 return -EINVAL;
390
391 if (pdata->get_batt_temp)
Dan Carpenterf96b3072013-01-25 18:43:50 +0300392 val->intval = pdata->get_batt_temp();
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400393 break;
394 default:
395 break;
396 }
397
398 return 0;
399}
400
401static void lp8727_charger_changed(struct power_supply *psy)
402{
403 struct lp8727_chg *pchg = dev_get_drvdata(psy->dev->parent);
Kim, Milo20414e22012-08-31 09:26:22 +0000404 u8 eoc_level;
405 u8 ichg;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400406 u8 val;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400407
Kim, Milo20414e22012-08-31 09:26:22 +0000408 /* skip if no charger exists */
409 if (!lp8727_is_charger_attached(psy->name, pchg->devid))
410 return;
411
412 /* update charging parameters */
Kim, Milo56d99ed2012-08-31 09:26:49 +0000413 if (pchg->chg_param) {
414 eoc_level = pchg->chg_param->eoc_level;
415 ichg = pchg->chg_param->ichg;
Kim, Milo20414e22012-08-31 09:26:22 +0000416 val = (ichg << LP8727_ICHG_SHIFT) | eoc_level;
417 lp8727_write_byte(pchg, LP8727_CHGCTRL2, val);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400418 }
419}
420
421static int lp8727_register_psy(struct lp8727_chg *pchg)
422{
423 struct lp8727_psy *psy;
424
Kim, Milo74727c52012-08-31 09:22:46 +0000425 psy = devm_kzalloc(pchg->dev, sizeof(*psy), GFP_KERNEL);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400426 if (!psy)
Devendra Naga6297b5e2012-07-29 23:31:55 +0545427 return -ENOMEM;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400428
429 pchg->psy = psy;
430
431 psy->ac.name = "ac";
432 psy->ac.type = POWER_SUPPLY_TYPE_MAINS;
433 psy->ac.properties = lp8727_charger_prop;
434 psy->ac.num_properties = ARRAY_SIZE(lp8727_charger_prop);
435 psy->ac.get_property = lp8727_charger_get_property;
436 psy->ac.supplied_to = battery_supplied_to;
437 psy->ac.num_supplicants = ARRAY_SIZE(battery_supplied_to);
438
439 if (power_supply_register(pchg->dev, &psy->ac))
Devendra Naga6297b5e2012-07-29 23:31:55 +0545440 goto err_psy_ac;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400441
442 psy->usb.name = "usb";
443 psy->usb.type = POWER_SUPPLY_TYPE_USB;
444 psy->usb.properties = lp8727_charger_prop;
445 psy->usb.num_properties = ARRAY_SIZE(lp8727_charger_prop);
446 psy->usb.get_property = lp8727_charger_get_property;
447 psy->usb.supplied_to = battery_supplied_to;
448 psy->usb.num_supplicants = ARRAY_SIZE(battery_supplied_to);
449
450 if (power_supply_register(pchg->dev, &psy->usb))
Devendra Naga6297b5e2012-07-29 23:31:55 +0545451 goto err_psy_usb;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400452
453 psy->batt.name = "main_batt";
454 psy->batt.type = POWER_SUPPLY_TYPE_BATTERY;
455 psy->batt.properties = lp8727_battery_prop;
456 psy->batt.num_properties = ARRAY_SIZE(lp8727_battery_prop);
457 psy->batt.get_property = lp8727_battery_get_property;
458 psy->batt.external_power_changed = lp8727_charger_changed;
459
460 if (power_supply_register(pchg->dev, &psy->batt))
Devendra Naga6297b5e2012-07-29 23:31:55 +0545461 goto err_psy_batt;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400462
463 return 0;
464
Devendra Naga6297b5e2012-07-29 23:31:55 +0545465err_psy_batt:
466 power_supply_unregister(&psy->usb);
467err_psy_usb:
468 power_supply_unregister(&psy->ac);
469err_psy_ac:
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400470 return -EPERM;
471}
472
473static void lp8727_unregister_psy(struct lp8727_chg *pchg)
474{
475 struct lp8727_psy *psy = pchg->psy;
476
Kim, Miloce09aff2012-01-04 09:03:18 +0400477 if (!psy)
478 return;
479
480 power_supply_unregister(&psy->ac);
481 power_supply_unregister(&psy->usb);
482 power_supply_unregister(&psy->batt);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400483}
484
Kim, Milo17b45652013-05-10 07:17:51 +0000485#ifdef CONFIG_OF
486static struct lp8727_chg_param
487*lp8727_parse_charge_pdata(struct device *dev, struct device_node *np)
488{
489 struct lp8727_chg_param *param;
490
491 param = devm_kzalloc(dev, sizeof(*param), GFP_KERNEL);
492 if (!param)
493 goto out;
494
495 of_property_read_u8(np, "eoc-level", (u8 *)&param->eoc_level);
496 of_property_read_u8(np, "charging-current", (u8 *)&param->ichg);
497out:
498 return param;
499}
500
501static int lp8727_parse_dt(struct device *dev)
502{
503 struct device_node *np = dev->of_node;
504 struct device_node *child;
505 struct lp8727_platform_data *pdata;
506 const char *type;
507
508 /* If charging parameter is not defined, just skip parsing the dt */
509 if (of_get_child_count(np) == 0)
510 goto out;
511
512 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
513 if (!pdata)
514 return -ENOMEM;
515
516 of_property_read_u32(np, "debounce-ms", &pdata->debounce_msec);
517
518 for_each_child_of_node(np, child) {
519 of_property_read_string(child, "charger-type", &type);
520
521 if (!strcmp(type, "ac"))
522 pdata->ac = lp8727_parse_charge_pdata(dev, child);
523
524 if (!strcmp(type, "usb"))
525 pdata->usb = lp8727_parse_charge_pdata(dev, child);
526 }
527
528 dev->platform_data = pdata;
529out:
530 return 0;
531}
532#else
533static int lp8727_parse_dt(struct device *dev)
534{
535 return 0;
536}
537#endif
538
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400539static int lp8727_probe(struct i2c_client *cl, const struct i2c_device_id *id)
540{
541 struct lp8727_chg *pchg;
542 int ret;
543
Kim, Milo998a8e72011-11-17 21:43:06 -0800544 if (!i2c_check_functionality(cl->adapter, I2C_FUNC_SMBUS_I2C_BLOCK))
545 return -EIO;
546
Kim, Milo17b45652013-05-10 07:17:51 +0000547 if (cl->dev.of_node) {
548 ret = lp8727_parse_dt(&cl->dev);
549 if (ret)
550 return ret;
551 }
552
Kim, Milo74727c52012-08-31 09:22:46 +0000553 pchg = devm_kzalloc(&cl->dev, sizeof(*pchg), GFP_KERNEL);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400554 if (!pchg)
555 return -ENOMEM;
556
557 pchg->client = cl;
558 pchg->dev = &cl->dev;
559 pchg->pdata = cl->dev.platform_data;
560 i2c_set_clientdata(cl, pchg);
561
562 mutex_init(&pchg->xfer_lock);
563
Kim, Milo7da63342012-01-26 22:58:30 -0800564 ret = lp8727_init_device(pchg);
565 if (ret) {
566 dev_err(pchg->dev, "i2c communication err: %d", ret);
Kim, Milofb9adc52012-08-31 09:23:03 +0000567 return ret;
Kim, Milo7da63342012-01-26 22:58:30 -0800568 }
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400569
570 ret = lp8727_register_psy(pchg);
Kim, Milo7da63342012-01-26 22:58:30 -0800571 if (ret) {
572 dev_err(pchg->dev, "power supplies register err: %d", ret);
Kim, Milofb9adc52012-08-31 09:23:03 +0000573 return ret;
574 }
575
Kim, Milod71fda02012-08-31 09:23:57 +0000576 ret = lp8727_setup_irq(pchg);
Kim, Milofb9adc52012-08-31 09:23:03 +0000577 if (ret) {
578 dev_err(pchg->dev, "irq handler err: %d", ret);
579 lp8727_unregister_psy(pchg);
580 return ret;
Kim, Milo7da63342012-01-26 22:58:30 -0800581 }
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400582
583 return 0;
584}
585
Bill Pemberton415ec692012-11-19 13:26:07 -0500586static int lp8727_remove(struct i2c_client *cl)
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400587{
588 struct lp8727_chg *pchg = i2c_get_clientdata(cl);
589
Kim, Milod71fda02012-08-31 09:23:57 +0000590 lp8727_release_irq(pchg);
Kim, Milofb9adc52012-08-31 09:23:03 +0000591 lp8727_unregister_psy(pchg);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400592 return 0;
593}
594
Kim, Milo17b45652013-05-10 07:17:51 +0000595static const struct of_device_id lp8727_dt_ids[] = {
596 { .compatible = "ti,lp8727", },
597 { }
598};
599MODULE_DEVICE_TABLE(of, lp8727_dt_ids);
600
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400601static const struct i2c_device_id lp8727_ids[] = {
602 {"lp8727", 0},
Axel Lin455a0e22012-01-16 13:48:20 +0800603 { }
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400604};
Axel Line2c5f7d2012-01-12 20:45:02 +0800605MODULE_DEVICE_TABLE(i2c, lp8727_ids);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400606
607static struct i2c_driver lp8727_driver = {
608 .driver = {
609 .name = "lp8727",
Kim, Milo17b45652013-05-10 07:17:51 +0000610 .of_match_table = of_match_ptr(lp8727_dt_ids),
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400611 },
612 .probe = lp8727_probe,
Bill Pemberton28ea73f2012-11-19 13:20:40 -0500613 .remove = lp8727_remove,
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400614 .id_table = lp8727_ids,
615};
Axel Lin5ff92e72012-01-21 14:42:54 +0800616module_i2c_driver(lp8727_driver);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400617
Kim, Miloe39b8282012-01-29 17:28:18 -0800618MODULE_DESCRIPTION("TI/National Semiconductor LP8727 charger driver");
Kim, Milo18259ca2012-08-31 09:27:12 +0000619MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>, Daniel Jeong <daniel.jeong@ti.com>");
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400620MODULE_LICENSE("GPL");