blob: 57d88a275de0a3a4dd6daa25a0d7c8e6d8eb01cf [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/**
Bartlomiej Zolnierkiewicz19445b22015-07-09 17:43:35 +0200134 * dev_pm_opp_is_turbo() - Returns if opp is turbo OPP or not
135 * @opp: opp for which turbo mode is being verified
136 *
137 * Turbo OPPs are not for normal use, and can be enabled (under certain
138 * conditions) for short duration of times to finish high throughput work
139 * quickly. Running on them for longer times may overheat the chip.
140 *
141 * Return: true if opp is turbo opp, else false.
Bartlomiej Zolnierkiewicz19445b22015-07-09 17:43:35 +0200142 */
143bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp)
144{
Viresh Kumar052c6f12017-01-23 10:11:49 +0530145 if (IS_ERR_OR_NULL(opp) || !opp->available) {
Bartlomiej Zolnierkiewicz19445b22015-07-09 17:43:35 +0200146 pr_err("%s: Invalid parameters\n", __func__);
147 return false;
148 }
149
Viresh Kumar052c6f12017-01-23 10:11:49 +0530150 return opp->turbo;
Bartlomiej Zolnierkiewicz19445b22015-07-09 17:43:35 +0200151}
152EXPORT_SYMBOL_GPL(dev_pm_opp_is_turbo);
153
154/**
Viresh Kumar3ca9bb32015-07-29 16:23:03 +0530155 * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds
156 * @dev: device for which we do this operation
157 *
158 * Return: This function returns the max clock latency in nanoseconds.
Viresh Kumar3ca9bb32015-07-29 16:23:03 +0530159 */
160unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev)
161{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530162 struct opp_table *opp_table;
Viresh Kumar3ca9bb32015-07-29 16:23:03 +0530163 unsigned long clock_latency_ns;
164
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530165 opp_table = _find_opp_table(dev);
166 if (IS_ERR(opp_table))
Viresh Kumar5b650b32017-01-23 10:11:48 +0530167 return 0;
Viresh Kumar3ca9bb32015-07-29 16:23:03 +0530168
Viresh Kumar5b650b32017-01-23 10:11:48 +0530169 clock_latency_ns = opp_table->clock_latency_ns_max;
170
171 dev_pm_opp_put_opp_table(opp_table);
172
Viresh Kumar3ca9bb32015-07-29 16:23:03 +0530173 return clock_latency_ns;
174}
175EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency);
176
177/**
Viresh Kumar655c9df2016-02-09 10:30:35 +0530178 * dev_pm_opp_get_max_volt_latency() - Get max voltage latency in nanoseconds
179 * @dev: device for which we do this operation
180 *
181 * Return: This function returns the max voltage latency in nanoseconds.
Viresh Kumar655c9df2016-02-09 10:30:35 +0530182 */
183unsigned long dev_pm_opp_get_max_volt_latency(struct device *dev)
184{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530185 struct opp_table *opp_table;
Viresh Kumar655c9df2016-02-09 10:30:35 +0530186 struct dev_pm_opp *opp;
Viresh Kumar478256b2017-05-23 09:32:11 +0530187 struct regulator *reg;
Viresh Kumar655c9df2016-02-09 10:30:35 +0530188 unsigned long latency_ns = 0;
Viresh Kumardfbe4672016-12-01 16:28:19 +0530189 int ret, i, count;
190 struct {
191 unsigned long min;
192 unsigned long max;
193 } *uV;
194
Viresh Kumarcdd3e612017-01-23 10:11:51 +0530195 opp_table = _find_opp_table(dev);
196 if (IS_ERR(opp_table))
197 return 0;
198
Viresh Kumardfbe4672016-12-01 16:28:19 +0530199 /* Regulator may not be required for the device */
Viresh Kumar90e35772018-12-11 16:32:47 +0530200 if (!opp_table->regulators)
Viresh Kumarcdd3e612017-01-23 10:11:51 +0530201 goto put_opp_table;
Viresh Kumardfbe4672016-12-01 16:28:19 +0530202
Viresh Kumar90e35772018-12-11 16:32:47 +0530203 count = opp_table->regulator_count;
204
Viresh Kumardfbe4672016-12-01 16:28:19 +0530205 uV = kmalloc_array(count, sizeof(*uV), GFP_KERNEL);
206 if (!uV)
Viresh Kumar478256b2017-05-23 09:32:11 +0530207 goto put_opp_table;
Viresh Kumar655c9df2016-02-09 10:30:35 +0530208
Viresh Kumar052c6f12017-01-23 10:11:49 +0530209 mutex_lock(&opp_table->lock);
210
Viresh Kumardfbe4672016-12-01 16:28:19 +0530211 for (i = 0; i < count; i++) {
212 uV[i].min = ~0;
213 uV[i].max = 0;
Viresh Kumar655c9df2016-02-09 10:30:35 +0530214
Viresh Kumar052c6f12017-01-23 10:11:49 +0530215 list_for_each_entry(opp, &opp_table->opp_list, node) {
Viresh Kumardfbe4672016-12-01 16:28:19 +0530216 if (!opp->available)
217 continue;
218
219 if (opp->supplies[i].u_volt_min < uV[i].min)
220 uV[i].min = opp->supplies[i].u_volt_min;
221 if (opp->supplies[i].u_volt_max > uV[i].max)
222 uV[i].max = opp->supplies[i].u_volt_max;
223 }
Viresh Kumar655c9df2016-02-09 10:30:35 +0530224 }
225
Viresh Kumar052c6f12017-01-23 10:11:49 +0530226 mutex_unlock(&opp_table->lock);
Viresh Kumar655c9df2016-02-09 10:30:35 +0530227
228 /*
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530229 * The caller needs to ensure that opp_table (and hence the regulator)
Viresh Kumar655c9df2016-02-09 10:30:35 +0530230 * isn't freed, while we are executing this routine.
231 */
Andrzej Hajda8cc31112017-02-20 19:57:57 +0100232 for (i = 0; i < count; i++) {
Viresh Kumar478256b2017-05-23 09:32:11 +0530233 reg = opp_table->regulators[i];
Viresh Kumardfbe4672016-12-01 16:28:19 +0530234 ret = regulator_set_voltage_time(reg, uV[i].min, uV[i].max);
235 if (ret > 0)
236 latency_ns += ret * 1000;
237 }
238
Viresh Kumardfbe4672016-12-01 16:28:19 +0530239 kfree(uV);
Viresh Kumarcdd3e612017-01-23 10:11:51 +0530240put_opp_table:
241 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar655c9df2016-02-09 10:30:35 +0530242
243 return latency_ns;
244}
245EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_volt_latency);
246
247/**
Viresh Kumar21743442016-02-09 10:30:36 +0530248 * dev_pm_opp_get_max_transition_latency() - Get max transition latency in
249 * nanoseconds
250 * @dev: device for which we do this operation
251 *
252 * Return: This function returns the max transition latency, in nanoseconds, to
253 * switch from one OPP to other.
Viresh Kumar21743442016-02-09 10:30:36 +0530254 */
255unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev)
256{
257 return dev_pm_opp_get_max_volt_latency(dev) +
258 dev_pm_opp_get_max_clock_latency(dev);
259}
260EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_transition_latency);
261
262/**
Viresh Kumar3aa26a32017-01-02 14:41:02 +0530263 * dev_pm_opp_get_suspend_opp_freq() - Get frequency of suspend opp in Hz
Bartlomiej Zolnierkiewicz4eafbd12015-09-08 18:41:01 +0200264 * @dev: device for which we do this operation
265 *
Viresh Kumar3aa26a32017-01-02 14:41:02 +0530266 * Return: This function returns the frequency of the OPP marked as suspend_opp
267 * if one is available, else returns 0;
Bartlomiej Zolnierkiewicz4eafbd12015-09-08 18:41:01 +0200268 */
Viresh Kumar3aa26a32017-01-02 14:41:02 +0530269unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev)
Bartlomiej Zolnierkiewicz4eafbd12015-09-08 18:41:01 +0200270{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530271 struct opp_table *opp_table;
Viresh Kumar3aa26a32017-01-02 14:41:02 +0530272 unsigned long freq = 0;
Bartlomiej Zolnierkiewicz4eafbd12015-09-08 18:41:01 +0200273
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530274 opp_table = _find_opp_table(dev);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530275 if (IS_ERR(opp_table))
276 return 0;
Bartlomiej Zolnierkiewicz4eafbd12015-09-08 18:41:01 +0200277
Viresh Kumar5b650b32017-01-23 10:11:48 +0530278 if (opp_table->suspend_opp && opp_table->suspend_opp->available)
279 freq = dev_pm_opp_get_freq(opp_table->suspend_opp);
Viresh Kumar3aa26a32017-01-02 14:41:02 +0530280
Viresh Kumar5b650b32017-01-23 10:11:48 +0530281 dev_pm_opp_put_opp_table(opp_table);
282
Viresh Kumar3aa26a32017-01-02 14:41:02 +0530283 return freq;
Bartlomiej Zolnierkiewicz4eafbd12015-09-08 18:41:01 +0200284}
Viresh Kumar3aa26a32017-01-02 14:41:02 +0530285EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp_freq);
Bartlomiej Zolnierkiewicz4eafbd12015-09-08 18:41:01 +0200286
Viresh Kumara1e8c132018-04-06 14:35:45 +0530287int _get_opp_count(struct opp_table *opp_table)
288{
289 struct dev_pm_opp *opp;
290 int count = 0;
291
292 mutex_lock(&opp_table->lock);
293
294 list_for_each_entry(opp, &opp_table->opp_list, node) {
295 if (opp->available)
296 count++;
297 }
298
299 mutex_unlock(&opp_table->lock);
300
301 return count;
302}
303
Bartlomiej Zolnierkiewicz4eafbd12015-09-08 18:41:01 +0200304/**
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530305 * dev_pm_opp_get_opp_count() - Get number of opps available in the opp table
Nishanth Menone1f60b22010-10-13 00:13:10 +0200306 * @dev: device for which we do this operation
307 *
Nishanth Menon984f16c2014-12-24 11:22:57 -0600308 * Return: This function returns the number of available opps if there are any,
Nishanth Menone1f60b22010-10-13 00:13:10 +0200309 * else returns 0 if none or the corresponding error value.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200310 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500311int dev_pm_opp_get_opp_count(struct device *dev)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200312{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530313 struct opp_table *opp_table;
Viresh Kumara1e8c132018-04-06 14:35:45 +0530314 int count;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200315
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530316 opp_table = _find_opp_table(dev);
317 if (IS_ERR(opp_table)) {
318 count = PTR_ERR(opp_table);
Fabio Estevam035ed072017-09-29 14:39:49 -0300319 dev_dbg(dev, "%s: OPP table not found (%d)\n",
Dmitry Torokhovb4718c02014-12-16 15:09:38 -0800320 __func__, count);
Viresh Kumar09f662f2018-10-03 15:22:03 +0530321 return count;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200322 }
323
Viresh Kumara1e8c132018-04-06 14:35:45 +0530324 count = _get_opp_count(opp_table);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530325 dev_pm_opp_put_opp_table(opp_table);
326
Nishanth Menone1f60b22010-10-13 00:13:10 +0200327 return count;
328}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500329EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200330
331/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500332 * dev_pm_opp_find_freq_exact() - search for an exact frequency
Nishanth Menone1f60b22010-10-13 00:13:10 +0200333 * @dev: device for which we do this operation
334 * @freq: frequency to search for
Nishanth Menon7ae49612011-02-25 23:46:18 +0100335 * @available: true/false - match for available opp
Nishanth Menone1f60b22010-10-13 00:13:10 +0200336 *
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530337 * Return: Searches for exact match in the opp table and returns pointer to the
Nishanth Menon984f16c2014-12-24 11:22:57 -0600338 * matching opp if found, else returns ERR_PTR in case of error and should
339 * be handled using IS_ERR. Error return values can be:
Nishanth Menon07797262012-10-24 22:00:12 +0200340 * EINVAL: for bad pointer
341 * ERANGE: no match found for search
342 * ENODEV: if device not found in list of registered devices
Nishanth Menone1f60b22010-10-13 00:13:10 +0200343 *
344 * Note: available is a modifier for the search. if available=true, then the
345 * match is for exact matching frequency and is available in the stored OPP
346 * table. if false, the match is for exact frequency which is not available.
347 *
348 * This provides a mechanism to enable an opp which is not available currently
349 * or the opposite as well.
350 *
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530351 * The callers are required to call dev_pm_opp_put() for the returned OPP after
352 * use.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200353 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500354struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
355 unsigned long freq,
356 bool available)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200357{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530358 struct opp_table *opp_table;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500359 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200360
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530361 opp_table = _find_opp_table(dev);
362 if (IS_ERR(opp_table)) {
363 int r = PTR_ERR(opp_table);
364
365 dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200366 return ERR_PTR(r);
367 }
368
Viresh Kumar052c6f12017-01-23 10:11:49 +0530369 mutex_lock(&opp_table->lock);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530370
Viresh Kumar052c6f12017-01-23 10:11:49 +0530371 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
Nishanth Menone1f60b22010-10-13 00:13:10 +0200372 if (temp_opp->available == available &&
373 temp_opp->rate == freq) {
374 opp = temp_opp;
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530375
376 /* Increment the reference count of OPP */
377 dev_pm_opp_get(opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200378 break;
379 }
380 }
381
Viresh Kumar052c6f12017-01-23 10:11:49 +0530382 mutex_unlock(&opp_table->lock);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530383 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530384
Nishanth Menone1f60b22010-10-13 00:13:10 +0200385 return opp;
386}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500387EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200388
Jisheng Zhang067b7ce2016-07-25 14:11:16 +0800389static noinline struct dev_pm_opp *_find_freq_ceil(struct opp_table *opp_table,
390 unsigned long *freq)
391{
392 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
393
Viresh Kumar052c6f12017-01-23 10:11:49 +0530394 mutex_lock(&opp_table->lock);
395
396 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
Jisheng Zhang067b7ce2016-07-25 14:11:16 +0800397 if (temp_opp->available && temp_opp->rate >= *freq) {
398 opp = temp_opp;
399 *freq = opp->rate;
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530400
401 /* Increment the reference count of OPP */
402 dev_pm_opp_get(opp);
Jisheng Zhang067b7ce2016-07-25 14:11:16 +0800403 break;
404 }
405 }
406
Viresh Kumar052c6f12017-01-23 10:11:49 +0530407 mutex_unlock(&opp_table->lock);
408
Jisheng Zhang067b7ce2016-07-25 14:11:16 +0800409 return opp;
410}
411
Nishanth Menone1f60b22010-10-13 00:13:10 +0200412/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500413 * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
Nishanth Menone1f60b22010-10-13 00:13:10 +0200414 * @dev: device for which we do this operation
415 * @freq: Start frequency
416 *
417 * Search for the matching ceil *available* OPP from a starting freq
418 * for a device.
419 *
Nishanth Menon984f16c2014-12-24 11:22:57 -0600420 * Return: matching *opp and refreshes *freq accordingly, else returns
Nishanth Menon07797262012-10-24 22:00:12 +0200421 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
422 * values can be:
423 * EINVAL: for bad pointer
424 * ERANGE: no match found for search
425 * ENODEV: if device not found in list of registered devices
Nishanth Menone1f60b22010-10-13 00:13:10 +0200426 *
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530427 * The callers are required to call dev_pm_opp_put() for the returned OPP after
428 * use.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200429 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500430struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
431 unsigned long *freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200432{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530433 struct opp_table *opp_table;
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530434 struct dev_pm_opp *opp;
Dmitry Torokhovb02ded22014-12-16 15:09:36 -0800435
Nishanth Menone1f60b22010-10-13 00:13:10 +0200436 if (!dev || !freq) {
437 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
438 return ERR_PTR(-EINVAL);
439 }
440
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530441 opp_table = _find_opp_table(dev);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530442 if (IS_ERR(opp_table))
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530443 return ERR_CAST(opp_table);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530444
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530445 opp = _find_freq_ceil(opp_table, freq);
446
Viresh Kumar5b650b32017-01-23 10:11:48 +0530447 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530448
449 return opp;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200450}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500451EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200452
453/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500454 * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
Nishanth Menone1f60b22010-10-13 00:13:10 +0200455 * @dev: device for which we do this operation
456 * @freq: Start frequency
457 *
458 * Search for the matching floor *available* OPP from a starting freq
459 * for a device.
460 *
Nishanth Menon984f16c2014-12-24 11:22:57 -0600461 * Return: matching *opp and refreshes *freq accordingly, else returns
Nishanth Menon07797262012-10-24 22:00:12 +0200462 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
463 * values can be:
464 * EINVAL: for bad pointer
465 * ERANGE: no match found for search
466 * ENODEV: if device not found in list of registered devices
Nishanth Menone1f60b22010-10-13 00:13:10 +0200467 *
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530468 * The callers are required to call dev_pm_opp_put() for the returned OPP after
469 * use.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200470 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500471struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
472 unsigned long *freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200473{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530474 struct opp_table *opp_table;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500475 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200476
477 if (!dev || !freq) {
478 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
479 return ERR_PTR(-EINVAL);
480 }
481
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530482 opp_table = _find_opp_table(dev);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530483 if (IS_ERR(opp_table))
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530484 return ERR_CAST(opp_table);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530485
Viresh Kumar052c6f12017-01-23 10:11:49 +0530486 mutex_lock(&opp_table->lock);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200487
Viresh Kumar052c6f12017-01-23 10:11:49 +0530488 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
Nishanth Menone1f60b22010-10-13 00:13:10 +0200489 if (temp_opp->available) {
490 /* go to the next node, before choosing prev */
491 if (temp_opp->rate > *freq)
492 break;
493 else
494 opp = temp_opp;
495 }
496 }
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530497
498 /* Increment the reference count of OPP */
499 if (!IS_ERR(opp))
500 dev_pm_opp_get(opp);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530501 mutex_unlock(&opp_table->lock);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530502 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530503
Nishanth Menone1f60b22010-10-13 00:13:10 +0200504 if (!IS_ERR(opp))
505 *freq = opp->rate;
506
507 return opp;
508}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500509EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200510
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530511static int _set_opp_voltage(struct device *dev, struct regulator *reg,
Viresh Kumarce317812016-12-01 16:28:18 +0530512 struct dev_pm_opp_supply *supply)
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530513{
514 int ret;
515
516 /* Regulator not available for device */
517 if (IS_ERR(reg)) {
518 dev_dbg(dev, "%s: regulator not available: %ld\n", __func__,
519 PTR_ERR(reg));
520 return 0;
521 }
522
Viresh Kumarce317812016-12-01 16:28:18 +0530523 dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__,
524 supply->u_volt_min, supply->u_volt, supply->u_volt_max);
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530525
Viresh Kumarce317812016-12-01 16:28:18 +0530526 ret = regulator_set_voltage_triplet(reg, supply->u_volt_min,
527 supply->u_volt, supply->u_volt_max);
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530528 if (ret)
529 dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n",
Viresh Kumarce317812016-12-01 16:28:18 +0530530 __func__, supply->u_volt_min, supply->u_volt,
531 supply->u_volt_max, ret);
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530532
533 return ret;
534}
535
Viresh Kumar285881b2019-01-31 13:53:25 +0530536static inline int _generic_set_opp_clk_only(struct device *dev, struct clk *clk,
537 unsigned long freq)
Viresh Kumar94735582016-12-01 16:28:20 +0530538{
539 int ret;
540
541 ret = clk_set_rate(clk, freq);
542 if (ret) {
543 dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
544 ret);
545 }
546
547 return ret;
548}
549
Viresh Kumarc74b32f2017-05-23 09:32:10 +0530550static int _generic_set_opp_regulator(const struct opp_table *opp_table,
551 struct device *dev,
552 unsigned long old_freq,
553 unsigned long freq,
554 struct dev_pm_opp_supply *old_supply,
555 struct dev_pm_opp_supply *new_supply)
Viresh Kumar94735582016-12-01 16:28:20 +0530556{
Viresh Kumarc74b32f2017-05-23 09:32:10 +0530557 struct regulator *reg = opp_table->regulators[0];
Viresh Kumar94735582016-12-01 16:28:20 +0530558 int ret;
559
560 /* This function only supports single regulator per device */
Viresh Kumarc74b32f2017-05-23 09:32:10 +0530561 if (WARN_ON(opp_table->regulator_count > 1)) {
Viresh Kumar94735582016-12-01 16:28:20 +0530562 dev_err(dev, "multiple regulators are not supported\n");
563 return -EINVAL;
564 }
565
566 /* Scaling up? Scale voltage before frequency */
Waldemar Rymarkiewiczc5c2a972018-06-14 15:56:08 +0200567 if (freq >= old_freq) {
Viresh Kumar94735582016-12-01 16:28:20 +0530568 ret = _set_opp_voltage(dev, reg, new_supply);
569 if (ret)
570 goto restore_voltage;
571 }
572
573 /* Change frequency */
Viresh Kumar285881b2019-01-31 13:53:25 +0530574 ret = _generic_set_opp_clk_only(dev, opp_table->clk, freq);
Viresh Kumar94735582016-12-01 16:28:20 +0530575 if (ret)
576 goto restore_voltage;
577
578 /* Scaling down? Scale voltage after frequency */
579 if (freq < old_freq) {
580 ret = _set_opp_voltage(dev, reg, new_supply);
581 if (ret)
582 goto restore_freq;
583 }
584
585 return 0;
586
587restore_freq:
Viresh Kumar285881b2019-01-31 13:53:25 +0530588 if (_generic_set_opp_clk_only(dev, opp_table->clk, old_freq))
Viresh Kumar94735582016-12-01 16:28:20 +0530589 dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n",
590 __func__, old_freq);
591restore_voltage:
592 /* This shouldn't harm even if the voltages weren't updated earlier */
Viresh Kumarc74b32f2017-05-23 09:32:10 +0530593 if (old_supply)
Viresh Kumar94735582016-12-01 16:28:20 +0530594 _set_opp_voltage(dev, reg, old_supply);
595
596 return ret;
597}
598
Viresh Kumar7e5359932018-06-12 14:47:03 +0530599static int _set_opp_custom(const struct opp_table *opp_table,
600 struct device *dev, unsigned long old_freq,
601 unsigned long freq,
602 struct dev_pm_opp_supply *old_supply,
603 struct dev_pm_opp_supply *new_supply)
604{
605 struct dev_pm_set_opp_data *data;
606 int size;
607
608 data = opp_table->set_opp_data;
609 data->regulators = opp_table->regulators;
610 data->regulator_count = opp_table->regulator_count;
611 data->clk = opp_table->clk;
612 data->dev = dev;
613
614 data->old_opp.rate = old_freq;
615 size = sizeof(*old_supply) * opp_table->regulator_count;
616 if (IS_ERR(old_supply))
617 memset(data->old_opp.supplies, 0, size);
618 else
619 memcpy(data->old_opp.supplies, old_supply, size);
620
621 data->new_opp.rate = freq;
622 memcpy(data->new_opp.supplies, new_supply, size);
623
624 return opp_table->set_opp(data);
625}
626
Viresh Kumarca1b5d72018-06-14 10:03:26 +0530627/* This is only called for PM domain for now */
628static int _set_required_opps(struct device *dev,
629 struct opp_table *opp_table,
630 struct dev_pm_opp *opp)
631{
632 struct opp_table **required_opp_tables = opp_table->required_opp_tables;
633 struct device **genpd_virt_devs = opp_table->genpd_virt_devs;
634 unsigned int pstate;
635 int i, ret = 0;
636
637 if (!required_opp_tables)
638 return 0;
639
640 /* Single genpd case */
641 if (!genpd_virt_devs) {
642 pstate = opp->required_opps[0]->pstate;
643 ret = dev_pm_genpd_set_performance_state(dev, pstate);
644 if (ret) {
645 dev_err(dev, "Failed to set performance state of %s: %d (%d)\n",
646 dev_name(dev), pstate, ret);
647 }
648 return ret;
649 }
650
651 /* Multiple genpd case */
652
653 /*
654 * Acquire genpd_virt_dev_lock to make sure we don't use a genpd_dev
655 * after it is freed from another thread.
656 */
657 mutex_lock(&opp_table->genpd_virt_dev_lock);
658
659 for (i = 0; i < opp_table->required_opp_count; i++) {
660 pstate = opp->required_opps[i]->pstate;
661
662 if (!genpd_virt_devs[i])
663 continue;
664
665 ret = dev_pm_genpd_set_performance_state(genpd_virt_devs[i], pstate);
666 if (ret) {
667 dev_err(dev, "Failed to set performance rate of %s: %d (%d)\n",
668 dev_name(genpd_virt_devs[i]), pstate, ret);
669 break;
670 }
671 }
672 mutex_unlock(&opp_table->genpd_virt_dev_lock);
673
674 return ret;
675}
676
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530677/**
678 * dev_pm_opp_set_rate() - Configure new OPP based on frequency
679 * @dev: device for which we do this operation
680 * @target_freq: frequency to achieve
681 *
682 * This configures the power-supplies and clock source to the levels specified
683 * by the OPP corresponding to the target_freq.
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530684 */
685int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq)
686{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530687 struct opp_table *opp_table;
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530688 unsigned long freq, old_freq;
Viresh Kumar94735582016-12-01 16:28:20 +0530689 struct dev_pm_opp *old_opp, *opp;
Viresh Kumar94735582016-12-01 16:28:20 +0530690 struct clk *clk;
Viresh Kumar7e5359932018-06-12 14:47:03 +0530691 int ret;
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530692
693 if (unlikely(!target_freq)) {
694 dev_err(dev, "%s: Invalid target frequency %lu\n", __func__,
695 target_freq);
696 return -EINVAL;
697 }
698
Viresh Kumar052c6f12017-01-23 10:11:49 +0530699 opp_table = _find_opp_table(dev);
700 if (IS_ERR(opp_table)) {
701 dev_err(dev, "%s: device opp doesn't exist\n", __func__);
702 return PTR_ERR(opp_table);
703 }
704
705 clk = opp_table->clk;
706 if (IS_ERR(clk)) {
707 dev_err(dev, "%s: No clock available for the device\n",
708 __func__);
709 ret = PTR_ERR(clk);
710 goto put_opp_table;
711 }
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530712
713 freq = clk_round_rate(clk, target_freq);
714 if ((long)freq <= 0)
715 freq = target_freq;
716
717 old_freq = clk_get_rate(clk);
718
719 /* Return early if nothing to do */
720 if (old_freq == freq) {
721 dev_dbg(dev, "%s: old/new frequencies (%lu Hz) are same, nothing to do\n",
722 __func__, freq);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530723 ret = 0;
724 goto put_opp_table;
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530725 }
726
Jisheng Zhang067b7ce2016-07-25 14:11:16 +0800727 old_opp = _find_freq_ceil(opp_table, &old_freq);
Arnd Bergmann4df27c92016-09-15 17:38:38 +0200728 if (IS_ERR(old_opp)) {
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530729 dev_err(dev, "%s: failed to find current OPP for freq %lu (%ld)\n",
730 __func__, old_freq, PTR_ERR(old_opp));
731 }
732
Jisheng Zhang067b7ce2016-07-25 14:11:16 +0800733 opp = _find_freq_ceil(opp_table, &freq);
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530734 if (IS_ERR(opp)) {
735 ret = PTR_ERR(opp);
736 dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n",
737 __func__, freq, ret);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530738 goto put_old_opp;
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530739 }
740
Viresh Kumar94735582016-12-01 16:28:20 +0530741 dev_dbg(dev, "%s: switching OPP: %lu Hz --> %lu Hz\n", __func__,
742 old_freq, freq);
Viresh Kumardfbe4672016-12-01 16:28:19 +0530743
Viresh Kumarca1b5d72018-06-14 10:03:26 +0530744 /* Scaling up? Configure required OPPs before frequency */
745 if (freq > old_freq) {
746 ret = _set_required_opps(dev, opp_table, opp);
747 if (ret)
748 goto put_opp;
749 }
750
Viresh Kumar7e5359932018-06-12 14:47:03 +0530751 if (opp_table->set_opp) {
752 ret = _set_opp_custom(opp_table, dev, old_freq, freq,
753 IS_ERR(old_opp) ? NULL : old_opp->supplies,
754 opp->supplies);
755 } else if (opp_table->regulators) {
Viresh Kumarc74b32f2017-05-23 09:32:10 +0530756 ret = _generic_set_opp_regulator(opp_table, dev, old_freq, freq,
757 IS_ERR(old_opp) ? NULL : old_opp->supplies,
758 opp->supplies);
759 } else {
Viresh Kumar7e5359932018-06-12 14:47:03 +0530760 /* Only frequency scaling */
Viresh Kumar285881b2019-01-31 13:53:25 +0530761 ret = _generic_set_opp_clk_only(dev, clk, freq);
Viresh Kumardfbe4672016-12-01 16:28:19 +0530762 }
763
Viresh Kumarca1b5d72018-06-14 10:03:26 +0530764 /* Scaling down? Configure required OPPs after frequency */
765 if (!ret && freq < old_freq) {
766 ret = _set_required_opps(dev, opp_table, opp);
767 if (ret)
768 dev_err(dev, "Failed to set required opps: %d\n", ret);
769 }
770
771put_opp:
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530772 dev_pm_opp_put(opp);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530773put_old_opp:
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530774 if (!IS_ERR(old_opp))
775 dev_pm_opp_put(old_opp);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530776put_opp_table:
Viresh Kumar5b650b32017-01-23 10:11:48 +0530777 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530778 return ret;
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530779}
780EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate);
781
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530782/* OPP-dev Helpers */
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530783static void _remove_opp_dev(struct opp_device *opp_dev,
784 struct opp_table *opp_table)
Viresh Kumar06441652015-07-29 16:23:04 +0530785{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530786 opp_debug_unregister(opp_dev, opp_table);
787 list_del(&opp_dev->node);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530788 kfree(opp_dev);
Viresh Kumar06441652015-07-29 16:23:04 +0530789}
790
Viresh Kumar283d55e2018-09-07 09:01:54 +0530791static struct opp_device *_add_opp_dev_unlocked(const struct device *dev,
792 struct opp_table *opp_table)
Viresh Kumar06441652015-07-29 16:23:04 +0530793{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530794 struct opp_device *opp_dev;
Viresh Kumardeaa5142015-11-11 07:59:01 +0530795 int ret;
Viresh Kumar06441652015-07-29 16:23:04 +0530796
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530797 opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL);
798 if (!opp_dev)
Viresh Kumar06441652015-07-29 16:23:04 +0530799 return NULL;
800
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530801 /* Initialize opp-dev */
802 opp_dev->dev = dev;
Viresh Kumar3d255692018-08-03 07:05:21 +0530803
Viresh Kumar052c6f12017-01-23 10:11:49 +0530804 list_add(&opp_dev->node, &opp_table->dev_list);
Viresh Kumar06441652015-07-29 16:23:04 +0530805
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530806 /* Create debugfs entries for the opp_table */
807 ret = opp_debug_register(opp_dev, opp_table);
Viresh Kumardeaa5142015-11-11 07:59:01 +0530808 if (ret)
809 dev_err(dev, "%s: Failed to register opp debugfs (%d)\n",
810 __func__, ret);
Viresh Kumar283d55e2018-09-07 09:01:54 +0530811
812 return opp_dev;
813}
814
815struct opp_device *_add_opp_dev(const struct device *dev,
816 struct opp_table *opp_table)
817{
818 struct opp_device *opp_dev;
819
820 mutex_lock(&opp_table->lock);
821 opp_dev = _add_opp_dev_unlocked(dev, opp_table);
Viresh Kumar3d255692018-08-03 07:05:21 +0530822 mutex_unlock(&opp_table->lock);
Viresh Kumardeaa5142015-11-11 07:59:01 +0530823
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530824 return opp_dev;
Viresh Kumar06441652015-07-29 16:23:04 +0530825}
826
Viresh Kumareb7c87432018-09-05 16:17:14 +0530827static struct opp_table *_allocate_opp_table(struct device *dev, int index)
Viresh Kumar07cce742014-12-10 09:45:34 +0530828{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530829 struct opp_table *opp_table;
830 struct opp_device *opp_dev;
Viresh Kumard54974c2016-02-09 10:30:38 +0530831 int ret;
Viresh Kumar07cce742014-12-10 09:45:34 +0530832
833 /*
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530834 * Allocate a new OPP table. In the infrequent case where a new
Viresh Kumar07cce742014-12-10 09:45:34 +0530835 * device is needed to be added, we pay this penalty.
836 */
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530837 opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL);
838 if (!opp_table)
Viresh Kumar07cce742014-12-10 09:45:34 +0530839 return NULL;
840
Viresh Kumar3d255692018-08-03 07:05:21 +0530841 mutex_init(&opp_table->lock);
Viresh Kumar4f018bc2018-06-26 16:29:34 +0530842 mutex_init(&opp_table->genpd_virt_dev_lock);
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530843 INIT_LIST_HEAD(&opp_table->dev_list);
Viresh Kumar06441652015-07-29 16:23:04 +0530844
Viresh Kumar46f48ac2018-12-11 16:39:36 +0530845 /* Mark regulator count uninitialized */
846 opp_table->regulator_count = -1;
847
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530848 opp_dev = _add_opp_dev(dev, opp_table);
849 if (!opp_dev) {
850 kfree(opp_table);
Viresh Kumar06441652015-07-29 16:23:04 +0530851 return NULL;
852 }
853
Viresh Kumareb7c87432018-09-05 16:17:14 +0530854 _of_init_opp_table(opp_table, dev, index);
Viresh Kumar50f8cfb2016-02-09 10:30:37 +0530855
Viresh Kumard54974c2016-02-09 10:30:38 +0530856 /* Find clk for the device */
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530857 opp_table->clk = clk_get(dev, NULL);
858 if (IS_ERR(opp_table->clk)) {
859 ret = PTR_ERR(opp_table->clk);
Viresh Kumard54974c2016-02-09 10:30:38 +0530860 if (ret != -EPROBE_DEFER)
861 dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__,
862 ret);
863 }
864
Viresh Kumar052c6f12017-01-23 10:11:49 +0530865 BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head);
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530866 INIT_LIST_HEAD(&opp_table->opp_list);
Viresh Kumarf067a982017-01-23 10:11:42 +0530867 kref_init(&opp_table->kref);
Viresh Kumar07cce742014-12-10 09:45:34 +0530868
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530869 /* Secure the device table modification */
Viresh Kumar052c6f12017-01-23 10:11:49 +0530870 list_add(&opp_table->node, &opp_tables);
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530871 return opp_table;
Viresh Kumar07cce742014-12-10 09:45:34 +0530872}
873
Viresh Kumarf067a982017-01-23 10:11:42 +0530874void _get_opp_table_kref(struct opp_table *opp_table)
Viresh Kumar3bac42c2015-07-29 16:22:59 +0530875{
Viresh Kumarf067a982017-01-23 10:11:42 +0530876 kref_get(&opp_table->kref);
877}
878
Viresh Kumareb7c87432018-09-05 16:17:14 +0530879static struct opp_table *_opp_get_opp_table(struct device *dev, int index)
Viresh Kumarf067a982017-01-23 10:11:42 +0530880{
881 struct opp_table *opp_table;
882
883 /* Hold our table modification lock here */
884 mutex_lock(&opp_table_lock);
885
Viresh Kumar5b650b32017-01-23 10:11:48 +0530886 opp_table = _find_opp_table_unlocked(dev);
887 if (!IS_ERR(opp_table))
Viresh Kumarf067a982017-01-23 10:11:42 +0530888 goto unlock;
Viresh Kumarf067a982017-01-23 10:11:42 +0530889
Viresh Kumar283d55e2018-09-07 09:01:54 +0530890 opp_table = _managed_opp(dev, index);
891 if (opp_table) {
892 if (!_add_opp_dev_unlocked(dev, opp_table)) {
893 dev_pm_opp_put_opp_table(opp_table);
894 opp_table = NULL;
895 }
896 goto unlock;
897 }
898
Viresh Kumareb7c87432018-09-05 16:17:14 +0530899 opp_table = _allocate_opp_table(dev, index);
Viresh Kumarf067a982017-01-23 10:11:42 +0530900
901unlock:
902 mutex_unlock(&opp_table_lock);
903
904 return opp_table;
905}
Viresh Kumareb7c87432018-09-05 16:17:14 +0530906
907struct opp_table *dev_pm_opp_get_opp_table(struct device *dev)
908{
909 return _opp_get_opp_table(dev, 0);
910}
Viresh Kumarf067a982017-01-23 10:11:42 +0530911EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table);
912
Viresh Kumareb7c87432018-09-05 16:17:14 +0530913struct opp_table *dev_pm_opp_get_opp_table_indexed(struct device *dev,
914 int index)
915{
916 return _opp_get_opp_table(dev, index);
917}
918
Viresh Kumarb83c1892017-01-23 10:11:45 +0530919static void _opp_table_kref_release(struct kref *kref)
Viresh Kumarf067a982017-01-23 10:11:42 +0530920{
921 struct opp_table *opp_table = container_of(kref, struct opp_table, kref);
Viresh Kumarcdd6ed92018-09-12 12:35:19 +0530922 struct opp_device *opp_dev, *temp;
Viresh Kumar06441652015-07-29 16:23:04 +0530923
Viresh Kumar5d6d1062018-06-07 14:50:43 +0530924 _of_clear_opp_table(opp_table);
925
Viresh Kumard54974c2016-02-09 10:30:38 +0530926 /* Release clk */
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530927 if (!IS_ERR(opp_table->clk))
928 clk_put(opp_table->clk);
Viresh Kumard54974c2016-02-09 10:30:38 +0530929
Viresh Kumarcdd6ed92018-09-12 12:35:19 +0530930 WARN_ON(!list_empty(&opp_table->opp_list));
Viresh Kumar06441652015-07-29 16:23:04 +0530931
Viresh Kumarcdd6ed92018-09-12 12:35:19 +0530932 list_for_each_entry_safe(opp_dev, temp, &opp_table->dev_list, node) {
933 /*
934 * The OPP table is getting removed, drop the performance state
935 * constraints.
936 */
937 if (opp_table->genpd_performance_state)
938 dev_pm_genpd_set_performance_state((struct device *)(opp_dev->dev), 0);
Viresh Kumar06441652015-07-29 16:23:04 +0530939
Viresh Kumarcdd6ed92018-09-12 12:35:19 +0530940 _remove_opp_dev(opp_dev, opp_table);
941 }
Viresh Kumar06441652015-07-29 16:23:04 +0530942
Viresh Kumar4f018bc2018-06-26 16:29:34 +0530943 mutex_destroy(&opp_table->genpd_virt_dev_lock);
Viresh Kumar37a73ec2017-01-23 10:11:41 +0530944 mutex_destroy(&opp_table->lock);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530945 list_del(&opp_table->node);
946 kfree(opp_table);
Viresh Kumar3bac42c2015-07-29 16:22:59 +0530947
Viresh Kumarf067a982017-01-23 10:11:42 +0530948 mutex_unlock(&opp_table_lock);
949}
950
Viresh Kumard0e8ae62018-09-11 11:14:11 +0530951void _opp_remove_all_static(struct opp_table *opp_table)
952{
953 struct dev_pm_opp *opp, *tmp;
954
955 list_for_each_entry_safe(opp, tmp, &opp_table->opp_list, node) {
956 if (!opp->dynamic)
957 dev_pm_opp_put(opp);
958 }
959
960 opp_table->parsed_static_opps = false;
961}
962
963static void _opp_table_list_kref_release(struct kref *kref)
964{
965 struct opp_table *opp_table = container_of(kref, struct opp_table,
966 list_kref);
967
968 _opp_remove_all_static(opp_table);
969 mutex_unlock(&opp_table_lock);
970}
971
972void _put_opp_list_kref(struct opp_table *opp_table)
973{
974 kref_put_mutex(&opp_table->list_kref, _opp_table_list_kref_release,
975 &opp_table_lock);
976}
977
Viresh Kumarf067a982017-01-23 10:11:42 +0530978void dev_pm_opp_put_opp_table(struct opp_table *opp_table)
979{
980 kref_put_mutex(&opp_table->kref, _opp_table_kref_release,
981 &opp_table_lock);
982}
983EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table);
984
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +0530985void _opp_free(struct dev_pm_opp *opp)
Viresh Kumar969fceb2017-01-02 14:40:59 +0530986{
987 kfree(opp);
Viresh Kumar969fceb2017-01-02 14:40:59 +0530988}
989
Viresh Kumar70347642017-01-23 10:11:46 +0530990static void _opp_kref_release(struct kref *kref)
Viresh Kumar129eec52014-11-27 08:54:06 +0530991{
Viresh Kumar70347642017-01-23 10:11:46 +0530992 struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref);
993 struct opp_table *opp_table = opp->opp_table;
Viresh Kumar37a73ec2017-01-23 10:11:41 +0530994
Viresh Kumar129eec52014-11-27 08:54:06 +0530995 /*
996 * Notify the changes in the availability of the operable
997 * frequency/voltage list.
998 */
Viresh Kumar052c6f12017-01-23 10:11:49 +0530999 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_REMOVE, opp);
Viresh Kumarda544b62018-06-07 14:50:43 +05301000 _of_opp_free_required_opps(opp_table, opp);
Viresh Kumardeaa5142015-11-11 07:59:01 +05301001 opp_debug_remove_one(opp);
Viresh Kumar052c6f12017-01-23 10:11:49 +05301002 list_del(&opp->node);
1003 kfree(opp);
Viresh Kumar129eec52014-11-27 08:54:06 +05301004
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301005 mutex_unlock(&opp_table->lock);
Viresh Kumar129eec52014-11-27 08:54:06 +05301006}
1007
Viresh Kumara88bd2a2017-11-29 15:18:36 +05301008void dev_pm_opp_get(struct dev_pm_opp *opp)
Viresh Kumar8a31d9d92017-01-23 10:11:47 +05301009{
1010 kref_get(&opp->kref);
1011}
1012
Viresh Kumar70347642017-01-23 10:11:46 +05301013void dev_pm_opp_put(struct dev_pm_opp *opp)
1014{
1015 kref_put_mutex(&opp->kref, _opp_kref_release, &opp->opp_table->lock);
1016}
1017EXPORT_SYMBOL_GPL(dev_pm_opp_put);
1018
Viresh Kumar129eec52014-11-27 08:54:06 +05301019/**
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301020 * dev_pm_opp_remove() - Remove an OPP from OPP table
Viresh Kumar129eec52014-11-27 08:54:06 +05301021 * @dev: device for which we do this operation
1022 * @freq: OPP to remove with matching 'freq'
1023 *
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301024 * This function removes an opp from the opp table.
Viresh Kumar129eec52014-11-27 08:54:06 +05301025 */
1026void dev_pm_opp_remove(struct device *dev, unsigned long freq)
1027{
1028 struct dev_pm_opp *opp;
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301029 struct opp_table *opp_table;
Viresh Kumar129eec52014-11-27 08:54:06 +05301030 bool found = false;
1031
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301032 opp_table = _find_opp_table(dev);
1033 if (IS_ERR(opp_table))
Viresh Kumar5b650b32017-01-23 10:11:48 +05301034 return;
Viresh Kumar129eec52014-11-27 08:54:06 +05301035
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301036 mutex_lock(&opp_table->lock);
1037
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301038 list_for_each_entry(opp, &opp_table->opp_list, node) {
Viresh Kumar129eec52014-11-27 08:54:06 +05301039 if (opp->rate == freq) {
1040 found = true;
1041 break;
1042 }
1043 }
1044
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301045 mutex_unlock(&opp_table->lock);
1046
Viresh Kumar5b650b32017-01-23 10:11:48 +05301047 if (found) {
1048 dev_pm_opp_put(opp);
Viresh Kumar0ad8c622018-08-02 14:23:09 +05301049
1050 /* Drop the reference taken by dev_pm_opp_add() */
1051 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar5b650b32017-01-23 10:11:48 +05301052 } else {
Viresh Kumar129eec52014-11-27 08:54:06 +05301053 dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
1054 __func__, freq);
Viresh Kumar129eec52014-11-27 08:54:06 +05301055 }
1056
Viresh Kumar0ad8c622018-08-02 14:23:09 +05301057 /* Drop the reference taken by _find_opp_table() */
Viresh Kumar5b650b32017-01-23 10:11:48 +05301058 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar129eec52014-11-27 08:54:06 +05301059}
1060EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
1061
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301062struct dev_pm_opp *_opp_allocate(struct opp_table *table)
Viresh Kumar23dacf62015-07-29 16:23:01 +05301063{
1064 struct dev_pm_opp *opp;
Viresh Kumardfbe4672016-12-01 16:28:19 +05301065 int count, supply_size;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301066
Viresh Kumardfbe4672016-12-01 16:28:19 +05301067 /* Allocate space for at least one supply */
Viresh Kumar46f48ac2018-12-11 16:39:36 +05301068 count = table->regulator_count > 0 ? table->regulator_count : 1;
Viresh Kumardfbe4672016-12-01 16:28:19 +05301069 supply_size = sizeof(*opp->supplies) * count;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301070
Viresh Kumardfbe4672016-12-01 16:28:19 +05301071 /* allocate new OPP node and supplies structures */
1072 opp = kzalloc(sizeof(*opp) + supply_size, GFP_KERNEL);
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301073 if (!opp)
Viresh Kumar23dacf62015-07-29 16:23:01 +05301074 return NULL;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301075
Viresh Kumardfbe4672016-12-01 16:28:19 +05301076 /* Put the supplies at the end of the OPP structure as an empty array */
1077 opp->supplies = (struct dev_pm_opp_supply *)(opp + 1);
1078 INIT_LIST_HEAD(&opp->node);
1079
Viresh Kumar23dacf62015-07-29 16:23:01 +05301080 return opp;
1081}
1082
Viresh Kumar7d34d562016-02-09 10:30:34 +05301083static bool _opp_supported_by_regulators(struct dev_pm_opp *opp,
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301084 struct opp_table *opp_table)
Viresh Kumar7d34d562016-02-09 10:30:34 +05301085{
Viresh Kumardfbe4672016-12-01 16:28:19 +05301086 struct regulator *reg;
1087 int i;
Viresh Kumar7d34d562016-02-09 10:30:34 +05301088
Viresh Kumar90e35772018-12-11 16:32:47 +05301089 if (!opp_table->regulators)
1090 return true;
1091
Viresh Kumardfbe4672016-12-01 16:28:19 +05301092 for (i = 0; i < opp_table->regulator_count; i++) {
1093 reg = opp_table->regulators[i];
1094
1095 if (!regulator_is_supported_voltage(reg,
1096 opp->supplies[i].u_volt_min,
1097 opp->supplies[i].u_volt_max)) {
1098 pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n",
1099 __func__, opp->supplies[i].u_volt_min,
1100 opp->supplies[i].u_volt_max);
1101 return false;
1102 }
Viresh Kumar7d34d562016-02-09 10:30:34 +05301103 }
1104
1105 return true;
1106}
1107
Viresh Kumara1e8c132018-04-06 14:35:45 +05301108static int _opp_is_duplicate(struct device *dev, struct dev_pm_opp *new_opp,
1109 struct opp_table *opp_table,
1110 struct list_head **head)
1111{
1112 struct dev_pm_opp *opp;
1113
1114 /*
1115 * Insert new OPP in order of increasing frequency and discard if
1116 * already present.
1117 *
1118 * Need to use &opp_table->opp_list in the condition part of the 'for'
1119 * loop, don't replace it with head otherwise it will become an infinite
1120 * loop.
1121 */
1122 list_for_each_entry(opp, &opp_table->opp_list, node) {
1123 if (new_opp->rate > opp->rate) {
1124 *head = &opp->node;
1125 continue;
1126 }
1127
1128 if (new_opp->rate < opp->rate)
1129 return 0;
1130
1131 /* Duplicate OPPs */
1132 dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
1133 __func__, opp->rate, opp->supplies[0].u_volt,
1134 opp->available, new_opp->rate,
1135 new_opp->supplies[0].u_volt, new_opp->available);
1136
1137 /* Should we compare voltages for all regulators here ? */
1138 return opp->available &&
1139 new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST;
1140 }
1141
1142 return 0;
1143}
1144
Viresh Kumar7f8538e2017-01-02 14:40:55 +05301145/*
1146 * Returns:
1147 * 0: On success. And appropriate error message for duplicate OPPs.
1148 * -EBUSY: For OPP with same freq/volt and is available. The callers of
1149 * _opp_add() must return 0 if they receive -EBUSY from it. This is to make
1150 * sure we don't print error messages unnecessarily if different parts of
1151 * kernel try to initialize the OPP table.
1152 * -EEXIST: For OPP with same freq but different volt or is unavailable. This
1153 * should be considered an error by the callers of _opp_add().
1154 */
Viresh Kumarf47b72a2016-05-05 16:20:33 +05301155int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
Viresh Kumara1e8c132018-04-06 14:35:45 +05301156 struct opp_table *opp_table, bool rate_not_available)
Viresh Kumar23dacf62015-07-29 16:23:01 +05301157{
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301158 struct list_head *head;
Viresh Kumardeaa5142015-11-11 07:59:01 +05301159 int ret;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301160
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301161 mutex_lock(&opp_table->lock);
1162 head = &opp_table->opp_list;
1163
Viresh Kumara1e8c132018-04-06 14:35:45 +05301164 if (likely(!rate_not_available)) {
1165 ret = _opp_is_duplicate(dev, new_opp, opp_table, &head);
1166 if (ret) {
1167 mutex_unlock(&opp_table->lock);
1168 return ret;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301169 }
Viresh Kumar23dacf62015-07-29 16:23:01 +05301170 }
1171
Viresh Kumar052c6f12017-01-23 10:11:49 +05301172 list_add(&new_opp->node, head);
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301173 mutex_unlock(&opp_table->lock);
1174
1175 new_opp->opp_table = opp_table;
Viresh Kumar70347642017-01-23 10:11:46 +05301176 kref_init(&new_opp->kref);
Viresh Kumar23dacf62015-07-29 16:23:01 +05301177
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301178 ret = opp_debug_create_one(new_opp, opp_table);
Viresh Kumardeaa5142015-11-11 07:59:01 +05301179 if (ret)
1180 dev_err(dev, "%s: Failed to register opp to debugfs (%d)\n",
1181 __func__, ret);
1182
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301183 if (!_opp_supported_by_regulators(new_opp, opp_table)) {
Viresh Kumar7d34d562016-02-09 10:30:34 +05301184 new_opp->available = false;
1185 dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n",
1186 __func__, new_opp->rate);
1187 }
1188
Viresh Kumar23dacf62015-07-29 16:23:01 +05301189 return 0;
1190}
1191
Viresh Kumar737002b2015-07-29 16:22:58 +05301192/**
Viresh Kumarb64b9c3f2015-10-15 21:42:44 +05301193 * _opp_add_v1() - Allocate a OPP based on v1 bindings.
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301194 * @opp_table: OPP table
Nishanth Menone1f60b22010-10-13 00:13:10 +02001195 * @dev: device for which we do this operation
1196 * @freq: Frequency in Hz for this OPP
1197 * @u_volt: Voltage in uVolts for this OPP
1198 * @dynamic: Dynamically added OPPs.
1199 *
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301200 * This function adds an opp definition to the opp table and returns status.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001201 * The opp is made available by default and it can be controlled using
1202 * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
1203 *
Viresh Kumar8f8d37b2015-09-04 13:47:24 +05301204 * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table
1205 * and freed by dev_pm_opp_of_remove_table.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001206 *
Nishanth Menone1f60b22010-10-13 00:13:10 +02001207 * Return:
1208 * 0 On success OR
1209 * Duplicate OPPs (both freq and volt are same) and opp->available
1210 * -EEXIST Freq are same and volt are different OR
1211 * Duplicate OPPs (both freq and volt are same) and !opp->available
1212 * -ENOMEM Memory allocation failure
1213 */
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301214int _opp_add_v1(struct opp_table *opp_table, struct device *dev,
1215 unsigned long freq, long u_volt, bool dynamic)
Nishanth Menone1f60b22010-10-13 00:13:10 +02001216{
Viresh Kumar23dacf62015-07-29 16:23:01 +05301217 struct dev_pm_opp *new_opp;
Viresh Kumar50f8cfb2016-02-09 10:30:37 +05301218 unsigned long tol;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001219 int ret;
1220
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301221 new_opp = _opp_allocate(opp_table);
1222 if (!new_opp)
1223 return -ENOMEM;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301224
Nishanth Menone1f60b22010-10-13 00:13:10 +02001225 /* populate the opp table */
1226 new_opp->rate = freq;
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301227 tol = u_volt * opp_table->voltage_tolerance_v1 / 100;
Viresh Kumardfbe4672016-12-01 16:28:19 +05301228 new_opp->supplies[0].u_volt = u_volt;
1229 new_opp->supplies[0].u_volt_min = u_volt - tol;
1230 new_opp->supplies[0].u_volt_max = u_volt + tol;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001231 new_opp->available = true;
Viresh Kumaraa5f2f82015-07-29 16:23:00 +05301232 new_opp->dynamic = dynamic;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301233
Viresh Kumara1e8c132018-04-06 14:35:45 +05301234 ret = _opp_add(dev, new_opp, opp_table, false);
Viresh Kumar7f8538e2017-01-02 14:40:55 +05301235 if (ret) {
1236 /* Don't return error for duplicate OPPs */
1237 if (ret == -EBUSY)
1238 ret = 0;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301239 goto free_opp;
Viresh Kumar7f8538e2017-01-02 14:40:55 +05301240 }
Viresh Kumar23dacf62015-07-29 16:23:01 +05301241
Nishanth Menone1f60b22010-10-13 00:13:10 +02001242 /*
1243 * Notify the changes in the availability of the operable
1244 * frequency/voltage list.
1245 */
Viresh Kumar052c6f12017-01-23 10:11:49 +05301246 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001247 return 0;
1248
1249free_opp:
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301250 _opp_free(new_opp);
1251
Nishanth Menone1f60b22010-10-13 00:13:10 +02001252 return ret;
1253}
Viresh Kumar383934092014-11-25 16:04:18 +05301254
Viresh Kumar27465902015-07-29 16:23:02 +05301255/**
Viresh Kumar7de36b02015-12-09 08:01:46 +05301256 * dev_pm_opp_set_supported_hw() - Set supported platforms
1257 * @dev: Device for which supported-hw has to be set.
1258 * @versions: Array of hierarchy of versions to match.
1259 * @count: Number of elements in the array.
1260 *
1261 * This is required only for the V2 bindings, and it enables a platform to
1262 * specify the hierarchy of versions it supports. OPP layer will then enable
1263 * OPPs, which are available for those versions, based on its 'opp-supported-hw'
1264 * property.
Viresh Kumar7de36b02015-12-09 08:01:46 +05301265 */
Viresh Kumarfa301842017-01-23 10:11:43 +05301266struct opp_table *dev_pm_opp_set_supported_hw(struct device *dev,
1267 const u32 *versions, unsigned int count)
Viresh Kumar7de36b02015-12-09 08:01:46 +05301268{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301269 struct opp_table *opp_table;
Viresh Kumar7de36b02015-12-09 08:01:46 +05301270
Viresh Kumarfa301842017-01-23 10:11:43 +05301271 opp_table = dev_pm_opp_get_opp_table(dev);
1272 if (!opp_table)
1273 return ERR_PTR(-ENOMEM);
Viresh Kumar7de36b02015-12-09 08:01:46 +05301274
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301275 /* Make sure there are no concurrent readers while updating opp_table */
1276 WARN_ON(!list_empty(&opp_table->opp_list));
Viresh Kumar7de36b02015-12-09 08:01:46 +05301277
Viresh Kumar25419de2018-05-22 16:38:08 +05301278 /* Another CPU that shares the OPP table has set the property ? */
1279 if (opp_table->supported_hw)
1280 return opp_table;
Viresh Kumar7de36b02015-12-09 08:01:46 +05301281
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301282 opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions),
Viresh Kumar7de36b02015-12-09 08:01:46 +05301283 GFP_KERNEL);
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301284 if (!opp_table->supported_hw) {
Viresh Kumar25419de2018-05-22 16:38:08 +05301285 dev_pm_opp_put_opp_table(opp_table);
1286 return ERR_PTR(-ENOMEM);
Viresh Kumar7de36b02015-12-09 08:01:46 +05301287 }
1288
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301289 opp_table->supported_hw_count = count;
Viresh Kumarfa301842017-01-23 10:11:43 +05301290
1291 return opp_table;
Viresh Kumar7de36b02015-12-09 08:01:46 +05301292}
1293EXPORT_SYMBOL_GPL(dev_pm_opp_set_supported_hw);
1294
1295/**
1296 * dev_pm_opp_put_supported_hw() - Releases resources blocked for supported hw
Viresh Kumarfa301842017-01-23 10:11:43 +05301297 * @opp_table: OPP table returned by dev_pm_opp_set_supported_hw().
Viresh Kumar7de36b02015-12-09 08:01:46 +05301298 *
1299 * This is required only for the V2 bindings, and is called for a matching
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301300 * dev_pm_opp_set_supported_hw(). Until this is called, the opp_table structure
Viresh Kumar7de36b02015-12-09 08:01:46 +05301301 * will not be freed.
Viresh Kumar7de36b02015-12-09 08:01:46 +05301302 */
Viresh Kumarfa301842017-01-23 10:11:43 +05301303void dev_pm_opp_put_supported_hw(struct opp_table *opp_table)
Viresh Kumar7de36b02015-12-09 08:01:46 +05301304{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301305 /* Make sure there are no concurrent readers while updating opp_table */
1306 WARN_ON(!list_empty(&opp_table->opp_list));
Viresh Kumar7de36b02015-12-09 08:01:46 +05301307
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301308 kfree(opp_table->supported_hw);
1309 opp_table->supported_hw = NULL;
1310 opp_table->supported_hw_count = 0;
Viresh Kumar7de36b02015-12-09 08:01:46 +05301311
Viresh Kumarfa301842017-01-23 10:11:43 +05301312 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar7de36b02015-12-09 08:01:46 +05301313}
1314EXPORT_SYMBOL_GPL(dev_pm_opp_put_supported_hw);
1315
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301316/**
1317 * dev_pm_opp_set_prop_name() - Set prop-extn name
Viresh Kumara5da6442016-02-16 14:17:52 +05301318 * @dev: Device for which the prop-name has to be set.
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301319 * @name: name to postfix to properties.
1320 *
1321 * This is required only for the V2 bindings, and it enables a platform to
1322 * specify the extn to be used for certain property names. The properties to
1323 * which the extension will apply are opp-microvolt and opp-microamp. OPP core
1324 * should postfix the property name with -<name> while looking for them.
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301325 */
Viresh Kumarfa301842017-01-23 10:11:43 +05301326struct opp_table *dev_pm_opp_set_prop_name(struct device *dev, const char *name)
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301327{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301328 struct opp_table *opp_table;
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301329
Viresh Kumarfa301842017-01-23 10:11:43 +05301330 opp_table = dev_pm_opp_get_opp_table(dev);
1331 if (!opp_table)
1332 return ERR_PTR(-ENOMEM);
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301333
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301334 /* Make sure there are no concurrent readers while updating opp_table */
1335 WARN_ON(!list_empty(&opp_table->opp_list));
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301336
Viresh Kumar878ec1a2018-05-22 16:38:08 +05301337 /* Another CPU that shares the OPP table has set the property ? */
1338 if (opp_table->prop_name)
1339 return opp_table;
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301340
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301341 opp_table->prop_name = kstrdup(name, GFP_KERNEL);
1342 if (!opp_table->prop_name) {
Viresh Kumar878ec1a2018-05-22 16:38:08 +05301343 dev_pm_opp_put_opp_table(opp_table);
1344 return ERR_PTR(-ENOMEM);
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301345 }
1346
Viresh Kumarfa301842017-01-23 10:11:43 +05301347 return opp_table;
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301348}
1349EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name);
1350
1351/**
1352 * dev_pm_opp_put_prop_name() - Releases resources blocked for prop-name
Viresh Kumarfa301842017-01-23 10:11:43 +05301353 * @opp_table: OPP table returned by dev_pm_opp_set_prop_name().
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301354 *
1355 * This is required only for the V2 bindings, and is called for a matching
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301356 * dev_pm_opp_set_prop_name(). Until this is called, the opp_table structure
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301357 * will not be freed.
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301358 */
Viresh Kumarfa301842017-01-23 10:11:43 +05301359void dev_pm_opp_put_prop_name(struct opp_table *opp_table)
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301360{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301361 /* Make sure there are no concurrent readers while updating opp_table */
1362 WARN_ON(!list_empty(&opp_table->opp_list));
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301363
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301364 kfree(opp_table->prop_name);
1365 opp_table->prop_name = NULL;
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301366
Viresh Kumarfa301842017-01-23 10:11:43 +05301367 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301368}
1369EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name);
1370
Viresh Kumar94735582016-12-01 16:28:20 +05301371static int _allocate_set_opp_data(struct opp_table *opp_table)
1372{
1373 struct dev_pm_set_opp_data *data;
1374 int len, count = opp_table->regulator_count;
1375
Viresh Kumar90e35772018-12-11 16:32:47 +05301376 if (WARN_ON(!opp_table->regulators))
Viresh Kumar94735582016-12-01 16:28:20 +05301377 return -EINVAL;
1378
1379 /* space for set_opp_data */
1380 len = sizeof(*data);
1381
1382 /* space for old_opp.supplies and new_opp.supplies */
1383 len += 2 * sizeof(struct dev_pm_opp_supply) * count;
1384
1385 data = kzalloc(len, GFP_KERNEL);
1386 if (!data)
1387 return -ENOMEM;
1388
1389 data->old_opp.supplies = (void *)(data + 1);
1390 data->new_opp.supplies = data->old_opp.supplies + count;
1391
1392 opp_table->set_opp_data = data;
1393
1394 return 0;
1395}
1396
1397static void _free_set_opp_data(struct opp_table *opp_table)
1398{
1399 kfree(opp_table->set_opp_data);
1400 opp_table->set_opp_data = NULL;
1401}
1402
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301403/**
Viresh Kumardfbe4672016-12-01 16:28:19 +05301404 * dev_pm_opp_set_regulators() - Set regulator names for the device
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301405 * @dev: Device for which regulator name is being set.
Viresh Kumardfbe4672016-12-01 16:28:19 +05301406 * @names: Array of pointers to the names of the regulator.
1407 * @count: Number of regulators.
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301408 *
1409 * In order to support OPP switching, OPP layer needs to know the name of the
Viresh Kumardfbe4672016-12-01 16:28:19 +05301410 * device's regulators, as the core would be required to switch voltages as
1411 * well.
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301412 *
1413 * This must be called before any OPPs are initialized for the device.
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301414 */
Viresh Kumardfbe4672016-12-01 16:28:19 +05301415struct opp_table *dev_pm_opp_set_regulators(struct device *dev,
1416 const char * const names[],
1417 unsigned int count)
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301418{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301419 struct opp_table *opp_table;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301420 struct regulator *reg;
Viresh Kumardfbe4672016-12-01 16:28:19 +05301421 int ret, i;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301422
Viresh Kumarfa301842017-01-23 10:11:43 +05301423 opp_table = dev_pm_opp_get_opp_table(dev);
1424 if (!opp_table)
1425 return ERR_PTR(-ENOMEM);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301426
1427 /* This should be called before OPPs are initialized */
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301428 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301429 ret = -EBUSY;
1430 goto err;
1431 }
1432
Viresh Kumar779b7832018-05-22 16:38:08 +05301433 /* Another CPU that shares the OPP table has set the regulators ? */
1434 if (opp_table->regulators)
1435 return opp_table;
Viresh Kumardfbe4672016-12-01 16:28:19 +05301436
1437 opp_table->regulators = kmalloc_array(count,
1438 sizeof(*opp_table->regulators),
1439 GFP_KERNEL);
1440 if (!opp_table->regulators) {
1441 ret = -ENOMEM;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301442 goto err;
1443 }
1444
Viresh Kumardfbe4672016-12-01 16:28:19 +05301445 for (i = 0; i < count; i++) {
1446 reg = regulator_get_optional(dev, names[i]);
1447 if (IS_ERR(reg)) {
1448 ret = PTR_ERR(reg);
1449 if (ret != -EPROBE_DEFER)
1450 dev_err(dev, "%s: no regulator (%s) found: %d\n",
1451 __func__, names[i], ret);
1452 goto free_regulators;
1453 }
1454
1455 opp_table->regulators[i] = reg;
1456 }
1457
1458 opp_table->regulator_count = count;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301459
Viresh Kumar94735582016-12-01 16:28:20 +05301460 /* Allocate block only once to pass to set_opp() routines */
1461 ret = _allocate_set_opp_data(opp_table);
1462 if (ret)
1463 goto free_regulators;
1464
Stephen Boyd91291d92016-11-30 16:21:25 +05301465 return opp_table;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301466
Viresh Kumardfbe4672016-12-01 16:28:19 +05301467free_regulators:
1468 while (i != 0)
1469 regulator_put(opp_table->regulators[--i]);
1470
1471 kfree(opp_table->regulators);
1472 opp_table->regulators = NULL;
Viresh Kumar46f48ac2018-12-11 16:39:36 +05301473 opp_table->regulator_count = -1;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301474err:
Viresh Kumarfa301842017-01-23 10:11:43 +05301475 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301476
Stephen Boyd91291d92016-11-30 16:21:25 +05301477 return ERR_PTR(ret);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301478}
Viresh Kumardfbe4672016-12-01 16:28:19 +05301479EXPORT_SYMBOL_GPL(dev_pm_opp_set_regulators);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301480
1481/**
Viresh Kumardfbe4672016-12-01 16:28:19 +05301482 * dev_pm_opp_put_regulators() - Releases resources blocked for regulator
1483 * @opp_table: OPP table returned from dev_pm_opp_set_regulators().
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301484 */
Viresh Kumardfbe4672016-12-01 16:28:19 +05301485void dev_pm_opp_put_regulators(struct opp_table *opp_table)
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301486{
Viresh Kumardfbe4672016-12-01 16:28:19 +05301487 int i;
1488
Viresh Kumar779b7832018-05-22 16:38:08 +05301489 if (!opp_table->regulators)
1490 goto put_opp_table;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301491
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301492 /* Make sure there are no concurrent readers while updating opp_table */
1493 WARN_ON(!list_empty(&opp_table->opp_list));
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301494
Viresh Kumardfbe4672016-12-01 16:28:19 +05301495 for (i = opp_table->regulator_count - 1; i >= 0; i--)
1496 regulator_put(opp_table->regulators[i]);
1497
Viresh Kumar94735582016-12-01 16:28:20 +05301498 _free_set_opp_data(opp_table);
1499
Viresh Kumardfbe4672016-12-01 16:28:19 +05301500 kfree(opp_table->regulators);
1501 opp_table->regulators = NULL;
Viresh Kumar46f48ac2018-12-11 16:39:36 +05301502 opp_table->regulator_count = -1;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301503
Viresh Kumar779b7832018-05-22 16:38:08 +05301504put_opp_table:
Viresh Kumarfa301842017-01-23 10:11:43 +05301505 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301506}
Viresh Kumardfbe4672016-12-01 16:28:19 +05301507EXPORT_SYMBOL_GPL(dev_pm_opp_put_regulators);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301508
Viresh Kumar383934092014-11-25 16:04:18 +05301509/**
Viresh Kumar829a4e82017-06-21 10:29:13 +05301510 * dev_pm_opp_set_clkname() - Set clk name for the device
1511 * @dev: Device for which clk name is being set.
1512 * @name: Clk name.
1513 *
1514 * In order to support OPP switching, OPP layer needs to get pointer to the
1515 * clock for the device. Simple cases work fine without using this routine (i.e.
1516 * by passing connection-id as NULL), but for a device with multiple clocks
1517 * available, the OPP core needs to know the exact name of the clk to use.
1518 *
1519 * This must be called before any OPPs are initialized for the device.
1520 */
1521struct opp_table *dev_pm_opp_set_clkname(struct device *dev, const char *name)
1522{
1523 struct opp_table *opp_table;
1524 int ret;
1525
1526 opp_table = dev_pm_opp_get_opp_table(dev);
1527 if (!opp_table)
1528 return ERR_PTR(-ENOMEM);
1529
1530 /* This should be called before OPPs are initialized */
1531 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
1532 ret = -EBUSY;
1533 goto err;
1534 }
1535
1536 /* Already have default clk set, free it */
1537 if (!IS_ERR(opp_table->clk))
1538 clk_put(opp_table->clk);
1539
1540 /* Find clk for the device */
1541 opp_table->clk = clk_get(dev, name);
1542 if (IS_ERR(opp_table->clk)) {
1543 ret = PTR_ERR(opp_table->clk);
1544 if (ret != -EPROBE_DEFER) {
1545 dev_err(dev, "%s: Couldn't find clock: %d\n", __func__,
1546 ret);
1547 }
1548 goto err;
1549 }
1550
1551 return opp_table;
1552
1553err:
1554 dev_pm_opp_put_opp_table(opp_table);
1555
1556 return ERR_PTR(ret);
1557}
1558EXPORT_SYMBOL_GPL(dev_pm_opp_set_clkname);
1559
1560/**
1561 * dev_pm_opp_put_clkname() - Releases resources blocked for clk.
1562 * @opp_table: OPP table returned from dev_pm_opp_set_clkname().
1563 */
1564void dev_pm_opp_put_clkname(struct opp_table *opp_table)
1565{
1566 /* Make sure there are no concurrent readers while updating opp_table */
1567 WARN_ON(!list_empty(&opp_table->opp_list));
1568
1569 clk_put(opp_table->clk);
1570 opp_table->clk = ERR_PTR(-EINVAL);
1571
1572 dev_pm_opp_put_opp_table(opp_table);
1573}
1574EXPORT_SYMBOL_GPL(dev_pm_opp_put_clkname);
1575
1576/**
Viresh Kumar4dab1602016-12-01 16:28:21 +05301577 * dev_pm_opp_register_set_opp_helper() - Register custom set OPP helper
1578 * @dev: Device for which the helper is getting registered.
1579 * @set_opp: Custom set OPP helper.
1580 *
1581 * This is useful to support complex platforms (like platforms with multiple
1582 * regulators per device), instead of the generic OPP set rate helper.
1583 *
1584 * This must be called before any OPPs are initialized for the device.
Viresh Kumar4dab1602016-12-01 16:28:21 +05301585 */
Viresh Kumarfa301842017-01-23 10:11:43 +05301586struct opp_table *dev_pm_opp_register_set_opp_helper(struct device *dev,
Viresh Kumar4dab1602016-12-01 16:28:21 +05301587 int (*set_opp)(struct dev_pm_set_opp_data *data))
1588{
1589 struct opp_table *opp_table;
Viresh Kumar4dab1602016-12-01 16:28:21 +05301590
1591 if (!set_opp)
Viresh Kumarfa301842017-01-23 10:11:43 +05301592 return ERR_PTR(-EINVAL);
Viresh Kumar4dab1602016-12-01 16:28:21 +05301593
Viresh Kumarfa301842017-01-23 10:11:43 +05301594 opp_table = dev_pm_opp_get_opp_table(dev);
1595 if (!opp_table)
1596 return ERR_PTR(-ENOMEM);
Viresh Kumar4dab1602016-12-01 16:28:21 +05301597
1598 /* This should be called before OPPs are initialized */
1599 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
Viresh Kumar5019acc2018-05-22 16:38:08 +05301600 dev_pm_opp_put_opp_table(opp_table);
1601 return ERR_PTR(-EBUSY);
Viresh Kumar4dab1602016-12-01 16:28:21 +05301602 }
1603
Viresh Kumar5019acc2018-05-22 16:38:08 +05301604 /* Another CPU that shares the OPP table has set the helper ? */
1605 if (!opp_table->set_opp)
1606 opp_table->set_opp = set_opp;
Viresh Kumar4dab1602016-12-01 16:28:21 +05301607
Viresh Kumarfa301842017-01-23 10:11:43 +05301608 return opp_table;
Viresh Kumar4dab1602016-12-01 16:28:21 +05301609}
1610EXPORT_SYMBOL_GPL(dev_pm_opp_register_set_opp_helper);
1611
1612/**
Viresh Kumar604a7ae2017-10-05 17:26:21 +05301613 * dev_pm_opp_unregister_set_opp_helper() - Releases resources blocked for
Viresh Kumar4dab1602016-12-01 16:28:21 +05301614 * set_opp helper
Viresh Kumarfa301842017-01-23 10:11:43 +05301615 * @opp_table: OPP table returned from dev_pm_opp_register_set_opp_helper().
Viresh Kumar4dab1602016-12-01 16:28:21 +05301616 *
Viresh Kumarfa301842017-01-23 10:11:43 +05301617 * Release resources blocked for platform specific set_opp helper.
Viresh Kumar4dab1602016-12-01 16:28:21 +05301618 */
Viresh Kumar604a7ae2017-10-05 17:26:21 +05301619void dev_pm_opp_unregister_set_opp_helper(struct opp_table *opp_table)
Viresh Kumar4dab1602016-12-01 16:28:21 +05301620{
Viresh Kumar4dab1602016-12-01 16:28:21 +05301621 /* Make sure there are no concurrent readers while updating opp_table */
1622 WARN_ON(!list_empty(&opp_table->opp_list));
1623
1624 opp_table->set_opp = NULL;
Viresh Kumarfa301842017-01-23 10:11:43 +05301625 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar4dab1602016-12-01 16:28:21 +05301626}
Viresh Kumar604a7ae2017-10-05 17:26:21 +05301627EXPORT_SYMBOL_GPL(dev_pm_opp_unregister_set_opp_helper);
Viresh Kumar4dab1602016-12-01 16:28:21 +05301628
1629/**
Viresh Kumar4f018bc2018-06-26 16:29:34 +05301630 * dev_pm_opp_set_genpd_virt_dev - Set virtual genpd device for an index
1631 * @dev: Consumer device for which the genpd device is getting set.
1632 * @virt_dev: virtual genpd device.
1633 * @index: index.
1634 *
1635 * Multiple generic power domains for a device are supported with the help of
1636 * virtual genpd devices, which are created for each consumer device - genpd
1637 * pair. These are the device structures which are attached to the power domain
1638 * and are required by the OPP core to set the performance state of the genpd.
1639 *
1640 * This helper will normally be called by the consumer driver of the device
1641 * "dev", as only that has details of the genpd devices.
1642 *
1643 * This helper needs to be called once for each of those virtual devices, but
1644 * only if multiple domains are available for a device. Otherwise the original
1645 * device structure will be used instead by the OPP core.
1646 */
1647struct opp_table *dev_pm_opp_set_genpd_virt_dev(struct device *dev,
1648 struct device *virt_dev,
1649 int index)
1650{
1651 struct opp_table *opp_table;
1652
1653 opp_table = dev_pm_opp_get_opp_table(dev);
1654 if (!opp_table)
1655 return ERR_PTR(-ENOMEM);
1656
1657 mutex_lock(&opp_table->genpd_virt_dev_lock);
1658
1659 if (unlikely(!opp_table->genpd_virt_devs ||
1660 index >= opp_table->required_opp_count ||
1661 opp_table->genpd_virt_devs[index])) {
1662
1663 dev_err(dev, "Invalid request to set required device\n");
1664 dev_pm_opp_put_opp_table(opp_table);
1665 mutex_unlock(&opp_table->genpd_virt_dev_lock);
1666
1667 return ERR_PTR(-EINVAL);
1668 }
1669
1670 opp_table->genpd_virt_devs[index] = virt_dev;
1671 mutex_unlock(&opp_table->genpd_virt_dev_lock);
1672
1673 return opp_table;
1674}
1675
1676/**
1677 * dev_pm_opp_put_genpd_virt_dev() - Releases resources blocked for genpd device.
1678 * @opp_table: OPP table returned by dev_pm_opp_set_genpd_virt_dev().
1679 * @virt_dev: virtual genpd device.
1680 *
1681 * This releases the resource previously acquired with a call to
1682 * dev_pm_opp_set_genpd_virt_dev(). The consumer driver shall call this helper
1683 * if it doesn't want OPP core to update performance state of a power domain
1684 * anymore.
1685 */
1686void dev_pm_opp_put_genpd_virt_dev(struct opp_table *opp_table,
1687 struct device *virt_dev)
1688{
1689 int i;
1690
1691 /*
1692 * Acquire genpd_virt_dev_lock to make sure virt_dev isn't getting
1693 * used in parallel.
1694 */
1695 mutex_lock(&opp_table->genpd_virt_dev_lock);
1696
1697 for (i = 0; i < opp_table->required_opp_count; i++) {
1698 if (opp_table->genpd_virt_devs[i] != virt_dev)
1699 continue;
1700
1701 opp_table->genpd_virt_devs[i] = NULL;
1702 dev_pm_opp_put_opp_table(opp_table);
1703
1704 /* Drop the vote */
1705 dev_pm_genpd_set_performance_state(virt_dev, 0);
1706 break;
1707 }
1708
1709 mutex_unlock(&opp_table->genpd_virt_dev_lock);
1710
1711 if (unlikely(i == opp_table->required_opp_count))
1712 dev_err(virt_dev, "Failed to find required device entry\n");
1713}
1714
1715/**
Viresh Kumarc8a59102018-11-02 14:36:42 +05301716 * dev_pm_opp_xlate_performance_state() - Find required OPP's pstate for src_table.
1717 * @src_table: OPP table which has dst_table as one of its required OPP table.
1718 * @dst_table: Required OPP table of the src_table.
1719 * @pstate: Current performance state of the src_table.
1720 *
1721 * This Returns pstate of the OPP (present in @dst_table) pointed out by the
1722 * "required-opps" property of the OPP (present in @src_table) which has
1723 * performance state set to @pstate.
1724 *
1725 * Return: Zero or positive performance state on success, otherwise negative
1726 * value on errors.
1727 */
1728int dev_pm_opp_xlate_performance_state(struct opp_table *src_table,
1729 struct opp_table *dst_table,
1730 unsigned int pstate)
1731{
1732 struct dev_pm_opp *opp;
1733 int dest_pstate = -EINVAL;
1734 int i;
1735
1736 if (!pstate)
1737 return 0;
1738
1739 /*
1740 * Normally the src_table will have the "required_opps" property set to
1741 * point to one of the OPPs in the dst_table, but in some cases the
1742 * genpd and its master have one to one mapping of performance states
1743 * and so none of them have the "required-opps" property set. Return the
1744 * pstate of the src_table as it is in such cases.
1745 */
1746 if (!src_table->required_opp_count)
1747 return pstate;
1748
1749 for (i = 0; i < src_table->required_opp_count; i++) {
1750 if (src_table->required_opp_tables[i]->np == dst_table->np)
1751 break;
1752 }
1753
1754 if (unlikely(i == src_table->required_opp_count)) {
1755 pr_err("%s: Couldn't find matching OPP table (%p: %p)\n",
1756 __func__, src_table, dst_table);
1757 return -EINVAL;
1758 }
1759
1760 mutex_lock(&src_table->lock);
1761
1762 list_for_each_entry(opp, &src_table->opp_list, node) {
1763 if (opp->pstate == pstate) {
1764 dest_pstate = opp->required_opps[i]->pstate;
1765 goto unlock;
1766 }
1767 }
1768
1769 pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__, src_table,
1770 dst_table);
1771
1772unlock:
1773 mutex_unlock(&src_table->lock);
1774
1775 return dest_pstate;
1776}
1777
1778/**
Nishanth Menone1f60b22010-10-13 00:13:10 +02001779 * dev_pm_opp_add() - Add an OPP table from a table definitions
1780 * @dev: device for which we do this operation
Nishanth Menon47d43ba2013-09-19 16:03:51 -05001781 * @freq: Frequency in Hz for this OPP
Nishanth Menone1f60b22010-10-13 00:13:10 +02001782 * @u_volt: Voltage in uVolts for this OPP
1783 *
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301784 * This function adds an opp definition to the opp table and returns status.
Nishanth Menon47d43ba2013-09-19 16:03:51 -05001785 * The opp is made available by default and it can be controlled using
Nishanth Menone1f60b22010-10-13 00:13:10 +02001786 * dev_pm_opp_enable/disable functions.
1787 *
Viresh Kumara7470db2014-11-25 16:04:17 +05301788 * Return:
1789 * 0 On success OR
1790 * Duplicate OPPs (both freq and volt are same) and opp->available
1791 * -EEXIST Freq are same and volt are different OR
1792 * Duplicate OPPs (both freq and volt are same) and !opp->available
Viresh Kumar383934092014-11-25 16:04:18 +05301793 * -ENOMEM Memory allocation failure
Viresh Kumara7470db2014-11-25 16:04:17 +05301794 */
Nishanth Menone1f60b22010-10-13 00:13:10 +02001795int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
1796{
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301797 struct opp_table *opp_table;
1798 int ret;
1799
Viresh Kumarb83c1892017-01-23 10:11:45 +05301800 opp_table = dev_pm_opp_get_opp_table(dev);
1801 if (!opp_table)
1802 return -ENOMEM;
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301803
Viresh Kumar46f48ac2018-12-11 16:39:36 +05301804 /* Fix regulator count for dynamic OPPs */
1805 opp_table->regulator_count = 1;
1806
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301807 ret = _opp_add_v1(opp_table, dev, freq, u_volt, true);
Viresh Kumar0ad8c622018-08-02 14:23:09 +05301808 if (ret)
1809 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301810
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301811 return ret;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001812}
1813EXPORT_SYMBOL_GPL(dev_pm_opp_add);
1814
Nishanth Menone1f60b22010-10-13 00:13:10 +02001815/**
Nishanth Menon327854c2014-12-24 11:22:56 -06001816 * _opp_set_availability() - helper to set the availability of an opp
Nishanth Menone1f60b22010-10-13 00:13:10 +02001817 * @dev: device for which we do this operation
1818 * @freq: OPP frequency to modify availability
1819 * @availability_req: availability status requested for this opp
1820 *
Viresh Kumar052c6f12017-01-23 10:11:49 +05301821 * Set the availability of an OPP, opp_{enable,disable} share a common logic
1822 * which is isolated here.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001823 *
Nishanth Menon984f16c2014-12-24 11:22:57 -06001824 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
Stephen Boyde1a2d492015-09-24 12:28:44 -07001825 * copy operation, returns 0 if no modification was done OR modification was
Nishanth Menone1f60b22010-10-13 00:13:10 +02001826 * successful.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001827 */
Nishanth Menon327854c2014-12-24 11:22:56 -06001828static int _opp_set_availability(struct device *dev, unsigned long freq,
1829 bool availability_req)
Nishanth Menone1f60b22010-10-13 00:13:10 +02001830{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301831 struct opp_table *opp_table;
Viresh Kumara7f39872017-01-23 10:11:50 +05301832 struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001833 int r = 0;
1834
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301835 /* Find the opp_table */
1836 opp_table = _find_opp_table(dev);
1837 if (IS_ERR(opp_table)) {
1838 r = PTR_ERR(opp_table);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001839 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
Viresh Kumara7f39872017-01-23 10:11:50 +05301840 return r;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001841 }
1842
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301843 mutex_lock(&opp_table->lock);
1844
Nishanth Menone1f60b22010-10-13 00:13:10 +02001845 /* Do we have the frequency? */
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301846 list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
Nishanth Menone1f60b22010-10-13 00:13:10 +02001847 if (tmp_opp->rate == freq) {
1848 opp = tmp_opp;
1849 break;
1850 }
1851 }
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301852
Nishanth Menone1f60b22010-10-13 00:13:10 +02001853 if (IS_ERR(opp)) {
1854 r = PTR_ERR(opp);
1855 goto unlock;
1856 }
1857
1858 /* Is update really needed? */
1859 if (opp->available == availability_req)
1860 goto unlock;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001861
Viresh Kumara7f39872017-01-23 10:11:50 +05301862 opp->available = availability_req;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001863
Viresh Kumare4d8ae02017-09-21 10:44:36 -07001864 dev_pm_opp_get(opp);
1865 mutex_unlock(&opp_table->lock);
1866
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001867 /* Notify the change of the OPP availability */
1868 if (availability_req)
Viresh Kumar052c6f12017-01-23 10:11:49 +05301869 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE,
Viresh Kumara7f39872017-01-23 10:11:50 +05301870 opp);
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001871 else
Viresh Kumar052c6f12017-01-23 10:11:49 +05301872 blocking_notifier_call_chain(&opp_table->head,
Viresh Kumara7f39872017-01-23 10:11:50 +05301873 OPP_EVENT_DISABLE, opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001874
Viresh Kumare4d8ae02017-09-21 10:44:36 -07001875 dev_pm_opp_put(opp);
1876 goto put_table;
1877
Nishanth Menone1f60b22010-10-13 00:13:10 +02001878unlock:
Viresh Kumar5b650b32017-01-23 10:11:48 +05301879 mutex_unlock(&opp_table->lock);
Viresh Kumare4d8ae02017-09-21 10:44:36 -07001880put_table:
Viresh Kumar5b650b32017-01-23 10:11:48 +05301881 dev_pm_opp_put_opp_table(opp_table);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001882 return r;
1883}
1884
1885/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001886 * dev_pm_opp_enable() - Enable a specific OPP
Nishanth Menone1f60b22010-10-13 00:13:10 +02001887 * @dev: device for which we do this operation
1888 * @freq: OPP frequency to enable
1889 *
1890 * Enables a provided opp. If the operation is valid, this returns 0, else the
1891 * corresponding error value. It is meant to be used for users an OPP available
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001892 * after being temporarily made unavailable with dev_pm_opp_disable.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001893 *
Nishanth Menon984f16c2014-12-24 11:22:57 -06001894 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
Stephen Boyde1a2d492015-09-24 12:28:44 -07001895 * copy operation, returns 0 if no modification was done OR modification was
Nishanth Menon984f16c2014-12-24 11:22:57 -06001896 * successful.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001897 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001898int dev_pm_opp_enable(struct device *dev, unsigned long freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +02001899{
Nishanth Menon327854c2014-12-24 11:22:56 -06001900 return _opp_set_availability(dev, freq, true);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001901}
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001902EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001903
1904/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001905 * dev_pm_opp_disable() - Disable a specific OPP
Nishanth Menone1f60b22010-10-13 00:13:10 +02001906 * @dev: device for which we do this operation
1907 * @freq: OPP frequency to disable
1908 *
1909 * Disables a provided opp. If the operation is valid, this returns
1910 * 0, else the corresponding error value. It is meant to be a temporary
1911 * control by users to make this OPP not available until the circumstances are
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001912 * right to make it available again (with a call to dev_pm_opp_enable).
Nishanth Menone1f60b22010-10-13 00:13:10 +02001913 *
Nishanth Menon984f16c2014-12-24 11:22:57 -06001914 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
Stephen Boyde1a2d492015-09-24 12:28:44 -07001915 * copy operation, returns 0 if no modification was done OR modification was
Nishanth Menon984f16c2014-12-24 11:22:57 -06001916 * successful.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001917 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001918int dev_pm_opp_disable(struct device *dev, unsigned long freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +02001919{
Nishanth Menon327854c2014-12-24 11:22:56 -06001920 return _opp_set_availability(dev, freq, false);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001921}
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001922EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001923
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001924/**
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301925 * dev_pm_opp_register_notifier() - Register OPP notifier for the device
1926 * @dev: Device for which notifier needs to be registered
1927 * @nb: Notifier block to be registered
Nishanth Menon984f16c2014-12-24 11:22:57 -06001928 *
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301929 * Return: 0 on success or a negative error value.
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001930 */
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301931int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb)
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001932{
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301933 struct opp_table *opp_table;
1934 int ret;
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001935
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301936 opp_table = _find_opp_table(dev);
Viresh Kumar5b650b32017-01-23 10:11:48 +05301937 if (IS_ERR(opp_table))
1938 return PTR_ERR(opp_table);
1939
Viresh Kumar052c6f12017-01-23 10:11:49 +05301940 ret = blocking_notifier_chain_register(&opp_table->head, nb);
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301941
Viresh Kumar5b650b32017-01-23 10:11:48 +05301942 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301943
1944 return ret;
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001945}
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301946EXPORT_SYMBOL(dev_pm_opp_register_notifier);
1947
1948/**
1949 * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device
1950 * @dev: Device for which notifier needs to be unregistered
1951 * @nb: Notifier block to be unregistered
1952 *
1953 * Return: 0 on success or a negative error value.
1954 */
1955int dev_pm_opp_unregister_notifier(struct device *dev,
1956 struct notifier_block *nb)
1957{
1958 struct opp_table *opp_table;
1959 int ret;
1960
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301961 opp_table = _find_opp_table(dev);
Viresh Kumar5b650b32017-01-23 10:11:48 +05301962 if (IS_ERR(opp_table))
1963 return PTR_ERR(opp_table);
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301964
Viresh Kumar052c6f12017-01-23 10:11:49 +05301965 ret = blocking_notifier_chain_unregister(&opp_table->head, nb);
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301966
Viresh Kumar5b650b32017-01-23 10:11:48 +05301967 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301968
1969 return ret;
1970}
1971EXPORT_SYMBOL(dev_pm_opp_unregister_notifier);
Shawn Guob496dfb2012-09-05 01:09:12 +02001972
Viresh Kumar2a4eb732018-09-13 13:14:36 +05301973void _dev_pm_opp_find_and_remove_table(struct device *dev)
Viresh Kumar737002b2015-07-29 16:22:58 +05301974{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301975 struct opp_table *opp_table;
Viresh Kumar737002b2015-07-29 16:22:58 +05301976
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301977 /* Check for existing table for 'dev' */
1978 opp_table = _find_opp_table(dev);
1979 if (IS_ERR(opp_table)) {
1980 int error = PTR_ERR(opp_table);
Viresh Kumar737002b2015-07-29 16:22:58 +05301981
1982 if (error != -ENODEV)
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301983 WARN(1, "%s: opp_table: %d\n",
Viresh Kumar737002b2015-07-29 16:22:58 +05301984 IS_ERR_OR_NULL(dev) ?
1985 "Invalid device" : dev_name(dev),
1986 error);
Viresh Kumar5b650b32017-01-23 10:11:48 +05301987 return;
Viresh Kumar737002b2015-07-29 16:22:58 +05301988 }
1989
Viresh Kumarcdd6ed92018-09-12 12:35:19 +05301990 _put_opp_list_kref(opp_table);
Viresh Kumar737002b2015-07-29 16:22:58 +05301991
Viresh Kumarcdd6ed92018-09-12 12:35:19 +05301992 /* Drop reference taken by _find_opp_table() */
1993 dev_pm_opp_put_opp_table(opp_table);
1994
1995 /* Drop reference taken while the OPP table was added */
Viresh Kumar5b650b32017-01-23 10:11:48 +05301996 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar737002b2015-07-29 16:22:58 +05301997}
Viresh Kumar129eec52014-11-27 08:54:06 +05301998
1999/**
Sudeep Holla411466c2016-05-03 15:05:04 +01002000 * dev_pm_opp_remove_table() - Free all OPPs associated with the device
Viresh Kumar2c2709d2016-02-16 14:17:53 +05302001 * @dev: device pointer used to lookup OPP table.
Viresh Kumar129eec52014-11-27 08:54:06 +05302002 *
Sudeep Holla411466c2016-05-03 15:05:04 +01002003 * Free both OPPs created using static entries present in DT and the
2004 * dynamically added entries.
Viresh Kumar129eec52014-11-27 08:54:06 +05302005 */
Sudeep Holla411466c2016-05-03 15:05:04 +01002006void dev_pm_opp_remove_table(struct device *dev)
Viresh Kumar129eec52014-11-27 08:54:06 +05302007{
Viresh Kumar2a4eb732018-09-13 13:14:36 +05302008 _dev_pm_opp_find_and_remove_table(dev);
Viresh Kumar8d4d4e92015-06-12 17:10:38 +05302009}
Sudeep Holla411466c2016-05-03 15:05:04 +01002010EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table);