Skip to content

Commit 469a951

Browse files
anderssonbroonie
authored andcommitted
regulator: qcom: Refactor of-parsing code
Refactor out all custom property parsing code from the probe function into a function suitable for regulator_desc->of_parse_cb usage. Reviewed-by: Stephen Boyd <sboyd@codeaurora.org> Signed-off-by: Bjorn Andersson <bjorn.andersson@sonymobile.com> Signed-off-by: Mark Brown <broonie@kernel.org>
1 parent 4d1e4d6 commit 469a951

1 file changed

Lines changed: 81 additions & 60 deletions

File tree

drivers/regulator/qcom_rpm-regulator.c

Lines changed: 81 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,9 @@ static int rpm_reg_set(struct qcom_rpm_reg *vreg,
645645
return 0;
646646
}
647647

648-
static int rpm_reg_of_parse_freq(struct device *dev, struct qcom_rpm_reg *vreg)
648+
static int rpm_reg_of_parse_freq(struct device *dev,
649+
struct device_node *node,
650+
struct qcom_rpm_reg *vreg)
649651
{
650652
static const int freq_table[] = {
651653
19200000, 9600000, 6400000, 4800000, 3840000, 3200000, 2740000,
@@ -659,7 +661,7 @@ static int rpm_reg_of_parse_freq(struct device *dev, struct qcom_rpm_reg *vreg)
659661
int i;
660662

661663
key = "qcom,switch-mode-frequency";
662-
ret = of_property_read_u32(dev->of_node, key, &freq);
664+
ret = of_property_read_u32(node, key, &freq);
663665
if (ret) {
664666
dev_err(dev, "regulator requires %s property\n", key);
665667
return -EINVAL;
@@ -676,84 +678,40 @@ static int rpm_reg_of_parse_freq(struct device *dev, struct qcom_rpm_reg *vreg)
676678
return -EINVAL;
677679
}
678680

679-
static int rpm_reg_probe(struct platform_device *pdev)
681+
static int rpm_reg_of_parse(struct device_node *node,
682+
const struct regulator_desc *desc,
683+
struct regulator_config *config)
680684
{
681-
struct regulator_init_data *initdata;
682-
const struct qcom_rpm_reg *template;
683-
const struct of_device_id *match;
684-
struct regulator_config config = { };
685-
struct regulator_dev *rdev;
686-
struct qcom_rpm_reg *vreg;
685+
struct qcom_rpm_reg *vreg = config->driver_data;
686+
struct device *dev = config->dev;
687687
const char *key;
688688
u32 force_mode;
689689
bool pwm;
690690
u32 val;
691691
int ret;
692692

693-
match = of_match_device(rpm_of_match, &pdev->dev);
694-
template = match->data;
695-
696-
vreg = devm_kmalloc(&pdev->dev, sizeof(*vreg), GFP_KERNEL);
697-
if (!vreg) {
698-
dev_err(&pdev->dev, "failed to allocate vreg\n");
699-
return -ENOMEM;
700-
}
701-
memcpy(vreg, template, sizeof(*vreg));
702-
mutex_init(&vreg->lock);
703-
vreg->dev = &pdev->dev;
704-
vreg->desc.id = -1;
705-
vreg->desc.owner = THIS_MODULE;
706-
vreg->desc.type = REGULATOR_VOLTAGE;
707-
vreg->desc.name = pdev->dev.of_node->name;
708-
vreg->desc.supply_name = "vin";
709-
710-
vreg->rpm = dev_get_drvdata(pdev->dev.parent);
711-
if (!vreg->rpm) {
712-
dev_err(&pdev->dev, "unable to retrieve handle to rpm\n");
713-
return -ENODEV;
714-
}
715-
716-
initdata = of_get_regulator_init_data(&pdev->dev, pdev->dev.of_node,
717-
&vreg->desc);
718-
if (!initdata)
719-
return -EINVAL;
720-
721-
key = "reg";
722-
ret = of_property_read_u32(pdev->dev.of_node, key, &val);
723-
if (ret) {
724-
dev_err(&pdev->dev, "failed to read %s\n", key);
725-
return ret;
726-
}
727-
vreg->resource = val;
728-
729-
if ((vreg->parts->uV.mask || vreg->parts->mV.mask) &&
730-
(!initdata->constraints.min_uV || !initdata->constraints.max_uV)) {
731-
dev_err(&pdev->dev, "no voltage specified for regulator\n");
732-
return -EINVAL;
733-
}
734-
735693
key = "bias-pull-down";
736-
if (of_property_read_bool(pdev->dev.of_node, key)) {
694+
if (of_property_read_bool(node, key)) {
737695
ret = rpm_reg_set(vreg, &vreg->parts->pd, 1);
738696
if (ret) {
739-
dev_err(&pdev->dev, "%s is invalid", key);
697+
dev_err(dev, "%s is invalid", key);
740698
return ret;
741699
}
742700
}
743701

744702
if (vreg->parts->freq.mask) {
745-
ret = rpm_reg_of_parse_freq(&pdev->dev, vreg);
703+
ret = rpm_reg_of_parse_freq(dev, node, vreg);
746704
if (ret < 0)
747705
return ret;
748706
}
749707

750708
if (vreg->parts->pm.mask) {
751709
key = "qcom,power-mode-hysteretic";
752-
pwm = !of_property_read_bool(pdev->dev.of_node, key);
710+
pwm = !of_property_read_bool(node, key);
753711

754712
ret = rpm_reg_set(vreg, &vreg->parts->pm, pwm);
755713
if (ret) {
756-
dev_err(&pdev->dev, "failed to set power mode\n");
714+
dev_err(dev, "failed to set power mode\n");
757715
return ret;
758716
}
759717
}
@@ -762,11 +720,11 @@ static int rpm_reg_probe(struct platform_device *pdev)
762720
force_mode = -1;
763721

764722
key = "qcom,force-mode";
765-
ret = of_property_read_u32(pdev->dev.of_node, key, &val);
723+
ret = of_property_read_u32(node, key, &val);
766724
if (ret == -EINVAL) {
767725
val = QCOM_RPM_FORCE_MODE_NONE;
768726
} else if (ret < 0) {
769-
dev_err(&pdev->dev, "failed to read %s\n", key);
727+
dev_err(dev, "failed to read %s\n", key);
770728
return ret;
771729
}
772730

@@ -801,21 +759,84 @@ static int rpm_reg_probe(struct platform_device *pdev)
801759
}
802760

803761
if (force_mode == -1) {
804-
dev_err(&pdev->dev, "invalid force mode\n");
762+
dev_err(dev, "invalid force mode\n");
805763
return -EINVAL;
806764
}
807765

808766
ret = rpm_reg_set(vreg, &vreg->parts->fm, force_mode);
809767
if (ret) {
810-
dev_err(&pdev->dev, "failed to set force mode\n");
768+
dev_err(dev, "failed to set force mode\n");
811769
return ret;
812770
}
813771
}
814772

773+
return 0;
774+
}
775+
776+
static int rpm_reg_probe(struct platform_device *pdev)
777+
{
778+
struct regulator_init_data *initdata;
779+
const struct qcom_rpm_reg *template;
780+
const struct of_device_id *match;
781+
struct regulator_config config = { };
782+
struct regulator_dev *rdev;
783+
struct qcom_rpm_reg *vreg;
784+
const char *key;
785+
u32 val;
786+
int ret;
787+
788+
match = of_match_device(rpm_of_match, &pdev->dev);
789+
template = match->data;
790+
791+
vreg = devm_kmalloc(&pdev->dev, sizeof(*vreg), GFP_KERNEL);
792+
if (!vreg) {
793+
dev_err(&pdev->dev, "failed to allocate vreg\n");
794+
return -ENOMEM;
795+
}
796+
memcpy(vreg, template, sizeof(*vreg));
797+
mutex_init(&vreg->lock);
798+
vreg->dev = &pdev->dev;
799+
vreg->desc.id = -1;
800+
vreg->desc.owner = THIS_MODULE;
801+
vreg->desc.type = REGULATOR_VOLTAGE;
802+
vreg->desc.name = pdev->dev.of_node->name;
803+
vreg->desc.supply_name = "vin";
804+
805+
vreg->rpm = dev_get_drvdata(pdev->dev.parent);
806+
if (!vreg->rpm) {
807+
dev_err(&pdev->dev, "unable to retrieve handle to rpm\n");
808+
return -ENODEV;
809+
}
810+
811+
initdata = of_get_regulator_init_data(&pdev->dev, pdev->dev.of_node,
812+
&vreg->desc);
813+
if (!initdata)
814+
return -EINVAL;
815+
816+
key = "reg";
817+
ret = of_property_read_u32(pdev->dev.of_node, key, &val);
818+
if (ret) {
819+
dev_err(&pdev->dev, "failed to read %s\n", key);
820+
return ret;
821+
}
822+
vreg->resource = val;
823+
824+
if ((vreg->parts->uV.mask || vreg->parts->mV.mask) &&
825+
(!initdata->constraints.min_uV || !initdata->constraints.max_uV)) {
826+
dev_err(&pdev->dev, "no voltage specified for regulator\n");
827+
return -EINVAL;
828+
}
829+
830+
815831
config.dev = &pdev->dev;
816832
config.init_data = initdata;
817833
config.driver_data = vreg;
818834
config.of_node = pdev->dev.of_node;
835+
836+
ret = rpm_reg_of_parse(pdev->dev.of_node, &vreg->desc, &config);
837+
if (ret)
838+
return ret;
839+
819840
rdev = devm_regulator_register(&pdev->dev, &vreg->desc, &config);
820841
if (IS_ERR(rdev)) {
821842
dev_err(&pdev->dev, "can't register regulator\n");

0 commit comments

Comments
 (0)