Lines Matching full:charger

3  * Charger Driver for Rockchip rk817
170 static void rk817_bat_calib_vol(struct rk817_charger *charger) in rk817_bat_calib_vol() argument
177 regmap_bulk_read(charger->rk808->regmap, RK817_GAS_GAUGE_VCALIB0_H, in rk817_bat_calib_vol()
181 regmap_bulk_read(charger->rk808->regmap, RK817_GAS_GAUGE_VCALIB1_H, in rk817_bat_calib_vol()
186 charger->voltage_k = (4025 - 2300) * 1000 / in rk817_bat_calib_vol()
188 charger->voltage_b = 4025 - (charger->voltage_k * vcalib1) / 1000; in rk817_bat_calib_vol()
191 static void rk817_bat_calib_cur(struct rk817_charger *charger) in rk817_bat_calib_cur() argument
196 regmap_bulk_read(charger->rk808->regmap, RK817_GAS_GAUGE_IOFFSET_H, in rk817_bat_calib_cur()
198 regmap_bulk_write(charger->rk808->regmap, RK817_GAS_GAUGE_CAL_OFFSET_H, in rk817_bat_calib_cur()
206 static int rk817_record_battery_nvram_values(struct rk817_charger *charger) in rk817_record_battery_nvram_values() argument
215 put_unaligned_le24(charger->soc, bulk_reg); in rk817_record_battery_nvram_values()
216 ret = regmap_bulk_write(charger->rk808->regmap, RK817_GAS_GAUGE_BAT_R1, in rk817_record_battery_nvram_values()
224 rsoc = (charger->soc * charger->fcc_mah) / 100000; in rk817_record_battery_nvram_values()
226 ret = regmap_bulk_write(charger->rk808->regmap, RK817_GAS_GAUGE_DATA0, in rk817_record_battery_nvram_values()
231 put_unaligned_le24(charger->fcc_mah, bulk_reg); in rk817_record_battery_nvram_values()
232 ret = regmap_bulk_write(charger->rk808->regmap, RK817_GAS_GAUGE_DATA3, in rk817_record_battery_nvram_values()
240 static int rk817_bat_calib_cap(struct rk817_charger *charger) in rk817_bat_calib_cap() argument
242 struct rk808 *rk808 = charger->rk808; in rk817_bat_calib_cap()
248 if (charger->charge_status == CHARGE_FINISH && (!charger->soc_cal)) { in rk817_bat_calib_cap()
260 charger->soc = 100000; in rk817_bat_calib_cap()
261 charge_now_adc = CHARGE_TO_ADC(charger->fcc_mah, in rk817_bat_calib_cap()
262 charger->res_div); in rk817_bat_calib_cap()
267 charger->soc_cal = 1; in rk817_bat_calib_cap()
268 dev_dbg(charger->dev, in rk817_bat_calib_cap()
270 charger->soc, charger->fcc_mah * 1000); in rk817_bat_calib_cap()
277 if (charger->charge_status == CHARGE_FINISH && charger->soc_cal) { in rk817_bat_calib_cap()
284 charger->res_div); in rk817_bat_calib_cap()
289 if (charge_now / 1000 > charger->fcc_mah) { in rk817_bat_calib_cap()
290 dev_dbg(charger->dev, in rk817_bat_calib_cap()
298 charge_now_adc = CHARGE_TO_ADC(charger->fcc_mah, in rk817_bat_calib_cap()
299 charger->res_div); in rk817_bat_calib_cap()
315 regmap_bulk_read(charger->rk808->regmap, RK817_GAS_GAUGE_BAT_VOL_H, in rk817_bat_calib_cap()
318 volt_avg = (charger->voltage_k * tmp) + 1000 * charger->voltage_b; in rk817_bat_calib_cap()
319 if (volt_avg <= charger->bat_voltage_min_design_uv && in rk817_bat_calib_cap()
320 charger->soc_cal) { in rk817_bat_calib_cap()
325 charger->res_div); in rk817_bat_calib_cap()
331 charger->fcc_mah = charger->fcc_mah - (charge_now / 1000); in rk817_bat_calib_cap()
333 dev_dbg(charger->dev, in rk817_bat_calib_cap()
335 charger->fcc_mah * 1000); in rk817_bat_calib_cap()
341 if (volt_avg <= charger->bat_voltage_min_design_uv) { in rk817_bat_calib_cap()
342 charger->soc = 0; in rk817_bat_calib_cap()
343 charge_now_adc = CHARGE_TO_ADC(0, charger->res_div); in rk817_bat_calib_cap()
347 dev_warn(charger->dev, in rk817_bat_calib_cap()
349 volt_avg, charger->bat_voltage_min_design_uv); in rk817_bat_calib_cap()
352 rk817_record_battery_nvram_values(charger); in rk817_bat_calib_cap()
357 static void rk817_read_props(struct rk817_charger *charger) in rk817_read_props() argument
368 regmap_read(charger->rk808->regmap, RK817_GAS_GAUGE_ADC_CONFIG1, &reg); in rk817_read_props()
370 rk817_bat_calib_cur(charger); in rk817_read_props()
371 rk817_bat_calib_vol(charger); in rk817_read_props()
372 regmap_write_bits(charger->rk808->regmap, in rk817_read_props()
379 regmap_bulk_read(charger->rk808->regmap, RK817_GAS_GAUGE_Q_PRES_H3, in rk817_read_props()
382 charger->charge_now_uah = ADC_TO_CHARGE_UAH(tmp, charger->res_div); in rk817_read_props()
383 if (charger->charge_now_uah < 0) in rk817_read_props()
384 charger->charge_now_uah = 0; in rk817_read_props()
385 if (charger->charge_now_uah > charger->fcc_mah * 1000) in rk817_read_props()
386 charger->charge_now_uah = charger->fcc_mah * 1000; in rk817_read_props()
389 charger->soc = charger->charge_now_uah * 100 / charger->fcc_mah; in rk817_read_props()
392 regmap_bulk_read(charger->rk808->regmap, RK817_GAS_GAUGE_BAT_VOL_H, in rk817_read_props()
395 charger->volt_avg_uv = (charger->voltage_k * tmp) + 1000 * in rk817_read_props()
396 charger->voltage_b; in rk817_read_props()
402 regmap_bulk_read(charger->rk808->regmap, RK817_GAS_GAUGE_BAT_CUR_H, in rk817_read_props()
405 charger->cur_avg_ua = ADC_TO_CURRENT(tmp, charger->res_div); in rk817_read_props()
412 regmap_read(charger->rk808->regmap, RK817_PMIC_CHRG_OUT, &reg); in rk817_read_props()
413 charger->max_chg_cur_ua = in rk817_read_props()
420 regmap_read(charger->rk808->regmap, RK817_PMIC_CHRG_OUT, &reg); in rk817_read_props()
421 charger->max_chg_volt_uv = ((((reg & RK817_CHRG_VOL_SEL) >> 4) * in rk817_read_props()
425 regmap_read(charger->rk808->regmap, RK817_PMIC_CHRG_STS, &reg); in rk817_read_props()
426 charger->battery_present = (reg & RK817_BAT_EXS); in rk817_read_props()
429 regmap_read(charger->rk808->regmap, RK817_PMIC_CHRG_STS, &reg); in rk817_read_props()
430 charger->charge_status = (reg >> 4) & 0x07; in rk817_read_props()
433 * Get charger input voltage. Note that on my example hardware (an in rk817_read_props()
444 regmap_bulk_read(charger->rk808->regmap, RK817_GAS_GAUGE_USB_VOL_H, in rk817_read_props()
448 tmp = ((charger->voltage_k * reg / 1000 + charger->voltage_b) * in rk817_read_props()
450 charger->charger_input_volt_avg_uv = tmp * 1000; in rk817_read_props()
452 charger->charger_input_volt_avg_uv = 0; in rk817_read_props()
456 rk817_bat_calib_cap(charger); in rk817_read_props()
463 struct rk817_charger *charger = power_supply_get_drvdata(ps); in rk817_bat_get_prop() local
467 val->intval = charger->battery_present; in rk817_bat_get_prop()
470 if (charger->cur_avg_ua < 0) { in rk817_bat_get_prop()
474 switch (charger->charge_status) { in rk817_bat_get_prop()
498 switch (charger->charge_status) { in rk817_bat_get_prop()
516 val->intval = charger->fcc_mah * 1000; in rk817_bat_get_prop()
519 val->intval = charger->bat_charge_full_design_uah; in rk817_bat_get_prop()
525 val->intval = charger->charge_now_uah; in rk817_bat_get_prop()
528 val->intval = charger->bat_voltage_min_design_uv; in rk817_bat_get_prop()
532 val->intval = (charger->soc + 500) / 1000; in rk817_bat_get_prop()
539 val->intval = charger->volt_avg_uv; in rk817_bat_get_prop()
542 val->intval = charger->cur_avg_ua; in rk817_bat_get_prop()
545 val->intval = charger->max_chg_cur_ua; in rk817_bat_get_prop()
548 val->intval = charger->max_chg_volt_uv; in rk817_bat_get_prop()
551 val->intval = charger->bat_voltage_max_design_uv; in rk817_bat_get_prop()
563 struct rk817_charger *charger = power_supply_get_drvdata(ps); in rk817_chg_get_prop() local
567 val->intval = charger->plugged_in; in rk817_chg_get_prop()
578 val->intval = charger->charger_input_volt_avg_uv; in rk817_chg_get_prop()
597 struct rk817_charger *charger; in rk817_plug_in_isr() local
599 charger = (struct rk817_charger *)cg; in rk817_plug_in_isr()
600 charger->plugged_in = 1; in rk817_plug_in_isr()
601 power_supply_changed(charger->chg_ps); in rk817_plug_in_isr()
602 power_supply_changed(charger->bat_ps); in rk817_plug_in_isr()
604 charger->soc_cal = 0; in rk817_plug_in_isr()
606 rk817_read_props(charger); in rk817_plug_in_isr()
608 dev_dbg(charger->dev, "Power Cord Inserted\n"); in rk817_plug_in_isr()
615 struct rk817_charger *charger; in rk817_plug_out_isr() local
618 charger = (struct rk817_charger *)cg; in rk817_plug_out_isr()
619 rk808 = charger->rk808; in rk817_plug_out_isr()
620 charger->plugged_in = 0; in rk817_plug_out_isr()
621 power_supply_changed(charger->bat_ps); in rk817_plug_out_isr()
622 power_supply_changed(charger->chg_ps); in rk817_plug_out_isr()
644 rk817_read_props(charger); in rk817_plug_out_isr()
646 dev_dbg(charger->dev, "Power Cord Removed\n"); in rk817_plug_out_isr()
690 .name = "rk817-charger",
699 static int rk817_read_battery_nvram_values(struct rk817_charger *charger) in rk817_read_battery_nvram_values() argument
705 ret = regmap_bulk_read(charger->rk808->regmap, in rk817_read_battery_nvram_values()
709 charger->fcc_mah = get_unaligned_le24(bulk_reg); in rk817_read_battery_nvram_values()
717 if ((charger->fcc_mah < 500) || in rk817_read_battery_nvram_values()
718 ((charger->fcc_mah * 1000) > charger->bat_charge_full_design_uah)) { in rk817_read_battery_nvram_values()
719 dev_info(charger->dev, in rk817_read_battery_nvram_values()
721 charger->bat_charge_full_design_uah); in rk817_read_battery_nvram_values()
722 charger->fcc_mah = charger->bat_charge_full_design_uah / 1000; in rk817_read_battery_nvram_values()
732 ret = regmap_bulk_read(charger->rk808->regmap, in rk817_read_battery_nvram_values()
736 charger->soc = get_unaligned_le24(bulk_reg); in rk817_read_battery_nvram_values()
737 if (charger->soc > 10000) in rk817_read_battery_nvram_values()
738 charger->soc = 10000; in rk817_read_battery_nvram_values()
739 if (charger->soc < 0) in rk817_read_battery_nvram_values()
740 charger->soc = 0; in rk817_read_battery_nvram_values()
746 rk817_read_or_set_full_charge_on_boot(struct rk817_charger *charger, in rk817_read_or_set_full_charge_on_boot() argument
749 struct rk808 *rk808 = charger->rk808; in rk817_read_or_set_full_charge_on_boot()
772 boot_voltage = (charger->voltage_k * tmp) + in rk817_read_or_set_full_charge_on_boot()
773 1000 * charger->voltage_b; in rk817_read_or_set_full_charge_on_boot()
779 charger->soc = power_supply_batinfo_ocv2cap(bat_info, in rk817_read_or_set_full_charge_on_boot()
782 if (charger->soc < 0) in rk817_read_or_set_full_charge_on_boot()
783 charger->soc = 0; in rk817_read_or_set_full_charge_on_boot()
786 charger->fcc_mah = charger->bat_charge_full_design_uah / 1000; in rk817_read_or_set_full_charge_on_boot()
794 ret = rk817_record_battery_nvram_values(charger); in rk817_read_or_set_full_charge_on_boot()
798 ret = rk817_read_battery_nvram_values(charger); in rk817_read_or_set_full_charge_on_boot()
808 charger->res_div) / 1000; in rk817_read_or_set_full_charge_on_boot()
826 boot_voltage = (charger->voltage_k * tmp) + in rk817_read_or_set_full_charge_on_boot()
827 1000 * charger->voltage_b; in rk817_read_or_set_full_charge_on_boot()
828 charger->soc = in rk817_read_or_set_full_charge_on_boot()
833 charger->soc = (boot_charge_mah * 1000 * 100 / in rk817_read_or_set_full_charge_on_boot()
834 charger->fcc_mah); in rk817_read_or_set_full_charge_on_boot()
842 boot_charge_mah = charger->soc * charger->fcc_mah / 100 / 1000; in rk817_read_or_set_full_charge_on_boot()
843 if (boot_charge_mah > charger->fcc_mah) in rk817_read_or_set_full_charge_on_boot()
844 boot_charge_mah = charger->fcc_mah; in rk817_read_or_set_full_charge_on_boot()
845 tmp = CHARGE_TO_ADC(boot_charge_mah, charger->res_div); in rk817_read_or_set_full_charge_on_boot()
853 tmp = CHARGE_TO_ADC((charger->bat_charge_full_design_uah / 1000), in rk817_read_or_set_full_charge_on_boot()
854 charger->res_div); in rk817_read_or_set_full_charge_on_boot()
864 static int rk817_battery_init(struct rk817_charger *charger, in rk817_battery_init() argument
867 struct rk808 *rk808 = charger->rk808; in rk817_battery_init()
875 charger->plugged_in = (tmp & RK817_PLUG_IN_STS); in rk817_battery_init()
892 rk817_bat_calib_vol(charger); in rk817_battery_init()
895 tmp = CURRENT_TO_ADC(charger->sleep_enter_current_ua, in rk817_battery_init()
896 charger->res_div); in rk817_battery_init()
902 tmp = CURRENT_TO_ADC(charger->sleep_filter_current_ua, in rk817_battery_init()
903 charger->res_div); in rk817_battery_init()
927 return dev_err_probe(charger->dev, -EINVAL, in rk817_battery_init()
928 "invalid max charger voltage, value %u unsupported\n", in rk817_battery_init()
932 dev_info(charger->dev, in rk817_battery_init()
938 return dev_err_probe(charger->dev, -EINVAL, in rk817_battery_init()
939 "invalid max charger current, value %u unsupported\n", in rk817_battery_init()
943 dev_info(charger->dev, in rk817_battery_init()
957 return dev_err_probe(charger->dev, -EINVAL, in rk817_battery_init()
958 "invalid max charger voltage, value %u unsupported\n", in rk817_battery_init()
962 return dev_err_probe(charger->dev, -EINVAL, in rk817_battery_init()
963 "invalid max charger current, value %u unsupported\n", in rk817_battery_init()
974 dev_emerg(charger->dev, in rk817_battery_init()
975 "Danger, unable to set max charger voltage: %u\n", in rk817_battery_init()
982 dev_emerg(charger->dev, in rk817_battery_init()
983 "Danger, unable to set max charger current: %u\n", in rk817_battery_init()
997 dev_warn(charger->dev, in rk817_battery_init()
1011 ret = rk817_read_or_set_full_charge_on_boot(charger, bat_info); in rk817_battery_init()
1038 struct rk817_charger *charger; in rk817_charging_monitor() local
1040 charger = container_of(work, struct rk817_charger, work.work); in rk817_charging_monitor()
1042 rk817_read_props(charger); in rk817_charging_monitor()
1045 queue_delayed_work(system_wq, &charger->work, msecs_to_jiffies(8000)); in rk817_charging_monitor()
1058 struct rk817_charger *charger; in rk817_charger_probe() local
1067 node = of_get_child_by_name(dev->parent->of_node, "charger"); in rk817_charger_probe()
1075 charger = devm_kzalloc(&pdev->dev, sizeof(*charger), GFP_KERNEL); in rk817_charger_probe()
1076 if (!charger) in rk817_charger_probe()
1079 charger->rk808 = rk808; in rk817_charger_probe()
1081 charger->dev = &pdev->dev; in rk817_charger_probe()
1082 platform_set_drvdata(pdev, charger); in rk817_charger_probe()
1084 rk817_bat_calib_vol(charger); in rk817_charger_probe()
1086 pscfg.drv_data = charger; in rk817_charger_probe()
1104 charger->res_div = (of_value == 20000) ? 2 : 1; in rk817_charger_probe()
1117 charger->sleep_enter_current_ua = of_value; in rk817_charger_probe()
1128 charger->sleep_filter_current_ua = of_value; in rk817_charger_probe()
1130 charger->bat_ps = devm_power_supply_register(&pdev->dev, in rk817_charger_probe()
1132 if (IS_ERR(charger->bat_ps)) in rk817_charger_probe()
1136 charger->chg_ps = devm_power_supply_register(&pdev->dev, in rk817_charger_probe()
1138 if (IS_ERR(charger->chg_ps)) in rk817_charger_probe()
1140 "Charger failed to probe\n"); in rk817_charger_probe()
1142 ret = power_supply_get_battery_info(charger->bat_ps, in rk817_charger_probe()
1159 charger->bat_charge_full_design_uah = bat_info->charge_full_design_uah; in rk817_charger_probe()
1160 charger->bat_voltage_min_design_uv = bat_info->voltage_min_design_uv; in rk817_charger_probe()
1161 charger->bat_voltage_max_design_uv = bat_info->voltage_max_design_uv; in rk817_charger_probe()
1167 ret = rk817_battery_init(charger, bat_info); in rk817_charger_probe()
1171 power_supply_put_battery_info(charger->bat_ps, bat_info); in rk817_charger_probe()
1181 ret = devm_request_threaded_irq(charger->dev, plugin_irq, NULL, in rk817_charger_probe()
1184 "rk817_plug_in", charger); in rk817_charger_probe()
1190 ret = devm_request_threaded_irq(charger->dev, plugout_irq, NULL, in rk817_charger_probe()
1193 "rk817_plug_out", charger); in rk817_charger_probe()
1199 ret = devm_delayed_work_autocancel(&pdev->dev, &charger->work, in rk817_charger_probe()
1205 mod_delayed_work(system_wq, &charger->work, 0); in rk817_charger_probe()
1214 .name = "rk817-charger",
1223 MODULE_ALIAS("platform:rk817-charger");