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"
73 /* Initial field values, converted to initial register values */
74 struct bd9995x_init_data
{
75 u16 vsysreg_set
; /* VSYS Regulation Setting */
76 u16 ibus_lim_set
; /* VBUS input current limitation */
77 u16 icc_lim_set
; /* VCC/VACP Input Current Limit Setting */
78 u16 itrich_set
; /* Trickle-charge Current Setting */
79 u16 iprech_set
; /* Pre-Charge Current Setting */
80 u16 ichg_set
; /* Fast-Charge constant current */
81 u16 vfastchg_reg_set1
; /* Fast Charging Regulation Voltage */
82 u16 vprechg_th_set
; /* Pre-charge Voltage Threshold Setting */
83 u16 vrechg_set
; /* Re-charge Battery Voltage Setting */
84 u16 vbatovp_set
; /* Battery Over Voltage Threshold Setting */
85 u16 iterm_set
; /* Charging termination current */
88 struct bd9995x_state
{
95 struct bd9995x_device
{
96 struct i2c_client
*client
;
98 struct power_supply
*charger
;
101 struct regmap_field
*rmap_fields
[F_MAX_FIELDS
];
105 struct bd9995x_init_data init_data
;
106 struct bd9995x_state state
;
108 struct mutex lock
; /* Protect state data */
111 static const struct regmap_range bd9995x_readonly_reg_ranges
[] = {
112 regmap_reg_range(CHGSTM_STATUS
, SEL_ILIM_VAL
),
113 regmap_reg_range(IOUT_DACIN_VAL
, IOUT_DACIN_VAL
),
114 regmap_reg_range(VCC_UCD_STATUS
, VCC_IDD_STATUS
),
115 regmap_reg_range(VBUS_UCD_STATUS
, VBUS_IDD_STATUS
),
116 regmap_reg_range(CHIP_ID
, CHIP_REV
),
117 regmap_reg_range(SYSTEM_STATUS
, SYSTEM_STATUS
),
118 regmap_reg_range(IBATP_VAL
, VBAT_AVE_VAL
),
119 regmap_reg_range(VTH_VAL
, EXTIADP_AVE_VAL
),
122 static const struct regmap_access_table bd9995x_writeable_regs
= {
123 .no_ranges
= bd9995x_readonly_reg_ranges
,
124 .n_no_ranges
= ARRAY_SIZE(bd9995x_readonly_reg_ranges
),
127 static const struct regmap_range bd9995x_volatile_reg_ranges
[] = {
128 regmap_reg_range(CHGSTM_STATUS
, WDT_STATUS
),
129 regmap_reg_range(VCC_UCD_STATUS
, VCC_IDD_STATUS
),
130 regmap_reg_range(VBUS_UCD_STATUS
, VBUS_IDD_STATUS
),
131 regmap_reg_range(INT0_STATUS
, INT7_STATUS
),
132 regmap_reg_range(SYSTEM_STATUS
, SYSTEM_CTRL_SET
),
133 regmap_reg_range(IBATP_VAL
, EXTIADP_AVE_VAL
), /* Measurement regs */
136 static const struct regmap_access_table bd9995x_volatile_regs
= {
137 .yes_ranges
= bd9995x_volatile_reg_ranges
,
138 .n_yes_ranges
= ARRAY_SIZE(bd9995x_volatile_reg_ranges
),
141 static const struct regmap_range_cfg regmap_range_cfg
[] = {
143 .selector_reg
= MAP_SET
,
144 .selector_mask
= 0xFFFF,
148 .range_min
= 0 * 0x100,
149 .range_max
= 3 * 0x100,
153 static const struct regmap_config bd9995x_regmap_config
= {
158 .max_register
= 3 * 0x100,
159 .cache_type
= REGCACHE_RBTREE
,
161 .ranges
= regmap_range_cfg
,
162 .num_ranges
= ARRAY_SIZE(regmap_range_cfg
),
163 .val_format_endian
= REGMAP_ENDIAN_LITTLE
,
164 .wr_table
= &bd9995x_writeable_regs
,
165 .volatile_table
= &bd9995x_volatile_regs
,
168 enum bd9995x_chrg_fault
{
171 CHRG_FAULT_THERMAL_SHUTDOWN
,
172 CHRG_FAULT_TIMER_EXPIRED
,
175 static int bd9995x_get_prop_batt_health(struct bd9995x_device
*bd
)
179 ret
= regmap_field_read(bd
->rmap_fields
[F_BATTEMP
], &tmp
);
181 return POWER_SUPPLY_HEALTH_UNKNOWN
;
183 /* TODO: Check these against datasheet page 34 */
187 return POWER_SUPPLY_HEALTH_GOOD
;
191 return POWER_SUPPLY_HEALTH_OVERHEAT
;
194 return POWER_SUPPLY_HEALTH_COLD
;
198 return POWER_SUPPLY_HEALTH_UNKNOWN
;
202 static int bd9995x_get_prop_charge_type(struct bd9995x_device
*bd
)
206 ret
= regmap_field_read(bd
->rmap_fields
[F_CHGSTM_STATE
], &tmp
);
208 return POWER_SUPPLY_CHARGE_TYPE_UNKNOWN
;
211 case CHGSTM_TRICKLE_CHARGE
:
212 case CHGSTM_PRE_CHARGE
:
213 return POWER_SUPPLY_CHARGE_TYPE_TRICKLE
;
214 case CHGSTM_FAST_CHARGE
:
215 return POWER_SUPPLY_CHARGE_TYPE_FAST
;
219 return POWER_SUPPLY_CHARGE_TYPE_NONE
;
220 default: /* Rest of the states are error related, no charging */
221 return POWER_SUPPLY_CHARGE_TYPE_NONE
;
225 static bool bd9995x_get_prop_batt_present(struct bd9995x_device
*bd
)
229 ret
= regmap_field_read(bd
->rmap_fields
[F_BATTEMP
], &tmp
);
233 return tmp
!= BATT_OPEN
;
236 static int bd9995x_get_prop_batt_voltage(struct bd9995x_device
*bd
)
240 ret
= regmap_field_read(bd
->rmap_fields
[F_VBAT_VAL
], &tmp
);
244 tmp
= min(tmp
, 19200);
249 static int bd9995x_get_prop_batt_current(struct bd9995x_device
*bd
)
253 ret
= regmap_field_read(bd
->rmap_fields
[F_IBATP_VAL
], &tmp
);
260 #define DEFAULT_BATTERY_TEMPERATURE 250
262 static int bd9995x_get_prop_batt_temp(struct bd9995x_device
*bd
)
266 ret
= regmap_field_read(bd
->rmap_fields
[F_THERM_VAL
], &tmp
);
268 return DEFAULT_BATTERY_TEMPERATURE
;
270 return (200 - tmp
) * 10;
273 static int bd9995x_power_supply_get_property(struct power_supply
*psy
,
274 enum power_supply_property psp
,
275 union power_supply_propval
*val
)
278 struct bd9995x_device
*bd
= power_supply_get_drvdata(psy
);
279 struct bd9995x_state state
;
281 mutex_lock(&bd
->lock
);
283 mutex_unlock(&bd
->lock
);
286 case POWER_SUPPLY_PROP_STATUS
:
287 switch (state
.chgstm_status
) {
288 case CHGSTM_TRICKLE_CHARGE
:
289 case CHGSTM_PRE_CHARGE
:
290 case CHGSTM_FAST_CHARGE
:
292 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
296 val
->intval
= POWER_SUPPLY_STATUS_FULL
;
300 case CHGSTM_TEMPERATURE_ERROR_1
:
301 case CHGSTM_TEMPERATURE_ERROR_2
:
302 case CHGSTM_TEMPERATURE_ERROR_3
:
303 case CHGSTM_TEMPERATURE_ERROR_4
:
304 case CHGSTM_TEMPERATURE_ERROR_5
:
305 case CHGSTM_TEMPERATURE_ERROR_6
:
306 case CHGSTM_TEMPERATURE_ERROR_7
:
307 case CHGSTM_THERMAL_SHUT_DOWN_1
:
308 case CHGSTM_THERMAL_SHUT_DOWN_2
:
309 case CHGSTM_THERMAL_SHUT_DOWN_3
:
310 case CHGSTM_THERMAL_SHUT_DOWN_4
:
311 case CHGSTM_THERMAL_SHUT_DOWN_5
:
312 case CHGSTM_THERMAL_SHUT_DOWN_6
:
313 case CHGSTM_THERMAL_SHUT_DOWN_7
:
314 case CHGSTM_BATTERY_ERROR
:
315 val
->intval
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
319 val
->intval
= POWER_SUPPLY_STATUS_UNKNOWN
;
324 case POWER_SUPPLY_PROP_MANUFACTURER
:
325 val
->strval
= BD9995X_MANUFACTURER
;
328 case POWER_SUPPLY_PROP_ONLINE
:
329 val
->intval
= state
.online
;
332 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT
:
333 ret
= regmap_field_read(bd
->rmap_fields
[F_IBATP_VAL
], &tmp
);
336 val
->intval
= tmp
* 1000;
339 case POWER_SUPPLY_PROP_CHARGE_AVG
:
340 ret
= regmap_field_read(bd
->rmap_fields
[F_IBATP_AVE_VAL
], &tmp
);
343 val
->intval
= tmp
* 1000;
346 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX
:
348 * Currently the DT uses this property to give the
349 * target current for fast-charging constant current phase.
350 * I think it is correct in a sense.
352 * Yet, this prop we read and return here is the programmed
353 * safety limit for combined input currents. This feels
354 * also correct in a sense.
356 * However, this results a mismatch to DT value and value
359 ret
= regmap_field_read(bd
->rmap_fields
[F_SEL_ILIM_VAL
], &tmp
);
362 val
->intval
= tmp
* 1000;
365 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE
:
371 ret
= regmap_field_read(bd
->rmap_fields
[F_VFASTCHG_REG_SET1
],
377 * The actual range : 2560 to 19200 mV. No matter what the
380 val
->intval
= clamp_val(tmp
<< 4, 2560, 19200);
384 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT
:
385 ret
= regmap_field_read(bd
->rmap_fields
[F_ITERM_SET
], &tmp
);
388 /* Start step is 64 mA */
389 val
->intval
= tmp
<< 6;
390 /* Maximum is 1024 mA - no matter what register says */
391 val
->intval
= min(val
->intval
, 1024);
395 /* Battery properties which we access through charger */
396 case POWER_SUPPLY_PROP_PRESENT
:
397 val
->intval
= bd9995x_get_prop_batt_present(bd
);
400 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
401 val
->intval
= bd9995x_get_prop_batt_voltage(bd
);
404 case POWER_SUPPLY_PROP_CURRENT_NOW
:
405 val
->intval
= bd9995x_get_prop_batt_current(bd
);
408 case POWER_SUPPLY_PROP_CHARGE_TYPE
:
409 val
->intval
= bd9995x_get_prop_charge_type(bd
);
412 case POWER_SUPPLY_PROP_HEALTH
:
413 val
->intval
= bd9995x_get_prop_batt_health(bd
);
416 case POWER_SUPPLY_PROP_TEMP
:
417 val
->intval
= bd9995x_get_prop_batt_temp(bd
);
420 case POWER_SUPPLY_PROP_TECHNOLOGY
:
421 val
->intval
= POWER_SUPPLY_TECHNOLOGY_LION
;
424 case POWER_SUPPLY_PROP_MODEL_NAME
:
425 val
->strval
= "bd99954";
436 static int bd9995x_get_chip_state(struct bd9995x_device
*bd
,
437 struct bd9995x_state
*state
)
441 struct regmap_field
*id
;
445 bd
->rmap_fields
[F_CHGSTM_STATE
], &state
->chgstm_status
,
447 bd
->rmap_fields
[F_VBAT_VSYS_STATUS
],
448 &state
->vbat_vsys_status
,
450 bd
->rmap_fields
[F_VBUS_VCC_STATUS
],
451 &state
->vbus_vcc_status
,
456 for (i
= 0; i
< ARRAY_SIZE(state_fields
); i
++) {
457 ret
= regmap_field_read(state_fields
[i
].id
, &tmp
);
461 *state_fields
[i
].data
= tmp
;
464 if (state
->vbus_vcc_status
& STATUS_VCC_DET
||
465 state
->vbus_vcc_status
& STATUS_VBUS_DET
)
473 static irqreturn_t
bd9995x_irq_handler_thread(int irq
, void *private)
475 struct bd9995x_device
*bd
= private;
476 int ret
, status
, mask
, i
;
478 struct bd9995x_state state
;
481 * The bd9995x does not seem to generate big amount of interrupts.
482 * The logic regarding which interrupts can cause relevant
483 * status changes seem to be pretty complex.
485 * So lets implement really simple and hopefully bullet-proof handler:
486 * It does not really matter which IRQ we handle, we just go and
487 * re-read all interesting statuses + give the framework a nudge.
489 * Other option would be building a _complex_ and error prone logic
490 * trying to decide what could have been changed (resulting this IRQ
491 * we are now handling). During the normal operation the BD99954 does
492 * not seem to be generating much of interrupts so benefit from such
493 * logic would probably be minimal.
496 ret
= regmap_read(bd
->rmap
, INT0_STATUS
, &status
);
498 dev_err(bd
->dev
, "Failed to read IRQ status\n");
502 ret
= regmap_field_read(bd
->rmap_fields
[F_INT0_SET
], &mask
);
504 dev_err(bd
->dev
, "Failed to read IRQ mask\n");
508 /* Handle only IRQs that are not masked */
512 /* Lowest bit does not represent any sub-registers */
516 * Mask and ack IRQs we will handle (+ the idiot bit)
518 ret
= regmap_field_write(bd
->rmap_fields
[F_INT0_SET
], 0);
520 dev_err(bd
->dev
, "Failed to mask F_INT0\n");
524 ret
= regmap_write(bd
->rmap
, INT0_STATUS
, status
);
526 dev_err(bd
->dev
, "Failed to ack F_INT0\n");
530 for_each_set_bit(i
, &tmp
, 7) {
531 int sub_status
, sub_mask
;
532 static const int sub_status_reg
[] = {
533 INT1_STATUS
, INT2_STATUS
, INT3_STATUS
, INT4_STATUS
,
534 INT5_STATUS
, INT6_STATUS
, INT7_STATUS
,
536 struct regmap_field
*sub_mask_f
[] = {
537 bd
->rmap_fields
[F_INT1_SET
],
538 bd
->rmap_fields
[F_INT2_SET
],
539 bd
->rmap_fields
[F_INT3_SET
],
540 bd
->rmap_fields
[F_INT4_SET
],
541 bd
->rmap_fields
[F_INT5_SET
],
542 bd
->rmap_fields
[F_INT6_SET
],
543 bd
->rmap_fields
[F_INT7_SET
],
547 ret
= regmap_read(bd
->rmap
, sub_status_reg
[i
], &sub_status
);
549 dev_err(bd
->dev
, "Failed to read IRQ sub-status\n");
553 ret
= regmap_field_read(sub_mask_f
[i
], &sub_mask
);
555 dev_err(bd
->dev
, "Failed to read IRQ sub-mask\n");
559 /* Ack active sub-statuses */
560 sub_status
&= sub_mask
;
562 ret
= regmap_write(bd
->rmap
, sub_status_reg
[i
], sub_status
);
564 dev_err(bd
->dev
, "Failed to ack sub-IRQ\n");
569 ret
= regmap_field_write(bd
->rmap_fields
[F_INT0_SET
], mask
);
571 /* May as well retry once */
574 /* Read whole chip state */
575 ret
= bd9995x_get_chip_state(bd
, &state
);
577 dev_err(bd
->dev
, "Failed to read chip state\n");
579 mutex_lock(&bd
->lock
);
581 mutex_unlock(&bd
->lock
);
583 power_supply_changed(bd
->charger
);
589 ret
= regmap_field_write(bd
->rmap_fields
[F_INT0_SET
], mask
);
592 "Failed to un-mask F_INT0 - IRQ permanently disabled\n");
597 static int __bd9995x_chip_reset(struct bd9995x_device
*bd
)
600 int rst_check_counter
= 10;
601 u16 tmp
= ALLRST
| OTPLD
;
603 ret
= regmap_raw_write(bd
->rmap
, SYSTEM_CTRL_SET
, &tmp
, 2);
608 ret
= regmap_field_read(bd
->rmap_fields
[F_OTPLD_STATE
], &state
);
613 } while (state
== 0 && --rst_check_counter
);
615 if (!rst_check_counter
) {
616 dev_err(bd
->dev
, "chip reset not completed\n");
621 ret
= regmap_raw_write(bd
->rmap
, SYSTEM_CTRL_SET
, &tmp
, 2);
626 static int bd9995x_hw_init(struct bd9995x_device
*bd
)
630 struct bd9995x_state state
;
631 struct bd9995x_init_data
*id
= &bd
->init_data
;
634 enum bd9995x_fields id
;
637 /* Enable the charging trigger after SDP charger attached */
638 {F_SDP_CHG_TRIG_EN
, 1},
639 /* Enable charging trigger after SDP charger attached */
641 /* Disable charging trigger by BC1.2 detection */
642 {F_VBUS_BC_DISEN
, 1},
643 /* Disable charging trigger by BC1.2 detection */
645 /* Disable automatic limitation of the input current */
646 {F_ILIM_AUTO_DISEN
, 1},
647 /* Select current limitation when SDP charger attached*/
649 /* Select current limitation when DCP charger attached */
651 {F_VSYSREG_SET
, id
->vsysreg_set
},
652 /* Activate USB charging and DC/DC converter */
654 /* DCDC clock: 1200 kHz*/
656 /* Enable charging */
658 /* Disable Input current Limit setting voltage measurement */
660 /* Disable input current limiting */
661 {F_VSYS_PRIORITY
, 1},
662 {F_IBUS_LIM_SET
, id
->ibus_lim_set
},
663 {F_ICC_LIM_SET
, id
->icc_lim_set
},
664 /* Charge Termination Current Setting to 0*/
665 {F_ITERM_SET
, id
->iterm_set
},
666 /* Trickle-charge Current Setting */
667 {F_ITRICH_SET
, id
->itrich_set
},
668 /* Pre-charge Current setting */
669 {F_IPRECH_SET
, id
->iprech_set
},
670 /* Fast Charge Current for constant current phase */
671 {F_ICHG_SET
, id
->ichg_set
},
672 /* Fast Charge Voltage Regulation Setting */
673 {F_VFASTCHG_REG_SET1
, id
->vfastchg_reg_set1
},
674 /* Set Pre-charge Voltage Threshold for trickle charging. */
675 {F_VPRECHG_TH_SET
, id
->vprechg_th_set
},
676 {F_VRECHG_SET
, id
->vrechg_set
},
677 {F_VBATOVP_SET
, id
->vbatovp_set
},
678 /* Reverse buck boost voltage Setting */
680 /* Disable fast-charging watchdog */
682 /* Disable pre-charging watchdog */
685 {F_POWER_SAVE_MODE
, 0},
686 {F_INT1_SET
, INT1_ALL
},
687 {F_INT2_SET
, INT2_ALL
},
688 {F_INT3_SET
, INT3_ALL
},
689 {F_INT4_SET
, INT4_ALL
},
690 {F_INT5_SET
, INT5_ALL
},
691 {F_INT6_SET
, INT6_ALL
},
692 {F_INT7_SET
, INT7_ALL
},
696 * Currently we initialize charger to a known state at startup.
697 * If we want to allow for example the boot code to initialize
698 * charger we should get rid of this.
700 ret
= __bd9995x_chip_reset(bd
);
704 /* Initialize currents/voltages and other parameters */
705 for (i
= 0; i
< ARRAY_SIZE(init_data
); i
++) {
706 ret
= regmap_field_write(bd
->rmap_fields
[init_data
[i
].id
],
709 dev_err(bd
->dev
, "failed to initialize charger (%d)\n",
715 ret
= bd9995x_get_chip_state(bd
, &state
);
719 mutex_lock(&bd
->lock
);
721 mutex_unlock(&bd
->lock
);
726 static enum power_supply_property bd9995x_power_supply_props
[] = {
727 POWER_SUPPLY_PROP_MANUFACTURER
,
728 POWER_SUPPLY_PROP_STATUS
,
729 POWER_SUPPLY_PROP_ONLINE
,
730 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT
,
731 POWER_SUPPLY_PROP_CHARGE_AVG
,
732 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX
,
733 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE
,
734 POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT
,
735 /* Battery props we access through charger */
736 POWER_SUPPLY_PROP_PRESENT
,
737 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
738 POWER_SUPPLY_PROP_CURRENT_NOW
,
739 POWER_SUPPLY_PROP_CHARGE_TYPE
,
740 POWER_SUPPLY_PROP_HEALTH
,
741 POWER_SUPPLY_PROP_TEMP
,
742 POWER_SUPPLY_PROP_TECHNOLOGY
,
743 POWER_SUPPLY_PROP_MODEL_NAME
,
746 static const struct power_supply_desc bd9995x_power_supply_desc
= {
747 .name
= "bd9995x-charger",
748 .type
= POWER_SUPPLY_TYPE_USB
,
749 .properties
= bd9995x_power_supply_props
,
750 .num_properties
= ARRAY_SIZE(bd9995x_power_supply_props
),
751 .get_property
= bd9995x_power_supply_get_property
,
755 * Limit configurations for vbus-input-current and vcc-vacp-input-current
756 * Minimum limit is 0 uA. Max is 511 * 32000 uA = 16352000 uA. This is
757 * configured by writing a register so that each increment in register
758 * value equals to 32000 uA limit increment.
760 * Eg, value 0x0 is limit 0, value 0x1 is limit 32000, ...
761 * Describe the setting in linear_range table.
763 static const struct linear_range input_current_limit_ranges
[] = {
764 LINEAR_RANGE(0, 0x0, 0x1ff, 32000),
767 /* Possible trickle, pre-charging and termination current values */
768 static const struct linear_range charging_current_ranges
[] = {
769 LINEAR_RANGE(0, 0x0, 0x10, 64000),
770 LINEAR_RANGE(1024000, 0x11, 0x1f, 0),
774 * Fast charging voltage regulation, starting re-charging limit
775 * and battery over voltage protection have same possible values
777 static const struct linear_range charge_voltage_regulation_ranges
[] = {
778 LINEAR_RANGE(2560000, 0, 0xA0, 0),
779 LINEAR_RANGE(2560000, 0xA0, 0x4B0, 16000),
780 LINEAR_RANGE(19200000, 0x4B0, 0x7FF, 0),
783 /* Possible VSYS voltage regulation values */
784 static const struct linear_range vsys_voltage_regulation_ranges
[] = {
785 LINEAR_RANGE(2560000, 0, 0x28, 0),
786 LINEAR_RANGE(2560000, 0x28, 0x12C, 64000),
787 LINEAR_RANGE(19200000, 0x12C, 0x1FF, 0),
790 /* Possible settings for switching from trickle to pre-charging limits */
791 static const struct linear_range trickle_to_pre_threshold_ranges
[] = {
792 LINEAR_RANGE(2048000, 0, 0x20, 0),
793 LINEAR_RANGE(2048000, 0x20, 0x12C, 64000),
794 LINEAR_RANGE(19200000, 0x12C, 0x1FF, 0),
797 /* Possible current values for fast-charging constant current phase */
798 static const struct linear_range fast_charge_current_ranges
[] = {
799 LINEAR_RANGE(0, 0, 0xFF, 64000),
802 struct battery_init
{
805 const struct linear_range
*range
;
812 const struct linear_range
*range
;
817 static int bd9995x_fw_probe(struct bd9995x_device
*bd
)
820 struct power_supply_battery_info
*info
;
825 struct bd9995x_init_data
*init
= &bd
->init_data
;
826 struct battery_init battery_inits
[] = {
828 .name
= "trickle-charging current",
829 .range
= &charging_current_ranges
[0],
831 .data
= &init
->itrich_set
,
833 .name
= "pre-charging current",
834 .range
= &charging_current_ranges
[0],
836 .data
= &init
->iprech_set
,
838 .name
= "pre-to-trickle charge voltage threshold",
839 .range
= &trickle_to_pre_threshold_ranges
[0],
841 .data
= &init
->vprechg_th_set
,
843 .name
= "charging termination current",
844 .range
= &charging_current_ranges
[0],
846 .data
= &init
->iterm_set
,
848 .name
= "charging re-start voltage",
849 .range
= &charge_voltage_regulation_ranges
[0],
851 .data
= &init
->vrechg_set
,
853 .name
= "battery overvoltage limit",
854 .range
= &charge_voltage_regulation_ranges
[0],
856 .data
= &init
->vbatovp_set
,
858 .name
= "fast-charging max current",
859 .range
= &fast_charge_current_ranges
[0],
861 .data
= &init
->ichg_set
,
863 .name
= "fast-charging voltage",
864 .range
= &charge_voltage_regulation_ranges
[0],
866 .data
= &init
->vfastchg_reg_set1
,
869 struct dt_init props
[] = {
871 .prop
= "rohm,vsys-regulation-microvolt",
872 .range
= &vsys_voltage_regulation_ranges
[0],
874 .data
= &init
->vsysreg_set
,
876 .prop
= "rohm,vbus-input-current-limit-microamp",
877 .range
= &input_current_limit_ranges
[0],
879 .data
= &init
->ibus_lim_set
,
881 .prop
= "rohm,vcc-input-current-limit-microamp",
882 .range
= &input_current_limit_ranges
[0],
884 .data
= &init
->icc_lim_set
,
889 * The power_supply_get_battery_info() does not support getting values
890 * from ACPI. Let's fix it if ACPI is required here.
892 ret
= power_supply_get_battery_info(bd
->charger
, &info
);
896 /* Put pointers to the generic battery info */
897 battery_inits
[0].info_data
= &info
->tricklecharge_current_ua
;
898 battery_inits
[1].info_data
= &info
->precharge_current_ua
;
899 battery_inits
[2].info_data
= &info
->precharge_voltage_max_uv
;
900 battery_inits
[3].info_data
= &info
->charge_term_current_ua
;
901 battery_inits
[4].info_data
= &info
->charge_restart_voltage_uv
;
902 battery_inits
[5].info_data
= &info
->overvoltage_limit_uv
;
903 battery_inits
[6].info_data
= &info
->constant_charge_current_max_ua
;
904 battery_inits
[7].info_data
= &info
->constant_charge_voltage_max_uv
;
906 for (i
= 0; i
< ARRAY_SIZE(battery_inits
); i
++) {
907 int val
= *battery_inits
[i
].info_data
;
908 const struct linear_range
*range
= battery_inits
[i
].range
;
909 int ranges
= battery_inits
[i
].ranges
;
914 ret
= linear_range_get_selector_low_array(range
, ranges
, val
,
917 dev_err(bd
->dev
, "Unsupported value for %s\n",
918 battery_inits
[i
].name
);
920 power_supply_put_battery_info(bd
->charger
, info
);
925 "Unsupported value for %s - using smaller\n",
926 battery_inits
[i
].name
);
928 *(battery_inits
[i
].data
) = regval
;
931 power_supply_put_battery_info(bd
->charger
, info
);
933 for (i
= 0; i
< ARRAY_SIZE(props
); i
++) {
934 ret
= device_property_read_u32(bd
->dev
, props
[i
].prop
,
937 dev_err(bd
->dev
, "failed to read %s", props
[i
].prop
);
942 ret
= linear_range_get_selector_low_array(props
[i
].range
,
947 dev_err(bd
->dev
, "Unsupported value for '%s'\n",
955 "Unsupported value for '%s' - using smaller\n",
959 *(props
[i
].data
) = regval
;
965 static void bd9995x_chip_reset(void *bd
)
967 __bd9995x_chip_reset(bd
);
970 static int bd9995x_probe(struct i2c_client
*client
)
972 struct device
*dev
= &client
->dev
;
973 struct bd9995x_device
*bd
;
974 struct power_supply_config psy_cfg
= {};
978 bd
= devm_kzalloc(dev
, sizeof(*bd
), GFP_KERNEL
);
984 psy_cfg
.drv_data
= bd
;
985 psy_cfg
.of_node
= dev
->of_node
;
987 mutex_init(&bd
->lock
);
989 bd
->rmap
= devm_regmap_init_i2c(client
, &bd9995x_regmap_config
);
990 if (IS_ERR(bd
->rmap
)) {
991 dev_err(dev
, "Failed to setup register access via i2c\n");
992 return PTR_ERR(bd
->rmap
);
995 for (i
= 0; i
< ARRAY_SIZE(bd9995x_reg_fields
); i
++) {
996 const struct reg_field
*reg_fields
= bd9995x_reg_fields
;
998 bd
->rmap_fields
[i
] = devm_regmap_field_alloc(dev
, bd
->rmap
,
1000 if (IS_ERR(bd
->rmap_fields
[i
])) {
1001 dev_err(dev
, "cannot allocate regmap field\n");
1002 return PTR_ERR(bd
->rmap_fields
[i
]);
1006 i2c_set_clientdata(client
, bd
);
1008 ret
= regmap_field_read(bd
->rmap_fields
[F_CHIP_ID
], &bd
->chip_id
);
1010 dev_err(dev
, "Cannot read chip ID.\n");
1014 if (bd
->chip_id
!= BD99954_ID
) {
1015 dev_err(dev
, "Chip with ID=0x%x, not supported!\n",
1020 ret
= regmap_field_read(bd
->rmap_fields
[F_CHIP_REV
], &bd
->chip_rev
);
1022 dev_err(dev
, "Cannot read revision.\n");
1026 dev_info(bd
->dev
, "Found BD99954 chip rev %d\n", bd
->chip_rev
);
1029 * We need to init the psy before we can call
1030 * power_supply_get_battery_info() for it
1032 bd
->charger
= devm_power_supply_register(bd
->dev
,
1033 &bd9995x_power_supply_desc
,
1035 if (IS_ERR(bd
->charger
)) {
1036 dev_err(dev
, "Failed to register power supply\n");
1037 return PTR_ERR(bd
->charger
);
1040 ret
= bd9995x_fw_probe(bd
);
1042 dev_err(dev
, "Cannot read device properties.\n");
1046 ret
= bd9995x_hw_init(bd
);
1048 dev_err(dev
, "Cannot initialize the chip.\n");
1052 ret
= devm_add_action_or_reset(dev
, bd9995x_chip_reset
, bd
);
1056 return devm_request_threaded_irq(dev
, client
->irq
, NULL
,
1057 bd9995x_irq_handler_thread
,
1058 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
,
1059 BD9995X_IRQ_PIN
, bd
);
1062 static const struct of_device_id bd9995x_of_match
[] = {
1063 { .compatible
= "rohm,bd99954", },
1066 MODULE_DEVICE_TABLE(of
, bd9995x_of_match
);
1068 static struct i2c_driver bd9995x_driver
= {
1070 .name
= "bd9995x-charger",
1071 .of_match_table
= bd9995x_of_match
,
1073 .probe
= bd9995x_probe
,
1075 module_i2c_driver(bd9995x_driver
);
1077 MODULE_AUTHOR("Laine Markus <markus.laine@fi.rohmeurope.com>");
1078 MODULE_DESCRIPTION("ROHM BD99954 charger driver");
1079 MODULE_LICENSE("GPL");