blob: 3a9789388bfb947980c35f540c3cf86d51ec4b83 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Nishanth Menone1f60b22010-10-13 00:13:10 +02002/*
3 * Generic OPP Interface
4 *
5 * Copyright (C) 2009-2010 Texas Instruments Incorporated.
6 * Nishanth Menon
7 * Romit Dasgupta
8 * Kevin Hilman
Nishanth Menone1f60b22010-10-13 00:13:10 +02009 */
10
Viresh Kumard6d2a522015-10-17 09:45:18 +053011#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
Viresh Kumard54974c2016-02-09 10:30:38 +053013#include <linux/clk.h>
Nishanth Menone1f60b22010-10-13 00:13:10 +020014#include <linux/errno.h>
15#include <linux/err.h>
Nishanth Menone1f60b22010-10-13 00:13:10 +020016#include <linux/slab.h>
Paul Gortmaker51990e82012-01-22 11:23:42 -050017#include <linux/device.h>
Liam Girdwood80126ce2012-10-23 01:27:44 +020018#include <linux/export.h>
Viresh Kumar009acd12017-10-11 12:54:14 +053019#include <linux/pm_domain.h>
Viresh Kumar9f8ea962016-02-09 10:30:33 +053020#include <linux/regulator/consumer.h>
Nishanth Menone1f60b22010-10-13 00:13:10 +020021
Viresh Kumarf59d3ee2015-09-04 13:47:26 +053022#include "opp.h"
Nishanth Menone1f60b22010-10-13 00:13:10 +020023
24/*
Viresh Kumar2c2709d2016-02-16 14:17:53 +053025 * The root of the list of all opp-tables. All opp_table structures branch off
26 * from here, with each opp_table containing the list of opps it supports in
Nishanth Menone1f60b22010-10-13 00:13:10 +020027 * various states of availability.
28 */
Viresh Kumarf47b72a2016-05-05 16:20:33 +053029LIST_HEAD(opp_tables);
Nishanth Menone1f60b22010-10-13 00:13:10 +020030/* Lock to allow exclusive modification to the device and opp lists */
Viresh Kumar2c2709d2016-02-16 14:17:53 +053031DEFINE_MUTEX(opp_table_lock);
Nishanth Menone1f60b22010-10-13 00:13:10 +020032
Viresh Kumar2c2709d2016-02-16 14:17:53 +053033static struct opp_device *_find_opp_dev(const struct device *dev,
34 struct opp_table *opp_table)
Viresh Kumar06441652015-07-29 16:23:04 +053035{
Viresh Kumar2c2709d2016-02-16 14:17:53 +053036 struct opp_device *opp_dev;
Viresh Kumar06441652015-07-29 16:23:04 +053037
Viresh Kumar2c2709d2016-02-16 14:17:53 +053038 list_for_each_entry(opp_dev, &opp_table->dev_list, node)
39 if (opp_dev->dev == dev)
40 return opp_dev;
Viresh Kumar06441652015-07-29 16:23:04 +053041
42 return NULL;
43}
44
Wei Yongjun6ac42392017-02-06 14:29:55 +000045static struct opp_table *_find_opp_table_unlocked(struct device *dev)
Viresh Kumar5b650b32017-01-23 10:11:48 +053046{
47 struct opp_table *opp_table;
Viresh Kumar3d255692018-08-03 07:05:21 +053048 bool found;
Viresh Kumar5b650b32017-01-23 10:11:48 +053049
50 list_for_each_entry(opp_table, &opp_tables, node) {
Viresh Kumar3d255692018-08-03 07:05:21 +053051 mutex_lock(&opp_table->lock);
52 found = !!_find_opp_dev(dev, opp_table);
53 mutex_unlock(&opp_table->lock);
54
55 if (found) {
Viresh Kumar5b650b32017-01-23 10:11:48 +053056 _get_opp_table_kref(opp_table);
57
58 return opp_table;
59 }
60 }
61
62 return ERR_PTR(-ENODEV);
63}
64
Nishanth Menone1f60b22010-10-13 00:13:10 +020065/**
Viresh Kumar2c2709d2016-02-16 14:17:53 +053066 * _find_opp_table() - find opp_table struct using device pointer
67 * @dev: device pointer used to lookup OPP table
Nishanth Menone1f60b22010-10-13 00:13:10 +020068 *
Viresh Kumar052c6f12017-01-23 10:11:49 +053069 * Search OPP table for one containing matching device.
Nishanth Menone1f60b22010-10-13 00:13:10 +020070 *
Viresh Kumar2c2709d2016-02-16 14:17:53 +053071 * Return: pointer to 'struct opp_table' if found, otherwise -ENODEV or
Nishanth Menone1f60b22010-10-13 00:13:10 +020072 * -EINVAL based on type of error.
73 *
Viresh Kumar5b650b32017-01-23 10:11:48 +053074 * The callers must call dev_pm_opp_put_opp_table() after the table is used.
Nishanth Menone1f60b22010-10-13 00:13:10 +020075 */
Viresh Kumar2c2709d2016-02-16 14:17:53 +053076struct opp_table *_find_opp_table(struct device *dev)
Nishanth Menone1f60b22010-10-13 00:13:10 +020077{
Viresh Kumar2c2709d2016-02-16 14:17:53 +053078 struct opp_table *opp_table;
Nishanth Menone1f60b22010-10-13 00:13:10 +020079
Viresh Kumar50a3cb02015-08-12 15:59:39 +053080 if (IS_ERR_OR_NULL(dev)) {
Nishanth Menone1f60b22010-10-13 00:13:10 +020081 pr_err("%s: Invalid parameters\n", __func__);
82 return ERR_PTR(-EINVAL);
83 }
84
Viresh Kumar5b650b32017-01-23 10:11:48 +053085 mutex_lock(&opp_table_lock);
86 opp_table = _find_opp_table_unlocked(dev);
87 mutex_unlock(&opp_table_lock);
Nishanth Menone1f60b22010-10-13 00:13:10 +020088
Viresh Kumar5b650b32017-01-23 10:11:48 +053089 return opp_table;
Nishanth Menone1f60b22010-10-13 00:13:10 +020090}
91
92/**
Linus Torvaldsbaf51c42015-11-11 09:03:01 -080093 * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an opp
Nishanth Menone1f60b22010-10-13 00:13:10 +020094 * @opp: opp for which voltage has to be returned for
95 *
Nishanth Menon984f16c82014-12-24 11:22:57 -060096 * Return: voltage in micro volt corresponding to the opp, else
Nishanth Menone1f60b22010-10-13 00:13:10 +020097 * return 0
98 *
Viresh Kumardfbe4672016-12-01 16:28:19 +053099 * This is useful only for devices with single power supply.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200100 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500101unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200102{
Viresh Kumar052c6f12017-01-23 10:11:49 +0530103 if (IS_ERR_OR_NULL(opp)) {
Nishanth Menone1f60b22010-10-13 00:13:10 +0200104 pr_err("%s: Invalid parameters\n", __func__);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530105 return 0;
106 }
Nishanth Menone1f60b22010-10-13 00:13:10 +0200107
Viresh Kumar052c6f12017-01-23 10:11:49 +0530108 return opp->supplies[0].u_volt;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200109}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500110EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200111
112/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500113 * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp
Nishanth Menone1f60b22010-10-13 00:13:10 +0200114 * @opp: opp for which frequency has to be returned for
115 *
Nishanth Menon984f16c82014-12-24 11:22:57 -0600116 * Return: frequency in hertz corresponding to the opp, else
Nishanth Menone1f60b22010-10-13 00:13:10 +0200117 * return 0
Nishanth Menone1f60b22010-10-13 00:13:10 +0200118 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500119unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200120{
Viresh Kumar052c6f12017-01-23 10:11:49 +0530121 if (IS_ERR_OR_NULL(opp) || !opp->available) {
Nishanth Menone1f60b22010-10-13 00:13:10 +0200122 pr_err("%s: Invalid parameters\n", __func__);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530123 return 0;
124 }
Nishanth Menone1f60b22010-10-13 00:13:10 +0200125
Viresh Kumar052c6f12017-01-23 10:11:49 +0530126 return opp->rate;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200127}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500128EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200129
130/**
Rajendra Nayak5b93ac52019-01-10 09:32:02 +0530131 * dev_pm_opp_get_level() - Gets the level corresponding to an available opp
132 * @opp: opp for which level value has to be returned for
133 *
134 * Return: level read from device tree corresponding to the opp, else
135 * return 0.
136 */
137unsigned int dev_pm_opp_get_level(struct dev_pm_opp *opp)
138{
139 if (IS_ERR_OR_NULL(opp) || !opp->available) {
140 pr_err("%s: Invalid parameters\n", __func__);
141 return 0;
142 }
143
144 return opp->level;
145}
146EXPORT_SYMBOL_GPL(dev_pm_opp_get_level);
147
148/**
Bartlomiej Zolnierkiewicz19445b22015-07-09 17:43:35 +0200149 * dev_pm_opp_is_turbo() - Returns if opp is turbo OPP or not
150 * @opp: opp for which turbo mode is being verified
151 *
152 * Turbo OPPs are not for normal use, and can be enabled (under certain
153 * conditions) for short duration of times to finish high throughput work
154 * quickly. Running on them for longer times may overheat the chip.
155 *
156 * Return: true if opp is turbo opp, else false.
Bartlomiej Zolnierkiewicz19445b22015-07-09 17:43:35 +0200157 */
158bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp)
159{
Viresh Kumar052c6f12017-01-23 10:11:49 +0530160 if (IS_ERR_OR_NULL(opp) || !opp->available) {
Bartlomiej Zolnierkiewicz19445b22015-07-09 17:43:35 +0200161 pr_err("%s: Invalid parameters\n", __func__);
162 return false;
163 }
164
Viresh Kumar052c6f12017-01-23 10:11:49 +0530165 return opp->turbo;
Bartlomiej Zolnierkiewicz19445b22015-07-09 17:43:35 +0200166}
167EXPORT_SYMBOL_GPL(dev_pm_opp_is_turbo);
168
169/**
Viresh Kumar3ca9bb32015-07-29 16:23:03 +0530170 * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds
171 * @dev: device for which we do this operation
172 *
173 * Return: This function returns the max clock latency in nanoseconds.
Viresh Kumar3ca9bb32015-07-29 16:23:03 +0530174 */
175unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev)
176{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530177 struct opp_table *opp_table;
Viresh Kumar3ca9bb32015-07-29 16:23:03 +0530178 unsigned long clock_latency_ns;
179
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530180 opp_table = _find_opp_table(dev);
181 if (IS_ERR(opp_table))
Viresh Kumar5b650b32017-01-23 10:11:48 +0530182 return 0;
Viresh Kumar3ca9bb32015-07-29 16:23:03 +0530183
Viresh Kumar5b650b32017-01-23 10:11:48 +0530184 clock_latency_ns = opp_table->clock_latency_ns_max;
185
186 dev_pm_opp_put_opp_table(opp_table);
187
Viresh Kumar3ca9bb32015-07-29 16:23:03 +0530188 return clock_latency_ns;
189}
190EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency);
191
192/**
Viresh Kumar655c9df2016-02-09 10:30:35 +0530193 * dev_pm_opp_get_max_volt_latency() - Get max voltage latency in nanoseconds
194 * @dev: device for which we do this operation
195 *
196 * Return: This function returns the max voltage latency in nanoseconds.
Viresh Kumar655c9df2016-02-09 10:30:35 +0530197 */
198unsigned long dev_pm_opp_get_max_volt_latency(struct device *dev)
199{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530200 struct opp_table *opp_table;
Viresh Kumar655c9df2016-02-09 10:30:35 +0530201 struct dev_pm_opp *opp;
Viresh Kumar478256b2017-05-23 09:32:11 +0530202 struct regulator *reg;
Viresh Kumar655c9df2016-02-09 10:30:35 +0530203 unsigned long latency_ns = 0;
Viresh Kumardfbe4672016-12-01 16:28:19 +0530204 int ret, i, count;
205 struct {
206 unsigned long min;
207 unsigned long max;
208 } *uV;
209
Viresh Kumarcdd3e612017-01-23 10:11:51 +0530210 opp_table = _find_opp_table(dev);
211 if (IS_ERR(opp_table))
212 return 0;
213
Viresh Kumardfbe4672016-12-01 16:28:19 +0530214 /* Regulator may not be required for the device */
Viresh Kumar90e35772018-12-11 16:32:47 +0530215 if (!opp_table->regulators)
Viresh Kumarcdd3e612017-01-23 10:11:51 +0530216 goto put_opp_table;
Viresh Kumardfbe4672016-12-01 16:28:19 +0530217
Viresh Kumar90e35772018-12-11 16:32:47 +0530218 count = opp_table->regulator_count;
219
Viresh Kumardfbe4672016-12-01 16:28:19 +0530220 uV = kmalloc_array(count, sizeof(*uV), GFP_KERNEL);
221 if (!uV)
Viresh Kumar478256b2017-05-23 09:32:11 +0530222 goto put_opp_table;
Viresh Kumar655c9df2016-02-09 10:30:35 +0530223
Viresh Kumar052c6f12017-01-23 10:11:49 +0530224 mutex_lock(&opp_table->lock);
225
Viresh Kumardfbe4672016-12-01 16:28:19 +0530226 for (i = 0; i < count; i++) {
227 uV[i].min = ~0;
228 uV[i].max = 0;
Viresh Kumar655c9df2016-02-09 10:30:35 +0530229
Viresh Kumar052c6f12017-01-23 10:11:49 +0530230 list_for_each_entry(opp, &opp_table->opp_list, node) {
Viresh Kumardfbe4672016-12-01 16:28:19 +0530231 if (!opp->available)
232 continue;
233
234 if (opp->supplies[i].u_volt_min < uV[i].min)
235 uV[i].min = opp->supplies[i].u_volt_min;
236 if (opp->supplies[i].u_volt_max > uV[i].max)
237 uV[i].max = opp->supplies[i].u_volt_max;
238 }
Viresh Kumar655c9df2016-02-09 10:30:35 +0530239 }
240
Viresh Kumar052c6f12017-01-23 10:11:49 +0530241 mutex_unlock(&opp_table->lock);
Viresh Kumar655c9df2016-02-09 10:30:35 +0530242
243 /*
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530244 * The caller needs to ensure that opp_table (and hence the regulator)
Viresh Kumar655c9df2016-02-09 10:30:35 +0530245 * isn't freed, while we are executing this routine.
246 */
Andrzej Hajda8cc311162017-02-20 19:57:57 +0100247 for (i = 0; i < count; i++) {
Viresh Kumar478256b2017-05-23 09:32:11 +0530248 reg = opp_table->regulators[i];
Viresh Kumardfbe4672016-12-01 16:28:19 +0530249 ret = regulator_set_voltage_time(reg, uV[i].min, uV[i].max);
250 if (ret > 0)
251 latency_ns += ret * 1000;
252 }
253
Viresh Kumardfbe4672016-12-01 16:28:19 +0530254 kfree(uV);
Viresh Kumarcdd3e612017-01-23 10:11:51 +0530255put_opp_table:
256 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar655c9df2016-02-09 10:30:35 +0530257
258 return latency_ns;
259}
260EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_volt_latency);
261
262/**
Viresh Kumar21743442016-02-09 10:30:36 +0530263 * dev_pm_opp_get_max_transition_latency() - Get max transition latency in
264 * nanoseconds
265 * @dev: device for which we do this operation
266 *
267 * Return: This function returns the max transition latency, in nanoseconds, to
268 * switch from one OPP to other.
Viresh Kumar21743442016-02-09 10:30:36 +0530269 */
270unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev)
271{
272 return dev_pm_opp_get_max_volt_latency(dev) +
273 dev_pm_opp_get_max_clock_latency(dev);
274}
275EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_transition_latency);
276
277/**
Viresh Kumar3aa26a32017-01-02 14:41:02 +0530278 * dev_pm_opp_get_suspend_opp_freq() - Get frequency of suspend opp in Hz
Bartlomiej Zolnierkiewicz4eafbd12015-09-08 18:41:01 +0200279 * @dev: device for which we do this operation
280 *
Viresh Kumar3aa26a32017-01-02 14:41:02 +0530281 * Return: This function returns the frequency of the OPP marked as suspend_opp
282 * if one is available, else returns 0;
Bartlomiej Zolnierkiewicz4eafbd12015-09-08 18:41:01 +0200283 */
Viresh Kumar3aa26a32017-01-02 14:41:02 +0530284unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev)
Bartlomiej Zolnierkiewicz4eafbd12015-09-08 18:41:01 +0200285{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530286 struct opp_table *opp_table;
Viresh Kumar3aa26a32017-01-02 14:41:02 +0530287 unsigned long freq = 0;
Bartlomiej Zolnierkiewicz4eafbd12015-09-08 18:41:01 +0200288
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530289 opp_table = _find_opp_table(dev);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530290 if (IS_ERR(opp_table))
291 return 0;
Bartlomiej Zolnierkiewicz4eafbd12015-09-08 18:41:01 +0200292
Viresh Kumar5b650b32017-01-23 10:11:48 +0530293 if (opp_table->suspend_opp && opp_table->suspend_opp->available)
294 freq = dev_pm_opp_get_freq(opp_table->suspend_opp);
Viresh Kumar3aa26a32017-01-02 14:41:02 +0530295
Viresh Kumar5b650b32017-01-23 10:11:48 +0530296 dev_pm_opp_put_opp_table(opp_table);
297
Viresh Kumar3aa26a32017-01-02 14:41:02 +0530298 return freq;
Bartlomiej Zolnierkiewicz4eafbd12015-09-08 18:41:01 +0200299}
Viresh Kumar3aa26a32017-01-02 14:41:02 +0530300EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp_freq);
Bartlomiej Zolnierkiewicz4eafbd12015-09-08 18:41:01 +0200301
Viresh Kumara1e8c132018-04-06 14:35:45 +0530302int _get_opp_count(struct opp_table *opp_table)
303{
304 struct dev_pm_opp *opp;
305 int count = 0;
306
307 mutex_lock(&opp_table->lock);
308
309 list_for_each_entry(opp, &opp_table->opp_list, node) {
310 if (opp->available)
311 count++;
312 }
313
314 mutex_unlock(&opp_table->lock);
315
316 return count;
317}
318
Bartlomiej Zolnierkiewicz4eafbd12015-09-08 18:41:01 +0200319/**
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530320 * dev_pm_opp_get_opp_count() - Get number of opps available in the opp table
Nishanth Menone1f60b22010-10-13 00:13:10 +0200321 * @dev: device for which we do this operation
322 *
Nishanth Menon984f16c82014-12-24 11:22:57 -0600323 * Return: This function returns the number of available opps if there are any,
Nishanth Menone1f60b22010-10-13 00:13:10 +0200324 * else returns 0 if none or the corresponding error value.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200325 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500326int dev_pm_opp_get_opp_count(struct device *dev)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200327{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530328 struct opp_table *opp_table;
Viresh Kumara1e8c132018-04-06 14:35:45 +0530329 int count;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200330
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530331 opp_table = _find_opp_table(dev);
332 if (IS_ERR(opp_table)) {
333 count = PTR_ERR(opp_table);
Fabio Estevam035ed072017-09-29 14:39:49 -0300334 dev_dbg(dev, "%s: OPP table not found (%d)\n",
Dmitry Torokhovb4718c02014-12-16 15:09:38 -0800335 __func__, count);
Viresh Kumar09f662f2018-10-03 15:22:03 +0530336 return count;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200337 }
338
Viresh Kumara1e8c132018-04-06 14:35:45 +0530339 count = _get_opp_count(opp_table);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530340 dev_pm_opp_put_opp_table(opp_table);
341
Nishanth Menone1f60b22010-10-13 00:13:10 +0200342 return count;
343}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500344EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200345
346/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500347 * dev_pm_opp_find_freq_exact() - search for an exact frequency
Nishanth Menone1f60b22010-10-13 00:13:10 +0200348 * @dev: device for which we do this operation
349 * @freq: frequency to search for
Nishanth Menon7ae49612011-02-25 23:46:18 +0100350 * @available: true/false - match for available opp
Nishanth Menone1f60b22010-10-13 00:13:10 +0200351 *
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530352 * Return: Searches for exact match in the opp table and returns pointer to the
Nishanth Menon984f16c82014-12-24 11:22:57 -0600353 * matching opp if found, else returns ERR_PTR in case of error and should
354 * be handled using IS_ERR. Error return values can be:
Nishanth Menon07797262012-10-24 22:00:12 +0200355 * EINVAL: for bad pointer
356 * ERANGE: no match found for search
357 * ENODEV: if device not found in list of registered devices
Nishanth Menone1f60b22010-10-13 00:13:10 +0200358 *
359 * Note: available is a modifier for the search. if available=true, then the
360 * match is for exact matching frequency and is available in the stored OPP
361 * table. if false, the match is for exact frequency which is not available.
362 *
363 * This provides a mechanism to enable an opp which is not available currently
364 * or the opposite as well.
365 *
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530366 * The callers are required to call dev_pm_opp_put() for the returned OPP after
367 * use.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200368 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500369struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
370 unsigned long freq,
371 bool available)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200372{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530373 struct opp_table *opp_table;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500374 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200375
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530376 opp_table = _find_opp_table(dev);
377 if (IS_ERR(opp_table)) {
378 int r = PTR_ERR(opp_table);
379
380 dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200381 return ERR_PTR(r);
382 }
383
Viresh Kumar052c6f12017-01-23 10:11:49 +0530384 mutex_lock(&opp_table->lock);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530385
Viresh Kumar052c6f12017-01-23 10:11:49 +0530386 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
Nishanth Menone1f60b22010-10-13 00:13:10 +0200387 if (temp_opp->available == available &&
388 temp_opp->rate == freq) {
389 opp = temp_opp;
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530390
391 /* Increment the reference count of OPP */
392 dev_pm_opp_get(opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200393 break;
394 }
395 }
396
Viresh Kumar052c6f12017-01-23 10:11:49 +0530397 mutex_unlock(&opp_table->lock);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530398 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530399
Nishanth Menone1f60b22010-10-13 00:13:10 +0200400 return opp;
401}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500402EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200403
Jisheng Zhang067b7ce2016-07-25 14:11:16 +0800404static noinline struct dev_pm_opp *_find_freq_ceil(struct opp_table *opp_table,
405 unsigned long *freq)
406{
407 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
408
Viresh Kumar052c6f12017-01-23 10:11:49 +0530409 mutex_lock(&opp_table->lock);
410
411 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
Jisheng Zhang067b7ce2016-07-25 14:11:16 +0800412 if (temp_opp->available && temp_opp->rate >= *freq) {
413 opp = temp_opp;
414 *freq = opp->rate;
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530415
416 /* Increment the reference count of OPP */
417 dev_pm_opp_get(opp);
Jisheng Zhang067b7ce2016-07-25 14:11:16 +0800418 break;
419 }
420 }
421
Viresh Kumar052c6f12017-01-23 10:11:49 +0530422 mutex_unlock(&opp_table->lock);
423
Jisheng Zhang067b7ce2016-07-25 14:11:16 +0800424 return opp;
425}
426
Nishanth Menone1f60b22010-10-13 00:13:10 +0200427/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500428 * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
Nishanth Menone1f60b22010-10-13 00:13:10 +0200429 * @dev: device for which we do this operation
430 * @freq: Start frequency
431 *
432 * Search for the matching ceil *available* OPP from a starting freq
433 * for a device.
434 *
Nishanth Menon984f16c82014-12-24 11:22:57 -0600435 * Return: matching *opp and refreshes *freq accordingly, else returns
Nishanth Menon07797262012-10-24 22:00:12 +0200436 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
437 * values can be:
438 * EINVAL: for bad pointer
439 * ERANGE: no match found for search
440 * ENODEV: if device not found in list of registered devices
Nishanth Menone1f60b22010-10-13 00:13:10 +0200441 *
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530442 * The callers are required to call dev_pm_opp_put() for the returned OPP after
443 * use.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200444 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500445struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
446 unsigned long *freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200447{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530448 struct opp_table *opp_table;
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530449 struct dev_pm_opp *opp;
Dmitry Torokhovb02ded22014-12-16 15:09:36 -0800450
Nishanth Menone1f60b22010-10-13 00:13:10 +0200451 if (!dev || !freq) {
452 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
453 return ERR_PTR(-EINVAL);
454 }
455
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530456 opp_table = _find_opp_table(dev);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530457 if (IS_ERR(opp_table))
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530458 return ERR_CAST(opp_table);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530459
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530460 opp = _find_freq_ceil(opp_table, freq);
461
Viresh Kumar5b650b32017-01-23 10:11:48 +0530462 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530463
464 return opp;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200465}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500466EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200467
468/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500469 * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
Nishanth Menone1f60b22010-10-13 00:13:10 +0200470 * @dev: device for which we do this operation
471 * @freq: Start frequency
472 *
473 * Search for the matching floor *available* OPP from a starting freq
474 * for a device.
475 *
Nishanth Menon984f16c82014-12-24 11:22:57 -0600476 * Return: matching *opp and refreshes *freq accordingly, else returns
Nishanth Menon07797262012-10-24 22:00:12 +0200477 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
478 * values can be:
479 * EINVAL: for bad pointer
480 * ERANGE: no match found for search
481 * ENODEV: if device not found in list of registered devices
Nishanth Menone1f60b22010-10-13 00:13:10 +0200482 *
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530483 * The callers are required to call dev_pm_opp_put() for the returned OPP after
484 * use.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200485 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500486struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
487 unsigned long *freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200488{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530489 struct opp_table *opp_table;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500490 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200491
492 if (!dev || !freq) {
493 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
494 return ERR_PTR(-EINVAL);
495 }
496
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530497 opp_table = _find_opp_table(dev);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530498 if (IS_ERR(opp_table))
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530499 return ERR_CAST(opp_table);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530500
Viresh Kumar052c6f12017-01-23 10:11:49 +0530501 mutex_lock(&opp_table->lock);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200502
Viresh Kumar052c6f12017-01-23 10:11:49 +0530503 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
Nishanth Menone1f60b22010-10-13 00:13:10 +0200504 if (temp_opp->available) {
505 /* go to the next node, before choosing prev */
506 if (temp_opp->rate > *freq)
507 break;
508 else
509 opp = temp_opp;
510 }
511 }
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530512
513 /* Increment the reference count of OPP */
514 if (!IS_ERR(opp))
515 dev_pm_opp_get(opp);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530516 mutex_unlock(&opp_table->lock);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530517 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530518
Nishanth Menone1f60b22010-10-13 00:13:10 +0200519 if (!IS_ERR(opp))
520 *freq = opp->rate;
521
522 return opp;
523}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500524EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200525
Andrew-sh.Cheng2f36bde2019-03-29 14:46:10 +0800526/**
527 * dev_pm_opp_find_freq_ceil_by_volt() - Find OPP with highest frequency for
528 * target voltage.
529 * @dev: Device for which we do this operation.
530 * @u_volt: Target voltage.
531 *
532 * Search for OPP with highest (ceil) frequency and has voltage <= u_volt.
533 *
534 * Return: matching *opp, else returns ERR_PTR in case of error which should be
535 * handled using IS_ERR.
536 *
537 * Error return values can be:
538 * EINVAL: bad parameters
539 *
540 * The callers are required to call dev_pm_opp_put() for the returned OPP after
541 * use.
542 */
543struct dev_pm_opp *dev_pm_opp_find_freq_ceil_by_volt(struct device *dev,
544 unsigned long u_volt)
545{
546 struct opp_table *opp_table;
547 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
548
549 if (!dev || !u_volt) {
550 dev_err(dev, "%s: Invalid argument volt=%lu\n", __func__,
551 u_volt);
552 return ERR_PTR(-EINVAL);
553 }
554
555 opp_table = _find_opp_table(dev);
556 if (IS_ERR(opp_table))
557 return ERR_CAST(opp_table);
558
559 mutex_lock(&opp_table->lock);
560
561 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
562 if (temp_opp->available) {
563 if (temp_opp->supplies[0].u_volt > u_volt)
564 break;
565 opp = temp_opp;
566 }
567 }
568
569 /* Increment the reference count of OPP */
570 if (!IS_ERR(opp))
571 dev_pm_opp_get(opp);
572
573 mutex_unlock(&opp_table->lock);
574 dev_pm_opp_put_opp_table(opp_table);
575
576 return opp;
577}
578EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil_by_volt);
579
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530580static int _set_opp_voltage(struct device *dev, struct regulator *reg,
Viresh Kumarce317812016-12-01 16:28:18 +0530581 struct dev_pm_opp_supply *supply)
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530582{
583 int ret;
584
585 /* Regulator not available for device */
586 if (IS_ERR(reg)) {
587 dev_dbg(dev, "%s: regulator not available: %ld\n", __func__,
588 PTR_ERR(reg));
589 return 0;
590 }
591
Viresh Kumarce317812016-12-01 16:28:18 +0530592 dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__,
593 supply->u_volt_min, supply->u_volt, supply->u_volt_max);
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530594
Viresh Kumarce317812016-12-01 16:28:18 +0530595 ret = regulator_set_voltage_triplet(reg, supply->u_volt_min,
596 supply->u_volt, supply->u_volt_max);
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530597 if (ret)
598 dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n",
Viresh Kumarce317812016-12-01 16:28:18 +0530599 __func__, supply->u_volt_min, supply->u_volt,
600 supply->u_volt_max, ret);
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530601
602 return ret;
603}
604
Viresh Kumar285881b2019-01-31 13:53:25 +0530605static inline int _generic_set_opp_clk_only(struct device *dev, struct clk *clk,
606 unsigned long freq)
Viresh Kumar94735582016-12-01 16:28:20 +0530607{
608 int ret;
609
610 ret = clk_set_rate(clk, freq);
611 if (ret) {
612 dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
613 ret);
614 }
615
616 return ret;
617}
618
Viresh Kumarc74b32f2017-05-23 09:32:10 +0530619static int _generic_set_opp_regulator(const struct opp_table *opp_table,
620 struct device *dev,
621 unsigned long old_freq,
622 unsigned long freq,
623 struct dev_pm_opp_supply *old_supply,
624 struct dev_pm_opp_supply *new_supply)
Viresh Kumar94735582016-12-01 16:28:20 +0530625{
Viresh Kumarc74b32f2017-05-23 09:32:10 +0530626 struct regulator *reg = opp_table->regulators[0];
Viresh Kumar94735582016-12-01 16:28:20 +0530627 int ret;
628
629 /* This function only supports single regulator per device */
Viresh Kumarc74b32f2017-05-23 09:32:10 +0530630 if (WARN_ON(opp_table->regulator_count > 1)) {
Viresh Kumar94735582016-12-01 16:28:20 +0530631 dev_err(dev, "multiple regulators are not supported\n");
632 return -EINVAL;
633 }
634
635 /* Scaling up? Scale voltage before frequency */
Waldemar Rymarkiewiczc5c2a972018-06-14 15:56:08 +0200636 if (freq >= old_freq) {
Viresh Kumar94735582016-12-01 16:28:20 +0530637 ret = _set_opp_voltage(dev, reg, new_supply);
638 if (ret)
639 goto restore_voltage;
640 }
641
642 /* Change frequency */
Viresh Kumar285881b2019-01-31 13:53:25 +0530643 ret = _generic_set_opp_clk_only(dev, opp_table->clk, freq);
Viresh Kumar94735582016-12-01 16:28:20 +0530644 if (ret)
645 goto restore_voltage;
646
647 /* Scaling down? Scale voltage after frequency */
648 if (freq < old_freq) {
649 ret = _set_opp_voltage(dev, reg, new_supply);
650 if (ret)
651 goto restore_freq;
652 }
653
654 return 0;
655
656restore_freq:
Viresh Kumar285881b2019-01-31 13:53:25 +0530657 if (_generic_set_opp_clk_only(dev, opp_table->clk, old_freq))
Viresh Kumar94735582016-12-01 16:28:20 +0530658 dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n",
659 __func__, old_freq);
660restore_voltage:
661 /* This shouldn't harm even if the voltages weren't updated earlier */
Viresh Kumarc74b32f2017-05-23 09:32:10 +0530662 if (old_supply)
Viresh Kumar94735582016-12-01 16:28:20 +0530663 _set_opp_voltage(dev, reg, old_supply);
664
665 return ret;
666}
667
Viresh Kumar7e5359932018-06-12 14:47:03 +0530668static int _set_opp_custom(const struct opp_table *opp_table,
669 struct device *dev, unsigned long old_freq,
670 unsigned long freq,
671 struct dev_pm_opp_supply *old_supply,
672 struct dev_pm_opp_supply *new_supply)
673{
674 struct dev_pm_set_opp_data *data;
675 int size;
676
677 data = opp_table->set_opp_data;
678 data->regulators = opp_table->regulators;
679 data->regulator_count = opp_table->regulator_count;
680 data->clk = opp_table->clk;
681 data->dev = dev;
682
683 data->old_opp.rate = old_freq;
684 size = sizeof(*old_supply) * opp_table->regulator_count;
685 if (IS_ERR(old_supply))
686 memset(data->old_opp.supplies, 0, size);
687 else
688 memcpy(data->old_opp.supplies, old_supply, size);
689
690 data->new_opp.rate = freq;
691 memcpy(data->new_opp.supplies, new_supply, size);
692
693 return opp_table->set_opp(data);
694}
695
Viresh Kumarca1b5d72018-06-14 10:03:26 +0530696/* This is only called for PM domain for now */
697static int _set_required_opps(struct device *dev,
698 struct opp_table *opp_table,
699 struct dev_pm_opp *opp)
700{
701 struct opp_table **required_opp_tables = opp_table->required_opp_tables;
702 struct device **genpd_virt_devs = opp_table->genpd_virt_devs;
703 unsigned int pstate;
704 int i, ret = 0;
705
706 if (!required_opp_tables)
707 return 0;
708
709 /* Single genpd case */
710 if (!genpd_virt_devs) {
711 pstate = opp->required_opps[0]->pstate;
712 ret = dev_pm_genpd_set_performance_state(dev, pstate);
713 if (ret) {
714 dev_err(dev, "Failed to set performance state of %s: %d (%d)\n",
715 dev_name(dev), pstate, ret);
716 }
717 return ret;
718 }
719
720 /* Multiple genpd case */
721
722 /*
723 * Acquire genpd_virt_dev_lock to make sure we don't use a genpd_dev
724 * after it is freed from another thread.
725 */
726 mutex_lock(&opp_table->genpd_virt_dev_lock);
727
728 for (i = 0; i < opp_table->required_opp_count; i++) {
729 pstate = opp->required_opps[i]->pstate;
730
731 if (!genpd_virt_devs[i])
732 continue;
733
734 ret = dev_pm_genpd_set_performance_state(genpd_virt_devs[i], pstate);
735 if (ret) {
736 dev_err(dev, "Failed to set performance rate of %s: %d (%d)\n",
737 dev_name(genpd_virt_devs[i]), pstate, ret);
738 break;
739 }
740 }
741 mutex_unlock(&opp_table->genpd_virt_dev_lock);
742
743 return ret;
744}
745
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530746/**
747 * dev_pm_opp_set_rate() - Configure new OPP based on frequency
748 * @dev: device for which we do this operation
749 * @target_freq: frequency to achieve
750 *
751 * This configures the power-supplies and clock source to the levels specified
752 * by the OPP corresponding to the target_freq.
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530753 */
754int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq)
755{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530756 struct opp_table *opp_table;
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530757 unsigned long freq, old_freq;
Viresh Kumar94735582016-12-01 16:28:20 +0530758 struct dev_pm_opp *old_opp, *opp;
Viresh Kumar94735582016-12-01 16:28:20 +0530759 struct clk *clk;
Viresh Kumar7e5359932018-06-12 14:47:03 +0530760 int ret;
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530761
762 if (unlikely(!target_freq)) {
763 dev_err(dev, "%s: Invalid target frequency %lu\n", __func__,
764 target_freq);
765 return -EINVAL;
766 }
767
Viresh Kumar052c6f12017-01-23 10:11:49 +0530768 opp_table = _find_opp_table(dev);
769 if (IS_ERR(opp_table)) {
770 dev_err(dev, "%s: device opp doesn't exist\n", __func__);
771 return PTR_ERR(opp_table);
772 }
773
774 clk = opp_table->clk;
775 if (IS_ERR(clk)) {
776 dev_err(dev, "%s: No clock available for the device\n",
777 __func__);
778 ret = PTR_ERR(clk);
779 goto put_opp_table;
780 }
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530781
782 freq = clk_round_rate(clk, target_freq);
783 if ((long)freq <= 0)
784 freq = target_freq;
785
786 old_freq = clk_get_rate(clk);
787
788 /* Return early if nothing to do */
789 if (old_freq == freq) {
790 dev_dbg(dev, "%s: old/new frequencies (%lu Hz) are same, nothing to do\n",
791 __func__, freq);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530792 ret = 0;
793 goto put_opp_table;
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530794 }
795
Jisheng Zhang067b7ce2016-07-25 14:11:16 +0800796 old_opp = _find_freq_ceil(opp_table, &old_freq);
Arnd Bergmann4df27c92016-09-15 17:38:38 +0200797 if (IS_ERR(old_opp)) {
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530798 dev_err(dev, "%s: failed to find current OPP for freq %lu (%ld)\n",
799 __func__, old_freq, PTR_ERR(old_opp));
800 }
801
Jisheng Zhang067b7ce2016-07-25 14:11:16 +0800802 opp = _find_freq_ceil(opp_table, &freq);
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530803 if (IS_ERR(opp)) {
804 ret = PTR_ERR(opp);
805 dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n",
806 __func__, freq, ret);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530807 goto put_old_opp;
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530808 }
809
Viresh Kumar94735582016-12-01 16:28:20 +0530810 dev_dbg(dev, "%s: switching OPP: %lu Hz --> %lu Hz\n", __func__,
811 old_freq, freq);
Viresh Kumardfbe4672016-12-01 16:28:19 +0530812
Viresh Kumarca1b5d72018-06-14 10:03:26 +0530813 /* Scaling up? Configure required OPPs before frequency */
Viresh Kumarfaef0802019-03-12 10:27:18 +0530814 if (freq >= old_freq) {
Viresh Kumarca1b5d72018-06-14 10:03:26 +0530815 ret = _set_required_opps(dev, opp_table, opp);
816 if (ret)
817 goto put_opp;
818 }
819
Viresh Kumar7e5359932018-06-12 14:47:03 +0530820 if (opp_table->set_opp) {
821 ret = _set_opp_custom(opp_table, dev, old_freq, freq,
822 IS_ERR(old_opp) ? NULL : old_opp->supplies,
823 opp->supplies);
824 } else if (opp_table->regulators) {
Viresh Kumarc74b32f2017-05-23 09:32:10 +0530825 ret = _generic_set_opp_regulator(opp_table, dev, old_freq, freq,
826 IS_ERR(old_opp) ? NULL : old_opp->supplies,
827 opp->supplies);
828 } else {
Viresh Kumar7e5359932018-06-12 14:47:03 +0530829 /* Only frequency scaling */
Viresh Kumar285881b2019-01-31 13:53:25 +0530830 ret = _generic_set_opp_clk_only(dev, clk, freq);
Viresh Kumardfbe4672016-12-01 16:28:19 +0530831 }
832
Viresh Kumarca1b5d72018-06-14 10:03:26 +0530833 /* Scaling down? Configure required OPPs after frequency */
834 if (!ret && freq < old_freq) {
835 ret = _set_required_opps(dev, opp_table, opp);
836 if (ret)
837 dev_err(dev, "Failed to set required opps: %d\n", ret);
838 }
839
840put_opp:
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530841 dev_pm_opp_put(opp);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530842put_old_opp:
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530843 if (!IS_ERR(old_opp))
844 dev_pm_opp_put(old_opp);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530845put_opp_table:
Viresh Kumar5b650b32017-01-23 10:11:48 +0530846 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530847 return ret;
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530848}
849EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate);
850
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530851/* OPP-dev Helpers */
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530852static void _remove_opp_dev(struct opp_device *opp_dev,
853 struct opp_table *opp_table)
Viresh Kumar06441652015-07-29 16:23:04 +0530854{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530855 opp_debug_unregister(opp_dev, opp_table);
856 list_del(&opp_dev->node);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530857 kfree(opp_dev);
Viresh Kumar06441652015-07-29 16:23:04 +0530858}
859
Viresh Kumar283d55e2018-09-07 09:01:54 +0530860static struct opp_device *_add_opp_dev_unlocked(const struct device *dev,
861 struct opp_table *opp_table)
Viresh Kumar06441652015-07-29 16:23:04 +0530862{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530863 struct opp_device *opp_dev;
Viresh Kumar06441652015-07-29 16:23:04 +0530864
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530865 opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL);
866 if (!opp_dev)
Viresh Kumar06441652015-07-29 16:23:04 +0530867 return NULL;
868
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530869 /* Initialize opp-dev */
870 opp_dev->dev = dev;
Viresh Kumar3d255692018-08-03 07:05:21 +0530871
Viresh Kumar052c6f12017-01-23 10:11:49 +0530872 list_add(&opp_dev->node, &opp_table->dev_list);
Viresh Kumar06441652015-07-29 16:23:04 +0530873
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530874 /* Create debugfs entries for the opp_table */
Greg Kroah-Hartmana2dea4c2019-01-22 16:21:17 +0100875 opp_debug_register(opp_dev, opp_table);
Viresh Kumar283d55e2018-09-07 09:01:54 +0530876
877 return opp_dev;
878}
879
880struct opp_device *_add_opp_dev(const struct device *dev,
881 struct opp_table *opp_table)
882{
883 struct opp_device *opp_dev;
884
885 mutex_lock(&opp_table->lock);
886 opp_dev = _add_opp_dev_unlocked(dev, opp_table);
Viresh Kumar3d255692018-08-03 07:05:21 +0530887 mutex_unlock(&opp_table->lock);
Viresh Kumardeaa5142015-11-11 07:59:01 +0530888
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530889 return opp_dev;
Viresh Kumar06441652015-07-29 16:23:04 +0530890}
891
Viresh Kumareb7c87432018-09-05 16:17:14 +0530892static struct opp_table *_allocate_opp_table(struct device *dev, int index)
Viresh Kumar07cce742014-12-10 09:45:34 +0530893{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530894 struct opp_table *opp_table;
895 struct opp_device *opp_dev;
Viresh Kumard54974c2016-02-09 10:30:38 +0530896 int ret;
Viresh Kumar07cce742014-12-10 09:45:34 +0530897
898 /*
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530899 * Allocate a new OPP table. In the infrequent case where a new
Viresh Kumar07cce742014-12-10 09:45:34 +0530900 * device is needed to be added, we pay this penalty.
901 */
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530902 opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL);
903 if (!opp_table)
Viresh Kumar07cce742014-12-10 09:45:34 +0530904 return NULL;
905
Viresh Kumar3d255692018-08-03 07:05:21 +0530906 mutex_init(&opp_table->lock);
Viresh Kumar4f018bc2018-06-26 16:29:34 +0530907 mutex_init(&opp_table->genpd_virt_dev_lock);
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530908 INIT_LIST_HEAD(&opp_table->dev_list);
Viresh Kumar06441652015-07-29 16:23:04 +0530909
Viresh Kumar46f48ac2018-12-11 16:39:36 +0530910 /* Mark regulator count uninitialized */
911 opp_table->regulator_count = -1;
912
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530913 opp_dev = _add_opp_dev(dev, opp_table);
914 if (!opp_dev) {
915 kfree(opp_table);
Viresh Kumar06441652015-07-29 16:23:04 +0530916 return NULL;
917 }
918
Viresh Kumareb7c87432018-09-05 16:17:14 +0530919 _of_init_opp_table(opp_table, dev, index);
Viresh Kumar50f8cfb2016-02-09 10:30:37 +0530920
Viresh Kumard54974c2016-02-09 10:30:38 +0530921 /* Find clk for the device */
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530922 opp_table->clk = clk_get(dev, NULL);
923 if (IS_ERR(opp_table->clk)) {
924 ret = PTR_ERR(opp_table->clk);
Viresh Kumard54974c2016-02-09 10:30:38 +0530925 if (ret != -EPROBE_DEFER)
926 dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__,
927 ret);
928 }
929
Viresh Kumar052c6f12017-01-23 10:11:49 +0530930 BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head);
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530931 INIT_LIST_HEAD(&opp_table->opp_list);
Viresh Kumarf067a982017-01-23 10:11:42 +0530932 kref_init(&opp_table->kref);
Viresh Kumar07cce742014-12-10 09:45:34 +0530933
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530934 /* Secure the device table modification */
Viresh Kumar052c6f12017-01-23 10:11:49 +0530935 list_add(&opp_table->node, &opp_tables);
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530936 return opp_table;
Viresh Kumar07cce742014-12-10 09:45:34 +0530937}
938
Viresh Kumarf067a982017-01-23 10:11:42 +0530939void _get_opp_table_kref(struct opp_table *opp_table)
Viresh Kumar3bac42c2015-07-29 16:22:59 +0530940{
Viresh Kumarf067a982017-01-23 10:11:42 +0530941 kref_get(&opp_table->kref);
942}
943
Viresh Kumareb7c87432018-09-05 16:17:14 +0530944static struct opp_table *_opp_get_opp_table(struct device *dev, int index)
Viresh Kumarf067a982017-01-23 10:11:42 +0530945{
946 struct opp_table *opp_table;
947
948 /* Hold our table modification lock here */
949 mutex_lock(&opp_table_lock);
950
Viresh Kumar5b650b32017-01-23 10:11:48 +0530951 opp_table = _find_opp_table_unlocked(dev);
952 if (!IS_ERR(opp_table))
Viresh Kumarf067a982017-01-23 10:11:42 +0530953 goto unlock;
Viresh Kumarf067a982017-01-23 10:11:42 +0530954
Viresh Kumar283d55e2018-09-07 09:01:54 +0530955 opp_table = _managed_opp(dev, index);
956 if (opp_table) {
957 if (!_add_opp_dev_unlocked(dev, opp_table)) {
958 dev_pm_opp_put_opp_table(opp_table);
959 opp_table = NULL;
960 }
961 goto unlock;
962 }
963
Viresh Kumareb7c87432018-09-05 16:17:14 +0530964 opp_table = _allocate_opp_table(dev, index);
Viresh Kumarf067a982017-01-23 10:11:42 +0530965
966unlock:
967 mutex_unlock(&opp_table_lock);
968
969 return opp_table;
970}
Viresh Kumareb7c87432018-09-05 16:17:14 +0530971
972struct opp_table *dev_pm_opp_get_opp_table(struct device *dev)
973{
974 return _opp_get_opp_table(dev, 0);
975}
Viresh Kumarf067a982017-01-23 10:11:42 +0530976EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table);
977
Viresh Kumareb7c87432018-09-05 16:17:14 +0530978struct opp_table *dev_pm_opp_get_opp_table_indexed(struct device *dev,
979 int index)
980{
981 return _opp_get_opp_table(dev, index);
982}
983
Viresh Kumarb83c1892017-01-23 10:11:45 +0530984static void _opp_table_kref_release(struct kref *kref)
Viresh Kumarf067a982017-01-23 10:11:42 +0530985{
986 struct opp_table *opp_table = container_of(kref, struct opp_table, kref);
Viresh Kumarcdd6ed92018-09-12 12:35:19 +0530987 struct opp_device *opp_dev, *temp;
Viresh Kumar06441652015-07-29 16:23:04 +0530988
Viresh Kumar5d6d1062018-06-07 14:50:43 +0530989 _of_clear_opp_table(opp_table);
990
Viresh Kumard54974c2016-02-09 10:30:38 +0530991 /* Release clk */
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530992 if (!IS_ERR(opp_table->clk))
993 clk_put(opp_table->clk);
Viresh Kumard54974c2016-02-09 10:30:38 +0530994
Viresh Kumarcdd6ed92018-09-12 12:35:19 +0530995 WARN_ON(!list_empty(&opp_table->opp_list));
Viresh Kumar06441652015-07-29 16:23:04 +0530996
Viresh Kumarcdd6ed92018-09-12 12:35:19 +0530997 list_for_each_entry_safe(opp_dev, temp, &opp_table->dev_list, node) {
998 /*
999 * The OPP table is getting removed, drop the performance state
1000 * constraints.
1001 */
1002 if (opp_table->genpd_performance_state)
1003 dev_pm_genpd_set_performance_state((struct device *)(opp_dev->dev), 0);
Viresh Kumar06441652015-07-29 16:23:04 +05301004
Viresh Kumarcdd6ed92018-09-12 12:35:19 +05301005 _remove_opp_dev(opp_dev, opp_table);
1006 }
Viresh Kumar06441652015-07-29 16:23:04 +05301007
Viresh Kumar4f018bc2018-06-26 16:29:34 +05301008 mutex_destroy(&opp_table->genpd_virt_dev_lock);
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301009 mutex_destroy(&opp_table->lock);
Viresh Kumar052c6f12017-01-23 10:11:49 +05301010 list_del(&opp_table->node);
1011 kfree(opp_table);
Viresh Kumar3bac42c2015-07-29 16:22:59 +05301012
Viresh Kumarf067a982017-01-23 10:11:42 +05301013 mutex_unlock(&opp_table_lock);
1014}
1015
Viresh Kumard0e8ae62018-09-11 11:14:11 +05301016void _opp_remove_all_static(struct opp_table *opp_table)
1017{
1018 struct dev_pm_opp *opp, *tmp;
1019
1020 list_for_each_entry_safe(opp, tmp, &opp_table->opp_list, node) {
1021 if (!opp->dynamic)
1022 dev_pm_opp_put(opp);
1023 }
1024
1025 opp_table->parsed_static_opps = false;
1026}
1027
1028static void _opp_table_list_kref_release(struct kref *kref)
1029{
1030 struct opp_table *opp_table = container_of(kref, struct opp_table,
1031 list_kref);
1032
1033 _opp_remove_all_static(opp_table);
1034 mutex_unlock(&opp_table_lock);
1035}
1036
1037void _put_opp_list_kref(struct opp_table *opp_table)
1038{
1039 kref_put_mutex(&opp_table->list_kref, _opp_table_list_kref_release,
1040 &opp_table_lock);
1041}
1042
Viresh Kumarf067a982017-01-23 10:11:42 +05301043void dev_pm_opp_put_opp_table(struct opp_table *opp_table)
1044{
1045 kref_put_mutex(&opp_table->kref, _opp_table_kref_release,
1046 &opp_table_lock);
1047}
1048EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table);
1049
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301050void _opp_free(struct dev_pm_opp *opp)
Viresh Kumar969fceb2017-01-02 14:40:59 +05301051{
1052 kfree(opp);
Viresh Kumar969fceb2017-01-02 14:40:59 +05301053}
1054
Viresh Kumar1690d8b2019-01-04 15:14:33 +05301055static void _opp_kref_release(struct dev_pm_opp *opp,
1056 struct opp_table *opp_table)
Viresh Kumar129eec52014-11-27 08:54:06 +05301057{
1058 /*
1059 * Notify the changes in the availability of the operable
1060 * frequency/voltage list.
1061 */
Viresh Kumar052c6f12017-01-23 10:11:49 +05301062 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_REMOVE, opp);
Viresh Kumarda544b62018-06-07 14:50:43 +05301063 _of_opp_free_required_opps(opp_table, opp);
Viresh Kumardeaa5142015-11-11 07:59:01 +05301064 opp_debug_remove_one(opp);
Viresh Kumar052c6f12017-01-23 10:11:49 +05301065 list_del(&opp->node);
1066 kfree(opp);
Viresh Kumar1690d8b2019-01-04 15:14:33 +05301067}
Viresh Kumar129eec52014-11-27 08:54:06 +05301068
Viresh Kumar1690d8b2019-01-04 15:14:33 +05301069static void _opp_kref_release_unlocked(struct kref *kref)
1070{
1071 struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref);
1072 struct opp_table *opp_table = opp->opp_table;
1073
1074 _opp_kref_release(opp, opp_table);
1075}
1076
1077static void _opp_kref_release_locked(struct kref *kref)
1078{
1079 struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref);
1080 struct opp_table *opp_table = opp->opp_table;
1081
1082 _opp_kref_release(opp, opp_table);
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301083 mutex_unlock(&opp_table->lock);
Viresh Kumar129eec52014-11-27 08:54:06 +05301084}
1085
Viresh Kumara88bd2a2017-11-29 15:18:36 +05301086void dev_pm_opp_get(struct dev_pm_opp *opp)
Viresh Kumar8a31d9d92017-01-23 10:11:47 +05301087{
1088 kref_get(&opp->kref);
1089}
1090
Viresh Kumar70347642017-01-23 10:11:46 +05301091void dev_pm_opp_put(struct dev_pm_opp *opp)
1092{
Viresh Kumar1690d8b2019-01-04 15:14:33 +05301093 kref_put_mutex(&opp->kref, _opp_kref_release_locked,
1094 &opp->opp_table->lock);
Viresh Kumar70347642017-01-23 10:11:46 +05301095}
1096EXPORT_SYMBOL_GPL(dev_pm_opp_put);
1097
Viresh Kumar1690d8b2019-01-04 15:14:33 +05301098static void dev_pm_opp_put_unlocked(struct dev_pm_opp *opp)
1099{
1100 kref_put(&opp->kref, _opp_kref_release_unlocked);
1101}
1102
Viresh Kumar129eec52014-11-27 08:54:06 +05301103/**
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301104 * dev_pm_opp_remove() - Remove an OPP from OPP table
Viresh Kumar129eec52014-11-27 08:54:06 +05301105 * @dev: device for which we do this operation
1106 * @freq: OPP to remove with matching 'freq'
1107 *
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301108 * This function removes an opp from the opp table.
Viresh Kumar129eec52014-11-27 08:54:06 +05301109 */
1110void dev_pm_opp_remove(struct device *dev, unsigned long freq)
1111{
1112 struct dev_pm_opp *opp;
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301113 struct opp_table *opp_table;
Viresh Kumar129eec52014-11-27 08:54:06 +05301114 bool found = false;
1115
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301116 opp_table = _find_opp_table(dev);
1117 if (IS_ERR(opp_table))
Viresh Kumar5b650b32017-01-23 10:11:48 +05301118 return;
Viresh Kumar129eec52014-11-27 08:54:06 +05301119
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301120 mutex_lock(&opp_table->lock);
1121
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301122 list_for_each_entry(opp, &opp_table->opp_list, node) {
Viresh Kumar129eec52014-11-27 08:54:06 +05301123 if (opp->rate == freq) {
1124 found = true;
1125 break;
1126 }
1127 }
1128
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301129 mutex_unlock(&opp_table->lock);
1130
Viresh Kumar5b650b32017-01-23 10:11:48 +05301131 if (found) {
1132 dev_pm_opp_put(opp);
Viresh Kumar0ad8c622018-08-02 14:23:09 +05301133
1134 /* Drop the reference taken by dev_pm_opp_add() */
1135 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar5b650b32017-01-23 10:11:48 +05301136 } else {
Viresh Kumar129eec52014-11-27 08:54:06 +05301137 dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
1138 __func__, freq);
Viresh Kumar129eec52014-11-27 08:54:06 +05301139 }
1140
Viresh Kumar0ad8c622018-08-02 14:23:09 +05301141 /* Drop the reference taken by _find_opp_table() */
Viresh Kumar5b650b32017-01-23 10:11:48 +05301142 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar129eec52014-11-27 08:54:06 +05301143}
1144EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
1145
Viresh Kumar1690d8b2019-01-04 15:14:33 +05301146/**
1147 * dev_pm_opp_remove_all_dynamic() - Remove all dynamically created OPPs
1148 * @dev: device for which we do this operation
1149 *
1150 * This function removes all dynamically created OPPs from the opp table.
1151 */
1152void dev_pm_opp_remove_all_dynamic(struct device *dev)
1153{
1154 struct opp_table *opp_table;
1155 struct dev_pm_opp *opp, *temp;
1156 int count = 0;
1157
1158 opp_table = _find_opp_table(dev);
1159 if (IS_ERR(opp_table))
1160 return;
1161
1162 mutex_lock(&opp_table->lock);
1163 list_for_each_entry_safe(opp, temp, &opp_table->opp_list, node) {
1164 if (opp->dynamic) {
1165 dev_pm_opp_put_unlocked(opp);
1166 count++;
1167 }
1168 }
1169 mutex_unlock(&opp_table->lock);
1170
1171 /* Drop the references taken by dev_pm_opp_add() */
1172 while (count--)
1173 dev_pm_opp_put_opp_table(opp_table);
1174
1175 /* Drop the reference taken by _find_opp_table() */
1176 dev_pm_opp_put_opp_table(opp_table);
1177}
1178EXPORT_SYMBOL_GPL(dev_pm_opp_remove_all_dynamic);
1179
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301180struct dev_pm_opp *_opp_allocate(struct opp_table *table)
Viresh Kumar23dacf62015-07-29 16:23:01 +05301181{
1182 struct dev_pm_opp *opp;
Viresh Kumardfbe4672016-12-01 16:28:19 +05301183 int count, supply_size;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301184
Viresh Kumardfbe4672016-12-01 16:28:19 +05301185 /* Allocate space for at least one supply */
Viresh Kumar46f48ac2018-12-11 16:39:36 +05301186 count = table->regulator_count > 0 ? table->regulator_count : 1;
Viresh Kumardfbe4672016-12-01 16:28:19 +05301187 supply_size = sizeof(*opp->supplies) * count;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301188
Viresh Kumardfbe4672016-12-01 16:28:19 +05301189 /* allocate new OPP node and supplies structures */
1190 opp = kzalloc(sizeof(*opp) + supply_size, GFP_KERNEL);
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301191 if (!opp)
Viresh Kumar23dacf62015-07-29 16:23:01 +05301192 return NULL;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301193
Viresh Kumardfbe4672016-12-01 16:28:19 +05301194 /* Put the supplies at the end of the OPP structure as an empty array */
1195 opp->supplies = (struct dev_pm_opp_supply *)(opp + 1);
1196 INIT_LIST_HEAD(&opp->node);
1197
Viresh Kumar23dacf62015-07-29 16:23:01 +05301198 return opp;
1199}
1200
Viresh Kumar7d34d562016-02-09 10:30:34 +05301201static bool _opp_supported_by_regulators(struct dev_pm_opp *opp,
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301202 struct opp_table *opp_table)
Viresh Kumar7d34d562016-02-09 10:30:34 +05301203{
Viresh Kumardfbe4672016-12-01 16:28:19 +05301204 struct regulator *reg;
1205 int i;
Viresh Kumar7d34d562016-02-09 10:30:34 +05301206
Viresh Kumar90e35772018-12-11 16:32:47 +05301207 if (!opp_table->regulators)
1208 return true;
1209
Viresh Kumardfbe4672016-12-01 16:28:19 +05301210 for (i = 0; i < opp_table->regulator_count; i++) {
1211 reg = opp_table->regulators[i];
1212
1213 if (!regulator_is_supported_voltage(reg,
1214 opp->supplies[i].u_volt_min,
1215 opp->supplies[i].u_volt_max)) {
1216 pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n",
1217 __func__, opp->supplies[i].u_volt_min,
1218 opp->supplies[i].u_volt_max);
1219 return false;
1220 }
Viresh Kumar7d34d562016-02-09 10:30:34 +05301221 }
1222
1223 return true;
1224}
1225
Viresh Kumara1e8c132018-04-06 14:35:45 +05301226static int _opp_is_duplicate(struct device *dev, struct dev_pm_opp *new_opp,
1227 struct opp_table *opp_table,
1228 struct list_head **head)
1229{
1230 struct dev_pm_opp *opp;
1231
1232 /*
1233 * Insert new OPP in order of increasing frequency and discard if
1234 * already present.
1235 *
1236 * Need to use &opp_table->opp_list in the condition part of the 'for'
1237 * loop, don't replace it with head otherwise it will become an infinite
1238 * loop.
1239 */
1240 list_for_each_entry(opp, &opp_table->opp_list, node) {
1241 if (new_opp->rate > opp->rate) {
1242 *head = &opp->node;
1243 continue;
1244 }
1245
1246 if (new_opp->rate < opp->rate)
1247 return 0;
1248
1249 /* Duplicate OPPs */
1250 dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
1251 __func__, opp->rate, opp->supplies[0].u_volt,
1252 opp->available, new_opp->rate,
1253 new_opp->supplies[0].u_volt, new_opp->available);
1254
1255 /* Should we compare voltages for all regulators here ? */
1256 return opp->available &&
1257 new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST;
1258 }
1259
1260 return 0;
1261}
1262
Viresh Kumar7f8538e2017-01-02 14:40:55 +05301263/*
1264 * Returns:
1265 * 0: On success. And appropriate error message for duplicate OPPs.
1266 * -EBUSY: For OPP with same freq/volt and is available. The callers of
1267 * _opp_add() must return 0 if they receive -EBUSY from it. This is to make
1268 * sure we don't print error messages unnecessarily if different parts of
1269 * kernel try to initialize the OPP table.
1270 * -EEXIST: For OPP with same freq but different volt or is unavailable. This
1271 * should be considered an error by the callers of _opp_add().
1272 */
Viresh Kumarf47b72a2016-05-05 16:20:33 +05301273int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
Viresh Kumara1e8c132018-04-06 14:35:45 +05301274 struct opp_table *opp_table, bool rate_not_available)
Viresh Kumar23dacf62015-07-29 16:23:01 +05301275{
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301276 struct list_head *head;
Viresh Kumardeaa5142015-11-11 07:59:01 +05301277 int ret;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301278
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301279 mutex_lock(&opp_table->lock);
1280 head = &opp_table->opp_list;
1281
Viresh Kumara1e8c132018-04-06 14:35:45 +05301282 if (likely(!rate_not_available)) {
1283 ret = _opp_is_duplicate(dev, new_opp, opp_table, &head);
1284 if (ret) {
1285 mutex_unlock(&opp_table->lock);
1286 return ret;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301287 }
Viresh Kumar23dacf62015-07-29 16:23:01 +05301288 }
1289
Viresh Kumar052c6f12017-01-23 10:11:49 +05301290 list_add(&new_opp->node, head);
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301291 mutex_unlock(&opp_table->lock);
1292
1293 new_opp->opp_table = opp_table;
Viresh Kumar70347642017-01-23 10:11:46 +05301294 kref_init(&new_opp->kref);
Viresh Kumar23dacf62015-07-29 16:23:01 +05301295
Greg Kroah-Hartmana2dea4c2019-01-22 16:21:17 +01001296 opp_debug_create_one(new_opp, opp_table);
Viresh Kumardeaa5142015-11-11 07:59:01 +05301297
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301298 if (!_opp_supported_by_regulators(new_opp, opp_table)) {
Viresh Kumar7d34d562016-02-09 10:30:34 +05301299 new_opp->available = false;
1300 dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n",
1301 __func__, new_opp->rate);
1302 }
1303
Viresh Kumar23dacf62015-07-29 16:23:01 +05301304 return 0;
1305}
1306
Viresh Kumar737002b2015-07-29 16:22:58 +05301307/**
Viresh Kumarb64b9c3f2015-10-15 21:42:44 +05301308 * _opp_add_v1() - Allocate a OPP based on v1 bindings.
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301309 * @opp_table: OPP table
Nishanth Menone1f60b22010-10-13 00:13:10 +02001310 * @dev: device for which we do this operation
1311 * @freq: Frequency in Hz for this OPP
1312 * @u_volt: Voltage in uVolts for this OPP
1313 * @dynamic: Dynamically added OPPs.
1314 *
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301315 * This function adds an opp definition to the opp table and returns status.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001316 * The opp is made available by default and it can be controlled using
1317 * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
1318 *
Viresh Kumar8f8d37b2015-09-04 13:47:24 +05301319 * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table
1320 * and freed by dev_pm_opp_of_remove_table.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001321 *
Nishanth Menone1f60b22010-10-13 00:13:10 +02001322 * Return:
1323 * 0 On success OR
1324 * Duplicate OPPs (both freq and volt are same) and opp->available
1325 * -EEXIST Freq are same and volt are different OR
1326 * Duplicate OPPs (both freq and volt are same) and !opp->available
1327 * -ENOMEM Memory allocation failure
1328 */
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301329int _opp_add_v1(struct opp_table *opp_table, struct device *dev,
1330 unsigned long freq, long u_volt, bool dynamic)
Nishanth Menone1f60b22010-10-13 00:13:10 +02001331{
Viresh Kumar23dacf62015-07-29 16:23:01 +05301332 struct dev_pm_opp *new_opp;
Viresh Kumar50f8cfb2016-02-09 10:30:37 +05301333 unsigned long tol;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001334 int ret;
1335
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301336 new_opp = _opp_allocate(opp_table);
1337 if (!new_opp)
1338 return -ENOMEM;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301339
Nishanth Menone1f60b22010-10-13 00:13:10 +02001340 /* populate the opp table */
1341 new_opp->rate = freq;
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301342 tol = u_volt * opp_table->voltage_tolerance_v1 / 100;
Viresh Kumardfbe4672016-12-01 16:28:19 +05301343 new_opp->supplies[0].u_volt = u_volt;
1344 new_opp->supplies[0].u_volt_min = u_volt - tol;
1345 new_opp->supplies[0].u_volt_max = u_volt + tol;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001346 new_opp->available = true;
Viresh Kumaraa5f2f82015-07-29 16:23:00 +05301347 new_opp->dynamic = dynamic;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301348
Viresh Kumara1e8c132018-04-06 14:35:45 +05301349 ret = _opp_add(dev, new_opp, opp_table, false);
Viresh Kumar7f8538e2017-01-02 14:40:55 +05301350 if (ret) {
1351 /* Don't return error for duplicate OPPs */
1352 if (ret == -EBUSY)
1353 ret = 0;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301354 goto free_opp;
Viresh Kumar7f8538e2017-01-02 14:40:55 +05301355 }
Viresh Kumar23dacf62015-07-29 16:23:01 +05301356
Nishanth Menone1f60b22010-10-13 00:13:10 +02001357 /*
1358 * Notify the changes in the availability of the operable
1359 * frequency/voltage list.
1360 */
Viresh Kumar052c6f12017-01-23 10:11:49 +05301361 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001362 return 0;
1363
1364free_opp:
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301365 _opp_free(new_opp);
1366
Nishanth Menone1f60b22010-10-13 00:13:10 +02001367 return ret;
1368}
Viresh Kumar383934092014-11-25 16:04:18 +05301369
Viresh Kumar27465902015-07-29 16:23:02 +05301370/**
Viresh Kumar7de36b02015-12-09 08:01:46 +05301371 * dev_pm_opp_set_supported_hw() - Set supported platforms
1372 * @dev: Device for which supported-hw has to be set.
1373 * @versions: Array of hierarchy of versions to match.
1374 * @count: Number of elements in the array.
1375 *
1376 * This is required only for the V2 bindings, and it enables a platform to
1377 * specify the hierarchy of versions it supports. OPP layer will then enable
1378 * OPPs, which are available for those versions, based on its 'opp-supported-hw'
1379 * property.
Viresh Kumar7de36b02015-12-09 08:01:46 +05301380 */
Viresh Kumarfa301842017-01-23 10:11:43 +05301381struct opp_table *dev_pm_opp_set_supported_hw(struct device *dev,
1382 const u32 *versions, unsigned int count)
Viresh Kumar7de36b02015-12-09 08:01:46 +05301383{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301384 struct opp_table *opp_table;
Viresh Kumar7de36b02015-12-09 08:01:46 +05301385
Viresh Kumarfa301842017-01-23 10:11:43 +05301386 opp_table = dev_pm_opp_get_opp_table(dev);
1387 if (!opp_table)
1388 return ERR_PTR(-ENOMEM);
Viresh Kumar7de36b02015-12-09 08:01:46 +05301389
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301390 /* Make sure there are no concurrent readers while updating opp_table */
1391 WARN_ON(!list_empty(&opp_table->opp_list));
Viresh Kumar7de36b02015-12-09 08:01:46 +05301392
Viresh Kumar25419de2018-05-22 16:38:08 +05301393 /* Another CPU that shares the OPP table has set the property ? */
1394 if (opp_table->supported_hw)
1395 return opp_table;
Viresh Kumar7de36b02015-12-09 08:01:46 +05301396
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301397 opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions),
Viresh Kumar7de36b02015-12-09 08:01:46 +05301398 GFP_KERNEL);
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301399 if (!opp_table->supported_hw) {
Viresh Kumar25419de2018-05-22 16:38:08 +05301400 dev_pm_opp_put_opp_table(opp_table);
1401 return ERR_PTR(-ENOMEM);
Viresh Kumar7de36b02015-12-09 08:01:46 +05301402 }
1403
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301404 opp_table->supported_hw_count = count;
Viresh Kumarfa301842017-01-23 10:11:43 +05301405
1406 return opp_table;
Viresh Kumar7de36b02015-12-09 08:01:46 +05301407}
1408EXPORT_SYMBOL_GPL(dev_pm_opp_set_supported_hw);
1409
1410/**
1411 * dev_pm_opp_put_supported_hw() - Releases resources blocked for supported hw
Viresh Kumarfa301842017-01-23 10:11:43 +05301412 * @opp_table: OPP table returned by dev_pm_opp_set_supported_hw().
Viresh Kumar7de36b02015-12-09 08:01:46 +05301413 *
1414 * This is required only for the V2 bindings, and is called for a matching
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301415 * dev_pm_opp_set_supported_hw(). Until this is called, the opp_table structure
Viresh Kumar7de36b02015-12-09 08:01:46 +05301416 * will not be freed.
Viresh Kumar7de36b02015-12-09 08:01:46 +05301417 */
Viresh Kumarfa301842017-01-23 10:11:43 +05301418void dev_pm_opp_put_supported_hw(struct opp_table *opp_table)
Viresh Kumar7de36b02015-12-09 08:01:46 +05301419{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301420 /* Make sure there are no concurrent readers while updating opp_table */
1421 WARN_ON(!list_empty(&opp_table->opp_list));
Viresh Kumar7de36b02015-12-09 08:01:46 +05301422
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301423 kfree(opp_table->supported_hw);
1424 opp_table->supported_hw = NULL;
1425 opp_table->supported_hw_count = 0;
Viresh Kumar7de36b02015-12-09 08:01:46 +05301426
Viresh Kumarfa301842017-01-23 10:11:43 +05301427 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar7de36b02015-12-09 08:01:46 +05301428}
1429EXPORT_SYMBOL_GPL(dev_pm_opp_put_supported_hw);
1430
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301431/**
1432 * dev_pm_opp_set_prop_name() - Set prop-extn name
Viresh Kumara5da6442016-02-16 14:17:52 +05301433 * @dev: Device for which the prop-name has to be set.
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301434 * @name: name to postfix to properties.
1435 *
1436 * This is required only for the V2 bindings, and it enables a platform to
1437 * specify the extn to be used for certain property names. The properties to
1438 * which the extension will apply are opp-microvolt and opp-microamp. OPP core
1439 * should postfix the property name with -<name> while looking for them.
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301440 */
Viresh Kumarfa301842017-01-23 10:11:43 +05301441struct opp_table *dev_pm_opp_set_prop_name(struct device *dev, const char *name)
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301442{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301443 struct opp_table *opp_table;
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301444
Viresh Kumarfa301842017-01-23 10:11:43 +05301445 opp_table = dev_pm_opp_get_opp_table(dev);
1446 if (!opp_table)
1447 return ERR_PTR(-ENOMEM);
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301448
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301449 /* Make sure there are no concurrent readers while updating opp_table */
1450 WARN_ON(!list_empty(&opp_table->opp_list));
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301451
Viresh Kumar878ec1a2018-05-22 16:38:08 +05301452 /* Another CPU that shares the OPP table has set the property ? */
1453 if (opp_table->prop_name)
1454 return opp_table;
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301455
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301456 opp_table->prop_name = kstrdup(name, GFP_KERNEL);
1457 if (!opp_table->prop_name) {
Viresh Kumar878ec1a2018-05-22 16:38:08 +05301458 dev_pm_opp_put_opp_table(opp_table);
1459 return ERR_PTR(-ENOMEM);
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301460 }
1461
Viresh Kumarfa301842017-01-23 10:11:43 +05301462 return opp_table;
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301463}
1464EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name);
1465
1466/**
1467 * dev_pm_opp_put_prop_name() - Releases resources blocked for prop-name
Viresh Kumarfa301842017-01-23 10:11:43 +05301468 * @opp_table: OPP table returned by dev_pm_opp_set_prop_name().
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301469 *
1470 * This is required only for the V2 bindings, and is called for a matching
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301471 * dev_pm_opp_set_prop_name(). Until this is called, the opp_table structure
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301472 * will not be freed.
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301473 */
Viresh Kumarfa301842017-01-23 10:11:43 +05301474void dev_pm_opp_put_prop_name(struct opp_table *opp_table)
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301475{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301476 /* Make sure there are no concurrent readers while updating opp_table */
1477 WARN_ON(!list_empty(&opp_table->opp_list));
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301478
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301479 kfree(opp_table->prop_name);
1480 opp_table->prop_name = NULL;
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301481
Viresh Kumarfa301842017-01-23 10:11:43 +05301482 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301483}
1484EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name);
1485
Viresh Kumar94735582016-12-01 16:28:20 +05301486static int _allocate_set_opp_data(struct opp_table *opp_table)
1487{
1488 struct dev_pm_set_opp_data *data;
1489 int len, count = opp_table->regulator_count;
1490
Viresh Kumar90e35772018-12-11 16:32:47 +05301491 if (WARN_ON(!opp_table->regulators))
Viresh Kumar94735582016-12-01 16:28:20 +05301492 return -EINVAL;
1493
1494 /* space for set_opp_data */
1495 len = sizeof(*data);
1496
1497 /* space for old_opp.supplies and new_opp.supplies */
1498 len += 2 * sizeof(struct dev_pm_opp_supply) * count;
1499
1500 data = kzalloc(len, GFP_KERNEL);
1501 if (!data)
1502 return -ENOMEM;
1503
1504 data->old_opp.supplies = (void *)(data + 1);
1505 data->new_opp.supplies = data->old_opp.supplies + count;
1506
1507 opp_table->set_opp_data = data;
1508
1509 return 0;
1510}
1511
1512static void _free_set_opp_data(struct opp_table *opp_table)
1513{
1514 kfree(opp_table->set_opp_data);
1515 opp_table->set_opp_data = NULL;
1516}
1517
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301518/**
Viresh Kumardfbe4672016-12-01 16:28:19 +05301519 * dev_pm_opp_set_regulators() - Set regulator names for the device
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301520 * @dev: Device for which regulator name is being set.
Viresh Kumardfbe4672016-12-01 16:28:19 +05301521 * @names: Array of pointers to the names of the regulator.
1522 * @count: Number of regulators.
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301523 *
1524 * In order to support OPP switching, OPP layer needs to know the name of the
Viresh Kumardfbe4672016-12-01 16:28:19 +05301525 * device's regulators, as the core would be required to switch voltages as
1526 * well.
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301527 *
1528 * This must be called before any OPPs are initialized for the device.
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301529 */
Viresh Kumardfbe4672016-12-01 16:28:19 +05301530struct opp_table *dev_pm_opp_set_regulators(struct device *dev,
1531 const char * const names[],
1532 unsigned int count)
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301533{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301534 struct opp_table *opp_table;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301535 struct regulator *reg;
Viresh Kumardfbe4672016-12-01 16:28:19 +05301536 int ret, i;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301537
Viresh Kumarfa301842017-01-23 10:11:43 +05301538 opp_table = dev_pm_opp_get_opp_table(dev);
1539 if (!opp_table)
1540 return ERR_PTR(-ENOMEM);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301541
1542 /* This should be called before OPPs are initialized */
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301543 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301544 ret = -EBUSY;
1545 goto err;
1546 }
1547
Viresh Kumar779b7832018-05-22 16:38:08 +05301548 /* Another CPU that shares the OPP table has set the regulators ? */
1549 if (opp_table->regulators)
1550 return opp_table;
Viresh Kumardfbe4672016-12-01 16:28:19 +05301551
1552 opp_table->regulators = kmalloc_array(count,
1553 sizeof(*opp_table->regulators),
1554 GFP_KERNEL);
1555 if (!opp_table->regulators) {
1556 ret = -ENOMEM;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301557 goto err;
1558 }
1559
Viresh Kumardfbe4672016-12-01 16:28:19 +05301560 for (i = 0; i < count; i++) {
1561 reg = regulator_get_optional(dev, names[i]);
1562 if (IS_ERR(reg)) {
1563 ret = PTR_ERR(reg);
1564 if (ret != -EPROBE_DEFER)
1565 dev_err(dev, "%s: no regulator (%s) found: %d\n",
1566 __func__, names[i], ret);
1567 goto free_regulators;
1568 }
1569
1570 opp_table->regulators[i] = reg;
1571 }
1572
1573 opp_table->regulator_count = count;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301574
Viresh Kumar94735582016-12-01 16:28:20 +05301575 /* Allocate block only once to pass to set_opp() routines */
1576 ret = _allocate_set_opp_data(opp_table);
1577 if (ret)
1578 goto free_regulators;
1579
Stephen Boyd91291d92016-11-30 16:21:25 +05301580 return opp_table;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301581
Viresh Kumardfbe4672016-12-01 16:28:19 +05301582free_regulators:
1583 while (i != 0)
1584 regulator_put(opp_table->regulators[--i]);
1585
1586 kfree(opp_table->regulators);
1587 opp_table->regulators = NULL;
Viresh Kumar46f48ac2018-12-11 16:39:36 +05301588 opp_table->regulator_count = -1;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301589err:
Viresh Kumarfa301842017-01-23 10:11:43 +05301590 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301591
Stephen Boyd91291d92016-11-30 16:21:25 +05301592 return ERR_PTR(ret);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301593}
Viresh Kumardfbe4672016-12-01 16:28:19 +05301594EXPORT_SYMBOL_GPL(dev_pm_opp_set_regulators);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301595
1596/**
Viresh Kumardfbe4672016-12-01 16:28:19 +05301597 * dev_pm_opp_put_regulators() - Releases resources blocked for regulator
1598 * @opp_table: OPP table returned from dev_pm_opp_set_regulators().
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301599 */
Viresh Kumardfbe4672016-12-01 16:28:19 +05301600void dev_pm_opp_put_regulators(struct opp_table *opp_table)
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301601{
Viresh Kumardfbe4672016-12-01 16:28:19 +05301602 int i;
1603
Viresh Kumar779b7832018-05-22 16:38:08 +05301604 if (!opp_table->regulators)
1605 goto put_opp_table;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301606
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301607 /* Make sure there are no concurrent readers while updating opp_table */
1608 WARN_ON(!list_empty(&opp_table->opp_list));
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301609
Viresh Kumardfbe4672016-12-01 16:28:19 +05301610 for (i = opp_table->regulator_count - 1; i >= 0; i--)
1611 regulator_put(opp_table->regulators[i]);
1612
Viresh Kumar94735582016-12-01 16:28:20 +05301613 _free_set_opp_data(opp_table);
1614
Viresh Kumardfbe4672016-12-01 16:28:19 +05301615 kfree(opp_table->regulators);
1616 opp_table->regulators = NULL;
Viresh Kumar46f48ac2018-12-11 16:39:36 +05301617 opp_table->regulator_count = -1;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301618
Viresh Kumar779b7832018-05-22 16:38:08 +05301619put_opp_table:
Viresh Kumarfa301842017-01-23 10:11:43 +05301620 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301621}
Viresh Kumardfbe4672016-12-01 16:28:19 +05301622EXPORT_SYMBOL_GPL(dev_pm_opp_put_regulators);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301623
Viresh Kumar383934092014-11-25 16:04:18 +05301624/**
Viresh Kumar829a4e82017-06-21 10:29:13 +05301625 * dev_pm_opp_set_clkname() - Set clk name for the device
1626 * @dev: Device for which clk name is being set.
1627 * @name: Clk name.
1628 *
1629 * In order to support OPP switching, OPP layer needs to get pointer to the
1630 * clock for the device. Simple cases work fine without using this routine (i.e.
1631 * by passing connection-id as NULL), but for a device with multiple clocks
1632 * available, the OPP core needs to know the exact name of the clk to use.
1633 *
1634 * This must be called before any OPPs are initialized for the device.
1635 */
1636struct opp_table *dev_pm_opp_set_clkname(struct device *dev, const char *name)
1637{
1638 struct opp_table *opp_table;
1639 int ret;
1640
1641 opp_table = dev_pm_opp_get_opp_table(dev);
1642 if (!opp_table)
1643 return ERR_PTR(-ENOMEM);
1644
1645 /* This should be called before OPPs are initialized */
1646 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
1647 ret = -EBUSY;
1648 goto err;
1649 }
1650
1651 /* Already have default clk set, free it */
1652 if (!IS_ERR(opp_table->clk))
1653 clk_put(opp_table->clk);
1654
1655 /* Find clk for the device */
1656 opp_table->clk = clk_get(dev, name);
1657 if (IS_ERR(opp_table->clk)) {
1658 ret = PTR_ERR(opp_table->clk);
1659 if (ret != -EPROBE_DEFER) {
1660 dev_err(dev, "%s: Couldn't find clock: %d\n", __func__,
1661 ret);
1662 }
1663 goto err;
1664 }
1665
1666 return opp_table;
1667
1668err:
1669 dev_pm_opp_put_opp_table(opp_table);
1670
1671 return ERR_PTR(ret);
1672}
1673EXPORT_SYMBOL_GPL(dev_pm_opp_set_clkname);
1674
1675/**
1676 * dev_pm_opp_put_clkname() - Releases resources blocked for clk.
1677 * @opp_table: OPP table returned from dev_pm_opp_set_clkname().
1678 */
1679void dev_pm_opp_put_clkname(struct opp_table *opp_table)
1680{
1681 /* Make sure there are no concurrent readers while updating opp_table */
1682 WARN_ON(!list_empty(&opp_table->opp_list));
1683
1684 clk_put(opp_table->clk);
1685 opp_table->clk = ERR_PTR(-EINVAL);
1686
1687 dev_pm_opp_put_opp_table(opp_table);
1688}
1689EXPORT_SYMBOL_GPL(dev_pm_opp_put_clkname);
1690
1691/**
Viresh Kumar4dab1602016-12-01 16:28:21 +05301692 * dev_pm_opp_register_set_opp_helper() - Register custom set OPP helper
1693 * @dev: Device for which the helper is getting registered.
1694 * @set_opp: Custom set OPP helper.
1695 *
1696 * This is useful to support complex platforms (like platforms with multiple
1697 * regulators per device), instead of the generic OPP set rate helper.
1698 *
1699 * This must be called before any OPPs are initialized for the device.
Viresh Kumar4dab1602016-12-01 16:28:21 +05301700 */
Viresh Kumarfa301842017-01-23 10:11:43 +05301701struct opp_table *dev_pm_opp_register_set_opp_helper(struct device *dev,
Viresh Kumar4dab1602016-12-01 16:28:21 +05301702 int (*set_opp)(struct dev_pm_set_opp_data *data))
1703{
1704 struct opp_table *opp_table;
Viresh Kumar4dab1602016-12-01 16:28:21 +05301705
1706 if (!set_opp)
Viresh Kumarfa301842017-01-23 10:11:43 +05301707 return ERR_PTR(-EINVAL);
Viresh Kumar4dab1602016-12-01 16:28:21 +05301708
Viresh Kumarfa301842017-01-23 10:11:43 +05301709 opp_table = dev_pm_opp_get_opp_table(dev);
1710 if (!opp_table)
1711 return ERR_PTR(-ENOMEM);
Viresh Kumar4dab1602016-12-01 16:28:21 +05301712
1713 /* This should be called before OPPs are initialized */
1714 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
Viresh Kumar5019acc2018-05-22 16:38:08 +05301715 dev_pm_opp_put_opp_table(opp_table);
1716 return ERR_PTR(-EBUSY);
Viresh Kumar4dab1602016-12-01 16:28:21 +05301717 }
1718
Viresh Kumar5019acc2018-05-22 16:38:08 +05301719 /* Another CPU that shares the OPP table has set the helper ? */
1720 if (!opp_table->set_opp)
1721 opp_table->set_opp = set_opp;
Viresh Kumar4dab1602016-12-01 16:28:21 +05301722
Viresh Kumarfa301842017-01-23 10:11:43 +05301723 return opp_table;
Viresh Kumar4dab1602016-12-01 16:28:21 +05301724}
1725EXPORT_SYMBOL_GPL(dev_pm_opp_register_set_opp_helper);
1726
1727/**
Viresh Kumar604a7ae2017-10-05 17:26:21 +05301728 * dev_pm_opp_unregister_set_opp_helper() - Releases resources blocked for
Viresh Kumar4dab1602016-12-01 16:28:21 +05301729 * set_opp helper
Viresh Kumarfa301842017-01-23 10:11:43 +05301730 * @opp_table: OPP table returned from dev_pm_opp_register_set_opp_helper().
Viresh Kumar4dab1602016-12-01 16:28:21 +05301731 *
Viresh Kumarfa301842017-01-23 10:11:43 +05301732 * Release resources blocked for platform specific set_opp helper.
Viresh Kumar4dab1602016-12-01 16:28:21 +05301733 */
Viresh Kumar604a7ae2017-10-05 17:26:21 +05301734void dev_pm_opp_unregister_set_opp_helper(struct opp_table *opp_table)
Viresh Kumar4dab1602016-12-01 16:28:21 +05301735{
Viresh Kumar4dab1602016-12-01 16:28:21 +05301736 /* Make sure there are no concurrent readers while updating opp_table */
1737 WARN_ON(!list_empty(&opp_table->opp_list));
1738
1739 opp_table->set_opp = NULL;
Viresh Kumarfa301842017-01-23 10:11:43 +05301740 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar4dab1602016-12-01 16:28:21 +05301741}
Viresh Kumar604a7ae2017-10-05 17:26:21 +05301742EXPORT_SYMBOL_GPL(dev_pm_opp_unregister_set_opp_helper);
Viresh Kumar4dab1602016-12-01 16:28:21 +05301743
1744/**
Viresh Kumar4f018bc2018-06-26 16:29:34 +05301745 * 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.
1749 *
1750 * Multiple generic power domains for a device are supported with the help of
1751 * virtual genpd devices, which are created for each consumer device - genpd
1752 * pair. These are the device structures which are attached to the power domain
1753 * and are required by the OPP core to set the performance state of the genpd.
1754 *
1755 * This helper will normally be called by the consumer driver of the device
1756 * "dev", as only that has details of the genpd devices.
1757 *
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.
1761 */
1762struct opp_table *dev_pm_opp_set_genpd_virt_dev(struct device *dev,
1763 struct device *virt_dev,
1764 int index)
1765{
1766 struct opp_table *opp_table;
1767
1768 opp_table = dev_pm_opp_get_opp_table(dev);
1769 if (!opp_table)
1770 return ERR_PTR(-ENOMEM);
1771
1772 mutex_lock(&opp_table->genpd_virt_dev_lock);
1773
1774 if (unlikely(!opp_table->genpd_virt_devs ||
1775 index >= opp_table->required_opp_count ||
1776 opp_table->genpd_virt_devs[index])) {
1777
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);
1781
1782 return ERR_PTR(-EINVAL);
1783 }
1784
1785 opp_table->genpd_virt_devs[index] = virt_dev;
1786 mutex_unlock(&opp_table->genpd_virt_dev_lock);
1787
1788 return opp_table;
1789}
1790
1791/**
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.
1795 *
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.
1800 */
1801void dev_pm_opp_put_genpd_virt_dev(struct opp_table *opp_table,
1802 struct device *virt_dev)
1803{
1804 int i;
1805
1806 /*
1807 * Acquire genpd_virt_dev_lock to make sure virt_dev isn't getting
1808 * used in parallel.
1809 */
1810 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
1824 mutex_unlock(&opp_table->genpd_virt_dev_lock);
1825
1826 if (unlikely(i == opp_table->required_opp_count))
1827 dev_err(virt_dev, "Failed to find required device entry\n");
1828}
1829
1830/**
Viresh Kumarc8a59102018-11-02 14:36:42 +05301831 * dev_pm_opp_xlate_performance_state() - Find required OPP's pstate for src_table.
1832 * @src_table: OPP table which has dst_table as one of its required OPP table.
1833 * @dst_table: Required OPP table of the src_table.
1834 * @pstate: Current performance state of the src_table.
1835 *
1836 * This Returns pstate of the OPP (present in @dst_table) pointed out by the
1837 * "required-opps" property of the OPP (present in @src_table) which has
1838 * performance state set to @pstate.
1839 *
1840 * Return: Zero or positive performance state on success, otherwise negative
1841 * value on errors.
1842 */
1843int dev_pm_opp_xlate_performance_state(struct opp_table *src_table,
1844 struct opp_table *dst_table,
1845 unsigned int pstate)
1846{
1847 struct dev_pm_opp *opp;
1848 int dest_pstate = -EINVAL;
1849 int i;
1850
1851 if (!pstate)
1852 return 0;
1853
1854 /*
1855 * Normally the src_table will have the "required_opps" property set to
1856 * point to one of the OPPs in the dst_table, but in some cases the
1857 * genpd and its master have one to one mapping of performance states
1858 * and so none of them have the "required-opps" property set. Return the
1859 * pstate of the src_table as it is in such cases.
1860 */
1861 if (!src_table->required_opp_count)
1862 return pstate;
1863
1864 for (i = 0; i < src_table->required_opp_count; i++) {
1865 if (src_table->required_opp_tables[i]->np == dst_table->np)
1866 break;
1867 }
1868
1869 if (unlikely(i == src_table->required_opp_count)) {
1870 pr_err("%s: Couldn't find matching OPP table (%p: %p)\n",
1871 __func__, src_table, dst_table);
1872 return -EINVAL;
1873 }
1874
1875 mutex_lock(&src_table->lock);
1876
1877 list_for_each_entry(opp, &src_table->opp_list, node) {
1878 if (opp->pstate == pstate) {
1879 dest_pstate = opp->required_opps[i]->pstate;
1880 goto unlock;
1881 }
1882 }
1883
1884 pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__, src_table,
1885 dst_table);
1886
1887unlock:
1888 mutex_unlock(&src_table->lock);
1889
1890 return dest_pstate;
1891}
1892
1893/**
Nishanth Menone1f60b22010-10-13 00:13:10 +02001894 * dev_pm_opp_add() - Add an OPP table from a table definitions
1895 * @dev: device for which we do this operation
Nishanth Menon47d43ba2013-09-19 16:03:51 -05001896 * @freq: Frequency in Hz for this OPP
Nishanth Menone1f60b22010-10-13 00:13:10 +02001897 * @u_volt: Voltage in uVolts for this OPP
1898 *
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301899 * This function adds an opp definition to the opp table and returns status.
Nishanth Menon47d43ba2013-09-19 16:03:51 -05001900 * The opp is made available by default and it can be controlled using
Nishanth Menone1f60b22010-10-13 00:13:10 +02001901 * dev_pm_opp_enable/disable functions.
1902 *
Viresh Kumara7470db2014-11-25 16:04:17 +05301903 * Return:
1904 * 0 On success OR
1905 * Duplicate OPPs (both freq and volt are same) and opp->available
1906 * -EEXIST Freq are same and volt are different OR
1907 * Duplicate OPPs (both freq and volt are same) and !opp->available
Viresh Kumar383934092014-11-25 16:04:18 +05301908 * -ENOMEM Memory allocation failure
Viresh Kumara7470db2014-11-25 16:04:17 +05301909 */
Nishanth Menone1f60b22010-10-13 00:13:10 +02001910int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
1911{
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301912 struct opp_table *opp_table;
1913 int ret;
1914
Viresh Kumarb83c1892017-01-23 10:11:45 +05301915 opp_table = dev_pm_opp_get_opp_table(dev);
1916 if (!opp_table)
1917 return -ENOMEM;
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301918
Viresh Kumar46f48ac2018-12-11 16:39:36 +05301919 /* Fix regulator count for dynamic OPPs */
1920 opp_table->regulator_count = 1;
1921
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301922 ret = _opp_add_v1(opp_table, dev, freq, u_volt, true);
Viresh Kumar0ad8c622018-08-02 14:23:09 +05301923 if (ret)
1924 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301925
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301926 return ret;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001927}
1928EXPORT_SYMBOL_GPL(dev_pm_opp_add);
1929
Nishanth Menone1f60b22010-10-13 00:13:10 +02001930/**
Nishanth Menon327854c2014-12-24 11:22:56 -06001931 * _opp_set_availability() - helper to set the availability of an opp
Nishanth Menone1f60b22010-10-13 00:13:10 +02001932 * @dev: device for which we do this operation
1933 * @freq: OPP frequency to modify availability
1934 * @availability_req: availability status requested for this opp
1935 *
Viresh Kumar052c6f12017-01-23 10:11:49 +05301936 * Set the availability of an OPP, opp_{enable,disable} share a common logic
1937 * which is isolated here.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001938 *
Nishanth Menon984f16c82014-12-24 11:22:57 -06001939 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
Stephen Boyde1a2d492015-09-24 12:28:44 -07001940 * copy operation, returns 0 if no modification was done OR modification was
Nishanth Menone1f60b22010-10-13 00:13:10 +02001941 * successful.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001942 */
Nishanth Menon327854c2014-12-24 11:22:56 -06001943static int _opp_set_availability(struct device *dev, unsigned long freq,
1944 bool availability_req)
Nishanth Menone1f60b22010-10-13 00:13:10 +02001945{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301946 struct opp_table *opp_table;
Viresh Kumara7f39872017-01-23 10:11:50 +05301947 struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001948 int r = 0;
1949
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301950 /* Find the opp_table */
1951 opp_table = _find_opp_table(dev);
1952 if (IS_ERR(opp_table)) {
1953 r = PTR_ERR(opp_table);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001954 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
Viresh Kumara7f39872017-01-23 10:11:50 +05301955 return r;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001956 }
1957
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301958 mutex_lock(&opp_table->lock);
1959
Nishanth Menone1f60b22010-10-13 00:13:10 +02001960 /* Do we have the frequency? */
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301961 list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
Nishanth Menone1f60b22010-10-13 00:13:10 +02001962 if (tmp_opp->rate == freq) {
1963 opp = tmp_opp;
1964 break;
1965 }
1966 }
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301967
Nishanth Menone1f60b22010-10-13 00:13:10 +02001968 if (IS_ERR(opp)) {
1969 r = PTR_ERR(opp);
1970 goto unlock;
1971 }
1972
1973 /* Is update really needed? */
1974 if (opp->available == availability_req)
1975 goto unlock;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001976
Viresh Kumara7f39872017-01-23 10:11:50 +05301977 opp->available = availability_req;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001978
Viresh Kumare4d8ae02017-09-21 10:44:36 -07001979 dev_pm_opp_get(opp);
1980 mutex_unlock(&opp_table->lock);
1981
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001982 /* Notify the change of the OPP availability */
1983 if (availability_req)
Viresh Kumar052c6f12017-01-23 10:11:49 +05301984 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE,
Viresh Kumara7f39872017-01-23 10:11:50 +05301985 opp);
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001986 else
Viresh Kumar052c6f12017-01-23 10:11:49 +05301987 blocking_notifier_call_chain(&opp_table->head,
Viresh Kumara7f39872017-01-23 10:11:50 +05301988 OPP_EVENT_DISABLE, opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001989
Viresh Kumare4d8ae02017-09-21 10:44:36 -07001990 dev_pm_opp_put(opp);
1991 goto put_table;
1992
Nishanth Menone1f60b22010-10-13 00:13:10 +02001993unlock:
Viresh Kumar5b650b32017-01-23 10:11:48 +05301994 mutex_unlock(&opp_table->lock);
Viresh Kumare4d8ae02017-09-21 10:44:36 -07001995put_table:
Viresh Kumar5b650b32017-01-23 10:11:48 +05301996 dev_pm_opp_put_opp_table(opp_table);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001997 return r;
1998}
1999
2000/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -05002001 * dev_pm_opp_enable() - Enable a specific OPP
Nishanth Menone1f60b22010-10-13 00:13:10 +02002002 * @dev: device for which we do this operation
2003 * @freq: OPP frequency to enable
2004 *
2005 * Enables a provided opp. If the operation is valid, this returns 0, else the
2006 * corresponding error value. It is meant to be used for users an OPP available
Nishanth Menon5d4879c2013-09-19 16:03:50 -05002007 * after being temporarily made unavailable with dev_pm_opp_disable.
Nishanth Menone1f60b22010-10-13 00:13:10 +02002008 *
Nishanth Menon984f16c82014-12-24 11:22:57 -06002009 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
Stephen Boyde1a2d492015-09-24 12:28:44 -07002010 * copy operation, returns 0 if no modification was done OR modification was
Nishanth Menon984f16c82014-12-24 11:22:57 -06002011 * successful.
Nishanth Menone1f60b22010-10-13 00:13:10 +02002012 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -05002013int dev_pm_opp_enable(struct device *dev, unsigned long freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +02002014{
Nishanth Menon327854c2014-12-24 11:22:56 -06002015 return _opp_set_availability(dev, freq, true);
Nishanth Menone1f60b22010-10-13 00:13:10 +02002016}
Nishanth Menon5d4879c2013-09-19 16:03:50 -05002017EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
Nishanth Menone1f60b22010-10-13 00:13:10 +02002018
2019/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -05002020 * dev_pm_opp_disable() - Disable a specific OPP
Nishanth Menone1f60b22010-10-13 00:13:10 +02002021 * @dev: device for which we do this operation
2022 * @freq: OPP frequency to disable
2023 *
2024 * Disables a provided opp. If the operation is valid, this returns
2025 * 0, else the corresponding error value. It is meant to be a temporary
2026 * control by users to make this OPP not available until the circumstances are
Nishanth Menon5d4879c2013-09-19 16:03:50 -05002027 * right to make it available again (with a call to dev_pm_opp_enable).
Nishanth Menone1f60b22010-10-13 00:13:10 +02002028 *
Nishanth Menon984f16c82014-12-24 11:22:57 -06002029 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
Stephen Boyde1a2d492015-09-24 12:28:44 -07002030 * copy operation, returns 0 if no modification was done OR modification was
Nishanth Menon984f16c82014-12-24 11:22:57 -06002031 * successful.
Nishanth Menone1f60b22010-10-13 00:13:10 +02002032 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -05002033int dev_pm_opp_disable(struct device *dev, unsigned long freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +02002034{
Nishanth Menon327854c2014-12-24 11:22:56 -06002035 return _opp_set_availability(dev, freq, false);
Nishanth Menone1f60b22010-10-13 00:13:10 +02002036}
Nishanth Menon5d4879c2013-09-19 16:03:50 -05002037EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
Nishanth Menone1f60b22010-10-13 00:13:10 +02002038
MyungJoo Ham03ca3702011-09-30 22:35:12 +02002039/**
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05302040 * dev_pm_opp_register_notifier() - Register OPP notifier for the device
2041 * @dev: Device for which notifier needs to be registered
2042 * @nb: Notifier block to be registered
Nishanth Menon984f16c82014-12-24 11:22:57 -06002043 *
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05302044 * Return: 0 on success or a negative error value.
MyungJoo Ham03ca3702011-09-30 22:35:12 +02002045 */
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05302046int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb)
MyungJoo Ham03ca3702011-09-30 22:35:12 +02002047{
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05302048 struct opp_table *opp_table;
2049 int ret;
MyungJoo Ham03ca3702011-09-30 22:35:12 +02002050
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05302051 opp_table = _find_opp_table(dev);
Viresh Kumar5b650b32017-01-23 10:11:48 +05302052 if (IS_ERR(opp_table))
2053 return PTR_ERR(opp_table);
2054
Viresh Kumar052c6f12017-01-23 10:11:49 +05302055 ret = blocking_notifier_chain_register(&opp_table->head, nb);
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05302056
Viresh Kumar5b650b32017-01-23 10:11:48 +05302057 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05302058
2059 return ret;
MyungJoo Ham03ca3702011-09-30 22:35:12 +02002060}
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05302061EXPORT_SYMBOL(dev_pm_opp_register_notifier);
2062
2063/**
2064 * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device
2065 * @dev: Device for which notifier needs to be unregistered
2066 * @nb: Notifier block to be unregistered
2067 *
2068 * Return: 0 on success or a negative error value.
2069 */
2070int dev_pm_opp_unregister_notifier(struct device *dev,
2071 struct notifier_block *nb)
2072{
2073 struct opp_table *opp_table;
2074 int ret;
2075
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05302076 opp_table = _find_opp_table(dev);
Viresh Kumar5b650b32017-01-23 10:11:48 +05302077 if (IS_ERR(opp_table))
2078 return PTR_ERR(opp_table);
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05302079
Viresh Kumar052c6f12017-01-23 10:11:49 +05302080 ret = blocking_notifier_chain_unregister(&opp_table->head, nb);
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05302081
Viresh Kumar5b650b32017-01-23 10:11:48 +05302082 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05302083
2084 return ret;
2085}
2086EXPORT_SYMBOL(dev_pm_opp_unregister_notifier);
Shawn Guob496dfb2012-09-05 01:09:12 +02002087
Viresh Kumar2a4eb732018-09-13 13:14:36 +05302088void _dev_pm_opp_find_and_remove_table(struct device *dev)
Viresh Kumar737002b2015-07-29 16:22:58 +05302089{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05302090 struct opp_table *opp_table;
Viresh Kumar737002b2015-07-29 16:22:58 +05302091
Viresh Kumar2c2709d2016-02-16 14:17:53 +05302092 /* Check for existing table for 'dev' */
2093 opp_table = _find_opp_table(dev);
2094 if (IS_ERR(opp_table)) {
2095 int error = PTR_ERR(opp_table);
Viresh Kumar737002b2015-07-29 16:22:58 +05302096
2097 if (error != -ENODEV)
Viresh Kumar2c2709d2016-02-16 14:17:53 +05302098 WARN(1, "%s: opp_table: %d\n",
Viresh Kumar737002b2015-07-29 16:22:58 +05302099 IS_ERR_OR_NULL(dev) ?
2100 "Invalid device" : dev_name(dev),
2101 error);
Viresh Kumar5b650b32017-01-23 10:11:48 +05302102 return;
Viresh Kumar737002b2015-07-29 16:22:58 +05302103 }
2104
Viresh Kumarcdd6ed92018-09-12 12:35:19 +05302105 _put_opp_list_kref(opp_table);
Viresh Kumar737002b2015-07-29 16:22:58 +05302106
Viresh Kumarcdd6ed92018-09-12 12:35:19 +05302107 /* Drop reference taken by _find_opp_table() */
2108 dev_pm_opp_put_opp_table(opp_table);
2109
2110 /* Drop reference taken while the OPP table was added */
Viresh Kumar5b650b32017-01-23 10:11:48 +05302111 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar737002b2015-07-29 16:22:58 +05302112}
Viresh Kumar129eec52014-11-27 08:54:06 +05302113
2114/**
Sudeep Holla411466c2016-05-03 15:05:04 +01002115 * dev_pm_opp_remove_table() - Free all OPPs associated with the device
Viresh Kumar2c2709d2016-02-16 14:17:53 +05302116 * @dev: device pointer used to lookup OPP table.
Viresh Kumar129eec52014-11-27 08:54:06 +05302117 *
Sudeep Holla411466c2016-05-03 15:05:04 +01002118 * Free both OPPs created using static entries present in DT and the
2119 * dynamically added entries.
Viresh Kumar129eec52014-11-27 08:54:06 +05302120 */
Sudeep Holla411466c2016-05-03 15:05:04 +01002121void dev_pm_opp_remove_table(struct device *dev)
Viresh Kumar129eec52014-11-27 08:54:06 +05302122{
Viresh Kumar2a4eb732018-09-13 13:14:36 +05302123 _dev_pm_opp_find_and_remove_table(dev);
Viresh Kumar8d4d4e92015-06-12 17:10:38 +05302124}
Sudeep Holla411466c2016-05-03 15:05:04 +01002125EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table);