• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Generic OPP Interface
4  *
5  * Copyright (C) 2009-2010 Texas Instruments Incorporated.
6  *	Nishanth Menon
7  *	Romit Dasgupta
8  *	Kevin Hilman
9  */
10 
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 
13 #include <linux/clk.h>
14 #include <linux/errno.h>
15 #include <linux/err.h>
16 #include <linux/device.h>
17 #include <linux/export.h>
18 #include <linux/pm_domain.h>
19 #include <linux/regulator/consumer.h>
20 #include <linux/slab.h>
21 #include <linux/xarray.h>
22 
23 #include "opp.h"
24 
25 /*
26  * The root of the list of all opp-tables. All opp_table structures branch off
27  * from here, with each opp_table containing the list of opps it supports in
28  * various states of availability.
29  */
30 LIST_HEAD(opp_tables);
31 
32 /* Lock to allow exclusive modification to the device and opp lists */
33 DEFINE_MUTEX(opp_table_lock);
34 /* Flag indicating that opp_tables list is being updated at the moment */
35 static bool opp_tables_busy;
36 
37 /* OPP ID allocator */
38 static DEFINE_XARRAY_ALLOC1(opp_configs);
39 
_find_opp_dev(const struct device * dev,struct opp_table * opp_table)40 static bool _find_opp_dev(const struct device *dev, struct opp_table *opp_table)
41 {
42 	struct opp_device *opp_dev;
43 	bool found = false;
44 
45 	mutex_lock(&opp_table->lock);
46 	list_for_each_entry(opp_dev, &opp_table->dev_list, node)
47 		if (opp_dev->dev == dev) {
48 			found = true;
49 			break;
50 		}
51 
52 	mutex_unlock(&opp_table->lock);
53 	return found;
54 }
55 
_find_opp_table_unlocked(struct device * dev)56 static struct opp_table *_find_opp_table_unlocked(struct device *dev)
57 {
58 	struct opp_table *opp_table;
59 
60 	list_for_each_entry(opp_table, &opp_tables, node) {
61 		if (_find_opp_dev(dev, opp_table)) {
62 			_get_opp_table_kref(opp_table);
63 			return opp_table;
64 		}
65 	}
66 
67 	return ERR_PTR(-ENODEV);
68 }
69 
70 /**
71  * _find_opp_table() - find opp_table struct using device pointer
72  * @dev:	device pointer used to lookup OPP table
73  *
74  * Search OPP table for one containing matching device.
75  *
76  * Return: pointer to 'struct opp_table' if found, otherwise -ENODEV or
77  * -EINVAL based on type of error.
78  *
79  * The callers must call dev_pm_opp_put_opp_table() after the table is used.
80  */
_find_opp_table(struct device * dev)81 struct opp_table *_find_opp_table(struct device *dev)
82 {
83 	struct opp_table *opp_table;
84 
85 	if (IS_ERR_OR_NULL(dev)) {
86 		pr_err("%s: Invalid parameters\n", __func__);
87 		return ERR_PTR(-EINVAL);
88 	}
89 
90 	mutex_lock(&opp_table_lock);
91 	opp_table = _find_opp_table_unlocked(dev);
92 	mutex_unlock(&opp_table_lock);
93 
94 	return opp_table;
95 }
96 
97 /*
98  * Returns true if multiple clocks aren't there, else returns false with WARN.
99  *
100  * We don't force clk_count == 1 here as there are users who don't have a clock
101  * representation in the OPP table and manage the clock configuration themselves
102  * in an platform specific way.
103  */
assert_single_clk(struct opp_table * opp_table)104 static bool assert_single_clk(struct opp_table *opp_table)
105 {
106 	return !WARN_ON(opp_table->clk_count > 1);
107 }
108 
109 /**
110  * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an opp
111  * @opp:	opp for which voltage has to be returned for
112  *
113  * Return: voltage in micro volt corresponding to the opp, else
114  * return 0
115  *
116  * This is useful only for devices with single power supply.
117  */
dev_pm_opp_get_voltage(struct dev_pm_opp * opp)118 unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp)
119 {
120 	if (IS_ERR_OR_NULL(opp)) {
121 		pr_err("%s: Invalid parameters\n", __func__);
122 		return 0;
123 	}
124 
125 	return opp->supplies[0].u_volt;
126 }
127 EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage);
128 
129 /**
130  * dev_pm_opp_get_supplies() - Gets the supply information corresponding to an opp
131  * @opp:	opp for which voltage has to be returned for
132  * @supplies:	Placeholder for copying the supply information.
133  *
134  * Return: negative error number on failure, 0 otherwise on success after
135  * setting @supplies.
136  *
137  * This can be used for devices with any number of power supplies. The caller
138  * must ensure the @supplies array must contain space for each regulator.
139  */
dev_pm_opp_get_supplies(struct dev_pm_opp * opp,struct dev_pm_opp_supply * supplies)140 int dev_pm_opp_get_supplies(struct dev_pm_opp *opp,
141 			    struct dev_pm_opp_supply *supplies)
142 {
143 	if (IS_ERR_OR_NULL(opp) || !supplies) {
144 		pr_err("%s: Invalid parameters\n", __func__);
145 		return -EINVAL;
146 	}
147 
148 	memcpy(supplies, opp->supplies,
149 	       sizeof(*supplies) * opp->opp_table->regulator_count);
150 	return 0;
151 }
152 EXPORT_SYMBOL_GPL(dev_pm_opp_get_supplies);
153 
154 /**
155  * dev_pm_opp_get_power() - Gets the power corresponding to an opp
156  * @opp:	opp for which power has to be returned for
157  *
158  * Return: power in micro watt corresponding to the opp, else
159  * return 0
160  *
161  * This is useful only for devices with single power supply.
162  */
dev_pm_opp_get_power(struct dev_pm_opp * opp)163 unsigned long dev_pm_opp_get_power(struct dev_pm_opp *opp)
164 {
165 	unsigned long opp_power = 0;
166 	int i;
167 
168 	if (IS_ERR_OR_NULL(opp)) {
169 		pr_err("%s: Invalid parameters\n", __func__);
170 		return 0;
171 	}
172 	for (i = 0; i < opp->opp_table->regulator_count; i++)
173 		opp_power += opp->supplies[i].u_watt;
174 
175 	return opp_power;
176 }
177 EXPORT_SYMBOL_GPL(dev_pm_opp_get_power);
178 
179 /**
180  * dev_pm_opp_get_freq_indexed() - Gets the frequency corresponding to an
181  *				   available opp with specified index
182  * @opp: opp for which frequency has to be returned for
183  * @index: index of the frequency within the required opp
184  *
185  * Return: frequency in hertz corresponding to the opp with specified index,
186  * else return 0
187  */
dev_pm_opp_get_freq_indexed(struct dev_pm_opp * opp,u32 index)188 unsigned long dev_pm_opp_get_freq_indexed(struct dev_pm_opp *opp, u32 index)
189 {
190 	if (IS_ERR_OR_NULL(opp) || index >= opp->opp_table->clk_count) {
191 		pr_err("%s: Invalid parameters\n", __func__);
192 		return 0;
193 	}
194 
195 	return opp->rates[index];
196 }
197 EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq_indexed);
198 
199 /**
200  * dev_pm_opp_get_level() - Gets the level corresponding to an available opp
201  * @opp:	opp for which level value has to be returned for
202  *
203  * Return: level read from device tree corresponding to the opp, else
204  * return 0.
205  */
dev_pm_opp_get_level(struct dev_pm_opp * opp)206 unsigned int dev_pm_opp_get_level(struct dev_pm_opp *opp)
207 {
208 	if (IS_ERR_OR_NULL(opp) || !opp->available) {
209 		pr_err("%s: Invalid parameters\n", __func__);
210 		return 0;
211 	}
212 
213 	return opp->level;
214 }
215 EXPORT_SYMBOL_GPL(dev_pm_opp_get_level);
216 
217 /**
218  * dev_pm_opp_get_required_pstate() - Gets the required performance state
219  *                                    corresponding to an available opp
220  * @opp:	opp for which performance state has to be returned for
221  * @index:	index of the required opp
222  *
223  * Return: performance state read from device tree corresponding to the
224  * required opp, else return 0.
225  */
dev_pm_opp_get_required_pstate(struct dev_pm_opp * opp,unsigned int index)226 unsigned int dev_pm_opp_get_required_pstate(struct dev_pm_opp *opp,
227 					    unsigned int index)
228 {
229 	if (IS_ERR_OR_NULL(opp) || !opp->available ||
230 	    index >= opp->opp_table->required_opp_count) {
231 		pr_err("%s: Invalid parameters\n", __func__);
232 		return 0;
233 	}
234 
235 	/* required-opps not fully initialized yet */
236 	if (lazy_linking_pending(opp->opp_table))
237 		return 0;
238 
239 	/* The required OPP table must belong to a genpd */
240 	if (unlikely(!opp->opp_table->required_opp_tables[index]->is_genpd)) {
241 		pr_err("%s: Performance state is only valid for genpds.\n", __func__);
242 		return 0;
243 	}
244 
245 	return opp->required_opps[index]->level;
246 }
247 EXPORT_SYMBOL_GPL(dev_pm_opp_get_required_pstate);
248 
249 /**
250  * dev_pm_opp_is_turbo() - Returns if opp is turbo OPP or not
251  * @opp: opp for which turbo mode is being verified
252  *
253  * Turbo OPPs are not for normal use, and can be enabled (under certain
254  * conditions) for short duration of times to finish high throughput work
255  * quickly. Running on them for longer times may overheat the chip.
256  *
257  * Return: true if opp is turbo opp, else false.
258  */
dev_pm_opp_is_turbo(struct dev_pm_opp * opp)259 bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp)
260 {
261 	if (IS_ERR_OR_NULL(opp) || !opp->available) {
262 		pr_err("%s: Invalid parameters\n", __func__);
263 		return false;
264 	}
265 
266 	return opp->turbo;
267 }
268 EXPORT_SYMBOL_GPL(dev_pm_opp_is_turbo);
269 
270 /**
271  * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds
272  * @dev:	device for which we do this operation
273  *
274  * Return: This function returns the max clock latency in nanoseconds.
275  */
dev_pm_opp_get_max_clock_latency(struct device * dev)276 unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev)
277 {
278 	struct opp_table *opp_table;
279 	unsigned long clock_latency_ns;
280 
281 	opp_table = _find_opp_table(dev);
282 	if (IS_ERR(opp_table))
283 		return 0;
284 
285 	clock_latency_ns = opp_table->clock_latency_ns_max;
286 
287 	dev_pm_opp_put_opp_table(opp_table);
288 
289 	return clock_latency_ns;
290 }
291 EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency);
292 
293 /**
294  * dev_pm_opp_get_max_volt_latency() - Get max voltage latency in nanoseconds
295  * @dev: device for which we do this operation
296  *
297  * Return: This function returns the max voltage latency in nanoseconds.
298  */
dev_pm_opp_get_max_volt_latency(struct device * dev)299 unsigned long dev_pm_opp_get_max_volt_latency(struct device *dev)
300 {
301 	struct opp_table *opp_table;
302 	struct dev_pm_opp *opp;
303 	struct regulator *reg;
304 	unsigned long latency_ns = 0;
305 	int ret, i, count;
306 	struct {
307 		unsigned long min;
308 		unsigned long max;
309 	} *uV;
310 
311 	opp_table = _find_opp_table(dev);
312 	if (IS_ERR(opp_table))
313 		return 0;
314 
315 	/* Regulator may not be required for the device */
316 	if (!opp_table->regulators)
317 		goto put_opp_table;
318 
319 	count = opp_table->regulator_count;
320 
321 	uV = kmalloc_array(count, sizeof(*uV), GFP_KERNEL);
322 	if (!uV)
323 		goto put_opp_table;
324 
325 	mutex_lock(&opp_table->lock);
326 
327 	for (i = 0; i < count; i++) {
328 		uV[i].min = ~0;
329 		uV[i].max = 0;
330 
331 		list_for_each_entry(opp, &opp_table->opp_list, node) {
332 			if (!opp->available)
333 				continue;
334 
335 			if (opp->supplies[i].u_volt_min < uV[i].min)
336 				uV[i].min = opp->supplies[i].u_volt_min;
337 			if (opp->supplies[i].u_volt_max > uV[i].max)
338 				uV[i].max = opp->supplies[i].u_volt_max;
339 		}
340 	}
341 
342 	mutex_unlock(&opp_table->lock);
343 
344 	/*
345 	 * The caller needs to ensure that opp_table (and hence the regulator)
346 	 * isn't freed, while we are executing this routine.
347 	 */
348 	for (i = 0; i < count; i++) {
349 		reg = opp_table->regulators[i];
350 		ret = regulator_set_voltage_time(reg, uV[i].min, uV[i].max);
351 		if (ret > 0)
352 			latency_ns += ret * 1000;
353 	}
354 
355 	kfree(uV);
356 put_opp_table:
357 	dev_pm_opp_put_opp_table(opp_table);
358 
359 	return latency_ns;
360 }
361 EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_volt_latency);
362 
363 /**
364  * dev_pm_opp_get_max_transition_latency() - Get max transition latency in
365  *					     nanoseconds
366  * @dev: device for which we do this operation
367  *
368  * Return: This function returns the max transition latency, in nanoseconds, to
369  * switch from one OPP to other.
370  */
dev_pm_opp_get_max_transition_latency(struct device * dev)371 unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev)
372 {
373 	return dev_pm_opp_get_max_volt_latency(dev) +
374 		dev_pm_opp_get_max_clock_latency(dev);
375 }
376 EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_transition_latency);
377 
378 /**
379  * dev_pm_opp_get_suspend_opp_freq() - Get frequency of suspend opp in Hz
380  * @dev:	device for which we do this operation
381  *
382  * Return: This function returns the frequency of the OPP marked as suspend_opp
383  * if one is available, else returns 0;
384  */
dev_pm_opp_get_suspend_opp_freq(struct device * dev)385 unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev)
386 {
387 	struct opp_table *opp_table;
388 	unsigned long freq = 0;
389 
390 	opp_table = _find_opp_table(dev);
391 	if (IS_ERR(opp_table))
392 		return 0;
393 
394 	if (opp_table->suspend_opp && opp_table->suspend_opp->available)
395 		freq = dev_pm_opp_get_freq(opp_table->suspend_opp);
396 
397 	dev_pm_opp_put_opp_table(opp_table);
398 
399 	return freq;
400 }
401 EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp_freq);
402 
_get_opp_count(struct opp_table * opp_table)403 int _get_opp_count(struct opp_table *opp_table)
404 {
405 	struct dev_pm_opp *opp;
406 	int count = 0;
407 
408 	mutex_lock(&opp_table->lock);
409 
410 	list_for_each_entry(opp, &opp_table->opp_list, node) {
411 		if (opp->available)
412 			count++;
413 	}
414 
415 	mutex_unlock(&opp_table->lock);
416 
417 	return count;
418 }
419 
420 /**
421  * dev_pm_opp_get_opp_count() - Get number of opps available in the opp table
422  * @dev:	device for which we do this operation
423  *
424  * Return: This function returns the number of available opps if there are any,
425  * else returns 0 if none or the corresponding error value.
426  */
dev_pm_opp_get_opp_count(struct device * dev)427 int dev_pm_opp_get_opp_count(struct device *dev)
428 {
429 	struct opp_table *opp_table;
430 	int count;
431 
432 	opp_table = _find_opp_table(dev);
433 	if (IS_ERR(opp_table)) {
434 		count = PTR_ERR(opp_table);
435 		dev_dbg(dev, "%s: OPP table not found (%d)\n",
436 			__func__, count);
437 		return count;
438 	}
439 
440 	count = _get_opp_count(opp_table);
441 	dev_pm_opp_put_opp_table(opp_table);
442 
443 	return count;
444 }
445 EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
446 
447 /* Helpers to read keys */
_read_freq(struct dev_pm_opp * opp,int index)448 static unsigned long _read_freq(struct dev_pm_opp *opp, int index)
449 {
450 	return opp->rates[index];
451 }
452 
_read_level(struct dev_pm_opp * opp,int index)453 static unsigned long _read_level(struct dev_pm_opp *opp, int index)
454 {
455 	return opp->level;
456 }
457 
_read_bw(struct dev_pm_opp * opp,int index)458 static unsigned long _read_bw(struct dev_pm_opp *opp, int index)
459 {
460 	return opp->bandwidth[index].peak;
461 }
462 
463 /* Generic comparison helpers */
_compare_exact(struct dev_pm_opp ** opp,struct dev_pm_opp * temp_opp,unsigned long opp_key,unsigned long key)464 static bool _compare_exact(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp,
465 			   unsigned long opp_key, unsigned long key)
466 {
467 	if (opp_key == key) {
468 		*opp = temp_opp;
469 		return true;
470 	}
471 
472 	return false;
473 }
474 
_compare_ceil(struct dev_pm_opp ** opp,struct dev_pm_opp * temp_opp,unsigned long opp_key,unsigned long key)475 static bool _compare_ceil(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp,
476 			  unsigned long opp_key, unsigned long key)
477 {
478 	if (opp_key >= key) {
479 		*opp = temp_opp;
480 		return true;
481 	}
482 
483 	return false;
484 }
485 
_compare_floor(struct dev_pm_opp ** opp,struct dev_pm_opp * temp_opp,unsigned long opp_key,unsigned long key)486 static bool _compare_floor(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp,
487 			   unsigned long opp_key, unsigned long key)
488 {
489 	if (opp_key > key)
490 		return true;
491 
492 	*opp = temp_opp;
493 	return false;
494 }
495 
496 /* Generic key finding helpers */
_opp_table_find_key(struct opp_table * opp_table,unsigned long * key,int index,bool available,unsigned long (* read)(struct dev_pm_opp * opp,int index),bool (* compare)(struct dev_pm_opp ** opp,struct dev_pm_opp * temp_opp,unsigned long opp_key,unsigned long key),bool (* assert)(struct opp_table * opp_table))497 static struct dev_pm_opp *_opp_table_find_key(struct opp_table *opp_table,
498 		unsigned long *key, int index, bool available,
499 		unsigned long (*read)(struct dev_pm_opp *opp, int index),
500 		bool (*compare)(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp,
501 				unsigned long opp_key, unsigned long key),
502 		bool (*assert)(struct opp_table *opp_table))
503 {
504 	struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
505 
506 	/* Assert that the requirement is met */
507 	if (assert && !assert(opp_table))
508 		return ERR_PTR(-EINVAL);
509 
510 	mutex_lock(&opp_table->lock);
511 
512 	list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
513 		if (temp_opp->available == available) {
514 			if (compare(&opp, temp_opp, read(temp_opp, index), *key))
515 				break;
516 		}
517 	}
518 
519 	/* Increment the reference count of OPP */
520 	if (!IS_ERR(opp)) {
521 		*key = read(opp, index);
522 		dev_pm_opp_get(opp);
523 	}
524 
525 	mutex_unlock(&opp_table->lock);
526 
527 	return opp;
528 }
529 
530 static struct dev_pm_opp *
_find_key(struct device * dev,unsigned long * key,int index,bool available,unsigned long (* read)(struct dev_pm_opp * opp,int index),bool (* compare)(struct dev_pm_opp ** opp,struct dev_pm_opp * temp_opp,unsigned long opp_key,unsigned long key),bool (* assert)(struct opp_table * opp_table))531 _find_key(struct device *dev, unsigned long *key, int index, bool available,
532 	  unsigned long (*read)(struct dev_pm_opp *opp, int index),
533 	  bool (*compare)(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp,
534 			  unsigned long opp_key, unsigned long key),
535 	  bool (*assert)(struct opp_table *opp_table))
536 {
537 	struct opp_table *opp_table;
538 	struct dev_pm_opp *opp;
539 
540 	opp_table = _find_opp_table(dev);
541 	if (IS_ERR(opp_table)) {
542 		dev_err(dev, "%s: OPP table not found (%ld)\n", __func__,
543 			PTR_ERR(opp_table));
544 		return ERR_CAST(opp_table);
545 	}
546 
547 	opp = _opp_table_find_key(opp_table, key, index, available, read,
548 				  compare, assert);
549 
550 	dev_pm_opp_put_opp_table(opp_table);
551 
552 	return opp;
553 }
554 
_find_key_exact(struct device * dev,unsigned long key,int index,bool available,unsigned long (* read)(struct dev_pm_opp * opp,int index),bool (* assert)(struct opp_table * opp_table))555 static struct dev_pm_opp *_find_key_exact(struct device *dev,
556 		unsigned long key, int index, bool available,
557 		unsigned long (*read)(struct dev_pm_opp *opp, int index),
558 		bool (*assert)(struct opp_table *opp_table))
559 {
560 	/*
561 	 * The value of key will be updated here, but will be ignored as the
562 	 * caller doesn't need it.
563 	 */
564 	return _find_key(dev, &key, index, available, read, _compare_exact,
565 			 assert);
566 }
567 
_opp_table_find_key_ceil(struct opp_table * opp_table,unsigned long * key,int index,bool available,unsigned long (* read)(struct dev_pm_opp * opp,int index),bool (* assert)(struct opp_table * opp_table))568 static struct dev_pm_opp *_opp_table_find_key_ceil(struct opp_table *opp_table,
569 		unsigned long *key, int index, bool available,
570 		unsigned long (*read)(struct dev_pm_opp *opp, int index),
571 		bool (*assert)(struct opp_table *opp_table))
572 {
573 	return _opp_table_find_key(opp_table, key, index, available, read,
574 				   _compare_ceil, assert);
575 }
576 
_find_key_ceil(struct device * dev,unsigned long * key,int index,bool available,unsigned long (* read)(struct dev_pm_opp * opp,int index),bool (* assert)(struct opp_table * opp_table))577 static struct dev_pm_opp *_find_key_ceil(struct device *dev, unsigned long *key,
578 		int index, bool available,
579 		unsigned long (*read)(struct dev_pm_opp *opp, int index),
580 		bool (*assert)(struct opp_table *opp_table))
581 {
582 	return _find_key(dev, key, index, available, read, _compare_ceil,
583 			 assert);
584 }
585 
_find_key_floor(struct device * dev,unsigned long * key,int index,bool available,unsigned long (* read)(struct dev_pm_opp * opp,int index),bool (* assert)(struct opp_table * opp_table))586 static struct dev_pm_opp *_find_key_floor(struct device *dev,
587 		unsigned long *key, int index, bool available,
588 		unsigned long (*read)(struct dev_pm_opp *opp, int index),
589 		bool (*assert)(struct opp_table *opp_table))
590 {
591 	return _find_key(dev, key, index, available, read, _compare_floor,
592 			 assert);
593 }
594 
595 /**
596  * dev_pm_opp_find_freq_exact() - search for an exact frequency
597  * @dev:		device for which we do this operation
598  * @freq:		frequency to search for
599  * @available:		true/false - match for available opp
600  *
601  * Return: Searches for exact match in the opp table and returns pointer to the
602  * matching opp if found, else returns ERR_PTR in case of error and should
603  * be handled using IS_ERR. Error return values can be:
604  * EINVAL:	for bad pointer
605  * ERANGE:	no match found for search
606  * ENODEV:	if device not found in list of registered devices
607  *
608  * Note: available is a modifier for the search. if available=true, then the
609  * match is for exact matching frequency and is available in the stored OPP
610  * table. if false, the match is for exact frequency which is not available.
611  *
612  * This provides a mechanism to enable an opp which is not available currently
613  * or the opposite as well.
614  *
615  * The callers are required to call dev_pm_opp_put() for the returned OPP after
616  * use.
617  */
dev_pm_opp_find_freq_exact(struct device * dev,unsigned long freq,bool available)618 struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
619 		unsigned long freq, bool available)
620 {
621 	return _find_key_exact(dev, freq, 0, available, _read_freq,
622 			       assert_single_clk);
623 }
624 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
625 
626 /**
627  * dev_pm_opp_find_freq_exact_indexed() - Search for an exact freq for the
628  *					 clock corresponding to the index
629  * @dev:	Device for which we do this operation
630  * @freq:	frequency to search for
631  * @index:	Clock index
632  * @available:	true/false - match for available opp
633  *
634  * Search for the matching exact OPP for the clock corresponding to the
635  * specified index from a starting freq for a device.
636  *
637  * Return: matching *opp , else returns ERR_PTR in case of error and should be
638  * handled using IS_ERR. Error return values can be:
639  * EINVAL:	for bad pointer
640  * ERANGE:	no match found for search
641  * ENODEV:	if device not found in list of registered devices
642  *
643  * The callers are required to call dev_pm_opp_put() for the returned OPP after
644  * use.
645  */
646 struct dev_pm_opp *
dev_pm_opp_find_freq_exact_indexed(struct device * dev,unsigned long freq,u32 index,bool available)647 dev_pm_opp_find_freq_exact_indexed(struct device *dev, unsigned long freq,
648 				   u32 index, bool available)
649 {
650 	return _find_key_exact(dev, freq, index, available, _read_freq, NULL);
651 }
652 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact_indexed);
653 
_find_freq_ceil(struct opp_table * opp_table,unsigned long * freq)654 static noinline struct dev_pm_opp *_find_freq_ceil(struct opp_table *opp_table,
655 						   unsigned long *freq)
656 {
657 	return _opp_table_find_key_ceil(opp_table, freq, 0, true, _read_freq,
658 					assert_single_clk);
659 }
660 
661 /**
662  * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
663  * @dev:	device for which we do this operation
664  * @freq:	Start frequency
665  *
666  * Search for the matching ceil *available* OPP from a starting freq
667  * for a device.
668  *
669  * Return: matching *opp and refreshes *freq accordingly, else returns
670  * ERR_PTR in case of error and should be handled using IS_ERR. Error return
671  * values can be:
672  * EINVAL:	for bad pointer
673  * ERANGE:	no match found for search
674  * ENODEV:	if device not found in list of registered devices
675  *
676  * The callers are required to call dev_pm_opp_put() for the returned OPP after
677  * use.
678  */
dev_pm_opp_find_freq_ceil(struct device * dev,unsigned long * freq)679 struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
680 					     unsigned long *freq)
681 {
682 	return _find_key_ceil(dev, freq, 0, true, _read_freq, assert_single_clk);
683 }
684 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
685 
686 /**
687  * dev_pm_opp_find_freq_ceil_indexed() - Search for a rounded ceil freq for the
688  *					 clock corresponding to the index
689  * @dev:	Device for which we do this operation
690  * @freq:	Start frequency
691  * @index:	Clock index
692  *
693  * Search for the matching ceil *available* OPP for the clock corresponding to
694  * the specified index from a starting freq for a device.
695  *
696  * Return: matching *opp and refreshes *freq accordingly, else returns
697  * ERR_PTR in case of error and should be handled using IS_ERR. Error return
698  * values can be:
699  * EINVAL:	for bad pointer
700  * ERANGE:	no match found for search
701  * ENODEV:	if device not found in list of registered devices
702  *
703  * The callers are required to call dev_pm_opp_put() for the returned OPP after
704  * use.
705  */
706 struct dev_pm_opp *
dev_pm_opp_find_freq_ceil_indexed(struct device * dev,unsigned long * freq,u32 index)707 dev_pm_opp_find_freq_ceil_indexed(struct device *dev, unsigned long *freq,
708 				  u32 index)
709 {
710 	return _find_key_ceil(dev, freq, index, true, _read_freq, NULL);
711 }
712 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil_indexed);
713 
714 /**
715  * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
716  * @dev:	device for which we do this operation
717  * @freq:	Start frequency
718  *
719  * Search for the matching floor *available* OPP from a starting freq
720  * for a device.
721  *
722  * Return: matching *opp and refreshes *freq accordingly, else returns
723  * ERR_PTR in case of error and should be handled using IS_ERR. Error return
724  * values can be:
725  * EINVAL:	for bad pointer
726  * ERANGE:	no match found for search
727  * ENODEV:	if device not found in list of registered devices
728  *
729  * The callers are required to call dev_pm_opp_put() for the returned OPP after
730  * use.
731  */
dev_pm_opp_find_freq_floor(struct device * dev,unsigned long * freq)732 struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
733 					      unsigned long *freq)
734 {
735 	return _find_key_floor(dev, freq, 0, true, _read_freq, assert_single_clk);
736 }
737 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
738 
739 /**
740  * dev_pm_opp_find_freq_floor_indexed() - Search for a rounded floor freq for the
741  *					  clock corresponding to the index
742  * @dev:	Device for which we do this operation
743  * @freq:	Start frequency
744  * @index:	Clock index
745  *
746  * Search for the matching floor *available* OPP for the clock corresponding to
747  * the specified index from a starting freq for a device.
748  *
749  * Return: matching *opp and refreshes *freq accordingly, else returns
750  * ERR_PTR in case of error and should be handled using IS_ERR. Error return
751  * values can be:
752  * EINVAL:	for bad pointer
753  * ERANGE:	no match found for search
754  * ENODEV:	if device not found in list of registered devices
755  *
756  * The callers are required to call dev_pm_opp_put() for the returned OPP after
757  * use.
758  */
759 struct dev_pm_opp *
dev_pm_opp_find_freq_floor_indexed(struct device * dev,unsigned long * freq,u32 index)760 dev_pm_opp_find_freq_floor_indexed(struct device *dev, unsigned long *freq,
761 				   u32 index)
762 {
763 	return _find_key_floor(dev, freq, index, true, _read_freq, NULL);
764 }
765 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor_indexed);
766 
767 /**
768  * dev_pm_opp_find_level_exact() - search for an exact level
769  * @dev:		device for which we do this operation
770  * @level:		level to search for
771  *
772  * Return: Searches for exact match in the opp table and returns pointer to the
773  * matching opp if found, else returns ERR_PTR in case of error and should
774  * be handled using IS_ERR. Error return values can be:
775  * EINVAL:	for bad pointer
776  * ERANGE:	no match found for search
777  * ENODEV:	if device not found in list of registered devices
778  *
779  * The callers are required to call dev_pm_opp_put() for the returned OPP after
780  * use.
781  */
dev_pm_opp_find_level_exact(struct device * dev,unsigned int level)782 struct dev_pm_opp *dev_pm_opp_find_level_exact(struct device *dev,
783 					       unsigned int level)
784 {
785 	return _find_key_exact(dev, level, 0, true, _read_level, NULL);
786 }
787 EXPORT_SYMBOL_GPL(dev_pm_opp_find_level_exact);
788 
789 /**
790  * dev_pm_opp_find_level_ceil() - search for an rounded up level
791  * @dev:		device for which we do this operation
792  * @level:		level to search for
793  *
794  * Return: Searches for rounded up match in the opp table and returns pointer
795  * to the  matching opp if found, else returns ERR_PTR in case of error and
796  * should be handled using IS_ERR. Error return values can be:
797  * EINVAL:	for bad pointer
798  * ERANGE:	no match found for search
799  * ENODEV:	if device not found in list of registered devices
800  *
801  * The callers are required to call dev_pm_opp_put() for the returned OPP after
802  * use.
803  */
dev_pm_opp_find_level_ceil(struct device * dev,unsigned int * level)804 struct dev_pm_opp *dev_pm_opp_find_level_ceil(struct device *dev,
805 					      unsigned int *level)
806 {
807 	unsigned long temp = *level;
808 	struct dev_pm_opp *opp;
809 
810 	opp = _find_key_ceil(dev, &temp, 0, true, _read_level, NULL);
811 	*level = temp;
812 	return opp;
813 }
814 EXPORT_SYMBOL_GPL(dev_pm_opp_find_level_ceil);
815 
816 /**
817  * dev_pm_opp_find_bw_ceil() - Search for a rounded ceil bandwidth
818  * @dev:	device for which we do this operation
819  * @bw:	start bandwidth
820  * @index:	which bandwidth to compare, in case of OPPs with several values
821  *
822  * Search for the matching floor *available* OPP from a starting bandwidth
823  * for a device.
824  *
825  * Return: matching *opp and refreshes *bw accordingly, else returns
826  * ERR_PTR in case of error and should be handled using IS_ERR. Error return
827  * values can be:
828  * EINVAL:	for bad pointer
829  * ERANGE:	no match found for search
830  * ENODEV:	if device not found in list of registered devices
831  *
832  * The callers are required to call dev_pm_opp_put() for the returned OPP after
833  * use.
834  */
dev_pm_opp_find_bw_ceil(struct device * dev,unsigned int * bw,int index)835 struct dev_pm_opp *dev_pm_opp_find_bw_ceil(struct device *dev, unsigned int *bw,
836 					   int index)
837 {
838 	unsigned long temp = *bw;
839 	struct dev_pm_opp *opp;
840 
841 	opp = _find_key_ceil(dev, &temp, index, true, _read_bw, NULL);
842 	*bw = temp;
843 	return opp;
844 }
845 EXPORT_SYMBOL_GPL(dev_pm_opp_find_bw_ceil);
846 
847 /**
848  * dev_pm_opp_find_bw_floor() - Search for a rounded floor bandwidth
849  * @dev:	device for which we do this operation
850  * @bw:	start bandwidth
851  * @index:	which bandwidth to compare, in case of OPPs with several values
852  *
853  * Search for the matching floor *available* OPP from a starting bandwidth
854  * for a device.
855  *
856  * Return: matching *opp and refreshes *bw accordingly, else returns
857  * ERR_PTR in case of error and should be handled using IS_ERR. Error return
858  * values can be:
859  * EINVAL:	for bad pointer
860  * ERANGE:	no match found for search
861  * ENODEV:	if device not found in list of registered devices
862  *
863  * The callers are required to call dev_pm_opp_put() for the returned OPP after
864  * use.
865  */
dev_pm_opp_find_bw_floor(struct device * dev,unsigned int * bw,int index)866 struct dev_pm_opp *dev_pm_opp_find_bw_floor(struct device *dev,
867 					    unsigned int *bw, int index)
868 {
869 	unsigned long temp = *bw;
870 	struct dev_pm_opp *opp;
871 
872 	opp = _find_key_floor(dev, &temp, index, true, _read_bw, NULL);
873 	*bw = temp;
874 	return opp;
875 }
876 EXPORT_SYMBOL_GPL(dev_pm_opp_find_bw_floor);
877 
_set_opp_voltage(struct device * dev,struct regulator * reg,struct dev_pm_opp_supply * supply)878 static int _set_opp_voltage(struct device *dev, struct regulator *reg,
879 			    struct dev_pm_opp_supply *supply)
880 {
881 	int ret;
882 
883 	/* Regulator not available for device */
884 	if (IS_ERR(reg)) {
885 		dev_dbg(dev, "%s: regulator not available: %ld\n", __func__,
886 			PTR_ERR(reg));
887 		return 0;
888 	}
889 
890 	dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__,
891 		supply->u_volt_min, supply->u_volt, supply->u_volt_max);
892 
893 	ret = regulator_set_voltage_triplet(reg, supply->u_volt_min,
894 					    supply->u_volt, supply->u_volt_max);
895 	if (ret)
896 		dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n",
897 			__func__, supply->u_volt_min, supply->u_volt,
898 			supply->u_volt_max, ret);
899 
900 	return ret;
901 }
902 
903 static int
_opp_config_clk_single(struct device * dev,struct opp_table * opp_table,struct dev_pm_opp * opp,void * data,bool scaling_down)904 _opp_config_clk_single(struct device *dev, struct opp_table *opp_table,
905 		       struct dev_pm_opp *opp, void *data, bool scaling_down)
906 {
907 	unsigned long *target = data;
908 	unsigned long freq;
909 	int ret;
910 
911 	/* One of target and opp must be available */
912 	if (target) {
913 		freq = *target;
914 	} else if (opp) {
915 		freq = opp->rates[0];
916 	} else {
917 		WARN_ON(1);
918 		return -EINVAL;
919 	}
920 
921 	ret = clk_set_rate(opp_table->clk, freq);
922 	if (ret) {
923 		dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
924 			ret);
925 	} else {
926 		opp_table->rate_clk_single = freq;
927 	}
928 
929 	return ret;
930 }
931 
932 /*
933  * Simple implementation for configuring multiple clocks. Configure clocks in
934  * the order in which they are present in the array while scaling up.
935  */
dev_pm_opp_config_clks_simple(struct device * dev,struct opp_table * opp_table,struct dev_pm_opp * opp,void * data,bool scaling_down)936 int dev_pm_opp_config_clks_simple(struct device *dev,
937 		struct opp_table *opp_table, struct dev_pm_opp *opp, void *data,
938 		bool scaling_down)
939 {
940 	int ret, i;
941 
942 	if (scaling_down) {
943 		for (i = opp_table->clk_count - 1; i >= 0; i--) {
944 			ret = clk_set_rate(opp_table->clks[i], opp->rates[i]);
945 			if (ret) {
946 				dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
947 					ret);
948 				return ret;
949 			}
950 		}
951 	} else {
952 		for (i = 0; i < opp_table->clk_count; i++) {
953 			ret = clk_set_rate(opp_table->clks[i], opp->rates[i]);
954 			if (ret) {
955 				dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
956 					ret);
957 				return ret;
958 			}
959 		}
960 	}
961 
962 	return 0;
963 }
964 EXPORT_SYMBOL_GPL(dev_pm_opp_config_clks_simple);
965 
_opp_config_regulator_single(struct device * dev,struct dev_pm_opp * old_opp,struct dev_pm_opp * new_opp,struct regulator ** regulators,unsigned int count)966 static int _opp_config_regulator_single(struct device *dev,
967 			struct dev_pm_opp *old_opp, struct dev_pm_opp *new_opp,
968 			struct regulator **regulators, unsigned int count)
969 {
970 	struct regulator *reg = regulators[0];
971 	int ret;
972 
973 	/* This function only supports single regulator per device */
974 	if (WARN_ON(count > 1)) {
975 		dev_err(dev, "multiple regulators are not supported\n");
976 		return -EINVAL;
977 	}
978 
979 	ret = _set_opp_voltage(dev, reg, new_opp->supplies);
980 	if (ret)
981 		return ret;
982 
983 	/*
984 	 * Enable the regulator after setting its voltages, otherwise it breaks
985 	 * some boot-enabled regulators.
986 	 */
987 	if (unlikely(!new_opp->opp_table->enabled)) {
988 		ret = regulator_enable(reg);
989 		if (ret < 0)
990 			dev_warn(dev, "Failed to enable regulator: %d", ret);
991 	}
992 
993 	return 0;
994 }
995 
_set_opp_bw(const struct opp_table * opp_table,struct dev_pm_opp * opp,struct device * dev)996 static int _set_opp_bw(const struct opp_table *opp_table,
997 		       struct dev_pm_opp *opp, struct device *dev)
998 {
999 	u32 avg, peak;
1000 	int i, ret;
1001 
1002 	if (!opp_table->paths)
1003 		return 0;
1004 
1005 	for (i = 0; i < opp_table->path_count; i++) {
1006 		if (!opp) {
1007 			avg = 0;
1008 			peak = 0;
1009 		} else {
1010 			avg = opp->bandwidth[i].avg;
1011 			peak = opp->bandwidth[i].peak;
1012 		}
1013 		ret = icc_set_bw(opp_table->paths[i], avg, peak);
1014 		if (ret) {
1015 			dev_err(dev, "Failed to %s bandwidth[%d]: %d\n",
1016 				opp ? "set" : "remove", i, ret);
1017 			return ret;
1018 		}
1019 	}
1020 
1021 	return 0;
1022 }
1023 
_set_performance_state(struct device * dev,struct device * pd_dev,struct dev_pm_opp * opp,int i)1024 static int _set_performance_state(struct device *dev, struct device *pd_dev,
1025 				  struct dev_pm_opp *opp, int i)
1026 {
1027 	unsigned int pstate = likely(opp) ? opp->required_opps[i]->level: 0;
1028 	int ret;
1029 
1030 	if (!pd_dev)
1031 		return 0;
1032 
1033 	ret = dev_pm_domain_set_performance_state(pd_dev, pstate);
1034 	if (ret) {
1035 		dev_err(dev, "Failed to set performance state of %s: %d (%d)\n",
1036 			dev_name(pd_dev), pstate, ret);
1037 	}
1038 
1039 	return ret;
1040 }
1041 
_opp_set_required_opps_generic(struct device * dev,struct opp_table * opp_table,struct dev_pm_opp * opp,bool scaling_down)1042 static int _opp_set_required_opps_generic(struct device *dev,
1043 	struct opp_table *opp_table, struct dev_pm_opp *opp, bool scaling_down)
1044 {
1045 	dev_err(dev, "setting required-opps isn't supported for non-genpd devices\n");
1046 	return -ENOENT;
1047 }
1048 
_opp_set_required_opps_genpd(struct device * dev,struct opp_table * opp_table,struct dev_pm_opp * opp,bool scaling_down)1049 static int _opp_set_required_opps_genpd(struct device *dev,
1050 	struct opp_table *opp_table, struct dev_pm_opp *opp, bool scaling_down)
1051 {
1052 	struct device **genpd_virt_devs =
1053 		opp_table->genpd_virt_devs ? opp_table->genpd_virt_devs : &dev;
1054 	int i, ret = 0;
1055 
1056 	/*
1057 	 * Acquire genpd_virt_dev_lock to make sure we don't use a genpd_dev
1058 	 * after it is freed from another thread.
1059 	 */
1060 	mutex_lock(&opp_table->genpd_virt_dev_lock);
1061 
1062 	/* Scaling up? Set required OPPs in normal order, else reverse */
1063 	if (!scaling_down) {
1064 		for (i = 0; i < opp_table->required_opp_count; i++) {
1065 			ret = _set_performance_state(dev, genpd_virt_devs[i], opp, i);
1066 			if (ret)
1067 				break;
1068 		}
1069 	} else {
1070 		for (i = opp_table->required_opp_count - 1; i >= 0; i--) {
1071 			ret = _set_performance_state(dev, genpd_virt_devs[i], opp, i);
1072 			if (ret)
1073 				break;
1074 		}
1075 	}
1076 
1077 	mutex_unlock(&opp_table->genpd_virt_dev_lock);
1078 
1079 	return ret;
1080 }
1081 
1082 /* This is only called for PM domain for now */
_set_required_opps(struct device * dev,struct opp_table * opp_table,struct dev_pm_opp * opp,bool up)1083 static int _set_required_opps(struct device *dev, struct opp_table *opp_table,
1084 			      struct dev_pm_opp *opp, bool up)
1085 {
1086 	/* required-opps not fully initialized yet */
1087 	if (lazy_linking_pending(opp_table))
1088 		return -EBUSY;
1089 
1090 	if (opp_table->set_required_opps)
1091 		return opp_table->set_required_opps(dev, opp_table, opp, up);
1092 
1093 	return 0;
1094 }
1095 
1096 /* Update set_required_opps handler */
_update_set_required_opps(struct opp_table * opp_table)1097 void _update_set_required_opps(struct opp_table *opp_table)
1098 {
1099 	/* Already set */
1100 	if (opp_table->set_required_opps)
1101 		return;
1102 
1103 	/* All required OPPs will belong to genpd or none */
1104 	if (opp_table->required_opp_tables[0]->is_genpd)
1105 		opp_table->set_required_opps = _opp_set_required_opps_genpd;
1106 	else
1107 		opp_table->set_required_opps = _opp_set_required_opps_generic;
1108 }
1109 
_set_opp_level(struct device * dev,struct opp_table * opp_table,struct dev_pm_opp * opp)1110 static int _set_opp_level(struct device *dev, struct opp_table *opp_table,
1111 			  struct dev_pm_opp *opp)
1112 {
1113 	unsigned int level = 0;
1114 	int ret = 0;
1115 
1116 	if (opp) {
1117 		if (!opp->level)
1118 			return 0;
1119 
1120 		level = opp->level;
1121 	}
1122 
1123 	/* Request a new performance state through the device's PM domain. */
1124 	ret = dev_pm_domain_set_performance_state(dev, level);
1125 	if (ret)
1126 		dev_err(dev, "Failed to set performance state %u (%d)\n", level,
1127 			ret);
1128 
1129 	return ret;
1130 }
1131 
_find_current_opp(struct device * dev,struct opp_table * opp_table)1132 static void _find_current_opp(struct device *dev, struct opp_table *opp_table)
1133 {
1134 	struct dev_pm_opp *opp = ERR_PTR(-ENODEV);
1135 	unsigned long freq;
1136 
1137 	if (!IS_ERR(opp_table->clk)) {
1138 		freq = clk_get_rate(opp_table->clk);
1139 		opp = _find_freq_ceil(opp_table, &freq);
1140 	}
1141 
1142 	/*
1143 	 * Unable to find the current OPP ? Pick the first from the list since
1144 	 * it is in ascending order, otherwise rest of the code will need to
1145 	 * make special checks to validate current_opp.
1146 	 */
1147 	if (IS_ERR(opp)) {
1148 		mutex_lock(&opp_table->lock);
1149 		opp = list_first_entry(&opp_table->opp_list, struct dev_pm_opp, node);
1150 		dev_pm_opp_get(opp);
1151 		mutex_unlock(&opp_table->lock);
1152 	}
1153 
1154 	opp_table->current_opp = opp;
1155 }
1156 
_disable_opp_table(struct device * dev,struct opp_table * opp_table)1157 static int _disable_opp_table(struct device *dev, struct opp_table *opp_table)
1158 {
1159 	int ret;
1160 
1161 	if (!opp_table->enabled)
1162 		return 0;
1163 
1164 	/*
1165 	 * Some drivers need to support cases where some platforms may
1166 	 * have OPP table for the device, while others don't and
1167 	 * opp_set_rate() just needs to behave like clk_set_rate().
1168 	 */
1169 	if (!_get_opp_count(opp_table))
1170 		return 0;
1171 
1172 	ret = _set_opp_bw(opp_table, NULL, dev);
1173 	if (ret)
1174 		return ret;
1175 
1176 	if (opp_table->regulators)
1177 		regulator_disable(opp_table->regulators[0]);
1178 
1179 	ret = _set_opp_level(dev, opp_table, NULL);
1180 	if (ret)
1181 		goto out;
1182 
1183 	ret = _set_required_opps(dev, opp_table, NULL, false);
1184 
1185 out:
1186 	opp_table->enabled = false;
1187 	return ret;
1188 }
1189 
_set_opp(struct device * dev,struct opp_table * opp_table,struct dev_pm_opp * opp,void * clk_data,bool forced)1190 static int _set_opp(struct device *dev, struct opp_table *opp_table,
1191 		    struct dev_pm_opp *opp, void *clk_data, bool forced)
1192 {
1193 	struct dev_pm_opp *old_opp;
1194 	int scaling_down, ret;
1195 
1196 	if (unlikely(!opp))
1197 		return _disable_opp_table(dev, opp_table);
1198 
1199 	/* Find the currently set OPP if we don't know already */
1200 	if (unlikely(!opp_table->current_opp))
1201 		_find_current_opp(dev, opp_table);
1202 
1203 	old_opp = opp_table->current_opp;
1204 
1205 	/* Return early if nothing to do */
1206 	if (!forced && old_opp == opp && opp_table->enabled) {
1207 		dev_dbg_ratelimited(dev, "%s: OPPs are same, nothing to do\n", __func__);
1208 		return 0;
1209 	}
1210 
1211 	dev_dbg(dev, "%s: switching OPP: Freq %lu -> %lu Hz, Level %u -> %u, Bw %u -> %u\n",
1212 		__func__, old_opp->rates[0], opp->rates[0], old_opp->level,
1213 		opp->level, old_opp->bandwidth ? old_opp->bandwidth[0].peak : 0,
1214 		opp->bandwidth ? opp->bandwidth[0].peak : 0);
1215 
1216 	scaling_down = _opp_compare_key(opp_table, old_opp, opp);
1217 	if (scaling_down == -1)
1218 		scaling_down = 0;
1219 
1220 	/* Scaling up? Configure required OPPs before frequency */
1221 	if (!scaling_down) {
1222 		ret = _set_required_opps(dev, opp_table, opp, true);
1223 		if (ret) {
1224 			dev_err(dev, "Failed to set required opps: %d\n", ret);
1225 			return ret;
1226 		}
1227 
1228 		ret = _set_opp_level(dev, opp_table, opp);
1229 		if (ret)
1230 			return ret;
1231 
1232 		ret = _set_opp_bw(opp_table, opp, dev);
1233 		if (ret) {
1234 			dev_err(dev, "Failed to set bw: %d\n", ret);
1235 			return ret;
1236 		}
1237 
1238 		if (opp_table->config_regulators) {
1239 			ret = opp_table->config_regulators(dev, old_opp, opp,
1240 							   opp_table->regulators,
1241 							   opp_table->regulator_count);
1242 			if (ret) {
1243 				dev_err(dev, "Failed to set regulator voltages: %d\n",
1244 					ret);
1245 				return ret;
1246 			}
1247 		}
1248 	}
1249 
1250 	if (opp_table->config_clks) {
1251 		ret = opp_table->config_clks(dev, opp_table, opp, clk_data, scaling_down);
1252 		if (ret)
1253 			return ret;
1254 	}
1255 
1256 	/* Scaling down? Configure required OPPs after frequency */
1257 	if (scaling_down) {
1258 		if (opp_table->config_regulators) {
1259 			ret = opp_table->config_regulators(dev, old_opp, opp,
1260 							   opp_table->regulators,
1261 							   opp_table->regulator_count);
1262 			if (ret) {
1263 				dev_err(dev, "Failed to set regulator voltages: %d\n",
1264 					ret);
1265 				return ret;
1266 			}
1267 		}
1268 
1269 		ret = _set_opp_bw(opp_table, opp, dev);
1270 		if (ret) {
1271 			dev_err(dev, "Failed to set bw: %d\n", ret);
1272 			return ret;
1273 		}
1274 
1275 		ret = _set_opp_level(dev, opp_table, opp);
1276 		if (ret)
1277 			return ret;
1278 
1279 		ret = _set_required_opps(dev, opp_table, opp, false);
1280 		if (ret) {
1281 			dev_err(dev, "Failed to set required opps: %d\n", ret);
1282 			return ret;
1283 		}
1284 	}
1285 
1286 	opp_table->enabled = true;
1287 	dev_pm_opp_put(old_opp);
1288 
1289 	/* Make sure current_opp doesn't get freed */
1290 	dev_pm_opp_get(opp);
1291 	opp_table->current_opp = opp;
1292 
1293 	return ret;
1294 }
1295 
1296 /**
1297  * dev_pm_opp_set_rate() - Configure new OPP based on frequency
1298  * @dev:	 device for which we do this operation
1299  * @target_freq: frequency to achieve
1300  *
1301  * This configures the power-supplies to the levels specified by the OPP
1302  * corresponding to the target_freq, and programs the clock to a value <=
1303  * target_freq, as rounded by clk_round_rate(). Device wanting to run at fmax
1304  * provided by the opp, should have already rounded to the target OPP's
1305  * frequency.
1306  */
dev_pm_opp_set_rate(struct device * dev,unsigned long target_freq)1307 int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq)
1308 {
1309 	struct opp_table *opp_table;
1310 	unsigned long freq = 0, temp_freq;
1311 	struct dev_pm_opp *opp = NULL;
1312 	bool forced = false;
1313 	int ret;
1314 
1315 	opp_table = _find_opp_table(dev);
1316 	if (IS_ERR(opp_table)) {
1317 		dev_err(dev, "%s: device's opp table doesn't exist\n", __func__);
1318 		return PTR_ERR(opp_table);
1319 	}
1320 
1321 	if (target_freq) {
1322 		/*
1323 		 * For IO devices which require an OPP on some platforms/SoCs
1324 		 * while just needing to scale the clock on some others
1325 		 * we look for empty OPP tables with just a clock handle and
1326 		 * scale only the clk. This makes dev_pm_opp_set_rate()
1327 		 * equivalent to a clk_set_rate()
1328 		 */
1329 		if (!_get_opp_count(opp_table)) {
1330 			ret = opp_table->config_clks(dev, opp_table, NULL,
1331 						     &target_freq, false);
1332 			goto put_opp_table;
1333 		}
1334 
1335 		freq = clk_round_rate(opp_table->clk, target_freq);
1336 		if ((long)freq <= 0)
1337 			freq = target_freq;
1338 
1339 		/*
1340 		 * The clock driver may support finer resolution of the
1341 		 * frequencies than the OPP table, don't update the frequency we
1342 		 * pass to clk_set_rate() here.
1343 		 */
1344 		temp_freq = freq;
1345 		opp = _find_freq_ceil(opp_table, &temp_freq);
1346 		if (IS_ERR(opp)) {
1347 			ret = PTR_ERR(opp);
1348 			dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n",
1349 				__func__, freq, ret);
1350 			goto put_opp_table;
1351 		}
1352 
1353 		/*
1354 		 * An OPP entry specifies the highest frequency at which other
1355 		 * properties of the OPP entry apply. Even if the new OPP is
1356 		 * same as the old one, we may still reach here for a different
1357 		 * value of the frequency. In such a case, do not abort but
1358 		 * configure the hardware to the desired frequency forcefully.
1359 		 */
1360 		forced = opp_table->rate_clk_single != freq;
1361 	}
1362 
1363 	ret = _set_opp(dev, opp_table, opp, &freq, forced);
1364 
1365 	if (freq)
1366 		dev_pm_opp_put(opp);
1367 
1368 put_opp_table:
1369 	dev_pm_opp_put_opp_table(opp_table);
1370 	return ret;
1371 }
1372 EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate);
1373 
1374 /**
1375  * dev_pm_opp_set_opp() - Configure device for OPP
1376  * @dev: device for which we do this operation
1377  * @opp: OPP to set to
1378  *
1379  * This configures the device based on the properties of the OPP passed to this
1380  * routine.
1381  *
1382  * Return: 0 on success, a negative error number otherwise.
1383  */
dev_pm_opp_set_opp(struct device * dev,struct dev_pm_opp * opp)1384 int dev_pm_opp_set_opp(struct device *dev, struct dev_pm_opp *opp)
1385 {
1386 	struct opp_table *opp_table;
1387 	int ret;
1388 
1389 	opp_table = _find_opp_table(dev);
1390 	if (IS_ERR(opp_table)) {
1391 		dev_err(dev, "%s: device opp doesn't exist\n", __func__);
1392 		return PTR_ERR(opp_table);
1393 	}
1394 
1395 	ret = _set_opp(dev, opp_table, opp, NULL, false);
1396 	dev_pm_opp_put_opp_table(opp_table);
1397 
1398 	return ret;
1399 }
1400 EXPORT_SYMBOL_GPL(dev_pm_opp_set_opp);
1401 
1402 /* OPP-dev Helpers */
_remove_opp_dev(struct opp_device * opp_dev,struct opp_table * opp_table)1403 static void _remove_opp_dev(struct opp_device *opp_dev,
1404 			    struct opp_table *opp_table)
1405 {
1406 	opp_debug_unregister(opp_dev, opp_table);
1407 	list_del(&opp_dev->node);
1408 	kfree(opp_dev);
1409 }
1410 
_add_opp_dev(const struct device * dev,struct opp_table * opp_table)1411 struct opp_device *_add_opp_dev(const struct device *dev,
1412 				struct opp_table *opp_table)
1413 {
1414 	struct opp_device *opp_dev;
1415 
1416 	opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL);
1417 	if (!opp_dev)
1418 		return NULL;
1419 
1420 	/* Initialize opp-dev */
1421 	opp_dev->dev = dev;
1422 
1423 	mutex_lock(&opp_table->lock);
1424 	list_add(&opp_dev->node, &opp_table->dev_list);
1425 	mutex_unlock(&opp_table->lock);
1426 
1427 	/* Create debugfs entries for the opp_table */
1428 	opp_debug_register(opp_dev, opp_table);
1429 
1430 	return opp_dev;
1431 }
1432 
_allocate_opp_table(struct device * dev,int index)1433 static struct opp_table *_allocate_opp_table(struct device *dev, int index)
1434 {
1435 	struct opp_table *opp_table;
1436 	struct opp_device *opp_dev;
1437 	int ret;
1438 
1439 	/*
1440 	 * Allocate a new OPP table. In the infrequent case where a new
1441 	 * device is needed to be added, we pay this penalty.
1442 	 */
1443 	opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL);
1444 	if (!opp_table)
1445 		return ERR_PTR(-ENOMEM);
1446 
1447 	mutex_init(&opp_table->lock);
1448 	mutex_init(&opp_table->genpd_virt_dev_lock);
1449 	INIT_LIST_HEAD(&opp_table->dev_list);
1450 	INIT_LIST_HEAD(&opp_table->lazy);
1451 
1452 	opp_table->clk = ERR_PTR(-ENODEV);
1453 
1454 	/* Mark regulator count uninitialized */
1455 	opp_table->regulator_count = -1;
1456 
1457 	opp_dev = _add_opp_dev(dev, opp_table);
1458 	if (!opp_dev) {
1459 		ret = -ENOMEM;
1460 		goto err;
1461 	}
1462 
1463 	_of_init_opp_table(opp_table, dev, index);
1464 
1465 	/* Find interconnect path(s) for the device */
1466 	ret = dev_pm_opp_of_find_icc_paths(dev, opp_table);
1467 	if (ret) {
1468 		if (ret == -EPROBE_DEFER)
1469 			goto remove_opp_dev;
1470 
1471 		dev_warn(dev, "%s: Error finding interconnect paths: %d\n",
1472 			 __func__, ret);
1473 	}
1474 
1475 	BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head);
1476 	INIT_LIST_HEAD(&opp_table->opp_list);
1477 	kref_init(&opp_table->kref);
1478 
1479 	return opp_table;
1480 
1481 remove_opp_dev:
1482 	_of_clear_opp_table(opp_table);
1483 	_remove_opp_dev(opp_dev, opp_table);
1484 	mutex_destroy(&opp_table->genpd_virt_dev_lock);
1485 	mutex_destroy(&opp_table->lock);
1486 err:
1487 	kfree(opp_table);
1488 	return ERR_PTR(ret);
1489 }
1490 
_get_opp_table_kref(struct opp_table * opp_table)1491 void _get_opp_table_kref(struct opp_table *opp_table)
1492 {
1493 	kref_get(&opp_table->kref);
1494 }
1495 
_update_opp_table_clk(struct device * dev,struct opp_table * opp_table,bool getclk)1496 static struct opp_table *_update_opp_table_clk(struct device *dev,
1497 					       struct opp_table *opp_table,
1498 					       bool getclk)
1499 {
1500 	int ret;
1501 
1502 	/*
1503 	 * Return early if we don't need to get clk or we have already done it
1504 	 * earlier.
1505 	 */
1506 	if (!getclk || IS_ERR(opp_table) || !IS_ERR(opp_table->clk) ||
1507 	    opp_table->clks)
1508 		return opp_table;
1509 
1510 	/* Find clk for the device */
1511 	opp_table->clk = clk_get(dev, NULL);
1512 
1513 	ret = PTR_ERR_OR_ZERO(opp_table->clk);
1514 	if (!ret) {
1515 		opp_table->config_clks = _opp_config_clk_single;
1516 		opp_table->clk_count = 1;
1517 		return opp_table;
1518 	}
1519 
1520 	if (ret == -ENOENT) {
1521 		/*
1522 		 * There are few platforms which don't want the OPP core to
1523 		 * manage device's clock settings. In such cases neither the
1524 		 * platform provides the clks explicitly to us, nor the DT
1525 		 * contains a valid clk entry. The OPP nodes in DT may still
1526 		 * contain "opp-hz" property though, which we need to parse and
1527 		 * allow the platform to find an OPP based on freq later on.
1528 		 *
1529 		 * This is a simple solution to take care of such corner cases,
1530 		 * i.e. make the clk_count 1, which lets us allocate space for
1531 		 * frequency in opp->rates and also parse the entries in DT.
1532 		 */
1533 		opp_table->clk_count = 1;
1534 
1535 		dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__, ret);
1536 		return opp_table;
1537 	}
1538 
1539 	dev_pm_opp_put_opp_table(opp_table);
1540 	dev_err_probe(dev, ret, "Couldn't find clock\n");
1541 
1542 	return ERR_PTR(ret);
1543 }
1544 
1545 /*
1546  * We need to make sure that the OPP table for a device doesn't get added twice,
1547  * if this routine gets called in parallel with the same device pointer.
1548  *
1549  * The simplest way to enforce that is to perform everything (find existing
1550  * table and if not found, create a new one) under the opp_table_lock, so only
1551  * one creator gets access to the same. But that expands the critical section
1552  * under the lock and may end up causing circular dependencies with frameworks
1553  * like debugfs, interconnect or clock framework as they may be direct or
1554  * indirect users of OPP core.
1555  *
1556  * And for that reason we have to go for a bit tricky implementation here, which
1557  * uses the opp_tables_busy flag to indicate if another creator is in the middle
1558  * of adding an OPP table and others should wait for it to finish.
1559  */
_add_opp_table_indexed(struct device * dev,int index,bool getclk)1560 struct opp_table *_add_opp_table_indexed(struct device *dev, int index,
1561 					 bool getclk)
1562 {
1563 	struct opp_table *opp_table;
1564 
1565 again:
1566 	mutex_lock(&opp_table_lock);
1567 
1568 	opp_table = _find_opp_table_unlocked(dev);
1569 	if (!IS_ERR(opp_table))
1570 		goto unlock;
1571 
1572 	/*
1573 	 * The opp_tables list or an OPP table's dev_list is getting updated by
1574 	 * another user, wait for it to finish.
1575 	 */
1576 	if (unlikely(opp_tables_busy)) {
1577 		mutex_unlock(&opp_table_lock);
1578 		cpu_relax();
1579 		goto again;
1580 	}
1581 
1582 	opp_tables_busy = true;
1583 	opp_table = _managed_opp(dev, index);
1584 
1585 	/* Drop the lock to reduce the size of critical section */
1586 	mutex_unlock(&opp_table_lock);
1587 
1588 	if (opp_table) {
1589 		if (!_add_opp_dev(dev, opp_table)) {
1590 			dev_pm_opp_put_opp_table(opp_table);
1591 			opp_table = ERR_PTR(-ENOMEM);
1592 		}
1593 
1594 		mutex_lock(&opp_table_lock);
1595 	} else {
1596 		opp_table = _allocate_opp_table(dev, index);
1597 
1598 		mutex_lock(&opp_table_lock);
1599 		if (!IS_ERR(opp_table))
1600 			list_add(&opp_table->node, &opp_tables);
1601 	}
1602 
1603 	opp_tables_busy = false;
1604 
1605 unlock:
1606 	mutex_unlock(&opp_table_lock);
1607 
1608 	return _update_opp_table_clk(dev, opp_table, getclk);
1609 }
1610 
_add_opp_table(struct device * dev,bool getclk)1611 static struct opp_table *_add_opp_table(struct device *dev, bool getclk)
1612 {
1613 	return _add_opp_table_indexed(dev, 0, getclk);
1614 }
1615 
dev_pm_opp_get_opp_table(struct device * dev)1616 struct opp_table *dev_pm_opp_get_opp_table(struct device *dev)
1617 {
1618 	return _find_opp_table(dev);
1619 }
1620 EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table);
1621 
_opp_table_kref_release(struct kref * kref)1622 static void _opp_table_kref_release(struct kref *kref)
1623 {
1624 	struct opp_table *opp_table = container_of(kref, struct opp_table, kref);
1625 	struct opp_device *opp_dev, *temp;
1626 	int i;
1627 
1628 	/* Drop the lock as soon as we can */
1629 	list_del(&opp_table->node);
1630 	mutex_unlock(&opp_table_lock);
1631 
1632 	if (opp_table->current_opp)
1633 		dev_pm_opp_put(opp_table->current_opp);
1634 
1635 	_of_clear_opp_table(opp_table);
1636 
1637 	/* Release automatically acquired single clk */
1638 	if (!IS_ERR(opp_table->clk))
1639 		clk_put(opp_table->clk);
1640 
1641 	if (opp_table->paths) {
1642 		for (i = 0; i < opp_table->path_count; i++)
1643 			icc_put(opp_table->paths[i]);
1644 		kfree(opp_table->paths);
1645 	}
1646 
1647 	WARN_ON(!list_empty(&opp_table->opp_list));
1648 
1649 	list_for_each_entry_safe(opp_dev, temp, &opp_table->dev_list, node)
1650 		_remove_opp_dev(opp_dev, opp_table);
1651 
1652 	mutex_destroy(&opp_table->genpd_virt_dev_lock);
1653 	mutex_destroy(&opp_table->lock);
1654 	kfree(opp_table);
1655 }
1656 
dev_pm_opp_put_opp_table(struct opp_table * opp_table)1657 void dev_pm_opp_put_opp_table(struct opp_table *opp_table)
1658 {
1659 	kref_put_mutex(&opp_table->kref, _opp_table_kref_release,
1660 		       &opp_table_lock);
1661 }
1662 EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table);
1663 
_opp_free(struct dev_pm_opp * opp)1664 void _opp_free(struct dev_pm_opp *opp)
1665 {
1666 	kfree(opp);
1667 }
1668 
_opp_kref_release(struct kref * kref)1669 static void _opp_kref_release(struct kref *kref)
1670 {
1671 	struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref);
1672 	struct opp_table *opp_table = opp->opp_table;
1673 
1674 	list_del(&opp->node);
1675 	mutex_unlock(&opp_table->lock);
1676 
1677 	/*
1678 	 * Notify the changes in the availability of the operable
1679 	 * frequency/voltage list.
1680 	 */
1681 	blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_REMOVE, opp);
1682 	_of_clear_opp(opp_table, opp);
1683 	opp_debug_remove_one(opp);
1684 	kfree(opp);
1685 }
1686 
dev_pm_opp_get(struct dev_pm_opp * opp)1687 void dev_pm_opp_get(struct dev_pm_opp *opp)
1688 {
1689 	kref_get(&opp->kref);
1690 }
1691 
dev_pm_opp_put(struct dev_pm_opp * opp)1692 void dev_pm_opp_put(struct dev_pm_opp *opp)
1693 {
1694 	kref_put_mutex(&opp->kref, _opp_kref_release, &opp->opp_table->lock);
1695 }
1696 EXPORT_SYMBOL_GPL(dev_pm_opp_put);
1697 
1698 /**
1699  * dev_pm_opp_remove()  - Remove an OPP from OPP table
1700  * @dev:	device for which we do this operation
1701  * @freq:	OPP to remove with matching 'freq'
1702  *
1703  * This function removes an opp from the opp table.
1704  */
dev_pm_opp_remove(struct device * dev,unsigned long freq)1705 void dev_pm_opp_remove(struct device *dev, unsigned long freq)
1706 {
1707 	struct dev_pm_opp *opp = NULL, *iter;
1708 	struct opp_table *opp_table;
1709 
1710 	opp_table = _find_opp_table(dev);
1711 	if (IS_ERR(opp_table))
1712 		return;
1713 
1714 	if (!assert_single_clk(opp_table))
1715 		goto put_table;
1716 
1717 	mutex_lock(&opp_table->lock);
1718 
1719 	list_for_each_entry(iter, &opp_table->opp_list, node) {
1720 		if (iter->rates[0] == freq) {
1721 			opp = iter;
1722 			break;
1723 		}
1724 	}
1725 
1726 	mutex_unlock(&opp_table->lock);
1727 
1728 	if (opp) {
1729 		dev_pm_opp_put(opp);
1730 
1731 		/* Drop the reference taken by dev_pm_opp_add() */
1732 		dev_pm_opp_put_opp_table(opp_table);
1733 	} else {
1734 		dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
1735 			 __func__, freq);
1736 	}
1737 
1738 put_table:
1739 	/* Drop the reference taken by _find_opp_table() */
1740 	dev_pm_opp_put_opp_table(opp_table);
1741 }
1742 EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
1743 
_opp_get_next(struct opp_table * opp_table,bool dynamic)1744 static struct dev_pm_opp *_opp_get_next(struct opp_table *opp_table,
1745 					bool dynamic)
1746 {
1747 	struct dev_pm_opp *opp = NULL, *temp;
1748 
1749 	mutex_lock(&opp_table->lock);
1750 	list_for_each_entry(temp, &opp_table->opp_list, node) {
1751 		/*
1752 		 * Refcount must be dropped only once for each OPP by OPP core,
1753 		 * do that with help of "removed" flag.
1754 		 */
1755 		if (!temp->removed && dynamic == temp->dynamic) {
1756 			opp = temp;
1757 			break;
1758 		}
1759 	}
1760 
1761 	mutex_unlock(&opp_table->lock);
1762 	return opp;
1763 }
1764 
1765 /*
1766  * Can't call dev_pm_opp_put() from under the lock as debugfs removal needs to
1767  * happen lock less to avoid circular dependency issues. This routine must be
1768  * called without the opp_table->lock held.
1769  */
_opp_remove_all(struct opp_table * opp_table,bool dynamic)1770 static void _opp_remove_all(struct opp_table *opp_table, bool dynamic)
1771 {
1772 	struct dev_pm_opp *opp;
1773 
1774 	while ((opp = _opp_get_next(opp_table, dynamic))) {
1775 		opp->removed = true;
1776 		dev_pm_opp_put(opp);
1777 
1778 		/* Drop the references taken by dev_pm_opp_add() */
1779 		if (dynamic)
1780 			dev_pm_opp_put_opp_table(opp_table);
1781 	}
1782 }
1783 
_opp_remove_all_static(struct opp_table * opp_table)1784 bool _opp_remove_all_static(struct opp_table *opp_table)
1785 {
1786 	mutex_lock(&opp_table->lock);
1787 
1788 	if (!opp_table->parsed_static_opps) {
1789 		mutex_unlock(&opp_table->lock);
1790 		return false;
1791 	}
1792 
1793 	if (--opp_table->parsed_static_opps) {
1794 		mutex_unlock(&opp_table->lock);
1795 		return true;
1796 	}
1797 
1798 	mutex_unlock(&opp_table->lock);
1799 
1800 	_opp_remove_all(opp_table, false);
1801 	return true;
1802 }
1803 
1804 /**
1805  * dev_pm_opp_remove_all_dynamic() - Remove all dynamically created OPPs
1806  * @dev:	device for which we do this operation
1807  *
1808  * This function removes all dynamically created OPPs from the opp table.
1809  */
dev_pm_opp_remove_all_dynamic(struct device * dev)1810 void dev_pm_opp_remove_all_dynamic(struct device *dev)
1811 {
1812 	struct opp_table *opp_table;
1813 
1814 	opp_table = _find_opp_table(dev);
1815 	if (IS_ERR(opp_table))
1816 		return;
1817 
1818 	_opp_remove_all(opp_table, true);
1819 
1820 	/* Drop the reference taken by _find_opp_table() */
1821 	dev_pm_opp_put_opp_table(opp_table);
1822 }
1823 EXPORT_SYMBOL_GPL(dev_pm_opp_remove_all_dynamic);
1824 
_opp_allocate(struct opp_table * opp_table)1825 struct dev_pm_opp *_opp_allocate(struct opp_table *opp_table)
1826 {
1827 	struct dev_pm_opp *opp;
1828 	int supply_count, supply_size, icc_size, clk_size;
1829 
1830 	/* Allocate space for at least one supply */
1831 	supply_count = opp_table->regulator_count > 0 ?
1832 			opp_table->regulator_count : 1;
1833 	supply_size = sizeof(*opp->supplies) * supply_count;
1834 	clk_size = sizeof(*opp->rates) * opp_table->clk_count;
1835 	icc_size = sizeof(*opp->bandwidth) * opp_table->path_count;
1836 
1837 	/* allocate new OPP node and supplies structures */
1838 	opp = kzalloc(sizeof(*opp) + supply_size + clk_size + icc_size, GFP_KERNEL);
1839 	if (!opp)
1840 		return NULL;
1841 
1842 	/* Put the supplies, bw and clock at the end of the OPP structure */
1843 	opp->supplies = (struct dev_pm_opp_supply *)(opp + 1);
1844 
1845 	opp->rates = (unsigned long *)(opp->supplies + supply_count);
1846 
1847 	if (icc_size)
1848 		opp->bandwidth = (struct dev_pm_opp_icc_bw *)(opp->rates + opp_table->clk_count);
1849 
1850 	INIT_LIST_HEAD(&opp->node);
1851 
1852 	return opp;
1853 }
1854 
_opp_supported_by_regulators(struct dev_pm_opp * opp,struct opp_table * opp_table)1855 static bool _opp_supported_by_regulators(struct dev_pm_opp *opp,
1856 					 struct opp_table *opp_table)
1857 {
1858 	struct regulator *reg;
1859 	int i;
1860 
1861 	if (!opp_table->regulators)
1862 		return true;
1863 
1864 	for (i = 0; i < opp_table->regulator_count; i++) {
1865 		reg = opp_table->regulators[i];
1866 
1867 		if (!regulator_is_supported_voltage(reg,
1868 					opp->supplies[i].u_volt_min,
1869 					opp->supplies[i].u_volt_max)) {
1870 			pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n",
1871 				__func__, opp->supplies[i].u_volt_min,
1872 				opp->supplies[i].u_volt_max);
1873 			return false;
1874 		}
1875 	}
1876 
1877 	return true;
1878 }
1879 
_opp_compare_rate(struct opp_table * opp_table,struct dev_pm_opp * opp1,struct dev_pm_opp * opp2)1880 static int _opp_compare_rate(struct opp_table *opp_table,
1881 			     struct dev_pm_opp *opp1, struct dev_pm_opp *opp2)
1882 {
1883 	int i;
1884 
1885 	for (i = 0; i < opp_table->clk_count; i++) {
1886 		if (opp1->rates[i] != opp2->rates[i])
1887 			return opp1->rates[i] < opp2->rates[i] ? -1 : 1;
1888 	}
1889 
1890 	/* Same rates for both OPPs */
1891 	return 0;
1892 }
1893 
_opp_compare_bw(struct opp_table * opp_table,struct dev_pm_opp * opp1,struct dev_pm_opp * opp2)1894 static int _opp_compare_bw(struct opp_table *opp_table, struct dev_pm_opp *opp1,
1895 			   struct dev_pm_opp *opp2)
1896 {
1897 	int i;
1898 
1899 	for (i = 0; i < opp_table->path_count; i++) {
1900 		if (opp1->bandwidth[i].peak != opp2->bandwidth[i].peak)
1901 			return opp1->bandwidth[i].peak < opp2->bandwidth[i].peak ? -1 : 1;
1902 	}
1903 
1904 	/* Same bw for both OPPs */
1905 	return 0;
1906 }
1907 
1908 /*
1909  * Returns
1910  * 0: opp1 == opp2
1911  * 1: opp1 > opp2
1912  * -1: opp1 < opp2
1913  */
_opp_compare_key(struct opp_table * opp_table,struct dev_pm_opp * opp1,struct dev_pm_opp * opp2)1914 int _opp_compare_key(struct opp_table *opp_table, struct dev_pm_opp *opp1,
1915 		     struct dev_pm_opp *opp2)
1916 {
1917 	int ret;
1918 
1919 	ret = _opp_compare_rate(opp_table, opp1, opp2);
1920 	if (ret)
1921 		return ret;
1922 
1923 	ret = _opp_compare_bw(opp_table, opp1, opp2);
1924 	if (ret)
1925 		return ret;
1926 
1927 	if (opp1->level != opp2->level)
1928 		return opp1->level < opp2->level ? -1 : 1;
1929 
1930 	/* Duplicate OPPs */
1931 	return 0;
1932 }
1933 
_opp_is_duplicate(struct device * dev,struct dev_pm_opp * new_opp,struct opp_table * opp_table,struct list_head ** head)1934 static int _opp_is_duplicate(struct device *dev, struct dev_pm_opp *new_opp,
1935 			     struct opp_table *opp_table,
1936 			     struct list_head **head)
1937 {
1938 	struct dev_pm_opp *opp;
1939 	int opp_cmp;
1940 
1941 	/*
1942 	 * Insert new OPP in order of increasing frequency and discard if
1943 	 * already present.
1944 	 *
1945 	 * Need to use &opp_table->opp_list in the condition part of the 'for'
1946 	 * loop, don't replace it with head otherwise it will become an infinite
1947 	 * loop.
1948 	 */
1949 	list_for_each_entry(opp, &opp_table->opp_list, node) {
1950 		opp_cmp = _opp_compare_key(opp_table, new_opp, opp);
1951 		if (opp_cmp > 0) {
1952 			*head = &opp->node;
1953 			continue;
1954 		}
1955 
1956 		if (opp_cmp < 0)
1957 			return 0;
1958 
1959 		/* Duplicate OPPs */
1960 		dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
1961 			 __func__, opp->rates[0], opp->supplies[0].u_volt,
1962 			 opp->available, new_opp->rates[0],
1963 			 new_opp->supplies[0].u_volt, new_opp->available);
1964 
1965 		/* Should we compare voltages for all regulators here ? */
1966 		return opp->available &&
1967 		       new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST;
1968 	}
1969 
1970 	return 0;
1971 }
1972 
_required_opps_available(struct dev_pm_opp * opp,int count)1973 void _required_opps_available(struct dev_pm_opp *opp, int count)
1974 {
1975 	int i;
1976 
1977 	for (i = 0; i < count; i++) {
1978 		if (opp->required_opps[i]->available)
1979 			continue;
1980 
1981 		opp->available = false;
1982 		pr_warn("%s: OPP not supported by required OPP %pOF (%lu)\n",
1983 			 __func__, opp->required_opps[i]->np, opp->rates[0]);
1984 		return;
1985 	}
1986 }
1987 
1988 /*
1989  * Returns:
1990  * 0: On success. And appropriate error message for duplicate OPPs.
1991  * -EBUSY: For OPP with same freq/volt and is available. The callers of
1992  *  _opp_add() must return 0 if they receive -EBUSY from it. This is to make
1993  *  sure we don't print error messages unnecessarily if different parts of
1994  *  kernel try to initialize the OPP table.
1995  * -EEXIST: For OPP with same freq but different volt or is unavailable. This
1996  *  should be considered an error by the callers of _opp_add().
1997  */
_opp_add(struct device * dev,struct dev_pm_opp * new_opp,struct opp_table * opp_table)1998 int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
1999 	     struct opp_table *opp_table)
2000 {
2001 	struct list_head *head;
2002 	int ret;
2003 
2004 	mutex_lock(&opp_table->lock);
2005 	head = &opp_table->opp_list;
2006 
2007 	ret = _opp_is_duplicate(dev, new_opp, opp_table, &head);
2008 	if (ret) {
2009 		mutex_unlock(&opp_table->lock);
2010 		return ret;
2011 	}
2012 
2013 	list_add(&new_opp->node, head);
2014 	mutex_unlock(&opp_table->lock);
2015 
2016 	new_opp->opp_table = opp_table;
2017 	kref_init(&new_opp->kref);
2018 
2019 	opp_debug_create_one(new_opp, opp_table);
2020 
2021 	if (!_opp_supported_by_regulators(new_opp, opp_table)) {
2022 		new_opp->available = false;
2023 		dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n",
2024 			 __func__, new_opp->rates[0]);
2025 	}
2026 
2027 	/* required-opps not fully initialized yet */
2028 	if (lazy_linking_pending(opp_table))
2029 		return 0;
2030 
2031 	_required_opps_available(new_opp, opp_table->required_opp_count);
2032 
2033 	return 0;
2034 }
2035 
2036 /**
2037  * _opp_add_v1() - Allocate a OPP based on v1 bindings.
2038  * @opp_table:	OPP table
2039  * @dev:	device for which we do this operation
2040  * @data:	The OPP data for the OPP to add
2041  * @dynamic:	Dynamically added OPPs.
2042  *
2043  * This function adds an opp definition to the opp table and returns status.
2044  * The opp is made available by default and it can be controlled using
2045  * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
2046  *
2047  * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table
2048  * and freed by dev_pm_opp_of_remove_table.
2049  *
2050  * Return:
2051  * 0		On success OR
2052  *		Duplicate OPPs (both freq and volt are same) and opp->available
2053  * -EEXIST	Freq are same and volt are different OR
2054  *		Duplicate OPPs (both freq and volt are same) and !opp->available
2055  * -ENOMEM	Memory allocation failure
2056  */
_opp_add_v1(struct opp_table * opp_table,struct device * dev,struct dev_pm_opp_data * data,bool dynamic)2057 int _opp_add_v1(struct opp_table *opp_table, struct device *dev,
2058 		struct dev_pm_opp_data *data, bool dynamic)
2059 {
2060 	struct dev_pm_opp *new_opp;
2061 	unsigned long tol, u_volt = data->u_volt;
2062 	int ret;
2063 
2064 	if (!assert_single_clk(opp_table))
2065 		return -EINVAL;
2066 
2067 	new_opp = _opp_allocate(opp_table);
2068 	if (!new_opp)
2069 		return -ENOMEM;
2070 
2071 	/* populate the opp table */
2072 	new_opp->rates[0] = data->freq;
2073 	new_opp->level = data->level;
2074 	tol = u_volt * opp_table->voltage_tolerance_v1 / 100;
2075 	new_opp->supplies[0].u_volt = u_volt;
2076 	new_opp->supplies[0].u_volt_min = u_volt - tol;
2077 	new_opp->supplies[0].u_volt_max = u_volt + tol;
2078 	new_opp->available = true;
2079 	new_opp->dynamic = dynamic;
2080 
2081 	ret = _opp_add(dev, new_opp, opp_table);
2082 	if (ret) {
2083 		/* Don't return error for duplicate OPPs */
2084 		if (ret == -EBUSY)
2085 			ret = 0;
2086 		goto free_opp;
2087 	}
2088 
2089 	/*
2090 	 * Notify the changes in the availability of the operable
2091 	 * frequency/voltage list.
2092 	 */
2093 	blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
2094 	return 0;
2095 
2096 free_opp:
2097 	_opp_free(new_opp);
2098 
2099 	return ret;
2100 }
2101 
2102 /**
2103  * _opp_set_supported_hw() - Set supported platforms
2104  * @dev: Device for which supported-hw has to be set.
2105  * @versions: Array of hierarchy of versions to match.
2106  * @count: Number of elements in the array.
2107  *
2108  * This is required only for the V2 bindings, and it enables a platform to
2109  * specify the hierarchy of versions it supports. OPP layer will then enable
2110  * OPPs, which are available for those versions, based on its 'opp-supported-hw'
2111  * property.
2112  */
_opp_set_supported_hw(struct opp_table * opp_table,const u32 * versions,unsigned int count)2113 static int _opp_set_supported_hw(struct opp_table *opp_table,
2114 				 const u32 *versions, unsigned int count)
2115 {
2116 	/* Another CPU that shares the OPP table has set the property ? */
2117 	if (opp_table->supported_hw)
2118 		return 0;
2119 
2120 	opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions),
2121 					GFP_KERNEL);
2122 	if (!opp_table->supported_hw)
2123 		return -ENOMEM;
2124 
2125 	opp_table->supported_hw_count = count;
2126 
2127 	return 0;
2128 }
2129 
2130 /**
2131  * _opp_put_supported_hw() - Releases resources blocked for supported hw
2132  * @opp_table: OPP table returned by _opp_set_supported_hw().
2133  *
2134  * This is required only for the V2 bindings, and is called for a matching
2135  * _opp_set_supported_hw(). Until this is called, the opp_table structure
2136  * will not be freed.
2137  */
_opp_put_supported_hw(struct opp_table * opp_table)2138 static void _opp_put_supported_hw(struct opp_table *opp_table)
2139 {
2140 	if (opp_table->supported_hw) {
2141 		kfree(opp_table->supported_hw);
2142 		opp_table->supported_hw = NULL;
2143 		opp_table->supported_hw_count = 0;
2144 	}
2145 }
2146 
2147 /**
2148  * _opp_set_prop_name() - Set prop-extn name
2149  * @dev: Device for which the prop-name has to be set.
2150  * @name: name to postfix to properties.
2151  *
2152  * This is required only for the V2 bindings, and it enables a platform to
2153  * specify the extn to be used for certain property names. The properties to
2154  * which the extension will apply are opp-microvolt and opp-microamp. OPP core
2155  * should postfix the property name with -<name> while looking for them.
2156  */
_opp_set_prop_name(struct opp_table * opp_table,const char * name)2157 static int _opp_set_prop_name(struct opp_table *opp_table, const char *name)
2158 {
2159 	/* Another CPU that shares the OPP table has set the property ? */
2160 	if (!opp_table->prop_name) {
2161 		opp_table->prop_name = kstrdup(name, GFP_KERNEL);
2162 		if (!opp_table->prop_name)
2163 			return -ENOMEM;
2164 	}
2165 
2166 	return 0;
2167 }
2168 
2169 /**
2170  * _opp_put_prop_name() - Releases resources blocked for prop-name
2171  * @opp_table: OPP table returned by _opp_set_prop_name().
2172  *
2173  * This is required only for the V2 bindings, and is called for a matching
2174  * _opp_set_prop_name(). Until this is called, the opp_table structure
2175  * will not be freed.
2176  */
_opp_put_prop_name(struct opp_table * opp_table)2177 static void _opp_put_prop_name(struct opp_table *opp_table)
2178 {
2179 	if (opp_table->prop_name) {
2180 		kfree(opp_table->prop_name);
2181 		opp_table->prop_name = NULL;
2182 	}
2183 }
2184 
2185 /**
2186  * _opp_set_regulators() - Set regulator names for the device
2187  * @dev: Device for which regulator name is being set.
2188  * @names: Array of pointers to the names of the regulator.
2189  * @count: Number of regulators.
2190  *
2191  * In order to support OPP switching, OPP layer needs to know the name of the
2192  * device's regulators, as the core would be required to switch voltages as
2193  * well.
2194  *
2195  * This must be called before any OPPs are initialized for the device.
2196  */
_opp_set_regulators(struct opp_table * opp_table,struct device * dev,const char * const names[])2197 static int _opp_set_regulators(struct opp_table *opp_table, struct device *dev,
2198 			       const char * const names[])
2199 {
2200 	const char * const *temp = names;
2201 	struct regulator *reg;
2202 	int count = 0, ret, i;
2203 
2204 	/* Count number of regulators */
2205 	while (*temp++)
2206 		count++;
2207 
2208 	if (!count)
2209 		return -EINVAL;
2210 
2211 	/* Another CPU that shares the OPP table has set the regulators ? */
2212 	if (opp_table->regulators)
2213 		return 0;
2214 
2215 	opp_table->regulators = kmalloc_array(count,
2216 					      sizeof(*opp_table->regulators),
2217 					      GFP_KERNEL);
2218 	if (!opp_table->regulators)
2219 		return -ENOMEM;
2220 
2221 	for (i = 0; i < count; i++) {
2222 		reg = regulator_get_optional(dev, names[i]);
2223 		if (IS_ERR(reg)) {
2224 			ret = dev_err_probe(dev, PTR_ERR(reg),
2225 					    "%s: no regulator (%s) found\n",
2226 					    __func__, names[i]);
2227 			goto free_regulators;
2228 		}
2229 
2230 		opp_table->regulators[i] = reg;
2231 	}
2232 
2233 	opp_table->regulator_count = count;
2234 
2235 	/* Set generic config_regulators() for single regulators here */
2236 	if (count == 1)
2237 		opp_table->config_regulators = _opp_config_regulator_single;
2238 
2239 	return 0;
2240 
2241 free_regulators:
2242 	while (i != 0)
2243 		regulator_put(opp_table->regulators[--i]);
2244 
2245 	kfree(opp_table->regulators);
2246 	opp_table->regulators = NULL;
2247 	opp_table->regulator_count = -1;
2248 
2249 	return ret;
2250 }
2251 
2252 /**
2253  * _opp_put_regulators() - Releases resources blocked for regulator
2254  * @opp_table: OPP table returned from _opp_set_regulators().
2255  */
_opp_put_regulators(struct opp_table * opp_table)2256 static void _opp_put_regulators(struct opp_table *opp_table)
2257 {
2258 	int i;
2259 
2260 	if (!opp_table->regulators)
2261 		return;
2262 
2263 	if (opp_table->enabled) {
2264 		for (i = opp_table->regulator_count - 1; i >= 0; i--)
2265 			regulator_disable(opp_table->regulators[i]);
2266 	}
2267 
2268 	for (i = opp_table->regulator_count - 1; i >= 0; i--)
2269 		regulator_put(opp_table->regulators[i]);
2270 
2271 	kfree(opp_table->regulators);
2272 	opp_table->regulators = NULL;
2273 	opp_table->regulator_count = -1;
2274 }
2275 
_put_clks(struct opp_table * opp_table,int count)2276 static void _put_clks(struct opp_table *opp_table, int count)
2277 {
2278 	int i;
2279 
2280 	for (i = count - 1; i >= 0; i--)
2281 		clk_put(opp_table->clks[i]);
2282 
2283 	kfree(opp_table->clks);
2284 	opp_table->clks = NULL;
2285 }
2286 
2287 /**
2288  * _opp_set_clknames() - Set clk names for the device
2289  * @dev: Device for which clk names is being set.
2290  * @names: Clk names.
2291  *
2292  * In order to support OPP switching, OPP layer needs to get pointers to the
2293  * clocks for the device. Simple cases work fine without using this routine
2294  * (i.e. by passing connection-id as NULL), but for a device with multiple
2295  * clocks available, the OPP core needs to know the exact names of the clks to
2296  * use.
2297  *
2298  * This must be called before any OPPs are initialized for the device.
2299  */
_opp_set_clknames(struct opp_table * opp_table,struct device * dev,const char * const names[],config_clks_t config_clks)2300 static int _opp_set_clknames(struct opp_table *opp_table, struct device *dev,
2301 			     const char * const names[],
2302 			     config_clks_t config_clks)
2303 {
2304 	const char * const *temp = names;
2305 	int count = 0, ret, i;
2306 	struct clk *clk;
2307 
2308 	/* Count number of clks */
2309 	while (*temp++)
2310 		count++;
2311 
2312 	/*
2313 	 * This is a special case where we have a single clock, whose connection
2314 	 * id name is NULL, i.e. first two entries are NULL in the array.
2315 	 */
2316 	if (!count && !names[1])
2317 		count = 1;
2318 
2319 	/* Fail early for invalid configurations */
2320 	if (!count || (!config_clks && count > 1))
2321 		return -EINVAL;
2322 
2323 	/* Another CPU that shares the OPP table has set the clkname ? */
2324 	if (opp_table->clks)
2325 		return 0;
2326 
2327 	opp_table->clks = kmalloc_array(count, sizeof(*opp_table->clks),
2328 					GFP_KERNEL);
2329 	if (!opp_table->clks)
2330 		return -ENOMEM;
2331 
2332 	/* Find clks for the device */
2333 	for (i = 0; i < count; i++) {
2334 		clk = clk_get(dev, names[i]);
2335 		if (IS_ERR(clk)) {
2336 			ret = dev_err_probe(dev, PTR_ERR(clk),
2337 					    "%s: Couldn't find clock with name: %s\n",
2338 					    __func__, names[i]);
2339 			goto free_clks;
2340 		}
2341 
2342 		opp_table->clks[i] = clk;
2343 	}
2344 
2345 	opp_table->clk_count = count;
2346 	opp_table->config_clks = config_clks;
2347 
2348 	/* Set generic single clk set here */
2349 	if (count == 1) {
2350 		if (!opp_table->config_clks)
2351 			opp_table->config_clks = _opp_config_clk_single;
2352 
2353 		/*
2354 		 * We could have just dropped the "clk" field and used "clks"
2355 		 * everywhere. Instead we kept the "clk" field around for
2356 		 * following reasons:
2357 		 *
2358 		 * - avoiding clks[0] everywhere else.
2359 		 * - not running single clk helpers for multiple clk usecase by
2360 		 *   mistake.
2361 		 *
2362 		 * Since this is single-clk case, just update the clk pointer
2363 		 * too.
2364 		 */
2365 		opp_table->clk = opp_table->clks[0];
2366 	}
2367 
2368 	return 0;
2369 
2370 free_clks:
2371 	_put_clks(opp_table, i);
2372 	return ret;
2373 }
2374 
2375 /**
2376  * _opp_put_clknames() - Releases resources blocked for clks.
2377  * @opp_table: OPP table returned from _opp_set_clknames().
2378  */
_opp_put_clknames(struct opp_table * opp_table)2379 static void _opp_put_clknames(struct opp_table *opp_table)
2380 {
2381 	if (!opp_table->clks)
2382 		return;
2383 
2384 	opp_table->config_clks = NULL;
2385 	opp_table->clk = ERR_PTR(-ENODEV);
2386 
2387 	_put_clks(opp_table, opp_table->clk_count);
2388 }
2389 
2390 /**
2391  * _opp_set_config_regulators_helper() - Register custom set regulator helper.
2392  * @dev: Device for which the helper is getting registered.
2393  * @config_regulators: Custom set regulator helper.
2394  *
2395  * This is useful to support platforms with multiple regulators per device.
2396  *
2397  * This must be called before any OPPs are initialized for the device.
2398  */
_opp_set_config_regulators_helper(struct opp_table * opp_table,struct device * dev,config_regulators_t config_regulators)2399 static int _opp_set_config_regulators_helper(struct opp_table *opp_table,
2400 		struct device *dev, config_regulators_t config_regulators)
2401 {
2402 	/* Another CPU that shares the OPP table has set the helper ? */
2403 	if (!opp_table->config_regulators)
2404 		opp_table->config_regulators = config_regulators;
2405 
2406 	return 0;
2407 }
2408 
2409 /**
2410  * _opp_put_config_regulators_helper() - Releases resources blocked for
2411  *					 config_regulators helper.
2412  * @opp_table: OPP table returned from _opp_set_config_regulators_helper().
2413  *
2414  * Release resources blocked for platform specific config_regulators helper.
2415  */
_opp_put_config_regulators_helper(struct opp_table * opp_table)2416 static void _opp_put_config_regulators_helper(struct opp_table *opp_table)
2417 {
2418 	if (opp_table->config_regulators)
2419 		opp_table->config_regulators = NULL;
2420 }
2421 
_detach_genpd(struct opp_table * opp_table)2422 static void _detach_genpd(struct opp_table *opp_table)
2423 {
2424 	int index;
2425 
2426 	if (!opp_table->genpd_virt_devs)
2427 		return;
2428 
2429 	for (index = 0; index < opp_table->required_opp_count; index++) {
2430 		if (!opp_table->genpd_virt_devs[index])
2431 			continue;
2432 
2433 		dev_pm_domain_detach(opp_table->genpd_virt_devs[index], false);
2434 		opp_table->genpd_virt_devs[index] = NULL;
2435 	}
2436 
2437 	kfree(opp_table->genpd_virt_devs);
2438 	opp_table->genpd_virt_devs = NULL;
2439 }
2440 
2441 /**
2442  * _opp_attach_genpd - Attach genpd(s) for the device and save virtual device pointer
2443  * @dev: Consumer device for which the genpd is getting attached.
2444  * @names: Null terminated array of pointers containing names of genpd to attach.
2445  * @virt_devs: Pointer to return the array of virtual devices.
2446  *
2447  * Multiple generic power domains for a device are supported with the help of
2448  * virtual genpd devices, which are created for each consumer device - genpd
2449  * pair. These are the device structures which are attached to the power domain
2450  * and are required by the OPP core to set the performance state of the genpd.
2451  * The same API also works for the case where single genpd is available and so
2452  * we don't need to support that separately.
2453  *
2454  * This helper will normally be called by the consumer driver of the device
2455  * "dev", as only that has details of the genpd names.
2456  *
2457  * This helper needs to be called once with a list of all genpd to attach.
2458  * Otherwise the original device structure will be used instead by the OPP core.
2459  *
2460  * The order of entries in the names array must match the order in which
2461  * "required-opps" are added in DT.
2462  */
_opp_attach_genpd(struct opp_table * opp_table,struct device * dev,const char * const * names,struct device *** virt_devs)2463 static int _opp_attach_genpd(struct opp_table *opp_table, struct device *dev,
2464 			const char * const *names, struct device ***virt_devs)
2465 {
2466 	struct device *virt_dev;
2467 	int index = 0, ret = -EINVAL;
2468 	const char * const *name = names;
2469 
2470 	if (opp_table->genpd_virt_devs)
2471 		return 0;
2472 
2473 	/*
2474 	 * If the genpd's OPP table isn't already initialized, parsing of the
2475 	 * required-opps fail for dev. We should retry this after genpd's OPP
2476 	 * table is added.
2477 	 */
2478 	if (!opp_table->required_opp_count)
2479 		return -EPROBE_DEFER;
2480 
2481 	mutex_lock(&opp_table->genpd_virt_dev_lock);
2482 
2483 	opp_table->genpd_virt_devs = kcalloc(opp_table->required_opp_count,
2484 					     sizeof(*opp_table->genpd_virt_devs),
2485 					     GFP_KERNEL);
2486 	if (!opp_table->genpd_virt_devs)
2487 		goto unlock;
2488 
2489 	while (*name) {
2490 		if (index >= opp_table->required_opp_count) {
2491 			dev_err(dev, "Index can't be greater than required-opp-count - 1, %s (%d : %d)\n",
2492 				*name, opp_table->required_opp_count, index);
2493 			goto err;
2494 		}
2495 
2496 		virt_dev = dev_pm_domain_attach_by_name(dev, *name);
2497 		if (IS_ERR_OR_NULL(virt_dev)) {
2498 			ret = virt_dev ? PTR_ERR(virt_dev) : -ENODEV;
2499 			dev_err(dev, "Couldn't attach to pm_domain: %d\n", ret);
2500 			goto err;
2501 		}
2502 
2503 		opp_table->genpd_virt_devs[index] = virt_dev;
2504 		index++;
2505 		name++;
2506 	}
2507 
2508 	if (virt_devs)
2509 		*virt_devs = opp_table->genpd_virt_devs;
2510 	mutex_unlock(&opp_table->genpd_virt_dev_lock);
2511 
2512 	return 0;
2513 
2514 err:
2515 	_detach_genpd(opp_table);
2516 unlock:
2517 	mutex_unlock(&opp_table->genpd_virt_dev_lock);
2518 	return ret;
2519 
2520 }
2521 
2522 /**
2523  * _opp_detach_genpd() - Detach genpd(s) from the device.
2524  * @opp_table: OPP table returned by _opp_attach_genpd().
2525  *
2526  * This detaches the genpd(s), resets the virtual device pointers, and puts the
2527  * OPP table.
2528  */
_opp_detach_genpd(struct opp_table * opp_table)2529 static void _opp_detach_genpd(struct opp_table *opp_table)
2530 {
2531 	/*
2532 	 * Acquire genpd_virt_dev_lock to make sure virt_dev isn't getting
2533 	 * used in parallel.
2534 	 */
2535 	mutex_lock(&opp_table->genpd_virt_dev_lock);
2536 	_detach_genpd(opp_table);
2537 	mutex_unlock(&opp_table->genpd_virt_dev_lock);
2538 }
2539 
_opp_clear_config(struct opp_config_data * data)2540 static void _opp_clear_config(struct opp_config_data *data)
2541 {
2542 	if (data->flags & OPP_CONFIG_GENPD)
2543 		_opp_detach_genpd(data->opp_table);
2544 	if (data->flags & OPP_CONFIG_REGULATOR)
2545 		_opp_put_regulators(data->opp_table);
2546 	if (data->flags & OPP_CONFIG_SUPPORTED_HW)
2547 		_opp_put_supported_hw(data->opp_table);
2548 	if (data->flags & OPP_CONFIG_REGULATOR_HELPER)
2549 		_opp_put_config_regulators_helper(data->opp_table);
2550 	if (data->flags & OPP_CONFIG_PROP_NAME)
2551 		_opp_put_prop_name(data->opp_table);
2552 	if (data->flags & OPP_CONFIG_CLK)
2553 		_opp_put_clknames(data->opp_table);
2554 
2555 	dev_pm_opp_put_opp_table(data->opp_table);
2556 	kfree(data);
2557 }
2558 
2559 /**
2560  * dev_pm_opp_set_config() - Set OPP configuration for the device.
2561  * @dev: Device for which configuration is being set.
2562  * @config: OPP configuration.
2563  *
2564  * This allows all device OPP configurations to be performed at once.
2565  *
2566  * This must be called before any OPPs are initialized for the device. This may
2567  * be called multiple times for the same OPP table, for example once for each
2568  * CPU that share the same table. This must be balanced by the same number of
2569  * calls to dev_pm_opp_clear_config() in order to free the OPP table properly.
2570  *
2571  * This returns a token to the caller, which must be passed to
2572  * dev_pm_opp_clear_config() to free the resources later. The value of the
2573  * returned token will be >= 1 for success and negative for errors. The minimum
2574  * value of 1 is chosen here to make it easy for callers to manage the resource.
2575  */
dev_pm_opp_set_config(struct device * dev,struct dev_pm_opp_config * config)2576 int dev_pm_opp_set_config(struct device *dev, struct dev_pm_opp_config *config)
2577 {
2578 	struct opp_table *opp_table;
2579 	struct opp_config_data *data;
2580 	unsigned int id;
2581 	int ret;
2582 
2583 	data = kmalloc(sizeof(*data), GFP_KERNEL);
2584 	if (!data)
2585 		return -ENOMEM;
2586 
2587 	opp_table = _add_opp_table(dev, false);
2588 	if (IS_ERR(opp_table)) {
2589 		kfree(data);
2590 		return PTR_ERR(opp_table);
2591 	}
2592 
2593 	data->opp_table = opp_table;
2594 	data->flags = 0;
2595 
2596 	/* This should be called before OPPs are initialized */
2597 	if (WARN_ON(!list_empty(&opp_table->opp_list))) {
2598 		ret = -EBUSY;
2599 		goto err;
2600 	}
2601 
2602 	/* Configure clocks */
2603 	if (config->clk_names) {
2604 		ret = _opp_set_clknames(opp_table, dev, config->clk_names,
2605 					config->config_clks);
2606 		if (ret)
2607 			goto err;
2608 
2609 		data->flags |= OPP_CONFIG_CLK;
2610 	} else if (config->config_clks) {
2611 		/* Don't allow config callback without clocks */
2612 		ret = -EINVAL;
2613 		goto err;
2614 	}
2615 
2616 	/* Configure property names */
2617 	if (config->prop_name) {
2618 		ret = _opp_set_prop_name(opp_table, config->prop_name);
2619 		if (ret)
2620 			goto err;
2621 
2622 		data->flags |= OPP_CONFIG_PROP_NAME;
2623 	}
2624 
2625 	/* Configure config_regulators helper */
2626 	if (config->config_regulators) {
2627 		ret = _opp_set_config_regulators_helper(opp_table, dev,
2628 						config->config_regulators);
2629 		if (ret)
2630 			goto err;
2631 
2632 		data->flags |= OPP_CONFIG_REGULATOR_HELPER;
2633 	}
2634 
2635 	/* Configure supported hardware */
2636 	if (config->supported_hw) {
2637 		ret = _opp_set_supported_hw(opp_table, config->supported_hw,
2638 					    config->supported_hw_count);
2639 		if (ret)
2640 			goto err;
2641 
2642 		data->flags |= OPP_CONFIG_SUPPORTED_HW;
2643 	}
2644 
2645 	/* Configure supplies */
2646 	if (config->regulator_names) {
2647 		ret = _opp_set_regulators(opp_table, dev,
2648 					  config->regulator_names);
2649 		if (ret)
2650 			goto err;
2651 
2652 		data->flags |= OPP_CONFIG_REGULATOR;
2653 	}
2654 
2655 	/* Attach genpds */
2656 	if (config->genpd_names) {
2657 		ret = _opp_attach_genpd(opp_table, dev, config->genpd_names,
2658 					config->virt_devs);
2659 		if (ret)
2660 			goto err;
2661 
2662 		data->flags |= OPP_CONFIG_GENPD;
2663 	}
2664 
2665 	ret = xa_alloc(&opp_configs, &id, data, XA_LIMIT(1, INT_MAX),
2666 		       GFP_KERNEL);
2667 	if (ret)
2668 		goto err;
2669 
2670 	return id;
2671 
2672 err:
2673 	_opp_clear_config(data);
2674 	return ret;
2675 }
2676 EXPORT_SYMBOL_GPL(dev_pm_opp_set_config);
2677 
2678 /**
2679  * dev_pm_opp_clear_config() - Releases resources blocked for OPP configuration.
2680  * @opp_table: OPP table returned from dev_pm_opp_set_config().
2681  *
2682  * This allows all device OPP configurations to be cleared at once. This must be
2683  * called once for each call made to dev_pm_opp_set_config(), in order to free
2684  * the OPPs properly.
2685  *
2686  * Currently the first call itself ends up freeing all the OPP configurations,
2687  * while the later ones only drop the OPP table reference. This works well for
2688  * now as we would never want to use an half initialized OPP table and want to
2689  * remove the configurations together.
2690  */
dev_pm_opp_clear_config(int token)2691 void dev_pm_opp_clear_config(int token)
2692 {
2693 	struct opp_config_data *data;
2694 
2695 	/*
2696 	 * This lets the callers call this unconditionally and keep their code
2697 	 * simple.
2698 	 */
2699 	if (unlikely(token <= 0))
2700 		return;
2701 
2702 	data = xa_erase(&opp_configs, token);
2703 	if (WARN_ON(!data))
2704 		return;
2705 
2706 	_opp_clear_config(data);
2707 }
2708 EXPORT_SYMBOL_GPL(dev_pm_opp_clear_config);
2709 
devm_pm_opp_config_release(void * token)2710 static void devm_pm_opp_config_release(void *token)
2711 {
2712 	dev_pm_opp_clear_config((unsigned long)token);
2713 }
2714 
2715 /**
2716  * devm_pm_opp_set_config() - Set OPP configuration for the device.
2717  * @dev: Device for which configuration is being set.
2718  * @config: OPP configuration.
2719  *
2720  * This allows all device OPP configurations to be performed at once.
2721  * This is a resource-managed variant of dev_pm_opp_set_config().
2722  *
2723  * Return: 0 on success and errorno otherwise.
2724  */
devm_pm_opp_set_config(struct device * dev,struct dev_pm_opp_config * config)2725 int devm_pm_opp_set_config(struct device *dev, struct dev_pm_opp_config *config)
2726 {
2727 	int token = dev_pm_opp_set_config(dev, config);
2728 
2729 	if (token < 0)
2730 		return token;
2731 
2732 	return devm_add_action_or_reset(dev, devm_pm_opp_config_release,
2733 					(void *) ((unsigned long) token));
2734 }
2735 EXPORT_SYMBOL_GPL(devm_pm_opp_set_config);
2736 
2737 /**
2738  * dev_pm_opp_xlate_required_opp() - Find required OPP for @src_table OPP.
2739  * @src_table: OPP table which has @dst_table as one of its required OPP table.
2740  * @dst_table: Required OPP table of the @src_table.
2741  * @src_opp: OPP from the @src_table.
2742  *
2743  * This function returns the OPP (present in @dst_table) pointed out by the
2744  * "required-opps" property of the @src_opp (present in @src_table).
2745  *
2746  * The callers are required to call dev_pm_opp_put() for the returned OPP after
2747  * use.
2748  *
2749  * Return: pointer to 'struct dev_pm_opp' on success and errorno otherwise.
2750  */
dev_pm_opp_xlate_required_opp(struct opp_table * src_table,struct opp_table * dst_table,struct dev_pm_opp * src_opp)2751 struct dev_pm_opp *dev_pm_opp_xlate_required_opp(struct opp_table *src_table,
2752 						 struct opp_table *dst_table,
2753 						 struct dev_pm_opp *src_opp)
2754 {
2755 	struct dev_pm_opp *opp, *dest_opp = ERR_PTR(-ENODEV);
2756 	int i;
2757 
2758 	if (!src_table || !dst_table || !src_opp ||
2759 	    !src_table->required_opp_tables)
2760 		return ERR_PTR(-EINVAL);
2761 
2762 	/* required-opps not fully initialized yet */
2763 	if (lazy_linking_pending(src_table))
2764 		return ERR_PTR(-EBUSY);
2765 
2766 	for (i = 0; i < src_table->required_opp_count; i++) {
2767 		if (src_table->required_opp_tables[i] == dst_table) {
2768 			mutex_lock(&src_table->lock);
2769 
2770 			list_for_each_entry(opp, &src_table->opp_list, node) {
2771 				if (opp == src_opp) {
2772 					dest_opp = opp->required_opps[i];
2773 					dev_pm_opp_get(dest_opp);
2774 					break;
2775 				}
2776 			}
2777 
2778 			mutex_unlock(&src_table->lock);
2779 			break;
2780 		}
2781 	}
2782 
2783 	if (IS_ERR(dest_opp)) {
2784 		pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__,
2785 		       src_table, dst_table);
2786 	}
2787 
2788 	return dest_opp;
2789 }
2790 EXPORT_SYMBOL_GPL(dev_pm_opp_xlate_required_opp);
2791 
2792 /**
2793  * dev_pm_opp_xlate_performance_state() - Find required OPP's pstate for src_table.
2794  * @src_table: OPP table which has dst_table as one of its required OPP table.
2795  * @dst_table: Required OPP table of the src_table.
2796  * @pstate: Current performance state of the src_table.
2797  *
2798  * This Returns pstate of the OPP (present in @dst_table) pointed out by the
2799  * "required-opps" property of the OPP (present in @src_table) which has
2800  * performance state set to @pstate.
2801  *
2802  * Return: Zero or positive performance state on success, otherwise negative
2803  * value on errors.
2804  */
dev_pm_opp_xlate_performance_state(struct opp_table * src_table,struct opp_table * dst_table,unsigned int pstate)2805 int dev_pm_opp_xlate_performance_state(struct opp_table *src_table,
2806 				       struct opp_table *dst_table,
2807 				       unsigned int pstate)
2808 {
2809 	struct dev_pm_opp *opp;
2810 	int dest_pstate = -EINVAL;
2811 	int i;
2812 
2813 	/*
2814 	 * Normally the src_table will have the "required_opps" property set to
2815 	 * point to one of the OPPs in the dst_table, but in some cases the
2816 	 * genpd and its master have one to one mapping of performance states
2817 	 * and so none of them have the "required-opps" property set. Return the
2818 	 * pstate of the src_table as it is in such cases.
2819 	 */
2820 	if (!src_table || !src_table->required_opp_count)
2821 		return pstate;
2822 
2823 	/* Both OPP tables must belong to genpds */
2824 	if (unlikely(!src_table->is_genpd || !dst_table->is_genpd)) {
2825 		pr_err("%s: Performance state is only valid for genpds.\n", __func__);
2826 		return -EINVAL;
2827 	}
2828 
2829 	/* required-opps not fully initialized yet */
2830 	if (lazy_linking_pending(src_table))
2831 		return -EBUSY;
2832 
2833 	for (i = 0; i < src_table->required_opp_count; i++) {
2834 		if (src_table->required_opp_tables[i]->np == dst_table->np)
2835 			break;
2836 	}
2837 
2838 	if (unlikely(i == src_table->required_opp_count)) {
2839 		pr_err("%s: Couldn't find matching OPP table (%p: %p)\n",
2840 		       __func__, src_table, dst_table);
2841 		return -EINVAL;
2842 	}
2843 
2844 	mutex_lock(&src_table->lock);
2845 
2846 	list_for_each_entry(opp, &src_table->opp_list, node) {
2847 		if (opp->level == pstate) {
2848 			dest_pstate = opp->required_opps[i]->level;
2849 			goto unlock;
2850 		}
2851 	}
2852 
2853 	pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__, src_table,
2854 	       dst_table);
2855 
2856 unlock:
2857 	mutex_unlock(&src_table->lock);
2858 
2859 	return dest_pstate;
2860 }
2861 
2862 /**
2863  * dev_pm_opp_add_dynamic()  - Add an OPP table from a table definitions
2864  * @dev:	The device for which we do this operation
2865  * @data:	The OPP data for the OPP to add
2866  *
2867  * This function adds an opp definition to the opp table and returns status.
2868  * The opp is made available by default and it can be controlled using
2869  * dev_pm_opp_enable/disable functions.
2870  *
2871  * Return:
2872  * 0		On success OR
2873  *		Duplicate OPPs (both freq and volt are same) and opp->available
2874  * -EEXIST	Freq are same and volt are different OR
2875  *		Duplicate OPPs (both freq and volt are same) and !opp->available
2876  * -ENOMEM	Memory allocation failure
2877  */
dev_pm_opp_add_dynamic(struct device * dev,struct dev_pm_opp_data * data)2878 int dev_pm_opp_add_dynamic(struct device *dev, struct dev_pm_opp_data *data)
2879 {
2880 	struct opp_table *opp_table;
2881 	int ret;
2882 
2883 	opp_table = _add_opp_table(dev, true);
2884 	if (IS_ERR(opp_table))
2885 		return PTR_ERR(opp_table);
2886 
2887 	/* Fix regulator count for dynamic OPPs */
2888 	opp_table->regulator_count = 1;
2889 
2890 	ret = _opp_add_v1(opp_table, dev, data, true);
2891 	if (ret)
2892 		dev_pm_opp_put_opp_table(opp_table);
2893 
2894 	return ret;
2895 }
2896 EXPORT_SYMBOL_GPL(dev_pm_opp_add_dynamic);
2897 
2898 /**
2899  * _opp_set_availability() - helper to set the availability of an opp
2900  * @dev:		device for which we do this operation
2901  * @freq:		OPP frequency to modify availability
2902  * @availability_req:	availability status requested for this opp
2903  *
2904  * Set the availability of an OPP, opp_{enable,disable} share a common logic
2905  * which is isolated here.
2906  *
2907  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
2908  * copy operation, returns 0 if no modification was done OR modification was
2909  * successful.
2910  */
_opp_set_availability(struct device * dev,unsigned long freq,bool availability_req)2911 static int _opp_set_availability(struct device *dev, unsigned long freq,
2912 				 bool availability_req)
2913 {
2914 	struct opp_table *opp_table;
2915 	struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
2916 	int r = 0;
2917 
2918 	/* Find the opp_table */
2919 	opp_table = _find_opp_table(dev);
2920 	if (IS_ERR(opp_table)) {
2921 		r = PTR_ERR(opp_table);
2922 		dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
2923 		return r;
2924 	}
2925 
2926 	if (!assert_single_clk(opp_table)) {
2927 		r = -EINVAL;
2928 		goto put_table;
2929 	}
2930 
2931 	mutex_lock(&opp_table->lock);
2932 
2933 	/* Do we have the frequency? */
2934 	list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
2935 		if (tmp_opp->rates[0] == freq) {
2936 			opp = tmp_opp;
2937 			break;
2938 		}
2939 	}
2940 
2941 	if (IS_ERR(opp)) {
2942 		r = PTR_ERR(opp);
2943 		goto unlock;
2944 	}
2945 
2946 	/* Is update really needed? */
2947 	if (opp->available == availability_req)
2948 		goto unlock;
2949 
2950 	opp->available = availability_req;
2951 
2952 	dev_pm_opp_get(opp);
2953 	mutex_unlock(&opp_table->lock);
2954 
2955 	/* Notify the change of the OPP availability */
2956 	if (availability_req)
2957 		blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE,
2958 					     opp);
2959 	else
2960 		blocking_notifier_call_chain(&opp_table->head,
2961 					     OPP_EVENT_DISABLE, opp);
2962 
2963 	dev_pm_opp_put(opp);
2964 	goto put_table;
2965 
2966 unlock:
2967 	mutex_unlock(&opp_table->lock);
2968 put_table:
2969 	dev_pm_opp_put_opp_table(opp_table);
2970 	return r;
2971 }
2972 
2973 /**
2974  * dev_pm_opp_adjust_voltage() - helper to change the voltage of an OPP
2975  * @dev:		device for which we do this operation
2976  * @freq:		OPP frequency to adjust voltage of
2977  * @u_volt:		new OPP target voltage
2978  * @u_volt_min:		new OPP min voltage
2979  * @u_volt_max:		new OPP max voltage
2980  *
2981  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
2982  * copy operation, returns 0 if no modifcation was done OR modification was
2983  * successful.
2984  */
dev_pm_opp_adjust_voltage(struct device * dev,unsigned long freq,unsigned long u_volt,unsigned long u_volt_min,unsigned long u_volt_max)2985 int dev_pm_opp_adjust_voltage(struct device *dev, unsigned long freq,
2986 			      unsigned long u_volt, unsigned long u_volt_min,
2987 			      unsigned long u_volt_max)
2988 
2989 {
2990 	struct opp_table *opp_table;
2991 	struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
2992 	int r = 0;
2993 
2994 	/* Find the opp_table */
2995 	opp_table = _find_opp_table(dev);
2996 	if (IS_ERR(opp_table)) {
2997 		r = PTR_ERR(opp_table);
2998 		dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
2999 		return r;
3000 	}
3001 
3002 	if (!assert_single_clk(opp_table)) {
3003 		r = -EINVAL;
3004 		goto put_table;
3005 	}
3006 
3007 	mutex_lock(&opp_table->lock);
3008 
3009 	/* Do we have the frequency? */
3010 	list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
3011 		if (tmp_opp->rates[0] == freq) {
3012 			opp = tmp_opp;
3013 			break;
3014 		}
3015 	}
3016 
3017 	if (IS_ERR(opp)) {
3018 		r = PTR_ERR(opp);
3019 		goto adjust_unlock;
3020 	}
3021 
3022 	/* Is update really needed? */
3023 	if (opp->supplies->u_volt == u_volt)
3024 		goto adjust_unlock;
3025 
3026 	opp->supplies->u_volt = u_volt;
3027 	opp->supplies->u_volt_min = u_volt_min;
3028 	opp->supplies->u_volt_max = u_volt_max;
3029 
3030 	dev_pm_opp_get(opp);
3031 	mutex_unlock(&opp_table->lock);
3032 
3033 	/* Notify the voltage change of the OPP */
3034 	blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADJUST_VOLTAGE,
3035 				     opp);
3036 
3037 	dev_pm_opp_put(opp);
3038 	goto put_table;
3039 
3040 adjust_unlock:
3041 	mutex_unlock(&opp_table->lock);
3042 put_table:
3043 	dev_pm_opp_put_opp_table(opp_table);
3044 	return r;
3045 }
3046 EXPORT_SYMBOL_GPL(dev_pm_opp_adjust_voltage);
3047 
3048 /**
3049  * dev_pm_opp_enable() - Enable a specific OPP
3050  * @dev:	device for which we do this operation
3051  * @freq:	OPP frequency to enable
3052  *
3053  * Enables a provided opp. If the operation is valid, this returns 0, else the
3054  * corresponding error value. It is meant to be used for users an OPP available
3055  * after being temporarily made unavailable with dev_pm_opp_disable.
3056  *
3057  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
3058  * copy operation, returns 0 if no modification was done OR modification was
3059  * successful.
3060  */
dev_pm_opp_enable(struct device * dev,unsigned long freq)3061 int dev_pm_opp_enable(struct device *dev, unsigned long freq)
3062 {
3063 	return _opp_set_availability(dev, freq, true);
3064 }
3065 EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
3066 
3067 /**
3068  * dev_pm_opp_disable() - Disable a specific OPP
3069  * @dev:	device for which we do this operation
3070  * @freq:	OPP frequency to disable
3071  *
3072  * Disables a provided opp. If the operation is valid, this returns
3073  * 0, else the corresponding error value. It is meant to be a temporary
3074  * control by users to make this OPP not available until the circumstances are
3075  * right to make it available again (with a call to dev_pm_opp_enable).
3076  *
3077  * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
3078  * copy operation, returns 0 if no modification was done OR modification was
3079  * successful.
3080  */
dev_pm_opp_disable(struct device * dev,unsigned long freq)3081 int dev_pm_opp_disable(struct device *dev, unsigned long freq)
3082 {
3083 	return _opp_set_availability(dev, freq, false);
3084 }
3085 EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
3086 
3087 /**
3088  * dev_pm_opp_register_notifier() - Register OPP notifier for the device
3089  * @dev:	Device for which notifier needs to be registered
3090  * @nb:		Notifier block to be registered
3091  *
3092  * Return: 0 on success or a negative error value.
3093  */
dev_pm_opp_register_notifier(struct device * dev,struct notifier_block * nb)3094 int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb)
3095 {
3096 	struct opp_table *opp_table;
3097 	int ret;
3098 
3099 	opp_table = _find_opp_table(dev);
3100 	if (IS_ERR(opp_table))
3101 		return PTR_ERR(opp_table);
3102 
3103 	ret = blocking_notifier_chain_register(&opp_table->head, nb);
3104 
3105 	dev_pm_opp_put_opp_table(opp_table);
3106 
3107 	return ret;
3108 }
3109 EXPORT_SYMBOL(dev_pm_opp_register_notifier);
3110 
3111 /**
3112  * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device
3113  * @dev:	Device for which notifier needs to be unregistered
3114  * @nb:		Notifier block to be unregistered
3115  *
3116  * Return: 0 on success or a negative error value.
3117  */
dev_pm_opp_unregister_notifier(struct device * dev,struct notifier_block * nb)3118 int dev_pm_opp_unregister_notifier(struct device *dev,
3119 				   struct notifier_block *nb)
3120 {
3121 	struct opp_table *opp_table;
3122 	int ret;
3123 
3124 	opp_table = _find_opp_table(dev);
3125 	if (IS_ERR(opp_table))
3126 		return PTR_ERR(opp_table);
3127 
3128 	ret = blocking_notifier_chain_unregister(&opp_table->head, nb);
3129 
3130 	dev_pm_opp_put_opp_table(opp_table);
3131 
3132 	return ret;
3133 }
3134 EXPORT_SYMBOL(dev_pm_opp_unregister_notifier);
3135 
3136 /**
3137  * dev_pm_opp_remove_table() - Free all OPPs associated with the device
3138  * @dev:	device pointer used to lookup OPP table.
3139  *
3140  * Free both OPPs created using static entries present in DT and the
3141  * dynamically added entries.
3142  */
dev_pm_opp_remove_table(struct device * dev)3143 void dev_pm_opp_remove_table(struct device *dev)
3144 {
3145 	struct opp_table *opp_table;
3146 
3147 	/* Check for existing table for 'dev' */
3148 	opp_table = _find_opp_table(dev);
3149 	if (IS_ERR(opp_table)) {
3150 		int error = PTR_ERR(opp_table);
3151 
3152 		if (error != -ENODEV)
3153 			WARN(1, "%s: opp_table: %d\n",
3154 			     IS_ERR_OR_NULL(dev) ?
3155 					"Invalid device" : dev_name(dev),
3156 			     error);
3157 		return;
3158 	}
3159 
3160 	/*
3161 	 * Drop the extra reference only if the OPP table was successfully added
3162 	 * with dev_pm_opp_of_add_table() earlier.
3163 	 **/
3164 	if (_opp_remove_all_static(opp_table))
3165 		dev_pm_opp_put_opp_table(opp_table);
3166 
3167 	/* Drop reference taken by _find_opp_table() */
3168 	dev_pm_opp_put_opp_table(opp_table);
3169 }
3170 EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table);
3171 
3172 /**
3173  * dev_pm_opp_sync_regulators() - Sync state of voltage regulators
3174  * @dev:	device for which we do this operation
3175  *
3176  * Sync voltage state of the OPP table regulators.
3177  *
3178  * Return: 0 on success or a negative error value.
3179  */
dev_pm_opp_sync_regulators(struct device * dev)3180 int dev_pm_opp_sync_regulators(struct device *dev)
3181 {
3182 	struct opp_table *opp_table;
3183 	struct regulator *reg;
3184 	int i, ret = 0;
3185 
3186 	/* Device may not have OPP table */
3187 	opp_table = _find_opp_table(dev);
3188 	if (IS_ERR(opp_table))
3189 		return 0;
3190 
3191 	/* Regulator may not be required for the device */
3192 	if (unlikely(!opp_table->regulators))
3193 		goto put_table;
3194 
3195 	/* Nothing to sync if voltage wasn't changed */
3196 	if (!opp_table->enabled)
3197 		goto put_table;
3198 
3199 	for (i = 0; i < opp_table->regulator_count; i++) {
3200 		reg = opp_table->regulators[i];
3201 		ret = regulator_sync_voltage(reg);
3202 		if (ret)
3203 			break;
3204 	}
3205 put_table:
3206 	/* Drop reference taken by _find_opp_table() */
3207 	dev_pm_opp_put_opp_table(opp_table);
3208 
3209 	return ret;
3210 }
3211 EXPORT_SYMBOL_GPL(dev_pm_opp_sync_regulators);
3212