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()
210 static int max77693_charger_get_property(struct power_supply *psy, in max77693_charger_get_property()
214 struct max77693_charger *chg = power_supply_get_drvdata(psy); in max77693_charger_get_property() local
215 struct regmap *regmap = chg->max77693->regmap; in max77693_charger_get_property()
216 int ret = 0; in max77693_charger_get_property()
220 ret = max77693_get_charger_state(regmap, &val->intval); in max77693_charger_get_property()
223 ret = max77693_get_charge_type(regmap, &val->intval); in max77693_charger_get_property()
226 ret = max77693_get_battery_health(regmap, &val->intval); in max77693_charger_get_property()
229 ret = max77693_get_present(regmap, &val->intval); in max77693_charger_get_property()
232 ret = max77693_get_online(regmap, &val->intval); in max77693_charger_get_property()
235 val->strval = max77693_charger_model; in max77693_charger_get_property()
238 val->strval = max77693_charger_manufacturer; in max77693_charger_get_property()
241 return -EINVAL; in max77693_charger_get_property()
257 int (*fn)(struct max77693_charger *, unsigned long)) in device_attr_store()
259 struct max77693_charger *chg = dev_get_drvdata(dev); in device_attr_store() local
261 int ret; in device_attr_store()
267 ret = fn(chg, val); in device_attr_store()
277 struct max77693_charger *chg = dev_get_drvdata(dev); in fast_charge_timer_show() local
278 unsigned int data, val; in fast_charge_timer_show()
279 int ret; in fast_charge_timer_show()
281 ret = regmap_read(chg->max77693->regmap, MAX77693_CHG_REG_CHG_CNFG_01, in fast_charge_timer_show()
291 val = 4 + (data - 1) * 2; in fast_charge_timer_show()
302 static int max77693_set_fast_charge_timer(struct max77693_charger *chg, in max77693_set_fast_charge_timer() argument
305 unsigned int data; in max77693_set_fast_charge_timer()
308 * 0x00 - disable in max77693_set_fast_charge_timer()
309 * 0x01 - 4h in max77693_set_fast_charge_timer()
310 * 0x02 - 6h in max77693_set_fast_charge_timer()
312 * 0x07 - 16h in max77693_set_fast_charge_timer()
317 data = (hours - 4) / 2 + 1; in max77693_set_fast_charge_timer()
324 return -EINVAL; in max77693_set_fast_charge_timer()
328 return regmap_update_bits(chg->max77693->regmap, in max77693_set_fast_charge_timer()
343 struct max77693_charger *chg = dev_get_drvdata(dev); in top_off_threshold_current_show() local
344 unsigned int data, val; in top_off_threshold_current_show()
345 int ret; in top_off_threshold_current_show()
347 ret = regmap_read(chg->max77693->regmap, MAX77693_CHG_REG_CHG_CNFG_03, in top_off_threshold_current_show()
363 static int max77693_set_top_off_threshold_current(struct max77693_charger *chg, in max77693_set_top_off_threshold_current() argument
366 unsigned int data; in max77693_set_top_off_threshold_current()
369 return -EINVAL; in max77693_set_top_off_threshold_current()
372 data = (uamp - 100000) / 25000; in max77693_set_top_off_threshold_current()
379 return regmap_update_bits(chg->max77693->regmap, in max77693_set_top_off_threshold_current()
394 struct max77693_charger *chg = dev_get_drvdata(dev); in top_off_timer_show() local
395 unsigned int data, val; in top_off_timer_show()
396 int ret; in top_off_timer_show()
398 ret = regmap_read(chg->max77693->regmap, MAX77693_CHG_REG_CHG_CNFG_03, in top_off_timer_show()
411 static int max77693_set_top_off_timer(struct max77693_charger *chg, in max77693_set_top_off_timer() argument
414 unsigned int data; in max77693_set_top_off_timer()
417 return -EINVAL; in max77693_set_top_off_timer()
422 return regmap_update_bits(chg->max77693->regmap, in max77693_set_top_off_timer()
438 static int max77693_set_constant_volt(struct max77693_charger *chg, in max77693_set_constant_volt() argument
439 unsigned int uvolt) in max77693_set_constant_volt()
441 unsigned int data; in max77693_set_constant_volt()
444 * 0x00 - 3.650 V in max77693_set_constant_volt()
445 * 0x01 - 3.675 V in max77693_set_constant_volt()
447 * 0x1b - 4.325 V in max77693_set_constant_volt()
448 * 0x1c - 4.340 V in max77693_set_constant_volt()
449 * 0x1d - 4.350 V in max77693_set_constant_volt()
450 * 0x1e - 4.375 V in max77693_set_constant_volt()
451 * 0x1f - 4.400 V in max77693_set_constant_volt()
454 data = (uvolt - 3650000) / 25000; in max77693_set_constant_volt()
458 data = 0x1d + (uvolt - 4350000) / 25000; in max77693_set_constant_volt()
460 dev_err(chg->dev, "Wrong value for charging constant voltage\n"); in max77693_set_constant_volt()
461 return -EINVAL; in max77693_set_constant_volt()
466 dev_dbg(chg->dev, "Charging constant voltage: %u (0x%x)\n", uvolt, in max77693_set_constant_volt()
469 return regmap_update_bits(chg->max77693->regmap, in max77693_set_constant_volt()
474 static int max77693_set_min_system_volt(struct max77693_charger *chg, in max77693_set_min_system_volt() argument
475 unsigned int uvolt) in max77693_set_min_system_volt()
477 unsigned int data; in max77693_set_min_system_volt()
480 dev_err(chg->dev, "Wrong value for minimum system regulation voltage\n"); in max77693_set_min_system_volt()
481 return -EINVAL; in max77693_set_min_system_volt()
484 data = (uvolt - 3000000) / 100000; in max77693_set_min_system_volt()
488 dev_dbg(chg->dev, "Minimum system regulation voltage: %u (0x%x)\n", in max77693_set_min_system_volt()
491 return regmap_update_bits(chg->max77693->regmap, in max77693_set_min_system_volt()
496 static int max77693_set_thermal_regulation_temp(struct max77693_charger *chg, in max77693_set_thermal_regulation_temp() argument
497 unsigned int cels) in max77693_set_thermal_regulation_temp()
499 unsigned int data; in max77693_set_thermal_regulation_temp()
506 data = (cels - 70) / 15; in max77693_set_thermal_regulation_temp()
509 dev_err(chg->dev, "Wrong value for thermal regulation loop temperature\n"); in max77693_set_thermal_regulation_temp()
510 return -EINVAL; in max77693_set_thermal_regulation_temp()
515 dev_dbg(chg->dev, "Thermal regulation loop temperature: %u (0x%x)\n", in max77693_set_thermal_regulation_temp()
518 return regmap_update_bits(chg->max77693->regmap, in max77693_set_thermal_regulation_temp()
523 static int max77693_set_batttery_overcurrent(struct max77693_charger *chg, in max77693_set_batttery_overcurrent() argument
524 unsigned int uamp) in max77693_set_batttery_overcurrent()
526 unsigned int data; in max77693_set_batttery_overcurrent()
529 dev_err(chg->dev, "Wrong value for battery overcurrent\n"); in max77693_set_batttery_overcurrent()
530 return -EINVAL; in max77693_set_batttery_overcurrent()
534 data = ((uamp - 2000000) / 250000) + 1; in max77693_set_batttery_overcurrent()
540 dev_dbg(chg->dev, "Battery overcurrent: %u (0x%x)\n", uamp, data); in max77693_set_batttery_overcurrent()
542 return regmap_update_bits(chg->max77693->regmap, in max77693_set_batttery_overcurrent()
547 static int max77693_set_charge_input_threshold_volt(struct max77693_charger *chg, in max77693_set_charge_input_threshold_volt() argument
548 unsigned int uvolt) in max77693_set_charge_input_threshold_volt()
550 unsigned int data; in max77693_set_charge_input_threshold_volt()
559 data = (uvolt - 4700000) / 100000; in max77693_set_charge_input_threshold_volt()
562 dev_err(chg->dev, "Wrong value for charge input voltage regulation threshold\n"); in max77693_set_charge_input_threshold_volt()
563 return -EINVAL; in max77693_set_charge_input_threshold_volt()
568 dev_dbg(chg->dev, "Charge input voltage regulation threshold: %u (0x%x)\n", in max77693_set_charge_input_threshold_volt()
571 return regmap_update_bits(chg->max77693->regmap, in max77693_set_charge_input_threshold_volt()
579 static int max77693_reg_init(struct max77693_charger *chg) in max77693_reg_init() argument
581 int ret; in max77693_reg_init()
582 unsigned int data; in max77693_reg_init()
586 ret = regmap_update_bits(chg->max77693->regmap, in max77693_reg_init()
590 dev_err(chg->dev, "Error unlocking registers: %d\n", ret); in max77693_reg_init()
594 ret = max77693_set_fast_charge_timer(chg, DEFAULT_FAST_CHARGE_TIMER); in max77693_reg_init()
598 ret = max77693_set_top_off_threshold_current(chg, in max77693_reg_init()
603 ret = max77693_set_top_off_timer(chg, DEFAULT_TOP_OFF_TIMER); in max77693_reg_init()
607 ret = max77693_set_constant_volt(chg, chg->constant_volt); in max77693_reg_init()
611 ret = max77693_set_min_system_volt(chg, chg->min_system_volt); in max77693_reg_init()
615 ret = max77693_set_thermal_regulation_temp(chg, in max77693_reg_init()
616 chg->thermal_regulation_temp); in max77693_reg_init()
620 ret = max77693_set_batttery_overcurrent(chg, chg->batttery_overcurrent); in max77693_reg_init()
624 return max77693_set_charge_input_threshold_volt(chg, in max77693_reg_init()
625 chg->charge_input_threshold_volt); in max77693_reg_init()
629 static int max77693_dt_init(struct device *dev, struct max77693_charger *chg) in max77693_dt_init() argument
631 struct device_node *np = dev->of_node; in max77693_dt_init()
635 return -EINVAL; in max77693_dt_init()
638 if (of_property_read_u32(np, "maxim,constant-microvolt", in max77693_dt_init()
639 &chg->constant_volt)) in max77693_dt_init()
640 chg->constant_volt = DEFAULT_CONSTANT_VOLT; in max77693_dt_init()
642 if (of_property_read_u32(np, "maxim,min-system-microvolt", in max77693_dt_init()
643 &chg->min_system_volt)) in max77693_dt_init()
644 chg->min_system_volt = DEFAULT_MIN_SYSTEM_VOLT; in max77693_dt_init()
646 if (of_property_read_u32(np, "maxim,thermal-regulation-celsius", in max77693_dt_init()
647 &chg->thermal_regulation_temp)) in max77693_dt_init()
648 chg->thermal_regulation_temp = DEFAULT_THERMAL_REGULATION_TEMP; in max77693_dt_init()
650 if (of_property_read_u32(np, "maxim,battery-overcurrent-microamp", in max77693_dt_init()
651 &chg->batttery_overcurrent)) in max77693_dt_init()
652 chg->batttery_overcurrent = DEFAULT_BATTERY_OVERCURRENT; in max77693_dt_init()
654 if (of_property_read_u32(np, "maxim,charge-input-threshold-microvolt", in max77693_dt_init()
655 &chg->charge_input_threshold_volt)) in max77693_dt_init()
656 chg->charge_input_threshold_volt = in max77693_dt_init()
662 static int max77693_dt_init(struct device *dev, struct max77693_charger *chg) in max77693_dt_init() argument
668 static int max77693_charger_probe(struct platform_device *pdev) in max77693_charger_probe()
670 struct max77693_charger *chg; in max77693_charger_probe() local
672 struct max77693_dev *max77693 = dev_get_drvdata(pdev->dev.parent); in max77693_charger_probe()
673 int ret; in max77693_charger_probe()
675 chg = devm_kzalloc(&pdev->dev, sizeof(*chg), GFP_KERNEL); in max77693_charger_probe()
676 if (!chg) in max77693_charger_probe()
677 return -ENOMEM; in max77693_charger_probe()
679 platform_set_drvdata(pdev, chg); in max77693_charger_probe()
680 chg->dev = &pdev->dev; in max77693_charger_probe()
681 chg->max77693 = max77693; in max77693_charger_probe()
683 ret = max77693_dt_init(&pdev->dev, chg); in max77693_charger_probe()
687 ret = max77693_reg_init(chg); in max77693_charger_probe()
691 psy_cfg.drv_data = chg; in max77693_charger_probe()
693 ret = device_create_file(&pdev->dev, &dev_attr_fast_charge_timer); in max77693_charger_probe()
695 dev_err(&pdev->dev, "failed: create fast charge timer sysfs entry\n"); in max77693_charger_probe()
699 ret = device_create_file(&pdev->dev, in max77693_charger_probe()
702 dev_err(&pdev->dev, "failed: create top off current sysfs entry\n"); in max77693_charger_probe()
706 ret = device_create_file(&pdev->dev, &dev_attr_top_off_timer); in max77693_charger_probe()
708 dev_err(&pdev->dev, "failed: create top off timer sysfs entry\n"); in max77693_charger_probe()
712 chg->charger = power_supply_register(&pdev->dev, in max77693_charger_probe()
715 if (IS_ERR(chg->charger)) { in max77693_charger_probe()
716 dev_err(&pdev->dev, "failed: power supply register\n"); in max77693_charger_probe()
717 ret = PTR_ERR(chg->charger); in max77693_charger_probe()
724 device_remove_file(&pdev->dev, &dev_attr_top_off_timer); in max77693_charger_probe()
725 device_remove_file(&pdev->dev, &dev_attr_top_off_threshold_current); in max77693_charger_probe()
726 device_remove_file(&pdev->dev, &dev_attr_fast_charge_timer); in max77693_charger_probe()
733 struct max77693_charger *chg = platform_get_drvdata(pdev); in max77693_charger_remove() local
735 device_remove_file(&pdev->dev, &dev_attr_top_off_timer); in max77693_charger_remove()
736 device_remove_file(&pdev->dev, &dev_attr_top_off_threshold_current); in max77693_charger_remove()
737 device_remove_file(&pdev->dev, &dev_attr_fast_charge_timer); in max77693_charger_remove()
739 power_supply_unregister(chg->charger); in max77693_charger_remove()
743 { "max77693-charger", 0, },
750 .name = "max77693-charger",