]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/power/bq24257_charger.c
power: bq24257: Allow input current limit sysfs access
[mirror_ubuntu-bionic-kernel.git] / drivers / power / bq24257_charger.c
CommitLineData
2219a935
LP
1/*
2 * TI BQ24257 charger driver
3 *
4 * Copyright (C) 2015 Intel Corporation
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
bf02dca9
AD
16 * Datasheets:
17 * http://www.ti.com/product/bq24250
18 * http://www.ti.com/product/bq24251
19 * http://www.ti.com/product/bq24257
2219a935
LP
20 */
21
22#include <linux/module.h>
23#include <linux/i2c.h>
24#include <linux/power_supply.h>
25#include <linux/regmap.h>
26#include <linux/types.h>
27#include <linux/gpio/consumer.h>
28#include <linux/interrupt.h>
29#include <linux/delay.h>
30
31#include <linux/acpi.h>
32#include <linux/of.h>
33
34#define BQ24257_REG_1 0x00
35#define BQ24257_REG_2 0x01
36#define BQ24257_REG_3 0x02
37#define BQ24257_REG_4 0x03
38#define BQ24257_REG_5 0x04
39#define BQ24257_REG_6 0x05
40#define BQ24257_REG_7 0x06
41
42#define BQ24257_MANUFACTURER "Texas Instruments"
2219a935
LP
43#define BQ24257_PG_GPIO "pg"
44
45#define BQ24257_ILIM_SET_DELAY 1000 /* msec */
46
bf02dca9
AD
47/*
48 * When adding support for new devices make sure that enum bq2425x_chip and
49 * bq2425x_chip_name[] always stay in sync!
50 */
51enum bq2425x_chip {
52 BQ24250,
53 BQ24251,
54 BQ24257,
55};
56
57static const char *const bq2425x_chip_name[] = {
58 "bq24250",
59 "bq24251",
60 "bq24257",
61};
62
2219a935
LP
63enum bq24257_fields {
64 F_WD_FAULT, F_WD_EN, F_STAT, F_FAULT, /* REG 1 */
65 F_RESET, F_IILIMIT, F_EN_STAT, F_EN_TERM, F_CE, F_HZ_MODE, /* REG 2 */
66 F_VBAT, F_USB_DET, /* REG 3 */
67 F_ICHG, F_ITERM, /* REG 4 */
68 F_LOOP_STATUS, F_LOW_CHG, F_DPDM_EN, F_CE_STATUS, F_VINDPM, /* REG 5 */
7ef62365 69 F_X2_TMR_EN, F_TMR, F_SYSOFF, F_TS_EN, F_TS_STAT, /* REG 6 */
2219a935
LP
70 F_VOVP, F_CLR_VDP, F_FORCE_BATDET, F_FORCE_PTM, /* REG 7 */
71
72 F_MAX_FIELDS
73};
74
75/* initial field values, converted from uV/uA */
76struct bq24257_init_data {
77 u8 ichg; /* charge current */
78 u8 vbat; /* regulation voltage */
79 u8 iterm; /* termination current */
eb9fbcc6 80 u8 iilimit; /* input current limit */
bb2956e8 81 u8 vovp; /* over voltage protection voltage */
138606ff 82 u8 vindpm; /* VDMP input threshold voltage */
2219a935
LP
83};
84
85struct bq24257_state {
86 u8 status;
87 u8 fault;
88 bool power_good;
89};
90
91struct bq24257_device {
92 struct i2c_client *client;
93 struct device *dev;
94 struct power_supply *charger;
95
bf02dca9
AD
96 enum bq2425x_chip chip;
97
2219a935
LP
98 struct regmap *rmap;
99 struct regmap_field *rmap_fields[F_MAX_FIELDS];
100
101 struct gpio_desc *pg;
102
103 struct delayed_work iilimit_setup_work;
104
105 struct bq24257_init_data init_data;
106 struct bq24257_state state;
107
108 struct mutex lock; /* protect state data */
eb9fbcc6
AD
109
110 bool iilimit_autoset_enable;
2219a935
LP
111};
112
113static bool bq24257_is_volatile_reg(struct device *dev, unsigned int reg)
114{
115 switch (reg) {
116 case BQ24257_REG_2:
117 case BQ24257_REG_4:
118 return false;
119
120 default:
121 return true;
122 }
123}
124
125static const struct regmap_config bq24257_regmap_config = {
126 .reg_bits = 8,
127 .val_bits = 8,
128
129 .max_register = BQ24257_REG_7,
130 .cache_type = REGCACHE_RBTREE,
131
132 .volatile_reg = bq24257_is_volatile_reg,
133};
134
135static const struct reg_field bq24257_reg_fields[] = {
136 /* REG 1 */
137 [F_WD_FAULT] = REG_FIELD(BQ24257_REG_1, 7, 7),
138 [F_WD_EN] = REG_FIELD(BQ24257_REG_1, 6, 6),
139 [F_STAT] = REG_FIELD(BQ24257_REG_1, 4, 5),
140 [F_FAULT] = REG_FIELD(BQ24257_REG_1, 0, 3),
141 /* REG 2 */
142 [F_RESET] = REG_FIELD(BQ24257_REG_2, 7, 7),
143 [F_IILIMIT] = REG_FIELD(BQ24257_REG_2, 4, 6),
144 [F_EN_STAT] = REG_FIELD(BQ24257_REG_2, 3, 3),
145 [F_EN_TERM] = REG_FIELD(BQ24257_REG_2, 2, 2),
146 [F_CE] = REG_FIELD(BQ24257_REG_2, 1, 1),
147 [F_HZ_MODE] = REG_FIELD(BQ24257_REG_2, 0, 0),
148 /* REG 3 */
149 [F_VBAT] = REG_FIELD(BQ24257_REG_3, 2, 7),
150 [F_USB_DET] = REG_FIELD(BQ24257_REG_3, 0, 1),
151 /* REG 4 */
152 [F_ICHG] = REG_FIELD(BQ24257_REG_4, 3, 7),
153 [F_ITERM] = REG_FIELD(BQ24257_REG_4, 0, 2),
154 /* REG 5 */
155 [F_LOOP_STATUS] = REG_FIELD(BQ24257_REG_5, 6, 7),
156 [F_LOW_CHG] = REG_FIELD(BQ24257_REG_5, 5, 5),
157 [F_DPDM_EN] = REG_FIELD(BQ24257_REG_5, 4, 4),
158 [F_CE_STATUS] = REG_FIELD(BQ24257_REG_5, 3, 3),
159 [F_VINDPM] = REG_FIELD(BQ24257_REG_5, 0, 2),
160 /* REG 6 */
161 [F_X2_TMR_EN] = REG_FIELD(BQ24257_REG_6, 7, 7),
162 [F_TMR] = REG_FIELD(BQ24257_REG_6, 5, 6),
163 [F_SYSOFF] = REG_FIELD(BQ24257_REG_6, 4, 4),
7ef62365 164 [F_TS_EN] = REG_FIELD(BQ24257_REG_6, 3, 3),
2219a935
LP
165 [F_TS_STAT] = REG_FIELD(BQ24257_REG_6, 0, 2),
166 /* REG 7 */
167 [F_VOVP] = REG_FIELD(BQ24257_REG_7, 5, 7),
168 [F_CLR_VDP] = REG_FIELD(BQ24257_REG_7, 4, 4),
169 [F_FORCE_BATDET] = REG_FIELD(BQ24257_REG_7, 3, 3),
170 [F_FORCE_PTM] = REG_FIELD(BQ24257_REG_7, 2, 2)
171};
172
173static const u32 bq24257_vbat_map[] = {
174 3500000, 3520000, 3540000, 3560000, 3580000, 3600000, 3620000, 3640000,
175 3660000, 3680000, 3700000, 3720000, 3740000, 3760000, 3780000, 3800000,
176 3820000, 3840000, 3860000, 3880000, 3900000, 3920000, 3940000, 3960000,
177 3980000, 4000000, 4020000, 4040000, 4060000, 4080000, 4100000, 4120000,
178 4140000, 4160000, 4180000, 4200000, 4220000, 4240000, 4260000, 4280000,
179 4300000, 4320000, 4340000, 4360000, 4380000, 4400000, 4420000, 4440000
180};
181
182#define BQ24257_VBAT_MAP_SIZE ARRAY_SIZE(bq24257_vbat_map)
183
184static const u32 bq24257_ichg_map[] = {
185 500000, 550000, 600000, 650000, 700000, 750000, 800000, 850000, 900000,
186 950000, 1000000, 1050000, 1100000, 1150000, 1200000, 1250000, 1300000,
187 1350000, 1400000, 1450000, 1500000, 1550000, 1600000, 1650000, 1700000,
188 1750000, 1800000, 1850000, 1900000, 1950000, 2000000
189};
190
191#define BQ24257_ICHG_MAP_SIZE ARRAY_SIZE(bq24257_ichg_map)
192
193static const u32 bq24257_iterm_map[] = {
194 50000, 75000, 100000, 125000, 150000, 175000, 200000, 225000
195};
196
197#define BQ24257_ITERM_MAP_SIZE ARRAY_SIZE(bq24257_iterm_map)
198
eb9fbcc6
AD
199static const u32 bq24257_iilimit_map[] = {
200 100000, 150000, 500000, 900000, 1500000, 2000000
201};
202
203#define BQ24257_IILIMIT_MAP_SIZE ARRAY_SIZE(bq24257_iilimit_map)
204
bb2956e8
AD
205static const u32 bq24257_vovp_map[] = {
206 6000000, 6500000, 7000000, 8000000, 9000000, 9500000, 10000000,
207 10500000
208};
209
210#define BQ24257_VOVP_MAP_SIZE ARRAY_SIZE(bq24257_vovp_map)
211
138606ff
AD
212static const u32 bq24257_vindpm_map[] = {
213 4200000, 4280000, 4360000, 4440000, 4520000, 4600000, 4680000,
214 4760000
215};
216
217#define BQ24257_VINDPM_MAP_SIZE ARRAY_SIZE(bq24257_vindpm_map)
218
2219a935
LP
219static int bq24257_field_read(struct bq24257_device *bq,
220 enum bq24257_fields field_id)
221{
222 int ret;
223 int val;
224
225 ret = regmap_field_read(bq->rmap_fields[field_id], &val);
226 if (ret < 0)
227 return ret;
228
229 return val;
230}
231
232static int bq24257_field_write(struct bq24257_device *bq,
233 enum bq24257_fields field_id, u8 val)
234{
235 return regmap_field_write(bq->rmap_fields[field_id], val);
236}
237
238static u8 bq24257_find_idx(u32 value, const u32 *map, u8 map_size)
239{
240 u8 idx;
241
242 for (idx = 1; idx < map_size; idx++)
243 if (value < map[idx])
244 break;
245
246 return idx - 1;
247}
248
249enum bq24257_status {
250 STATUS_READY,
251 STATUS_CHARGE_IN_PROGRESS,
252 STATUS_CHARGE_DONE,
253 STATUS_FAULT,
254};
255
256enum bq24257_fault {
257 FAULT_NORMAL,
258 FAULT_INPUT_OVP,
259 FAULT_INPUT_UVLO,
260 FAULT_SLEEP,
261 FAULT_BAT_TS,
262 FAULT_BAT_OVP,
263 FAULT_TS,
264 FAULT_TIMER,
265 FAULT_NO_BAT,
266 FAULT_ISET,
267 FAULT_INPUT_LDO_LOW,
268};
269
0cfbfde6
AD
270static int bq24257_get_input_current_limit(struct bq24257_device *bq,
271 union power_supply_propval *val)
272{
273 int ret;
274
275 ret = bq24257_field_read(bq, F_IILIMIT);
276 if (ret < 0)
277 return ret;
278
279 /*
280 * The "External ILIM" and "Production & Test" modes are not exposed
281 * through this driver and not being covered by the lookup table.
282 * Should such a mode have become active let's return an error rather
283 * than exceeding the bounds of the lookup table and returning
284 * garbage.
285 */
286 if (ret >= BQ24257_IILIMIT_MAP_SIZE)
287 return -ENODATA;
288
289 val->intval = bq24257_iilimit_map[ret];
290
291 return 0;
292}
293
294static int bq24257_set_input_current_limit(struct bq24257_device *bq,
295 const union power_supply_propval *val)
296{
297 /*
298 * Address the case where the user manually sets an input current limit
299 * while the charger auto-detection mechanism is is active. In this
300 * case we want to abort and go straight to the user-specified value.
301 */
302 if (bq->iilimit_autoset_enable)
303 cancel_delayed_work_sync(&bq->iilimit_setup_work);
304
305 return bq24257_field_write(bq, F_IILIMIT,
306 bq24257_find_idx(val->intval,
307 bq24257_iilimit_map,
308 BQ24257_IILIMIT_MAP_SIZE));
309}
310
2219a935
LP
311static int bq24257_power_supply_get_property(struct power_supply *psy,
312 enum power_supply_property psp,
313 union power_supply_propval *val)
314{
315 struct bq24257_device *bq = power_supply_get_drvdata(psy);
316 struct bq24257_state state;
317
318 mutex_lock(&bq->lock);
319 state = bq->state;
320 mutex_unlock(&bq->lock);
321
322 switch (psp) {
323 case POWER_SUPPLY_PROP_STATUS:
324 if (!state.power_good)
325 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
326 else if (state.status == STATUS_READY)
327 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
328 else if (state.status == STATUS_CHARGE_IN_PROGRESS)
329 val->intval = POWER_SUPPLY_STATUS_CHARGING;
330 else if (state.status == STATUS_CHARGE_DONE)
331 val->intval = POWER_SUPPLY_STATUS_FULL;
332 else
333 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
334 break;
335
336 case POWER_SUPPLY_PROP_MANUFACTURER:
337 val->strval = BQ24257_MANUFACTURER;
338 break;
339
bf02dca9
AD
340 case POWER_SUPPLY_PROP_MODEL_NAME:
341 val->strval = bq2425x_chip_name[bq->chip];
342 break;
343
2219a935
LP
344 case POWER_SUPPLY_PROP_ONLINE:
345 val->intval = state.power_good;
346 break;
347
348 case POWER_SUPPLY_PROP_HEALTH:
349 switch (state.fault) {
350 case FAULT_NORMAL:
351 val->intval = POWER_SUPPLY_HEALTH_GOOD;
352 break;
353
354 case FAULT_INPUT_OVP:
355 case FAULT_BAT_OVP:
356 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
357 break;
358
359 case FAULT_TS:
360 case FAULT_BAT_TS:
361 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
362 break;
363
364 case FAULT_TIMER:
365 val->intval = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
366 break;
367
368 default:
369 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
370 break;
371 }
372
373 break;
374
375 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
376 val->intval = bq24257_ichg_map[bq->init_data.ichg];
377 break;
378
379 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
380 val->intval = bq24257_ichg_map[BQ24257_ICHG_MAP_SIZE - 1];
381 break;
382
383 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
384 val->intval = bq24257_vbat_map[bq->init_data.vbat];
385 break;
386
387 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
388 val->intval = bq24257_vbat_map[BQ24257_VBAT_MAP_SIZE - 1];
389 break;
390
391 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
392 val->intval = bq24257_iterm_map[bq->init_data.iterm];
393 break;
394
0cfbfde6
AD
395 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
396 return bq24257_get_input_current_limit(bq, val);
397
2219a935
LP
398 default:
399 return -EINVAL;
400 }
401
402 return 0;
403}
404
0cfbfde6
AD
405static int bq24257_power_supply_set_property(struct power_supply *psy,
406 enum power_supply_property prop,
407 const union power_supply_propval *val)
408{
409 struct bq24257_device *bq = power_supply_get_drvdata(psy);
410
411 switch (prop) {
412 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
413 return bq24257_set_input_current_limit(bq, val);
414 default:
415 return -EINVAL;
416 }
417}
418
419static int bq24257_power_supply_property_is_writeable(struct power_supply *psy,
420 enum power_supply_property psp)
421{
422 switch (psp) {
423 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
424 return true;
425 default:
426 return false;
427 }
428}
429
2219a935
LP
430static int bq24257_get_chip_state(struct bq24257_device *bq,
431 struct bq24257_state *state)
432{
433 int ret;
434
435 ret = bq24257_field_read(bq, F_STAT);
436 if (ret < 0)
437 return ret;
438
439 state->status = ret;
440
441 ret = bq24257_field_read(bq, F_FAULT);
442 if (ret < 0)
443 return ret;
444
445 state->fault = ret;
446
7c071a0a
AD
447 if (bq->pg)
448 state->power_good = !gpiod_get_value_cansleep(bq->pg);
449 else
450 /*
451 * If we have a chip without a dedicated power-good GPIO or
452 * some other explicit bit that would provide this information
453 * assume the power is good if there is no supply related
454 * fault - and not good otherwise. There is a possibility for
455 * other errors to mask that power in fact is not good but this
456 * is probably the best we can do here.
457 */
458 switch (state->fault) {
459 case FAULT_INPUT_OVP:
460 case FAULT_INPUT_UVLO:
461 case FAULT_INPUT_LDO_LOW:
462 state->power_good = false;
463 break;
464 default:
465 state->power_good = true;
466 }
2219a935
LP
467
468 return 0;
469}
470
471static bool bq24257_state_changed(struct bq24257_device *bq,
472 struct bq24257_state *new_state)
473{
474 int ret;
475
476 mutex_lock(&bq->lock);
477 ret = (bq->state.status != new_state->status ||
478 bq->state.fault != new_state->fault ||
479 bq->state.power_good != new_state->power_good);
480 mutex_unlock(&bq->lock);
481
482 return ret;
483}
484
485enum bq24257_loop_status {
486 LOOP_STATUS_NONE,
487 LOOP_STATUS_IN_DPM,
488 LOOP_STATUS_IN_CURRENT_LIMIT,
489 LOOP_STATUS_THERMAL,
490};
491
492enum bq24257_in_ilimit {
493 IILIMIT_100,
494 IILIMIT_150,
495 IILIMIT_500,
496 IILIMIT_900,
497 IILIMIT_1500,
498 IILIMIT_2000,
499 IILIMIT_EXT,
500 IILIMIT_NONE,
501};
502
bb2956e8
AD
503enum bq24257_vovp {
504 VOVP_6000,
505 VOVP_6500,
506 VOVP_7000,
507 VOVP_8000,
508 VOVP_9000,
509 VOVP_9500,
510 VOVP_10000,
511 VOVP_10500
512};
513
138606ff
AD
514enum bq24257_vindpm {
515 VINDPM_4200,
516 VINDPM_4280,
517 VINDPM_4360,
518 VINDPM_4440,
519 VINDPM_4520,
520 VINDPM_4600,
521 VINDPM_4680,
522 VINDPM_4760
523};
524
2219a935
LP
525enum bq24257_port_type {
526 PORT_TYPE_DCP, /* Dedicated Charging Port */
527 PORT_TYPE_CDP, /* Charging Downstream Port */
528 PORT_TYPE_SDP, /* Standard Downstream Port */
529 PORT_TYPE_NON_STANDARD,
530};
531
532enum bq24257_safety_timer {
533 SAFETY_TIMER_45,
534 SAFETY_TIMER_360,
535 SAFETY_TIMER_540,
536 SAFETY_TIMER_NONE,
537};
538
539static int bq24257_iilimit_autoset(struct bq24257_device *bq)
540{
541 int loop_status;
542 int iilimit;
543 int port_type;
544 int ret;
545 const u8 new_iilimit[] = {
546 [PORT_TYPE_DCP] = IILIMIT_2000,
547 [PORT_TYPE_CDP] = IILIMIT_2000,
548 [PORT_TYPE_SDP] = IILIMIT_500,
549 [PORT_TYPE_NON_STANDARD] = IILIMIT_500
550 };
551
552 ret = bq24257_field_read(bq, F_LOOP_STATUS);
553 if (ret < 0)
554 goto error;
555
556 loop_status = ret;
557
558 ret = bq24257_field_read(bq, F_IILIMIT);
559 if (ret < 0)
560 goto error;
561
562 iilimit = ret;
563
564 /*
565 * All USB ports should be able to handle 500mA. If not, DPM will lower
566 * the charging current to accommodate the power source. No need to set
567 * a lower IILIMIT value.
568 */
569 if (loop_status == LOOP_STATUS_IN_DPM && iilimit == IILIMIT_500)
570 return 0;
571
572 ret = bq24257_field_read(bq, F_USB_DET);
573 if (ret < 0)
574 goto error;
575
576 port_type = ret;
577
578 ret = bq24257_field_write(bq, F_IILIMIT, new_iilimit[port_type]);
579 if (ret < 0)
580 goto error;
581
582 ret = bq24257_field_write(bq, F_TMR, SAFETY_TIMER_360);
583 if (ret < 0)
584 goto error;
585
586 ret = bq24257_field_write(bq, F_CLR_VDP, 1);
587 if (ret < 0)
588 goto error;
589
590 dev_dbg(bq->dev, "port/loop = %d/%d -> iilimit = %d\n",
591 port_type, loop_status, new_iilimit[port_type]);
592
593 return 0;
594
595error:
596 dev_err(bq->dev, "%s: Error communicating with the chip.\n", __func__);
597 return ret;
598}
599
600static void bq24257_iilimit_setup_work(struct work_struct *work)
601{
602 struct bq24257_device *bq = container_of(work, struct bq24257_device,
603 iilimit_setup_work.work);
604
605 bq24257_iilimit_autoset(bq);
606}
607
608static void bq24257_handle_state_change(struct bq24257_device *bq,
609 struct bq24257_state *new_state)
610{
611 int ret;
612 struct bq24257_state old_state;
2219a935
LP
613
614 mutex_lock(&bq->lock);
615 old_state = bq->state;
616 mutex_unlock(&bq->lock);
617
eb9fbcc6
AD
618 /*
619 * Handle BQ2425x state changes observing whether the D+/D- based input
620 * current limit autoset functionality is enabled.
621 */
9b1cf1e4
AD
622 if (!new_state->power_good) {
623 dev_dbg(bq->dev, "Power removed\n");
eb9fbcc6
AD
624 if (bq->iilimit_autoset_enable) {
625 cancel_delayed_work_sync(&bq->iilimit_setup_work);
626
627 /* activate D+/D- port detection algorithm */
628 ret = bq24257_field_write(bq, F_DPDM_EN, 1);
629 if (ret < 0)
630 goto error;
eb9fbcc6 631 }
0cfbfde6
AD
632 /*
633 * When power is removed always return to the default input
634 * current limit as configured during probe.
635 */
636 ret = bq24257_field_write(bq, F_IILIMIT, bq->init_data.iilimit);
637 if (ret < 0)
638 goto error;
9b1cf1e4
AD
639 } else if (!old_state.power_good) {
640 dev_dbg(bq->dev, "Power inserted\n");
641
eb9fbcc6
AD
642 if (bq->iilimit_autoset_enable)
643 /* configure input current limit */
644 schedule_delayed_work(&bq->iilimit_setup_work,
2219a935 645 msecs_to_jiffies(BQ24257_ILIM_SET_DELAY));
9b1cf1e4
AD
646 } else if (new_state->fault == FAULT_NO_BAT) {
647 dev_warn(bq->dev, "Battery removed\n");
648 } else if (new_state->fault == FAULT_TIMER) {
649 dev_err(bq->dev, "Safety timer expired! Battery dead?\n");
2219a935
LP
650 }
651
652 return;
653
654error:
655 dev_err(bq->dev, "%s: Error communicating with the chip.\n", __func__);
656}
657
658static irqreturn_t bq24257_irq_handler_thread(int irq, void *private)
659{
660 int ret;
661 struct bq24257_device *bq = private;
662 struct bq24257_state state;
663
664 ret = bq24257_get_chip_state(bq, &state);
665 if (ret < 0)
666 return IRQ_HANDLED;
667
668 if (!bq24257_state_changed(bq, &state))
669 return IRQ_HANDLED;
670
671 dev_dbg(bq->dev, "irq(state changed): status/fault/pg = %d/%d/%d\n",
672 state.status, state.fault, state.power_good);
673
674 bq24257_handle_state_change(bq, &state);
675
676 mutex_lock(&bq->lock);
677 bq->state = state;
678 mutex_unlock(&bq->lock);
679
680 power_supply_changed(bq->charger);
681
682 return IRQ_HANDLED;
683}
684
685static int bq24257_hw_init(struct bq24257_device *bq)
686{
687 int ret;
688 int i;
689 struct bq24257_state state;
690
691 const struct {
692 int field;
693 u32 value;
694 } init_data[] = {
695 {F_ICHG, bq->init_data.ichg},
696 {F_VBAT, bq->init_data.vbat},
bb2956e8
AD
697 {F_ITERM, bq->init_data.iterm},
698 {F_VOVP, bq->init_data.vovp},
138606ff 699 {F_VINDPM, bq->init_data.vindpm},
2219a935
LP
700 };
701
702 /*
703 * Disable the watchdog timer to prevent the IC from going back to
704 * default settings after 50 seconds of I2C inactivity.
705 */
706 ret = bq24257_field_write(bq, F_WD_EN, 0);
707 if (ret < 0)
708 return ret;
709
710 /* configure the charge currents and voltages */
711 for (i = 0; i < ARRAY_SIZE(init_data); i++) {
712 ret = bq24257_field_write(bq, init_data[i].field,
713 init_data[i].value);
714 if (ret < 0)
715 return ret;
716 }
717
718 ret = bq24257_get_chip_state(bq, &state);
719 if (ret < 0)
720 return ret;
721
722 mutex_lock(&bq->lock);
723 bq->state = state;
724 mutex_unlock(&bq->lock);
725
eb9fbcc6
AD
726 if (!bq->iilimit_autoset_enable) {
727 dev_dbg(bq->dev, "manually setting iilimit = %u\n",
728 bq->init_data.iilimit);
729
730 /* program fixed input current limit */
731 ret = bq24257_field_write(bq, F_IILIMIT,
732 bq->init_data.iilimit);
733 if (ret < 0)
734 return ret;
735 } else if (!state.power_good)
2219a935
LP
736 /* activate D+/D- detection algorithm */
737 ret = bq24257_field_write(bq, F_DPDM_EN, 1);
738 else if (state.fault != FAULT_NO_BAT)
739 ret = bq24257_iilimit_autoset(bq);
740
741 return ret;
742}
743
744static enum power_supply_property bq24257_power_supply_props[] = {
745 POWER_SUPPLY_PROP_MANUFACTURER,
bf02dca9 746 POWER_SUPPLY_PROP_MODEL_NAME,
2219a935
LP
747 POWER_SUPPLY_PROP_STATUS,
748 POWER_SUPPLY_PROP_ONLINE,
749 POWER_SUPPLY_PROP_HEALTH,
750 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
751 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
752 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
753 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
754 POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
0cfbfde6 755 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
2219a935
LP
756};
757
758static char *bq24257_charger_supplied_to[] = {
759 "main-battery",
760};
761
762static const struct power_supply_desc bq24257_power_supply_desc = {
763 .name = "bq24257-charger",
764 .type = POWER_SUPPLY_TYPE_USB,
765 .properties = bq24257_power_supply_props,
766 .num_properties = ARRAY_SIZE(bq24257_power_supply_props),
767 .get_property = bq24257_power_supply_get_property,
0cfbfde6
AD
768 .set_property = bq24257_power_supply_set_property,
769 .property_is_writeable = bq24257_power_supply_property_is_writeable,
2219a935
LP
770};
771
bb2956e8
AD
772static ssize_t bq24257_show_ovp_voltage(struct device *dev,
773 struct device_attribute *attr,
774 char *buf)
775{
776 struct power_supply *psy = dev_get_drvdata(dev);
777 struct bq24257_device *bq = power_supply_get_drvdata(psy);
778
779 return scnprintf(buf, PAGE_SIZE, "%u\n",
780 bq24257_vovp_map[bq->init_data.vovp]);
781}
782
138606ff
AD
783static ssize_t bq24257_show_in_dpm_voltage(struct device *dev,
784 struct device_attribute *attr,
785 char *buf)
786{
787 struct power_supply *psy = dev_get_drvdata(dev);
788 struct bq24257_device *bq = power_supply_get_drvdata(psy);
789
790 return scnprintf(buf, PAGE_SIZE, "%u\n",
791 bq24257_vindpm_map[bq->init_data.vindpm]);
792}
793
bb2956e8 794static DEVICE_ATTR(ovp_voltage, S_IRUGO, bq24257_show_ovp_voltage, NULL);
138606ff 795static DEVICE_ATTR(in_dpm_voltage, S_IRUGO, bq24257_show_in_dpm_voltage, NULL);
bb2956e8
AD
796
797static struct attribute *bq24257_charger_attr[] = {
798 &dev_attr_ovp_voltage.attr,
138606ff 799 &dev_attr_in_dpm_voltage.attr,
bb2956e8
AD
800 NULL,
801};
802
803static const struct attribute_group bq24257_attr_group = {
804 .attrs = bq24257_charger_attr,
805};
806
2219a935
LP
807static int bq24257_power_supply_init(struct bq24257_device *bq)
808{
809 struct power_supply_config psy_cfg = { .drv_data = bq, };
810
811 psy_cfg.supplied_to = bq24257_charger_supplied_to;
812 psy_cfg.num_supplicants = ARRAY_SIZE(bq24257_charger_supplied_to);
813
dfc60252
AD
814 bq->charger = devm_power_supply_register(bq->dev,
815 &bq24257_power_supply_desc,
816 &psy_cfg);
817
3b84b8ef 818 return PTR_ERR_OR_ZERO(bq->charger);
2219a935
LP
819}
820
7c071a0a 821static void bq24257_pg_gpio_probe(struct bq24257_device *bq)
2219a935 822{
7c071a0a
AD
823 bq->pg = devm_gpiod_get_optional(bq->dev, BQ24257_PG_GPIO, GPIOD_IN);
824
825 if (PTR_ERR(bq->pg) == -EPROBE_DEFER) {
826 dev_info(bq->dev, "probe retry requested for PG pin\n");
827 return;
828 } else if (IS_ERR(bq->pg)) {
829 dev_err(bq->dev, "error probing PG pin\n");
830 bq->pg = NULL;
831 return;
2219a935
LP
832 }
833
7c071a0a
AD
834 if (bq->pg)
835 dev_dbg(bq->dev, "probed PG pin = %d\n", desc_to_gpio(bq->pg));
2219a935
LP
836}
837
838static int bq24257_fw_probe(struct bq24257_device *bq)
839{
840 int ret;
841 u32 property;
842
eb9fbcc6 843 /* Required properties */
2219a935
LP
844 ret = device_property_read_u32(bq->dev, "ti,charge-current", &property);
845 if (ret < 0)
846 return ret;
847
848 bq->init_data.ichg = bq24257_find_idx(property, bq24257_ichg_map,
849 BQ24257_ICHG_MAP_SIZE);
850
851 ret = device_property_read_u32(bq->dev, "ti,battery-regulation-voltage",
852 &property);
853 if (ret < 0)
854 return ret;
855
856 bq->init_data.vbat = bq24257_find_idx(property, bq24257_vbat_map,
857 BQ24257_VBAT_MAP_SIZE);
858
859 ret = device_property_read_u32(bq->dev, "ti,termination-current",
860 &property);
861 if (ret < 0)
862 return ret;
863
864 bq->init_data.iterm = bq24257_find_idx(property, bq24257_iterm_map,
865 BQ24257_ITERM_MAP_SIZE);
866
eb9fbcc6
AD
867 /* Optional properties. If not provided use reasonable default. */
868 ret = device_property_read_u32(bq->dev, "ti,current-limit",
869 &property);
870 if (ret < 0) {
871 bq->iilimit_autoset_enable = true;
872
873 /*
874 * Explicitly set a default value which will be needed for
875 * devices that don't support the automatic setting of the input
876 * current limit through the charger type detection mechanism.
877 */
878 bq->init_data.iilimit = IILIMIT_500;
879 } else
880 bq->init_data.iilimit =
881 bq24257_find_idx(property,
882 bq24257_iilimit_map,
883 BQ24257_IILIMIT_MAP_SIZE);
884
bb2956e8
AD
885 ret = device_property_read_u32(bq->dev, "ti,ovp-voltage",
886 &property);
887 if (ret < 0)
888 bq->init_data.vovp = VOVP_6500;
889 else
890 bq->init_data.vovp = bq24257_find_idx(property,
891 bq24257_vovp_map,
892 BQ24257_VOVP_MAP_SIZE);
893
138606ff
AD
894 ret = device_property_read_u32(bq->dev, "ti,in-dpm-voltage",
895 &property);
896 if (ret < 0)
897 bq->init_data.vindpm = VINDPM_4360;
898 else
899 bq->init_data.vindpm =
900 bq24257_find_idx(property,
901 bq24257_vindpm_map,
902 BQ24257_VINDPM_MAP_SIZE);
903
2219a935
LP
904 return 0;
905}
906
907static int bq24257_probe(struct i2c_client *client,
908 const struct i2c_device_id *id)
909{
910 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
911 struct device *dev = &client->dev;
bf02dca9 912 const struct acpi_device_id *acpi_id;
2219a935
LP
913 struct bq24257_device *bq;
914 int ret;
915 int i;
916
917 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
918 dev_err(dev, "No support for SMBUS_BYTE_DATA\n");
919 return -ENODEV;
920 }
921
922 bq = devm_kzalloc(dev, sizeof(*bq), GFP_KERNEL);
923 if (!bq)
924 return -ENOMEM;
925
926 bq->client = client;
927 bq->dev = dev;
928
bf02dca9
AD
929 if (ACPI_HANDLE(dev)) {
930 acpi_id = acpi_match_device(dev->driver->acpi_match_table,
931 &client->dev);
932 if (!acpi_id) {
933 dev_err(dev, "Failed to match ACPI device\n");
934 return -ENODEV;
935 }
936 bq->chip = (enum bq2425x_chip)acpi_id->driver_data;
937 } else {
938 bq->chip = (enum bq2425x_chip)id->driver_data;
939 }
940
2219a935
LP
941 mutex_init(&bq->lock);
942
943 bq->rmap = devm_regmap_init_i2c(client, &bq24257_regmap_config);
944 if (IS_ERR(bq->rmap)) {
945 dev_err(dev, "failed to allocate register map\n");
946 return PTR_ERR(bq->rmap);
947 }
948
949 for (i = 0; i < ARRAY_SIZE(bq24257_reg_fields); i++) {
950 const struct reg_field *reg_fields = bq24257_reg_fields;
951
952 bq->rmap_fields[i] = devm_regmap_field_alloc(dev, bq->rmap,
953 reg_fields[i]);
954 if (IS_ERR(bq->rmap_fields[i])) {
955 dev_err(dev, "cannot allocate regmap field\n");
956 return PTR_ERR(bq->rmap_fields[i]);
957 }
958 }
959
960 i2c_set_clientdata(client, bq);
961
2219a935
LP
962 if (!dev->platform_data) {
963 ret = bq24257_fw_probe(bq);
964 if (ret < 0) {
965 dev_err(dev, "Cannot read device properties.\n");
966 return ret;
967 }
968 } else {
969 return -ENODEV;
970 }
971
eb9fbcc6
AD
972 /*
973 * The BQ24250 doesn't support the D+/D- based charger type detection
974 * used for the automatic setting of the input current limit setting so
975 * explicitly disable that feature.
976 */
977 if (bq->chip == BQ24250)
978 bq->iilimit_autoset_enable = false;
979
980 if (bq->iilimit_autoset_enable)
981 INIT_DELAYED_WORK(&bq->iilimit_setup_work,
982 bq24257_iilimit_setup_work);
983
7c071a0a
AD
984 /*
985 * The BQ24250 doesn't have a dedicated Power Good (PG) pin so let's
986 * not probe for it and instead use a SW-based approach to determine
987 * the PG state. We also use a SW-based approach for all other devices
988 * if the PG pin is either not defined or can't be probed.
989 */
990 if (bq->chip != BQ24250)
991 bq24257_pg_gpio_probe(bq);
992
993 if (PTR_ERR(bq->pg) == -EPROBE_DEFER)
994 return PTR_ERR(bq->pg);
995 else if (!bq->pg)
996 dev_info(bq->dev, "using SW-based power-good detection\n");
2219a935
LP
997
998 /* reset all registers to defaults */
999 ret = bq24257_field_write(bq, F_RESET, 1);
1000 if (ret < 0)
1001 return ret;
1002
1003 /*
1004 * Put the RESET bit back to 0, in cache. For some reason the HW always
1005 * returns 1 on this bit, so this is the only way to avoid resetting the
1006 * chip every time we update another field in this register.
1007 */
1008 ret = bq24257_field_write(bq, F_RESET, 0);
1009 if (ret < 0)
1010 return ret;
1011
1012 ret = bq24257_hw_init(bq);
1013 if (ret < 0) {
1014 dev_err(dev, "Cannot initialize the chip.\n");
1015 return ret;
1016 }
1017
2219a935
LP
1018 ret = devm_request_threaded_irq(dev, client->irq, NULL,
1019 bq24257_irq_handler_thread,
1020 IRQF_TRIGGER_FALLING |
1021 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
bf02dca9 1022 bq2425x_chip_name[bq->chip], bq);
5ff8c89d
AD
1023 if (ret) {
1024 dev_err(dev, "Failed to request IRQ #%d\n", client->irq);
2219a935 1025 return ret;
5ff8c89d 1026 }
2219a935
LP
1027
1028 ret = bq24257_power_supply_init(bq);
bb2956e8 1029 if (ret < 0) {
2219a935 1030 dev_err(dev, "Failed to register power supply\n");
bb2956e8
AD
1031 return ret;
1032 }
2219a935 1033
bb2956e8
AD
1034 ret = sysfs_create_group(&bq->charger->dev.kobj, &bq24257_attr_group);
1035 if (ret < 0) {
1036 dev_err(dev, "Can't create sysfs entries\n");
1037 return ret;
1038 }
1039
1040 return 0;
2219a935
LP
1041}
1042
1043static int bq24257_remove(struct i2c_client *client)
1044{
1045 struct bq24257_device *bq = i2c_get_clientdata(client);
1046
eb9fbcc6
AD
1047 if (bq->iilimit_autoset_enable)
1048 cancel_delayed_work_sync(&bq->iilimit_setup_work);
2219a935 1049
bb2956e8
AD
1050 sysfs_remove_group(&bq->charger->dev.kobj, &bq24257_attr_group);
1051
2219a935
LP
1052 bq24257_field_write(bq, F_RESET, 1); /* reset to defaults */
1053
1054 return 0;
1055}
1056
1057#ifdef CONFIG_PM_SLEEP
1058static int bq24257_suspend(struct device *dev)
1059{
1060 struct bq24257_device *bq = dev_get_drvdata(dev);
1061 int ret = 0;
1062
eb9fbcc6
AD
1063 if (bq->iilimit_autoset_enable)
1064 cancel_delayed_work_sync(&bq->iilimit_setup_work);
2219a935
LP
1065
1066 /* reset all registers to default (and activate standalone mode) */
1067 ret = bq24257_field_write(bq, F_RESET, 1);
1068 if (ret < 0)
1069 dev_err(bq->dev, "Cannot reset chip to standalone mode.\n");
1070
1071 return ret;
1072}
1073
1074static int bq24257_resume(struct device *dev)
1075{
1076 int ret;
1077 struct bq24257_device *bq = dev_get_drvdata(dev);
1078
1079 ret = regcache_drop_region(bq->rmap, BQ24257_REG_1, BQ24257_REG_7);
1080 if (ret < 0)
1081 return ret;
1082
1083 ret = bq24257_field_write(bq, F_RESET, 0);
1084 if (ret < 0)
1085 return ret;
1086
1087 ret = bq24257_hw_init(bq);
1088 if (ret < 0) {
1089 dev_err(bq->dev, "Cannot init chip after resume.\n");
1090 return ret;
1091 }
1092
1093 /* signal userspace, maybe state changed while suspended */
1094 power_supply_changed(bq->charger);
1095
1096 return 0;
1097}
1098#endif
1099
1100static const struct dev_pm_ops bq24257_pm = {
1101 SET_SYSTEM_SLEEP_PM_OPS(bq24257_suspend, bq24257_resume)
1102};
1103
1104static const struct i2c_device_id bq24257_i2c_ids[] = {
bf02dca9
AD
1105 { "bq24250", BQ24250 },
1106 { "bq24251", BQ24251 },
1107 { "bq24257", BQ24257 },
2219a935
LP
1108 {},
1109};
1110MODULE_DEVICE_TABLE(i2c, bq24257_i2c_ids);
1111
1112static const struct of_device_id bq24257_of_match[] = {
bf02dca9
AD
1113 { .compatible = "ti,bq24250", },
1114 { .compatible = "ti,bq24251", },
2219a935
LP
1115 { .compatible = "ti,bq24257", },
1116 { },
1117};
1118MODULE_DEVICE_TABLE(of, bq24257_of_match);
1119
1120static const struct acpi_device_id bq24257_acpi_match[] = {
bf02dca9
AD
1121 { "BQ242500", BQ24250 },
1122 { "BQ242510", BQ24251 },
1123 { "BQ242570", BQ24257 },
2219a935
LP
1124 {},
1125};
1126MODULE_DEVICE_TABLE(acpi, bq24257_acpi_match);
1127
1128static struct i2c_driver bq24257_driver = {
1129 .driver = {
1130 .name = "bq24257-charger",
1131 .of_match_table = of_match_ptr(bq24257_of_match),
1132 .acpi_match_table = ACPI_PTR(bq24257_acpi_match),
1133 .pm = &bq24257_pm,
1134 },
1135 .probe = bq24257_probe,
1136 .remove = bq24257_remove,
1137 .id_table = bq24257_i2c_ids,
1138};
1139module_i2c_driver(bq24257_driver);
1140
1141MODULE_AUTHOR("Laurentiu Palcu <laurentiu.palcu@intel.com>");
1142MODULE_DESCRIPTION("bq24257 charger driver");
1143MODULE_LICENSE("GPL");