blob: 060f754b41e5596fe33fabd00ab740fb4bbe7678 [file] [log] [blame]
Laurentiu Palcu2219a932015-04-16 12:31:16 +03001/*
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 *
16 */
17
18#include <linux/module.h>
19#include <linux/i2c.h>
20#include <linux/power_supply.h>
21#include <linux/regmap.h>
22#include <linux/types.h>
23#include <linux/gpio/consumer.h>
24#include <linux/interrupt.h>
25#include <linux/delay.h>
26
27#include <linux/acpi.h>
28#include <linux/of.h>
29
30#define BQ24257_REG_1 0x00
31#define BQ24257_REG_2 0x01
32#define BQ24257_REG_3 0x02
33#define BQ24257_REG_4 0x03
34#define BQ24257_REG_5 0x04
35#define BQ24257_REG_6 0x05
36#define BQ24257_REG_7 0x06
37
38#define BQ24257_MANUFACTURER "Texas Instruments"
Laurentiu Palcu2219a932015-04-16 12:31:16 +030039#define BQ24257_PG_GPIO "pg"
40
41#define BQ24257_ILIM_SET_DELAY 1000 /* msec */
42
43enum bq24257_fields {
44 F_WD_FAULT, F_WD_EN, F_STAT, F_FAULT, /* REG 1 */
45 F_RESET, F_IILIMIT, F_EN_STAT, F_EN_TERM, F_CE, F_HZ_MODE, /* REG 2 */
46 F_VBAT, F_USB_DET, /* REG 3 */
47 F_ICHG, F_ITERM, /* REG 4 */
48 F_LOOP_STATUS, F_LOW_CHG, F_DPDM_EN, F_CE_STATUS, F_VINDPM, /* REG 5 */
49 F_X2_TMR_EN, F_TMR, F_SYSOFF, F_TS_STAT, /* REG 6 */
50 F_VOVP, F_CLR_VDP, F_FORCE_BATDET, F_FORCE_PTM, /* REG 7 */
51
52 F_MAX_FIELDS
53};
54
55/* initial field values, converted from uV/uA */
56struct bq24257_init_data {
57 u8 ichg; /* charge current */
58 u8 vbat; /* regulation voltage */
59 u8 iterm; /* termination current */
60};
61
62struct bq24257_state {
63 u8 status;
64 u8 fault;
65 bool power_good;
66};
67
68struct bq24257_device {
69 struct i2c_client *client;
70 struct device *dev;
71 struct power_supply *charger;
72
73 struct regmap *rmap;
74 struct regmap_field *rmap_fields[F_MAX_FIELDS];
75
76 struct gpio_desc *pg;
77
78 struct delayed_work iilimit_setup_work;
79
80 struct bq24257_init_data init_data;
81 struct bq24257_state state;
82
83 struct mutex lock; /* protect state data */
84};
85
86static bool bq24257_is_volatile_reg(struct device *dev, unsigned int reg)
87{
88 switch (reg) {
89 case BQ24257_REG_2:
90 case BQ24257_REG_4:
91 return false;
92
93 default:
94 return true;
95 }
96}
97
98static const struct regmap_config bq24257_regmap_config = {
99 .reg_bits = 8,
100 .val_bits = 8,
101
102 .max_register = BQ24257_REG_7,
103 .cache_type = REGCACHE_RBTREE,
104
105 .volatile_reg = bq24257_is_volatile_reg,
106};
107
108static const struct reg_field bq24257_reg_fields[] = {
109 /* REG 1 */
110 [F_WD_FAULT] = REG_FIELD(BQ24257_REG_1, 7, 7),
111 [F_WD_EN] = REG_FIELD(BQ24257_REG_1, 6, 6),
112 [F_STAT] = REG_FIELD(BQ24257_REG_1, 4, 5),
113 [F_FAULT] = REG_FIELD(BQ24257_REG_1, 0, 3),
114 /* REG 2 */
115 [F_RESET] = REG_FIELD(BQ24257_REG_2, 7, 7),
116 [F_IILIMIT] = REG_FIELD(BQ24257_REG_2, 4, 6),
117 [F_EN_STAT] = REG_FIELD(BQ24257_REG_2, 3, 3),
118 [F_EN_TERM] = REG_FIELD(BQ24257_REG_2, 2, 2),
119 [F_CE] = REG_FIELD(BQ24257_REG_2, 1, 1),
120 [F_HZ_MODE] = REG_FIELD(BQ24257_REG_2, 0, 0),
121 /* REG 3 */
122 [F_VBAT] = REG_FIELD(BQ24257_REG_3, 2, 7),
123 [F_USB_DET] = REG_FIELD(BQ24257_REG_3, 0, 1),
124 /* REG 4 */
125 [F_ICHG] = REG_FIELD(BQ24257_REG_4, 3, 7),
126 [F_ITERM] = REG_FIELD(BQ24257_REG_4, 0, 2),
127 /* REG 5 */
128 [F_LOOP_STATUS] = REG_FIELD(BQ24257_REG_5, 6, 7),
129 [F_LOW_CHG] = REG_FIELD(BQ24257_REG_5, 5, 5),
130 [F_DPDM_EN] = REG_FIELD(BQ24257_REG_5, 4, 4),
131 [F_CE_STATUS] = REG_FIELD(BQ24257_REG_5, 3, 3),
132 [F_VINDPM] = REG_FIELD(BQ24257_REG_5, 0, 2),
133 /* REG 6 */
134 [F_X2_TMR_EN] = REG_FIELD(BQ24257_REG_6, 7, 7),
135 [F_TMR] = REG_FIELD(BQ24257_REG_6, 5, 6),
136 [F_SYSOFF] = REG_FIELD(BQ24257_REG_6, 4, 4),
137 [F_TS_STAT] = REG_FIELD(BQ24257_REG_6, 0, 2),
138 /* REG 7 */
139 [F_VOVP] = REG_FIELD(BQ24257_REG_7, 5, 7),
140 [F_CLR_VDP] = REG_FIELD(BQ24257_REG_7, 4, 4),
141 [F_FORCE_BATDET] = REG_FIELD(BQ24257_REG_7, 3, 3),
142 [F_FORCE_PTM] = REG_FIELD(BQ24257_REG_7, 2, 2)
143};
144
145static const u32 bq24257_vbat_map[] = {
146 3500000, 3520000, 3540000, 3560000, 3580000, 3600000, 3620000, 3640000,
147 3660000, 3680000, 3700000, 3720000, 3740000, 3760000, 3780000, 3800000,
148 3820000, 3840000, 3860000, 3880000, 3900000, 3920000, 3940000, 3960000,
149 3980000, 4000000, 4020000, 4040000, 4060000, 4080000, 4100000, 4120000,
150 4140000, 4160000, 4180000, 4200000, 4220000, 4240000, 4260000, 4280000,
151 4300000, 4320000, 4340000, 4360000, 4380000, 4400000, 4420000, 4440000
152};
153
154#define BQ24257_VBAT_MAP_SIZE ARRAY_SIZE(bq24257_vbat_map)
155
156static const u32 bq24257_ichg_map[] = {
157 500000, 550000, 600000, 650000, 700000, 750000, 800000, 850000, 900000,
158 950000, 1000000, 1050000, 1100000, 1150000, 1200000, 1250000, 1300000,
159 1350000, 1400000, 1450000, 1500000, 1550000, 1600000, 1650000, 1700000,
160 1750000, 1800000, 1850000, 1900000, 1950000, 2000000
161};
162
163#define BQ24257_ICHG_MAP_SIZE ARRAY_SIZE(bq24257_ichg_map)
164
165static const u32 bq24257_iterm_map[] = {
166 50000, 75000, 100000, 125000, 150000, 175000, 200000, 225000
167};
168
169#define BQ24257_ITERM_MAP_SIZE ARRAY_SIZE(bq24257_iterm_map)
170
171static int bq24257_field_read(struct bq24257_device *bq,
172 enum bq24257_fields field_id)
173{
174 int ret;
175 int val;
176
177 ret = regmap_field_read(bq->rmap_fields[field_id], &val);
178 if (ret < 0)
179 return ret;
180
181 return val;
182}
183
184static int bq24257_field_write(struct bq24257_device *bq,
185 enum bq24257_fields field_id, u8 val)
186{
187 return regmap_field_write(bq->rmap_fields[field_id], val);
188}
189
190static u8 bq24257_find_idx(u32 value, const u32 *map, u8 map_size)
191{
192 u8 idx;
193
194 for (idx = 1; idx < map_size; idx++)
195 if (value < map[idx])
196 break;
197
198 return idx - 1;
199}
200
201enum bq24257_status {
202 STATUS_READY,
203 STATUS_CHARGE_IN_PROGRESS,
204 STATUS_CHARGE_DONE,
205 STATUS_FAULT,
206};
207
208enum bq24257_fault {
209 FAULT_NORMAL,
210 FAULT_INPUT_OVP,
211 FAULT_INPUT_UVLO,
212 FAULT_SLEEP,
213 FAULT_BAT_TS,
214 FAULT_BAT_OVP,
215 FAULT_TS,
216 FAULT_TIMER,
217 FAULT_NO_BAT,
218 FAULT_ISET,
219 FAULT_INPUT_LDO_LOW,
220};
221
222static int bq24257_power_supply_get_property(struct power_supply *psy,
223 enum power_supply_property psp,
224 union power_supply_propval *val)
225{
226 struct bq24257_device *bq = power_supply_get_drvdata(psy);
227 struct bq24257_state state;
228
229 mutex_lock(&bq->lock);
230 state = bq->state;
231 mutex_unlock(&bq->lock);
232
233 switch (psp) {
234 case POWER_SUPPLY_PROP_STATUS:
235 if (!state.power_good)
236 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
237 else if (state.status == STATUS_READY)
238 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
239 else if (state.status == STATUS_CHARGE_IN_PROGRESS)
240 val->intval = POWER_SUPPLY_STATUS_CHARGING;
241 else if (state.status == STATUS_CHARGE_DONE)
242 val->intval = POWER_SUPPLY_STATUS_FULL;
243 else
244 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
245 break;
246
247 case POWER_SUPPLY_PROP_MANUFACTURER:
248 val->strval = BQ24257_MANUFACTURER;
249 break;
250
251 case POWER_SUPPLY_PROP_ONLINE:
252 val->intval = state.power_good;
253 break;
254
255 case POWER_SUPPLY_PROP_HEALTH:
256 switch (state.fault) {
257 case FAULT_NORMAL:
258 val->intval = POWER_SUPPLY_HEALTH_GOOD;
259 break;
260
261 case FAULT_INPUT_OVP:
262 case FAULT_BAT_OVP:
263 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
264 break;
265
266 case FAULT_TS:
267 case FAULT_BAT_TS:
268 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
269 break;
270
271 case FAULT_TIMER:
272 val->intval = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
273 break;
274
275 default:
276 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
277 break;
278 }
279
280 break;
281
282 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
283 val->intval = bq24257_ichg_map[bq->init_data.ichg];
284 break;
285
286 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
287 val->intval = bq24257_ichg_map[BQ24257_ICHG_MAP_SIZE - 1];
288 break;
289
290 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
291 val->intval = bq24257_vbat_map[bq->init_data.vbat];
292 break;
293
294 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
295 val->intval = bq24257_vbat_map[BQ24257_VBAT_MAP_SIZE - 1];
296 break;
297
298 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
299 val->intval = bq24257_iterm_map[bq->init_data.iterm];
300 break;
301
302 default:
303 return -EINVAL;
304 }
305
306 return 0;
307}
308
309static int bq24257_get_chip_state(struct bq24257_device *bq,
310 struct bq24257_state *state)
311{
312 int ret;
313
314 ret = bq24257_field_read(bq, F_STAT);
315 if (ret < 0)
316 return ret;
317
318 state->status = ret;
319
320 ret = bq24257_field_read(bq, F_FAULT);
321 if (ret < 0)
322 return ret;
323
324 state->fault = ret;
325
326 state->power_good = !gpiod_get_value_cansleep(bq->pg);
327
328 return 0;
329}
330
331static bool bq24257_state_changed(struct bq24257_device *bq,
332 struct bq24257_state *new_state)
333{
334 int ret;
335
336 mutex_lock(&bq->lock);
337 ret = (bq->state.status != new_state->status ||
338 bq->state.fault != new_state->fault ||
339 bq->state.power_good != new_state->power_good);
340 mutex_unlock(&bq->lock);
341
342 return ret;
343}
344
345enum bq24257_loop_status {
346 LOOP_STATUS_NONE,
347 LOOP_STATUS_IN_DPM,
348 LOOP_STATUS_IN_CURRENT_LIMIT,
349 LOOP_STATUS_THERMAL,
350};
351
352enum bq24257_in_ilimit {
353 IILIMIT_100,
354 IILIMIT_150,
355 IILIMIT_500,
356 IILIMIT_900,
357 IILIMIT_1500,
358 IILIMIT_2000,
359 IILIMIT_EXT,
360 IILIMIT_NONE,
361};
362
363enum bq24257_port_type {
364 PORT_TYPE_DCP, /* Dedicated Charging Port */
365 PORT_TYPE_CDP, /* Charging Downstream Port */
366 PORT_TYPE_SDP, /* Standard Downstream Port */
367 PORT_TYPE_NON_STANDARD,
368};
369
370enum bq24257_safety_timer {
371 SAFETY_TIMER_45,
372 SAFETY_TIMER_360,
373 SAFETY_TIMER_540,
374 SAFETY_TIMER_NONE,
375};
376
377static int bq24257_iilimit_autoset(struct bq24257_device *bq)
378{
379 int loop_status;
380 int iilimit;
381 int port_type;
382 int ret;
383 const u8 new_iilimit[] = {
384 [PORT_TYPE_DCP] = IILIMIT_2000,
385 [PORT_TYPE_CDP] = IILIMIT_2000,
386 [PORT_TYPE_SDP] = IILIMIT_500,
387 [PORT_TYPE_NON_STANDARD] = IILIMIT_500
388 };
389
390 ret = bq24257_field_read(bq, F_LOOP_STATUS);
391 if (ret < 0)
392 goto error;
393
394 loop_status = ret;
395
396 ret = bq24257_field_read(bq, F_IILIMIT);
397 if (ret < 0)
398 goto error;
399
400 iilimit = ret;
401
402 /*
403 * All USB ports should be able to handle 500mA. If not, DPM will lower
404 * the charging current to accommodate the power source. No need to set
405 * a lower IILIMIT value.
406 */
407 if (loop_status == LOOP_STATUS_IN_DPM && iilimit == IILIMIT_500)
408 return 0;
409
410 ret = bq24257_field_read(bq, F_USB_DET);
411 if (ret < 0)
412 goto error;
413
414 port_type = ret;
415
416 ret = bq24257_field_write(bq, F_IILIMIT, new_iilimit[port_type]);
417 if (ret < 0)
418 goto error;
419
420 ret = bq24257_field_write(bq, F_TMR, SAFETY_TIMER_360);
421 if (ret < 0)
422 goto error;
423
424 ret = bq24257_field_write(bq, F_CLR_VDP, 1);
425 if (ret < 0)
426 goto error;
427
428 dev_dbg(bq->dev, "port/loop = %d/%d -> iilimit = %d\n",
429 port_type, loop_status, new_iilimit[port_type]);
430
431 return 0;
432
433error:
434 dev_err(bq->dev, "%s: Error communicating with the chip.\n", __func__);
435 return ret;
436}
437
438static void bq24257_iilimit_setup_work(struct work_struct *work)
439{
440 struct bq24257_device *bq = container_of(work, struct bq24257_device,
441 iilimit_setup_work.work);
442
443 bq24257_iilimit_autoset(bq);
444}
445
446static void bq24257_handle_state_change(struct bq24257_device *bq,
447 struct bq24257_state *new_state)
448{
449 int ret;
450 struct bq24257_state old_state;
Laurentiu Palcu2219a932015-04-16 12:31:16 +0300451
452 mutex_lock(&bq->lock);
453 old_state = bq->state;
454 mutex_unlock(&bq->lock);
455
Andreas Dannenberg9b1cf1e2015-09-25 10:54:08 -0500456 if (!new_state->power_good) {
457 dev_dbg(bq->dev, "Power removed\n");
Laurentiu Palcu2219a932015-04-16 12:31:16 +0300458 cancel_delayed_work_sync(&bq->iilimit_setup_work);
459
460 /* activate D+/D- port detection algorithm */
461 ret = bq24257_field_write(bq, F_DPDM_EN, 1);
462 if (ret < 0)
463 goto error;
464
Andreas Dannenberg9b1cf1e2015-09-25 10:54:08 -0500465 /* reset input current limit */
Laurentiu Palcu2219a932015-04-16 12:31:16 +0300466 ret = bq24257_field_write(bq, F_IILIMIT, IILIMIT_500);
467 if (ret < 0)
468 goto error;
Andreas Dannenberg9b1cf1e2015-09-25 10:54:08 -0500469 } else if (!old_state.power_good) {
470 dev_dbg(bq->dev, "Power inserted\n");
471
472 /* configure input current limit */
Laurentiu Palcu2219a932015-04-16 12:31:16 +0300473 schedule_delayed_work(&bq->iilimit_setup_work,
474 msecs_to_jiffies(BQ24257_ILIM_SET_DELAY));
Andreas Dannenberg9b1cf1e2015-09-25 10:54:08 -0500475 } else if (new_state->fault == FAULT_NO_BAT) {
476 dev_warn(bq->dev, "Battery removed\n");
477 } else if (new_state->fault == FAULT_TIMER) {
478 dev_err(bq->dev, "Safety timer expired! Battery dead?\n");
Laurentiu Palcu2219a932015-04-16 12:31:16 +0300479 }
480
481 return;
482
483error:
484 dev_err(bq->dev, "%s: Error communicating with the chip.\n", __func__);
485}
486
487static irqreturn_t bq24257_irq_handler_thread(int irq, void *private)
488{
489 int ret;
490 struct bq24257_device *bq = private;
491 struct bq24257_state state;
492
493 ret = bq24257_get_chip_state(bq, &state);
494 if (ret < 0)
495 return IRQ_HANDLED;
496
497 if (!bq24257_state_changed(bq, &state))
498 return IRQ_HANDLED;
499
500 dev_dbg(bq->dev, "irq(state changed): status/fault/pg = %d/%d/%d\n",
501 state.status, state.fault, state.power_good);
502
503 bq24257_handle_state_change(bq, &state);
504
505 mutex_lock(&bq->lock);
506 bq->state = state;
507 mutex_unlock(&bq->lock);
508
509 power_supply_changed(bq->charger);
510
511 return IRQ_HANDLED;
512}
513
514static int bq24257_hw_init(struct bq24257_device *bq)
515{
516 int ret;
517 int i;
518 struct bq24257_state state;
519
520 const struct {
521 int field;
522 u32 value;
523 } init_data[] = {
524 {F_ICHG, bq->init_data.ichg},
525 {F_VBAT, bq->init_data.vbat},
526 {F_ITERM, bq->init_data.iterm}
527 };
528
529 /*
530 * Disable the watchdog timer to prevent the IC from going back to
531 * default settings after 50 seconds of I2C inactivity.
532 */
533 ret = bq24257_field_write(bq, F_WD_EN, 0);
534 if (ret < 0)
535 return ret;
536
537 /* configure the charge currents and voltages */
538 for (i = 0; i < ARRAY_SIZE(init_data); i++) {
539 ret = bq24257_field_write(bq, init_data[i].field,
540 init_data[i].value);
541 if (ret < 0)
542 return ret;
543 }
544
545 ret = bq24257_get_chip_state(bq, &state);
546 if (ret < 0)
547 return ret;
548
549 mutex_lock(&bq->lock);
550 bq->state = state;
551 mutex_unlock(&bq->lock);
552
553 if (!state.power_good)
554 /* activate D+/D- detection algorithm */
555 ret = bq24257_field_write(bq, F_DPDM_EN, 1);
556 else if (state.fault != FAULT_NO_BAT)
557 ret = bq24257_iilimit_autoset(bq);
558
559 return ret;
560}
561
562static enum power_supply_property bq24257_power_supply_props[] = {
563 POWER_SUPPLY_PROP_MANUFACTURER,
564 POWER_SUPPLY_PROP_STATUS,
565 POWER_SUPPLY_PROP_ONLINE,
566 POWER_SUPPLY_PROP_HEALTH,
567 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
568 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
569 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
570 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
571 POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
572};
573
574static char *bq24257_charger_supplied_to[] = {
575 "main-battery",
576};
577
578static const struct power_supply_desc bq24257_power_supply_desc = {
579 .name = "bq24257-charger",
580 .type = POWER_SUPPLY_TYPE_USB,
581 .properties = bq24257_power_supply_props,
582 .num_properties = ARRAY_SIZE(bq24257_power_supply_props),
583 .get_property = bq24257_power_supply_get_property,
584};
585
586static int bq24257_power_supply_init(struct bq24257_device *bq)
587{
588 struct power_supply_config psy_cfg = { .drv_data = bq, };
589
590 psy_cfg.supplied_to = bq24257_charger_supplied_to;
591 psy_cfg.num_supplicants = ARRAY_SIZE(bq24257_charger_supplied_to);
592
Andreas Dannenbergdfc60252015-09-28 17:33:51 -0500593 bq->charger = devm_power_supply_register(bq->dev,
594 &bq24257_power_supply_desc,
595 &psy_cfg);
596
Andreas Dannenberg3b84b8e2015-09-28 17:33:52 -0500597 return PTR_ERR_OR_ZERO(bq->charger);
Laurentiu Palcu2219a932015-04-16 12:31:16 +0300598}
599
Laurentiu Palcu2219a932015-04-16 12:31:16 +0300600static int bq24257_pg_gpio_probe(struct bq24257_device *bq)
601{
Uwe Kleine-Könige31cd782015-06-12 10:54:04 +0200602 bq->pg = devm_gpiod_get_index(bq->dev, BQ24257_PG_GPIO, 0, GPIOD_IN);
Laurentiu Palcu2219a932015-04-16 12:31:16 +0300603 if (IS_ERR(bq->pg)) {
604 dev_err(bq->dev, "could not probe PG pin\n");
605 return PTR_ERR(bq->pg);
606 }
607
Uwe Kleine-Könige31cd782015-06-12 10:54:04 +0200608 return 0;
Laurentiu Palcu2219a932015-04-16 12:31:16 +0300609}
610
611static int bq24257_fw_probe(struct bq24257_device *bq)
612{
613 int ret;
614 u32 property;
615
616 ret = device_property_read_u32(bq->dev, "ti,charge-current", &property);
617 if (ret < 0)
618 return ret;
619
620 bq->init_data.ichg = bq24257_find_idx(property, bq24257_ichg_map,
621 BQ24257_ICHG_MAP_SIZE);
622
623 ret = device_property_read_u32(bq->dev, "ti,battery-regulation-voltage",
624 &property);
625 if (ret < 0)
626 return ret;
627
628 bq->init_data.vbat = bq24257_find_idx(property, bq24257_vbat_map,
629 BQ24257_VBAT_MAP_SIZE);
630
631 ret = device_property_read_u32(bq->dev, "ti,termination-current",
632 &property);
633 if (ret < 0)
634 return ret;
635
636 bq->init_data.iterm = bq24257_find_idx(property, bq24257_iterm_map,
637 BQ24257_ITERM_MAP_SIZE);
638
639 return 0;
640}
641
642static int bq24257_probe(struct i2c_client *client,
643 const struct i2c_device_id *id)
644{
645 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
646 struct device *dev = &client->dev;
647 struct bq24257_device *bq;
648 int ret;
649 int i;
650
651 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
652 dev_err(dev, "No support for SMBUS_BYTE_DATA\n");
653 return -ENODEV;
654 }
655
656 bq = devm_kzalloc(dev, sizeof(*bq), GFP_KERNEL);
657 if (!bq)
658 return -ENOMEM;
659
660 bq->client = client;
661 bq->dev = dev;
662
663 mutex_init(&bq->lock);
664
665 bq->rmap = devm_regmap_init_i2c(client, &bq24257_regmap_config);
666 if (IS_ERR(bq->rmap)) {
667 dev_err(dev, "failed to allocate register map\n");
668 return PTR_ERR(bq->rmap);
669 }
670
671 for (i = 0; i < ARRAY_SIZE(bq24257_reg_fields); i++) {
672 const struct reg_field *reg_fields = bq24257_reg_fields;
673
674 bq->rmap_fields[i] = devm_regmap_field_alloc(dev, bq->rmap,
675 reg_fields[i]);
676 if (IS_ERR(bq->rmap_fields[i])) {
677 dev_err(dev, "cannot allocate regmap field\n");
678 return PTR_ERR(bq->rmap_fields[i]);
679 }
680 }
681
682 i2c_set_clientdata(client, bq);
683
684 INIT_DELAYED_WORK(&bq->iilimit_setup_work, bq24257_iilimit_setup_work);
685
686 if (!dev->platform_data) {
687 ret = bq24257_fw_probe(bq);
688 if (ret < 0) {
689 dev_err(dev, "Cannot read device properties.\n");
690 return ret;
691 }
692 } else {
693 return -ENODEV;
694 }
695
696 /* we can only check Power Good status by probing the PG pin */
697 ret = bq24257_pg_gpio_probe(bq);
698 if (ret < 0)
699 return ret;
700
701 /* reset all registers to defaults */
702 ret = bq24257_field_write(bq, F_RESET, 1);
703 if (ret < 0)
704 return ret;
705
706 /*
707 * Put the RESET bit back to 0, in cache. For some reason the HW always
708 * returns 1 on this bit, so this is the only way to avoid resetting the
709 * chip every time we update another field in this register.
710 */
711 ret = bq24257_field_write(bq, F_RESET, 0);
712 if (ret < 0)
713 return ret;
714
715 ret = bq24257_hw_init(bq);
716 if (ret < 0) {
717 dev_err(dev, "Cannot initialize the chip.\n");
718 return ret;
719 }
720
Laurentiu Palcu2219a932015-04-16 12:31:16 +0300721 ret = devm_request_threaded_irq(dev, client->irq, NULL,
722 bq24257_irq_handler_thread,
723 IRQF_TRIGGER_FALLING |
724 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
Andreas Dannenberg5ff8c892015-09-25 10:54:07 -0500725 "bq24257", bq);
726 if (ret) {
727 dev_err(dev, "Failed to request IRQ #%d\n", client->irq);
Laurentiu Palcu2219a932015-04-16 12:31:16 +0300728 return ret;
Andreas Dannenberg5ff8c892015-09-25 10:54:07 -0500729 }
Laurentiu Palcu2219a932015-04-16 12:31:16 +0300730
731 ret = bq24257_power_supply_init(bq);
732 if (ret < 0)
733 dev_err(dev, "Failed to register power supply\n");
734
735 return ret;
736}
737
738static int bq24257_remove(struct i2c_client *client)
739{
740 struct bq24257_device *bq = i2c_get_clientdata(client);
741
742 cancel_delayed_work_sync(&bq->iilimit_setup_work);
743
Laurentiu Palcu2219a932015-04-16 12:31:16 +0300744 bq24257_field_write(bq, F_RESET, 1); /* reset to defaults */
745
746 return 0;
747}
748
749#ifdef CONFIG_PM_SLEEP
750static int bq24257_suspend(struct device *dev)
751{
752 struct bq24257_device *bq = dev_get_drvdata(dev);
753 int ret = 0;
754
755 cancel_delayed_work_sync(&bq->iilimit_setup_work);
756
757 /* reset all registers to default (and activate standalone mode) */
758 ret = bq24257_field_write(bq, F_RESET, 1);
759 if (ret < 0)
760 dev_err(bq->dev, "Cannot reset chip to standalone mode.\n");
761
762 return ret;
763}
764
765static int bq24257_resume(struct device *dev)
766{
767 int ret;
768 struct bq24257_device *bq = dev_get_drvdata(dev);
769
770 ret = regcache_drop_region(bq->rmap, BQ24257_REG_1, BQ24257_REG_7);
771 if (ret < 0)
772 return ret;
773
774 ret = bq24257_field_write(bq, F_RESET, 0);
775 if (ret < 0)
776 return ret;
777
778 ret = bq24257_hw_init(bq);
779 if (ret < 0) {
780 dev_err(bq->dev, "Cannot init chip after resume.\n");
781 return ret;
782 }
783
784 /* signal userspace, maybe state changed while suspended */
785 power_supply_changed(bq->charger);
786
787 return 0;
788}
789#endif
790
791static const struct dev_pm_ops bq24257_pm = {
792 SET_SYSTEM_SLEEP_PM_OPS(bq24257_suspend, bq24257_resume)
793};
794
795static const struct i2c_device_id bq24257_i2c_ids[] = {
796 { "bq24257", 0 },
797 {},
798};
799MODULE_DEVICE_TABLE(i2c, bq24257_i2c_ids);
800
801static const struct of_device_id bq24257_of_match[] = {
802 { .compatible = "ti,bq24257", },
803 { },
804};
805MODULE_DEVICE_TABLE(of, bq24257_of_match);
806
807static const struct acpi_device_id bq24257_acpi_match[] = {
808 {"BQ242570", 0},
809 {},
810};
811MODULE_DEVICE_TABLE(acpi, bq24257_acpi_match);
812
813static struct i2c_driver bq24257_driver = {
814 .driver = {
815 .name = "bq24257-charger",
816 .of_match_table = of_match_ptr(bq24257_of_match),
817 .acpi_match_table = ACPI_PTR(bq24257_acpi_match),
818 .pm = &bq24257_pm,
819 },
820 .probe = bq24257_probe,
821 .remove = bq24257_remove,
822 .id_table = bq24257_i2c_ids,
823};
824module_i2c_driver(bq24257_driver);
825
826MODULE_AUTHOR("Laurentiu Palcu <laurentiu.palcu@intel.com>");
827MODULE_DESCRIPTION("bq24257 charger driver");
828MODULE_LICENSE("GPL");