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.
17 * http://www.ti.com/product/bq24250
18 * http://www.ti.com/product/bq24251
19 * http://www.ti.com/product/bq24257
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>
31 #include <linux/acpi.h>
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
42 #define BQ24257_MANUFACTURER "Texas Instruments"
43 #define BQ24257_PG_GPIO "pg"
45 #define BQ24257_ILIM_SET_DELAY 1000 /* msec */
48 * When adding support for new devices make sure that enum bq2425x_chip and
49 * bq2425x_chip_name[] always stay in sync!
57 static const char *const bq2425x_chip_name
[] = {
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 */
69 F_X2_TMR_EN
, F_TMR
, F_SYSOFF
, F_TS_EN
, F_TS_STAT
, /* REG 6 */
70 F_VOVP
, F_CLR_VDP
, F_FORCE_BATDET
, F_FORCE_PTM
, /* REG 7 */
75 /* initial field values, converted from uV/uA */
76 struct bq24257_init_data
{
77 u8 ichg
; /* charge current */
78 u8 vbat
; /* regulation voltage */
79 u8 iterm
; /* termination current */
80 u8 iilimit
; /* input current limit */
81 u8 vovp
; /* over voltage protection voltage */
82 u8 vindpm
; /* VDMP input threshold voltage */
85 struct bq24257_state
{
91 struct bq24257_device
{
92 struct i2c_client
*client
;
94 struct power_supply
*charger
;
96 enum bq2425x_chip chip
;
99 struct regmap_field
*rmap_fields
[F_MAX_FIELDS
];
101 struct gpio_desc
*pg
;
103 struct delayed_work iilimit_setup_work
;
105 struct bq24257_init_data init_data
;
106 struct bq24257_state state
;
108 struct mutex lock
; /* protect state data */
110 bool iilimit_autoset_enable
;
113 static bool bq24257_is_volatile_reg(struct device
*dev
, unsigned int reg
)
125 static const struct regmap_config bq24257_regmap_config
= {
129 .max_register
= BQ24257_REG_7
,
130 .cache_type
= REGCACHE_RBTREE
,
132 .volatile_reg
= bq24257_is_volatile_reg
,
135 static const struct reg_field bq24257_reg_fields
[] = {
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),
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),
149 [F_VBAT
] = REG_FIELD(BQ24257_REG_3
, 2, 7),
150 [F_USB_DET
] = REG_FIELD(BQ24257_REG_3
, 0, 1),
152 [F_ICHG
] = REG_FIELD(BQ24257_REG_4
, 3, 7),
153 [F_ITERM
] = REG_FIELD(BQ24257_REG_4
, 0, 2),
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),
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),
164 [F_TS_EN
] = REG_FIELD(BQ24257_REG_6
, 3, 3),
165 [F_TS_STAT
] = REG_FIELD(BQ24257_REG_6
, 0, 2),
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)
173 static 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
182 #define BQ24257_VBAT_MAP_SIZE ARRAY_SIZE(bq24257_vbat_map)
184 static 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
191 #define BQ24257_ICHG_MAP_SIZE ARRAY_SIZE(bq24257_ichg_map)
193 static const u32 bq24257_iterm_map
[] = {
194 50000, 75000, 100000, 125000, 150000, 175000, 200000, 225000
197 #define BQ24257_ITERM_MAP_SIZE ARRAY_SIZE(bq24257_iterm_map)
199 static const u32 bq24257_iilimit_map
[] = {
200 100000, 150000, 500000, 900000, 1500000, 2000000
203 #define BQ24257_IILIMIT_MAP_SIZE ARRAY_SIZE(bq24257_iilimit_map)
205 static const u32 bq24257_vovp_map
[] = {
206 6000000, 6500000, 7000000, 8000000, 9000000, 9500000, 10000000,
210 #define BQ24257_VOVP_MAP_SIZE ARRAY_SIZE(bq24257_vovp_map)
212 static const u32 bq24257_vindpm_map
[] = {
213 4200000, 4280000, 4360000, 4440000, 4520000, 4600000, 4680000,
217 #define BQ24257_VINDPM_MAP_SIZE ARRAY_SIZE(bq24257_vindpm_map)
219 static int bq24257_field_read(struct bq24257_device
*bq
,
220 enum bq24257_fields field_id
)
225 ret
= regmap_field_read(bq
->rmap_fields
[field_id
], &val
);
232 static int bq24257_field_write(struct bq24257_device
*bq
,
233 enum bq24257_fields field_id
, u8 val
)
235 return regmap_field_write(bq
->rmap_fields
[field_id
], val
);
238 static u8
bq24257_find_idx(u32 value
, const u32
*map
, u8 map_size
)
242 for (idx
= 1; idx
< map_size
; idx
++)
243 if (value
< map
[idx
])
249 enum bq24257_status
{
251 STATUS_CHARGE_IN_PROGRESS
,
270 static int bq24257_get_input_current_limit(struct bq24257_device
*bq
,
271 union power_supply_propval
*val
)
275 ret
= bq24257_field_read(bq
, F_IILIMIT
);
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
286 if (ret
>= BQ24257_IILIMIT_MAP_SIZE
)
289 val
->intval
= bq24257_iilimit_map
[ret
];
294 static int bq24257_set_input_current_limit(struct bq24257_device
*bq
,
295 const union power_supply_propval
*val
)
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.
302 if (bq
->iilimit_autoset_enable
)
303 cancel_delayed_work_sync(&bq
->iilimit_setup_work
);
305 return bq24257_field_write(bq
, F_IILIMIT
,
306 bq24257_find_idx(val
->intval
,
308 BQ24257_IILIMIT_MAP_SIZE
));
311 static int bq24257_power_supply_get_property(struct power_supply
*psy
,
312 enum power_supply_property psp
,
313 union power_supply_propval
*val
)
315 struct bq24257_device
*bq
= power_supply_get_drvdata(psy
);
316 struct bq24257_state state
;
318 mutex_lock(&bq
->lock
);
320 mutex_unlock(&bq
->lock
);
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
;
333 val
->intval
= POWER_SUPPLY_STATUS_UNKNOWN
;
336 case POWER_SUPPLY_PROP_MANUFACTURER
:
337 val
->strval
= BQ24257_MANUFACTURER
;
340 case POWER_SUPPLY_PROP_MODEL_NAME
:
341 val
->strval
= bq2425x_chip_name
[bq
->chip
];
344 case POWER_SUPPLY_PROP_ONLINE
:
345 val
->intval
= state
.power_good
;
348 case POWER_SUPPLY_PROP_HEALTH
:
349 switch (state
.fault
) {
351 val
->intval
= POWER_SUPPLY_HEALTH_GOOD
;
354 case FAULT_INPUT_OVP
:
356 val
->intval
= POWER_SUPPLY_HEALTH_OVERVOLTAGE
;
361 val
->intval
= POWER_SUPPLY_HEALTH_OVERHEAT
;
365 val
->intval
= POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE
;
369 val
->intval
= POWER_SUPPLY_HEALTH_UNSPEC_FAILURE
;
375 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT
:
376 val
->intval
= bq24257_ichg_map
[bq
->init_data
.ichg
];
379 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX
:
380 val
->intval
= bq24257_ichg_map
[BQ24257_ICHG_MAP_SIZE
- 1];
383 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE
:
384 val
->intval
= bq24257_vbat_map
[bq
->init_data
.vbat
];
387 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX
:
388 val
->intval
= bq24257_vbat_map
[BQ24257_VBAT_MAP_SIZE
- 1];
391 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT
:
392 val
->intval
= bq24257_iterm_map
[bq
->init_data
.iterm
];
395 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
:
396 return bq24257_get_input_current_limit(bq
, val
);
405 static int bq24257_power_supply_set_property(struct power_supply
*psy
,
406 enum power_supply_property prop
,
407 const union power_supply_propval
*val
)
409 struct bq24257_device
*bq
= power_supply_get_drvdata(psy
);
412 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
:
413 return bq24257_set_input_current_limit(bq
, val
);
419 static int bq24257_power_supply_property_is_writeable(struct power_supply
*psy
,
420 enum power_supply_property psp
)
423 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
:
430 static int bq24257_get_chip_state(struct bq24257_device
*bq
,
431 struct bq24257_state
*state
)
435 ret
= bq24257_field_read(bq
, F_STAT
);
441 ret
= bq24257_field_read(bq
, F_FAULT
);
448 state
->power_good
= !gpiod_get_value_cansleep(bq
->pg
);
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.
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;
465 state
->power_good
= true;
471 static bool bq24257_state_changed(struct bq24257_device
*bq
,
472 struct bq24257_state
*new_state
)
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
);
485 enum bq24257_loop_status
{
488 LOOP_STATUS_IN_CURRENT_LIMIT
,
492 enum bq24257_in_ilimit
{
514 enum bq24257_vindpm
{
525 enum 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
,
532 enum bq24257_safety_timer
{
539 static int bq24257_iilimit_autoset(struct bq24257_device
*bq
)
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
552 ret
= bq24257_field_read(bq
, F_LOOP_STATUS
);
558 ret
= bq24257_field_read(bq
, F_IILIMIT
);
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.
569 if (loop_status
== LOOP_STATUS_IN_DPM
&& iilimit
== IILIMIT_500
)
572 ret
= bq24257_field_read(bq
, F_USB_DET
);
578 ret
= bq24257_field_write(bq
, F_IILIMIT
, new_iilimit
[port_type
]);
582 ret
= bq24257_field_write(bq
, F_TMR
, SAFETY_TIMER_360
);
586 ret
= bq24257_field_write(bq
, F_CLR_VDP
, 1);
590 dev_dbg(bq
->dev
, "port/loop = %d/%d -> iilimit = %d\n",
591 port_type
, loop_status
, new_iilimit
[port_type
]);
596 dev_err(bq
->dev
, "%s: Error communicating with the chip.\n", __func__
);
600 static void bq24257_iilimit_setup_work(struct work_struct
*work
)
602 struct bq24257_device
*bq
= container_of(work
, struct bq24257_device
,
603 iilimit_setup_work
.work
);
605 bq24257_iilimit_autoset(bq
);
608 static void bq24257_handle_state_change(struct bq24257_device
*bq
,
609 struct bq24257_state
*new_state
)
612 struct bq24257_state old_state
;
614 mutex_lock(&bq
->lock
);
615 old_state
= bq
->state
;
616 mutex_unlock(&bq
->lock
);
619 * Handle BQ2425x state changes observing whether the D+/D- based input
620 * current limit autoset functionality is enabled.
622 if (!new_state
->power_good
) {
623 dev_dbg(bq
->dev
, "Power removed\n");
624 if (bq
->iilimit_autoset_enable
) {
625 cancel_delayed_work_sync(&bq
->iilimit_setup_work
);
627 /* activate D+/D- port detection algorithm */
628 ret
= bq24257_field_write(bq
, F_DPDM_EN
, 1);
633 * When power is removed always return to the default input
634 * current limit as configured during probe.
636 ret
= bq24257_field_write(bq
, F_IILIMIT
, bq
->init_data
.iilimit
);
639 } else if (!old_state
.power_good
) {
640 dev_dbg(bq
->dev
, "Power inserted\n");
642 if (bq
->iilimit_autoset_enable
)
643 /* configure input current limit */
644 schedule_delayed_work(&bq
->iilimit_setup_work
,
645 msecs_to_jiffies(BQ24257_ILIM_SET_DELAY
));
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");
655 dev_err(bq
->dev
, "%s: Error communicating with the chip.\n", __func__
);
658 static irqreturn_t
bq24257_irq_handler_thread(int irq
, void *private)
661 struct bq24257_device
*bq
= private;
662 struct bq24257_state state
;
664 ret
= bq24257_get_chip_state(bq
, &state
);
668 if (!bq24257_state_changed(bq
, &state
))
671 dev_dbg(bq
->dev
, "irq(state changed): status/fault/pg = %d/%d/%d\n",
672 state
.status
, state
.fault
, state
.power_good
);
674 bq24257_handle_state_change(bq
, &state
);
676 mutex_lock(&bq
->lock
);
678 mutex_unlock(&bq
->lock
);
680 power_supply_changed(bq
->charger
);
685 static int bq24257_hw_init(struct bq24257_device
*bq
)
689 struct bq24257_state state
;
695 {F_ICHG
, bq
->init_data
.ichg
},
696 {F_VBAT
, bq
->init_data
.vbat
},
697 {F_ITERM
, bq
->init_data
.iterm
},
698 {F_VOVP
, bq
->init_data
.vovp
},
699 {F_VINDPM
, bq
->init_data
.vindpm
},
703 * Disable the watchdog timer to prevent the IC from going back to
704 * default settings after 50 seconds of I2C inactivity.
706 ret
= bq24257_field_write(bq
, F_WD_EN
, 0);
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
,
718 ret
= bq24257_get_chip_state(bq
, &state
);
722 mutex_lock(&bq
->lock
);
724 mutex_unlock(&bq
->lock
);
726 if (!bq
->iilimit_autoset_enable
) {
727 dev_dbg(bq
->dev
, "manually setting iilimit = %u\n",
728 bq
->init_data
.iilimit
);
730 /* program fixed input current limit */
731 ret
= bq24257_field_write(bq
, F_IILIMIT
,
732 bq
->init_data
.iilimit
);
735 } else if (!state
.power_good
)
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
);
744 static enum power_supply_property bq24257_power_supply_props
[] = {
745 POWER_SUPPLY_PROP_MANUFACTURER
,
746 POWER_SUPPLY_PROP_MODEL_NAME
,
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
,
755 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
,
758 static char *bq24257_charger_supplied_to
[] = {
762 static 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
,
768 .set_property
= bq24257_power_supply_set_property
,
769 .property_is_writeable
= bq24257_power_supply_property_is_writeable
,
772 static ssize_t
bq24257_show_ovp_voltage(struct device
*dev
,
773 struct device_attribute
*attr
,
776 struct power_supply
*psy
= dev_get_drvdata(dev
);
777 struct bq24257_device
*bq
= power_supply_get_drvdata(psy
);
779 return scnprintf(buf
, PAGE_SIZE
, "%u\n",
780 bq24257_vovp_map
[bq
->init_data
.vovp
]);
783 static ssize_t
bq24257_show_in_dpm_voltage(struct device
*dev
,
784 struct device_attribute
*attr
,
787 struct power_supply
*psy
= dev_get_drvdata(dev
);
788 struct bq24257_device
*bq
= power_supply_get_drvdata(psy
);
790 return scnprintf(buf
, PAGE_SIZE
, "%u\n",
791 bq24257_vindpm_map
[bq
->init_data
.vindpm
]);
794 static ssize_t
bq24257_sysfs_show_enable(struct device
*dev
,
795 struct device_attribute
*attr
,
798 struct power_supply
*psy
= dev_get_drvdata(dev
);
799 struct bq24257_device
*bq
= power_supply_get_drvdata(psy
);
802 if (strcmp(attr
->attr
.name
, "high_impedance_enable") == 0)
803 ret
= bq24257_field_read(bq
, F_HZ_MODE
);
804 else if (strcmp(attr
->attr
.name
, "sysoff_enable") == 0)
805 ret
= bq24257_field_read(bq
, F_SYSOFF
);
812 return scnprintf(buf
, PAGE_SIZE
, "%d\n", ret
);
815 static ssize_t
bq24257_sysfs_set_enable(struct device
*dev
,
816 struct device_attribute
*attr
,
820 struct power_supply
*psy
= dev_get_drvdata(dev
);
821 struct bq24257_device
*bq
= power_supply_get_drvdata(psy
);
825 if (kstrtol(buf
, 10, &val
) < 0)
828 if (strcmp(attr
->attr
.name
, "high_impedance_enable") == 0)
829 ret
= bq24257_field_write(bq
, F_HZ_MODE
, (bool)val
);
830 else if (strcmp(attr
->attr
.name
, "sysoff_enable") == 0)
831 ret
= bq24257_field_write(bq
, F_SYSOFF
, (bool)val
);
841 static DEVICE_ATTR(ovp_voltage
, S_IRUGO
, bq24257_show_ovp_voltage
, NULL
);
842 static DEVICE_ATTR(in_dpm_voltage
, S_IRUGO
, bq24257_show_in_dpm_voltage
, NULL
);
843 static DEVICE_ATTR(high_impedance_enable
, S_IWUSR
| S_IRUGO
,
844 bq24257_sysfs_show_enable
, bq24257_sysfs_set_enable
);
845 static DEVICE_ATTR(sysoff_enable
, S_IWUSR
| S_IRUGO
,
846 bq24257_sysfs_show_enable
, bq24257_sysfs_set_enable
);
848 static struct attribute
*bq24257_charger_attr
[] = {
849 &dev_attr_ovp_voltage
.attr
,
850 &dev_attr_in_dpm_voltage
.attr
,
851 &dev_attr_high_impedance_enable
.attr
,
852 &dev_attr_sysoff_enable
.attr
,
856 static const struct attribute_group bq24257_attr_group
= {
857 .attrs
= bq24257_charger_attr
,
860 static int bq24257_power_supply_init(struct bq24257_device
*bq
)
862 struct power_supply_config psy_cfg
= { .drv_data
= bq
, };
864 psy_cfg
.supplied_to
= bq24257_charger_supplied_to
;
865 psy_cfg
.num_supplicants
= ARRAY_SIZE(bq24257_charger_supplied_to
);
867 bq
->charger
= devm_power_supply_register(bq
->dev
,
868 &bq24257_power_supply_desc
,
871 return PTR_ERR_OR_ZERO(bq
->charger
);
874 static void bq24257_pg_gpio_probe(struct bq24257_device
*bq
)
876 bq
->pg
= devm_gpiod_get_optional(bq
->dev
, BQ24257_PG_GPIO
, GPIOD_IN
);
878 if (PTR_ERR(bq
->pg
) == -EPROBE_DEFER
) {
879 dev_info(bq
->dev
, "probe retry requested for PG pin\n");
881 } else if (IS_ERR(bq
->pg
)) {
882 dev_err(bq
->dev
, "error probing PG pin\n");
888 dev_dbg(bq
->dev
, "probed PG pin = %d\n", desc_to_gpio(bq
->pg
));
891 static int bq24257_fw_probe(struct bq24257_device
*bq
)
896 /* Required properties */
897 ret
= device_property_read_u32(bq
->dev
, "ti,charge-current", &property
);
901 bq
->init_data
.ichg
= bq24257_find_idx(property
, bq24257_ichg_map
,
902 BQ24257_ICHG_MAP_SIZE
);
904 ret
= device_property_read_u32(bq
->dev
, "ti,battery-regulation-voltage",
909 bq
->init_data
.vbat
= bq24257_find_idx(property
, bq24257_vbat_map
,
910 BQ24257_VBAT_MAP_SIZE
);
912 ret
= device_property_read_u32(bq
->dev
, "ti,termination-current",
917 bq
->init_data
.iterm
= bq24257_find_idx(property
, bq24257_iterm_map
,
918 BQ24257_ITERM_MAP_SIZE
);
920 /* Optional properties. If not provided use reasonable default. */
921 ret
= device_property_read_u32(bq
->dev
, "ti,current-limit",
924 bq
->iilimit_autoset_enable
= true;
927 * Explicitly set a default value which will be needed for
928 * devices that don't support the automatic setting of the input
929 * current limit through the charger type detection mechanism.
931 bq
->init_data
.iilimit
= IILIMIT_500
;
933 bq
->init_data
.iilimit
=
934 bq24257_find_idx(property
,
936 BQ24257_IILIMIT_MAP_SIZE
);
938 ret
= device_property_read_u32(bq
->dev
, "ti,ovp-voltage",
941 bq
->init_data
.vovp
= VOVP_6500
;
943 bq
->init_data
.vovp
= bq24257_find_idx(property
,
945 BQ24257_VOVP_MAP_SIZE
);
947 ret
= device_property_read_u32(bq
->dev
, "ti,in-dpm-voltage",
950 bq
->init_data
.vindpm
= VINDPM_4360
;
952 bq
->init_data
.vindpm
=
953 bq24257_find_idx(property
,
955 BQ24257_VINDPM_MAP_SIZE
);
960 static int bq24257_probe(struct i2c_client
*client
,
961 const struct i2c_device_id
*id
)
963 struct i2c_adapter
*adapter
= to_i2c_adapter(client
->dev
.parent
);
964 struct device
*dev
= &client
->dev
;
965 const struct acpi_device_id
*acpi_id
;
966 struct bq24257_device
*bq
;
970 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
)) {
971 dev_err(dev
, "No support for SMBUS_BYTE_DATA\n");
975 bq
= devm_kzalloc(dev
, sizeof(*bq
), GFP_KERNEL
);
982 if (ACPI_HANDLE(dev
)) {
983 acpi_id
= acpi_match_device(dev
->driver
->acpi_match_table
,
986 dev_err(dev
, "Failed to match ACPI device\n");
989 bq
->chip
= (enum bq2425x_chip
)acpi_id
->driver_data
;
991 bq
->chip
= (enum bq2425x_chip
)id
->driver_data
;
994 mutex_init(&bq
->lock
);
996 bq
->rmap
= devm_regmap_init_i2c(client
, &bq24257_regmap_config
);
997 if (IS_ERR(bq
->rmap
)) {
998 dev_err(dev
, "failed to allocate register map\n");
999 return PTR_ERR(bq
->rmap
);
1002 for (i
= 0; i
< ARRAY_SIZE(bq24257_reg_fields
); i
++) {
1003 const struct reg_field
*reg_fields
= bq24257_reg_fields
;
1005 bq
->rmap_fields
[i
] = devm_regmap_field_alloc(dev
, bq
->rmap
,
1007 if (IS_ERR(bq
->rmap_fields
[i
])) {
1008 dev_err(dev
, "cannot allocate regmap field\n");
1009 return PTR_ERR(bq
->rmap_fields
[i
]);
1013 i2c_set_clientdata(client
, bq
);
1015 if (!dev
->platform_data
) {
1016 ret
= bq24257_fw_probe(bq
);
1018 dev_err(dev
, "Cannot read device properties.\n");
1026 * The BQ24250 doesn't support the D+/D- based charger type detection
1027 * used for the automatic setting of the input current limit setting so
1028 * explicitly disable that feature.
1030 if (bq
->chip
== BQ24250
)
1031 bq
->iilimit_autoset_enable
= false;
1033 if (bq
->iilimit_autoset_enable
)
1034 INIT_DELAYED_WORK(&bq
->iilimit_setup_work
,
1035 bq24257_iilimit_setup_work
);
1038 * The BQ24250 doesn't have a dedicated Power Good (PG) pin so let's
1039 * not probe for it and instead use a SW-based approach to determine
1040 * the PG state. We also use a SW-based approach for all other devices
1041 * if the PG pin is either not defined or can't be probed.
1043 if (bq
->chip
!= BQ24250
)
1044 bq24257_pg_gpio_probe(bq
);
1046 if (PTR_ERR(bq
->pg
) == -EPROBE_DEFER
)
1047 return PTR_ERR(bq
->pg
);
1049 dev_info(bq
->dev
, "using SW-based power-good detection\n");
1051 /* reset all registers to defaults */
1052 ret
= bq24257_field_write(bq
, F_RESET
, 1);
1057 * Put the RESET bit back to 0, in cache. For some reason the HW always
1058 * returns 1 on this bit, so this is the only way to avoid resetting the
1059 * chip every time we update another field in this register.
1061 ret
= bq24257_field_write(bq
, F_RESET
, 0);
1065 ret
= bq24257_hw_init(bq
);
1067 dev_err(dev
, "Cannot initialize the chip.\n");
1071 ret
= bq24257_power_supply_init(bq
);
1073 dev_err(dev
, "Failed to register power supply\n");
1077 ret
= devm_request_threaded_irq(dev
, client
->irq
, NULL
,
1078 bq24257_irq_handler_thread
,
1079 IRQF_TRIGGER_FALLING
|
1080 IRQF_TRIGGER_RISING
| IRQF_ONESHOT
,
1081 bq2425x_chip_name
[bq
->chip
], bq
);
1083 dev_err(dev
, "Failed to request IRQ #%d\n", client
->irq
);
1087 ret
= sysfs_create_group(&bq
->charger
->dev
.kobj
, &bq24257_attr_group
);
1089 dev_err(dev
, "Can't create sysfs entries\n");
1096 static int bq24257_remove(struct i2c_client
*client
)
1098 struct bq24257_device
*bq
= i2c_get_clientdata(client
);
1100 if (bq
->iilimit_autoset_enable
)
1101 cancel_delayed_work_sync(&bq
->iilimit_setup_work
);
1103 sysfs_remove_group(&bq
->charger
->dev
.kobj
, &bq24257_attr_group
);
1105 bq24257_field_write(bq
, F_RESET
, 1); /* reset to defaults */
1110 #ifdef CONFIG_PM_SLEEP
1111 static int bq24257_suspend(struct device
*dev
)
1113 struct bq24257_device
*bq
= dev_get_drvdata(dev
);
1116 if (bq
->iilimit_autoset_enable
)
1117 cancel_delayed_work_sync(&bq
->iilimit_setup_work
);
1119 /* reset all registers to default (and activate standalone mode) */
1120 ret
= bq24257_field_write(bq
, F_RESET
, 1);
1122 dev_err(bq
->dev
, "Cannot reset chip to standalone mode.\n");
1127 static int bq24257_resume(struct device
*dev
)
1130 struct bq24257_device
*bq
= dev_get_drvdata(dev
);
1132 ret
= regcache_drop_region(bq
->rmap
, BQ24257_REG_1
, BQ24257_REG_7
);
1136 ret
= bq24257_field_write(bq
, F_RESET
, 0);
1140 ret
= bq24257_hw_init(bq
);
1142 dev_err(bq
->dev
, "Cannot init chip after resume.\n");
1146 /* signal userspace, maybe state changed while suspended */
1147 power_supply_changed(bq
->charger
);
1153 static const struct dev_pm_ops bq24257_pm
= {
1154 SET_SYSTEM_SLEEP_PM_OPS(bq24257_suspend
, bq24257_resume
)
1157 static const struct i2c_device_id bq24257_i2c_ids
[] = {
1158 { "bq24250", BQ24250
},
1159 { "bq24251", BQ24251
},
1160 { "bq24257", BQ24257
},
1163 MODULE_DEVICE_TABLE(i2c
, bq24257_i2c_ids
);
1165 static const struct of_device_id bq24257_of_match
[] = {
1166 { .compatible
= "ti,bq24250", },
1167 { .compatible
= "ti,bq24251", },
1168 { .compatible
= "ti,bq24257", },
1171 MODULE_DEVICE_TABLE(of
, bq24257_of_match
);
1173 static const struct acpi_device_id bq24257_acpi_match
[] = {
1174 { "BQ242500", BQ24250
},
1175 { "BQ242510", BQ24251
},
1176 { "BQ242570", BQ24257
},
1179 MODULE_DEVICE_TABLE(acpi
, bq24257_acpi_match
);
1181 static struct i2c_driver bq24257_driver
= {
1183 .name
= "bq24257-charger",
1184 .of_match_table
= of_match_ptr(bq24257_of_match
),
1185 .acpi_match_table
= ACPI_PTR(bq24257_acpi_match
),
1188 .probe
= bq24257_probe
,
1189 .remove
= bq24257_remove
,
1190 .id_table
= bq24257_i2c_ids
,
1192 module_i2c_driver(bq24257_driver
);
1194 MODULE_AUTHOR("Laurentiu Palcu <laurentiu.palcu@intel.com>");
1195 MODULE_DESCRIPTION("bq24257 charger driver");
1196 MODULE_LICENSE("GPL");