blob: 481affb783f369a48fa5b0ffbe547d731d671719 [file] [log] [blame]
Nishanth Menone1f60b22010-10-13 00:13:10 +02001/*
2 * Generic OPP Interface
3 *
4 * Copyright (C) 2009-2010 Texas Instruments Incorporated.
5 * Nishanth Menon
6 * Romit Dasgupta
7 * Kevin Hilman
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
Viresh Kumard6d2a522015-10-17 09:45:18 +053014#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
Viresh Kumard54974c2016-02-09 10:30:38 +053016#include <linux/clk.h>
Nishanth Menone1f60b22010-10-13 00:13:10 +020017#include <linux/errno.h>
18#include <linux/err.h>
Nishanth Menone1f60b22010-10-13 00:13:10 +020019#include <linux/slab.h>
Paul Gortmaker51990e82012-01-22 11:23:42 -050020#include <linux/device.h>
Liam Girdwood80126ce2012-10-23 01:27:44 +020021#include <linux/export.h>
Viresh Kumar009acd12017-10-11 12:54:14 +053022#include <linux/pm_domain.h>
Viresh Kumar9f8ea962016-02-09 10:30:33 +053023#include <linux/regulator/consumer.h>
Nishanth Menone1f60b22010-10-13 00:13:10 +020024
Viresh Kumarf59d3ee2015-09-04 13:47:26 +053025#include "opp.h"
Nishanth Menone1f60b22010-10-13 00:13:10 +020026
27/*
Viresh Kumar2c2709d2016-02-16 14:17:53 +053028 * The root of the list of all opp-tables. All opp_table structures branch off
29 * from here, with each opp_table containing the list of opps it supports in
Nishanth Menone1f60b22010-10-13 00:13:10 +020030 * various states of availability.
31 */
Viresh Kumarf47b72a2016-05-05 16:20:33 +053032LIST_HEAD(opp_tables);
Nishanth Menone1f60b22010-10-13 00:13:10 +020033/* Lock to allow exclusive modification to the device and opp lists */
Viresh Kumar2c2709d2016-02-16 14:17:53 +053034DEFINE_MUTEX(opp_table_lock);
Nishanth Menone1f60b22010-10-13 00:13:10 +020035
Viresh Kumar2c2709d2016-02-16 14:17:53 +053036static struct opp_device *_find_opp_dev(const struct device *dev,
37 struct opp_table *opp_table)
Viresh Kumar06441652015-07-29 16:23:04 +053038{
Viresh Kumar2c2709d2016-02-16 14:17:53 +053039 struct opp_device *opp_dev;
Viresh Kumar06441652015-07-29 16:23:04 +053040
Viresh Kumar2c2709d2016-02-16 14:17:53 +053041 list_for_each_entry(opp_dev, &opp_table->dev_list, node)
42 if (opp_dev->dev == dev)
43 return opp_dev;
Viresh Kumar06441652015-07-29 16:23:04 +053044
45 return NULL;
46}
47
Wei Yongjun6ac42392017-02-06 14:29:55 +000048static struct opp_table *_find_opp_table_unlocked(struct device *dev)
Viresh Kumar5b650b32017-01-23 10:11:48 +053049{
50 struct opp_table *opp_table;
51
52 list_for_each_entry(opp_table, &opp_tables, node) {
53 if (_find_opp_dev(dev, opp_table)) {
54 _get_opp_table_kref(opp_table);
55
56 return opp_table;
57 }
58 }
59
60 return ERR_PTR(-ENODEV);
61}
62
Nishanth Menone1f60b22010-10-13 00:13:10 +020063/**
Viresh Kumar2c2709d2016-02-16 14:17:53 +053064 * _find_opp_table() - find opp_table struct using device pointer
65 * @dev: device pointer used to lookup OPP table
Nishanth Menone1f60b22010-10-13 00:13:10 +020066 *
Viresh Kumar052c6f12017-01-23 10:11:49 +053067 * Search OPP table for one containing matching device.
Nishanth Menone1f60b22010-10-13 00:13:10 +020068 *
Viresh Kumar2c2709d2016-02-16 14:17:53 +053069 * Return: pointer to 'struct opp_table' if found, otherwise -ENODEV or
Nishanth Menone1f60b22010-10-13 00:13:10 +020070 * -EINVAL based on type of error.
71 *
Viresh Kumar5b650b32017-01-23 10:11:48 +053072 * The callers must call dev_pm_opp_put_opp_table() after the table is used.
Nishanth Menone1f60b22010-10-13 00:13:10 +020073 */
Viresh Kumar2c2709d2016-02-16 14:17:53 +053074struct opp_table *_find_opp_table(struct device *dev)
Nishanth Menone1f60b22010-10-13 00:13:10 +020075{
Viresh Kumar2c2709d2016-02-16 14:17:53 +053076 struct opp_table *opp_table;
Nishanth Menone1f60b22010-10-13 00:13:10 +020077
Viresh Kumar50a3cb02015-08-12 15:59:39 +053078 if (IS_ERR_OR_NULL(dev)) {
Nishanth Menone1f60b22010-10-13 00:13:10 +020079 pr_err("%s: Invalid parameters\n", __func__);
80 return ERR_PTR(-EINVAL);
81 }
82
Viresh Kumar5b650b32017-01-23 10:11:48 +053083 mutex_lock(&opp_table_lock);
84 opp_table = _find_opp_table_unlocked(dev);
85 mutex_unlock(&opp_table_lock);
Nishanth Menone1f60b22010-10-13 00:13:10 +020086
Viresh Kumar5b650b32017-01-23 10:11:48 +053087 return opp_table;
Nishanth Menone1f60b22010-10-13 00:13:10 +020088}
89
90/**
Linus Torvaldsbaf51c42015-11-11 09:03:01 -080091 * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an opp
Nishanth Menone1f60b22010-10-13 00:13:10 +020092 * @opp: opp for which voltage has to be returned for
93 *
Nishanth Menon984f16c2014-12-24 11:22:57 -060094 * Return: voltage in micro volt corresponding to the opp, else
Nishanth Menone1f60b22010-10-13 00:13:10 +020095 * return 0
96 *
Viresh Kumardfbe4672016-12-01 16:28:19 +053097 * This is useful only for devices with single power supply.
Nishanth Menone1f60b22010-10-13 00:13:10 +020098 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -050099unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200100{
Viresh Kumar052c6f12017-01-23 10:11:49 +0530101 if (IS_ERR_OR_NULL(opp)) {
Nishanth Menone1f60b22010-10-13 00:13:10 +0200102 pr_err("%s: Invalid parameters\n", __func__);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530103 return 0;
104 }
Nishanth Menone1f60b22010-10-13 00:13:10 +0200105
Viresh Kumar052c6f12017-01-23 10:11:49 +0530106 return opp->supplies[0].u_volt;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200107}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500108EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200109
110/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500111 * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp
Nishanth Menone1f60b22010-10-13 00:13:10 +0200112 * @opp: opp for which frequency has to be returned for
113 *
Nishanth Menon984f16c2014-12-24 11:22:57 -0600114 * Return: frequency in hertz corresponding to the opp, else
Nishanth Menone1f60b22010-10-13 00:13:10 +0200115 * return 0
Nishanth Menone1f60b22010-10-13 00:13:10 +0200116 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500117unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200118{
Viresh Kumar052c6f12017-01-23 10:11:49 +0530119 if (IS_ERR_OR_NULL(opp) || !opp->available) {
Nishanth Menone1f60b22010-10-13 00:13:10 +0200120 pr_err("%s: Invalid parameters\n", __func__);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530121 return 0;
122 }
Nishanth Menone1f60b22010-10-13 00:13:10 +0200123
Viresh Kumar052c6f12017-01-23 10:11:49 +0530124 return opp->rate;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200125}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500126EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200127
128/**
Bartlomiej Zolnierkiewicz19445b22015-07-09 17:43:35 +0200129 * dev_pm_opp_is_turbo() - Returns if opp is turbo OPP or not
130 * @opp: opp for which turbo mode is being verified
131 *
132 * Turbo OPPs are not for normal use, and can be enabled (under certain
133 * conditions) for short duration of times to finish high throughput work
134 * quickly. Running on them for longer times may overheat the chip.
135 *
136 * Return: true if opp is turbo opp, else false.
Bartlomiej Zolnierkiewicz19445b22015-07-09 17:43:35 +0200137 */
138bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp)
139{
Viresh Kumar052c6f12017-01-23 10:11:49 +0530140 if (IS_ERR_OR_NULL(opp) || !opp->available) {
Bartlomiej Zolnierkiewicz19445b22015-07-09 17:43:35 +0200141 pr_err("%s: Invalid parameters\n", __func__);
142 return false;
143 }
144
Viresh Kumar052c6f12017-01-23 10:11:49 +0530145 return opp->turbo;
Bartlomiej Zolnierkiewicz19445b22015-07-09 17:43:35 +0200146}
147EXPORT_SYMBOL_GPL(dev_pm_opp_is_turbo);
148
149/**
Viresh Kumar3ca9bb32015-07-29 16:23:03 +0530150 * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds
151 * @dev: device for which we do this operation
152 *
153 * Return: This function returns the max clock latency in nanoseconds.
Viresh Kumar3ca9bb32015-07-29 16:23:03 +0530154 */
155unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev)
156{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530157 struct opp_table *opp_table;
Viresh Kumar3ca9bb32015-07-29 16:23:03 +0530158 unsigned long clock_latency_ns;
159
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530160 opp_table = _find_opp_table(dev);
161 if (IS_ERR(opp_table))
Viresh Kumar5b650b32017-01-23 10:11:48 +0530162 return 0;
Viresh Kumar3ca9bb32015-07-29 16:23:03 +0530163
Viresh Kumar5b650b32017-01-23 10:11:48 +0530164 clock_latency_ns = opp_table->clock_latency_ns_max;
165
166 dev_pm_opp_put_opp_table(opp_table);
167
Viresh Kumar3ca9bb32015-07-29 16:23:03 +0530168 return clock_latency_ns;
169}
170EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency);
171
172/**
Viresh Kumar655c9df2016-02-09 10:30:35 +0530173 * dev_pm_opp_get_max_volt_latency() - Get max voltage latency in nanoseconds
174 * @dev: device for which we do this operation
175 *
176 * Return: This function returns the max voltage latency in nanoseconds.
Viresh Kumar655c9df2016-02-09 10:30:35 +0530177 */
178unsigned long dev_pm_opp_get_max_volt_latency(struct device *dev)
179{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530180 struct opp_table *opp_table;
Viresh Kumar655c9df2016-02-09 10:30:35 +0530181 struct dev_pm_opp *opp;
Viresh Kumar478256b2017-05-23 09:32:11 +0530182 struct regulator *reg;
Viresh Kumar655c9df2016-02-09 10:30:35 +0530183 unsigned long latency_ns = 0;
Viresh Kumardfbe4672016-12-01 16:28:19 +0530184 int ret, i, count;
185 struct {
186 unsigned long min;
187 unsigned long max;
188 } *uV;
189
Viresh Kumarcdd3e612017-01-23 10:11:51 +0530190 opp_table = _find_opp_table(dev);
191 if (IS_ERR(opp_table))
192 return 0;
193
194 count = opp_table->regulator_count;
Viresh Kumardfbe4672016-12-01 16:28:19 +0530195
196 /* Regulator may not be required for the device */
197 if (!count)
Viresh Kumarcdd3e612017-01-23 10:11:51 +0530198 goto put_opp_table;
Viresh Kumardfbe4672016-12-01 16:28:19 +0530199
Viresh Kumardfbe4672016-12-01 16:28:19 +0530200 uV = kmalloc_array(count, sizeof(*uV), GFP_KERNEL);
201 if (!uV)
Viresh Kumar478256b2017-05-23 09:32:11 +0530202 goto put_opp_table;
Viresh Kumar655c9df2016-02-09 10:30:35 +0530203
Viresh Kumar052c6f12017-01-23 10:11:49 +0530204 mutex_lock(&opp_table->lock);
205
Viresh Kumardfbe4672016-12-01 16:28:19 +0530206 for (i = 0; i < count; i++) {
207 uV[i].min = ~0;
208 uV[i].max = 0;
Viresh Kumar655c9df2016-02-09 10:30:35 +0530209
Viresh Kumar052c6f12017-01-23 10:11:49 +0530210 list_for_each_entry(opp, &opp_table->opp_list, node) {
Viresh Kumardfbe4672016-12-01 16:28:19 +0530211 if (!opp->available)
212 continue;
213
214 if (opp->supplies[i].u_volt_min < uV[i].min)
215 uV[i].min = opp->supplies[i].u_volt_min;
216 if (opp->supplies[i].u_volt_max > uV[i].max)
217 uV[i].max = opp->supplies[i].u_volt_max;
218 }
Viresh Kumar655c9df2016-02-09 10:30:35 +0530219 }
220
Viresh Kumar052c6f12017-01-23 10:11:49 +0530221 mutex_unlock(&opp_table->lock);
Viresh Kumar655c9df2016-02-09 10:30:35 +0530222
223 /*
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530224 * The caller needs to ensure that opp_table (and hence the regulator)
Viresh Kumar655c9df2016-02-09 10:30:35 +0530225 * isn't freed, while we are executing this routine.
226 */
Andrzej Hajda8cc31112017-02-20 19:57:57 +0100227 for (i = 0; i < count; i++) {
Viresh Kumar478256b2017-05-23 09:32:11 +0530228 reg = opp_table->regulators[i];
Viresh Kumardfbe4672016-12-01 16:28:19 +0530229 ret = regulator_set_voltage_time(reg, uV[i].min, uV[i].max);
230 if (ret > 0)
231 latency_ns += ret * 1000;
232 }
233
Viresh Kumardfbe4672016-12-01 16:28:19 +0530234 kfree(uV);
Viresh Kumarcdd3e612017-01-23 10:11:51 +0530235put_opp_table:
236 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar655c9df2016-02-09 10:30:35 +0530237
238 return latency_ns;
239}
240EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_volt_latency);
241
242/**
Viresh Kumar21743442016-02-09 10:30:36 +0530243 * dev_pm_opp_get_max_transition_latency() - Get max transition latency in
244 * nanoseconds
245 * @dev: device for which we do this operation
246 *
247 * Return: This function returns the max transition latency, in nanoseconds, to
248 * switch from one OPP to other.
Viresh Kumar21743442016-02-09 10:30:36 +0530249 */
250unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev)
251{
252 return dev_pm_opp_get_max_volt_latency(dev) +
253 dev_pm_opp_get_max_clock_latency(dev);
254}
255EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_transition_latency);
256
257/**
Viresh Kumar3aa26a32017-01-02 14:41:02 +0530258 * dev_pm_opp_get_suspend_opp_freq() - Get frequency of suspend opp in Hz
Bartlomiej Zolnierkiewicz4eafbd12015-09-08 18:41:01 +0200259 * @dev: device for which we do this operation
260 *
Viresh Kumar3aa26a32017-01-02 14:41:02 +0530261 * Return: This function returns the frequency of the OPP marked as suspend_opp
262 * if one is available, else returns 0;
Bartlomiej Zolnierkiewicz4eafbd12015-09-08 18:41:01 +0200263 */
Viresh Kumar3aa26a32017-01-02 14:41:02 +0530264unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev)
Bartlomiej Zolnierkiewicz4eafbd12015-09-08 18:41:01 +0200265{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530266 struct opp_table *opp_table;
Viresh Kumar3aa26a32017-01-02 14:41:02 +0530267 unsigned long freq = 0;
Bartlomiej Zolnierkiewicz4eafbd12015-09-08 18:41:01 +0200268
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530269 opp_table = _find_opp_table(dev);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530270 if (IS_ERR(opp_table))
271 return 0;
Bartlomiej Zolnierkiewicz4eafbd12015-09-08 18:41:01 +0200272
Viresh Kumar5b650b32017-01-23 10:11:48 +0530273 if (opp_table->suspend_opp && opp_table->suspend_opp->available)
274 freq = dev_pm_opp_get_freq(opp_table->suspend_opp);
Viresh Kumar3aa26a32017-01-02 14:41:02 +0530275
Viresh Kumar5b650b32017-01-23 10:11:48 +0530276 dev_pm_opp_put_opp_table(opp_table);
277
Viresh Kumar3aa26a32017-01-02 14:41:02 +0530278 return freq;
Bartlomiej Zolnierkiewicz4eafbd12015-09-08 18:41:01 +0200279}
Viresh Kumar3aa26a32017-01-02 14:41:02 +0530280EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp_freq);
Bartlomiej Zolnierkiewicz4eafbd12015-09-08 18:41:01 +0200281
Viresh Kumara1e8c132018-04-06 14:35:45 +0530282int _get_opp_count(struct opp_table *opp_table)
283{
284 struct dev_pm_opp *opp;
285 int count = 0;
286
287 mutex_lock(&opp_table->lock);
288
289 list_for_each_entry(opp, &opp_table->opp_list, node) {
290 if (opp->available)
291 count++;
292 }
293
294 mutex_unlock(&opp_table->lock);
295
296 return count;
297}
298
Bartlomiej Zolnierkiewicz4eafbd12015-09-08 18:41:01 +0200299/**
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530300 * dev_pm_opp_get_opp_count() - Get number of opps available in the opp table
Nishanth Menone1f60b22010-10-13 00:13:10 +0200301 * @dev: device for which we do this operation
302 *
Nishanth Menon984f16c2014-12-24 11:22:57 -0600303 * Return: This function returns the number of available opps if there are any,
Nishanth Menone1f60b22010-10-13 00:13:10 +0200304 * else returns 0 if none or the corresponding error value.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200305 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500306int dev_pm_opp_get_opp_count(struct device *dev)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200307{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530308 struct opp_table *opp_table;
Viresh Kumara1e8c132018-04-06 14:35:45 +0530309 int count;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200310
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530311 opp_table = _find_opp_table(dev);
312 if (IS_ERR(opp_table)) {
313 count = PTR_ERR(opp_table);
Fabio Estevam035ed072017-09-29 14:39:49 -0300314 dev_dbg(dev, "%s: OPP table not found (%d)\n",
Dmitry Torokhovb4718c02014-12-16 15:09:38 -0800315 __func__, count);
Viresh Kumara1e8c132018-04-06 14:35:45 +0530316 return 0;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200317 }
318
Viresh Kumara1e8c132018-04-06 14:35:45 +0530319 count = _get_opp_count(opp_table);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530320 dev_pm_opp_put_opp_table(opp_table);
321
Nishanth Menone1f60b22010-10-13 00:13:10 +0200322 return count;
323}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500324EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200325
326/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500327 * dev_pm_opp_find_freq_exact() - search for an exact frequency
Nishanth Menone1f60b22010-10-13 00:13:10 +0200328 * @dev: device for which we do this operation
329 * @freq: frequency to search for
Nishanth Menon7ae49612011-02-25 23:46:18 +0100330 * @available: true/false - match for available opp
Nishanth Menone1f60b22010-10-13 00:13:10 +0200331 *
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530332 * Return: Searches for exact match in the opp table and returns pointer to the
Nishanth Menon984f16c2014-12-24 11:22:57 -0600333 * matching opp if found, else returns ERR_PTR in case of error and should
334 * be handled using IS_ERR. Error return values can be:
Nishanth Menon07797262012-10-24 22:00:12 +0200335 * EINVAL: for bad pointer
336 * ERANGE: no match found for search
337 * ENODEV: if device not found in list of registered devices
Nishanth Menone1f60b22010-10-13 00:13:10 +0200338 *
339 * Note: available is a modifier for the search. if available=true, then the
340 * match is for exact matching frequency and is available in the stored OPP
341 * table. if false, the match is for exact frequency which is not available.
342 *
343 * This provides a mechanism to enable an opp which is not available currently
344 * or the opposite as well.
345 *
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530346 * The callers are required to call dev_pm_opp_put() for the returned OPP after
347 * use.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200348 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500349struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
350 unsigned long freq,
351 bool available)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200352{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530353 struct opp_table *opp_table;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500354 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200355
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530356 opp_table = _find_opp_table(dev);
357 if (IS_ERR(opp_table)) {
358 int r = PTR_ERR(opp_table);
359
360 dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200361 return ERR_PTR(r);
362 }
363
Viresh Kumar052c6f12017-01-23 10:11:49 +0530364 mutex_lock(&opp_table->lock);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530365
Viresh Kumar052c6f12017-01-23 10:11:49 +0530366 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
Nishanth Menone1f60b22010-10-13 00:13:10 +0200367 if (temp_opp->available == available &&
368 temp_opp->rate == freq) {
369 opp = temp_opp;
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530370
371 /* Increment the reference count of OPP */
372 dev_pm_opp_get(opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200373 break;
374 }
375 }
376
Viresh Kumar052c6f12017-01-23 10:11:49 +0530377 mutex_unlock(&opp_table->lock);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530378 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530379
Nishanth Menone1f60b22010-10-13 00:13:10 +0200380 return opp;
381}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500382EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200383
Jisheng Zhang067b7ce2016-07-25 14:11:16 +0800384static noinline struct dev_pm_opp *_find_freq_ceil(struct opp_table *opp_table,
385 unsigned long *freq)
386{
387 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
388
Viresh Kumar052c6f12017-01-23 10:11:49 +0530389 mutex_lock(&opp_table->lock);
390
391 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
Jisheng Zhang067b7ce2016-07-25 14:11:16 +0800392 if (temp_opp->available && temp_opp->rate >= *freq) {
393 opp = temp_opp;
394 *freq = opp->rate;
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530395
396 /* Increment the reference count of OPP */
397 dev_pm_opp_get(opp);
Jisheng Zhang067b7ce2016-07-25 14:11:16 +0800398 break;
399 }
400 }
401
Viresh Kumar052c6f12017-01-23 10:11:49 +0530402 mutex_unlock(&opp_table->lock);
403
Jisheng Zhang067b7ce2016-07-25 14:11:16 +0800404 return opp;
405}
406
Nishanth Menone1f60b22010-10-13 00:13:10 +0200407/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500408 * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
Nishanth Menone1f60b22010-10-13 00:13:10 +0200409 * @dev: device for which we do this operation
410 * @freq: Start frequency
411 *
412 * Search for the matching ceil *available* OPP from a starting freq
413 * for a device.
414 *
Nishanth Menon984f16c2014-12-24 11:22:57 -0600415 * Return: matching *opp and refreshes *freq accordingly, else returns
Nishanth Menon07797262012-10-24 22:00:12 +0200416 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
417 * values can be:
418 * EINVAL: for bad pointer
419 * ERANGE: no match found for search
420 * ENODEV: if device not found in list of registered devices
Nishanth Menone1f60b22010-10-13 00:13:10 +0200421 *
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530422 * The callers are required to call dev_pm_opp_put() for the returned OPP after
423 * use.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200424 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500425struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
426 unsigned long *freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200427{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530428 struct opp_table *opp_table;
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530429 struct dev_pm_opp *opp;
Dmitry Torokhovb02ded22014-12-16 15:09:36 -0800430
Nishanth Menone1f60b22010-10-13 00:13:10 +0200431 if (!dev || !freq) {
432 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
433 return ERR_PTR(-EINVAL);
434 }
435
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530436 opp_table = _find_opp_table(dev);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530437 if (IS_ERR(opp_table))
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530438 return ERR_CAST(opp_table);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530439
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530440 opp = _find_freq_ceil(opp_table, freq);
441
Viresh Kumar5b650b32017-01-23 10:11:48 +0530442 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530443
444 return opp;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200445}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500446EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200447
448/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500449 * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
Nishanth Menone1f60b22010-10-13 00:13:10 +0200450 * @dev: device for which we do this operation
451 * @freq: Start frequency
452 *
453 * Search for the matching floor *available* OPP from a starting freq
454 * for a device.
455 *
Nishanth Menon984f16c2014-12-24 11:22:57 -0600456 * Return: matching *opp and refreshes *freq accordingly, else returns
Nishanth Menon07797262012-10-24 22:00:12 +0200457 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
458 * values can be:
459 * EINVAL: for bad pointer
460 * ERANGE: no match found for search
461 * ENODEV: if device not found in list of registered devices
Nishanth Menone1f60b22010-10-13 00:13:10 +0200462 *
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530463 * The callers are required to call dev_pm_opp_put() for the returned OPP after
464 * use.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200465 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500466struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
467 unsigned long *freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200468{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530469 struct opp_table *opp_table;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500470 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200471
472 if (!dev || !freq) {
473 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
474 return ERR_PTR(-EINVAL);
475 }
476
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530477 opp_table = _find_opp_table(dev);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530478 if (IS_ERR(opp_table))
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530479 return ERR_CAST(opp_table);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530480
Viresh Kumar052c6f12017-01-23 10:11:49 +0530481 mutex_lock(&opp_table->lock);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200482
Viresh Kumar052c6f12017-01-23 10:11:49 +0530483 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
Nishanth Menone1f60b22010-10-13 00:13:10 +0200484 if (temp_opp->available) {
485 /* go to the next node, before choosing prev */
486 if (temp_opp->rate > *freq)
487 break;
488 else
489 opp = temp_opp;
490 }
491 }
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530492
493 /* Increment the reference count of OPP */
494 if (!IS_ERR(opp))
495 dev_pm_opp_get(opp);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530496 mutex_unlock(&opp_table->lock);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530497 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530498
Nishanth Menone1f60b22010-10-13 00:13:10 +0200499 if (!IS_ERR(opp))
500 *freq = opp->rate;
501
502 return opp;
503}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500504EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200505
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530506static int _set_opp_voltage(struct device *dev, struct regulator *reg,
Viresh Kumarce317812016-12-01 16:28:18 +0530507 struct dev_pm_opp_supply *supply)
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530508{
509 int ret;
510
511 /* Regulator not available for device */
512 if (IS_ERR(reg)) {
513 dev_dbg(dev, "%s: regulator not available: %ld\n", __func__,
514 PTR_ERR(reg));
515 return 0;
516 }
517
Viresh Kumarce317812016-12-01 16:28:18 +0530518 dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__,
519 supply->u_volt_min, supply->u_volt, supply->u_volt_max);
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530520
Viresh Kumarce317812016-12-01 16:28:18 +0530521 ret = regulator_set_voltage_triplet(reg, supply->u_volt_min,
522 supply->u_volt, supply->u_volt_max);
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530523 if (ret)
524 dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n",
Viresh Kumarce317812016-12-01 16:28:18 +0530525 __func__, supply->u_volt_min, supply->u_volt,
526 supply->u_volt_max, ret);
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530527
528 return ret;
529}
530
Viresh Kumar94735582016-12-01 16:28:20 +0530531static inline int
532_generic_set_opp_clk_only(struct device *dev, struct clk *clk,
533 unsigned long old_freq, unsigned long freq)
534{
535 int ret;
536
537 ret = clk_set_rate(clk, freq);
538 if (ret) {
539 dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
540 ret);
541 }
542
543 return ret;
544}
545
Viresh Kumar009acd12017-10-11 12:54:14 +0530546static inline int
547_generic_set_opp_domain(struct device *dev, struct clk *clk,
548 unsigned long old_freq, unsigned long freq,
549 unsigned int old_pstate, unsigned int new_pstate)
550{
551 int ret;
552
553 /* Scaling up? Scale domain performance state before frequency */
554 if (freq > old_freq) {
555 ret = dev_pm_genpd_set_performance_state(dev, new_pstate);
556 if (ret)
557 return ret;
558 }
559
560 ret = _generic_set_opp_clk_only(dev, clk, old_freq, freq);
561 if (ret)
562 goto restore_domain_state;
563
564 /* Scaling down? Scale domain performance state after frequency */
565 if (freq < old_freq) {
566 ret = dev_pm_genpd_set_performance_state(dev, new_pstate);
567 if (ret)
568 goto restore_freq;
569 }
570
571 return 0;
572
573restore_freq:
574 if (_generic_set_opp_clk_only(dev, clk, freq, old_freq))
575 dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n",
576 __func__, old_freq);
577restore_domain_state:
578 if (freq > old_freq)
579 dev_pm_genpd_set_performance_state(dev, old_pstate);
580
581 return ret;
582}
583
Viresh Kumarc74b32f2017-05-23 09:32:10 +0530584static int _generic_set_opp_regulator(const struct opp_table *opp_table,
585 struct device *dev,
586 unsigned long old_freq,
587 unsigned long freq,
588 struct dev_pm_opp_supply *old_supply,
589 struct dev_pm_opp_supply *new_supply)
Viresh Kumar94735582016-12-01 16:28:20 +0530590{
Viresh Kumarc74b32f2017-05-23 09:32:10 +0530591 struct regulator *reg = opp_table->regulators[0];
Viresh Kumar94735582016-12-01 16:28:20 +0530592 int ret;
593
594 /* This function only supports single regulator per device */
Viresh Kumarc74b32f2017-05-23 09:32:10 +0530595 if (WARN_ON(opp_table->regulator_count > 1)) {
Viresh Kumar94735582016-12-01 16:28:20 +0530596 dev_err(dev, "multiple regulators are not supported\n");
597 return -EINVAL;
598 }
599
600 /* Scaling up? Scale voltage before frequency */
601 if (freq > old_freq) {
602 ret = _set_opp_voltage(dev, reg, new_supply);
603 if (ret)
604 goto restore_voltage;
605 }
606
607 /* Change frequency */
Viresh Kumarc74b32f2017-05-23 09:32:10 +0530608 ret = _generic_set_opp_clk_only(dev, opp_table->clk, old_freq, freq);
Viresh Kumar94735582016-12-01 16:28:20 +0530609 if (ret)
610 goto restore_voltage;
611
612 /* Scaling down? Scale voltage after frequency */
613 if (freq < old_freq) {
614 ret = _set_opp_voltage(dev, reg, new_supply);
615 if (ret)
616 goto restore_freq;
617 }
618
619 return 0;
620
621restore_freq:
Viresh Kumarc74b32f2017-05-23 09:32:10 +0530622 if (_generic_set_opp_clk_only(dev, opp_table->clk, freq, old_freq))
Viresh Kumar94735582016-12-01 16:28:20 +0530623 dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n",
624 __func__, old_freq);
625restore_voltage:
626 /* This shouldn't harm even if the voltages weren't updated earlier */
Viresh Kumarc74b32f2017-05-23 09:32:10 +0530627 if (old_supply)
Viresh Kumar94735582016-12-01 16:28:20 +0530628 _set_opp_voltage(dev, reg, old_supply);
629
630 return ret;
631}
632
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530633/**
634 * dev_pm_opp_set_rate() - Configure new OPP based on frequency
635 * @dev: device for which we do this operation
636 * @target_freq: frequency to achieve
637 *
638 * This configures the power-supplies and clock source to the levels specified
639 * by the OPP corresponding to the target_freq.
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530640 */
641int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq)
642{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530643 struct opp_table *opp_table;
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530644 unsigned long freq, old_freq;
Viresh Kumar94735582016-12-01 16:28:20 +0530645 struct dev_pm_opp *old_opp, *opp;
Viresh Kumar94735582016-12-01 16:28:20 +0530646 struct clk *clk;
647 int ret, size;
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530648
649 if (unlikely(!target_freq)) {
650 dev_err(dev, "%s: Invalid target frequency %lu\n", __func__,
651 target_freq);
652 return -EINVAL;
653 }
654
Viresh Kumar052c6f12017-01-23 10:11:49 +0530655 opp_table = _find_opp_table(dev);
656 if (IS_ERR(opp_table)) {
657 dev_err(dev, "%s: device opp doesn't exist\n", __func__);
658 return PTR_ERR(opp_table);
659 }
660
661 clk = opp_table->clk;
662 if (IS_ERR(clk)) {
663 dev_err(dev, "%s: No clock available for the device\n",
664 __func__);
665 ret = PTR_ERR(clk);
666 goto put_opp_table;
667 }
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530668
669 freq = clk_round_rate(clk, target_freq);
670 if ((long)freq <= 0)
671 freq = target_freq;
672
673 old_freq = clk_get_rate(clk);
674
675 /* Return early if nothing to do */
676 if (old_freq == freq) {
677 dev_dbg(dev, "%s: old/new frequencies (%lu Hz) are same, nothing to do\n",
678 __func__, freq);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530679 ret = 0;
680 goto put_opp_table;
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530681 }
682
Jisheng Zhang067b7ce2016-07-25 14:11:16 +0800683 old_opp = _find_freq_ceil(opp_table, &old_freq);
Arnd Bergmann4df27c92016-09-15 17:38:38 +0200684 if (IS_ERR(old_opp)) {
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530685 dev_err(dev, "%s: failed to find current OPP for freq %lu (%ld)\n",
686 __func__, old_freq, PTR_ERR(old_opp));
687 }
688
Jisheng Zhang067b7ce2016-07-25 14:11:16 +0800689 opp = _find_freq_ceil(opp_table, &freq);
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530690 if (IS_ERR(opp)) {
691 ret = PTR_ERR(opp);
692 dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n",
693 __func__, freq, ret);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530694 goto put_old_opp;
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530695 }
696
Viresh Kumar94735582016-12-01 16:28:20 +0530697 dev_dbg(dev, "%s: switching OPP: %lu Hz --> %lu Hz\n", __func__,
698 old_freq, freq);
Viresh Kumardfbe4672016-12-01 16:28:19 +0530699
Viresh Kumar94735582016-12-01 16:28:20 +0530700 /* Only frequency scaling */
Viresh Kumarc74b32f2017-05-23 09:32:10 +0530701 if (!opp_table->regulators) {
Viresh Kumar009acd12017-10-11 12:54:14 +0530702 /*
703 * We don't support devices with both regulator and
704 * domain performance-state for now.
705 */
706 if (opp_table->genpd_performance_state)
707 ret = _generic_set_opp_domain(dev, clk, old_freq, freq,
708 IS_ERR(old_opp) ? 0 : old_opp->pstate,
709 opp->pstate);
710 else
711 ret = _generic_set_opp_clk_only(dev, clk, old_freq, freq);
Viresh Kumarc74b32f2017-05-23 09:32:10 +0530712 } else if (!opp_table->set_opp) {
713 ret = _generic_set_opp_regulator(opp_table, dev, old_freq, freq,
714 IS_ERR(old_opp) ? NULL : old_opp->supplies,
715 opp->supplies);
716 } else {
717 struct dev_pm_set_opp_data *data;
718
719 data = opp_table->set_opp_data;
720 data->regulators = opp_table->regulators;
721 data->regulator_count = opp_table->regulator_count;
722 data->clk = clk;
723 data->dev = dev;
724
725 data->old_opp.rate = old_freq;
726 size = sizeof(*opp->supplies) * opp_table->regulator_count;
727 if (IS_ERR(old_opp))
728 memset(data->old_opp.supplies, 0, size);
729 else
730 memcpy(data->old_opp.supplies, old_opp->supplies, size);
731
732 data->new_opp.rate = freq;
733 memcpy(data->new_opp.supplies, opp->supplies, size);
734
735 ret = opp_table->set_opp(data);
Viresh Kumardfbe4672016-12-01 16:28:19 +0530736 }
737
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530738 dev_pm_opp_put(opp);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530739put_old_opp:
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530740 if (!IS_ERR(old_opp))
741 dev_pm_opp_put(old_opp);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530742put_opp_table:
Viresh Kumar5b650b32017-01-23 10:11:48 +0530743 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530744 return ret;
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530745}
746EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate);
747
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530748/* OPP-dev Helpers */
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530749static void _remove_opp_dev(struct opp_device *opp_dev,
750 struct opp_table *opp_table)
Viresh Kumar06441652015-07-29 16:23:04 +0530751{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530752 opp_debug_unregister(opp_dev, opp_table);
753 list_del(&opp_dev->node);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530754 kfree(opp_dev);
Viresh Kumar06441652015-07-29 16:23:04 +0530755}
756
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530757struct opp_device *_add_opp_dev(const struct device *dev,
758 struct opp_table *opp_table)
Viresh Kumar06441652015-07-29 16:23:04 +0530759{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530760 struct opp_device *opp_dev;
Viresh Kumardeaa5142015-11-11 07:59:01 +0530761 int ret;
Viresh Kumar06441652015-07-29 16:23:04 +0530762
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530763 opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL);
764 if (!opp_dev)
Viresh Kumar06441652015-07-29 16:23:04 +0530765 return NULL;
766
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530767 /* Initialize opp-dev */
768 opp_dev->dev = dev;
Viresh Kumar052c6f12017-01-23 10:11:49 +0530769 list_add(&opp_dev->node, &opp_table->dev_list);
Viresh Kumar06441652015-07-29 16:23:04 +0530770
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530771 /* Create debugfs entries for the opp_table */
772 ret = opp_debug_register(opp_dev, opp_table);
Viresh Kumardeaa5142015-11-11 07:59:01 +0530773 if (ret)
774 dev_err(dev, "%s: Failed to register opp debugfs (%d)\n",
775 __func__, ret);
776
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530777 return opp_dev;
Viresh Kumar06441652015-07-29 16:23:04 +0530778}
779
Viresh Kumarb6160e22017-01-02 14:41:04 +0530780static struct opp_table *_allocate_opp_table(struct device *dev)
Viresh Kumar07cce742014-12-10 09:45:34 +0530781{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530782 struct opp_table *opp_table;
783 struct opp_device *opp_dev;
Viresh Kumard54974c2016-02-09 10:30:38 +0530784 int ret;
Viresh Kumar07cce742014-12-10 09:45:34 +0530785
786 /*
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530787 * Allocate a new OPP table. In the infrequent case where a new
Viresh Kumar07cce742014-12-10 09:45:34 +0530788 * device is needed to be added, we pay this penalty.
789 */
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530790 opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL);
791 if (!opp_table)
Viresh Kumar07cce742014-12-10 09:45:34 +0530792 return NULL;
793
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530794 INIT_LIST_HEAD(&opp_table->dev_list);
Viresh Kumar06441652015-07-29 16:23:04 +0530795
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530796 opp_dev = _add_opp_dev(dev, opp_table);
797 if (!opp_dev) {
798 kfree(opp_table);
Viresh Kumar06441652015-07-29 16:23:04 +0530799 return NULL;
800 }
801
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530802 _of_init_opp_table(opp_table, dev);
Viresh Kumar50f8cfb2016-02-09 10:30:37 +0530803
Viresh Kumard54974c2016-02-09 10:30:38 +0530804 /* Find clk for the device */
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530805 opp_table->clk = clk_get(dev, NULL);
806 if (IS_ERR(opp_table->clk)) {
807 ret = PTR_ERR(opp_table->clk);
Viresh Kumard54974c2016-02-09 10:30:38 +0530808 if (ret != -EPROBE_DEFER)
809 dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__,
810 ret);
811 }
812
Viresh Kumar052c6f12017-01-23 10:11:49 +0530813 BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head);
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530814 INIT_LIST_HEAD(&opp_table->opp_list);
Viresh Kumar37a73ec2017-01-23 10:11:41 +0530815 mutex_init(&opp_table->lock);
Viresh Kumarf067a982017-01-23 10:11:42 +0530816 kref_init(&opp_table->kref);
Viresh Kumar07cce742014-12-10 09:45:34 +0530817
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530818 /* Secure the device table modification */
Viresh Kumar052c6f12017-01-23 10:11:49 +0530819 list_add(&opp_table->node, &opp_tables);
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530820 return opp_table;
Viresh Kumar07cce742014-12-10 09:45:34 +0530821}
822
Viresh Kumarf067a982017-01-23 10:11:42 +0530823void _get_opp_table_kref(struct opp_table *opp_table)
Viresh Kumar3bac42c2015-07-29 16:22:59 +0530824{
Viresh Kumarf067a982017-01-23 10:11:42 +0530825 kref_get(&opp_table->kref);
826}
827
828struct opp_table *dev_pm_opp_get_opp_table(struct device *dev)
829{
830 struct opp_table *opp_table;
831
832 /* Hold our table modification lock here */
833 mutex_lock(&opp_table_lock);
834
Viresh Kumar5b650b32017-01-23 10:11:48 +0530835 opp_table = _find_opp_table_unlocked(dev);
836 if (!IS_ERR(opp_table))
Viresh Kumarf067a982017-01-23 10:11:42 +0530837 goto unlock;
Viresh Kumarf067a982017-01-23 10:11:42 +0530838
839 opp_table = _allocate_opp_table(dev);
840
841unlock:
842 mutex_unlock(&opp_table_lock);
843
844 return opp_table;
845}
846EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table);
847
Viresh Kumarb83c1892017-01-23 10:11:45 +0530848static void _opp_table_kref_release(struct kref *kref)
Viresh Kumarf067a982017-01-23 10:11:42 +0530849{
850 struct opp_table *opp_table = container_of(kref, struct opp_table, kref);
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530851 struct opp_device *opp_dev;
Viresh Kumar06441652015-07-29 16:23:04 +0530852
Viresh Kumard54974c2016-02-09 10:30:38 +0530853 /* Release clk */
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530854 if (!IS_ERR(opp_table->clk))
855 clk_put(opp_table->clk);
Viresh Kumard54974c2016-02-09 10:30:38 +0530856
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530857 opp_dev = list_first_entry(&opp_table->dev_list, struct opp_device,
858 node);
Viresh Kumar06441652015-07-29 16:23:04 +0530859
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530860 _remove_opp_dev(opp_dev, opp_table);
Viresh Kumar06441652015-07-29 16:23:04 +0530861
862 /* dev_list must be empty now */
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530863 WARN_ON(!list_empty(&opp_table->dev_list));
Viresh Kumar06441652015-07-29 16:23:04 +0530864
Viresh Kumar37a73ec2017-01-23 10:11:41 +0530865 mutex_destroy(&opp_table->lock);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530866 list_del(&opp_table->node);
867 kfree(opp_table);
Viresh Kumar3bac42c2015-07-29 16:22:59 +0530868
Viresh Kumarf067a982017-01-23 10:11:42 +0530869 mutex_unlock(&opp_table_lock);
870}
871
872void dev_pm_opp_put_opp_table(struct opp_table *opp_table)
873{
874 kref_put_mutex(&opp_table->kref, _opp_table_kref_release,
875 &opp_table_lock);
876}
877EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table);
878
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +0530879void _opp_free(struct dev_pm_opp *opp)
Viresh Kumar969fceb2017-01-02 14:40:59 +0530880{
881 kfree(opp);
Viresh Kumar969fceb2017-01-02 14:40:59 +0530882}
883
Viresh Kumar70347642017-01-23 10:11:46 +0530884static void _opp_kref_release(struct kref *kref)
Viresh Kumar129eec52014-11-27 08:54:06 +0530885{
Viresh Kumar70347642017-01-23 10:11:46 +0530886 struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref);
887 struct opp_table *opp_table = opp->opp_table;
Viresh Kumar37a73ec2017-01-23 10:11:41 +0530888
Viresh Kumar129eec52014-11-27 08:54:06 +0530889 /*
890 * Notify the changes in the availability of the operable
891 * frequency/voltage list.
892 */
Viresh Kumar052c6f12017-01-23 10:11:49 +0530893 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_REMOVE, opp);
Viresh Kumardeaa5142015-11-11 07:59:01 +0530894 opp_debug_remove_one(opp);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530895 list_del(&opp->node);
896 kfree(opp);
Viresh Kumar129eec52014-11-27 08:54:06 +0530897
Viresh Kumar37a73ec2017-01-23 10:11:41 +0530898 mutex_unlock(&opp_table->lock);
Viresh Kumar31641cd2017-01-23 10:11:44 +0530899 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar129eec52014-11-27 08:54:06 +0530900}
901
Viresh Kumara88bd2a2017-11-29 15:18:36 +0530902void dev_pm_opp_get(struct dev_pm_opp *opp)
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530903{
904 kref_get(&opp->kref);
905}
906
Viresh Kumar70347642017-01-23 10:11:46 +0530907void dev_pm_opp_put(struct dev_pm_opp *opp)
908{
909 kref_put_mutex(&opp->kref, _opp_kref_release, &opp->opp_table->lock);
910}
911EXPORT_SYMBOL_GPL(dev_pm_opp_put);
912
Viresh Kumar129eec52014-11-27 08:54:06 +0530913/**
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530914 * dev_pm_opp_remove() - Remove an OPP from OPP table
Viresh Kumar129eec52014-11-27 08:54:06 +0530915 * @dev: device for which we do this operation
916 * @freq: OPP to remove with matching 'freq'
917 *
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530918 * This function removes an opp from the opp table.
Viresh Kumar129eec52014-11-27 08:54:06 +0530919 */
920void dev_pm_opp_remove(struct device *dev, unsigned long freq)
921{
922 struct dev_pm_opp *opp;
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530923 struct opp_table *opp_table;
Viresh Kumar129eec52014-11-27 08:54:06 +0530924 bool found = false;
925
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530926 opp_table = _find_opp_table(dev);
927 if (IS_ERR(opp_table))
Viresh Kumar5b650b32017-01-23 10:11:48 +0530928 return;
Viresh Kumar129eec52014-11-27 08:54:06 +0530929
Viresh Kumar37a73ec2017-01-23 10:11:41 +0530930 mutex_lock(&opp_table->lock);
931
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530932 list_for_each_entry(opp, &opp_table->opp_list, node) {
Viresh Kumar129eec52014-11-27 08:54:06 +0530933 if (opp->rate == freq) {
934 found = true;
935 break;
936 }
937 }
938
Viresh Kumar37a73ec2017-01-23 10:11:41 +0530939 mutex_unlock(&opp_table->lock);
940
Viresh Kumar5b650b32017-01-23 10:11:48 +0530941 if (found) {
942 dev_pm_opp_put(opp);
943 } else {
Viresh Kumar129eec52014-11-27 08:54:06 +0530944 dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
945 __func__, freq);
Viresh Kumar129eec52014-11-27 08:54:06 +0530946 }
947
Viresh Kumar5b650b32017-01-23 10:11:48 +0530948 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar129eec52014-11-27 08:54:06 +0530949}
950EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
951
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +0530952struct dev_pm_opp *_opp_allocate(struct opp_table *table)
Viresh Kumar23dacf62015-07-29 16:23:01 +0530953{
954 struct dev_pm_opp *opp;
Viresh Kumardfbe4672016-12-01 16:28:19 +0530955 int count, supply_size;
Viresh Kumar23dacf62015-07-29 16:23:01 +0530956
Viresh Kumardfbe4672016-12-01 16:28:19 +0530957 /* Allocate space for at least one supply */
958 count = table->regulator_count ? table->regulator_count : 1;
959 supply_size = sizeof(*opp->supplies) * count;
Viresh Kumar23dacf62015-07-29 16:23:01 +0530960
Viresh Kumardfbe4672016-12-01 16:28:19 +0530961 /* allocate new OPP node and supplies structures */
962 opp = kzalloc(sizeof(*opp) + supply_size, GFP_KERNEL);
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +0530963 if (!opp)
Viresh Kumar23dacf62015-07-29 16:23:01 +0530964 return NULL;
Viresh Kumar23dacf62015-07-29 16:23:01 +0530965
Viresh Kumardfbe4672016-12-01 16:28:19 +0530966 /* Put the supplies at the end of the OPP structure as an empty array */
967 opp->supplies = (struct dev_pm_opp_supply *)(opp + 1);
968 INIT_LIST_HEAD(&opp->node);
969
Viresh Kumar23dacf62015-07-29 16:23:01 +0530970 return opp;
971}
972
Viresh Kumar7d34d562016-02-09 10:30:34 +0530973static bool _opp_supported_by_regulators(struct dev_pm_opp *opp,
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530974 struct opp_table *opp_table)
Viresh Kumar7d34d562016-02-09 10:30:34 +0530975{
Viresh Kumardfbe4672016-12-01 16:28:19 +0530976 struct regulator *reg;
977 int i;
Viresh Kumar7d34d562016-02-09 10:30:34 +0530978
Viresh Kumardfbe4672016-12-01 16:28:19 +0530979 for (i = 0; i < opp_table->regulator_count; i++) {
980 reg = opp_table->regulators[i];
981
982 if (!regulator_is_supported_voltage(reg,
983 opp->supplies[i].u_volt_min,
984 opp->supplies[i].u_volt_max)) {
985 pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n",
986 __func__, opp->supplies[i].u_volt_min,
987 opp->supplies[i].u_volt_max);
988 return false;
989 }
Viresh Kumar7d34d562016-02-09 10:30:34 +0530990 }
991
992 return true;
993}
994
Viresh Kumara1e8c132018-04-06 14:35:45 +0530995static int _opp_is_duplicate(struct device *dev, struct dev_pm_opp *new_opp,
996 struct opp_table *opp_table,
997 struct list_head **head)
998{
999 struct dev_pm_opp *opp;
1000
1001 /*
1002 * Insert new OPP in order of increasing frequency and discard if
1003 * already present.
1004 *
1005 * Need to use &opp_table->opp_list in the condition part of the 'for'
1006 * loop, don't replace it with head otherwise it will become an infinite
1007 * loop.
1008 */
1009 list_for_each_entry(opp, &opp_table->opp_list, node) {
1010 if (new_opp->rate > opp->rate) {
1011 *head = &opp->node;
1012 continue;
1013 }
1014
1015 if (new_opp->rate < opp->rate)
1016 return 0;
1017
1018 /* Duplicate OPPs */
1019 dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
1020 __func__, opp->rate, opp->supplies[0].u_volt,
1021 opp->available, new_opp->rate,
1022 new_opp->supplies[0].u_volt, new_opp->available);
1023
1024 /* Should we compare voltages for all regulators here ? */
1025 return opp->available &&
1026 new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST;
1027 }
1028
1029 return 0;
1030}
1031
Viresh Kumar7f8538e2017-01-02 14:40:55 +05301032/*
1033 * Returns:
1034 * 0: On success. And appropriate error message for duplicate OPPs.
1035 * -EBUSY: For OPP with same freq/volt and is available. The callers of
1036 * _opp_add() must return 0 if they receive -EBUSY from it. This is to make
1037 * sure we don't print error messages unnecessarily if different parts of
1038 * kernel try to initialize the OPP table.
1039 * -EEXIST: For OPP with same freq but different volt or is unavailable. This
1040 * should be considered an error by the callers of _opp_add().
1041 */
Viresh Kumarf47b72a2016-05-05 16:20:33 +05301042int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
Viresh Kumara1e8c132018-04-06 14:35:45 +05301043 struct opp_table *opp_table, bool rate_not_available)
Viresh Kumar23dacf62015-07-29 16:23:01 +05301044{
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301045 struct list_head *head;
Viresh Kumardeaa5142015-11-11 07:59:01 +05301046 int ret;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301047
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301048 mutex_lock(&opp_table->lock);
1049 head = &opp_table->opp_list;
1050
Viresh Kumara1e8c132018-04-06 14:35:45 +05301051 if (likely(!rate_not_available)) {
1052 ret = _opp_is_duplicate(dev, new_opp, opp_table, &head);
1053 if (ret) {
1054 mutex_unlock(&opp_table->lock);
1055 return ret;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301056 }
Viresh Kumar23dacf62015-07-29 16:23:01 +05301057 }
1058
Viresh Kumar052c6f12017-01-23 10:11:49 +05301059 list_add(&new_opp->node, head);
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301060 mutex_unlock(&opp_table->lock);
1061
1062 new_opp->opp_table = opp_table;
Viresh Kumar70347642017-01-23 10:11:46 +05301063 kref_init(&new_opp->kref);
Viresh Kumar23dacf62015-07-29 16:23:01 +05301064
Viresh Kumar31641cd2017-01-23 10:11:44 +05301065 /* Get a reference to the OPP table */
1066 _get_opp_table_kref(opp_table);
1067
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301068 ret = opp_debug_create_one(new_opp, opp_table);
Viresh Kumardeaa5142015-11-11 07:59:01 +05301069 if (ret)
1070 dev_err(dev, "%s: Failed to register opp to debugfs (%d)\n",
1071 __func__, ret);
1072
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301073 if (!_opp_supported_by_regulators(new_opp, opp_table)) {
Viresh Kumar7d34d562016-02-09 10:30:34 +05301074 new_opp->available = false;
1075 dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n",
1076 __func__, new_opp->rate);
1077 }
1078
Viresh Kumar23dacf62015-07-29 16:23:01 +05301079 return 0;
1080}
1081
Viresh Kumar737002b2015-07-29 16:22:58 +05301082/**
Viresh Kumarb64b9c3f2015-10-15 21:42:44 +05301083 * _opp_add_v1() - Allocate a OPP based on v1 bindings.
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301084 * @opp_table: OPP table
Nishanth Menone1f60b22010-10-13 00:13:10 +02001085 * @dev: device for which we do this operation
1086 * @freq: Frequency in Hz for this OPP
1087 * @u_volt: Voltage in uVolts for this OPP
1088 * @dynamic: Dynamically added OPPs.
1089 *
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301090 * This function adds an opp definition to the opp table and returns status.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001091 * The opp is made available by default and it can be controlled using
1092 * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
1093 *
Viresh Kumar8f8d37b2015-09-04 13:47:24 +05301094 * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table
1095 * and freed by dev_pm_opp_of_remove_table.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001096 *
Nishanth Menone1f60b22010-10-13 00:13:10 +02001097 * Return:
1098 * 0 On success OR
1099 * Duplicate OPPs (both freq and volt are same) and opp->available
1100 * -EEXIST Freq are same and volt are different OR
1101 * Duplicate OPPs (both freq and volt are same) and !opp->available
1102 * -ENOMEM Memory allocation failure
1103 */
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301104int _opp_add_v1(struct opp_table *opp_table, struct device *dev,
1105 unsigned long freq, long u_volt, bool dynamic)
Nishanth Menone1f60b22010-10-13 00:13:10 +02001106{
Viresh Kumar23dacf62015-07-29 16:23:01 +05301107 struct dev_pm_opp *new_opp;
Viresh Kumar50f8cfb2016-02-09 10:30:37 +05301108 unsigned long tol;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001109 int ret;
1110
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301111 new_opp = _opp_allocate(opp_table);
1112 if (!new_opp)
1113 return -ENOMEM;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301114
Nishanth Menone1f60b22010-10-13 00:13:10 +02001115 /* populate the opp table */
1116 new_opp->rate = freq;
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301117 tol = u_volt * opp_table->voltage_tolerance_v1 / 100;
Viresh Kumardfbe4672016-12-01 16:28:19 +05301118 new_opp->supplies[0].u_volt = u_volt;
1119 new_opp->supplies[0].u_volt_min = u_volt - tol;
1120 new_opp->supplies[0].u_volt_max = u_volt + tol;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001121 new_opp->available = true;
Viresh Kumaraa5f2f82015-07-29 16:23:00 +05301122 new_opp->dynamic = dynamic;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301123
Viresh Kumara1e8c132018-04-06 14:35:45 +05301124 ret = _opp_add(dev, new_opp, opp_table, false);
Viresh Kumar7f8538e2017-01-02 14:40:55 +05301125 if (ret) {
1126 /* Don't return error for duplicate OPPs */
1127 if (ret == -EBUSY)
1128 ret = 0;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301129 goto free_opp;
Viresh Kumar7f8538e2017-01-02 14:40:55 +05301130 }
Viresh Kumar23dacf62015-07-29 16:23:01 +05301131
Nishanth Menone1f60b22010-10-13 00:13:10 +02001132 /*
1133 * Notify the changes in the availability of the operable
1134 * frequency/voltage list.
1135 */
Viresh Kumar052c6f12017-01-23 10:11:49 +05301136 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001137 return 0;
1138
1139free_opp:
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301140 _opp_free(new_opp);
1141
Nishanth Menone1f60b22010-10-13 00:13:10 +02001142 return ret;
1143}
Viresh Kumar383934092014-11-25 16:04:18 +05301144
Viresh Kumar27465902015-07-29 16:23:02 +05301145/**
Viresh Kumar7de36b02015-12-09 08:01:46 +05301146 * dev_pm_opp_set_supported_hw() - Set supported platforms
1147 * @dev: Device for which supported-hw has to be set.
1148 * @versions: Array of hierarchy of versions to match.
1149 * @count: Number of elements in the array.
1150 *
1151 * This is required only for the V2 bindings, and it enables a platform to
1152 * specify the hierarchy of versions it supports. OPP layer will then enable
1153 * OPPs, which are available for those versions, based on its 'opp-supported-hw'
1154 * property.
Viresh Kumar7de36b02015-12-09 08:01:46 +05301155 */
Viresh Kumarfa301842017-01-23 10:11:43 +05301156struct opp_table *dev_pm_opp_set_supported_hw(struct device *dev,
1157 const u32 *versions, unsigned int count)
Viresh Kumar7de36b02015-12-09 08:01:46 +05301158{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301159 struct opp_table *opp_table;
Viresh Kumar7de36b02015-12-09 08:01:46 +05301160
Viresh Kumarfa301842017-01-23 10:11:43 +05301161 opp_table = dev_pm_opp_get_opp_table(dev);
1162 if (!opp_table)
1163 return ERR_PTR(-ENOMEM);
Viresh Kumar7de36b02015-12-09 08:01:46 +05301164
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301165 /* Make sure there are no concurrent readers while updating opp_table */
1166 WARN_ON(!list_empty(&opp_table->opp_list));
Viresh Kumar7de36b02015-12-09 08:01:46 +05301167
Viresh Kumar25419de2018-05-22 16:38:08 +05301168 /* Another CPU that shares the OPP table has set the property ? */
1169 if (opp_table->supported_hw)
1170 return opp_table;
Viresh Kumar7de36b02015-12-09 08:01:46 +05301171
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301172 opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions),
Viresh Kumar7de36b02015-12-09 08:01:46 +05301173 GFP_KERNEL);
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301174 if (!opp_table->supported_hw) {
Viresh Kumar25419de2018-05-22 16:38:08 +05301175 dev_pm_opp_put_opp_table(opp_table);
1176 return ERR_PTR(-ENOMEM);
Viresh Kumar7de36b02015-12-09 08:01:46 +05301177 }
1178
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301179 opp_table->supported_hw_count = count;
Viresh Kumarfa301842017-01-23 10:11:43 +05301180
1181 return opp_table;
Viresh Kumar7de36b02015-12-09 08:01:46 +05301182}
1183EXPORT_SYMBOL_GPL(dev_pm_opp_set_supported_hw);
1184
1185/**
1186 * dev_pm_opp_put_supported_hw() - Releases resources blocked for supported hw
Viresh Kumarfa301842017-01-23 10:11:43 +05301187 * @opp_table: OPP table returned by dev_pm_opp_set_supported_hw().
Viresh Kumar7de36b02015-12-09 08:01:46 +05301188 *
1189 * This is required only for the V2 bindings, and is called for a matching
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301190 * dev_pm_opp_set_supported_hw(). Until this is called, the opp_table structure
Viresh Kumar7de36b02015-12-09 08:01:46 +05301191 * will not be freed.
Viresh Kumar7de36b02015-12-09 08:01:46 +05301192 */
Viresh Kumarfa301842017-01-23 10:11:43 +05301193void dev_pm_opp_put_supported_hw(struct opp_table *opp_table)
Viresh Kumar7de36b02015-12-09 08:01:46 +05301194{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301195 /* Make sure there are no concurrent readers while updating opp_table */
1196 WARN_ON(!list_empty(&opp_table->opp_list));
Viresh Kumar7de36b02015-12-09 08:01:46 +05301197
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301198 kfree(opp_table->supported_hw);
1199 opp_table->supported_hw = NULL;
1200 opp_table->supported_hw_count = 0;
Viresh Kumar7de36b02015-12-09 08:01:46 +05301201
Viresh Kumarfa301842017-01-23 10:11:43 +05301202 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar7de36b02015-12-09 08:01:46 +05301203}
1204EXPORT_SYMBOL_GPL(dev_pm_opp_put_supported_hw);
1205
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301206/**
1207 * dev_pm_opp_set_prop_name() - Set prop-extn name
Viresh Kumara5da6442016-02-16 14:17:52 +05301208 * @dev: Device for which the prop-name has to be set.
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301209 * @name: name to postfix to properties.
1210 *
1211 * This is required only for the V2 bindings, and it enables a platform to
1212 * specify the extn to be used for certain property names. The properties to
1213 * which the extension will apply are opp-microvolt and opp-microamp. OPP core
1214 * should postfix the property name with -<name> while looking for them.
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301215 */
Viresh Kumarfa301842017-01-23 10:11:43 +05301216struct opp_table *dev_pm_opp_set_prop_name(struct device *dev, const char *name)
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301217{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301218 struct opp_table *opp_table;
Viresh Kumarfa301842017-01-23 10:11:43 +05301219 int ret;
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301220
Viresh Kumarfa301842017-01-23 10:11:43 +05301221 opp_table = dev_pm_opp_get_opp_table(dev);
1222 if (!opp_table)
1223 return ERR_PTR(-ENOMEM);
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301224
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301225 /* Make sure there are no concurrent readers while updating opp_table */
1226 WARN_ON(!list_empty(&opp_table->opp_list));
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301227
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301228 /* Do we already have a prop-name associated with opp_table? */
1229 if (opp_table->prop_name) {
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301230 dev_err(dev, "%s: Already have prop-name %s\n", __func__,
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301231 opp_table->prop_name);
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301232 ret = -EBUSY;
1233 goto err;
1234 }
1235
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301236 opp_table->prop_name = kstrdup(name, GFP_KERNEL);
1237 if (!opp_table->prop_name) {
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301238 ret = -ENOMEM;
1239 goto err;
1240 }
1241
Viresh Kumarfa301842017-01-23 10:11:43 +05301242 return opp_table;
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301243
1244err:
Viresh Kumarfa301842017-01-23 10:11:43 +05301245 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301246
Viresh Kumarfa301842017-01-23 10:11:43 +05301247 return ERR_PTR(ret);
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301248}
1249EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name);
1250
1251/**
1252 * dev_pm_opp_put_prop_name() - Releases resources blocked for prop-name
Viresh Kumarfa301842017-01-23 10:11:43 +05301253 * @opp_table: OPP table returned by dev_pm_opp_set_prop_name().
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301254 *
1255 * This is required only for the V2 bindings, and is called for a matching
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301256 * dev_pm_opp_set_prop_name(). Until this is called, the opp_table structure
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301257 * will not be freed.
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301258 */
Viresh Kumarfa301842017-01-23 10:11:43 +05301259void dev_pm_opp_put_prop_name(struct opp_table *opp_table)
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301260{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301261 /* Make sure there are no concurrent readers while updating opp_table */
1262 WARN_ON(!list_empty(&opp_table->opp_list));
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301263
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301264 if (!opp_table->prop_name) {
Viresh Kumarfa301842017-01-23 10:11:43 +05301265 pr_err("%s: Doesn't have a prop-name\n", __func__);
1266 return;
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301267 }
1268
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301269 kfree(opp_table->prop_name);
1270 opp_table->prop_name = NULL;
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301271
Viresh Kumarfa301842017-01-23 10:11:43 +05301272 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301273}
1274EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name);
1275
Viresh Kumar94735582016-12-01 16:28:20 +05301276static int _allocate_set_opp_data(struct opp_table *opp_table)
1277{
1278 struct dev_pm_set_opp_data *data;
1279 int len, count = opp_table->regulator_count;
1280
1281 if (WARN_ON(!count))
1282 return -EINVAL;
1283
1284 /* space for set_opp_data */
1285 len = sizeof(*data);
1286
1287 /* space for old_opp.supplies and new_opp.supplies */
1288 len += 2 * sizeof(struct dev_pm_opp_supply) * count;
1289
1290 data = kzalloc(len, GFP_KERNEL);
1291 if (!data)
1292 return -ENOMEM;
1293
1294 data->old_opp.supplies = (void *)(data + 1);
1295 data->new_opp.supplies = data->old_opp.supplies + count;
1296
1297 opp_table->set_opp_data = data;
1298
1299 return 0;
1300}
1301
1302static void _free_set_opp_data(struct opp_table *opp_table)
1303{
1304 kfree(opp_table->set_opp_data);
1305 opp_table->set_opp_data = NULL;
1306}
1307
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301308/**
Viresh Kumardfbe4672016-12-01 16:28:19 +05301309 * dev_pm_opp_set_regulators() - Set regulator names for the device
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301310 * @dev: Device for which regulator name is being set.
Viresh Kumardfbe4672016-12-01 16:28:19 +05301311 * @names: Array of pointers to the names of the regulator.
1312 * @count: Number of regulators.
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301313 *
1314 * In order to support OPP switching, OPP layer needs to know the name of the
Viresh Kumardfbe4672016-12-01 16:28:19 +05301315 * device's regulators, as the core would be required to switch voltages as
1316 * well.
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301317 *
1318 * This must be called before any OPPs are initialized for the device.
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301319 */
Viresh Kumardfbe4672016-12-01 16:28:19 +05301320struct opp_table *dev_pm_opp_set_regulators(struct device *dev,
1321 const char * const names[],
1322 unsigned int count)
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301323{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301324 struct opp_table *opp_table;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301325 struct regulator *reg;
Viresh Kumardfbe4672016-12-01 16:28:19 +05301326 int ret, i;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301327
Viresh Kumarfa301842017-01-23 10:11:43 +05301328 opp_table = dev_pm_opp_get_opp_table(dev);
1329 if (!opp_table)
1330 return ERR_PTR(-ENOMEM);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301331
1332 /* This should be called before OPPs are initialized */
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301333 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301334 ret = -EBUSY;
1335 goto err;
1336 }
1337
Viresh Kumardfbe4672016-12-01 16:28:19 +05301338 /* Already have regulators set */
Viresh Kumare231f8d2016-12-01 16:28:22 +05301339 if (opp_table->regulators) {
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301340 ret = -EBUSY;
1341 goto err;
1342 }
Viresh Kumardfbe4672016-12-01 16:28:19 +05301343
1344 opp_table->regulators = kmalloc_array(count,
1345 sizeof(*opp_table->regulators),
1346 GFP_KERNEL);
1347 if (!opp_table->regulators) {
1348 ret = -ENOMEM;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301349 goto err;
1350 }
1351
Viresh Kumardfbe4672016-12-01 16:28:19 +05301352 for (i = 0; i < count; i++) {
1353 reg = regulator_get_optional(dev, names[i]);
1354 if (IS_ERR(reg)) {
1355 ret = PTR_ERR(reg);
1356 if (ret != -EPROBE_DEFER)
1357 dev_err(dev, "%s: no regulator (%s) found: %d\n",
1358 __func__, names[i], ret);
1359 goto free_regulators;
1360 }
1361
1362 opp_table->regulators[i] = reg;
1363 }
1364
1365 opp_table->regulator_count = count;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301366
Viresh Kumar94735582016-12-01 16:28:20 +05301367 /* Allocate block only once to pass to set_opp() routines */
1368 ret = _allocate_set_opp_data(opp_table);
1369 if (ret)
1370 goto free_regulators;
1371
Stephen Boyd91291d92016-11-30 16:21:25 +05301372 return opp_table;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301373
Viresh Kumardfbe4672016-12-01 16:28:19 +05301374free_regulators:
1375 while (i != 0)
1376 regulator_put(opp_table->regulators[--i]);
1377
1378 kfree(opp_table->regulators);
1379 opp_table->regulators = NULL;
Viresh Kumar94735582016-12-01 16:28:20 +05301380 opp_table->regulator_count = 0;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301381err:
Viresh Kumarfa301842017-01-23 10:11:43 +05301382 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301383
Stephen Boyd91291d92016-11-30 16:21:25 +05301384 return ERR_PTR(ret);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301385}
Viresh Kumardfbe4672016-12-01 16:28:19 +05301386EXPORT_SYMBOL_GPL(dev_pm_opp_set_regulators);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301387
1388/**
Viresh Kumardfbe4672016-12-01 16:28:19 +05301389 * dev_pm_opp_put_regulators() - Releases resources blocked for regulator
1390 * @opp_table: OPP table returned from dev_pm_opp_set_regulators().
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301391 */
Viresh Kumardfbe4672016-12-01 16:28:19 +05301392void dev_pm_opp_put_regulators(struct opp_table *opp_table)
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301393{
Viresh Kumardfbe4672016-12-01 16:28:19 +05301394 int i;
1395
Viresh Kumardfbe4672016-12-01 16:28:19 +05301396 if (!opp_table->regulators) {
1397 pr_err("%s: Doesn't have regulators set\n", __func__);
Viresh Kumarfa301842017-01-23 10:11:43 +05301398 return;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301399 }
1400
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301401 /* Make sure there are no concurrent readers while updating opp_table */
1402 WARN_ON(!list_empty(&opp_table->opp_list));
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301403
Viresh Kumardfbe4672016-12-01 16:28:19 +05301404 for (i = opp_table->regulator_count - 1; i >= 0; i--)
1405 regulator_put(opp_table->regulators[i]);
1406
Viresh Kumar94735582016-12-01 16:28:20 +05301407 _free_set_opp_data(opp_table);
1408
Viresh Kumardfbe4672016-12-01 16:28:19 +05301409 kfree(opp_table->regulators);
1410 opp_table->regulators = NULL;
1411 opp_table->regulator_count = 0;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301412
Viresh Kumarfa301842017-01-23 10:11:43 +05301413 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301414}
Viresh Kumardfbe4672016-12-01 16:28:19 +05301415EXPORT_SYMBOL_GPL(dev_pm_opp_put_regulators);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301416
Viresh Kumar383934092014-11-25 16:04:18 +05301417/**
Viresh Kumar829a4e82017-06-21 10:29:13 +05301418 * dev_pm_opp_set_clkname() - Set clk name for the device
1419 * @dev: Device for which clk name is being set.
1420 * @name: Clk name.
1421 *
1422 * In order to support OPP switching, OPP layer needs to get pointer to the
1423 * clock for the device. Simple cases work fine without using this routine (i.e.
1424 * by passing connection-id as NULL), but for a device with multiple clocks
1425 * available, the OPP core needs to know the exact name of the clk to use.
1426 *
1427 * This must be called before any OPPs are initialized for the device.
1428 */
1429struct opp_table *dev_pm_opp_set_clkname(struct device *dev, const char *name)
1430{
1431 struct opp_table *opp_table;
1432 int ret;
1433
1434 opp_table = dev_pm_opp_get_opp_table(dev);
1435 if (!opp_table)
1436 return ERR_PTR(-ENOMEM);
1437
1438 /* This should be called before OPPs are initialized */
1439 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
1440 ret = -EBUSY;
1441 goto err;
1442 }
1443
1444 /* Already have default clk set, free it */
1445 if (!IS_ERR(opp_table->clk))
1446 clk_put(opp_table->clk);
1447
1448 /* Find clk for the device */
1449 opp_table->clk = clk_get(dev, name);
1450 if (IS_ERR(opp_table->clk)) {
1451 ret = PTR_ERR(opp_table->clk);
1452 if (ret != -EPROBE_DEFER) {
1453 dev_err(dev, "%s: Couldn't find clock: %d\n", __func__,
1454 ret);
1455 }
1456 goto err;
1457 }
1458
1459 return opp_table;
1460
1461err:
1462 dev_pm_opp_put_opp_table(opp_table);
1463
1464 return ERR_PTR(ret);
1465}
1466EXPORT_SYMBOL_GPL(dev_pm_opp_set_clkname);
1467
1468/**
1469 * dev_pm_opp_put_clkname() - Releases resources blocked for clk.
1470 * @opp_table: OPP table returned from dev_pm_opp_set_clkname().
1471 */
1472void dev_pm_opp_put_clkname(struct opp_table *opp_table)
1473{
1474 /* Make sure there are no concurrent readers while updating opp_table */
1475 WARN_ON(!list_empty(&opp_table->opp_list));
1476
1477 clk_put(opp_table->clk);
1478 opp_table->clk = ERR_PTR(-EINVAL);
1479
1480 dev_pm_opp_put_opp_table(opp_table);
1481}
1482EXPORT_SYMBOL_GPL(dev_pm_opp_put_clkname);
1483
1484/**
Viresh Kumar4dab1602016-12-01 16:28:21 +05301485 * dev_pm_opp_register_set_opp_helper() - Register custom set OPP helper
1486 * @dev: Device for which the helper is getting registered.
1487 * @set_opp: Custom set OPP helper.
1488 *
1489 * This is useful to support complex platforms (like platforms with multiple
1490 * regulators per device), instead of the generic OPP set rate helper.
1491 *
1492 * This must be called before any OPPs are initialized for the device.
Viresh Kumar4dab1602016-12-01 16:28:21 +05301493 */
Viresh Kumarfa301842017-01-23 10:11:43 +05301494struct opp_table *dev_pm_opp_register_set_opp_helper(struct device *dev,
Viresh Kumar4dab1602016-12-01 16:28:21 +05301495 int (*set_opp)(struct dev_pm_set_opp_data *data))
1496{
1497 struct opp_table *opp_table;
1498 int ret;
1499
1500 if (!set_opp)
Viresh Kumarfa301842017-01-23 10:11:43 +05301501 return ERR_PTR(-EINVAL);
Viresh Kumar4dab1602016-12-01 16:28:21 +05301502
Viresh Kumarfa301842017-01-23 10:11:43 +05301503 opp_table = dev_pm_opp_get_opp_table(dev);
1504 if (!opp_table)
1505 return ERR_PTR(-ENOMEM);
Viresh Kumar4dab1602016-12-01 16:28:21 +05301506
1507 /* This should be called before OPPs are initialized */
1508 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
1509 ret = -EBUSY;
1510 goto err;
1511 }
1512
1513 /* Already have custom set_opp helper */
1514 if (WARN_ON(opp_table->set_opp)) {
1515 ret = -EBUSY;
1516 goto err;
1517 }
1518
1519 opp_table->set_opp = set_opp;
1520
Viresh Kumarfa301842017-01-23 10:11:43 +05301521 return opp_table;
Viresh Kumar4dab1602016-12-01 16:28:21 +05301522
1523err:
Viresh Kumarfa301842017-01-23 10:11:43 +05301524 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar4dab1602016-12-01 16:28:21 +05301525
Viresh Kumarfa301842017-01-23 10:11:43 +05301526 return ERR_PTR(ret);
Viresh Kumar4dab1602016-12-01 16:28:21 +05301527}
1528EXPORT_SYMBOL_GPL(dev_pm_opp_register_set_opp_helper);
1529
1530/**
Viresh Kumar604a7ae2017-10-05 17:26:21 +05301531 * dev_pm_opp_unregister_set_opp_helper() - Releases resources blocked for
Viresh Kumar4dab1602016-12-01 16:28:21 +05301532 * set_opp helper
Viresh Kumarfa301842017-01-23 10:11:43 +05301533 * @opp_table: OPP table returned from dev_pm_opp_register_set_opp_helper().
Viresh Kumar4dab1602016-12-01 16:28:21 +05301534 *
Viresh Kumarfa301842017-01-23 10:11:43 +05301535 * Release resources blocked for platform specific set_opp helper.
Viresh Kumar4dab1602016-12-01 16:28:21 +05301536 */
Viresh Kumar604a7ae2017-10-05 17:26:21 +05301537void dev_pm_opp_unregister_set_opp_helper(struct opp_table *opp_table)
Viresh Kumar4dab1602016-12-01 16:28:21 +05301538{
Viresh Kumar4dab1602016-12-01 16:28:21 +05301539 if (!opp_table->set_opp) {
Viresh Kumarfa301842017-01-23 10:11:43 +05301540 pr_err("%s: Doesn't have custom set_opp helper set\n",
1541 __func__);
1542 return;
Viresh Kumar4dab1602016-12-01 16:28:21 +05301543 }
1544
1545 /* Make sure there are no concurrent readers while updating opp_table */
1546 WARN_ON(!list_empty(&opp_table->opp_list));
1547
1548 opp_table->set_opp = NULL;
1549
Viresh Kumarfa301842017-01-23 10:11:43 +05301550 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar4dab1602016-12-01 16:28:21 +05301551}
Viresh Kumar604a7ae2017-10-05 17:26:21 +05301552EXPORT_SYMBOL_GPL(dev_pm_opp_unregister_set_opp_helper);
Viresh Kumar4dab1602016-12-01 16:28:21 +05301553
1554/**
Nishanth Menone1f60b22010-10-13 00:13:10 +02001555 * dev_pm_opp_add() - Add an OPP table from a table definitions
1556 * @dev: device for which we do this operation
Nishanth Menon47d43ba2013-09-19 16:03:51 -05001557 * @freq: Frequency in Hz for this OPP
Nishanth Menone1f60b22010-10-13 00:13:10 +02001558 * @u_volt: Voltage in uVolts for this OPP
1559 *
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301560 * This function adds an opp definition to the opp table and returns status.
Nishanth Menon47d43ba2013-09-19 16:03:51 -05001561 * The opp is made available by default and it can be controlled using
Nishanth Menone1f60b22010-10-13 00:13:10 +02001562 * dev_pm_opp_enable/disable functions.
1563 *
Viresh Kumara7470db2014-11-25 16:04:17 +05301564 * Return:
1565 * 0 On success OR
1566 * Duplicate OPPs (both freq and volt are same) and opp->available
1567 * -EEXIST Freq are same and volt are different OR
1568 * Duplicate OPPs (both freq and volt are same) and !opp->available
Viresh Kumar383934092014-11-25 16:04:18 +05301569 * -ENOMEM Memory allocation failure
Viresh Kumara7470db2014-11-25 16:04:17 +05301570 */
Nishanth Menone1f60b22010-10-13 00:13:10 +02001571int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
1572{
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301573 struct opp_table *opp_table;
1574 int ret;
1575
Viresh Kumarb83c1892017-01-23 10:11:45 +05301576 opp_table = dev_pm_opp_get_opp_table(dev);
1577 if (!opp_table)
1578 return -ENOMEM;
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301579
1580 ret = _opp_add_v1(opp_table, dev, freq, u_volt, true);
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301581
Viresh Kumarb83c1892017-01-23 10:11:45 +05301582 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301583 return ret;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001584}
1585EXPORT_SYMBOL_GPL(dev_pm_opp_add);
1586
Nishanth Menone1f60b22010-10-13 00:13:10 +02001587/**
Nishanth Menon327854c2014-12-24 11:22:56 -06001588 * _opp_set_availability() - helper to set the availability of an opp
Nishanth Menone1f60b22010-10-13 00:13:10 +02001589 * @dev: device for which we do this operation
1590 * @freq: OPP frequency to modify availability
1591 * @availability_req: availability status requested for this opp
1592 *
Viresh Kumar052c6f12017-01-23 10:11:49 +05301593 * Set the availability of an OPP, opp_{enable,disable} share a common logic
1594 * which is isolated here.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001595 *
Nishanth Menon984f16c2014-12-24 11:22:57 -06001596 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
Stephen Boyde1a2d492015-09-24 12:28:44 -07001597 * copy operation, returns 0 if no modification was done OR modification was
Nishanth Menone1f60b22010-10-13 00:13:10 +02001598 * successful.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001599 */
Nishanth Menon327854c2014-12-24 11:22:56 -06001600static int _opp_set_availability(struct device *dev, unsigned long freq,
1601 bool availability_req)
Nishanth Menone1f60b22010-10-13 00:13:10 +02001602{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301603 struct opp_table *opp_table;
Viresh Kumara7f39872017-01-23 10:11:50 +05301604 struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001605 int r = 0;
1606
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301607 /* Find the opp_table */
1608 opp_table = _find_opp_table(dev);
1609 if (IS_ERR(opp_table)) {
1610 r = PTR_ERR(opp_table);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001611 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
Viresh Kumara7f39872017-01-23 10:11:50 +05301612 return r;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001613 }
1614
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301615 mutex_lock(&opp_table->lock);
1616
Nishanth Menone1f60b22010-10-13 00:13:10 +02001617 /* Do we have the frequency? */
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301618 list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
Nishanth Menone1f60b22010-10-13 00:13:10 +02001619 if (tmp_opp->rate == freq) {
1620 opp = tmp_opp;
1621 break;
1622 }
1623 }
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301624
Nishanth Menone1f60b22010-10-13 00:13:10 +02001625 if (IS_ERR(opp)) {
1626 r = PTR_ERR(opp);
1627 goto unlock;
1628 }
1629
1630 /* Is update really needed? */
1631 if (opp->available == availability_req)
1632 goto unlock;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001633
Viresh Kumara7f39872017-01-23 10:11:50 +05301634 opp->available = availability_req;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001635
Viresh Kumare4d8ae02017-09-21 10:44:36 -07001636 dev_pm_opp_get(opp);
1637 mutex_unlock(&opp_table->lock);
1638
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001639 /* Notify the change of the OPP availability */
1640 if (availability_req)
Viresh Kumar052c6f12017-01-23 10:11:49 +05301641 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE,
Viresh Kumara7f39872017-01-23 10:11:50 +05301642 opp);
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001643 else
Viresh Kumar052c6f12017-01-23 10:11:49 +05301644 blocking_notifier_call_chain(&opp_table->head,
Viresh Kumara7f39872017-01-23 10:11:50 +05301645 OPP_EVENT_DISABLE, opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001646
Viresh Kumare4d8ae02017-09-21 10:44:36 -07001647 dev_pm_opp_put(opp);
1648 goto put_table;
1649
Nishanth Menone1f60b22010-10-13 00:13:10 +02001650unlock:
Viresh Kumar5b650b32017-01-23 10:11:48 +05301651 mutex_unlock(&opp_table->lock);
Viresh Kumare4d8ae02017-09-21 10:44:36 -07001652put_table:
Viresh Kumar5b650b32017-01-23 10:11:48 +05301653 dev_pm_opp_put_opp_table(opp_table);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001654 return r;
1655}
1656
1657/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001658 * dev_pm_opp_enable() - Enable a specific OPP
Nishanth Menone1f60b22010-10-13 00:13:10 +02001659 * @dev: device for which we do this operation
1660 * @freq: OPP frequency to enable
1661 *
1662 * Enables a provided opp. If the operation is valid, this returns 0, else the
1663 * corresponding error value. It is meant to be used for users an OPP available
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001664 * after being temporarily made unavailable with dev_pm_opp_disable.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001665 *
Nishanth Menon984f16c2014-12-24 11:22:57 -06001666 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
Stephen Boyde1a2d492015-09-24 12:28:44 -07001667 * copy operation, returns 0 if no modification was done OR modification was
Nishanth Menon984f16c2014-12-24 11:22:57 -06001668 * successful.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001669 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001670int dev_pm_opp_enable(struct device *dev, unsigned long freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +02001671{
Nishanth Menon327854c2014-12-24 11:22:56 -06001672 return _opp_set_availability(dev, freq, true);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001673}
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001674EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001675
1676/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001677 * dev_pm_opp_disable() - Disable a specific OPP
Nishanth Menone1f60b22010-10-13 00:13:10 +02001678 * @dev: device for which we do this operation
1679 * @freq: OPP frequency to disable
1680 *
1681 * Disables a provided opp. If the operation is valid, this returns
1682 * 0, else the corresponding error value. It is meant to be a temporary
1683 * control by users to make this OPP not available until the circumstances are
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001684 * right to make it available again (with a call to dev_pm_opp_enable).
Nishanth Menone1f60b22010-10-13 00:13:10 +02001685 *
Nishanth Menon984f16c2014-12-24 11:22:57 -06001686 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
Stephen Boyde1a2d492015-09-24 12:28:44 -07001687 * copy operation, returns 0 if no modification was done OR modification was
Nishanth Menon984f16c2014-12-24 11:22:57 -06001688 * successful.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001689 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001690int dev_pm_opp_disable(struct device *dev, unsigned long freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +02001691{
Nishanth Menon327854c2014-12-24 11:22:56 -06001692 return _opp_set_availability(dev, freq, false);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001693}
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001694EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001695
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001696/**
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301697 * dev_pm_opp_register_notifier() - Register OPP notifier for the device
1698 * @dev: Device for which notifier needs to be registered
1699 * @nb: Notifier block to be registered
Nishanth Menon984f16c2014-12-24 11:22:57 -06001700 *
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301701 * Return: 0 on success or a negative error value.
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001702 */
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301703int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb)
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001704{
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301705 struct opp_table *opp_table;
1706 int ret;
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001707
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301708 opp_table = _find_opp_table(dev);
Viresh Kumar5b650b32017-01-23 10:11:48 +05301709 if (IS_ERR(opp_table))
1710 return PTR_ERR(opp_table);
1711
Viresh Kumar052c6f12017-01-23 10:11:49 +05301712 ret = blocking_notifier_chain_register(&opp_table->head, nb);
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301713
Viresh Kumar5b650b32017-01-23 10:11:48 +05301714 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301715
1716 return ret;
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001717}
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301718EXPORT_SYMBOL(dev_pm_opp_register_notifier);
1719
1720/**
1721 * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device
1722 * @dev: Device for which notifier needs to be unregistered
1723 * @nb: Notifier block to be unregistered
1724 *
1725 * Return: 0 on success or a negative error value.
1726 */
1727int dev_pm_opp_unregister_notifier(struct device *dev,
1728 struct notifier_block *nb)
1729{
1730 struct opp_table *opp_table;
1731 int ret;
1732
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301733 opp_table = _find_opp_table(dev);
Viresh Kumar5b650b32017-01-23 10:11:48 +05301734 if (IS_ERR(opp_table))
1735 return PTR_ERR(opp_table);
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301736
Viresh Kumar052c6f12017-01-23 10:11:49 +05301737 ret = blocking_notifier_chain_unregister(&opp_table->head, nb);
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301738
Viresh Kumar5b650b32017-01-23 10:11:48 +05301739 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301740
1741 return ret;
1742}
1743EXPORT_SYMBOL(dev_pm_opp_unregister_notifier);
Shawn Guob496dfb2012-09-05 01:09:12 +02001744
Sudeep Holla411466c2016-05-03 15:05:04 +01001745/*
1746 * Free OPPs either created using static entries present in DT or even the
1747 * dynamically added entries based on remove_all param.
Shawn Guob496dfb2012-09-05 01:09:12 +02001748 */
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301749void _dev_pm_opp_remove_table(struct opp_table *opp_table, struct device *dev,
1750 bool remove_all)
Viresh Kumar9274c892017-01-02 14:41:00 +05301751{
1752 struct dev_pm_opp *opp, *tmp;
1753
Viresh Kumar9274c892017-01-02 14:41:00 +05301754 /* Find if opp_table manages a single device */
1755 if (list_is_singular(&opp_table->dev_list)) {
1756 /* Free static OPPs */
1757 list_for_each_entry_safe(opp, tmp, &opp_table->opp_list, node) {
1758 if (remove_all || !opp->dynamic)
Viresh Kumar70347642017-01-23 10:11:46 +05301759 dev_pm_opp_put(opp);
Viresh Kumar9274c892017-01-02 14:41:00 +05301760 }
Viresh Kumar009acd12017-10-11 12:54:14 +05301761
1762 /*
1763 * The OPP table is getting removed, drop the performance state
1764 * constraints.
1765 */
1766 if (opp_table->genpd_performance_state)
1767 dev_pm_genpd_set_performance_state(dev, 0);
Viresh Kumar9274c892017-01-02 14:41:00 +05301768 } else {
1769 _remove_opp_dev(_find_opp_dev(dev, opp_table), opp_table);
1770 }
1771}
1772
1773void _dev_pm_opp_find_and_remove_table(struct device *dev, bool remove_all)
Viresh Kumar737002b2015-07-29 16:22:58 +05301774{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301775 struct opp_table *opp_table;
Viresh Kumar737002b2015-07-29 16:22:58 +05301776
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301777 /* Check for existing table for 'dev' */
1778 opp_table = _find_opp_table(dev);
1779 if (IS_ERR(opp_table)) {
1780 int error = PTR_ERR(opp_table);
Viresh Kumar737002b2015-07-29 16:22:58 +05301781
1782 if (error != -ENODEV)
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301783 WARN(1, "%s: opp_table: %d\n",
Viresh Kumar737002b2015-07-29 16:22:58 +05301784 IS_ERR_OR_NULL(dev) ?
1785 "Invalid device" : dev_name(dev),
1786 error);
Viresh Kumar5b650b32017-01-23 10:11:48 +05301787 return;
Viresh Kumar737002b2015-07-29 16:22:58 +05301788 }
1789
Viresh Kumar9274c892017-01-02 14:41:00 +05301790 _dev_pm_opp_remove_table(opp_table, dev, remove_all);
Viresh Kumar737002b2015-07-29 16:22:58 +05301791
Viresh Kumar5b650b32017-01-23 10:11:48 +05301792 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar737002b2015-07-29 16:22:58 +05301793}
Viresh Kumar129eec52014-11-27 08:54:06 +05301794
1795/**
Sudeep Holla411466c2016-05-03 15:05:04 +01001796 * dev_pm_opp_remove_table() - Free all OPPs associated with the device
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301797 * @dev: device pointer used to lookup OPP table.
Viresh Kumar129eec52014-11-27 08:54:06 +05301798 *
Sudeep Holla411466c2016-05-03 15:05:04 +01001799 * Free both OPPs created using static entries present in DT and the
1800 * dynamically added entries.
Viresh Kumar129eec52014-11-27 08:54:06 +05301801 */
Sudeep Holla411466c2016-05-03 15:05:04 +01001802void dev_pm_opp_remove_table(struct device *dev)
Viresh Kumar129eec52014-11-27 08:54:06 +05301803{
Viresh Kumar9274c892017-01-02 14:41:00 +05301804 _dev_pm_opp_find_and_remove_table(dev, true);
Viresh Kumar8d4d4e92015-06-12 17:10:38 +05301805}
Sudeep Holla411466c2016-05-03 15:05:04 +01001806EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table);