blob: 0ce8069d68436bb53f3b0beec8ef29d3d6cb93c3 [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
284/**
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530285 * dev_pm_opp_get_opp_count() - Get number of opps available in the opp table
Nishanth Menone1f60b22010-10-13 00:13:10 +0200286 * @dev: device for which we do this operation
287 *
Nishanth Menon984f16c2014-12-24 11:22:57 -0600288 * Return: This function returns the number of available opps if there are any,
Nishanth Menone1f60b22010-10-13 00:13:10 +0200289 * else returns 0 if none or the corresponding error value.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200290 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500291int dev_pm_opp_get_opp_count(struct device *dev)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200292{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530293 struct opp_table *opp_table;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500294 struct dev_pm_opp *temp_opp;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200295 int count = 0;
296
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530297 opp_table = _find_opp_table(dev);
298 if (IS_ERR(opp_table)) {
299 count = PTR_ERR(opp_table);
Fabio Estevam035ed072017-09-29 14:39:49 -0300300 dev_dbg(dev, "%s: OPP table not found (%d)\n",
Dmitry Torokhovb4718c02014-12-16 15:09:38 -0800301 __func__, count);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530302 return count;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200303 }
304
Viresh Kumar052c6f12017-01-23 10:11:49 +0530305 mutex_lock(&opp_table->lock);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530306
Viresh Kumar052c6f12017-01-23 10:11:49 +0530307 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
Nishanth Menone1f60b22010-10-13 00:13:10 +0200308 if (temp_opp->available)
309 count++;
310 }
311
Viresh Kumar052c6f12017-01-23 10:11:49 +0530312 mutex_unlock(&opp_table->lock);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530313 dev_pm_opp_put_opp_table(opp_table);
314
Nishanth Menone1f60b22010-10-13 00:13:10 +0200315 return count;
316}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500317EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200318
319/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500320 * dev_pm_opp_find_freq_exact() - search for an exact frequency
Nishanth Menone1f60b22010-10-13 00:13:10 +0200321 * @dev: device for which we do this operation
322 * @freq: frequency to search for
Nishanth Menon7ae49612011-02-25 23:46:18 +0100323 * @available: true/false - match for available opp
Nishanth Menone1f60b22010-10-13 00:13:10 +0200324 *
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530325 * Return: Searches for exact match in the opp table and returns pointer to the
Nishanth Menon984f16c2014-12-24 11:22:57 -0600326 * matching opp if found, else returns ERR_PTR in case of error and should
327 * be handled using IS_ERR. Error return values can be:
Nishanth Menon07797262012-10-24 22:00:12 +0200328 * EINVAL: for bad pointer
329 * ERANGE: no match found for search
330 * ENODEV: if device not found in list of registered devices
Nishanth Menone1f60b22010-10-13 00:13:10 +0200331 *
332 * Note: available is a modifier for the search. if available=true, then the
333 * match is for exact matching frequency and is available in the stored OPP
334 * table. if false, the match is for exact frequency which is not available.
335 *
336 * This provides a mechanism to enable an opp which is not available currently
337 * or the opposite as well.
338 *
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530339 * The callers are required to call dev_pm_opp_put() for the returned OPP after
340 * use.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200341 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500342struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
343 unsigned long freq,
344 bool available)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200345{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530346 struct opp_table *opp_table;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500347 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200348
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530349 opp_table = _find_opp_table(dev);
350 if (IS_ERR(opp_table)) {
351 int r = PTR_ERR(opp_table);
352
353 dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200354 return ERR_PTR(r);
355 }
356
Viresh Kumar052c6f12017-01-23 10:11:49 +0530357 mutex_lock(&opp_table->lock);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530358
Viresh Kumar052c6f12017-01-23 10:11:49 +0530359 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
Nishanth Menone1f60b22010-10-13 00:13:10 +0200360 if (temp_opp->available == available &&
361 temp_opp->rate == freq) {
362 opp = temp_opp;
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530363
364 /* Increment the reference count of OPP */
365 dev_pm_opp_get(opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200366 break;
367 }
368 }
369
Viresh Kumar052c6f12017-01-23 10:11:49 +0530370 mutex_unlock(&opp_table->lock);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530371 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530372
Nishanth Menone1f60b22010-10-13 00:13:10 +0200373 return opp;
374}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500375EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200376
Jisheng Zhang067b7ce2016-07-25 14:11:16 +0800377static noinline struct dev_pm_opp *_find_freq_ceil(struct opp_table *opp_table,
378 unsigned long *freq)
379{
380 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
381
Viresh Kumar052c6f12017-01-23 10:11:49 +0530382 mutex_lock(&opp_table->lock);
383
384 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
Jisheng Zhang067b7ce2016-07-25 14:11:16 +0800385 if (temp_opp->available && temp_opp->rate >= *freq) {
386 opp = temp_opp;
387 *freq = opp->rate;
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530388
389 /* Increment the reference count of OPP */
390 dev_pm_opp_get(opp);
Jisheng Zhang067b7ce2016-07-25 14:11:16 +0800391 break;
392 }
393 }
394
Viresh Kumar052c6f12017-01-23 10:11:49 +0530395 mutex_unlock(&opp_table->lock);
396
Jisheng Zhang067b7ce2016-07-25 14:11:16 +0800397 return opp;
398}
399
Nishanth Menone1f60b22010-10-13 00:13:10 +0200400/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500401 * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
Nishanth Menone1f60b22010-10-13 00:13:10 +0200402 * @dev: device for which we do this operation
403 * @freq: Start frequency
404 *
405 * Search for the matching ceil *available* OPP from a starting freq
406 * for a device.
407 *
Nishanth Menon984f16c2014-12-24 11:22:57 -0600408 * Return: matching *opp and refreshes *freq accordingly, else returns
Nishanth Menon07797262012-10-24 22:00:12 +0200409 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
410 * values can be:
411 * EINVAL: for bad pointer
412 * ERANGE: no match found for search
413 * ENODEV: if device not found in list of registered devices
Nishanth Menone1f60b22010-10-13 00:13:10 +0200414 *
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530415 * The callers are required to call dev_pm_opp_put() for the returned OPP after
416 * use.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200417 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500418struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
419 unsigned long *freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200420{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530421 struct opp_table *opp_table;
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530422 struct dev_pm_opp *opp;
Dmitry Torokhovb02ded22014-12-16 15:09:36 -0800423
Nishanth Menone1f60b22010-10-13 00:13:10 +0200424 if (!dev || !freq) {
425 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
426 return ERR_PTR(-EINVAL);
427 }
428
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530429 opp_table = _find_opp_table(dev);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530430 if (IS_ERR(opp_table))
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530431 return ERR_CAST(opp_table);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530432
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530433 opp = _find_freq_ceil(opp_table, freq);
434
Viresh Kumar5b650b32017-01-23 10:11:48 +0530435 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530436
437 return opp;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200438}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500439EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200440
441/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500442 * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
Nishanth Menone1f60b22010-10-13 00:13:10 +0200443 * @dev: device for which we do this operation
444 * @freq: Start frequency
445 *
446 * Search for the matching floor *available* OPP from a starting freq
447 * for a device.
448 *
Nishanth Menon984f16c2014-12-24 11:22:57 -0600449 * Return: matching *opp and refreshes *freq accordingly, else returns
Nishanth Menon07797262012-10-24 22:00:12 +0200450 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
451 * values can be:
452 * EINVAL: for bad pointer
453 * ERANGE: no match found for search
454 * ENODEV: if device not found in list of registered devices
Nishanth Menone1f60b22010-10-13 00:13:10 +0200455 *
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530456 * The callers are required to call dev_pm_opp_put() for the returned OPP after
457 * use.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200458 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500459struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
460 unsigned long *freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200461{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530462 struct opp_table *opp_table;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500463 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200464
465 if (!dev || !freq) {
466 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
467 return ERR_PTR(-EINVAL);
468 }
469
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530470 opp_table = _find_opp_table(dev);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530471 if (IS_ERR(opp_table))
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530472 return ERR_CAST(opp_table);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530473
Viresh Kumar052c6f12017-01-23 10:11:49 +0530474 mutex_lock(&opp_table->lock);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200475
Viresh Kumar052c6f12017-01-23 10:11:49 +0530476 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
Nishanth Menone1f60b22010-10-13 00:13:10 +0200477 if (temp_opp->available) {
478 /* go to the next node, before choosing prev */
479 if (temp_opp->rate > *freq)
480 break;
481 else
482 opp = temp_opp;
483 }
484 }
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530485
486 /* Increment the reference count of OPP */
487 if (!IS_ERR(opp))
488 dev_pm_opp_get(opp);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530489 mutex_unlock(&opp_table->lock);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530490 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530491
Nishanth Menone1f60b22010-10-13 00:13:10 +0200492 if (!IS_ERR(opp))
493 *freq = opp->rate;
494
495 return opp;
496}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500497EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200498
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530499static int _set_opp_voltage(struct device *dev, struct regulator *reg,
Viresh Kumarce317812016-12-01 16:28:18 +0530500 struct dev_pm_opp_supply *supply)
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530501{
502 int ret;
503
504 /* Regulator not available for device */
505 if (IS_ERR(reg)) {
506 dev_dbg(dev, "%s: regulator not available: %ld\n", __func__,
507 PTR_ERR(reg));
508 return 0;
509 }
510
Viresh Kumarce317812016-12-01 16:28:18 +0530511 dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__,
512 supply->u_volt_min, supply->u_volt, supply->u_volt_max);
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530513
Viresh Kumarce317812016-12-01 16:28:18 +0530514 ret = regulator_set_voltage_triplet(reg, supply->u_volt_min,
515 supply->u_volt, supply->u_volt_max);
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530516 if (ret)
517 dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n",
Viresh Kumarce317812016-12-01 16:28:18 +0530518 __func__, supply->u_volt_min, supply->u_volt,
519 supply->u_volt_max, ret);
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530520
521 return ret;
522}
523
Viresh Kumar94735582016-12-01 16:28:20 +0530524static inline int
525_generic_set_opp_clk_only(struct device *dev, struct clk *clk,
526 unsigned long old_freq, unsigned long freq)
527{
528 int ret;
529
530 ret = clk_set_rate(clk, freq);
531 if (ret) {
532 dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
533 ret);
534 }
535
536 return ret;
537}
538
Viresh Kumar009acd12017-10-11 12:54:14 +0530539static inline int
540_generic_set_opp_domain(struct device *dev, struct clk *clk,
541 unsigned long old_freq, unsigned long freq,
542 unsigned int old_pstate, unsigned int new_pstate)
543{
544 int ret;
545
546 /* Scaling up? Scale domain performance state before frequency */
547 if (freq > old_freq) {
548 ret = dev_pm_genpd_set_performance_state(dev, new_pstate);
549 if (ret)
550 return ret;
551 }
552
553 ret = _generic_set_opp_clk_only(dev, clk, old_freq, freq);
554 if (ret)
555 goto restore_domain_state;
556
557 /* Scaling down? Scale domain performance state after frequency */
558 if (freq < old_freq) {
559 ret = dev_pm_genpd_set_performance_state(dev, new_pstate);
560 if (ret)
561 goto restore_freq;
562 }
563
564 return 0;
565
566restore_freq:
567 if (_generic_set_opp_clk_only(dev, clk, freq, old_freq))
568 dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n",
569 __func__, old_freq);
570restore_domain_state:
571 if (freq > old_freq)
572 dev_pm_genpd_set_performance_state(dev, old_pstate);
573
574 return ret;
575}
576
Viresh Kumarc74b32f2017-05-23 09:32:10 +0530577static int _generic_set_opp_regulator(const struct opp_table *opp_table,
578 struct device *dev,
579 unsigned long old_freq,
580 unsigned long freq,
581 struct dev_pm_opp_supply *old_supply,
582 struct dev_pm_opp_supply *new_supply)
Viresh Kumar94735582016-12-01 16:28:20 +0530583{
Viresh Kumarc74b32f2017-05-23 09:32:10 +0530584 struct regulator *reg = opp_table->regulators[0];
Viresh Kumar94735582016-12-01 16:28:20 +0530585 int ret;
586
587 /* This function only supports single regulator per device */
Viresh Kumarc74b32f2017-05-23 09:32:10 +0530588 if (WARN_ON(opp_table->regulator_count > 1)) {
Viresh Kumar94735582016-12-01 16:28:20 +0530589 dev_err(dev, "multiple regulators are not supported\n");
590 return -EINVAL;
591 }
592
593 /* Scaling up? Scale voltage before frequency */
594 if (freq > old_freq) {
595 ret = _set_opp_voltage(dev, reg, new_supply);
596 if (ret)
597 goto restore_voltage;
598 }
599
600 /* Change frequency */
Viresh Kumarc74b32f2017-05-23 09:32:10 +0530601 ret = _generic_set_opp_clk_only(dev, opp_table->clk, old_freq, freq);
Viresh Kumar94735582016-12-01 16:28:20 +0530602 if (ret)
603 goto restore_voltage;
604
605 /* Scaling down? Scale voltage after frequency */
606 if (freq < old_freq) {
607 ret = _set_opp_voltage(dev, reg, new_supply);
608 if (ret)
609 goto restore_freq;
610 }
611
612 return 0;
613
614restore_freq:
Viresh Kumarc74b32f2017-05-23 09:32:10 +0530615 if (_generic_set_opp_clk_only(dev, opp_table->clk, freq, old_freq))
Viresh Kumar94735582016-12-01 16:28:20 +0530616 dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n",
617 __func__, old_freq);
618restore_voltage:
619 /* This shouldn't harm even if the voltages weren't updated earlier */
Viresh Kumarc74b32f2017-05-23 09:32:10 +0530620 if (old_supply)
Viresh Kumar94735582016-12-01 16:28:20 +0530621 _set_opp_voltage(dev, reg, old_supply);
622
623 return ret;
624}
625
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530626/**
627 * dev_pm_opp_set_rate() - Configure new OPP based on frequency
628 * @dev: device for which we do this operation
629 * @target_freq: frequency to achieve
630 *
631 * This configures the power-supplies and clock source to the levels specified
632 * by the OPP corresponding to the target_freq.
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530633 */
634int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq)
635{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530636 struct opp_table *opp_table;
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530637 unsigned long freq, old_freq;
Viresh Kumar94735582016-12-01 16:28:20 +0530638 struct dev_pm_opp *old_opp, *opp;
Viresh Kumar94735582016-12-01 16:28:20 +0530639 struct clk *clk;
640 int ret, size;
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530641
642 if (unlikely(!target_freq)) {
643 dev_err(dev, "%s: Invalid target frequency %lu\n", __func__,
644 target_freq);
645 return -EINVAL;
646 }
647
Viresh Kumar052c6f12017-01-23 10:11:49 +0530648 opp_table = _find_opp_table(dev);
649 if (IS_ERR(opp_table)) {
650 dev_err(dev, "%s: device opp doesn't exist\n", __func__);
651 return PTR_ERR(opp_table);
652 }
653
654 clk = opp_table->clk;
655 if (IS_ERR(clk)) {
656 dev_err(dev, "%s: No clock available for the device\n",
657 __func__);
658 ret = PTR_ERR(clk);
659 goto put_opp_table;
660 }
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530661
662 freq = clk_round_rate(clk, target_freq);
663 if ((long)freq <= 0)
664 freq = target_freq;
665
666 old_freq = clk_get_rate(clk);
667
668 /* Return early if nothing to do */
669 if (old_freq == freq) {
670 dev_dbg(dev, "%s: old/new frequencies (%lu Hz) are same, nothing to do\n",
671 __func__, freq);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530672 ret = 0;
673 goto put_opp_table;
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530674 }
675
Jisheng Zhang067b7ce2016-07-25 14:11:16 +0800676 old_opp = _find_freq_ceil(opp_table, &old_freq);
Arnd Bergmann4df27c92016-09-15 17:38:38 +0200677 if (IS_ERR(old_opp)) {
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530678 dev_err(dev, "%s: failed to find current OPP for freq %lu (%ld)\n",
679 __func__, old_freq, PTR_ERR(old_opp));
680 }
681
Jisheng Zhang067b7ce2016-07-25 14:11:16 +0800682 opp = _find_freq_ceil(opp_table, &freq);
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530683 if (IS_ERR(opp)) {
684 ret = PTR_ERR(opp);
685 dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n",
686 __func__, freq, ret);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530687 goto put_old_opp;
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530688 }
689
Viresh Kumar94735582016-12-01 16:28:20 +0530690 dev_dbg(dev, "%s: switching OPP: %lu Hz --> %lu Hz\n", __func__,
691 old_freq, freq);
Viresh Kumardfbe4672016-12-01 16:28:19 +0530692
Viresh Kumar94735582016-12-01 16:28:20 +0530693 /* Only frequency scaling */
Viresh Kumarc74b32f2017-05-23 09:32:10 +0530694 if (!opp_table->regulators) {
Viresh Kumar009acd12017-10-11 12:54:14 +0530695 /*
696 * We don't support devices with both regulator and
697 * domain performance-state for now.
698 */
699 if (opp_table->genpd_performance_state)
700 ret = _generic_set_opp_domain(dev, clk, old_freq, freq,
701 IS_ERR(old_opp) ? 0 : old_opp->pstate,
702 opp->pstate);
703 else
704 ret = _generic_set_opp_clk_only(dev, clk, old_freq, freq);
Viresh Kumarc74b32f2017-05-23 09:32:10 +0530705 } else if (!opp_table->set_opp) {
706 ret = _generic_set_opp_regulator(opp_table, dev, old_freq, freq,
707 IS_ERR(old_opp) ? NULL : old_opp->supplies,
708 opp->supplies);
709 } else {
710 struct dev_pm_set_opp_data *data;
711
712 data = opp_table->set_opp_data;
713 data->regulators = opp_table->regulators;
714 data->regulator_count = opp_table->regulator_count;
715 data->clk = clk;
716 data->dev = dev;
717
718 data->old_opp.rate = old_freq;
719 size = sizeof(*opp->supplies) * opp_table->regulator_count;
720 if (IS_ERR(old_opp))
721 memset(data->old_opp.supplies, 0, size);
722 else
723 memcpy(data->old_opp.supplies, old_opp->supplies, size);
724
725 data->new_opp.rate = freq;
726 memcpy(data->new_opp.supplies, opp->supplies, size);
727
728 ret = opp_table->set_opp(data);
Viresh Kumardfbe4672016-12-01 16:28:19 +0530729 }
730
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530731 dev_pm_opp_put(opp);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530732put_old_opp:
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530733 if (!IS_ERR(old_opp))
734 dev_pm_opp_put(old_opp);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530735put_opp_table:
Viresh Kumar5b650b32017-01-23 10:11:48 +0530736 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530737 return ret;
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530738}
739EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate);
740
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530741/* OPP-dev Helpers */
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530742static void _remove_opp_dev(struct opp_device *opp_dev,
743 struct opp_table *opp_table)
Viresh Kumar06441652015-07-29 16:23:04 +0530744{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530745 opp_debug_unregister(opp_dev, opp_table);
746 list_del(&opp_dev->node);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530747 kfree(opp_dev);
Viresh Kumar06441652015-07-29 16:23:04 +0530748}
749
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530750struct opp_device *_add_opp_dev(const struct device *dev,
751 struct opp_table *opp_table)
Viresh Kumar06441652015-07-29 16:23:04 +0530752{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530753 struct opp_device *opp_dev;
Viresh Kumardeaa5142015-11-11 07:59:01 +0530754 int ret;
Viresh Kumar06441652015-07-29 16:23:04 +0530755
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530756 opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL);
757 if (!opp_dev)
Viresh Kumar06441652015-07-29 16:23:04 +0530758 return NULL;
759
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530760 /* Initialize opp-dev */
761 opp_dev->dev = dev;
Viresh Kumar052c6f12017-01-23 10:11:49 +0530762 list_add(&opp_dev->node, &opp_table->dev_list);
Viresh Kumar06441652015-07-29 16:23:04 +0530763
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530764 /* Create debugfs entries for the opp_table */
765 ret = opp_debug_register(opp_dev, opp_table);
Viresh Kumardeaa5142015-11-11 07:59:01 +0530766 if (ret)
767 dev_err(dev, "%s: Failed to register opp debugfs (%d)\n",
768 __func__, ret);
769
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530770 return opp_dev;
Viresh Kumar06441652015-07-29 16:23:04 +0530771}
772
Viresh Kumarb6160e22017-01-02 14:41:04 +0530773static struct opp_table *_allocate_opp_table(struct device *dev)
Viresh Kumar07cce742014-12-10 09:45:34 +0530774{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530775 struct opp_table *opp_table;
776 struct opp_device *opp_dev;
Viresh Kumard54974c2016-02-09 10:30:38 +0530777 int ret;
Viresh Kumar07cce742014-12-10 09:45:34 +0530778
779 /*
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530780 * Allocate a new OPP table. In the infrequent case where a new
Viresh Kumar07cce742014-12-10 09:45:34 +0530781 * device is needed to be added, we pay this penalty.
782 */
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530783 opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL);
784 if (!opp_table)
Viresh Kumar07cce742014-12-10 09:45:34 +0530785 return NULL;
786
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530787 INIT_LIST_HEAD(&opp_table->dev_list);
Viresh Kumar06441652015-07-29 16:23:04 +0530788
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530789 opp_dev = _add_opp_dev(dev, opp_table);
790 if (!opp_dev) {
791 kfree(opp_table);
Viresh Kumar06441652015-07-29 16:23:04 +0530792 return NULL;
793 }
794
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530795 _of_init_opp_table(opp_table, dev);
Viresh Kumar50f8cfb2016-02-09 10:30:37 +0530796
Viresh Kumard54974c2016-02-09 10:30:38 +0530797 /* Find clk for the device */
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530798 opp_table->clk = clk_get(dev, NULL);
799 if (IS_ERR(opp_table->clk)) {
800 ret = PTR_ERR(opp_table->clk);
Viresh Kumard54974c2016-02-09 10:30:38 +0530801 if (ret != -EPROBE_DEFER)
802 dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__,
803 ret);
804 }
805
Viresh Kumar052c6f12017-01-23 10:11:49 +0530806 BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head);
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530807 INIT_LIST_HEAD(&opp_table->opp_list);
Viresh Kumar37a73ec2017-01-23 10:11:41 +0530808 mutex_init(&opp_table->lock);
Viresh Kumarf067a982017-01-23 10:11:42 +0530809 kref_init(&opp_table->kref);
Viresh Kumar07cce742014-12-10 09:45:34 +0530810
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530811 /* Secure the device table modification */
Viresh Kumar052c6f12017-01-23 10:11:49 +0530812 list_add(&opp_table->node, &opp_tables);
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530813 return opp_table;
Viresh Kumar07cce742014-12-10 09:45:34 +0530814}
815
Viresh Kumarf067a982017-01-23 10:11:42 +0530816void _get_opp_table_kref(struct opp_table *opp_table)
Viresh Kumar3bac42c2015-07-29 16:22:59 +0530817{
Viresh Kumarf067a982017-01-23 10:11:42 +0530818 kref_get(&opp_table->kref);
819}
820
821struct opp_table *dev_pm_opp_get_opp_table(struct device *dev)
822{
823 struct opp_table *opp_table;
824
825 /* Hold our table modification lock here */
826 mutex_lock(&opp_table_lock);
827
Viresh Kumar5b650b32017-01-23 10:11:48 +0530828 opp_table = _find_opp_table_unlocked(dev);
829 if (!IS_ERR(opp_table))
Viresh Kumarf067a982017-01-23 10:11:42 +0530830 goto unlock;
Viresh Kumarf067a982017-01-23 10:11:42 +0530831
832 opp_table = _allocate_opp_table(dev);
833
834unlock:
835 mutex_unlock(&opp_table_lock);
836
837 return opp_table;
838}
839EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table);
840
Viresh Kumarb83c1892017-01-23 10:11:45 +0530841static void _opp_table_kref_release(struct kref *kref)
Viresh Kumarf067a982017-01-23 10:11:42 +0530842{
843 struct opp_table *opp_table = container_of(kref, struct opp_table, kref);
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530844 struct opp_device *opp_dev;
Viresh Kumar06441652015-07-29 16:23:04 +0530845
Viresh Kumard54974c2016-02-09 10:30:38 +0530846 /* Release clk */
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530847 if (!IS_ERR(opp_table->clk))
848 clk_put(opp_table->clk);
Viresh Kumard54974c2016-02-09 10:30:38 +0530849
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530850 opp_dev = list_first_entry(&opp_table->dev_list, struct opp_device,
851 node);
Viresh Kumar06441652015-07-29 16:23:04 +0530852
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530853 _remove_opp_dev(opp_dev, opp_table);
Viresh Kumar06441652015-07-29 16:23:04 +0530854
855 /* dev_list must be empty now */
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530856 WARN_ON(!list_empty(&opp_table->dev_list));
Viresh Kumar06441652015-07-29 16:23:04 +0530857
Viresh Kumar37a73ec2017-01-23 10:11:41 +0530858 mutex_destroy(&opp_table->lock);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530859 list_del(&opp_table->node);
860 kfree(opp_table);
Viresh Kumar3bac42c2015-07-29 16:22:59 +0530861
Viresh Kumarf067a982017-01-23 10:11:42 +0530862 mutex_unlock(&opp_table_lock);
863}
864
865void dev_pm_opp_put_opp_table(struct opp_table *opp_table)
866{
867 kref_put_mutex(&opp_table->kref, _opp_table_kref_release,
868 &opp_table_lock);
869}
870EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table);
871
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +0530872void _opp_free(struct dev_pm_opp *opp)
Viresh Kumar969fceb2017-01-02 14:40:59 +0530873{
874 kfree(opp);
Viresh Kumar969fceb2017-01-02 14:40:59 +0530875}
876
Viresh Kumar70347642017-01-23 10:11:46 +0530877static void _opp_kref_release(struct kref *kref)
Viresh Kumar129eec52014-11-27 08:54:06 +0530878{
Viresh Kumar70347642017-01-23 10:11:46 +0530879 struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref);
880 struct opp_table *opp_table = opp->opp_table;
Viresh Kumar37a73ec2017-01-23 10:11:41 +0530881
Viresh Kumar129eec52014-11-27 08:54:06 +0530882 /*
883 * Notify the changes in the availability of the operable
884 * frequency/voltage list.
885 */
Viresh Kumar052c6f12017-01-23 10:11:49 +0530886 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_REMOVE, opp);
Viresh Kumardeaa5142015-11-11 07:59:01 +0530887 opp_debug_remove_one(opp);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530888 list_del(&opp->node);
889 kfree(opp);
Viresh Kumar129eec52014-11-27 08:54:06 +0530890
Viresh Kumar37a73ec2017-01-23 10:11:41 +0530891 mutex_unlock(&opp_table->lock);
Viresh Kumar31641cd2017-01-23 10:11:44 +0530892 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar129eec52014-11-27 08:54:06 +0530893}
894
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530895static void dev_pm_opp_get(struct dev_pm_opp *opp)
896{
897 kref_get(&opp->kref);
898}
899
Viresh Kumar70347642017-01-23 10:11:46 +0530900void dev_pm_opp_put(struct dev_pm_opp *opp)
901{
902 kref_put_mutex(&opp->kref, _opp_kref_release, &opp->opp_table->lock);
903}
904EXPORT_SYMBOL_GPL(dev_pm_opp_put);
905
Viresh Kumar129eec52014-11-27 08:54:06 +0530906/**
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530907 * dev_pm_opp_remove() - Remove an OPP from OPP table
Viresh Kumar129eec52014-11-27 08:54:06 +0530908 * @dev: device for which we do this operation
909 * @freq: OPP to remove with matching 'freq'
910 *
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530911 * This function removes an opp from the opp table.
Viresh Kumar129eec52014-11-27 08:54:06 +0530912 */
913void dev_pm_opp_remove(struct device *dev, unsigned long freq)
914{
915 struct dev_pm_opp *opp;
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530916 struct opp_table *opp_table;
Viresh Kumar129eec52014-11-27 08:54:06 +0530917 bool found = false;
918
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530919 opp_table = _find_opp_table(dev);
920 if (IS_ERR(opp_table))
Viresh Kumar5b650b32017-01-23 10:11:48 +0530921 return;
Viresh Kumar129eec52014-11-27 08:54:06 +0530922
Viresh Kumar37a73ec2017-01-23 10:11:41 +0530923 mutex_lock(&opp_table->lock);
924
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530925 list_for_each_entry(opp, &opp_table->opp_list, node) {
Viresh Kumar129eec52014-11-27 08:54:06 +0530926 if (opp->rate == freq) {
927 found = true;
928 break;
929 }
930 }
931
Viresh Kumar37a73ec2017-01-23 10:11:41 +0530932 mutex_unlock(&opp_table->lock);
933
Viresh Kumar5b650b32017-01-23 10:11:48 +0530934 if (found) {
935 dev_pm_opp_put(opp);
936 } else {
Viresh Kumar129eec52014-11-27 08:54:06 +0530937 dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
938 __func__, freq);
Viresh Kumar129eec52014-11-27 08:54:06 +0530939 }
940
Viresh Kumar5b650b32017-01-23 10:11:48 +0530941 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar129eec52014-11-27 08:54:06 +0530942}
943EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
944
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +0530945struct dev_pm_opp *_opp_allocate(struct opp_table *table)
Viresh Kumar23dacf62015-07-29 16:23:01 +0530946{
947 struct dev_pm_opp *opp;
Viresh Kumardfbe4672016-12-01 16:28:19 +0530948 int count, supply_size;
Viresh Kumar23dacf62015-07-29 16:23:01 +0530949
Viresh Kumardfbe4672016-12-01 16:28:19 +0530950 /* Allocate space for at least one supply */
951 count = table->regulator_count ? table->regulator_count : 1;
952 supply_size = sizeof(*opp->supplies) * count;
Viresh Kumar23dacf62015-07-29 16:23:01 +0530953
Viresh Kumardfbe4672016-12-01 16:28:19 +0530954 /* allocate new OPP node and supplies structures */
955 opp = kzalloc(sizeof(*opp) + supply_size, GFP_KERNEL);
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +0530956 if (!opp)
Viresh Kumar23dacf62015-07-29 16:23:01 +0530957 return NULL;
Viresh Kumar23dacf62015-07-29 16:23:01 +0530958
Viresh Kumardfbe4672016-12-01 16:28:19 +0530959 /* Put the supplies at the end of the OPP structure as an empty array */
960 opp->supplies = (struct dev_pm_opp_supply *)(opp + 1);
961 INIT_LIST_HEAD(&opp->node);
962
Viresh Kumar23dacf62015-07-29 16:23:01 +0530963 return opp;
964}
965
Viresh Kumar7d34d562016-02-09 10:30:34 +0530966static bool _opp_supported_by_regulators(struct dev_pm_opp *opp,
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530967 struct opp_table *opp_table)
Viresh Kumar7d34d562016-02-09 10:30:34 +0530968{
Viresh Kumardfbe4672016-12-01 16:28:19 +0530969 struct regulator *reg;
970 int i;
Viresh Kumar7d34d562016-02-09 10:30:34 +0530971
Viresh Kumardfbe4672016-12-01 16:28:19 +0530972 for (i = 0; i < opp_table->regulator_count; i++) {
973 reg = opp_table->regulators[i];
974
975 if (!regulator_is_supported_voltage(reg,
976 opp->supplies[i].u_volt_min,
977 opp->supplies[i].u_volt_max)) {
978 pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n",
979 __func__, opp->supplies[i].u_volt_min,
980 opp->supplies[i].u_volt_max);
981 return false;
982 }
Viresh Kumar7d34d562016-02-09 10:30:34 +0530983 }
984
985 return true;
986}
987
Viresh Kumar7f8538e2017-01-02 14:40:55 +0530988/*
989 * Returns:
990 * 0: On success. And appropriate error message for duplicate OPPs.
991 * -EBUSY: For OPP with same freq/volt and is available. The callers of
992 * _opp_add() must return 0 if they receive -EBUSY from it. This is to make
993 * sure we don't print error messages unnecessarily if different parts of
994 * kernel try to initialize the OPP table.
995 * -EEXIST: For OPP with same freq but different volt or is unavailable. This
996 * should be considered an error by the callers of _opp_add().
997 */
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530998int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
999 struct opp_table *opp_table)
Viresh Kumar23dacf62015-07-29 16:23:01 +05301000{
1001 struct dev_pm_opp *opp;
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301002 struct list_head *head;
Viresh Kumardeaa5142015-11-11 07:59:01 +05301003 int ret;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301004
1005 /*
1006 * Insert new OPP in order of increasing frequency and discard if
1007 * already present.
1008 *
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301009 * Need to use &opp_table->opp_list in the condition part of the 'for'
Viresh Kumar23dacf62015-07-29 16:23:01 +05301010 * loop, don't replace it with head otherwise it will become an infinite
1011 * loop.
1012 */
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301013 mutex_lock(&opp_table->lock);
1014 head = &opp_table->opp_list;
1015
Viresh Kumar052c6f12017-01-23 10:11:49 +05301016 list_for_each_entry(opp, &opp_table->opp_list, node) {
Viresh Kumar23dacf62015-07-29 16:23:01 +05301017 if (new_opp->rate > opp->rate) {
1018 head = &opp->node;
1019 continue;
1020 }
1021
1022 if (new_opp->rate < opp->rate)
1023 break;
1024
1025 /* Duplicate OPPs */
Viresh Kumar06441652015-07-29 16:23:04 +05301026 dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
Viresh Kumardfbe4672016-12-01 16:28:19 +05301027 __func__, opp->rate, opp->supplies[0].u_volt,
1028 opp->available, new_opp->rate,
1029 new_opp->supplies[0].u_volt, new_opp->available);
Viresh Kumar23dacf62015-07-29 16:23:01 +05301030
Viresh Kumardfbe4672016-12-01 16:28:19 +05301031 /* Should we compare voltages for all regulators here ? */
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301032 ret = opp->available &&
1033 new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST;
1034
1035 mutex_unlock(&opp_table->lock);
1036 return ret;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301037 }
1038
Viresh Kumar052c6f12017-01-23 10:11:49 +05301039 list_add(&new_opp->node, head);
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301040 mutex_unlock(&opp_table->lock);
1041
1042 new_opp->opp_table = opp_table;
Viresh Kumar70347642017-01-23 10:11:46 +05301043 kref_init(&new_opp->kref);
Viresh Kumar23dacf62015-07-29 16:23:01 +05301044
Viresh Kumar31641cd2017-01-23 10:11:44 +05301045 /* Get a reference to the OPP table */
1046 _get_opp_table_kref(opp_table);
1047
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301048 ret = opp_debug_create_one(new_opp, opp_table);
Viresh Kumardeaa5142015-11-11 07:59:01 +05301049 if (ret)
1050 dev_err(dev, "%s: Failed to register opp to debugfs (%d)\n",
1051 __func__, ret);
1052
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301053 if (!_opp_supported_by_regulators(new_opp, opp_table)) {
Viresh Kumar7d34d562016-02-09 10:30:34 +05301054 new_opp->available = false;
1055 dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n",
1056 __func__, new_opp->rate);
1057 }
1058
Viresh Kumar23dacf62015-07-29 16:23:01 +05301059 return 0;
1060}
1061
Viresh Kumar737002b2015-07-29 16:22:58 +05301062/**
Viresh Kumarb64b9c3f2015-10-15 21:42:44 +05301063 * _opp_add_v1() - Allocate a OPP based on v1 bindings.
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301064 * @opp_table: OPP table
Nishanth Menone1f60b22010-10-13 00:13:10 +02001065 * @dev: device for which we do this operation
1066 * @freq: Frequency in Hz for this OPP
1067 * @u_volt: Voltage in uVolts for this OPP
1068 * @dynamic: Dynamically added OPPs.
1069 *
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301070 * This function adds an opp definition to the opp table and returns status.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001071 * The opp is made available by default and it can be controlled using
1072 * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
1073 *
Viresh Kumar8f8d37b2015-09-04 13:47:24 +05301074 * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table
1075 * and freed by dev_pm_opp_of_remove_table.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001076 *
Nishanth Menone1f60b22010-10-13 00:13:10 +02001077 * Return:
1078 * 0 On success OR
1079 * Duplicate OPPs (both freq and volt are same) and opp->available
1080 * -EEXIST Freq are same and volt are different OR
1081 * Duplicate OPPs (both freq and volt are same) and !opp->available
1082 * -ENOMEM Memory allocation failure
1083 */
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301084int _opp_add_v1(struct opp_table *opp_table, struct device *dev,
1085 unsigned long freq, long u_volt, bool dynamic)
Nishanth Menone1f60b22010-10-13 00:13:10 +02001086{
Viresh Kumar23dacf62015-07-29 16:23:01 +05301087 struct dev_pm_opp *new_opp;
Viresh Kumar50f8cfb2016-02-09 10:30:37 +05301088 unsigned long tol;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001089 int ret;
1090
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301091 new_opp = _opp_allocate(opp_table);
1092 if (!new_opp)
1093 return -ENOMEM;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301094
Nishanth Menone1f60b22010-10-13 00:13:10 +02001095 /* populate the opp table */
1096 new_opp->rate = freq;
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301097 tol = u_volt * opp_table->voltage_tolerance_v1 / 100;
Viresh Kumardfbe4672016-12-01 16:28:19 +05301098 new_opp->supplies[0].u_volt = u_volt;
1099 new_opp->supplies[0].u_volt_min = u_volt - tol;
1100 new_opp->supplies[0].u_volt_max = u_volt + tol;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001101 new_opp->available = true;
Viresh Kumaraa5f2f82015-07-29 16:23:00 +05301102 new_opp->dynamic = dynamic;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301103
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301104 ret = _opp_add(dev, new_opp, opp_table);
Viresh Kumar7f8538e2017-01-02 14:40:55 +05301105 if (ret) {
1106 /* Don't return error for duplicate OPPs */
1107 if (ret == -EBUSY)
1108 ret = 0;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301109 goto free_opp;
Viresh Kumar7f8538e2017-01-02 14:40:55 +05301110 }
Viresh Kumar23dacf62015-07-29 16:23:01 +05301111
Nishanth Menone1f60b22010-10-13 00:13:10 +02001112 /*
1113 * Notify the changes in the availability of the operable
1114 * frequency/voltage list.
1115 */
Viresh Kumar052c6f12017-01-23 10:11:49 +05301116 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001117 return 0;
1118
1119free_opp:
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301120 _opp_free(new_opp);
1121
Nishanth Menone1f60b22010-10-13 00:13:10 +02001122 return ret;
1123}
Viresh Kumar383934092014-11-25 16:04:18 +05301124
Viresh Kumar27465902015-07-29 16:23:02 +05301125/**
Viresh Kumar7de36b02015-12-09 08:01:46 +05301126 * dev_pm_opp_set_supported_hw() - Set supported platforms
1127 * @dev: Device for which supported-hw has to be set.
1128 * @versions: Array of hierarchy of versions to match.
1129 * @count: Number of elements in the array.
1130 *
1131 * This is required only for the V2 bindings, and it enables a platform to
1132 * specify the hierarchy of versions it supports. OPP layer will then enable
1133 * OPPs, which are available for those versions, based on its 'opp-supported-hw'
1134 * property.
Viresh Kumar7de36b02015-12-09 08:01:46 +05301135 */
Viresh Kumarfa301842017-01-23 10:11:43 +05301136struct opp_table *dev_pm_opp_set_supported_hw(struct device *dev,
1137 const u32 *versions, unsigned int count)
Viresh Kumar7de36b02015-12-09 08:01:46 +05301138{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301139 struct opp_table *opp_table;
Viresh Kumarfa301842017-01-23 10:11:43 +05301140 int ret;
Viresh Kumar7de36b02015-12-09 08:01:46 +05301141
Viresh Kumarfa301842017-01-23 10:11:43 +05301142 opp_table = dev_pm_opp_get_opp_table(dev);
1143 if (!opp_table)
1144 return ERR_PTR(-ENOMEM);
Viresh Kumar7de36b02015-12-09 08:01:46 +05301145
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301146 /* Make sure there are no concurrent readers while updating opp_table */
1147 WARN_ON(!list_empty(&opp_table->opp_list));
Viresh Kumar7de36b02015-12-09 08:01:46 +05301148
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301149 /* Do we already have a version hierarchy associated with opp_table? */
1150 if (opp_table->supported_hw) {
Viresh Kumar7de36b02015-12-09 08:01:46 +05301151 dev_err(dev, "%s: Already have supported hardware list\n",
1152 __func__);
1153 ret = -EBUSY;
1154 goto err;
1155 }
1156
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301157 opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions),
Viresh Kumar7de36b02015-12-09 08:01:46 +05301158 GFP_KERNEL);
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301159 if (!opp_table->supported_hw) {
Viresh Kumar7de36b02015-12-09 08:01:46 +05301160 ret = -ENOMEM;
1161 goto err;
1162 }
1163
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301164 opp_table->supported_hw_count = count;
Viresh Kumarfa301842017-01-23 10:11:43 +05301165
1166 return opp_table;
Viresh Kumar7de36b02015-12-09 08:01:46 +05301167
1168err:
Viresh Kumarfa301842017-01-23 10:11:43 +05301169 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar7de36b02015-12-09 08:01:46 +05301170
Viresh Kumarfa301842017-01-23 10:11:43 +05301171 return ERR_PTR(ret);
Viresh Kumar7de36b02015-12-09 08:01:46 +05301172}
1173EXPORT_SYMBOL_GPL(dev_pm_opp_set_supported_hw);
1174
1175/**
1176 * dev_pm_opp_put_supported_hw() - Releases resources blocked for supported hw
Viresh Kumarfa301842017-01-23 10:11:43 +05301177 * @opp_table: OPP table returned by dev_pm_opp_set_supported_hw().
Viresh Kumar7de36b02015-12-09 08:01:46 +05301178 *
1179 * This is required only for the V2 bindings, and is called for a matching
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301180 * dev_pm_opp_set_supported_hw(). Until this is called, the opp_table structure
Viresh Kumar7de36b02015-12-09 08:01:46 +05301181 * will not be freed.
Viresh Kumar7de36b02015-12-09 08:01:46 +05301182 */
Viresh Kumarfa301842017-01-23 10:11:43 +05301183void dev_pm_opp_put_supported_hw(struct opp_table *opp_table)
Viresh Kumar7de36b02015-12-09 08:01:46 +05301184{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301185 /* Make sure there are no concurrent readers while updating opp_table */
1186 WARN_ON(!list_empty(&opp_table->opp_list));
Viresh Kumar7de36b02015-12-09 08:01:46 +05301187
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301188 if (!opp_table->supported_hw) {
Viresh Kumarfa301842017-01-23 10:11:43 +05301189 pr_err("%s: Doesn't have supported hardware list\n",
1190 __func__);
1191 return;
Viresh Kumar7de36b02015-12-09 08:01:46 +05301192 }
1193
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301194 kfree(opp_table->supported_hw);
1195 opp_table->supported_hw = NULL;
1196 opp_table->supported_hw_count = 0;
Viresh Kumar7de36b02015-12-09 08:01:46 +05301197
Viresh Kumarfa301842017-01-23 10:11:43 +05301198 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar7de36b02015-12-09 08:01:46 +05301199}
1200EXPORT_SYMBOL_GPL(dev_pm_opp_put_supported_hw);
1201
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301202/**
1203 * dev_pm_opp_set_prop_name() - Set prop-extn name
Viresh Kumara5da6442016-02-16 14:17:52 +05301204 * @dev: Device for which the prop-name has to be set.
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301205 * @name: name to postfix to properties.
1206 *
1207 * This is required only for the V2 bindings, and it enables a platform to
1208 * specify the extn to be used for certain property names. The properties to
1209 * which the extension will apply are opp-microvolt and opp-microamp. OPP core
1210 * should postfix the property name with -<name> while looking for them.
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301211 */
Viresh Kumarfa301842017-01-23 10:11:43 +05301212struct opp_table *dev_pm_opp_set_prop_name(struct device *dev, const char *name)
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301213{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301214 struct opp_table *opp_table;
Viresh Kumarfa301842017-01-23 10:11:43 +05301215 int ret;
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301216
Viresh Kumarfa301842017-01-23 10:11:43 +05301217 opp_table = dev_pm_opp_get_opp_table(dev);
1218 if (!opp_table)
1219 return ERR_PTR(-ENOMEM);
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301220
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301221 /* Make sure there are no concurrent readers while updating opp_table */
1222 WARN_ON(!list_empty(&opp_table->opp_list));
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301223
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301224 /* Do we already have a prop-name associated with opp_table? */
1225 if (opp_table->prop_name) {
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301226 dev_err(dev, "%s: Already have prop-name %s\n", __func__,
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301227 opp_table->prop_name);
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301228 ret = -EBUSY;
1229 goto err;
1230 }
1231
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301232 opp_table->prop_name = kstrdup(name, GFP_KERNEL);
1233 if (!opp_table->prop_name) {
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301234 ret = -ENOMEM;
1235 goto err;
1236 }
1237
Viresh Kumarfa301842017-01-23 10:11:43 +05301238 return opp_table;
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301239
1240err:
Viresh Kumarfa301842017-01-23 10:11:43 +05301241 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301242
Viresh Kumarfa301842017-01-23 10:11:43 +05301243 return ERR_PTR(ret);
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301244}
1245EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name);
1246
1247/**
1248 * dev_pm_opp_put_prop_name() - Releases resources blocked for prop-name
Viresh Kumarfa301842017-01-23 10:11:43 +05301249 * @opp_table: OPP table returned by dev_pm_opp_set_prop_name().
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301250 *
1251 * This is required only for the V2 bindings, and is called for a matching
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301252 * dev_pm_opp_set_prop_name(). Until this is called, the opp_table structure
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301253 * will not be freed.
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301254 */
Viresh Kumarfa301842017-01-23 10:11:43 +05301255void dev_pm_opp_put_prop_name(struct opp_table *opp_table)
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301256{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301257 /* Make sure there are no concurrent readers while updating opp_table */
1258 WARN_ON(!list_empty(&opp_table->opp_list));
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301259
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301260 if (!opp_table->prop_name) {
Viresh Kumarfa301842017-01-23 10:11:43 +05301261 pr_err("%s: Doesn't have a prop-name\n", __func__);
1262 return;
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301263 }
1264
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301265 kfree(opp_table->prop_name);
1266 opp_table->prop_name = NULL;
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301267
Viresh Kumarfa301842017-01-23 10:11:43 +05301268 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301269}
1270EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name);
1271
Viresh Kumar94735582016-12-01 16:28:20 +05301272static int _allocate_set_opp_data(struct opp_table *opp_table)
1273{
1274 struct dev_pm_set_opp_data *data;
1275 int len, count = opp_table->regulator_count;
1276
1277 if (WARN_ON(!count))
1278 return -EINVAL;
1279
1280 /* space for set_opp_data */
1281 len = sizeof(*data);
1282
1283 /* space for old_opp.supplies and new_opp.supplies */
1284 len += 2 * sizeof(struct dev_pm_opp_supply) * count;
1285
1286 data = kzalloc(len, GFP_KERNEL);
1287 if (!data)
1288 return -ENOMEM;
1289
1290 data->old_opp.supplies = (void *)(data + 1);
1291 data->new_opp.supplies = data->old_opp.supplies + count;
1292
1293 opp_table->set_opp_data = data;
1294
1295 return 0;
1296}
1297
1298static void _free_set_opp_data(struct opp_table *opp_table)
1299{
1300 kfree(opp_table->set_opp_data);
1301 opp_table->set_opp_data = NULL;
1302}
1303
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301304/**
Viresh Kumardfbe4672016-12-01 16:28:19 +05301305 * dev_pm_opp_set_regulators() - Set regulator names for the device
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301306 * @dev: Device for which regulator name is being set.
Viresh Kumardfbe4672016-12-01 16:28:19 +05301307 * @names: Array of pointers to the names of the regulator.
1308 * @count: Number of regulators.
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301309 *
1310 * In order to support OPP switching, OPP layer needs to know the name of the
Viresh Kumardfbe4672016-12-01 16:28:19 +05301311 * device's regulators, as the core would be required to switch voltages as
1312 * well.
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301313 *
1314 * This must be called before any OPPs are initialized for the device.
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301315 */
Viresh Kumardfbe4672016-12-01 16:28:19 +05301316struct opp_table *dev_pm_opp_set_regulators(struct device *dev,
1317 const char * const names[],
1318 unsigned int count)
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301319{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301320 struct opp_table *opp_table;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301321 struct regulator *reg;
Viresh Kumardfbe4672016-12-01 16:28:19 +05301322 int ret, i;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301323
Viresh Kumarfa301842017-01-23 10:11:43 +05301324 opp_table = dev_pm_opp_get_opp_table(dev);
1325 if (!opp_table)
1326 return ERR_PTR(-ENOMEM);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301327
1328 /* This should be called before OPPs are initialized */
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301329 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301330 ret = -EBUSY;
1331 goto err;
1332 }
1333
Viresh Kumardfbe4672016-12-01 16:28:19 +05301334 /* Already have regulators set */
Viresh Kumare231f8d2016-12-01 16:28:22 +05301335 if (opp_table->regulators) {
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301336 ret = -EBUSY;
1337 goto err;
1338 }
Viresh Kumardfbe4672016-12-01 16:28:19 +05301339
1340 opp_table->regulators = kmalloc_array(count,
1341 sizeof(*opp_table->regulators),
1342 GFP_KERNEL);
1343 if (!opp_table->regulators) {
1344 ret = -ENOMEM;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301345 goto err;
1346 }
1347
Viresh Kumardfbe4672016-12-01 16:28:19 +05301348 for (i = 0; i < count; i++) {
1349 reg = regulator_get_optional(dev, names[i]);
1350 if (IS_ERR(reg)) {
1351 ret = PTR_ERR(reg);
1352 if (ret != -EPROBE_DEFER)
1353 dev_err(dev, "%s: no regulator (%s) found: %d\n",
1354 __func__, names[i], ret);
1355 goto free_regulators;
1356 }
1357
1358 opp_table->regulators[i] = reg;
1359 }
1360
1361 opp_table->regulator_count = count;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301362
Viresh Kumar94735582016-12-01 16:28:20 +05301363 /* Allocate block only once to pass to set_opp() routines */
1364 ret = _allocate_set_opp_data(opp_table);
1365 if (ret)
1366 goto free_regulators;
1367
Stephen Boyd91291d92016-11-30 16:21:25 +05301368 return opp_table;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301369
Viresh Kumardfbe4672016-12-01 16:28:19 +05301370free_regulators:
1371 while (i != 0)
1372 regulator_put(opp_table->regulators[--i]);
1373
1374 kfree(opp_table->regulators);
1375 opp_table->regulators = NULL;
Viresh Kumar94735582016-12-01 16:28:20 +05301376 opp_table->regulator_count = 0;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301377err:
Viresh Kumarfa301842017-01-23 10:11:43 +05301378 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301379
Stephen Boyd91291d92016-11-30 16:21:25 +05301380 return ERR_PTR(ret);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301381}
Viresh Kumardfbe4672016-12-01 16:28:19 +05301382EXPORT_SYMBOL_GPL(dev_pm_opp_set_regulators);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301383
1384/**
Viresh Kumardfbe4672016-12-01 16:28:19 +05301385 * dev_pm_opp_put_regulators() - Releases resources blocked for regulator
1386 * @opp_table: OPP table returned from dev_pm_opp_set_regulators().
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301387 */
Viresh Kumardfbe4672016-12-01 16:28:19 +05301388void dev_pm_opp_put_regulators(struct opp_table *opp_table)
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301389{
Viresh Kumardfbe4672016-12-01 16:28:19 +05301390 int i;
1391
Viresh Kumardfbe4672016-12-01 16:28:19 +05301392 if (!opp_table->regulators) {
1393 pr_err("%s: Doesn't have regulators set\n", __func__);
Viresh Kumarfa301842017-01-23 10:11:43 +05301394 return;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301395 }
1396
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301397 /* Make sure there are no concurrent readers while updating opp_table */
1398 WARN_ON(!list_empty(&opp_table->opp_list));
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301399
Viresh Kumardfbe4672016-12-01 16:28:19 +05301400 for (i = opp_table->regulator_count - 1; i >= 0; i--)
1401 regulator_put(opp_table->regulators[i]);
1402
Viresh Kumar94735582016-12-01 16:28:20 +05301403 _free_set_opp_data(opp_table);
1404
Viresh Kumardfbe4672016-12-01 16:28:19 +05301405 kfree(opp_table->regulators);
1406 opp_table->regulators = NULL;
1407 opp_table->regulator_count = 0;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301408
Viresh Kumarfa301842017-01-23 10:11:43 +05301409 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301410}
Viresh Kumardfbe4672016-12-01 16:28:19 +05301411EXPORT_SYMBOL_GPL(dev_pm_opp_put_regulators);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301412
Viresh Kumar383934092014-11-25 16:04:18 +05301413/**
Viresh Kumar829a4e82017-06-21 10:29:13 +05301414 * dev_pm_opp_set_clkname() - Set clk name for the device
1415 * @dev: Device for which clk name is being set.
1416 * @name: Clk name.
1417 *
1418 * In order to support OPP switching, OPP layer needs to get pointer to the
1419 * clock for the device. Simple cases work fine without using this routine (i.e.
1420 * by passing connection-id as NULL), but for a device with multiple clocks
1421 * available, the OPP core needs to know the exact name of the clk to use.
1422 *
1423 * This must be called before any OPPs are initialized for the device.
1424 */
1425struct opp_table *dev_pm_opp_set_clkname(struct device *dev, const char *name)
1426{
1427 struct opp_table *opp_table;
1428 int ret;
1429
1430 opp_table = dev_pm_opp_get_opp_table(dev);
1431 if (!opp_table)
1432 return ERR_PTR(-ENOMEM);
1433
1434 /* This should be called before OPPs are initialized */
1435 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
1436 ret = -EBUSY;
1437 goto err;
1438 }
1439
1440 /* Already have default clk set, free it */
1441 if (!IS_ERR(opp_table->clk))
1442 clk_put(opp_table->clk);
1443
1444 /* Find clk for the device */
1445 opp_table->clk = clk_get(dev, name);
1446 if (IS_ERR(opp_table->clk)) {
1447 ret = PTR_ERR(opp_table->clk);
1448 if (ret != -EPROBE_DEFER) {
1449 dev_err(dev, "%s: Couldn't find clock: %d\n", __func__,
1450 ret);
1451 }
1452 goto err;
1453 }
1454
1455 return opp_table;
1456
1457err:
1458 dev_pm_opp_put_opp_table(opp_table);
1459
1460 return ERR_PTR(ret);
1461}
1462EXPORT_SYMBOL_GPL(dev_pm_opp_set_clkname);
1463
1464/**
1465 * dev_pm_opp_put_clkname() - Releases resources blocked for clk.
1466 * @opp_table: OPP table returned from dev_pm_opp_set_clkname().
1467 */
1468void dev_pm_opp_put_clkname(struct opp_table *opp_table)
1469{
1470 /* Make sure there are no concurrent readers while updating opp_table */
1471 WARN_ON(!list_empty(&opp_table->opp_list));
1472
1473 clk_put(opp_table->clk);
1474 opp_table->clk = ERR_PTR(-EINVAL);
1475
1476 dev_pm_opp_put_opp_table(opp_table);
1477}
1478EXPORT_SYMBOL_GPL(dev_pm_opp_put_clkname);
1479
1480/**
Viresh Kumar4dab1602016-12-01 16:28:21 +05301481 * dev_pm_opp_register_set_opp_helper() - Register custom set OPP helper
1482 * @dev: Device for which the helper is getting registered.
1483 * @set_opp: Custom set OPP helper.
1484 *
1485 * This is useful to support complex platforms (like platforms with multiple
1486 * regulators per device), instead of the generic OPP set rate helper.
1487 *
1488 * This must be called before any OPPs are initialized for the device.
Viresh Kumar4dab1602016-12-01 16:28:21 +05301489 */
Viresh Kumarfa301842017-01-23 10:11:43 +05301490struct opp_table *dev_pm_opp_register_set_opp_helper(struct device *dev,
Viresh Kumar4dab1602016-12-01 16:28:21 +05301491 int (*set_opp)(struct dev_pm_set_opp_data *data))
1492{
1493 struct opp_table *opp_table;
1494 int ret;
1495
1496 if (!set_opp)
Viresh Kumarfa301842017-01-23 10:11:43 +05301497 return ERR_PTR(-EINVAL);
Viresh Kumar4dab1602016-12-01 16:28:21 +05301498
Viresh Kumarfa301842017-01-23 10:11:43 +05301499 opp_table = dev_pm_opp_get_opp_table(dev);
1500 if (!opp_table)
1501 return ERR_PTR(-ENOMEM);
Viresh Kumar4dab1602016-12-01 16:28:21 +05301502
1503 /* This should be called before OPPs are initialized */
1504 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
1505 ret = -EBUSY;
1506 goto err;
1507 }
1508
1509 /* Already have custom set_opp helper */
1510 if (WARN_ON(opp_table->set_opp)) {
1511 ret = -EBUSY;
1512 goto err;
1513 }
1514
1515 opp_table->set_opp = set_opp;
1516
Viresh Kumarfa301842017-01-23 10:11:43 +05301517 return opp_table;
Viresh Kumar4dab1602016-12-01 16:28:21 +05301518
1519err:
Viresh Kumarfa301842017-01-23 10:11:43 +05301520 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar4dab1602016-12-01 16:28:21 +05301521
Viresh Kumarfa301842017-01-23 10:11:43 +05301522 return ERR_PTR(ret);
Viresh Kumar4dab1602016-12-01 16:28:21 +05301523}
1524EXPORT_SYMBOL_GPL(dev_pm_opp_register_set_opp_helper);
1525
1526/**
Viresh Kumar604a7ae2017-10-05 17:26:21 +05301527 * dev_pm_opp_unregister_set_opp_helper() - Releases resources blocked for
Viresh Kumar4dab1602016-12-01 16:28:21 +05301528 * set_opp helper
Viresh Kumarfa301842017-01-23 10:11:43 +05301529 * @opp_table: OPP table returned from dev_pm_opp_register_set_opp_helper().
Viresh Kumar4dab1602016-12-01 16:28:21 +05301530 *
Viresh Kumarfa301842017-01-23 10:11:43 +05301531 * Release resources blocked for platform specific set_opp helper.
Viresh Kumar4dab1602016-12-01 16:28:21 +05301532 */
Viresh Kumar604a7ae2017-10-05 17:26:21 +05301533void dev_pm_opp_unregister_set_opp_helper(struct opp_table *opp_table)
Viresh Kumar4dab1602016-12-01 16:28:21 +05301534{
Viresh Kumar4dab1602016-12-01 16:28:21 +05301535 if (!opp_table->set_opp) {
Viresh Kumarfa301842017-01-23 10:11:43 +05301536 pr_err("%s: Doesn't have custom set_opp helper set\n",
1537 __func__);
1538 return;
Viresh Kumar4dab1602016-12-01 16:28:21 +05301539 }
1540
1541 /* Make sure there are no concurrent readers while updating opp_table */
1542 WARN_ON(!list_empty(&opp_table->opp_list));
1543
1544 opp_table->set_opp = NULL;
1545
Viresh Kumarfa301842017-01-23 10:11:43 +05301546 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar4dab1602016-12-01 16:28:21 +05301547}
Viresh Kumar604a7ae2017-10-05 17:26:21 +05301548EXPORT_SYMBOL_GPL(dev_pm_opp_unregister_set_opp_helper);
Viresh Kumar4dab1602016-12-01 16:28:21 +05301549
1550/**
Nishanth Menone1f60b22010-10-13 00:13:10 +02001551 * dev_pm_opp_add() - Add an OPP table from a table definitions
1552 * @dev: device for which we do this operation
Nishanth Menon47d43ba2013-09-19 16:03:51 -05001553 * @freq: Frequency in Hz for this OPP
Nishanth Menone1f60b22010-10-13 00:13:10 +02001554 * @u_volt: Voltage in uVolts for this OPP
1555 *
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301556 * This function adds an opp definition to the opp table and returns status.
Nishanth Menon47d43ba2013-09-19 16:03:51 -05001557 * The opp is made available by default and it can be controlled using
Nishanth Menone1f60b22010-10-13 00:13:10 +02001558 * dev_pm_opp_enable/disable functions.
1559 *
Viresh Kumara7470db2014-11-25 16:04:17 +05301560 * Return:
1561 * 0 On success OR
1562 * Duplicate OPPs (both freq and volt are same) and opp->available
1563 * -EEXIST Freq are same and volt are different OR
1564 * Duplicate OPPs (both freq and volt are same) and !opp->available
Viresh Kumar383934092014-11-25 16:04:18 +05301565 * -ENOMEM Memory allocation failure
Viresh Kumara7470db2014-11-25 16:04:17 +05301566 */
Nishanth Menone1f60b22010-10-13 00:13:10 +02001567int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
1568{
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301569 struct opp_table *opp_table;
1570 int ret;
1571
Viresh Kumarb83c1892017-01-23 10:11:45 +05301572 opp_table = dev_pm_opp_get_opp_table(dev);
1573 if (!opp_table)
1574 return -ENOMEM;
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301575
1576 ret = _opp_add_v1(opp_table, dev, freq, u_volt, true);
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301577
Viresh Kumarb83c1892017-01-23 10:11:45 +05301578 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301579 return ret;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001580}
1581EXPORT_SYMBOL_GPL(dev_pm_opp_add);
1582
Nishanth Menone1f60b22010-10-13 00:13:10 +02001583/**
Nishanth Menon327854c2014-12-24 11:22:56 -06001584 * _opp_set_availability() - helper to set the availability of an opp
Nishanth Menone1f60b22010-10-13 00:13:10 +02001585 * @dev: device for which we do this operation
1586 * @freq: OPP frequency to modify availability
1587 * @availability_req: availability status requested for this opp
1588 *
Viresh Kumar052c6f12017-01-23 10:11:49 +05301589 * Set the availability of an OPP, opp_{enable,disable} share a common logic
1590 * which is isolated here.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001591 *
Nishanth Menon984f16c2014-12-24 11:22:57 -06001592 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
Stephen Boyde1a2d492015-09-24 12:28:44 -07001593 * copy operation, returns 0 if no modification was done OR modification was
Nishanth Menone1f60b22010-10-13 00:13:10 +02001594 * successful.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001595 */
Nishanth Menon327854c2014-12-24 11:22:56 -06001596static int _opp_set_availability(struct device *dev, unsigned long freq,
1597 bool availability_req)
Nishanth Menone1f60b22010-10-13 00:13:10 +02001598{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301599 struct opp_table *opp_table;
Viresh Kumara7f39872017-01-23 10:11:50 +05301600 struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001601 int r = 0;
1602
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301603 /* Find the opp_table */
1604 opp_table = _find_opp_table(dev);
1605 if (IS_ERR(opp_table)) {
1606 r = PTR_ERR(opp_table);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001607 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
Viresh Kumara7f39872017-01-23 10:11:50 +05301608 return r;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001609 }
1610
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301611 mutex_lock(&opp_table->lock);
1612
Nishanth Menone1f60b22010-10-13 00:13:10 +02001613 /* Do we have the frequency? */
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301614 list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
Nishanth Menone1f60b22010-10-13 00:13:10 +02001615 if (tmp_opp->rate == freq) {
1616 opp = tmp_opp;
1617 break;
1618 }
1619 }
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301620
Nishanth Menone1f60b22010-10-13 00:13:10 +02001621 if (IS_ERR(opp)) {
1622 r = PTR_ERR(opp);
1623 goto unlock;
1624 }
1625
1626 /* Is update really needed? */
1627 if (opp->available == availability_req)
1628 goto unlock;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001629
Viresh Kumara7f39872017-01-23 10:11:50 +05301630 opp->available = availability_req;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001631
Viresh Kumare4d8ae02017-09-21 10:44:36 -07001632 dev_pm_opp_get(opp);
1633 mutex_unlock(&opp_table->lock);
1634
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001635 /* Notify the change of the OPP availability */
1636 if (availability_req)
Viresh Kumar052c6f12017-01-23 10:11:49 +05301637 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE,
Viresh Kumara7f39872017-01-23 10:11:50 +05301638 opp);
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001639 else
Viresh Kumar052c6f12017-01-23 10:11:49 +05301640 blocking_notifier_call_chain(&opp_table->head,
Viresh Kumara7f39872017-01-23 10:11:50 +05301641 OPP_EVENT_DISABLE, opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001642
Viresh Kumare4d8ae02017-09-21 10:44:36 -07001643 dev_pm_opp_put(opp);
1644 goto put_table;
1645
Nishanth Menone1f60b22010-10-13 00:13:10 +02001646unlock:
Viresh Kumar5b650b32017-01-23 10:11:48 +05301647 mutex_unlock(&opp_table->lock);
Viresh Kumare4d8ae02017-09-21 10:44:36 -07001648put_table:
Viresh Kumar5b650b32017-01-23 10:11:48 +05301649 dev_pm_opp_put_opp_table(opp_table);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001650 return r;
1651}
1652
1653/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001654 * dev_pm_opp_enable() - Enable a specific OPP
Nishanth Menone1f60b22010-10-13 00:13:10 +02001655 * @dev: device for which we do this operation
1656 * @freq: OPP frequency to enable
1657 *
1658 * Enables a provided opp. If the operation is valid, this returns 0, else the
1659 * corresponding error value. It is meant to be used for users an OPP available
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001660 * after being temporarily made unavailable with dev_pm_opp_disable.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001661 *
Nishanth Menon984f16c2014-12-24 11:22:57 -06001662 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
Stephen Boyde1a2d492015-09-24 12:28:44 -07001663 * copy operation, returns 0 if no modification was done OR modification was
Nishanth Menon984f16c2014-12-24 11:22:57 -06001664 * successful.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001665 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001666int dev_pm_opp_enable(struct device *dev, unsigned long freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +02001667{
Nishanth Menon327854c2014-12-24 11:22:56 -06001668 return _opp_set_availability(dev, freq, true);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001669}
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001670EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001671
1672/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001673 * dev_pm_opp_disable() - Disable a specific OPP
Nishanth Menone1f60b22010-10-13 00:13:10 +02001674 * @dev: device for which we do this operation
1675 * @freq: OPP frequency to disable
1676 *
1677 * Disables a provided opp. If the operation is valid, this returns
1678 * 0, else the corresponding error value. It is meant to be a temporary
1679 * control by users to make this OPP not available until the circumstances are
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001680 * right to make it available again (with a call to dev_pm_opp_enable).
Nishanth Menone1f60b22010-10-13 00:13:10 +02001681 *
Nishanth Menon984f16c2014-12-24 11:22:57 -06001682 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
Stephen Boyde1a2d492015-09-24 12:28:44 -07001683 * copy operation, returns 0 if no modification was done OR modification was
Nishanth Menon984f16c2014-12-24 11:22:57 -06001684 * successful.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001685 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001686int dev_pm_opp_disable(struct device *dev, unsigned long freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +02001687{
Nishanth Menon327854c2014-12-24 11:22:56 -06001688 return _opp_set_availability(dev, freq, false);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001689}
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001690EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001691
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001692/**
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301693 * dev_pm_opp_register_notifier() - Register OPP notifier for the device
1694 * @dev: Device for which notifier needs to be registered
1695 * @nb: Notifier block to be registered
Nishanth Menon984f16c2014-12-24 11:22:57 -06001696 *
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301697 * Return: 0 on success or a negative error value.
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001698 */
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301699int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb)
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001700{
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301701 struct opp_table *opp_table;
1702 int ret;
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001703
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301704 opp_table = _find_opp_table(dev);
Viresh Kumar5b650b32017-01-23 10:11:48 +05301705 if (IS_ERR(opp_table))
1706 return PTR_ERR(opp_table);
1707
Viresh Kumar052c6f12017-01-23 10:11:49 +05301708 ret = blocking_notifier_chain_register(&opp_table->head, nb);
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301709
Viresh Kumar5b650b32017-01-23 10:11:48 +05301710 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301711
1712 return ret;
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001713}
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301714EXPORT_SYMBOL(dev_pm_opp_register_notifier);
1715
1716/**
1717 * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device
1718 * @dev: Device for which notifier needs to be unregistered
1719 * @nb: Notifier block to be unregistered
1720 *
1721 * Return: 0 on success or a negative error value.
1722 */
1723int dev_pm_opp_unregister_notifier(struct device *dev,
1724 struct notifier_block *nb)
1725{
1726 struct opp_table *opp_table;
1727 int ret;
1728
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301729 opp_table = _find_opp_table(dev);
Viresh Kumar5b650b32017-01-23 10:11:48 +05301730 if (IS_ERR(opp_table))
1731 return PTR_ERR(opp_table);
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301732
Viresh Kumar052c6f12017-01-23 10:11:49 +05301733 ret = blocking_notifier_chain_unregister(&opp_table->head, nb);
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301734
Viresh Kumar5b650b32017-01-23 10:11:48 +05301735 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301736
1737 return ret;
1738}
1739EXPORT_SYMBOL(dev_pm_opp_unregister_notifier);
Shawn Guob496dfb2012-09-05 01:09:12 +02001740
Sudeep Holla411466c2016-05-03 15:05:04 +01001741/*
1742 * Free OPPs either created using static entries present in DT or even the
1743 * dynamically added entries based on remove_all param.
Shawn Guob496dfb2012-09-05 01:09:12 +02001744 */
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301745void _dev_pm_opp_remove_table(struct opp_table *opp_table, struct device *dev,
1746 bool remove_all)
Viresh Kumar9274c892017-01-02 14:41:00 +05301747{
1748 struct dev_pm_opp *opp, *tmp;
1749
Viresh Kumar9274c892017-01-02 14:41:00 +05301750 /* Find if opp_table manages a single device */
1751 if (list_is_singular(&opp_table->dev_list)) {
1752 /* Free static OPPs */
1753 list_for_each_entry_safe(opp, tmp, &opp_table->opp_list, node) {
1754 if (remove_all || !opp->dynamic)
Viresh Kumar70347642017-01-23 10:11:46 +05301755 dev_pm_opp_put(opp);
Viresh Kumar9274c892017-01-02 14:41:00 +05301756 }
Viresh Kumar009acd12017-10-11 12:54:14 +05301757
1758 /*
1759 * The OPP table is getting removed, drop the performance state
1760 * constraints.
1761 */
1762 if (opp_table->genpd_performance_state)
1763 dev_pm_genpd_set_performance_state(dev, 0);
Viresh Kumar9274c892017-01-02 14:41:00 +05301764 } else {
1765 _remove_opp_dev(_find_opp_dev(dev, opp_table), opp_table);
1766 }
1767}
1768
1769void _dev_pm_opp_find_and_remove_table(struct device *dev, bool remove_all)
Viresh Kumar737002b2015-07-29 16:22:58 +05301770{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301771 struct opp_table *opp_table;
Viresh Kumar737002b2015-07-29 16:22:58 +05301772
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301773 /* Check for existing table for 'dev' */
1774 opp_table = _find_opp_table(dev);
1775 if (IS_ERR(opp_table)) {
1776 int error = PTR_ERR(opp_table);
Viresh Kumar737002b2015-07-29 16:22:58 +05301777
1778 if (error != -ENODEV)
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301779 WARN(1, "%s: opp_table: %d\n",
Viresh Kumar737002b2015-07-29 16:22:58 +05301780 IS_ERR_OR_NULL(dev) ?
1781 "Invalid device" : dev_name(dev),
1782 error);
Viresh Kumar5b650b32017-01-23 10:11:48 +05301783 return;
Viresh Kumar737002b2015-07-29 16:22:58 +05301784 }
1785
Viresh Kumar9274c892017-01-02 14:41:00 +05301786 _dev_pm_opp_remove_table(opp_table, dev, remove_all);
Viresh Kumar737002b2015-07-29 16:22:58 +05301787
Viresh Kumar5b650b32017-01-23 10:11:48 +05301788 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar737002b2015-07-29 16:22:58 +05301789}
Viresh Kumar129eec52014-11-27 08:54:06 +05301790
1791/**
Sudeep Holla411466c2016-05-03 15:05:04 +01001792 * dev_pm_opp_remove_table() - Free all OPPs associated with the device
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301793 * @dev: device pointer used to lookup OPP table.
Viresh Kumar129eec52014-11-27 08:54:06 +05301794 *
Sudeep Holla411466c2016-05-03 15:05:04 +01001795 * Free both OPPs created using static entries present in DT and the
1796 * dynamically added entries.
Viresh Kumar129eec52014-11-27 08:54:06 +05301797 */
Sudeep Holla411466c2016-05-03 15:05:04 +01001798void dev_pm_opp_remove_table(struct device *dev)
Viresh Kumar129eec52014-11-27 08:54:06 +05301799{
Viresh Kumar9274c892017-01-02 14:41:00 +05301800 _dev_pm_opp_find_and_remove_table(dev, true);
Viresh Kumar8d4d4e92015-06-12 17:10:38 +05301801}
Sudeep Holla411466c2016-05-03 15:05:04 +01001802EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table);