blob: bed458172dd28ff1073d33c783d71ad3de63991c [file] [log] [blame]
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +03001/*
2 * TWL4030/TPS65950 BCI (Battery Charger Interface) driver
3 *
4 * Copyright (C) 2010 Gražvydas Ignotas <notasas@gmail.com>
5 *
6 * based on twl4030_bci_battery.c by TI
7 * Copyright (C) 2008 Texas Instruments, Inc.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 */
14
15#include <linux/init.h>
16#include <linux/module.h>
17#include <linux/slab.h>
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +053018#include <linux/err.h>
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +030019#include <linux/platform_device.h>
20#include <linux/interrupt.h>
21#include <linux/i2c/twl.h>
22#include <linux/power_supply.h>
23#include <linux/notifier.h>
24#include <linux/usb/otg.h>
NeilBrownab378132012-05-09 07:40:40 +100025#include <linux/regulator/machine.h>
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +030026
27#define TWL4030_BCIMSTATEC 0x02
28#define TWL4030_BCIICHG 0x08
29#define TWL4030_BCIVAC 0x0a
30#define TWL4030_BCIVBUS 0x0c
31#define TWL4030_BCIMFSTS4 0x10
32#define TWL4030_BCICTL1 0x23
NeilBrown210d4bc2012-05-09 07:40:40 +100033#define TWL4030_BB_CFG 0x12
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +030034
35#define TWL4030_BCIAUTOWEN BIT(5)
36#define TWL4030_CONFIG_DONE BIT(4)
37#define TWL4030_BCIAUTOUSB BIT(1)
38#define TWL4030_BCIAUTOAC BIT(0)
39#define TWL4030_CGAIN BIT(5)
40#define TWL4030_USBFASTMCHG BIT(2)
41#define TWL4030_STS_VBUS BIT(7)
42#define TWL4030_STS_USB_ID BIT(2)
NeilBrown210d4bc2012-05-09 07:40:40 +100043#define TWL4030_BBCHEN BIT(4)
Il Han94b05ca2012-08-17 16:49:43 +090044#define TWL4030_BBSEL_MASK 0x0c
45#define TWL4030_BBSEL_2V5 0x00
46#define TWL4030_BBSEL_3V0 0x04
47#define TWL4030_BBSEL_3V1 0x08
48#define TWL4030_BBSEL_3V2 0x0c
49#define TWL4030_BBISEL_MASK 0x03
50#define TWL4030_BBISEL_25uA 0x00
51#define TWL4030_BBISEL_150uA 0x01
52#define TWL4030_BBISEL_500uA 0x02
53#define TWL4030_BBISEL_1000uA 0x03
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +030054
55/* BCI interrupts */
56#define TWL4030_WOVF BIT(0) /* Watchdog overflow */
57#define TWL4030_TMOVF BIT(1) /* Timer overflow */
58#define TWL4030_ICHGHIGH BIT(2) /* Battery charge current high */
59#define TWL4030_ICHGLOW BIT(3) /* Battery cc. low / FSM state change */
60#define TWL4030_ICHGEOC BIT(4) /* Battery current end-of-charge */
61#define TWL4030_TBATOR2 BIT(5) /* Battery temperature out of range 2 */
62#define TWL4030_TBATOR1 BIT(6) /* Battery temperature out of range 1 */
63#define TWL4030_BATSTS BIT(7) /* Battery status */
64
65#define TWL4030_VBATLVL BIT(0) /* VBAT level */
66#define TWL4030_VBATOV BIT(1) /* VBAT overvoltage */
67#define TWL4030_VBUSOV BIT(2) /* VBUS overvoltage */
68#define TWL4030_ACCHGOV BIT(3) /* Ac charger overvoltage */
69
70#define TWL4030_MSTATEC_USB BIT(4)
71#define TWL4030_MSTATEC_AC BIT(5)
72#define TWL4030_MSTATEC_MASK 0x0f
73#define TWL4030_MSTATEC_QUICK1 0x02
74#define TWL4030_MSTATEC_QUICK7 0x07
75#define TWL4030_MSTATEC_COMPLETE1 0x0b
76#define TWL4030_MSTATEC_COMPLETE4 0x0e
77
78static bool allow_usb;
Grazvydas Ignotas28d48f02011-06-03 14:43:11 +030079module_param(allow_usb, bool, 0644);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +030080MODULE_PARM_DESC(allow_usb, "Allow USB charge drawing default current");
81
82struct twl4030_bci {
83 struct device *dev;
84 struct power_supply ac;
85 struct power_supply usb;
Heikki Krogerus86753812012-02-13 13:24:02 +020086 struct usb_phy *transceiver;
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +020087 struct notifier_block usb_nb;
Heikki Krogerusd6ccc442011-02-25 13:56:36 +020088 struct work_struct work;
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +030089 int irq_chg;
90 int irq_bci;
NeilBrownab378132012-05-09 07:40:40 +100091 struct regulator *usb_reg;
92 int usb_enabled;
Heikki Krogerusd6ccc442011-02-25 13:56:36 +020093
94 unsigned long event;
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +030095};
96
97/*
98 * clear and set bits on an given register on a given module
99 */
100static int twl4030_clear_set(u8 mod_no, u8 clear, u8 set, u8 reg)
101{
102 u8 val = 0;
103 int ret;
104
105 ret = twl_i2c_read_u8(mod_no, &val, reg);
106 if (ret)
107 return ret;
108
109 val &= ~clear;
110 val |= set;
111
112 return twl_i2c_write_u8(mod_no, val, reg);
113}
114
115static int twl4030_bci_read(u8 reg, u8 *val)
116{
Peter Ujfalusie9f14c12012-11-13 10:42:11 +0100117 return twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, val, reg);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300118}
119
120static int twl4030_clear_set_boot_bci(u8 clear, u8 set)
121{
Peter Ujfalusie9f14c12012-11-13 10:42:11 +0100122 return twl4030_clear_set(TWL_MODULE_PM_MASTER, clear,
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300123 TWL4030_CONFIG_DONE | TWL4030_BCIAUTOWEN | set,
124 TWL4030_PM_MASTER_BOOT_BCI);
125}
126
127static int twl4030bci_read_adc_val(u8 reg)
128{
129 int ret, temp;
130 u8 val;
131
132 /* read MSB */
133 ret = twl4030_bci_read(reg + 1, &val);
134 if (ret)
135 return ret;
136
137 temp = (int)(val & 0x03) << 8;
138
139 /* read LSB */
140 ret = twl4030_bci_read(reg, &val);
141 if (ret)
142 return ret;
143
144 return temp | val;
145}
146
147/*
148 * Check if VBUS power is present
149 */
150static int twl4030_bci_have_vbus(struct twl4030_bci *bci)
151{
152 int ret;
153 u8 hwsts;
154
Peter Ujfalusie9f14c12012-11-13 10:42:11 +0100155 ret = twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &hwsts,
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300156 TWL4030_PM_MASTER_STS_HW_CONDITIONS);
157 if (ret < 0)
158 return 0;
159
160 dev_dbg(bci->dev, "check_vbus: HW_CONDITIONS %02x\n", hwsts);
161
162 /* in case we also have STS_USB_ID, VBUS is driven by TWL itself */
163 if ((hwsts & TWL4030_STS_VBUS) && !(hwsts & TWL4030_STS_USB_ID))
164 return 1;
165
166 return 0;
167}
168
169/*
NeilBrown9c645d22012-05-09 07:40:39 +1000170 * Enable/Disable USB Charge functionality.
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300171 */
172static int twl4030_charger_enable_usb(struct twl4030_bci *bci, bool enable)
173{
174 int ret;
175
176 if (enable) {
NeilBrown9c645d22012-05-09 07:40:39 +1000177 /* Check for USB charger connected */
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300178 if (!twl4030_bci_have_vbus(bci))
179 return -ENODEV;
180
181 /*
182 * Until we can find out what current the device can provide,
183 * require a module param to enable USB charging.
184 */
185 if (!allow_usb) {
186 dev_warn(bci->dev, "USB charging is disabled.\n");
187 return -EACCES;
188 }
189
NeilBrownab378132012-05-09 07:40:40 +1000190 /* Need to keep regulator on */
191 if (!bci->usb_enabled) {
192 regulator_enable(bci->usb_reg);
193 bci->usb_enabled = 1;
194 }
195
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300196 /* forcing the field BCIAUTOUSB (BOOT_BCI[1]) to 1 */
197 ret = twl4030_clear_set_boot_bci(0, TWL4030_BCIAUTOUSB);
198 if (ret < 0)
199 return ret;
200
201 /* forcing USBFASTMCHG(BCIMFSTS4[2]) to 1 */
Peter Ujfalusie9f14c12012-11-13 10:42:11 +0100202 ret = twl4030_clear_set(TWL_MODULE_MAIN_CHARGE, 0,
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300203 TWL4030_USBFASTMCHG, TWL4030_BCIMFSTS4);
204 } else {
205 ret = twl4030_clear_set_boot_bci(TWL4030_BCIAUTOUSB, 0);
NeilBrownab378132012-05-09 07:40:40 +1000206 if (bci->usb_enabled) {
207 regulator_disable(bci->usb_reg);
208 bci->usb_enabled = 0;
209 }
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300210 }
211
212 return ret;
213}
214
215/*
216 * Enable/Disable AC Charge funtionality.
217 */
218static int twl4030_charger_enable_ac(bool enable)
219{
220 int ret;
221
222 if (enable)
223 ret = twl4030_clear_set_boot_bci(0, TWL4030_BCIAUTOAC);
224 else
225 ret = twl4030_clear_set_boot_bci(TWL4030_BCIAUTOAC, 0);
226
227 return ret;
228}
229
230/*
NeilBrown210d4bc2012-05-09 07:40:40 +1000231 * Enable/Disable charging of Backup Battery.
232 */
233static int twl4030_charger_enable_backup(int uvolt, int uamp)
234{
235 int ret;
236 u8 flags;
237
238 if (uvolt < 2500000 ||
239 uamp < 25) {
240 /* disable charging of backup battery */
Peter Ujfalusie9f14c12012-11-13 10:42:11 +0100241 ret = twl4030_clear_set(TWL_MODULE_PM_RECEIVER,
NeilBrown210d4bc2012-05-09 07:40:40 +1000242 TWL4030_BBCHEN, 0, TWL4030_BB_CFG);
243 return ret;
244 }
245
246 flags = TWL4030_BBCHEN;
247 if (uvolt >= 3200000)
248 flags |= TWL4030_BBSEL_3V2;
249 else if (uvolt >= 3100000)
250 flags |= TWL4030_BBSEL_3V1;
251 else if (uvolt >= 3000000)
252 flags |= TWL4030_BBSEL_3V0;
253 else
254 flags |= TWL4030_BBSEL_2V5;
255
256 if (uamp >= 1000)
257 flags |= TWL4030_BBISEL_1000uA;
258 else if (uamp >= 500)
259 flags |= TWL4030_BBISEL_500uA;
260 else if (uamp >= 150)
261 flags |= TWL4030_BBISEL_150uA;
262 else
263 flags |= TWL4030_BBISEL_25uA;
264
Peter Ujfalusie9f14c12012-11-13 10:42:11 +0100265 ret = twl4030_clear_set(TWL_MODULE_PM_RECEIVER,
NeilBrown210d4bc2012-05-09 07:40:40 +1000266 TWL4030_BBSEL_MASK | TWL4030_BBISEL_MASK,
267 flags,
268 TWL4030_BB_CFG);
269
270 return ret;
271}
272
273/*
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300274 * TWL4030 CHG_PRES (AC charger presence) events
275 */
276static irqreturn_t twl4030_charger_interrupt(int irq, void *arg)
277{
278 struct twl4030_bci *bci = arg;
279
280 dev_dbg(bci->dev, "CHG_PRES irq\n");
281 power_supply_changed(&bci->ac);
282 power_supply_changed(&bci->usb);
283
284 return IRQ_HANDLED;
285}
286
287/*
288 * TWL4030 BCI monitoring events
289 */
290static irqreturn_t twl4030_bci_interrupt(int irq, void *arg)
291{
292 struct twl4030_bci *bci = arg;
293 u8 irqs1, irqs2;
294 int ret;
295
296 ret = twl_i2c_read_u8(TWL4030_MODULE_INTERRUPTS, &irqs1,
297 TWL4030_INTERRUPTS_BCIISR1A);
298 if (ret < 0)
299 return IRQ_HANDLED;
300
301 ret = twl_i2c_read_u8(TWL4030_MODULE_INTERRUPTS, &irqs2,
302 TWL4030_INTERRUPTS_BCIISR2A);
303 if (ret < 0)
304 return IRQ_HANDLED;
305
306 dev_dbg(bci->dev, "BCI irq %02x %02x\n", irqs2, irqs1);
307
308 if (irqs1 & (TWL4030_ICHGLOW | TWL4030_ICHGEOC)) {
309 /* charger state change, inform the core */
310 power_supply_changed(&bci->ac);
311 power_supply_changed(&bci->usb);
312 }
313
314 /* various monitoring events, for now we just log them here */
315 if (irqs1 & (TWL4030_TBATOR2 | TWL4030_TBATOR1))
316 dev_warn(bci->dev, "battery temperature out of range\n");
317
318 if (irqs1 & TWL4030_BATSTS)
319 dev_crit(bci->dev, "battery disconnected\n");
320
321 if (irqs2 & TWL4030_VBATOV)
322 dev_crit(bci->dev, "VBAT overvoltage\n");
323
324 if (irqs2 & TWL4030_VBUSOV)
325 dev_crit(bci->dev, "VBUS overvoltage\n");
326
327 if (irqs2 & TWL4030_ACCHGOV)
328 dev_crit(bci->dev, "Ac charger overvoltage\n");
329
330 return IRQ_HANDLED;
331}
332
Heikki Krogerusd6ccc442011-02-25 13:56:36 +0200333static void twl4030_bci_usb_work(struct work_struct *data)
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300334{
Heikki Krogerusd6ccc442011-02-25 13:56:36 +0200335 struct twl4030_bci *bci = container_of(data, struct twl4030_bci, work);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300336
Heikki Krogerusd6ccc442011-02-25 13:56:36 +0200337 switch (bci->event) {
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300338 case USB_EVENT_VBUS:
339 case USB_EVENT_CHARGER:
340 twl4030_charger_enable_usb(bci, true);
341 break;
342 case USB_EVENT_NONE:
343 twl4030_charger_enable_usb(bci, false);
344 break;
345 }
Heikki Krogerusd6ccc442011-02-25 13:56:36 +0200346}
347
348static int twl4030_bci_usb_ncb(struct notifier_block *nb, unsigned long val,
349 void *priv)
350{
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200351 struct twl4030_bci *bci = container_of(nb, struct twl4030_bci, usb_nb);
Heikki Krogerusd6ccc442011-02-25 13:56:36 +0200352
353 dev_dbg(bci->dev, "OTG notify %lu\n", val);
354
355 bci->event = val;
356 schedule_work(&bci->work);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300357
358 return NOTIFY_OK;
359}
360
361/*
362 * TI provided formulas:
363 * CGAIN == 0: ICHG = (BCIICHG * 1.7) / (2^10 - 1) - 0.85
364 * CGAIN == 1: ICHG = (BCIICHG * 3.4) / (2^10 - 1) - 1.7
365 * Here we use integer approximation of:
366 * CGAIN == 0: val * 1.6618 - 0.85
367 * CGAIN == 1: (val * 1.6618 - 0.85) * 2
368 */
369static int twl4030_charger_get_current(void)
370{
371 int curr;
372 int ret;
373 u8 bcictl1;
374
375 curr = twl4030bci_read_adc_val(TWL4030_BCIICHG);
376 if (curr < 0)
377 return curr;
378
379 ret = twl4030_bci_read(TWL4030_BCICTL1, &bcictl1);
380 if (ret)
381 return ret;
382
383 ret = (curr * 16618 - 850 * 10000) / 10;
384 if (bcictl1 & TWL4030_CGAIN)
385 ret *= 2;
386
387 return ret;
388}
389
390/*
391 * Returns the main charge FSM state
392 * Or < 0 on failure.
393 */
394static int twl4030bci_state(struct twl4030_bci *bci)
395{
396 int ret;
397 u8 state;
398
399 ret = twl4030_bci_read(TWL4030_BCIMSTATEC, &state);
400 if (ret) {
401 pr_err("twl4030_bci: error reading BCIMSTATEC\n");
402 return ret;
403 }
404
405 dev_dbg(bci->dev, "state: %02x\n", state);
406
407 return state;
408}
409
410static int twl4030_bci_state_to_status(int state)
411{
412 state &= TWL4030_MSTATEC_MASK;
413 if (TWL4030_MSTATEC_QUICK1 <= state && state <= TWL4030_MSTATEC_QUICK7)
414 return POWER_SUPPLY_STATUS_CHARGING;
415 else if (TWL4030_MSTATEC_COMPLETE1 <= state &&
416 state <= TWL4030_MSTATEC_COMPLETE4)
417 return POWER_SUPPLY_STATUS_FULL;
418 else
419 return POWER_SUPPLY_STATUS_NOT_CHARGING;
420}
421
422static int twl4030_bci_get_property(struct power_supply *psy,
423 enum power_supply_property psp,
424 union power_supply_propval *val)
425{
426 struct twl4030_bci *bci = dev_get_drvdata(psy->dev->parent);
427 int is_charging;
428 int state;
429 int ret;
430
431 state = twl4030bci_state(bci);
432 if (state < 0)
433 return state;
434
435 if (psy->type == POWER_SUPPLY_TYPE_USB)
436 is_charging = state & TWL4030_MSTATEC_USB;
437 else
438 is_charging = state & TWL4030_MSTATEC_AC;
439
440 switch (psp) {
441 case POWER_SUPPLY_PROP_STATUS:
442 if (is_charging)
443 val->intval = twl4030_bci_state_to_status(state);
444 else
445 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
446 break;
447 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
448 /* charging must be active for meaningful result */
449 if (!is_charging)
450 return -ENODATA;
451 if (psy->type == POWER_SUPPLY_TYPE_USB) {
452 ret = twl4030bci_read_adc_val(TWL4030_BCIVBUS);
453 if (ret < 0)
454 return ret;
455 /* BCIVBUS uses ADCIN8, 7/1023 V/step */
456 val->intval = ret * 6843;
457 } else {
458 ret = twl4030bci_read_adc_val(TWL4030_BCIVAC);
459 if (ret < 0)
460 return ret;
461 /* BCIVAC uses ADCIN11, 10/1023 V/step */
462 val->intval = ret * 9775;
463 }
464 break;
465 case POWER_SUPPLY_PROP_CURRENT_NOW:
466 if (!is_charging)
467 return -ENODATA;
468 /* current measurement is shared between AC and USB */
469 ret = twl4030_charger_get_current();
470 if (ret < 0)
471 return ret;
472 val->intval = ret;
473 break;
474 case POWER_SUPPLY_PROP_ONLINE:
475 val->intval = is_charging &&
476 twl4030_bci_state_to_status(state) !=
477 POWER_SUPPLY_STATUS_NOT_CHARGING;
478 break;
479 default:
480 return -EINVAL;
481 }
482
483 return 0;
484}
485
486static enum power_supply_property twl4030_charger_props[] = {
487 POWER_SUPPLY_PROP_STATUS,
488 POWER_SUPPLY_PROP_ONLINE,
489 POWER_SUPPLY_PROP_VOLTAGE_NOW,
490 POWER_SUPPLY_PROP_CURRENT_NOW,
491};
492
493static int __init twl4030_bci_probe(struct platform_device *pdev)
494{
495 struct twl4030_bci *bci;
NeilBrown210d4bc2012-05-09 07:40:40 +1000496 struct twl4030_bci_platform_data *pdata = pdev->dev.platform_data;
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300497 int ret;
Grazvydas Ignotas28d48f02011-06-03 14:43:11 +0300498 u32 reg;
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300499
500 bci = kzalloc(sizeof(*bci), GFP_KERNEL);
501 if (bci == NULL)
502 return -ENOMEM;
503
504 bci->dev = &pdev->dev;
505 bci->irq_chg = platform_get_irq(pdev, 0);
506 bci->irq_bci = platform_get_irq(pdev, 1);
507
508 platform_set_drvdata(pdev, bci);
509
510 bci->ac.name = "twl4030_ac";
511 bci->ac.type = POWER_SUPPLY_TYPE_MAINS;
512 bci->ac.properties = twl4030_charger_props;
513 bci->ac.num_properties = ARRAY_SIZE(twl4030_charger_props);
514 bci->ac.get_property = twl4030_bci_get_property;
515
516 ret = power_supply_register(&pdev->dev, &bci->ac);
517 if (ret) {
518 dev_err(&pdev->dev, "failed to register ac: %d\n", ret);
519 goto fail_register_ac;
520 }
521
522 bci->usb.name = "twl4030_usb";
523 bci->usb.type = POWER_SUPPLY_TYPE_USB;
524 bci->usb.properties = twl4030_charger_props;
525 bci->usb.num_properties = ARRAY_SIZE(twl4030_charger_props);
526 bci->usb.get_property = twl4030_bci_get_property;
527
NeilBrownab378132012-05-09 07:40:40 +1000528 bci->usb_reg = regulator_get(bci->dev, "bci3v1");
529
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300530 ret = power_supply_register(&pdev->dev, &bci->usb);
531 if (ret) {
532 dev_err(&pdev->dev, "failed to register usb: %d\n", ret);
533 goto fail_register_usb;
534 }
535
536 ret = request_threaded_irq(bci->irq_chg, NULL,
Fengguang Wua2778b02012-08-23 19:56:43 +0800537 twl4030_charger_interrupt, IRQF_ONESHOT, pdev->name,
538 bci);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300539 if (ret < 0) {
540 dev_err(&pdev->dev, "could not request irq %d, status %d\n",
541 bci->irq_chg, ret);
542 goto fail_chg_irq;
543 }
544
545 ret = request_threaded_irq(bci->irq_bci, NULL,
Fengguang Wua2778b02012-08-23 19:56:43 +0800546 twl4030_bci_interrupt, IRQF_ONESHOT, pdev->name, bci);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300547 if (ret < 0) {
548 dev_err(&pdev->dev, "could not request irq %d, status %d\n",
549 bci->irq_bci, ret);
550 goto fail_bci_irq;
551 }
552
Heikki Krogerusd6ccc442011-02-25 13:56:36 +0200553 INIT_WORK(&bci->work, twl4030_bci_usb_work);
554
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +0530555 bci->transceiver = usb_get_phy(USB_PHY_TYPE_USB2);
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +0530556 if (!IS_ERR_OR_NULL(bci->transceiver)) {
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200557 bci->usb_nb.notifier_call = twl4030_bci_usb_ncb;
558 usb_register_notifier(bci->transceiver, &bci->usb_nb);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300559 }
560
561 /* Enable interrupts now. */
Grazvydas Ignotas28d48f02011-06-03 14:43:11 +0300562 reg = ~(u32)(TWL4030_ICHGLOW | TWL4030_ICHGEOC | TWL4030_TBATOR2 |
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300563 TWL4030_TBATOR1 | TWL4030_BATSTS);
564 ret = twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS, reg,
565 TWL4030_INTERRUPTS_BCIIMR1A);
566 if (ret < 0) {
567 dev_err(&pdev->dev, "failed to unmask interrupts: %d\n", ret);
568 goto fail_unmask_interrupts;
569 }
570
Grazvydas Ignotas28d48f02011-06-03 14:43:11 +0300571 reg = ~(u32)(TWL4030_VBATOV | TWL4030_VBUSOV | TWL4030_ACCHGOV);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300572 ret = twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS, reg,
573 TWL4030_INTERRUPTS_BCIIMR2A);
574 if (ret < 0)
575 dev_warn(&pdev->dev, "failed to unmask interrupts: %d\n", ret);
576
577 twl4030_charger_enable_ac(true);
578 twl4030_charger_enable_usb(bci, true);
NeilBrown210d4bc2012-05-09 07:40:40 +1000579 twl4030_charger_enable_backup(pdata->bb_uvolt,
580 pdata->bb_uamp);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300581
582 return 0;
583
584fail_unmask_interrupts:
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +0530585 if (!IS_ERR_OR_NULL(bci->transceiver)) {
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200586 usb_unregister_notifier(bci->transceiver, &bci->usb_nb);
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +0530587 usb_put_phy(bci->transceiver);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300588 }
589 free_irq(bci->irq_bci, bci);
590fail_bci_irq:
591 free_irq(bci->irq_chg, bci);
592fail_chg_irq:
593 power_supply_unregister(&bci->usb);
594fail_register_usb:
595 power_supply_unregister(&bci->ac);
596fail_register_ac:
597 platform_set_drvdata(pdev, NULL);
598 kfree(bci);
599
600 return ret;
601}
602
603static int __exit twl4030_bci_remove(struct platform_device *pdev)
604{
605 struct twl4030_bci *bci = platform_get_drvdata(pdev);
606
607 twl4030_charger_enable_ac(false);
608 twl4030_charger_enable_usb(bci, false);
NeilBrown210d4bc2012-05-09 07:40:40 +1000609 twl4030_charger_enable_backup(0, 0);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300610
611 /* mask interrupts */
612 twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS, 0xff,
613 TWL4030_INTERRUPTS_BCIIMR1A);
614 twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS, 0xff,
615 TWL4030_INTERRUPTS_BCIIMR2A);
616
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +0530617 if (!IS_ERR_OR_NULL(bci->transceiver)) {
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200618 usb_unregister_notifier(bci->transceiver, &bci->usb_nb);
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +0530619 usb_put_phy(bci->transceiver);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300620 }
621 free_irq(bci->irq_bci, bci);
622 free_irq(bci->irq_chg, bci);
623 power_supply_unregister(&bci->usb);
624 power_supply_unregister(&bci->ac);
625 platform_set_drvdata(pdev, NULL);
626 kfree(bci);
627
628 return 0;
629}
630
631static struct platform_driver twl4030_bci_driver = {
632 .driver = {
633 .name = "twl4030_bci",
634 .owner = THIS_MODULE,
635 },
636 .remove = __exit_p(twl4030_bci_remove),
637};
638
Jingoo Han4420dc22013-03-05 13:01:36 +0900639module_platform_driver_probe(twl4030_bci_driver, twl4030_bci_probe);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300640
Grazvydas Ignotas28d48f02011-06-03 14:43:11 +0300641MODULE_AUTHOR("Gražvydas Ignotas");
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300642MODULE_DESCRIPTION("TWL4030 Battery Charger Interface driver");
643MODULE_LICENSE("GPL");
644MODULE_ALIAS("platform:twl4030_bci");