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 <linux/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 charge_now
, charge_now_adc
;
246 /* Don't do anything if there's no battery. */
247 if (!charger
->battery_present
)
251 * When resuming from suspend, sometimes the voltage value would be
252 * incorrect. BSP would simply wait two seconds and try reading the
253 * values again. Do not do any sort of calibration activity when the
254 * reported value is incorrect. The next scheduled update of battery
255 * vaules should then return valid data and the driver can continue.
256 * Use 2.7v as the sanity value because per the datasheet the PMIC
257 * can in no way support a battery voltage lower than this. BSP only
258 * checked for values too low, but I'm adding in a check for values
259 * too high just in case; again the PMIC can in no way support
260 * voltages above 4.45v, so this seems like a good value.
262 if ((charger
->volt_avg_uv
< 2700000) || (charger
->volt_avg_uv
> 4450000)) {
263 dev_dbg(charger
->dev
,
264 "Battery voltage of %d is invalid, ignoring.\n",
265 charger
->volt_avg_uv
);
269 /* Calibrate the soc and fcc on a fully charged battery */
271 if (charger
->charge_status
== CHARGE_FINISH
&& (!charger
->soc_cal
)) {
273 * soc should be 100000 and columb counter should show the full
274 * charge capacity. Note that if the device is unplugged for a
275 * period of several days the columb counter will have a large
276 * margin of error, so setting it back to the full charge on
277 * a completed charge cycle should correct this (my device was
278 * showing 33% battery after 3 days unplugged when it should
279 * have been closer to 95% based on voltage and charge
283 charger
->soc
= 100000;
284 charge_now_adc
= CHARGE_TO_ADC(charger
->fcc_mah
,
286 put_unaligned_be32(charge_now_adc
, bulk_reg
);
287 regmap_bulk_write(rk808
->regmap
, RK817_GAS_GAUGE_Q_INIT_H3
,
290 charger
->soc_cal
= 1;
291 dev_dbg(charger
->dev
,
292 "Fully charged. SOC is %d, full capacity is %d\n",
293 charger
->soc
, charger
->fcc_mah
* 1000);
297 * The columb counter can drift up slightly, so we should correct for
298 * it. But don't correct it until we're at 100% soc.
300 if (charger
->charge_status
== CHARGE_FINISH
&& charger
->soc_cal
) {
301 regmap_bulk_read(rk808
->regmap
, RK817_GAS_GAUGE_Q_PRES_H3
,
303 charge_now_adc
= get_unaligned_be32(bulk_reg
);
304 if (charge_now_adc
< 0)
305 return charge_now_adc
;
306 charge_now
= ADC_TO_CHARGE_UAH(charge_now_adc
,
310 * Re-init columb counter with updated values to correct drift.
312 if (charge_now
/ 1000 > charger
->fcc_mah
) {
313 dev_dbg(charger
->dev
,
314 "Recalibrating columb counter to %d uah\n",
317 * Order of operations matters here to ensure we keep
318 * enough precision until the last step to keep from
319 * making needless updates to columb counter.
321 charge_now_adc
= CHARGE_TO_ADC(charger
->fcc_mah
,
323 put_unaligned_be32(charge_now_adc
, bulk_reg
);
324 regmap_bulk_write(rk808
->regmap
,
325 RK817_GAS_GAUGE_Q_INIT_H3
,
330 rk817_record_battery_nvram_values(charger
);
335 static void rk817_read_props(struct rk817_charger
*charger
)
341 * Recalibrate voltage and current readings if we need to BSP does both
342 * on CUR_CALIB_UPD, ignoring VOL_CALIB_UPD. Curiously enough, both
343 * documentation and the BSP show that you perform an update if bit 7
344 * is 1, but you clear the status by writing a 1 to bit 7.
346 regmap_read(charger
->rk808
->regmap
, RK817_GAS_GAUGE_ADC_CONFIG1
, ®
);
347 if (reg
& RK817_VOL_CUR_CALIB_UPD
) {
348 rk817_bat_calib_cur(charger
);
349 rk817_bat_calib_vol(charger
);
350 regmap_write_bits(charger
->rk808
->regmap
,
351 RK817_GAS_GAUGE_ADC_CONFIG1
,
352 RK817_VOL_CUR_CALIB_UPD
,
353 RK817_VOL_CUR_CALIB_UPD
);
356 /* Update reported charge. */
357 regmap_bulk_read(charger
->rk808
->regmap
, RK817_GAS_GAUGE_Q_PRES_H3
,
359 tmp
= get_unaligned_be32(bulk_reg
);
360 charger
->charge_now_uah
= ADC_TO_CHARGE_UAH(tmp
, charger
->res_div
);
361 if (charger
->charge_now_uah
< 0)
362 charger
->charge_now_uah
= 0;
363 if (charger
->charge_now_uah
> charger
->fcc_mah
* 1000)
364 charger
->charge_now_uah
= charger
->fcc_mah
* 1000;
366 /* Update soc based on reported charge. */
367 charger
->soc
= charger
->charge_now_uah
* 100 / charger
->fcc_mah
;
369 /* Update reported voltage. */
370 regmap_bulk_read(charger
->rk808
->regmap
, RK817_GAS_GAUGE_BAT_VOL_H
,
372 tmp
= get_unaligned_be16(bulk_reg
);
373 charger
->volt_avg_uv
= (charger
->voltage_k
* tmp
) + 1000 *
377 * Update reported current. Note value from registers is a signed 16
380 regmap_bulk_read(charger
->rk808
->regmap
, RK817_GAS_GAUGE_BAT_CUR_H
,
382 tmp
= (short int)get_unaligned_be16(bulk_reg
);
383 charger
->cur_avg_ua
= ADC_TO_CURRENT(tmp
, charger
->res_div
);
386 * Update the max charge current. This value shouldn't change, but we
387 * can read it to report what the PMIC says it is instead of simply
388 * returning the default value.
390 regmap_read(charger
->rk808
->regmap
, RK817_PMIC_CHRG_OUT
, ®
);
391 charger
->max_chg_cur_ua
=
392 rk817_chg_cur_from_reg(reg
& RK817_CHRG_CUR_SEL
);
395 * Update max charge voltage. Like the max charge current this value
396 * shouldn't change, but we can report what the PMIC says.
398 regmap_read(charger
->rk808
->regmap
, RK817_PMIC_CHRG_OUT
, ®
);
399 charger
->max_chg_volt_uv
= ((((reg
& RK817_CHRG_VOL_SEL
) >> 4) *
402 /* Check if battery still present. */
403 regmap_read(charger
->rk808
->regmap
, RK817_PMIC_CHRG_STS
, ®
);
404 charger
->battery_present
= (reg
& RK817_BAT_EXS
);
406 /* Get which type of charge we are using (if any). */
407 regmap_read(charger
->rk808
->regmap
, RK817_PMIC_CHRG_STS
, ®
);
408 charger
->charge_status
= (reg
>> 4) & 0x07;
411 * Get charger input voltage. Note that on my example hardware (an
412 * Odroid Go Advance) the voltage of the power connector is measured
413 * on the register labelled USB in the datasheet; I don't know if this
414 * is how it is designed or just a quirk of the implementation. I
415 * believe this will also measure the voltage of the USB output when in
416 * OTG mode, if that is the case we may need to change this in the
417 * future to return 0 if the power supply status is offline (I can't
418 * test this with my current implementation. Also, when the voltage
419 * should be zero sometimes the ADC still shows a single bit (which
420 * would register as 20000uv). When this happens set it to 0.
422 regmap_bulk_read(charger
->rk808
->regmap
, RK817_GAS_GAUGE_USB_VOL_H
,
424 reg
= get_unaligned_be16(bulk_reg
);
426 tmp
= ((charger
->voltage_k
* reg
/ 1000 + charger
->voltage_b
) *
428 charger
->charger_input_volt_avg_uv
= tmp
* 1000;
430 charger
->charger_input_volt_avg_uv
= 0;
433 /* Calibrate battery capacity and soc. */
434 rk817_bat_calib_cap(charger
);
437 static int rk817_bat_get_prop(struct power_supply
*ps
,
438 enum power_supply_property prop
,
439 union power_supply_propval
*val
)
441 struct rk817_charger
*charger
= power_supply_get_drvdata(ps
);
444 case POWER_SUPPLY_PROP_PRESENT
:
445 val
->intval
= charger
->battery_present
;
447 case POWER_SUPPLY_PROP_STATUS
:
448 if (charger
->cur_avg_ua
< 0) {
449 val
->intval
= POWER_SUPPLY_STATUS_DISCHARGING
;
452 switch (charger
->charge_status
) {
454 val
->intval
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
457 * Dead charge is documented, but not explained. I never
458 * observed it but assume it's a pre-charge for a dead
464 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
467 val
->intval
= POWER_SUPPLY_STATUS_FULL
;
470 val
->intval
= POWER_SUPPLY_STATUS_UNKNOWN
;
475 case POWER_SUPPLY_PROP_CHARGE_TYPE
:
476 switch (charger
->charge_status
) {
479 val
->intval
= POWER_SUPPLY_CHARGE_TYPE_NONE
;
482 val
->intval
= POWER_SUPPLY_CHARGE_TYPE_TRICKLE
;
486 val
->intval
= POWER_SUPPLY_CHARGE_TYPE_STANDARD
;
489 val
->intval
= POWER_SUPPLY_CHARGE_TYPE_UNKNOWN
;
493 case POWER_SUPPLY_PROP_CHARGE_FULL
:
494 val
->intval
= charger
->fcc_mah
* 1000;
496 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
:
497 val
->intval
= charger
->bat_charge_full_design_uah
;
499 case POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN
:
502 case POWER_SUPPLY_PROP_CHARGE_NOW
:
503 val
->intval
= charger
->charge_now_uah
;
505 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
:
506 val
->intval
= charger
->bat_voltage_min_design_uv
;
508 case POWER_SUPPLY_PROP_CAPACITY
:
509 /* Add 500 so that values like 99999 are 100% not 99%. */
510 val
->intval
= (charger
->soc
+ 500) / 1000;
511 if (val
->intval
> 100)
516 case POWER_SUPPLY_PROP_VOLTAGE_AVG
:
517 val
->intval
= charger
->volt_avg_uv
;
519 case POWER_SUPPLY_PROP_CURRENT_AVG
:
520 val
->intval
= charger
->cur_avg_ua
;
522 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX
:
523 val
->intval
= charger
->max_chg_cur_ua
;
525 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX
:
526 val
->intval
= charger
->max_chg_volt_uv
;
528 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
:
529 val
->intval
= charger
->bat_voltage_max_design_uv
;
537 static int rk817_chg_get_prop(struct power_supply
*ps
,
538 enum power_supply_property prop
,
539 union power_supply_propval
*val
)
541 struct rk817_charger
*charger
= power_supply_get_drvdata(ps
);
544 case POWER_SUPPLY_PROP_ONLINE
:
545 val
->intval
= charger
->plugged_in
;
547 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
:
548 /* max voltage from datasheet at 5.5v (default 5.0v) */
549 val
->intval
= 5500000;
551 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
:
552 /* min voltage from datasheet at 3.8v (default 5.0v) */
553 val
->intval
= 3800000;
555 case POWER_SUPPLY_PROP_VOLTAGE_AVG
:
556 val
->intval
= charger
->charger_input_volt_avg_uv
;
559 * While it's possible that other implementations could use different
560 * USB types, the current implementation for this PMIC (the Odroid Go
561 * Advance) only uses a dedicated charging port with no rx/tx lines.
563 case POWER_SUPPLY_PROP_USB_TYPE
:
564 val
->intval
= POWER_SUPPLY_USB_TYPE_DCP
;
573 static irqreturn_t
rk817_plug_in_isr(int irq
, void *cg
)
575 struct rk817_charger
*charger
;
577 charger
= (struct rk817_charger
*)cg
;
578 charger
->plugged_in
= 1;
579 power_supply_changed(charger
->chg_ps
);
580 power_supply_changed(charger
->bat_ps
);
581 /* try to recalibrate capacity if we hit full charge. */
582 charger
->soc_cal
= 0;
584 rk817_read_props(charger
);
586 dev_dbg(charger
->dev
, "Power Cord Inserted\n");
591 static irqreturn_t
rk817_plug_out_isr(int irq
, void *cg
)
593 struct rk817_charger
*charger
;
596 charger
= (struct rk817_charger
*)cg
;
597 rk808
= charger
->rk808
;
598 charger
->plugged_in
= 0;
599 power_supply_changed(charger
->bat_ps
);
600 power_supply_changed(charger
->chg_ps
);
603 * For some reason the bits of RK817_PMIC_CHRG_IN reset whenever the
604 * power cord is unplugged. This was not documented in the BSP kernel
605 * or the datasheet and only discovered by trial and error. Set minimum
606 * USB input voltage to 4.5v and enable USB voltage input limit.
608 regmap_write_bits(rk808
->regmap
, RK817_PMIC_CHRG_IN
,
609 RK817_USB_VLIM_SEL
, (0x05 << 4));
610 regmap_write_bits(rk808
->regmap
, RK817_PMIC_CHRG_IN
, RK817_USB_VLIM_EN
,
614 * Set average USB input current limit to 1.5A and enable USB current
617 regmap_write_bits(rk808
->regmap
, RK817_PMIC_CHRG_IN
,
618 RK817_USB_ILIM_SEL
, 0x03);
619 regmap_write_bits(rk808
->regmap
, RK817_PMIC_CHRG_IN
, RK817_USB_ILIM_EN
,
622 rk817_read_props(charger
);
624 dev_dbg(charger
->dev
, "Power Cord Removed\n");
629 static int rk817_bat_set_prop(struct power_supply
*ps
,
630 enum power_supply_property prop
,
631 const union power_supply_propval
*val
)
633 struct rk817_charger
*charger
= power_supply_get_drvdata(ps
);
636 case POWER_SUPPLY_PROP_CHARGE_FULL
:
637 if ((val
->intval
< 500000) ||
638 (val
->intval
> charger
->bat_charge_full_design_uah
))
640 charger
->fcc_mah
= val
->intval
/ 1000;
641 return rk817_bat_calib_cap(charger
);
647 static enum power_supply_property rk817_bat_props
[] = {
648 POWER_SUPPLY_PROP_PRESENT
,
649 POWER_SUPPLY_PROP_STATUS
,
650 POWER_SUPPLY_PROP_CHARGE_TYPE
,
651 POWER_SUPPLY_PROP_CHARGE_FULL
,
652 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
,
653 POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN
,
654 POWER_SUPPLY_PROP_CHARGE_NOW
,
655 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX
,
656 POWER_SUPPLY_PROP_VOLTAGE_AVG
,
657 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX
,
658 POWER_SUPPLY_PROP_CURRENT_AVG
,
659 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
,
660 POWER_SUPPLY_PROP_CAPACITY
,
661 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
,
664 static enum power_supply_property rk817_chg_props
[] = {
665 POWER_SUPPLY_PROP_ONLINE
,
666 POWER_SUPPLY_PROP_USB_TYPE
,
667 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
,
668 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
,
669 POWER_SUPPLY_PROP_VOLTAGE_AVG
,
672 static int rk817_bat_prop_writeable(struct power_supply
*psy
,
673 enum power_supply_property psp
)
676 case POWER_SUPPLY_PROP_CHARGE_FULL
:
683 static const struct power_supply_desc rk817_bat_desc
= {
684 .name
= "rk817-battery",
685 .type
= POWER_SUPPLY_TYPE_BATTERY
,
686 .properties
= rk817_bat_props
,
687 .property_is_writeable
= rk817_bat_prop_writeable
,
688 .num_properties
= ARRAY_SIZE(rk817_bat_props
),
689 .get_property
= rk817_bat_get_prop
,
690 .set_property
= rk817_bat_set_prop
,
693 static const struct power_supply_desc rk817_chg_desc
= {
694 .name
= "rk817-charger",
695 .type
= POWER_SUPPLY_TYPE_USB
,
696 .usb_types
= BIT(POWER_SUPPLY_USB_TYPE_DCP
) |
697 BIT(POWER_SUPPLY_USB_TYPE_UNKNOWN
),
698 .properties
= rk817_chg_props
,
699 .num_properties
= ARRAY_SIZE(rk817_chg_props
),
700 .get_property
= rk817_chg_get_prop
,
703 static int rk817_read_battery_nvram_values(struct rk817_charger
*charger
)
708 /* Read the nvram data for full charge capacity. */
709 ret
= regmap_bulk_read(charger
->rk808
->regmap
,
710 RK817_GAS_GAUGE_DATA3
, bulk_reg
, 3);
713 charger
->fcc_mah
= get_unaligned_le24(bulk_reg
);
716 * Sanity checking for values equal to zero or less than would be
717 * practical for this device (BSP Kernel assumes 500mAH or less) for
718 * practicality purposes. Also check if the value is too large and
721 if ((charger
->fcc_mah
< 500) ||
722 ((charger
->fcc_mah
* 1000) > charger
->bat_charge_full_design_uah
)) {
723 dev_info(charger
->dev
,
724 "Invalid NVRAM max charge, setting to %u uAH\n",
725 charger
->bat_charge_full_design_uah
);
726 charger
->fcc_mah
= charger
->bat_charge_full_design_uah
/ 1000;
730 * Read the nvram for state of charge. Sanity check for values greater
731 * than 100 (10000) or less than 0, because other things (BSP kernels,
732 * U-Boot, or even i2cset) can write to this register. If the value is
733 * off it should get corrected automatically when the voltage drops to
734 * the min (soc is 0) or when the battery is full (soc is 100).
736 ret
= regmap_bulk_read(charger
->rk808
->regmap
,
737 RK817_GAS_GAUGE_BAT_R1
, bulk_reg
, 3);
740 charger
->soc
= get_unaligned_le24(bulk_reg
);
741 if (charger
->soc
> 10000)
742 charger
->soc
= 10000;
743 if (charger
->soc
< 0)
750 rk817_read_or_set_full_charge_on_boot(struct rk817_charger
*charger
,
751 struct power_supply_battery_info
*bat_info
)
753 struct rk808
*rk808
= charger
->rk808
;
755 u32 boot_voltage
, boot_charge_mah
;
756 int ret
, reg
, off_time
, tmp
;
760 * Check if the battery is uninitalized. If it is, the columb counter
761 * needs to be set up.
763 ret
= regmap_read(rk808
->regmap
, RK817_GAS_GAUGE_GG_STS
, ®
);
766 first_boot
= reg
& RK817_BAT_CON
;
768 * If the battery is uninitialized, use the poweron voltage and an ocv
769 * lookup to guess our charge. The number won't be very accurate until
770 * we hit either our minimum voltage (0%) or full charge (100%).
773 regmap_bulk_read(rk808
->regmap
, RK817_GAS_GAUGE_PWRON_VOL_H
,
775 tmp
= get_unaligned_be16(bulk_reg
);
776 boot_voltage
= (charger
->voltage_k
* tmp
) +
777 1000 * charger
->voltage_b
;
779 * Since only implementation has no working thermistor, assume
780 * 20C for OCV lookup. If lookup fails, report error with OCV
783 charger
->soc
= power_supply_batinfo_ocv2cap(bat_info
,
786 if (charger
->soc
< 0)
789 /* Guess that full charge capacity is the design capacity */
790 charger
->fcc_mah
= charger
->bat_charge_full_design_uah
/ 1000;
792 * Set battery as "set up". BSP driver uses this value even
793 * though datasheet claims it's a read-only value.
795 regmap_write_bits(rk808
->regmap
, RK817_GAS_GAUGE_GG_STS
,
797 /* Save nvram values */
798 ret
= rk817_record_battery_nvram_values(charger
);
802 ret
= rk817_read_battery_nvram_values(charger
);
806 regmap_bulk_read(rk808
->regmap
, RK817_GAS_GAUGE_Q_PRES_H3
,
808 tmp
= get_unaligned_be32(bulk_reg
);
811 boot_charge_mah
= ADC_TO_CHARGE_UAH(tmp
,
812 charger
->res_div
) / 1000;
814 * Check if the columb counter has been off for more than 30
815 * minutes as it tends to drift downward. If so, re-init soc
816 * with the boot voltage instead. Note the unit values for the
817 * OFF_CNT register appear to be in decaminutes and stops
818 * counting at 2550 (0xFF) minutes. BSP kernel used OCV, but
819 * for me occasionally that would show invalid values. Boot
820 * voltage is only accurate for me on first poweron (not
821 * reboots), but we shouldn't ever encounter an OFF_CNT more
822 * than 0 on a reboot anyway.
824 regmap_read(rk808
->regmap
, RK817_GAS_GAUGE_OFF_CNT
, &off_time
);
826 regmap_bulk_read(rk808
->regmap
,
827 RK817_GAS_GAUGE_PWRON_VOL_H
,
829 tmp
= get_unaligned_be16(bulk_reg
);
830 boot_voltage
= (charger
->voltage_k
* tmp
) +
831 1000 * charger
->voltage_b
;
833 power_supply_batinfo_ocv2cap(bat_info
,
837 charger
->soc
= (boot_charge_mah
* 1000 * 100 /
843 * Now we have our full charge capacity and soc, init the columb
846 boot_charge_mah
= charger
->soc
* charger
->fcc_mah
/ 100 / 1000;
847 if (boot_charge_mah
> charger
->fcc_mah
)
848 boot_charge_mah
= charger
->fcc_mah
;
849 tmp
= CHARGE_TO_ADC(boot_charge_mah
, charger
->res_div
);
850 put_unaligned_be32(tmp
, bulk_reg
);
851 ret
= regmap_bulk_write(rk808
->regmap
, RK817_GAS_GAUGE_Q_INIT_H3
,
856 /* Set QMAX value to max design capacity. */
857 tmp
= CHARGE_TO_ADC((charger
->bat_charge_full_design_uah
/ 1000),
859 put_unaligned_be32(tmp
, bulk_reg
);
860 ret
= regmap_bulk_write(rk808
->regmap
, RK817_GAS_GAUGE_Q_MAX_H3
,
868 static int rk817_battery_init(struct rk817_charger
*charger
,
869 struct power_supply_battery_info
*bat_info
)
871 struct rk808
*rk808
= charger
->rk808
;
872 u32 tmp
, max_chg_vol_mv
, max_chg_cur_ma
;
873 u8 max_chg_vol_reg
, chg_term_i_reg
;
874 int ret
, chg_term_ma
, max_chg_cur_reg
;
877 /* Get initial plug state */
878 regmap_read(rk808
->regmap
, RK817_SYS_STS
, &tmp
);
879 charger
->plugged_in
= (tmp
& RK817_PLUG_IN_STS
);
882 * Turn on all ADC functions to measure battery, USB, and sys voltage,
883 * as well as batt temp. Note only tested implementation so far does
884 * not use a battery with a thermistor.
886 regmap_write(rk808
->regmap
, RK817_GAS_GAUGE_ADC_CONFIG0
, 0xfc);
889 * Set relax mode voltage sampling interval and ADC offset calibration
890 * interval to 8 minutes to mirror BSP kernel. Set voltage and current
891 * modes to average to mirror BSP kernel.
893 regmap_write(rk808
->regmap
, RK817_GAS_GAUGE_GG_CON
, 0x04);
895 /* Calibrate voltage like the BSP does here. */
896 rk817_bat_calib_vol(charger
);
898 /* Write relax threshold, derived from sleep enter current. */
899 tmp
= CURRENT_TO_ADC(charger
->sleep_enter_current_ua
,
901 put_unaligned_be16(tmp
, bulk_reg
);
902 regmap_bulk_write(rk808
->regmap
, RK817_GAS_GAUGE_RELAX_THRE_H
,
905 /* Write sleep sample current, derived from sleep filter current. */
906 tmp
= CURRENT_TO_ADC(charger
->sleep_filter_current_ua
,
908 put_unaligned_be16(tmp
, bulk_reg
);
909 regmap_bulk_write(rk808
->regmap
, RK817_GAS_GAUGE_SLEEP_CON_SAMP_CUR_H
,
912 /* Restart battery relax voltage */
913 regmap_write_bits(rk808
->regmap
, RK817_GAS_GAUGE_GG_STS
,
914 RK817_RELAX_VOL_UPD
, (0x0 << 2));
917 * Set OCV Threshold Voltage to 127.5mV. This was hard coded like this
920 regmap_write(rk808
->regmap
, RK817_GAS_GAUGE_OCV_THRE_VOL
, 0xff);
923 * Set maximum charging voltage to battery max voltage. Trying to be
924 * incredibly safe with these value, as setting them wrong could
925 * overcharge the battery, which would be very bad.
927 max_chg_vol_mv
= bat_info
->constant_charge_voltage_max_uv
/ 1000;
928 max_chg_cur_ma
= bat_info
->constant_charge_current_max_ua
/ 1000;
930 if (max_chg_vol_mv
< 4100) {
931 return dev_err_probe(charger
->dev
, -EINVAL
,
932 "invalid max charger voltage, value %u unsupported\n",
933 max_chg_vol_mv
* 1000);
935 if (max_chg_vol_mv
> 4450) {
936 dev_info(charger
->dev
,
937 "Setting max charge voltage to 4450000uv\n");
938 max_chg_vol_mv
= 4450;
941 if (max_chg_cur_ma
< 500) {
942 return dev_err_probe(charger
->dev
, -EINVAL
,
943 "invalid max charger current, value %u unsupported\n",
944 max_chg_cur_ma
* 1000);
946 if (max_chg_cur_ma
> 3500)
947 dev_info(charger
->dev
,
948 "Setting max charge current to 3500000ua\n");
951 * Now that the values are sanity checked, if we subtract 4100 from the
952 * max voltage and divide by 50, we conviently get the exact value for
953 * the registers, which are 4.1v, 4.15v, 4.2v, 4.25v, 4.3v, 4.35v,
954 * 4.4v, and 4.45v; these correspond to values 0x00 through 0x07.
956 max_chg_vol_reg
= (max_chg_vol_mv
- 4100) / 50;
958 max_chg_cur_reg
= rk817_chg_cur_to_reg(max_chg_cur_ma
);
960 if (max_chg_vol_reg
< 0 || max_chg_vol_reg
> 7) {
961 return dev_err_probe(charger
->dev
, -EINVAL
,
962 "invalid max charger voltage, value %u unsupported\n",
963 max_chg_vol_mv
* 1000);
965 if (max_chg_cur_reg
< 0 || max_chg_cur_reg
> 7) {
966 return dev_err_probe(charger
->dev
, -EINVAL
,
967 "invalid max charger current, value %u unsupported\n",
968 max_chg_cur_ma
* 1000);
972 * Write the values to the registers, and deliver an emergency warning
973 * in the event they are not written correctly.
975 ret
= regmap_write_bits(rk808
->regmap
, RK817_PMIC_CHRG_OUT
,
976 RK817_CHRG_VOL_SEL
, (max_chg_vol_reg
<< 4));
978 dev_emerg(charger
->dev
,
979 "Danger, unable to set max charger voltage: %u\n",
983 ret
= regmap_write_bits(rk808
->regmap
, RK817_PMIC_CHRG_OUT
,
984 RK817_CHRG_CUR_SEL
, max_chg_cur_reg
);
986 dev_emerg(charger
->dev
,
987 "Danger, unable to set max charger current: %u\n",
991 /* Set charge finishing mode to analog */
992 regmap_write_bits(rk808
->regmap
, RK817_PMIC_CHRG_TERM
,
993 RK817_CHRG_TERM_ANA_DIG
, (0x0 << 2));
996 * Set charge finish current, warn if value not in range and keep
999 chg_term_ma
= bat_info
->charge_term_current_ua
/ 1000;
1000 if (chg_term_ma
< 150 || chg_term_ma
> 400) {
1001 dev_warn(charger
->dev
,
1002 "Invalid charge termination %u, keeping default\n",
1003 chg_term_ma
* 1000);
1008 * Values of 150ma, 200ma, 300ma, and 400ma correspond to 00, 01, 10,
1011 chg_term_i_reg
= (chg_term_ma
- 100) / 100;
1012 regmap_write_bits(rk808
->regmap
, RK817_PMIC_CHRG_TERM
,
1013 RK817_CHRG_TERM_ANA_SEL
, chg_term_i_reg
);
1015 ret
= rk817_read_or_set_full_charge_on_boot(charger
, bat_info
);
1020 * Set minimum USB input voltage to 4.5v and enable USB voltage input
1023 regmap_write_bits(rk808
->regmap
, RK817_PMIC_CHRG_IN
,
1024 RK817_USB_VLIM_SEL
, (0x05 << 4));
1025 regmap_write_bits(rk808
->regmap
, RK817_PMIC_CHRG_IN
, RK817_USB_VLIM_EN
,
1029 * Set average USB input current limit to 1.5A and enable USB current
1032 regmap_write_bits(rk808
->regmap
, RK817_PMIC_CHRG_IN
,
1033 RK817_USB_ILIM_SEL
, 0x03);
1034 regmap_write_bits(rk808
->regmap
, RK817_PMIC_CHRG_IN
, RK817_USB_ILIM_EN
,
1040 static void rk817_charging_monitor(struct work_struct
*work
)
1042 struct rk817_charger
*charger
;
1044 charger
= container_of(work
, struct rk817_charger
, work
.work
);
1046 rk817_read_props(charger
);
1048 /* Run every 8 seconds like the BSP driver did. */
1049 queue_delayed_work(system_wq
, &charger
->work
, msecs_to_jiffies(8000));
1052 static void rk817_cleanup_node(void *data
)
1054 struct device_node
*node
= data
;
1059 static int rk817_charger_probe(struct platform_device
*pdev
)
1061 struct rk808
*rk808
= dev_get_drvdata(pdev
->dev
.parent
);
1062 struct rk817_charger
*charger
;
1063 struct device_node
*node
;
1064 struct power_supply_battery_info
*bat_info
;
1065 struct device
*dev
= &pdev
->dev
;
1066 struct power_supply_config pscfg
= {};
1067 int plugin_irq
, plugout_irq
;
1071 node
= of_get_child_by_name(dev
->parent
->of_node
, "charger");
1075 ret
= devm_add_action_or_reset(&pdev
->dev
, rk817_cleanup_node
, node
);
1079 charger
= devm_kzalloc(&pdev
->dev
, sizeof(*charger
), GFP_KERNEL
);
1083 charger
->rk808
= rk808
;
1085 charger
->dev
= &pdev
->dev
;
1086 platform_set_drvdata(pdev
, charger
);
1088 rk817_bat_calib_vol(charger
);
1090 pscfg
.drv_data
= charger
;
1091 pscfg
.of_node
= node
;
1094 * Get sample resistor value. Note only values of 10000 or 20000
1095 * microohms are allowed. Schematic for my test implementation (an
1096 * Odroid Go Advance) shows a 10 milliohm resistor for reference.
1098 ret
= of_property_read_u32(node
, "rockchip,resistor-sense-micro-ohms",
1101 return dev_err_probe(dev
, ret
,
1102 "Error reading sample resistor value\n");
1105 * Store as a 1 or a 2, since all we really use the value for is as a
1106 * divisor in some calculations.
1108 charger
->res_div
= (of_value
== 20000) ? 2 : 1;
1111 * Get sleep enter current value. Not sure what this value is for
1112 * other than to help calibrate the relax threshold.
1114 ret
= of_property_read_u32(node
,
1115 "rockchip,sleep-enter-current-microamp",
1118 return dev_err_probe(dev
, ret
,
1119 "Error reading sleep enter cur value\n");
1121 charger
->sleep_enter_current_ua
= of_value
;
1123 /* Get sleep filter current value */
1124 ret
= of_property_read_u32(node
,
1125 "rockchip,sleep-filter-current-microamp",
1128 return dev_err_probe(dev
, ret
,
1129 "Error reading sleep filter cur value\n");
1132 charger
->sleep_filter_current_ua
= of_value
;
1134 charger
->bat_ps
= devm_power_supply_register(&pdev
->dev
,
1135 &rk817_bat_desc
, &pscfg
);
1136 if (IS_ERR(charger
->bat_ps
))
1137 return dev_err_probe(dev
, -EINVAL
,
1138 "Battery failed to probe\n");
1140 charger
->chg_ps
= devm_power_supply_register(&pdev
->dev
,
1141 &rk817_chg_desc
, &pscfg
);
1142 if (IS_ERR(charger
->chg_ps
))
1143 return dev_err_probe(dev
, -EINVAL
,
1144 "Charger failed to probe\n");
1146 ret
= power_supply_get_battery_info(charger
->bat_ps
,
1149 return dev_err_probe(dev
, ret
,
1150 "Unable to get battery info\n");
1153 if ((bat_info
->charge_full_design_uah
<= 0) ||
1154 (bat_info
->voltage_min_design_uv
<= 0) ||
1155 (bat_info
->voltage_max_design_uv
<= 0) ||
1156 (bat_info
->constant_charge_voltage_max_uv
<= 0) ||
1157 (bat_info
->constant_charge_current_max_ua
<= 0) ||
1158 (bat_info
->charge_term_current_ua
<= 0)) {
1159 return dev_err_probe(dev
, -EINVAL
,
1160 "Required bat info missing or invalid\n");
1163 charger
->bat_charge_full_design_uah
= bat_info
->charge_full_design_uah
;
1164 charger
->bat_voltage_min_design_uv
= bat_info
->voltage_min_design_uv
;
1165 charger
->bat_voltage_max_design_uv
= bat_info
->voltage_max_design_uv
;
1168 * Has to run after power_supply_get_battery_info as it depends on some
1169 * values discovered from that routine.
1171 ret
= rk817_battery_init(charger
, bat_info
);
1175 power_supply_put_battery_info(charger
->bat_ps
, bat_info
);
1177 plugin_irq
= platform_get_irq(pdev
, 0);
1181 plugout_irq
= platform_get_irq(pdev
, 1);
1182 if (plugout_irq
< 0)
1185 ret
= devm_request_threaded_irq(charger
->dev
, plugin_irq
, NULL
,
1187 IRQF_TRIGGER_RISING
| IRQF_ONESHOT
,
1188 "rk817_plug_in", charger
);
1190 return dev_err_probe(&pdev
->dev
, ret
,
1191 "plug_in_irq request failed!\n");
1194 ret
= devm_request_threaded_irq(charger
->dev
, plugout_irq
, NULL
,
1196 IRQF_TRIGGER_RISING
| IRQF_ONESHOT
,
1197 "rk817_plug_out", charger
);
1199 return dev_err_probe(&pdev
->dev
, ret
,
1200 "plug_out_irq request failed!\n");
1203 ret
= devm_delayed_work_autocancel(&pdev
->dev
, &charger
->work
,
1204 rk817_charging_monitor
);
1208 /* Force the first update immediately. */
1209 mod_delayed_work(system_wq
, &charger
->work
, 0);
1214 static int __maybe_unused
rk817_suspend(struct device
*dev
)
1216 struct rk817_charger
*charger
= dev_get_drvdata(dev
);
1218 cancel_delayed_work_sync(&charger
->work
);
1223 static int __maybe_unused
rk817_resume(struct device
*dev
)
1226 struct rk817_charger
*charger
= dev_get_drvdata(dev
);
1228 /* force an immediate update */
1229 mod_delayed_work(system_wq
, &charger
->work
, 0);
1234 static SIMPLE_DEV_PM_OPS(rk817_charger_pm
, rk817_suspend
, rk817_resume
);
1236 static struct platform_driver rk817_charger_driver
= {
1237 .probe
= rk817_charger_probe
,
1239 .name
= "rk817-charger",
1240 .pm
= &rk817_charger_pm
,
1243 module_platform_driver(rk817_charger_driver
);
1245 MODULE_DESCRIPTION("Battery power supply driver for RK817 PMIC");
1246 MODULE_AUTHOR("Maya Matuszczyk <maccraft123mc@gmail.com>");
1247 MODULE_AUTHOR("Chris Morgan <macromorgan@hotmail.com>");
1248 MODULE_LICENSE("GPL");
1249 MODULE_ALIAS("platform:rk817-charger");