1 From 111139c943a082364fbbcd9e0cc94cd442481340 Mon Sep 17 00:00:00 2001
2 From: Stephen Boyd <sboyd@codeaurora.org>
3 Date: Mon, 1 Jun 2015 18:47:56 -0700
4 Subject: PM / OPP: Support adjusting OPP voltages at runtime
6 On some SoCs the Adaptive Voltage Scaling (AVS) technique is
7 employed to optimize the operating voltage of a device. At a
8 given frequency, the hardware monitors dynamic factors and either
9 makes a suggestion for how much to adjust a voltage for the
10 current frequency, or it automatically adjusts the voltage
11 without software intervention. Add an API to the OPP library for
12 the former case, so that AVS type devices can update the voltages
13 for an OPP when the hardware determines the voltage should
14 change. The assumption is that drivers like CPUfreq or devfreq
15 will register for the OPP notifiers and adjust the voltage
16 according to suggestions that AVS makes.
18 Cc: Nishanth Menon <nm@ti.com>
19 Cc: Viresh Kumar <viresh.kumar@linaro.org>
20 Signed-off-by: Stephen Boyd <sboyd@codeaurora.org>
21 Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
23 drivers/base/power/opp/core.c | 77 +++++++++++++++++++++++++++++++++++++++++++
24 include/linux/pm_opp.h | 10 ++++++
25 2 files changed, 87 insertions(+)
27 --- a/drivers/base/power/opp/core.c
28 +++ b/drivers/base/power/opp/core.c
29 @@ -1039,6 +1039,83 @@ unlock:
33 + * dev_pm_opp_adjust_voltage() - helper to change the voltage of an opp
34 + * @dev: device for which we do this operation
35 + * @freq: OPP frequency to adjust voltage of
36 + * @u_volt: new OPP voltage
38 + * Change the voltage of an OPP with an RCU operation.
40 + * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
41 + * copy operation, returns 0 if no modifcation was done OR modification was
44 + * Locking: The internal device_opp and opp structures are RCU protected.
45 + * Hence this function internally uses RCU updater strategy with mutex locks to
46 + * keep the integrity of the internal data structures. Callers should ensure
47 + * that this function is *NOT* called under RCU protection or in contexts where
48 + * mutex locking or synchronize_rcu() blocking calls cannot be used.
50 +int dev_pm_opp_adjust_voltage(struct device *dev, unsigned long freq,
51 + unsigned long u_volt)
53 + struct device_opp *dev_opp;
54 + struct dev_pm_opp *new_opp, *tmp_opp, *opp = ERR_PTR(-ENODEV);
57 + /* keep the node allocated */
58 + new_opp = kmalloc(sizeof(*new_opp), GFP_KERNEL);
62 + mutex_lock(&dev_opp_list_lock);
64 + /* Find the device_opp */
65 + dev_opp = _find_device_opp(dev);
66 + if (IS_ERR(dev_opp)) {
67 + r = PTR_ERR(dev_opp);
68 + dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
72 + /* Do we have the frequency? */
73 + list_for_each_entry(tmp_opp, &dev_opp->opp_list, node) {
74 + if (tmp_opp->rate == freq) {
84 + /* Is update really needed? */
85 + if (opp->u_volt == u_volt)
87 + /* copy the old data over */
90 + /* plug in new node */
91 + new_opp->u_volt = u_volt;
93 + list_replace_rcu(&opp->node, &new_opp->node);
94 + mutex_unlock(&dev_opp_list_lock);
95 + call_srcu(&dev_opp->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu);
97 + /* Notify the change of the OPP */
98 + srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_ADJUST_VOLTAGE,
104 + mutex_unlock(&dev_opp_list_lock);
110 * dev_pm_opp_enable() - Enable a specific OPP
111 * @dev: device for which we do this operation
112 * @freq: OPP frequency to enable
113 --- a/include/linux/pm_opp.h
114 +++ b/include/linux/pm_opp.h
115 @@ -22,6 +22,7 @@ struct device;
117 enum dev_pm_opp_event {
118 OPP_EVENT_ADD, OPP_EVENT_REMOVE, OPP_EVENT_ENABLE, OPP_EVENT_DISABLE,
119 + OPP_EVENT_ADJUST_VOLTAGE,
122 #if defined(CONFIG_PM_OPP)
123 @@ -50,6 +51,9 @@ int dev_pm_opp_add(struct device *dev, u
124 unsigned long u_volt);
125 void dev_pm_opp_remove(struct device *dev, unsigned long freq);
127 +int dev_pm_opp_adjust_voltage(struct device *dev, unsigned long freq,
128 + unsigned long u_volt);
130 int dev_pm_opp_enable(struct device *dev, unsigned long freq);
132 int dev_pm_opp_disable(struct device *dev, unsigned long freq);
133 @@ -114,6 +118,12 @@ static inline void dev_pm_opp_remove(str
137 +static inline int dev_pm_opp_adjust_voltage(struct device *dev,
138 + unsigned long freq, unsigned long u_volt)
143 static inline int dev_pm_opp_enable(struct device *dev, unsigned long freq)