Lines Matching +full:chg +full:- +full:int

1 // SPDX-License-Identifier: GPL-2.0+
3 // max77693_charger.c - Battery charger driver for the Maxim 77693
13 #include <linux/mfd/max77693-common.h>
14 #include <linux/mfd/max77693-private.h>
16 #define MAX77693_CHARGER_NAME "max77693-charger"
32 static int max77693_get_charger_state(struct regmap *regmap, int *val) in max77693_get_charger_state()
34 int ret; in max77693_get_charger_state()
35 unsigned int data; in max77693_get_charger_state()
73 static int max77693_get_charge_type(struct regmap *regmap, int *val) in max77693_get_charge_type()
75 int ret; in max77693_get_charge_type()
76 unsigned int data; in max77693_get_charge_type()
88 * Top-off: trickle or fast? In top-off the current varies between in max77693_get_charge_type()
118 * - POWER_SUPPLY_HEALTH_DEAD
119 * - POWER_SUPPLY_HEALTH_GOOD
120 * - POWER_SUPPLY_HEALTH_OVERVOLTAGE
121 * - POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE
122 * - POWER_SUPPLY_HEALTH_UNKNOWN
123 * - POWER_SUPPLY_HEALTH_UNSPEC_FAILURE
125 static int max77693_get_battery_health(struct regmap *regmap, int *val) in max77693_get_battery_health()
127 int ret; in max77693_get_battery_health()
128 unsigned int data; in max77693_get_battery_health()
168 static int max77693_get_present(struct regmap *regmap, int *val) in max77693_get_present()
170 unsigned int data; in max77693_get_present()
171 int ret; in max77693_get_present()
186 static int max77693_get_online(struct regmap *regmap, int *val) in max77693_get_online()
188 unsigned int data; in max77693_get_online()
189 int ret; in max77693_get_online()
202 * - CHGIN limit, which limits the input current from the external charger;
203 * - Fast charge current limit, which limits the current going to the battery.
206 static int max77693_get_input_current_limit(struct regmap *regmap, int *val) in max77693_get_input_current_limit()
208 unsigned int data; in max77693_get_input_current_limit()
209 int ret; in max77693_get_input_current_limit()
227 static int max77693_get_fast_charge_current(struct regmap *regmap, int *val) in max77693_get_fast_charge_current()
229 unsigned int data; in max77693_get_fast_charge_current()
230 int ret; in max77693_get_fast_charge_current()
256 static int max77693_charger_get_property(struct power_supply *psy, in max77693_charger_get_property()
260 struct max77693_charger *chg = power_supply_get_drvdata(psy); in max77693_charger_get_property() local
261 struct regmap *regmap = chg->max77693->regmap; in max77693_charger_get_property()
262 int ret = 0; in max77693_charger_get_property()
266 ret = max77693_get_charger_state(regmap, &val->intval); in max77693_charger_get_property()
269 ret = max77693_get_charge_type(regmap, &val->intval); in max77693_charger_get_property()
272 ret = max77693_get_battery_health(regmap, &val->intval); in max77693_charger_get_property()
275 ret = max77693_get_present(regmap, &val->intval); in max77693_charger_get_property()
278 ret = max77693_get_online(regmap, &val->intval); in max77693_charger_get_property()
281 ret = max77693_get_input_current_limit(regmap, &val->intval); in max77693_charger_get_property()
284 ret = max77693_get_fast_charge_current(regmap, &val->intval); in max77693_charger_get_property()
287 val->strval = max77693_charger_model; in max77693_charger_get_property()
290 val->strval = max77693_charger_manufacturer; in max77693_charger_get_property()
293 return -EINVAL; in max77693_charger_get_property()
309 int (*fn)(struct max77693_charger *, unsigned long)) in device_attr_store()
311 struct max77693_charger *chg = dev_get_drvdata(dev); in device_attr_store() local
313 int ret; in device_attr_store()
319 ret = fn(chg, val); in device_attr_store()
329 struct max77693_charger *chg = dev_get_drvdata(dev); in fast_charge_timer_show() local
330 unsigned int data, val; in fast_charge_timer_show()
331 int ret; in fast_charge_timer_show()
333 ret = regmap_read(chg->max77693->regmap, MAX77693_CHG_REG_CHG_CNFG_01, in fast_charge_timer_show()
343 val = 4 + (data - 1) * 2; in fast_charge_timer_show()
354 static int max77693_set_fast_charge_timer(struct max77693_charger *chg, in max77693_set_fast_charge_timer() argument
357 unsigned int data; in max77693_set_fast_charge_timer()
360 * 0x00 - disable in max77693_set_fast_charge_timer()
361 * 0x01 - 4h in max77693_set_fast_charge_timer()
362 * 0x02 - 6h in max77693_set_fast_charge_timer()
364 * 0x07 - 16h in max77693_set_fast_charge_timer()
369 data = (hours - 4) / 2 + 1; in max77693_set_fast_charge_timer()
376 return -EINVAL; in max77693_set_fast_charge_timer()
380 return regmap_update_bits(chg->max77693->regmap, in max77693_set_fast_charge_timer()
395 struct max77693_charger *chg = dev_get_drvdata(dev); in top_off_threshold_current_show() local
396 unsigned int data, val; in top_off_threshold_current_show()
397 int ret; in top_off_threshold_current_show()
399 ret = regmap_read(chg->max77693->regmap, MAX77693_CHG_REG_CHG_CNFG_03, in top_off_threshold_current_show()
415 static int max77693_set_top_off_threshold_current(struct max77693_charger *chg, in max77693_set_top_off_threshold_current() argument
418 unsigned int data; in max77693_set_top_off_threshold_current()
421 return -EINVAL; in max77693_set_top_off_threshold_current()
424 data = (uamp - 100000) / 25000; in max77693_set_top_off_threshold_current()
431 return regmap_update_bits(chg->max77693->regmap, in max77693_set_top_off_threshold_current()
446 struct max77693_charger *chg = dev_get_drvdata(dev); in top_off_timer_show() local
447 unsigned int data, val; in top_off_timer_show()
448 int ret; in top_off_timer_show()
450 ret = regmap_read(chg->max77693->regmap, MAX77693_CHG_REG_CHG_CNFG_03, in top_off_timer_show()
463 static int max77693_set_top_off_timer(struct max77693_charger *chg, in max77693_set_top_off_timer() argument
466 unsigned int data; in max77693_set_top_off_timer()
469 return -EINVAL; in max77693_set_top_off_timer()
474 return regmap_update_bits(chg->max77693->regmap, in max77693_set_top_off_timer()
490 static int max77693_set_constant_volt(struct max77693_charger *chg, in max77693_set_constant_volt() argument
491 unsigned int uvolt) in max77693_set_constant_volt()
493 unsigned int data; in max77693_set_constant_volt()
496 * 0x00 - 3.650 V in max77693_set_constant_volt()
497 * 0x01 - 3.675 V in max77693_set_constant_volt()
499 * 0x1b - 4.325 V in max77693_set_constant_volt()
500 * 0x1c - 4.340 V in max77693_set_constant_volt()
501 * 0x1d - 4.350 V in max77693_set_constant_volt()
502 * 0x1e - 4.375 V in max77693_set_constant_volt()
503 * 0x1f - 4.400 V in max77693_set_constant_volt()
506 data = (uvolt - 3650000) / 25000; in max77693_set_constant_volt()
510 data = 0x1d + (uvolt - 4350000) / 25000; in max77693_set_constant_volt()
512 dev_err(chg->dev, "Wrong value for charging constant voltage\n"); in max77693_set_constant_volt()
513 return -EINVAL; in max77693_set_constant_volt()
518 dev_dbg(chg->dev, "Charging constant voltage: %u (0x%x)\n", uvolt, in max77693_set_constant_volt()
521 return regmap_update_bits(chg->max77693->regmap, in max77693_set_constant_volt()
526 static int max77693_set_min_system_volt(struct max77693_charger *chg, in max77693_set_min_system_volt() argument
527 unsigned int uvolt) in max77693_set_min_system_volt()
529 unsigned int data; in max77693_set_min_system_volt()
532 dev_err(chg->dev, "Wrong value for minimum system regulation voltage\n"); in max77693_set_min_system_volt()
533 return -EINVAL; in max77693_set_min_system_volt()
536 data = (uvolt - 3000000) / 100000; in max77693_set_min_system_volt()
540 dev_dbg(chg->dev, "Minimum system regulation voltage: %u (0x%x)\n", in max77693_set_min_system_volt()
543 return regmap_update_bits(chg->max77693->regmap, in max77693_set_min_system_volt()
548 static int max77693_set_thermal_regulation_temp(struct max77693_charger *chg, in max77693_set_thermal_regulation_temp() argument
549 unsigned int cels) in max77693_set_thermal_regulation_temp()
551 unsigned int data; in max77693_set_thermal_regulation_temp()
558 data = (cels - 70) / 15; in max77693_set_thermal_regulation_temp()
561 dev_err(chg->dev, "Wrong value for thermal regulation loop temperature\n"); in max77693_set_thermal_regulation_temp()
562 return -EINVAL; in max77693_set_thermal_regulation_temp()
567 dev_dbg(chg->dev, "Thermal regulation loop temperature: %u (0x%x)\n", in max77693_set_thermal_regulation_temp()
570 return regmap_update_bits(chg->max77693->regmap, in max77693_set_thermal_regulation_temp()
575 static int max77693_set_batttery_overcurrent(struct max77693_charger *chg, in max77693_set_batttery_overcurrent() argument
576 unsigned int uamp) in max77693_set_batttery_overcurrent()
578 unsigned int data; in max77693_set_batttery_overcurrent()
581 dev_err(chg->dev, "Wrong value for battery overcurrent\n"); in max77693_set_batttery_overcurrent()
582 return -EINVAL; in max77693_set_batttery_overcurrent()
586 data = ((uamp - 2000000) / 250000) + 1; in max77693_set_batttery_overcurrent()
592 dev_dbg(chg->dev, "Battery overcurrent: %u (0x%x)\n", uamp, data); in max77693_set_batttery_overcurrent()
594 return regmap_update_bits(chg->max77693->regmap, in max77693_set_batttery_overcurrent()
599 static int max77693_set_charge_input_threshold_volt(struct max77693_charger *chg, in max77693_set_charge_input_threshold_volt() argument
600 unsigned int uvolt) in max77693_set_charge_input_threshold_volt()
602 unsigned int data; in max77693_set_charge_input_threshold_volt()
611 data = ((uvolt - 4700000) / 100000) + 1; in max77693_set_charge_input_threshold_volt()
614 dev_err(chg->dev, "Wrong value for charge input voltage regulation threshold\n"); in max77693_set_charge_input_threshold_volt()
615 return -EINVAL; in max77693_set_charge_input_threshold_volt()
620 dev_dbg(chg->dev, "Charge input voltage regulation threshold: %u (0x%x)\n", in max77693_set_charge_input_threshold_volt()
623 return regmap_update_bits(chg->max77693->regmap, in max77693_set_charge_input_threshold_volt()
631 static int max77693_reg_init(struct max77693_charger *chg) in max77693_reg_init() argument
633 int ret; in max77693_reg_init()
634 unsigned int data; in max77693_reg_init()
638 ret = regmap_update_bits(chg->max77693->regmap, in max77693_reg_init()
642 dev_err(chg->dev, "Error unlocking registers: %d\n", ret); in max77693_reg_init()
646 ret = max77693_set_fast_charge_timer(chg, DEFAULT_FAST_CHARGE_TIMER); in max77693_reg_init()
650 ret = max77693_set_top_off_threshold_current(chg, in max77693_reg_init()
655 ret = max77693_set_top_off_timer(chg, DEFAULT_TOP_OFF_TIMER); in max77693_reg_init()
659 ret = max77693_set_constant_volt(chg, chg->constant_volt); in max77693_reg_init()
663 ret = max77693_set_min_system_volt(chg, chg->min_system_volt); in max77693_reg_init()
667 ret = max77693_set_thermal_regulation_temp(chg, in max77693_reg_init()
668 chg->thermal_regulation_temp); in max77693_reg_init()
672 ret = max77693_set_batttery_overcurrent(chg, chg->batttery_overcurrent); in max77693_reg_init()
676 return max77693_set_charge_input_threshold_volt(chg, in max77693_reg_init()
677 chg->charge_input_threshold_volt); in max77693_reg_init()
681 static int max77693_dt_init(struct device *dev, struct max77693_charger *chg) in max77693_dt_init() argument
683 struct device_node *np = dev->of_node; in max77693_dt_init()
687 return -EINVAL; in max77693_dt_init()
690 if (of_property_read_u32(np, "maxim,constant-microvolt", in max77693_dt_init()
691 &chg->constant_volt)) in max77693_dt_init()
692 chg->constant_volt = DEFAULT_CONSTANT_VOLT; in max77693_dt_init()
694 if (of_property_read_u32(np, "maxim,min-system-microvolt", in max77693_dt_init()
695 &chg->min_system_volt)) in max77693_dt_init()
696 chg->min_system_volt = DEFAULT_MIN_SYSTEM_VOLT; in max77693_dt_init()
698 if (of_property_read_u32(np, "maxim,thermal-regulation-celsius", in max77693_dt_init()
699 &chg->thermal_regulation_temp)) in max77693_dt_init()
700 chg->thermal_regulation_temp = DEFAULT_THERMAL_REGULATION_TEMP; in max77693_dt_init()
702 if (of_property_read_u32(np, "maxim,battery-overcurrent-microamp", in max77693_dt_init()
703 &chg->batttery_overcurrent)) in max77693_dt_init()
704 chg->batttery_overcurrent = DEFAULT_BATTERY_OVERCURRENT; in max77693_dt_init()
706 if (of_property_read_u32(np, "maxim,charge-input-threshold-microvolt", in max77693_dt_init()
707 &chg->charge_input_threshold_volt)) in max77693_dt_init()
708 chg->charge_input_threshold_volt = in max77693_dt_init()
714 static int max77693_dt_init(struct device *dev, struct max77693_charger *chg) in max77693_dt_init() argument
720 static int max77693_charger_probe(struct platform_device *pdev) in max77693_charger_probe()
722 struct max77693_charger *chg; in max77693_charger_probe() local
724 struct max77693_dev *max77693 = dev_get_drvdata(pdev->dev.parent); in max77693_charger_probe()
725 int ret; in max77693_charger_probe()
727 chg = devm_kzalloc(&pdev->dev, sizeof(*chg), GFP_KERNEL); in max77693_charger_probe()
728 if (!chg) in max77693_charger_probe()
729 return -ENOMEM; in max77693_charger_probe()
731 platform_set_drvdata(pdev, chg); in max77693_charger_probe()
732 chg->dev = &pdev->dev; in max77693_charger_probe()
733 chg->max77693 = max77693; in max77693_charger_probe()
735 ret = max77693_dt_init(&pdev->dev, chg); in max77693_charger_probe()
739 ret = max77693_reg_init(chg); in max77693_charger_probe()
743 psy_cfg.drv_data = chg; in max77693_charger_probe()
745 ret = device_create_file(&pdev->dev, &dev_attr_fast_charge_timer); in max77693_charger_probe()
747 dev_err(&pdev->dev, "failed: create fast charge timer sysfs entry\n"); in max77693_charger_probe()
751 ret = device_create_file(&pdev->dev, in max77693_charger_probe()
754 dev_err(&pdev->dev, "failed: create top off current sysfs entry\n"); in max77693_charger_probe()
758 ret = device_create_file(&pdev->dev, &dev_attr_top_off_timer); in max77693_charger_probe()
760 dev_err(&pdev->dev, "failed: create top off timer sysfs entry\n"); in max77693_charger_probe()
764 chg->charger = devm_power_supply_register(&pdev->dev, in max77693_charger_probe()
767 if (IS_ERR(chg->charger)) { in max77693_charger_probe()
768 dev_err(&pdev->dev, "failed: power supply register\n"); in max77693_charger_probe()
769 ret = PTR_ERR(chg->charger); in max77693_charger_probe()
776 device_remove_file(&pdev->dev, &dev_attr_top_off_timer); in max77693_charger_probe()
777 device_remove_file(&pdev->dev, &dev_attr_top_off_threshold_current); in max77693_charger_probe()
778 device_remove_file(&pdev->dev, &dev_attr_fast_charge_timer); in max77693_charger_probe()
785 device_remove_file(&pdev->dev, &dev_attr_top_off_timer); in max77693_charger_remove()
786 device_remove_file(&pdev->dev, &dev_attr_top_off_threshold_current); in max77693_charger_remove()
787 device_remove_file(&pdev->dev, &dev_attr_fast_charge_timer); in max77693_charger_remove()
791 { "max77693-charger", 0, },
798 .name = "max77693-charger",