diff options
author | Bjorn Andersson <bjorn.andersson@sonymobile.com> | 2015-04-06 19:33:58 -0400 |
---|---|---|
committer | Mark Brown <broonie@kernel.org> | 2015-04-08 07:22:03 -0400 |
commit | 469a951446460da843028014a90100428ff6e0c1 (patch) | |
tree | 4daa562c6e0d6362330cdc0add0e7416eeb8e7cf /drivers/regulator | |
parent | 4d1e4d629a484b725d5fc88d7e0f967435ab6edd (diff) |
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>
Diffstat (limited to 'drivers/regulator')
-rw-r--r-- | drivers/regulator/qcom_rpm-regulator.c | 141 |
1 files changed, 81 insertions, 60 deletions
diff --git a/drivers/regulator/qcom_rpm-regulator.c b/drivers/regulator/qcom_rpm-regulator.c index ddca8cb363ad..bd8360ca7750 100644 --- a/drivers/regulator/qcom_rpm-regulator.c +++ b/drivers/regulator/qcom_rpm-regulator.c | |||
@@ -645,7 +645,9 @@ static int rpm_reg_set(struct qcom_rpm_reg *vreg, | |||
645 | return 0; | 645 | return 0; |
646 | } | 646 | } |
647 | 647 | ||
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) | ||
649 | { | 651 | { |
650 | static const int freq_table[] = { | 652 | static const int freq_table[] = { |
651 | 19200000, 9600000, 6400000, 4800000, 3840000, 3200000, 2740000, | 653 | 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) | |||
659 | int i; | 661 | int i; |
660 | 662 | ||
661 | key = "qcom,switch-mode-frequency"; | 663 | 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); |
663 | if (ret) { | 665 | if (ret) { |
664 | dev_err(dev, "regulator requires %s property\n", key); | 666 | dev_err(dev, "regulator requires %s property\n", key); |
665 | return -EINVAL; | 667 | return -EINVAL; |
@@ -676,84 +678,40 @@ static int rpm_reg_of_parse_freq(struct device *dev, struct qcom_rpm_reg *vreg) | |||
676 | return -EINVAL; | 678 | return -EINVAL; |
677 | } | 679 | } |
678 | 680 | ||
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) | ||
680 | { | 684 | { |
681 | struct regulator_init_data *initdata; | 685 | struct qcom_rpm_reg *vreg = config->driver_data; |
682 | const struct qcom_rpm_reg *template; | 686 | struct device *dev = config->dev; |
683 | const struct of_device_id *match; | ||
684 | struct regulator_config config = { }; | ||
685 | struct regulator_dev *rdev; | ||
686 | struct qcom_rpm_reg *vreg; | ||
687 | const char *key; | 687 | const char *key; |
688 | u32 force_mode; | 688 | u32 force_mode; |
689 | bool pwm; | 689 | bool pwm; |
690 | u32 val; | 690 | u32 val; |
691 | int ret; | 691 | int ret; |
692 | 692 | ||
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 | |||
735 | key = "bias-pull-down"; | 693 | key = "bias-pull-down"; |
736 | if (of_property_read_bool(pdev->dev.of_node, key)) { | 694 | if (of_property_read_bool(node, key)) { |
737 | ret = rpm_reg_set(vreg, &vreg->parts->pd, 1); | 695 | ret = rpm_reg_set(vreg, &vreg->parts->pd, 1); |
738 | if (ret) { | 696 | if (ret) { |
739 | dev_err(&pdev->dev, "%s is invalid", key); | 697 | dev_err(dev, "%s is invalid", key); |
740 | return ret; | 698 | return ret; |
741 | } | 699 | } |
742 | } | 700 | } |
743 | 701 | ||
744 | if (vreg->parts->freq.mask) { | 702 | 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); |
746 | if (ret < 0) | 704 | if (ret < 0) |
747 | return ret; | 705 | return ret; |
748 | } | 706 | } |
749 | 707 | ||
750 | if (vreg->parts->pm.mask) { | 708 | if (vreg->parts->pm.mask) { |
751 | key = "qcom,power-mode-hysteretic"; | 709 | key = "qcom,power-mode-hysteretic"; |
752 | pwm = !of_property_read_bool(pdev->dev.of_node, key); | 710 | pwm = !of_property_read_bool(node, key); |
753 | 711 | ||
754 | ret = rpm_reg_set(vreg, &vreg->parts->pm, pwm); | 712 | ret = rpm_reg_set(vreg, &vreg->parts->pm, pwm); |
755 | if (ret) { | 713 | if (ret) { |
756 | dev_err(&pdev->dev, "failed to set power mode\n"); | 714 | dev_err(dev, "failed to set power mode\n"); |
757 | return ret; | 715 | return ret; |
758 | } | 716 | } |
759 | } | 717 | } |
@@ -762,11 +720,11 @@ static int rpm_reg_probe(struct platform_device *pdev) | |||
762 | force_mode = -1; | 720 | force_mode = -1; |
763 | 721 | ||
764 | key = "qcom,force-mode"; | 722 | 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); |
766 | if (ret == -EINVAL) { | 724 | if (ret == -EINVAL) { |
767 | val = QCOM_RPM_FORCE_MODE_NONE; | 725 | val = QCOM_RPM_FORCE_MODE_NONE; |
768 | } else if (ret < 0) { | 726 | } 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); |
770 | return ret; | 728 | return ret; |
771 | } | 729 | } |
772 | 730 | ||
@@ -801,21 +759,84 @@ static int rpm_reg_probe(struct platform_device *pdev) | |||
801 | } | 759 | } |
802 | 760 | ||
803 | if (force_mode == -1) { | 761 | if (force_mode == -1) { |
804 | dev_err(&pdev->dev, "invalid force mode\n"); | 762 | dev_err(dev, "invalid force mode\n"); |
805 | return -EINVAL; | 763 | return -EINVAL; |
806 | } | 764 | } |
807 | 765 | ||
808 | ret = rpm_reg_set(vreg, &vreg->parts->fm, force_mode); | 766 | ret = rpm_reg_set(vreg, &vreg->parts->fm, force_mode); |
809 | if (ret) { | 767 | if (ret) { |
810 | dev_err(&pdev->dev, "failed to set force mode\n"); | 768 | dev_err(dev, "failed to set force mode\n"); |
811 | return ret; | 769 | return ret; |
812 | } | 770 | } |
813 | } | 771 | } |
814 | 772 | ||
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 | |||
815 | config.dev = &pdev->dev; | 831 | config.dev = &pdev->dev; |
816 | config.init_data = initdata; | 832 | config.init_data = initdata; |
817 | config.driver_data = vreg; | 833 | config.driver_data = vreg; |
818 | config.of_node = pdev->dev.of_node; | 834 | 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 | |||
819 | rdev = devm_regulator_register(&pdev->dev, &vreg->desc, &config); | 840 | rdev = devm_regulator_register(&pdev->dev, &vreg->desc, &config); |
820 | if (IS_ERR(rdev)) { | 841 | if (IS_ERR(rdev)) { |
821 | dev_err(&pdev->dev, "can't register regulator\n"); | 842 | dev_err(&pdev->dev, "can't register regulator\n"); |