1 // SPDX-License-Identifier: GPL-2.0+
3 * Charger Driver for Rockchip rk817
5 * Copyright (c) 2021 Maya Matuszczyk <maccraft123mc@gmail.com>
7 * Authors: Maya Matuszczyk <maccraft123mc@gmail.com>
8 * Chris Morgan <macromorgan@hotmail.com>
11 #include <asm/unaligned.h>
12 #include <linux/devm-helpers.h>
13 #include <linux/mfd/rk808.h>
14 #include <linux/irq.h>
16 #include <linux/platform_device.h>
17 #include <linux/power_supply.h>
18 #include <linux/regmap.h>
20 /* Charging statuses reported by hardware register */
21 enum rk817_charge_status
{
33 * Max charging current read to/written from hardware register.
34 * Note how highest value corresponding to 0x7 is the lowest
35 * current, this is per the datasheet.
48 struct rk817_charger
{
52 struct power_supply
*bat_ps
;
53 struct power_supply
*chg_ps
;
58 * voltage_k and voltage_b values are used to calibrate the ADC
59 * voltage readings. While they are documented in the BSP kernel and
60 * datasheet as voltage_k and voltage_b, there is no further
61 * information explaining them in more detail.
68 * soc - state of charge - like the BSP this is stored as a percentage,
69 * to the thousandth. BSP has a display state of charge (dsoc) and a
70 * remaining state of charge (rsoc). This value will be used for both
71 * purposes here so we don't do any fancy math to try and "smooth" the
72 * charge and just report it as it is. Note for example an soc of 100
73 * is stored as 100000, an soc of 50 is stored as 50000, etc.
78 * Capacity of battery when fully charged, equal or less than design
79 * capacity depending upon wear. BSP kernel saves to nvram in mAh,
80 * so this value is in mAh not the standard uAh.
85 * Calibrate the SOC on a fully charged battery, this way we can use
86 * the calibrated SOC value to correct for columb counter drift.
90 /* Implementation specific immutable properties from device tree */
92 int sleep_enter_current_ua
;
93 int sleep_filter_current_ua
;
94 int bat_charge_full_design_uah
;
95 int bat_voltage_min_design_uv
;
96 int bat_voltage_max_design_uv
;
98 /* Values updated periodically by driver for display. */
105 int charger_input_volt_avg_uv
;
107 /* Work queue to periodically update values. */
108 struct delayed_work work
;
111 /* ADC coefficients extracted from BSP kernel */
112 #define ADC_TO_CURRENT(adc_value, res_div) \
113 (adc_value * 172 / res_div)
115 #define CURRENT_TO_ADC(current, samp_res) \
116 (current * samp_res / 172)
118 #define CHARGE_TO_ADC(capacity, res_div) \
119 (capacity * res_div * 3600 / 172 * 1000)
121 #define ADC_TO_CHARGE_UAH(adc_value, res_div) \
122 (adc_value / 3600 * 172 / res_div)
124 static int rk817_chg_cur_to_reg(u32 chg_cur_ma
)
126 if (chg_cur_ma
>= 3500)
128 else if (chg_cur_ma
>= 3000)
130 else if (chg_cur_ma
>= 2750)
132 else if (chg_cur_ma
>= 2500)
134 else if (chg_cur_ma
>= 2000)
136 else if (chg_cur_ma
>= 1500)
138 else if (chg_cur_ma
>= 1000)
140 else if (chg_cur_ma
>= 500)
146 static int rk817_chg_cur_from_reg(u8 reg
)
170 static void rk817_bat_calib_vol(struct rk817_charger
*charger
)
172 uint32_t vcalib0
= 0;
173 uint32_t vcalib1
= 0;
176 /* calibrate voltage */
177 regmap_bulk_read(charger
->rk808
->regmap
, RK817_GAS_GAUGE_VCALIB0_H
,
179 vcalib0
= get_unaligned_be16(bulk_reg
);
181 regmap_bulk_read(charger
->rk808
->regmap
, RK817_GAS_GAUGE_VCALIB1_H
,
183 vcalib1
= get_unaligned_be16(bulk_reg
);
185 /* values were taken from BSP kernel */
186 charger
->voltage_k
= (4025 - 2300) * 1000 /
187 ((vcalib1
- vcalib0
) ? (vcalib1
- vcalib0
) : 1);
188 charger
->voltage_b
= 4025 - (charger
->voltage_k
* vcalib1
) / 1000;
191 static void rk817_bat_calib_cur(struct rk817_charger
*charger
)
195 /* calibrate current */
196 regmap_bulk_read(charger
->rk808
->regmap
, RK817_GAS_GAUGE_IOFFSET_H
,
198 regmap_bulk_write(charger
->rk808
->regmap
, RK817_GAS_GAUGE_CAL_OFFSET_H
,
203 * note that only the fcc_mah is really used by this driver, the other values
204 * are to ensure we can remain backwards compatible with the BSP kernel.
206 static int rk817_record_battery_nvram_values(struct rk817_charger
*charger
)
212 * write the soc value to the nvram location used by the BSP kernel
213 * for the dsoc value.
215 put_unaligned_le24(charger
->soc
, bulk_reg
);
216 ret
= regmap_bulk_write(charger
->rk808
->regmap
, RK817_GAS_GAUGE_BAT_R1
,
221 * write the remaining capacity in mah to the nvram location used by
222 * the BSP kernel for the rsoc value.
224 rsoc
= (charger
->soc
* charger
->fcc_mah
) / 100000;
225 put_unaligned_le24(rsoc
, bulk_reg
);
226 ret
= regmap_bulk_write(charger
->rk808
->regmap
, RK817_GAS_GAUGE_DATA0
,
230 /* write the fcc_mah in mAh, just as the BSP kernel does. */
231 put_unaligned_le24(charger
->fcc_mah
, bulk_reg
);
232 ret
= regmap_bulk_write(charger
->rk808
->regmap
, RK817_GAS_GAUGE_DATA3
,
240 static int rk817_bat_calib_cap(struct rk817_charger
*charger
)
242 struct rk808
*rk808
= charger
->rk808
;
243 int tmp
, charge_now
, charge_now_adc
, volt_avg
;
246 /* Calibrate the soc and fcc on a fully charged battery */
248 if (charger
->charge_status
== CHARGE_FINISH
&& (!charger
->soc_cal
)) {
250 * soc should be 100000 and columb counter should show the full
251 * charge capacity. Note that if the device is unplugged for a
252 * period of several days the columb counter will have a large
253 * margin of error, so setting it back to the full charge on
254 * a completed charge cycle should correct this (my device was
255 * showing 33% battery after 3 days unplugged when it should
256 * have been closer to 95% based on voltage and charge
260 charger
->soc
= 100000;
261 charge_now_adc
= CHARGE_TO_ADC(charger
->fcc_mah
,
263 put_unaligned_be32(charge_now_adc
, bulk_reg
);
264 regmap_bulk_write(rk808
->regmap
, RK817_GAS_GAUGE_Q_INIT_H3
,
267 charger
->soc_cal
= 1;
268 dev_dbg(charger
->dev
,
269 "Fully charged. SOC is %d, full capacity is %d\n",
270 charger
->soc
, charger
->fcc_mah
* 1000);
274 * The columb counter can drift up slightly, so we should correct for
275 * it. But don't correct it until we're at 100% soc.
277 if (charger
->charge_status
== CHARGE_FINISH
&& charger
->soc_cal
) {
278 regmap_bulk_read(rk808
->regmap
, RK817_GAS_GAUGE_Q_PRES_H3
,
280 charge_now_adc
= get_unaligned_be32(bulk_reg
);
281 if (charge_now_adc
< 0)
282 return charge_now_adc
;
283 charge_now
= ADC_TO_CHARGE_UAH(charge_now_adc
,
287 * Re-init columb counter with updated values to correct drift.
289 if (charge_now
/ 1000 > charger
->fcc_mah
) {
290 dev_dbg(charger
->dev
,
291 "Recalibrating columb counter to %d uah\n",
294 * Order of operations matters here to ensure we keep
295 * enough precision until the last step to keep from
296 * making needless updates to columb counter.
298 charge_now_adc
= CHARGE_TO_ADC(charger
->fcc_mah
,
300 put_unaligned_be32(charge_now_adc
, bulk_reg
);
301 regmap_bulk_write(rk808
->regmap
,
302 RK817_GAS_GAUGE_Q_INIT_H3
,
308 * Calibrate the fully charged capacity when we previously had a full
309 * battery (soc_cal = 1) and are now empty (at or below minimum design
310 * voltage). If our columb counter is still positive, subtract that
311 * from our fcc value to get a calibrated fcc, and if our columb
312 * counter is negative add that to our fcc (but not to exceed our
315 regmap_bulk_read(charger
->rk808
->regmap
, RK817_GAS_GAUGE_BAT_VOL_H
,
317 tmp
= get_unaligned_be16(bulk_reg
);
318 volt_avg
= (charger
->voltage_k
* tmp
) + 1000 * charger
->voltage_b
;
319 if (volt_avg
<= charger
->bat_voltage_min_design_uv
&&
321 regmap_bulk_read(rk808
->regmap
, RK817_GAS_GAUGE_Q_PRES_H3
,
323 charge_now_adc
= get_unaligned_be32(bulk_reg
);
324 charge_now
= ADC_TO_CHARGE_UAH(charge_now_adc
,
327 * Note, if charge_now is negative this will add it (what we
328 * want) and if it's positive this will subtract (also what
331 charger
->fcc_mah
= charger
->fcc_mah
- (charge_now
/ 1000);
333 dev_dbg(charger
->dev
,
334 "Recalibrating full charge capacity to %d uah\n",
335 charger
->fcc_mah
* 1000);
339 * Set the SOC to 0 if we are below the minimum system voltage.
341 if (volt_avg
<= charger
->bat_voltage_min_design_uv
) {
343 charge_now_adc
= CHARGE_TO_ADC(0, charger
->res_div
);
344 put_unaligned_be32(charge_now_adc
, bulk_reg
);
345 regmap_bulk_write(rk808
->regmap
,
346 RK817_GAS_GAUGE_Q_INIT_H3
, bulk_reg
, 4);
347 dev_warn(charger
->dev
,
348 "Battery voltage %d below minimum voltage %d\n",
349 volt_avg
, charger
->bat_voltage_min_design_uv
);
352 rk817_record_battery_nvram_values(charger
);
357 static void rk817_read_props(struct rk817_charger
*charger
)
363 * Recalibrate voltage and current readings if we need to BSP does both
364 * on CUR_CALIB_UPD, ignoring VOL_CALIB_UPD. Curiously enough, both
365 * documentation and the BSP show that you perform an update if bit 7
366 * is 1, but you clear the status by writing a 1 to bit 7.
368 regmap_read(charger
->rk808
->regmap
, RK817_GAS_GAUGE_ADC_CONFIG1
, ®
);
369 if (reg
& RK817_VOL_CUR_CALIB_UPD
) {
370 rk817_bat_calib_cur(charger
);
371 rk817_bat_calib_vol(charger
);
372 regmap_write_bits(charger
->rk808
->regmap
,
373 RK817_GAS_GAUGE_ADC_CONFIG1
,
374 RK817_VOL_CUR_CALIB_UPD
,
375 RK817_VOL_CUR_CALIB_UPD
);
378 /* Update reported charge. */
379 regmap_bulk_read(charger
->rk808
->regmap
, RK817_GAS_GAUGE_Q_PRES_H3
,
381 tmp
= get_unaligned_be32(bulk_reg
);
382 charger
->charge_now_uah
= ADC_TO_CHARGE_UAH(tmp
, charger
->res_div
);
383 if (charger
->charge_now_uah
< 0)
384 charger
->charge_now_uah
= 0;
385 if (charger
->charge_now_uah
> charger
->fcc_mah
* 1000)
386 charger
->charge_now_uah
= charger
->fcc_mah
* 1000;
388 /* Update soc based on reported charge. */
389 charger
->soc
= charger
->charge_now_uah
* 100 / charger
->fcc_mah
;
391 /* Update reported voltage. */
392 regmap_bulk_read(charger
->rk808
->regmap
, RK817_GAS_GAUGE_BAT_VOL_H
,
394 tmp
= get_unaligned_be16(bulk_reg
);
395 charger
->volt_avg_uv
= (charger
->voltage_k
* tmp
) + 1000 *
399 * Update reported current. Note value from registers is a signed 16
402 regmap_bulk_read(charger
->rk808
->regmap
, RK817_GAS_GAUGE_BAT_CUR_H
,
404 tmp
= (short int)get_unaligned_be16(bulk_reg
);
405 charger
->cur_avg_ua
= ADC_TO_CURRENT(tmp
, charger
->res_div
);
408 * Update the max charge current. This value shouldn't change, but we
409 * can read it to report what the PMIC says it is instead of simply
410 * returning the default value.
412 regmap_read(charger
->rk808
->regmap
, RK817_PMIC_CHRG_OUT
, ®
);
413 charger
->max_chg_cur_ua
=
414 rk817_chg_cur_from_reg(reg
& RK817_CHRG_CUR_SEL
);
417 * Update max charge voltage. Like the max charge current this value
418 * shouldn't change, but we can report what the PMIC says.
420 regmap_read(charger
->rk808
->regmap
, RK817_PMIC_CHRG_OUT
, ®
);
421 charger
->max_chg_volt_uv
= ((((reg
& RK817_CHRG_VOL_SEL
) >> 4) *
424 /* Check if battery still present. */
425 regmap_read(charger
->rk808
->regmap
, RK817_PMIC_CHRG_STS
, ®
);
426 charger
->battery_present
= (reg
& RK817_BAT_EXS
);
428 /* Get which type of charge we are using (if any). */
429 regmap_read(charger
->rk808
->regmap
, RK817_PMIC_CHRG_STS
, ®
);
430 charger
->charge_status
= (reg
>> 4) & 0x07;
433 * Get charger input voltage. Note that on my example hardware (an
434 * Odroid Go Advance) the voltage of the power connector is measured
435 * on the register labelled USB in the datasheet; I don't know if this
436 * is how it is designed or just a quirk of the implementation. I
437 * believe this will also measure the voltage of the USB output when in
438 * OTG mode, if that is the case we may need to change this in the
439 * future to return 0 if the power supply status is offline (I can't
440 * test this with my current implementation. Also, when the voltage
441 * should be zero sometimes the ADC still shows a single bit (which
442 * would register as 20000uv). When this happens set it to 0.
444 regmap_bulk_read(charger
->rk808
->regmap
, RK817_GAS_GAUGE_USB_VOL_H
,
446 reg
= get_unaligned_be16(bulk_reg
);
448 tmp
= ((charger
->voltage_k
* reg
/ 1000 + charger
->voltage_b
) *
450 charger
->charger_input_volt_avg_uv
= tmp
* 1000;
452 charger
->charger_input_volt_avg_uv
= 0;
455 /* Calibrate battery capacity and soc. */
456 rk817_bat_calib_cap(charger
);
459 static int rk817_bat_get_prop(struct power_supply
*ps
,
460 enum power_supply_property prop
,
461 union power_supply_propval
*val
)
463 struct rk817_charger
*charger
= power_supply_get_drvdata(ps
);
466 case POWER_SUPPLY_PROP_PRESENT
:
467 val
->intval
= charger
->battery_present
;
469 case POWER_SUPPLY_PROP_STATUS
:
470 if (charger
->cur_avg_ua
< 0) {
471 val
->intval
= POWER_SUPPLY_STATUS_DISCHARGING
;
474 switch (charger
->charge_status
) {
476 val
->intval
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
479 * Dead charge is documented, but not explained. I never
480 * observed it but assume it's a pre-charge for a dead
486 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
489 val
->intval
= POWER_SUPPLY_STATUS_FULL
;
492 val
->intval
= POWER_SUPPLY_STATUS_UNKNOWN
;
497 case POWER_SUPPLY_PROP_CHARGE_TYPE
:
498 switch (charger
->charge_status
) {
501 val
->intval
= POWER_SUPPLY_CHARGE_TYPE_NONE
;
504 val
->intval
= POWER_SUPPLY_CHARGE_TYPE_TRICKLE
;
508 val
->intval
= POWER_SUPPLY_CHARGE_TYPE_STANDARD
;
511 val
->intval
= POWER_SUPPLY_CHARGE_TYPE_UNKNOWN
;
515 case POWER_SUPPLY_PROP_CHARGE_FULL
:
516 val
->intval
= charger
->fcc_mah
* 1000;
518 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
:
519 val
->intval
= charger
->bat_charge_full_design_uah
;
521 case POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN
:
524 case POWER_SUPPLY_PROP_CHARGE_NOW
:
525 val
->intval
= charger
->charge_now_uah
;
527 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
:
528 val
->intval
= charger
->bat_voltage_min_design_uv
;
530 case POWER_SUPPLY_PROP_CAPACITY
:
531 /* Add 500 so that values like 99999 are 100% not 99%. */
532 val
->intval
= (charger
->soc
+ 500) / 1000;
533 if (val
->intval
> 100)
538 case POWER_SUPPLY_PROP_VOLTAGE_AVG
:
539 val
->intval
= charger
->volt_avg_uv
;
541 case POWER_SUPPLY_PROP_CURRENT_AVG
:
542 val
->intval
= charger
->cur_avg_ua
;
544 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX
:
545 val
->intval
= charger
->max_chg_cur_ua
;
547 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX
:
548 val
->intval
= charger
->max_chg_volt_uv
;
550 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
:
551 val
->intval
= charger
->bat_voltage_max_design_uv
;
559 static int rk817_chg_get_prop(struct power_supply
*ps
,
560 enum power_supply_property prop
,
561 union power_supply_propval
*val
)
563 struct rk817_charger
*charger
= power_supply_get_drvdata(ps
);
566 case POWER_SUPPLY_PROP_ONLINE
:
567 val
->intval
= charger
->plugged_in
;
569 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
:
570 /* max voltage from datasheet at 5.5v (default 5.0v) */
571 val
->intval
= 5500000;
573 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
:
574 /* min voltage from datasheet at 3.8v (default 5.0v) */
575 val
->intval
= 3800000;
577 case POWER_SUPPLY_PROP_VOLTAGE_AVG
:
578 val
->intval
= charger
->charger_input_volt_avg_uv
;
581 * While it's possible that other implementations could use different
582 * USB types, the current implementation for this PMIC (the Odroid Go
583 * Advance) only uses a dedicated charging port with no rx/tx lines.
585 case POWER_SUPPLY_PROP_USB_TYPE
:
586 val
->intval
= POWER_SUPPLY_USB_TYPE_DCP
;
595 static irqreturn_t
rk817_plug_in_isr(int irq
, void *cg
)
597 struct rk817_charger
*charger
;
599 charger
= (struct rk817_charger
*)cg
;
600 charger
->plugged_in
= 1;
601 power_supply_changed(charger
->chg_ps
);
602 power_supply_changed(charger
->bat_ps
);
603 /* try to recalibrate capacity if we hit full charge. */
604 charger
->soc_cal
= 0;
606 rk817_read_props(charger
);
608 dev_dbg(charger
->dev
, "Power Cord Inserted\n");
613 static irqreturn_t
rk817_plug_out_isr(int irq
, void *cg
)
615 struct rk817_charger
*charger
;
618 charger
= (struct rk817_charger
*)cg
;
619 rk808
= charger
->rk808
;
620 charger
->plugged_in
= 0;
621 power_supply_changed(charger
->bat_ps
);
622 power_supply_changed(charger
->chg_ps
);
625 * For some reason the bits of RK817_PMIC_CHRG_IN reset whenever the
626 * power cord is unplugged. This was not documented in the BSP kernel
627 * or the datasheet and only discovered by trial and error. Set minimum
628 * USB input voltage to 4.5v and enable USB voltage input limit.
630 regmap_write_bits(rk808
->regmap
, RK817_PMIC_CHRG_IN
,
631 RK817_USB_VLIM_SEL
, (0x05 << 4));
632 regmap_write_bits(rk808
->regmap
, RK817_PMIC_CHRG_IN
, RK817_USB_VLIM_EN
,
636 * Set average USB input current limit to 1.5A and enable USB current
639 regmap_write_bits(rk808
->regmap
, RK817_PMIC_CHRG_IN
,
640 RK817_USB_ILIM_SEL
, 0x03);
641 regmap_write_bits(rk808
->regmap
, RK817_PMIC_CHRG_IN
, RK817_USB_ILIM_EN
,
644 rk817_read_props(charger
);
646 dev_dbg(charger
->dev
, "Power Cord Removed\n");
651 static enum power_supply_property rk817_bat_props
[] = {
652 POWER_SUPPLY_PROP_PRESENT
,
653 POWER_SUPPLY_PROP_STATUS
,
654 POWER_SUPPLY_PROP_CHARGE_TYPE
,
655 POWER_SUPPLY_PROP_CHARGE_FULL
,
656 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
,
657 POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN
,
658 POWER_SUPPLY_PROP_CHARGE_NOW
,
659 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX
,
660 POWER_SUPPLY_PROP_VOLTAGE_AVG
,
661 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX
,
662 POWER_SUPPLY_PROP_CURRENT_AVG
,
663 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
,
664 POWER_SUPPLY_PROP_CAPACITY
,
665 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
,
668 static enum power_supply_property rk817_chg_props
[] = {
669 POWER_SUPPLY_PROP_ONLINE
,
670 POWER_SUPPLY_PROP_USB_TYPE
,
671 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
,
672 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
,
673 POWER_SUPPLY_PROP_VOLTAGE_AVG
,
676 static const struct power_supply_desc rk817_bat_desc
= {
677 .name
= "rk817-battery",
678 .type
= POWER_SUPPLY_TYPE_BATTERY
,
679 .properties
= rk817_bat_props
,
680 .num_properties
= ARRAY_SIZE(rk817_bat_props
),
681 .get_property
= rk817_bat_get_prop
,
684 static const struct power_supply_desc rk817_chg_desc
= {
685 .name
= "rk817-charger",
686 .type
= POWER_SUPPLY_TYPE_USB
,
687 .usb_types
= BIT(POWER_SUPPLY_USB_TYPE_DCP
) |
688 BIT(POWER_SUPPLY_USB_TYPE_UNKNOWN
),
689 .properties
= rk817_chg_props
,
690 .num_properties
= ARRAY_SIZE(rk817_chg_props
),
691 .get_property
= rk817_chg_get_prop
,
694 static int rk817_read_battery_nvram_values(struct rk817_charger
*charger
)
699 /* Read the nvram data for full charge capacity. */
700 ret
= regmap_bulk_read(charger
->rk808
->regmap
,
701 RK817_GAS_GAUGE_DATA3
, bulk_reg
, 3);
704 charger
->fcc_mah
= get_unaligned_le24(bulk_reg
);
707 * Sanity checking for values equal to zero or less than would be
708 * practical for this device (BSP Kernel assumes 500mAH or less) for
709 * practicality purposes. Also check if the value is too large and
712 if ((charger
->fcc_mah
< 500) ||
713 ((charger
->fcc_mah
* 1000) > charger
->bat_charge_full_design_uah
)) {
714 dev_info(charger
->dev
,
715 "Invalid NVRAM max charge, setting to %u uAH\n",
716 charger
->bat_charge_full_design_uah
);
717 charger
->fcc_mah
= charger
->bat_charge_full_design_uah
/ 1000;
721 * Read the nvram for state of charge. Sanity check for values greater
722 * than 100 (10000) or less than 0, because other things (BSP kernels,
723 * U-Boot, or even i2cset) can write to this register. If the value is
724 * off it should get corrected automatically when the voltage drops to
725 * the min (soc is 0) or when the battery is full (soc is 100).
727 ret
= regmap_bulk_read(charger
->rk808
->regmap
,
728 RK817_GAS_GAUGE_BAT_R1
, bulk_reg
, 3);
731 charger
->soc
= get_unaligned_le24(bulk_reg
);
732 if (charger
->soc
> 10000)
733 charger
->soc
= 10000;
734 if (charger
->soc
< 0)
741 rk817_read_or_set_full_charge_on_boot(struct rk817_charger
*charger
,
742 struct power_supply_battery_info
*bat_info
)
744 struct rk808
*rk808
= charger
->rk808
;
746 u32 boot_voltage
, boot_charge_mah
;
747 int ret
, reg
, off_time
, tmp
;
751 * Check if the battery is uninitalized. If it is, the columb counter
752 * needs to be set up.
754 ret
= regmap_read(rk808
->regmap
, RK817_GAS_GAUGE_GG_STS
, ®
);
757 first_boot
= reg
& RK817_BAT_CON
;
759 * If the battery is uninitialized, use the poweron voltage and an ocv
760 * lookup to guess our charge. The number won't be very accurate until
761 * we hit either our minimum voltage (0%) or full charge (100%).
764 regmap_bulk_read(rk808
->regmap
, RK817_GAS_GAUGE_PWRON_VOL_H
,
766 tmp
= get_unaligned_be16(bulk_reg
);
767 boot_voltage
= (charger
->voltage_k
* tmp
) +
768 1000 * charger
->voltage_b
;
770 * Since only implementation has no working thermistor, assume
771 * 20C for OCV lookup. If lookup fails, report error with OCV
774 charger
->soc
= power_supply_batinfo_ocv2cap(bat_info
,
777 if (charger
->soc
< 0)
780 /* Guess that full charge capacity is the design capacity */
781 charger
->fcc_mah
= charger
->bat_charge_full_design_uah
/ 1000;
783 * Set battery as "set up". BSP driver uses this value even
784 * though datasheet claims it's a read-only value.
786 regmap_write_bits(rk808
->regmap
, RK817_GAS_GAUGE_GG_STS
,
788 /* Save nvram values */
789 ret
= rk817_record_battery_nvram_values(charger
);
793 ret
= rk817_read_battery_nvram_values(charger
);
797 regmap_bulk_read(rk808
->regmap
, RK817_GAS_GAUGE_Q_PRES_H3
,
799 tmp
= get_unaligned_be32(bulk_reg
);
802 boot_charge_mah
= ADC_TO_CHARGE_UAH(tmp
,
803 charger
->res_div
) / 1000;
805 * Check if the columb counter has been off for more than 30
806 * minutes as it tends to drift downward. If so, re-init soc
807 * with the boot voltage instead. Note the unit values for the
808 * OFF_CNT register appear to be in decaminutes and stops
809 * counting at 2550 (0xFF) minutes. BSP kernel used OCV, but
810 * for me occasionally that would show invalid values. Boot
811 * voltage is only accurate for me on first poweron (not
812 * reboots), but we shouldn't ever encounter an OFF_CNT more
813 * than 0 on a reboot anyway.
815 regmap_read(rk808
->regmap
, RK817_GAS_GAUGE_OFF_CNT
, &off_time
);
817 regmap_bulk_read(rk808
->regmap
,
818 RK817_GAS_GAUGE_PWRON_VOL_H
,
820 tmp
= get_unaligned_be16(bulk_reg
);
821 boot_voltage
= (charger
->voltage_k
* tmp
) +
822 1000 * charger
->voltage_b
;
824 power_supply_batinfo_ocv2cap(bat_info
,
828 charger
->soc
= (boot_charge_mah
* 1000 * 100 /
834 * Now we have our full charge capacity and soc, init the columb
837 boot_charge_mah
= charger
->soc
* charger
->fcc_mah
/ 100 / 1000;
838 if (boot_charge_mah
> charger
->fcc_mah
)
839 boot_charge_mah
= charger
->fcc_mah
;
840 tmp
= CHARGE_TO_ADC(boot_charge_mah
, charger
->res_div
);
841 put_unaligned_be32(tmp
, bulk_reg
);
842 ret
= regmap_bulk_write(rk808
->regmap
, RK817_GAS_GAUGE_Q_INIT_H3
,
847 /* Set QMAX value to max design capacity. */
848 tmp
= CHARGE_TO_ADC((charger
->bat_charge_full_design_uah
/ 1000),
850 put_unaligned_be32(tmp
, bulk_reg
);
851 ret
= regmap_bulk_write(rk808
->regmap
, RK817_GAS_GAUGE_Q_MAX_H3
,
859 static int rk817_battery_init(struct rk817_charger
*charger
,
860 struct power_supply_battery_info
*bat_info
)
862 struct rk808
*rk808
= charger
->rk808
;
863 u32 tmp
, max_chg_vol_mv
, max_chg_cur_ma
;
864 u8 max_chg_vol_reg
, chg_term_i_reg
;
865 int ret
, chg_term_ma
, max_chg_cur_reg
;
868 /* Get initial plug state */
869 regmap_read(rk808
->regmap
, RK817_SYS_STS
, &tmp
);
870 charger
->plugged_in
= (tmp
& RK817_PLUG_IN_STS
);
873 * Turn on all ADC functions to measure battery, USB, and sys voltage,
874 * as well as batt temp. Note only tested implementation so far does
875 * not use a battery with a thermistor.
877 regmap_write(rk808
->regmap
, RK817_GAS_GAUGE_ADC_CONFIG0
, 0xfc);
880 * Set relax mode voltage sampling interval and ADC offset calibration
881 * interval to 8 minutes to mirror BSP kernel. Set voltage and current
882 * modes to average to mirror BSP kernel.
884 regmap_write(rk808
->regmap
, RK817_GAS_GAUGE_GG_CON
, 0x04);
886 /* Calibrate voltage like the BSP does here. */
887 rk817_bat_calib_vol(charger
);
889 /* Write relax threshold, derived from sleep enter current. */
890 tmp
= CURRENT_TO_ADC(charger
->sleep_enter_current_ua
,
892 put_unaligned_be16(tmp
, bulk_reg
);
893 regmap_bulk_write(rk808
->regmap
, RK817_GAS_GAUGE_RELAX_THRE_H
,
896 /* Write sleep sample current, derived from sleep filter current. */
897 tmp
= CURRENT_TO_ADC(charger
->sleep_filter_current_ua
,
899 put_unaligned_be16(tmp
, bulk_reg
);
900 regmap_bulk_write(rk808
->regmap
, RK817_GAS_GAUGE_SLEEP_CON_SAMP_CUR_H
,
903 /* Restart battery relax voltage */
904 regmap_write_bits(rk808
->regmap
, RK817_GAS_GAUGE_GG_STS
,
905 RK817_RELAX_VOL_UPD
, (0x0 << 2));
908 * Set OCV Threshold Voltage to 127.5mV. This was hard coded like this
911 regmap_write(rk808
->regmap
, RK817_GAS_GAUGE_OCV_THRE_VOL
, 0xff);
914 * Set maximum charging voltage to battery max voltage. Trying to be
915 * incredibly safe with these value, as setting them wrong could
916 * overcharge the battery, which would be very bad.
918 max_chg_vol_mv
= bat_info
->constant_charge_voltage_max_uv
/ 1000;
919 max_chg_cur_ma
= bat_info
->constant_charge_current_max_ua
/ 1000;
921 if (max_chg_vol_mv
< 4100) {
922 return dev_err_probe(charger
->dev
, -EINVAL
,
923 "invalid max charger voltage, value %u unsupported\n",
924 max_chg_vol_mv
* 1000);
926 if (max_chg_vol_mv
> 4450) {
927 dev_info(charger
->dev
,
928 "Setting max charge voltage to 4450000uv\n");
929 max_chg_vol_mv
= 4450;
932 if (max_chg_cur_ma
< 500) {
933 return dev_err_probe(charger
->dev
, -EINVAL
,
934 "invalid max charger current, value %u unsupported\n",
935 max_chg_cur_ma
* 1000);
937 if (max_chg_cur_ma
> 3500)
938 dev_info(charger
->dev
,
939 "Setting max charge current to 3500000ua\n");
942 * Now that the values are sanity checked, if we subtract 4100 from the
943 * max voltage and divide by 50, we conviently get the exact value for
944 * the registers, which are 4.1v, 4.15v, 4.2v, 4.25v, 4.3v, 4.35v,
945 * 4.4v, and 4.45v; these correspond to values 0x00 through 0x07.
947 max_chg_vol_reg
= (max_chg_vol_mv
- 4100) / 50;
949 max_chg_cur_reg
= rk817_chg_cur_to_reg(max_chg_cur_ma
);
951 if (max_chg_vol_reg
< 0 || max_chg_vol_reg
> 7) {
952 return dev_err_probe(charger
->dev
, -EINVAL
,
953 "invalid max charger voltage, value %u unsupported\n",
954 max_chg_vol_mv
* 1000);
956 if (max_chg_cur_reg
< 0 || max_chg_cur_reg
> 7) {
957 return dev_err_probe(charger
->dev
, -EINVAL
,
958 "invalid max charger current, value %u unsupported\n",
959 max_chg_cur_ma
* 1000);
963 * Write the values to the registers, and deliver an emergency warning
964 * in the event they are not written correctly.
966 ret
= regmap_write_bits(rk808
->regmap
, RK817_PMIC_CHRG_OUT
,
967 RK817_CHRG_VOL_SEL
, (max_chg_vol_reg
<< 4));
969 dev_emerg(charger
->dev
,
970 "Danger, unable to set max charger voltage: %u\n",
974 ret
= regmap_write_bits(rk808
->regmap
, RK817_PMIC_CHRG_OUT
,
975 RK817_CHRG_CUR_SEL
, max_chg_cur_reg
);
977 dev_emerg(charger
->dev
,
978 "Danger, unable to set max charger current: %u\n",
982 /* Set charge finishing mode to analog */
983 regmap_write_bits(rk808
->regmap
, RK817_PMIC_CHRG_TERM
,
984 RK817_CHRG_TERM_ANA_DIG
, (0x0 << 2));
987 * Set charge finish current, warn if value not in range and keep
990 chg_term_ma
= bat_info
->charge_term_current_ua
/ 1000;
991 if (chg_term_ma
< 150 || chg_term_ma
> 400) {
992 dev_warn(charger
->dev
,
993 "Invalid charge termination %u, keeping default\n",
999 * Values of 150ma, 200ma, 300ma, and 400ma correspond to 00, 01, 10,
1002 chg_term_i_reg
= (chg_term_ma
- 100) / 100;
1003 regmap_write_bits(rk808
->regmap
, RK817_PMIC_CHRG_TERM
,
1004 RK817_CHRG_TERM_ANA_SEL
, chg_term_i_reg
);
1006 ret
= rk817_read_or_set_full_charge_on_boot(charger
, bat_info
);
1011 * Set minimum USB input voltage to 4.5v and enable USB voltage input
1014 regmap_write_bits(rk808
->regmap
, RK817_PMIC_CHRG_IN
,
1015 RK817_USB_VLIM_SEL
, (0x05 << 4));
1016 regmap_write_bits(rk808
->regmap
, RK817_PMIC_CHRG_IN
, RK817_USB_VLIM_EN
,
1020 * Set average USB input current limit to 1.5A and enable USB current
1023 regmap_write_bits(rk808
->regmap
, RK817_PMIC_CHRG_IN
,
1024 RK817_USB_ILIM_SEL
, 0x03);
1025 regmap_write_bits(rk808
->regmap
, RK817_PMIC_CHRG_IN
, RK817_USB_ILIM_EN
,
1031 static void rk817_charging_monitor(struct work_struct
*work
)
1033 struct rk817_charger
*charger
;
1035 charger
= container_of(work
, struct rk817_charger
, work
.work
);
1037 rk817_read_props(charger
);
1039 /* Run every 8 seconds like the BSP driver did. */
1040 queue_delayed_work(system_wq
, &charger
->work
, msecs_to_jiffies(8000));
1043 static void rk817_cleanup_node(void *data
)
1045 struct device_node
*node
= data
;
1050 static int rk817_charger_probe(struct platform_device
*pdev
)
1052 struct rk808
*rk808
= dev_get_drvdata(pdev
->dev
.parent
);
1053 struct rk817_charger
*charger
;
1054 struct device_node
*node
;
1055 struct power_supply_battery_info
*bat_info
;
1056 struct device
*dev
= &pdev
->dev
;
1057 struct power_supply_config pscfg
= {};
1058 int plugin_irq
, plugout_irq
;
1062 node
= of_get_child_by_name(dev
->parent
->of_node
, "charger");
1066 ret
= devm_add_action_or_reset(&pdev
->dev
, rk817_cleanup_node
, node
);
1070 charger
= devm_kzalloc(&pdev
->dev
, sizeof(*charger
), GFP_KERNEL
);
1074 charger
->rk808
= rk808
;
1076 charger
->dev
= &pdev
->dev
;
1077 platform_set_drvdata(pdev
, charger
);
1079 rk817_bat_calib_vol(charger
);
1081 pscfg
.drv_data
= charger
;
1082 pscfg
.of_node
= node
;
1085 * Get sample resistor value. Note only values of 10000 or 20000
1086 * microohms are allowed. Schematic for my test implementation (an
1087 * Odroid Go Advance) shows a 10 milliohm resistor for reference.
1089 ret
= of_property_read_u32(node
, "rockchip,resistor-sense-micro-ohms",
1092 return dev_err_probe(dev
, ret
,
1093 "Error reading sample resistor value\n");
1096 * Store as a 1 or a 2, since all we really use the value for is as a
1097 * divisor in some calculations.
1099 charger
->res_div
= (of_value
== 20000) ? 2 : 1;
1102 * Get sleep enter current value. Not sure what this value is for
1103 * other than to help calibrate the relax threshold.
1105 ret
= of_property_read_u32(node
,
1106 "rockchip,sleep-enter-current-microamp",
1109 return dev_err_probe(dev
, ret
,
1110 "Error reading sleep enter cur value\n");
1112 charger
->sleep_enter_current_ua
= of_value
;
1114 /* Get sleep filter current value */
1115 ret
= of_property_read_u32(node
,
1116 "rockchip,sleep-filter-current-microamp",
1119 return dev_err_probe(dev
, ret
,
1120 "Error reading sleep filter cur value\n");
1123 charger
->sleep_filter_current_ua
= of_value
;
1125 charger
->bat_ps
= devm_power_supply_register(&pdev
->dev
,
1126 &rk817_bat_desc
, &pscfg
);
1127 if (IS_ERR(charger
->bat_ps
))
1128 return dev_err_probe(dev
, -EINVAL
,
1129 "Battery failed to probe\n");
1131 charger
->chg_ps
= devm_power_supply_register(&pdev
->dev
,
1132 &rk817_chg_desc
, &pscfg
);
1133 if (IS_ERR(charger
->chg_ps
))
1134 return dev_err_probe(dev
, -EINVAL
,
1135 "Charger failed to probe\n");
1137 ret
= power_supply_get_battery_info(charger
->bat_ps
,
1140 return dev_err_probe(dev
, ret
,
1141 "Unable to get battery info\n");
1144 if ((bat_info
->charge_full_design_uah
<= 0) ||
1145 (bat_info
->voltage_min_design_uv
<= 0) ||
1146 (bat_info
->voltage_max_design_uv
<= 0) ||
1147 (bat_info
->constant_charge_voltage_max_uv
<= 0) ||
1148 (bat_info
->constant_charge_current_max_ua
<= 0) ||
1149 (bat_info
->charge_term_current_ua
<= 0)) {
1150 return dev_err_probe(dev
, -EINVAL
,
1151 "Required bat info missing or invalid\n");
1154 charger
->bat_charge_full_design_uah
= bat_info
->charge_full_design_uah
;
1155 charger
->bat_voltage_min_design_uv
= bat_info
->voltage_min_design_uv
;
1156 charger
->bat_voltage_max_design_uv
= bat_info
->voltage_max_design_uv
;
1159 * Has to run after power_supply_get_battery_info as it depends on some
1160 * values discovered from that routine.
1162 ret
= rk817_battery_init(charger
, bat_info
);
1166 power_supply_put_battery_info(charger
->bat_ps
, bat_info
);
1168 plugin_irq
= platform_get_irq(pdev
, 0);
1172 plugout_irq
= platform_get_irq(pdev
, 1);
1173 if (plugout_irq
< 0)
1176 ret
= devm_request_threaded_irq(charger
->dev
, plugin_irq
, NULL
,
1178 IRQF_TRIGGER_RISING
| IRQF_ONESHOT
,
1179 "rk817_plug_in", charger
);
1181 return dev_err_probe(&pdev
->dev
, ret
,
1182 "plug_in_irq request failed!\n");
1185 ret
= devm_request_threaded_irq(charger
->dev
, plugout_irq
, NULL
,
1187 IRQF_TRIGGER_RISING
| IRQF_ONESHOT
,
1188 "rk817_plug_out", charger
);
1190 return dev_err_probe(&pdev
->dev
, ret
,
1191 "plug_out_irq request failed!\n");
1194 ret
= devm_delayed_work_autocancel(&pdev
->dev
, &charger
->work
,
1195 rk817_charging_monitor
);
1199 /* Force the first update immediately. */
1200 mod_delayed_work(system_wq
, &charger
->work
, 0);
1205 static int __maybe_unused
rk817_resume(struct device
*dev
)
1208 struct rk817_charger
*charger
= dev_get_drvdata(dev
);
1210 /* force an immediate update */
1211 mod_delayed_work(system_wq
, &charger
->work
, 0);
1216 static SIMPLE_DEV_PM_OPS(rk817_charger_pm
, NULL
, rk817_resume
);
1218 static struct platform_driver rk817_charger_driver
= {
1219 .probe
= rk817_charger_probe
,
1221 .name
= "rk817-charger",
1222 .pm
= &rk817_charger_pm
,
1225 module_platform_driver(rk817_charger_driver
);
1227 MODULE_DESCRIPTION("Battery power supply driver for RK817 PMIC");
1228 MODULE_AUTHOR("Maya Matuszczyk <maccraft123mc@gmail.com>");
1229 MODULE_AUTHOR("Chris Morgan <macromorgan@hotmail.com>");
1230 MODULE_LICENSE("GPL");
1231 MODULE_ALIAS("platform:rk817-charger");