blob: 780c89a49d1816094a771be7d2eca24efa8242c8 [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 Kumar01fb4d32015-12-09 08:01:47 +05301219
Viresh Kumarfa301842017-01-23 10:11:43 +05301220 opp_table = dev_pm_opp_get_opp_table(dev);
1221 if (!opp_table)
1222 return ERR_PTR(-ENOMEM);
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301223
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301224 /* Make sure there are no concurrent readers while updating opp_table */
1225 WARN_ON(!list_empty(&opp_table->opp_list));
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301226
Viresh Kumar878ec1a2018-05-22 16:38:08 +05301227 /* Another CPU that shares the OPP table has set the property ? */
1228 if (opp_table->prop_name)
1229 return opp_table;
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301230
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301231 opp_table->prop_name = kstrdup(name, GFP_KERNEL);
1232 if (!opp_table->prop_name) {
Viresh Kumar878ec1a2018-05-22 16:38:08 +05301233 dev_pm_opp_put_opp_table(opp_table);
1234 return ERR_PTR(-ENOMEM);
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301235 }
1236
Viresh Kumarfa301842017-01-23 10:11:43 +05301237 return opp_table;
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301238}
1239EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name);
1240
1241/**
1242 * dev_pm_opp_put_prop_name() - Releases resources blocked for prop-name
Viresh Kumarfa301842017-01-23 10:11:43 +05301243 * @opp_table: OPP table returned by dev_pm_opp_set_prop_name().
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301244 *
1245 * This is required only for the V2 bindings, and is called for a matching
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301246 * dev_pm_opp_set_prop_name(). Until this is called, the opp_table structure
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301247 * will not be freed.
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301248 */
Viresh Kumarfa301842017-01-23 10:11:43 +05301249void dev_pm_opp_put_prop_name(struct opp_table *opp_table)
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301250{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301251 /* Make sure there are no concurrent readers while updating opp_table */
1252 WARN_ON(!list_empty(&opp_table->opp_list));
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301253
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301254 kfree(opp_table->prop_name);
1255 opp_table->prop_name = NULL;
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301256
Viresh Kumarfa301842017-01-23 10:11:43 +05301257 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301258}
1259EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name);
1260
Viresh Kumar94735582016-12-01 16:28:20 +05301261static int _allocate_set_opp_data(struct opp_table *opp_table)
1262{
1263 struct dev_pm_set_opp_data *data;
1264 int len, count = opp_table->regulator_count;
1265
1266 if (WARN_ON(!count))
1267 return -EINVAL;
1268
1269 /* space for set_opp_data */
1270 len = sizeof(*data);
1271
1272 /* space for old_opp.supplies and new_opp.supplies */
1273 len += 2 * sizeof(struct dev_pm_opp_supply) * count;
1274
1275 data = kzalloc(len, GFP_KERNEL);
1276 if (!data)
1277 return -ENOMEM;
1278
1279 data->old_opp.supplies = (void *)(data + 1);
1280 data->new_opp.supplies = data->old_opp.supplies + count;
1281
1282 opp_table->set_opp_data = data;
1283
1284 return 0;
1285}
1286
1287static void _free_set_opp_data(struct opp_table *opp_table)
1288{
1289 kfree(opp_table->set_opp_data);
1290 opp_table->set_opp_data = NULL;
1291}
1292
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301293/**
Viresh Kumardfbe4672016-12-01 16:28:19 +05301294 * dev_pm_opp_set_regulators() - Set regulator names for the device
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301295 * @dev: Device for which regulator name is being set.
Viresh Kumardfbe4672016-12-01 16:28:19 +05301296 * @names: Array of pointers to the names of the regulator.
1297 * @count: Number of regulators.
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301298 *
1299 * In order to support OPP switching, OPP layer needs to know the name of the
Viresh Kumardfbe4672016-12-01 16:28:19 +05301300 * device's regulators, as the core would be required to switch voltages as
1301 * well.
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301302 *
1303 * This must be called before any OPPs are initialized for the device.
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301304 */
Viresh Kumardfbe4672016-12-01 16:28:19 +05301305struct opp_table *dev_pm_opp_set_regulators(struct device *dev,
1306 const char * const names[],
1307 unsigned int count)
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301308{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301309 struct opp_table *opp_table;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301310 struct regulator *reg;
Viresh Kumardfbe4672016-12-01 16:28:19 +05301311 int ret, i;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301312
Viresh Kumarfa301842017-01-23 10:11:43 +05301313 opp_table = dev_pm_opp_get_opp_table(dev);
1314 if (!opp_table)
1315 return ERR_PTR(-ENOMEM);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301316
1317 /* This should be called before OPPs are initialized */
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301318 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301319 ret = -EBUSY;
1320 goto err;
1321 }
1322
Viresh Kumar779b7832018-05-22 16:38:08 +05301323 /* Another CPU that shares the OPP table has set the regulators ? */
1324 if (opp_table->regulators)
1325 return opp_table;
Viresh Kumardfbe4672016-12-01 16:28:19 +05301326
1327 opp_table->regulators = kmalloc_array(count,
1328 sizeof(*opp_table->regulators),
1329 GFP_KERNEL);
1330 if (!opp_table->regulators) {
1331 ret = -ENOMEM;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301332 goto err;
1333 }
1334
Viresh Kumardfbe4672016-12-01 16:28:19 +05301335 for (i = 0; i < count; i++) {
1336 reg = regulator_get_optional(dev, names[i]);
1337 if (IS_ERR(reg)) {
1338 ret = PTR_ERR(reg);
1339 if (ret != -EPROBE_DEFER)
1340 dev_err(dev, "%s: no regulator (%s) found: %d\n",
1341 __func__, names[i], ret);
1342 goto free_regulators;
1343 }
1344
1345 opp_table->regulators[i] = reg;
1346 }
1347
1348 opp_table->regulator_count = count;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301349
Viresh Kumar94735582016-12-01 16:28:20 +05301350 /* Allocate block only once to pass to set_opp() routines */
1351 ret = _allocate_set_opp_data(opp_table);
1352 if (ret)
1353 goto free_regulators;
1354
Stephen Boyd91291d92016-11-30 16:21:25 +05301355 return opp_table;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301356
Viresh Kumardfbe4672016-12-01 16:28:19 +05301357free_regulators:
1358 while (i != 0)
1359 regulator_put(opp_table->regulators[--i]);
1360
1361 kfree(opp_table->regulators);
1362 opp_table->regulators = NULL;
Viresh Kumar94735582016-12-01 16:28:20 +05301363 opp_table->regulator_count = 0;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301364err:
Viresh Kumarfa301842017-01-23 10:11:43 +05301365 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301366
Stephen Boyd91291d92016-11-30 16:21:25 +05301367 return ERR_PTR(ret);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301368}
Viresh Kumardfbe4672016-12-01 16:28:19 +05301369EXPORT_SYMBOL_GPL(dev_pm_opp_set_regulators);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301370
1371/**
Viresh Kumardfbe4672016-12-01 16:28:19 +05301372 * dev_pm_opp_put_regulators() - Releases resources blocked for regulator
1373 * @opp_table: OPP table returned from dev_pm_opp_set_regulators().
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301374 */
Viresh Kumardfbe4672016-12-01 16:28:19 +05301375void dev_pm_opp_put_regulators(struct opp_table *opp_table)
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301376{
Viresh Kumardfbe4672016-12-01 16:28:19 +05301377 int i;
1378
Viresh Kumar779b7832018-05-22 16:38:08 +05301379 if (!opp_table->regulators)
1380 goto put_opp_table;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301381
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301382 /* Make sure there are no concurrent readers while updating opp_table */
1383 WARN_ON(!list_empty(&opp_table->opp_list));
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301384
Viresh Kumardfbe4672016-12-01 16:28:19 +05301385 for (i = opp_table->regulator_count - 1; i >= 0; i--)
1386 regulator_put(opp_table->regulators[i]);
1387
Viresh Kumar94735582016-12-01 16:28:20 +05301388 _free_set_opp_data(opp_table);
1389
Viresh Kumardfbe4672016-12-01 16:28:19 +05301390 kfree(opp_table->regulators);
1391 opp_table->regulators = NULL;
1392 opp_table->regulator_count = 0;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301393
Viresh Kumar779b7832018-05-22 16:38:08 +05301394put_opp_table:
Viresh Kumarfa301842017-01-23 10:11:43 +05301395 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301396}
Viresh Kumardfbe4672016-12-01 16:28:19 +05301397EXPORT_SYMBOL_GPL(dev_pm_opp_put_regulators);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301398
Viresh Kumar383934092014-11-25 16:04:18 +05301399/**
Viresh Kumar829a4e82017-06-21 10:29:13 +05301400 * dev_pm_opp_set_clkname() - Set clk name for the device
1401 * @dev: Device for which clk name is being set.
1402 * @name: Clk name.
1403 *
1404 * In order to support OPP switching, OPP layer needs to get pointer to the
1405 * clock for the device. Simple cases work fine without using this routine (i.e.
1406 * by passing connection-id as NULL), but for a device with multiple clocks
1407 * available, the OPP core needs to know the exact name of the clk to use.
1408 *
1409 * This must be called before any OPPs are initialized for the device.
1410 */
1411struct opp_table *dev_pm_opp_set_clkname(struct device *dev, const char *name)
1412{
1413 struct opp_table *opp_table;
1414 int ret;
1415
1416 opp_table = dev_pm_opp_get_opp_table(dev);
1417 if (!opp_table)
1418 return ERR_PTR(-ENOMEM);
1419
1420 /* This should be called before OPPs are initialized */
1421 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
1422 ret = -EBUSY;
1423 goto err;
1424 }
1425
1426 /* Already have default clk set, free it */
1427 if (!IS_ERR(opp_table->clk))
1428 clk_put(opp_table->clk);
1429
1430 /* Find clk for the device */
1431 opp_table->clk = clk_get(dev, name);
1432 if (IS_ERR(opp_table->clk)) {
1433 ret = PTR_ERR(opp_table->clk);
1434 if (ret != -EPROBE_DEFER) {
1435 dev_err(dev, "%s: Couldn't find clock: %d\n", __func__,
1436 ret);
1437 }
1438 goto err;
1439 }
1440
1441 return opp_table;
1442
1443err:
1444 dev_pm_opp_put_opp_table(opp_table);
1445
1446 return ERR_PTR(ret);
1447}
1448EXPORT_SYMBOL_GPL(dev_pm_opp_set_clkname);
1449
1450/**
1451 * dev_pm_opp_put_clkname() - Releases resources blocked for clk.
1452 * @opp_table: OPP table returned from dev_pm_opp_set_clkname().
1453 */
1454void dev_pm_opp_put_clkname(struct opp_table *opp_table)
1455{
1456 /* Make sure there are no concurrent readers while updating opp_table */
1457 WARN_ON(!list_empty(&opp_table->opp_list));
1458
1459 clk_put(opp_table->clk);
1460 opp_table->clk = ERR_PTR(-EINVAL);
1461
1462 dev_pm_opp_put_opp_table(opp_table);
1463}
1464EXPORT_SYMBOL_GPL(dev_pm_opp_put_clkname);
1465
1466/**
Viresh Kumar4dab1602016-12-01 16:28:21 +05301467 * dev_pm_opp_register_set_opp_helper() - Register custom set OPP helper
1468 * @dev: Device for which the helper is getting registered.
1469 * @set_opp: Custom set OPP helper.
1470 *
1471 * This is useful to support complex platforms (like platforms with multiple
1472 * regulators per device), instead of the generic OPP set rate helper.
1473 *
1474 * This must be called before any OPPs are initialized for the device.
Viresh Kumar4dab1602016-12-01 16:28:21 +05301475 */
Viresh Kumarfa301842017-01-23 10:11:43 +05301476struct opp_table *dev_pm_opp_register_set_opp_helper(struct device *dev,
Viresh Kumar4dab1602016-12-01 16:28:21 +05301477 int (*set_opp)(struct dev_pm_set_opp_data *data))
1478{
1479 struct opp_table *opp_table;
1480 int ret;
1481
1482 if (!set_opp)
Viresh Kumarfa301842017-01-23 10:11:43 +05301483 return ERR_PTR(-EINVAL);
Viresh Kumar4dab1602016-12-01 16:28:21 +05301484
Viresh Kumarfa301842017-01-23 10:11:43 +05301485 opp_table = dev_pm_opp_get_opp_table(dev);
1486 if (!opp_table)
1487 return ERR_PTR(-ENOMEM);
Viresh Kumar4dab1602016-12-01 16:28:21 +05301488
1489 /* This should be called before OPPs are initialized */
1490 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
1491 ret = -EBUSY;
1492 goto err;
1493 }
1494
1495 /* Already have custom set_opp helper */
1496 if (WARN_ON(opp_table->set_opp)) {
1497 ret = -EBUSY;
1498 goto err;
1499 }
1500
1501 opp_table->set_opp = set_opp;
1502
Viresh Kumarfa301842017-01-23 10:11:43 +05301503 return opp_table;
Viresh Kumar4dab1602016-12-01 16:28:21 +05301504
1505err:
Viresh Kumarfa301842017-01-23 10:11:43 +05301506 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar4dab1602016-12-01 16:28:21 +05301507
Viresh Kumarfa301842017-01-23 10:11:43 +05301508 return ERR_PTR(ret);
Viresh Kumar4dab1602016-12-01 16:28:21 +05301509}
1510EXPORT_SYMBOL_GPL(dev_pm_opp_register_set_opp_helper);
1511
1512/**
Viresh Kumar604a7ae2017-10-05 17:26:21 +05301513 * dev_pm_opp_unregister_set_opp_helper() - Releases resources blocked for
Viresh Kumar4dab1602016-12-01 16:28:21 +05301514 * set_opp helper
Viresh Kumarfa301842017-01-23 10:11:43 +05301515 * @opp_table: OPP table returned from dev_pm_opp_register_set_opp_helper().
Viresh Kumar4dab1602016-12-01 16:28:21 +05301516 *
Viresh Kumarfa301842017-01-23 10:11:43 +05301517 * Release resources blocked for platform specific set_opp helper.
Viresh Kumar4dab1602016-12-01 16:28:21 +05301518 */
Viresh Kumar604a7ae2017-10-05 17:26:21 +05301519void dev_pm_opp_unregister_set_opp_helper(struct opp_table *opp_table)
Viresh Kumar4dab1602016-12-01 16:28:21 +05301520{
Viresh Kumar4dab1602016-12-01 16:28:21 +05301521 if (!opp_table->set_opp) {
Viresh Kumarfa301842017-01-23 10:11:43 +05301522 pr_err("%s: Doesn't have custom set_opp helper set\n",
1523 __func__);
1524 return;
Viresh Kumar4dab1602016-12-01 16:28:21 +05301525 }
1526
1527 /* Make sure there are no concurrent readers while updating opp_table */
1528 WARN_ON(!list_empty(&opp_table->opp_list));
1529
1530 opp_table->set_opp = NULL;
1531
Viresh Kumarfa301842017-01-23 10:11:43 +05301532 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar4dab1602016-12-01 16:28:21 +05301533}
Viresh Kumar604a7ae2017-10-05 17:26:21 +05301534EXPORT_SYMBOL_GPL(dev_pm_opp_unregister_set_opp_helper);
Viresh Kumar4dab1602016-12-01 16:28:21 +05301535
1536/**
Nishanth Menone1f60b22010-10-13 00:13:10 +02001537 * dev_pm_opp_add() - Add an OPP table from a table definitions
1538 * @dev: device for which we do this operation
Nishanth Menon47d43ba2013-09-19 16:03:51 -05001539 * @freq: Frequency in Hz for this OPP
Nishanth Menone1f60b22010-10-13 00:13:10 +02001540 * @u_volt: Voltage in uVolts for this OPP
1541 *
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301542 * This function adds an opp definition to the opp table and returns status.
Nishanth Menon47d43ba2013-09-19 16:03:51 -05001543 * The opp is made available by default and it can be controlled using
Nishanth Menone1f60b22010-10-13 00:13:10 +02001544 * dev_pm_opp_enable/disable functions.
1545 *
Viresh Kumara7470db2014-11-25 16:04:17 +05301546 * Return:
1547 * 0 On success OR
1548 * Duplicate OPPs (both freq and volt are same) and opp->available
1549 * -EEXIST Freq are same and volt are different OR
1550 * Duplicate OPPs (both freq and volt are same) and !opp->available
Viresh Kumar383934092014-11-25 16:04:18 +05301551 * -ENOMEM Memory allocation failure
Viresh Kumara7470db2014-11-25 16:04:17 +05301552 */
Nishanth Menone1f60b22010-10-13 00:13:10 +02001553int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
1554{
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301555 struct opp_table *opp_table;
1556 int ret;
1557
Viresh Kumarb83c1892017-01-23 10:11:45 +05301558 opp_table = dev_pm_opp_get_opp_table(dev);
1559 if (!opp_table)
1560 return -ENOMEM;
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301561
1562 ret = _opp_add_v1(opp_table, dev, freq, u_volt, true);
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301563
Viresh Kumarb83c1892017-01-23 10:11:45 +05301564 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301565 return ret;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001566}
1567EXPORT_SYMBOL_GPL(dev_pm_opp_add);
1568
Nishanth Menone1f60b22010-10-13 00:13:10 +02001569/**
Nishanth Menon327854c2014-12-24 11:22:56 -06001570 * _opp_set_availability() - helper to set the availability of an opp
Nishanth Menone1f60b22010-10-13 00:13:10 +02001571 * @dev: device for which we do this operation
1572 * @freq: OPP frequency to modify availability
1573 * @availability_req: availability status requested for this opp
1574 *
Viresh Kumar052c6f12017-01-23 10:11:49 +05301575 * Set the availability of an OPP, opp_{enable,disable} share a common logic
1576 * which is isolated here.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001577 *
Nishanth Menon984f16c2014-12-24 11:22:57 -06001578 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
Stephen Boyde1a2d492015-09-24 12:28:44 -07001579 * copy operation, returns 0 if no modification was done OR modification was
Nishanth Menone1f60b22010-10-13 00:13:10 +02001580 * successful.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001581 */
Nishanth Menon327854c2014-12-24 11:22:56 -06001582static int _opp_set_availability(struct device *dev, unsigned long freq,
1583 bool availability_req)
Nishanth Menone1f60b22010-10-13 00:13:10 +02001584{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301585 struct opp_table *opp_table;
Viresh Kumara7f39872017-01-23 10:11:50 +05301586 struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001587 int r = 0;
1588
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301589 /* Find the opp_table */
1590 opp_table = _find_opp_table(dev);
1591 if (IS_ERR(opp_table)) {
1592 r = PTR_ERR(opp_table);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001593 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
Viresh Kumara7f39872017-01-23 10:11:50 +05301594 return r;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001595 }
1596
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301597 mutex_lock(&opp_table->lock);
1598
Nishanth Menone1f60b22010-10-13 00:13:10 +02001599 /* Do we have the frequency? */
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301600 list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
Nishanth Menone1f60b22010-10-13 00:13:10 +02001601 if (tmp_opp->rate == freq) {
1602 opp = tmp_opp;
1603 break;
1604 }
1605 }
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301606
Nishanth Menone1f60b22010-10-13 00:13:10 +02001607 if (IS_ERR(opp)) {
1608 r = PTR_ERR(opp);
1609 goto unlock;
1610 }
1611
1612 /* Is update really needed? */
1613 if (opp->available == availability_req)
1614 goto unlock;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001615
Viresh Kumara7f39872017-01-23 10:11:50 +05301616 opp->available = availability_req;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001617
Viresh Kumare4d8ae02017-09-21 10:44:36 -07001618 dev_pm_opp_get(opp);
1619 mutex_unlock(&opp_table->lock);
1620
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001621 /* Notify the change of the OPP availability */
1622 if (availability_req)
Viresh Kumar052c6f12017-01-23 10:11:49 +05301623 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE,
Viresh Kumara7f39872017-01-23 10:11:50 +05301624 opp);
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001625 else
Viresh Kumar052c6f12017-01-23 10:11:49 +05301626 blocking_notifier_call_chain(&opp_table->head,
Viresh Kumara7f39872017-01-23 10:11:50 +05301627 OPP_EVENT_DISABLE, opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001628
Viresh Kumare4d8ae02017-09-21 10:44:36 -07001629 dev_pm_opp_put(opp);
1630 goto put_table;
1631
Nishanth Menone1f60b22010-10-13 00:13:10 +02001632unlock:
Viresh Kumar5b650b32017-01-23 10:11:48 +05301633 mutex_unlock(&opp_table->lock);
Viresh Kumare4d8ae02017-09-21 10:44:36 -07001634put_table:
Viresh Kumar5b650b32017-01-23 10:11:48 +05301635 dev_pm_opp_put_opp_table(opp_table);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001636 return r;
1637}
1638
1639/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001640 * dev_pm_opp_enable() - Enable a specific OPP
Nishanth Menone1f60b22010-10-13 00:13:10 +02001641 * @dev: device for which we do this operation
1642 * @freq: OPP frequency to enable
1643 *
1644 * Enables a provided opp. If the operation is valid, this returns 0, else the
1645 * corresponding error value. It is meant to be used for users an OPP available
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001646 * after being temporarily made unavailable with dev_pm_opp_disable.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001647 *
Nishanth Menon984f16c2014-12-24 11:22:57 -06001648 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
Stephen Boyde1a2d492015-09-24 12:28:44 -07001649 * copy operation, returns 0 if no modification was done OR modification was
Nishanth Menon984f16c2014-12-24 11:22:57 -06001650 * successful.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001651 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001652int dev_pm_opp_enable(struct device *dev, unsigned long freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +02001653{
Nishanth Menon327854c2014-12-24 11:22:56 -06001654 return _opp_set_availability(dev, freq, true);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001655}
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001656EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001657
1658/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001659 * dev_pm_opp_disable() - Disable a specific OPP
Nishanth Menone1f60b22010-10-13 00:13:10 +02001660 * @dev: device for which we do this operation
1661 * @freq: OPP frequency to disable
1662 *
1663 * Disables a provided opp. If the operation is valid, this returns
1664 * 0, else the corresponding error value. It is meant to be a temporary
1665 * control by users to make this OPP not available until the circumstances are
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001666 * right to make it available again (with a call to dev_pm_opp_enable).
Nishanth Menone1f60b22010-10-13 00:13:10 +02001667 *
Nishanth Menon984f16c2014-12-24 11:22:57 -06001668 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
Stephen Boyde1a2d492015-09-24 12:28:44 -07001669 * copy operation, returns 0 if no modification was done OR modification was
Nishanth Menon984f16c2014-12-24 11:22:57 -06001670 * successful.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001671 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001672int dev_pm_opp_disable(struct device *dev, unsigned long freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +02001673{
Nishanth Menon327854c2014-12-24 11:22:56 -06001674 return _opp_set_availability(dev, freq, false);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001675}
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001676EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001677
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001678/**
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301679 * dev_pm_opp_register_notifier() - Register OPP notifier for the device
1680 * @dev: Device for which notifier needs to be registered
1681 * @nb: Notifier block to be registered
Nishanth Menon984f16c2014-12-24 11:22:57 -06001682 *
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301683 * Return: 0 on success or a negative error value.
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001684 */
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301685int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb)
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001686{
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301687 struct opp_table *opp_table;
1688 int ret;
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001689
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301690 opp_table = _find_opp_table(dev);
Viresh Kumar5b650b32017-01-23 10:11:48 +05301691 if (IS_ERR(opp_table))
1692 return PTR_ERR(opp_table);
1693
Viresh Kumar052c6f12017-01-23 10:11:49 +05301694 ret = blocking_notifier_chain_register(&opp_table->head, nb);
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301695
Viresh Kumar5b650b32017-01-23 10:11:48 +05301696 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301697
1698 return ret;
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001699}
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301700EXPORT_SYMBOL(dev_pm_opp_register_notifier);
1701
1702/**
1703 * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device
1704 * @dev: Device for which notifier needs to be unregistered
1705 * @nb: Notifier block to be unregistered
1706 *
1707 * Return: 0 on success or a negative error value.
1708 */
1709int dev_pm_opp_unregister_notifier(struct device *dev,
1710 struct notifier_block *nb)
1711{
1712 struct opp_table *opp_table;
1713 int ret;
1714
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301715 opp_table = _find_opp_table(dev);
Viresh Kumar5b650b32017-01-23 10:11:48 +05301716 if (IS_ERR(opp_table))
1717 return PTR_ERR(opp_table);
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301718
Viresh Kumar052c6f12017-01-23 10:11:49 +05301719 ret = blocking_notifier_chain_unregister(&opp_table->head, nb);
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301720
Viresh Kumar5b650b32017-01-23 10:11:48 +05301721 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301722
1723 return ret;
1724}
1725EXPORT_SYMBOL(dev_pm_opp_unregister_notifier);
Shawn Guob496dfb2012-09-05 01:09:12 +02001726
Sudeep Holla411466c2016-05-03 15:05:04 +01001727/*
1728 * Free OPPs either created using static entries present in DT or even the
1729 * dynamically added entries based on remove_all param.
Shawn Guob496dfb2012-09-05 01:09:12 +02001730 */
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301731void _dev_pm_opp_remove_table(struct opp_table *opp_table, struct device *dev,
1732 bool remove_all)
Viresh Kumar9274c892017-01-02 14:41:00 +05301733{
1734 struct dev_pm_opp *opp, *tmp;
1735
Viresh Kumar9274c892017-01-02 14:41:00 +05301736 /* Find if opp_table manages a single device */
1737 if (list_is_singular(&opp_table->dev_list)) {
1738 /* Free static OPPs */
1739 list_for_each_entry_safe(opp, tmp, &opp_table->opp_list, node) {
1740 if (remove_all || !opp->dynamic)
Viresh Kumar70347642017-01-23 10:11:46 +05301741 dev_pm_opp_put(opp);
Viresh Kumar9274c892017-01-02 14:41:00 +05301742 }
Viresh Kumar009acd12017-10-11 12:54:14 +05301743
1744 /*
1745 * The OPP table is getting removed, drop the performance state
1746 * constraints.
1747 */
1748 if (opp_table->genpd_performance_state)
1749 dev_pm_genpd_set_performance_state(dev, 0);
Viresh Kumar9274c892017-01-02 14:41:00 +05301750 } else {
1751 _remove_opp_dev(_find_opp_dev(dev, opp_table), opp_table);
1752 }
1753}
1754
1755void _dev_pm_opp_find_and_remove_table(struct device *dev, bool remove_all)
Viresh Kumar737002b2015-07-29 16:22:58 +05301756{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301757 struct opp_table *opp_table;
Viresh Kumar737002b2015-07-29 16:22:58 +05301758
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301759 /* Check for existing table for 'dev' */
1760 opp_table = _find_opp_table(dev);
1761 if (IS_ERR(opp_table)) {
1762 int error = PTR_ERR(opp_table);
Viresh Kumar737002b2015-07-29 16:22:58 +05301763
1764 if (error != -ENODEV)
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301765 WARN(1, "%s: opp_table: %d\n",
Viresh Kumar737002b2015-07-29 16:22:58 +05301766 IS_ERR_OR_NULL(dev) ?
1767 "Invalid device" : dev_name(dev),
1768 error);
Viresh Kumar5b650b32017-01-23 10:11:48 +05301769 return;
Viresh Kumar737002b2015-07-29 16:22:58 +05301770 }
1771
Viresh Kumar9274c892017-01-02 14:41:00 +05301772 _dev_pm_opp_remove_table(opp_table, dev, remove_all);
Viresh Kumar737002b2015-07-29 16:22:58 +05301773
Viresh Kumar5b650b32017-01-23 10:11:48 +05301774 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar737002b2015-07-29 16:22:58 +05301775}
Viresh Kumar129eec52014-11-27 08:54:06 +05301776
1777/**
Sudeep Holla411466c2016-05-03 15:05:04 +01001778 * dev_pm_opp_remove_table() - Free all OPPs associated with the device
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301779 * @dev: device pointer used to lookup OPP table.
Viresh Kumar129eec52014-11-27 08:54:06 +05301780 *
Sudeep Holla411466c2016-05-03 15:05:04 +01001781 * Free both OPPs created using static entries present in DT and the
1782 * dynamically added entries.
Viresh Kumar129eec52014-11-27 08:54:06 +05301783 */
Sudeep Holla411466c2016-05-03 15:05:04 +01001784void dev_pm_opp_remove_table(struct device *dev)
Viresh Kumar129eec52014-11-27 08:54:06 +05301785{
Viresh Kumar9274c892017-01-02 14:41:00 +05301786 _dev_pm_opp_find_and_remove_table(dev, true);
Viresh Kumar8d4d4e92015-06-12 17:10:38 +05301787}
Sudeep Holla411466c2016-05-03 15:05:04 +01001788EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table);