blob: b555121b878b1c0c234b02a1f7d74958d9549eb9 [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
199 count = opp_table->regulator_count;
Viresh Kumardfbe4672016-12-01 16:28:19 +0530200
201 /* Regulator may not be required for the device */
202 if (!count)
Viresh Kumarcdd3e612017-01-23 10:11:51 +0530203 goto put_opp_table;
Viresh Kumardfbe4672016-12-01 16:28:19 +0530204
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 Kumara1e8c132018-04-06 14:35:45 +0530321 return 0;
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 Kumar94735582016-12-01 16:28:20 +0530536static inline int
537_generic_set_opp_clk_only(struct device *dev, struct clk *clk,
538 unsigned long old_freq, unsigned long freq)
539{
540 int ret;
541
542 ret = clk_set_rate(clk, freq);
543 if (ret) {
544 dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
545 ret);
546 }
547
548 return ret;
549}
550
Viresh Kumar009acd12017-10-11 12:54:14 +0530551static inline int
552_generic_set_opp_domain(struct device *dev, struct clk *clk,
553 unsigned long old_freq, unsigned long freq,
554 unsigned int old_pstate, unsigned int new_pstate)
555{
556 int ret;
557
558 /* Scaling up? Scale domain performance state before frequency */
559 if (freq > old_freq) {
560 ret = dev_pm_genpd_set_performance_state(dev, new_pstate);
561 if (ret)
562 return ret;
563 }
564
565 ret = _generic_set_opp_clk_only(dev, clk, old_freq, freq);
566 if (ret)
567 goto restore_domain_state;
568
569 /* Scaling down? Scale domain performance state after frequency */
570 if (freq < old_freq) {
571 ret = dev_pm_genpd_set_performance_state(dev, new_pstate);
572 if (ret)
573 goto restore_freq;
574 }
575
576 return 0;
577
578restore_freq:
579 if (_generic_set_opp_clk_only(dev, clk, freq, old_freq))
580 dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n",
581 __func__, old_freq);
582restore_domain_state:
583 if (freq > old_freq)
584 dev_pm_genpd_set_performance_state(dev, old_pstate);
585
586 return ret;
587}
588
Viresh Kumarc74b32f2017-05-23 09:32:10 +0530589static int _generic_set_opp_regulator(const struct opp_table *opp_table,
590 struct device *dev,
591 unsigned long old_freq,
592 unsigned long freq,
593 struct dev_pm_opp_supply *old_supply,
594 struct dev_pm_opp_supply *new_supply)
Viresh Kumar94735582016-12-01 16:28:20 +0530595{
Viresh Kumarc74b32f2017-05-23 09:32:10 +0530596 struct regulator *reg = opp_table->regulators[0];
Viresh Kumar94735582016-12-01 16:28:20 +0530597 int ret;
598
599 /* This function only supports single regulator per device */
Viresh Kumarc74b32f2017-05-23 09:32:10 +0530600 if (WARN_ON(opp_table->regulator_count > 1)) {
Viresh Kumar94735582016-12-01 16:28:20 +0530601 dev_err(dev, "multiple regulators are not supported\n");
602 return -EINVAL;
603 }
604
605 /* Scaling up? Scale voltage before frequency */
Waldemar Rymarkiewiczc5c2a972018-06-14 15:56:08 +0200606 if (freq >= old_freq) {
Viresh Kumar94735582016-12-01 16:28:20 +0530607 ret = _set_opp_voltage(dev, reg, new_supply);
608 if (ret)
609 goto restore_voltage;
610 }
611
612 /* Change frequency */
Viresh Kumarc74b32f2017-05-23 09:32:10 +0530613 ret = _generic_set_opp_clk_only(dev, opp_table->clk, old_freq, freq);
Viresh Kumar94735582016-12-01 16:28:20 +0530614 if (ret)
615 goto restore_voltage;
616
617 /* Scaling down? Scale voltage after frequency */
618 if (freq < old_freq) {
619 ret = _set_opp_voltage(dev, reg, new_supply);
620 if (ret)
621 goto restore_freq;
622 }
623
624 return 0;
625
626restore_freq:
Viresh Kumarc74b32f2017-05-23 09:32:10 +0530627 if (_generic_set_opp_clk_only(dev, opp_table->clk, freq, old_freq))
Viresh Kumar94735582016-12-01 16:28:20 +0530628 dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n",
629 __func__, old_freq);
630restore_voltage:
631 /* This shouldn't harm even if the voltages weren't updated earlier */
Viresh Kumarc74b32f2017-05-23 09:32:10 +0530632 if (old_supply)
Viresh Kumar94735582016-12-01 16:28:20 +0530633 _set_opp_voltage(dev, reg, old_supply);
634
635 return ret;
636}
637
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530638/**
639 * dev_pm_opp_set_rate() - Configure new OPP based on frequency
640 * @dev: device for which we do this operation
641 * @target_freq: frequency to achieve
642 *
643 * This configures the power-supplies and clock source to the levels specified
644 * by the OPP corresponding to the target_freq.
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530645 */
646int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq)
647{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530648 struct opp_table *opp_table;
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530649 unsigned long freq, old_freq;
Viresh Kumar94735582016-12-01 16:28:20 +0530650 struct dev_pm_opp *old_opp, *opp;
Viresh Kumar94735582016-12-01 16:28:20 +0530651 struct clk *clk;
652 int ret, size;
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530653
654 if (unlikely(!target_freq)) {
655 dev_err(dev, "%s: Invalid target frequency %lu\n", __func__,
656 target_freq);
657 return -EINVAL;
658 }
659
Viresh Kumar052c6f12017-01-23 10:11:49 +0530660 opp_table = _find_opp_table(dev);
661 if (IS_ERR(opp_table)) {
662 dev_err(dev, "%s: device opp doesn't exist\n", __func__);
663 return PTR_ERR(opp_table);
664 }
665
666 clk = opp_table->clk;
667 if (IS_ERR(clk)) {
668 dev_err(dev, "%s: No clock available for the device\n",
669 __func__);
670 ret = PTR_ERR(clk);
671 goto put_opp_table;
672 }
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530673
674 freq = clk_round_rate(clk, target_freq);
675 if ((long)freq <= 0)
676 freq = target_freq;
677
678 old_freq = clk_get_rate(clk);
679
680 /* Return early if nothing to do */
681 if (old_freq == freq) {
682 dev_dbg(dev, "%s: old/new frequencies (%lu Hz) are same, nothing to do\n",
683 __func__, freq);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530684 ret = 0;
685 goto put_opp_table;
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530686 }
687
Jisheng Zhang067b7ce2016-07-25 14:11:16 +0800688 old_opp = _find_freq_ceil(opp_table, &old_freq);
Arnd Bergmann4df27c92016-09-15 17:38:38 +0200689 if (IS_ERR(old_opp)) {
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530690 dev_err(dev, "%s: failed to find current OPP for freq %lu (%ld)\n",
691 __func__, old_freq, PTR_ERR(old_opp));
692 }
693
Jisheng Zhang067b7ce2016-07-25 14:11:16 +0800694 opp = _find_freq_ceil(opp_table, &freq);
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530695 if (IS_ERR(opp)) {
696 ret = PTR_ERR(opp);
697 dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n",
698 __func__, freq, ret);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530699 goto put_old_opp;
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530700 }
701
Viresh Kumar94735582016-12-01 16:28:20 +0530702 dev_dbg(dev, "%s: switching OPP: %lu Hz --> %lu Hz\n", __func__,
703 old_freq, freq);
Viresh Kumardfbe4672016-12-01 16:28:19 +0530704
Viresh Kumar94735582016-12-01 16:28:20 +0530705 /* Only frequency scaling */
Viresh Kumarc74b32f2017-05-23 09:32:10 +0530706 if (!opp_table->regulators) {
Viresh Kumar009acd12017-10-11 12:54:14 +0530707 /*
708 * We don't support devices with both regulator and
709 * domain performance-state for now.
710 */
711 if (opp_table->genpd_performance_state)
712 ret = _generic_set_opp_domain(dev, clk, old_freq, freq,
713 IS_ERR(old_opp) ? 0 : old_opp->pstate,
714 opp->pstate);
715 else
716 ret = _generic_set_opp_clk_only(dev, clk, old_freq, freq);
Viresh Kumarc74b32f2017-05-23 09:32:10 +0530717 } else if (!opp_table->set_opp) {
718 ret = _generic_set_opp_regulator(opp_table, dev, old_freq, freq,
719 IS_ERR(old_opp) ? NULL : old_opp->supplies,
720 opp->supplies);
721 } else {
722 struct dev_pm_set_opp_data *data;
723
724 data = opp_table->set_opp_data;
725 data->regulators = opp_table->regulators;
726 data->regulator_count = opp_table->regulator_count;
727 data->clk = clk;
728 data->dev = dev;
729
730 data->old_opp.rate = old_freq;
731 size = sizeof(*opp->supplies) * opp_table->regulator_count;
732 if (IS_ERR(old_opp))
733 memset(data->old_opp.supplies, 0, size);
734 else
735 memcpy(data->old_opp.supplies, old_opp->supplies, size);
736
737 data->new_opp.rate = freq;
738 memcpy(data->new_opp.supplies, opp->supplies, size);
739
740 ret = opp_table->set_opp(data);
Viresh Kumardfbe4672016-12-01 16:28:19 +0530741 }
742
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530743 dev_pm_opp_put(opp);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530744put_old_opp:
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530745 if (!IS_ERR(old_opp))
746 dev_pm_opp_put(old_opp);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530747put_opp_table:
Viresh Kumar5b650b32017-01-23 10:11:48 +0530748 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530749 return ret;
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530750}
751EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate);
752
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530753/* OPP-dev Helpers */
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530754static void _remove_opp_dev(struct opp_device *opp_dev,
755 struct opp_table *opp_table)
Viresh Kumar06441652015-07-29 16:23:04 +0530756{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530757 opp_debug_unregister(opp_dev, opp_table);
758 list_del(&opp_dev->node);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530759 kfree(opp_dev);
Viresh Kumar06441652015-07-29 16:23:04 +0530760}
761
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530762struct opp_device *_add_opp_dev(const struct device *dev,
763 struct opp_table *opp_table)
Viresh Kumar06441652015-07-29 16:23:04 +0530764{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530765 struct opp_device *opp_dev;
Viresh Kumardeaa5142015-11-11 07:59:01 +0530766 int ret;
Viresh Kumar06441652015-07-29 16:23:04 +0530767
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530768 opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL);
769 if (!opp_dev)
Viresh Kumar06441652015-07-29 16:23:04 +0530770 return NULL;
771
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530772 /* Initialize opp-dev */
773 opp_dev->dev = dev;
Viresh Kumar3d255692018-08-03 07:05:21 +0530774
775 mutex_lock(&opp_table->lock);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530776 list_add(&opp_dev->node, &opp_table->dev_list);
Viresh Kumar06441652015-07-29 16:23:04 +0530777
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530778 /* Create debugfs entries for the opp_table */
779 ret = opp_debug_register(opp_dev, opp_table);
Viresh Kumardeaa5142015-11-11 07:59:01 +0530780 if (ret)
781 dev_err(dev, "%s: Failed to register opp debugfs (%d)\n",
782 __func__, ret);
Viresh Kumar3d255692018-08-03 07:05:21 +0530783 mutex_unlock(&opp_table->lock);
Viresh Kumardeaa5142015-11-11 07:59:01 +0530784
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530785 return opp_dev;
Viresh Kumar06441652015-07-29 16:23:04 +0530786}
787
Viresh Kumareb7c87432018-09-05 16:17:14 +0530788static struct opp_table *_allocate_opp_table(struct device *dev, int index)
Viresh Kumar07cce742014-12-10 09:45:34 +0530789{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530790 struct opp_table *opp_table;
791 struct opp_device *opp_dev;
Viresh Kumard54974c2016-02-09 10:30:38 +0530792 int ret;
Viresh Kumar07cce742014-12-10 09:45:34 +0530793
794 /*
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530795 * Allocate a new OPP table. In the infrequent case where a new
Viresh Kumar07cce742014-12-10 09:45:34 +0530796 * device is needed to be added, we pay this penalty.
797 */
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530798 opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL);
799 if (!opp_table)
Viresh Kumar07cce742014-12-10 09:45:34 +0530800 return NULL;
801
Viresh Kumar3d255692018-08-03 07:05:21 +0530802 mutex_init(&opp_table->lock);
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530803 INIT_LIST_HEAD(&opp_table->dev_list);
Viresh Kumar06441652015-07-29 16:23:04 +0530804
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530805 opp_dev = _add_opp_dev(dev, opp_table);
806 if (!opp_dev) {
807 kfree(opp_table);
Viresh Kumar06441652015-07-29 16:23:04 +0530808 return NULL;
809 }
810
Viresh Kumareb7c87432018-09-05 16:17:14 +0530811 _of_init_opp_table(opp_table, dev, index);
Viresh Kumar50f8cfb2016-02-09 10:30:37 +0530812
Viresh Kumard54974c2016-02-09 10:30:38 +0530813 /* Find clk for the device */
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530814 opp_table->clk = clk_get(dev, NULL);
815 if (IS_ERR(opp_table->clk)) {
816 ret = PTR_ERR(opp_table->clk);
Viresh Kumard54974c2016-02-09 10:30:38 +0530817 if (ret != -EPROBE_DEFER)
818 dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__,
819 ret);
820 }
821
Viresh Kumar052c6f12017-01-23 10:11:49 +0530822 BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head);
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530823 INIT_LIST_HEAD(&opp_table->opp_list);
Viresh Kumarf067a982017-01-23 10:11:42 +0530824 kref_init(&opp_table->kref);
Viresh Kumar07cce742014-12-10 09:45:34 +0530825
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530826 /* Secure the device table modification */
Viresh Kumar052c6f12017-01-23 10:11:49 +0530827 list_add(&opp_table->node, &opp_tables);
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530828 return opp_table;
Viresh Kumar07cce742014-12-10 09:45:34 +0530829}
830
Viresh Kumarf067a982017-01-23 10:11:42 +0530831void _get_opp_table_kref(struct opp_table *opp_table)
Viresh Kumar3bac42c2015-07-29 16:22:59 +0530832{
Viresh Kumarf067a982017-01-23 10:11:42 +0530833 kref_get(&opp_table->kref);
834}
835
Viresh Kumareb7c87432018-09-05 16:17:14 +0530836static struct opp_table *_opp_get_opp_table(struct device *dev, int index)
Viresh Kumarf067a982017-01-23 10:11:42 +0530837{
838 struct opp_table *opp_table;
839
840 /* Hold our table modification lock here */
841 mutex_lock(&opp_table_lock);
842
Viresh Kumar5b650b32017-01-23 10:11:48 +0530843 opp_table = _find_opp_table_unlocked(dev);
844 if (!IS_ERR(opp_table))
Viresh Kumarf067a982017-01-23 10:11:42 +0530845 goto unlock;
Viresh Kumarf067a982017-01-23 10:11:42 +0530846
Viresh Kumareb7c87432018-09-05 16:17:14 +0530847 opp_table = _allocate_opp_table(dev, index);
Viresh Kumarf067a982017-01-23 10:11:42 +0530848
849unlock:
850 mutex_unlock(&opp_table_lock);
851
852 return opp_table;
853}
Viresh Kumareb7c87432018-09-05 16:17:14 +0530854
855struct opp_table *dev_pm_opp_get_opp_table(struct device *dev)
856{
857 return _opp_get_opp_table(dev, 0);
858}
Viresh Kumarf067a982017-01-23 10:11:42 +0530859EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table);
860
Viresh Kumareb7c87432018-09-05 16:17:14 +0530861struct opp_table *dev_pm_opp_get_opp_table_indexed(struct device *dev,
862 int index)
863{
864 return _opp_get_opp_table(dev, index);
865}
866
Viresh Kumarb83c1892017-01-23 10:11:45 +0530867static void _opp_table_kref_release(struct kref *kref)
Viresh Kumarf067a982017-01-23 10:11:42 +0530868{
869 struct opp_table *opp_table = container_of(kref, struct opp_table, kref);
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530870 struct opp_device *opp_dev;
Viresh Kumar06441652015-07-29 16:23:04 +0530871
Viresh Kumard54974c2016-02-09 10:30:38 +0530872 /* Release clk */
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530873 if (!IS_ERR(opp_table->clk))
874 clk_put(opp_table->clk);
Viresh Kumard54974c2016-02-09 10:30:38 +0530875
Viresh Kumar3d255692018-08-03 07:05:21 +0530876 /*
877 * No need to take opp_table->lock here as we are guaranteed that no
878 * references to the OPP table are taken at this point.
879 */
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530880 opp_dev = list_first_entry(&opp_table->dev_list, struct opp_device,
881 node);
Viresh Kumar06441652015-07-29 16:23:04 +0530882
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530883 _remove_opp_dev(opp_dev, opp_table);
Viresh Kumar06441652015-07-29 16:23:04 +0530884
885 /* dev_list must be empty now */
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530886 WARN_ON(!list_empty(&opp_table->dev_list));
Viresh Kumar06441652015-07-29 16:23:04 +0530887
Viresh Kumar37a73ec2017-01-23 10:11:41 +0530888 mutex_destroy(&opp_table->lock);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530889 list_del(&opp_table->node);
890 kfree(opp_table);
Viresh Kumar3bac42c2015-07-29 16:22:59 +0530891
Viresh Kumarf067a982017-01-23 10:11:42 +0530892 mutex_unlock(&opp_table_lock);
893}
894
Viresh Kumard0e8ae62018-09-11 11:14:11 +0530895void _opp_remove_all_static(struct opp_table *opp_table)
896{
897 struct dev_pm_opp *opp, *tmp;
898
899 list_for_each_entry_safe(opp, tmp, &opp_table->opp_list, node) {
900 if (!opp->dynamic)
901 dev_pm_opp_put(opp);
902 }
903
904 opp_table->parsed_static_opps = false;
905}
906
907static void _opp_table_list_kref_release(struct kref *kref)
908{
909 struct opp_table *opp_table = container_of(kref, struct opp_table,
910 list_kref);
911
912 _opp_remove_all_static(opp_table);
913 mutex_unlock(&opp_table_lock);
914}
915
916void _put_opp_list_kref(struct opp_table *opp_table)
917{
918 kref_put_mutex(&opp_table->list_kref, _opp_table_list_kref_release,
919 &opp_table_lock);
920}
921
Viresh Kumarf067a982017-01-23 10:11:42 +0530922void dev_pm_opp_put_opp_table(struct opp_table *opp_table)
923{
924 kref_put_mutex(&opp_table->kref, _opp_table_kref_release,
925 &opp_table_lock);
926}
927EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table);
928
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +0530929void _opp_free(struct dev_pm_opp *opp)
Viresh Kumar969fceb2017-01-02 14:40:59 +0530930{
931 kfree(opp);
Viresh Kumar969fceb2017-01-02 14:40:59 +0530932}
933
Viresh Kumar70347642017-01-23 10:11:46 +0530934static void _opp_kref_release(struct kref *kref)
Viresh Kumar129eec52014-11-27 08:54:06 +0530935{
Viresh Kumar70347642017-01-23 10:11:46 +0530936 struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref);
937 struct opp_table *opp_table = opp->opp_table;
Viresh Kumar37a73ec2017-01-23 10:11:41 +0530938
Viresh Kumar129eec52014-11-27 08:54:06 +0530939 /*
940 * Notify the changes in the availability of the operable
941 * frequency/voltage list.
942 */
Viresh Kumar052c6f12017-01-23 10:11:49 +0530943 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_REMOVE, opp);
Viresh Kumardeaa5142015-11-11 07:59:01 +0530944 opp_debug_remove_one(opp);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530945 list_del(&opp->node);
946 kfree(opp);
Viresh Kumar129eec52014-11-27 08:54:06 +0530947
Viresh Kumar37a73ec2017-01-23 10:11:41 +0530948 mutex_unlock(&opp_table->lock);
Viresh Kumar129eec52014-11-27 08:54:06 +0530949}
950
Viresh Kumara88bd2a2017-11-29 15:18:36 +0530951void dev_pm_opp_get(struct dev_pm_opp *opp)
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530952{
953 kref_get(&opp->kref);
954}
955
Viresh Kumar70347642017-01-23 10:11:46 +0530956void dev_pm_opp_put(struct dev_pm_opp *opp)
957{
958 kref_put_mutex(&opp->kref, _opp_kref_release, &opp->opp_table->lock);
959}
960EXPORT_SYMBOL_GPL(dev_pm_opp_put);
961
Viresh Kumar129eec52014-11-27 08:54:06 +0530962/**
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530963 * dev_pm_opp_remove() - Remove an OPP from OPP table
Viresh Kumar129eec52014-11-27 08:54:06 +0530964 * @dev: device for which we do this operation
965 * @freq: OPP to remove with matching 'freq'
966 *
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530967 * This function removes an opp from the opp table.
Viresh Kumar129eec52014-11-27 08:54:06 +0530968 */
969void dev_pm_opp_remove(struct device *dev, unsigned long freq)
970{
971 struct dev_pm_opp *opp;
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530972 struct opp_table *opp_table;
Viresh Kumar129eec52014-11-27 08:54:06 +0530973 bool found = false;
974
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530975 opp_table = _find_opp_table(dev);
976 if (IS_ERR(opp_table))
Viresh Kumar5b650b32017-01-23 10:11:48 +0530977 return;
Viresh Kumar129eec52014-11-27 08:54:06 +0530978
Viresh Kumar37a73ec2017-01-23 10:11:41 +0530979 mutex_lock(&opp_table->lock);
980
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530981 list_for_each_entry(opp, &opp_table->opp_list, node) {
Viresh Kumar129eec52014-11-27 08:54:06 +0530982 if (opp->rate == freq) {
983 found = true;
984 break;
985 }
986 }
987
Viresh Kumar37a73ec2017-01-23 10:11:41 +0530988 mutex_unlock(&opp_table->lock);
989
Viresh Kumar5b650b32017-01-23 10:11:48 +0530990 if (found) {
991 dev_pm_opp_put(opp);
Viresh Kumar0ad8c622018-08-02 14:23:09 +0530992
993 /* Drop the reference taken by dev_pm_opp_add() */
994 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530995 } else {
Viresh Kumar129eec52014-11-27 08:54:06 +0530996 dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
997 __func__, freq);
Viresh Kumar129eec52014-11-27 08:54:06 +0530998 }
999
Viresh Kumar0ad8c622018-08-02 14:23:09 +05301000 /* Drop the reference taken by _find_opp_table() */
Viresh Kumar5b650b32017-01-23 10:11:48 +05301001 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar129eec52014-11-27 08:54:06 +05301002}
1003EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
1004
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301005struct dev_pm_opp *_opp_allocate(struct opp_table *table)
Viresh Kumar23dacf62015-07-29 16:23:01 +05301006{
1007 struct dev_pm_opp *opp;
Viresh Kumardfbe4672016-12-01 16:28:19 +05301008 int count, supply_size;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301009
Viresh Kumardfbe4672016-12-01 16:28:19 +05301010 /* Allocate space for at least one supply */
1011 count = table->regulator_count ? table->regulator_count : 1;
1012 supply_size = sizeof(*opp->supplies) * count;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301013
Viresh Kumardfbe4672016-12-01 16:28:19 +05301014 /* allocate new OPP node and supplies structures */
1015 opp = kzalloc(sizeof(*opp) + supply_size, GFP_KERNEL);
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301016 if (!opp)
Viresh Kumar23dacf62015-07-29 16:23:01 +05301017 return NULL;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301018
Viresh Kumardfbe4672016-12-01 16:28:19 +05301019 /* Put the supplies at the end of the OPP structure as an empty array */
1020 opp->supplies = (struct dev_pm_opp_supply *)(opp + 1);
1021 INIT_LIST_HEAD(&opp->node);
1022
Viresh Kumar23dacf62015-07-29 16:23:01 +05301023 return opp;
1024}
1025
Viresh Kumar7d34d562016-02-09 10:30:34 +05301026static bool _opp_supported_by_regulators(struct dev_pm_opp *opp,
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301027 struct opp_table *opp_table)
Viresh Kumar7d34d562016-02-09 10:30:34 +05301028{
Viresh Kumardfbe4672016-12-01 16:28:19 +05301029 struct regulator *reg;
1030 int i;
Viresh Kumar7d34d562016-02-09 10:30:34 +05301031
Viresh Kumardfbe4672016-12-01 16:28:19 +05301032 for (i = 0; i < opp_table->regulator_count; i++) {
1033 reg = opp_table->regulators[i];
1034
1035 if (!regulator_is_supported_voltage(reg,
1036 opp->supplies[i].u_volt_min,
1037 opp->supplies[i].u_volt_max)) {
1038 pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n",
1039 __func__, opp->supplies[i].u_volt_min,
1040 opp->supplies[i].u_volt_max);
1041 return false;
1042 }
Viresh Kumar7d34d562016-02-09 10:30:34 +05301043 }
1044
1045 return true;
1046}
1047
Viresh Kumara1e8c132018-04-06 14:35:45 +05301048static int _opp_is_duplicate(struct device *dev, struct dev_pm_opp *new_opp,
1049 struct opp_table *opp_table,
1050 struct list_head **head)
1051{
1052 struct dev_pm_opp *opp;
1053
1054 /*
1055 * Insert new OPP in order of increasing frequency and discard if
1056 * already present.
1057 *
1058 * Need to use &opp_table->opp_list in the condition part of the 'for'
1059 * loop, don't replace it with head otherwise it will become an infinite
1060 * loop.
1061 */
1062 list_for_each_entry(opp, &opp_table->opp_list, node) {
1063 if (new_opp->rate > opp->rate) {
1064 *head = &opp->node;
1065 continue;
1066 }
1067
1068 if (new_opp->rate < opp->rate)
1069 return 0;
1070
1071 /* Duplicate OPPs */
1072 dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
1073 __func__, opp->rate, opp->supplies[0].u_volt,
1074 opp->available, new_opp->rate,
1075 new_opp->supplies[0].u_volt, new_opp->available);
1076
1077 /* Should we compare voltages for all regulators here ? */
1078 return opp->available &&
1079 new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST;
1080 }
1081
1082 return 0;
1083}
1084
Viresh Kumar7f8538e2017-01-02 14:40:55 +05301085/*
1086 * Returns:
1087 * 0: On success. And appropriate error message for duplicate OPPs.
1088 * -EBUSY: For OPP with same freq/volt and is available. The callers of
1089 * _opp_add() must return 0 if they receive -EBUSY from it. This is to make
1090 * sure we don't print error messages unnecessarily if different parts of
1091 * kernel try to initialize the OPP table.
1092 * -EEXIST: For OPP with same freq but different volt or is unavailable. This
1093 * should be considered an error by the callers of _opp_add().
1094 */
Viresh Kumarf47b72a2016-05-05 16:20:33 +05301095int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
Viresh Kumara1e8c132018-04-06 14:35:45 +05301096 struct opp_table *opp_table, bool rate_not_available)
Viresh Kumar23dacf62015-07-29 16:23:01 +05301097{
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301098 struct list_head *head;
Viresh Kumardeaa5142015-11-11 07:59:01 +05301099 int ret;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301100
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301101 mutex_lock(&opp_table->lock);
1102 head = &opp_table->opp_list;
1103
Viresh Kumara1e8c132018-04-06 14:35:45 +05301104 if (likely(!rate_not_available)) {
1105 ret = _opp_is_duplicate(dev, new_opp, opp_table, &head);
1106 if (ret) {
1107 mutex_unlock(&opp_table->lock);
1108 return ret;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301109 }
Viresh Kumar23dacf62015-07-29 16:23:01 +05301110 }
1111
Viresh Kumar052c6f12017-01-23 10:11:49 +05301112 list_add(&new_opp->node, head);
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301113 mutex_unlock(&opp_table->lock);
1114
1115 new_opp->opp_table = opp_table;
Viresh Kumar70347642017-01-23 10:11:46 +05301116 kref_init(&new_opp->kref);
Viresh Kumar23dacf62015-07-29 16:23:01 +05301117
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301118 ret = opp_debug_create_one(new_opp, opp_table);
Viresh Kumardeaa5142015-11-11 07:59:01 +05301119 if (ret)
1120 dev_err(dev, "%s: Failed to register opp to debugfs (%d)\n",
1121 __func__, ret);
1122
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301123 if (!_opp_supported_by_regulators(new_opp, opp_table)) {
Viresh Kumar7d34d562016-02-09 10:30:34 +05301124 new_opp->available = false;
1125 dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n",
1126 __func__, new_opp->rate);
1127 }
1128
Viresh Kumar23dacf62015-07-29 16:23:01 +05301129 return 0;
1130}
1131
Viresh Kumar737002b2015-07-29 16:22:58 +05301132/**
Viresh Kumarb64b9c3f2015-10-15 21:42:44 +05301133 * _opp_add_v1() - Allocate a OPP based on v1 bindings.
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301134 * @opp_table: OPP table
Nishanth Menone1f60b22010-10-13 00:13:10 +02001135 * @dev: device for which we do this operation
1136 * @freq: Frequency in Hz for this OPP
1137 * @u_volt: Voltage in uVolts for this OPP
1138 * @dynamic: Dynamically added OPPs.
1139 *
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301140 * This function adds an opp definition to the opp table and returns status.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001141 * The opp is made available by default and it can be controlled using
1142 * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
1143 *
Viresh Kumar8f8d37b2015-09-04 13:47:24 +05301144 * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table
1145 * and freed by dev_pm_opp_of_remove_table.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001146 *
Nishanth Menone1f60b22010-10-13 00:13:10 +02001147 * Return:
1148 * 0 On success OR
1149 * Duplicate OPPs (both freq and volt are same) and opp->available
1150 * -EEXIST Freq are same and volt are different OR
1151 * Duplicate OPPs (both freq and volt are same) and !opp->available
1152 * -ENOMEM Memory allocation failure
1153 */
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301154int _opp_add_v1(struct opp_table *opp_table, struct device *dev,
1155 unsigned long freq, long u_volt, bool dynamic)
Nishanth Menone1f60b22010-10-13 00:13:10 +02001156{
Viresh Kumar23dacf62015-07-29 16:23:01 +05301157 struct dev_pm_opp *new_opp;
Viresh Kumar50f8cfb2016-02-09 10:30:37 +05301158 unsigned long tol;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001159 int ret;
1160
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301161 new_opp = _opp_allocate(opp_table);
1162 if (!new_opp)
1163 return -ENOMEM;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301164
Nishanth Menone1f60b22010-10-13 00:13:10 +02001165 /* populate the opp table */
1166 new_opp->rate = freq;
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301167 tol = u_volt * opp_table->voltage_tolerance_v1 / 100;
Viresh Kumardfbe4672016-12-01 16:28:19 +05301168 new_opp->supplies[0].u_volt = u_volt;
1169 new_opp->supplies[0].u_volt_min = u_volt - tol;
1170 new_opp->supplies[0].u_volt_max = u_volt + tol;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001171 new_opp->available = true;
Viresh Kumaraa5f2f82015-07-29 16:23:00 +05301172 new_opp->dynamic = dynamic;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301173
Viresh Kumara1e8c132018-04-06 14:35:45 +05301174 ret = _opp_add(dev, new_opp, opp_table, false);
Viresh Kumar7f8538e2017-01-02 14:40:55 +05301175 if (ret) {
1176 /* Don't return error for duplicate OPPs */
1177 if (ret == -EBUSY)
1178 ret = 0;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301179 goto free_opp;
Viresh Kumar7f8538e2017-01-02 14:40:55 +05301180 }
Viresh Kumar23dacf62015-07-29 16:23:01 +05301181
Nishanth Menone1f60b22010-10-13 00:13:10 +02001182 /*
1183 * Notify the changes in the availability of the operable
1184 * frequency/voltage list.
1185 */
Viresh Kumar052c6f12017-01-23 10:11:49 +05301186 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001187 return 0;
1188
1189free_opp:
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301190 _opp_free(new_opp);
1191
Nishanth Menone1f60b22010-10-13 00:13:10 +02001192 return ret;
1193}
Viresh Kumar383934092014-11-25 16:04:18 +05301194
Viresh Kumar27465902015-07-29 16:23:02 +05301195/**
Viresh Kumar7de36b02015-12-09 08:01:46 +05301196 * dev_pm_opp_set_supported_hw() - Set supported platforms
1197 * @dev: Device for which supported-hw has to be set.
1198 * @versions: Array of hierarchy of versions to match.
1199 * @count: Number of elements in the array.
1200 *
1201 * This is required only for the V2 bindings, and it enables a platform to
1202 * specify the hierarchy of versions it supports. OPP layer will then enable
1203 * OPPs, which are available for those versions, based on its 'opp-supported-hw'
1204 * property.
Viresh Kumar7de36b02015-12-09 08:01:46 +05301205 */
Viresh Kumarfa301842017-01-23 10:11:43 +05301206struct opp_table *dev_pm_opp_set_supported_hw(struct device *dev,
1207 const u32 *versions, unsigned int count)
Viresh Kumar7de36b02015-12-09 08:01:46 +05301208{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301209 struct opp_table *opp_table;
Viresh Kumar7de36b02015-12-09 08:01:46 +05301210
Viresh Kumarfa301842017-01-23 10:11:43 +05301211 opp_table = dev_pm_opp_get_opp_table(dev);
1212 if (!opp_table)
1213 return ERR_PTR(-ENOMEM);
Viresh Kumar7de36b02015-12-09 08:01:46 +05301214
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301215 /* Make sure there are no concurrent readers while updating opp_table */
1216 WARN_ON(!list_empty(&opp_table->opp_list));
Viresh Kumar7de36b02015-12-09 08:01:46 +05301217
Viresh Kumar25419de2018-05-22 16:38:08 +05301218 /* Another CPU that shares the OPP table has set the property ? */
1219 if (opp_table->supported_hw)
1220 return opp_table;
Viresh Kumar7de36b02015-12-09 08:01:46 +05301221
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301222 opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions),
Viresh Kumar7de36b02015-12-09 08:01:46 +05301223 GFP_KERNEL);
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301224 if (!opp_table->supported_hw) {
Viresh Kumar25419de2018-05-22 16:38:08 +05301225 dev_pm_opp_put_opp_table(opp_table);
1226 return ERR_PTR(-ENOMEM);
Viresh Kumar7de36b02015-12-09 08:01:46 +05301227 }
1228
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301229 opp_table->supported_hw_count = count;
Viresh Kumarfa301842017-01-23 10:11:43 +05301230
1231 return opp_table;
Viresh Kumar7de36b02015-12-09 08:01:46 +05301232}
1233EXPORT_SYMBOL_GPL(dev_pm_opp_set_supported_hw);
1234
1235/**
1236 * dev_pm_opp_put_supported_hw() - Releases resources blocked for supported hw
Viresh Kumarfa301842017-01-23 10:11:43 +05301237 * @opp_table: OPP table returned by dev_pm_opp_set_supported_hw().
Viresh Kumar7de36b02015-12-09 08:01:46 +05301238 *
1239 * This is required only for the V2 bindings, and is called for a matching
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301240 * dev_pm_opp_set_supported_hw(). Until this is called, the opp_table structure
Viresh Kumar7de36b02015-12-09 08:01:46 +05301241 * will not be freed.
Viresh Kumar7de36b02015-12-09 08:01:46 +05301242 */
Viresh Kumarfa301842017-01-23 10:11:43 +05301243void dev_pm_opp_put_supported_hw(struct opp_table *opp_table)
Viresh Kumar7de36b02015-12-09 08:01:46 +05301244{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301245 /* Make sure there are no concurrent readers while updating opp_table */
1246 WARN_ON(!list_empty(&opp_table->opp_list));
Viresh Kumar7de36b02015-12-09 08:01:46 +05301247
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301248 kfree(opp_table->supported_hw);
1249 opp_table->supported_hw = NULL;
1250 opp_table->supported_hw_count = 0;
Viresh Kumar7de36b02015-12-09 08:01:46 +05301251
Viresh Kumarfa301842017-01-23 10:11:43 +05301252 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar7de36b02015-12-09 08:01:46 +05301253}
1254EXPORT_SYMBOL_GPL(dev_pm_opp_put_supported_hw);
1255
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301256/**
1257 * dev_pm_opp_set_prop_name() - Set prop-extn name
Viresh Kumara5da6442016-02-16 14:17:52 +05301258 * @dev: Device for which the prop-name has to be set.
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301259 * @name: name to postfix to properties.
1260 *
1261 * This is required only for the V2 bindings, and it enables a platform to
1262 * specify the extn to be used for certain property names. The properties to
1263 * which the extension will apply are opp-microvolt and opp-microamp. OPP core
1264 * should postfix the property name with -<name> while looking for them.
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301265 */
Viresh Kumarfa301842017-01-23 10:11:43 +05301266struct opp_table *dev_pm_opp_set_prop_name(struct device *dev, const char *name)
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301267{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301268 struct opp_table *opp_table;
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301269
Viresh Kumarfa301842017-01-23 10:11:43 +05301270 opp_table = dev_pm_opp_get_opp_table(dev);
1271 if (!opp_table)
1272 return ERR_PTR(-ENOMEM);
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301273
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301274 /* Make sure there are no concurrent readers while updating opp_table */
1275 WARN_ON(!list_empty(&opp_table->opp_list));
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301276
Viresh Kumar878ec1a2018-05-22 16:38:08 +05301277 /* Another CPU that shares the OPP table has set the property ? */
1278 if (opp_table->prop_name)
1279 return opp_table;
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301280
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301281 opp_table->prop_name = kstrdup(name, GFP_KERNEL);
1282 if (!opp_table->prop_name) {
Viresh Kumar878ec1a2018-05-22 16:38:08 +05301283 dev_pm_opp_put_opp_table(opp_table);
1284 return ERR_PTR(-ENOMEM);
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301285 }
1286
Viresh Kumarfa301842017-01-23 10:11:43 +05301287 return opp_table;
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301288}
1289EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name);
1290
1291/**
1292 * dev_pm_opp_put_prop_name() - Releases resources blocked for prop-name
Viresh Kumarfa301842017-01-23 10:11:43 +05301293 * @opp_table: OPP table returned by dev_pm_opp_set_prop_name().
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301294 *
1295 * This is required only for the V2 bindings, and is called for a matching
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301296 * dev_pm_opp_set_prop_name(). Until this is called, the opp_table structure
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301297 * will not be freed.
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301298 */
Viresh Kumarfa301842017-01-23 10:11:43 +05301299void dev_pm_opp_put_prop_name(struct opp_table *opp_table)
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301300{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301301 /* Make sure there are no concurrent readers while updating opp_table */
1302 WARN_ON(!list_empty(&opp_table->opp_list));
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301303
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301304 kfree(opp_table->prop_name);
1305 opp_table->prop_name = NULL;
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301306
Viresh Kumarfa301842017-01-23 10:11:43 +05301307 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301308}
1309EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name);
1310
Viresh Kumar94735582016-12-01 16:28:20 +05301311static int _allocate_set_opp_data(struct opp_table *opp_table)
1312{
1313 struct dev_pm_set_opp_data *data;
1314 int len, count = opp_table->regulator_count;
1315
1316 if (WARN_ON(!count))
1317 return -EINVAL;
1318
1319 /* space for set_opp_data */
1320 len = sizeof(*data);
1321
1322 /* space for old_opp.supplies and new_opp.supplies */
1323 len += 2 * sizeof(struct dev_pm_opp_supply) * count;
1324
1325 data = kzalloc(len, GFP_KERNEL);
1326 if (!data)
1327 return -ENOMEM;
1328
1329 data->old_opp.supplies = (void *)(data + 1);
1330 data->new_opp.supplies = data->old_opp.supplies + count;
1331
1332 opp_table->set_opp_data = data;
1333
1334 return 0;
1335}
1336
1337static void _free_set_opp_data(struct opp_table *opp_table)
1338{
1339 kfree(opp_table->set_opp_data);
1340 opp_table->set_opp_data = NULL;
1341}
1342
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301343/**
Viresh Kumardfbe4672016-12-01 16:28:19 +05301344 * dev_pm_opp_set_regulators() - Set regulator names for the device
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301345 * @dev: Device for which regulator name is being set.
Viresh Kumardfbe4672016-12-01 16:28:19 +05301346 * @names: Array of pointers to the names of the regulator.
1347 * @count: Number of regulators.
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301348 *
1349 * In order to support OPP switching, OPP layer needs to know the name of the
Viresh Kumardfbe4672016-12-01 16:28:19 +05301350 * device's regulators, as the core would be required to switch voltages as
1351 * well.
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301352 *
1353 * This must be called before any OPPs are initialized for the device.
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301354 */
Viresh Kumardfbe4672016-12-01 16:28:19 +05301355struct opp_table *dev_pm_opp_set_regulators(struct device *dev,
1356 const char * const names[],
1357 unsigned int count)
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301358{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301359 struct opp_table *opp_table;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301360 struct regulator *reg;
Viresh Kumardfbe4672016-12-01 16:28:19 +05301361 int ret, i;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301362
Viresh Kumarfa301842017-01-23 10:11:43 +05301363 opp_table = dev_pm_opp_get_opp_table(dev);
1364 if (!opp_table)
1365 return ERR_PTR(-ENOMEM);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301366
1367 /* This should be called before OPPs are initialized */
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301368 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301369 ret = -EBUSY;
1370 goto err;
1371 }
1372
Viresh Kumar779b7832018-05-22 16:38:08 +05301373 /* Another CPU that shares the OPP table has set the regulators ? */
1374 if (opp_table->regulators)
1375 return opp_table;
Viresh Kumardfbe4672016-12-01 16:28:19 +05301376
1377 opp_table->regulators = kmalloc_array(count,
1378 sizeof(*opp_table->regulators),
1379 GFP_KERNEL);
1380 if (!opp_table->regulators) {
1381 ret = -ENOMEM;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301382 goto err;
1383 }
1384
Viresh Kumardfbe4672016-12-01 16:28:19 +05301385 for (i = 0; i < count; i++) {
1386 reg = regulator_get_optional(dev, names[i]);
1387 if (IS_ERR(reg)) {
1388 ret = PTR_ERR(reg);
1389 if (ret != -EPROBE_DEFER)
1390 dev_err(dev, "%s: no regulator (%s) found: %d\n",
1391 __func__, names[i], ret);
1392 goto free_regulators;
1393 }
1394
1395 opp_table->regulators[i] = reg;
1396 }
1397
1398 opp_table->regulator_count = count;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301399
Viresh Kumar94735582016-12-01 16:28:20 +05301400 /* Allocate block only once to pass to set_opp() routines */
1401 ret = _allocate_set_opp_data(opp_table);
1402 if (ret)
1403 goto free_regulators;
1404
Stephen Boyd91291d92016-11-30 16:21:25 +05301405 return opp_table;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301406
Viresh Kumardfbe4672016-12-01 16:28:19 +05301407free_regulators:
1408 while (i != 0)
1409 regulator_put(opp_table->regulators[--i]);
1410
1411 kfree(opp_table->regulators);
1412 opp_table->regulators = NULL;
Viresh Kumar94735582016-12-01 16:28:20 +05301413 opp_table->regulator_count = 0;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301414err:
Viresh Kumarfa301842017-01-23 10:11:43 +05301415 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301416
Stephen Boyd91291d92016-11-30 16:21:25 +05301417 return ERR_PTR(ret);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301418}
Viresh Kumardfbe4672016-12-01 16:28:19 +05301419EXPORT_SYMBOL_GPL(dev_pm_opp_set_regulators);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301420
1421/**
Viresh Kumardfbe4672016-12-01 16:28:19 +05301422 * dev_pm_opp_put_regulators() - Releases resources blocked for regulator
1423 * @opp_table: OPP table returned from dev_pm_opp_set_regulators().
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301424 */
Viresh Kumardfbe4672016-12-01 16:28:19 +05301425void dev_pm_opp_put_regulators(struct opp_table *opp_table)
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301426{
Viresh Kumardfbe4672016-12-01 16:28:19 +05301427 int i;
1428
Viresh Kumar779b7832018-05-22 16:38:08 +05301429 if (!opp_table->regulators)
1430 goto put_opp_table;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301431
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301432 /* Make sure there are no concurrent readers while updating opp_table */
1433 WARN_ON(!list_empty(&opp_table->opp_list));
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301434
Viresh Kumardfbe4672016-12-01 16:28:19 +05301435 for (i = opp_table->regulator_count - 1; i >= 0; i--)
1436 regulator_put(opp_table->regulators[i]);
1437
Viresh Kumar94735582016-12-01 16:28:20 +05301438 _free_set_opp_data(opp_table);
1439
Viresh Kumardfbe4672016-12-01 16:28:19 +05301440 kfree(opp_table->regulators);
1441 opp_table->regulators = NULL;
1442 opp_table->regulator_count = 0;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301443
Viresh Kumar779b7832018-05-22 16:38:08 +05301444put_opp_table:
Viresh Kumarfa301842017-01-23 10:11:43 +05301445 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301446}
Viresh Kumardfbe4672016-12-01 16:28:19 +05301447EXPORT_SYMBOL_GPL(dev_pm_opp_put_regulators);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301448
Viresh Kumar383934092014-11-25 16:04:18 +05301449/**
Viresh Kumar829a4e82017-06-21 10:29:13 +05301450 * dev_pm_opp_set_clkname() - Set clk name for the device
1451 * @dev: Device for which clk name is being set.
1452 * @name: Clk name.
1453 *
1454 * In order to support OPP switching, OPP layer needs to get pointer to the
1455 * clock for the device. Simple cases work fine without using this routine (i.e.
1456 * by passing connection-id as NULL), but for a device with multiple clocks
1457 * available, the OPP core needs to know the exact name of the clk to use.
1458 *
1459 * This must be called before any OPPs are initialized for the device.
1460 */
1461struct opp_table *dev_pm_opp_set_clkname(struct device *dev, const char *name)
1462{
1463 struct opp_table *opp_table;
1464 int ret;
1465
1466 opp_table = dev_pm_opp_get_opp_table(dev);
1467 if (!opp_table)
1468 return ERR_PTR(-ENOMEM);
1469
1470 /* This should be called before OPPs are initialized */
1471 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
1472 ret = -EBUSY;
1473 goto err;
1474 }
1475
1476 /* Already have default clk set, free it */
1477 if (!IS_ERR(opp_table->clk))
1478 clk_put(opp_table->clk);
1479
1480 /* Find clk for the device */
1481 opp_table->clk = clk_get(dev, name);
1482 if (IS_ERR(opp_table->clk)) {
1483 ret = PTR_ERR(opp_table->clk);
1484 if (ret != -EPROBE_DEFER) {
1485 dev_err(dev, "%s: Couldn't find clock: %d\n", __func__,
1486 ret);
1487 }
1488 goto err;
1489 }
1490
1491 return opp_table;
1492
1493err:
1494 dev_pm_opp_put_opp_table(opp_table);
1495
1496 return ERR_PTR(ret);
1497}
1498EXPORT_SYMBOL_GPL(dev_pm_opp_set_clkname);
1499
1500/**
1501 * dev_pm_opp_put_clkname() - Releases resources blocked for clk.
1502 * @opp_table: OPP table returned from dev_pm_opp_set_clkname().
1503 */
1504void dev_pm_opp_put_clkname(struct opp_table *opp_table)
1505{
1506 /* Make sure there are no concurrent readers while updating opp_table */
1507 WARN_ON(!list_empty(&opp_table->opp_list));
1508
1509 clk_put(opp_table->clk);
1510 opp_table->clk = ERR_PTR(-EINVAL);
1511
1512 dev_pm_opp_put_opp_table(opp_table);
1513}
1514EXPORT_SYMBOL_GPL(dev_pm_opp_put_clkname);
1515
1516/**
Viresh Kumar4dab1602016-12-01 16:28:21 +05301517 * dev_pm_opp_register_set_opp_helper() - Register custom set OPP helper
1518 * @dev: Device for which the helper is getting registered.
1519 * @set_opp: Custom set OPP helper.
1520 *
1521 * This is useful to support complex platforms (like platforms with multiple
1522 * regulators per device), instead of the generic OPP set rate helper.
1523 *
1524 * This must be called before any OPPs are initialized for the device.
Viresh Kumar4dab1602016-12-01 16:28:21 +05301525 */
Viresh Kumarfa301842017-01-23 10:11:43 +05301526struct opp_table *dev_pm_opp_register_set_opp_helper(struct device *dev,
Viresh Kumar4dab1602016-12-01 16:28:21 +05301527 int (*set_opp)(struct dev_pm_set_opp_data *data))
1528{
1529 struct opp_table *opp_table;
Viresh Kumar4dab1602016-12-01 16:28:21 +05301530
1531 if (!set_opp)
Viresh Kumarfa301842017-01-23 10:11:43 +05301532 return ERR_PTR(-EINVAL);
Viresh Kumar4dab1602016-12-01 16:28:21 +05301533
Viresh Kumarfa301842017-01-23 10:11:43 +05301534 opp_table = dev_pm_opp_get_opp_table(dev);
1535 if (!opp_table)
1536 return ERR_PTR(-ENOMEM);
Viresh Kumar4dab1602016-12-01 16:28:21 +05301537
1538 /* This should be called before OPPs are initialized */
1539 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
Viresh Kumar5019acc2018-05-22 16:38:08 +05301540 dev_pm_opp_put_opp_table(opp_table);
1541 return ERR_PTR(-EBUSY);
Viresh Kumar4dab1602016-12-01 16:28:21 +05301542 }
1543
Viresh Kumar5019acc2018-05-22 16:38:08 +05301544 /* Another CPU that shares the OPP table has set the helper ? */
1545 if (!opp_table->set_opp)
1546 opp_table->set_opp = set_opp;
Viresh Kumar4dab1602016-12-01 16:28:21 +05301547
Viresh Kumarfa301842017-01-23 10:11:43 +05301548 return opp_table;
Viresh Kumar4dab1602016-12-01 16:28:21 +05301549}
1550EXPORT_SYMBOL_GPL(dev_pm_opp_register_set_opp_helper);
1551
1552/**
Viresh Kumar604a7ae2017-10-05 17:26:21 +05301553 * dev_pm_opp_unregister_set_opp_helper() - Releases resources blocked for
Viresh Kumar4dab1602016-12-01 16:28:21 +05301554 * set_opp helper
Viresh Kumarfa301842017-01-23 10:11:43 +05301555 * @opp_table: OPP table returned from dev_pm_opp_register_set_opp_helper().
Viresh Kumar4dab1602016-12-01 16:28:21 +05301556 *
Viresh Kumarfa301842017-01-23 10:11:43 +05301557 * Release resources blocked for platform specific set_opp helper.
Viresh Kumar4dab1602016-12-01 16:28:21 +05301558 */
Viresh Kumar604a7ae2017-10-05 17:26:21 +05301559void dev_pm_opp_unregister_set_opp_helper(struct opp_table *opp_table)
Viresh Kumar4dab1602016-12-01 16:28:21 +05301560{
Viresh Kumar4dab1602016-12-01 16:28:21 +05301561 /* Make sure there are no concurrent readers while updating opp_table */
1562 WARN_ON(!list_empty(&opp_table->opp_list));
1563
1564 opp_table->set_opp = NULL;
Viresh Kumarfa301842017-01-23 10:11:43 +05301565 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar4dab1602016-12-01 16:28:21 +05301566}
Viresh Kumar604a7ae2017-10-05 17:26:21 +05301567EXPORT_SYMBOL_GPL(dev_pm_opp_unregister_set_opp_helper);
Viresh Kumar4dab1602016-12-01 16:28:21 +05301568
1569/**
Nishanth Menone1f60b22010-10-13 00:13:10 +02001570 * dev_pm_opp_add() - Add an OPP table from a table definitions
1571 * @dev: device for which we do this operation
Nishanth Menon47d43ba2013-09-19 16:03:51 -05001572 * @freq: Frequency in Hz for this OPP
Nishanth Menone1f60b22010-10-13 00:13:10 +02001573 * @u_volt: Voltage in uVolts for this OPP
1574 *
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301575 * This function adds an opp definition to the opp table and returns status.
Nishanth Menon47d43ba2013-09-19 16:03:51 -05001576 * The opp is made available by default and it can be controlled using
Nishanth Menone1f60b22010-10-13 00:13:10 +02001577 * dev_pm_opp_enable/disable functions.
1578 *
Viresh Kumara7470db2014-11-25 16:04:17 +05301579 * Return:
1580 * 0 On success OR
1581 * Duplicate OPPs (both freq and volt are same) and opp->available
1582 * -EEXIST Freq are same and volt are different OR
1583 * Duplicate OPPs (both freq and volt are same) and !opp->available
Viresh Kumar383934092014-11-25 16:04:18 +05301584 * -ENOMEM Memory allocation failure
Viresh Kumara7470db2014-11-25 16:04:17 +05301585 */
Nishanth Menone1f60b22010-10-13 00:13:10 +02001586int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
1587{
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301588 struct opp_table *opp_table;
1589 int ret;
1590
Viresh Kumarb83c1892017-01-23 10:11:45 +05301591 opp_table = dev_pm_opp_get_opp_table(dev);
1592 if (!opp_table)
1593 return -ENOMEM;
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301594
1595 ret = _opp_add_v1(opp_table, dev, freq, u_volt, true);
Viresh Kumar0ad8c622018-08-02 14:23:09 +05301596 if (ret)
1597 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301598
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301599 return ret;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001600}
1601EXPORT_SYMBOL_GPL(dev_pm_opp_add);
1602
Nishanth Menone1f60b22010-10-13 00:13:10 +02001603/**
Nishanth Menon327854c2014-12-24 11:22:56 -06001604 * _opp_set_availability() - helper to set the availability of an opp
Nishanth Menone1f60b22010-10-13 00:13:10 +02001605 * @dev: device for which we do this operation
1606 * @freq: OPP frequency to modify availability
1607 * @availability_req: availability status requested for this opp
1608 *
Viresh Kumar052c6f12017-01-23 10:11:49 +05301609 * Set the availability of an OPP, opp_{enable,disable} share a common logic
1610 * which is isolated here.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001611 *
Nishanth Menon984f16c2014-12-24 11:22:57 -06001612 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
Stephen Boyde1a2d492015-09-24 12:28:44 -07001613 * copy operation, returns 0 if no modification was done OR modification was
Nishanth Menone1f60b22010-10-13 00:13:10 +02001614 * successful.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001615 */
Nishanth Menon327854c2014-12-24 11:22:56 -06001616static int _opp_set_availability(struct device *dev, unsigned long freq,
1617 bool availability_req)
Nishanth Menone1f60b22010-10-13 00:13:10 +02001618{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301619 struct opp_table *opp_table;
Viresh Kumara7f39872017-01-23 10:11:50 +05301620 struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001621 int r = 0;
1622
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301623 /* Find the opp_table */
1624 opp_table = _find_opp_table(dev);
1625 if (IS_ERR(opp_table)) {
1626 r = PTR_ERR(opp_table);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001627 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
Viresh Kumara7f39872017-01-23 10:11:50 +05301628 return r;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001629 }
1630
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301631 mutex_lock(&opp_table->lock);
1632
Nishanth Menone1f60b22010-10-13 00:13:10 +02001633 /* Do we have the frequency? */
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301634 list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
Nishanth Menone1f60b22010-10-13 00:13:10 +02001635 if (tmp_opp->rate == freq) {
1636 opp = tmp_opp;
1637 break;
1638 }
1639 }
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301640
Nishanth Menone1f60b22010-10-13 00:13:10 +02001641 if (IS_ERR(opp)) {
1642 r = PTR_ERR(opp);
1643 goto unlock;
1644 }
1645
1646 /* Is update really needed? */
1647 if (opp->available == availability_req)
1648 goto unlock;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001649
Viresh Kumara7f39872017-01-23 10:11:50 +05301650 opp->available = availability_req;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001651
Viresh Kumare4d8ae02017-09-21 10:44:36 -07001652 dev_pm_opp_get(opp);
1653 mutex_unlock(&opp_table->lock);
1654
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001655 /* Notify the change of the OPP availability */
1656 if (availability_req)
Viresh Kumar052c6f12017-01-23 10:11:49 +05301657 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE,
Viresh Kumara7f39872017-01-23 10:11:50 +05301658 opp);
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001659 else
Viresh Kumar052c6f12017-01-23 10:11:49 +05301660 blocking_notifier_call_chain(&opp_table->head,
Viresh Kumara7f39872017-01-23 10:11:50 +05301661 OPP_EVENT_DISABLE, opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001662
Viresh Kumare4d8ae02017-09-21 10:44:36 -07001663 dev_pm_opp_put(opp);
1664 goto put_table;
1665
Nishanth Menone1f60b22010-10-13 00:13:10 +02001666unlock:
Viresh Kumar5b650b32017-01-23 10:11:48 +05301667 mutex_unlock(&opp_table->lock);
Viresh Kumare4d8ae02017-09-21 10:44:36 -07001668put_table:
Viresh Kumar5b650b32017-01-23 10:11:48 +05301669 dev_pm_opp_put_opp_table(opp_table);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001670 return r;
1671}
1672
1673/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001674 * dev_pm_opp_enable() - Enable a specific OPP
Nishanth Menone1f60b22010-10-13 00:13:10 +02001675 * @dev: device for which we do this operation
1676 * @freq: OPP frequency to enable
1677 *
1678 * Enables a provided opp. If the operation is valid, this returns 0, else the
1679 * corresponding error value. It is meant to be used for users an OPP available
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001680 * after being temporarily made unavailable with dev_pm_opp_disable.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001681 *
Nishanth Menon984f16c2014-12-24 11:22:57 -06001682 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
Stephen Boyde1a2d492015-09-24 12:28:44 -07001683 * copy operation, returns 0 if no modification was done OR modification was
Nishanth Menon984f16c2014-12-24 11:22:57 -06001684 * successful.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001685 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001686int dev_pm_opp_enable(struct device *dev, unsigned long freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +02001687{
Nishanth Menon327854c2014-12-24 11:22:56 -06001688 return _opp_set_availability(dev, freq, true);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001689}
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001690EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001691
1692/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001693 * dev_pm_opp_disable() - Disable a specific OPP
Nishanth Menone1f60b22010-10-13 00:13:10 +02001694 * @dev: device for which we do this operation
1695 * @freq: OPP frequency to disable
1696 *
1697 * Disables a provided opp. If the operation is valid, this returns
1698 * 0, else the corresponding error value. It is meant to be a temporary
1699 * control by users to make this OPP not available until the circumstances are
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001700 * right to make it available again (with a call to dev_pm_opp_enable).
Nishanth Menone1f60b22010-10-13 00:13:10 +02001701 *
Nishanth Menon984f16c2014-12-24 11:22:57 -06001702 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
Stephen Boyde1a2d492015-09-24 12:28:44 -07001703 * copy operation, returns 0 if no modification was done OR modification was
Nishanth Menon984f16c2014-12-24 11:22:57 -06001704 * successful.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001705 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001706int dev_pm_opp_disable(struct device *dev, unsigned long freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +02001707{
Nishanth Menon327854c2014-12-24 11:22:56 -06001708 return _opp_set_availability(dev, freq, false);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001709}
Nishanth Menon5d4879c2013-09-19 16:03:50 -05001710EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001711
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001712/**
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301713 * dev_pm_opp_register_notifier() - Register OPP notifier for the device
1714 * @dev: Device for which notifier needs to be registered
1715 * @nb: Notifier block to be registered
Nishanth Menon984f16c2014-12-24 11:22:57 -06001716 *
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301717 * Return: 0 on success or a negative error value.
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001718 */
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301719int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb)
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001720{
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301721 struct opp_table *opp_table;
1722 int ret;
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001723
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301724 opp_table = _find_opp_table(dev);
Viresh Kumar5b650b32017-01-23 10:11:48 +05301725 if (IS_ERR(opp_table))
1726 return PTR_ERR(opp_table);
1727
Viresh Kumar052c6f12017-01-23 10:11:49 +05301728 ret = blocking_notifier_chain_register(&opp_table->head, nb);
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301729
Viresh Kumar5b650b32017-01-23 10:11:48 +05301730 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301731
1732 return ret;
MyungJoo Ham03ca3702011-09-30 22:35:12 +02001733}
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301734EXPORT_SYMBOL(dev_pm_opp_register_notifier);
1735
1736/**
1737 * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device
1738 * @dev: Device for which notifier needs to be unregistered
1739 * @nb: Notifier block to be unregistered
1740 *
1741 * Return: 0 on success or a negative error value.
1742 */
1743int dev_pm_opp_unregister_notifier(struct device *dev,
1744 struct notifier_block *nb)
1745{
1746 struct opp_table *opp_table;
1747 int ret;
1748
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301749 opp_table = _find_opp_table(dev);
Viresh Kumar5b650b32017-01-23 10:11:48 +05301750 if (IS_ERR(opp_table))
1751 return PTR_ERR(opp_table);
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301752
Viresh Kumar052c6f12017-01-23 10:11:49 +05301753 ret = blocking_notifier_chain_unregister(&opp_table->head, nb);
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301754
Viresh Kumar5b650b32017-01-23 10:11:48 +05301755 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05301756
1757 return ret;
1758}
1759EXPORT_SYMBOL(dev_pm_opp_unregister_notifier);
Shawn Guob496dfb2012-09-05 01:09:12 +02001760
Sudeep Holla411466c2016-05-03 15:05:04 +01001761/*
1762 * Free OPPs either created using static entries present in DT or even the
1763 * dynamically added entries based on remove_all param.
Shawn Guob496dfb2012-09-05 01:09:12 +02001764 */
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301765void _dev_pm_opp_remove_table(struct opp_table *opp_table, struct device *dev,
1766 bool remove_all)
Viresh Kumar9274c892017-01-02 14:41:00 +05301767{
1768 struct dev_pm_opp *opp, *tmp;
1769
Viresh Kumar3d255692018-08-03 07:05:21 +05301770 /* Protect dev_list */
1771 mutex_lock(&opp_table->lock);
1772
Viresh Kumar9274c892017-01-02 14:41:00 +05301773 /* Find if opp_table manages a single device */
1774 if (list_is_singular(&opp_table->dev_list)) {
1775 /* Free static OPPs */
Viresh Kumard0e8ae62018-09-11 11:14:11 +05301776 _put_opp_list_kref(opp_table);
1777
1778 /* Free dynamic OPPs */
Viresh Kumar9274c892017-01-02 14:41:00 +05301779 list_for_each_entry_safe(opp, tmp, &opp_table->opp_list, node) {
Viresh Kumard0e8ae62018-09-11 11:14:11 +05301780 if (remove_all)
Viresh Kumar70347642017-01-23 10:11:46 +05301781 dev_pm_opp_put(opp);
Viresh Kumar9274c892017-01-02 14:41:00 +05301782 }
Viresh Kumar009acd12017-10-11 12:54:14 +05301783
1784 /*
1785 * The OPP table is getting removed, drop the performance state
1786 * constraints.
1787 */
1788 if (opp_table->genpd_performance_state)
1789 dev_pm_genpd_set_performance_state(dev, 0);
Viresh Kumar9274c892017-01-02 14:41:00 +05301790 } else {
Viresh Kumard0e8ae62018-09-11 11:14:11 +05301791 _put_opp_list_kref(opp_table);
Viresh Kumar9274c892017-01-02 14:41:00 +05301792 _remove_opp_dev(_find_opp_dev(dev, opp_table), opp_table);
1793 }
Viresh Kumar3d255692018-08-03 07:05:21 +05301794
1795 mutex_unlock(&opp_table->lock);
Viresh Kumar9274c892017-01-02 14:41:00 +05301796}
1797
1798void _dev_pm_opp_find_and_remove_table(struct device *dev, bool remove_all)
Viresh Kumar737002b2015-07-29 16:22:58 +05301799{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301800 struct opp_table *opp_table;
Viresh Kumar737002b2015-07-29 16:22:58 +05301801
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301802 /* Check for existing table for 'dev' */
1803 opp_table = _find_opp_table(dev);
1804 if (IS_ERR(opp_table)) {
1805 int error = PTR_ERR(opp_table);
Viresh Kumar737002b2015-07-29 16:22:58 +05301806
1807 if (error != -ENODEV)
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301808 WARN(1, "%s: opp_table: %d\n",
Viresh Kumar737002b2015-07-29 16:22:58 +05301809 IS_ERR_OR_NULL(dev) ?
1810 "Invalid device" : dev_name(dev),
1811 error);
Viresh Kumar5b650b32017-01-23 10:11:48 +05301812 return;
Viresh Kumar737002b2015-07-29 16:22:58 +05301813 }
1814
Viresh Kumar9274c892017-01-02 14:41:00 +05301815 _dev_pm_opp_remove_table(opp_table, dev, remove_all);
Viresh Kumar737002b2015-07-29 16:22:58 +05301816
Viresh Kumar5b650b32017-01-23 10:11:48 +05301817 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar737002b2015-07-29 16:22:58 +05301818}
Viresh Kumar129eec52014-11-27 08:54:06 +05301819
1820/**
Sudeep Holla411466c2016-05-03 15:05:04 +01001821 * dev_pm_opp_remove_table() - Free all OPPs associated with the device
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301822 * @dev: device pointer used to lookup OPP table.
Viresh Kumar129eec52014-11-27 08:54:06 +05301823 *
Sudeep Holla411466c2016-05-03 15:05:04 +01001824 * Free both OPPs created using static entries present in DT and the
1825 * dynamically added entries.
Viresh Kumar129eec52014-11-27 08:54:06 +05301826 */
Sudeep Holla411466c2016-05-03 15:05:04 +01001827void dev_pm_opp_remove_table(struct device *dev)
Viresh Kumar129eec52014-11-27 08:54:06 +05301828{
Viresh Kumar9274c892017-01-02 14:41:00 +05301829 _dev_pm_opp_find_and_remove_table(dev, true);
Viresh Kumar8d4d4e92015-06-12 17:10:38 +05301830}
Sudeep Holla411466c2016-05-03 15:05:04 +01001831EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table);