blob: a0f72c732718c2c314e3232f07c0ad20b9c21c6b [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 Kumar8a31d9d92017-01-23 10:11:47 +053036static void dev_pm_opp_get(struct dev_pm_opp *opp);
37
Viresh Kumar2c2709d2016-02-16 14:17:53 +053038static struct opp_device *_find_opp_dev(const struct device *dev,
39 struct opp_table *opp_table)
Viresh Kumar06441652015-07-29 16:23:04 +053040{
Viresh Kumar2c2709d2016-02-16 14:17:53 +053041 struct opp_device *opp_dev;
Viresh Kumar06441652015-07-29 16:23:04 +053042
Viresh Kumar2c2709d2016-02-16 14:17:53 +053043 list_for_each_entry(opp_dev, &opp_table->dev_list, node)
44 if (opp_dev->dev == dev)
45 return opp_dev;
Viresh Kumar06441652015-07-29 16:23:04 +053046
47 return NULL;
48}
49
Wei Yongjun6ac42392017-02-06 14:29:55 +000050static struct opp_table *_find_opp_table_unlocked(struct device *dev)
Viresh Kumar5b650b32017-01-23 10:11:48 +053051{
52 struct opp_table *opp_table;
53
54 list_for_each_entry(opp_table, &opp_tables, node) {
55 if (_find_opp_dev(dev, opp_table)) {
56 _get_opp_table_kref(opp_table);
57
58 return opp_table;
59 }
60 }
61
62 return ERR_PTR(-ENODEV);
63}
64
Nishanth Menone1f60b22010-10-13 00:13:10 +020065/**
Viresh Kumar2c2709d2016-02-16 14:17:53 +053066 * _find_opp_table() - find opp_table struct using device pointer
67 * @dev: device pointer used to lookup OPP table
Nishanth Menone1f60b22010-10-13 00:13:10 +020068 *
Viresh Kumar052c6f12017-01-23 10:11:49 +053069 * Search OPP table for one containing matching device.
Nishanth Menone1f60b22010-10-13 00:13:10 +020070 *
Viresh Kumar2c2709d2016-02-16 14:17:53 +053071 * Return: pointer to 'struct opp_table' if found, otherwise -ENODEV or
Nishanth Menone1f60b22010-10-13 00:13:10 +020072 * -EINVAL based on type of error.
73 *
Viresh Kumar5b650b32017-01-23 10:11:48 +053074 * The callers must call dev_pm_opp_put_opp_table() after the table is used.
Nishanth Menone1f60b22010-10-13 00:13:10 +020075 */
Viresh Kumar2c2709d2016-02-16 14:17:53 +053076struct opp_table *_find_opp_table(struct device *dev)
Nishanth Menone1f60b22010-10-13 00:13:10 +020077{
Viresh Kumar2c2709d2016-02-16 14:17:53 +053078 struct opp_table *opp_table;
Nishanth Menone1f60b22010-10-13 00:13:10 +020079
Viresh Kumar50a3cb02015-08-12 15:59:39 +053080 if (IS_ERR_OR_NULL(dev)) {
Nishanth Menone1f60b22010-10-13 00:13:10 +020081 pr_err("%s: Invalid parameters\n", __func__);
82 return ERR_PTR(-EINVAL);
83 }
84
Viresh Kumar5b650b32017-01-23 10:11:48 +053085 mutex_lock(&opp_table_lock);
86 opp_table = _find_opp_table_unlocked(dev);
87 mutex_unlock(&opp_table_lock);
Nishanth Menone1f60b22010-10-13 00:13:10 +020088
Viresh Kumar5b650b32017-01-23 10:11:48 +053089 return opp_table;
Nishanth Menone1f60b22010-10-13 00:13:10 +020090}
91
92/**
Linus Torvaldsbaf51c42015-11-11 09:03:01 -080093 * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an opp
Nishanth Menone1f60b22010-10-13 00:13:10 +020094 * @opp: opp for which voltage has to be returned for
95 *
Nishanth Menon984f16c2014-12-24 11:22:57 -060096 * Return: voltage in micro volt corresponding to the opp, else
Nishanth Menone1f60b22010-10-13 00:13:10 +020097 * return 0
98 *
Viresh Kumardfbe4672016-12-01 16:28:19 +053099 * This is useful only for devices with single power supply.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200100 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500101unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200102{
Viresh Kumar052c6f12017-01-23 10:11:49 +0530103 if (IS_ERR_OR_NULL(opp)) {
Nishanth Menone1f60b22010-10-13 00:13:10 +0200104 pr_err("%s: Invalid parameters\n", __func__);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530105 return 0;
106 }
Nishanth Menone1f60b22010-10-13 00:13:10 +0200107
Viresh Kumar052c6f12017-01-23 10:11:49 +0530108 return opp->supplies[0].u_volt;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200109}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500110EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200111
112/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500113 * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp
Nishanth Menone1f60b22010-10-13 00:13:10 +0200114 * @opp: opp for which frequency has to be returned for
115 *
Nishanth Menon984f16c2014-12-24 11:22:57 -0600116 * Return: frequency in hertz corresponding to the opp, else
Nishanth Menone1f60b22010-10-13 00:13:10 +0200117 * return 0
Nishanth Menone1f60b22010-10-13 00:13:10 +0200118 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500119unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200120{
Viresh Kumar052c6f12017-01-23 10:11:49 +0530121 if (IS_ERR_OR_NULL(opp) || !opp->available) {
Nishanth Menone1f60b22010-10-13 00:13:10 +0200122 pr_err("%s: Invalid parameters\n", __func__);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530123 return 0;
124 }
Nishanth Menone1f60b22010-10-13 00:13:10 +0200125
Viresh Kumar052c6f12017-01-23 10:11:49 +0530126 return opp->rate;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200127}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500128EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200129
130/**
Bartlomiej Zolnierkiewicz19445b22015-07-09 17:43:35 +0200131 * dev_pm_opp_is_turbo() - Returns if opp is turbo OPP or not
132 * @opp: opp for which turbo mode is being verified
133 *
134 * Turbo OPPs are not for normal use, and can be enabled (under certain
135 * conditions) for short duration of times to finish high throughput work
136 * quickly. Running on them for longer times may overheat the chip.
137 *
138 * Return: true if opp is turbo opp, else false.
Bartlomiej Zolnierkiewicz19445b22015-07-09 17:43:35 +0200139 */
140bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp)
141{
Viresh Kumar052c6f12017-01-23 10:11:49 +0530142 if (IS_ERR_OR_NULL(opp) || !opp->available) {
Bartlomiej Zolnierkiewicz19445b22015-07-09 17:43:35 +0200143 pr_err("%s: Invalid parameters\n", __func__);
144 return false;
145 }
146
Viresh Kumar052c6f12017-01-23 10:11:49 +0530147 return opp->turbo;
Bartlomiej Zolnierkiewicz19445b22015-07-09 17:43:35 +0200148}
149EXPORT_SYMBOL_GPL(dev_pm_opp_is_turbo);
150
151/**
Viresh Kumar3ca9bb32015-07-29 16:23:03 +0530152 * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds
153 * @dev: device for which we do this operation
154 *
155 * Return: This function returns the max clock latency in nanoseconds.
Viresh Kumar3ca9bb32015-07-29 16:23:03 +0530156 */
157unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev)
158{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530159 struct opp_table *opp_table;
Viresh Kumar3ca9bb32015-07-29 16:23:03 +0530160 unsigned long clock_latency_ns;
161
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530162 opp_table = _find_opp_table(dev);
163 if (IS_ERR(opp_table))
Viresh Kumar5b650b32017-01-23 10:11:48 +0530164 return 0;
Viresh Kumar3ca9bb32015-07-29 16:23:03 +0530165
Viresh Kumar5b650b32017-01-23 10:11:48 +0530166 clock_latency_ns = opp_table->clock_latency_ns_max;
167
168 dev_pm_opp_put_opp_table(opp_table);
169
Viresh Kumar3ca9bb32015-07-29 16:23:03 +0530170 return clock_latency_ns;
171}
172EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency);
173
174/**
Viresh Kumar655c9df2016-02-09 10:30:35 +0530175 * dev_pm_opp_get_max_volt_latency() - Get max voltage latency in nanoseconds
176 * @dev: device for which we do this operation
177 *
178 * Return: This function returns the max voltage latency in nanoseconds.
Viresh Kumar655c9df2016-02-09 10:30:35 +0530179 */
180unsigned long dev_pm_opp_get_max_volt_latency(struct device *dev)
181{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530182 struct opp_table *opp_table;
Viresh Kumar655c9df2016-02-09 10:30:35 +0530183 struct dev_pm_opp *opp;
Viresh Kumar478256b2017-05-23 09:32:11 +0530184 struct regulator *reg;
Viresh Kumar655c9df2016-02-09 10:30:35 +0530185 unsigned long latency_ns = 0;
Viresh Kumardfbe4672016-12-01 16:28:19 +0530186 int ret, i, count;
187 struct {
188 unsigned long min;
189 unsigned long max;
190 } *uV;
191
Viresh Kumarcdd3e612017-01-23 10:11:51 +0530192 opp_table = _find_opp_table(dev);
193 if (IS_ERR(opp_table))
194 return 0;
195
196 count = opp_table->regulator_count;
Viresh Kumardfbe4672016-12-01 16:28:19 +0530197
198 /* Regulator may not be required for the device */
199 if (!count)
Viresh Kumarcdd3e612017-01-23 10:11:51 +0530200 goto put_opp_table;
Viresh Kumardfbe4672016-12-01 16:28:19 +0530201
Viresh Kumardfbe4672016-12-01 16:28:19 +0530202 uV = kmalloc_array(count, sizeof(*uV), GFP_KERNEL);
203 if (!uV)
Viresh Kumar478256b2017-05-23 09:32:11 +0530204 goto put_opp_table;
Viresh Kumar655c9df2016-02-09 10:30:35 +0530205
Viresh Kumar052c6f12017-01-23 10:11:49 +0530206 mutex_lock(&opp_table->lock);
207
Viresh Kumardfbe4672016-12-01 16:28:19 +0530208 for (i = 0; i < count; i++) {
209 uV[i].min = ~0;
210 uV[i].max = 0;
Viresh Kumar655c9df2016-02-09 10:30:35 +0530211
Viresh Kumar052c6f12017-01-23 10:11:49 +0530212 list_for_each_entry(opp, &opp_table->opp_list, node) {
Viresh Kumardfbe4672016-12-01 16:28:19 +0530213 if (!opp->available)
214 continue;
215
216 if (opp->supplies[i].u_volt_min < uV[i].min)
217 uV[i].min = opp->supplies[i].u_volt_min;
218 if (opp->supplies[i].u_volt_max > uV[i].max)
219 uV[i].max = opp->supplies[i].u_volt_max;
220 }
Viresh Kumar655c9df2016-02-09 10:30:35 +0530221 }
222
Viresh Kumar052c6f12017-01-23 10:11:49 +0530223 mutex_unlock(&opp_table->lock);
Viresh Kumar655c9df2016-02-09 10:30:35 +0530224
225 /*
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530226 * The caller needs to ensure that opp_table (and hence the regulator)
Viresh Kumar655c9df2016-02-09 10:30:35 +0530227 * isn't freed, while we are executing this routine.
228 */
Andrzej Hajda8cc31112017-02-20 19:57:57 +0100229 for (i = 0; i < count; i++) {
Viresh Kumar478256b2017-05-23 09:32:11 +0530230 reg = opp_table->regulators[i];
Viresh Kumardfbe4672016-12-01 16:28:19 +0530231 ret = regulator_set_voltage_time(reg, uV[i].min, uV[i].max);
232 if (ret > 0)
233 latency_ns += ret * 1000;
234 }
235
Viresh Kumardfbe4672016-12-01 16:28:19 +0530236 kfree(uV);
Viresh Kumarcdd3e612017-01-23 10:11:51 +0530237put_opp_table:
238 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar655c9df2016-02-09 10:30:35 +0530239
240 return latency_ns;
241}
242EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_volt_latency);
243
244/**
Viresh Kumar21743442016-02-09 10:30:36 +0530245 * dev_pm_opp_get_max_transition_latency() - Get max transition latency in
246 * nanoseconds
247 * @dev: device for which we do this operation
248 *
249 * Return: This function returns the max transition latency, in nanoseconds, to
250 * switch from one OPP to other.
Viresh Kumar21743442016-02-09 10:30:36 +0530251 */
252unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev)
253{
254 return dev_pm_opp_get_max_volt_latency(dev) +
255 dev_pm_opp_get_max_clock_latency(dev);
256}
257EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_transition_latency);
258
259/**
Viresh Kumar3aa26a32017-01-02 14:41:02 +0530260 * dev_pm_opp_get_suspend_opp_freq() - Get frequency of suspend opp in Hz
Bartlomiej Zolnierkiewicz4eafbd12015-09-08 18:41:01 +0200261 * @dev: device for which we do this operation
262 *
Viresh Kumar3aa26a32017-01-02 14:41:02 +0530263 * Return: This function returns the frequency of the OPP marked as suspend_opp
264 * if one is available, else returns 0;
Bartlomiej Zolnierkiewicz4eafbd12015-09-08 18:41:01 +0200265 */
Viresh Kumar3aa26a32017-01-02 14:41:02 +0530266unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev)
Bartlomiej Zolnierkiewicz4eafbd12015-09-08 18:41:01 +0200267{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530268 struct opp_table *opp_table;
Viresh Kumar3aa26a32017-01-02 14:41:02 +0530269 unsigned long freq = 0;
Bartlomiej Zolnierkiewicz4eafbd12015-09-08 18:41:01 +0200270
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530271 opp_table = _find_opp_table(dev);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530272 if (IS_ERR(opp_table))
273 return 0;
Bartlomiej Zolnierkiewicz4eafbd12015-09-08 18:41:01 +0200274
Viresh Kumar5b650b32017-01-23 10:11:48 +0530275 if (opp_table->suspend_opp && opp_table->suspend_opp->available)
276 freq = dev_pm_opp_get_freq(opp_table->suspend_opp);
Viresh Kumar3aa26a32017-01-02 14:41:02 +0530277
Viresh Kumar5b650b32017-01-23 10:11:48 +0530278 dev_pm_opp_put_opp_table(opp_table);
279
Viresh Kumar3aa26a32017-01-02 14:41:02 +0530280 return freq;
Bartlomiej Zolnierkiewicz4eafbd12015-09-08 18:41:01 +0200281}
Viresh Kumar3aa26a32017-01-02 14:41:02 +0530282EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp_freq);
Bartlomiej Zolnierkiewicz4eafbd12015-09-08 18:41:01 +0200283
Viresh Kumara1e8c132018-04-06 14:35:45 +0530284int _get_opp_count(struct opp_table *opp_table)
285{
286 struct dev_pm_opp *opp;
287 int count = 0;
288
289 mutex_lock(&opp_table->lock);
290
291 list_for_each_entry(opp, &opp_table->opp_list, node) {
292 if (opp->available)
293 count++;
294 }
295
296 mutex_unlock(&opp_table->lock);
297
298 return count;
299}
300
Bartlomiej Zolnierkiewicz4eafbd12015-09-08 18:41:01 +0200301/**
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530302 * dev_pm_opp_get_opp_count() - Get number of opps available in the opp table
Nishanth Menone1f60b22010-10-13 00:13:10 +0200303 * @dev: device for which we do this operation
304 *
Nishanth Menon984f16c2014-12-24 11:22:57 -0600305 * Return: This function returns the number of available opps if there are any,
Nishanth Menone1f60b22010-10-13 00:13:10 +0200306 * else returns 0 if none or the corresponding error value.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200307 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500308int dev_pm_opp_get_opp_count(struct device *dev)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200309{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530310 struct opp_table *opp_table;
Viresh Kumara1e8c132018-04-06 14:35:45 +0530311 int count;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200312
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530313 opp_table = _find_opp_table(dev);
314 if (IS_ERR(opp_table)) {
315 count = PTR_ERR(opp_table);
Fabio Estevam035ed072017-09-29 14:39:49 -0300316 dev_dbg(dev, "%s: OPP table not found (%d)\n",
Dmitry Torokhovb4718c02014-12-16 15:09:38 -0800317 __func__, count);
Viresh Kumara1e8c132018-04-06 14:35:45 +0530318 return 0;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200319 }
320
Viresh Kumara1e8c132018-04-06 14:35:45 +0530321 count = _get_opp_count(opp_table);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530322 dev_pm_opp_put_opp_table(opp_table);
323
Nishanth Menone1f60b22010-10-13 00:13:10 +0200324 return count;
325}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500326EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200327
328/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500329 * dev_pm_opp_find_freq_exact() - search for an exact frequency
Nishanth Menone1f60b22010-10-13 00:13:10 +0200330 * @dev: device for which we do this operation
331 * @freq: frequency to search for
Nishanth Menon7ae49612011-02-25 23:46:18 +0100332 * @available: true/false - match for available opp
Nishanth Menone1f60b22010-10-13 00:13:10 +0200333 *
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530334 * Return: Searches for exact match in the opp table and returns pointer to the
Nishanth Menon984f16c2014-12-24 11:22:57 -0600335 * matching opp if found, else returns ERR_PTR in case of error and should
336 * be handled using IS_ERR. Error return values can be:
Nishanth Menon07797262012-10-24 22:00:12 +0200337 * EINVAL: for bad pointer
338 * ERANGE: no match found for search
339 * ENODEV: if device not found in list of registered devices
Nishanth Menone1f60b22010-10-13 00:13:10 +0200340 *
341 * Note: available is a modifier for the search. if available=true, then the
342 * match is for exact matching frequency and is available in the stored OPP
343 * table. if false, the match is for exact frequency which is not available.
344 *
345 * This provides a mechanism to enable an opp which is not available currently
346 * or the opposite as well.
347 *
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530348 * The callers are required to call dev_pm_opp_put() for the returned OPP after
349 * use.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200350 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500351struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
352 unsigned long freq,
353 bool available)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200354{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530355 struct opp_table *opp_table;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500356 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200357
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530358 opp_table = _find_opp_table(dev);
359 if (IS_ERR(opp_table)) {
360 int r = PTR_ERR(opp_table);
361
362 dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200363 return ERR_PTR(r);
364 }
365
Viresh Kumar052c6f12017-01-23 10:11:49 +0530366 mutex_lock(&opp_table->lock);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530367
Viresh Kumar052c6f12017-01-23 10:11:49 +0530368 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
Nishanth Menone1f60b22010-10-13 00:13:10 +0200369 if (temp_opp->available == available &&
370 temp_opp->rate == freq) {
371 opp = temp_opp;
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530372
373 /* Increment the reference count of OPP */
374 dev_pm_opp_get(opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200375 break;
376 }
377 }
378
Viresh Kumar052c6f12017-01-23 10:11:49 +0530379 mutex_unlock(&opp_table->lock);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530380 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530381
Nishanth Menone1f60b22010-10-13 00:13:10 +0200382 return opp;
383}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500384EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200385
Jisheng Zhang067b7ce2016-07-25 14:11:16 +0800386static noinline struct dev_pm_opp *_find_freq_ceil(struct opp_table *opp_table,
387 unsigned long *freq)
388{
389 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
390
Viresh Kumar052c6f12017-01-23 10:11:49 +0530391 mutex_lock(&opp_table->lock);
392
393 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
Jisheng Zhang067b7ce2016-07-25 14:11:16 +0800394 if (temp_opp->available && temp_opp->rate >= *freq) {
395 opp = temp_opp;
396 *freq = opp->rate;
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530397
398 /* Increment the reference count of OPP */
399 dev_pm_opp_get(opp);
Jisheng Zhang067b7ce2016-07-25 14:11:16 +0800400 break;
401 }
402 }
403
Viresh Kumar052c6f12017-01-23 10:11:49 +0530404 mutex_unlock(&opp_table->lock);
405
Jisheng Zhang067b7ce2016-07-25 14:11:16 +0800406 return opp;
407}
408
Nishanth Menone1f60b22010-10-13 00:13:10 +0200409/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500410 * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
Nishanth Menone1f60b22010-10-13 00:13:10 +0200411 * @dev: device for which we do this operation
412 * @freq: Start frequency
413 *
414 * Search for the matching ceil *available* OPP from a starting freq
415 * for a device.
416 *
Nishanth Menon984f16c2014-12-24 11:22:57 -0600417 * Return: matching *opp and refreshes *freq accordingly, else returns
Nishanth Menon07797262012-10-24 22:00:12 +0200418 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
419 * values can be:
420 * EINVAL: for bad pointer
421 * ERANGE: no match found for search
422 * ENODEV: if device not found in list of registered devices
Nishanth Menone1f60b22010-10-13 00:13:10 +0200423 *
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530424 * The callers are required to call dev_pm_opp_put() for the returned OPP after
425 * use.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200426 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500427struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
428 unsigned long *freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200429{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530430 struct opp_table *opp_table;
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530431 struct dev_pm_opp *opp;
Dmitry Torokhovb02ded22014-12-16 15:09:36 -0800432
Nishanth Menone1f60b22010-10-13 00:13:10 +0200433 if (!dev || !freq) {
434 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
435 return ERR_PTR(-EINVAL);
436 }
437
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530438 opp_table = _find_opp_table(dev);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530439 if (IS_ERR(opp_table))
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530440 return ERR_CAST(opp_table);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530441
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530442 opp = _find_freq_ceil(opp_table, freq);
443
Viresh Kumar5b650b32017-01-23 10:11:48 +0530444 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530445
446 return opp;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200447}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500448EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200449
450/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500451 * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
Nishanth Menone1f60b22010-10-13 00:13:10 +0200452 * @dev: device for which we do this operation
453 * @freq: Start frequency
454 *
455 * Search for the matching floor *available* OPP from a starting freq
456 * for a device.
457 *
Nishanth Menon984f16c2014-12-24 11:22:57 -0600458 * Return: matching *opp and refreshes *freq accordingly, else returns
Nishanth Menon07797262012-10-24 22:00:12 +0200459 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
460 * values can be:
461 * EINVAL: for bad pointer
462 * ERANGE: no match found for search
463 * ENODEV: if device not found in list of registered devices
Nishanth Menone1f60b22010-10-13 00:13:10 +0200464 *
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530465 * The callers are required to call dev_pm_opp_put() for the returned OPP after
466 * use.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200467 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500468struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
469 unsigned long *freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200470{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530471 struct opp_table *opp_table;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500472 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200473
474 if (!dev || !freq) {
475 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
476 return ERR_PTR(-EINVAL);
477 }
478
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530479 opp_table = _find_opp_table(dev);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530480 if (IS_ERR(opp_table))
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530481 return ERR_CAST(opp_table);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530482
Viresh Kumar052c6f12017-01-23 10:11:49 +0530483 mutex_lock(&opp_table->lock);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200484
Viresh Kumar052c6f12017-01-23 10:11:49 +0530485 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
Nishanth Menone1f60b22010-10-13 00:13:10 +0200486 if (temp_opp->available) {
487 /* go to the next node, before choosing prev */
488 if (temp_opp->rate > *freq)
489 break;
490 else
491 opp = temp_opp;
492 }
493 }
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530494
495 /* Increment the reference count of OPP */
496 if (!IS_ERR(opp))
497 dev_pm_opp_get(opp);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530498 mutex_unlock(&opp_table->lock);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530499 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530500
Nishanth Menone1f60b22010-10-13 00:13:10 +0200501 if (!IS_ERR(opp))
502 *freq = opp->rate;
503
504 return opp;
505}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500506EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200507
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530508static int _set_opp_voltage(struct device *dev, struct regulator *reg,
Viresh Kumarce317812016-12-01 16:28:18 +0530509 struct dev_pm_opp_supply *supply)
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530510{
511 int ret;
512
513 /* Regulator not available for device */
514 if (IS_ERR(reg)) {
515 dev_dbg(dev, "%s: regulator not available: %ld\n", __func__,
516 PTR_ERR(reg));
517 return 0;
518 }
519
Viresh Kumarce317812016-12-01 16:28:18 +0530520 dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__,
521 supply->u_volt_min, supply->u_volt, supply->u_volt_max);
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530522
Viresh Kumarce317812016-12-01 16:28:18 +0530523 ret = regulator_set_voltage_triplet(reg, supply->u_volt_min,
524 supply->u_volt, supply->u_volt_max);
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530525 if (ret)
526 dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n",
Viresh Kumarce317812016-12-01 16:28:18 +0530527 __func__, supply->u_volt_min, supply->u_volt,
528 supply->u_volt_max, ret);
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530529
530 return ret;
531}
532
Viresh Kumar94735582016-12-01 16:28:20 +0530533static inline int
534_generic_set_opp_clk_only(struct device *dev, struct clk *clk,
535 unsigned long old_freq, unsigned long freq)
536{
537 int ret;
538
539 ret = clk_set_rate(clk, freq);
540 if (ret) {
541 dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
542 ret);
543 }
544
545 return ret;
546}
547
Viresh Kumar009acd12017-10-11 12:54:14 +0530548static inline int
549_generic_set_opp_domain(struct device *dev, struct clk *clk,
550 unsigned long old_freq, unsigned long freq,
551 unsigned int old_pstate, unsigned int new_pstate)
552{
553 int ret;
554
555 /* Scaling up? Scale domain performance state before frequency */
556 if (freq > old_freq) {
557 ret = dev_pm_genpd_set_performance_state(dev, new_pstate);
558 if (ret)
559 return ret;
560 }
561
562 ret = _generic_set_opp_clk_only(dev, clk, old_freq, freq);
563 if (ret)
564 goto restore_domain_state;
565
566 /* Scaling down? Scale domain performance state after frequency */
567 if (freq < old_freq) {
568 ret = dev_pm_genpd_set_performance_state(dev, new_pstate);
569 if (ret)
570 goto restore_freq;
571 }
572
573 return 0;
574
575restore_freq:
576 if (_generic_set_opp_clk_only(dev, clk, freq, old_freq))
577 dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n",
578 __func__, old_freq);
579restore_domain_state:
580 if (freq > old_freq)
581 dev_pm_genpd_set_performance_state(dev, old_pstate);
582
583 return ret;
584}
585
Viresh Kumarc74b32f2017-05-23 09:32:10 +0530586static int _generic_set_opp_regulator(const struct opp_table *opp_table,
587 struct device *dev,
588 unsigned long old_freq,
589 unsigned long freq,
590 struct dev_pm_opp_supply *old_supply,
591 struct dev_pm_opp_supply *new_supply)
Viresh Kumar94735582016-12-01 16:28:20 +0530592{
Viresh Kumarc74b32f2017-05-23 09:32:10 +0530593 struct regulator *reg = opp_table->regulators[0];
Viresh Kumar94735582016-12-01 16:28:20 +0530594 int ret;
595
596 /* This function only supports single regulator per device */
Viresh Kumarc74b32f2017-05-23 09:32:10 +0530597 if (WARN_ON(opp_table->regulator_count > 1)) {
Viresh Kumar94735582016-12-01 16:28:20 +0530598 dev_err(dev, "multiple regulators are not supported\n");
599 return -EINVAL;
600 }
601
602 /* Scaling up? Scale voltage before frequency */
603 if (freq > old_freq) {
604 ret = _set_opp_voltage(dev, reg, new_supply);
605 if (ret)
606 goto restore_voltage;
607 }
608
609 /* Change frequency */
Viresh Kumarc74b32f2017-05-23 09:32:10 +0530610 ret = _generic_set_opp_clk_only(dev, opp_table->clk, old_freq, freq);
Viresh Kumar94735582016-12-01 16:28:20 +0530611 if (ret)
612 goto restore_voltage;
613
614 /* Scaling down? Scale voltage after frequency */
615 if (freq < old_freq) {
616 ret = _set_opp_voltage(dev, reg, new_supply);
617 if (ret)
618 goto restore_freq;
619 }
620
621 return 0;
622
623restore_freq:
Viresh Kumarc74b32f2017-05-23 09:32:10 +0530624 if (_generic_set_opp_clk_only(dev, opp_table->clk, freq, old_freq))
Viresh Kumar94735582016-12-01 16:28:20 +0530625 dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n",
626 __func__, old_freq);
627restore_voltage:
628 /* This shouldn't harm even if the voltages weren't updated earlier */
Viresh Kumarc74b32f2017-05-23 09:32:10 +0530629 if (old_supply)
Viresh Kumar94735582016-12-01 16:28:20 +0530630 _set_opp_voltage(dev, reg, old_supply);
631
632 return ret;
633}
634
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530635/**
636 * dev_pm_opp_set_rate() - Configure new OPP based on frequency
637 * @dev: device for which we do this operation
638 * @target_freq: frequency to achieve
639 *
640 * This configures the power-supplies and clock source to the levels specified
641 * by the OPP corresponding to the target_freq.
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530642 */
643int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq)
644{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530645 struct opp_table *opp_table;
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530646 unsigned long freq, old_freq;
Viresh Kumar94735582016-12-01 16:28:20 +0530647 struct dev_pm_opp *old_opp, *opp;
Viresh Kumar94735582016-12-01 16:28:20 +0530648 struct clk *clk;
649 int ret, size;
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530650
651 if (unlikely(!target_freq)) {
652 dev_err(dev, "%s: Invalid target frequency %lu\n", __func__,
653 target_freq);
654 return -EINVAL;
655 }
656
Viresh Kumar052c6f12017-01-23 10:11:49 +0530657 opp_table = _find_opp_table(dev);
658 if (IS_ERR(opp_table)) {
659 dev_err(dev, "%s: device opp doesn't exist\n", __func__);
660 return PTR_ERR(opp_table);
661 }
662
663 clk = opp_table->clk;
664 if (IS_ERR(clk)) {
665 dev_err(dev, "%s: No clock available for the device\n",
666 __func__);
667 ret = PTR_ERR(clk);
668 goto put_opp_table;
669 }
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530670
671 freq = clk_round_rate(clk, target_freq);
672 if ((long)freq <= 0)
673 freq = target_freq;
674
675 old_freq = clk_get_rate(clk);
676
677 /* Return early if nothing to do */
678 if (old_freq == freq) {
679 dev_dbg(dev, "%s: old/new frequencies (%lu Hz) are same, nothing to do\n",
680 __func__, freq);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530681 ret = 0;
682 goto put_opp_table;
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530683 }
684
Jisheng Zhang067b7ce2016-07-25 14:11:16 +0800685 old_opp = _find_freq_ceil(opp_table, &old_freq);
Arnd Bergmann4df27c92016-09-15 17:38:38 +0200686 if (IS_ERR(old_opp)) {
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530687 dev_err(dev, "%s: failed to find current OPP for freq %lu (%ld)\n",
688 __func__, old_freq, PTR_ERR(old_opp));
689 }
690
Jisheng Zhang067b7ce2016-07-25 14:11:16 +0800691 opp = _find_freq_ceil(opp_table, &freq);
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530692 if (IS_ERR(opp)) {
693 ret = PTR_ERR(opp);
694 dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n",
695 __func__, freq, ret);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530696 goto put_old_opp;
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530697 }
698
Viresh Kumar94735582016-12-01 16:28:20 +0530699 dev_dbg(dev, "%s: switching OPP: %lu Hz --> %lu Hz\n", __func__,
700 old_freq, freq);
Viresh Kumardfbe4672016-12-01 16:28:19 +0530701
Viresh Kumar94735582016-12-01 16:28:20 +0530702 /* Only frequency scaling */
Viresh Kumarc74b32f2017-05-23 09:32:10 +0530703 if (!opp_table->regulators) {
Viresh Kumar009acd12017-10-11 12:54:14 +0530704 /*
705 * We don't support devices with both regulator and
706 * domain performance-state for now.
707 */
708 if (opp_table->genpd_performance_state)
709 ret = _generic_set_opp_domain(dev, clk, old_freq, freq,
710 IS_ERR(old_opp) ? 0 : old_opp->pstate,
711 opp->pstate);
712 else
713 ret = _generic_set_opp_clk_only(dev, clk, old_freq, freq);
Viresh Kumarc74b32f2017-05-23 09:32:10 +0530714 } else if (!opp_table->set_opp) {
715 ret = _generic_set_opp_regulator(opp_table, dev, old_freq, freq,
716 IS_ERR(old_opp) ? NULL : old_opp->supplies,
717 opp->supplies);
718 } else {
719 struct dev_pm_set_opp_data *data;
720
721 data = opp_table->set_opp_data;
722 data->regulators = opp_table->regulators;
723 data->regulator_count = opp_table->regulator_count;
724 data->clk = clk;
725 data->dev = dev;
726
727 data->old_opp.rate = old_freq;
728 size = sizeof(*opp->supplies) * opp_table->regulator_count;
729 if (IS_ERR(old_opp))
730 memset(data->old_opp.supplies, 0, size);
731 else
732 memcpy(data->old_opp.supplies, old_opp->supplies, size);
733
734 data->new_opp.rate = freq;
735 memcpy(data->new_opp.supplies, opp->supplies, size);
736
737 ret = opp_table->set_opp(data);
Viresh Kumardfbe4672016-12-01 16:28:19 +0530738 }
739
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530740 dev_pm_opp_put(opp);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530741put_old_opp:
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530742 if (!IS_ERR(old_opp))
743 dev_pm_opp_put(old_opp);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530744put_opp_table:
Viresh Kumar5b650b32017-01-23 10:11:48 +0530745 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530746 return ret;
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530747}
748EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate);
749
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530750/* OPP-dev Helpers */
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530751static void _remove_opp_dev(struct opp_device *opp_dev,
752 struct opp_table *opp_table)
Viresh Kumar06441652015-07-29 16:23:04 +0530753{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530754 opp_debug_unregister(opp_dev, opp_table);
755 list_del(&opp_dev->node);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530756 kfree(opp_dev);
Viresh Kumar06441652015-07-29 16:23:04 +0530757}
758
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530759struct opp_device *_add_opp_dev(const struct device *dev,
760 struct opp_table *opp_table)
Viresh Kumar06441652015-07-29 16:23:04 +0530761{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530762 struct opp_device *opp_dev;
Viresh Kumardeaa5142015-11-11 07:59:01 +0530763 int ret;
Viresh Kumar06441652015-07-29 16:23:04 +0530764
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530765 opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL);
766 if (!opp_dev)
Viresh Kumar06441652015-07-29 16:23:04 +0530767 return NULL;
768
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530769 /* Initialize opp-dev */
770 opp_dev->dev = dev;
Viresh Kumar052c6f12017-01-23 10:11:49 +0530771 list_add(&opp_dev->node, &opp_table->dev_list);
Viresh Kumar06441652015-07-29 16:23:04 +0530772
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530773 /* Create debugfs entries for the opp_table */
774 ret = opp_debug_register(opp_dev, opp_table);
Viresh Kumardeaa5142015-11-11 07:59:01 +0530775 if (ret)
776 dev_err(dev, "%s: Failed to register opp debugfs (%d)\n",
777 __func__, ret);
778
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530779 return opp_dev;
Viresh Kumar06441652015-07-29 16:23:04 +0530780}
781
Viresh Kumarb6160e22017-01-02 14:41:04 +0530782static struct opp_table *_allocate_opp_table(struct device *dev)
Viresh Kumar07cce742014-12-10 09:45:34 +0530783{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530784 struct opp_table *opp_table;
785 struct opp_device *opp_dev;
Viresh Kumard54974c2016-02-09 10:30:38 +0530786 int ret;
Viresh Kumar07cce742014-12-10 09:45:34 +0530787
788 /*
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530789 * Allocate a new OPP table. In the infrequent case where a new
Viresh Kumar07cce742014-12-10 09:45:34 +0530790 * device is needed to be added, we pay this penalty.
791 */
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530792 opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL);
793 if (!opp_table)
Viresh Kumar07cce742014-12-10 09:45:34 +0530794 return NULL;
795
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530796 INIT_LIST_HEAD(&opp_table->dev_list);
Viresh Kumar06441652015-07-29 16:23:04 +0530797
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530798 opp_dev = _add_opp_dev(dev, opp_table);
799 if (!opp_dev) {
800 kfree(opp_table);
Viresh Kumar06441652015-07-29 16:23:04 +0530801 return NULL;
802 }
803
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530804 _of_init_opp_table(opp_table, dev);
Viresh Kumar50f8cfb2016-02-09 10:30:37 +0530805
Viresh Kumard54974c2016-02-09 10:30:38 +0530806 /* Find clk for the device */
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530807 opp_table->clk = clk_get(dev, NULL);
808 if (IS_ERR(opp_table->clk)) {
809 ret = PTR_ERR(opp_table->clk);
Viresh Kumard54974c2016-02-09 10:30:38 +0530810 if (ret != -EPROBE_DEFER)
811 dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__,
812 ret);
813 }
814
Viresh Kumar052c6f12017-01-23 10:11:49 +0530815 BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head);
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530816 INIT_LIST_HEAD(&opp_table->opp_list);
Viresh Kumar37a73ec2017-01-23 10:11:41 +0530817 mutex_init(&opp_table->lock);
Viresh Kumarf067a982017-01-23 10:11:42 +0530818 kref_init(&opp_table->kref);
Viresh Kumar07cce742014-12-10 09:45:34 +0530819
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530820 /* Secure the device table modification */
Viresh Kumar052c6f12017-01-23 10:11:49 +0530821 list_add(&opp_table->node, &opp_tables);
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530822 return opp_table;
Viresh Kumar07cce742014-12-10 09:45:34 +0530823}
824
Viresh Kumarf067a982017-01-23 10:11:42 +0530825void _get_opp_table_kref(struct opp_table *opp_table)
Viresh Kumar3bac42c2015-07-29 16:22:59 +0530826{
Viresh Kumarf067a982017-01-23 10:11:42 +0530827 kref_get(&opp_table->kref);
828}
829
830struct opp_table *dev_pm_opp_get_opp_table(struct device *dev)
831{
832 struct opp_table *opp_table;
833
834 /* Hold our table modification lock here */
835 mutex_lock(&opp_table_lock);
836
Viresh Kumar5b650b32017-01-23 10:11:48 +0530837 opp_table = _find_opp_table_unlocked(dev);
838 if (!IS_ERR(opp_table))
Viresh Kumarf067a982017-01-23 10:11:42 +0530839 goto unlock;
Viresh Kumarf067a982017-01-23 10:11:42 +0530840
841 opp_table = _allocate_opp_table(dev);
842
843unlock:
844 mutex_unlock(&opp_table_lock);
845
846 return opp_table;
847}
848EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table);
849
Viresh Kumarb83c1892017-01-23 10:11:45 +0530850static void _opp_table_kref_release(struct kref *kref)
Viresh Kumarf067a982017-01-23 10:11:42 +0530851{
852 struct opp_table *opp_table = container_of(kref, struct opp_table, kref);
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530853 struct opp_device *opp_dev;
Viresh Kumar06441652015-07-29 16:23:04 +0530854
Viresh Kumard54974c2016-02-09 10:30:38 +0530855 /* Release clk */
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530856 if (!IS_ERR(opp_table->clk))
857 clk_put(opp_table->clk);
Viresh Kumard54974c2016-02-09 10:30:38 +0530858
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530859 opp_dev = list_first_entry(&opp_table->dev_list, struct opp_device,
860 node);
Viresh Kumar06441652015-07-29 16:23:04 +0530861
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530862 _remove_opp_dev(opp_dev, opp_table);
Viresh Kumar06441652015-07-29 16:23:04 +0530863
864 /* dev_list must be empty now */
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530865 WARN_ON(!list_empty(&opp_table->dev_list));
Viresh Kumar06441652015-07-29 16:23:04 +0530866
Viresh Kumar37a73ec2017-01-23 10:11:41 +0530867 mutex_destroy(&opp_table->lock);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530868 list_del(&opp_table->node);
869 kfree(opp_table);
Viresh Kumar3bac42c2015-07-29 16:22:59 +0530870
Viresh Kumarf067a982017-01-23 10:11:42 +0530871 mutex_unlock(&opp_table_lock);
872}
873
874void dev_pm_opp_put_opp_table(struct opp_table *opp_table)
875{
876 kref_put_mutex(&opp_table->kref, _opp_table_kref_release,
877 &opp_table_lock);
878}
879EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table);
880
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +0530881void _opp_free(struct dev_pm_opp *opp)
Viresh Kumar969fceb2017-01-02 14:40:59 +0530882{
883 kfree(opp);
Viresh Kumar969fceb2017-01-02 14:40:59 +0530884}
885
Viresh Kumar70347642017-01-23 10:11:46 +0530886static void _opp_kref_release(struct kref *kref)
Viresh Kumar129eec52014-11-27 08:54:06 +0530887{
Viresh Kumar70347642017-01-23 10:11:46 +0530888 struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref);
889 struct opp_table *opp_table = opp->opp_table;
Viresh Kumar37a73ec2017-01-23 10:11:41 +0530890
Viresh Kumar129eec52014-11-27 08:54:06 +0530891 /*
892 * Notify the changes in the availability of the operable
893 * frequency/voltage list.
894 */
Viresh Kumar052c6f12017-01-23 10:11:49 +0530895 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_REMOVE, opp);
Viresh Kumardeaa5142015-11-11 07:59:01 +0530896 opp_debug_remove_one(opp);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530897 list_del(&opp->node);
898 kfree(opp);
Viresh Kumar129eec52014-11-27 08:54:06 +0530899
Viresh Kumar37a73ec2017-01-23 10:11:41 +0530900 mutex_unlock(&opp_table->lock);
Viresh Kumar31641cd2017-01-23 10:11:44 +0530901 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar129eec52014-11-27 08:54:06 +0530902}
903
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530904static void dev_pm_opp_get(struct dev_pm_opp *opp)
905{
906 kref_get(&opp->kref);
907}
908
Viresh Kumar70347642017-01-23 10:11:46 +0530909void dev_pm_opp_put(struct dev_pm_opp *opp)
910{
911 kref_put_mutex(&opp->kref, _opp_kref_release, &opp->opp_table->lock);
912}
913EXPORT_SYMBOL_GPL(dev_pm_opp_put);
914
Viresh Kumar129eec52014-11-27 08:54:06 +0530915/**
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530916 * dev_pm_opp_remove() - Remove an OPP from OPP table
Viresh Kumar129eec52014-11-27 08:54:06 +0530917 * @dev: device for which we do this operation
918 * @freq: OPP to remove with matching 'freq'
919 *
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530920 * This function removes an opp from the opp table.
Viresh Kumar129eec52014-11-27 08:54:06 +0530921 */
922void dev_pm_opp_remove(struct device *dev, unsigned long freq)
923{
924 struct dev_pm_opp *opp;
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530925 struct opp_table *opp_table;
Viresh Kumar129eec52014-11-27 08:54:06 +0530926 bool found = false;
927
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530928 opp_table = _find_opp_table(dev);
929 if (IS_ERR(opp_table))
Viresh Kumar5b650b32017-01-23 10:11:48 +0530930 return;
Viresh Kumar129eec52014-11-27 08:54:06 +0530931
Viresh Kumar37a73ec2017-01-23 10:11:41 +0530932 mutex_lock(&opp_table->lock);
933
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530934 list_for_each_entry(opp, &opp_table->opp_list, node) {
Viresh Kumar129eec52014-11-27 08:54:06 +0530935 if (opp->rate == freq) {
936 found = true;
937 break;
938 }
939 }
940
Viresh Kumar37a73ec2017-01-23 10:11:41 +0530941 mutex_unlock(&opp_table->lock);
942
Viresh Kumar5b650b32017-01-23 10:11:48 +0530943 if (found) {
944 dev_pm_opp_put(opp);
945 } else {
Viresh Kumar129eec52014-11-27 08:54:06 +0530946 dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
947 __func__, freq);
Viresh Kumar129eec52014-11-27 08:54:06 +0530948 }
949
Viresh Kumar5b650b32017-01-23 10:11:48 +0530950 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar129eec52014-11-27 08:54:06 +0530951}
952EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
953
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +0530954struct dev_pm_opp *_opp_allocate(struct opp_table *table)
Viresh Kumar23dacf62015-07-29 16:23:01 +0530955{
956 struct dev_pm_opp *opp;
Viresh Kumardfbe4672016-12-01 16:28:19 +0530957 int count, supply_size;
Viresh Kumar23dacf62015-07-29 16:23:01 +0530958
Viresh Kumardfbe4672016-12-01 16:28:19 +0530959 /* Allocate space for at least one supply */
960 count = table->regulator_count ? table->regulator_count : 1;
961 supply_size = sizeof(*opp->supplies) * count;
Viresh Kumar23dacf62015-07-29 16:23:01 +0530962
Viresh Kumardfbe4672016-12-01 16:28:19 +0530963 /* allocate new OPP node and supplies structures */
964 opp = kzalloc(sizeof(*opp) + supply_size, GFP_KERNEL);
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +0530965 if (!opp)
Viresh Kumar23dacf62015-07-29 16:23:01 +0530966 return NULL;
Viresh Kumar23dacf62015-07-29 16:23:01 +0530967
Viresh Kumardfbe4672016-12-01 16:28:19 +0530968 /* Put the supplies at the end of the OPP structure as an empty array */
969 opp->supplies = (struct dev_pm_opp_supply *)(opp + 1);
970 INIT_LIST_HEAD(&opp->node);
971
Viresh Kumar23dacf62015-07-29 16:23:01 +0530972 return opp;
973}
974
Viresh Kumar7d34d562016-02-09 10:30:34 +0530975static bool _opp_supported_by_regulators(struct dev_pm_opp *opp,
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530976 struct opp_table *opp_table)
Viresh Kumar7d34d562016-02-09 10:30:34 +0530977{
Viresh Kumardfbe4672016-12-01 16:28:19 +0530978 struct regulator *reg;
979 int i;
Viresh Kumar7d34d562016-02-09 10:30:34 +0530980
Viresh Kumardfbe4672016-12-01 16:28:19 +0530981 for (i = 0; i < opp_table->regulator_count; i++) {
982 reg = opp_table->regulators[i];
983
984 if (!regulator_is_supported_voltage(reg,
985 opp->supplies[i].u_volt_min,
986 opp->supplies[i].u_volt_max)) {
987 pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n",
988 __func__, opp->supplies[i].u_volt_min,
989 opp->supplies[i].u_volt_max);
990 return false;
991 }
Viresh Kumar7d34d562016-02-09 10:30:34 +0530992 }
993
994 return true;
995}
996
Viresh Kumara1e8c132018-04-06 14:35:45 +0530997static int _opp_is_duplicate(struct device *dev, struct dev_pm_opp *new_opp,
998 struct opp_table *opp_table,
999 struct list_head **head)
1000{
1001 struct dev_pm_opp *opp;
1002
1003 /*
1004 * Insert new OPP in order of increasing frequency and discard if
1005 * already present.
1006 *
1007 * Need to use &opp_table->opp_list in the condition part of the 'for'
1008 * loop, don't replace it with head otherwise it will become an infinite
1009 * loop.
1010 */
1011 list_for_each_entry(opp, &opp_table->opp_list, node) {
1012 if (new_opp->rate > opp->rate) {
1013 *head = &opp->node;
1014 continue;
1015 }
1016
1017 if (new_opp->rate < opp->rate)
1018 return 0;
1019
1020 /* Duplicate OPPs */
1021 dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
1022 __func__, opp->rate, opp->supplies[0].u_volt,
1023 opp->available, new_opp->rate,
1024 new_opp->supplies[0].u_volt, new_opp->available);
1025
1026 /* Should we compare voltages for all regulators here ? */
1027 return opp->available &&
1028 new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST;
1029 }
1030
1031 return 0;
1032}
1033
Viresh Kumar7f8538e2017-01-02 14:40:55 +05301034/*
1035 * Returns:
1036 * 0: On success. And appropriate error message for duplicate OPPs.
1037 * -EBUSY: For OPP with same freq/volt and is available. The callers of
1038 * _opp_add() must return 0 if they receive -EBUSY from it. This is to make
1039 * sure we don't print error messages unnecessarily if different parts of
1040 * kernel try to initialize the OPP table.
1041 * -EEXIST: For OPP with same freq but different volt or is unavailable. This
1042 * should be considered an error by the callers of _opp_add().
1043 */
Viresh Kumarf47b72a2016-05-05 16:20:33 +05301044int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
Viresh Kumara1e8c132018-04-06 14:35:45 +05301045 struct opp_table *opp_table, bool rate_not_available)
Viresh Kumar23dacf62015-07-29 16:23:01 +05301046{
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301047 struct list_head *head;
Viresh Kumardeaa5142015-11-11 07:59:01 +05301048 int ret;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301049
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301050 mutex_lock(&opp_table->lock);
1051 head = &opp_table->opp_list;
1052
Viresh Kumara1e8c132018-04-06 14:35:45 +05301053 if (likely(!rate_not_available)) {
1054 ret = _opp_is_duplicate(dev, new_opp, opp_table, &head);
1055 if (ret) {
1056 mutex_unlock(&opp_table->lock);
1057 return ret;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301058 }
Viresh Kumar23dacf62015-07-29 16:23:01 +05301059 }
1060
Viresh Kumarb6aa9832017-10-11 12:54:15 +05301061 if (opp_table->get_pstate)
1062 new_opp->pstate = opp_table->get_pstate(dev, new_opp->rate);
1063
Viresh Kumar052c6f12017-01-23 10:11:49 +05301064 list_add(&new_opp->node, head);
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301065 mutex_unlock(&opp_table->lock);
1066
1067 new_opp->opp_table = opp_table;
Viresh Kumar70347642017-01-23 10:11:46 +05301068 kref_init(&new_opp->kref);
Viresh Kumar23dacf62015-07-29 16:23:01 +05301069
Viresh Kumar31641cd2017-01-23 10:11:44 +05301070 /* Get a reference to the OPP table */
1071 _get_opp_table_kref(opp_table);
1072
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301073 ret = opp_debug_create_one(new_opp, opp_table);
Viresh Kumardeaa5142015-11-11 07:59:01 +05301074 if (ret)
1075 dev_err(dev, "%s: Failed to register opp to debugfs (%d)\n",
1076 __func__, ret);
1077
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301078 if (!_opp_supported_by_regulators(new_opp, opp_table)) {
Viresh Kumar7d34d562016-02-09 10:30:34 +05301079 new_opp->available = false;
1080 dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n",
1081 __func__, new_opp->rate);
1082 }
1083
Viresh Kumar23dacf62015-07-29 16:23:01 +05301084 return 0;
1085}
1086
Viresh Kumar737002b2015-07-29 16:22:58 +05301087/**
Viresh Kumarb64b9c3f2015-10-15 21:42:44 +05301088 * _opp_add_v1() - Allocate a OPP based on v1 bindings.
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301089 * @opp_table: OPP table
Nishanth Menone1f60b22010-10-13 00:13:10 +02001090 * @dev: device for which we do this operation
1091 * @freq: Frequency in Hz for this OPP
1092 * @u_volt: Voltage in uVolts for this OPP
1093 * @dynamic: Dynamically added OPPs.
1094 *
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301095 * This function adds an opp definition to the opp table and returns status.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001096 * The opp is made available by default and it can be controlled using
1097 * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
1098 *
Viresh Kumar8f8d37b2015-09-04 13:47:24 +05301099 * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table
1100 * and freed by dev_pm_opp_of_remove_table.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001101 *
Nishanth Menone1f60b22010-10-13 00:13:10 +02001102 * Return:
1103 * 0 On success OR
1104 * Duplicate OPPs (both freq and volt are same) and opp->available
1105 * -EEXIST Freq are same and volt are different OR
1106 * Duplicate OPPs (both freq and volt are same) and !opp->available
1107 * -ENOMEM Memory allocation failure
1108 */
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301109int _opp_add_v1(struct opp_table *opp_table, struct device *dev,
1110 unsigned long freq, long u_volt, bool dynamic)
Nishanth Menone1f60b22010-10-13 00:13:10 +02001111{
Viresh Kumar23dacf62015-07-29 16:23:01 +05301112 struct dev_pm_opp *new_opp;
Viresh Kumar50f8cfb2016-02-09 10:30:37 +05301113 unsigned long tol;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001114 int ret;
1115
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301116 new_opp = _opp_allocate(opp_table);
1117 if (!new_opp)
1118 return -ENOMEM;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301119
Nishanth Menone1f60b22010-10-13 00:13:10 +02001120 /* populate the opp table */
1121 new_opp->rate = freq;
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301122 tol = u_volt * opp_table->voltage_tolerance_v1 / 100;
Viresh Kumardfbe4672016-12-01 16:28:19 +05301123 new_opp->supplies[0].u_volt = u_volt;
1124 new_opp->supplies[0].u_volt_min = u_volt - tol;
1125 new_opp->supplies[0].u_volt_max = u_volt + tol;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001126 new_opp->available = true;
Viresh Kumaraa5f2f82015-07-29 16:23:00 +05301127 new_opp->dynamic = dynamic;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301128
Viresh Kumara1e8c132018-04-06 14:35:45 +05301129 ret = _opp_add(dev, new_opp, opp_table, false);
Viresh Kumar7f8538e2017-01-02 14:40:55 +05301130 if (ret) {
1131 /* Don't return error for duplicate OPPs */
1132 if (ret == -EBUSY)
1133 ret = 0;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301134 goto free_opp;
Viresh Kumar7f8538e2017-01-02 14:40:55 +05301135 }
Viresh Kumar23dacf62015-07-29 16:23:01 +05301136
Nishanth Menone1f60b22010-10-13 00:13:10 +02001137 /*
1138 * Notify the changes in the availability of the operable
1139 * frequency/voltage list.
1140 */
Viresh Kumar052c6f12017-01-23 10:11:49 +05301141 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001142 return 0;
1143
1144free_opp:
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301145 _opp_free(new_opp);
1146
Nishanth Menone1f60b22010-10-13 00:13:10 +02001147 return ret;
1148}
Viresh Kumar383934092014-11-25 16:04:18 +05301149
Viresh Kumar27465902015-07-29 16:23:02 +05301150/**
Viresh Kumar7de36b02015-12-09 08:01:46 +05301151 * dev_pm_opp_set_supported_hw() - Set supported platforms
1152 * @dev: Device for which supported-hw has to be set.
1153 * @versions: Array of hierarchy of versions to match.
1154 * @count: Number of elements in the array.
1155 *
1156 * This is required only for the V2 bindings, and it enables a platform to
1157 * specify the hierarchy of versions it supports. OPP layer will then enable
1158 * OPPs, which are available for those versions, based on its 'opp-supported-hw'
1159 * property.
Viresh Kumar7de36b02015-12-09 08:01:46 +05301160 */
Viresh Kumarfa301842017-01-23 10:11:43 +05301161struct opp_table *dev_pm_opp_set_supported_hw(struct device *dev,
1162 const u32 *versions, unsigned int count)
Viresh Kumar7de36b02015-12-09 08:01:46 +05301163{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301164 struct opp_table *opp_table;
Viresh Kumarfa301842017-01-23 10:11:43 +05301165 int ret;
Viresh Kumar7de36b02015-12-09 08:01:46 +05301166
Viresh Kumarfa301842017-01-23 10:11:43 +05301167 opp_table = dev_pm_opp_get_opp_table(dev);
1168 if (!opp_table)
1169 return ERR_PTR(-ENOMEM);
Viresh Kumar7de36b02015-12-09 08:01:46 +05301170
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301171 /* Make sure there are no concurrent readers while updating opp_table */
1172 WARN_ON(!list_empty(&opp_table->opp_list));
Viresh Kumar7de36b02015-12-09 08:01:46 +05301173
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301174 /* Do we already have a version hierarchy associated with opp_table? */
1175 if (opp_table->supported_hw) {
Viresh Kumar7de36b02015-12-09 08:01:46 +05301176 dev_err(dev, "%s: Already have supported hardware list\n",
1177 __func__);
1178 ret = -EBUSY;
1179 goto err;
1180 }
1181
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301182 opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions),
Viresh Kumar7de36b02015-12-09 08:01:46 +05301183 GFP_KERNEL);
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301184 if (!opp_table->supported_hw) {
Viresh Kumar7de36b02015-12-09 08:01:46 +05301185 ret = -ENOMEM;
1186 goto err;
1187 }
1188
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301189 opp_table->supported_hw_count = count;
Viresh Kumarfa301842017-01-23 10:11:43 +05301190
1191 return opp_table;
Viresh Kumar7de36b02015-12-09 08:01:46 +05301192
1193err:
Viresh Kumarfa301842017-01-23 10:11:43 +05301194 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar7de36b02015-12-09 08:01:46 +05301195
Viresh Kumarfa301842017-01-23 10:11:43 +05301196 return ERR_PTR(ret);
Viresh Kumar7de36b02015-12-09 08:01:46 +05301197}
1198EXPORT_SYMBOL_GPL(dev_pm_opp_set_supported_hw);
1199
1200/**
1201 * dev_pm_opp_put_supported_hw() - Releases resources blocked for supported hw
Viresh Kumarfa301842017-01-23 10:11:43 +05301202 * @opp_table: OPP table returned by dev_pm_opp_set_supported_hw().
Viresh Kumar7de36b02015-12-09 08:01:46 +05301203 *
1204 * This is required only for the V2 bindings, and is called for a matching
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301205 * dev_pm_opp_set_supported_hw(). Until this is called, the opp_table structure
Viresh Kumar7de36b02015-12-09 08:01:46 +05301206 * will not be freed.
Viresh Kumar7de36b02015-12-09 08:01:46 +05301207 */
Viresh Kumarfa301842017-01-23 10:11:43 +05301208void dev_pm_opp_put_supported_hw(struct opp_table *opp_table)
Viresh Kumar7de36b02015-12-09 08:01:46 +05301209{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301210 /* Make sure there are no concurrent readers while updating opp_table */
1211 WARN_ON(!list_empty(&opp_table->opp_list));
Viresh Kumar7de36b02015-12-09 08:01:46 +05301212
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301213 if (!opp_table->supported_hw) {
Viresh Kumarfa301842017-01-23 10:11:43 +05301214 pr_err("%s: Doesn't have supported hardware list\n",
1215 __func__);
1216 return;
Viresh Kumar7de36b02015-12-09 08:01:46 +05301217 }
1218
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301219 kfree(opp_table->supported_hw);
1220 opp_table->supported_hw = NULL;
1221 opp_table->supported_hw_count = 0;
Viresh Kumar7de36b02015-12-09 08:01:46 +05301222
Viresh Kumarfa301842017-01-23 10:11:43 +05301223 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar7de36b02015-12-09 08:01:46 +05301224}
1225EXPORT_SYMBOL_GPL(dev_pm_opp_put_supported_hw);
1226
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301227/**
1228 * dev_pm_opp_set_prop_name() - Set prop-extn name
Viresh Kumara5da6442016-02-16 14:17:52 +05301229 * @dev: Device for which the prop-name has to be set.
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301230 * @name: name to postfix to properties.
1231 *
1232 * This is required only for the V2 bindings, and it enables a platform to
1233 * specify the extn to be used for certain property names. The properties to
1234 * which the extension will apply are opp-microvolt and opp-microamp. OPP core
1235 * should postfix the property name with -<name> while looking for them.
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301236 */
Viresh Kumarfa301842017-01-23 10:11:43 +05301237struct opp_table *dev_pm_opp_set_prop_name(struct device *dev, const char *name)
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301238{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301239 struct opp_table *opp_table;
Viresh Kumarfa301842017-01-23 10:11:43 +05301240 int ret;
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301241
Viresh Kumarfa301842017-01-23 10:11:43 +05301242 opp_table = dev_pm_opp_get_opp_table(dev);
1243 if (!opp_table)
1244 return ERR_PTR(-ENOMEM);
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301245
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301246 /* Make sure there are no concurrent readers while updating opp_table */
1247 WARN_ON(!list_empty(&opp_table->opp_list));
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301248
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301249 /* Do we already have a prop-name associated with opp_table? */
1250 if (opp_table->prop_name) {
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301251 dev_err(dev, "%s: Already have prop-name %s\n", __func__,
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301252 opp_table->prop_name);
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301253 ret = -EBUSY;
1254 goto err;
1255 }
1256
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301257 opp_table->prop_name = kstrdup(name, GFP_KERNEL);
1258 if (!opp_table->prop_name) {
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301259 ret = -ENOMEM;
1260 goto err;
1261 }
1262
Viresh Kumarfa301842017-01-23 10:11:43 +05301263 return opp_table;
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301264
1265err:
Viresh Kumarfa301842017-01-23 10:11:43 +05301266 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301267
Viresh Kumarfa301842017-01-23 10:11:43 +05301268 return ERR_PTR(ret);
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301269}
1270EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name);
1271
1272/**
1273 * dev_pm_opp_put_prop_name() - Releases resources blocked for prop-name
Viresh Kumarfa301842017-01-23 10:11:43 +05301274 * @opp_table: OPP table returned by dev_pm_opp_set_prop_name().
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301275 *
1276 * This is required only for the V2 bindings, and is called for a matching
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301277 * dev_pm_opp_set_prop_name(). Until this is called, the opp_table structure
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301278 * will not be freed.
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301279 */
Viresh Kumarfa301842017-01-23 10:11:43 +05301280void dev_pm_opp_put_prop_name(struct opp_table *opp_table)
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301281{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301282 /* Make sure there are no concurrent readers while updating opp_table */
1283 WARN_ON(!list_empty(&opp_table->opp_list));
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301284
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301285 if (!opp_table->prop_name) {
Viresh Kumarfa301842017-01-23 10:11:43 +05301286 pr_err("%s: Doesn't have a prop-name\n", __func__);
1287 return;
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301288 }
1289
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301290 kfree(opp_table->prop_name);
1291 opp_table->prop_name = NULL;
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301292
Viresh Kumarfa301842017-01-23 10:11:43 +05301293 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301294}
1295EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name);
1296
Viresh Kumar94735582016-12-01 16:28:20 +05301297static int _allocate_set_opp_data(struct opp_table *opp_table)
1298{
1299 struct dev_pm_set_opp_data *data;
1300 int len, count = opp_table->regulator_count;
1301
1302 if (WARN_ON(!count))
1303 return -EINVAL;
1304
1305 /* space for set_opp_data */
1306 len = sizeof(*data);
1307
1308 /* space for old_opp.supplies and new_opp.supplies */
1309 len += 2 * sizeof(struct dev_pm_opp_supply) * count;
1310
1311 data = kzalloc(len, GFP_KERNEL);
1312 if (!data)
1313 return -ENOMEM;
1314
1315 data->old_opp.supplies = (void *)(data + 1);
1316 data->new_opp.supplies = data->old_opp.supplies + count;
1317
1318 opp_table->set_opp_data = data;
1319
1320 return 0;
1321}
1322
1323static void _free_set_opp_data(struct opp_table *opp_table)
1324{
1325 kfree(opp_table->set_opp_data);
1326 opp_table->set_opp_data = NULL;
1327}
1328
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301329/**
Viresh Kumardfbe4672016-12-01 16:28:19 +05301330 * dev_pm_opp_set_regulators() - Set regulator names for the device
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301331 * @dev: Device for which regulator name is being set.
Viresh Kumardfbe4672016-12-01 16:28:19 +05301332 * @names: Array of pointers to the names of the regulator.
1333 * @count: Number of regulators.
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301334 *
1335 * In order to support OPP switching, OPP layer needs to know the name of the
Viresh Kumardfbe4672016-12-01 16:28:19 +05301336 * device's regulators, as the core would be required to switch voltages as
1337 * well.
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301338 *
1339 * This must be called before any OPPs are initialized for the device.
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301340 */
Viresh Kumardfbe4672016-12-01 16:28:19 +05301341struct opp_table *dev_pm_opp_set_regulators(struct device *dev,
1342 const char * const names[],
1343 unsigned int count)
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301344{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301345 struct opp_table *opp_table;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301346 struct regulator *reg;
Viresh Kumardfbe4672016-12-01 16:28:19 +05301347 int ret, i;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301348
Viresh Kumarfa301842017-01-23 10:11:43 +05301349 opp_table = dev_pm_opp_get_opp_table(dev);
1350 if (!opp_table)
1351 return ERR_PTR(-ENOMEM);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301352
1353 /* This should be called before OPPs are initialized */
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301354 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301355 ret = -EBUSY;
1356 goto err;
1357 }
1358
Viresh Kumardfbe4672016-12-01 16:28:19 +05301359 /* Already have regulators set */
Viresh Kumare231f8d2016-12-01 16:28:22 +05301360 if (opp_table->regulators) {
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301361 ret = -EBUSY;
1362 goto err;
1363 }
Viresh Kumardfbe4672016-12-01 16:28:19 +05301364
1365 opp_table->regulators = kmalloc_array(count,
1366 sizeof(*opp_table->regulators),
1367 GFP_KERNEL);
1368 if (!opp_table->regulators) {
1369 ret = -ENOMEM;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301370 goto err;
1371 }
1372
Viresh Kumardfbe4672016-12-01 16:28:19 +05301373 for (i = 0; i < count; i++) {
1374 reg = regulator_get_optional(dev, names[i]);
1375 if (IS_ERR(reg)) {
1376 ret = PTR_ERR(reg);
1377 if (ret != -EPROBE_DEFER)
1378 dev_err(dev, "%s: no regulator (%s) found: %d\n",
1379 __func__, names[i], ret);
1380 goto free_regulators;
1381 }
1382
1383 opp_table->regulators[i] = reg;
1384 }
1385
1386 opp_table->regulator_count = count;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301387
Viresh Kumar94735582016-12-01 16:28:20 +05301388 /* Allocate block only once to pass to set_opp() routines */
1389 ret = _allocate_set_opp_data(opp_table);
1390 if (ret)
1391 goto free_regulators;
1392
Stephen Boyd91291d92016-11-30 16:21:25 +05301393 return opp_table;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301394
Viresh Kumardfbe4672016-12-01 16:28:19 +05301395free_regulators:
1396 while (i != 0)
1397 regulator_put(opp_table->regulators[--i]);
1398
1399 kfree(opp_table->regulators);
1400 opp_table->regulators = NULL;
Viresh Kumar94735582016-12-01 16:28:20 +05301401 opp_table->regulator_count = 0;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301402err:
Viresh Kumarfa301842017-01-23 10:11:43 +05301403 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301404
Stephen Boyd91291d92016-11-30 16:21:25 +05301405 return ERR_PTR(ret);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301406}
Viresh Kumardfbe4672016-12-01 16:28:19 +05301407EXPORT_SYMBOL_GPL(dev_pm_opp_set_regulators);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301408
1409/**
Viresh Kumardfbe4672016-12-01 16:28:19 +05301410 * dev_pm_opp_put_regulators() - Releases resources blocked for regulator
1411 * @opp_table: OPP table returned from dev_pm_opp_set_regulators().
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301412 */
Viresh Kumardfbe4672016-12-01 16:28:19 +05301413void dev_pm_opp_put_regulators(struct opp_table *opp_table)
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301414{
Viresh Kumardfbe4672016-12-01 16:28:19 +05301415 int i;
1416
Viresh Kumardfbe4672016-12-01 16:28:19 +05301417 if (!opp_table->regulators) {
1418 pr_err("%s: Doesn't have regulators set\n", __func__);
Viresh Kumarfa301842017-01-23 10:11:43 +05301419 return;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301420 }
1421
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301422 /* Make sure there are no concurrent readers while updating opp_table */
1423 WARN_ON(!list_empty(&opp_table->opp_list));
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301424
Viresh Kumardfbe4672016-12-01 16:28:19 +05301425 for (i = opp_table->regulator_count - 1; i >= 0; i--)
1426 regulator_put(opp_table->regulators[i]);
1427
Viresh Kumar94735582016-12-01 16:28:20 +05301428 _free_set_opp_data(opp_table);
1429
Viresh Kumardfbe4672016-12-01 16:28:19 +05301430 kfree(opp_table->regulators);
1431 opp_table->regulators = NULL;
1432 opp_table->regulator_count = 0;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301433
Viresh Kumarfa301842017-01-23 10:11:43 +05301434 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301435}
Viresh Kumardfbe4672016-12-01 16:28:19 +05301436EXPORT_SYMBOL_GPL(dev_pm_opp_put_regulators);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301437
Viresh Kumar383934092014-11-25 16:04:18 +05301438/**
Viresh Kumar829a4e82017-06-21 10:29:13 +05301439 * dev_pm_opp_set_clkname() - Set clk name for the device
1440 * @dev: Device for which clk name is being set.
1441 * @name: Clk name.
1442 *
1443 * In order to support OPP switching, OPP layer needs to get pointer to the
1444 * clock for the device. Simple cases work fine without using this routine (i.e.
1445 * by passing connection-id as NULL), but for a device with multiple clocks
1446 * available, the OPP core needs to know the exact name of the clk to use.
1447 *
1448 * This must be called before any OPPs are initialized for the device.
1449 */
1450struct opp_table *dev_pm_opp_set_clkname(struct device *dev, const char *name)
1451{
1452 struct opp_table *opp_table;
1453 int ret;
1454
1455 opp_table = dev_pm_opp_get_opp_table(dev);
1456 if (!opp_table)
1457 return ERR_PTR(-ENOMEM);
1458
1459 /* This should be called before OPPs are initialized */
1460 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
1461 ret = -EBUSY;
1462 goto err;
1463 }
1464
1465 /* Already have default clk set, free it */
1466 if (!IS_ERR(opp_table->clk))
1467 clk_put(opp_table->clk);
1468
1469 /* Find clk for the device */
1470 opp_table->clk = clk_get(dev, name);
1471 if (IS_ERR(opp_table->clk)) {
1472 ret = PTR_ERR(opp_table->clk);
1473 if (ret != -EPROBE_DEFER) {
1474 dev_err(dev, "%s: Couldn't find clock: %d\n", __func__,
1475 ret);
1476 }
1477 goto err;
1478 }
1479
1480 return opp_table;
1481
1482err:
1483 dev_pm_opp_put_opp_table(opp_table);
1484
1485 return ERR_PTR(ret);
1486}
1487EXPORT_SYMBOL_GPL(dev_pm_opp_set_clkname);
1488
1489/**
1490 * dev_pm_opp_put_clkname() - Releases resources blocked for clk.
1491 * @opp_table: OPP table returned from dev_pm_opp_set_clkname().
1492 */
1493void dev_pm_opp_put_clkname(struct opp_table *opp_table)
1494{
1495 /* Make sure there are no concurrent readers while updating opp_table */
1496 WARN_ON(!list_empty(&opp_table->opp_list));
1497
1498 clk_put(opp_table->clk);
1499 opp_table->clk = ERR_PTR(-EINVAL);
1500
1501 dev_pm_opp_put_opp_table(opp_table);
1502}
1503EXPORT_SYMBOL_GPL(dev_pm_opp_put_clkname);
1504
1505/**
Viresh Kumar4dab1602016-12-01 16:28:21 +05301506 * dev_pm_opp_register_set_opp_helper() - Register custom set OPP helper
1507 * @dev: Device for which the helper is getting registered.
1508 * @set_opp: Custom set OPP helper.
1509 *
1510 * This is useful to support complex platforms (like platforms with multiple
1511 * regulators per device), instead of the generic OPP set rate helper.
1512 *
1513 * This must be called before any OPPs are initialized for the device.
Viresh Kumar4dab1602016-12-01 16:28:21 +05301514 */
Viresh Kumarfa301842017-01-23 10:11:43 +05301515struct opp_table *dev_pm_opp_register_set_opp_helper(struct device *dev,
Viresh Kumar4dab1602016-12-01 16:28:21 +05301516 int (*set_opp)(struct dev_pm_set_opp_data *data))
1517{
1518 struct opp_table *opp_table;
1519 int ret;
1520
1521 if (!set_opp)
Viresh Kumarfa301842017-01-23 10:11:43 +05301522 return ERR_PTR(-EINVAL);
Viresh Kumar4dab1602016-12-01 16:28:21 +05301523
Viresh Kumarfa301842017-01-23 10:11:43 +05301524 opp_table = dev_pm_opp_get_opp_table(dev);
1525 if (!opp_table)
1526 return ERR_PTR(-ENOMEM);
Viresh Kumar4dab1602016-12-01 16:28:21 +05301527
1528 /* This should be called before OPPs are initialized */
1529 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
1530 ret = -EBUSY;
1531 goto err;
1532 }
1533
1534 /* Already have custom set_opp helper */
1535 if (WARN_ON(opp_table->set_opp)) {
1536 ret = -EBUSY;
1537 goto err;
1538 }
1539
1540 opp_table->set_opp = set_opp;
1541
Viresh Kumarfa301842017-01-23 10:11:43 +05301542 return opp_table;
Viresh Kumar4dab1602016-12-01 16:28:21 +05301543
1544err:
Viresh Kumarfa301842017-01-23 10:11:43 +05301545 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar4dab1602016-12-01 16:28:21 +05301546
Viresh Kumarfa301842017-01-23 10:11:43 +05301547 return ERR_PTR(ret);
Viresh Kumar4dab1602016-12-01 16:28:21 +05301548}
1549EXPORT_SYMBOL_GPL(dev_pm_opp_register_set_opp_helper);
1550
1551/**
Viresh Kumar604a7ae2017-10-05 17:26:21 +05301552 * dev_pm_opp_unregister_set_opp_helper() - Releases resources blocked for
Viresh Kumar4dab1602016-12-01 16:28:21 +05301553 * set_opp helper
Viresh Kumarfa301842017-01-23 10:11:43 +05301554 * @opp_table: OPP table returned from dev_pm_opp_register_set_opp_helper().
Viresh Kumar4dab1602016-12-01 16:28:21 +05301555 *
Viresh Kumarfa301842017-01-23 10:11:43 +05301556 * Release resources blocked for platform specific set_opp helper.
Viresh Kumar4dab1602016-12-01 16:28:21 +05301557 */
Viresh Kumar604a7ae2017-10-05 17:26:21 +05301558void dev_pm_opp_unregister_set_opp_helper(struct opp_table *opp_table)
Viresh Kumar4dab1602016-12-01 16:28:21 +05301559{
Viresh Kumar4dab1602016-12-01 16:28:21 +05301560 if (!opp_table->set_opp) {
Viresh Kumarfa301842017-01-23 10:11:43 +05301561 pr_err("%s: Doesn't have custom set_opp helper set\n",
1562 __func__);
1563 return;
Viresh Kumar4dab1602016-12-01 16:28:21 +05301564 }
1565
1566 /* Make sure there are no concurrent readers while updating opp_table */
1567 WARN_ON(!list_empty(&opp_table->opp_list));
1568
1569 opp_table->set_opp = NULL;
1570
Viresh Kumarfa301842017-01-23 10:11:43 +05301571 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar4dab1602016-12-01 16:28:21 +05301572}
Viresh Kumar604a7ae2017-10-05 17:26:21 +05301573EXPORT_SYMBOL_GPL(dev_pm_opp_unregister_set_opp_helper);
Viresh Kumar4dab1602016-12-01 16:28:21 +05301574
1575/**
Viresh Kumarb6aa9832017-10-11 12:54:15 +05301576 * dev_pm_opp_register_get_pstate_helper() - Register get_pstate() helper.
1577 * @dev: Device for which the helper is getting registered.
1578 * @get_pstate: Helper.
1579 *
1580 * TODO: Remove this callback after the same information is available via Device
1581 * Tree.
1582 *
1583 * This allows a platform to initialize the performance states of individual
1584 * OPPs for its devices, until we get similar information directly from DT.
1585 *
1586 * This must be called before the OPPs are initialized for the device.
1587 */
1588struct opp_table *dev_pm_opp_register_get_pstate_helper(struct device *dev,
1589 int (*get_pstate)(struct device *dev, unsigned long rate))
1590{
1591 struct opp_table *opp_table;
1592 int ret;
1593
1594 if (!get_pstate)
1595 return ERR_PTR(-EINVAL);
1596
1597 opp_table = dev_pm_opp_get_opp_table(dev);
1598 if (!opp_table)
1599 return ERR_PTR(-ENOMEM);
1600
1601 /* This should be called before OPPs are initialized */
1602 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
1603 ret = -EBUSY;
1604 goto err;
1605 }
1606
1607 /* Already have genpd_performance_state set */
1608 if (WARN_ON(opp_table->genpd_performance_state)) {
1609 ret = -EBUSY;
1610 goto err;
1611 }
1612
1613 opp_table->genpd_performance_state = true;
1614 opp_table->get_pstate = get_pstate;
1615
1616 return opp_table;
1617
1618err:
1619 dev_pm_opp_put_opp_table(opp_table);
1620
1621 return ERR_PTR(ret);
1622}
1623EXPORT_SYMBOL_GPL(dev_pm_opp_register_get_pstate_helper);
1624
1625/**
1626 * dev_pm_opp_unregister_get_pstate_helper() - Releases resources blocked for
1627 * get_pstate() helper
1628 * @opp_table: OPP table returned from dev_pm_opp_register_get_pstate_helper().
1629 *
1630 * Release resources blocked for platform specific get_pstate() helper.
1631 */
1632void dev_pm_opp_unregister_get_pstate_helper(struct opp_table *opp_table)
1633{
1634 if (!opp_table->genpd_performance_state) {
1635 pr_err("%s: Doesn't have performance states set\n",
1636 __func__);
1637 return;
1638 }
1639
1640 /* Make sure there are no concurrent readers while updating opp_table */
1641 WARN_ON(!list_empty(&opp_table->opp_list));
1642
1643 opp_table->genpd_performance_state = false;
1644 opp_table->get_pstate = NULL;
1645
1646 dev_pm_opp_put_opp_table(opp_table);
1647}
1648EXPORT_SYMBOL_GPL(dev_pm_opp_unregister_get_pstate_helper);
1649
1650/**
Nishanth Menone1f60b22010-10-13 00:13:10 +02001651 * dev_pm_opp_add() - Add an OPP table from a table definitions
1652 * @dev: device for which we do this operation
Nishanth Menon47d43ba2013-09-19 16:03:51 -05001653 * @freq: Frequency in Hz for this OPP
Nishanth Menone1f60b22010-10-13 00:13:10 +02001654 * @u_volt: Voltage in uVolts for this OPP
1655 *
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301656 * This function adds an opp definition to the opp table and returns status.
Nishanth Menon47d43ba2013-09-19 16:03:51 -05001657 * The opp is made available by default and it can be controlled using
Nishanth Menone1f60b22010-10-13 00:13:10 +02001658 * dev_pm_opp_enable/disable functions.
1659 *
Viresh Kumara7470db2014-11-25 16:04:17 +05301660 * Return:
1661 * 0 On success OR
1662 * Duplicate OPPs (both freq and volt are same) and opp->available
1663 * -EEXIST Freq are same and volt are different OR
1664 * Duplicate OPPs (both freq and volt are same) and !opp->available
Viresh Kumar383934092014-11-25 16:04:18 +05301665 * -ENOMEM Memory allocation failure
Viresh Kumara7470db2014-11-25 16:04:17 +05301666 */
Nishanth Menone1f60b22010-10-13 00:13:10 +02001667int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
1668{
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301669 struct opp_table *opp_table;
1670 int ret;
1671
Viresh Kumarb83c1892017-01-23 10:11:45 +05301672 opp_table = dev_pm_opp_get_opp_table(dev);
1673 if (!opp_table)
1674 return -ENOMEM;
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301675
1676 ret = _opp_add_v1(opp_table, dev, freq, u_volt, true);
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301677
Viresh Kumarb83c1892017-01-23 10:11:45 +05301678 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301679 return ret;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001680}
1681EXPORT_SYMBOL_GPL(dev_pm_opp_add);
1682
Nishanth Menone1f60b22010-10-13 00:13:10 +02001683/**
Nishanth Menon327854c2014-12-24 11:22:56 -06001684 * _opp_set_availability() - helper to set the availability of an opp
Nishanth Menone1f60b22010-10-13 00:13:10 +02001685 * @dev: device for which we do this operation
1686 * @freq: OPP frequency to modify availability
1687 * @availability_req: availability status requested for this opp
1688 *
Viresh Kumar052c6f12017-01-23 10:11:49 +05301689 * Set the availability of an OPP, opp_{enable,disable} share a common logic
1690 * which is isolated here.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001691 *
Nishanth Menon984f16c2014-12-24 11:22:57 -06001692 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
Stephen Boyde1a2d492015-09-24 12:28:44 -07001693 * copy operation, returns 0 if no modification was done OR modification was
Nishanth Menone1f60b22010-10-13 00:13:10 +02001694 * successful.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001695 */
Nishanth Menon327854c2014-12-24 11:22:56 -06001696static int _opp_set_availability(struct device *dev, unsigned long freq,
1697 bool availability_req)
Nishanth Menone1f60b22010-10-13 00:13:10 +02001698{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301699 struct opp_table *opp_table;
Viresh Kumara7f39872017-01-23 10:11:50 +05301700 struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001701 int r = 0;
1702
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301703 /* Find the opp_table */
1704 opp_table = _find_opp_table(dev);
1705 if (IS_ERR(opp_table)) {
1706 r = PTR_ERR(opp_table);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001707 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
Viresh Kumara7f39872017-01-23 10:11:50 +05301708 return r;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001709 }
1710
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301711 mutex_lock(&opp_table->lock);
1712
Nishanth Menone1f60b22010-10-13 00:13:10 +02001713 /* Do we have the frequency? */
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301714 list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
Nishanth Menone1f60b22010-10-13 00:13:10 +02001715 if (tmp_opp->rate == freq) {
1716 opp = tmp_opp;
1717 break;
1718 }
1719 }
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301720
Nishanth Menone1f60b22010-10-13 00:13:10 +02001721 if (IS_ERR(opp)) {
1722 r = PTR_ERR(opp);
1723 goto unlock;
1724 }
1725
1726 /* Is update really needed? */
1727 if (opp->available == availability_req)
1728 goto unlock;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001729
Viresh Kumara7f39872017-01-23 10:11:50 +05301730 opp->available = availability_req;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001731
Viresh Kumare4d8ae02017-09-21 10:44:36 -07001732 dev_pm_opp_get(opp);
1733 mutex_unlock(&opp_table->lock);
1734
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001735 /* Notify the change of the OPP availability */
1736 if (availability_req)
Viresh Kumar052c6f12017-01-23 10:11:49 +05301737 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE,
Viresh Kumara7f39872017-01-23 10:11:50 +05301738 opp);
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001739 else
Viresh Kumar052c6f12017-01-23 10:11:49 +05301740 blocking_notifier_call_chain(&opp_table->head,
Viresh Kumara7f39872017-01-23 10:11:50 +05301741 OPP_EVENT_DISABLE, opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001742
Viresh Kumare4d8ae02017-09-21 10:44:36 -07001743 dev_pm_opp_put(opp);
1744 goto put_table;
1745
Nishanth Menone1f60b22010-10-13 00:13:10 +02001746unlock:
Viresh Kumar5b650b32017-01-23 10:11:48 +05301747 mutex_unlock(&opp_table->lock);
Viresh Kumare4d8ae02017-09-21 10:44:36 -07001748put_table:
Viresh Kumar5b650b32017-01-23 10:11:48 +05301749 dev_pm_opp_put_opp_table(opp_table);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001750 return r;
1751}
1752
1753/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001754 * dev_pm_opp_enable() - Enable a specific OPP
Nishanth Menone1f60b22010-10-13 00:13:10 +02001755 * @dev: device for which we do this operation
1756 * @freq: OPP frequency to enable
1757 *
1758 * Enables a provided opp. If the operation is valid, this returns 0, else the
1759 * corresponding error value. It is meant to be used for users an OPP available
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001760 * after being temporarily made unavailable with dev_pm_opp_disable.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001761 *
Nishanth Menon984f16c2014-12-24 11:22:57 -06001762 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
Stephen Boyde1a2d492015-09-24 12:28:44 -07001763 * copy operation, returns 0 if no modification was done OR modification was
Nishanth Menon984f16c2014-12-24 11:22:57 -06001764 * successful.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001765 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001766int dev_pm_opp_enable(struct device *dev, unsigned long freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +02001767{
Nishanth Menon327854c2014-12-24 11:22:56 -06001768 return _opp_set_availability(dev, freq, true);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001769}
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001770EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001771
1772/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001773 * dev_pm_opp_disable() - Disable a specific OPP
Nishanth Menone1f60b22010-10-13 00:13:10 +02001774 * @dev: device for which we do this operation
1775 * @freq: OPP frequency to disable
1776 *
1777 * Disables a provided opp. If the operation is valid, this returns
1778 * 0, else the corresponding error value. It is meant to be a temporary
1779 * control by users to make this OPP not available until the circumstances are
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001780 * right to make it available again (with a call to dev_pm_opp_enable).
Nishanth Menone1f60b22010-10-13 00:13:10 +02001781 *
Nishanth Menon984f16c2014-12-24 11:22:57 -06001782 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
Stephen Boyde1a2d492015-09-24 12:28:44 -07001783 * copy operation, returns 0 if no modification was done OR modification was
Nishanth Menon984f16c2014-12-24 11:22:57 -06001784 * successful.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001785 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001786int dev_pm_opp_disable(struct device *dev, unsigned long freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +02001787{
Nishanth Menon327854c2014-12-24 11:22:56 -06001788 return _opp_set_availability(dev, freq, false);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001789}
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001790EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001791
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001792/**
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301793 * dev_pm_opp_register_notifier() - Register OPP notifier for the device
1794 * @dev: Device for which notifier needs to be registered
1795 * @nb: Notifier block to be registered
Nishanth Menon984f16c2014-12-24 11:22:57 -06001796 *
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301797 * Return: 0 on success or a negative error value.
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001798 */
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301799int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb)
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001800{
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301801 struct opp_table *opp_table;
1802 int ret;
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001803
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301804 opp_table = _find_opp_table(dev);
Viresh Kumar5b650b32017-01-23 10:11:48 +05301805 if (IS_ERR(opp_table))
1806 return PTR_ERR(opp_table);
1807
Viresh Kumar052c6f12017-01-23 10:11:49 +05301808 ret = blocking_notifier_chain_register(&opp_table->head, nb);
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301809
Viresh Kumar5b650b32017-01-23 10:11:48 +05301810 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301811
1812 return ret;
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001813}
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301814EXPORT_SYMBOL(dev_pm_opp_register_notifier);
1815
1816/**
1817 * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device
1818 * @dev: Device for which notifier needs to be unregistered
1819 * @nb: Notifier block to be unregistered
1820 *
1821 * Return: 0 on success or a negative error value.
1822 */
1823int dev_pm_opp_unregister_notifier(struct device *dev,
1824 struct notifier_block *nb)
1825{
1826 struct opp_table *opp_table;
1827 int ret;
1828
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301829 opp_table = _find_opp_table(dev);
Viresh Kumar5b650b32017-01-23 10:11:48 +05301830 if (IS_ERR(opp_table))
1831 return PTR_ERR(opp_table);
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301832
Viresh Kumar052c6f12017-01-23 10:11:49 +05301833 ret = blocking_notifier_chain_unregister(&opp_table->head, nb);
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301834
Viresh Kumar5b650b32017-01-23 10:11:48 +05301835 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301836
1837 return ret;
1838}
1839EXPORT_SYMBOL(dev_pm_opp_unregister_notifier);
Shawn Guob496dfb2012-09-05 01:09:12 +02001840
Sudeep Holla411466c2016-05-03 15:05:04 +01001841/*
1842 * Free OPPs either created using static entries present in DT or even the
1843 * dynamically added entries based on remove_all param.
Shawn Guob496dfb2012-09-05 01:09:12 +02001844 */
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301845void _dev_pm_opp_remove_table(struct opp_table *opp_table, struct device *dev,
1846 bool remove_all)
Viresh Kumar9274c892017-01-02 14:41:00 +05301847{
1848 struct dev_pm_opp *opp, *tmp;
1849
Viresh Kumar9274c892017-01-02 14:41:00 +05301850 /* Find if opp_table manages a single device */
1851 if (list_is_singular(&opp_table->dev_list)) {
1852 /* Free static OPPs */
1853 list_for_each_entry_safe(opp, tmp, &opp_table->opp_list, node) {
1854 if (remove_all || !opp->dynamic)
Viresh Kumar70347642017-01-23 10:11:46 +05301855 dev_pm_opp_put(opp);
Viresh Kumar9274c892017-01-02 14:41:00 +05301856 }
Viresh Kumar009acd12017-10-11 12:54:14 +05301857
1858 /*
1859 * The OPP table is getting removed, drop the performance state
1860 * constraints.
1861 */
1862 if (opp_table->genpd_performance_state)
1863 dev_pm_genpd_set_performance_state(dev, 0);
Viresh Kumar9274c892017-01-02 14:41:00 +05301864 } else {
1865 _remove_opp_dev(_find_opp_dev(dev, opp_table), opp_table);
1866 }
1867}
1868
1869void _dev_pm_opp_find_and_remove_table(struct device *dev, bool remove_all)
Viresh Kumar737002b2015-07-29 16:22:58 +05301870{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301871 struct opp_table *opp_table;
Viresh Kumar737002b2015-07-29 16:22:58 +05301872
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301873 /* Check for existing table for 'dev' */
1874 opp_table = _find_opp_table(dev);
1875 if (IS_ERR(opp_table)) {
1876 int error = PTR_ERR(opp_table);
Viresh Kumar737002b2015-07-29 16:22:58 +05301877
1878 if (error != -ENODEV)
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301879 WARN(1, "%s: opp_table: %d\n",
Viresh Kumar737002b2015-07-29 16:22:58 +05301880 IS_ERR_OR_NULL(dev) ?
1881 "Invalid device" : dev_name(dev),
1882 error);
Viresh Kumar5b650b32017-01-23 10:11:48 +05301883 return;
Viresh Kumar737002b2015-07-29 16:22:58 +05301884 }
1885
Viresh Kumar9274c892017-01-02 14:41:00 +05301886 _dev_pm_opp_remove_table(opp_table, dev, remove_all);
Viresh Kumar737002b2015-07-29 16:22:58 +05301887
Viresh Kumar5b650b32017-01-23 10:11:48 +05301888 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar737002b2015-07-29 16:22:58 +05301889}
Viresh Kumar129eec52014-11-27 08:54:06 +05301890
1891/**
Sudeep Holla411466c2016-05-03 15:05:04 +01001892 * dev_pm_opp_remove_table() - Free all OPPs associated with the device
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301893 * @dev: device pointer used to lookup OPP table.
Viresh Kumar129eec52014-11-27 08:54:06 +05301894 *
Sudeep Holla411466c2016-05-03 15:05:04 +01001895 * Free both OPPs created using static entries present in DT and the
1896 * dynamically added entries.
Viresh Kumar129eec52014-11-27 08:54:06 +05301897 */
Sudeep Holla411466c2016-05-03 15:05:04 +01001898void dev_pm_opp_remove_table(struct device *dev)
Viresh Kumar129eec52014-11-27 08:54:06 +05301899{
Viresh Kumar9274c892017-01-02 14:41:00 +05301900 _dev_pm_opp_find_and_remove_table(dev, true);
Viresh Kumar8d4d4e92015-06-12 17:10:38 +05301901}
Sudeep Holla411466c2016-05-03 15:05:04 +01001902EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table);