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