1 // SPDX-License-Identifier: GPL-2.0-only
3 * ROHM BD99954 charger driver
5 * Copyright (C) 2020 Rohm Semiconductors
6 * Originally written by:
7 * Mikko Mutanen <mikko.mutanen@fi.rohmeurope.com>
8 * Markus Laine <markus.laine@fi.rohmeurope.com>
10 * Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>
14 * The battery charging profile of BD99954.
16 * Curve (1) represents charging current.
17 * Curve (2) represents battery voltage.
19 * The BD99954 data sheet divides charging to three phases.
20 * a) Trickle-charge with constant current (8).
21 * b) pre-charge with constant current (6)
22 * c) fast-charge, first with constant current (5) phase. After
23 * the battery voltage has reached target level (4) we have constant
24 * voltage phase until charging current has dropped to termination
30 *(4)` `.` ` ` ` ` ` ` ` ` ` ` ` ` ` ----------------------------.
32 * . o----+/:/ ` ` ` ` ` ` ` ` ` ` ` ` `.` ` (5)
43 *(3)` `.`."" ` ` ` `+-------- ` ` ` ` ` ` `.:` ` ` ` ` ` ` ` ` .` ` (6)
48 * . (1) + `.+` ` ` `.` ` (7)
49 * -..............` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` + ` ` ` .` ` (8)
51 * -------------------------------------------------+++++++++-->
52 * | trickle | pre | fast |
54 * Details of DT properties for different limits can be found from BD99954
55 * device tree binding documentation.
58 #include <linux/delay.h>
59 #include <linux/gpio/consumer.h>
60 #include <linux/interrupt.h>
61 #include <linux/i2c.h>
62 #include <linux/kernel.h>
63 #include <linux/linear_range.h>
64 #include <linux/module.h>
65 #include <linux/mod_devicetable.h>
66 #include <linux/power_supply.h>
67 #include <linux/property.h>
68 #include <linux/regmap.h>
69 #include <linux/types.h>
71 #include "bd99954-charger.h"
74 u16 precharge_current
; /* Trickle-charge Current */
75 u16 fc_reg_voltage
; /* Fast Charging Regulation Voltage */
80 /* Initial field values, converted to initial register values */
81 struct bd9995x_init_data
{
82 u16 vsysreg_set
; /* VSYS Regulation Setting */
83 u16 ibus_lim_set
; /* VBUS input current limitation */
84 u16 icc_lim_set
; /* VCC/VACP Input Current Limit Setting */
85 u16 itrich_set
; /* Trickle-charge Current Setting */
86 u16 iprech_set
; /* Pre-Charge Current Setting */
87 u16 ichg_set
; /* Fast-Charge constant current */
88 u16 vfastchg_reg_set1
; /* Fast Charging Regulation Voltage */
89 u16 vprechg_th_set
; /* Pre-charge Voltage Threshold Setting */
90 u16 vrechg_set
; /* Re-charge Battery Voltage Setting */
91 u16 vbatovp_set
; /* Battery Over Voltage Threshold Setting */
92 u16 iterm_set
; /* Charging termination current */
95 struct bd9995x_state
{
102 struct bd9995x_device
{
103 struct i2c_client
*client
;
105 struct power_supply
*charger
;
108 struct regmap_field
*rmap_fields
[F_MAX_FIELDS
];
112 struct bd9995x_init_data init_data
;
113 struct bd9995x_state state
;
115 struct mutex lock
; /* Protect state data */
118 static const struct regmap_range bd9995x_readonly_reg_ranges
[] = {
119 regmap_reg_range(CHGSTM_STATUS
, SEL_ILIM_VAL
),
120 regmap_reg_range(IOUT_DACIN_VAL
, IOUT_DACIN_VAL
),
121 regmap_reg_range(VCC_UCD_STATUS
, VCC_IDD_STATUS
),
122 regmap_reg_range(VBUS_UCD_STATUS
, VBUS_IDD_STATUS
),
123 regmap_reg_range(CHIP_ID
, CHIP_REV
),
124 regmap_reg_range(SYSTEM_STATUS
, SYSTEM_STATUS
),
125 regmap_reg_range(IBATP_VAL
, VBAT_AVE_VAL
),
126 regmap_reg_range(VTH_VAL
, EXTIADP_AVE_VAL
),
129 static const struct regmap_access_table bd9995x_writeable_regs
= {
130 .no_ranges
= bd9995x_readonly_reg_ranges
,
131 .n_no_ranges
= ARRAY_SIZE(bd9995x_readonly_reg_ranges
),
134 static const struct regmap_range bd9995x_volatile_reg_ranges
[] = {
135 regmap_reg_range(CHGSTM_STATUS
, WDT_STATUS
),
136 regmap_reg_range(VCC_UCD_STATUS
, VCC_IDD_STATUS
),
137 regmap_reg_range(VBUS_UCD_STATUS
, VBUS_IDD_STATUS
),
138 regmap_reg_range(INT0_STATUS
, INT7_STATUS
),
139 regmap_reg_range(SYSTEM_STATUS
, SYSTEM_CTRL_SET
),
140 regmap_reg_range(IBATP_VAL
, EXTIADP_AVE_VAL
), /* Measurement regs */
143 static const struct regmap_access_table bd9995x_volatile_regs
= {
144 .yes_ranges
= bd9995x_volatile_reg_ranges
,
145 .n_yes_ranges
= ARRAY_SIZE(bd9995x_volatile_reg_ranges
),
148 static const struct regmap_range_cfg regmap_range_cfg
[] = {
150 .selector_reg
= MAP_SET
,
151 .selector_mask
= 0xFFFF,
155 .range_min
= 0 * 0x100,
156 .range_max
= 3 * 0x100,
160 static const struct regmap_config bd9995x_regmap_config
= {
165 .max_register
= 3 * 0x100,
166 .cache_type
= REGCACHE_RBTREE
,
168 .ranges
= regmap_range_cfg
,
169 .num_ranges
= ARRAY_SIZE(regmap_range_cfg
),
170 .val_format_endian
= REGMAP_ENDIAN_LITTLE
,
171 .wr_table
= &bd9995x_writeable_regs
,
172 .volatile_table
= &bd9995x_volatile_regs
,
175 enum bd9995x_chrg_fault
{
178 CHRG_FAULT_THERMAL_SHUTDOWN
,
179 CHRG_FAULT_TIMER_EXPIRED
,
182 static int bd9995x_get_prop_batt_health(struct bd9995x_device
*bd
)
186 ret
= regmap_field_read(bd
->rmap_fields
[F_BATTEMP
], &tmp
);
188 return POWER_SUPPLY_HEALTH_UNKNOWN
;
190 /* TODO: Check these against datasheet page 34 */
194 return POWER_SUPPLY_HEALTH_GOOD
;
198 return POWER_SUPPLY_HEALTH_OVERHEAT
;
201 return POWER_SUPPLY_HEALTH_COLD
;
205 return POWER_SUPPLY_HEALTH_UNKNOWN
;
209 static int bd9995x_get_prop_charge_type(struct bd9995x_device
*bd
)
213 ret
= regmap_field_read(bd
->rmap_fields
[F_CHGSTM_STATE
], &tmp
);
215 return POWER_SUPPLY_CHARGE_TYPE_UNKNOWN
;
218 case CHGSTM_TRICKLE_CHARGE
:
219 case CHGSTM_PRE_CHARGE
:
220 return POWER_SUPPLY_CHARGE_TYPE_TRICKLE
;
221 case CHGSTM_FAST_CHARGE
:
222 return POWER_SUPPLY_CHARGE_TYPE_FAST
;
226 return POWER_SUPPLY_CHARGE_TYPE_NONE
;
227 default: /* Rest of the states are error related, no charging */
228 return POWER_SUPPLY_CHARGE_TYPE_NONE
;
232 static bool bd9995x_get_prop_batt_present(struct bd9995x_device
*bd
)
236 ret
= regmap_field_read(bd
->rmap_fields
[F_BATTEMP
], &tmp
);
240 return tmp
!= BATT_OPEN
;
243 static int bd9995x_get_prop_batt_voltage(struct bd9995x_device
*bd
)
247 ret
= regmap_field_read(bd
->rmap_fields
[F_VBAT_VAL
], &tmp
);
251 tmp
= min(tmp
, 19200);
256 static int bd9995x_get_prop_batt_current(struct bd9995x_device
*bd
)
260 ret
= regmap_field_read(bd
->rmap_fields
[F_IBATP_VAL
], &tmp
);
267 #define DEFAULT_BATTERY_TEMPERATURE 250
269 static int bd9995x_get_prop_batt_temp(struct bd9995x_device
*bd
)
273 ret
= regmap_field_read(bd
->rmap_fields
[F_THERM_VAL
], &tmp
);
275 return DEFAULT_BATTERY_TEMPERATURE
;
277 return (200 - tmp
) * 10;
280 static int bd9995x_power_supply_get_property(struct power_supply
*psy
,
281 enum power_supply_property psp
,
282 union power_supply_propval
*val
)
285 struct bd9995x_device
*bd
= power_supply_get_drvdata(psy
);
286 struct bd9995x_state state
;
288 mutex_lock(&bd
->lock
);
290 mutex_unlock(&bd
->lock
);
293 case POWER_SUPPLY_PROP_STATUS
:
294 switch (state
.chgstm_status
) {
295 case CHGSTM_TRICKLE_CHARGE
:
296 case CHGSTM_PRE_CHARGE
:
297 case CHGSTM_FAST_CHARGE
:
299 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
303 val
->intval
= POWER_SUPPLY_STATUS_FULL
;
307 case CHGSTM_TEMPERATURE_ERROR_1
:
308 case CHGSTM_TEMPERATURE_ERROR_2
:
309 case CHGSTM_TEMPERATURE_ERROR_3
:
310 case CHGSTM_TEMPERATURE_ERROR_4
:
311 case CHGSTM_TEMPERATURE_ERROR_5
:
312 case CHGSTM_TEMPERATURE_ERROR_6
:
313 case CHGSTM_TEMPERATURE_ERROR_7
:
314 case CHGSTM_THERMAL_SHUT_DOWN_1
:
315 case CHGSTM_THERMAL_SHUT_DOWN_2
:
316 case CHGSTM_THERMAL_SHUT_DOWN_3
:
317 case CHGSTM_THERMAL_SHUT_DOWN_4
:
318 case CHGSTM_THERMAL_SHUT_DOWN_5
:
319 case CHGSTM_THERMAL_SHUT_DOWN_6
:
320 case CHGSTM_THERMAL_SHUT_DOWN_7
:
321 case CHGSTM_BATTERY_ERROR
:
322 val
->intval
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
326 val
->intval
= POWER_SUPPLY_STATUS_UNKNOWN
;
331 case POWER_SUPPLY_PROP_MANUFACTURER
:
332 val
->strval
= BD9995X_MANUFACTURER
;
335 case POWER_SUPPLY_PROP_ONLINE
:
336 val
->intval
= state
.online
;
339 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT
:
340 ret
= regmap_field_read(bd
->rmap_fields
[F_IBATP_VAL
], &tmp
);
343 val
->intval
= tmp
* 1000;
346 case POWER_SUPPLY_PROP_CHARGE_AVG
:
347 ret
= regmap_field_read(bd
->rmap_fields
[F_IBATP_AVE_VAL
], &tmp
);
350 val
->intval
= tmp
* 1000;
353 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX
:
355 * Currently the DT uses this property to give the
356 * target current for fast-charging constant current phase.
357 * I think it is correct in a sense.
359 * Yet, this prop we read and return here is the programmed
360 * safety limit for combined input currents. This feels
361 * also correct in a sense.
363 * However, this results a mismatch to DT value and value
366 ret
= regmap_field_read(bd
->rmap_fields
[F_SEL_ILIM_VAL
], &tmp
);
369 val
->intval
= tmp
* 1000;
372 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE
:
378 ret
= regmap_field_read(bd
->rmap_fields
[F_VFASTCHG_REG_SET1
],
384 * The actual range : 2560 to 19200 mV. No matter what the
387 val
->intval
= clamp_val(tmp
<< 4, 2560, 19200);
391 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT
:
392 ret
= regmap_field_read(bd
->rmap_fields
[F_ITERM_SET
], &tmp
);
395 /* Start step is 64 mA */
396 val
->intval
= tmp
<< 6;
397 /* Maximum is 1024 mA - no matter what register says */
398 val
->intval
= min(val
->intval
, 1024);
402 /* Battery properties which we access through charger */
403 case POWER_SUPPLY_PROP_PRESENT
:
404 val
->intval
= bd9995x_get_prop_batt_present(bd
);
407 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
408 val
->intval
= bd9995x_get_prop_batt_voltage(bd
);
411 case POWER_SUPPLY_PROP_CURRENT_NOW
:
412 val
->intval
= bd9995x_get_prop_batt_current(bd
);
415 case POWER_SUPPLY_PROP_CHARGE_TYPE
:
416 val
->intval
= bd9995x_get_prop_charge_type(bd
);
419 case POWER_SUPPLY_PROP_HEALTH
:
420 val
->intval
= bd9995x_get_prop_batt_health(bd
);
423 case POWER_SUPPLY_PROP_TEMP
:
424 val
->intval
= bd9995x_get_prop_batt_temp(bd
);
427 case POWER_SUPPLY_PROP_TECHNOLOGY
:
428 val
->intval
= POWER_SUPPLY_TECHNOLOGY_LION
;
431 case POWER_SUPPLY_PROP_MODEL_NAME
:
432 val
->strval
= "bd99954";
443 static int bd9995x_get_chip_state(struct bd9995x_device
*bd
,
444 struct bd9995x_state
*state
)
448 struct regmap_field
*id
;
452 bd
->rmap_fields
[F_CHGSTM_STATE
], &state
->chgstm_status
,
454 bd
->rmap_fields
[F_VBAT_VSYS_STATUS
],
455 &state
->vbat_vsys_status
,
457 bd
->rmap_fields
[F_VBUS_VCC_STATUS
],
458 &state
->vbus_vcc_status
,
463 for (i
= 0; i
< ARRAY_SIZE(state_fields
); i
++) {
464 ret
= regmap_field_read(state_fields
[i
].id
, &tmp
);
468 *state_fields
[i
].data
= tmp
;
471 if (state
->vbus_vcc_status
& STATUS_VCC_DET
||
472 state
->vbus_vcc_status
& STATUS_VBUS_DET
)
480 static irqreturn_t
bd9995x_irq_handler_thread(int irq
, void *private)
482 struct bd9995x_device
*bd
= private;
483 int ret
, status
, mask
, i
;
485 struct bd9995x_state state
;
488 * The bd9995x does not seem to generate big amount of interrupts.
489 * The logic regarding which interrupts can cause relevant
490 * status changes seem to be pretty complex.
492 * So lets implement really simple and hopefully bullet-proof handler:
493 * It does not really matter which IRQ we handle, we just go and
494 * re-read all interesting statuses + give the framework a nudge.
496 * Other option would be building a _complex_ and error prone logic
497 * trying to decide what could have been changed (resulting this IRQ
498 * we are now handling). During the normal operation the BD99954 does
499 * not seem to be generating much of interrupts so benefit from such
500 * logic would probably be minimal.
503 ret
= regmap_read(bd
->rmap
, INT0_STATUS
, &status
);
505 dev_err(bd
->dev
, "Failed to read IRQ status\n");
509 ret
= regmap_field_read(bd
->rmap_fields
[F_INT0_SET
], &mask
);
511 dev_err(bd
->dev
, "Failed to read IRQ mask\n");
515 /* Handle only IRQs that are not masked */
519 /* Lowest bit does not represent any sub-registers */
523 * Mask and ack IRQs we will handle (+ the idiot bit)
525 ret
= regmap_field_write(bd
->rmap_fields
[F_INT0_SET
], 0);
527 dev_err(bd
->dev
, "Failed to mask F_INT0\n");
531 ret
= regmap_write(bd
->rmap
, INT0_STATUS
, status
);
533 dev_err(bd
->dev
, "Failed to ack F_INT0\n");
537 for_each_set_bit(i
, &tmp
, 7) {
538 int sub_status
, sub_mask
;
539 int sub_status_reg
[] = {
540 INT1_STATUS
, INT2_STATUS
, INT3_STATUS
, INT4_STATUS
,
541 INT5_STATUS
, INT6_STATUS
, INT7_STATUS
,
543 struct regmap_field
*sub_mask_f
[] = {
544 bd
->rmap_fields
[F_INT1_SET
],
545 bd
->rmap_fields
[F_INT2_SET
],
546 bd
->rmap_fields
[F_INT3_SET
],
547 bd
->rmap_fields
[F_INT4_SET
],
548 bd
->rmap_fields
[F_INT5_SET
],
549 bd
->rmap_fields
[F_INT6_SET
],
550 bd
->rmap_fields
[F_INT7_SET
],
554 ret
= regmap_read(bd
->rmap
, sub_status_reg
[i
], &sub_status
);
556 dev_err(bd
->dev
, "Failed to read IRQ sub-status\n");
560 ret
= regmap_field_read(sub_mask_f
[i
], &sub_mask
);
562 dev_err(bd
->dev
, "Failed to read IRQ sub-mask\n");
566 /* Ack active sub-statuses */
567 sub_status
&= sub_mask
;
569 ret
= regmap_write(bd
->rmap
, sub_status_reg
[i
], sub_status
);
571 dev_err(bd
->dev
, "Failed to ack sub-IRQ\n");
576 ret
= regmap_field_write(bd
->rmap_fields
[F_INT0_SET
], mask
);
578 /* May as well retry once */
581 /* Read whole chip state */
582 ret
= bd9995x_get_chip_state(bd
, &state
);
584 dev_err(bd
->dev
, "Failed to read chip state\n");
586 mutex_lock(&bd
->lock
);
588 mutex_unlock(&bd
->lock
);
590 power_supply_changed(bd
->charger
);
596 ret
= regmap_field_write(bd
->rmap_fields
[F_INT0_SET
], mask
);
599 "Failed to un-mask F_INT0 - IRQ permanently disabled\n");
604 static int __bd9995x_chip_reset(struct bd9995x_device
*bd
)
607 int rst_check_counter
= 10;
608 u16 tmp
= ALLRST
| OTPLD
;
610 ret
= regmap_raw_write(bd
->rmap
, SYSTEM_CTRL_SET
, &tmp
, 2);
615 ret
= regmap_field_read(bd
->rmap_fields
[F_OTPLD_STATE
], &state
);
620 } while (state
== 0 && --rst_check_counter
);
622 if (!rst_check_counter
) {
623 dev_err(bd
->dev
, "chip reset not completed\n");
628 ret
= regmap_raw_write(bd
->rmap
, SYSTEM_CTRL_SET
, &tmp
, 2);
633 static int bd9995x_hw_init(struct bd9995x_device
*bd
)
637 struct bd9995x_state state
;
638 struct bd9995x_init_data
*id
= &bd
->init_data
;
641 enum bd9995x_fields id
;
644 /* Enable the charging trigger after SDP charger attached */
645 {F_SDP_CHG_TRIG_EN
, 1},
646 /* Enable charging trigger after SDP charger attached */
648 /* Disable charging trigger by BC1.2 detection */
649 {F_VBUS_BC_DISEN
, 1},
650 /* Disable charging trigger by BC1.2 detection */
652 /* Disable automatic limitation of the input current */
653 {F_ILIM_AUTO_DISEN
, 1},
654 /* Select current limitation when SDP charger attached*/
656 /* Select current limitation when DCP charger attached */
658 {F_VSYSREG_SET
, id
->vsysreg_set
},
659 /* Activate USB charging and DC/DC converter */
661 /* DCDC clock: 1200 kHz*/
663 /* Enable charging */
665 /* Disable Input current Limit setting voltage measurement */
667 /* Disable input current limiting */
668 {F_VSYS_PRIORITY
, 1},
669 {F_IBUS_LIM_SET
, id
->ibus_lim_set
},
670 {F_ICC_LIM_SET
, id
->icc_lim_set
},
671 /* Charge Termination Current Setting to 0*/
672 {F_ITERM_SET
, id
->iterm_set
},
673 /* Trickle-charge Current Setting */
674 {F_ITRICH_SET
, id
->itrich_set
},
675 /* Pre-charge Current setting */
676 {F_IPRECH_SET
, id
->iprech_set
},
677 /* Fast Charge Current for constant current phase */
678 {F_ICHG_SET
, id
->ichg_set
},
679 /* Fast Charge Voltage Regulation Setting */
680 {F_VFASTCHG_REG_SET1
, id
->vfastchg_reg_set1
},
681 /* Set Pre-charge Voltage Threshold for trickle charging. */
682 {F_VPRECHG_TH_SET
, id
->vprechg_th_set
},
683 {F_VRECHG_SET
, id
->vrechg_set
},
684 {F_VBATOVP_SET
, id
->vbatovp_set
},
685 /* Reverse buck boost voltage Setting */
687 /* Disable fast-charging watchdog */
689 /* Disable pre-charging watchdog */
692 {F_POWER_SAVE_MODE
, 0},
693 {F_INT1_SET
, INT1_ALL
},
694 {F_INT2_SET
, INT2_ALL
},
695 {F_INT3_SET
, INT3_ALL
},
696 {F_INT4_SET
, INT4_ALL
},
697 {F_INT5_SET
, INT5_ALL
},
698 {F_INT6_SET
, INT6_ALL
},
699 {F_INT7_SET
, INT7_ALL
},
703 * Currently we initialize charger to a known state at startup.
704 * If we want to allow for example the boot code to initialize
705 * charger we should get rid of this.
707 ret
= __bd9995x_chip_reset(bd
);
711 /* Initialize currents/voltages and other parameters */
712 for (i
= 0; i
< ARRAY_SIZE(init_data
); i
++) {
713 ret
= regmap_field_write(bd
->rmap_fields
[init_data
[i
].id
],
716 dev_err(bd
->dev
, "failed to initialize charger (%d)\n",
722 ret
= bd9995x_get_chip_state(bd
, &state
);
726 mutex_lock(&bd
->lock
);
728 mutex_unlock(&bd
->lock
);
733 static enum power_supply_property bd9995x_power_supply_props
[] = {
734 POWER_SUPPLY_PROP_MANUFACTURER
,
735 POWER_SUPPLY_PROP_STATUS
,
736 POWER_SUPPLY_PROP_ONLINE
,
737 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT
,
738 POWER_SUPPLY_PROP_CHARGE_AVG
,
739 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX
,
740 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE
,
741 POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT
,
742 /* Battery props we access through charger */
743 POWER_SUPPLY_PROP_PRESENT
,
744 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
745 POWER_SUPPLY_PROP_CURRENT_NOW
,
746 POWER_SUPPLY_PROP_CHARGE_TYPE
,
747 POWER_SUPPLY_PROP_HEALTH
,
748 POWER_SUPPLY_PROP_TEMP
,
749 POWER_SUPPLY_PROP_TECHNOLOGY
,
750 POWER_SUPPLY_PROP_MODEL_NAME
,
753 static const struct power_supply_desc bd9995x_power_supply_desc
= {
754 .name
= "bd9995x-charger",
755 .type
= POWER_SUPPLY_TYPE_USB
,
756 .properties
= bd9995x_power_supply_props
,
757 .num_properties
= ARRAY_SIZE(bd9995x_power_supply_props
),
758 .get_property
= bd9995x_power_supply_get_property
,
762 * Limit configurations for vbus-input-current and vcc-vacp-input-current
763 * Minimum limit is 0 uA. Max is 511 * 32000 uA = 16352000 uA. This is
764 * configured by writing a register so that each increment in register
765 * value equals to 32000 uA limit increment.
767 * Eg, value 0x0 is limit 0, value 0x1 is limit 32000, ...
768 * Describe the setting in linear_range table.
770 static const struct linear_range input_current_limit_ranges
[] = {
779 /* Possible trickle, pre-charging and termination current values */
780 static const struct linear_range charging_current_ranges
[] = {
795 * Fast charging voltage regulation, starting re-charging limit
796 * and battery over voltage protection have same possible values
798 static const struct linear_range charge_voltage_regulation_ranges
[] = {
817 /* Possible VSYS voltage regulation values */
818 static const struct linear_range vsys_voltage_regulation_ranges
[] = {
837 /* Possible settings for switching from trickle to pre-charging limits */
838 static const struct linear_range trickle_to_pre_threshold_ranges
[] = {
857 /* Possible current values for fast-charging constant current phase */
858 static const struct linear_range fast_charge_current_ranges
[] = {
867 struct battery_init
{
870 const struct linear_range
*range
;
877 const struct linear_range
*range
;
882 static int bd9995x_fw_probe(struct bd9995x_device
*bd
)
885 struct power_supply_battery_info info
;
890 struct bd9995x_init_data
*init
= &bd
->init_data
;
891 struct battery_init battery_inits
[] = {
893 .name
= "trickle-charging current",
894 .info_data
= &info
.tricklecharge_current_ua
,
895 .range
= &charging_current_ranges
[0],
897 .data
= &init
->itrich_set
,
899 .name
= "pre-charging current",
900 .info_data
= &info
.precharge_current_ua
,
901 .range
= &charging_current_ranges
[0],
903 .data
= &init
->iprech_set
,
905 .name
= "pre-to-trickle charge voltage threshold",
906 .info_data
= &info
.precharge_voltage_max_uv
,
907 .range
= &trickle_to_pre_threshold_ranges
[0],
909 .data
= &init
->vprechg_th_set
,
911 .name
= "charging termination current",
912 .info_data
= &info
.charge_term_current_ua
,
913 .range
= &charging_current_ranges
[0],
915 .data
= &init
->iterm_set
,
917 .name
= "charging re-start voltage",
918 .info_data
= &info
.charge_restart_voltage_uv
,
919 .range
= &charge_voltage_regulation_ranges
[0],
921 .data
= &init
->vrechg_set
,
923 .name
= "battery overvoltage limit",
924 .info_data
= &info
.overvoltage_limit_uv
,
925 .range
= &charge_voltage_regulation_ranges
[0],
927 .data
= &init
->vbatovp_set
,
929 .name
= "fast-charging max current",
930 .info_data
= &info
.constant_charge_current_max_ua
,
931 .range
= &fast_charge_current_ranges
[0],
933 .data
= &init
->ichg_set
,
935 .name
= "fast-charging voltage",
936 .info_data
= &info
.constant_charge_voltage_max_uv
,
937 .range
= &charge_voltage_regulation_ranges
[0],
939 .data
= &init
->vfastchg_reg_set1
,
942 struct dt_init props
[] = {
944 .prop
= "rohm,vsys-regulation-microvolt",
945 .range
= &vsys_voltage_regulation_ranges
[0],
947 .data
= &init
->vsysreg_set
,
949 .prop
= "rohm,vbus-input-current-limit-microamp",
950 .range
= &input_current_limit_ranges
[0],
952 .data
= &init
->ibus_lim_set
,
954 .prop
= "rohm,vcc-input-current-limit-microamp",
955 .range
= &input_current_limit_ranges
[0],
957 .data
= &init
->icc_lim_set
,
962 * The power_supply_get_battery_info() does not support getting values
963 * from ACPI. Let's fix it if ACPI is required here.
965 ret
= power_supply_get_battery_info(bd
->charger
, &info
);
969 for (i
= 0; i
< ARRAY_SIZE(battery_inits
); i
++) {
970 int val
= *battery_inits
[i
].info_data
;
971 const struct linear_range
*range
= battery_inits
[i
].range
;
972 int ranges
= battery_inits
[i
].ranges
;
977 ret
= linear_range_get_selector_low_array(range
, ranges
, val
,
980 dev_err(bd
->dev
, "Unsupported value for %s\n",
981 battery_inits
[i
].name
);
983 power_supply_put_battery_info(bd
->charger
, &info
);
988 "Unsupported value for %s - using smaller\n",
989 battery_inits
[i
].name
);
991 *(battery_inits
[i
].data
) = regval
;
994 power_supply_put_battery_info(bd
->charger
, &info
);
996 for (i
= 0; i
< ARRAY_SIZE(props
); i
++) {
997 ret
= device_property_read_u32(bd
->dev
, props
[i
].prop
,
1000 dev_err(bd
->dev
, "failed to read %s", props
[i
].prop
);
1005 ret
= linear_range_get_selector_low_array(props
[i
].range
,
1010 dev_err(bd
->dev
, "Unsupported value for '%s'\n",
1018 "Unsupported value for '%s' - using smaller\n",
1022 *(props
[i
].data
) = regval
;
1028 static void bd9995x_chip_reset(void *bd
)
1030 __bd9995x_chip_reset(bd
);
1033 static int bd9995x_probe(struct i2c_client
*client
)
1035 struct device
*dev
= &client
->dev
;
1036 struct bd9995x_device
*bd
;
1037 struct power_supply_config psy_cfg
= {};
1041 bd
= devm_kzalloc(dev
, sizeof(*bd
), GFP_KERNEL
);
1045 bd
->client
= client
;
1047 psy_cfg
.drv_data
= bd
;
1048 psy_cfg
.of_node
= dev
->of_node
;
1050 mutex_init(&bd
->lock
);
1052 bd
->rmap
= devm_regmap_init_i2c(client
, &bd9995x_regmap_config
);
1053 if (IS_ERR(bd
->rmap
)) {
1054 dev_err(dev
, "Failed to setup register access via i2c\n");
1055 return PTR_ERR(bd
->rmap
);
1058 for (i
= 0; i
< ARRAY_SIZE(bd9995x_reg_fields
); i
++) {
1059 const struct reg_field
*reg_fields
= bd9995x_reg_fields
;
1061 bd
->rmap_fields
[i
] = devm_regmap_field_alloc(dev
, bd
->rmap
,
1063 if (IS_ERR(bd
->rmap_fields
[i
])) {
1064 dev_err(dev
, "cannot allocate regmap field\n");
1065 return PTR_ERR(bd
->rmap_fields
[i
]);
1069 i2c_set_clientdata(client
, bd
);
1071 ret
= regmap_field_read(bd
->rmap_fields
[F_CHIP_ID
], &bd
->chip_id
);
1073 dev_err(dev
, "Cannot read chip ID.\n");
1077 if (bd
->chip_id
!= BD99954_ID
) {
1078 dev_err(dev
, "Chip with ID=0x%x, not supported!\n",
1083 ret
= regmap_field_read(bd
->rmap_fields
[F_CHIP_REV
], &bd
->chip_rev
);
1085 dev_err(dev
, "Cannot read revision.\n");
1089 dev_info(bd
->dev
, "Found BD99954 chip rev %d\n", bd
->chip_rev
);
1092 * We need to init the psy before we can call
1093 * power_supply_get_battery_info() for it
1095 bd
->charger
= devm_power_supply_register(bd
->dev
,
1096 &bd9995x_power_supply_desc
,
1098 if (IS_ERR(bd
->charger
)) {
1099 dev_err(dev
, "Failed to register power supply\n");
1100 return PTR_ERR(bd
->charger
);
1103 ret
= bd9995x_fw_probe(bd
);
1105 dev_err(dev
, "Cannot read device properties.\n");
1109 ret
= bd9995x_hw_init(bd
);
1111 dev_err(dev
, "Cannot initialize the chip.\n");
1115 ret
= devm_add_action_or_reset(dev
, bd9995x_chip_reset
, bd
);
1119 return devm_request_threaded_irq(dev
, client
->irq
, NULL
,
1120 bd9995x_irq_handler_thread
,
1121 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
,
1122 BD9995X_IRQ_PIN
, bd
);
1125 static const struct of_device_id bd9995x_of_match
[] = {
1126 { .compatible
= "rohm,bd99954", },
1129 MODULE_DEVICE_TABLE(of
, bd9995x_of_match
);
1131 static struct i2c_driver bd9995x_driver
= {
1133 .name
= "bd9995x-charger",
1134 .of_match_table
= bd9995x_of_match
,
1136 .probe_new
= bd9995x_probe
,
1138 module_i2c_driver(bd9995x_driver
);
1140 MODULE_AUTHOR("Laine Markus <markus.laine@fi.rohmeurope.com>");
1141 MODULE_DESCRIPTION("ROHM BD99954 charger driver");
1142 MODULE_LICENSE("GPL");