Skip to content

Commit db97fec

Browse files
macromorgansre
authored andcommitted
power: supply: axp20x_battery: Make iio and battery config per device
Move the configuration of battery specific information and available iio channels from the probe function to a device specific routine, allowing us to use this driver for devices with slightly different configurations (such as the AXP717). Signed-off-by: Chris Morgan <[email protected]> Acked-by: Krzysztof Kozlowski <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Sebastian Reichel <[email protected]>
1 parent 6197880 commit db97fec

File tree

1 file changed

+88
-49
lines changed

1 file changed

+88
-49
lines changed

drivers/power/supply/axp20x_battery.c

Lines changed: 88 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,19 @@
5858
struct axp20x_batt_ps;
5959

6060
struct axp_data {
61-
int ccc_scale;
62-
int ccc_offset;
63-
bool has_fg_valid;
61+
int ccc_scale;
62+
int ccc_offset;
63+
unsigned int ccc_reg;
64+
unsigned int ccc_mask;
65+
bool has_fg_valid;
66+
const struct power_supply_desc *bat_ps_desc;
6467
int (*get_max_voltage)(struct axp20x_batt_ps *batt, int *val);
6568
int (*set_max_voltage)(struct axp20x_batt_ps *batt, int val);
69+
int (*cfg_iio_chan)(struct platform_device *pdev,
70+
struct axp20x_batt_ps *axp_batt);
71+
void (*set_bat_info)(struct platform_device *pdev,
72+
struct axp20x_batt_ps *axp_batt,
73+
struct power_supply_battery_info *info);
6674
};
6775

6876
struct axp20x_batt_ps {
@@ -508,7 +516,7 @@ static int axp20x_battery_prop_writeable(struct power_supply *psy,
508516
psp == POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX;
509517
}
510518

511-
static const struct power_supply_desc axp20x_batt_ps_desc = {
519+
static const struct power_supply_desc axp209_batt_ps_desc = {
512520
.name = "axp20x-battery",
513521
.type = POWER_SUPPLY_TYPE_BATTERY,
514522
.properties = axp20x_battery_props,
@@ -518,27 +526,94 @@ static const struct power_supply_desc axp20x_batt_ps_desc = {
518526
.set_property = axp20x_battery_set_prop,
519527
};
520528

529+
static int axp209_bat_cfg_iio_channels(struct platform_device *pdev,
530+
struct axp20x_batt_ps *axp_batt)
531+
{
532+
axp_batt->batt_v = devm_iio_channel_get(&pdev->dev, "batt_v");
533+
if (IS_ERR(axp_batt->batt_v)) {
534+
if (PTR_ERR(axp_batt->batt_v) == -ENODEV)
535+
return -EPROBE_DEFER;
536+
return PTR_ERR(axp_batt->batt_v);
537+
}
538+
539+
axp_batt->batt_chrg_i = devm_iio_channel_get(&pdev->dev,
540+
"batt_chrg_i");
541+
if (IS_ERR(axp_batt->batt_chrg_i)) {
542+
if (PTR_ERR(axp_batt->batt_chrg_i) == -ENODEV)
543+
return -EPROBE_DEFER;
544+
return PTR_ERR(axp_batt->batt_chrg_i);
545+
}
546+
547+
axp_batt->batt_dischrg_i = devm_iio_channel_get(&pdev->dev,
548+
"batt_dischrg_i");
549+
if (IS_ERR(axp_batt->batt_dischrg_i)) {
550+
if (PTR_ERR(axp_batt->batt_dischrg_i) == -ENODEV)
551+
return -EPROBE_DEFER;
552+
return PTR_ERR(axp_batt->batt_dischrg_i);
553+
}
554+
555+
return 0;
556+
}
557+
558+
static void axp209_set_battery_info(struct platform_device *pdev,
559+
struct axp20x_batt_ps *axp_batt,
560+
struct power_supply_battery_info *info)
561+
{
562+
int vmin = info->voltage_min_design_uv;
563+
int ccc = info->constant_charge_current_max_ua;
564+
565+
if (vmin > 0 && axp20x_set_voltage_min_design(axp_batt, vmin))
566+
dev_err(&pdev->dev,
567+
"couldn't set voltage_min_design\n");
568+
569+
/* Set max to unverified value to be able to set CCC */
570+
axp_batt->max_ccc = ccc;
571+
572+
if (ccc <= 0 || axp20x_set_constant_charge_current(axp_batt, ccc)) {
573+
dev_err(&pdev->dev,
574+
"couldn't set ccc from DT: fallback to min value\n");
575+
ccc = 300000;
576+
axp_batt->max_ccc = ccc;
577+
axp20x_set_constant_charge_current(axp_batt, ccc);
578+
}
579+
}
580+
521581
static const struct axp_data axp209_data = {
522582
.ccc_scale = 100000,
523583
.ccc_offset = 300000,
584+
.ccc_reg = AXP20X_CHRG_CTRL1,
585+
.ccc_mask = AXP20X_CHRG_CTRL1_TGT_CURR,
586+
.bat_ps_desc = &axp209_batt_ps_desc,
524587
.get_max_voltage = axp20x_battery_get_max_voltage,
525588
.set_max_voltage = axp20x_battery_set_max_voltage,
589+
.cfg_iio_chan = axp209_bat_cfg_iio_channels,
590+
.set_bat_info = axp209_set_battery_info,
526591
};
527592

528593
static const struct axp_data axp221_data = {
529594
.ccc_scale = 150000,
530595
.ccc_offset = 300000,
596+
.ccc_reg = AXP20X_CHRG_CTRL1,
597+
.ccc_mask = AXP20X_CHRG_CTRL1_TGT_CURR,
531598
.has_fg_valid = true,
599+
.bat_ps_desc = &axp209_batt_ps_desc,
532600
.get_max_voltage = axp22x_battery_get_max_voltage,
533601
.set_max_voltage = axp22x_battery_set_max_voltage,
602+
.cfg_iio_chan = axp209_bat_cfg_iio_channels,
603+
.set_bat_info = axp209_set_battery_info,
534604
};
535605

536606
static const struct axp_data axp813_data = {
537607
.ccc_scale = 200000,
538608
.ccc_offset = 200000,
609+
.ccc_reg = AXP20X_CHRG_CTRL1,
610+
.ccc_mask = AXP20X_CHRG_CTRL1_TGT_CURR,
539611
.has_fg_valid = true,
612+
.bat_ps_desc = &axp209_batt_ps_desc,
540613
.get_max_voltage = axp813_battery_get_max_voltage,
541614
.set_max_voltage = axp20x_battery_set_max_voltage,
615+
.cfg_iio_chan = axp209_bat_cfg_iio_channels,
616+
.set_bat_info = axp209_set_battery_info,
542617
};
543618

544619
static const struct of_device_id axp20x_battery_ps_id[] = {
@@ -561,6 +636,7 @@ static int axp20x_power_probe(struct platform_device *pdev)
561636
struct power_supply_config psy_cfg = {};
562637
struct power_supply_battery_info *info;
563638
struct device *dev = &pdev->dev;
639+
int ret;
564640

565641
if (!of_device_is_available(pdev->dev.of_node))
566642
return -ENODEV;
@@ -572,29 +648,6 @@ static int axp20x_power_probe(struct platform_device *pdev)
572648

573649
axp20x_batt->dev = &pdev->dev;
574650

575-
axp20x_batt->batt_v = devm_iio_channel_get(&pdev->dev, "batt_v");
576-
if (IS_ERR(axp20x_batt->batt_v)) {
577-
if (PTR_ERR(axp20x_batt->batt_v) == -ENODEV)
578-
return -EPROBE_DEFER;
579-
return PTR_ERR(axp20x_batt->batt_v);
580-
}
581-
582-
axp20x_batt->batt_chrg_i = devm_iio_channel_get(&pdev->dev,
583-
"batt_chrg_i");
584-
if (IS_ERR(axp20x_batt->batt_chrg_i)) {
585-
if (PTR_ERR(axp20x_batt->batt_chrg_i) == -ENODEV)
586-
return -EPROBE_DEFER;
587-
return PTR_ERR(axp20x_batt->batt_chrg_i);
588-
}
589-
590-
axp20x_batt->batt_dischrg_i = devm_iio_channel_get(&pdev->dev,
591-
"batt_dischrg_i");
592-
if (IS_ERR(axp20x_batt->batt_dischrg_i)) {
593-
if (PTR_ERR(axp20x_batt->batt_dischrg_i) == -ENODEV)
594-
return -EPROBE_DEFER;
595-
return PTR_ERR(axp20x_batt->batt_dischrg_i);
596-
}
597-
598651
axp20x_batt->regmap = dev_get_regmap(pdev->dev.parent, NULL);
599652
platform_set_drvdata(pdev, axp20x_batt);
600653

@@ -603,8 +656,12 @@ static int axp20x_power_probe(struct platform_device *pdev)
603656

604657
axp20x_batt->data = (struct axp_data *)of_device_get_match_data(dev);
605658

659+
ret = axp20x_batt->data->cfg_iio_chan(pdev, axp20x_batt);
660+
if (ret)
661+
return ret;
662+
606663
axp20x_batt->batt = devm_power_supply_register(&pdev->dev,
607-
&axp20x_batt_ps_desc,
664+
axp20x_batt->data->bat_ps_desc,
608665
&psy_cfg);
609666
if (IS_ERR(axp20x_batt->batt)) {
610667
dev_err(&pdev->dev, "failed to register power supply: %ld\n",
@@ -613,33 +670,15 @@ static int axp20x_power_probe(struct platform_device *pdev)
613670
}
614671

615672
if (!power_supply_get_battery_info(axp20x_batt->batt, &info)) {
616-
int vmin = info->voltage_min_design_uv;
617-
int ccc = info->constant_charge_current_max_ua;
618-
619-
if (vmin > 0 && axp20x_set_voltage_min_design(axp20x_batt,
620-
vmin))
621-
dev_err(&pdev->dev,
622-
"couldn't set voltage_min_design\n");
623-
624-
/* Set max to unverified value to be able to set CCC */
625-
axp20x_batt->max_ccc = ccc;
626-
627-
if (ccc <= 0 || axp20x_set_constant_charge_current(axp20x_batt,
628-
ccc)) {
629-
dev_err(&pdev->dev,
630-
"couldn't set constant charge current from DT: fallback to minimum value\n");
631-
ccc = 300000;
632-
axp20x_batt->max_ccc = ccc;
633-
axp20x_set_constant_charge_current(axp20x_batt, ccc);
634-
}
673+
axp20x_batt->data->set_bat_info(pdev, axp20x_batt, info);
674+
power_supply_put_battery_info(axp20x_batt->batt, info);
635675
}
636676

637677
/*
638678
* Update max CCC to a valid value if battery info is present or set it
639679
* to current register value by default.
640680
*/
641-
axp20x_get_constant_charge_current(axp20x_batt,
642-
&axp20x_batt->max_ccc);
681+
axp20x_get_constant_charge_current(axp20x_batt, &axp20x_batt->max_ccc);
643682

644683
return 0;
645684
}

0 commit comments

Comments
 (0)