blob: 0420f7e8ad5b0f926b56e5fe33adf82753aa70a5 [file] [log] [blame]
Nishanth Menone1f60b22010-10-13 00:13:10 +02001/*
2 * Generic OPP Interface
3 *
4 * Copyright (C) 2009-2010 Texas Instruments Incorporated.
5 * Nishanth Menon
6 * Romit Dasgupta
7 * Kevin Hilman
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
Viresh Kumard6d2a522015-10-17 09:45:18 +053014#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
Viresh Kumard54974c2016-02-09 10:30:38 +053016#include <linux/clk.h>
Nishanth Menone1f60b22010-10-13 00:13:10 +020017#include <linux/errno.h>
18#include <linux/err.h>
Nishanth Menone1f60b22010-10-13 00:13:10 +020019#include <linux/slab.h>
Paul Gortmaker51990e82012-01-22 11:23:42 -050020#include <linux/device.h>
Liam Girdwood80126ce2012-10-23 01:27:44 +020021#include <linux/export.h>
Viresh Kumar009acd12017-10-11 12:54:14 +053022#include <linux/pm_domain.h>
Viresh Kumar9f8ea962016-02-09 10:30:33 +053023#include <linux/regulator/consumer.h>
Nishanth Menone1f60b22010-10-13 00:13:10 +020024
Viresh Kumarf59d3ee2015-09-04 13:47:26 +053025#include "opp.h"
Nishanth Menone1f60b22010-10-13 00:13:10 +020026
27/*
Viresh Kumar2c2709d2016-02-16 14:17:53 +053028 * The root of the list of all opp-tables. All opp_table structures branch off
29 * from here, with each opp_table containing the list of opps it supports in
Nishanth Menone1f60b22010-10-13 00:13:10 +020030 * various states of availability.
31 */
Viresh Kumarf47b72a2016-05-05 16:20:33 +053032LIST_HEAD(opp_tables);
Nishanth Menone1f60b22010-10-13 00:13:10 +020033/* Lock to allow exclusive modification to the device and opp lists */
Viresh Kumar2c2709d2016-02-16 14:17:53 +053034DEFINE_MUTEX(opp_table_lock);
Nishanth Menone1f60b22010-10-13 00:13:10 +020035
Viresh Kumar2c2709d2016-02-16 14:17:53 +053036static struct opp_device *_find_opp_dev(const struct device *dev,
37 struct opp_table *opp_table)
Viresh Kumar06441652015-07-29 16:23:04 +053038{
Viresh Kumar2c2709d2016-02-16 14:17:53 +053039 struct opp_device *opp_dev;
Viresh Kumar06441652015-07-29 16:23:04 +053040
Viresh Kumar2c2709d2016-02-16 14:17:53 +053041 list_for_each_entry(opp_dev, &opp_table->dev_list, node)
42 if (opp_dev->dev == dev)
43 return opp_dev;
Viresh Kumar06441652015-07-29 16:23:04 +053044
45 return NULL;
46}
47
Wei Yongjun6ac42392017-02-06 14:29:55 +000048static struct opp_table *_find_opp_table_unlocked(struct device *dev)
Viresh Kumar5b650b32017-01-23 10:11:48 +053049{
50 struct opp_table *opp_table;
Viresh Kumar3d255692018-08-03 07:05:21 +053051 bool found;
Viresh Kumar5b650b32017-01-23 10:11:48 +053052
53 list_for_each_entry(opp_table, &opp_tables, node) {
Viresh Kumar3d255692018-08-03 07:05:21 +053054 mutex_lock(&opp_table->lock);
55 found = !!_find_opp_dev(dev, opp_table);
56 mutex_unlock(&opp_table->lock);
57
58 if (found) {
Viresh Kumar5b650b32017-01-23 10:11:48 +053059 _get_opp_table_kref(opp_table);
60
61 return opp_table;
62 }
63 }
64
65 return ERR_PTR(-ENODEV);
66}
67
Nishanth Menone1f60b22010-10-13 00:13:10 +020068/**
Viresh Kumar2c2709d2016-02-16 14:17:53 +053069 * _find_opp_table() - find opp_table struct using device pointer
70 * @dev: device pointer used to lookup OPP table
Nishanth Menone1f60b22010-10-13 00:13:10 +020071 *
Viresh Kumar052c6f12017-01-23 10:11:49 +053072 * Search OPP table for one containing matching device.
Nishanth Menone1f60b22010-10-13 00:13:10 +020073 *
Viresh Kumar2c2709d2016-02-16 14:17:53 +053074 * Return: pointer to 'struct opp_table' if found, otherwise -ENODEV or
Nishanth Menone1f60b22010-10-13 00:13:10 +020075 * -EINVAL based on type of error.
76 *
Viresh Kumar5b650b32017-01-23 10:11:48 +053077 * The callers must call dev_pm_opp_put_opp_table() after the table is used.
Nishanth Menone1f60b22010-10-13 00:13:10 +020078 */
Viresh Kumar2c2709d2016-02-16 14:17:53 +053079struct opp_table *_find_opp_table(struct device *dev)
Nishanth Menone1f60b22010-10-13 00:13:10 +020080{
Viresh Kumar2c2709d2016-02-16 14:17:53 +053081 struct opp_table *opp_table;
Nishanth Menone1f60b22010-10-13 00:13:10 +020082
Viresh Kumar50a3cb02015-08-12 15:59:39 +053083 if (IS_ERR_OR_NULL(dev)) {
Nishanth Menone1f60b22010-10-13 00:13:10 +020084 pr_err("%s: Invalid parameters\n", __func__);
85 return ERR_PTR(-EINVAL);
86 }
87
Viresh Kumar5b650b32017-01-23 10:11:48 +053088 mutex_lock(&opp_table_lock);
89 opp_table = _find_opp_table_unlocked(dev);
90 mutex_unlock(&opp_table_lock);
Nishanth Menone1f60b22010-10-13 00:13:10 +020091
Viresh Kumar5b650b32017-01-23 10:11:48 +053092 return opp_table;
Nishanth Menone1f60b22010-10-13 00:13:10 +020093}
94
95/**
Linus Torvaldsbaf51c42015-11-11 09:03:01 -080096 * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an opp
Nishanth Menone1f60b22010-10-13 00:13:10 +020097 * @opp: opp for which voltage has to be returned for
98 *
Nishanth Menon984f16c82014-12-24 11:22:57 -060099 * Return: voltage in micro volt corresponding to the opp, else
Nishanth Menone1f60b22010-10-13 00:13:10 +0200100 * return 0
101 *
Viresh Kumardfbe4672016-12-01 16:28:19 +0530102 * This is useful only for devices with single power supply.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200103 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500104unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200105{
Viresh Kumar052c6f12017-01-23 10:11:49 +0530106 if (IS_ERR_OR_NULL(opp)) {
Nishanth Menone1f60b22010-10-13 00:13:10 +0200107 pr_err("%s: Invalid parameters\n", __func__);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530108 return 0;
109 }
Nishanth Menone1f60b22010-10-13 00:13:10 +0200110
Viresh Kumar052c6f12017-01-23 10:11:49 +0530111 return opp->supplies[0].u_volt;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200112}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500113EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200114
115/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500116 * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp
Nishanth Menone1f60b22010-10-13 00:13:10 +0200117 * @opp: opp for which frequency has to be returned for
118 *
Nishanth Menon984f16c82014-12-24 11:22:57 -0600119 * Return: frequency in hertz corresponding to the opp, else
Nishanth Menone1f60b22010-10-13 00:13:10 +0200120 * return 0
Nishanth Menone1f60b22010-10-13 00:13:10 +0200121 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500122unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200123{
Viresh Kumar052c6f12017-01-23 10:11:49 +0530124 if (IS_ERR_OR_NULL(opp) || !opp->available) {
Nishanth Menone1f60b22010-10-13 00:13:10 +0200125 pr_err("%s: Invalid parameters\n", __func__);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530126 return 0;
127 }
Nishanth Menone1f60b22010-10-13 00:13:10 +0200128
Viresh Kumar052c6f12017-01-23 10:11:49 +0530129 return opp->rate;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200130}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500131EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200132
133/**
Rajendra Nayak5b93ac52019-01-10 09:32:02 +0530134 * dev_pm_opp_get_level() - Gets the level corresponding to an available opp
135 * @opp: opp for which level value has to be returned for
136 *
137 * Return: level read from device tree corresponding to the opp, else
138 * return 0.
139 */
140unsigned int dev_pm_opp_get_level(struct dev_pm_opp *opp)
141{
142 if (IS_ERR_OR_NULL(opp) || !opp->available) {
143 pr_err("%s: Invalid parameters\n", __func__);
144 return 0;
145 }
146
147 return opp->level;
148}
149EXPORT_SYMBOL_GPL(dev_pm_opp_get_level);
150
151/**
Bartlomiej Zolnierkiewicz19445b22015-07-09 17:43:35 +0200152 * dev_pm_opp_is_turbo() - Returns if opp is turbo OPP or not
153 * @opp: opp for which turbo mode is being verified
154 *
155 * Turbo OPPs are not for normal use, and can be enabled (under certain
156 * conditions) for short duration of times to finish high throughput work
157 * quickly. Running on them for longer times may overheat the chip.
158 *
159 * Return: true if opp is turbo opp, else false.
Bartlomiej Zolnierkiewicz19445b22015-07-09 17:43:35 +0200160 */
161bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp)
162{
Viresh Kumar052c6f12017-01-23 10:11:49 +0530163 if (IS_ERR_OR_NULL(opp) || !opp->available) {
Bartlomiej Zolnierkiewicz19445b22015-07-09 17:43:35 +0200164 pr_err("%s: Invalid parameters\n", __func__);
165 return false;
166 }
167
Viresh Kumar052c6f12017-01-23 10:11:49 +0530168 return opp->turbo;
Bartlomiej Zolnierkiewicz19445b22015-07-09 17:43:35 +0200169}
170EXPORT_SYMBOL_GPL(dev_pm_opp_is_turbo);
171
172/**
Viresh Kumar3ca9bb32015-07-29 16:23:03 +0530173 * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds
174 * @dev: device for which we do this operation
175 *
176 * Return: This function returns the max clock latency in nanoseconds.
Viresh Kumar3ca9bb32015-07-29 16:23:03 +0530177 */
178unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev)
179{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530180 struct opp_table *opp_table;
Viresh Kumar3ca9bb32015-07-29 16:23:03 +0530181 unsigned long clock_latency_ns;
182
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530183 opp_table = _find_opp_table(dev);
184 if (IS_ERR(opp_table))
Viresh Kumar5b650b32017-01-23 10:11:48 +0530185 return 0;
Viresh Kumar3ca9bb32015-07-29 16:23:03 +0530186
Viresh Kumar5b650b32017-01-23 10:11:48 +0530187 clock_latency_ns = opp_table->clock_latency_ns_max;
188
189 dev_pm_opp_put_opp_table(opp_table);
190
Viresh Kumar3ca9bb32015-07-29 16:23:03 +0530191 return clock_latency_ns;
192}
193EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency);
194
195/**
Viresh Kumar655c9df2016-02-09 10:30:35 +0530196 * dev_pm_opp_get_max_volt_latency() - Get max voltage latency in nanoseconds
197 * @dev: device for which we do this operation
198 *
199 * Return: This function returns the max voltage latency in nanoseconds.
Viresh Kumar655c9df2016-02-09 10:30:35 +0530200 */
201unsigned long dev_pm_opp_get_max_volt_latency(struct device *dev)
202{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530203 struct opp_table *opp_table;
Viresh Kumar655c9df2016-02-09 10:30:35 +0530204 struct dev_pm_opp *opp;
Viresh Kumar478256b2017-05-23 09:32:11 +0530205 struct regulator *reg;
Viresh Kumar655c9df2016-02-09 10:30:35 +0530206 unsigned long latency_ns = 0;
Viresh Kumardfbe4672016-12-01 16:28:19 +0530207 int ret, i, count;
208 struct {
209 unsigned long min;
210 unsigned long max;
211 } *uV;
212
Viresh Kumarcdd3e612017-01-23 10:11:51 +0530213 opp_table = _find_opp_table(dev);
214 if (IS_ERR(opp_table))
215 return 0;
216
Viresh Kumardfbe4672016-12-01 16:28:19 +0530217 /* Regulator may not be required for the device */
Viresh Kumar90e35772018-12-11 16:32:47 +0530218 if (!opp_table->regulators)
Viresh Kumarcdd3e612017-01-23 10:11:51 +0530219 goto put_opp_table;
Viresh Kumardfbe4672016-12-01 16:28:19 +0530220
Viresh Kumar90e35772018-12-11 16:32:47 +0530221 count = opp_table->regulator_count;
222
Viresh Kumardfbe4672016-12-01 16:28:19 +0530223 uV = kmalloc_array(count, sizeof(*uV), GFP_KERNEL);
224 if (!uV)
Viresh Kumar478256b2017-05-23 09:32:11 +0530225 goto put_opp_table;
Viresh Kumar655c9df2016-02-09 10:30:35 +0530226
Viresh Kumar052c6f12017-01-23 10:11:49 +0530227 mutex_lock(&opp_table->lock);
228
Viresh Kumardfbe4672016-12-01 16:28:19 +0530229 for (i = 0; i < count; i++) {
230 uV[i].min = ~0;
231 uV[i].max = 0;
Viresh Kumar655c9df2016-02-09 10:30:35 +0530232
Viresh Kumar052c6f12017-01-23 10:11:49 +0530233 list_for_each_entry(opp, &opp_table->opp_list, node) {
Viresh Kumardfbe4672016-12-01 16:28:19 +0530234 if (!opp->available)
235 continue;
236
237 if (opp->supplies[i].u_volt_min < uV[i].min)
238 uV[i].min = opp->supplies[i].u_volt_min;
239 if (opp->supplies[i].u_volt_max > uV[i].max)
240 uV[i].max = opp->supplies[i].u_volt_max;
241 }
Viresh Kumar655c9df2016-02-09 10:30:35 +0530242 }
243
Viresh Kumar052c6f12017-01-23 10:11:49 +0530244 mutex_unlock(&opp_table->lock);
Viresh Kumar655c9df2016-02-09 10:30:35 +0530245
246 /*
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530247 * The caller needs to ensure that opp_table (and hence the regulator)
Viresh Kumar655c9df2016-02-09 10:30:35 +0530248 * isn't freed, while we are executing this routine.
249 */
Andrzej Hajda8cc311162017-02-20 19:57:57 +0100250 for (i = 0; i < count; i++) {
Viresh Kumar478256b2017-05-23 09:32:11 +0530251 reg = opp_table->regulators[i];
Viresh Kumardfbe4672016-12-01 16:28:19 +0530252 ret = regulator_set_voltage_time(reg, uV[i].min, uV[i].max);
253 if (ret > 0)
254 latency_ns += ret * 1000;
255 }
256
Viresh Kumardfbe4672016-12-01 16:28:19 +0530257 kfree(uV);
Viresh Kumarcdd3e612017-01-23 10:11:51 +0530258put_opp_table:
259 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar655c9df2016-02-09 10:30:35 +0530260
261 return latency_ns;
262}
263EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_volt_latency);
264
265/**
Viresh Kumar21743442016-02-09 10:30:36 +0530266 * dev_pm_opp_get_max_transition_latency() - Get max transition latency in
267 * nanoseconds
268 * @dev: device for which we do this operation
269 *
270 * Return: This function returns the max transition latency, in nanoseconds, to
271 * switch from one OPP to other.
Viresh Kumar21743442016-02-09 10:30:36 +0530272 */
273unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev)
274{
275 return dev_pm_opp_get_max_volt_latency(dev) +
276 dev_pm_opp_get_max_clock_latency(dev);
277}
278EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_transition_latency);
279
280/**
Viresh Kumar3aa26a32017-01-02 14:41:02 +0530281 * dev_pm_opp_get_suspend_opp_freq() - Get frequency of suspend opp in Hz
Bartlomiej Zolnierkiewicz4eafbd12015-09-08 18:41:01 +0200282 * @dev: device for which we do this operation
283 *
Viresh Kumar3aa26a32017-01-02 14:41:02 +0530284 * Return: This function returns the frequency of the OPP marked as suspend_opp
285 * if one is available, else returns 0;
Bartlomiej Zolnierkiewicz4eafbd12015-09-08 18:41:01 +0200286 */
Viresh Kumar3aa26a32017-01-02 14:41:02 +0530287unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev)
Bartlomiej Zolnierkiewicz4eafbd12015-09-08 18:41:01 +0200288{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530289 struct opp_table *opp_table;
Viresh Kumar3aa26a32017-01-02 14:41:02 +0530290 unsigned long freq = 0;
Bartlomiej Zolnierkiewicz4eafbd12015-09-08 18:41:01 +0200291
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530292 opp_table = _find_opp_table(dev);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530293 if (IS_ERR(opp_table))
294 return 0;
Bartlomiej Zolnierkiewicz4eafbd12015-09-08 18:41:01 +0200295
Viresh Kumar5b650b32017-01-23 10:11:48 +0530296 if (opp_table->suspend_opp && opp_table->suspend_opp->available)
297 freq = dev_pm_opp_get_freq(opp_table->suspend_opp);
Viresh Kumar3aa26a32017-01-02 14:41:02 +0530298
Viresh Kumar5b650b32017-01-23 10:11:48 +0530299 dev_pm_opp_put_opp_table(opp_table);
300
Viresh Kumar3aa26a32017-01-02 14:41:02 +0530301 return freq;
Bartlomiej Zolnierkiewicz4eafbd12015-09-08 18:41:01 +0200302}
Viresh Kumar3aa26a32017-01-02 14:41:02 +0530303EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp_freq);
Bartlomiej Zolnierkiewicz4eafbd12015-09-08 18:41:01 +0200304
Viresh Kumara1e8c132018-04-06 14:35:45 +0530305int _get_opp_count(struct opp_table *opp_table)
306{
307 struct dev_pm_opp *opp;
308 int count = 0;
309
310 mutex_lock(&opp_table->lock);
311
312 list_for_each_entry(opp, &opp_table->opp_list, node) {
313 if (opp->available)
314 count++;
315 }
316
317 mutex_unlock(&opp_table->lock);
318
319 return count;
320}
321
Bartlomiej Zolnierkiewicz4eafbd12015-09-08 18:41:01 +0200322/**
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530323 * dev_pm_opp_get_opp_count() - Get number of opps available in the opp table
Nishanth Menone1f60b22010-10-13 00:13:10 +0200324 * @dev: device for which we do this operation
325 *
Nishanth Menon984f16c82014-12-24 11:22:57 -0600326 * Return: This function returns the number of available opps if there are any,
Nishanth Menone1f60b22010-10-13 00:13:10 +0200327 * else returns 0 if none or the corresponding error value.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200328 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500329int dev_pm_opp_get_opp_count(struct device *dev)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200330{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530331 struct opp_table *opp_table;
Viresh Kumara1e8c132018-04-06 14:35:45 +0530332 int count;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200333
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530334 opp_table = _find_opp_table(dev);
335 if (IS_ERR(opp_table)) {
336 count = PTR_ERR(opp_table);
Fabio Estevam035ed072017-09-29 14:39:49 -0300337 dev_dbg(dev, "%s: OPP table not found (%d)\n",
Dmitry Torokhovb4718c02014-12-16 15:09:38 -0800338 __func__, count);
Viresh Kumar09f662f2018-10-03 15:22:03 +0530339 return count;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200340 }
341
Viresh Kumara1e8c132018-04-06 14:35:45 +0530342 count = _get_opp_count(opp_table);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530343 dev_pm_opp_put_opp_table(opp_table);
344
Nishanth Menone1f60b22010-10-13 00:13:10 +0200345 return count;
346}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500347EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200348
349/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500350 * dev_pm_opp_find_freq_exact() - search for an exact frequency
Nishanth Menone1f60b22010-10-13 00:13:10 +0200351 * @dev: device for which we do this operation
352 * @freq: frequency to search for
Nishanth Menon7ae49612011-02-25 23:46:18 +0100353 * @available: true/false - match for available opp
Nishanth Menone1f60b22010-10-13 00:13:10 +0200354 *
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530355 * Return: Searches for exact match in the opp table and returns pointer to the
Nishanth Menon984f16c82014-12-24 11:22:57 -0600356 * matching opp if found, else returns ERR_PTR in case of error and should
357 * be handled using IS_ERR. Error return values can be:
Nishanth Menon07797262012-10-24 22:00:12 +0200358 * EINVAL: for bad pointer
359 * ERANGE: no match found for search
360 * ENODEV: if device not found in list of registered devices
Nishanth Menone1f60b22010-10-13 00:13:10 +0200361 *
362 * Note: available is a modifier for the search. if available=true, then the
363 * match is for exact matching frequency and is available in the stored OPP
364 * table. if false, the match is for exact frequency which is not available.
365 *
366 * This provides a mechanism to enable an opp which is not available currently
367 * or the opposite as well.
368 *
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530369 * The callers are required to call dev_pm_opp_put() for the returned OPP after
370 * use.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200371 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500372struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
373 unsigned long freq,
374 bool available)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200375{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530376 struct opp_table *opp_table;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500377 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200378
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530379 opp_table = _find_opp_table(dev);
380 if (IS_ERR(opp_table)) {
381 int r = PTR_ERR(opp_table);
382
383 dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200384 return ERR_PTR(r);
385 }
386
Viresh Kumar052c6f12017-01-23 10:11:49 +0530387 mutex_lock(&opp_table->lock);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530388
Viresh Kumar052c6f12017-01-23 10:11:49 +0530389 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
Nishanth Menone1f60b22010-10-13 00:13:10 +0200390 if (temp_opp->available == available &&
391 temp_opp->rate == freq) {
392 opp = temp_opp;
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530393
394 /* Increment the reference count of OPP */
395 dev_pm_opp_get(opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200396 break;
397 }
398 }
399
Viresh Kumar052c6f12017-01-23 10:11:49 +0530400 mutex_unlock(&opp_table->lock);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530401 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530402
Nishanth Menone1f60b22010-10-13 00:13:10 +0200403 return opp;
404}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500405EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200406
Jisheng Zhang067b7ce2016-07-25 14:11:16 +0800407static noinline struct dev_pm_opp *_find_freq_ceil(struct opp_table *opp_table,
408 unsigned long *freq)
409{
410 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
411
Viresh Kumar052c6f12017-01-23 10:11:49 +0530412 mutex_lock(&opp_table->lock);
413
414 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
Jisheng Zhang067b7ce2016-07-25 14:11:16 +0800415 if (temp_opp->available && temp_opp->rate >= *freq) {
416 opp = temp_opp;
417 *freq = opp->rate;
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530418
419 /* Increment the reference count of OPP */
420 dev_pm_opp_get(opp);
Jisheng Zhang067b7ce2016-07-25 14:11:16 +0800421 break;
422 }
423 }
424
Viresh Kumar052c6f12017-01-23 10:11:49 +0530425 mutex_unlock(&opp_table->lock);
426
Jisheng Zhang067b7ce2016-07-25 14:11:16 +0800427 return opp;
428}
429
Nishanth Menone1f60b22010-10-13 00:13:10 +0200430/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500431 * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
Nishanth Menone1f60b22010-10-13 00:13:10 +0200432 * @dev: device for which we do this operation
433 * @freq: Start frequency
434 *
435 * Search for the matching ceil *available* OPP from a starting freq
436 * for a device.
437 *
Nishanth Menon984f16c82014-12-24 11:22:57 -0600438 * Return: matching *opp and refreshes *freq accordingly, else returns
Nishanth Menon07797262012-10-24 22:00:12 +0200439 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
440 * values can be:
441 * EINVAL: for bad pointer
442 * ERANGE: no match found for search
443 * ENODEV: if device not found in list of registered devices
Nishanth Menone1f60b22010-10-13 00:13:10 +0200444 *
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530445 * The callers are required to call dev_pm_opp_put() for the returned OPP after
446 * use.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200447 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500448struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
449 unsigned long *freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200450{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530451 struct opp_table *opp_table;
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530452 struct dev_pm_opp *opp;
Dmitry Torokhovb02ded22014-12-16 15:09:36 -0800453
Nishanth Menone1f60b22010-10-13 00:13:10 +0200454 if (!dev || !freq) {
455 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
456 return ERR_PTR(-EINVAL);
457 }
458
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530459 opp_table = _find_opp_table(dev);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530460 if (IS_ERR(opp_table))
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530461 return ERR_CAST(opp_table);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530462
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530463 opp = _find_freq_ceil(opp_table, freq);
464
Viresh Kumar5b650b32017-01-23 10:11:48 +0530465 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530466
467 return opp;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200468}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500469EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200470
471/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500472 * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
Nishanth Menone1f60b22010-10-13 00:13:10 +0200473 * @dev: device for which we do this operation
474 * @freq: Start frequency
475 *
476 * Search for the matching floor *available* OPP from a starting freq
477 * for a device.
478 *
Nishanth Menon984f16c82014-12-24 11:22:57 -0600479 * Return: matching *opp and refreshes *freq accordingly, else returns
Nishanth Menon07797262012-10-24 22:00:12 +0200480 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
481 * values can be:
482 * EINVAL: for bad pointer
483 * ERANGE: no match found for search
484 * ENODEV: if device not found in list of registered devices
Nishanth Menone1f60b22010-10-13 00:13:10 +0200485 *
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530486 * The callers are required to call dev_pm_opp_put() for the returned OPP after
487 * use.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200488 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500489struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
490 unsigned long *freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200491{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530492 struct opp_table *opp_table;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500493 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200494
495 if (!dev || !freq) {
496 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
497 return ERR_PTR(-EINVAL);
498 }
499
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530500 opp_table = _find_opp_table(dev);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530501 if (IS_ERR(opp_table))
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530502 return ERR_CAST(opp_table);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530503
Viresh Kumar052c6f12017-01-23 10:11:49 +0530504 mutex_lock(&opp_table->lock);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200505
Viresh Kumar052c6f12017-01-23 10:11:49 +0530506 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
Nishanth Menone1f60b22010-10-13 00:13:10 +0200507 if (temp_opp->available) {
508 /* go to the next node, before choosing prev */
509 if (temp_opp->rate > *freq)
510 break;
511 else
512 opp = temp_opp;
513 }
514 }
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530515
516 /* Increment the reference count of OPP */
517 if (!IS_ERR(opp))
518 dev_pm_opp_get(opp);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530519 mutex_unlock(&opp_table->lock);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530520 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530521
Nishanth Menone1f60b22010-10-13 00:13:10 +0200522 if (!IS_ERR(opp))
523 *freq = opp->rate;
524
525 return opp;
526}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500527EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200528
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530529static int _set_opp_voltage(struct device *dev, struct regulator *reg,
Viresh Kumarce317812016-12-01 16:28:18 +0530530 struct dev_pm_opp_supply *supply)
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530531{
532 int ret;
533
534 /* Regulator not available for device */
535 if (IS_ERR(reg)) {
536 dev_dbg(dev, "%s: regulator not available: %ld\n", __func__,
537 PTR_ERR(reg));
538 return 0;
539 }
540
Viresh Kumarce317812016-12-01 16:28:18 +0530541 dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__,
542 supply->u_volt_min, supply->u_volt, supply->u_volt_max);
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530543
Viresh Kumarce317812016-12-01 16:28:18 +0530544 ret = regulator_set_voltage_triplet(reg, supply->u_volt_min,
545 supply->u_volt, supply->u_volt_max);
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530546 if (ret)
547 dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n",
Viresh Kumarce317812016-12-01 16:28:18 +0530548 __func__, supply->u_volt_min, supply->u_volt,
549 supply->u_volt_max, ret);
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530550
551 return ret;
552}
553
Viresh Kumar285881b2019-01-31 13:53:25 +0530554static inline int _generic_set_opp_clk_only(struct device *dev, struct clk *clk,
555 unsigned long freq)
Viresh Kumar94735582016-12-01 16:28:20 +0530556{
557 int ret;
558
559 ret = clk_set_rate(clk, freq);
560 if (ret) {
561 dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
562 ret);
563 }
564
565 return ret;
566}
567
Viresh Kumarc74b32f2017-05-23 09:32:10 +0530568static int _generic_set_opp_regulator(const struct opp_table *opp_table,
569 struct device *dev,
570 unsigned long old_freq,
571 unsigned long freq,
572 struct dev_pm_opp_supply *old_supply,
573 struct dev_pm_opp_supply *new_supply)
Viresh Kumar94735582016-12-01 16:28:20 +0530574{
Viresh Kumarc74b32f2017-05-23 09:32:10 +0530575 struct regulator *reg = opp_table->regulators[0];
Viresh Kumar94735582016-12-01 16:28:20 +0530576 int ret;
577
578 /* This function only supports single regulator per device */
Viresh Kumarc74b32f2017-05-23 09:32:10 +0530579 if (WARN_ON(opp_table->regulator_count > 1)) {
Viresh Kumar94735582016-12-01 16:28:20 +0530580 dev_err(dev, "multiple regulators are not supported\n");
581 return -EINVAL;
582 }
583
584 /* Scaling up? Scale voltage before frequency */
Waldemar Rymarkiewiczc5c2a972018-06-14 15:56:08 +0200585 if (freq >= old_freq) {
Viresh Kumar94735582016-12-01 16:28:20 +0530586 ret = _set_opp_voltage(dev, reg, new_supply);
587 if (ret)
588 goto restore_voltage;
589 }
590
591 /* Change frequency */
Viresh Kumar285881b2019-01-31 13:53:25 +0530592 ret = _generic_set_opp_clk_only(dev, opp_table->clk, freq);
Viresh Kumar94735582016-12-01 16:28:20 +0530593 if (ret)
594 goto restore_voltage;
595
596 /* Scaling down? Scale voltage after frequency */
597 if (freq < old_freq) {
598 ret = _set_opp_voltage(dev, reg, new_supply);
599 if (ret)
600 goto restore_freq;
601 }
602
603 return 0;
604
605restore_freq:
Viresh Kumar285881b2019-01-31 13:53:25 +0530606 if (_generic_set_opp_clk_only(dev, opp_table->clk, old_freq))
Viresh Kumar94735582016-12-01 16:28:20 +0530607 dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n",
608 __func__, old_freq);
609restore_voltage:
610 /* This shouldn't harm even if the voltages weren't updated earlier */
Viresh Kumarc74b32f2017-05-23 09:32:10 +0530611 if (old_supply)
Viresh Kumar94735582016-12-01 16:28:20 +0530612 _set_opp_voltage(dev, reg, old_supply);
613
614 return ret;
615}
616
Viresh Kumar7e5359932018-06-12 14:47:03 +0530617static int _set_opp_custom(const struct opp_table *opp_table,
618 struct device *dev, unsigned long old_freq,
619 unsigned long freq,
620 struct dev_pm_opp_supply *old_supply,
621 struct dev_pm_opp_supply *new_supply)
622{
623 struct dev_pm_set_opp_data *data;
624 int size;
625
626 data = opp_table->set_opp_data;
627 data->regulators = opp_table->regulators;
628 data->regulator_count = opp_table->regulator_count;
629 data->clk = opp_table->clk;
630 data->dev = dev;
631
632 data->old_opp.rate = old_freq;
633 size = sizeof(*old_supply) * opp_table->regulator_count;
634 if (IS_ERR(old_supply))
635 memset(data->old_opp.supplies, 0, size);
636 else
637 memcpy(data->old_opp.supplies, old_supply, size);
638
639 data->new_opp.rate = freq;
640 memcpy(data->new_opp.supplies, new_supply, size);
641
642 return opp_table->set_opp(data);
643}
644
Viresh Kumarca1b5d72018-06-14 10:03:26 +0530645/* This is only called for PM domain for now */
646static int _set_required_opps(struct device *dev,
647 struct opp_table *opp_table,
648 struct dev_pm_opp *opp)
649{
650 struct opp_table **required_opp_tables = opp_table->required_opp_tables;
651 struct device **genpd_virt_devs = opp_table->genpd_virt_devs;
652 unsigned int pstate;
653 int i, ret = 0;
654
655 if (!required_opp_tables)
656 return 0;
657
658 /* Single genpd case */
659 if (!genpd_virt_devs) {
660 pstate = opp->required_opps[0]->pstate;
661 ret = dev_pm_genpd_set_performance_state(dev, pstate);
662 if (ret) {
663 dev_err(dev, "Failed to set performance state of %s: %d (%d)\n",
664 dev_name(dev), pstate, ret);
665 }
666 return ret;
667 }
668
669 /* Multiple genpd case */
670
671 /*
672 * Acquire genpd_virt_dev_lock to make sure we don't use a genpd_dev
673 * after it is freed from another thread.
674 */
675 mutex_lock(&opp_table->genpd_virt_dev_lock);
676
677 for (i = 0; i < opp_table->required_opp_count; i++) {
678 pstate = opp->required_opps[i]->pstate;
679
680 if (!genpd_virt_devs[i])
681 continue;
682
683 ret = dev_pm_genpd_set_performance_state(genpd_virt_devs[i], pstate);
684 if (ret) {
685 dev_err(dev, "Failed to set performance rate of %s: %d (%d)\n",
686 dev_name(genpd_virt_devs[i]), pstate, ret);
687 break;
688 }
689 }
690 mutex_unlock(&opp_table->genpd_virt_dev_lock);
691
692 return ret;
693}
694
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530695/**
696 * dev_pm_opp_set_rate() - Configure new OPP based on frequency
697 * @dev: device for which we do this operation
698 * @target_freq: frequency to achieve
699 *
700 * This configures the power-supplies and clock source to the levels specified
701 * by the OPP corresponding to the target_freq.
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530702 */
703int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq)
704{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530705 struct opp_table *opp_table;
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530706 unsigned long freq, old_freq;
Viresh Kumar94735582016-12-01 16:28:20 +0530707 struct dev_pm_opp *old_opp, *opp;
Viresh Kumar94735582016-12-01 16:28:20 +0530708 struct clk *clk;
Viresh Kumar7e5359932018-06-12 14:47:03 +0530709 int ret;
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530710
711 if (unlikely(!target_freq)) {
712 dev_err(dev, "%s: Invalid target frequency %lu\n", __func__,
713 target_freq);
714 return -EINVAL;
715 }
716
Viresh Kumar052c6f12017-01-23 10:11:49 +0530717 opp_table = _find_opp_table(dev);
718 if (IS_ERR(opp_table)) {
719 dev_err(dev, "%s: device opp doesn't exist\n", __func__);
720 return PTR_ERR(opp_table);
721 }
722
723 clk = opp_table->clk;
724 if (IS_ERR(clk)) {
725 dev_err(dev, "%s: No clock available for the device\n",
726 __func__);
727 ret = PTR_ERR(clk);
728 goto put_opp_table;
729 }
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530730
731 freq = clk_round_rate(clk, target_freq);
732 if ((long)freq <= 0)
733 freq = target_freq;
734
735 old_freq = clk_get_rate(clk);
736
737 /* Return early if nothing to do */
738 if (old_freq == freq) {
739 dev_dbg(dev, "%s: old/new frequencies (%lu Hz) are same, nothing to do\n",
740 __func__, freq);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530741 ret = 0;
742 goto put_opp_table;
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530743 }
744
Jisheng Zhang067b7ce2016-07-25 14:11:16 +0800745 old_opp = _find_freq_ceil(opp_table, &old_freq);
Arnd Bergmann4df27c92016-09-15 17:38:38 +0200746 if (IS_ERR(old_opp)) {
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530747 dev_err(dev, "%s: failed to find current OPP for freq %lu (%ld)\n",
748 __func__, old_freq, PTR_ERR(old_opp));
749 }
750
Jisheng Zhang067b7ce2016-07-25 14:11:16 +0800751 opp = _find_freq_ceil(opp_table, &freq);
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530752 if (IS_ERR(opp)) {
753 ret = PTR_ERR(opp);
754 dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n",
755 __func__, freq, ret);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530756 goto put_old_opp;
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530757 }
758
Viresh Kumar94735582016-12-01 16:28:20 +0530759 dev_dbg(dev, "%s: switching OPP: %lu Hz --> %lu Hz\n", __func__,
760 old_freq, freq);
Viresh Kumardfbe4672016-12-01 16:28:19 +0530761
Viresh Kumarca1b5d72018-06-14 10:03:26 +0530762 /* Scaling up? Configure required OPPs before frequency */
Viresh Kumarfaef0802019-03-12 10:27:18 +0530763 if (freq >= old_freq) {
Viresh Kumarca1b5d72018-06-14 10:03:26 +0530764 ret = _set_required_opps(dev, opp_table, opp);
765 if (ret)
766 goto put_opp;
767 }
768
Viresh Kumar7e5359932018-06-12 14:47:03 +0530769 if (opp_table->set_opp) {
770 ret = _set_opp_custom(opp_table, dev, old_freq, freq,
771 IS_ERR(old_opp) ? NULL : old_opp->supplies,
772 opp->supplies);
773 } else if (opp_table->regulators) {
Viresh Kumarc74b32f2017-05-23 09:32:10 +0530774 ret = _generic_set_opp_regulator(opp_table, dev, old_freq, freq,
775 IS_ERR(old_opp) ? NULL : old_opp->supplies,
776 opp->supplies);
777 } else {
Viresh Kumar7e5359932018-06-12 14:47:03 +0530778 /* Only frequency scaling */
Viresh Kumar285881b2019-01-31 13:53:25 +0530779 ret = _generic_set_opp_clk_only(dev, clk, freq);
Viresh Kumardfbe4672016-12-01 16:28:19 +0530780 }
781
Viresh Kumarca1b5d72018-06-14 10:03:26 +0530782 /* Scaling down? Configure required OPPs after frequency */
783 if (!ret && freq < old_freq) {
784 ret = _set_required_opps(dev, opp_table, opp);
785 if (ret)
786 dev_err(dev, "Failed to set required opps: %d\n", ret);
787 }
788
789put_opp:
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530790 dev_pm_opp_put(opp);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530791put_old_opp:
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530792 if (!IS_ERR(old_opp))
793 dev_pm_opp_put(old_opp);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530794put_opp_table:
Viresh Kumar5b650b32017-01-23 10:11:48 +0530795 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530796 return ret;
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530797}
798EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate);
799
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530800/* OPP-dev Helpers */
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530801static void _remove_opp_dev(struct opp_device *opp_dev,
802 struct opp_table *opp_table)
Viresh Kumar06441652015-07-29 16:23:04 +0530803{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530804 opp_debug_unregister(opp_dev, opp_table);
805 list_del(&opp_dev->node);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530806 kfree(opp_dev);
Viresh Kumar06441652015-07-29 16:23:04 +0530807}
808
Viresh Kumar283d55e2018-09-07 09:01:54 +0530809static struct opp_device *_add_opp_dev_unlocked(const struct device *dev,
810 struct opp_table *opp_table)
Viresh Kumar06441652015-07-29 16:23:04 +0530811{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530812 struct opp_device *opp_dev;
Viresh Kumar06441652015-07-29 16:23:04 +0530813
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530814 opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL);
815 if (!opp_dev)
Viresh Kumar06441652015-07-29 16:23:04 +0530816 return NULL;
817
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530818 /* Initialize opp-dev */
819 opp_dev->dev = dev;
Viresh Kumar3d255692018-08-03 07:05:21 +0530820
Viresh Kumar052c6f12017-01-23 10:11:49 +0530821 list_add(&opp_dev->node, &opp_table->dev_list);
Viresh Kumar06441652015-07-29 16:23:04 +0530822
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530823 /* Create debugfs entries for the opp_table */
Greg Kroah-Hartmana2dea4c2019-01-22 16:21:17 +0100824 opp_debug_register(opp_dev, opp_table);
Viresh Kumar283d55e2018-09-07 09:01:54 +0530825
826 return opp_dev;
827}
828
829struct opp_device *_add_opp_dev(const struct device *dev,
830 struct opp_table *opp_table)
831{
832 struct opp_device *opp_dev;
833
834 mutex_lock(&opp_table->lock);
835 opp_dev = _add_opp_dev_unlocked(dev, opp_table);
Viresh Kumar3d255692018-08-03 07:05:21 +0530836 mutex_unlock(&opp_table->lock);
Viresh Kumardeaa5142015-11-11 07:59:01 +0530837
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530838 return opp_dev;
Viresh Kumar06441652015-07-29 16:23:04 +0530839}
840
Viresh Kumareb7c87432018-09-05 16:17:14 +0530841static struct opp_table *_allocate_opp_table(struct device *dev, int index)
Viresh Kumar07cce742014-12-10 09:45:34 +0530842{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530843 struct opp_table *opp_table;
844 struct opp_device *opp_dev;
Viresh Kumard54974c2016-02-09 10:30:38 +0530845 int ret;
Viresh Kumar07cce742014-12-10 09:45:34 +0530846
847 /*
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530848 * Allocate a new OPP table. In the infrequent case where a new
Viresh Kumar07cce742014-12-10 09:45:34 +0530849 * device is needed to be added, we pay this penalty.
850 */
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530851 opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL);
852 if (!opp_table)
Viresh Kumar07cce742014-12-10 09:45:34 +0530853 return NULL;
854
Viresh Kumar3d255692018-08-03 07:05:21 +0530855 mutex_init(&opp_table->lock);
Viresh Kumar4f018bc2018-06-26 16:29:34 +0530856 mutex_init(&opp_table->genpd_virt_dev_lock);
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530857 INIT_LIST_HEAD(&opp_table->dev_list);
Viresh Kumar06441652015-07-29 16:23:04 +0530858
Viresh Kumar46f48ac2018-12-11 16:39:36 +0530859 /* Mark regulator count uninitialized */
860 opp_table->regulator_count = -1;
861
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530862 opp_dev = _add_opp_dev(dev, opp_table);
863 if (!opp_dev) {
864 kfree(opp_table);
Viresh Kumar06441652015-07-29 16:23:04 +0530865 return NULL;
866 }
867
Viresh Kumareb7c87432018-09-05 16:17:14 +0530868 _of_init_opp_table(opp_table, dev, index);
Viresh Kumar50f8cfb2016-02-09 10:30:37 +0530869
Viresh Kumard54974c2016-02-09 10:30:38 +0530870 /* Find clk for the device */
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530871 opp_table->clk = clk_get(dev, NULL);
872 if (IS_ERR(opp_table->clk)) {
873 ret = PTR_ERR(opp_table->clk);
Viresh Kumard54974c2016-02-09 10:30:38 +0530874 if (ret != -EPROBE_DEFER)
875 dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__,
876 ret);
877 }
878
Viresh Kumar052c6f12017-01-23 10:11:49 +0530879 BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head);
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530880 INIT_LIST_HEAD(&opp_table->opp_list);
Viresh Kumarf067a982017-01-23 10:11:42 +0530881 kref_init(&opp_table->kref);
Viresh Kumar07cce742014-12-10 09:45:34 +0530882
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530883 /* Secure the device table modification */
Viresh Kumar052c6f12017-01-23 10:11:49 +0530884 list_add(&opp_table->node, &opp_tables);
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530885 return opp_table;
Viresh Kumar07cce742014-12-10 09:45:34 +0530886}
887
Viresh Kumarf067a982017-01-23 10:11:42 +0530888void _get_opp_table_kref(struct opp_table *opp_table)
Viresh Kumar3bac42c2015-07-29 16:22:59 +0530889{
Viresh Kumarf067a982017-01-23 10:11:42 +0530890 kref_get(&opp_table->kref);
891}
892
Viresh Kumareb7c87432018-09-05 16:17:14 +0530893static struct opp_table *_opp_get_opp_table(struct device *dev, int index)
Viresh Kumarf067a982017-01-23 10:11:42 +0530894{
895 struct opp_table *opp_table;
896
897 /* Hold our table modification lock here */
898 mutex_lock(&opp_table_lock);
899
Viresh Kumar5b650b32017-01-23 10:11:48 +0530900 opp_table = _find_opp_table_unlocked(dev);
901 if (!IS_ERR(opp_table))
Viresh Kumarf067a982017-01-23 10:11:42 +0530902 goto unlock;
Viresh Kumarf067a982017-01-23 10:11:42 +0530903
Viresh Kumar283d55e2018-09-07 09:01:54 +0530904 opp_table = _managed_opp(dev, index);
905 if (opp_table) {
906 if (!_add_opp_dev_unlocked(dev, opp_table)) {
907 dev_pm_opp_put_opp_table(opp_table);
908 opp_table = NULL;
909 }
910 goto unlock;
911 }
912
Viresh Kumareb7c87432018-09-05 16:17:14 +0530913 opp_table = _allocate_opp_table(dev, index);
Viresh Kumarf067a982017-01-23 10:11:42 +0530914
915unlock:
916 mutex_unlock(&opp_table_lock);
917
918 return opp_table;
919}
Viresh Kumareb7c87432018-09-05 16:17:14 +0530920
921struct opp_table *dev_pm_opp_get_opp_table(struct device *dev)
922{
923 return _opp_get_opp_table(dev, 0);
924}
Viresh Kumarf067a982017-01-23 10:11:42 +0530925EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table);
926
Viresh Kumareb7c87432018-09-05 16:17:14 +0530927struct opp_table *dev_pm_opp_get_opp_table_indexed(struct device *dev,
928 int index)
929{
930 return _opp_get_opp_table(dev, index);
931}
932
Viresh Kumarb83c1892017-01-23 10:11:45 +0530933static void _opp_table_kref_release(struct kref *kref)
Viresh Kumarf067a982017-01-23 10:11:42 +0530934{
935 struct opp_table *opp_table = container_of(kref, struct opp_table, kref);
Viresh Kumarcdd6ed92018-09-12 12:35:19 +0530936 struct opp_device *opp_dev, *temp;
Viresh Kumar06441652015-07-29 16:23:04 +0530937
Viresh Kumar5d6d1062018-06-07 14:50:43 +0530938 _of_clear_opp_table(opp_table);
939
Viresh Kumard54974c2016-02-09 10:30:38 +0530940 /* Release clk */
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530941 if (!IS_ERR(opp_table->clk))
942 clk_put(opp_table->clk);
Viresh Kumard54974c2016-02-09 10:30:38 +0530943
Viresh Kumarcdd6ed92018-09-12 12:35:19 +0530944 WARN_ON(!list_empty(&opp_table->opp_list));
Viresh Kumar06441652015-07-29 16:23:04 +0530945
Viresh Kumarcdd6ed92018-09-12 12:35:19 +0530946 list_for_each_entry_safe(opp_dev, temp, &opp_table->dev_list, node) {
947 /*
948 * The OPP table is getting removed, drop the performance state
949 * constraints.
950 */
951 if (opp_table->genpd_performance_state)
952 dev_pm_genpd_set_performance_state((struct device *)(opp_dev->dev), 0);
Viresh Kumar06441652015-07-29 16:23:04 +0530953
Viresh Kumarcdd6ed92018-09-12 12:35:19 +0530954 _remove_opp_dev(opp_dev, opp_table);
955 }
Viresh Kumar06441652015-07-29 16:23:04 +0530956
Viresh Kumar4f018bc2018-06-26 16:29:34 +0530957 mutex_destroy(&opp_table->genpd_virt_dev_lock);
Viresh Kumar37a73ec2017-01-23 10:11:41 +0530958 mutex_destroy(&opp_table->lock);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530959 list_del(&opp_table->node);
960 kfree(opp_table);
Viresh Kumar3bac42c2015-07-29 16:22:59 +0530961
Viresh Kumarf067a982017-01-23 10:11:42 +0530962 mutex_unlock(&opp_table_lock);
963}
964
Viresh Kumard0e8ae62018-09-11 11:14:11 +0530965void _opp_remove_all_static(struct opp_table *opp_table)
966{
967 struct dev_pm_opp *opp, *tmp;
968
969 list_for_each_entry_safe(opp, tmp, &opp_table->opp_list, node) {
970 if (!opp->dynamic)
971 dev_pm_opp_put(opp);
972 }
973
974 opp_table->parsed_static_opps = false;
975}
976
977static void _opp_table_list_kref_release(struct kref *kref)
978{
979 struct opp_table *opp_table = container_of(kref, struct opp_table,
980 list_kref);
981
982 _opp_remove_all_static(opp_table);
983 mutex_unlock(&opp_table_lock);
984}
985
986void _put_opp_list_kref(struct opp_table *opp_table)
987{
988 kref_put_mutex(&opp_table->list_kref, _opp_table_list_kref_release,
989 &opp_table_lock);
990}
991
Viresh Kumarf067a982017-01-23 10:11:42 +0530992void dev_pm_opp_put_opp_table(struct opp_table *opp_table)
993{
994 kref_put_mutex(&opp_table->kref, _opp_table_kref_release,
995 &opp_table_lock);
996}
997EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table);
998
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +0530999void _opp_free(struct dev_pm_opp *opp)
Viresh Kumar969fceb2017-01-02 14:40:59 +05301000{
1001 kfree(opp);
Viresh Kumar969fceb2017-01-02 14:40:59 +05301002}
1003
Viresh Kumar1690d8b2019-01-04 15:14:33 +05301004static void _opp_kref_release(struct dev_pm_opp *opp,
1005 struct opp_table *opp_table)
Viresh Kumar129eec52014-11-27 08:54:06 +05301006{
1007 /*
1008 * Notify the changes in the availability of the operable
1009 * frequency/voltage list.
1010 */
Viresh Kumar052c6f12017-01-23 10:11:49 +05301011 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_REMOVE, opp);
Viresh Kumarda544b62018-06-07 14:50:43 +05301012 _of_opp_free_required_opps(opp_table, opp);
Viresh Kumardeaa5142015-11-11 07:59:01 +05301013 opp_debug_remove_one(opp);
Viresh Kumar052c6f12017-01-23 10:11:49 +05301014 list_del(&opp->node);
1015 kfree(opp);
Viresh Kumar1690d8b2019-01-04 15:14:33 +05301016}
Viresh Kumar129eec52014-11-27 08:54:06 +05301017
Viresh Kumar1690d8b2019-01-04 15:14:33 +05301018static void _opp_kref_release_unlocked(struct kref *kref)
1019{
1020 struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref);
1021 struct opp_table *opp_table = opp->opp_table;
1022
1023 _opp_kref_release(opp, opp_table);
1024}
1025
1026static void _opp_kref_release_locked(struct kref *kref)
1027{
1028 struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref);
1029 struct opp_table *opp_table = opp->opp_table;
1030
1031 _opp_kref_release(opp, opp_table);
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301032 mutex_unlock(&opp_table->lock);
Viresh Kumar129eec52014-11-27 08:54:06 +05301033}
1034
Viresh Kumara88bd2a2017-11-29 15:18:36 +05301035void dev_pm_opp_get(struct dev_pm_opp *opp)
Viresh Kumar8a31d9d92017-01-23 10:11:47 +05301036{
1037 kref_get(&opp->kref);
1038}
1039
Viresh Kumar70347642017-01-23 10:11:46 +05301040void dev_pm_opp_put(struct dev_pm_opp *opp)
1041{
Viresh Kumar1690d8b2019-01-04 15:14:33 +05301042 kref_put_mutex(&opp->kref, _opp_kref_release_locked,
1043 &opp->opp_table->lock);
Viresh Kumar70347642017-01-23 10:11:46 +05301044}
1045EXPORT_SYMBOL_GPL(dev_pm_opp_put);
1046
Viresh Kumar1690d8b2019-01-04 15:14:33 +05301047static void dev_pm_opp_put_unlocked(struct dev_pm_opp *opp)
1048{
1049 kref_put(&opp->kref, _opp_kref_release_unlocked);
1050}
1051
Viresh Kumar129eec52014-11-27 08:54:06 +05301052/**
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301053 * dev_pm_opp_remove() - Remove an OPP from OPP table
Viresh Kumar129eec52014-11-27 08:54:06 +05301054 * @dev: device for which we do this operation
1055 * @freq: OPP to remove with matching 'freq'
1056 *
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301057 * This function removes an opp from the opp table.
Viresh Kumar129eec52014-11-27 08:54:06 +05301058 */
1059void dev_pm_opp_remove(struct device *dev, unsigned long freq)
1060{
1061 struct dev_pm_opp *opp;
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301062 struct opp_table *opp_table;
Viresh Kumar129eec52014-11-27 08:54:06 +05301063 bool found = false;
1064
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301065 opp_table = _find_opp_table(dev);
1066 if (IS_ERR(opp_table))
Viresh Kumar5b650b32017-01-23 10:11:48 +05301067 return;
Viresh Kumar129eec52014-11-27 08:54:06 +05301068
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301069 mutex_lock(&opp_table->lock);
1070
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301071 list_for_each_entry(opp, &opp_table->opp_list, node) {
Viresh Kumar129eec52014-11-27 08:54:06 +05301072 if (opp->rate == freq) {
1073 found = true;
1074 break;
1075 }
1076 }
1077
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301078 mutex_unlock(&opp_table->lock);
1079
Viresh Kumar5b650b32017-01-23 10:11:48 +05301080 if (found) {
1081 dev_pm_opp_put(opp);
Viresh Kumar0ad8c622018-08-02 14:23:09 +05301082
1083 /* Drop the reference taken by dev_pm_opp_add() */
1084 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar5b650b32017-01-23 10:11:48 +05301085 } else {
Viresh Kumar129eec52014-11-27 08:54:06 +05301086 dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
1087 __func__, freq);
Viresh Kumar129eec52014-11-27 08:54:06 +05301088 }
1089
Viresh Kumar0ad8c622018-08-02 14:23:09 +05301090 /* Drop the reference taken by _find_opp_table() */
Viresh Kumar5b650b32017-01-23 10:11:48 +05301091 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar129eec52014-11-27 08:54:06 +05301092}
1093EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
1094
Viresh Kumar1690d8b2019-01-04 15:14:33 +05301095/**
1096 * dev_pm_opp_remove_all_dynamic() - Remove all dynamically created OPPs
1097 * @dev: device for which we do this operation
1098 *
1099 * This function removes all dynamically created OPPs from the opp table.
1100 */
1101void dev_pm_opp_remove_all_dynamic(struct device *dev)
1102{
1103 struct opp_table *opp_table;
1104 struct dev_pm_opp *opp, *temp;
1105 int count = 0;
1106
1107 opp_table = _find_opp_table(dev);
1108 if (IS_ERR(opp_table))
1109 return;
1110
1111 mutex_lock(&opp_table->lock);
1112 list_for_each_entry_safe(opp, temp, &opp_table->opp_list, node) {
1113 if (opp->dynamic) {
1114 dev_pm_opp_put_unlocked(opp);
1115 count++;
1116 }
1117 }
1118 mutex_unlock(&opp_table->lock);
1119
1120 /* Drop the references taken by dev_pm_opp_add() */
1121 while (count--)
1122 dev_pm_opp_put_opp_table(opp_table);
1123
1124 /* Drop the reference taken by _find_opp_table() */
1125 dev_pm_opp_put_opp_table(opp_table);
1126}
1127EXPORT_SYMBOL_GPL(dev_pm_opp_remove_all_dynamic);
1128
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301129struct dev_pm_opp *_opp_allocate(struct opp_table *table)
Viresh Kumar23dacf62015-07-29 16:23:01 +05301130{
1131 struct dev_pm_opp *opp;
Viresh Kumardfbe4672016-12-01 16:28:19 +05301132 int count, supply_size;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301133
Viresh Kumardfbe4672016-12-01 16:28:19 +05301134 /* Allocate space for at least one supply */
Viresh Kumar46f48ac2018-12-11 16:39:36 +05301135 count = table->regulator_count > 0 ? table->regulator_count : 1;
Viresh Kumardfbe4672016-12-01 16:28:19 +05301136 supply_size = sizeof(*opp->supplies) * count;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301137
Viresh Kumardfbe4672016-12-01 16:28:19 +05301138 /* allocate new OPP node and supplies structures */
1139 opp = kzalloc(sizeof(*opp) + supply_size, GFP_KERNEL);
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301140 if (!opp)
Viresh Kumar23dacf62015-07-29 16:23:01 +05301141 return NULL;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301142
Viresh Kumardfbe4672016-12-01 16:28:19 +05301143 /* Put the supplies at the end of the OPP structure as an empty array */
1144 opp->supplies = (struct dev_pm_opp_supply *)(opp + 1);
1145 INIT_LIST_HEAD(&opp->node);
1146
Viresh Kumar23dacf62015-07-29 16:23:01 +05301147 return opp;
1148}
1149
Viresh Kumar7d34d562016-02-09 10:30:34 +05301150static bool _opp_supported_by_regulators(struct dev_pm_opp *opp,
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301151 struct opp_table *opp_table)
Viresh Kumar7d34d562016-02-09 10:30:34 +05301152{
Viresh Kumardfbe4672016-12-01 16:28:19 +05301153 struct regulator *reg;
1154 int i;
Viresh Kumar7d34d562016-02-09 10:30:34 +05301155
Viresh Kumar90e35772018-12-11 16:32:47 +05301156 if (!opp_table->regulators)
1157 return true;
1158
Viresh Kumardfbe4672016-12-01 16:28:19 +05301159 for (i = 0; i < opp_table->regulator_count; i++) {
1160 reg = opp_table->regulators[i];
1161
1162 if (!regulator_is_supported_voltage(reg,
1163 opp->supplies[i].u_volt_min,
1164 opp->supplies[i].u_volt_max)) {
1165 pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n",
1166 __func__, opp->supplies[i].u_volt_min,
1167 opp->supplies[i].u_volt_max);
1168 return false;
1169 }
Viresh Kumar7d34d562016-02-09 10:30:34 +05301170 }
1171
1172 return true;
1173}
1174
Viresh Kumara1e8c132018-04-06 14:35:45 +05301175static int _opp_is_duplicate(struct device *dev, struct dev_pm_opp *new_opp,
1176 struct opp_table *opp_table,
1177 struct list_head **head)
1178{
1179 struct dev_pm_opp *opp;
1180
1181 /*
1182 * Insert new OPP in order of increasing frequency and discard if
1183 * already present.
1184 *
1185 * Need to use &opp_table->opp_list in the condition part of the 'for'
1186 * loop, don't replace it with head otherwise it will become an infinite
1187 * loop.
1188 */
1189 list_for_each_entry(opp, &opp_table->opp_list, node) {
1190 if (new_opp->rate > opp->rate) {
1191 *head = &opp->node;
1192 continue;
1193 }
1194
1195 if (new_opp->rate < opp->rate)
1196 return 0;
1197
1198 /* Duplicate OPPs */
1199 dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
1200 __func__, opp->rate, opp->supplies[0].u_volt,
1201 opp->available, new_opp->rate,
1202 new_opp->supplies[0].u_volt, new_opp->available);
1203
1204 /* Should we compare voltages for all regulators here ? */
1205 return opp->available &&
1206 new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST;
1207 }
1208
1209 return 0;
1210}
1211
Viresh Kumar7f8538e2017-01-02 14:40:55 +05301212/*
1213 * Returns:
1214 * 0: On success. And appropriate error message for duplicate OPPs.
1215 * -EBUSY: For OPP with same freq/volt and is available. The callers of
1216 * _opp_add() must return 0 if they receive -EBUSY from it. This is to make
1217 * sure we don't print error messages unnecessarily if different parts of
1218 * kernel try to initialize the OPP table.
1219 * -EEXIST: For OPP with same freq but different volt or is unavailable. This
1220 * should be considered an error by the callers of _opp_add().
1221 */
Viresh Kumarf47b72a2016-05-05 16:20:33 +05301222int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
Viresh Kumara1e8c132018-04-06 14:35:45 +05301223 struct opp_table *opp_table, bool rate_not_available)
Viresh Kumar23dacf62015-07-29 16:23:01 +05301224{
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301225 struct list_head *head;
Viresh Kumardeaa5142015-11-11 07:59:01 +05301226 int ret;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301227
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301228 mutex_lock(&opp_table->lock);
1229 head = &opp_table->opp_list;
1230
Viresh Kumara1e8c132018-04-06 14:35:45 +05301231 if (likely(!rate_not_available)) {
1232 ret = _opp_is_duplicate(dev, new_opp, opp_table, &head);
1233 if (ret) {
1234 mutex_unlock(&opp_table->lock);
1235 return ret;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301236 }
Viresh Kumar23dacf62015-07-29 16:23:01 +05301237 }
1238
Viresh Kumar052c6f12017-01-23 10:11:49 +05301239 list_add(&new_opp->node, head);
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301240 mutex_unlock(&opp_table->lock);
1241
1242 new_opp->opp_table = opp_table;
Viresh Kumar70347642017-01-23 10:11:46 +05301243 kref_init(&new_opp->kref);
Viresh Kumar23dacf62015-07-29 16:23:01 +05301244
Greg Kroah-Hartmana2dea4c2019-01-22 16:21:17 +01001245 opp_debug_create_one(new_opp, opp_table);
Viresh Kumardeaa5142015-11-11 07:59:01 +05301246
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301247 if (!_opp_supported_by_regulators(new_opp, opp_table)) {
Viresh Kumar7d34d562016-02-09 10:30:34 +05301248 new_opp->available = false;
1249 dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n",
1250 __func__, new_opp->rate);
1251 }
1252
Viresh Kumar23dacf62015-07-29 16:23:01 +05301253 return 0;
1254}
1255
Viresh Kumar737002b2015-07-29 16:22:58 +05301256/**
Viresh Kumarb64b9c3f2015-10-15 21:42:44 +05301257 * _opp_add_v1() - Allocate a OPP based on v1 bindings.
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301258 * @opp_table: OPP table
Nishanth Menone1f60b22010-10-13 00:13:10 +02001259 * @dev: device for which we do this operation
1260 * @freq: Frequency in Hz for this OPP
1261 * @u_volt: Voltage in uVolts for this OPP
1262 * @dynamic: Dynamically added OPPs.
1263 *
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301264 * This function adds an opp definition to the opp table and returns status.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001265 * The opp is made available by default and it can be controlled using
1266 * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
1267 *
Viresh Kumar8f8d37b2015-09-04 13:47:24 +05301268 * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table
1269 * and freed by dev_pm_opp_of_remove_table.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001270 *
Nishanth Menone1f60b22010-10-13 00:13:10 +02001271 * Return:
1272 * 0 On success OR
1273 * Duplicate OPPs (both freq and volt are same) and opp->available
1274 * -EEXIST Freq are same and volt are different OR
1275 * Duplicate OPPs (both freq and volt are same) and !opp->available
1276 * -ENOMEM Memory allocation failure
1277 */
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301278int _opp_add_v1(struct opp_table *opp_table, struct device *dev,
1279 unsigned long freq, long u_volt, bool dynamic)
Nishanth Menone1f60b22010-10-13 00:13:10 +02001280{
Viresh Kumar23dacf62015-07-29 16:23:01 +05301281 struct dev_pm_opp *new_opp;
Viresh Kumar50f8cfb2016-02-09 10:30:37 +05301282 unsigned long tol;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001283 int ret;
1284
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301285 new_opp = _opp_allocate(opp_table);
1286 if (!new_opp)
1287 return -ENOMEM;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301288
Nishanth Menone1f60b22010-10-13 00:13:10 +02001289 /* populate the opp table */
1290 new_opp->rate = freq;
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301291 tol = u_volt * opp_table->voltage_tolerance_v1 / 100;
Viresh Kumardfbe4672016-12-01 16:28:19 +05301292 new_opp->supplies[0].u_volt = u_volt;
1293 new_opp->supplies[0].u_volt_min = u_volt - tol;
1294 new_opp->supplies[0].u_volt_max = u_volt + tol;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001295 new_opp->available = true;
Viresh Kumaraa5f2f82015-07-29 16:23:00 +05301296 new_opp->dynamic = dynamic;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301297
Viresh Kumara1e8c132018-04-06 14:35:45 +05301298 ret = _opp_add(dev, new_opp, opp_table, false);
Viresh Kumar7f8538e2017-01-02 14:40:55 +05301299 if (ret) {
1300 /* Don't return error for duplicate OPPs */
1301 if (ret == -EBUSY)
1302 ret = 0;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301303 goto free_opp;
Viresh Kumar7f8538e2017-01-02 14:40:55 +05301304 }
Viresh Kumar23dacf62015-07-29 16:23:01 +05301305
Nishanth Menone1f60b22010-10-13 00:13:10 +02001306 /*
1307 * Notify the changes in the availability of the operable
1308 * frequency/voltage list.
1309 */
Viresh Kumar052c6f12017-01-23 10:11:49 +05301310 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001311 return 0;
1312
1313free_opp:
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301314 _opp_free(new_opp);
1315
Nishanth Menone1f60b22010-10-13 00:13:10 +02001316 return ret;
1317}
Viresh Kumar383934092014-11-25 16:04:18 +05301318
Viresh Kumar27465902015-07-29 16:23:02 +05301319/**
Viresh Kumar7de36b02015-12-09 08:01:46 +05301320 * dev_pm_opp_set_supported_hw() - Set supported platforms
1321 * @dev: Device for which supported-hw has to be set.
1322 * @versions: Array of hierarchy of versions to match.
1323 * @count: Number of elements in the array.
1324 *
1325 * This is required only for the V2 bindings, and it enables a platform to
1326 * specify the hierarchy of versions it supports. OPP layer will then enable
1327 * OPPs, which are available for those versions, based on its 'opp-supported-hw'
1328 * property.
Viresh Kumar7de36b02015-12-09 08:01:46 +05301329 */
Viresh Kumarfa301842017-01-23 10:11:43 +05301330struct opp_table *dev_pm_opp_set_supported_hw(struct device *dev,
1331 const u32 *versions, unsigned int count)
Viresh Kumar7de36b02015-12-09 08:01:46 +05301332{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301333 struct opp_table *opp_table;
Viresh Kumar7de36b02015-12-09 08:01:46 +05301334
Viresh Kumarfa301842017-01-23 10:11:43 +05301335 opp_table = dev_pm_opp_get_opp_table(dev);
1336 if (!opp_table)
1337 return ERR_PTR(-ENOMEM);
Viresh Kumar7de36b02015-12-09 08:01:46 +05301338
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301339 /* Make sure there are no concurrent readers while updating opp_table */
1340 WARN_ON(!list_empty(&opp_table->opp_list));
Viresh Kumar7de36b02015-12-09 08:01:46 +05301341
Viresh Kumar25419de2018-05-22 16:38:08 +05301342 /* Another CPU that shares the OPP table has set the property ? */
1343 if (opp_table->supported_hw)
1344 return opp_table;
Viresh Kumar7de36b02015-12-09 08:01:46 +05301345
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301346 opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions),
Viresh Kumar7de36b02015-12-09 08:01:46 +05301347 GFP_KERNEL);
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301348 if (!opp_table->supported_hw) {
Viresh Kumar25419de2018-05-22 16:38:08 +05301349 dev_pm_opp_put_opp_table(opp_table);
1350 return ERR_PTR(-ENOMEM);
Viresh Kumar7de36b02015-12-09 08:01:46 +05301351 }
1352
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301353 opp_table->supported_hw_count = count;
Viresh Kumarfa301842017-01-23 10:11:43 +05301354
1355 return opp_table;
Viresh Kumar7de36b02015-12-09 08:01:46 +05301356}
1357EXPORT_SYMBOL_GPL(dev_pm_opp_set_supported_hw);
1358
1359/**
1360 * dev_pm_opp_put_supported_hw() - Releases resources blocked for supported hw
Viresh Kumarfa301842017-01-23 10:11:43 +05301361 * @opp_table: OPP table returned by dev_pm_opp_set_supported_hw().
Viresh Kumar7de36b02015-12-09 08:01:46 +05301362 *
1363 * This is required only for the V2 bindings, and is called for a matching
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301364 * dev_pm_opp_set_supported_hw(). Until this is called, the opp_table structure
Viresh Kumar7de36b02015-12-09 08:01:46 +05301365 * will not be freed.
Viresh Kumar7de36b02015-12-09 08:01:46 +05301366 */
Viresh Kumarfa301842017-01-23 10:11:43 +05301367void dev_pm_opp_put_supported_hw(struct opp_table *opp_table)
Viresh Kumar7de36b02015-12-09 08:01:46 +05301368{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301369 /* Make sure there are no concurrent readers while updating opp_table */
1370 WARN_ON(!list_empty(&opp_table->opp_list));
Viresh Kumar7de36b02015-12-09 08:01:46 +05301371
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301372 kfree(opp_table->supported_hw);
1373 opp_table->supported_hw = NULL;
1374 opp_table->supported_hw_count = 0;
Viresh Kumar7de36b02015-12-09 08:01:46 +05301375
Viresh Kumarfa301842017-01-23 10:11:43 +05301376 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar7de36b02015-12-09 08:01:46 +05301377}
1378EXPORT_SYMBOL_GPL(dev_pm_opp_put_supported_hw);
1379
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301380/**
1381 * dev_pm_opp_set_prop_name() - Set prop-extn name
Viresh Kumara5da6442016-02-16 14:17:52 +05301382 * @dev: Device for which the prop-name has to be set.
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301383 * @name: name to postfix to properties.
1384 *
1385 * This is required only for the V2 bindings, and it enables a platform to
1386 * specify the extn to be used for certain property names. The properties to
1387 * which the extension will apply are opp-microvolt and opp-microamp. OPP core
1388 * should postfix the property name with -<name> while looking for them.
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301389 */
Viresh Kumarfa301842017-01-23 10:11:43 +05301390struct opp_table *dev_pm_opp_set_prop_name(struct device *dev, const char *name)
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301391{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301392 struct opp_table *opp_table;
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301393
Viresh Kumarfa301842017-01-23 10:11:43 +05301394 opp_table = dev_pm_opp_get_opp_table(dev);
1395 if (!opp_table)
1396 return ERR_PTR(-ENOMEM);
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301397
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301398 /* Make sure there are no concurrent readers while updating opp_table */
1399 WARN_ON(!list_empty(&opp_table->opp_list));
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301400
Viresh Kumar878ec1a2018-05-22 16:38:08 +05301401 /* Another CPU that shares the OPP table has set the property ? */
1402 if (opp_table->prop_name)
1403 return opp_table;
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301404
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301405 opp_table->prop_name = kstrdup(name, GFP_KERNEL);
1406 if (!opp_table->prop_name) {
Viresh Kumar878ec1a2018-05-22 16:38:08 +05301407 dev_pm_opp_put_opp_table(opp_table);
1408 return ERR_PTR(-ENOMEM);
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301409 }
1410
Viresh Kumarfa301842017-01-23 10:11:43 +05301411 return opp_table;
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301412}
1413EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name);
1414
1415/**
1416 * dev_pm_opp_put_prop_name() - Releases resources blocked for prop-name
Viresh Kumarfa301842017-01-23 10:11:43 +05301417 * @opp_table: OPP table returned by dev_pm_opp_set_prop_name().
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301418 *
1419 * This is required only for the V2 bindings, and is called for a matching
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301420 * dev_pm_opp_set_prop_name(). Until this is called, the opp_table structure
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301421 * will not be freed.
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301422 */
Viresh Kumarfa301842017-01-23 10:11:43 +05301423void dev_pm_opp_put_prop_name(struct opp_table *opp_table)
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301424{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301425 /* Make sure there are no concurrent readers while updating opp_table */
1426 WARN_ON(!list_empty(&opp_table->opp_list));
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301427
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301428 kfree(opp_table->prop_name);
1429 opp_table->prop_name = NULL;
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301430
Viresh Kumarfa301842017-01-23 10:11:43 +05301431 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301432}
1433EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name);
1434
Viresh Kumar94735582016-12-01 16:28:20 +05301435static int _allocate_set_opp_data(struct opp_table *opp_table)
1436{
1437 struct dev_pm_set_opp_data *data;
1438 int len, count = opp_table->regulator_count;
1439
Viresh Kumar90e35772018-12-11 16:32:47 +05301440 if (WARN_ON(!opp_table->regulators))
Viresh Kumar94735582016-12-01 16:28:20 +05301441 return -EINVAL;
1442
1443 /* space for set_opp_data */
1444 len = sizeof(*data);
1445
1446 /* space for old_opp.supplies and new_opp.supplies */
1447 len += 2 * sizeof(struct dev_pm_opp_supply) * count;
1448
1449 data = kzalloc(len, GFP_KERNEL);
1450 if (!data)
1451 return -ENOMEM;
1452
1453 data->old_opp.supplies = (void *)(data + 1);
1454 data->new_opp.supplies = data->old_opp.supplies + count;
1455
1456 opp_table->set_opp_data = data;
1457
1458 return 0;
1459}
1460
1461static void _free_set_opp_data(struct opp_table *opp_table)
1462{
1463 kfree(opp_table->set_opp_data);
1464 opp_table->set_opp_data = NULL;
1465}
1466
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301467/**
Viresh Kumardfbe4672016-12-01 16:28:19 +05301468 * dev_pm_opp_set_regulators() - Set regulator names for the device
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301469 * @dev: Device for which regulator name is being set.
Viresh Kumardfbe4672016-12-01 16:28:19 +05301470 * @names: Array of pointers to the names of the regulator.
1471 * @count: Number of regulators.
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301472 *
1473 * In order to support OPP switching, OPP layer needs to know the name of the
Viresh Kumardfbe4672016-12-01 16:28:19 +05301474 * device's regulators, as the core would be required to switch voltages as
1475 * well.
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301476 *
1477 * This must be called before any OPPs are initialized for the device.
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301478 */
Viresh Kumardfbe4672016-12-01 16:28:19 +05301479struct opp_table *dev_pm_opp_set_regulators(struct device *dev,
1480 const char * const names[],
1481 unsigned int count)
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301482{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301483 struct opp_table *opp_table;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301484 struct regulator *reg;
Viresh Kumardfbe4672016-12-01 16:28:19 +05301485 int ret, i;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301486
Viresh Kumarfa301842017-01-23 10:11:43 +05301487 opp_table = dev_pm_opp_get_opp_table(dev);
1488 if (!opp_table)
1489 return ERR_PTR(-ENOMEM);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301490
1491 /* This should be called before OPPs are initialized */
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301492 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301493 ret = -EBUSY;
1494 goto err;
1495 }
1496
Viresh Kumar779b7832018-05-22 16:38:08 +05301497 /* Another CPU that shares the OPP table has set the regulators ? */
1498 if (opp_table->regulators)
1499 return opp_table;
Viresh Kumardfbe4672016-12-01 16:28:19 +05301500
1501 opp_table->regulators = kmalloc_array(count,
1502 sizeof(*opp_table->regulators),
1503 GFP_KERNEL);
1504 if (!opp_table->regulators) {
1505 ret = -ENOMEM;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301506 goto err;
1507 }
1508
Viresh Kumardfbe4672016-12-01 16:28:19 +05301509 for (i = 0; i < count; i++) {
1510 reg = regulator_get_optional(dev, names[i]);
1511 if (IS_ERR(reg)) {
1512 ret = PTR_ERR(reg);
1513 if (ret != -EPROBE_DEFER)
1514 dev_err(dev, "%s: no regulator (%s) found: %d\n",
1515 __func__, names[i], ret);
1516 goto free_regulators;
1517 }
1518
1519 opp_table->regulators[i] = reg;
1520 }
1521
1522 opp_table->regulator_count = count;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301523
Viresh Kumar94735582016-12-01 16:28:20 +05301524 /* Allocate block only once to pass to set_opp() routines */
1525 ret = _allocate_set_opp_data(opp_table);
1526 if (ret)
1527 goto free_regulators;
1528
Stephen Boyd91291d92016-11-30 16:21:25 +05301529 return opp_table;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301530
Viresh Kumardfbe4672016-12-01 16:28:19 +05301531free_regulators:
1532 while (i != 0)
1533 regulator_put(opp_table->regulators[--i]);
1534
1535 kfree(opp_table->regulators);
1536 opp_table->regulators = NULL;
Viresh Kumar46f48ac2018-12-11 16:39:36 +05301537 opp_table->regulator_count = -1;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301538err:
Viresh Kumarfa301842017-01-23 10:11:43 +05301539 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301540
Stephen Boyd91291d92016-11-30 16:21:25 +05301541 return ERR_PTR(ret);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301542}
Viresh Kumardfbe4672016-12-01 16:28:19 +05301543EXPORT_SYMBOL_GPL(dev_pm_opp_set_regulators);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301544
1545/**
Viresh Kumardfbe4672016-12-01 16:28:19 +05301546 * dev_pm_opp_put_regulators() - Releases resources blocked for regulator
1547 * @opp_table: OPP table returned from dev_pm_opp_set_regulators().
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301548 */
Viresh Kumardfbe4672016-12-01 16:28:19 +05301549void dev_pm_opp_put_regulators(struct opp_table *opp_table)
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301550{
Viresh Kumardfbe4672016-12-01 16:28:19 +05301551 int i;
1552
Viresh Kumar779b7832018-05-22 16:38:08 +05301553 if (!opp_table->regulators)
1554 goto put_opp_table;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301555
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301556 /* Make sure there are no concurrent readers while updating opp_table */
1557 WARN_ON(!list_empty(&opp_table->opp_list));
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301558
Viresh Kumardfbe4672016-12-01 16:28:19 +05301559 for (i = opp_table->regulator_count - 1; i >= 0; i--)
1560 regulator_put(opp_table->regulators[i]);
1561
Viresh Kumar94735582016-12-01 16:28:20 +05301562 _free_set_opp_data(opp_table);
1563
Viresh Kumardfbe4672016-12-01 16:28:19 +05301564 kfree(opp_table->regulators);
1565 opp_table->regulators = NULL;
Viresh Kumar46f48ac2018-12-11 16:39:36 +05301566 opp_table->regulator_count = -1;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301567
Viresh Kumar779b7832018-05-22 16:38:08 +05301568put_opp_table:
Viresh Kumarfa301842017-01-23 10:11:43 +05301569 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301570}
Viresh Kumardfbe4672016-12-01 16:28:19 +05301571EXPORT_SYMBOL_GPL(dev_pm_opp_put_regulators);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301572
Viresh Kumar383934092014-11-25 16:04:18 +05301573/**
Viresh Kumar829a4e82017-06-21 10:29:13 +05301574 * dev_pm_opp_set_clkname() - Set clk name for the device
1575 * @dev: Device for which clk name is being set.
1576 * @name: Clk name.
1577 *
1578 * In order to support OPP switching, OPP layer needs to get pointer to the
1579 * clock for the device. Simple cases work fine without using this routine (i.e.
1580 * by passing connection-id as NULL), but for a device with multiple clocks
1581 * available, the OPP core needs to know the exact name of the clk to use.
1582 *
1583 * This must be called before any OPPs are initialized for the device.
1584 */
1585struct opp_table *dev_pm_opp_set_clkname(struct device *dev, const char *name)
1586{
1587 struct opp_table *opp_table;
1588 int ret;
1589
1590 opp_table = dev_pm_opp_get_opp_table(dev);
1591 if (!opp_table)
1592 return ERR_PTR(-ENOMEM);
1593
1594 /* This should be called before OPPs are initialized */
1595 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
1596 ret = -EBUSY;
1597 goto err;
1598 }
1599
1600 /* Already have default clk set, free it */
1601 if (!IS_ERR(opp_table->clk))
1602 clk_put(opp_table->clk);
1603
1604 /* Find clk for the device */
1605 opp_table->clk = clk_get(dev, name);
1606 if (IS_ERR(opp_table->clk)) {
1607 ret = PTR_ERR(opp_table->clk);
1608 if (ret != -EPROBE_DEFER) {
1609 dev_err(dev, "%s: Couldn't find clock: %d\n", __func__,
1610 ret);
1611 }
1612 goto err;
1613 }
1614
1615 return opp_table;
1616
1617err:
1618 dev_pm_opp_put_opp_table(opp_table);
1619
1620 return ERR_PTR(ret);
1621}
1622EXPORT_SYMBOL_GPL(dev_pm_opp_set_clkname);
1623
1624/**
1625 * dev_pm_opp_put_clkname() - Releases resources blocked for clk.
1626 * @opp_table: OPP table returned from dev_pm_opp_set_clkname().
1627 */
1628void dev_pm_opp_put_clkname(struct opp_table *opp_table)
1629{
1630 /* Make sure there are no concurrent readers while updating opp_table */
1631 WARN_ON(!list_empty(&opp_table->opp_list));
1632
1633 clk_put(opp_table->clk);
1634 opp_table->clk = ERR_PTR(-EINVAL);
1635
1636 dev_pm_opp_put_opp_table(opp_table);
1637}
1638EXPORT_SYMBOL_GPL(dev_pm_opp_put_clkname);
1639
1640/**
Viresh Kumar4dab1602016-12-01 16:28:21 +05301641 * dev_pm_opp_register_set_opp_helper() - Register custom set OPP helper
1642 * @dev: Device for which the helper is getting registered.
1643 * @set_opp: Custom set OPP helper.
1644 *
1645 * This is useful to support complex platforms (like platforms with multiple
1646 * regulators per device), instead of the generic OPP set rate helper.
1647 *
1648 * This must be called before any OPPs are initialized for the device.
Viresh Kumar4dab1602016-12-01 16:28:21 +05301649 */
Viresh Kumarfa301842017-01-23 10:11:43 +05301650struct opp_table *dev_pm_opp_register_set_opp_helper(struct device *dev,
Viresh Kumar4dab1602016-12-01 16:28:21 +05301651 int (*set_opp)(struct dev_pm_set_opp_data *data))
1652{
1653 struct opp_table *opp_table;
Viresh Kumar4dab1602016-12-01 16:28:21 +05301654
1655 if (!set_opp)
Viresh Kumarfa301842017-01-23 10:11:43 +05301656 return ERR_PTR(-EINVAL);
Viresh Kumar4dab1602016-12-01 16:28:21 +05301657
Viresh Kumarfa301842017-01-23 10:11:43 +05301658 opp_table = dev_pm_opp_get_opp_table(dev);
1659 if (!opp_table)
1660 return ERR_PTR(-ENOMEM);
Viresh Kumar4dab1602016-12-01 16:28:21 +05301661
1662 /* This should be called before OPPs are initialized */
1663 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
Viresh Kumar5019acc2018-05-22 16:38:08 +05301664 dev_pm_opp_put_opp_table(opp_table);
1665 return ERR_PTR(-EBUSY);
Viresh Kumar4dab1602016-12-01 16:28:21 +05301666 }
1667
Viresh Kumar5019acc2018-05-22 16:38:08 +05301668 /* Another CPU that shares the OPP table has set the helper ? */
1669 if (!opp_table->set_opp)
1670 opp_table->set_opp = set_opp;
Viresh Kumar4dab1602016-12-01 16:28:21 +05301671
Viresh Kumarfa301842017-01-23 10:11:43 +05301672 return opp_table;
Viresh Kumar4dab1602016-12-01 16:28:21 +05301673}
1674EXPORT_SYMBOL_GPL(dev_pm_opp_register_set_opp_helper);
1675
1676/**
Viresh Kumar604a7ae2017-10-05 17:26:21 +05301677 * dev_pm_opp_unregister_set_opp_helper() - Releases resources blocked for
Viresh Kumar4dab1602016-12-01 16:28:21 +05301678 * set_opp helper
Viresh Kumarfa301842017-01-23 10:11:43 +05301679 * @opp_table: OPP table returned from dev_pm_opp_register_set_opp_helper().
Viresh Kumar4dab1602016-12-01 16:28:21 +05301680 *
Viresh Kumarfa301842017-01-23 10:11:43 +05301681 * Release resources blocked for platform specific set_opp helper.
Viresh Kumar4dab1602016-12-01 16:28:21 +05301682 */
Viresh Kumar604a7ae2017-10-05 17:26:21 +05301683void dev_pm_opp_unregister_set_opp_helper(struct opp_table *opp_table)
Viresh Kumar4dab1602016-12-01 16:28:21 +05301684{
Viresh Kumar4dab1602016-12-01 16:28:21 +05301685 /* Make sure there are no concurrent readers while updating opp_table */
1686 WARN_ON(!list_empty(&opp_table->opp_list));
1687
1688 opp_table->set_opp = NULL;
Viresh Kumarfa301842017-01-23 10:11:43 +05301689 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar4dab1602016-12-01 16:28:21 +05301690}
Viresh Kumar604a7ae2017-10-05 17:26:21 +05301691EXPORT_SYMBOL_GPL(dev_pm_opp_unregister_set_opp_helper);
Viresh Kumar4dab1602016-12-01 16:28:21 +05301692
1693/**
Viresh Kumar4f018bc2018-06-26 16:29:34 +05301694 * dev_pm_opp_set_genpd_virt_dev - Set virtual genpd device for an index
1695 * @dev: Consumer device for which the genpd device is getting set.
1696 * @virt_dev: virtual genpd device.
1697 * @index: index.
1698 *
1699 * Multiple generic power domains for a device are supported with the help of
1700 * virtual genpd devices, which are created for each consumer device - genpd
1701 * pair. These are the device structures which are attached to the power domain
1702 * and are required by the OPP core to set the performance state of the genpd.
1703 *
1704 * This helper will normally be called by the consumer driver of the device
1705 * "dev", as only that has details of the genpd devices.
1706 *
1707 * This helper needs to be called once for each of those virtual devices, but
1708 * only if multiple domains are available for a device. Otherwise the original
1709 * device structure will be used instead by the OPP core.
1710 */
1711struct opp_table *dev_pm_opp_set_genpd_virt_dev(struct device *dev,
1712 struct device *virt_dev,
1713 int index)
1714{
1715 struct opp_table *opp_table;
1716
1717 opp_table = dev_pm_opp_get_opp_table(dev);
1718 if (!opp_table)
1719 return ERR_PTR(-ENOMEM);
1720
1721 mutex_lock(&opp_table->genpd_virt_dev_lock);
1722
1723 if (unlikely(!opp_table->genpd_virt_devs ||
1724 index >= opp_table->required_opp_count ||
1725 opp_table->genpd_virt_devs[index])) {
1726
1727 dev_err(dev, "Invalid request to set required device\n");
1728 dev_pm_opp_put_opp_table(opp_table);
1729 mutex_unlock(&opp_table->genpd_virt_dev_lock);
1730
1731 return ERR_PTR(-EINVAL);
1732 }
1733
1734 opp_table->genpd_virt_devs[index] = virt_dev;
1735 mutex_unlock(&opp_table->genpd_virt_dev_lock);
1736
1737 return opp_table;
1738}
1739
1740/**
1741 * dev_pm_opp_put_genpd_virt_dev() - Releases resources blocked for genpd device.
1742 * @opp_table: OPP table returned by dev_pm_opp_set_genpd_virt_dev().
1743 * @virt_dev: virtual genpd device.
1744 *
1745 * This releases the resource previously acquired with a call to
1746 * dev_pm_opp_set_genpd_virt_dev(). The consumer driver shall call this helper
1747 * if it doesn't want OPP core to update performance state of a power domain
1748 * anymore.
1749 */
1750void dev_pm_opp_put_genpd_virt_dev(struct opp_table *opp_table,
1751 struct device *virt_dev)
1752{
1753 int i;
1754
1755 /*
1756 * Acquire genpd_virt_dev_lock to make sure virt_dev isn't getting
1757 * used in parallel.
1758 */
1759 mutex_lock(&opp_table->genpd_virt_dev_lock);
1760
1761 for (i = 0; i < opp_table->required_opp_count; i++) {
1762 if (opp_table->genpd_virt_devs[i] != virt_dev)
1763 continue;
1764
1765 opp_table->genpd_virt_devs[i] = NULL;
1766 dev_pm_opp_put_opp_table(opp_table);
1767
1768 /* Drop the vote */
1769 dev_pm_genpd_set_performance_state(virt_dev, 0);
1770 break;
1771 }
1772
1773 mutex_unlock(&opp_table->genpd_virt_dev_lock);
1774
1775 if (unlikely(i == opp_table->required_opp_count))
1776 dev_err(virt_dev, "Failed to find required device entry\n");
1777}
1778
1779/**
Viresh Kumarc8a59102018-11-02 14:36:42 +05301780 * dev_pm_opp_xlate_performance_state() - Find required OPP's pstate for src_table.
1781 * @src_table: OPP table which has dst_table as one of its required OPP table.
1782 * @dst_table: Required OPP table of the src_table.
1783 * @pstate: Current performance state of the src_table.
1784 *
1785 * This Returns pstate of the OPP (present in @dst_table) pointed out by the
1786 * "required-opps" property of the OPP (present in @src_table) which has
1787 * performance state set to @pstate.
1788 *
1789 * Return: Zero or positive performance state on success, otherwise negative
1790 * value on errors.
1791 */
1792int dev_pm_opp_xlate_performance_state(struct opp_table *src_table,
1793 struct opp_table *dst_table,
1794 unsigned int pstate)
1795{
1796 struct dev_pm_opp *opp;
1797 int dest_pstate = -EINVAL;
1798 int i;
1799
1800 if (!pstate)
1801 return 0;
1802
1803 /*
1804 * Normally the src_table will have the "required_opps" property set to
1805 * point to one of the OPPs in the dst_table, but in some cases the
1806 * genpd and its master have one to one mapping of performance states
1807 * and so none of them have the "required-opps" property set. Return the
1808 * pstate of the src_table as it is in such cases.
1809 */
1810 if (!src_table->required_opp_count)
1811 return pstate;
1812
1813 for (i = 0; i < src_table->required_opp_count; i++) {
1814 if (src_table->required_opp_tables[i]->np == dst_table->np)
1815 break;
1816 }
1817
1818 if (unlikely(i == src_table->required_opp_count)) {
1819 pr_err("%s: Couldn't find matching OPP table (%p: %p)\n",
1820 __func__, src_table, dst_table);
1821 return -EINVAL;
1822 }
1823
1824 mutex_lock(&src_table->lock);
1825
1826 list_for_each_entry(opp, &src_table->opp_list, node) {
1827 if (opp->pstate == pstate) {
1828 dest_pstate = opp->required_opps[i]->pstate;
1829 goto unlock;
1830 }
1831 }
1832
1833 pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__, src_table,
1834 dst_table);
1835
1836unlock:
1837 mutex_unlock(&src_table->lock);
1838
1839 return dest_pstate;
1840}
1841
1842/**
Nishanth Menone1f60b22010-10-13 00:13:10 +02001843 * dev_pm_opp_add() - Add an OPP table from a table definitions
1844 * @dev: device for which we do this operation
Nishanth Menon47d43ba2013-09-19 16:03:51 -05001845 * @freq: Frequency in Hz for this OPP
Nishanth Menone1f60b22010-10-13 00:13:10 +02001846 * @u_volt: Voltage in uVolts for this OPP
1847 *
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301848 * This function adds an opp definition to the opp table and returns status.
Nishanth Menon47d43ba2013-09-19 16:03:51 -05001849 * The opp is made available by default and it can be controlled using
Nishanth Menone1f60b22010-10-13 00:13:10 +02001850 * dev_pm_opp_enable/disable functions.
1851 *
Viresh Kumara7470db2014-11-25 16:04:17 +05301852 * Return:
1853 * 0 On success OR
1854 * Duplicate OPPs (both freq and volt are same) and opp->available
1855 * -EEXIST Freq are same and volt are different OR
1856 * Duplicate OPPs (both freq and volt are same) and !opp->available
Viresh Kumar383934092014-11-25 16:04:18 +05301857 * -ENOMEM Memory allocation failure
Viresh Kumara7470db2014-11-25 16:04:17 +05301858 */
Nishanth Menone1f60b22010-10-13 00:13:10 +02001859int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
1860{
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301861 struct opp_table *opp_table;
1862 int ret;
1863
Viresh Kumarb83c1892017-01-23 10:11:45 +05301864 opp_table = dev_pm_opp_get_opp_table(dev);
1865 if (!opp_table)
1866 return -ENOMEM;
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301867
Viresh Kumar46f48ac2018-12-11 16:39:36 +05301868 /* Fix regulator count for dynamic OPPs */
1869 opp_table->regulator_count = 1;
1870
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301871 ret = _opp_add_v1(opp_table, dev, freq, u_volt, true);
Viresh Kumar0ad8c622018-08-02 14:23:09 +05301872 if (ret)
1873 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301874
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301875 return ret;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001876}
1877EXPORT_SYMBOL_GPL(dev_pm_opp_add);
1878
Nishanth Menone1f60b22010-10-13 00:13:10 +02001879/**
Nishanth Menon327854c2014-12-24 11:22:56 -06001880 * _opp_set_availability() - helper to set the availability of an opp
Nishanth Menone1f60b22010-10-13 00:13:10 +02001881 * @dev: device for which we do this operation
1882 * @freq: OPP frequency to modify availability
1883 * @availability_req: availability status requested for this opp
1884 *
Viresh Kumar052c6f12017-01-23 10:11:49 +05301885 * Set the availability of an OPP, opp_{enable,disable} share a common logic
1886 * which is isolated here.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001887 *
Nishanth Menon984f16c82014-12-24 11:22:57 -06001888 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
Stephen Boyde1a2d492015-09-24 12:28:44 -07001889 * copy operation, returns 0 if no modification was done OR modification was
Nishanth Menone1f60b22010-10-13 00:13:10 +02001890 * successful.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001891 */
Nishanth Menon327854c2014-12-24 11:22:56 -06001892static int _opp_set_availability(struct device *dev, unsigned long freq,
1893 bool availability_req)
Nishanth Menone1f60b22010-10-13 00:13:10 +02001894{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301895 struct opp_table *opp_table;
Viresh Kumara7f39872017-01-23 10:11:50 +05301896 struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001897 int r = 0;
1898
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301899 /* Find the opp_table */
1900 opp_table = _find_opp_table(dev);
1901 if (IS_ERR(opp_table)) {
1902 r = PTR_ERR(opp_table);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001903 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
Viresh Kumara7f39872017-01-23 10:11:50 +05301904 return r;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001905 }
1906
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301907 mutex_lock(&opp_table->lock);
1908
Nishanth Menone1f60b22010-10-13 00:13:10 +02001909 /* Do we have the frequency? */
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301910 list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
Nishanth Menone1f60b22010-10-13 00:13:10 +02001911 if (tmp_opp->rate == freq) {
1912 opp = tmp_opp;
1913 break;
1914 }
1915 }
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301916
Nishanth Menone1f60b22010-10-13 00:13:10 +02001917 if (IS_ERR(opp)) {
1918 r = PTR_ERR(opp);
1919 goto unlock;
1920 }
1921
1922 /* Is update really needed? */
1923 if (opp->available == availability_req)
1924 goto unlock;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001925
Viresh Kumara7f39872017-01-23 10:11:50 +05301926 opp->available = availability_req;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001927
Viresh Kumare4d8ae02017-09-21 10:44:36 -07001928 dev_pm_opp_get(opp);
1929 mutex_unlock(&opp_table->lock);
1930
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001931 /* Notify the change of the OPP availability */
1932 if (availability_req)
Viresh Kumar052c6f12017-01-23 10:11:49 +05301933 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE,
Viresh Kumara7f39872017-01-23 10:11:50 +05301934 opp);
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001935 else
Viresh Kumar052c6f12017-01-23 10:11:49 +05301936 blocking_notifier_call_chain(&opp_table->head,
Viresh Kumara7f39872017-01-23 10:11:50 +05301937 OPP_EVENT_DISABLE, opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001938
Viresh Kumare4d8ae02017-09-21 10:44:36 -07001939 dev_pm_opp_put(opp);
1940 goto put_table;
1941
Nishanth Menone1f60b22010-10-13 00:13:10 +02001942unlock:
Viresh Kumar5b650b32017-01-23 10:11:48 +05301943 mutex_unlock(&opp_table->lock);
Viresh Kumare4d8ae02017-09-21 10:44:36 -07001944put_table:
Viresh Kumar5b650b32017-01-23 10:11:48 +05301945 dev_pm_opp_put_opp_table(opp_table);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001946 return r;
1947}
1948
1949/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001950 * dev_pm_opp_enable() - Enable a specific OPP
Nishanth Menone1f60b22010-10-13 00:13:10 +02001951 * @dev: device for which we do this operation
1952 * @freq: OPP frequency to enable
1953 *
1954 * Enables a provided opp. If the operation is valid, this returns 0, else the
1955 * corresponding error value. It is meant to be used for users an OPP available
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001956 * after being temporarily made unavailable with dev_pm_opp_disable.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001957 *
Nishanth Menon984f16c82014-12-24 11:22:57 -06001958 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
Stephen Boyde1a2d492015-09-24 12:28:44 -07001959 * copy operation, returns 0 if no modification was done OR modification was
Nishanth Menon984f16c82014-12-24 11:22:57 -06001960 * successful.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001961 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001962int dev_pm_opp_enable(struct device *dev, unsigned long freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +02001963{
Nishanth Menon327854c2014-12-24 11:22:56 -06001964 return _opp_set_availability(dev, freq, true);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001965}
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001966EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001967
1968/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001969 * dev_pm_opp_disable() - Disable a specific OPP
Nishanth Menone1f60b22010-10-13 00:13:10 +02001970 * @dev: device for which we do this operation
1971 * @freq: OPP frequency to disable
1972 *
1973 * Disables a provided opp. If the operation is valid, this returns
1974 * 0, else the corresponding error value. It is meant to be a temporary
1975 * control by users to make this OPP not available until the circumstances are
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001976 * right to make it available again (with a call to dev_pm_opp_enable).
Nishanth Menone1f60b22010-10-13 00:13:10 +02001977 *
Nishanth Menon984f16c82014-12-24 11:22:57 -06001978 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
Stephen Boyde1a2d492015-09-24 12:28:44 -07001979 * copy operation, returns 0 if no modification was done OR modification was
Nishanth Menon984f16c82014-12-24 11:22:57 -06001980 * successful.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001981 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001982int dev_pm_opp_disable(struct device *dev, unsigned long freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +02001983{
Nishanth Menon327854c2014-12-24 11:22:56 -06001984 return _opp_set_availability(dev, freq, false);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001985}
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001986EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001987
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001988/**
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301989 * dev_pm_opp_register_notifier() - Register OPP notifier for the device
1990 * @dev: Device for which notifier needs to be registered
1991 * @nb: Notifier block to be registered
Nishanth Menon984f16c82014-12-24 11:22:57 -06001992 *
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301993 * Return: 0 on success or a negative error value.
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001994 */
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301995int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb)
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001996{
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301997 struct opp_table *opp_table;
1998 int ret;
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001999
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05302000 opp_table = _find_opp_table(dev);
Viresh Kumar5b650b32017-01-23 10:11:48 +05302001 if (IS_ERR(opp_table))
2002 return PTR_ERR(opp_table);
2003
Viresh Kumar052c6f12017-01-23 10:11:49 +05302004 ret = blocking_notifier_chain_register(&opp_table->head, nb);
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05302005
Viresh Kumar5b650b32017-01-23 10:11:48 +05302006 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05302007
2008 return ret;
MyungJoo Ham03ca3702011-09-30 22:35:12 +02002009}
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05302010EXPORT_SYMBOL(dev_pm_opp_register_notifier);
2011
2012/**
2013 * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device
2014 * @dev: Device for which notifier needs to be unregistered
2015 * @nb: Notifier block to be unregistered
2016 *
2017 * Return: 0 on success or a negative error value.
2018 */
2019int dev_pm_opp_unregister_notifier(struct device *dev,
2020 struct notifier_block *nb)
2021{
2022 struct opp_table *opp_table;
2023 int ret;
2024
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05302025 opp_table = _find_opp_table(dev);
Viresh Kumar5b650b32017-01-23 10:11:48 +05302026 if (IS_ERR(opp_table))
2027 return PTR_ERR(opp_table);
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05302028
Viresh Kumar052c6f12017-01-23 10:11:49 +05302029 ret = blocking_notifier_chain_unregister(&opp_table->head, nb);
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05302030
Viresh Kumar5b650b32017-01-23 10:11:48 +05302031 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05302032
2033 return ret;
2034}
2035EXPORT_SYMBOL(dev_pm_opp_unregister_notifier);
Shawn Guob496dfb2012-09-05 01:09:12 +02002036
Viresh Kumar2a4eb732018-09-13 13:14:36 +05302037void _dev_pm_opp_find_and_remove_table(struct device *dev)
Viresh Kumar737002b2015-07-29 16:22:58 +05302038{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05302039 struct opp_table *opp_table;
Viresh Kumar737002b2015-07-29 16:22:58 +05302040
Viresh Kumar2c2709d2016-02-16 14:17:53 +05302041 /* Check for existing table for 'dev' */
2042 opp_table = _find_opp_table(dev);
2043 if (IS_ERR(opp_table)) {
2044 int error = PTR_ERR(opp_table);
Viresh Kumar737002b2015-07-29 16:22:58 +05302045
2046 if (error != -ENODEV)
Viresh Kumar2c2709d2016-02-16 14:17:53 +05302047 WARN(1, "%s: opp_table: %d\n",
Viresh Kumar737002b2015-07-29 16:22:58 +05302048 IS_ERR_OR_NULL(dev) ?
2049 "Invalid device" : dev_name(dev),
2050 error);
Viresh Kumar5b650b32017-01-23 10:11:48 +05302051 return;
Viresh Kumar737002b2015-07-29 16:22:58 +05302052 }
2053
Viresh Kumarcdd6ed92018-09-12 12:35:19 +05302054 _put_opp_list_kref(opp_table);
Viresh Kumar737002b2015-07-29 16:22:58 +05302055
Viresh Kumarcdd6ed92018-09-12 12:35:19 +05302056 /* Drop reference taken by _find_opp_table() */
2057 dev_pm_opp_put_opp_table(opp_table);
2058
2059 /* Drop reference taken while the OPP table was added */
Viresh Kumar5b650b32017-01-23 10:11:48 +05302060 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar737002b2015-07-29 16:22:58 +05302061}
Viresh Kumar129eec52014-11-27 08:54:06 +05302062
2063/**
Sudeep Holla411466c2016-05-03 15:05:04 +01002064 * dev_pm_opp_remove_table() - Free all OPPs associated with the device
Viresh Kumar2c2709d2016-02-16 14:17:53 +05302065 * @dev: device pointer used to lookup OPP table.
Viresh Kumar129eec52014-11-27 08:54:06 +05302066 *
Sudeep Holla411466c2016-05-03 15:05:04 +01002067 * Free both OPPs created using static entries present in DT and the
2068 * dynamically added entries.
Viresh Kumar129eec52014-11-27 08:54:06 +05302069 */
Sudeep Holla411466c2016-05-03 15:05:04 +01002070void dev_pm_opp_remove_table(struct device *dev)
Viresh Kumar129eec52014-11-27 08:54:06 +05302071{
Viresh Kumar2a4eb732018-09-13 13:14:36 +05302072 _dev_pm_opp_find_and_remove_table(dev);
Viresh Kumar8d4d4e92015-06-12 17:10:38 +05302073}
Sudeep Holla411466c2016-05-03 15:05:04 +01002074EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table);