blob: a5915f498eea334a60180eea99cea800ca08f3d7 [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>
Wolfram Sanga2054252017-08-14 18:34:24 +020021#include <linux/mfd/twl.h>
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +030022#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 */
Andy Shevchenko37853952017-06-11 18:15:48 +0300156static const 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/*
NeilBrown3b542f02015-07-30 10:11:24 +1000209 * TI provided formulas:
210 * CGAIN == 0: ICHG = (BCIICHG * 1.7) / (2^10 - 1) - 0.85
211 * CGAIN == 1: ICHG = (BCIICHG * 3.4) / (2^10 - 1) - 1.7
212 * Here we use integer approximation of:
213 * CGAIN == 0: val * 1.6618 - 0.85 * 1000
214 * CGAIN == 1: (val * 1.6618 - 0.85 * 1000) * 2
215 */
216/*
217 * convert twl register value for currents into uA
218 */
219static int regval2ua(int regval, bool cgain)
220{
221 if (cgain)
222 return (regval * 16618 - 8500 * 1000) / 5;
223 else
224 return (regval * 16618 - 8500 * 1000) / 10;
225}
226
227/*
228 * convert uA currents into twl register value
229 */
230static int ua2regval(int ua, bool cgain)
231{
232 int ret;
233 if (cgain)
234 ua /= 2;
235 ret = (ua * 10 + 8500 * 1000) / 16618;
236 /* rounding problems */
237 if (ret < 512)
238 ret = 512;
239 return ret;
240}
241
NeilBrown1098cb52015-07-30 10:11:24 +1000242static int twl4030_charger_update_current(struct twl4030_bci *bci)
243{
244 int status;
NeilBrowne4ae5372015-07-30 10:11:24 +1000245 int cur;
NeilBrown1098cb52015-07-30 10:11:24 +1000246 unsigned reg, cur_reg;
247 u8 bcictl1, oldreg, fullreg;
248 bool cgain = false;
249 u8 boot_bci;
250
NeilBrowne4ae5372015-07-30 10:11:24 +1000251 /*
252 * If AC (Accessory Charger) voltage exceeds 4.5V (MADC 11)
253 * and AC is enabled, set current for 'ac'
254 */
Marek Belisko2202e1f2015-09-25 22:20:45 +0200255 if (ac_available(bci->channel_vac)) {
NeilBrowne4ae5372015-07-30 10:11:24 +1000256 cur = bci->ac_cur;
257 bci->ac_is_active = true;
258 } else {
259 cur = bci->usb_cur;
260 bci->ac_is_active = false;
NeilBrown21ae4042015-07-30 10:11:24 +1000261 if (cur > bci->usb_cur_target) {
262 cur = bci->usb_cur_target;
263 bci->usb_cur = cur;
264 }
265 if (cur < bci->usb_cur_target)
266 schedule_delayed_work(&bci->current_worker, USB_CUR_DELAY);
NeilBrowne4ae5372015-07-30 10:11:24 +1000267 }
268
NeilBrown1098cb52015-07-30 10:11:24 +1000269 /* First, check thresholds and see if cgain is needed */
270 if (bci->ichg_eoc >= 200000)
271 cgain = true;
272 if (bci->ichg_lo >= 400000)
273 cgain = true;
274 if (bci->ichg_hi >= 820000)
275 cgain = true;
NeilBrowne4ae5372015-07-30 10:11:24 +1000276 if (cur > 852000)
NeilBrown1098cb52015-07-30 10:11:24 +1000277 cgain = true;
278
279 status = twl4030_bci_read(TWL4030_BCICTL1, &bcictl1);
280 if (status < 0)
281 return status;
282 if (twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &boot_bci,
283 TWL4030_PM_MASTER_BOOT_BCI) < 0)
284 boot_bci = 0;
285 boot_bci &= 7;
286
287 if ((!!cgain) != !!(bcictl1 & TWL4030_CGAIN))
288 /* Need to turn for charging while we change the
289 * CGAIN bit. Leave it off while everything is
290 * updated.
291 */
292 twl4030_clear_set_boot_bci(boot_bci, 0);
293
294 /*
295 * For ichg_eoc, the hardware only supports reg values matching
296 * 100XXXX000, and requires the XXXX be stored in the high nibble
297 * of TWL4030_BCIMFTH8.
298 */
299 reg = ua2regval(bci->ichg_eoc, cgain);
300 if (reg > 0x278)
301 reg = 0x278;
302 if (reg < 0x200)
303 reg = 0x200;
304 reg = (reg >> 3) & 0xf;
305 fullreg = reg << 4;
306
307 /*
308 * For ichg_lo, reg value must match 10XXXX0000.
309 * XXXX is stored in low nibble of TWL4030_BCIMFTH8.
310 */
311 reg = ua2regval(bci->ichg_lo, cgain);
312 if (reg > 0x2F0)
313 reg = 0x2F0;
314 if (reg < 0x200)
315 reg = 0x200;
316 reg = (reg >> 4) & 0xf;
317 fullreg |= reg;
318
319 /* ichg_eoc and ichg_lo live in same register */
320 status = twl4030_bci_read(TWL4030_BCIMFTH8, &oldreg);
321 if (status < 0)
322 return status;
323 if (oldreg != fullreg) {
324 status = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0xF4,
325 TWL4030_BCIMFKEY);
326 if (status < 0)
327 return status;
328 twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
329 fullreg, TWL4030_BCIMFTH8);
330 }
331
332 /* ichg_hi threshold must be 1XXXX01100 (I think) */
333 reg = ua2regval(bci->ichg_hi, cgain);
334 if (reg > 0x3E0)
335 reg = 0x3E0;
336 if (reg < 0x200)
337 reg = 0x200;
338 fullreg = (reg >> 5) & 0xF;
339 fullreg <<= 4;
340 status = twl4030_bci_read(TWL4030_BCIMFTH9, &oldreg);
341 if (status < 0)
342 return status;
343 if ((oldreg & 0xF0) != fullreg) {
344 fullreg |= (oldreg & 0x0F);
345 status = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0xE7,
346 TWL4030_BCIMFKEY);
347 if (status < 0)
348 return status;
349 twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
350 fullreg, TWL4030_BCIMFTH9);
351 }
352
353 /*
354 * And finally, set the current. This is stored in
355 * two registers.
356 */
NeilBrowne4ae5372015-07-30 10:11:24 +1000357 reg = ua2regval(cur, cgain);
NeilBrown1098cb52015-07-30 10:11:24 +1000358 /* we have only 10 bits */
359 if (reg > 0x3ff)
360 reg = 0x3ff;
361 status = twl4030_bci_read(TWL4030_BCIIREF1, &oldreg);
362 if (status < 0)
363 return status;
364 cur_reg = oldreg;
365 status = twl4030_bci_read(TWL4030_BCIIREF2, &oldreg);
366 if (status < 0)
367 return status;
368 cur_reg |= oldreg << 8;
369 if (reg != oldreg) {
370 /* disable write protection for one write access for
371 * BCIIREF */
372 status = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0xE7,
373 TWL4030_BCIMFKEY);
374 if (status < 0)
375 return status;
376 status = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
377 (reg & 0x100) ? 3 : 2,
378 TWL4030_BCIIREF2);
379 if (status < 0)
380 return status;
381 /* disable write protection for one write access for
382 * BCIIREF */
383 status = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0xE7,
384 TWL4030_BCIMFKEY);
385 if (status < 0)
386 return status;
387 status = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
388 reg & 0xff,
389 TWL4030_BCIIREF1);
390 }
391 if ((!!cgain) != !!(bcictl1 & TWL4030_CGAIN)) {
392 /* Flip CGAIN and re-enable charging */
393 bcictl1 ^= TWL4030_CGAIN;
394 twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
395 bcictl1, TWL4030_BCICTL1);
396 twl4030_clear_set_boot_bci(0, boot_bci);
397 }
398 return 0;
399}
400
NeilBrown21ae4042015-07-30 10:11:24 +1000401static int twl4030_charger_get_current(void);
402
403static void twl4030_current_worker(struct work_struct *data)
404{
405 int v, curr;
406 int res;
407 struct twl4030_bci *bci = container_of(data, struct twl4030_bci,
408 current_worker.work);
409
410 res = twl4030bci_read_adc_val(TWL4030_BCIVBUS);
411 if (res < 0)
412 v = 0;
413 else
414 /* BCIVBUS uses ADCIN8, 7/1023 V/step */
415 v = res * 6843;
416 curr = twl4030_charger_get_current();
417
418 dev_dbg(bci->dev, "v=%d cur=%d limit=%d target=%d\n", v, curr,
419 bci->usb_cur, bci->usb_cur_target);
420
421 if (v < USB_MIN_VOLT) {
422 /* Back up and stop adjusting. */
423 bci->usb_cur -= USB_CUR_STEP;
424 bci->usb_cur_target = bci->usb_cur;
425 } else if (bci->usb_cur >= bci->usb_cur_target ||
426 bci->usb_cur + USB_CUR_STEP > USB_MAX_CURRENT) {
427 /* Reached target and voltage is OK - stop */
428 return;
429 } else {
430 bci->usb_cur += USB_CUR_STEP;
431 schedule_delayed_work(&bci->current_worker, USB_CUR_DELAY);
432 }
433 twl4030_charger_update_current(bci);
434}
435
NeilBrown3b542f02015-07-30 10:11:24 +1000436/*
NeilBrown9c645d22012-05-09 07:40:39 +1000437 * Enable/Disable USB Charge functionality.
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300438 */
439static int twl4030_charger_enable_usb(struct twl4030_bci *bci, bool enable)
440{
441 int ret;
442
NeilBrown22d4c332015-07-30 10:11:24 +1000443 if (bci->usb_mode == CHARGE_OFF)
444 enable = false;
NeilBrowne57c4a62015-07-30 10:11:24 +1000445 if (enable && !IS_ERR_OR_NULL(bci->transceiver)) {
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300446
NeilBrowne4ae5372015-07-30 10:11:24 +1000447 twl4030_charger_update_current(bci);
448
NeilBrowne57c4a62015-07-30 10:11:24 +1000449 /* Need to keep phy powered */
NeilBrownab378132012-05-09 07:40:40 +1000450 if (!bci->usb_enabled) {
NeilBrowne57c4a62015-07-30 10:11:24 +1000451 pm_runtime_get_sync(bci->transceiver->dev);
NeilBrownab378132012-05-09 07:40:40 +1000452 bci->usb_enabled = 1;
453 }
454
NeilBrown7f4a6332015-07-30 10:11:24 +1000455 if (bci->usb_mode == CHARGE_AUTO)
456 /* forcing the field BCIAUTOUSB (BOOT_BCI[1]) to 1 */
457 ret = twl4030_clear_set_boot_bci(0, TWL4030_BCIAUTOUSB);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300458
459 /* forcing USBFASTMCHG(BCIMFSTS4[2]) to 1 */
Peter Ujfalusie9f14c12012-11-13 10:42:11 +0100460 ret = twl4030_clear_set(TWL_MODULE_MAIN_CHARGE, 0,
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300461 TWL4030_USBFASTMCHG, TWL4030_BCIMFSTS4);
NeilBrown7f4a6332015-07-30 10:11:24 +1000462 if (bci->usb_mode == CHARGE_LINEAR) {
463 twl4030_clear_set_boot_bci(TWL4030_BCIAUTOAC|TWL4030_CVENAC, 0);
464 /* Watch dog key: WOVF acknowledge */
465 ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0x33,
466 TWL4030_BCIWDKEY);
467 /* 0x24 + EKEY6: off mode */
468 ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0x2a,
469 TWL4030_BCIMDKEY);
470 /* EKEY2: Linear charge: USB path */
471 ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0x26,
472 TWL4030_BCIMDKEY);
473 /* WDKEY5: stop watchdog count */
474 ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0xf3,
475 TWL4030_BCIWDKEY);
476 /* enable MFEN3 access */
477 ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0x9c,
478 TWL4030_BCIMFKEY);
479 /* ICHGEOCEN - end-of-charge monitor (current < 80mA)
480 * (charging continues)
481 * ICHGLOWEN - current level monitor (charge continues)
482 * don't monitor over-current or heat save
483 */
484 ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0xf0,
485 TWL4030_BCIMFEN3);
486 }
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300487 } else {
488 ret = twl4030_clear_set_boot_bci(TWL4030_BCIAUTOUSB, 0);
NeilBrown7f4a6332015-07-30 10:11:24 +1000489 ret |= twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0x2a,
490 TWL4030_BCIMDKEY);
NeilBrownab378132012-05-09 07:40:40 +1000491 if (bci->usb_enabled) {
NeilBrowne57c4a62015-07-30 10:11:24 +1000492 pm_runtime_mark_last_busy(bci->transceiver->dev);
493 pm_runtime_put_autosuspend(bci->transceiver->dev);
NeilBrownab378132012-05-09 07:40:40 +1000494 bci->usb_enabled = 0;
495 }
NeilBrown21ae4042015-07-30 10:11:24 +1000496 bci->usb_cur = 0;
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300497 }
498
499 return ret;
500}
501
502/*
503 * Enable/Disable AC Charge funtionality.
504 */
NeilBrownb04b9082015-07-30 10:11:24 +1000505static int twl4030_charger_enable_ac(struct twl4030_bci *bci, bool enable)
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300506{
507 int ret;
508
NeilBrownb04b9082015-07-30 10:11:24 +1000509 if (bci->ac_mode == CHARGE_OFF)
510 enable = false;
511
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300512 if (enable)
513 ret = twl4030_clear_set_boot_bci(0, TWL4030_BCIAUTOAC);
514 else
515 ret = twl4030_clear_set_boot_bci(TWL4030_BCIAUTOAC, 0);
516
517 return ret;
518}
519
520/*
NeilBrown210d4bc2012-05-09 07:40:40 +1000521 * Enable/Disable charging of Backup Battery.
522 */
523static int twl4030_charger_enable_backup(int uvolt, int uamp)
524{
525 int ret;
526 u8 flags;
527
528 if (uvolt < 2500000 ||
529 uamp < 25) {
530 /* disable charging of backup battery */
Peter Ujfalusie9f14c12012-11-13 10:42:11 +0100531 ret = twl4030_clear_set(TWL_MODULE_PM_RECEIVER,
NeilBrown210d4bc2012-05-09 07:40:40 +1000532 TWL4030_BBCHEN, 0, TWL4030_BB_CFG);
533 return ret;
534 }
535
536 flags = TWL4030_BBCHEN;
537 if (uvolt >= 3200000)
538 flags |= TWL4030_BBSEL_3V2;
539 else if (uvolt >= 3100000)
540 flags |= TWL4030_BBSEL_3V1;
541 else if (uvolt >= 3000000)
542 flags |= TWL4030_BBSEL_3V0;
543 else
544 flags |= TWL4030_BBSEL_2V5;
545
546 if (uamp >= 1000)
547 flags |= TWL4030_BBISEL_1000uA;
548 else if (uamp >= 500)
549 flags |= TWL4030_BBISEL_500uA;
550 else if (uamp >= 150)
551 flags |= TWL4030_BBISEL_150uA;
552 else
553 flags |= TWL4030_BBISEL_25uA;
554
Peter Ujfalusie9f14c12012-11-13 10:42:11 +0100555 ret = twl4030_clear_set(TWL_MODULE_PM_RECEIVER,
NeilBrown210d4bc2012-05-09 07:40:40 +1000556 TWL4030_BBSEL_MASK | TWL4030_BBISEL_MASK,
557 flags,
558 TWL4030_BB_CFG);
559
560 return ret;
561}
562
563/*
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300564 * TWL4030 CHG_PRES (AC charger presence) events
565 */
566static irqreturn_t twl4030_charger_interrupt(int irq, void *arg)
567{
568 struct twl4030_bci *bci = arg;
569
570 dev_dbg(bci->dev, "CHG_PRES irq\n");
NeilBrownaca3c352015-07-30 10:11:24 +1000571 /* reset current on each 'plug' event */
572 bci->ac_cur = 500000;
NeilBrowne4ae5372015-07-30 10:11:24 +1000573 twl4030_charger_update_current(bci);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100574 power_supply_changed(bci->ac);
575 power_supply_changed(bci->usb);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300576
577 return IRQ_HANDLED;
578}
579
580/*
581 * TWL4030 BCI monitoring events
582 */
583static irqreturn_t twl4030_bci_interrupt(int irq, void *arg)
584{
585 struct twl4030_bci *bci = arg;
586 u8 irqs1, irqs2;
587 int ret;
588
589 ret = twl_i2c_read_u8(TWL4030_MODULE_INTERRUPTS, &irqs1,
590 TWL4030_INTERRUPTS_BCIISR1A);
591 if (ret < 0)
592 return IRQ_HANDLED;
593
594 ret = twl_i2c_read_u8(TWL4030_MODULE_INTERRUPTS, &irqs2,
595 TWL4030_INTERRUPTS_BCIISR2A);
596 if (ret < 0)
597 return IRQ_HANDLED;
598
599 dev_dbg(bci->dev, "BCI irq %02x %02x\n", irqs2, irqs1);
600
601 if (irqs1 & (TWL4030_ICHGLOW | TWL4030_ICHGEOC)) {
602 /* charger state change, inform the core */
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 }
NeilBrowne4ae5372015-07-30 10:11:24 +1000606 twl4030_charger_update_current(bci);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300607
608 /* various monitoring events, for now we just log them here */
609 if (irqs1 & (TWL4030_TBATOR2 | TWL4030_TBATOR1))
610 dev_warn(bci->dev, "battery temperature out of range\n");
611
612 if (irqs1 & TWL4030_BATSTS)
613 dev_crit(bci->dev, "battery disconnected\n");
614
615 if (irqs2 & TWL4030_VBATOV)
616 dev_crit(bci->dev, "VBAT overvoltage\n");
617
618 if (irqs2 & TWL4030_VBUSOV)
619 dev_crit(bci->dev, "VBUS overvoltage\n");
620
621 if (irqs2 & TWL4030_ACCHGOV)
622 dev_crit(bci->dev, "Ac charger overvoltage\n");
623
624 return IRQ_HANDLED;
625}
626
Heikki Krogerusd6ccc442011-02-25 13:56:36 +0200627static void twl4030_bci_usb_work(struct work_struct *data)
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300628{
Heikki Krogerusd6ccc442011-02-25 13:56:36 +0200629 struct twl4030_bci *bci = container_of(data, struct twl4030_bci, work);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300630
Heikki Krogerusd6ccc442011-02-25 13:56:36 +0200631 switch (bci->event) {
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300632 case USB_EVENT_VBUS:
633 case USB_EVENT_CHARGER:
634 twl4030_charger_enable_usb(bci, true);
635 break;
636 case USB_EVENT_NONE:
637 twl4030_charger_enable_usb(bci, false);
638 break;
639 }
Heikki Krogerusd6ccc442011-02-25 13:56:36 +0200640}
641
642static int twl4030_bci_usb_ncb(struct notifier_block *nb, unsigned long val,
643 void *priv)
644{
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200645 struct twl4030_bci *bci = container_of(nb, struct twl4030_bci, usb_nb);
Heikki Krogerusd6ccc442011-02-25 13:56:36 +0200646
647 dev_dbg(bci->dev, "OTG notify %lu\n", val);
648
NeilBrownaca3c352015-07-30 10:11:24 +1000649 /* reset current on each 'plug' event */
650 if (allow_usb)
NeilBrown21ae4042015-07-30 10:11:24 +1000651 bci->usb_cur_target = 500000;
NeilBrownaca3c352015-07-30 10:11:24 +1000652 else
NeilBrown21ae4042015-07-30 10:11:24 +1000653 bci->usb_cur_target = 100000;
NeilBrownaca3c352015-07-30 10:11:24 +1000654
Heikki Krogerusd6ccc442011-02-25 13:56:36 +0200655 bci->event = val;
656 schedule_work(&bci->work);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300657
658 return NOTIFY_OK;
659}
660
NeilBrown22d4c332015-07-30 10:11:24 +1000661/*
662 * sysfs charger enabled store
663 */
664static ssize_t
665twl4030_bci_mode_store(struct device *dev, struct device_attribute *attr,
666 const char *buf, size_t n)
667{
668 struct twl4030_bci *bci = dev_get_drvdata(dev->parent);
669 int mode;
670 int status;
671
Andy Shevchenko37853952017-06-11 18:15:48 +0300672 mode = sysfs_match_string(modes, buf);
673 if (mode < 0)
674 return mode;
675
NeilBrownb04b9082015-07-30 10:11:24 +1000676 if (dev == &bci->ac->dev) {
677 if (mode == 2)
678 return -EINVAL;
679 twl4030_charger_enable_ac(bci, false);
680 bci->ac_mode = mode;
681 status = twl4030_charger_enable_ac(bci, true);
682 } else {
683 twl4030_charger_enable_usb(bci, false);
684 bci->usb_mode = mode;
685 status = twl4030_charger_enable_usb(bci, true);
686 }
NeilBrown22d4c332015-07-30 10:11:24 +1000687 return (status == 0) ? n : status;
688}
689
690/*
691 * sysfs charger enabled show
692 */
693static ssize_t
694twl4030_bci_mode_show(struct device *dev,
695 struct device_attribute *attr, char *buf)
696{
697 struct twl4030_bci *bci = dev_get_drvdata(dev->parent);
698 int len = 0;
699 int i;
NeilBrownb04b9082015-07-30 10:11:24 +1000700 int mode = bci->usb_mode;
701
702 if (dev == &bci->ac->dev)
703 mode = bci->ac_mode;
NeilBrown22d4c332015-07-30 10:11:24 +1000704
705 for (i = 0; i < ARRAY_SIZE(modes); i++)
NeilBrownb04b9082015-07-30 10:11:24 +1000706 if (mode == i)
NeilBrown22d4c332015-07-30 10:11:24 +1000707 len += snprintf(buf+len, PAGE_SIZE-len,
708 "[%s] ", modes[i]);
709 else
710 len += snprintf(buf+len, PAGE_SIZE-len,
711 "%s ", modes[i]);
712 buf[len-1] = '\n';
713 return len;
714}
715static DEVICE_ATTR(mode, 0644, twl4030_bci_mode_show,
716 twl4030_bci_mode_store);
717
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300718static int twl4030_charger_get_current(void)
719{
720 int curr;
721 int ret;
722 u8 bcictl1;
723
724 curr = twl4030bci_read_adc_val(TWL4030_BCIICHG);
725 if (curr < 0)
726 return curr;
727
728 ret = twl4030_bci_read(TWL4030_BCICTL1, &bcictl1);
729 if (ret)
730 return ret;
731
NeilBrown3b542f02015-07-30 10:11:24 +1000732 return regval2ua(curr, bcictl1 & TWL4030_CGAIN);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300733}
734
735/*
736 * Returns the main charge FSM state
737 * Or < 0 on failure.
738 */
739static int twl4030bci_state(struct twl4030_bci *bci)
740{
741 int ret;
742 u8 state;
743
744 ret = twl4030_bci_read(TWL4030_BCIMSTATEC, &state);
745 if (ret) {
746 pr_err("twl4030_bci: error reading BCIMSTATEC\n");
747 return ret;
748 }
749
750 dev_dbg(bci->dev, "state: %02x\n", state);
751
752 return state;
753}
754
755static int twl4030_bci_state_to_status(int state)
756{
757 state &= TWL4030_MSTATEC_MASK;
758 if (TWL4030_MSTATEC_QUICK1 <= state && state <= TWL4030_MSTATEC_QUICK7)
759 return POWER_SUPPLY_STATUS_CHARGING;
760 else if (TWL4030_MSTATEC_COMPLETE1 <= state &&
761 state <= TWL4030_MSTATEC_COMPLETE4)
762 return POWER_SUPPLY_STATUS_FULL;
763 else
764 return POWER_SUPPLY_STATUS_NOT_CHARGING;
765}
766
767static int twl4030_bci_get_property(struct power_supply *psy,
768 enum power_supply_property psp,
769 union power_supply_propval *val)
770{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100771 struct twl4030_bci *bci = dev_get_drvdata(psy->dev.parent);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300772 int is_charging;
773 int state;
774 int ret;
775
776 state = twl4030bci_state(bci);
777 if (state < 0)
778 return state;
779
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100780 if (psy->desc->type == POWER_SUPPLY_TYPE_USB)
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300781 is_charging = state & TWL4030_MSTATEC_USB;
782 else
783 is_charging = state & TWL4030_MSTATEC_AC;
NeilBrown7f4a6332015-07-30 10:11:24 +1000784 if (!is_charging) {
785 u8 s;
786 twl4030_bci_read(TWL4030_BCIMDEN, &s);
787 if (psy->desc->type == POWER_SUPPLY_TYPE_USB)
788 is_charging = s & 1;
789 else
790 is_charging = s & 2;
791 if (is_charging)
792 /* A little white lie */
793 state = TWL4030_MSTATEC_QUICK1;
794 }
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300795
796 switch (psp) {
797 case POWER_SUPPLY_PROP_STATUS:
798 if (is_charging)
799 val->intval = twl4030_bci_state_to_status(state);
800 else
801 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
802 break;
803 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
804 /* charging must be active for meaningful result */
805 if (!is_charging)
806 return -ENODATA;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100807 if (psy->desc->type == POWER_SUPPLY_TYPE_USB) {
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300808 ret = twl4030bci_read_adc_val(TWL4030_BCIVBUS);
809 if (ret < 0)
810 return ret;
811 /* BCIVBUS uses ADCIN8, 7/1023 V/step */
812 val->intval = ret * 6843;
813 } else {
814 ret = twl4030bci_read_adc_val(TWL4030_BCIVAC);
815 if (ret < 0)
816 return ret;
817 /* BCIVAC uses ADCIN11, 10/1023 V/step */
818 val->intval = ret * 9775;
819 }
820 break;
821 case POWER_SUPPLY_PROP_CURRENT_NOW:
822 if (!is_charging)
823 return -ENODATA;
824 /* current measurement is shared between AC and USB */
825 ret = twl4030_charger_get_current();
826 if (ret < 0)
827 return ret;
828 val->intval = ret;
829 break;
830 case POWER_SUPPLY_PROP_ONLINE:
831 val->intval = is_charging &&
832 twl4030_bci_state_to_status(state) !=
833 POWER_SUPPLY_STATUS_NOT_CHARGING;
834 break;
H. Nikolaus Schaller3fb319c2017-04-14 20:25:56 +0200835 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
836 val->intval = -1;
837 if (psy->desc->type != POWER_SUPPLY_TYPE_USB) {
838 if (!bci->ac_is_active)
839 val->intval = bci->ac_cur;
840 } else {
841 if (bci->ac_is_active)
842 val->intval = bci->usb_cur_target;
843 }
844 if (val->intval < 0) {
845 u8 bcictl1;
846
847 val->intval = twl4030bci_read_adc_val(TWL4030_BCIIREF1);
848 if (val->intval < 0)
849 return val->intval;
850 ret = twl4030_bci_read(TWL4030_BCICTL1, &bcictl1);
851 if (ret < 0)
852 return ret;
853 val->intval = regval2ua(val->intval, bcictl1 &
854 TWL4030_CGAIN);
855 }
856 break;
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300857 default:
858 return -EINVAL;
859 }
860
861 return 0;
862}
863
H. Nikolaus Schaller3fb319c2017-04-14 20:25:56 +0200864static int twl4030_bci_set_property(struct power_supply *psy,
865 enum power_supply_property psp,
866 const union power_supply_propval *val)
867{
868 struct twl4030_bci *bci = dev_get_drvdata(psy->dev.parent);
869
870 switch (psp) {
871 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
872 if (psy->desc->type == POWER_SUPPLY_TYPE_USB)
873 bci->usb_cur_target = val->intval;
874 else
875 bci->ac_cur = val->intval;
876 twl4030_charger_update_current(bci);
877 break;
878 default:
879 return -EINVAL;
880 }
881
882 return 0;
883}
884
Sebastian Reichel50730eb2017-05-03 11:33:57 +0100885static int twl4030_bci_property_is_writeable(struct power_supply *psy,
H. Nikolaus Schaller3fb319c2017-04-14 20:25:56 +0200886 enum power_supply_property psp)
887{
888 switch (psp) {
889 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
890 return true;
891 default:
892 return false;
893 }
894}
895
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300896static enum power_supply_property twl4030_charger_props[] = {
897 POWER_SUPPLY_PROP_STATUS,
898 POWER_SUPPLY_PROP_ONLINE,
899 POWER_SUPPLY_PROP_VOLTAGE_NOW,
900 POWER_SUPPLY_PROP_CURRENT_NOW,
H. Nikolaus Schaller3fb319c2017-04-14 20:25:56 +0200901 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300902};
903
NeilBrownec0b3802013-10-31 17:05:50 +1100904#ifdef CONFIG_OF
905static const struct twl4030_bci_platform_data *
906twl4030_bci_parse_dt(struct device *dev)
907{
908 struct device_node *np = dev->of_node;
909 struct twl4030_bci_platform_data *pdata;
910 u32 num;
911
912 if (!np)
913 return NULL;
914 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
915 if (!pdata)
916 return pdata;
917
918 if (of_property_read_u32(np, "ti,bb-uvolt", &num) == 0)
919 pdata->bb_uvolt = num;
920 if (of_property_read_u32(np, "ti,bb-uamp", &num) == 0)
921 pdata->bb_uamp = num;
922 return pdata;
923}
924#else
925static inline const struct twl4030_bci_platform_data *
926twl4030_bci_parse_dt(struct device *dev)
927{
928 return NULL;
929}
930#endif
931
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100932static const struct power_supply_desc twl4030_bci_ac_desc = {
933 .name = "twl4030_ac",
934 .type = POWER_SUPPLY_TYPE_MAINS,
935 .properties = twl4030_charger_props,
936 .num_properties = ARRAY_SIZE(twl4030_charger_props),
937 .get_property = twl4030_bci_get_property,
H. Nikolaus Schaller3fb319c2017-04-14 20:25:56 +0200938 .set_property = twl4030_bci_set_property,
939 .property_is_writeable = twl4030_bci_property_is_writeable,
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100940};
941
942static const struct power_supply_desc twl4030_bci_usb_desc = {
943 .name = "twl4030_usb",
944 .type = POWER_SUPPLY_TYPE_USB,
945 .properties = twl4030_charger_props,
946 .num_properties = ARRAY_SIZE(twl4030_charger_props),
947 .get_property = twl4030_bci_get_property,
H. Nikolaus Schaller3fb319c2017-04-14 20:25:56 +0200948 .set_property = twl4030_bci_set_property,
949 .property_is_writeable = twl4030_bci_property_is_writeable,
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100950};
951
Pavel Machek7396f702015-07-30 10:11:24 +1000952static int twl4030_bci_probe(struct platform_device *pdev)
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300953{
954 struct twl4030_bci *bci;
NeilBrownec0b3802013-10-31 17:05:50 +1100955 const struct twl4030_bci_platform_data *pdata = pdev->dev.platform_data;
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300956 int ret;
Grazvydas Ignotas28d48f02011-06-03 14:43:11 +0300957 u32 reg;
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300958
NeilBrown325b50a2015-03-23 10:20:28 +1100959 bci = devm_kzalloc(&pdev->dev, sizeof(*bci), GFP_KERNEL);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300960 if (bci == NULL)
961 return -ENOMEM;
962
NeilBrownec0b3802013-10-31 17:05:50 +1100963 if (!pdata)
964 pdata = twl4030_bci_parse_dt(&pdev->dev);
965
NeilBrown1098cb52015-07-30 10:11:24 +1000966 bci->ichg_eoc = 80100; /* Stop charging when current drops to here */
967 bci->ichg_lo = 241000; /* Low threshold */
968 bci->ichg_hi = 500000; /* High threshold */
NeilBrowne4ae5372015-07-30 10:11:24 +1000969 bci->ac_cur = 500000; /* 500mA */
NeilBrown1098cb52015-07-30 10:11:24 +1000970 if (allow_usb)
NeilBrown21ae4042015-07-30 10:11:24 +1000971 bci->usb_cur_target = 500000; /* 500mA */
NeilBrown1098cb52015-07-30 10:11:24 +1000972 else
NeilBrown21ae4042015-07-30 10:11:24 +1000973 bci->usb_cur_target = 100000; /* 100mA */
NeilBrown22d4c332015-07-30 10:11:24 +1000974 bci->usb_mode = CHARGE_AUTO;
NeilBrownb04b9082015-07-30 10:11:24 +1000975 bci->ac_mode = CHARGE_AUTO;
NeilBrown1098cb52015-07-30 10:11:24 +1000976
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300977 bci->dev = &pdev->dev;
978 bci->irq_chg = platform_get_irq(pdev, 0);
979 bci->irq_bci = platform_get_irq(pdev, 1);
980
Nishanth Menon61a77842014-05-28 16:46:49 -0500981 platform_set_drvdata(pdev, bci);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +0300982
H. Nikolaus Schaller3950c952017-07-03 18:37:10 +0200983 INIT_WORK(&bci->work, twl4030_bci_usb_work);
984 INIT_DELAYED_WORK(&bci->current_worker, twl4030_current_worker);
985
H. Nikolaus Schaller5e6eb022017-06-14 11:25:54 +0200986 bci->channel_vac = devm_iio_channel_get(&pdev->dev, "vac");
987 if (IS_ERR(bci->channel_vac)) {
H. Nikolaus Schaller7bb5a652017-07-03 18:37:11 +0200988 ret = PTR_ERR(bci->channel_vac);
989 if (ret == -EPROBE_DEFER)
990 return ret; /* iio not ready */
991 dev_warn(&pdev->dev, "could not request vac iio channel (%d)",
992 ret);
H. Nikolaus Schaller5e6eb022017-06-14 11:25:54 +0200993 bci->channel_vac = NULL;
H. Nikolaus Schaller5e6eb022017-06-14 11:25:54 +0200994 }
995
H. Nikolaus Schaller3950c952017-07-03 18:37:10 +0200996 if (bci->dev->of_node) {
997 struct device_node *phynode;
998
999 phynode = of_find_compatible_node(bci->dev->of_node->parent,
1000 NULL, "ti,twl4030-usb");
1001 if (phynode) {
1002 bci->usb_nb.notifier_call = twl4030_bci_usb_ncb;
1003 bci->transceiver = devm_usb_get_phy_by_node(
1004 bci->dev, phynode, &bci->usb_nb);
H. Nikolaus Schaller7bb5a652017-07-03 18:37:11 +02001005 if (IS_ERR(bci->transceiver)) {
1006 ret = PTR_ERR(bci->transceiver);
1007 if (ret == -EPROBE_DEFER)
1008 return ret; /* phy not ready */
1009 dev_warn(&pdev->dev, "could not request transceiver (%d)",
1010 ret);
1011 bci->transceiver = NULL;
1012 }
H. Nikolaus Schaller3950c952017-07-03 18:37:10 +02001013 }
1014 }
1015
NeilBrown325b50a2015-03-23 10:20:28 +11001016 bci->ac = devm_power_supply_register(&pdev->dev, &twl4030_bci_ac_desc,
1017 NULL);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001018 if (IS_ERR(bci->ac)) {
1019 ret = PTR_ERR(bci->ac);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +03001020 dev_err(&pdev->dev, "failed to register ac: %d\n", ret);
NeilBrown325b50a2015-03-23 10:20:28 +11001021 return ret;
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +03001022 }
1023
NeilBrown325b50a2015-03-23 10:20:28 +11001024 bci->usb = devm_power_supply_register(&pdev->dev, &twl4030_bci_usb_desc,
1025 NULL);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001026 if (IS_ERR(bci->usb)) {
1027 ret = PTR_ERR(bci->usb);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +03001028 dev_err(&pdev->dev, "failed to register usb: %d\n", ret);
NeilBrown325b50a2015-03-23 10:20:28 +11001029 return ret;
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +03001030 }
1031
NeilBrown1b7cac22015-03-23 10:20:28 +11001032 ret = devm_request_threaded_irq(&pdev->dev, bci->irq_chg, NULL,
Fengguang Wua2778b02012-08-23 19:56:43 +08001033 twl4030_charger_interrupt, IRQF_ONESHOT, pdev->name,
1034 bci);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +03001035 if (ret < 0) {
1036 dev_err(&pdev->dev, "could not request irq %d, status %d\n",
1037 bci->irq_chg, ret);
NeilBrown325b50a2015-03-23 10:20:28 +11001038 return ret;
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +03001039 }
1040
NeilBrown1b7cac22015-03-23 10:20:28 +11001041 ret = devm_request_threaded_irq(&pdev->dev, bci->irq_bci, NULL,
Fengguang Wua2778b02012-08-23 19:56:43 +08001042 twl4030_bci_interrupt, IRQF_ONESHOT, pdev->name, bci);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +03001043 if (ret < 0) {
1044 dev_err(&pdev->dev, "could not request irq %d, status %d\n",
1045 bci->irq_bci, ret);
NeilBrown325b50a2015-03-23 10:20:28 +11001046 return ret;
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +03001047 }
1048
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +03001049 /* Enable interrupts now. */
Grazvydas Ignotas28d48f02011-06-03 14:43:11 +03001050 reg = ~(u32)(TWL4030_ICHGLOW | TWL4030_ICHGEOC | TWL4030_TBATOR2 |
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +03001051 TWL4030_TBATOR1 | TWL4030_BATSTS);
1052 ret = twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS, reg,
1053 TWL4030_INTERRUPTS_BCIIMR1A);
1054 if (ret < 0) {
1055 dev_err(&pdev->dev, "failed to unmask interrupts: %d\n", ret);
H. Nikolaus Schallere8847c52017-06-14 11:25:53 +02001056 return ret;
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +03001057 }
1058
Grazvydas Ignotas28d48f02011-06-03 14:43:11 +03001059 reg = ~(u32)(TWL4030_VBATOV | TWL4030_VBUSOV | TWL4030_ACCHGOV);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +03001060 ret = twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS, reg,
1061 TWL4030_INTERRUPTS_BCIIMR2A);
1062 if (ret < 0)
1063 dev_warn(&pdev->dev, "failed to unmask interrupts: %d\n", ret);
1064
NeilBrown1098cb52015-07-30 10:11:24 +10001065 twl4030_charger_update_current(bci);
NeilBrown22d4c332015-07-30 10:11:24 +10001066 if (device_create_file(&bci->usb->dev, &dev_attr_mode))
1067 dev_warn(&pdev->dev, "could not create sysfs file\n");
NeilBrownb04b9082015-07-30 10:11:24 +10001068 if (device_create_file(&bci->ac->dev, &dev_attr_mode))
1069 dev_warn(&pdev->dev, "could not create sysfs file\n");
NeilBrownaca3c352015-07-30 10:11:24 +10001070
NeilBrownb04b9082015-07-30 10:11:24 +10001071 twl4030_charger_enable_ac(bci, true);
NeilBrown6e37ec8c2015-07-30 10:11:24 +10001072 if (!IS_ERR_OR_NULL(bci->transceiver))
1073 twl4030_bci_usb_ncb(&bci->usb_nb,
1074 bci->transceiver->last_event,
1075 NULL);
1076 else
1077 twl4030_charger_enable_usb(bci, false);
NeilBrownec0b3802013-10-31 17:05:50 +11001078 if (pdata)
1079 twl4030_charger_enable_backup(pdata->bb_uvolt,
1080 pdata->bb_uamp);
1081 else
1082 twl4030_charger_enable_backup(0, 0);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +03001083
1084 return 0;
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +03001085}
1086
Dmitry Torokhov420b2d42017-03-01 15:44:53 -08001087static int twl4030_bci_remove(struct platform_device *pdev)
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +03001088{
1089 struct twl4030_bci *bci = platform_get_drvdata(pdev);
1090
NeilBrownb04b9082015-07-30 10:11:24 +10001091 twl4030_charger_enable_ac(bci, false);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +03001092 twl4030_charger_enable_usb(bci, false);
NeilBrown210d4bc2012-05-09 07:40:40 +10001093 twl4030_charger_enable_backup(0, 0);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +03001094
NeilBrown22d4c332015-07-30 10:11:24 +10001095 device_remove_file(&bci->usb->dev, &dev_attr_mode);
NeilBrownb04b9082015-07-30 10:11:24 +10001096 device_remove_file(&bci->ac->dev, &dev_attr_mode);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +03001097 /* mask interrupts */
1098 twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS, 0xff,
1099 TWL4030_INTERRUPTS_BCIIMR1A);
1100 twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS, 0xff,
1101 TWL4030_INTERRUPTS_BCIIMR2A);
1102
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +03001103 return 0;
1104}
1105
NeilBrownec0b3802013-10-31 17:05:50 +11001106static const struct of_device_id twl_bci_of_match[] = {
1107 {.compatible = "ti,twl4030-bci", },
1108 { }
1109};
1110MODULE_DEVICE_TABLE(of, twl_bci_of_match);
1111
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +03001112static struct platform_driver twl4030_bci_driver = {
Pavel Machek7396f702015-07-30 10:11:24 +10001113 .probe = twl4030_bci_probe,
Dmitry Torokhov420b2d42017-03-01 15:44:53 -08001114 .remove = twl4030_bci_remove,
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +03001115 .driver = {
1116 .name = "twl4030_bci",
NeilBrownec0b3802013-10-31 17:05:50 +11001117 .of_match_table = of_match_ptr(twl_bci_of_match),
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +03001118 },
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +03001119};
Pavel Machek7396f702015-07-30 10:11:24 +10001120module_platform_driver(twl4030_bci_driver);
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +03001121
Grazvydas Ignotas28d48f02011-06-03 14:43:11 +03001122MODULE_AUTHOR("Gražvydas Ignotas");
Grazvydas Ignotas2e727f12010-09-27 23:18:24 +03001123MODULE_DESCRIPTION("TWL4030 Battery Charger Interface driver");
1124MODULE_LICENSE("GPL");
1125MODULE_ALIAS("platform:twl4030_bci");