1 /*
2 * Generic OPP Interface
3 *
4 * Copyright (C) 2009-2010 Texas Instruments Incorporated.
5 * Nishanth Menon
6 * Romit Dasgupta
7 * Kevin Hilman
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
16 #include <linux/clk.h>
17 #include <linux/errno.h>
18 #include <linux/err.h>
19 #include <linux/slab.h>
20 #include <linux/device.h>
21 #include <linux/export.h>
22 #include <linux/pm_domain.h>
23 #include <linux/regulator/consumer.h>
24
25 #include "opp.h"
26
27 /*
28 * The root of the list of all opp-tables. All opp_table structures branch off
29 * from here, with each opp_table containing the list of opps it supports in
30 * various states of availability.
31 */
32 LIST_HEAD(opp_tables);
33 /* Lock to allow exclusive modification to the device and opp lists */
34 DEFINE_MUTEX(opp_table_lock);
35
_find_opp_dev(const struct device * dev,struct opp_table * opp_table)36 static struct opp_device *_find_opp_dev(const struct device *dev,
37 struct opp_table *opp_table)
38 {
39 struct opp_device *opp_dev;
40
41 list_for_each_entry(opp_dev, &opp_table->dev_list, node)
42 if (opp_dev->dev == dev)
43 return opp_dev;
44
45 return NULL;
46 }
47
_find_opp_table_unlocked(struct device * dev)48 static struct opp_table *_find_opp_table_unlocked(struct device *dev)
49 {
50 struct opp_table *opp_table;
51
52 list_for_each_entry(opp_table, &opp_tables, node) {
53 if (_find_opp_dev(dev, opp_table)) {
54 _get_opp_table_kref(opp_table);
55
56 return opp_table;
57 }
58 }
59
60 return ERR_PTR(-ENODEV);
61 }
62
63 /**
64 * _find_opp_table() - find opp_table struct using device pointer
65 * @dev: device pointer used to lookup OPP table
66 *
67 * Search OPP table for one containing matching device.
68 *
69 * Return: pointer to 'struct opp_table' if found, otherwise -ENODEV or
70 * -EINVAL based on type of error.
71 *
72 * The callers must call dev_pm_opp_put_opp_table() after the table is used.
73 */
_find_opp_table(struct device * dev)74 struct opp_table *_find_opp_table(struct device *dev)
75 {
76 struct opp_table *opp_table;
77
78 if (IS_ERR_OR_NULL(dev)) {
79 pr_err("%s: Invalid parameters\n", __func__);
80 return ERR_PTR(-EINVAL);
81 }
82
83 mutex_lock(&opp_table_lock);
84 opp_table = _find_opp_table_unlocked(dev);
85 mutex_unlock(&opp_table_lock);
86
87 return opp_table;
88 }
89
90 /**
91 * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an opp
92 * @opp: opp for which voltage has to be returned for
93 *
94 * Return: voltage in micro volt corresponding to the opp, else
95 * return 0
96 *
97 * This is useful only for devices with single power supply.
98 */
dev_pm_opp_get_voltage(struct dev_pm_opp * opp)99 unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp)
100 {
101 if (IS_ERR_OR_NULL(opp)) {
102 pr_err("%s: Invalid parameters\n", __func__);
103 return 0;
104 }
105
106 return opp->supplies[0].u_volt;
107 }
108 EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage);
109
110 /**
111 * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp
112 * @opp: opp for which frequency has to be returned for
113 *
114 * Return: frequency in hertz corresponding to the opp, else
115 * return 0
116 */
dev_pm_opp_get_freq(struct dev_pm_opp * opp)117 unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp)
118 {
119 if (IS_ERR_OR_NULL(opp) || !opp->available) {
120 pr_err("%s: Invalid parameters\n", __func__);
121 return 0;
122 }
123
124 return opp->rate;
125 }
126 EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq);
127
128 /**
129 * dev_pm_opp_is_turbo() - Returns if opp is turbo OPP or not
130 * @opp: opp for which turbo mode is being verified
131 *
132 * Turbo OPPs are not for normal use, and can be enabled (under certain
133 * conditions) for short duration of times to finish high throughput work
134 * quickly. Running on them for longer times may overheat the chip.
135 *
136 * Return: true if opp is turbo opp, else false.
137 */
dev_pm_opp_is_turbo(struct dev_pm_opp * opp)138 bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp)
139 {
140 if (IS_ERR_OR_NULL(opp) || !opp->available) {
141 pr_err("%s: Invalid parameters\n", __func__);
142 return false;
143 }
144
145 return opp->turbo;
146 }
147 EXPORT_SYMBOL_GPL(dev_pm_opp_is_turbo);
148
149 /**
150 * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds
151 * @dev: device for which we do this operation
152 *
153 * Return: This function returns the max clock latency in nanoseconds.
154 */
dev_pm_opp_get_max_clock_latency(struct device * dev)155 unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev)
156 {
157 struct opp_table *opp_table;
158 unsigned long clock_latency_ns;
159
160 opp_table = _find_opp_table(dev);
161 if (IS_ERR(opp_table))
162 return 0;
163
164 clock_latency_ns = opp_table->clock_latency_ns_max;
165
166 dev_pm_opp_put_opp_table(opp_table);
167
168 return clock_latency_ns;
169 }
170 EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency);
171
172 /**
173 * dev_pm_opp_get_max_volt_latency() - Get max voltage latency in nanoseconds
174 * @dev: device for which we do this operation
175 *
176 * Return: This function returns the max voltage latency in nanoseconds.
177 */
dev_pm_opp_get_max_volt_latency(struct device * dev)178 unsigned long dev_pm_opp_get_max_volt_latency(struct device *dev)
179 {
180 struct opp_table *opp_table;
181 struct dev_pm_opp *opp;
182 struct regulator *reg;
183 unsigned long latency_ns = 0;
184 int ret, i, count;
185 struct {
186 unsigned long min;
187 unsigned long max;
188 } *uV;
189
190 opp_table = _find_opp_table(dev);
191 if (IS_ERR(opp_table))
192 return 0;
193
194 /* Regulator may not be required for the device */
195 if (!opp_table->regulators)
196 goto put_opp_table;
197
198 count = opp_table->regulator_count;
199
200 uV = kmalloc_array(count, sizeof(*uV), GFP_KERNEL);
201 if (!uV)
202 goto put_opp_table;
203
204 mutex_lock(&opp_table->lock);
205
206 for (i = 0; i < count; i++) {
207 uV[i].min = ~0;
208 uV[i].max = 0;
209
210 list_for_each_entry(opp, &opp_table->opp_list, node) {
211 if (!opp->available)
212 continue;
213
214 if (opp->supplies[i].u_volt_min < uV[i].min)
215 uV[i].min = opp->supplies[i].u_volt_min;
216 if (opp->supplies[i].u_volt_max > uV[i].max)
217 uV[i].max = opp->supplies[i].u_volt_max;
218 }
219 }
220
221 mutex_unlock(&opp_table->lock);
222
223 /*
224 * The caller needs to ensure that opp_table (and hence the regulator)
225 * isn't freed, while we are executing this routine.
226 */
227 for (i = 0; i < count; i++) {
228 reg = opp_table->regulators[i];
229 ret = regulator_set_voltage_time(reg, uV[i].min, uV[i].max);
230 if (ret > 0)
231 latency_ns += ret * 1000;
232 }
233
234 kfree(uV);
235 put_opp_table:
236 dev_pm_opp_put_opp_table(opp_table);
237
238 return latency_ns;
239 }
240 EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_volt_latency);
241
242 /**
243 * dev_pm_opp_get_max_transition_latency() - Get max transition latency in
244 * nanoseconds
245 * @dev: device for which we do this operation
246 *
247 * Return: This function returns the max transition latency, in nanoseconds, to
248 * switch from one OPP to other.
249 */
dev_pm_opp_get_max_transition_latency(struct device * dev)250 unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev)
251 {
252 return dev_pm_opp_get_max_volt_latency(dev) +
253 dev_pm_opp_get_max_clock_latency(dev);
254 }
255 EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_transition_latency);
256
257 /**
258 * dev_pm_opp_get_suspend_opp_freq() - Get frequency of suspend opp in Hz
259 * @dev: device for which we do this operation
260 *
261 * Return: This function returns the frequency of the OPP marked as suspend_opp
262 * if one is available, else returns 0;
263 */
dev_pm_opp_get_suspend_opp_freq(struct device * dev)264 unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev)
265 {
266 struct opp_table *opp_table;
267 unsigned long freq = 0;
268
269 opp_table = _find_opp_table(dev);
270 if (IS_ERR(opp_table))
271 return 0;
272
273 if (opp_table->suspend_opp && opp_table->suspend_opp->available)
274 freq = dev_pm_opp_get_freq(opp_table->suspend_opp);
275
276 dev_pm_opp_put_opp_table(opp_table);
277
278 return freq;
279 }
280 EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp_freq);
281
_get_opp_count(struct opp_table * opp_table)282 int _get_opp_count(struct opp_table *opp_table)
283 {
284 struct dev_pm_opp *opp;
285 int count = 0;
286
287 mutex_lock(&opp_table->lock);
288
289 list_for_each_entry(opp, &opp_table->opp_list, node) {
290 if (opp->available)
291 count++;
292 }
293
294 mutex_unlock(&opp_table->lock);
295
296 return count;
297 }
298
299 /**
300 * dev_pm_opp_get_opp_count() - Get number of opps available in the opp table
301 * @dev: device for which we do this operation
302 *
303 * Return: This function returns the number of available opps if there are any,
304 * else returns 0 if none or the corresponding error value.
305 */
dev_pm_opp_get_opp_count(struct device * dev)306 int dev_pm_opp_get_opp_count(struct device *dev)
307 {
308 struct opp_table *opp_table;
309 int count;
310
311 opp_table = _find_opp_table(dev);
312 if (IS_ERR(opp_table)) {
313 count = PTR_ERR(opp_table);
314 dev_dbg(dev, "%s: OPP table not found (%d)\n",
315 __func__, count);
316 return count;
317 }
318
319 count = _get_opp_count(opp_table);
320 dev_pm_opp_put_opp_table(opp_table);
321
322 return count;
323 }
324 EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
325
326 /**
327 * dev_pm_opp_find_freq_exact() - search for an exact frequency
328 * @dev: device for which we do this operation
329 * @freq: frequency to search for
330 * @available: true/false - match for available opp
331 *
332 * Return: Searches for exact match in the opp table and returns pointer to the
333 * matching opp if found, else returns ERR_PTR in case of error and should
334 * be handled using IS_ERR. Error return values can be:
335 * EINVAL: for bad pointer
336 * ERANGE: no match found for search
337 * ENODEV: if device not found in list of registered devices
338 *
339 * Note: available is a modifier for the search. if available=true, then the
340 * match is for exact matching frequency and is available in the stored OPP
341 * table. if false, the match is for exact frequency which is not available.
342 *
343 * This provides a mechanism to enable an opp which is not available currently
344 * or the opposite as well.
345 *
346 * The callers are required to call dev_pm_opp_put() for the returned OPP after
347 * use.
348 */
dev_pm_opp_find_freq_exact(struct device * dev,unsigned long freq,bool available)349 struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
350 unsigned long freq,
351 bool available)
352 {
353 struct opp_table *opp_table;
354 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
355
356 opp_table = _find_opp_table(dev);
357 if (IS_ERR(opp_table)) {
358 int r = PTR_ERR(opp_table);
359
360 dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r);
361 return ERR_PTR(r);
362 }
363
364 mutex_lock(&opp_table->lock);
365
366 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
367 if (temp_opp->available == available &&
368 temp_opp->rate == freq) {
369 opp = temp_opp;
370
371 /* Increment the reference count of OPP */
372 dev_pm_opp_get(opp);
373 break;
374 }
375 }
376
377 mutex_unlock(&opp_table->lock);
378 dev_pm_opp_put_opp_table(opp_table);
379
380 return opp;
381 }
382 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
383
_find_freq_ceil(struct opp_table * opp_table,unsigned long * freq)384 static noinline struct dev_pm_opp *_find_freq_ceil(struct opp_table *opp_table,
385 unsigned long *freq)
386 {
387 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
388
389 mutex_lock(&opp_table->lock);
390
391 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
392 if (temp_opp->available && temp_opp->rate >= *freq) {
393 opp = temp_opp;
394 *freq = opp->rate;
395
396 /* Increment the reference count of OPP */
397 dev_pm_opp_get(opp);
398 break;
399 }
400 }
401
402 mutex_unlock(&opp_table->lock);
403
404 return opp;
405 }
406
407 /**
408 * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
409 * @dev: device for which we do this operation
410 * @freq: Start frequency
411 *
412 * Search for the matching ceil *available* OPP from a starting freq
413 * for a device.
414 *
415 * Return: matching *opp and refreshes *freq accordingly, else returns
416 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
417 * values can be:
418 * EINVAL: for bad pointer
419 * ERANGE: no match found for search
420 * ENODEV: if device not found in list of registered devices
421 *
422 * The callers are required to call dev_pm_opp_put() for the returned OPP after
423 * use.
424 */
dev_pm_opp_find_freq_ceil(struct device * dev,unsigned long * freq)425 struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
426 unsigned long *freq)
427 {
428 struct opp_table *opp_table;
429 struct dev_pm_opp *opp;
430
431 if (!dev || !freq) {
432 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
433 return ERR_PTR(-EINVAL);
434 }
435
436 opp_table = _find_opp_table(dev);
437 if (IS_ERR(opp_table))
438 return ERR_CAST(opp_table);
439
440 opp = _find_freq_ceil(opp_table, freq);
441
442 dev_pm_opp_put_opp_table(opp_table);
443
444 return opp;
445 }
446 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
447
448 /**
449 * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
450 * @dev: device for which we do this operation
451 * @freq: Start frequency
452 *
453 * Search for the matching floor *available* OPP from a starting freq
454 * for a device.
455 *
456 * Return: matching *opp and refreshes *freq accordingly, else returns
457 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
458 * values can be:
459 * EINVAL: for bad pointer
460 * ERANGE: no match found for search
461 * ENODEV: if device not found in list of registered devices
462 *
463 * The callers are required to call dev_pm_opp_put() for the returned OPP after
464 * use.
465 */
dev_pm_opp_find_freq_floor(struct device * dev,unsigned long * freq)466 struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
467 unsigned long *freq)
468 {
469 struct opp_table *opp_table;
470 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
471
472 if (!dev || !freq) {
473 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
474 return ERR_PTR(-EINVAL);
475 }
476
477 opp_table = _find_opp_table(dev);
478 if (IS_ERR(opp_table))
479 return ERR_CAST(opp_table);
480
481 mutex_lock(&opp_table->lock);
482
483 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
484 if (temp_opp->available) {
485 /* go to the next node, before choosing prev */
486 if (temp_opp->rate > *freq)
487 break;
488 else
489 opp = temp_opp;
490 }
491 }
492
493 /* Increment the reference count of OPP */
494 if (!IS_ERR(opp))
495 dev_pm_opp_get(opp);
496 mutex_unlock(&opp_table->lock);
497 dev_pm_opp_put_opp_table(opp_table);
498
499 if (!IS_ERR(opp))
500 *freq = opp->rate;
501
502 return opp;
503 }
504 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
505
_set_opp_voltage(struct device * dev,struct regulator * reg,struct dev_pm_opp_supply * supply)506 static int _set_opp_voltage(struct device *dev, struct regulator *reg,
507 struct dev_pm_opp_supply *supply)
508 {
509 int ret;
510
511 /* Regulator not available for device */
512 if (IS_ERR(reg)) {
513 dev_dbg(dev, "%s: regulator not available: %ld\n", __func__,
514 PTR_ERR(reg));
515 return 0;
516 }
517
518 dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__,
519 supply->u_volt_min, supply->u_volt, supply->u_volt_max);
520
521 ret = regulator_set_voltage_triplet(reg, supply->u_volt_min,
522 supply->u_volt, supply->u_volt_max);
523 if (ret)
524 dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n",
525 __func__, supply->u_volt_min, supply->u_volt,
526 supply->u_volt_max, ret);
527
528 return ret;
529 }
530
531 static inline int
_generic_set_opp_clk_only(struct device * dev,struct clk * clk,unsigned long old_freq,unsigned long freq)532 _generic_set_opp_clk_only(struct device *dev, struct clk *clk,
533 unsigned long old_freq, unsigned long freq)
534 {
535 int ret;
536
537 ret = clk_set_rate(clk, freq);
538 if (ret) {
539 dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
540 ret);
541 }
542
543 return ret;
544 }
545
546 static inline int
_generic_set_opp_domain(struct device * dev,struct clk * clk,unsigned long old_freq,unsigned long freq,unsigned int old_pstate,unsigned int new_pstate)547 _generic_set_opp_domain(struct device *dev, struct clk *clk,
548 unsigned long old_freq, unsigned long freq,
549 unsigned int old_pstate, unsigned int new_pstate)
550 {
551 int ret;
552
553 /* Scaling up? Scale domain performance state before frequency */
554 if (freq > old_freq) {
555 ret = dev_pm_genpd_set_performance_state(dev, new_pstate);
556 if (ret)
557 return ret;
558 }
559
560 ret = _generic_set_opp_clk_only(dev, clk, old_freq, freq);
561 if (ret)
562 goto restore_domain_state;
563
564 /* Scaling down? Scale domain performance state after frequency */
565 if (freq < old_freq) {
566 ret = dev_pm_genpd_set_performance_state(dev, new_pstate);
567 if (ret)
568 goto restore_freq;
569 }
570
571 return 0;
572
573 restore_freq:
574 if (_generic_set_opp_clk_only(dev, clk, freq, old_freq))
575 dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n",
576 __func__, old_freq);
577 restore_domain_state:
578 if (freq > old_freq)
579 dev_pm_genpd_set_performance_state(dev, old_pstate);
580
581 return ret;
582 }
583
_generic_set_opp_regulator(const struct opp_table * opp_table,struct device * dev,unsigned long old_freq,unsigned long freq,struct dev_pm_opp_supply * old_supply,struct dev_pm_opp_supply * new_supply)584 static int _generic_set_opp_regulator(const struct opp_table *opp_table,
585 struct device *dev,
586 unsigned long old_freq,
587 unsigned long freq,
588 struct dev_pm_opp_supply *old_supply,
589 struct dev_pm_opp_supply *new_supply)
590 {
591 struct regulator *reg = opp_table->regulators[0];
592 int ret;
593
594 /* This function only supports single regulator per device */
595 if (WARN_ON(opp_table->regulator_count > 1)) {
596 dev_err(dev, "multiple regulators are not supported\n");
597 return -EINVAL;
598 }
599
600 /* Scaling up? Scale voltage before frequency */
601 if (freq >= old_freq) {
602 ret = _set_opp_voltage(dev, reg, new_supply);
603 if (ret)
604 goto restore_voltage;
605 }
606
607 /* Change frequency */
608 ret = _generic_set_opp_clk_only(dev, opp_table->clk, old_freq, freq);
609 if (ret)
610 goto restore_voltage;
611
612 /* Scaling down? Scale voltage after frequency */
613 if (freq < old_freq) {
614 ret = _set_opp_voltage(dev, reg, new_supply);
615 if (ret)
616 goto restore_freq;
617 }
618
619 return 0;
620
621 restore_freq:
622 if (_generic_set_opp_clk_only(dev, opp_table->clk, freq, old_freq))
623 dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n",
624 __func__, old_freq);
625 restore_voltage:
626 /* This shouldn't harm even if the voltages weren't updated earlier */
627 if (old_supply)
628 _set_opp_voltage(dev, reg, old_supply);
629
630 return ret;
631 }
632
633 /**
634 * dev_pm_opp_set_rate() - Configure new OPP based on frequency
635 * @dev: device for which we do this operation
636 * @target_freq: frequency to achieve
637 *
638 * This configures the power-supplies and clock source to the levels specified
639 * by the OPP corresponding to the target_freq.
640 */
dev_pm_opp_set_rate(struct device * dev,unsigned long target_freq)641 int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq)
642 {
643 struct opp_table *opp_table;
644 unsigned long freq, old_freq;
645 struct dev_pm_opp *old_opp, *opp;
646 struct clk *clk;
647 int ret, size;
648
649 if (unlikely(!target_freq)) {
650 dev_err(dev, "%s: Invalid target frequency %lu\n", __func__,
651 target_freq);
652 return -EINVAL;
653 }
654
655 opp_table = _find_opp_table(dev);
656 if (IS_ERR(opp_table)) {
657 dev_err(dev, "%s: device opp doesn't exist\n", __func__);
658 return PTR_ERR(opp_table);
659 }
660
661 clk = opp_table->clk;
662 if (IS_ERR(clk)) {
663 dev_err(dev, "%s: No clock available for the device\n",
664 __func__);
665 ret = PTR_ERR(clk);
666 goto put_opp_table;
667 }
668
669 freq = clk_round_rate(clk, target_freq);
670 if ((long)freq <= 0)
671 freq = target_freq;
672
673 old_freq = clk_get_rate(clk);
674
675 /* Return early if nothing to do */
676 if (old_freq == freq) {
677 dev_dbg(dev, "%s: old/new frequencies (%lu Hz) are same, nothing to do\n",
678 __func__, freq);
679 ret = 0;
680 goto put_opp_table;
681 }
682
683 old_opp = _find_freq_ceil(opp_table, &old_freq);
684 if (IS_ERR(old_opp)) {
685 dev_err(dev, "%s: failed to find current OPP for freq %lu (%ld)\n",
686 __func__, old_freq, PTR_ERR(old_opp));
687 }
688
689 opp = _find_freq_ceil(opp_table, &freq);
690 if (IS_ERR(opp)) {
691 ret = PTR_ERR(opp);
692 dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n",
693 __func__, freq, ret);
694 goto put_old_opp;
695 }
696
697 dev_dbg(dev, "%s: switching OPP: %lu Hz --> %lu Hz\n", __func__,
698 old_freq, freq);
699
700 /* Only frequency scaling */
701 if (!opp_table->regulators) {
702 /*
703 * We don't support devices with both regulator and
704 * domain performance-state for now.
705 */
706 if (opp_table->genpd_performance_state)
707 ret = _generic_set_opp_domain(dev, clk, old_freq, freq,
708 IS_ERR(old_opp) ? 0 : old_opp->pstate,
709 opp->pstate);
710 else
711 ret = _generic_set_opp_clk_only(dev, clk, old_freq, freq);
712 } else if (!opp_table->set_opp) {
713 ret = _generic_set_opp_regulator(opp_table, dev, old_freq, freq,
714 IS_ERR(old_opp) ? NULL : old_opp->supplies,
715 opp->supplies);
716 } else {
717 struct dev_pm_set_opp_data *data;
718
719 data = opp_table->set_opp_data;
720 data->regulators = opp_table->regulators;
721 data->regulator_count = opp_table->regulator_count;
722 data->clk = clk;
723 data->dev = dev;
724
725 data->old_opp.rate = old_freq;
726 size = sizeof(*opp->supplies) * opp_table->regulator_count;
727 if (IS_ERR(old_opp))
728 memset(data->old_opp.supplies, 0, size);
729 else
730 memcpy(data->old_opp.supplies, old_opp->supplies, size);
731
732 data->new_opp.rate = freq;
733 memcpy(data->new_opp.supplies, opp->supplies, size);
734
735 ret = opp_table->set_opp(data);
736 }
737
738 dev_pm_opp_put(opp);
739 put_old_opp:
740 if (!IS_ERR(old_opp))
741 dev_pm_opp_put(old_opp);
742 put_opp_table:
743 dev_pm_opp_put_opp_table(opp_table);
744 return ret;
745 }
746 EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate);
747
748 /* OPP-dev Helpers */
_remove_opp_dev(struct opp_device * opp_dev,struct opp_table * opp_table)749 static void _remove_opp_dev(struct opp_device *opp_dev,
750 struct opp_table *opp_table)
751 {
752 opp_debug_unregister(opp_dev, opp_table);
753 list_del(&opp_dev->node);
754 kfree(opp_dev);
755 }
756
_add_opp_dev(const struct device * dev,struct opp_table * opp_table)757 struct opp_device *_add_opp_dev(const struct device *dev,
758 struct opp_table *opp_table)
759 {
760 struct opp_device *opp_dev;
761 int ret;
762
763 opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL);
764 if (!opp_dev)
765 return NULL;
766
767 /* Initialize opp-dev */
768 opp_dev->dev = dev;
769 list_add(&opp_dev->node, &opp_table->dev_list);
770
771 /* Create debugfs entries for the opp_table */
772 ret = opp_debug_register(opp_dev, opp_table);
773 if (ret)
774 dev_err(dev, "%s: Failed to register opp debugfs (%d)\n",
775 __func__, ret);
776
777 return opp_dev;
778 }
779
_allocate_opp_table(struct device * dev)780 static struct opp_table *_allocate_opp_table(struct device *dev)
781 {
782 struct opp_table *opp_table;
783 struct opp_device *opp_dev;
784 int ret;
785
786 /*
787 * Allocate a new OPP table. In the infrequent case where a new
788 * device is needed to be added, we pay this penalty.
789 */
790 opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL);
791 if (!opp_table)
792 return NULL;
793
794 INIT_LIST_HEAD(&opp_table->dev_list);
795
796 /* Mark regulator count uninitialized */
797 opp_table->regulator_count = -1;
798
799 opp_dev = _add_opp_dev(dev, opp_table);
800 if (!opp_dev) {
801 kfree(opp_table);
802 return NULL;
803 }
804
805 _of_init_opp_table(opp_table, dev);
806
807 /* Find clk for the device */
808 opp_table->clk = clk_get(dev, NULL);
809 if (IS_ERR(opp_table->clk)) {
810 ret = PTR_ERR(opp_table->clk);
811 if (ret != -EPROBE_DEFER)
812 dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__,
813 ret);
814 }
815
816 BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head);
817 INIT_LIST_HEAD(&opp_table->opp_list);
818 mutex_init(&opp_table->lock);
819 kref_init(&opp_table->kref);
820
821 /* Secure the device table modification */
822 list_add(&opp_table->node, &opp_tables);
823 return opp_table;
824 }
825
_get_opp_table_kref(struct opp_table * opp_table)826 void _get_opp_table_kref(struct opp_table *opp_table)
827 {
828 kref_get(&opp_table->kref);
829 }
830
dev_pm_opp_get_opp_table(struct device * dev)831 struct opp_table *dev_pm_opp_get_opp_table(struct device *dev)
832 {
833 struct opp_table *opp_table;
834
835 /* Hold our table modification lock here */
836 mutex_lock(&opp_table_lock);
837
838 opp_table = _find_opp_table_unlocked(dev);
839 if (!IS_ERR(opp_table))
840 goto unlock;
841
842 opp_table = _allocate_opp_table(dev);
843
844 unlock:
845 mutex_unlock(&opp_table_lock);
846
847 return opp_table;
848 }
849 EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table);
850
_opp_table_kref_release(struct kref * kref)851 static void _opp_table_kref_release(struct kref *kref)
852 {
853 struct opp_table *opp_table = container_of(kref, struct opp_table, kref);
854 struct opp_device *opp_dev;
855
856 /* Release clk */
857 if (!IS_ERR(opp_table->clk))
858 clk_put(opp_table->clk);
859
860 opp_dev = list_first_entry(&opp_table->dev_list, struct opp_device,
861 node);
862
863 _remove_opp_dev(opp_dev, opp_table);
864
865 /* dev_list must be empty now */
866 WARN_ON(!list_empty(&opp_table->dev_list));
867
868 mutex_destroy(&opp_table->lock);
869 list_del(&opp_table->node);
870 kfree(opp_table);
871
872 mutex_unlock(&opp_table_lock);
873 }
874
dev_pm_opp_put_opp_table(struct opp_table * opp_table)875 void dev_pm_opp_put_opp_table(struct opp_table *opp_table)
876 {
877 kref_put_mutex(&opp_table->kref, _opp_table_kref_release,
878 &opp_table_lock);
879 }
880 EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table);
881
_opp_free(struct dev_pm_opp * opp)882 void _opp_free(struct dev_pm_opp *opp)
883 {
884 kfree(opp);
885 }
886
_opp_kref_release(struct kref * kref)887 static void _opp_kref_release(struct kref *kref)
888 {
889 struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref);
890 struct opp_table *opp_table = opp->opp_table;
891
892 /*
893 * Notify the changes in the availability of the operable
894 * frequency/voltage list.
895 */
896 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_REMOVE, opp);
897 opp_debug_remove_one(opp);
898 list_del(&opp->node);
899 kfree(opp);
900
901 mutex_unlock(&opp_table->lock);
902 dev_pm_opp_put_opp_table(opp_table);
903 }
904
dev_pm_opp_get(struct dev_pm_opp * opp)905 void dev_pm_opp_get(struct dev_pm_opp *opp)
906 {
907 kref_get(&opp->kref);
908 }
909
dev_pm_opp_put(struct dev_pm_opp * opp)910 void dev_pm_opp_put(struct dev_pm_opp *opp)
911 {
912 kref_put_mutex(&opp->kref, _opp_kref_release, &opp->opp_table->lock);
913 }
914 EXPORT_SYMBOL_GPL(dev_pm_opp_put);
915
916 /**
917 * dev_pm_opp_remove() - Remove an OPP from OPP table
918 * @dev: device for which we do this operation
919 * @freq: OPP to remove with matching 'freq'
920 *
921 * This function removes an opp from the opp table.
922 */
dev_pm_opp_remove(struct device * dev,unsigned long freq)923 void dev_pm_opp_remove(struct device *dev, unsigned long freq)
924 {
925 struct dev_pm_opp *opp;
926 struct opp_table *opp_table;
927 bool found = false;
928
929 opp_table = _find_opp_table(dev);
930 if (IS_ERR(opp_table))
931 return;
932
933 mutex_lock(&opp_table->lock);
934
935 list_for_each_entry(opp, &opp_table->opp_list, node) {
936 if (opp->rate == freq) {
937 found = true;
938 break;
939 }
940 }
941
942 mutex_unlock(&opp_table->lock);
943
944 if (found) {
945 dev_pm_opp_put(opp);
946 } else {
947 dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
948 __func__, freq);
949 }
950
951 dev_pm_opp_put_opp_table(opp_table);
952 }
953 EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
954
_opp_allocate(struct opp_table * table)955 struct dev_pm_opp *_opp_allocate(struct opp_table *table)
956 {
957 struct dev_pm_opp *opp;
958 int count, supply_size;
959
960 /* Allocate space for at least one supply */
961 count = table->regulator_count > 0 ? table->regulator_count : 1;
962 supply_size = sizeof(*opp->supplies) * count;
963
964 /* allocate new OPP node and supplies structures */
965 opp = kzalloc(sizeof(*opp) + supply_size, GFP_KERNEL);
966 if (!opp)
967 return NULL;
968
969 /* Put the supplies at the end of the OPP structure as an empty array */
970 opp->supplies = (struct dev_pm_opp_supply *)(opp + 1);
971 INIT_LIST_HEAD(&opp->node);
972
973 return opp;
974 }
975
_opp_supported_by_regulators(struct dev_pm_opp * opp,struct opp_table * opp_table)976 static bool _opp_supported_by_regulators(struct dev_pm_opp *opp,
977 struct opp_table *opp_table)
978 {
979 struct regulator *reg;
980 int i;
981
982 if (!opp_table->regulators)
983 return true;
984
985 for (i = 0; i < opp_table->regulator_count; i++) {
986 reg = opp_table->regulators[i];
987
988 if (!regulator_is_supported_voltage(reg,
989 opp->supplies[i].u_volt_min,
990 opp->supplies[i].u_volt_max)) {
991 pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n",
992 __func__, opp->supplies[i].u_volt_min,
993 opp->supplies[i].u_volt_max);
994 return false;
995 }
996 }
997
998 return true;
999 }
1000
_opp_is_duplicate(struct device * dev,struct dev_pm_opp * new_opp,struct opp_table * opp_table,struct list_head ** head)1001 static int _opp_is_duplicate(struct device *dev, struct dev_pm_opp *new_opp,
1002 struct opp_table *opp_table,
1003 struct list_head **head)
1004 {
1005 struct dev_pm_opp *opp;
1006
1007 /*
1008 * Insert new OPP in order of increasing frequency and discard if
1009 * already present.
1010 *
1011 * Need to use &opp_table->opp_list in the condition part of the 'for'
1012 * loop, don't replace it with head otherwise it will become an infinite
1013 * loop.
1014 */
1015 list_for_each_entry(opp, &opp_table->opp_list, node) {
1016 if (new_opp->rate > opp->rate) {
1017 *head = &opp->node;
1018 continue;
1019 }
1020
1021 if (new_opp->rate < opp->rate)
1022 return 0;
1023
1024 /* Duplicate OPPs */
1025 dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
1026 __func__, opp->rate, opp->supplies[0].u_volt,
1027 opp->available, new_opp->rate,
1028 new_opp->supplies[0].u_volt, new_opp->available);
1029
1030 /* Should we compare voltages for all regulators here ? */
1031 return opp->available &&
1032 new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST;
1033 }
1034
1035 return 0;
1036 }
1037
1038 /*
1039 * Returns:
1040 * 0: On success. And appropriate error message for duplicate OPPs.
1041 * -EBUSY: For OPP with same freq/volt and is available. The callers of
1042 * _opp_add() must return 0 if they receive -EBUSY from it. This is to make
1043 * sure we don't print error messages unnecessarily if different parts of
1044 * kernel try to initialize the OPP table.
1045 * -EEXIST: For OPP with same freq but different volt or is unavailable. This
1046 * should be considered an error by the callers of _opp_add().
1047 */
_opp_add(struct device * dev,struct dev_pm_opp * new_opp,struct opp_table * opp_table,bool rate_not_available)1048 int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
1049 struct opp_table *opp_table, bool rate_not_available)
1050 {
1051 struct list_head *head;
1052 int ret;
1053
1054 mutex_lock(&opp_table->lock);
1055 head = &opp_table->opp_list;
1056
1057 if (likely(!rate_not_available)) {
1058 ret = _opp_is_duplicate(dev, new_opp, opp_table, &head);
1059 if (ret) {
1060 mutex_unlock(&opp_table->lock);
1061 return ret;
1062 }
1063 }
1064
1065 list_add(&new_opp->node, head);
1066 mutex_unlock(&opp_table->lock);
1067
1068 new_opp->opp_table = opp_table;
1069 kref_init(&new_opp->kref);
1070
1071 /* Get a reference to the OPP table */
1072 _get_opp_table_kref(opp_table);
1073
1074 ret = opp_debug_create_one(new_opp, opp_table);
1075 if (ret)
1076 dev_err(dev, "%s: Failed to register opp to debugfs (%d)\n",
1077 __func__, ret);
1078
1079 if (!_opp_supported_by_regulators(new_opp, opp_table)) {
1080 new_opp->available = false;
1081 dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n",
1082 __func__, new_opp->rate);
1083 }
1084
1085 return 0;
1086 }
1087
1088 /**
1089 * _opp_add_v1() - Allocate a OPP based on v1 bindings.
1090 * @opp_table: OPP table
1091 * @dev: device for which we do this operation
1092 * @freq: Frequency in Hz for this OPP
1093 * @u_volt: Voltage in uVolts for this OPP
1094 * @dynamic: Dynamically added OPPs.
1095 *
1096 * This function adds an opp definition to the opp table and returns status.
1097 * The opp is made available by default and it can be controlled using
1098 * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
1099 *
1100 * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table
1101 * and freed by dev_pm_opp_of_remove_table.
1102 *
1103 * Return:
1104 * 0 On success OR
1105 * Duplicate OPPs (both freq and volt are same) and opp->available
1106 * -EEXIST Freq are same and volt are different OR
1107 * Duplicate OPPs (both freq and volt are same) and !opp->available
1108 * -ENOMEM Memory allocation failure
1109 */
_opp_add_v1(struct opp_table * opp_table,struct device * dev,unsigned long freq,long u_volt,bool dynamic)1110 int _opp_add_v1(struct opp_table *opp_table, struct device *dev,
1111 unsigned long freq, long u_volt, bool dynamic)
1112 {
1113 struct dev_pm_opp *new_opp;
1114 unsigned long tol;
1115 int ret;
1116
1117 new_opp = _opp_allocate(opp_table);
1118 if (!new_opp)
1119 return -ENOMEM;
1120
1121 /* populate the opp table */
1122 new_opp->rate = freq;
1123 tol = u_volt * opp_table->voltage_tolerance_v1 / 100;
1124 new_opp->supplies[0].u_volt = u_volt;
1125 new_opp->supplies[0].u_volt_min = u_volt - tol;
1126 new_opp->supplies[0].u_volt_max = u_volt + tol;
1127 new_opp->available = true;
1128 new_opp->dynamic = dynamic;
1129
1130 ret = _opp_add(dev, new_opp, opp_table, false);
1131 if (ret) {
1132 /* Don't return error for duplicate OPPs */
1133 if (ret == -EBUSY)
1134 ret = 0;
1135 goto free_opp;
1136 }
1137
1138 /*
1139 * Notify the changes in the availability of the operable
1140 * frequency/voltage list.
1141 */
1142 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
1143 return 0;
1144
1145 free_opp:
1146 _opp_free(new_opp);
1147
1148 return ret;
1149 }
1150
1151 /**
1152 * dev_pm_opp_set_supported_hw() - Set supported platforms
1153 * @dev: Device for which supported-hw has to be set.
1154 * @versions: Array of hierarchy of versions to match.
1155 * @count: Number of elements in the array.
1156 *
1157 * This is required only for the V2 bindings, and it enables a platform to
1158 * specify the hierarchy of versions it supports. OPP layer will then enable
1159 * OPPs, which are available for those versions, based on its 'opp-supported-hw'
1160 * property.
1161 */
dev_pm_opp_set_supported_hw(struct device * dev,const u32 * versions,unsigned int count)1162 struct opp_table *dev_pm_opp_set_supported_hw(struct device *dev,
1163 const u32 *versions, unsigned int count)
1164 {
1165 struct opp_table *opp_table;
1166
1167 opp_table = dev_pm_opp_get_opp_table(dev);
1168 if (!opp_table)
1169 return ERR_PTR(-ENOMEM);
1170
1171 /* Make sure there are no concurrent readers while updating opp_table */
1172 WARN_ON(!list_empty(&opp_table->opp_list));
1173
1174 /* Another CPU that shares the OPP table has set the property ? */
1175 if (opp_table->supported_hw)
1176 return opp_table;
1177
1178 opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions),
1179 GFP_KERNEL);
1180 if (!opp_table->supported_hw) {
1181 dev_pm_opp_put_opp_table(opp_table);
1182 return ERR_PTR(-ENOMEM);
1183 }
1184
1185 opp_table->supported_hw_count = count;
1186
1187 return opp_table;
1188 }
1189 EXPORT_SYMBOL_GPL(dev_pm_opp_set_supported_hw);
1190
1191 /**
1192 * dev_pm_opp_put_supported_hw() - Releases resources blocked for supported hw
1193 * @opp_table: OPP table returned by dev_pm_opp_set_supported_hw().
1194 *
1195 * This is required only for the V2 bindings, and is called for a matching
1196 * dev_pm_opp_set_supported_hw(). Until this is called, the opp_table structure
1197 * will not be freed.
1198 */
dev_pm_opp_put_supported_hw(struct opp_table * opp_table)1199 void dev_pm_opp_put_supported_hw(struct opp_table *opp_table)
1200 {
1201 /* Make sure there are no concurrent readers while updating opp_table */
1202 WARN_ON(!list_empty(&opp_table->opp_list));
1203
1204 kfree(opp_table->supported_hw);
1205 opp_table->supported_hw = NULL;
1206 opp_table->supported_hw_count = 0;
1207
1208 dev_pm_opp_put_opp_table(opp_table);
1209 }
1210 EXPORT_SYMBOL_GPL(dev_pm_opp_put_supported_hw);
1211
1212 /**
1213 * dev_pm_opp_set_prop_name() - Set prop-extn name
1214 * @dev: Device for which the prop-name has to be set.
1215 * @name: name to postfix to properties.
1216 *
1217 * This is required only for the V2 bindings, and it enables a platform to
1218 * specify the extn to be used for certain property names. The properties to
1219 * which the extension will apply are opp-microvolt and opp-microamp. OPP core
1220 * should postfix the property name with -<name> while looking for them.
1221 */
dev_pm_opp_set_prop_name(struct device * dev,const char * name)1222 struct opp_table *dev_pm_opp_set_prop_name(struct device *dev, const char *name)
1223 {
1224 struct opp_table *opp_table;
1225
1226 opp_table = dev_pm_opp_get_opp_table(dev);
1227 if (!opp_table)
1228 return ERR_PTR(-ENOMEM);
1229
1230 /* Make sure there are no concurrent readers while updating opp_table */
1231 WARN_ON(!list_empty(&opp_table->opp_list));
1232
1233 /* Another CPU that shares the OPP table has set the property ? */
1234 if (opp_table->prop_name)
1235 return opp_table;
1236
1237 opp_table->prop_name = kstrdup(name, GFP_KERNEL);
1238 if (!opp_table->prop_name) {
1239 dev_pm_opp_put_opp_table(opp_table);
1240 return ERR_PTR(-ENOMEM);
1241 }
1242
1243 return opp_table;
1244 }
1245 EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name);
1246
1247 /**
1248 * dev_pm_opp_put_prop_name() - Releases resources blocked for prop-name
1249 * @opp_table: OPP table returned by dev_pm_opp_set_prop_name().
1250 *
1251 * This is required only for the V2 bindings, and is called for a matching
1252 * dev_pm_opp_set_prop_name(). Until this is called, the opp_table structure
1253 * will not be freed.
1254 */
dev_pm_opp_put_prop_name(struct opp_table * opp_table)1255 void dev_pm_opp_put_prop_name(struct opp_table *opp_table)
1256 {
1257 /* Make sure there are no concurrent readers while updating opp_table */
1258 WARN_ON(!list_empty(&opp_table->opp_list));
1259
1260 kfree(opp_table->prop_name);
1261 opp_table->prop_name = NULL;
1262
1263 dev_pm_opp_put_opp_table(opp_table);
1264 }
1265 EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name);
1266
_allocate_set_opp_data(struct opp_table * opp_table)1267 static int _allocate_set_opp_data(struct opp_table *opp_table)
1268 {
1269 struct dev_pm_set_opp_data *data;
1270 int len, count = opp_table->regulator_count;
1271
1272 if (WARN_ON(!opp_table->regulators))
1273 return -EINVAL;
1274
1275 /* space for set_opp_data */
1276 len = sizeof(*data);
1277
1278 /* space for old_opp.supplies and new_opp.supplies */
1279 len += 2 * sizeof(struct dev_pm_opp_supply) * count;
1280
1281 data = kzalloc(len, GFP_KERNEL);
1282 if (!data)
1283 return -ENOMEM;
1284
1285 data->old_opp.supplies = (void *)(data + 1);
1286 data->new_opp.supplies = data->old_opp.supplies + count;
1287
1288 opp_table->set_opp_data = data;
1289
1290 return 0;
1291 }
1292
_free_set_opp_data(struct opp_table * opp_table)1293 static void _free_set_opp_data(struct opp_table *opp_table)
1294 {
1295 kfree(opp_table->set_opp_data);
1296 opp_table->set_opp_data = NULL;
1297 }
1298
1299 /**
1300 * dev_pm_opp_set_regulators() - Set regulator names for the device
1301 * @dev: Device for which regulator name is being set.
1302 * @names: Array of pointers to the names of the regulator.
1303 * @count: Number of regulators.
1304 *
1305 * In order to support OPP switching, OPP layer needs to know the name of the
1306 * device's regulators, as the core would be required to switch voltages as
1307 * well.
1308 *
1309 * This must be called before any OPPs are initialized for the device.
1310 */
dev_pm_opp_set_regulators(struct device * dev,const char * const names[],unsigned int count)1311 struct opp_table *dev_pm_opp_set_regulators(struct device *dev,
1312 const char * const names[],
1313 unsigned int count)
1314 {
1315 struct opp_table *opp_table;
1316 struct regulator *reg;
1317 int ret, i;
1318
1319 opp_table = dev_pm_opp_get_opp_table(dev);
1320 if (!opp_table)
1321 return ERR_PTR(-ENOMEM);
1322
1323 /* This should be called before OPPs are initialized */
1324 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
1325 ret = -EBUSY;
1326 goto err;
1327 }
1328
1329 /* Another CPU that shares the OPP table has set the regulators ? */
1330 if (opp_table->regulators)
1331 return opp_table;
1332
1333 opp_table->regulators = kmalloc_array(count,
1334 sizeof(*opp_table->regulators),
1335 GFP_KERNEL);
1336 if (!opp_table->regulators) {
1337 ret = -ENOMEM;
1338 goto err;
1339 }
1340
1341 for (i = 0; i < count; i++) {
1342 reg = regulator_get_optional(dev, names[i]);
1343 if (IS_ERR(reg)) {
1344 ret = PTR_ERR(reg);
1345 if (ret != -EPROBE_DEFER)
1346 dev_err(dev, "%s: no regulator (%s) found: %d\n",
1347 __func__, names[i], ret);
1348 goto free_regulators;
1349 }
1350
1351 opp_table->regulators[i] = reg;
1352 }
1353
1354 opp_table->regulator_count = count;
1355
1356 /* Allocate block only once to pass to set_opp() routines */
1357 ret = _allocate_set_opp_data(opp_table);
1358 if (ret)
1359 goto free_regulators;
1360
1361 return opp_table;
1362
1363 free_regulators:
1364 while (i != 0)
1365 regulator_put(opp_table->regulators[--i]);
1366
1367 kfree(opp_table->regulators);
1368 opp_table->regulators = NULL;
1369 opp_table->regulator_count = -1;
1370 err:
1371 dev_pm_opp_put_opp_table(opp_table);
1372
1373 return ERR_PTR(ret);
1374 }
1375 EXPORT_SYMBOL_GPL(dev_pm_opp_set_regulators);
1376
1377 /**
1378 * dev_pm_opp_put_regulators() - Releases resources blocked for regulator
1379 * @opp_table: OPP table returned from dev_pm_opp_set_regulators().
1380 */
dev_pm_opp_put_regulators(struct opp_table * opp_table)1381 void dev_pm_opp_put_regulators(struct opp_table *opp_table)
1382 {
1383 int i;
1384
1385 if (!opp_table->regulators)
1386 goto put_opp_table;
1387
1388 /* Make sure there are no concurrent readers while updating opp_table */
1389 WARN_ON(!list_empty(&opp_table->opp_list));
1390
1391 for (i = opp_table->regulator_count - 1; i >= 0; i--)
1392 regulator_put(opp_table->regulators[i]);
1393
1394 _free_set_opp_data(opp_table);
1395
1396 kfree(opp_table->regulators);
1397 opp_table->regulators = NULL;
1398 opp_table->regulator_count = -1;
1399
1400 put_opp_table:
1401 dev_pm_opp_put_opp_table(opp_table);
1402 }
1403 EXPORT_SYMBOL_GPL(dev_pm_opp_put_regulators);
1404
1405 /**
1406 * dev_pm_opp_set_clkname() - Set clk name for the device
1407 * @dev: Device for which clk name is being set.
1408 * @name: Clk name.
1409 *
1410 * In order to support OPP switching, OPP layer needs to get pointer to the
1411 * clock for the device. Simple cases work fine without using this routine (i.e.
1412 * by passing connection-id as NULL), but for a device with multiple clocks
1413 * available, the OPP core needs to know the exact name of the clk to use.
1414 *
1415 * This must be called before any OPPs are initialized for the device.
1416 */
dev_pm_opp_set_clkname(struct device * dev,const char * name)1417 struct opp_table *dev_pm_opp_set_clkname(struct device *dev, const char *name)
1418 {
1419 struct opp_table *opp_table;
1420 int ret;
1421
1422 opp_table = dev_pm_opp_get_opp_table(dev);
1423 if (!opp_table)
1424 return ERR_PTR(-ENOMEM);
1425
1426 /* This should be called before OPPs are initialized */
1427 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
1428 ret = -EBUSY;
1429 goto err;
1430 }
1431
1432 /* Already have default clk set, free it */
1433 if (!IS_ERR(opp_table->clk))
1434 clk_put(opp_table->clk);
1435
1436 /* Find clk for the device */
1437 opp_table->clk = clk_get(dev, name);
1438 if (IS_ERR(opp_table->clk)) {
1439 ret = PTR_ERR(opp_table->clk);
1440 if (ret != -EPROBE_DEFER) {
1441 dev_err(dev, "%s: Couldn't find clock: %d\n", __func__,
1442 ret);
1443 }
1444 goto err;
1445 }
1446
1447 return opp_table;
1448
1449 err:
1450 dev_pm_opp_put_opp_table(opp_table);
1451
1452 return ERR_PTR(ret);
1453 }
1454 EXPORT_SYMBOL_GPL(dev_pm_opp_set_clkname);
1455
1456 /**
1457 * dev_pm_opp_put_clkname() - Releases resources blocked for clk.
1458 * @opp_table: OPP table returned from dev_pm_opp_set_clkname().
1459 */
dev_pm_opp_put_clkname(struct opp_table * opp_table)1460 void dev_pm_opp_put_clkname(struct opp_table *opp_table)
1461 {
1462 /* Make sure there are no concurrent readers while updating opp_table */
1463 WARN_ON(!list_empty(&opp_table->opp_list));
1464
1465 clk_put(opp_table->clk);
1466 opp_table->clk = ERR_PTR(-EINVAL);
1467
1468 dev_pm_opp_put_opp_table(opp_table);
1469 }
1470 EXPORT_SYMBOL_GPL(dev_pm_opp_put_clkname);
1471
1472 /**
1473 * dev_pm_opp_register_set_opp_helper() - Register custom set OPP helper
1474 * @dev: Device for which the helper is getting registered.
1475 * @set_opp: Custom set OPP helper.
1476 *
1477 * This is useful to support complex platforms (like platforms with multiple
1478 * regulators per device), instead of the generic OPP set rate helper.
1479 *
1480 * This must be called before any OPPs are initialized for the device.
1481 */
dev_pm_opp_register_set_opp_helper(struct device * dev,int (* set_opp)(struct dev_pm_set_opp_data * data))1482 struct opp_table *dev_pm_opp_register_set_opp_helper(struct device *dev,
1483 int (*set_opp)(struct dev_pm_set_opp_data *data))
1484 {
1485 struct opp_table *opp_table;
1486
1487 if (!set_opp)
1488 return ERR_PTR(-EINVAL);
1489
1490 opp_table = dev_pm_opp_get_opp_table(dev);
1491 if (!opp_table)
1492 return ERR_PTR(-ENOMEM);
1493
1494 /* This should be called before OPPs are initialized */
1495 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
1496 dev_pm_opp_put_opp_table(opp_table);
1497 return ERR_PTR(-EBUSY);
1498 }
1499
1500 /* Another CPU that shares the OPP table has set the helper ? */
1501 if (!opp_table->set_opp)
1502 opp_table->set_opp = set_opp;
1503
1504 return opp_table;
1505 }
1506 EXPORT_SYMBOL_GPL(dev_pm_opp_register_set_opp_helper);
1507
1508 /**
1509 * dev_pm_opp_unregister_set_opp_helper() - Releases resources blocked for
1510 * set_opp helper
1511 * @opp_table: OPP table returned from dev_pm_opp_register_set_opp_helper().
1512 *
1513 * Release resources blocked for platform specific set_opp helper.
1514 */
dev_pm_opp_unregister_set_opp_helper(struct opp_table * opp_table)1515 void dev_pm_opp_unregister_set_opp_helper(struct opp_table *opp_table)
1516 {
1517 /* Make sure there are no concurrent readers while updating opp_table */
1518 WARN_ON(!list_empty(&opp_table->opp_list));
1519
1520 opp_table->set_opp = NULL;
1521 dev_pm_opp_put_opp_table(opp_table);
1522 }
1523 EXPORT_SYMBOL_GPL(dev_pm_opp_unregister_set_opp_helper);
1524
1525 /**
1526 * dev_pm_opp_add() - Add an OPP table from a table definitions
1527 * @dev: device for which we do this operation
1528 * @freq: Frequency in Hz for this OPP
1529 * @u_volt: Voltage in uVolts for this OPP
1530 *
1531 * This function adds an opp definition to the opp table and returns status.
1532 * The opp is made available by default and it can be controlled using
1533 * dev_pm_opp_enable/disable functions.
1534 *
1535 * Return:
1536 * 0 On success OR
1537 * Duplicate OPPs (both freq and volt are same) and opp->available
1538 * -EEXIST Freq are same and volt are different OR
1539 * Duplicate OPPs (both freq and volt are same) and !opp->available
1540 * -ENOMEM Memory allocation failure
1541 */
dev_pm_opp_add(struct device * dev,unsigned long freq,unsigned long u_volt)1542 int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
1543 {
1544 struct opp_table *opp_table;
1545 int ret;
1546
1547 opp_table = dev_pm_opp_get_opp_table(dev);
1548 if (!opp_table)
1549 return -ENOMEM;
1550
1551 /* Fix regulator count for dynamic OPPs */
1552 opp_table->regulator_count = 1;
1553
1554 ret = _opp_add_v1(opp_table, dev, freq, u_volt, true);
1555
1556 dev_pm_opp_put_opp_table(opp_table);
1557 return ret;
1558 }
1559 EXPORT_SYMBOL_GPL(dev_pm_opp_add);
1560
1561 /**
1562 * _opp_set_availability() - helper to set the availability of an opp
1563 * @dev: device for which we do this operation
1564 * @freq: OPP frequency to modify availability
1565 * @availability_req: availability status requested for this opp
1566 *
1567 * Set the availability of an OPP, opp_{enable,disable} share a common logic
1568 * which is isolated here.
1569 *
1570 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
1571 * copy operation, returns 0 if no modification was done OR modification was
1572 * successful.
1573 */
_opp_set_availability(struct device * dev,unsigned long freq,bool availability_req)1574 static int _opp_set_availability(struct device *dev, unsigned long freq,
1575 bool availability_req)
1576 {
1577 struct opp_table *opp_table;
1578 struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
1579 int r = 0;
1580
1581 /* Find the opp_table */
1582 opp_table = _find_opp_table(dev);
1583 if (IS_ERR(opp_table)) {
1584 r = PTR_ERR(opp_table);
1585 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
1586 return r;
1587 }
1588
1589 mutex_lock(&opp_table->lock);
1590
1591 /* Do we have the frequency? */
1592 list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
1593 if (tmp_opp->rate == freq) {
1594 opp = tmp_opp;
1595 break;
1596 }
1597 }
1598
1599 if (IS_ERR(opp)) {
1600 r = PTR_ERR(opp);
1601 goto unlock;
1602 }
1603
1604 /* Is update really needed? */
1605 if (opp->available == availability_req)
1606 goto unlock;
1607
1608 opp->available = availability_req;
1609
1610 dev_pm_opp_get(opp);
1611 mutex_unlock(&opp_table->lock);
1612
1613 /* Notify the change of the OPP availability */
1614 if (availability_req)
1615 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE,
1616 opp);
1617 else
1618 blocking_notifier_call_chain(&opp_table->head,
1619 OPP_EVENT_DISABLE, opp);
1620
1621 dev_pm_opp_put(opp);
1622 goto put_table;
1623
1624 unlock:
1625 mutex_unlock(&opp_table->lock);
1626 put_table:
1627 dev_pm_opp_put_opp_table(opp_table);
1628 return r;
1629 }
1630
1631 /**
1632 * dev_pm_opp_enable() - Enable a specific OPP
1633 * @dev: device for which we do this operation
1634 * @freq: OPP frequency to enable
1635 *
1636 * Enables a provided opp. If the operation is valid, this returns 0, else the
1637 * corresponding error value. It is meant to be used for users an OPP available
1638 * after being temporarily made unavailable with dev_pm_opp_disable.
1639 *
1640 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
1641 * copy operation, returns 0 if no modification was done OR modification was
1642 * successful.
1643 */
dev_pm_opp_enable(struct device * dev,unsigned long freq)1644 int dev_pm_opp_enable(struct device *dev, unsigned long freq)
1645 {
1646 return _opp_set_availability(dev, freq, true);
1647 }
1648 EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
1649
1650 /**
1651 * dev_pm_opp_disable() - Disable a specific OPP
1652 * @dev: device for which we do this operation
1653 * @freq: OPP frequency to disable
1654 *
1655 * Disables a provided opp. If the operation is valid, this returns
1656 * 0, else the corresponding error value. It is meant to be a temporary
1657 * control by users to make this OPP not available until the circumstances are
1658 * right to make it available again (with a call to dev_pm_opp_enable).
1659 *
1660 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
1661 * copy operation, returns 0 if no modification was done OR modification was
1662 * successful.
1663 */
dev_pm_opp_disable(struct device * dev,unsigned long freq)1664 int dev_pm_opp_disable(struct device *dev, unsigned long freq)
1665 {
1666 return _opp_set_availability(dev, freq, false);
1667 }
1668 EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
1669
1670 /**
1671 * dev_pm_opp_register_notifier() - Register OPP notifier for the device
1672 * @dev: Device for which notifier needs to be registered
1673 * @nb: Notifier block to be registered
1674 *
1675 * Return: 0 on success or a negative error value.
1676 */
dev_pm_opp_register_notifier(struct device * dev,struct notifier_block * nb)1677 int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb)
1678 {
1679 struct opp_table *opp_table;
1680 int ret;
1681
1682 opp_table = _find_opp_table(dev);
1683 if (IS_ERR(opp_table))
1684 return PTR_ERR(opp_table);
1685
1686 ret = blocking_notifier_chain_register(&opp_table->head, nb);
1687
1688 dev_pm_opp_put_opp_table(opp_table);
1689
1690 return ret;
1691 }
1692 EXPORT_SYMBOL(dev_pm_opp_register_notifier);
1693
1694 /**
1695 * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device
1696 * @dev: Device for which notifier needs to be unregistered
1697 * @nb: Notifier block to be unregistered
1698 *
1699 * Return: 0 on success or a negative error value.
1700 */
dev_pm_opp_unregister_notifier(struct device * dev,struct notifier_block * nb)1701 int dev_pm_opp_unregister_notifier(struct device *dev,
1702 struct notifier_block *nb)
1703 {
1704 struct opp_table *opp_table;
1705 int ret;
1706
1707 opp_table = _find_opp_table(dev);
1708 if (IS_ERR(opp_table))
1709 return PTR_ERR(opp_table);
1710
1711 ret = blocking_notifier_chain_unregister(&opp_table->head, nb);
1712
1713 dev_pm_opp_put_opp_table(opp_table);
1714
1715 return ret;
1716 }
1717 EXPORT_SYMBOL(dev_pm_opp_unregister_notifier);
1718
1719 /*
1720 * Free OPPs either created using static entries present in DT or even the
1721 * dynamically added entries based on remove_all param.
1722 */
_dev_pm_opp_remove_table(struct opp_table * opp_table,struct device * dev,bool remove_all)1723 void _dev_pm_opp_remove_table(struct opp_table *opp_table, struct device *dev,
1724 bool remove_all)
1725 {
1726 struct dev_pm_opp *opp, *tmp;
1727
1728 /* Find if opp_table manages a single device */
1729 if (list_is_singular(&opp_table->dev_list)) {
1730 /* Free static OPPs */
1731 list_for_each_entry_safe(opp, tmp, &opp_table->opp_list, node) {
1732 if (remove_all || !opp->dynamic)
1733 dev_pm_opp_put(opp);
1734 }
1735
1736 /*
1737 * The OPP table is getting removed, drop the performance state
1738 * constraints.
1739 */
1740 if (opp_table->genpd_performance_state)
1741 dev_pm_genpd_set_performance_state(dev, 0);
1742 } else {
1743 _remove_opp_dev(_find_opp_dev(dev, opp_table), opp_table);
1744 }
1745 }
1746
_dev_pm_opp_find_and_remove_table(struct device * dev,bool remove_all)1747 void _dev_pm_opp_find_and_remove_table(struct device *dev, bool remove_all)
1748 {
1749 struct opp_table *opp_table;
1750
1751 /* Check for existing table for 'dev' */
1752 opp_table = _find_opp_table(dev);
1753 if (IS_ERR(opp_table)) {
1754 int error = PTR_ERR(opp_table);
1755
1756 if (error != -ENODEV)
1757 WARN(1, "%s: opp_table: %d\n",
1758 IS_ERR_OR_NULL(dev) ?
1759 "Invalid device" : dev_name(dev),
1760 error);
1761 return;
1762 }
1763
1764 _dev_pm_opp_remove_table(opp_table, dev, remove_all);
1765
1766 dev_pm_opp_put_opp_table(opp_table);
1767 }
1768
1769 /**
1770 * dev_pm_opp_remove_table() - Free all OPPs associated with the device
1771 * @dev: device pointer used to lookup OPP table.
1772 *
1773 * Free both OPPs created using static entries present in DT and the
1774 * dynamically added entries.
1775 */
dev_pm_opp_remove_table(struct device * dev)1776 void dev_pm_opp_remove_table(struct device *dev)
1777 {
1778 _dev_pm_opp_find_and_remove_table(dev, true);
1779 }
1780 EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table);
1781