arcnet: arcdevice.h: Add arcnet_readb and arcnet_writeb
[linux/fpc-iii.git] / drivers / power / bq24257_charger.c
blob5859bc7c16167d667f30ef66ff101297af319a63
1 /*
2 * TI BQ24257 charger driver
4 * Copyright (C) 2015 Intel Corporation
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.
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.
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>
27 #include <linux/acpi.h>
28 #include <linux/of.h>
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
38 #define BQ24257_MANUFACTURER "Texas Instruments"
39 #define BQ24257_STAT_IRQ "stat"
40 #define BQ24257_PG_GPIO "pg"
42 #define BQ24257_ILIM_SET_DELAY 1000 /* msec */
44 enum bq24257_fields {
45 F_WD_FAULT, F_WD_EN, F_STAT, F_FAULT, /* REG 1 */
46 F_RESET, F_IILIMIT, F_EN_STAT, F_EN_TERM, F_CE, F_HZ_MODE, /* REG 2 */
47 F_VBAT, F_USB_DET, /* REG 3 */
48 F_ICHG, F_ITERM, /* REG 4 */
49 F_LOOP_STATUS, F_LOW_CHG, F_DPDM_EN, F_CE_STATUS, F_VINDPM, /* REG 5 */
50 F_X2_TMR_EN, F_TMR, F_SYSOFF, F_TS_STAT, /* REG 6 */
51 F_VOVP, F_CLR_VDP, F_FORCE_BATDET, F_FORCE_PTM, /* REG 7 */
53 F_MAX_FIELDS
56 /* initial field values, converted from uV/uA */
57 struct bq24257_init_data {
58 u8 ichg; /* charge current */
59 u8 vbat; /* regulation voltage */
60 u8 iterm; /* termination current */
63 struct bq24257_state {
64 u8 status;
65 u8 fault;
66 bool power_good;
69 struct bq24257_device {
70 struct i2c_client *client;
71 struct device *dev;
72 struct power_supply *charger;
74 struct regmap *rmap;
75 struct regmap_field *rmap_fields[F_MAX_FIELDS];
77 struct gpio_desc *pg;
79 struct delayed_work iilimit_setup_work;
81 struct bq24257_init_data init_data;
82 struct bq24257_state state;
84 struct mutex lock; /* protect state data */
87 static bool bq24257_is_volatile_reg(struct device *dev, unsigned int reg)
89 switch (reg) {
90 case BQ24257_REG_2:
91 case BQ24257_REG_4:
92 return false;
94 default:
95 return true;
99 static const struct regmap_config bq24257_regmap_config = {
100 .reg_bits = 8,
101 .val_bits = 8,
103 .max_register = BQ24257_REG_7,
104 .cache_type = REGCACHE_RBTREE,
106 .volatile_reg = bq24257_is_volatile_reg,
109 static const struct reg_field bq24257_reg_fields[] = {
110 /* REG 1 */
111 [F_WD_FAULT] = REG_FIELD(BQ24257_REG_1, 7, 7),
112 [F_WD_EN] = REG_FIELD(BQ24257_REG_1, 6, 6),
113 [F_STAT] = REG_FIELD(BQ24257_REG_1, 4, 5),
114 [F_FAULT] = REG_FIELD(BQ24257_REG_1, 0, 3),
115 /* REG 2 */
116 [F_RESET] = REG_FIELD(BQ24257_REG_2, 7, 7),
117 [F_IILIMIT] = REG_FIELD(BQ24257_REG_2, 4, 6),
118 [F_EN_STAT] = REG_FIELD(BQ24257_REG_2, 3, 3),
119 [F_EN_TERM] = REG_FIELD(BQ24257_REG_2, 2, 2),
120 [F_CE] = REG_FIELD(BQ24257_REG_2, 1, 1),
121 [F_HZ_MODE] = REG_FIELD(BQ24257_REG_2, 0, 0),
122 /* REG 3 */
123 [F_VBAT] = REG_FIELD(BQ24257_REG_3, 2, 7),
124 [F_USB_DET] = REG_FIELD(BQ24257_REG_3, 0, 1),
125 /* REG 4 */
126 [F_ICHG] = REG_FIELD(BQ24257_REG_4, 3, 7),
127 [F_ITERM] = REG_FIELD(BQ24257_REG_4, 0, 2),
128 /* REG 5 */
129 [F_LOOP_STATUS] = REG_FIELD(BQ24257_REG_5, 6, 7),
130 [F_LOW_CHG] = REG_FIELD(BQ24257_REG_5, 5, 5),
131 [F_DPDM_EN] = REG_FIELD(BQ24257_REG_5, 4, 4),
132 [F_CE_STATUS] = REG_FIELD(BQ24257_REG_5, 3, 3),
133 [F_VINDPM] = REG_FIELD(BQ24257_REG_5, 0, 2),
134 /* REG 6 */
135 [F_X2_TMR_EN] = REG_FIELD(BQ24257_REG_6, 7, 7),
136 [F_TMR] = REG_FIELD(BQ24257_REG_6, 5, 6),
137 [F_SYSOFF] = REG_FIELD(BQ24257_REG_6, 4, 4),
138 [F_TS_STAT] = REG_FIELD(BQ24257_REG_6, 0, 2),
139 /* REG 7 */
140 [F_VOVP] = REG_FIELD(BQ24257_REG_7, 5, 7),
141 [F_CLR_VDP] = REG_FIELD(BQ24257_REG_7, 4, 4),
142 [F_FORCE_BATDET] = REG_FIELD(BQ24257_REG_7, 3, 3),
143 [F_FORCE_PTM] = REG_FIELD(BQ24257_REG_7, 2, 2)
146 static const u32 bq24257_vbat_map[] = {
147 3500000, 3520000, 3540000, 3560000, 3580000, 3600000, 3620000, 3640000,
148 3660000, 3680000, 3700000, 3720000, 3740000, 3760000, 3780000, 3800000,
149 3820000, 3840000, 3860000, 3880000, 3900000, 3920000, 3940000, 3960000,
150 3980000, 4000000, 4020000, 4040000, 4060000, 4080000, 4100000, 4120000,
151 4140000, 4160000, 4180000, 4200000, 4220000, 4240000, 4260000, 4280000,
152 4300000, 4320000, 4340000, 4360000, 4380000, 4400000, 4420000, 4440000
155 #define BQ24257_VBAT_MAP_SIZE ARRAY_SIZE(bq24257_vbat_map)
157 static const u32 bq24257_ichg_map[] = {
158 500000, 550000, 600000, 650000, 700000, 750000, 800000, 850000, 900000,
159 950000, 1000000, 1050000, 1100000, 1150000, 1200000, 1250000, 1300000,
160 1350000, 1400000, 1450000, 1500000, 1550000, 1600000, 1650000, 1700000,
161 1750000, 1800000, 1850000, 1900000, 1950000, 2000000
164 #define BQ24257_ICHG_MAP_SIZE ARRAY_SIZE(bq24257_ichg_map)
166 static const u32 bq24257_iterm_map[] = {
167 50000, 75000, 100000, 125000, 150000, 175000, 200000, 225000
170 #define BQ24257_ITERM_MAP_SIZE ARRAY_SIZE(bq24257_iterm_map)
172 static int bq24257_field_read(struct bq24257_device *bq,
173 enum bq24257_fields field_id)
175 int ret;
176 int val;
178 ret = regmap_field_read(bq->rmap_fields[field_id], &val);
179 if (ret < 0)
180 return ret;
182 return val;
185 static int bq24257_field_write(struct bq24257_device *bq,
186 enum bq24257_fields field_id, u8 val)
188 return regmap_field_write(bq->rmap_fields[field_id], val);
191 static u8 bq24257_find_idx(u32 value, const u32 *map, u8 map_size)
193 u8 idx;
195 for (idx = 1; idx < map_size; idx++)
196 if (value < map[idx])
197 break;
199 return idx - 1;
202 enum bq24257_status {
203 STATUS_READY,
204 STATUS_CHARGE_IN_PROGRESS,
205 STATUS_CHARGE_DONE,
206 STATUS_FAULT,
209 enum bq24257_fault {
210 FAULT_NORMAL,
211 FAULT_INPUT_OVP,
212 FAULT_INPUT_UVLO,
213 FAULT_SLEEP,
214 FAULT_BAT_TS,
215 FAULT_BAT_OVP,
216 FAULT_TS,
217 FAULT_TIMER,
218 FAULT_NO_BAT,
219 FAULT_ISET,
220 FAULT_INPUT_LDO_LOW,
223 static int bq24257_power_supply_get_property(struct power_supply *psy,
224 enum power_supply_property psp,
225 union power_supply_propval *val)
227 struct bq24257_device *bq = power_supply_get_drvdata(psy);
228 struct bq24257_state state;
230 mutex_lock(&bq->lock);
231 state = bq->state;
232 mutex_unlock(&bq->lock);
234 switch (psp) {
235 case POWER_SUPPLY_PROP_STATUS:
236 if (!state.power_good)
237 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
238 else if (state.status == STATUS_READY)
239 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
240 else if (state.status == STATUS_CHARGE_IN_PROGRESS)
241 val->intval = POWER_SUPPLY_STATUS_CHARGING;
242 else if (state.status == STATUS_CHARGE_DONE)
243 val->intval = POWER_SUPPLY_STATUS_FULL;
244 else
245 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
246 break;
248 case POWER_SUPPLY_PROP_MANUFACTURER:
249 val->strval = BQ24257_MANUFACTURER;
250 break;
252 case POWER_SUPPLY_PROP_ONLINE:
253 val->intval = state.power_good;
254 break;
256 case POWER_SUPPLY_PROP_HEALTH:
257 switch (state.fault) {
258 case FAULT_NORMAL:
259 val->intval = POWER_SUPPLY_HEALTH_GOOD;
260 break;
262 case FAULT_INPUT_OVP:
263 case FAULT_BAT_OVP:
264 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
265 break;
267 case FAULT_TS:
268 case FAULT_BAT_TS:
269 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
270 break;
272 case FAULT_TIMER:
273 val->intval = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
274 break;
276 default:
277 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
278 break;
281 break;
283 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
284 val->intval = bq24257_ichg_map[bq->init_data.ichg];
285 break;
287 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
288 val->intval = bq24257_ichg_map[BQ24257_ICHG_MAP_SIZE - 1];
289 break;
291 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
292 val->intval = bq24257_vbat_map[bq->init_data.vbat];
293 break;
295 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
296 val->intval = bq24257_vbat_map[BQ24257_VBAT_MAP_SIZE - 1];
297 break;
299 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
300 val->intval = bq24257_iterm_map[bq->init_data.iterm];
301 break;
303 default:
304 return -EINVAL;
307 return 0;
310 static int bq24257_get_chip_state(struct bq24257_device *bq,
311 struct bq24257_state *state)
313 int ret;
315 ret = bq24257_field_read(bq, F_STAT);
316 if (ret < 0)
317 return ret;
319 state->status = ret;
321 ret = bq24257_field_read(bq, F_FAULT);
322 if (ret < 0)
323 return ret;
325 state->fault = ret;
327 state->power_good = !gpiod_get_value_cansleep(bq->pg);
329 return 0;
332 static bool bq24257_state_changed(struct bq24257_device *bq,
333 struct bq24257_state *new_state)
335 int ret;
337 mutex_lock(&bq->lock);
338 ret = (bq->state.status != new_state->status ||
339 bq->state.fault != new_state->fault ||
340 bq->state.power_good != new_state->power_good);
341 mutex_unlock(&bq->lock);
343 return ret;
346 enum bq24257_loop_status {
347 LOOP_STATUS_NONE,
348 LOOP_STATUS_IN_DPM,
349 LOOP_STATUS_IN_CURRENT_LIMIT,
350 LOOP_STATUS_THERMAL,
353 enum bq24257_in_ilimit {
354 IILIMIT_100,
355 IILIMIT_150,
356 IILIMIT_500,
357 IILIMIT_900,
358 IILIMIT_1500,
359 IILIMIT_2000,
360 IILIMIT_EXT,
361 IILIMIT_NONE,
364 enum bq24257_port_type {
365 PORT_TYPE_DCP, /* Dedicated Charging Port */
366 PORT_TYPE_CDP, /* Charging Downstream Port */
367 PORT_TYPE_SDP, /* Standard Downstream Port */
368 PORT_TYPE_NON_STANDARD,
371 enum bq24257_safety_timer {
372 SAFETY_TIMER_45,
373 SAFETY_TIMER_360,
374 SAFETY_TIMER_540,
375 SAFETY_TIMER_NONE,
378 static int bq24257_iilimit_autoset(struct bq24257_device *bq)
380 int loop_status;
381 int iilimit;
382 int port_type;
383 int ret;
384 const u8 new_iilimit[] = {
385 [PORT_TYPE_DCP] = IILIMIT_2000,
386 [PORT_TYPE_CDP] = IILIMIT_2000,
387 [PORT_TYPE_SDP] = IILIMIT_500,
388 [PORT_TYPE_NON_STANDARD] = IILIMIT_500
391 ret = bq24257_field_read(bq, F_LOOP_STATUS);
392 if (ret < 0)
393 goto error;
395 loop_status = ret;
397 ret = bq24257_field_read(bq, F_IILIMIT);
398 if (ret < 0)
399 goto error;
401 iilimit = ret;
404 * All USB ports should be able to handle 500mA. If not, DPM will lower
405 * the charging current to accommodate the power source. No need to set
406 * a lower IILIMIT value.
408 if (loop_status == LOOP_STATUS_IN_DPM && iilimit == IILIMIT_500)
409 return 0;
411 ret = bq24257_field_read(bq, F_USB_DET);
412 if (ret < 0)
413 goto error;
415 port_type = ret;
417 ret = bq24257_field_write(bq, F_IILIMIT, new_iilimit[port_type]);
418 if (ret < 0)
419 goto error;
421 ret = bq24257_field_write(bq, F_TMR, SAFETY_TIMER_360);
422 if (ret < 0)
423 goto error;
425 ret = bq24257_field_write(bq, F_CLR_VDP, 1);
426 if (ret < 0)
427 goto error;
429 dev_dbg(bq->dev, "port/loop = %d/%d -> iilimit = %d\n",
430 port_type, loop_status, new_iilimit[port_type]);
432 return 0;
434 error:
435 dev_err(bq->dev, "%s: Error communicating with the chip.\n", __func__);
436 return ret;
439 static void bq24257_iilimit_setup_work(struct work_struct *work)
441 struct bq24257_device *bq = container_of(work, struct bq24257_device,
442 iilimit_setup_work.work);
444 bq24257_iilimit_autoset(bq);
447 static void bq24257_handle_state_change(struct bq24257_device *bq,
448 struct bq24257_state *new_state)
450 int ret;
451 struct bq24257_state old_state;
452 bool reset_iilimit = false;
453 bool config_iilimit = false;
455 mutex_lock(&bq->lock);
456 old_state = bq->state;
457 mutex_unlock(&bq->lock);
459 if (!new_state->power_good) { /* power removed */
460 cancel_delayed_work_sync(&bq->iilimit_setup_work);
462 /* activate D+/D- port detection algorithm */
463 ret = bq24257_field_write(bq, F_DPDM_EN, 1);
464 if (ret < 0)
465 goto error;
467 reset_iilimit = true;
468 } else if (!old_state.power_good) { /* power inserted */
469 config_iilimit = true;
470 } else if (new_state->fault == FAULT_NO_BAT) { /* battery removed */
471 cancel_delayed_work_sync(&bq->iilimit_setup_work);
473 reset_iilimit = true;
474 } else if (old_state.fault == FAULT_NO_BAT) { /* battery connected */
475 config_iilimit = true;
476 } else if (new_state->fault == FAULT_TIMER) { /* safety timer expired */
477 dev_err(bq->dev, "Safety timer expired! Battery dead?\n");
480 if (reset_iilimit) {
481 ret = bq24257_field_write(bq, F_IILIMIT, IILIMIT_500);
482 if (ret < 0)
483 goto error;
484 } else if (config_iilimit) {
485 schedule_delayed_work(&bq->iilimit_setup_work,
486 msecs_to_jiffies(BQ24257_ILIM_SET_DELAY));
489 return;
491 error:
492 dev_err(bq->dev, "%s: Error communicating with the chip.\n", __func__);
495 static irqreturn_t bq24257_irq_handler_thread(int irq, void *private)
497 int ret;
498 struct bq24257_device *bq = private;
499 struct bq24257_state state;
501 ret = bq24257_get_chip_state(bq, &state);
502 if (ret < 0)
503 return IRQ_HANDLED;
505 if (!bq24257_state_changed(bq, &state))
506 return IRQ_HANDLED;
508 dev_dbg(bq->dev, "irq(state changed): status/fault/pg = %d/%d/%d\n",
509 state.status, state.fault, state.power_good);
511 bq24257_handle_state_change(bq, &state);
513 mutex_lock(&bq->lock);
514 bq->state = state;
515 mutex_unlock(&bq->lock);
517 power_supply_changed(bq->charger);
519 return IRQ_HANDLED;
522 static int bq24257_hw_init(struct bq24257_device *bq)
524 int ret;
525 int i;
526 struct bq24257_state state;
528 const struct {
529 int field;
530 u32 value;
531 } init_data[] = {
532 {F_ICHG, bq->init_data.ichg},
533 {F_VBAT, bq->init_data.vbat},
534 {F_ITERM, bq->init_data.iterm}
538 * Disable the watchdog timer to prevent the IC from going back to
539 * default settings after 50 seconds of I2C inactivity.
541 ret = bq24257_field_write(bq, F_WD_EN, 0);
542 if (ret < 0)
543 return ret;
545 /* configure the charge currents and voltages */
546 for (i = 0; i < ARRAY_SIZE(init_data); i++) {
547 ret = bq24257_field_write(bq, init_data[i].field,
548 init_data[i].value);
549 if (ret < 0)
550 return ret;
553 ret = bq24257_get_chip_state(bq, &state);
554 if (ret < 0)
555 return ret;
557 mutex_lock(&bq->lock);
558 bq->state = state;
559 mutex_unlock(&bq->lock);
561 if (!state.power_good)
562 /* activate D+/D- detection algorithm */
563 ret = bq24257_field_write(bq, F_DPDM_EN, 1);
564 else if (state.fault != FAULT_NO_BAT)
565 ret = bq24257_iilimit_autoset(bq);
567 return ret;
570 static enum power_supply_property bq24257_power_supply_props[] = {
571 POWER_SUPPLY_PROP_MANUFACTURER,
572 POWER_SUPPLY_PROP_STATUS,
573 POWER_SUPPLY_PROP_ONLINE,
574 POWER_SUPPLY_PROP_HEALTH,
575 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
576 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
577 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
578 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
579 POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
582 static char *bq24257_charger_supplied_to[] = {
583 "main-battery",
586 static const struct power_supply_desc bq24257_power_supply_desc = {
587 .name = "bq24257-charger",
588 .type = POWER_SUPPLY_TYPE_USB,
589 .properties = bq24257_power_supply_props,
590 .num_properties = ARRAY_SIZE(bq24257_power_supply_props),
591 .get_property = bq24257_power_supply_get_property,
594 static int bq24257_power_supply_init(struct bq24257_device *bq)
596 struct power_supply_config psy_cfg = { .drv_data = bq, };
598 psy_cfg.supplied_to = bq24257_charger_supplied_to;
599 psy_cfg.num_supplicants = ARRAY_SIZE(bq24257_charger_supplied_to);
601 bq->charger = power_supply_register(bq->dev, &bq24257_power_supply_desc,
602 &psy_cfg);
603 if (IS_ERR(bq->charger))
604 return PTR_ERR(bq->charger);
606 return 0;
609 static int bq24257_irq_probe(struct bq24257_device *bq)
611 struct gpio_desc *stat_irq;
613 stat_irq = devm_gpiod_get_index(bq->dev, BQ24257_STAT_IRQ, 0, GPIOD_IN);
614 if (IS_ERR(stat_irq)) {
615 dev_err(bq->dev, "could not probe stat_irq pin\n");
616 return PTR_ERR(stat_irq);
619 return gpiod_to_irq(stat_irq);
622 static int bq24257_pg_gpio_probe(struct bq24257_device *bq)
624 bq->pg = devm_gpiod_get_index(bq->dev, BQ24257_PG_GPIO, 0, GPIOD_IN);
625 if (IS_ERR(bq->pg)) {
626 dev_err(bq->dev, "could not probe PG pin\n");
627 return PTR_ERR(bq->pg);
630 return 0;
633 static int bq24257_fw_probe(struct bq24257_device *bq)
635 int ret;
636 u32 property;
638 ret = device_property_read_u32(bq->dev, "ti,charge-current", &property);
639 if (ret < 0)
640 return ret;
642 bq->init_data.ichg = bq24257_find_idx(property, bq24257_ichg_map,
643 BQ24257_ICHG_MAP_SIZE);
645 ret = device_property_read_u32(bq->dev, "ti,battery-regulation-voltage",
646 &property);
647 if (ret < 0)
648 return ret;
650 bq->init_data.vbat = bq24257_find_idx(property, bq24257_vbat_map,
651 BQ24257_VBAT_MAP_SIZE);
653 ret = device_property_read_u32(bq->dev, "ti,termination-current",
654 &property);
655 if (ret < 0)
656 return ret;
658 bq->init_data.iterm = bq24257_find_idx(property, bq24257_iterm_map,
659 BQ24257_ITERM_MAP_SIZE);
661 return 0;
664 static int bq24257_probe(struct i2c_client *client,
665 const struct i2c_device_id *id)
667 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
668 struct device *dev = &client->dev;
669 struct bq24257_device *bq;
670 int ret;
671 int i;
673 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
674 dev_err(dev, "No support for SMBUS_BYTE_DATA\n");
675 return -ENODEV;
678 bq = devm_kzalloc(dev, sizeof(*bq), GFP_KERNEL);
679 if (!bq)
680 return -ENOMEM;
682 bq->client = client;
683 bq->dev = dev;
685 mutex_init(&bq->lock);
687 bq->rmap = devm_regmap_init_i2c(client, &bq24257_regmap_config);
688 if (IS_ERR(bq->rmap)) {
689 dev_err(dev, "failed to allocate register map\n");
690 return PTR_ERR(bq->rmap);
693 for (i = 0; i < ARRAY_SIZE(bq24257_reg_fields); i++) {
694 const struct reg_field *reg_fields = bq24257_reg_fields;
696 bq->rmap_fields[i] = devm_regmap_field_alloc(dev, bq->rmap,
697 reg_fields[i]);
698 if (IS_ERR(bq->rmap_fields[i])) {
699 dev_err(dev, "cannot allocate regmap field\n");
700 return PTR_ERR(bq->rmap_fields[i]);
704 i2c_set_clientdata(client, bq);
706 INIT_DELAYED_WORK(&bq->iilimit_setup_work, bq24257_iilimit_setup_work);
708 if (!dev->platform_data) {
709 ret = bq24257_fw_probe(bq);
710 if (ret < 0) {
711 dev_err(dev, "Cannot read device properties.\n");
712 return ret;
714 } else {
715 return -ENODEV;
718 /* we can only check Power Good status by probing the PG pin */
719 ret = bq24257_pg_gpio_probe(bq);
720 if (ret < 0)
721 return ret;
723 /* reset all registers to defaults */
724 ret = bq24257_field_write(bq, F_RESET, 1);
725 if (ret < 0)
726 return ret;
729 * Put the RESET bit back to 0, in cache. For some reason the HW always
730 * returns 1 on this bit, so this is the only way to avoid resetting the
731 * chip every time we update another field in this register.
733 ret = bq24257_field_write(bq, F_RESET, 0);
734 if (ret < 0)
735 return ret;
737 ret = bq24257_hw_init(bq);
738 if (ret < 0) {
739 dev_err(dev, "Cannot initialize the chip.\n");
740 return ret;
743 if (client->irq <= 0)
744 client->irq = bq24257_irq_probe(bq);
746 if (client->irq < 0) {
747 dev_err(dev, "no irq resource found\n");
748 return client->irq;
751 ret = devm_request_threaded_irq(dev, client->irq, NULL,
752 bq24257_irq_handler_thread,
753 IRQF_TRIGGER_FALLING |
754 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
755 BQ24257_STAT_IRQ, bq);
756 if (ret)
757 return ret;
759 ret = bq24257_power_supply_init(bq);
760 if (ret < 0)
761 dev_err(dev, "Failed to register power supply\n");
763 return ret;
766 static int bq24257_remove(struct i2c_client *client)
768 struct bq24257_device *bq = i2c_get_clientdata(client);
770 cancel_delayed_work_sync(&bq->iilimit_setup_work);
772 power_supply_unregister(bq->charger);
774 bq24257_field_write(bq, F_RESET, 1); /* reset to defaults */
776 return 0;
779 #ifdef CONFIG_PM_SLEEP
780 static int bq24257_suspend(struct device *dev)
782 struct bq24257_device *bq = dev_get_drvdata(dev);
783 int ret = 0;
785 cancel_delayed_work_sync(&bq->iilimit_setup_work);
787 /* reset all registers to default (and activate standalone mode) */
788 ret = bq24257_field_write(bq, F_RESET, 1);
789 if (ret < 0)
790 dev_err(bq->dev, "Cannot reset chip to standalone mode.\n");
792 return ret;
795 static int bq24257_resume(struct device *dev)
797 int ret;
798 struct bq24257_device *bq = dev_get_drvdata(dev);
800 ret = regcache_drop_region(bq->rmap, BQ24257_REG_1, BQ24257_REG_7);
801 if (ret < 0)
802 return ret;
804 ret = bq24257_field_write(bq, F_RESET, 0);
805 if (ret < 0)
806 return ret;
808 ret = bq24257_hw_init(bq);
809 if (ret < 0) {
810 dev_err(bq->dev, "Cannot init chip after resume.\n");
811 return ret;
814 /* signal userspace, maybe state changed while suspended */
815 power_supply_changed(bq->charger);
817 return 0;
819 #endif
821 static const struct dev_pm_ops bq24257_pm = {
822 SET_SYSTEM_SLEEP_PM_OPS(bq24257_suspend, bq24257_resume)
825 static const struct i2c_device_id bq24257_i2c_ids[] = {
826 { "bq24257", 0 },
829 MODULE_DEVICE_TABLE(i2c, bq24257_i2c_ids);
831 static const struct of_device_id bq24257_of_match[] = {
832 { .compatible = "ti,bq24257", },
833 { },
835 MODULE_DEVICE_TABLE(of, bq24257_of_match);
837 static const struct acpi_device_id bq24257_acpi_match[] = {
838 {"BQ242570", 0},
841 MODULE_DEVICE_TABLE(acpi, bq24257_acpi_match);
843 static struct i2c_driver bq24257_driver = {
844 .driver = {
845 .name = "bq24257-charger",
846 .of_match_table = of_match_ptr(bq24257_of_match),
847 .acpi_match_table = ACPI_PTR(bq24257_acpi_match),
848 .pm = &bq24257_pm,
850 .probe = bq24257_probe,
851 .remove = bq24257_remove,
852 .id_table = bq24257_i2c_ids,
854 module_i2c_driver(bq24257_driver);
856 MODULE_AUTHOR("Laurentiu Palcu <laurentiu.palcu@intel.com>");
857 MODULE_DESCRIPTION("bq24257 charger driver");
858 MODULE_LICENSE("GPL");