perf/x86/amd: Generalize northbridge constraints code for family 15h
[linux/fpc-iii.git] / drivers / power / lp8727_charger.c
blob4ee71a90e2483f90a32cc2857a0b95833d7cffb8
1 /*
2 * Driver for LP8727 Micro/Mini USB IC with integrated charger
4 * Copyright (C) 2011 Texas Instruments
5 * Copyright (C) 2011 National Semiconductor
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.
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>
18 #include <linux/platform_data/lp8727.h>
20 #define LP8788_NUM_INTREGS 2
21 #define DEFAULT_DEBOUNCE_MSEC 270
23 /* Registers */
24 #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
33 /* CTRL1 register */
34 #define LP8727_CP_EN BIT(0)
35 #define LP8727_ADC_EN BIT(1)
36 #define LP8727_ID200_EN BIT(4)
38 /* CTRL2 register */
39 #define LP8727_CHGDET_EN BIT(1)
40 #define LP8727_INT_EN BIT(6)
42 /* SWCTRL register */
43 #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)
48 /* INT1 register */
49 #define LP8727_IDNO (0xF << 0)
50 #define LP8727_VBUS BIT(4)
52 /* STATUS1 register */
53 #define LP8727_CHGSTAT (3 << 4)
54 #define LP8727_CHPORT BIT(6)
55 #define LP8727_DCPORT BIT(7)
56 #define LP8727_STAT_EOC 0x30
58 /* STATUS2 register */
59 #define LP8727_TEMP_STAT (3 << 5)
60 #define LP8727_TEMP_SHIFT 5
62 /* CHGCTRL2 register */
63 #define LP8727_ICHG_SHIFT 4
65 enum lp8727_dev_id {
66 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,
74 enum lp8727_die_temp {
75 LP8788_TEMP_75C,
76 LP8788_TEMP_95C,
77 LP8788_TEMP_115C,
78 LP8788_TEMP_135C,
81 struct lp8727_psy {
82 struct power_supply ac;
83 struct power_supply usb;
84 struct power_supply batt;
87 struct lp8727_chg {
88 struct device *dev;
89 struct i2c_client *client;
90 struct mutex xfer_lock;
91 struct lp8727_psy *psy;
92 struct lp8727_platform_data *pdata;
94 /* Charger Data */
95 enum lp8727_dev_id devid;
96 struct lp8727_chg_param *chg_param;
98 /* Interrupt Handling */
99 int irq;
100 struct delayed_work work;
101 unsigned long debounce_jiffies;
104 static int lp8727_read_bytes(struct lp8727_chg *pchg, u8 reg, u8 *data, u8 len)
106 s32 ret;
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);
112 return (ret != len) ? -EIO : 0;
115 static inline int lp8727_read_byte(struct lp8727_chg *pchg, u8 reg, u8 *data)
117 return lp8727_read_bytes(pchg, reg, data, 1);
120 static int lp8727_write_byte(struct lp8727_chg *pchg, u8 reg, u8 data)
122 int ret;
124 mutex_lock(&pchg->xfer_lock);
125 ret = i2c_smbus_write_byte_data(pchg->client, reg, data);
126 mutex_unlock(&pchg->xfer_lock);
128 return ret;
131 static bool lp8727_is_charger_attached(const char *name, int id)
133 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;
138 return id >= LP8727_ID_TA && id <= LP8727_ID_USB_CHG;
141 static int lp8727_init_device(struct lp8727_chg *pchg)
143 u8 val;
144 int ret;
145 u8 intstat[LP8788_NUM_INTREGS];
147 /* clear interrupts */
148 ret = lp8727_read_bytes(pchg, LP8727_INT1, intstat, LP8788_NUM_INTREGS);
149 if (ret)
150 return ret;
152 val = LP8727_ID200_EN | LP8727_ADC_EN | LP8727_CP_EN;
153 ret = lp8727_write_byte(pchg, LP8727_CTRL1, val);
154 if (ret)
155 return ret;
157 val = LP8727_INT_EN | LP8727_CHGDET_EN;
158 return lp8727_write_byte(pchg, LP8727_CTRL2, val);
161 static int lp8727_is_dedicated_charger(struct lp8727_chg *pchg)
163 u8 val;
165 lp8727_read_byte(pchg, LP8727_STATUS1, &val);
166 return val & LP8727_DCPORT;
169 static int lp8727_is_usb_charger(struct lp8727_chg *pchg)
171 u8 val;
173 lp8727_read_byte(pchg, LP8727_STATUS1, &val);
174 return val & LP8727_CHPORT;
177 static inline void lp8727_ctrl_switch(struct lp8727_chg *pchg, u8 sw)
179 lp8727_write_byte(pchg, LP8727_SWCTRL, sw);
182 static void lp8727_id_detection(struct lp8727_chg *pchg, u8 id, int vbusin)
184 struct lp8727_platform_data *pdata = pchg->pdata;
185 u8 devid = LP8727_ID_NONE;
186 u8 swctrl = LP8727_SW_DM1_HiZ | LP8727_SW_DP2_HiZ;
188 switch (id) {
189 case 0x5:
190 devid = LP8727_ID_TA;
191 pchg->chg_param = pdata ? pdata->ac : NULL;
192 break;
193 case 0xB:
194 if (lp8727_is_dedicated_charger(pchg)) {
195 pchg->chg_param = pdata ? pdata->ac : NULL;
196 devid = LP8727_ID_DEDICATED_CHG;
197 } else if (lp8727_is_usb_charger(pchg)) {
198 pchg->chg_param = pdata ? pdata->usb : NULL;
199 devid = LP8727_ID_USB_CHG;
200 swctrl = LP8727_SW_DM1_DM | LP8727_SW_DP2_DP;
201 } else if (vbusin) {
202 devid = LP8727_ID_USB_DS;
203 swctrl = LP8727_SW_DM1_DM | LP8727_SW_DP2_DP;
205 break;
206 default:
207 devid = LP8727_ID_NONE;
208 pchg->chg_param = NULL;
209 break;
212 pchg->devid = devid;
213 lp8727_ctrl_switch(pchg, swctrl);
216 static void lp8727_enable_chgdet(struct lp8727_chg *pchg)
218 u8 val;
220 lp8727_read_byte(pchg, LP8727_CTRL2, &val);
221 val |= LP8727_CHGDET_EN;
222 lp8727_write_byte(pchg, LP8727_CTRL2, val);
225 static void lp8727_delayed_func(struct work_struct *_work)
227 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;
233 if (lp8727_read_bytes(pchg, LP8727_INT1, intstat, LP8788_NUM_INTREGS)) {
234 dev_err(pchg->dev, "can not read INT registers\n");
235 return;
238 idno = intstat[0] & LP8727_IDNO;
239 vbus = intstat[0] & LP8727_VBUS;
241 lp8727_id_detection(pchg, idno, vbus);
242 lp8727_enable_chgdet(pchg);
244 power_supply_changed(&pchg->psy->ac);
245 power_supply_changed(&pchg->psy->usb);
246 power_supply_changed(&pchg->psy->batt);
249 static irqreturn_t lp8727_isr_func(int irq, void *ptr)
251 struct lp8727_chg *pchg = ptr;
253 schedule_delayed_work(&pchg->work, pchg->debounce_jiffies);
254 return IRQ_HANDLED;
257 static int lp8727_setup_irq(struct lp8727_chg *pchg)
259 int ret;
260 int irq = pchg->client->irq;
261 unsigned delay_msec = pchg->pdata ? pchg->pdata->debounce_msec :
262 DEFAULT_DEBOUNCE_MSEC;
264 INIT_DELAYED_WORK(&pchg->work, lp8727_delayed_func);
266 if (irq <= 0) {
267 dev_warn(pchg->dev, "invalid irq number: %d\n", irq);
268 return 0;
271 ret = request_threaded_irq(irq, NULL, lp8727_isr_func,
272 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
273 "lp8727_irq", pchg);
275 if (ret)
276 return ret;
278 pchg->irq = irq;
279 pchg->debounce_jiffies = msecs_to_jiffies(delay_msec);
281 return 0;
284 static void lp8727_release_irq(struct lp8727_chg *pchg)
286 cancel_delayed_work_sync(&pchg->work);
288 if (pchg->irq)
289 free_irq(pchg->irq, pchg);
292 static enum power_supply_property lp8727_charger_prop[] = {
293 POWER_SUPPLY_PROP_ONLINE,
296 static 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,
305 static char *battery_supplied_to[] = {
306 "main_batt",
309 static int lp8727_charger_get_property(struct power_supply *psy,
310 enum power_supply_property psp,
311 union power_supply_propval *val)
313 struct lp8727_chg *pchg = dev_get_drvdata(psy->dev->parent);
315 if (psp != POWER_SUPPLY_PROP_ONLINE)
316 return -EINVAL;
318 val->intval = lp8727_is_charger_attached(psy->name, pchg->devid);
320 return 0;
323 static bool lp8727_is_high_temperature(enum lp8727_die_temp temp)
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;
335 static int lp8727_battery_get_property(struct power_supply *psy,
336 enum power_supply_property psp,
337 union power_supply_propval *val)
339 struct lp8727_chg *pchg = dev_get_drvdata(psy->dev->parent);
340 struct lp8727_platform_data *pdata = pchg->pdata;
341 enum lp8727_die_temp temp;
342 u8 read;
344 switch (psp) {
345 case POWER_SUPPLY_PROP_STATUS:
346 if (!lp8727_is_charger_attached(psy->name, pchg->devid)) {
347 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
348 return 0;
351 lp8727_read_byte(pchg, LP8727_STATUS1, &read);
353 val->intval = (read & LP8727_CHGSTAT) == LP8727_STAT_EOC ?
354 POWER_SUPPLY_STATUS_FULL :
355 POWER_SUPPLY_STATUS_CHARGING;
356 break;
357 case POWER_SUPPLY_PROP_HEALTH:
358 lp8727_read_byte(pchg, LP8727_STATUS2, &read);
359 temp = (read & LP8727_TEMP_STAT) >> LP8727_TEMP_SHIFT;
361 val->intval = lp8727_is_high_temperature(temp) ?
362 POWER_SUPPLY_HEALTH_OVERHEAT :
363 POWER_SUPPLY_HEALTH_GOOD;
364 break;
365 case POWER_SUPPLY_PROP_PRESENT:
366 if (!pdata)
367 return -EINVAL;
369 if (pdata->get_batt_present)
370 val->intval = pchg->pdata->get_batt_present();
371 break;
372 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
373 if (!pdata)
374 return -EINVAL;
376 if (pdata->get_batt_level)
377 val->intval = pchg->pdata->get_batt_level();
378 break;
379 case POWER_SUPPLY_PROP_CAPACITY:
380 if (!pdata)
381 return -EINVAL;
383 if (pdata->get_batt_capacity)
384 val->intval = pchg->pdata->get_batt_capacity();
385 break;
386 case POWER_SUPPLY_PROP_TEMP:
387 if (!pdata)
388 return -EINVAL;
390 if (pdata->get_batt_temp)
391 val->intval = pchg->pdata->get_batt_temp();
392 break;
393 default:
394 break;
397 return 0;
400 static void lp8727_charger_changed(struct power_supply *psy)
402 struct lp8727_chg *pchg = dev_get_drvdata(psy->dev->parent);
403 u8 eoc_level;
404 u8 ichg;
405 u8 val;
407 /* skip if no charger exists */
408 if (!lp8727_is_charger_attached(psy->name, pchg->devid))
409 return;
411 /* update charging parameters */
412 if (pchg->chg_param) {
413 eoc_level = pchg->chg_param->eoc_level;
414 ichg = pchg->chg_param->ichg;
415 val = (ichg << LP8727_ICHG_SHIFT) | eoc_level;
416 lp8727_write_byte(pchg, LP8727_CHGCTRL2, val);
420 static int lp8727_register_psy(struct lp8727_chg *pchg)
422 struct lp8727_psy *psy;
424 psy = devm_kzalloc(pchg->dev, sizeof(*psy), GFP_KERNEL);
425 if (!psy)
426 return -ENOMEM;
428 pchg->psy = psy;
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);
438 if (power_supply_register(pchg->dev, &psy->ac))
439 goto err_psy_ac;
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);
449 if (power_supply_register(pchg->dev, &psy->usb))
450 goto err_psy_usb;
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;
459 if (power_supply_register(pchg->dev, &psy->batt))
460 goto err_psy_batt;
462 return 0;
464 err_psy_batt:
465 power_supply_unregister(&psy->usb);
466 err_psy_usb:
467 power_supply_unregister(&psy->ac);
468 err_psy_ac:
469 return -EPERM;
472 static void lp8727_unregister_psy(struct lp8727_chg *pchg)
474 struct lp8727_psy *psy = pchg->psy;
476 if (!psy)
477 return;
479 power_supply_unregister(&psy->ac);
480 power_supply_unregister(&psy->usb);
481 power_supply_unregister(&psy->batt);
484 static int lp8727_probe(struct i2c_client *cl, const struct i2c_device_id *id)
486 struct lp8727_chg *pchg;
487 int ret;
489 if (!i2c_check_functionality(cl->adapter, I2C_FUNC_SMBUS_I2C_BLOCK))
490 return -EIO;
492 pchg = devm_kzalloc(&cl->dev, sizeof(*pchg), GFP_KERNEL);
493 if (!pchg)
494 return -ENOMEM;
496 pchg->client = cl;
497 pchg->dev = &cl->dev;
498 pchg->pdata = cl->dev.platform_data;
499 i2c_set_clientdata(cl, pchg);
501 mutex_init(&pchg->xfer_lock);
503 ret = lp8727_init_device(pchg);
504 if (ret) {
505 dev_err(pchg->dev, "i2c communication err: %d", ret);
506 return ret;
509 ret = lp8727_register_psy(pchg);
510 if (ret) {
511 dev_err(pchg->dev, "power supplies register err: %d", ret);
512 return ret;
515 ret = lp8727_setup_irq(pchg);
516 if (ret) {
517 dev_err(pchg->dev, "irq handler err: %d", ret);
518 lp8727_unregister_psy(pchg);
519 return ret;
522 return 0;
525 static int lp8727_remove(struct i2c_client *cl)
527 struct lp8727_chg *pchg = i2c_get_clientdata(cl);
529 lp8727_release_irq(pchg);
530 lp8727_unregister_psy(pchg);
531 return 0;
534 static const struct i2c_device_id lp8727_ids[] = {
535 {"lp8727", 0},
538 MODULE_DEVICE_TABLE(i2c, lp8727_ids);
540 static struct i2c_driver lp8727_driver = {
541 .driver = {
542 .name = "lp8727",
544 .probe = lp8727_probe,
545 .remove = lp8727_remove,
546 .id_table = lp8727_ids,
548 module_i2c_driver(lp8727_driver);
550 MODULE_DESCRIPTION("TI/National Semiconductor LP8727 charger driver");
551 MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>, Daniel Jeong <daniel.jeong@ti.com>");
552 MODULE_LICENSE("GPL");