blob: 3b7cc631bb8a151970a2b011e053f0cddaa0e6e4 [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>
25
26#define TWL4030_BCIMSTATEC 0x02
27#define TWL4030_BCIICHG 0x08
28#define TWL4030_BCIVAC 0x0a
29#define TWL4030_BCIVBUS 0x0c
Nishanth Menon61a77842014-05-28 16:46:49 -050030#define TWL4030_BCIMFSTS3 0x0F
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +030031#define TWL4030_BCIMFSTS4 0x10
32#define TWL4030_BCICTL1 0x23
NeilBrown210d4bc2012-05-09 07:40:40 +100033#define TWL4030_BB_CFG 0x12
NeilBrown1098cb52015-07-30 10:11:24 +100034#define TWL4030_BCIIREF1 0x27
35#define TWL4030_BCIIREF2 0x28
36#define TWL4030_BCIMFKEY 0x11
37#define TWL4030_BCIMFTH8 0x1d
38#define TWL4030_BCIMFTH9 0x1e
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +030039
Nishanth Menon61a77842014-05-28 16:46:49 -050040#define TWL4030_BCIMFSTS1 0x01
41
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +030042#define TWL4030_BCIAUTOWEN BIT(5)
43#define TWL4030_CONFIG_DONE BIT(4)
44#define TWL4030_BCIAUTOUSB BIT(1)
45#define TWL4030_BCIAUTOAC BIT(0)
46#define TWL4030_CGAIN BIT(5)
47#define TWL4030_USBFASTMCHG BIT(2)
48#define TWL4030_STS_VBUS BIT(7)
49#define TWL4030_STS_USB_ID BIT(2)
NeilBrown210d4bc2012-05-09 07:40:40 +100050#define TWL4030_BBCHEN BIT(4)
Il Han94b05ca2012-08-17 16:49:43 +090051#define TWL4030_BBSEL_MASK 0x0c
52#define TWL4030_BBSEL_2V5 0x00
53#define TWL4030_BBSEL_3V0 0x04
54#define TWL4030_BBSEL_3V1 0x08
55#define TWL4030_BBSEL_3V2 0x0c
56#define TWL4030_BBISEL_MASK 0x03
57#define TWL4030_BBISEL_25uA 0x00
58#define TWL4030_BBISEL_150uA 0x01
59#define TWL4030_BBISEL_500uA 0x02
60#define TWL4030_BBISEL_1000uA 0x03
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +030061
Nishanth Menon61a77842014-05-28 16:46:49 -050062#define TWL4030_BATSTSPCHG BIT(2)
63#define TWL4030_BATSTSMCHG BIT(6)
64
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +030065/* BCI interrupts */
66#define TWL4030_WOVF BIT(0) /* Watchdog overflow */
67#define TWL4030_TMOVF BIT(1) /* Timer overflow */
68#define TWL4030_ICHGHIGH BIT(2) /* Battery charge current high */
69#define TWL4030_ICHGLOW BIT(3) /* Battery cc. low / FSM state change */
70#define TWL4030_ICHGEOC BIT(4) /* Battery current end-of-charge */
71#define TWL4030_TBATOR2 BIT(5) /* Battery temperature out of range 2 */
72#define TWL4030_TBATOR1 BIT(6) /* Battery temperature out of range 1 */
73#define TWL4030_BATSTS BIT(7) /* Battery status */
74
75#define TWL4030_VBATLVL BIT(0) /* VBAT level */
76#define TWL4030_VBATOV BIT(1) /* VBAT overvoltage */
77#define TWL4030_VBUSOV BIT(2) /* VBUS overvoltage */
78#define TWL4030_ACCHGOV BIT(3) /* Ac charger overvoltage */
79
80#define TWL4030_MSTATEC_USB BIT(4)
81#define TWL4030_MSTATEC_AC BIT(5)
82#define TWL4030_MSTATEC_MASK 0x0f
83#define TWL4030_MSTATEC_QUICK1 0x02
84#define TWL4030_MSTATEC_QUICK7 0x07
85#define TWL4030_MSTATEC_COMPLETE1 0x0b
86#define TWL4030_MSTATEC_COMPLETE4 0x0e
87
88static bool allow_usb;
Grazvydas Ignotas28d48f02011-06-03 14:43:11 +030089module_param(allow_usb, bool, 0644);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +030090MODULE_PARM_DESC(allow_usb, "Allow USB charge drawing default current");
91
92struct twl4030_bci {
93 struct device *dev;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +010094 struct power_supply *ac;
95 struct power_supply *usb;
Heikki Krogerus86753812012-02-13 13:24:02 +020096 struct usb_phy *transceiver;
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +020097 struct notifier_block usb_nb;
Heikki Krogerusd6ccc442011-02-25 13:56:36 +020098 struct work_struct work;
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +030099 int irq_chg;
100 int irq_bci;
NeilBrownab378132012-05-09 07:40:40 +1000101 int usb_enabled;
Heikki Krogerusd6ccc442011-02-25 13:56:36 +0200102
NeilBrown1098cb52015-07-30 10:11:24 +1000103 /*
104 * ichg values in uA. If any are 'large', we set CGAIN to
105 * '1' which doubles the range for half the precision.
106 */
107 unsigned int ichg_eoc, ichg_lo, ichg_hi, cur;
108
Heikki Krogerusd6ccc442011-02-25 13:56:36 +0200109 unsigned long event;
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300110};
111
112/*
113 * clear and set bits on an given register on a given module
114 */
115static int twl4030_clear_set(u8 mod_no, u8 clear, u8 set, u8 reg)
116{
117 u8 val = 0;
118 int ret;
119
120 ret = twl_i2c_read_u8(mod_no, &val, reg);
121 if (ret)
122 return ret;
123
124 val &= ~clear;
125 val |= set;
126
127 return twl_i2c_write_u8(mod_no, val, reg);
128}
129
130static int twl4030_bci_read(u8 reg, u8 *val)
131{
Peter Ujfalusie9f14c12012-11-13 10:42:11 +0100132 return twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, val, reg);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300133}
134
135static int twl4030_clear_set_boot_bci(u8 clear, u8 set)
136{
Peter Ujfalusie9f14c12012-11-13 10:42:11 +0100137 return twl4030_clear_set(TWL_MODULE_PM_MASTER, clear,
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300138 TWL4030_CONFIG_DONE | TWL4030_BCIAUTOWEN | set,
139 TWL4030_PM_MASTER_BOOT_BCI);
140}
141
142static int twl4030bci_read_adc_val(u8 reg)
143{
144 int ret, temp;
145 u8 val;
146
147 /* read MSB */
148 ret = twl4030_bci_read(reg + 1, &val);
149 if (ret)
150 return ret;
151
152 temp = (int)(val & 0x03) << 8;
153
154 /* read LSB */
155 ret = twl4030_bci_read(reg, &val);
156 if (ret)
157 return ret;
158
159 return temp | val;
160}
161
162/*
Nishanth Menon61a77842014-05-28 16:46:49 -0500163 * Check if Battery Pack was present
164 */
165static int twl4030_is_battery_present(struct twl4030_bci *bci)
166{
167 int ret;
168 u8 val = 0;
169
170 /* Battery presence in Main charge? */
171 ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, &val, TWL4030_BCIMFSTS3);
172 if (ret)
173 return ret;
174 if (val & TWL4030_BATSTSMCHG)
175 return 0;
176
177 /*
178 * OK, It could be that bootloader did not enable main charger,
179 * pre-charge is h/w auto. So, Battery presence in Pre-charge?
180 */
181 ret = twl_i2c_read_u8(TWL4030_MODULE_PRECHARGE, &val,
182 TWL4030_BCIMFSTS1);
183 if (ret)
184 return ret;
185 if (val & TWL4030_BATSTSPCHG)
186 return 0;
187
188 return -ENODEV;
189}
190
191/*
NeilBrown3b542f02015-07-30 10:11:24 +1000192 * TI provided formulas:
193 * CGAIN == 0: ICHG = (BCIICHG * 1.7) / (2^10 - 1) - 0.85
194 * CGAIN == 1: ICHG = (BCIICHG * 3.4) / (2^10 - 1) - 1.7
195 * Here we use integer approximation of:
196 * CGAIN == 0: val * 1.6618 - 0.85 * 1000
197 * CGAIN == 1: (val * 1.6618 - 0.85 * 1000) * 2
198 */
199/*
200 * convert twl register value for currents into uA
201 */
202static int regval2ua(int regval, bool cgain)
203{
204 if (cgain)
205 return (regval * 16618 - 8500 * 1000) / 5;
206 else
207 return (regval * 16618 - 8500 * 1000) / 10;
208}
209
210/*
211 * convert uA currents into twl register value
212 */
213static int ua2regval(int ua, bool cgain)
214{
215 int ret;
216 if (cgain)
217 ua /= 2;
218 ret = (ua * 10 + 8500 * 1000) / 16618;
219 /* rounding problems */
220 if (ret < 512)
221 ret = 512;
222 return ret;
223}
224
NeilBrown1098cb52015-07-30 10:11:24 +1000225static int twl4030_charger_update_current(struct twl4030_bci *bci)
226{
227 int status;
228 unsigned reg, cur_reg;
229 u8 bcictl1, oldreg, fullreg;
230 bool cgain = false;
231 u8 boot_bci;
232
233 /* First, check thresholds and see if cgain is needed */
234 if (bci->ichg_eoc >= 200000)
235 cgain = true;
236 if (bci->ichg_lo >= 400000)
237 cgain = true;
238 if (bci->ichg_hi >= 820000)
239 cgain = true;
240 if (bci->cur > 852000)
241 cgain = true;
242
243 status = twl4030_bci_read(TWL4030_BCICTL1, &bcictl1);
244 if (status < 0)
245 return status;
246 if (twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &boot_bci,
247 TWL4030_PM_MASTER_BOOT_BCI) < 0)
248 boot_bci = 0;
249 boot_bci &= 7;
250
251 if ((!!cgain) != !!(bcictl1 & TWL4030_CGAIN))
252 /* Need to turn for charging while we change the
253 * CGAIN bit. Leave it off while everything is
254 * updated.
255 */
256 twl4030_clear_set_boot_bci(boot_bci, 0);
257
258 /*
259 * For ichg_eoc, the hardware only supports reg values matching
260 * 100XXXX000, and requires the XXXX be stored in the high nibble
261 * of TWL4030_BCIMFTH8.
262 */
263 reg = ua2regval(bci->ichg_eoc, cgain);
264 if (reg > 0x278)
265 reg = 0x278;
266 if (reg < 0x200)
267 reg = 0x200;
268 reg = (reg >> 3) & 0xf;
269 fullreg = reg << 4;
270
271 /*
272 * For ichg_lo, reg value must match 10XXXX0000.
273 * XXXX is stored in low nibble of TWL4030_BCIMFTH8.
274 */
275 reg = ua2regval(bci->ichg_lo, cgain);
276 if (reg > 0x2F0)
277 reg = 0x2F0;
278 if (reg < 0x200)
279 reg = 0x200;
280 reg = (reg >> 4) & 0xf;
281 fullreg |= reg;
282
283 /* ichg_eoc and ichg_lo live in same register */
284 status = twl4030_bci_read(TWL4030_BCIMFTH8, &oldreg);
285 if (status < 0)
286 return status;
287 if (oldreg != fullreg) {
288 status = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0xF4,
289 TWL4030_BCIMFKEY);
290 if (status < 0)
291 return status;
292 twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
293 fullreg, TWL4030_BCIMFTH8);
294 }
295
296 /* ichg_hi threshold must be 1XXXX01100 (I think) */
297 reg = ua2regval(bci->ichg_hi, cgain);
298 if (reg > 0x3E0)
299 reg = 0x3E0;
300 if (reg < 0x200)
301 reg = 0x200;
302 fullreg = (reg >> 5) & 0xF;
303 fullreg <<= 4;
304 status = twl4030_bci_read(TWL4030_BCIMFTH9, &oldreg);
305 if (status < 0)
306 return status;
307 if ((oldreg & 0xF0) != fullreg) {
308 fullreg |= (oldreg & 0x0F);
309 status = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0xE7,
310 TWL4030_BCIMFKEY);
311 if (status < 0)
312 return status;
313 twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
314 fullreg, TWL4030_BCIMFTH9);
315 }
316
317 /*
318 * And finally, set the current. This is stored in
319 * two registers.
320 */
321 reg = ua2regval(bci->cur, cgain);
322 /* we have only 10 bits */
323 if (reg > 0x3ff)
324 reg = 0x3ff;
325 status = twl4030_bci_read(TWL4030_BCIIREF1, &oldreg);
326 if (status < 0)
327 return status;
328 cur_reg = oldreg;
329 status = twl4030_bci_read(TWL4030_BCIIREF2, &oldreg);
330 if (status < 0)
331 return status;
332 cur_reg |= oldreg << 8;
333 if (reg != oldreg) {
334 /* disable write protection for one write access for
335 * BCIIREF */
336 status = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0xE7,
337 TWL4030_BCIMFKEY);
338 if (status < 0)
339 return status;
340 status = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
341 (reg & 0x100) ? 3 : 2,
342 TWL4030_BCIIREF2);
343 if (status < 0)
344 return status;
345 /* disable write protection for one write access for
346 * BCIIREF */
347 status = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0xE7,
348 TWL4030_BCIMFKEY);
349 if (status < 0)
350 return status;
351 status = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
352 reg & 0xff,
353 TWL4030_BCIIREF1);
354 }
355 if ((!!cgain) != !!(bcictl1 & TWL4030_CGAIN)) {
356 /* Flip CGAIN and re-enable charging */
357 bcictl1 ^= TWL4030_CGAIN;
358 twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
359 bcictl1, TWL4030_BCICTL1);
360 twl4030_clear_set_boot_bci(0, boot_bci);
361 }
362 return 0;
363}
364
NeilBrown3b542f02015-07-30 10:11:24 +1000365/*
NeilBrown9c645d22012-05-09 07:40:39 +1000366 * Enable/Disable USB Charge functionality.
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300367 */
368static int twl4030_charger_enable_usb(struct twl4030_bci *bci, bool enable)
369{
370 int ret;
371
NeilBrowne57c4a62015-07-30 10:11:24 +1000372 if (enable && !IS_ERR_OR_NULL(bci->transceiver)) {
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300373
NeilBrowne57c4a62015-07-30 10:11:24 +1000374 /* Need to keep phy powered */
NeilBrownab378132012-05-09 07:40:40 +1000375 if (!bci->usb_enabled) {
NeilBrowne57c4a62015-07-30 10:11:24 +1000376 pm_runtime_get_sync(bci->transceiver->dev);
NeilBrownab378132012-05-09 07:40:40 +1000377 bci->usb_enabled = 1;
378 }
379
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300380 /* forcing the field BCIAUTOUSB (BOOT_BCI[1]) to 1 */
381 ret = twl4030_clear_set_boot_bci(0, TWL4030_BCIAUTOUSB);
382 if (ret < 0)
383 return ret;
384
385 /* forcing USBFASTMCHG(BCIMFSTS4[2]) to 1 */
Peter Ujfalusie9f14c12012-11-13 10:42:11 +0100386 ret = twl4030_clear_set(TWL_MODULE_MAIN_CHARGE, 0,
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300387 TWL4030_USBFASTMCHG, TWL4030_BCIMFSTS4);
388 } else {
389 ret = twl4030_clear_set_boot_bci(TWL4030_BCIAUTOUSB, 0);
NeilBrownab378132012-05-09 07:40:40 +1000390 if (bci->usb_enabled) {
NeilBrowne57c4a62015-07-30 10:11:24 +1000391 pm_runtime_mark_last_busy(bci->transceiver->dev);
392 pm_runtime_put_autosuspend(bci->transceiver->dev);
NeilBrownab378132012-05-09 07:40:40 +1000393 bci->usb_enabled = 0;
394 }
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300395 }
396
397 return ret;
398}
399
400/*
401 * Enable/Disable AC Charge funtionality.
402 */
403static int twl4030_charger_enable_ac(bool enable)
404{
405 int ret;
406
407 if (enable)
408 ret = twl4030_clear_set_boot_bci(0, TWL4030_BCIAUTOAC);
409 else
410 ret = twl4030_clear_set_boot_bci(TWL4030_BCIAUTOAC, 0);
411
412 return ret;
413}
414
415/*
NeilBrown210d4bc2012-05-09 07:40:40 +1000416 * Enable/Disable charging of Backup Battery.
417 */
418static int twl4030_charger_enable_backup(int uvolt, int uamp)
419{
420 int ret;
421 u8 flags;
422
423 if (uvolt < 2500000 ||
424 uamp < 25) {
425 /* disable charging of backup battery */
Peter Ujfalusie9f14c12012-11-13 10:42:11 +0100426 ret = twl4030_clear_set(TWL_MODULE_PM_RECEIVER,
NeilBrown210d4bc2012-05-09 07:40:40 +1000427 TWL4030_BBCHEN, 0, TWL4030_BB_CFG);
428 return ret;
429 }
430
431 flags = TWL4030_BBCHEN;
432 if (uvolt >= 3200000)
433 flags |= TWL4030_BBSEL_3V2;
434 else if (uvolt >= 3100000)
435 flags |= TWL4030_BBSEL_3V1;
436 else if (uvolt >= 3000000)
437 flags |= TWL4030_BBSEL_3V0;
438 else
439 flags |= TWL4030_BBSEL_2V5;
440
441 if (uamp >= 1000)
442 flags |= TWL4030_BBISEL_1000uA;
443 else if (uamp >= 500)
444 flags |= TWL4030_BBISEL_500uA;
445 else if (uamp >= 150)
446 flags |= TWL4030_BBISEL_150uA;
447 else
448 flags |= TWL4030_BBISEL_25uA;
449
Peter Ujfalusie9f14c12012-11-13 10:42:11 +0100450 ret = twl4030_clear_set(TWL_MODULE_PM_RECEIVER,
NeilBrown210d4bc2012-05-09 07:40:40 +1000451 TWL4030_BBSEL_MASK | TWL4030_BBISEL_MASK,
452 flags,
453 TWL4030_BB_CFG);
454
455 return ret;
456}
457
458/*
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300459 * TWL4030 CHG_PRES (AC charger presence) events
460 */
461static irqreturn_t twl4030_charger_interrupt(int irq, void *arg)
462{
463 struct twl4030_bci *bci = arg;
464
465 dev_dbg(bci->dev, "CHG_PRES irq\n");
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100466 power_supply_changed(bci->ac);
467 power_supply_changed(bci->usb);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300468
469 return IRQ_HANDLED;
470}
471
472/*
473 * TWL4030 BCI monitoring events
474 */
475static irqreturn_t twl4030_bci_interrupt(int irq, void *arg)
476{
477 struct twl4030_bci *bci = arg;
478 u8 irqs1, irqs2;
479 int ret;
480
481 ret = twl_i2c_read_u8(TWL4030_MODULE_INTERRUPTS, &irqs1,
482 TWL4030_INTERRUPTS_BCIISR1A);
483 if (ret < 0)
484 return IRQ_HANDLED;
485
486 ret = twl_i2c_read_u8(TWL4030_MODULE_INTERRUPTS, &irqs2,
487 TWL4030_INTERRUPTS_BCIISR2A);
488 if (ret < 0)
489 return IRQ_HANDLED;
490
491 dev_dbg(bci->dev, "BCI irq %02x %02x\n", irqs2, irqs1);
492
493 if (irqs1 & (TWL4030_ICHGLOW | TWL4030_ICHGEOC)) {
494 /* charger state change, inform the core */
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100495 power_supply_changed(bci->ac);
496 power_supply_changed(bci->usb);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300497 }
498
499 /* various monitoring events, for now we just log them here */
500 if (irqs1 & (TWL4030_TBATOR2 | TWL4030_TBATOR1))
501 dev_warn(bci->dev, "battery temperature out of range\n");
502
503 if (irqs1 & TWL4030_BATSTS)
504 dev_crit(bci->dev, "battery disconnected\n");
505
506 if (irqs2 & TWL4030_VBATOV)
507 dev_crit(bci->dev, "VBAT overvoltage\n");
508
509 if (irqs2 & TWL4030_VBUSOV)
510 dev_crit(bci->dev, "VBUS overvoltage\n");
511
512 if (irqs2 & TWL4030_ACCHGOV)
513 dev_crit(bci->dev, "Ac charger overvoltage\n");
514
515 return IRQ_HANDLED;
516}
517
Heikki Krogerusd6ccc442011-02-25 13:56:36 +0200518static void twl4030_bci_usb_work(struct work_struct *data)
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300519{
Heikki Krogerusd6ccc442011-02-25 13:56:36 +0200520 struct twl4030_bci *bci = container_of(data, struct twl4030_bci, work);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300521
Heikki Krogerusd6ccc442011-02-25 13:56:36 +0200522 switch (bci->event) {
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300523 case USB_EVENT_VBUS:
524 case USB_EVENT_CHARGER:
525 twl4030_charger_enable_usb(bci, true);
526 break;
527 case USB_EVENT_NONE:
528 twl4030_charger_enable_usb(bci, false);
529 break;
530 }
Heikki Krogerusd6ccc442011-02-25 13:56:36 +0200531}
532
533static int twl4030_bci_usb_ncb(struct notifier_block *nb, unsigned long val,
534 void *priv)
535{
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200536 struct twl4030_bci *bci = container_of(nb, struct twl4030_bci, usb_nb);
Heikki Krogerusd6ccc442011-02-25 13:56:36 +0200537
538 dev_dbg(bci->dev, "OTG notify %lu\n", val);
539
540 bci->event = val;
541 schedule_work(&bci->work);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300542
543 return NOTIFY_OK;
544}
545
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300546static int twl4030_charger_get_current(void)
547{
548 int curr;
549 int ret;
550 u8 bcictl1;
551
552 curr = twl4030bci_read_adc_val(TWL4030_BCIICHG);
553 if (curr < 0)
554 return curr;
555
556 ret = twl4030_bci_read(TWL4030_BCICTL1, &bcictl1);
557 if (ret)
558 return ret;
559
NeilBrown3b542f02015-07-30 10:11:24 +1000560 return regval2ua(curr, bcictl1 & TWL4030_CGAIN);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300561}
562
563/*
564 * Returns the main charge FSM state
565 * Or < 0 on failure.
566 */
567static int twl4030bci_state(struct twl4030_bci *bci)
568{
569 int ret;
570 u8 state;
571
572 ret = twl4030_bci_read(TWL4030_BCIMSTATEC, &state);
573 if (ret) {
574 pr_err("twl4030_bci: error reading BCIMSTATEC\n");
575 return ret;
576 }
577
578 dev_dbg(bci->dev, "state: %02x\n", state);
579
580 return state;
581}
582
583static int twl4030_bci_state_to_status(int state)
584{
585 state &= TWL4030_MSTATEC_MASK;
586 if (TWL4030_MSTATEC_QUICK1 <= state && state <= TWL4030_MSTATEC_QUICK7)
587 return POWER_SUPPLY_STATUS_CHARGING;
588 else if (TWL4030_MSTATEC_COMPLETE1 <= state &&
589 state <= TWL4030_MSTATEC_COMPLETE4)
590 return POWER_SUPPLY_STATUS_FULL;
591 else
592 return POWER_SUPPLY_STATUS_NOT_CHARGING;
593}
594
595static int twl4030_bci_get_property(struct power_supply *psy,
596 enum power_supply_property psp,
597 union power_supply_propval *val)
598{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100599 struct twl4030_bci *bci = dev_get_drvdata(psy->dev.parent);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300600 int is_charging;
601 int state;
602 int ret;
603
604 state = twl4030bci_state(bci);
605 if (state < 0)
606 return state;
607
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100608 if (psy->desc->type == POWER_SUPPLY_TYPE_USB)
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300609 is_charging = state & TWL4030_MSTATEC_USB;
610 else
611 is_charging = state & TWL4030_MSTATEC_AC;
612
613 switch (psp) {
614 case POWER_SUPPLY_PROP_STATUS:
615 if (is_charging)
616 val->intval = twl4030_bci_state_to_status(state);
617 else
618 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
619 break;
620 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
621 /* charging must be active for meaningful result */
622 if (!is_charging)
623 return -ENODATA;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100624 if (psy->desc->type == POWER_SUPPLY_TYPE_USB) {
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300625 ret = twl4030bci_read_adc_val(TWL4030_BCIVBUS);
626 if (ret < 0)
627 return ret;
628 /* BCIVBUS uses ADCIN8, 7/1023 V/step */
629 val->intval = ret * 6843;
630 } else {
631 ret = twl4030bci_read_adc_val(TWL4030_BCIVAC);
632 if (ret < 0)
633 return ret;
634 /* BCIVAC uses ADCIN11, 10/1023 V/step */
635 val->intval = ret * 9775;
636 }
637 break;
638 case POWER_SUPPLY_PROP_CURRENT_NOW:
639 if (!is_charging)
640 return -ENODATA;
641 /* current measurement is shared between AC and USB */
642 ret = twl4030_charger_get_current();
643 if (ret < 0)
644 return ret;
645 val->intval = ret;
646 break;
647 case POWER_SUPPLY_PROP_ONLINE:
648 val->intval = is_charging &&
649 twl4030_bci_state_to_status(state) !=
650 POWER_SUPPLY_STATUS_NOT_CHARGING;
651 break;
652 default:
653 return -EINVAL;
654 }
655
656 return 0;
657}
658
659static enum power_supply_property twl4030_charger_props[] = {
660 POWER_SUPPLY_PROP_STATUS,
661 POWER_SUPPLY_PROP_ONLINE,
662 POWER_SUPPLY_PROP_VOLTAGE_NOW,
663 POWER_SUPPLY_PROP_CURRENT_NOW,
664};
665
NeilBrownec0b3802013-10-31 17:05:50 +1100666#ifdef CONFIG_OF
667static const struct twl4030_bci_platform_data *
668twl4030_bci_parse_dt(struct device *dev)
669{
670 struct device_node *np = dev->of_node;
671 struct twl4030_bci_platform_data *pdata;
672 u32 num;
673
674 if (!np)
675 return NULL;
676 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
677 if (!pdata)
678 return pdata;
679
680 if (of_property_read_u32(np, "ti,bb-uvolt", &num) == 0)
681 pdata->bb_uvolt = num;
682 if (of_property_read_u32(np, "ti,bb-uamp", &num) == 0)
683 pdata->bb_uamp = num;
684 return pdata;
685}
686#else
687static inline const struct twl4030_bci_platform_data *
688twl4030_bci_parse_dt(struct device *dev)
689{
690 return NULL;
691}
692#endif
693
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100694static const struct power_supply_desc twl4030_bci_ac_desc = {
695 .name = "twl4030_ac",
696 .type = POWER_SUPPLY_TYPE_MAINS,
697 .properties = twl4030_charger_props,
698 .num_properties = ARRAY_SIZE(twl4030_charger_props),
699 .get_property = twl4030_bci_get_property,
700};
701
702static const struct power_supply_desc twl4030_bci_usb_desc = {
703 .name = "twl4030_usb",
704 .type = POWER_SUPPLY_TYPE_USB,
705 .properties = twl4030_charger_props,
706 .num_properties = ARRAY_SIZE(twl4030_charger_props),
707 .get_property = twl4030_bci_get_property,
708};
709
Pavel Machek7396f702015-07-30 10:11:24 +1000710static int twl4030_bci_probe(struct platform_device *pdev)
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300711{
712 struct twl4030_bci *bci;
NeilBrownec0b3802013-10-31 17:05:50 +1100713 const struct twl4030_bci_platform_data *pdata = pdev->dev.platform_data;
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300714 int ret;
Grazvydas Ignotas28d48f02011-06-03 14:43:11 +0300715 u32 reg;
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300716
NeilBrown325b50a2015-03-23 10:20:28 +1100717 bci = devm_kzalloc(&pdev->dev, sizeof(*bci), GFP_KERNEL);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300718 if (bci == NULL)
719 return -ENOMEM;
720
NeilBrownec0b3802013-10-31 17:05:50 +1100721 if (!pdata)
722 pdata = twl4030_bci_parse_dt(&pdev->dev);
723
NeilBrown1098cb52015-07-30 10:11:24 +1000724 bci->ichg_eoc = 80100; /* Stop charging when current drops to here */
725 bci->ichg_lo = 241000; /* Low threshold */
726 bci->ichg_hi = 500000; /* High threshold */
727 if (allow_usb)
728 bci->cur = 500000; /* 500mA */
729 else
730 bci->cur = 100000; /* 100mA */
731
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300732 bci->dev = &pdev->dev;
733 bci->irq_chg = platform_get_irq(pdev, 0);
734 bci->irq_bci = platform_get_irq(pdev, 1);
735
Nishanth Menon61a77842014-05-28 16:46:49 -0500736 /* Only proceed further *IF* battery is physically present */
737 ret = twl4030_is_battery_present(bci);
738 if (ret) {
739 dev_crit(&pdev->dev, "Battery was not detected:%d\n", ret);
NeilBrown325b50a2015-03-23 10:20:28 +1100740 return ret;
Nishanth Menon61a77842014-05-28 16:46:49 -0500741 }
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300742
Nishanth Menon61a77842014-05-28 16:46:49 -0500743 platform_set_drvdata(pdev, bci);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300744
NeilBrown325b50a2015-03-23 10:20:28 +1100745 bci->ac = devm_power_supply_register(&pdev->dev, &twl4030_bci_ac_desc,
746 NULL);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100747 if (IS_ERR(bci->ac)) {
748 ret = PTR_ERR(bci->ac);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300749 dev_err(&pdev->dev, "failed to register ac: %d\n", ret);
NeilBrown325b50a2015-03-23 10:20:28 +1100750 return ret;
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300751 }
752
NeilBrown325b50a2015-03-23 10:20:28 +1100753 bci->usb = devm_power_supply_register(&pdev->dev, &twl4030_bci_usb_desc,
754 NULL);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100755 if (IS_ERR(bci->usb)) {
756 ret = PTR_ERR(bci->usb);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300757 dev_err(&pdev->dev, "failed to register usb: %d\n", ret);
NeilBrown325b50a2015-03-23 10:20:28 +1100758 return ret;
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300759 }
760
NeilBrown1b7cac22015-03-23 10:20:28 +1100761 ret = devm_request_threaded_irq(&pdev->dev, bci->irq_chg, NULL,
Fengguang Wua2778b02012-08-23 19:56:43 +0800762 twl4030_charger_interrupt, IRQF_ONESHOT, pdev->name,
763 bci);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300764 if (ret < 0) {
765 dev_err(&pdev->dev, "could not request irq %d, status %d\n",
766 bci->irq_chg, ret);
NeilBrown325b50a2015-03-23 10:20:28 +1100767 return ret;
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300768 }
769
NeilBrown1b7cac22015-03-23 10:20:28 +1100770 ret = devm_request_threaded_irq(&pdev->dev, bci->irq_bci, NULL,
Fengguang Wua2778b02012-08-23 19:56:43 +0800771 twl4030_bci_interrupt, IRQF_ONESHOT, pdev->name, bci);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300772 if (ret < 0) {
773 dev_err(&pdev->dev, "could not request irq %d, status %d\n",
774 bci->irq_bci, ret);
NeilBrown325b50a2015-03-23 10:20:28 +1100775 return ret;
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300776 }
777
Heikki Krogerusd6ccc442011-02-25 13:56:36 +0200778 INIT_WORK(&bci->work, twl4030_bci_usb_work);
779
NeilBrownf5e4edb2015-03-23 09:52:48 +1100780 bci->usb_nb.notifier_call = twl4030_bci_usb_ncb;
781 if (bci->dev->of_node) {
782 struct device_node *phynode;
783
784 phynode = of_find_compatible_node(bci->dev->of_node->parent,
785 NULL, "ti,twl4030-usb");
NeilBrown3fc38952015-07-30 10:11:24 +1000786 if (phynode) {
NeilBrownf5e4edb2015-03-23 09:52:48 +1100787 bci->transceiver = devm_usb_get_phy_by_node(
788 bci->dev, phynode, &bci->usb_nb);
NeilBrown3fc38952015-07-30 10:11:24 +1000789 if (IS_ERR(bci->transceiver) &&
790 PTR_ERR(bci->transceiver) == -EPROBE_DEFER)
791 return -EPROBE_DEFER;
792 }
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300793 }
794
795 /* Enable interrupts now. */
Grazvydas Ignotas28d48f02011-06-03 14:43:11 +0300796 reg = ~(u32)(TWL4030_ICHGLOW | TWL4030_ICHGEOC | TWL4030_TBATOR2 |
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300797 TWL4030_TBATOR1 | TWL4030_BATSTS);
798 ret = twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS, reg,
799 TWL4030_INTERRUPTS_BCIIMR1A);
800 if (ret < 0) {
801 dev_err(&pdev->dev, "failed to unmask interrupts: %d\n", ret);
NeilBrown325b50a2015-03-23 10:20:28 +1100802 return ret;
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300803 }
804
Grazvydas Ignotas28d48f02011-06-03 14:43:11 +0300805 reg = ~(u32)(TWL4030_VBATOV | TWL4030_VBUSOV | TWL4030_ACCHGOV);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300806 ret = twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS, reg,
807 TWL4030_INTERRUPTS_BCIIMR2A);
808 if (ret < 0)
809 dev_warn(&pdev->dev, "failed to unmask interrupts: %d\n", ret);
810
NeilBrown1098cb52015-07-30 10:11:24 +1000811 twl4030_charger_update_current(bci);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300812 twl4030_charger_enable_ac(true);
NeilBrown6e37ec82015-07-30 10:11:24 +1000813 if (!IS_ERR_OR_NULL(bci->transceiver))
814 twl4030_bci_usb_ncb(&bci->usb_nb,
815 bci->transceiver->last_event,
816 NULL);
817 else
818 twl4030_charger_enable_usb(bci, false);
NeilBrownec0b3802013-10-31 17:05:50 +1100819 if (pdata)
820 twl4030_charger_enable_backup(pdata->bb_uvolt,
821 pdata->bb_uamp);
822 else
823 twl4030_charger_enable_backup(0, 0);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300824
825 return 0;
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300826}
827
828static int __exit twl4030_bci_remove(struct platform_device *pdev)
829{
830 struct twl4030_bci *bci = platform_get_drvdata(pdev);
831
832 twl4030_charger_enable_ac(false);
833 twl4030_charger_enable_usb(bci, false);
NeilBrown210d4bc2012-05-09 07:40:40 +1000834 twl4030_charger_enable_backup(0, 0);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300835
836 /* mask interrupts */
837 twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS, 0xff,
838 TWL4030_INTERRUPTS_BCIIMR1A);
839 twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS, 0xff,
840 TWL4030_INTERRUPTS_BCIIMR2A);
841
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300842 return 0;
843}
844
NeilBrownec0b3802013-10-31 17:05:50 +1100845static const struct of_device_id twl_bci_of_match[] = {
846 {.compatible = "ti,twl4030-bci", },
847 { }
848};
849MODULE_DEVICE_TABLE(of, twl_bci_of_match);
850
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300851static struct platform_driver twl4030_bci_driver = {
Pavel Machek7396f702015-07-30 10:11:24 +1000852 .probe = twl4030_bci_probe,
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300853 .driver = {
854 .name = "twl4030_bci",
NeilBrownec0b3802013-10-31 17:05:50 +1100855 .of_match_table = of_match_ptr(twl_bci_of_match),
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300856 },
857 .remove = __exit_p(twl4030_bci_remove),
858};
Pavel Machek7396f702015-07-30 10:11:24 +1000859module_platform_driver(twl4030_bci_driver);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300860
Grazvydas Ignotas28d48f02011-06-03 14:43:11 +0300861MODULE_AUTHOR("Gražvydas Ignotas");
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300862MODULE_DESCRIPTION("TWL4030 Battery Charger Interface driver");
863MODULE_LICENSE("GPL");
864MODULE_ALIAS("platform:twl4030_bci");