blob: bcd4dc304f270c50739e2b2ddfc2850e1efe3bf0 [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>
Marek Belisko2202e1f2015-09-25 22:20:45 +020025#include <linux/iio/consumer.h>
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +030026
NeilBrown7f4a6332015-07-30 10:11:24 +100027#define TWL4030_BCIMDEN 0x00
28#define TWL4030_BCIMDKEY 0x01
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +030029#define TWL4030_BCIMSTATEC 0x02
30#define TWL4030_BCIICHG 0x08
31#define TWL4030_BCIVAC 0x0a
32#define TWL4030_BCIVBUS 0x0c
Nishanth Menon61a77842014-05-28 16:46:49 -050033#define TWL4030_BCIMFSTS3 0x0F
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +030034#define TWL4030_BCIMFSTS4 0x10
35#define TWL4030_BCICTL1 0x23
NeilBrown210d4bc2012-05-09 07:40:40 +100036#define TWL4030_BB_CFG 0x12
NeilBrown1098cb52015-07-30 10:11:24 +100037#define TWL4030_BCIIREF1 0x27
38#define TWL4030_BCIIREF2 0x28
39#define TWL4030_BCIMFKEY 0x11
NeilBrown7f4a6332015-07-30 10:11:24 +100040#define TWL4030_BCIMFEN3 0x14
NeilBrown1098cb52015-07-30 10:11:24 +100041#define TWL4030_BCIMFTH8 0x1d
42#define TWL4030_BCIMFTH9 0x1e
NeilBrown7f4a6332015-07-30 10:11:24 +100043#define TWL4030_BCIWDKEY 0x21
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +030044
Nishanth Menon61a77842014-05-28 16:46:49 -050045#define TWL4030_BCIMFSTS1 0x01
46
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +030047#define TWL4030_BCIAUTOWEN BIT(5)
48#define TWL4030_CONFIG_DONE BIT(4)
NeilBrown7f4a6332015-07-30 10:11:24 +100049#define TWL4030_CVENAC BIT(2)
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +030050#define TWL4030_BCIAUTOUSB BIT(1)
51#define TWL4030_BCIAUTOAC BIT(0)
52#define TWL4030_CGAIN BIT(5)
53#define TWL4030_USBFASTMCHG BIT(2)
54#define TWL4030_STS_VBUS BIT(7)
55#define TWL4030_STS_USB_ID BIT(2)
NeilBrown210d4bc2012-05-09 07:40:40 +100056#define TWL4030_BBCHEN BIT(4)
Il Han94b05ca2012-08-17 16:49:43 +090057#define TWL4030_BBSEL_MASK 0x0c
58#define TWL4030_BBSEL_2V5 0x00
59#define TWL4030_BBSEL_3V0 0x04
60#define TWL4030_BBSEL_3V1 0x08
61#define TWL4030_BBSEL_3V2 0x0c
62#define TWL4030_BBISEL_MASK 0x03
63#define TWL4030_BBISEL_25uA 0x00
64#define TWL4030_BBISEL_150uA 0x01
65#define TWL4030_BBISEL_500uA 0x02
66#define TWL4030_BBISEL_1000uA 0x03
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +030067
Nishanth Menon61a77842014-05-28 16:46:49 -050068#define TWL4030_BATSTSPCHG BIT(2)
69#define TWL4030_BATSTSMCHG BIT(6)
70
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +030071/* BCI interrupts */
72#define TWL4030_WOVF BIT(0) /* Watchdog overflow */
73#define TWL4030_TMOVF BIT(1) /* Timer overflow */
74#define TWL4030_ICHGHIGH BIT(2) /* Battery charge current high */
75#define TWL4030_ICHGLOW BIT(3) /* Battery cc. low / FSM state change */
76#define TWL4030_ICHGEOC BIT(4) /* Battery current end-of-charge */
77#define TWL4030_TBATOR2 BIT(5) /* Battery temperature out of range 2 */
78#define TWL4030_TBATOR1 BIT(6) /* Battery temperature out of range 1 */
79#define TWL4030_BATSTS BIT(7) /* Battery status */
80
81#define TWL4030_VBATLVL BIT(0) /* VBAT level */
82#define TWL4030_VBATOV BIT(1) /* VBAT overvoltage */
83#define TWL4030_VBUSOV BIT(2) /* VBUS overvoltage */
84#define TWL4030_ACCHGOV BIT(3) /* Ac charger overvoltage */
85
86#define TWL4030_MSTATEC_USB BIT(4)
87#define TWL4030_MSTATEC_AC BIT(5)
88#define TWL4030_MSTATEC_MASK 0x0f
89#define TWL4030_MSTATEC_QUICK1 0x02
90#define TWL4030_MSTATEC_QUICK7 0x07
91#define TWL4030_MSTATEC_COMPLETE1 0x0b
92#define TWL4030_MSTATEC_COMPLETE4 0x0e
93
NeilBrownb49d15d2015-08-07 13:45:25 +100094/*
95 * If AC (Accessory Charger) voltage exceeds 4.5V (MADC 11)
96 * then AC is available.
97 */
Marek Belisko2202e1f2015-09-25 22:20:45 +020098static inline int ac_available(struct iio_channel *channel_vac)
NeilBrownb49d15d2015-08-07 13:45:25 +100099{
Marek Belisko2202e1f2015-09-25 22:20:45 +0200100 int val, err;
101
102 if (!channel_vac)
103 return 0;
104
105 err = iio_read_channel_processed(channel_vac, &val);
106 if (err < 0)
107 return 0;
108 return val > 4500;
NeilBrownb49d15d2015-08-07 13:45:25 +1000109}
Marek Belisko2202e1f2015-09-25 22:20:45 +0200110
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300111static bool allow_usb;
Grazvydas Ignotas28d48f02011-06-03 14:43:11 +0300112module_param(allow_usb, bool, 0644);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300113MODULE_PARM_DESC(allow_usb, "Allow USB charge drawing default current");
114
115struct twl4030_bci {
116 struct device *dev;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100117 struct power_supply *ac;
118 struct power_supply *usb;
Heikki Krogerus86753812012-02-13 13:24:02 +0200119 struct usb_phy *transceiver;
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200120 struct notifier_block usb_nb;
Heikki Krogerusd6ccc442011-02-25 13:56:36 +0200121 struct work_struct work;
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300122 int irq_chg;
123 int irq_bci;
NeilBrownab378132012-05-09 07:40:40 +1000124 int usb_enabled;
Heikki Krogerusd6ccc442011-02-25 13:56:36 +0200125
NeilBrown1098cb52015-07-30 10:11:24 +1000126 /*
NeilBrowne4ae5372015-07-30 10:11:24 +1000127 * ichg_* and *_cur values in uA. If any are 'large', we set
128 * CGAIN to '1' which doubles the range for half the
129 * precision.
NeilBrown1098cb52015-07-30 10:11:24 +1000130 */
NeilBrowne4ae5372015-07-30 10:11:24 +1000131 unsigned int ichg_eoc, ichg_lo, ichg_hi;
132 unsigned int usb_cur, ac_cur;
Marek Belisko2202e1f2015-09-25 22:20:45 +0200133 struct iio_channel *channel_vac;
NeilBrowne4ae5372015-07-30 10:11:24 +1000134 bool ac_is_active;
NeilBrownb04b9082015-07-30 10:11:24 +1000135 int usb_mode, ac_mode; /* charging mode requested */
NeilBrown22d4c332015-07-30 10:11:24 +1000136#define CHARGE_OFF 0
137#define CHARGE_AUTO 1
NeilBrown7f4a6332015-07-30 10:11:24 +1000138#define CHARGE_LINEAR 2
NeilBrown1098cb52015-07-30 10:11:24 +1000139
NeilBrown21ae4042015-07-30 10:11:24 +1000140 /* When setting the USB current we slowly increase the
141 * requested current until target is reached or the voltage
142 * drops below 4.75V. In the latter case we step back one
143 * step.
144 */
145 unsigned int usb_cur_target;
146 struct delayed_work current_worker;
147#define USB_CUR_STEP 20000 /* 20mA at a time */
148#define USB_MIN_VOLT 4750000 /* 4.75V */
149#define USB_CUR_DELAY msecs_to_jiffies(100)
150#define USB_MAX_CURRENT 1700000 /* TWL4030 caps at 1.7A */
151
Heikki Krogerusd6ccc442011-02-25 13:56:36 +0200152 unsigned long event;
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300153};
154
NeilBrown22d4c332015-07-30 10:11:24 +1000155/* strings for 'usb_mode' values */
NeilBrown7f4a6332015-07-30 10:11:24 +1000156static char *modes[] = { "off", "auto", "continuous" };
NeilBrown22d4c332015-07-30 10:11:24 +1000157
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300158/*
159 * clear and set bits on an given register on a given module
160 */
161static int twl4030_clear_set(u8 mod_no, u8 clear, u8 set, u8 reg)
162{
163 u8 val = 0;
164 int ret;
165
166 ret = twl_i2c_read_u8(mod_no, &val, reg);
167 if (ret)
168 return ret;
169
170 val &= ~clear;
171 val |= set;
172
173 return twl_i2c_write_u8(mod_no, val, reg);
174}
175
176static int twl4030_bci_read(u8 reg, u8 *val)
177{
Peter Ujfalusie9f14c12012-11-13 10:42:11 +0100178 return twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, val, reg);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300179}
180
181static int twl4030_clear_set_boot_bci(u8 clear, u8 set)
182{
Peter Ujfalusie9f14c12012-11-13 10:42:11 +0100183 return twl4030_clear_set(TWL_MODULE_PM_MASTER, clear,
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300184 TWL4030_CONFIG_DONE | TWL4030_BCIAUTOWEN | set,
185 TWL4030_PM_MASTER_BOOT_BCI);
186}
187
188static int twl4030bci_read_adc_val(u8 reg)
189{
190 int ret, temp;
191 u8 val;
192
193 /* read MSB */
194 ret = twl4030_bci_read(reg + 1, &val);
195 if (ret)
196 return ret;
197
198 temp = (int)(val & 0x03) << 8;
199
200 /* read LSB */
201 ret = twl4030_bci_read(reg, &val);
202 if (ret)
203 return ret;
204
205 return temp | val;
206}
207
208/*
Nishanth Menon61a77842014-05-28 16:46:49 -0500209 * Check if Battery Pack was present
210 */
211static int twl4030_is_battery_present(struct twl4030_bci *bci)
212{
213 int ret;
214 u8 val = 0;
215
216 /* Battery presence in Main charge? */
217 ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, &val, TWL4030_BCIMFSTS3);
218 if (ret)
219 return ret;
220 if (val & TWL4030_BATSTSMCHG)
221 return 0;
222
223 /*
224 * OK, It could be that bootloader did not enable main charger,
225 * pre-charge is h/w auto. So, Battery presence in Pre-charge?
226 */
227 ret = twl_i2c_read_u8(TWL4030_MODULE_PRECHARGE, &val,
228 TWL4030_BCIMFSTS1);
229 if (ret)
230 return ret;
231 if (val & TWL4030_BATSTSPCHG)
232 return 0;
233
234 return -ENODEV;
235}
236
237/*
NeilBrown3b542f02015-07-30 10:11:24 +1000238 * TI provided formulas:
239 * CGAIN == 0: ICHG = (BCIICHG * 1.7) / (2^10 - 1) - 0.85
240 * CGAIN == 1: ICHG = (BCIICHG * 3.4) / (2^10 - 1) - 1.7
241 * Here we use integer approximation of:
242 * CGAIN == 0: val * 1.6618 - 0.85 * 1000
243 * CGAIN == 1: (val * 1.6618 - 0.85 * 1000) * 2
244 */
245/*
246 * convert twl register value for currents into uA
247 */
248static int regval2ua(int regval, bool cgain)
249{
250 if (cgain)
251 return (regval * 16618 - 8500 * 1000) / 5;
252 else
253 return (regval * 16618 - 8500 * 1000) / 10;
254}
255
256/*
257 * convert uA currents into twl register value
258 */
259static int ua2regval(int ua, bool cgain)
260{
261 int ret;
262 if (cgain)
263 ua /= 2;
264 ret = (ua * 10 + 8500 * 1000) / 16618;
265 /* rounding problems */
266 if (ret < 512)
267 ret = 512;
268 return ret;
269}
270
NeilBrown1098cb52015-07-30 10:11:24 +1000271static int twl4030_charger_update_current(struct twl4030_bci *bci)
272{
273 int status;
NeilBrowne4ae5372015-07-30 10:11:24 +1000274 int cur;
NeilBrown1098cb52015-07-30 10:11:24 +1000275 unsigned reg, cur_reg;
276 u8 bcictl1, oldreg, fullreg;
277 bool cgain = false;
278 u8 boot_bci;
279
NeilBrowne4ae5372015-07-30 10:11:24 +1000280 /*
281 * If AC (Accessory Charger) voltage exceeds 4.5V (MADC 11)
282 * and AC is enabled, set current for 'ac'
283 */
Marek Belisko2202e1f2015-09-25 22:20:45 +0200284 if (ac_available(bci->channel_vac)) {
NeilBrowne4ae5372015-07-30 10:11:24 +1000285 cur = bci->ac_cur;
286 bci->ac_is_active = true;
287 } else {
288 cur = bci->usb_cur;
289 bci->ac_is_active = false;
NeilBrown21ae4042015-07-30 10:11:24 +1000290 if (cur > bci->usb_cur_target) {
291 cur = bci->usb_cur_target;
292 bci->usb_cur = cur;
293 }
294 if (cur < bci->usb_cur_target)
295 schedule_delayed_work(&bci->current_worker, USB_CUR_DELAY);
NeilBrowne4ae5372015-07-30 10:11:24 +1000296 }
297
NeilBrown1098cb52015-07-30 10:11:24 +1000298 /* First, check thresholds and see if cgain is needed */
299 if (bci->ichg_eoc >= 200000)
300 cgain = true;
301 if (bci->ichg_lo >= 400000)
302 cgain = true;
303 if (bci->ichg_hi >= 820000)
304 cgain = true;
NeilBrowne4ae5372015-07-30 10:11:24 +1000305 if (cur > 852000)
NeilBrown1098cb52015-07-30 10:11:24 +1000306 cgain = true;
307
308 status = twl4030_bci_read(TWL4030_BCICTL1, &bcictl1);
309 if (status < 0)
310 return status;
311 if (twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &boot_bci,
312 TWL4030_PM_MASTER_BOOT_BCI) < 0)
313 boot_bci = 0;
314 boot_bci &= 7;
315
316 if ((!!cgain) != !!(bcictl1 & TWL4030_CGAIN))
317 /* Need to turn for charging while we change the
318 * CGAIN bit. Leave it off while everything is
319 * updated.
320 */
321 twl4030_clear_set_boot_bci(boot_bci, 0);
322
323 /*
324 * For ichg_eoc, the hardware only supports reg values matching
325 * 100XXXX000, and requires the XXXX be stored in the high nibble
326 * of TWL4030_BCIMFTH8.
327 */
328 reg = ua2regval(bci->ichg_eoc, cgain);
329 if (reg > 0x278)
330 reg = 0x278;
331 if (reg < 0x200)
332 reg = 0x200;
333 reg = (reg >> 3) & 0xf;
334 fullreg = reg << 4;
335
336 /*
337 * For ichg_lo, reg value must match 10XXXX0000.
338 * XXXX is stored in low nibble of TWL4030_BCIMFTH8.
339 */
340 reg = ua2regval(bci->ichg_lo, cgain);
341 if (reg > 0x2F0)
342 reg = 0x2F0;
343 if (reg < 0x200)
344 reg = 0x200;
345 reg = (reg >> 4) & 0xf;
346 fullreg |= reg;
347
348 /* ichg_eoc and ichg_lo live in same register */
349 status = twl4030_bci_read(TWL4030_BCIMFTH8, &oldreg);
350 if (status < 0)
351 return status;
352 if (oldreg != fullreg) {
353 status = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0xF4,
354 TWL4030_BCIMFKEY);
355 if (status < 0)
356 return status;
357 twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
358 fullreg, TWL4030_BCIMFTH8);
359 }
360
361 /* ichg_hi threshold must be 1XXXX01100 (I think) */
362 reg = ua2regval(bci->ichg_hi, cgain);
363 if (reg > 0x3E0)
364 reg = 0x3E0;
365 if (reg < 0x200)
366 reg = 0x200;
367 fullreg = (reg >> 5) & 0xF;
368 fullreg <<= 4;
369 status = twl4030_bci_read(TWL4030_BCIMFTH9, &oldreg);
370 if (status < 0)
371 return status;
372 if ((oldreg & 0xF0) != fullreg) {
373 fullreg |= (oldreg & 0x0F);
374 status = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0xE7,
375 TWL4030_BCIMFKEY);
376 if (status < 0)
377 return status;
378 twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
379 fullreg, TWL4030_BCIMFTH9);
380 }
381
382 /*
383 * And finally, set the current. This is stored in
384 * two registers.
385 */
NeilBrowne4ae5372015-07-30 10:11:24 +1000386 reg = ua2regval(cur, cgain);
NeilBrown1098cb52015-07-30 10:11:24 +1000387 /* we have only 10 bits */
388 if (reg > 0x3ff)
389 reg = 0x3ff;
390 status = twl4030_bci_read(TWL4030_BCIIREF1, &oldreg);
391 if (status < 0)
392 return status;
393 cur_reg = oldreg;
394 status = twl4030_bci_read(TWL4030_BCIIREF2, &oldreg);
395 if (status < 0)
396 return status;
397 cur_reg |= oldreg << 8;
398 if (reg != oldreg) {
399 /* disable write protection for one write access for
400 * BCIIREF */
401 status = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0xE7,
402 TWL4030_BCIMFKEY);
403 if (status < 0)
404 return status;
405 status = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
406 (reg & 0x100) ? 3 : 2,
407 TWL4030_BCIIREF2);
408 if (status < 0)
409 return status;
410 /* disable write protection for one write access for
411 * BCIIREF */
412 status = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0xE7,
413 TWL4030_BCIMFKEY);
414 if (status < 0)
415 return status;
416 status = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
417 reg & 0xff,
418 TWL4030_BCIIREF1);
419 }
420 if ((!!cgain) != !!(bcictl1 & TWL4030_CGAIN)) {
421 /* Flip CGAIN and re-enable charging */
422 bcictl1 ^= TWL4030_CGAIN;
423 twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
424 bcictl1, TWL4030_BCICTL1);
425 twl4030_clear_set_boot_bci(0, boot_bci);
426 }
427 return 0;
428}
429
NeilBrown21ae4042015-07-30 10:11:24 +1000430static int twl4030_charger_get_current(void);
431
432static void twl4030_current_worker(struct work_struct *data)
433{
434 int v, curr;
435 int res;
436 struct twl4030_bci *bci = container_of(data, struct twl4030_bci,
437 current_worker.work);
438
439 res = twl4030bci_read_adc_val(TWL4030_BCIVBUS);
440 if (res < 0)
441 v = 0;
442 else
443 /* BCIVBUS uses ADCIN8, 7/1023 V/step */
444 v = res * 6843;
445 curr = twl4030_charger_get_current();
446
447 dev_dbg(bci->dev, "v=%d cur=%d limit=%d target=%d\n", v, curr,
448 bci->usb_cur, bci->usb_cur_target);
449
450 if (v < USB_MIN_VOLT) {
451 /* Back up and stop adjusting. */
452 bci->usb_cur -= USB_CUR_STEP;
453 bci->usb_cur_target = bci->usb_cur;
454 } else if (bci->usb_cur >= bci->usb_cur_target ||
455 bci->usb_cur + USB_CUR_STEP > USB_MAX_CURRENT) {
456 /* Reached target and voltage is OK - stop */
457 return;
458 } else {
459 bci->usb_cur += USB_CUR_STEP;
460 schedule_delayed_work(&bci->current_worker, USB_CUR_DELAY);
461 }
462 twl4030_charger_update_current(bci);
463}
464
NeilBrown3b542f02015-07-30 10:11:24 +1000465/*
NeilBrown9c645d22012-05-09 07:40:39 +1000466 * Enable/Disable USB Charge functionality.
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300467 */
468static int twl4030_charger_enable_usb(struct twl4030_bci *bci, bool enable)
469{
470 int ret;
471
NeilBrown22d4c332015-07-30 10:11:24 +1000472 if (bci->usb_mode == CHARGE_OFF)
473 enable = false;
NeilBrowne57c4a62015-07-30 10:11:24 +1000474 if (enable && !IS_ERR_OR_NULL(bci->transceiver)) {
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300475
NeilBrowne4ae5372015-07-30 10:11:24 +1000476 twl4030_charger_update_current(bci);
477
NeilBrowne57c4a62015-07-30 10:11:24 +1000478 /* Need to keep phy powered */
NeilBrownab378132012-05-09 07:40:40 +1000479 if (!bci->usb_enabled) {
NeilBrowne57c4a62015-07-30 10:11:24 +1000480 pm_runtime_get_sync(bci->transceiver->dev);
NeilBrownab378132012-05-09 07:40:40 +1000481 bci->usb_enabled = 1;
482 }
483
NeilBrown7f4a6332015-07-30 10:11:24 +1000484 if (bci->usb_mode == CHARGE_AUTO)
485 /* forcing the field BCIAUTOUSB (BOOT_BCI[1]) to 1 */
486 ret = twl4030_clear_set_boot_bci(0, TWL4030_BCIAUTOUSB);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300487
488 /* forcing USBFASTMCHG(BCIMFSTS4[2]) to 1 */
Peter Ujfalusie9f14c12012-11-13 10:42:11 +0100489 ret = twl4030_clear_set(TWL_MODULE_MAIN_CHARGE, 0,
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300490 TWL4030_USBFASTMCHG, TWL4030_BCIMFSTS4);
NeilBrown7f4a6332015-07-30 10:11:24 +1000491 if (bci->usb_mode == CHARGE_LINEAR) {
492 twl4030_clear_set_boot_bci(TWL4030_BCIAUTOAC|TWL4030_CVENAC, 0);
493 /* Watch dog key: WOVF acknowledge */
494 ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0x33,
495 TWL4030_BCIWDKEY);
496 /* 0x24 + EKEY6: off mode */
497 ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0x2a,
498 TWL4030_BCIMDKEY);
499 /* EKEY2: Linear charge: USB path */
500 ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0x26,
501 TWL4030_BCIMDKEY);
502 /* WDKEY5: stop watchdog count */
503 ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0xf3,
504 TWL4030_BCIWDKEY);
505 /* enable MFEN3 access */
506 ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0x9c,
507 TWL4030_BCIMFKEY);
508 /* ICHGEOCEN - end-of-charge monitor (current < 80mA)
509 * (charging continues)
510 * ICHGLOWEN - current level monitor (charge continues)
511 * don't monitor over-current or heat save
512 */
513 ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0xf0,
514 TWL4030_BCIMFEN3);
515 }
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300516 } else {
517 ret = twl4030_clear_set_boot_bci(TWL4030_BCIAUTOUSB, 0);
NeilBrown7f4a6332015-07-30 10:11:24 +1000518 ret |= twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0x2a,
519 TWL4030_BCIMDKEY);
NeilBrownab378132012-05-09 07:40:40 +1000520 if (bci->usb_enabled) {
NeilBrowne57c4a62015-07-30 10:11:24 +1000521 pm_runtime_mark_last_busy(bci->transceiver->dev);
522 pm_runtime_put_autosuspend(bci->transceiver->dev);
NeilBrownab378132012-05-09 07:40:40 +1000523 bci->usb_enabled = 0;
524 }
NeilBrown21ae4042015-07-30 10:11:24 +1000525 bci->usb_cur = 0;
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300526 }
527
528 return ret;
529}
530
531/*
532 * Enable/Disable AC Charge funtionality.
533 */
NeilBrownb04b9082015-07-30 10:11:24 +1000534static int twl4030_charger_enable_ac(struct twl4030_bci *bci, bool enable)
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300535{
536 int ret;
537
NeilBrownb04b9082015-07-30 10:11:24 +1000538 if (bci->ac_mode == CHARGE_OFF)
539 enable = false;
540
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300541 if (enable)
542 ret = twl4030_clear_set_boot_bci(0, TWL4030_BCIAUTOAC);
543 else
544 ret = twl4030_clear_set_boot_bci(TWL4030_BCIAUTOAC, 0);
545
546 return ret;
547}
548
549/*
NeilBrown210d4bc2012-05-09 07:40:40 +1000550 * Enable/Disable charging of Backup Battery.
551 */
552static int twl4030_charger_enable_backup(int uvolt, int uamp)
553{
554 int ret;
555 u8 flags;
556
557 if (uvolt < 2500000 ||
558 uamp < 25) {
559 /* disable charging of backup battery */
Peter Ujfalusie9f14c12012-11-13 10:42:11 +0100560 ret = twl4030_clear_set(TWL_MODULE_PM_RECEIVER,
NeilBrown210d4bc2012-05-09 07:40:40 +1000561 TWL4030_BBCHEN, 0, TWL4030_BB_CFG);
562 return ret;
563 }
564
565 flags = TWL4030_BBCHEN;
566 if (uvolt >= 3200000)
567 flags |= TWL4030_BBSEL_3V2;
568 else if (uvolt >= 3100000)
569 flags |= TWL4030_BBSEL_3V1;
570 else if (uvolt >= 3000000)
571 flags |= TWL4030_BBSEL_3V0;
572 else
573 flags |= TWL4030_BBSEL_2V5;
574
575 if (uamp >= 1000)
576 flags |= TWL4030_BBISEL_1000uA;
577 else if (uamp >= 500)
578 flags |= TWL4030_BBISEL_500uA;
579 else if (uamp >= 150)
580 flags |= TWL4030_BBISEL_150uA;
581 else
582 flags |= TWL4030_BBISEL_25uA;
583
Peter Ujfalusie9f14c12012-11-13 10:42:11 +0100584 ret = twl4030_clear_set(TWL_MODULE_PM_RECEIVER,
NeilBrown210d4bc2012-05-09 07:40:40 +1000585 TWL4030_BBSEL_MASK | TWL4030_BBISEL_MASK,
586 flags,
587 TWL4030_BB_CFG);
588
589 return ret;
590}
591
592/*
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300593 * TWL4030 CHG_PRES (AC charger presence) events
594 */
595static irqreturn_t twl4030_charger_interrupt(int irq, void *arg)
596{
597 struct twl4030_bci *bci = arg;
598
599 dev_dbg(bci->dev, "CHG_PRES irq\n");
NeilBrownaca3c352015-07-30 10:11:24 +1000600 /* reset current on each 'plug' event */
601 bci->ac_cur = 500000;
NeilBrowne4ae5372015-07-30 10:11:24 +1000602 twl4030_charger_update_current(bci);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100603 power_supply_changed(bci->ac);
604 power_supply_changed(bci->usb);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300605
606 return IRQ_HANDLED;
607}
608
609/*
610 * TWL4030 BCI monitoring events
611 */
612static irqreturn_t twl4030_bci_interrupt(int irq, void *arg)
613{
614 struct twl4030_bci *bci = arg;
615 u8 irqs1, irqs2;
616 int ret;
617
618 ret = twl_i2c_read_u8(TWL4030_MODULE_INTERRUPTS, &irqs1,
619 TWL4030_INTERRUPTS_BCIISR1A);
620 if (ret < 0)
621 return IRQ_HANDLED;
622
623 ret = twl_i2c_read_u8(TWL4030_MODULE_INTERRUPTS, &irqs2,
624 TWL4030_INTERRUPTS_BCIISR2A);
625 if (ret < 0)
626 return IRQ_HANDLED;
627
628 dev_dbg(bci->dev, "BCI irq %02x %02x\n", irqs2, irqs1);
629
630 if (irqs1 & (TWL4030_ICHGLOW | TWL4030_ICHGEOC)) {
631 /* charger state change, inform the core */
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100632 power_supply_changed(bci->ac);
633 power_supply_changed(bci->usb);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300634 }
NeilBrowne4ae5372015-07-30 10:11:24 +1000635 twl4030_charger_update_current(bci);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300636
637 /* various monitoring events, for now we just log them here */
638 if (irqs1 & (TWL4030_TBATOR2 | TWL4030_TBATOR1))
639 dev_warn(bci->dev, "battery temperature out of range\n");
640
641 if (irqs1 & TWL4030_BATSTS)
642 dev_crit(bci->dev, "battery disconnected\n");
643
644 if (irqs2 & TWL4030_VBATOV)
645 dev_crit(bci->dev, "VBAT overvoltage\n");
646
647 if (irqs2 & TWL4030_VBUSOV)
648 dev_crit(bci->dev, "VBUS overvoltage\n");
649
650 if (irqs2 & TWL4030_ACCHGOV)
651 dev_crit(bci->dev, "Ac charger overvoltage\n");
652
653 return IRQ_HANDLED;
654}
655
NeilBrownaca3c352015-07-30 10:11:24 +1000656/*
657 * Provide "max_current" attribute in sysfs.
658 */
659static ssize_t
660twl4030_bci_max_current_store(struct device *dev, struct device_attribute *attr,
661 const char *buf, size_t n)
662{
663 struct twl4030_bci *bci = dev_get_drvdata(dev->parent);
664 int cur = 0;
665 int status = 0;
666 status = kstrtoint(buf, 10, &cur);
667 if (status)
668 return status;
669 if (cur < 0)
670 return -EINVAL;
671 if (dev == &bci->ac->dev)
672 bci->ac_cur = cur;
673 else
NeilBrown21ae4042015-07-30 10:11:24 +1000674 bci->usb_cur_target = cur;
NeilBrownaca3c352015-07-30 10:11:24 +1000675
676 twl4030_charger_update_current(bci);
677 return n;
678}
679
680/*
681 * sysfs max_current show
682 */
683static ssize_t twl4030_bci_max_current_show(struct device *dev,
684 struct device_attribute *attr, char *buf)
685{
686 int status = 0;
687 int cur = -1;
688 u8 bcictl1;
689 struct twl4030_bci *bci = dev_get_drvdata(dev->parent);
690
691 if (dev == &bci->ac->dev) {
692 if (!bci->ac_is_active)
693 cur = bci->ac_cur;
694 } else {
695 if (bci->ac_is_active)
NeilBrown21ae4042015-07-30 10:11:24 +1000696 cur = bci->usb_cur_target;
NeilBrownaca3c352015-07-30 10:11:24 +1000697 }
698 if (cur < 0) {
699 cur = twl4030bci_read_adc_val(TWL4030_BCIIREF1);
700 if (cur < 0)
701 return cur;
702 status = twl4030_bci_read(TWL4030_BCICTL1, &bcictl1);
703 if (status < 0)
704 return status;
705 cur = regval2ua(cur, bcictl1 & TWL4030_CGAIN);
706 }
707 return scnprintf(buf, PAGE_SIZE, "%u\n", cur);
708}
709
710static DEVICE_ATTR(max_current, 0644, twl4030_bci_max_current_show,
711 twl4030_bci_max_current_store);
712
Heikki Krogerusd6ccc442011-02-25 13:56:36 +0200713static void twl4030_bci_usb_work(struct work_struct *data)
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300714{
Heikki Krogerusd6ccc442011-02-25 13:56:36 +0200715 struct twl4030_bci *bci = container_of(data, struct twl4030_bci, work);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300716
Heikki Krogerusd6ccc442011-02-25 13:56:36 +0200717 switch (bci->event) {
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300718 case USB_EVENT_VBUS:
719 case USB_EVENT_CHARGER:
720 twl4030_charger_enable_usb(bci, true);
721 break;
722 case USB_EVENT_NONE:
723 twl4030_charger_enable_usb(bci, false);
724 break;
725 }
Heikki Krogerusd6ccc442011-02-25 13:56:36 +0200726}
727
728static int twl4030_bci_usb_ncb(struct notifier_block *nb, unsigned long val,
729 void *priv)
730{
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200731 struct twl4030_bci *bci = container_of(nb, struct twl4030_bci, usb_nb);
Heikki Krogerusd6ccc442011-02-25 13:56:36 +0200732
733 dev_dbg(bci->dev, "OTG notify %lu\n", val);
734
NeilBrownaca3c352015-07-30 10:11:24 +1000735 /* reset current on each 'plug' event */
736 if (allow_usb)
NeilBrown21ae4042015-07-30 10:11:24 +1000737 bci->usb_cur_target = 500000;
NeilBrownaca3c352015-07-30 10:11:24 +1000738 else
NeilBrown21ae4042015-07-30 10:11:24 +1000739 bci->usb_cur_target = 100000;
NeilBrownaca3c352015-07-30 10:11:24 +1000740
Heikki Krogerusd6ccc442011-02-25 13:56:36 +0200741 bci->event = val;
742 schedule_work(&bci->work);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300743
744 return NOTIFY_OK;
745}
746
NeilBrown22d4c332015-07-30 10:11:24 +1000747/*
748 * sysfs charger enabled store
749 */
750static ssize_t
751twl4030_bci_mode_store(struct device *dev, struct device_attribute *attr,
752 const char *buf, size_t n)
753{
754 struct twl4030_bci *bci = dev_get_drvdata(dev->parent);
755 int mode;
756 int status;
757
758 if (sysfs_streq(buf, modes[0]))
759 mode = 0;
760 else if (sysfs_streq(buf, modes[1]))
761 mode = 1;
NeilBrown7f4a6332015-07-30 10:11:24 +1000762 else if (sysfs_streq(buf, modes[2]))
763 mode = 2;
NeilBrown22d4c332015-07-30 10:11:24 +1000764 else
765 return -EINVAL;
NeilBrownb04b9082015-07-30 10:11:24 +1000766 if (dev == &bci->ac->dev) {
767 if (mode == 2)
768 return -EINVAL;
769 twl4030_charger_enable_ac(bci, false);
770 bci->ac_mode = mode;
771 status = twl4030_charger_enable_ac(bci, true);
772 } else {
773 twl4030_charger_enable_usb(bci, false);
774 bci->usb_mode = mode;
775 status = twl4030_charger_enable_usb(bci, true);
776 }
NeilBrown22d4c332015-07-30 10:11:24 +1000777 return (status == 0) ? n : status;
778}
779
780/*
781 * sysfs charger enabled show
782 */
783static ssize_t
784twl4030_bci_mode_show(struct device *dev,
785 struct device_attribute *attr, char *buf)
786{
787 struct twl4030_bci *bci = dev_get_drvdata(dev->parent);
788 int len = 0;
789 int i;
NeilBrownb04b9082015-07-30 10:11:24 +1000790 int mode = bci->usb_mode;
791
792 if (dev == &bci->ac->dev)
793 mode = bci->ac_mode;
NeilBrown22d4c332015-07-30 10:11:24 +1000794
795 for (i = 0; i < ARRAY_SIZE(modes); i++)
NeilBrownb04b9082015-07-30 10:11:24 +1000796 if (mode == i)
NeilBrown22d4c332015-07-30 10:11:24 +1000797 len += snprintf(buf+len, PAGE_SIZE-len,
798 "[%s] ", modes[i]);
799 else
800 len += snprintf(buf+len, PAGE_SIZE-len,
801 "%s ", modes[i]);
802 buf[len-1] = '\n';
803 return len;
804}
805static DEVICE_ATTR(mode, 0644, twl4030_bci_mode_show,
806 twl4030_bci_mode_store);
807
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300808static int twl4030_charger_get_current(void)
809{
810 int curr;
811 int ret;
812 u8 bcictl1;
813
814 curr = twl4030bci_read_adc_val(TWL4030_BCIICHG);
815 if (curr < 0)
816 return curr;
817
818 ret = twl4030_bci_read(TWL4030_BCICTL1, &bcictl1);
819 if (ret)
820 return ret;
821
NeilBrown3b542f02015-07-30 10:11:24 +1000822 return regval2ua(curr, bcictl1 & TWL4030_CGAIN);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300823}
824
825/*
826 * Returns the main charge FSM state
827 * Or < 0 on failure.
828 */
829static int twl4030bci_state(struct twl4030_bci *bci)
830{
831 int ret;
832 u8 state;
833
834 ret = twl4030_bci_read(TWL4030_BCIMSTATEC, &state);
835 if (ret) {
836 pr_err("twl4030_bci: error reading BCIMSTATEC\n");
837 return ret;
838 }
839
840 dev_dbg(bci->dev, "state: %02x\n", state);
841
842 return state;
843}
844
845static int twl4030_bci_state_to_status(int state)
846{
847 state &= TWL4030_MSTATEC_MASK;
848 if (TWL4030_MSTATEC_QUICK1 <= state && state <= TWL4030_MSTATEC_QUICK7)
849 return POWER_SUPPLY_STATUS_CHARGING;
850 else if (TWL4030_MSTATEC_COMPLETE1 <= state &&
851 state <= TWL4030_MSTATEC_COMPLETE4)
852 return POWER_SUPPLY_STATUS_FULL;
853 else
854 return POWER_SUPPLY_STATUS_NOT_CHARGING;
855}
856
857static int twl4030_bci_get_property(struct power_supply *psy,
858 enum power_supply_property psp,
859 union power_supply_propval *val)
860{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100861 struct twl4030_bci *bci = dev_get_drvdata(psy->dev.parent);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300862 int is_charging;
863 int state;
864 int ret;
865
866 state = twl4030bci_state(bci);
867 if (state < 0)
868 return state;
869
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100870 if (psy->desc->type == POWER_SUPPLY_TYPE_USB)
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300871 is_charging = state & TWL4030_MSTATEC_USB;
872 else
873 is_charging = state & TWL4030_MSTATEC_AC;
NeilBrown7f4a6332015-07-30 10:11:24 +1000874 if (!is_charging) {
875 u8 s;
876 twl4030_bci_read(TWL4030_BCIMDEN, &s);
877 if (psy->desc->type == POWER_SUPPLY_TYPE_USB)
878 is_charging = s & 1;
879 else
880 is_charging = s & 2;
881 if (is_charging)
882 /* A little white lie */
883 state = TWL4030_MSTATEC_QUICK1;
884 }
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300885
886 switch (psp) {
887 case POWER_SUPPLY_PROP_STATUS:
888 if (is_charging)
889 val->intval = twl4030_bci_state_to_status(state);
890 else
891 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
892 break;
893 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
894 /* charging must be active for meaningful result */
895 if (!is_charging)
896 return -ENODATA;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100897 if (psy->desc->type == POWER_SUPPLY_TYPE_USB) {
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300898 ret = twl4030bci_read_adc_val(TWL4030_BCIVBUS);
899 if (ret < 0)
900 return ret;
901 /* BCIVBUS uses ADCIN8, 7/1023 V/step */
902 val->intval = ret * 6843;
903 } else {
904 ret = twl4030bci_read_adc_val(TWL4030_BCIVAC);
905 if (ret < 0)
906 return ret;
907 /* BCIVAC uses ADCIN11, 10/1023 V/step */
908 val->intval = ret * 9775;
909 }
910 break;
911 case POWER_SUPPLY_PROP_CURRENT_NOW:
912 if (!is_charging)
913 return -ENODATA;
914 /* current measurement is shared between AC and USB */
915 ret = twl4030_charger_get_current();
916 if (ret < 0)
917 return ret;
918 val->intval = ret;
919 break;
920 case POWER_SUPPLY_PROP_ONLINE:
921 val->intval = is_charging &&
922 twl4030_bci_state_to_status(state) !=
923 POWER_SUPPLY_STATUS_NOT_CHARGING;
924 break;
925 default:
926 return -EINVAL;
927 }
928
929 return 0;
930}
931
932static enum power_supply_property twl4030_charger_props[] = {
933 POWER_SUPPLY_PROP_STATUS,
934 POWER_SUPPLY_PROP_ONLINE,
935 POWER_SUPPLY_PROP_VOLTAGE_NOW,
936 POWER_SUPPLY_PROP_CURRENT_NOW,
937};
938
NeilBrownec0b3802013-10-31 17:05:50 +1100939#ifdef CONFIG_OF
940static const struct twl4030_bci_platform_data *
941twl4030_bci_parse_dt(struct device *dev)
942{
943 struct device_node *np = dev->of_node;
944 struct twl4030_bci_platform_data *pdata;
945 u32 num;
946
947 if (!np)
948 return NULL;
949 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
950 if (!pdata)
951 return pdata;
952
953 if (of_property_read_u32(np, "ti,bb-uvolt", &num) == 0)
954 pdata->bb_uvolt = num;
955 if (of_property_read_u32(np, "ti,bb-uamp", &num) == 0)
956 pdata->bb_uamp = num;
957 return pdata;
958}
959#else
960static inline const struct twl4030_bci_platform_data *
961twl4030_bci_parse_dt(struct device *dev)
962{
963 return NULL;
964}
965#endif
966
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100967static const struct power_supply_desc twl4030_bci_ac_desc = {
968 .name = "twl4030_ac",
969 .type = POWER_SUPPLY_TYPE_MAINS,
970 .properties = twl4030_charger_props,
971 .num_properties = ARRAY_SIZE(twl4030_charger_props),
972 .get_property = twl4030_bci_get_property,
973};
974
975static const struct power_supply_desc twl4030_bci_usb_desc = {
976 .name = "twl4030_usb",
977 .type = POWER_SUPPLY_TYPE_USB,
978 .properties = twl4030_charger_props,
979 .num_properties = ARRAY_SIZE(twl4030_charger_props),
980 .get_property = twl4030_bci_get_property,
981};
982
Pavel Machek7396f702015-07-30 10:11:24 +1000983static int twl4030_bci_probe(struct platform_device *pdev)
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300984{
985 struct twl4030_bci *bci;
NeilBrownec0b3802013-10-31 17:05:50 +1100986 const struct twl4030_bci_platform_data *pdata = pdev->dev.platform_data;
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300987 int ret;
Grazvydas Ignotas28d48f02011-06-03 14:43:11 +0300988 u32 reg;
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300989
NeilBrown325b50a2015-03-23 10:20:28 +1100990 bci = devm_kzalloc(&pdev->dev, sizeof(*bci), GFP_KERNEL);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300991 if (bci == NULL)
992 return -ENOMEM;
993
NeilBrownec0b3802013-10-31 17:05:50 +1100994 if (!pdata)
995 pdata = twl4030_bci_parse_dt(&pdev->dev);
996
NeilBrown1098cb52015-07-30 10:11:24 +1000997 bci->ichg_eoc = 80100; /* Stop charging when current drops to here */
998 bci->ichg_lo = 241000; /* Low threshold */
999 bci->ichg_hi = 500000; /* High threshold */
NeilBrowne4ae5372015-07-30 10:11:24 +10001000 bci->ac_cur = 500000; /* 500mA */
NeilBrown1098cb52015-07-30 10:11:24 +10001001 if (allow_usb)
NeilBrown21ae4042015-07-30 10:11:24 +10001002 bci->usb_cur_target = 500000; /* 500mA */
NeilBrown1098cb52015-07-30 10:11:24 +10001003 else
NeilBrown21ae4042015-07-30 10:11:24 +10001004 bci->usb_cur_target = 100000; /* 100mA */
NeilBrown22d4c332015-07-30 10:11:24 +10001005 bci->usb_mode = CHARGE_AUTO;
NeilBrownb04b9082015-07-30 10:11:24 +10001006 bci->ac_mode = CHARGE_AUTO;
NeilBrown1098cb52015-07-30 10:11:24 +10001007
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +03001008 bci->dev = &pdev->dev;
1009 bci->irq_chg = platform_get_irq(pdev, 0);
1010 bci->irq_bci = platform_get_irq(pdev, 1);
1011
Nishanth Menon61a77842014-05-28 16:46:49 -05001012 /* Only proceed further *IF* battery is physically present */
1013 ret = twl4030_is_battery_present(bci);
1014 if (ret) {
1015 dev_crit(&pdev->dev, "Battery was not detected:%d\n", ret);
NeilBrown325b50a2015-03-23 10:20:28 +11001016 return ret;
Nishanth Menon61a77842014-05-28 16:46:49 -05001017 }
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +03001018
Nishanth Menon61a77842014-05-28 16:46:49 -05001019 platform_set_drvdata(pdev, bci);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +03001020
NeilBrown325b50a2015-03-23 10:20:28 +11001021 bci->ac = devm_power_supply_register(&pdev->dev, &twl4030_bci_ac_desc,
1022 NULL);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001023 if (IS_ERR(bci->ac)) {
1024 ret = PTR_ERR(bci->ac);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +03001025 dev_err(&pdev->dev, "failed to register ac: %d\n", ret);
NeilBrown325b50a2015-03-23 10:20:28 +11001026 return ret;
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +03001027 }
1028
NeilBrown325b50a2015-03-23 10:20:28 +11001029 bci->usb = devm_power_supply_register(&pdev->dev, &twl4030_bci_usb_desc,
1030 NULL);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001031 if (IS_ERR(bci->usb)) {
1032 ret = PTR_ERR(bci->usb);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +03001033 dev_err(&pdev->dev, "failed to register usb: %d\n", ret);
NeilBrown325b50a2015-03-23 10:20:28 +11001034 return ret;
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +03001035 }
1036
NeilBrown1b7cac22015-03-23 10:20:28 +11001037 ret = devm_request_threaded_irq(&pdev->dev, bci->irq_chg, NULL,
Fengguang Wua2778b02012-08-23 19:56:43 +08001038 twl4030_charger_interrupt, IRQF_ONESHOT, pdev->name,
1039 bci);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +03001040 if (ret < 0) {
1041 dev_err(&pdev->dev, "could not request irq %d, status %d\n",
1042 bci->irq_chg, ret);
NeilBrown325b50a2015-03-23 10:20:28 +11001043 return ret;
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +03001044 }
1045
NeilBrown1b7cac22015-03-23 10:20:28 +11001046 ret = devm_request_threaded_irq(&pdev->dev, bci->irq_bci, NULL,
Fengguang Wua2778b02012-08-23 19:56:43 +08001047 twl4030_bci_interrupt, IRQF_ONESHOT, pdev->name, bci);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +03001048 if (ret < 0) {
1049 dev_err(&pdev->dev, "could not request irq %d, status %d\n",
1050 bci->irq_bci, ret);
NeilBrown325b50a2015-03-23 10:20:28 +11001051 return ret;
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +03001052 }
1053
Marek Belisko2202e1f2015-09-25 22:20:45 +02001054 bci->channel_vac = iio_channel_get(&pdev->dev, "vac");
1055 if (IS_ERR(bci->channel_vac)) {
1056 bci->channel_vac = NULL;
1057 dev_warn(&pdev->dev, "could not request vac iio channel");
1058 }
1059
Heikki Krogerusd6ccc442011-02-25 13:56:36 +02001060 INIT_WORK(&bci->work, twl4030_bci_usb_work);
NeilBrown21ae4042015-07-30 10:11:24 +10001061 INIT_DELAYED_WORK(&bci->current_worker, twl4030_current_worker);
Heikki Krogerusd6ccc442011-02-25 13:56:36 +02001062
NeilBrownf5e4edb2015-03-23 09:52:48 +11001063 bci->usb_nb.notifier_call = twl4030_bci_usb_ncb;
1064 if (bci->dev->of_node) {
1065 struct device_node *phynode;
1066
1067 phynode = of_find_compatible_node(bci->dev->of_node->parent,
1068 NULL, "ti,twl4030-usb");
Sebastian Reichelaefc574b2015-09-10 10:00:55 +02001069 if (phynode)
NeilBrownf5e4edb2015-03-23 09:52:48 +11001070 bci->transceiver = devm_usb_get_phy_by_node(
1071 bci->dev, phynode, &bci->usb_nb);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +03001072 }
1073
1074 /* Enable interrupts now. */
Grazvydas Ignotas28d48f02011-06-03 14:43:11 +03001075 reg = ~(u32)(TWL4030_ICHGLOW | TWL4030_ICHGEOC | TWL4030_TBATOR2 |
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +03001076 TWL4030_TBATOR1 | TWL4030_BATSTS);
1077 ret = twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS, reg,
1078 TWL4030_INTERRUPTS_BCIIMR1A);
1079 if (ret < 0) {
1080 dev_err(&pdev->dev, "failed to unmask interrupts: %d\n", ret);
Marek Belisko2202e1f2015-09-25 22:20:45 +02001081 goto fail;
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +03001082 }
1083
Grazvydas Ignotas28d48f02011-06-03 14:43:11 +03001084 reg = ~(u32)(TWL4030_VBATOV | TWL4030_VBUSOV | TWL4030_ACCHGOV);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +03001085 ret = twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS, reg,
1086 TWL4030_INTERRUPTS_BCIIMR2A);
1087 if (ret < 0)
1088 dev_warn(&pdev->dev, "failed to unmask interrupts: %d\n", ret);
1089
NeilBrown1098cb52015-07-30 10:11:24 +10001090 twl4030_charger_update_current(bci);
NeilBrownaca3c352015-07-30 10:11:24 +10001091 if (device_create_file(&bci->usb->dev, &dev_attr_max_current))
1092 dev_warn(&pdev->dev, "could not create sysfs file\n");
NeilBrown22d4c332015-07-30 10:11:24 +10001093 if (device_create_file(&bci->usb->dev, &dev_attr_mode))
1094 dev_warn(&pdev->dev, "could not create sysfs file\n");
NeilBrownb04b9082015-07-30 10:11:24 +10001095 if (device_create_file(&bci->ac->dev, &dev_attr_mode))
1096 dev_warn(&pdev->dev, "could not create sysfs file\n");
NeilBrownaca3c352015-07-30 10:11:24 +10001097 if (device_create_file(&bci->ac->dev, &dev_attr_max_current))
1098 dev_warn(&pdev->dev, "could not create sysfs file\n");
1099
NeilBrownb04b9082015-07-30 10:11:24 +10001100 twl4030_charger_enable_ac(bci, true);
NeilBrown6e37ec82015-07-30 10:11:24 +10001101 if (!IS_ERR_OR_NULL(bci->transceiver))
1102 twl4030_bci_usb_ncb(&bci->usb_nb,
1103 bci->transceiver->last_event,
1104 NULL);
1105 else
1106 twl4030_charger_enable_usb(bci, false);
NeilBrownec0b3802013-10-31 17:05:50 +11001107 if (pdata)
1108 twl4030_charger_enable_backup(pdata->bb_uvolt,
1109 pdata->bb_uamp);
1110 else
1111 twl4030_charger_enable_backup(0, 0);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +03001112
1113 return 0;
Marek Belisko2202e1f2015-09-25 22:20:45 +02001114fail:
1115 iio_channel_release(bci->channel_vac);
1116
1117 return ret;
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +03001118}
1119
1120static int __exit twl4030_bci_remove(struct platform_device *pdev)
1121{
1122 struct twl4030_bci *bci = platform_get_drvdata(pdev);
1123
NeilBrownb04b9082015-07-30 10:11:24 +10001124 twl4030_charger_enable_ac(bci, false);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +03001125 twl4030_charger_enable_usb(bci, false);
NeilBrown210d4bc2012-05-09 07:40:40 +10001126 twl4030_charger_enable_backup(0, 0);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +03001127
Marek Belisko2202e1f2015-09-25 22:20:45 +02001128 iio_channel_release(bci->channel_vac);
1129
NeilBrownaca3c352015-07-30 10:11:24 +10001130 device_remove_file(&bci->usb->dev, &dev_attr_max_current);
NeilBrown22d4c332015-07-30 10:11:24 +10001131 device_remove_file(&bci->usb->dev, &dev_attr_mode);
NeilBrownaca3c352015-07-30 10:11:24 +10001132 device_remove_file(&bci->ac->dev, &dev_attr_max_current);
NeilBrownb04b9082015-07-30 10:11:24 +10001133 device_remove_file(&bci->ac->dev, &dev_attr_mode);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +03001134 /* mask interrupts */
1135 twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS, 0xff,
1136 TWL4030_INTERRUPTS_BCIIMR1A);
1137 twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS, 0xff,
1138 TWL4030_INTERRUPTS_BCIIMR2A);
1139
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +03001140 return 0;
1141}
1142
NeilBrownec0b3802013-10-31 17:05:50 +11001143static const struct of_device_id twl_bci_of_match[] = {
1144 {.compatible = "ti,twl4030-bci", },
1145 { }
1146};
1147MODULE_DEVICE_TABLE(of, twl_bci_of_match);
1148
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +03001149static struct platform_driver twl4030_bci_driver = {
Pavel Machek7396f702015-07-30 10:11:24 +10001150 .probe = twl4030_bci_probe,
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +03001151 .driver = {
1152 .name = "twl4030_bci",
NeilBrownec0b3802013-10-31 17:05:50 +11001153 .of_match_table = of_match_ptr(twl_bci_of_match),
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +03001154 },
1155 .remove = __exit_p(twl4030_bci_remove),
1156};
Pavel Machek7396f702015-07-30 10:11:24 +10001157module_platform_driver(twl4030_bci_driver);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +03001158
Grazvydas Ignotas28d48f02011-06-03 14:43:11 +03001159MODULE_AUTHOR("Gražvydas Ignotas");
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +03001160MODULE_DESCRIPTION("TWL4030 Battery Charger Interface driver");
1161MODULE_LICENSE("GPL");
1162MODULE_ALIAS("platform:twl4030_bci");