blob: 0e7703fe733fc79cf672687d610d1a50f25aa6fb [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 Menon984f16c2014-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 Menon984f16c2014-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 Hajda8cc31112017-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 Menon984f16c2014-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 Menon984f16c2014-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 Menon984f16c2014-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 Menon984f16c2014-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
Andrew-sh.Cheng2f36bde2019-03-29 14:46:10 +0800529/**
530 * dev_pm_opp_find_freq_ceil_by_volt() - Find OPP with highest frequency for
531 * target voltage.
532 * @dev: Device for which we do this operation.
533 * @u_volt: Target voltage.
534 *
535 * Search for OPP with highest (ceil) frequency and has voltage <= u_volt.
536 *
537 * Return: matching *opp, else returns ERR_PTR in case of error which should be
538 * handled using IS_ERR.
539 *
540 * Error return values can be:
541 * EINVAL: bad parameters
542 *
543 * The callers are required to call dev_pm_opp_put() for the returned OPP after
544 * use.
545 */
546struct dev_pm_opp *dev_pm_opp_find_freq_ceil_by_volt(struct device *dev,
547 unsigned long u_volt)
548{
549 struct opp_table *opp_table;
550 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
551
552 if (!dev || !u_volt) {
553 dev_err(dev, "%s: Invalid argument volt=%lu\n", __func__,
554 u_volt);
555 return ERR_PTR(-EINVAL);
556 }
557
558 opp_table = _find_opp_table(dev);
559 if (IS_ERR(opp_table))
560 return ERR_CAST(opp_table);
561
562 mutex_lock(&opp_table->lock);
563
564 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
565 if (temp_opp->available) {
566 if (temp_opp->supplies[0].u_volt > u_volt)
567 break;
568 opp = temp_opp;
569 }
570 }
571
572 /* Increment the reference count of OPP */
573 if (!IS_ERR(opp))
574 dev_pm_opp_get(opp);
575
576 mutex_unlock(&opp_table->lock);
577 dev_pm_opp_put_opp_table(opp_table);
578
579 return opp;
580}
581EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil_by_volt);
582
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530583static int _set_opp_voltage(struct device *dev, struct regulator *reg,
Viresh Kumarce317812016-12-01 16:28:18 +0530584 struct dev_pm_opp_supply *supply)
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530585{
586 int ret;
587
588 /* Regulator not available for device */
589 if (IS_ERR(reg)) {
590 dev_dbg(dev, "%s: regulator not available: %ld\n", __func__,
591 PTR_ERR(reg));
592 return 0;
593 }
594
Viresh Kumarce317812016-12-01 16:28:18 +0530595 dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__,
596 supply->u_volt_min, supply->u_volt, supply->u_volt_max);
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530597
Viresh Kumarce317812016-12-01 16:28:18 +0530598 ret = regulator_set_voltage_triplet(reg, supply->u_volt_min,
599 supply->u_volt, supply->u_volt_max);
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530600 if (ret)
601 dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n",
Viresh Kumarce317812016-12-01 16:28:18 +0530602 __func__, supply->u_volt_min, supply->u_volt,
603 supply->u_volt_max, ret);
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530604
605 return ret;
606}
607
Viresh Kumar285881b2019-01-31 13:53:25 +0530608static inline int _generic_set_opp_clk_only(struct device *dev, struct clk *clk,
609 unsigned long freq)
Viresh Kumar94735582016-12-01 16:28:20 +0530610{
611 int ret;
612
613 ret = clk_set_rate(clk, freq);
614 if (ret) {
615 dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
616 ret);
617 }
618
619 return ret;
620}
621
Viresh Kumarc74b32f2017-05-23 09:32:10 +0530622static int _generic_set_opp_regulator(const struct opp_table *opp_table,
623 struct device *dev,
624 unsigned long old_freq,
625 unsigned long freq,
626 struct dev_pm_opp_supply *old_supply,
627 struct dev_pm_opp_supply *new_supply)
Viresh Kumar94735582016-12-01 16:28:20 +0530628{
Viresh Kumarc74b32f2017-05-23 09:32:10 +0530629 struct regulator *reg = opp_table->regulators[0];
Viresh Kumar94735582016-12-01 16:28:20 +0530630 int ret;
631
632 /* This function only supports single regulator per device */
Viresh Kumarc74b32f2017-05-23 09:32:10 +0530633 if (WARN_ON(opp_table->regulator_count > 1)) {
Viresh Kumar94735582016-12-01 16:28:20 +0530634 dev_err(dev, "multiple regulators are not supported\n");
635 return -EINVAL;
636 }
637
638 /* Scaling up? Scale voltage before frequency */
Waldemar Rymarkiewiczc5c2a972018-06-14 15:56:08 +0200639 if (freq >= old_freq) {
Viresh Kumar94735582016-12-01 16:28:20 +0530640 ret = _set_opp_voltage(dev, reg, new_supply);
641 if (ret)
642 goto restore_voltage;
643 }
644
645 /* Change frequency */
Viresh Kumar285881b2019-01-31 13:53:25 +0530646 ret = _generic_set_opp_clk_only(dev, opp_table->clk, freq);
Viresh Kumar94735582016-12-01 16:28:20 +0530647 if (ret)
648 goto restore_voltage;
649
650 /* Scaling down? Scale voltage after frequency */
651 if (freq < old_freq) {
652 ret = _set_opp_voltage(dev, reg, new_supply);
653 if (ret)
654 goto restore_freq;
655 }
656
657 return 0;
658
659restore_freq:
Viresh Kumar285881b2019-01-31 13:53:25 +0530660 if (_generic_set_opp_clk_only(dev, opp_table->clk, old_freq))
Viresh Kumar94735582016-12-01 16:28:20 +0530661 dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n",
662 __func__, old_freq);
663restore_voltage:
664 /* This shouldn't harm even if the voltages weren't updated earlier */
Viresh Kumarc74b32f2017-05-23 09:32:10 +0530665 if (old_supply)
Viresh Kumar94735582016-12-01 16:28:20 +0530666 _set_opp_voltage(dev, reg, old_supply);
667
668 return ret;
669}
670
Viresh Kumar7e5359932018-06-12 14:47:03 +0530671static int _set_opp_custom(const struct opp_table *opp_table,
672 struct device *dev, unsigned long old_freq,
673 unsigned long freq,
674 struct dev_pm_opp_supply *old_supply,
675 struct dev_pm_opp_supply *new_supply)
676{
677 struct dev_pm_set_opp_data *data;
678 int size;
679
680 data = opp_table->set_opp_data;
681 data->regulators = opp_table->regulators;
682 data->regulator_count = opp_table->regulator_count;
683 data->clk = opp_table->clk;
684 data->dev = dev;
685
686 data->old_opp.rate = old_freq;
687 size = sizeof(*old_supply) * opp_table->regulator_count;
688 if (IS_ERR(old_supply))
689 memset(data->old_opp.supplies, 0, size);
690 else
691 memcpy(data->old_opp.supplies, old_supply, size);
692
693 data->new_opp.rate = freq;
694 memcpy(data->new_opp.supplies, new_supply, size);
695
696 return opp_table->set_opp(data);
697}
698
Viresh Kumarca1b5d72018-06-14 10:03:26 +0530699/* This is only called for PM domain for now */
700static int _set_required_opps(struct device *dev,
701 struct opp_table *opp_table,
702 struct dev_pm_opp *opp)
703{
704 struct opp_table **required_opp_tables = opp_table->required_opp_tables;
705 struct device **genpd_virt_devs = opp_table->genpd_virt_devs;
706 unsigned int pstate;
707 int i, ret = 0;
708
709 if (!required_opp_tables)
710 return 0;
711
712 /* Single genpd case */
713 if (!genpd_virt_devs) {
714 pstate = opp->required_opps[0]->pstate;
715 ret = dev_pm_genpd_set_performance_state(dev, pstate);
716 if (ret) {
717 dev_err(dev, "Failed to set performance state of %s: %d (%d)\n",
718 dev_name(dev), pstate, ret);
719 }
720 return ret;
721 }
722
723 /* Multiple genpd case */
724
725 /*
726 * Acquire genpd_virt_dev_lock to make sure we don't use a genpd_dev
727 * after it is freed from another thread.
728 */
729 mutex_lock(&opp_table->genpd_virt_dev_lock);
730
731 for (i = 0; i < opp_table->required_opp_count; i++) {
732 pstate = opp->required_opps[i]->pstate;
733
734 if (!genpd_virt_devs[i])
735 continue;
736
737 ret = dev_pm_genpd_set_performance_state(genpd_virt_devs[i], pstate);
738 if (ret) {
739 dev_err(dev, "Failed to set performance rate of %s: %d (%d)\n",
740 dev_name(genpd_virt_devs[i]), pstate, ret);
741 break;
742 }
743 }
744 mutex_unlock(&opp_table->genpd_virt_dev_lock);
745
746 return ret;
747}
748
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530749/**
750 * dev_pm_opp_set_rate() - Configure new OPP based on frequency
751 * @dev: device for which we do this operation
752 * @target_freq: frequency to achieve
753 *
754 * This configures the power-supplies and clock source to the levels specified
755 * by the OPP corresponding to the target_freq.
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530756 */
757int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq)
758{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530759 struct opp_table *opp_table;
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530760 unsigned long freq, old_freq;
Viresh Kumar94735582016-12-01 16:28:20 +0530761 struct dev_pm_opp *old_opp, *opp;
Viresh Kumar94735582016-12-01 16:28:20 +0530762 struct clk *clk;
Viresh Kumar7e5359932018-06-12 14:47:03 +0530763 int ret;
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530764
765 if (unlikely(!target_freq)) {
766 dev_err(dev, "%s: Invalid target frequency %lu\n", __func__,
767 target_freq);
768 return -EINVAL;
769 }
770
Viresh Kumar052c6f12017-01-23 10:11:49 +0530771 opp_table = _find_opp_table(dev);
772 if (IS_ERR(opp_table)) {
773 dev_err(dev, "%s: device opp doesn't exist\n", __func__);
774 return PTR_ERR(opp_table);
775 }
776
777 clk = opp_table->clk;
778 if (IS_ERR(clk)) {
779 dev_err(dev, "%s: No clock available for the device\n",
780 __func__);
781 ret = PTR_ERR(clk);
782 goto put_opp_table;
783 }
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530784
785 freq = clk_round_rate(clk, target_freq);
786 if ((long)freq <= 0)
787 freq = target_freq;
788
789 old_freq = clk_get_rate(clk);
790
791 /* Return early if nothing to do */
792 if (old_freq == freq) {
793 dev_dbg(dev, "%s: old/new frequencies (%lu Hz) are same, nothing to do\n",
794 __func__, freq);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530795 ret = 0;
796 goto put_opp_table;
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530797 }
798
Jisheng Zhang067b7ce2016-07-25 14:11:16 +0800799 old_opp = _find_freq_ceil(opp_table, &old_freq);
Arnd Bergmann4df27c92016-09-15 17:38:38 +0200800 if (IS_ERR(old_opp)) {
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530801 dev_err(dev, "%s: failed to find current OPP for freq %lu (%ld)\n",
802 __func__, old_freq, PTR_ERR(old_opp));
803 }
804
Jisheng Zhang067b7ce2016-07-25 14:11:16 +0800805 opp = _find_freq_ceil(opp_table, &freq);
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530806 if (IS_ERR(opp)) {
807 ret = PTR_ERR(opp);
808 dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n",
809 __func__, freq, ret);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530810 goto put_old_opp;
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530811 }
812
Viresh Kumar94735582016-12-01 16:28:20 +0530813 dev_dbg(dev, "%s: switching OPP: %lu Hz --> %lu Hz\n", __func__,
814 old_freq, freq);
Viresh Kumardfbe4672016-12-01 16:28:19 +0530815
Viresh Kumarca1b5d72018-06-14 10:03:26 +0530816 /* Scaling up? Configure required OPPs before frequency */
Viresh Kumarfaef0802019-03-12 10:27:18 +0530817 if (freq >= old_freq) {
Viresh Kumarca1b5d72018-06-14 10:03:26 +0530818 ret = _set_required_opps(dev, opp_table, opp);
819 if (ret)
820 goto put_opp;
821 }
822
Viresh Kumar7e5359932018-06-12 14:47:03 +0530823 if (opp_table->set_opp) {
824 ret = _set_opp_custom(opp_table, dev, old_freq, freq,
825 IS_ERR(old_opp) ? NULL : old_opp->supplies,
826 opp->supplies);
827 } else if (opp_table->regulators) {
Viresh Kumarc74b32f2017-05-23 09:32:10 +0530828 ret = _generic_set_opp_regulator(opp_table, dev, old_freq, freq,
829 IS_ERR(old_opp) ? NULL : old_opp->supplies,
830 opp->supplies);
831 } else {
Viresh Kumar7e5359932018-06-12 14:47:03 +0530832 /* Only frequency scaling */
Viresh Kumar285881b2019-01-31 13:53:25 +0530833 ret = _generic_set_opp_clk_only(dev, clk, freq);
Viresh Kumardfbe4672016-12-01 16:28:19 +0530834 }
835
Viresh Kumarca1b5d72018-06-14 10:03:26 +0530836 /* Scaling down? Configure required OPPs after frequency */
837 if (!ret && freq < old_freq) {
838 ret = _set_required_opps(dev, opp_table, opp);
839 if (ret)
840 dev_err(dev, "Failed to set required opps: %d\n", ret);
841 }
842
843put_opp:
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530844 dev_pm_opp_put(opp);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530845put_old_opp:
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530846 if (!IS_ERR(old_opp))
847 dev_pm_opp_put(old_opp);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530848put_opp_table:
Viresh Kumar5b650b32017-01-23 10:11:48 +0530849 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530850 return ret;
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530851}
852EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate);
853
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530854/* OPP-dev Helpers */
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530855static void _remove_opp_dev(struct opp_device *opp_dev,
856 struct opp_table *opp_table)
Viresh Kumar06441652015-07-29 16:23:04 +0530857{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530858 opp_debug_unregister(opp_dev, opp_table);
859 list_del(&opp_dev->node);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530860 kfree(opp_dev);
Viresh Kumar06441652015-07-29 16:23:04 +0530861}
862
Viresh Kumar283d55e2018-09-07 09:01:54 +0530863static struct opp_device *_add_opp_dev_unlocked(const struct device *dev,
864 struct opp_table *opp_table)
Viresh Kumar06441652015-07-29 16:23:04 +0530865{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530866 struct opp_device *opp_dev;
Viresh Kumar06441652015-07-29 16:23:04 +0530867
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530868 opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL);
869 if (!opp_dev)
Viresh Kumar06441652015-07-29 16:23:04 +0530870 return NULL;
871
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530872 /* Initialize opp-dev */
873 opp_dev->dev = dev;
Viresh Kumar3d255692018-08-03 07:05:21 +0530874
Viresh Kumar052c6f12017-01-23 10:11:49 +0530875 list_add(&opp_dev->node, &opp_table->dev_list);
Viresh Kumar06441652015-07-29 16:23:04 +0530876
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530877 /* Create debugfs entries for the opp_table */
Greg Kroah-Hartmana2dea4c2019-01-22 16:21:17 +0100878 opp_debug_register(opp_dev, opp_table);
Viresh Kumar283d55e2018-09-07 09:01:54 +0530879
880 return opp_dev;
881}
882
883struct opp_device *_add_opp_dev(const struct device *dev,
884 struct opp_table *opp_table)
885{
886 struct opp_device *opp_dev;
887
888 mutex_lock(&opp_table->lock);
889 opp_dev = _add_opp_dev_unlocked(dev, opp_table);
Viresh Kumar3d255692018-08-03 07:05:21 +0530890 mutex_unlock(&opp_table->lock);
Viresh Kumardeaa5142015-11-11 07:59:01 +0530891
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530892 return opp_dev;
Viresh Kumar06441652015-07-29 16:23:04 +0530893}
894
Viresh Kumareb7c87432018-09-05 16:17:14 +0530895static struct opp_table *_allocate_opp_table(struct device *dev, int index)
Viresh Kumar07cce742014-12-10 09:45:34 +0530896{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530897 struct opp_table *opp_table;
898 struct opp_device *opp_dev;
Viresh Kumard54974c2016-02-09 10:30:38 +0530899 int ret;
Viresh Kumar07cce742014-12-10 09:45:34 +0530900
901 /*
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530902 * Allocate a new OPP table. In the infrequent case where a new
Viresh Kumar07cce742014-12-10 09:45:34 +0530903 * device is needed to be added, we pay this penalty.
904 */
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530905 opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL);
906 if (!opp_table)
Viresh Kumar07cce742014-12-10 09:45:34 +0530907 return NULL;
908
Viresh Kumar3d255692018-08-03 07:05:21 +0530909 mutex_init(&opp_table->lock);
Viresh Kumar4f018bc2018-06-26 16:29:34 +0530910 mutex_init(&opp_table->genpd_virt_dev_lock);
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530911 INIT_LIST_HEAD(&opp_table->dev_list);
Viresh Kumar06441652015-07-29 16:23:04 +0530912
Viresh Kumar46f48ac2018-12-11 16:39:36 +0530913 /* Mark regulator count uninitialized */
914 opp_table->regulator_count = -1;
915
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530916 opp_dev = _add_opp_dev(dev, opp_table);
917 if (!opp_dev) {
918 kfree(opp_table);
Viresh Kumar06441652015-07-29 16:23:04 +0530919 return NULL;
920 }
921
Viresh Kumareb7c87432018-09-05 16:17:14 +0530922 _of_init_opp_table(opp_table, dev, index);
Viresh Kumar50f8cfb2016-02-09 10:30:37 +0530923
Viresh Kumard54974c2016-02-09 10:30:38 +0530924 /* Find clk for the device */
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530925 opp_table->clk = clk_get(dev, NULL);
926 if (IS_ERR(opp_table->clk)) {
927 ret = PTR_ERR(opp_table->clk);
Viresh Kumard54974c2016-02-09 10:30:38 +0530928 if (ret != -EPROBE_DEFER)
929 dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__,
930 ret);
931 }
932
Viresh Kumar052c6f12017-01-23 10:11:49 +0530933 BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head);
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530934 INIT_LIST_HEAD(&opp_table->opp_list);
Viresh Kumarf067a982017-01-23 10:11:42 +0530935 kref_init(&opp_table->kref);
Viresh Kumar07cce742014-12-10 09:45:34 +0530936
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530937 /* Secure the device table modification */
Viresh Kumar052c6f12017-01-23 10:11:49 +0530938 list_add(&opp_table->node, &opp_tables);
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530939 return opp_table;
Viresh Kumar07cce742014-12-10 09:45:34 +0530940}
941
Viresh Kumarf067a982017-01-23 10:11:42 +0530942void _get_opp_table_kref(struct opp_table *opp_table)
Viresh Kumar3bac42c2015-07-29 16:22:59 +0530943{
Viresh Kumarf067a982017-01-23 10:11:42 +0530944 kref_get(&opp_table->kref);
945}
946
Viresh Kumareb7c87432018-09-05 16:17:14 +0530947static struct opp_table *_opp_get_opp_table(struct device *dev, int index)
Viresh Kumarf067a982017-01-23 10:11:42 +0530948{
949 struct opp_table *opp_table;
950
951 /* Hold our table modification lock here */
952 mutex_lock(&opp_table_lock);
953
Viresh Kumar5b650b32017-01-23 10:11:48 +0530954 opp_table = _find_opp_table_unlocked(dev);
955 if (!IS_ERR(opp_table))
Viresh Kumarf067a982017-01-23 10:11:42 +0530956 goto unlock;
Viresh Kumarf067a982017-01-23 10:11:42 +0530957
Viresh Kumar283d55e2018-09-07 09:01:54 +0530958 opp_table = _managed_opp(dev, index);
959 if (opp_table) {
960 if (!_add_opp_dev_unlocked(dev, opp_table)) {
961 dev_pm_opp_put_opp_table(opp_table);
962 opp_table = NULL;
963 }
964 goto unlock;
965 }
966
Viresh Kumareb7c87432018-09-05 16:17:14 +0530967 opp_table = _allocate_opp_table(dev, index);
Viresh Kumarf067a982017-01-23 10:11:42 +0530968
969unlock:
970 mutex_unlock(&opp_table_lock);
971
972 return opp_table;
973}
Viresh Kumareb7c87432018-09-05 16:17:14 +0530974
975struct opp_table *dev_pm_opp_get_opp_table(struct device *dev)
976{
977 return _opp_get_opp_table(dev, 0);
978}
Viresh Kumarf067a982017-01-23 10:11:42 +0530979EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table);
980
Viresh Kumareb7c87432018-09-05 16:17:14 +0530981struct opp_table *dev_pm_opp_get_opp_table_indexed(struct device *dev,
982 int index)
983{
984 return _opp_get_opp_table(dev, index);
985}
986
Viresh Kumarb83c1892017-01-23 10:11:45 +0530987static void _opp_table_kref_release(struct kref *kref)
Viresh Kumarf067a982017-01-23 10:11:42 +0530988{
989 struct opp_table *opp_table = container_of(kref, struct opp_table, kref);
Viresh Kumarcdd6ed92018-09-12 12:35:19 +0530990 struct opp_device *opp_dev, *temp;
Viresh Kumar06441652015-07-29 16:23:04 +0530991
Viresh Kumar5d6d1062018-06-07 14:50:43 +0530992 _of_clear_opp_table(opp_table);
993
Viresh Kumard54974c2016-02-09 10:30:38 +0530994 /* Release clk */
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530995 if (!IS_ERR(opp_table->clk))
996 clk_put(opp_table->clk);
Viresh Kumard54974c2016-02-09 10:30:38 +0530997
Viresh Kumarcdd6ed92018-09-12 12:35:19 +0530998 WARN_ON(!list_empty(&opp_table->opp_list));
Viresh Kumar06441652015-07-29 16:23:04 +0530999
Viresh Kumarcdd6ed92018-09-12 12:35:19 +05301000 list_for_each_entry_safe(opp_dev, temp, &opp_table->dev_list, node) {
1001 /*
1002 * The OPP table is getting removed, drop the performance state
1003 * constraints.
1004 */
1005 if (opp_table->genpd_performance_state)
1006 dev_pm_genpd_set_performance_state((struct device *)(opp_dev->dev), 0);
Viresh Kumar06441652015-07-29 16:23:04 +05301007
Viresh Kumarcdd6ed92018-09-12 12:35:19 +05301008 _remove_opp_dev(opp_dev, opp_table);
1009 }
Viresh Kumar06441652015-07-29 16:23:04 +05301010
Viresh Kumar4f018bc2018-06-26 16:29:34 +05301011 mutex_destroy(&opp_table->genpd_virt_dev_lock);
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301012 mutex_destroy(&opp_table->lock);
Viresh Kumar052c6f12017-01-23 10:11:49 +05301013 list_del(&opp_table->node);
1014 kfree(opp_table);
Viresh Kumar3bac42c2015-07-29 16:22:59 +05301015
Viresh Kumarf067a982017-01-23 10:11:42 +05301016 mutex_unlock(&opp_table_lock);
1017}
1018
Viresh Kumard0e8ae62018-09-11 11:14:11 +05301019void _opp_remove_all_static(struct opp_table *opp_table)
1020{
1021 struct dev_pm_opp *opp, *tmp;
1022
1023 list_for_each_entry_safe(opp, tmp, &opp_table->opp_list, node) {
1024 if (!opp->dynamic)
1025 dev_pm_opp_put(opp);
1026 }
1027
1028 opp_table->parsed_static_opps = false;
1029}
1030
1031static void _opp_table_list_kref_release(struct kref *kref)
1032{
1033 struct opp_table *opp_table = container_of(kref, struct opp_table,
1034 list_kref);
1035
1036 _opp_remove_all_static(opp_table);
1037 mutex_unlock(&opp_table_lock);
1038}
1039
1040void _put_opp_list_kref(struct opp_table *opp_table)
1041{
1042 kref_put_mutex(&opp_table->list_kref, _opp_table_list_kref_release,
1043 &opp_table_lock);
1044}
1045
Viresh Kumarf067a982017-01-23 10:11:42 +05301046void dev_pm_opp_put_opp_table(struct opp_table *opp_table)
1047{
1048 kref_put_mutex(&opp_table->kref, _opp_table_kref_release,
1049 &opp_table_lock);
1050}
1051EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table);
1052
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301053void _opp_free(struct dev_pm_opp *opp)
Viresh Kumar969fceb2017-01-02 14:40:59 +05301054{
1055 kfree(opp);
Viresh Kumar969fceb2017-01-02 14:40:59 +05301056}
1057
Viresh Kumar1690d8b2019-01-04 15:14:33 +05301058static void _opp_kref_release(struct dev_pm_opp *opp,
1059 struct opp_table *opp_table)
Viresh Kumar129eec52014-11-27 08:54:06 +05301060{
1061 /*
1062 * Notify the changes in the availability of the operable
1063 * frequency/voltage list.
1064 */
Viresh Kumar052c6f12017-01-23 10:11:49 +05301065 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_REMOVE, opp);
Viresh Kumarda544b62018-06-07 14:50:43 +05301066 _of_opp_free_required_opps(opp_table, opp);
Viresh Kumardeaa5142015-11-11 07:59:01 +05301067 opp_debug_remove_one(opp);
Viresh Kumar052c6f12017-01-23 10:11:49 +05301068 list_del(&opp->node);
1069 kfree(opp);
Viresh Kumar1690d8b2019-01-04 15:14:33 +05301070}
Viresh Kumar129eec52014-11-27 08:54:06 +05301071
Viresh Kumar1690d8b2019-01-04 15:14:33 +05301072static void _opp_kref_release_unlocked(struct kref *kref)
1073{
1074 struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref);
1075 struct opp_table *opp_table = opp->opp_table;
1076
1077 _opp_kref_release(opp, opp_table);
1078}
1079
1080static void _opp_kref_release_locked(struct kref *kref)
1081{
1082 struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref);
1083 struct opp_table *opp_table = opp->opp_table;
1084
1085 _opp_kref_release(opp, opp_table);
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301086 mutex_unlock(&opp_table->lock);
Viresh Kumar129eec52014-11-27 08:54:06 +05301087}
1088
Viresh Kumara88bd2a2017-11-29 15:18:36 +05301089void dev_pm_opp_get(struct dev_pm_opp *opp)
Viresh Kumar8a31d9d92017-01-23 10:11:47 +05301090{
1091 kref_get(&opp->kref);
1092}
1093
Viresh Kumar70347642017-01-23 10:11:46 +05301094void dev_pm_opp_put(struct dev_pm_opp *opp)
1095{
Viresh Kumar1690d8b2019-01-04 15:14:33 +05301096 kref_put_mutex(&opp->kref, _opp_kref_release_locked,
1097 &opp->opp_table->lock);
Viresh Kumar70347642017-01-23 10:11:46 +05301098}
1099EXPORT_SYMBOL_GPL(dev_pm_opp_put);
1100
Viresh Kumar1690d8b2019-01-04 15:14:33 +05301101static void dev_pm_opp_put_unlocked(struct dev_pm_opp *opp)
1102{
1103 kref_put(&opp->kref, _opp_kref_release_unlocked);
1104}
1105
Viresh Kumar129eec52014-11-27 08:54:06 +05301106/**
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301107 * dev_pm_opp_remove() - Remove an OPP from OPP table
Viresh Kumar129eec52014-11-27 08:54:06 +05301108 * @dev: device for which we do this operation
1109 * @freq: OPP to remove with matching 'freq'
1110 *
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301111 * This function removes an opp from the opp table.
Viresh Kumar129eec52014-11-27 08:54:06 +05301112 */
1113void dev_pm_opp_remove(struct device *dev, unsigned long freq)
1114{
1115 struct dev_pm_opp *opp;
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301116 struct opp_table *opp_table;
Viresh Kumar129eec52014-11-27 08:54:06 +05301117 bool found = false;
1118
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301119 opp_table = _find_opp_table(dev);
1120 if (IS_ERR(opp_table))
Viresh Kumar5b650b32017-01-23 10:11:48 +05301121 return;
Viresh Kumar129eec52014-11-27 08:54:06 +05301122
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301123 mutex_lock(&opp_table->lock);
1124
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301125 list_for_each_entry(opp, &opp_table->opp_list, node) {
Viresh Kumar129eec52014-11-27 08:54:06 +05301126 if (opp->rate == freq) {
1127 found = true;
1128 break;
1129 }
1130 }
1131
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301132 mutex_unlock(&opp_table->lock);
1133
Viresh Kumar5b650b32017-01-23 10:11:48 +05301134 if (found) {
1135 dev_pm_opp_put(opp);
Viresh Kumar0ad8c622018-08-02 14:23:09 +05301136
1137 /* Drop the reference taken by dev_pm_opp_add() */
1138 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar5b650b32017-01-23 10:11:48 +05301139 } else {
Viresh Kumar129eec52014-11-27 08:54:06 +05301140 dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
1141 __func__, freq);
Viresh Kumar129eec52014-11-27 08:54:06 +05301142 }
1143
Viresh Kumar0ad8c622018-08-02 14:23:09 +05301144 /* Drop the reference taken by _find_opp_table() */
Viresh Kumar5b650b32017-01-23 10:11:48 +05301145 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar129eec52014-11-27 08:54:06 +05301146}
1147EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
1148
Viresh Kumar1690d8b2019-01-04 15:14:33 +05301149/**
1150 * dev_pm_opp_remove_all_dynamic() - Remove all dynamically created OPPs
1151 * @dev: device for which we do this operation
1152 *
1153 * This function removes all dynamically created OPPs from the opp table.
1154 */
1155void dev_pm_opp_remove_all_dynamic(struct device *dev)
1156{
1157 struct opp_table *opp_table;
1158 struct dev_pm_opp *opp, *temp;
1159 int count = 0;
1160
1161 opp_table = _find_opp_table(dev);
1162 if (IS_ERR(opp_table))
1163 return;
1164
1165 mutex_lock(&opp_table->lock);
1166 list_for_each_entry_safe(opp, temp, &opp_table->opp_list, node) {
1167 if (opp->dynamic) {
1168 dev_pm_opp_put_unlocked(opp);
1169 count++;
1170 }
1171 }
1172 mutex_unlock(&opp_table->lock);
1173
1174 /* Drop the references taken by dev_pm_opp_add() */
1175 while (count--)
1176 dev_pm_opp_put_opp_table(opp_table);
1177
1178 /* Drop the reference taken by _find_opp_table() */
1179 dev_pm_opp_put_opp_table(opp_table);
1180}
1181EXPORT_SYMBOL_GPL(dev_pm_opp_remove_all_dynamic);
1182
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301183struct dev_pm_opp *_opp_allocate(struct opp_table *table)
Viresh Kumar23dacf62015-07-29 16:23:01 +05301184{
1185 struct dev_pm_opp *opp;
Viresh Kumardfbe4672016-12-01 16:28:19 +05301186 int count, supply_size;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301187
Viresh Kumardfbe4672016-12-01 16:28:19 +05301188 /* Allocate space for at least one supply */
Viresh Kumar46f48ac2018-12-11 16:39:36 +05301189 count = table->regulator_count > 0 ? table->regulator_count : 1;
Viresh Kumardfbe4672016-12-01 16:28:19 +05301190 supply_size = sizeof(*opp->supplies) * count;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301191
Viresh Kumardfbe4672016-12-01 16:28:19 +05301192 /* allocate new OPP node and supplies structures */
1193 opp = kzalloc(sizeof(*opp) + supply_size, GFP_KERNEL);
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301194 if (!opp)
Viresh Kumar23dacf62015-07-29 16:23:01 +05301195 return NULL;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301196
Viresh Kumardfbe4672016-12-01 16:28:19 +05301197 /* Put the supplies at the end of the OPP structure as an empty array */
1198 opp->supplies = (struct dev_pm_opp_supply *)(opp + 1);
1199 INIT_LIST_HEAD(&opp->node);
1200
Viresh Kumar23dacf62015-07-29 16:23:01 +05301201 return opp;
1202}
1203
Viresh Kumar7d34d562016-02-09 10:30:34 +05301204static bool _opp_supported_by_regulators(struct dev_pm_opp *opp,
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301205 struct opp_table *opp_table)
Viresh Kumar7d34d562016-02-09 10:30:34 +05301206{
Viresh Kumardfbe4672016-12-01 16:28:19 +05301207 struct regulator *reg;
1208 int i;
Viresh Kumar7d34d562016-02-09 10:30:34 +05301209
Viresh Kumar90e35772018-12-11 16:32:47 +05301210 if (!opp_table->regulators)
1211 return true;
1212
Viresh Kumardfbe4672016-12-01 16:28:19 +05301213 for (i = 0; i < opp_table->regulator_count; i++) {
1214 reg = opp_table->regulators[i];
1215
1216 if (!regulator_is_supported_voltage(reg,
1217 opp->supplies[i].u_volt_min,
1218 opp->supplies[i].u_volt_max)) {
1219 pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n",
1220 __func__, opp->supplies[i].u_volt_min,
1221 opp->supplies[i].u_volt_max);
1222 return false;
1223 }
Viresh Kumar7d34d562016-02-09 10:30:34 +05301224 }
1225
1226 return true;
1227}
1228
Viresh Kumara1e8c132018-04-06 14:35:45 +05301229static int _opp_is_duplicate(struct device *dev, struct dev_pm_opp *new_opp,
1230 struct opp_table *opp_table,
1231 struct list_head **head)
1232{
1233 struct dev_pm_opp *opp;
1234
1235 /*
1236 * Insert new OPP in order of increasing frequency and discard if
1237 * already present.
1238 *
1239 * Need to use &opp_table->opp_list in the condition part of the 'for'
1240 * loop, don't replace it with head otherwise it will become an infinite
1241 * loop.
1242 */
1243 list_for_each_entry(opp, &opp_table->opp_list, node) {
1244 if (new_opp->rate > opp->rate) {
1245 *head = &opp->node;
1246 continue;
1247 }
1248
1249 if (new_opp->rate < opp->rate)
1250 return 0;
1251
1252 /* Duplicate OPPs */
1253 dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
1254 __func__, opp->rate, opp->supplies[0].u_volt,
1255 opp->available, new_opp->rate,
1256 new_opp->supplies[0].u_volt, new_opp->available);
1257
1258 /* Should we compare voltages for all regulators here ? */
1259 return opp->available &&
1260 new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST;
1261 }
1262
1263 return 0;
1264}
1265
Viresh Kumar7f8538e2017-01-02 14:40:55 +05301266/*
1267 * Returns:
1268 * 0: On success. And appropriate error message for duplicate OPPs.
1269 * -EBUSY: For OPP with same freq/volt and is available. The callers of
1270 * _opp_add() must return 0 if they receive -EBUSY from it. This is to make
1271 * sure we don't print error messages unnecessarily if different parts of
1272 * kernel try to initialize the OPP table.
1273 * -EEXIST: For OPP with same freq but different volt or is unavailable. This
1274 * should be considered an error by the callers of _opp_add().
1275 */
Viresh Kumarf47b72a2016-05-05 16:20:33 +05301276int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
Viresh Kumara1e8c132018-04-06 14:35:45 +05301277 struct opp_table *opp_table, bool rate_not_available)
Viresh Kumar23dacf62015-07-29 16:23:01 +05301278{
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301279 struct list_head *head;
Viresh Kumardeaa5142015-11-11 07:59:01 +05301280 int ret;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301281
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301282 mutex_lock(&opp_table->lock);
1283 head = &opp_table->opp_list;
1284
Viresh Kumara1e8c132018-04-06 14:35:45 +05301285 if (likely(!rate_not_available)) {
1286 ret = _opp_is_duplicate(dev, new_opp, opp_table, &head);
1287 if (ret) {
1288 mutex_unlock(&opp_table->lock);
1289 return ret;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301290 }
Viresh Kumar23dacf62015-07-29 16:23:01 +05301291 }
1292
Viresh Kumar052c6f12017-01-23 10:11:49 +05301293 list_add(&new_opp->node, head);
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301294 mutex_unlock(&opp_table->lock);
1295
1296 new_opp->opp_table = opp_table;
Viresh Kumar70347642017-01-23 10:11:46 +05301297 kref_init(&new_opp->kref);
Viresh Kumar23dacf62015-07-29 16:23:01 +05301298
Greg Kroah-Hartmana2dea4c2019-01-22 16:21:17 +01001299 opp_debug_create_one(new_opp, opp_table);
Viresh Kumardeaa5142015-11-11 07:59:01 +05301300
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301301 if (!_opp_supported_by_regulators(new_opp, opp_table)) {
Viresh Kumar7d34d562016-02-09 10:30:34 +05301302 new_opp->available = false;
1303 dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n",
1304 __func__, new_opp->rate);
1305 }
1306
Viresh Kumar23dacf62015-07-29 16:23:01 +05301307 return 0;
1308}
1309
Viresh Kumar737002b2015-07-29 16:22:58 +05301310/**
Viresh Kumarb64b9c3f2015-10-15 21:42:44 +05301311 * _opp_add_v1() - Allocate a OPP based on v1 bindings.
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301312 * @opp_table: OPP table
Nishanth Menone1f60b22010-10-13 00:13:10 +02001313 * @dev: device for which we do this operation
1314 * @freq: Frequency in Hz for this OPP
1315 * @u_volt: Voltage in uVolts for this OPP
1316 * @dynamic: Dynamically added OPPs.
1317 *
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301318 * This function adds an opp definition to the opp table and returns status.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001319 * The opp is made available by default and it can be controlled using
1320 * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
1321 *
Viresh Kumar8f8d37b2015-09-04 13:47:24 +05301322 * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table
1323 * and freed by dev_pm_opp_of_remove_table.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001324 *
Nishanth Menone1f60b22010-10-13 00:13:10 +02001325 * Return:
1326 * 0 On success OR
1327 * Duplicate OPPs (both freq and volt are same) and opp->available
1328 * -EEXIST Freq are same and volt are different OR
1329 * Duplicate OPPs (both freq and volt are same) and !opp->available
1330 * -ENOMEM Memory allocation failure
1331 */
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301332int _opp_add_v1(struct opp_table *opp_table, struct device *dev,
1333 unsigned long freq, long u_volt, bool dynamic)
Nishanth Menone1f60b22010-10-13 00:13:10 +02001334{
Viresh Kumar23dacf62015-07-29 16:23:01 +05301335 struct dev_pm_opp *new_opp;
Viresh Kumar50f8cfb2016-02-09 10:30:37 +05301336 unsigned long tol;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001337 int ret;
1338
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301339 new_opp = _opp_allocate(opp_table);
1340 if (!new_opp)
1341 return -ENOMEM;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301342
Nishanth Menone1f60b22010-10-13 00:13:10 +02001343 /* populate the opp table */
1344 new_opp->rate = freq;
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301345 tol = u_volt * opp_table->voltage_tolerance_v1 / 100;
Viresh Kumardfbe4672016-12-01 16:28:19 +05301346 new_opp->supplies[0].u_volt = u_volt;
1347 new_opp->supplies[0].u_volt_min = u_volt - tol;
1348 new_opp->supplies[0].u_volt_max = u_volt + tol;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001349 new_opp->available = true;
Viresh Kumaraa5f2f82015-07-29 16:23:00 +05301350 new_opp->dynamic = dynamic;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301351
Viresh Kumara1e8c132018-04-06 14:35:45 +05301352 ret = _opp_add(dev, new_opp, opp_table, false);
Viresh Kumar7f8538e2017-01-02 14:40:55 +05301353 if (ret) {
1354 /* Don't return error for duplicate OPPs */
1355 if (ret == -EBUSY)
1356 ret = 0;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301357 goto free_opp;
Viresh Kumar7f8538e2017-01-02 14:40:55 +05301358 }
Viresh Kumar23dacf62015-07-29 16:23:01 +05301359
Nishanth Menone1f60b22010-10-13 00:13:10 +02001360 /*
1361 * Notify the changes in the availability of the operable
1362 * frequency/voltage list.
1363 */
Viresh Kumar052c6f12017-01-23 10:11:49 +05301364 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001365 return 0;
1366
1367free_opp:
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301368 _opp_free(new_opp);
1369
Nishanth Menone1f60b22010-10-13 00:13:10 +02001370 return ret;
1371}
Viresh Kumar383934092014-11-25 16:04:18 +05301372
Viresh Kumar27465902015-07-29 16:23:02 +05301373/**
Viresh Kumar7de36b02015-12-09 08:01:46 +05301374 * dev_pm_opp_set_supported_hw() - Set supported platforms
1375 * @dev: Device for which supported-hw has to be set.
1376 * @versions: Array of hierarchy of versions to match.
1377 * @count: Number of elements in the array.
1378 *
1379 * This is required only for the V2 bindings, and it enables a platform to
1380 * specify the hierarchy of versions it supports. OPP layer will then enable
1381 * OPPs, which are available for those versions, based on its 'opp-supported-hw'
1382 * property.
Viresh Kumar7de36b02015-12-09 08:01:46 +05301383 */
Viresh Kumarfa301842017-01-23 10:11:43 +05301384struct opp_table *dev_pm_opp_set_supported_hw(struct device *dev,
1385 const u32 *versions, unsigned int count)
Viresh Kumar7de36b02015-12-09 08:01:46 +05301386{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301387 struct opp_table *opp_table;
Viresh Kumar7de36b02015-12-09 08:01:46 +05301388
Viresh Kumarfa301842017-01-23 10:11:43 +05301389 opp_table = dev_pm_opp_get_opp_table(dev);
1390 if (!opp_table)
1391 return ERR_PTR(-ENOMEM);
Viresh Kumar7de36b02015-12-09 08:01:46 +05301392
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301393 /* Make sure there are no concurrent readers while updating opp_table */
1394 WARN_ON(!list_empty(&opp_table->opp_list));
Viresh Kumar7de36b02015-12-09 08:01:46 +05301395
Viresh Kumar25419de2018-05-22 16:38:08 +05301396 /* Another CPU that shares the OPP table has set the property ? */
1397 if (opp_table->supported_hw)
1398 return opp_table;
Viresh Kumar7de36b02015-12-09 08:01:46 +05301399
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301400 opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions),
Viresh Kumar7de36b02015-12-09 08:01:46 +05301401 GFP_KERNEL);
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301402 if (!opp_table->supported_hw) {
Viresh Kumar25419de2018-05-22 16:38:08 +05301403 dev_pm_opp_put_opp_table(opp_table);
1404 return ERR_PTR(-ENOMEM);
Viresh Kumar7de36b02015-12-09 08:01:46 +05301405 }
1406
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301407 opp_table->supported_hw_count = count;
Viresh Kumarfa301842017-01-23 10:11:43 +05301408
1409 return opp_table;
Viresh Kumar7de36b02015-12-09 08:01:46 +05301410}
1411EXPORT_SYMBOL_GPL(dev_pm_opp_set_supported_hw);
1412
1413/**
1414 * dev_pm_opp_put_supported_hw() - Releases resources blocked for supported hw
Viresh Kumarfa301842017-01-23 10:11:43 +05301415 * @opp_table: OPP table returned by dev_pm_opp_set_supported_hw().
Viresh Kumar7de36b02015-12-09 08:01:46 +05301416 *
1417 * This is required only for the V2 bindings, and is called for a matching
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301418 * dev_pm_opp_set_supported_hw(). Until this is called, the opp_table structure
Viresh Kumar7de36b02015-12-09 08:01:46 +05301419 * will not be freed.
Viresh Kumar7de36b02015-12-09 08:01:46 +05301420 */
Viresh Kumarfa301842017-01-23 10:11:43 +05301421void dev_pm_opp_put_supported_hw(struct opp_table *opp_table)
Viresh Kumar7de36b02015-12-09 08:01:46 +05301422{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301423 /* Make sure there are no concurrent readers while updating opp_table */
1424 WARN_ON(!list_empty(&opp_table->opp_list));
Viresh Kumar7de36b02015-12-09 08:01:46 +05301425
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301426 kfree(opp_table->supported_hw);
1427 opp_table->supported_hw = NULL;
1428 opp_table->supported_hw_count = 0;
Viresh Kumar7de36b02015-12-09 08:01:46 +05301429
Viresh Kumarfa301842017-01-23 10:11:43 +05301430 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar7de36b02015-12-09 08:01:46 +05301431}
1432EXPORT_SYMBOL_GPL(dev_pm_opp_put_supported_hw);
1433
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301434/**
1435 * dev_pm_opp_set_prop_name() - Set prop-extn name
Viresh Kumara5da6442016-02-16 14:17:52 +05301436 * @dev: Device for which the prop-name has to be set.
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301437 * @name: name to postfix to properties.
1438 *
1439 * This is required only for the V2 bindings, and it enables a platform to
1440 * specify the extn to be used for certain property names. The properties to
1441 * which the extension will apply are opp-microvolt and opp-microamp. OPP core
1442 * should postfix the property name with -<name> while looking for them.
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301443 */
Viresh Kumarfa301842017-01-23 10:11:43 +05301444struct opp_table *dev_pm_opp_set_prop_name(struct device *dev, const char *name)
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301445{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301446 struct opp_table *opp_table;
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301447
Viresh Kumarfa301842017-01-23 10:11:43 +05301448 opp_table = dev_pm_opp_get_opp_table(dev);
1449 if (!opp_table)
1450 return ERR_PTR(-ENOMEM);
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301451
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301452 /* Make sure there are no concurrent readers while updating opp_table */
1453 WARN_ON(!list_empty(&opp_table->opp_list));
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301454
Viresh Kumar878ec1a2018-05-22 16:38:08 +05301455 /* Another CPU that shares the OPP table has set the property ? */
1456 if (opp_table->prop_name)
1457 return opp_table;
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301458
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301459 opp_table->prop_name = kstrdup(name, GFP_KERNEL);
1460 if (!opp_table->prop_name) {
Viresh Kumar878ec1a2018-05-22 16:38:08 +05301461 dev_pm_opp_put_opp_table(opp_table);
1462 return ERR_PTR(-ENOMEM);
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301463 }
1464
Viresh Kumarfa301842017-01-23 10:11:43 +05301465 return opp_table;
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301466}
1467EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name);
1468
1469/**
1470 * dev_pm_opp_put_prop_name() - Releases resources blocked for prop-name
Viresh Kumarfa301842017-01-23 10:11:43 +05301471 * @opp_table: OPP table returned by dev_pm_opp_set_prop_name().
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301472 *
1473 * This is required only for the V2 bindings, and is called for a matching
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301474 * dev_pm_opp_set_prop_name(). Until this is called, the opp_table structure
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301475 * will not be freed.
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301476 */
Viresh Kumarfa301842017-01-23 10:11:43 +05301477void dev_pm_opp_put_prop_name(struct opp_table *opp_table)
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301478{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301479 /* Make sure there are no concurrent readers while updating opp_table */
1480 WARN_ON(!list_empty(&opp_table->opp_list));
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301481
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301482 kfree(opp_table->prop_name);
1483 opp_table->prop_name = NULL;
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301484
Viresh Kumarfa301842017-01-23 10:11:43 +05301485 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301486}
1487EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name);
1488
Viresh Kumar94735582016-12-01 16:28:20 +05301489static int _allocate_set_opp_data(struct opp_table *opp_table)
1490{
1491 struct dev_pm_set_opp_data *data;
1492 int len, count = opp_table->regulator_count;
1493
Viresh Kumar90e35772018-12-11 16:32:47 +05301494 if (WARN_ON(!opp_table->regulators))
Viresh Kumar94735582016-12-01 16:28:20 +05301495 return -EINVAL;
1496
1497 /* space for set_opp_data */
1498 len = sizeof(*data);
1499
1500 /* space for old_opp.supplies and new_opp.supplies */
1501 len += 2 * sizeof(struct dev_pm_opp_supply) * count;
1502
1503 data = kzalloc(len, GFP_KERNEL);
1504 if (!data)
1505 return -ENOMEM;
1506
1507 data->old_opp.supplies = (void *)(data + 1);
1508 data->new_opp.supplies = data->old_opp.supplies + count;
1509
1510 opp_table->set_opp_data = data;
1511
1512 return 0;
1513}
1514
1515static void _free_set_opp_data(struct opp_table *opp_table)
1516{
1517 kfree(opp_table->set_opp_data);
1518 opp_table->set_opp_data = NULL;
1519}
1520
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301521/**
Viresh Kumardfbe4672016-12-01 16:28:19 +05301522 * dev_pm_opp_set_regulators() - Set regulator names for the device
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301523 * @dev: Device for which regulator name is being set.
Viresh Kumardfbe4672016-12-01 16:28:19 +05301524 * @names: Array of pointers to the names of the regulator.
1525 * @count: Number of regulators.
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301526 *
1527 * In order to support OPP switching, OPP layer needs to know the name of the
Viresh Kumardfbe4672016-12-01 16:28:19 +05301528 * device's regulators, as the core would be required to switch voltages as
1529 * well.
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301530 *
1531 * This must be called before any OPPs are initialized for the device.
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301532 */
Viresh Kumardfbe4672016-12-01 16:28:19 +05301533struct opp_table *dev_pm_opp_set_regulators(struct device *dev,
1534 const char * const names[],
1535 unsigned int count)
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301536{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301537 struct opp_table *opp_table;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301538 struct regulator *reg;
Viresh Kumardfbe4672016-12-01 16:28:19 +05301539 int ret, i;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301540
Viresh Kumarfa301842017-01-23 10:11:43 +05301541 opp_table = dev_pm_opp_get_opp_table(dev);
1542 if (!opp_table)
1543 return ERR_PTR(-ENOMEM);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301544
1545 /* This should be called before OPPs are initialized */
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301546 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301547 ret = -EBUSY;
1548 goto err;
1549 }
1550
Viresh Kumar779b7832018-05-22 16:38:08 +05301551 /* Another CPU that shares the OPP table has set the regulators ? */
1552 if (opp_table->regulators)
1553 return opp_table;
Viresh Kumardfbe4672016-12-01 16:28:19 +05301554
1555 opp_table->regulators = kmalloc_array(count,
1556 sizeof(*opp_table->regulators),
1557 GFP_KERNEL);
1558 if (!opp_table->regulators) {
1559 ret = -ENOMEM;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301560 goto err;
1561 }
1562
Viresh Kumardfbe4672016-12-01 16:28:19 +05301563 for (i = 0; i < count; i++) {
1564 reg = regulator_get_optional(dev, names[i]);
1565 if (IS_ERR(reg)) {
1566 ret = PTR_ERR(reg);
1567 if (ret != -EPROBE_DEFER)
1568 dev_err(dev, "%s: no regulator (%s) found: %d\n",
1569 __func__, names[i], ret);
1570 goto free_regulators;
1571 }
1572
1573 opp_table->regulators[i] = reg;
1574 }
1575
1576 opp_table->regulator_count = count;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301577
Viresh Kumar94735582016-12-01 16:28:20 +05301578 /* Allocate block only once to pass to set_opp() routines */
1579 ret = _allocate_set_opp_data(opp_table);
1580 if (ret)
1581 goto free_regulators;
1582
Stephen Boyd91291d92016-11-30 16:21:25 +05301583 return opp_table;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301584
Viresh Kumardfbe4672016-12-01 16:28:19 +05301585free_regulators:
1586 while (i != 0)
1587 regulator_put(opp_table->regulators[--i]);
1588
1589 kfree(opp_table->regulators);
1590 opp_table->regulators = NULL;
Viresh Kumar46f48ac2018-12-11 16:39:36 +05301591 opp_table->regulator_count = -1;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301592err:
Viresh Kumarfa301842017-01-23 10:11:43 +05301593 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301594
Stephen Boyd91291d92016-11-30 16:21:25 +05301595 return ERR_PTR(ret);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301596}
Viresh Kumardfbe4672016-12-01 16:28:19 +05301597EXPORT_SYMBOL_GPL(dev_pm_opp_set_regulators);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301598
1599/**
Viresh Kumardfbe4672016-12-01 16:28:19 +05301600 * dev_pm_opp_put_regulators() - Releases resources blocked for regulator
1601 * @opp_table: OPP table returned from dev_pm_opp_set_regulators().
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301602 */
Viresh Kumardfbe4672016-12-01 16:28:19 +05301603void dev_pm_opp_put_regulators(struct opp_table *opp_table)
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301604{
Viresh Kumardfbe4672016-12-01 16:28:19 +05301605 int i;
1606
Viresh Kumar779b7832018-05-22 16:38:08 +05301607 if (!opp_table->regulators)
1608 goto put_opp_table;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301609
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301610 /* Make sure there are no concurrent readers while updating opp_table */
1611 WARN_ON(!list_empty(&opp_table->opp_list));
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301612
Viresh Kumardfbe4672016-12-01 16:28:19 +05301613 for (i = opp_table->regulator_count - 1; i >= 0; i--)
1614 regulator_put(opp_table->regulators[i]);
1615
Viresh Kumar94735582016-12-01 16:28:20 +05301616 _free_set_opp_data(opp_table);
1617
Viresh Kumardfbe4672016-12-01 16:28:19 +05301618 kfree(opp_table->regulators);
1619 opp_table->regulators = NULL;
Viresh Kumar46f48ac2018-12-11 16:39:36 +05301620 opp_table->regulator_count = -1;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301621
Viresh Kumar779b7832018-05-22 16:38:08 +05301622put_opp_table:
Viresh Kumarfa301842017-01-23 10:11:43 +05301623 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301624}
Viresh Kumardfbe4672016-12-01 16:28:19 +05301625EXPORT_SYMBOL_GPL(dev_pm_opp_put_regulators);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301626
Viresh Kumar383934092014-11-25 16:04:18 +05301627/**
Viresh Kumar829a4e82017-06-21 10:29:13 +05301628 * dev_pm_opp_set_clkname() - Set clk name for the device
1629 * @dev: Device for which clk name is being set.
1630 * @name: Clk name.
1631 *
1632 * In order to support OPP switching, OPP layer needs to get pointer to the
1633 * clock for the device. Simple cases work fine without using this routine (i.e.
1634 * by passing connection-id as NULL), but for a device with multiple clocks
1635 * available, the OPP core needs to know the exact name of the clk to use.
1636 *
1637 * This must be called before any OPPs are initialized for the device.
1638 */
1639struct opp_table *dev_pm_opp_set_clkname(struct device *dev, const char *name)
1640{
1641 struct opp_table *opp_table;
1642 int ret;
1643
1644 opp_table = dev_pm_opp_get_opp_table(dev);
1645 if (!opp_table)
1646 return ERR_PTR(-ENOMEM);
1647
1648 /* This should be called before OPPs are initialized */
1649 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
1650 ret = -EBUSY;
1651 goto err;
1652 }
1653
1654 /* Already have default clk set, free it */
1655 if (!IS_ERR(opp_table->clk))
1656 clk_put(opp_table->clk);
1657
1658 /* Find clk for the device */
1659 opp_table->clk = clk_get(dev, name);
1660 if (IS_ERR(opp_table->clk)) {
1661 ret = PTR_ERR(opp_table->clk);
1662 if (ret != -EPROBE_DEFER) {
1663 dev_err(dev, "%s: Couldn't find clock: %d\n", __func__,
1664 ret);
1665 }
1666 goto err;
1667 }
1668
1669 return opp_table;
1670
1671err:
1672 dev_pm_opp_put_opp_table(opp_table);
1673
1674 return ERR_PTR(ret);
1675}
1676EXPORT_SYMBOL_GPL(dev_pm_opp_set_clkname);
1677
1678/**
1679 * dev_pm_opp_put_clkname() - Releases resources blocked for clk.
1680 * @opp_table: OPP table returned from dev_pm_opp_set_clkname().
1681 */
1682void dev_pm_opp_put_clkname(struct opp_table *opp_table)
1683{
1684 /* Make sure there are no concurrent readers while updating opp_table */
1685 WARN_ON(!list_empty(&opp_table->opp_list));
1686
1687 clk_put(opp_table->clk);
1688 opp_table->clk = ERR_PTR(-EINVAL);
1689
1690 dev_pm_opp_put_opp_table(opp_table);
1691}
1692EXPORT_SYMBOL_GPL(dev_pm_opp_put_clkname);
1693
1694/**
Viresh Kumar4dab1602016-12-01 16:28:21 +05301695 * dev_pm_opp_register_set_opp_helper() - Register custom set OPP helper
1696 * @dev: Device for which the helper is getting registered.
1697 * @set_opp: Custom set OPP helper.
1698 *
1699 * This is useful to support complex platforms (like platforms with multiple
1700 * regulators per device), instead of the generic OPP set rate helper.
1701 *
1702 * This must be called before any OPPs are initialized for the device.
Viresh Kumar4dab1602016-12-01 16:28:21 +05301703 */
Viresh Kumarfa301842017-01-23 10:11:43 +05301704struct opp_table *dev_pm_opp_register_set_opp_helper(struct device *dev,
Viresh Kumar4dab1602016-12-01 16:28:21 +05301705 int (*set_opp)(struct dev_pm_set_opp_data *data))
1706{
1707 struct opp_table *opp_table;
Viresh Kumar4dab1602016-12-01 16:28:21 +05301708
1709 if (!set_opp)
Viresh Kumarfa301842017-01-23 10:11:43 +05301710 return ERR_PTR(-EINVAL);
Viresh Kumar4dab1602016-12-01 16:28:21 +05301711
Viresh Kumarfa301842017-01-23 10:11:43 +05301712 opp_table = dev_pm_opp_get_opp_table(dev);
1713 if (!opp_table)
1714 return ERR_PTR(-ENOMEM);
Viresh Kumar4dab1602016-12-01 16:28:21 +05301715
1716 /* This should be called before OPPs are initialized */
1717 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
Viresh Kumar5019acc2018-05-22 16:38:08 +05301718 dev_pm_opp_put_opp_table(opp_table);
1719 return ERR_PTR(-EBUSY);
Viresh Kumar4dab1602016-12-01 16:28:21 +05301720 }
1721
Viresh Kumar5019acc2018-05-22 16:38:08 +05301722 /* Another CPU that shares the OPP table has set the helper ? */
1723 if (!opp_table->set_opp)
1724 opp_table->set_opp = set_opp;
Viresh Kumar4dab1602016-12-01 16:28:21 +05301725
Viresh Kumarfa301842017-01-23 10:11:43 +05301726 return opp_table;
Viresh Kumar4dab1602016-12-01 16:28:21 +05301727}
1728EXPORT_SYMBOL_GPL(dev_pm_opp_register_set_opp_helper);
1729
1730/**
Viresh Kumar604a7ae2017-10-05 17:26:21 +05301731 * dev_pm_opp_unregister_set_opp_helper() - Releases resources blocked for
Viresh Kumar4dab1602016-12-01 16:28:21 +05301732 * set_opp helper
Viresh Kumarfa301842017-01-23 10:11:43 +05301733 * @opp_table: OPP table returned from dev_pm_opp_register_set_opp_helper().
Viresh Kumar4dab1602016-12-01 16:28:21 +05301734 *
Viresh Kumarfa301842017-01-23 10:11:43 +05301735 * Release resources blocked for platform specific set_opp helper.
Viresh Kumar4dab1602016-12-01 16:28:21 +05301736 */
Viresh Kumar604a7ae2017-10-05 17:26:21 +05301737void dev_pm_opp_unregister_set_opp_helper(struct opp_table *opp_table)
Viresh Kumar4dab1602016-12-01 16:28:21 +05301738{
Viresh Kumar4dab1602016-12-01 16:28:21 +05301739 /* Make sure there are no concurrent readers while updating opp_table */
1740 WARN_ON(!list_empty(&opp_table->opp_list));
1741
1742 opp_table->set_opp = NULL;
Viresh Kumarfa301842017-01-23 10:11:43 +05301743 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar4dab1602016-12-01 16:28:21 +05301744}
Viresh Kumar604a7ae2017-10-05 17:26:21 +05301745EXPORT_SYMBOL_GPL(dev_pm_opp_unregister_set_opp_helper);
Viresh Kumar4dab1602016-12-01 16:28:21 +05301746
1747/**
Viresh Kumar4f018bc2018-06-26 16:29:34 +05301748 * dev_pm_opp_set_genpd_virt_dev - Set virtual genpd device for an index
1749 * @dev: Consumer device for which the genpd device is getting set.
1750 * @virt_dev: virtual genpd device.
1751 * @index: index.
1752 *
1753 * Multiple generic power domains for a device are supported with the help of
1754 * virtual genpd devices, which are created for each consumer device - genpd
1755 * pair. These are the device structures which are attached to the power domain
1756 * and are required by the OPP core to set the performance state of the genpd.
1757 *
1758 * This helper will normally be called by the consumer driver of the device
1759 * "dev", as only that has details of the genpd devices.
1760 *
1761 * This helper needs to be called once for each of those virtual devices, but
1762 * only if multiple domains are available for a device. Otherwise the original
1763 * device structure will be used instead by the OPP core.
1764 */
1765struct opp_table *dev_pm_opp_set_genpd_virt_dev(struct device *dev,
1766 struct device *virt_dev,
1767 int index)
1768{
1769 struct opp_table *opp_table;
1770
1771 opp_table = dev_pm_opp_get_opp_table(dev);
1772 if (!opp_table)
1773 return ERR_PTR(-ENOMEM);
1774
1775 mutex_lock(&opp_table->genpd_virt_dev_lock);
1776
1777 if (unlikely(!opp_table->genpd_virt_devs ||
1778 index >= opp_table->required_opp_count ||
1779 opp_table->genpd_virt_devs[index])) {
1780
1781 dev_err(dev, "Invalid request to set required device\n");
1782 dev_pm_opp_put_opp_table(opp_table);
1783 mutex_unlock(&opp_table->genpd_virt_dev_lock);
1784
1785 return ERR_PTR(-EINVAL);
1786 }
1787
1788 opp_table->genpd_virt_devs[index] = virt_dev;
1789 mutex_unlock(&opp_table->genpd_virt_dev_lock);
1790
1791 return opp_table;
1792}
1793
1794/**
1795 * dev_pm_opp_put_genpd_virt_dev() - Releases resources blocked for genpd device.
1796 * @opp_table: OPP table returned by dev_pm_opp_set_genpd_virt_dev().
1797 * @virt_dev: virtual genpd device.
1798 *
1799 * This releases the resource previously acquired with a call to
1800 * dev_pm_opp_set_genpd_virt_dev(). The consumer driver shall call this helper
1801 * if it doesn't want OPP core to update performance state of a power domain
1802 * anymore.
1803 */
1804void dev_pm_opp_put_genpd_virt_dev(struct opp_table *opp_table,
1805 struct device *virt_dev)
1806{
1807 int i;
1808
1809 /*
1810 * Acquire genpd_virt_dev_lock to make sure virt_dev isn't getting
1811 * used in parallel.
1812 */
1813 mutex_lock(&opp_table->genpd_virt_dev_lock);
1814
1815 for (i = 0; i < opp_table->required_opp_count; i++) {
1816 if (opp_table->genpd_virt_devs[i] != virt_dev)
1817 continue;
1818
1819 opp_table->genpd_virt_devs[i] = NULL;
1820 dev_pm_opp_put_opp_table(opp_table);
1821
1822 /* Drop the vote */
1823 dev_pm_genpd_set_performance_state(virt_dev, 0);
1824 break;
1825 }
1826
1827 mutex_unlock(&opp_table->genpd_virt_dev_lock);
1828
1829 if (unlikely(i == opp_table->required_opp_count))
1830 dev_err(virt_dev, "Failed to find required device entry\n");
1831}
1832
1833/**
Viresh Kumarc8a59102018-11-02 14:36:42 +05301834 * dev_pm_opp_xlate_performance_state() - Find required OPP's pstate for src_table.
1835 * @src_table: OPP table which has dst_table as one of its required OPP table.
1836 * @dst_table: Required OPP table of the src_table.
1837 * @pstate: Current performance state of the src_table.
1838 *
1839 * This Returns pstate of the OPP (present in @dst_table) pointed out by the
1840 * "required-opps" property of the OPP (present in @src_table) which has
1841 * performance state set to @pstate.
1842 *
1843 * Return: Zero or positive performance state on success, otherwise negative
1844 * value on errors.
1845 */
1846int dev_pm_opp_xlate_performance_state(struct opp_table *src_table,
1847 struct opp_table *dst_table,
1848 unsigned int pstate)
1849{
1850 struct dev_pm_opp *opp;
1851 int dest_pstate = -EINVAL;
1852 int i;
1853
1854 if (!pstate)
1855 return 0;
1856
1857 /*
1858 * Normally the src_table will have the "required_opps" property set to
1859 * point to one of the OPPs in the dst_table, but in some cases the
1860 * genpd and its master have one to one mapping of performance states
1861 * and so none of them have the "required-opps" property set. Return the
1862 * pstate of the src_table as it is in such cases.
1863 */
1864 if (!src_table->required_opp_count)
1865 return pstate;
1866
1867 for (i = 0; i < src_table->required_opp_count; i++) {
1868 if (src_table->required_opp_tables[i]->np == dst_table->np)
1869 break;
1870 }
1871
1872 if (unlikely(i == src_table->required_opp_count)) {
1873 pr_err("%s: Couldn't find matching OPP table (%p: %p)\n",
1874 __func__, src_table, dst_table);
1875 return -EINVAL;
1876 }
1877
1878 mutex_lock(&src_table->lock);
1879
1880 list_for_each_entry(opp, &src_table->opp_list, node) {
1881 if (opp->pstate == pstate) {
1882 dest_pstate = opp->required_opps[i]->pstate;
1883 goto unlock;
1884 }
1885 }
1886
1887 pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__, src_table,
1888 dst_table);
1889
1890unlock:
1891 mutex_unlock(&src_table->lock);
1892
1893 return dest_pstate;
1894}
1895
1896/**
Nishanth Menone1f60b22010-10-13 00:13:10 +02001897 * dev_pm_opp_add() - Add an OPP table from a table definitions
1898 * @dev: device for which we do this operation
Nishanth Menon47d43ba2013-09-19 16:03:51 -05001899 * @freq: Frequency in Hz for this OPP
Nishanth Menone1f60b22010-10-13 00:13:10 +02001900 * @u_volt: Voltage in uVolts for this OPP
1901 *
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301902 * This function adds an opp definition to the opp table and returns status.
Nishanth Menon47d43ba2013-09-19 16:03:51 -05001903 * The opp is made available by default and it can be controlled using
Nishanth Menone1f60b22010-10-13 00:13:10 +02001904 * dev_pm_opp_enable/disable functions.
1905 *
Viresh Kumara7470db2014-11-25 16:04:17 +05301906 * Return:
1907 * 0 On success OR
1908 * Duplicate OPPs (both freq and volt are same) and opp->available
1909 * -EEXIST Freq are same and volt are different OR
1910 * Duplicate OPPs (both freq and volt are same) and !opp->available
Viresh Kumar383934092014-11-25 16:04:18 +05301911 * -ENOMEM Memory allocation failure
Viresh Kumara7470db2014-11-25 16:04:17 +05301912 */
Nishanth Menone1f60b22010-10-13 00:13:10 +02001913int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
1914{
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301915 struct opp_table *opp_table;
1916 int ret;
1917
Viresh Kumarb83c1892017-01-23 10:11:45 +05301918 opp_table = dev_pm_opp_get_opp_table(dev);
1919 if (!opp_table)
1920 return -ENOMEM;
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301921
Viresh Kumar46f48ac2018-12-11 16:39:36 +05301922 /* Fix regulator count for dynamic OPPs */
1923 opp_table->regulator_count = 1;
1924
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301925 ret = _opp_add_v1(opp_table, dev, freq, u_volt, true);
Viresh Kumar0ad8c622018-08-02 14:23:09 +05301926 if (ret)
1927 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301928
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301929 return ret;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001930}
1931EXPORT_SYMBOL_GPL(dev_pm_opp_add);
1932
Nishanth Menone1f60b22010-10-13 00:13:10 +02001933/**
Nishanth Menon327854c2014-12-24 11:22:56 -06001934 * _opp_set_availability() - helper to set the availability of an opp
Nishanth Menone1f60b22010-10-13 00:13:10 +02001935 * @dev: device for which we do this operation
1936 * @freq: OPP frequency to modify availability
1937 * @availability_req: availability status requested for this opp
1938 *
Viresh Kumar052c6f12017-01-23 10:11:49 +05301939 * Set the availability of an OPP, opp_{enable,disable} share a common logic
1940 * which is isolated here.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001941 *
Nishanth Menon984f16c2014-12-24 11:22:57 -06001942 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
Stephen Boyde1a2d492015-09-24 12:28:44 -07001943 * copy operation, returns 0 if no modification was done OR modification was
Nishanth Menone1f60b22010-10-13 00:13:10 +02001944 * successful.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001945 */
Nishanth Menon327854c2014-12-24 11:22:56 -06001946static int _opp_set_availability(struct device *dev, unsigned long freq,
1947 bool availability_req)
Nishanth Menone1f60b22010-10-13 00:13:10 +02001948{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301949 struct opp_table *opp_table;
Viresh Kumara7f39872017-01-23 10:11:50 +05301950 struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001951 int r = 0;
1952
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301953 /* Find the opp_table */
1954 opp_table = _find_opp_table(dev);
1955 if (IS_ERR(opp_table)) {
1956 r = PTR_ERR(opp_table);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001957 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
Viresh Kumara7f39872017-01-23 10:11:50 +05301958 return r;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001959 }
1960
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301961 mutex_lock(&opp_table->lock);
1962
Nishanth Menone1f60b22010-10-13 00:13:10 +02001963 /* Do we have the frequency? */
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301964 list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
Nishanth Menone1f60b22010-10-13 00:13:10 +02001965 if (tmp_opp->rate == freq) {
1966 opp = tmp_opp;
1967 break;
1968 }
1969 }
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301970
Nishanth Menone1f60b22010-10-13 00:13:10 +02001971 if (IS_ERR(opp)) {
1972 r = PTR_ERR(opp);
1973 goto unlock;
1974 }
1975
1976 /* Is update really needed? */
1977 if (opp->available == availability_req)
1978 goto unlock;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001979
Viresh Kumara7f39872017-01-23 10:11:50 +05301980 opp->available = availability_req;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001981
Viresh Kumare4d8ae02017-09-21 10:44:36 -07001982 dev_pm_opp_get(opp);
1983 mutex_unlock(&opp_table->lock);
1984
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001985 /* Notify the change of the OPP availability */
1986 if (availability_req)
Viresh Kumar052c6f12017-01-23 10:11:49 +05301987 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE,
Viresh Kumara7f39872017-01-23 10:11:50 +05301988 opp);
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001989 else
Viresh Kumar052c6f12017-01-23 10:11:49 +05301990 blocking_notifier_call_chain(&opp_table->head,
Viresh Kumara7f39872017-01-23 10:11:50 +05301991 OPP_EVENT_DISABLE, opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001992
Viresh Kumare4d8ae02017-09-21 10:44:36 -07001993 dev_pm_opp_put(opp);
1994 goto put_table;
1995
Nishanth Menone1f60b22010-10-13 00:13:10 +02001996unlock:
Viresh Kumar5b650b32017-01-23 10:11:48 +05301997 mutex_unlock(&opp_table->lock);
Viresh Kumare4d8ae02017-09-21 10:44:36 -07001998put_table:
Viresh Kumar5b650b32017-01-23 10:11:48 +05301999 dev_pm_opp_put_opp_table(opp_table);
Nishanth Menone1f60b22010-10-13 00:13:10 +02002000 return r;
2001}
2002
2003/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -05002004 * dev_pm_opp_enable() - Enable a specific OPP
Nishanth Menone1f60b22010-10-13 00:13:10 +02002005 * @dev: device for which we do this operation
2006 * @freq: OPP frequency to enable
2007 *
2008 * Enables a provided opp. If the operation is valid, this returns 0, else the
2009 * corresponding error value. It is meant to be used for users an OPP available
Nishanth Menon5d4879c2013-09-19 16:03:50 -05002010 * after being temporarily made unavailable with dev_pm_opp_disable.
Nishanth Menone1f60b22010-10-13 00:13:10 +02002011 *
Nishanth Menon984f16c2014-12-24 11:22:57 -06002012 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
Stephen Boyde1a2d492015-09-24 12:28:44 -07002013 * copy operation, returns 0 if no modification was done OR modification was
Nishanth Menon984f16c2014-12-24 11:22:57 -06002014 * successful.
Nishanth Menone1f60b22010-10-13 00:13:10 +02002015 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -05002016int dev_pm_opp_enable(struct device *dev, unsigned long freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +02002017{
Nishanth Menon327854c2014-12-24 11:22:56 -06002018 return _opp_set_availability(dev, freq, true);
Nishanth Menone1f60b22010-10-13 00:13:10 +02002019}
Nishanth Menon5d4879c2013-09-19 16:03:50 -05002020EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
Nishanth Menone1f60b22010-10-13 00:13:10 +02002021
2022/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -05002023 * dev_pm_opp_disable() - Disable a specific OPP
Nishanth Menone1f60b22010-10-13 00:13:10 +02002024 * @dev: device for which we do this operation
2025 * @freq: OPP frequency to disable
2026 *
2027 * Disables a provided opp. If the operation is valid, this returns
2028 * 0, else the corresponding error value. It is meant to be a temporary
2029 * control by users to make this OPP not available until the circumstances are
Nishanth Menon5d4879c2013-09-19 16:03:50 -05002030 * right to make it available again (with a call to dev_pm_opp_enable).
Nishanth Menone1f60b22010-10-13 00:13:10 +02002031 *
Nishanth Menon984f16c2014-12-24 11:22:57 -06002032 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
Stephen Boyde1a2d492015-09-24 12:28:44 -07002033 * copy operation, returns 0 if no modification was done OR modification was
Nishanth Menon984f16c2014-12-24 11:22:57 -06002034 * successful.
Nishanth Menone1f60b22010-10-13 00:13:10 +02002035 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -05002036int dev_pm_opp_disable(struct device *dev, unsigned long freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +02002037{
Nishanth Menon327854c2014-12-24 11:22:56 -06002038 return _opp_set_availability(dev, freq, false);
Nishanth Menone1f60b22010-10-13 00:13:10 +02002039}
Nishanth Menon5d4879c2013-09-19 16:03:50 -05002040EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
Nishanth Menone1f60b22010-10-13 00:13:10 +02002041
MyungJoo Ham03ca3702011-09-30 22:35:12 +02002042/**
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05302043 * dev_pm_opp_register_notifier() - Register OPP notifier for the device
2044 * @dev: Device for which notifier needs to be registered
2045 * @nb: Notifier block to be registered
Nishanth Menon984f16c2014-12-24 11:22:57 -06002046 *
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05302047 * Return: 0 on success or a negative error value.
MyungJoo Ham03ca3702011-09-30 22:35:12 +02002048 */
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05302049int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb)
MyungJoo Ham03ca3702011-09-30 22:35:12 +02002050{
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05302051 struct opp_table *opp_table;
2052 int ret;
MyungJoo Ham03ca3702011-09-30 22:35:12 +02002053
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05302054 opp_table = _find_opp_table(dev);
Viresh Kumar5b650b32017-01-23 10:11:48 +05302055 if (IS_ERR(opp_table))
2056 return PTR_ERR(opp_table);
2057
Viresh Kumar052c6f12017-01-23 10:11:49 +05302058 ret = blocking_notifier_chain_register(&opp_table->head, nb);
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05302059
Viresh Kumar5b650b32017-01-23 10:11:48 +05302060 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05302061
2062 return ret;
MyungJoo Ham03ca3702011-09-30 22:35:12 +02002063}
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05302064EXPORT_SYMBOL(dev_pm_opp_register_notifier);
2065
2066/**
2067 * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device
2068 * @dev: Device for which notifier needs to be unregistered
2069 * @nb: Notifier block to be unregistered
2070 *
2071 * Return: 0 on success or a negative error value.
2072 */
2073int dev_pm_opp_unregister_notifier(struct device *dev,
2074 struct notifier_block *nb)
2075{
2076 struct opp_table *opp_table;
2077 int ret;
2078
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05302079 opp_table = _find_opp_table(dev);
Viresh Kumar5b650b32017-01-23 10:11:48 +05302080 if (IS_ERR(opp_table))
2081 return PTR_ERR(opp_table);
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05302082
Viresh Kumar052c6f12017-01-23 10:11:49 +05302083 ret = blocking_notifier_chain_unregister(&opp_table->head, nb);
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05302084
Viresh Kumar5b650b32017-01-23 10:11:48 +05302085 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05302086
2087 return ret;
2088}
2089EXPORT_SYMBOL(dev_pm_opp_unregister_notifier);
Shawn Guob496dfb2012-09-05 01:09:12 +02002090
Viresh Kumar2a4eb732018-09-13 13:14:36 +05302091void _dev_pm_opp_find_and_remove_table(struct device *dev)
Viresh Kumar737002b2015-07-29 16:22:58 +05302092{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05302093 struct opp_table *opp_table;
Viresh Kumar737002b2015-07-29 16:22:58 +05302094
Viresh Kumar2c2709d2016-02-16 14:17:53 +05302095 /* Check for existing table for 'dev' */
2096 opp_table = _find_opp_table(dev);
2097 if (IS_ERR(opp_table)) {
2098 int error = PTR_ERR(opp_table);
Viresh Kumar737002b2015-07-29 16:22:58 +05302099
2100 if (error != -ENODEV)
Viresh Kumar2c2709d2016-02-16 14:17:53 +05302101 WARN(1, "%s: opp_table: %d\n",
Viresh Kumar737002b2015-07-29 16:22:58 +05302102 IS_ERR_OR_NULL(dev) ?
2103 "Invalid device" : dev_name(dev),
2104 error);
Viresh Kumar5b650b32017-01-23 10:11:48 +05302105 return;
Viresh Kumar737002b2015-07-29 16:22:58 +05302106 }
2107
Viresh Kumarcdd6ed92018-09-12 12:35:19 +05302108 _put_opp_list_kref(opp_table);
Viresh Kumar737002b2015-07-29 16:22:58 +05302109
Viresh Kumarcdd6ed92018-09-12 12:35:19 +05302110 /* Drop reference taken by _find_opp_table() */
2111 dev_pm_opp_put_opp_table(opp_table);
2112
2113 /* Drop reference taken while the OPP table was added */
Viresh Kumar5b650b32017-01-23 10:11:48 +05302114 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar737002b2015-07-29 16:22:58 +05302115}
Viresh Kumar129eec52014-11-27 08:54:06 +05302116
2117/**
Sudeep Holla411466c2016-05-03 15:05:04 +01002118 * dev_pm_opp_remove_table() - Free all OPPs associated with the device
Viresh Kumar2c2709d2016-02-16 14:17:53 +05302119 * @dev: device pointer used to lookup OPP table.
Viresh Kumar129eec52014-11-27 08:54:06 +05302120 *
Sudeep Holla411466c2016-05-03 15:05:04 +01002121 * Free both OPPs created using static entries present in DT and the
2122 * dynamically added entries.
Viresh Kumar129eec52014-11-27 08:54:06 +05302123 */
Sudeep Holla411466c2016-05-03 15:05:04 +01002124void dev_pm_opp_remove_table(struct device *dev)
Viresh Kumar129eec52014-11-27 08:54:06 +05302125{
Viresh Kumar2a4eb732018-09-13 13:14:36 +05302126 _dev_pm_opp_find_and_remove_table(dev);
Viresh Kumar8d4d4e92015-06-12 17:10:38 +05302127}
Sudeep Holla411466c2016-05-03 15:05:04 +01002128EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table);