| @@ -682,7 +682,7 @@ static int _set_opp_custom(const struct opp_table *opp_table, · 681 unmodified lines |
| 682 | 682 | |
| 683 | 683 | data->old_opp.rate = old_freq; |
| 684 | 684 | size = sizeof(*old_supply) * opp_table->regulator_count; |
| 685 | | − if (IS_ERR(old_supply)) |
| 685 | + if (!old_supply) |
| 686 | 686 | memset(data->old_opp.supplies, 0, size); |
| 687 | 687 | else |
| 688 | 688 | memcpy(data->old_opp.supplies, old_supply, size); |
| @@ -708,7 +708,7 @@ static int _set_required_opps(struct device *dev, · 19 unmodified lines |
| 708 | 708 | |
| 709 | 709 | /* Single genpd case */ |
| 710 | 710 | if (!genpd_virt_devs) { |
| 711 | | − pstate = opp->required_opps[0]->pstate; |
| 711 | + pstate = likely(opp) ? opp->required_opps[0]->pstate : 0; |
| 712 | 712 | ret = dev_pm_genpd_set_performance_state(dev, pstate); |
| 713 | 713 | if (ret) { |
| 714 | 714 | dev_err(dev, "Failed to set performance state of %s: %d (%d)\n", |
| @@ -726,7 +726,7 @@ static int _set_required_opps(struct device *dev, · 11 unmodified lines |
| 726 | 726 | mutex_lock(&opp_table->genpd_virt_dev_lock); |
| 727 | 727 | |
| 728 | 728 | for (i = 0; i < opp_table->required_opp_count; i++) { |
| 729 | | − pstate = opp->required_opps[i]->pstate; |
| 729 | + pstate = likely(opp) ? opp->required_opps[i]->pstate : 0; |
| 730 | 730 | |
| 731 | 731 | if (!genpd_virt_devs[i]) |
| 732 | 732 | continue; |
| @@ -748,29 +748,37 @@ static int _set_required_opps(struct device *dev, · 15 unmodified lines |
| 748 | 748 | * @dev: device for which we do this operation |
| 749 | 749 | * @target_freq: frequency to achieve |
| 750 | 750 | * |
| 751 | | − * This configures the power-supplies and clock source to the levels specified |
| 752 | | − * by the OPP corresponding to the target_freq. |
| 751 | + * This configures the power-supplies to the levels specified by the OPP |
| 752 | + * corresponding to the target_freq, and programs the clock to a value <= |
| 753 | + * target_freq, as rounded by clk_round_rate(). Device wanting to run at fmax |
| 754 | + * provided by the opp, should have already rounded to the target OPP's |
| 755 | + * frequency. |
| 753 | 756 | */ |
| 754 | 757 | int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq) |
| 755 | 758 | { |
| 756 | 759 | struct opp_table *opp_table; |
| 757 | | − unsigned long freq, old_freq; |
| 760 | + unsigned long freq, old_freq, temp_freq; |
| 758 | 761 | struct dev_pm_opp *old_opp, *opp; |
| 759 | 762 | struct clk *clk; |
| 760 | 763 | int ret; |
| 761 | 764 | |
| 762 | | − if (unlikely(!target_freq)) { |
| 763 | | − dev_err(dev, "%s: Invalid target frequency %lu\n", __func__, |
| 764 | | − target_freq); |
| 765 | | − return -EINVAL; |
| 766 | | − } |
| 767 | | − |
| 768 | 765 | opp_table = _find_opp_table(dev); |
| 769 | 766 | if (IS_ERR(opp_table)) { |
| 770 | 767 | dev_err(dev, "%s: device opp doesn't exist\n", __func__); |
| 771 | 768 | return PTR_ERR(opp_table); |
| 772 | 769 | } |
| 773 | 770 | |
| 771 | + if (unlikely(!target_freq)) { |
| 772 | + if (opp_table->required_opp_tables) { |
| 773 | + ret = _set_required_opps(dev, opp_table, NULL); |
| 774 | + } else { |
| 775 | + dev_err(dev, "target frequency can't be 0\n"); |
| 776 | + ret = -EINVAL; |
| 777 | + } |
| 778 | + |
| 779 | + goto put_opp_table; |
| 780 | + } |
| 781 | + |
| 774 | 782 | clk = opp_table->clk; |
| 775 | 783 | if (IS_ERR(clk)) { |
| 776 | 784 | dev_err(dev, "%s: No clock available for the device\n", |
| @@ -793,13 +801,15 @@ int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq) · 16 unmodified lines |
| 793 | 801 | goto put_opp_table; |
| 794 | 802 | } |
| 795 | 803 | |
| 796 | | − old_opp = _find_freq_ceil(opp_table, &old_freq); |
| 804 | + temp_freq = old_freq; |
| 805 | + old_opp = _find_freq_ceil(opp_table, &temp_freq); |
| 797 | 806 | if (IS_ERR(old_opp)) { |
| 798 | 807 | dev_err(dev, "%s: failed to find current OPP for freq %lu (%ld)\n", |
| 799 | 808 | __func__, old_freq, PTR_ERR(old_opp)); |
| 800 | 809 | } |
| 801 | 810 | |
| 802 | | − opp = _find_freq_ceil(opp_table, &freq); |
| 811 | + temp_freq = freq; |
| 812 | + opp = _find_freq_ceil(opp_table, &temp_freq); |
| 803 | 813 | if (IS_ERR(opp)) { |
| 804 | 814 | ret = PTR_ERR(opp); |
| 805 | 815 | dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n", |
| @@ -1741,91 +1751,137 @@ void dev_pm_opp_unregister_set_opp_helper(struct opp_table *opp_table) · 935 unmodified lines |
| 1741 | 1751 | } |
| 1742 | 1752 | EXPORT_SYMBOL_GPL(dev_pm_opp_unregister_set_opp_helper); |
| 1743 | 1753 | |
| 1754 | +static void _opp_detach_genpd(struct opp_table *opp_table) |
| 1755 | +{ |
| 1756 | + int index; |
| 1757 | + |
| 1758 | + for (index = 0; index < opp_table->required_opp_count; index++) { |
| 1759 | + if (!opp_table->genpd_virt_devs[index]) |
| 1760 | + continue; |
| 1761 | + |
| 1762 | + dev_pm_domain_detach(opp_table->genpd_virt_devs[index], false); |
| 1763 | + opp_table->genpd_virt_devs[index] = NULL; |
| 1764 | + } |
| 1765 | + |
| 1766 | + kfree(opp_table->genpd_virt_devs); |
| 1767 | + opp_table->genpd_virt_devs = NULL; |
| 1768 | +} |
| 1769 | + |
| 1744 | 1770 | /** |
| 1745 | | − * dev_pm_opp_set_genpd_virt_dev - Set virtual genpd device for an index |
| 1746 | | − * @dev: Consumer device for which the genpd device is getting set. |
| 1747 | | − * @virt_dev: virtual genpd device. |
| 1748 | | − * @index: index. |
| 1771 | + * dev_pm_opp_attach_genpd - Attach genpd(s) for the device and save virtual device pointer |
| 1772 | + * @dev: Consumer device for which the genpd is getting attached. |
| 1773 | + * @names: Null terminated array of pointers containing names of genpd to attach. |
| 1749 | 1774 | * |
| 1750 | 1775 | * Multiple generic power domains for a device are supported with the help of |
| 1751 | 1776 | * virtual genpd devices, which are created for each consumer device - genpd |
| 1752 | 1777 | * pair. These are the device structures which are attached to the power domain |
| 1753 | 1778 | * and are required by the OPP core to set the performance state of the genpd. |
| 1779 | + * The same API also works for the case where single genpd is available and so |
| 1780 | + * we don't need to support that separately. |
| 1754 | 1781 | * |
| 1755 | 1782 | * This helper will normally be called by the consumer driver of the device |
| 1756 | | − * "dev", as only that has details of the genpd devices. |
| 1783 | + * "dev", as only that has details of the genpd names. |
| 1757 | 1784 | * |
| 1758 | | − * This helper needs to be called once for each of those virtual devices, but |
| 1759 | | − * only if multiple domains are available for a device. Otherwise the original |
| 1760 | | − * device structure will be used instead by the OPP core. |
| 1785 | + * This helper needs to be called once with a list of all genpd to attach. |
| 1786 | + * Otherwise the original device structure will be used instead by the OPP core. |
| 1761 | 1787 | */ |
| 1762 | | −struct opp_table *dev_pm_opp_set_genpd_virt_dev(struct device *dev, |
| 1763 | | − struct device *virt_dev, |
| 1764 | | − int index) |
| 1788 | +struct opp_table *dev_pm_opp_attach_genpd(struct device *dev, const char **names) |
| 1765 | 1789 | { |
| 1766 | 1790 | struct opp_table *opp_table; |
| 1791 | + struct device *virt_dev; |
| 1792 | + int index, ret = -EINVAL; |
| 1793 | + const char **name = names; |
| 1767 | 1794 | |
| 1768 | 1795 | opp_table = dev_pm_opp_get_opp_table(dev); |
| 1769 | 1796 | if (!opp_table) |
| 1770 | 1797 | return ERR_PTR(-ENOMEM); |
| 1771 | 1798 | |
| 1799 | + /* |
| 1800 | + * If the genpd's OPP table isn't already initialized, parsing of the |
| 1801 | + * required-opps fail for dev. We should retry this after genpd's OPP |
| 1802 | + * table is added. |
| 1803 | + */ |
| 1804 | + if (!opp_table->required_opp_count) { |
| 1805 | + ret = -EPROBE_DEFER; |
| 1806 | + goto put_table; |
| 1807 | + } |
| 1808 | + |
| 1772 | 1809 | mutex_lock(&opp_table->genpd_virt_dev_lock); |
| 1773 | 1810 | |
| 1774 | | − if (unlikely(!opp_table->genpd_virt_devs || |
| 1775 | | − index >= opp_table->required_opp_count || |
| 1776 | | − opp_table->genpd_virt_devs[index])) { |
| 1811 | + opp_table->genpd_virt_devs = kcalloc(opp_table->required_opp_count, |
| 1812 | + sizeof(*opp_table->genpd_virt_devs), |
| 1813 | + GFP_KERNEL); |
| 1814 | + if (!opp_table->genpd_virt_devs) |
| 1815 | + goto unlock; |
| 1777 | 1816 | |
| 1778 | | − dev_err(dev, "Invalid request to set required device\n"); |
| 1779 | | − dev_pm_opp_put_opp_table(opp_table); |
| 1780 | | − mutex_unlock(&opp_table->genpd_virt_dev_lock); |
| 1817 | + while (*name) { |
| 1818 | + index = of_property_match_string(dev->of_node, |
| 1819 | + "power-domain-names", *name); |
| 1820 | + if (index < 0) { |
| 1821 | + dev_err(dev, "Failed to find power domain: %s (%d)\n", |
| 1822 | + *name, index); |
| 1823 | + goto err; |
| 1824 | + } |
| 1781 | 1825 | |
| 1782 | | − return ERR_PTR(-EINVAL); |
| 1826 | + if (index >= opp_table->required_opp_count) { |
| 1827 | + dev_err(dev, "Index can't be greater than required-opp-count - 1, %s (%d : %d)\n", |
| 1828 | + *name, opp_table->required_opp_count, index); |
| 1829 | + goto err; |
| 1830 | + } |
| 1831 | + |
| 1832 | + if (opp_table->genpd_virt_devs[index]) { |
| 1833 | + dev_err(dev, "Genpd virtual device already set %s\n", |
| 1834 | + *name); |
| 1835 | + goto err; |
| 1836 | + } |
| 1837 | + |
| 1838 | + virt_dev = dev_pm_domain_attach_by_name(dev, *name); |
| 1839 | + if (IS_ERR(virt_dev)) { |
| 1840 | + ret = PTR_ERR(virt_dev); |
| 1841 | + dev_err(dev, "Couldn't attach to pm_domain: %d\n", ret); |
| 1842 | + goto err; |
| 1843 | + } |
| 1844 | + |
| 1845 | + opp_table->genpd_virt_devs[index] = virt_dev; |
| 1846 | + name++; |
| 1783 | 1847 | } |
| 1784 | 1848 | |
| 1785 | | − opp_table->genpd_virt_devs[index] = virt_dev; |
| 1786 | 1849 | mutex_unlock(&opp_table->genpd_virt_dev_lock); |
| 1787 | 1850 | |
| 1788 | 1851 | return opp_table; |
| 1852 | + |
| 1853 | +err: |
| 1854 | + _opp_detach_genpd(opp_table); |
| 1855 | +unlock: |
| 1856 | + mutex_unlock(&opp_table->genpd_virt_dev_lock); |
| 1857 | + |
| 1858 | +put_table: |
| 1859 | + dev_pm_opp_put_opp_table(opp_table); |
| 1860 | + |
| 1861 | + return ERR_PTR(ret); |
| 1789 | 1862 | } |
| 1863 | +EXPORT_SYMBOL_GPL(dev_pm_opp_attach_genpd); |
| 1790 | 1864 | |
| 1791 | 1865 | /** |
| 1792 | | − * dev_pm_opp_put_genpd_virt_dev() - Releases resources blocked for genpd device. |
| 1793 | | − * @opp_table: OPP table returned by dev_pm_opp_set_genpd_virt_dev(). |
| 1794 | | − * @virt_dev: virtual genpd device. |
| 1866 | + * dev_pm_opp_detach_genpd() - Detach genpd(s) from the device. |
| 1867 | + * @opp_table: OPP table returned by dev_pm_opp_attach_genpd(). |
| 1795 | 1868 | * |
| 1796 | | − * This releases the resource previously acquired with a call to |
| 1797 | | − * dev_pm_opp_set_genpd_virt_dev(). The consumer driver shall call this helper |
| 1798 | | − * if it doesn't want OPP core to update performance state of a power domain |
| 1799 | | − * anymore. |
| 1869 | + * This detaches the genpd(s), resets the virtual device pointers, and puts the |
| 1870 | + * OPP table. |
| 1800 | 1871 | */ |
| 1801 | | −void dev_pm_opp_put_genpd_virt_dev(struct opp_table *opp_table, |
| 1802 | | − struct device *virt_dev) |
| 1872 | +void dev_pm_opp_detach_genpd(struct opp_table *opp_table) |
| 1803 | 1873 | { |
| 1804 | | − int i; |
| 1805 | | − |
| 1806 | 1874 | /* |
| 1807 | 1875 | * Acquire genpd_virt_dev_lock to make sure virt_dev isn't getting |
| 1808 | 1876 | * used in parallel. |
| 1809 | 1877 | */ |
| 1810 | 1878 | mutex_lock(&opp_table->genpd_virt_dev_lock); |
| 1811 | | − |
| 1812 | | − for (i = 0; i < opp_table->required_opp_count; i++) { |
| 1813 | | − if (opp_table->genpd_virt_devs[i] != virt_dev) |
| 1814 | | − continue; |
| 1815 | | − |
| 1816 | | − opp_table->genpd_virt_devs[i] = NULL; |
| 1817 | | − dev_pm_opp_put_opp_table(opp_table); |
| 1818 | | − |
| 1819 | | − /* Drop the vote */ |
| 1820 | | − dev_pm_genpd_set_performance_state(virt_dev, 0); |
| 1821 | | − break; |
| 1822 | | − } |
| 1823 | | − |
| 1879 | + _opp_detach_genpd(opp_table); |
| 1824 | 1880 | mutex_unlock(&opp_table->genpd_virt_dev_lock); |
| 1825 | 1881 | |
| 1826 | | − if (unlikely(i == opp_table->required_opp_count)) |
| 1827 | | − dev_err(virt_dev, "Failed to find required device entry\n"); |
| 1882 | + dev_pm_opp_put_opp_table(opp_table); |
| 1828 | 1883 | } |
| 1884 | +EXPORT_SYMBOL_GPL(dev_pm_opp_detach_genpd); |
| 1829 | 1885 | |
| 1830 | 1886 | /** |
| 1831 | 1887 | * dev_pm_opp_xlate_performance_state() - Find required OPP's pstate for src_table. |