1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Generic OPP Interface
5 * Copyright (C) 2009-2010 Texas Instruments Incorporated.
11 #ifndef __LINUX_OPP_H__
12 #define __LINUX_OPP_H__
14 #include <linux/energy_model.h>
15 #include <linux/err.h>
16 #include <linux/notifier.h>
19 struct cpufreq_frequency_table;
25 enum dev_pm_opp_event {
26 OPP_EVENT_ADD, OPP_EVENT_REMOVE, OPP_EVENT_ENABLE, OPP_EVENT_DISABLE,
27 OPP_EVENT_ADJUST_VOLTAGE,
31 * struct dev_pm_opp_supply - Power supply voltage/current values
32 * @u_volt: Target voltage in microvolts corresponding to this OPP
33 * @u_volt_min: Minimum voltage in microvolts corresponding to this OPP
34 * @u_volt_max: Maximum voltage in microvolts corresponding to this OPP
35 * @u_amp: Maximum current drawn by the device in microamperes
36 * @u_watt: Power used by the device in microwatts
38 * This structure stores the voltage/current/power values for a single power
41 struct dev_pm_opp_supply {
43 unsigned long u_volt_min;
44 unsigned long u_volt_max;
49 typedef int (*config_regulators_t)(struct device *dev,
50 struct dev_pm_opp *old_opp, struct dev_pm_opp *new_opp,
51 struct regulator **regulators, unsigned int count);
53 typedef int (*config_clks_t)(struct device *dev, struct opp_table *opp_table,
54 struct dev_pm_opp *opp, void *data, bool scaling_down);
57 * struct dev_pm_opp_config - Device OPP configuration values
58 * @clk_names: Clk names, NULL terminated array.
59 * @config_clks: Custom set clk helper.
60 * @prop_name: Name to postfix to properties.
61 * @config_regulators: Custom set regulator helper.
62 * @supported_hw: Array of hierarchy of versions to match.
63 * @supported_hw_count: Number of elements in the array.
64 * @regulator_names: Array of pointers to the names of the regulator, NULL terminated.
65 * @genpd_names: Null terminated array of pointers containing names of genpd to
66 * attach. Mutually exclusive with required_devs.
67 * @virt_devs: Pointer to return the array of genpd virtual devices. Mutually
68 * exclusive with required_devs.
69 * @required_devs: Required OPP devices. Mutually exclusive with genpd_names/virt_devs.
71 * This structure contains platform specific OPP configurations for the device.
73 struct dev_pm_opp_config {
75 const char * const *clk_names;
76 config_clks_t config_clks;
77 const char *prop_name;
78 config_regulators_t config_regulators;
79 const unsigned int *supported_hw;
80 unsigned int supported_hw_count;
81 const char * const *regulator_names;
82 const char * const *genpd_names;
83 struct device ***virt_devs;
84 struct device **required_devs;
87 #define OPP_LEVEL_UNSET U32_MAX
90 * struct dev_pm_opp_data - The data to use to initialize an OPP.
91 * @turbo: Flag to indicate whether the OPP is to be marked turbo or not.
92 * @level: The performance level for the OPP. Set level to OPP_LEVEL_UNSET if
93 * level field isn't used.
94 * @freq: The clock rate in Hz for the OPP.
95 * @u_volt: The voltage in uV for the OPP.
97 struct dev_pm_opp_data {
101 unsigned long u_volt;
104 #if defined(CONFIG_PM_OPP)
106 struct opp_table *dev_pm_opp_get_opp_table(struct device *dev);
107 void dev_pm_opp_put_opp_table(struct opp_table *opp_table);
109 unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp);
111 int dev_pm_opp_get_supplies(struct dev_pm_opp *opp, struct dev_pm_opp_supply *supplies);
113 unsigned long dev_pm_opp_get_power(struct dev_pm_opp *opp);
115 unsigned long dev_pm_opp_get_freq_indexed(struct dev_pm_opp *opp, u32 index);
117 unsigned int dev_pm_opp_get_level(struct dev_pm_opp *opp);
119 unsigned int dev_pm_opp_get_required_pstate(struct dev_pm_opp *opp,
122 bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp);
124 int dev_pm_opp_get_opp_count(struct device *dev);
125 unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev);
126 unsigned long dev_pm_opp_get_max_volt_latency(struct device *dev);
127 unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev);
128 unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev);
130 struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
135 dev_pm_opp_find_freq_exact_indexed(struct device *dev, unsigned long freq,
136 u32 index, bool available);
138 struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
139 unsigned long *freq);
141 struct dev_pm_opp *dev_pm_opp_find_freq_floor_indexed(struct device *dev,
142 unsigned long *freq, u32 index);
144 struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
145 unsigned long *freq);
147 struct dev_pm_opp *dev_pm_opp_find_freq_ceil_indexed(struct device *dev,
148 unsigned long *freq, u32 index);
150 struct dev_pm_opp *dev_pm_opp_find_level_exact(struct device *dev,
153 struct dev_pm_opp *dev_pm_opp_find_level_ceil(struct device *dev,
154 unsigned int *level);
156 struct dev_pm_opp *dev_pm_opp_find_level_floor(struct device *dev,
157 unsigned int *level);
159 struct dev_pm_opp *dev_pm_opp_find_bw_ceil(struct device *dev,
160 unsigned int *bw, int index);
162 struct dev_pm_opp *dev_pm_opp_find_bw_floor(struct device *dev,
163 unsigned int *bw, int index);
165 void dev_pm_opp_put(struct dev_pm_opp *opp);
167 int dev_pm_opp_add_dynamic(struct device *dev, struct dev_pm_opp_data *opp);
169 void dev_pm_opp_remove(struct device *dev, unsigned long freq);
170 void dev_pm_opp_remove_all_dynamic(struct device *dev);
172 int dev_pm_opp_adjust_voltage(struct device *dev, unsigned long freq,
173 unsigned long u_volt, unsigned long u_volt_min,
174 unsigned long u_volt_max);
176 int dev_pm_opp_enable(struct device *dev, unsigned long freq);
178 int dev_pm_opp_disable(struct device *dev, unsigned long freq);
180 int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb);
181 int dev_pm_opp_unregister_notifier(struct device *dev, struct notifier_block *nb);
183 int dev_pm_opp_set_config(struct device *dev, struct dev_pm_opp_config *config);
184 int devm_pm_opp_set_config(struct device *dev, struct dev_pm_opp_config *config);
185 void dev_pm_opp_clear_config(int token);
186 int dev_pm_opp_config_clks_simple(struct device *dev,
187 struct opp_table *opp_table, struct dev_pm_opp *opp, void *data,
190 struct dev_pm_opp *dev_pm_opp_xlate_required_opp(struct opp_table *src_table, struct opp_table *dst_table, struct dev_pm_opp *src_opp);
191 int dev_pm_opp_xlate_performance_state(struct opp_table *src_table, struct opp_table *dst_table, unsigned int pstate);
192 int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq);
193 int dev_pm_opp_set_opp(struct device *dev, struct dev_pm_opp *opp);
194 int dev_pm_opp_set_sharing_cpus(struct device *cpu_dev, const struct cpumask *cpumask);
195 int dev_pm_opp_get_sharing_cpus(struct device *cpu_dev, struct cpumask *cpumask);
196 void dev_pm_opp_remove_table(struct device *dev);
197 void dev_pm_opp_cpumask_remove_table(const struct cpumask *cpumask);
198 int dev_pm_opp_sync_regulators(struct device *dev);
200 static inline struct opp_table *dev_pm_opp_get_opp_table(struct device *dev)
202 return ERR_PTR(-EOPNOTSUPP);
205 static inline struct opp_table *dev_pm_opp_get_opp_table_indexed(struct device *dev, int index)
207 return ERR_PTR(-EOPNOTSUPP);
210 static inline void dev_pm_opp_put_opp_table(struct opp_table *opp_table) {}
212 static inline unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp)
217 static inline int dev_pm_opp_get_supplies(struct dev_pm_opp *opp, struct dev_pm_opp_supply *supplies)
222 static inline unsigned long dev_pm_opp_get_power(struct dev_pm_opp *opp)
227 static inline unsigned long dev_pm_opp_get_freq_indexed(struct dev_pm_opp *opp, u32 index)
232 static inline unsigned int dev_pm_opp_get_level(struct dev_pm_opp *opp)
238 unsigned int dev_pm_opp_get_required_pstate(struct dev_pm_opp *opp,
244 static inline bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp)
249 static inline int dev_pm_opp_get_opp_count(struct device *dev)
254 static inline unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev)
259 static inline unsigned long dev_pm_opp_get_max_volt_latency(struct device *dev)
264 static inline unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev)
269 static inline unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev)
274 static inline struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
275 unsigned long freq, bool available)
277 return ERR_PTR(-EOPNOTSUPP);
280 static inline struct dev_pm_opp *
281 dev_pm_opp_find_freq_exact_indexed(struct device *dev, unsigned long freq,
282 u32 index, bool available)
284 return ERR_PTR(-EOPNOTSUPP);
287 static inline struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
290 return ERR_PTR(-EOPNOTSUPP);
293 static inline struct dev_pm_opp *
294 dev_pm_opp_find_freq_floor_indexed(struct device *dev, unsigned long *freq, u32 index)
296 return ERR_PTR(-EOPNOTSUPP);
299 static inline struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
302 return ERR_PTR(-EOPNOTSUPP);
305 static inline struct dev_pm_opp *
306 dev_pm_opp_find_freq_ceil_indexed(struct device *dev, unsigned long *freq, u32 index)
308 return ERR_PTR(-EOPNOTSUPP);
311 static inline struct dev_pm_opp *dev_pm_opp_find_level_exact(struct device *dev,
314 return ERR_PTR(-EOPNOTSUPP);
317 static inline struct dev_pm_opp *dev_pm_opp_find_level_ceil(struct device *dev,
320 return ERR_PTR(-EOPNOTSUPP);
323 static inline struct dev_pm_opp *dev_pm_opp_find_level_floor(struct device *dev,
326 return ERR_PTR(-EOPNOTSUPP);
329 static inline struct dev_pm_opp *dev_pm_opp_find_bw_ceil(struct device *dev,
330 unsigned int *bw, int index)
332 return ERR_PTR(-EOPNOTSUPP);
335 static inline struct dev_pm_opp *dev_pm_opp_find_bw_floor(struct device *dev,
336 unsigned int *bw, int index)
338 return ERR_PTR(-EOPNOTSUPP);
341 static inline void dev_pm_opp_put(struct dev_pm_opp *opp) {}
344 dev_pm_opp_add_dynamic(struct device *dev, struct dev_pm_opp_data *opp)
349 static inline void dev_pm_opp_remove(struct device *dev, unsigned long freq)
353 static inline void dev_pm_opp_remove_all_dynamic(struct device *dev)
358 dev_pm_opp_adjust_voltage(struct device *dev, unsigned long freq,
359 unsigned long u_volt, unsigned long u_volt_min,
360 unsigned long u_volt_max)
365 static inline int dev_pm_opp_enable(struct device *dev, unsigned long freq)
370 static inline int dev_pm_opp_disable(struct device *dev, unsigned long freq)
375 static inline int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb)
380 static inline int dev_pm_opp_unregister_notifier(struct device *dev, struct notifier_block *nb)
385 static inline int dev_pm_opp_set_config(struct device *dev, struct dev_pm_opp_config *config)
390 static inline int devm_pm_opp_set_config(struct device *dev, struct dev_pm_opp_config *config)
395 static inline void dev_pm_opp_clear_config(int token) {}
397 static inline int dev_pm_opp_config_clks_simple(struct device *dev,
398 struct opp_table *opp_table, struct dev_pm_opp *opp, void *data,
404 static inline struct dev_pm_opp *dev_pm_opp_xlate_required_opp(struct opp_table *src_table,
405 struct opp_table *dst_table, struct dev_pm_opp *src_opp)
407 return ERR_PTR(-EOPNOTSUPP);
410 static inline int dev_pm_opp_xlate_performance_state(struct opp_table *src_table, struct opp_table *dst_table, unsigned int pstate)
415 static inline int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq)
420 static inline int dev_pm_opp_set_opp(struct device *dev, struct dev_pm_opp *opp)
425 static inline int dev_pm_opp_set_sharing_cpus(struct device *cpu_dev, const struct cpumask *cpumask)
430 static inline int dev_pm_opp_get_sharing_cpus(struct device *cpu_dev, struct cpumask *cpumask)
435 static inline void dev_pm_opp_remove_table(struct device *dev)
439 static inline void dev_pm_opp_cpumask_remove_table(const struct cpumask *cpumask)
443 static inline int dev_pm_opp_sync_regulators(struct device *dev)
448 #endif /* CONFIG_PM_OPP */
450 #if defined(CONFIG_CPU_FREQ) && defined(CONFIG_PM_OPP)
451 int dev_pm_opp_init_cpufreq_table(struct device *dev, struct cpufreq_frequency_table **table);
452 void dev_pm_opp_free_cpufreq_table(struct device *dev, struct cpufreq_frequency_table **table);
454 static inline int dev_pm_opp_init_cpufreq_table(struct device *dev, struct cpufreq_frequency_table **table)
459 static inline void dev_pm_opp_free_cpufreq_table(struct device *dev, struct cpufreq_frequency_table **table)
465 #if defined(CONFIG_PM_OPP) && defined(CONFIG_OF)
466 int dev_pm_opp_of_add_table(struct device *dev);
467 int dev_pm_opp_of_add_table_indexed(struct device *dev, int index);
468 int devm_pm_opp_of_add_table_indexed(struct device *dev, int index);
469 void dev_pm_opp_of_remove_table(struct device *dev);
470 int devm_pm_opp_of_add_table(struct device *dev);
471 int dev_pm_opp_of_cpumask_add_table(const struct cpumask *cpumask);
472 void dev_pm_opp_of_cpumask_remove_table(const struct cpumask *cpumask);
473 int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev, struct cpumask *cpumask);
474 struct device_node *dev_pm_opp_of_get_opp_desc_node(struct device *dev);
475 struct device_node *dev_pm_opp_get_of_node(struct dev_pm_opp *opp);
476 int of_get_required_opp_performance_state(struct device_node *np, int index);
477 int dev_pm_opp_of_find_icc_paths(struct device *dev, struct opp_table *opp_table);
478 int dev_pm_opp_of_register_em(struct device *dev, struct cpumask *cpus);
479 static inline void dev_pm_opp_of_unregister_em(struct device *dev)
481 em_dev_unregister_perf_domain(dev);
484 static inline int dev_pm_opp_of_add_table(struct device *dev)
489 static inline int dev_pm_opp_of_add_table_indexed(struct device *dev, int index)
494 static inline int devm_pm_opp_of_add_table_indexed(struct device *dev, int index)
499 static inline void dev_pm_opp_of_remove_table(struct device *dev)
503 static inline int devm_pm_opp_of_add_table(struct device *dev)
508 static inline int dev_pm_opp_of_cpumask_add_table(const struct cpumask *cpumask)
513 static inline void dev_pm_opp_of_cpumask_remove_table(const struct cpumask *cpumask)
517 static inline int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev, struct cpumask *cpumask)
522 static inline struct device_node *dev_pm_opp_of_get_opp_desc_node(struct device *dev)
527 static inline struct device_node *dev_pm_opp_get_of_node(struct dev_pm_opp *opp)
532 static inline int dev_pm_opp_of_register_em(struct device *dev,
533 struct cpumask *cpus)
538 static inline void dev_pm_opp_of_unregister_em(struct device *dev)
542 static inline int of_get_required_opp_performance_state(struct device_node *np, int index)
547 static inline int dev_pm_opp_of_find_icc_paths(struct device *dev, struct opp_table *opp_table)
553 /* OPP Configuration helpers */
555 static inline int dev_pm_opp_add(struct device *dev, unsigned long freq,
556 unsigned long u_volt)
558 struct dev_pm_opp_data data = {
563 return dev_pm_opp_add_dynamic(dev, &data);
566 /* Regulators helpers */
567 static inline int dev_pm_opp_set_regulators(struct device *dev,
568 const char * const names[])
570 struct dev_pm_opp_config config = {
571 .regulator_names = names,
574 return dev_pm_opp_set_config(dev, &config);
577 static inline void dev_pm_opp_put_regulators(int token)
579 dev_pm_opp_clear_config(token);
582 static inline int devm_pm_opp_set_regulators(struct device *dev,
583 const char * const names[])
585 struct dev_pm_opp_config config = {
586 .regulator_names = names,
589 return devm_pm_opp_set_config(dev, &config);
592 /* Supported-hw helpers */
593 static inline int dev_pm_opp_set_supported_hw(struct device *dev,
597 struct dev_pm_opp_config config = {
598 .supported_hw = versions,
599 .supported_hw_count = count,
602 return dev_pm_opp_set_config(dev, &config);
605 static inline void dev_pm_opp_put_supported_hw(int token)
607 dev_pm_opp_clear_config(token);
610 static inline int devm_pm_opp_set_supported_hw(struct device *dev,
614 struct dev_pm_opp_config config = {
615 .supported_hw = versions,
616 .supported_hw_count = count,
619 return devm_pm_opp_set_config(dev, &config);
622 /* clkname helpers */
623 static inline int dev_pm_opp_set_clkname(struct device *dev, const char *name)
625 const char *names[] = { name, NULL };
626 struct dev_pm_opp_config config = {
630 return dev_pm_opp_set_config(dev, &config);
633 static inline void dev_pm_opp_put_clkname(int token)
635 dev_pm_opp_clear_config(token);
638 static inline int devm_pm_opp_set_clkname(struct device *dev, const char *name)
640 const char *names[] = { name, NULL };
641 struct dev_pm_opp_config config = {
645 return devm_pm_opp_set_config(dev, &config);
648 /* config-regulators helpers */
649 static inline int dev_pm_opp_set_config_regulators(struct device *dev,
650 config_regulators_t helper)
652 struct dev_pm_opp_config config = {
653 .config_regulators = helper,
656 return dev_pm_opp_set_config(dev, &config);
659 static inline void dev_pm_opp_put_config_regulators(int token)
661 dev_pm_opp_clear_config(token);
665 static inline int dev_pm_opp_attach_genpd(struct device *dev,
666 const char * const *names,
667 struct device ***virt_devs)
669 struct dev_pm_opp_config config = {
670 .genpd_names = names,
671 .virt_devs = virt_devs,
674 return dev_pm_opp_set_config(dev, &config);
677 static inline void dev_pm_opp_detach_genpd(int token)
679 dev_pm_opp_clear_config(token);
682 static inline int devm_pm_opp_attach_genpd(struct device *dev,
683 const char * const *names,
684 struct device ***virt_devs)
686 struct dev_pm_opp_config config = {
687 .genpd_names = names,
688 .virt_devs = virt_devs,
691 return devm_pm_opp_set_config(dev, &config);
694 /* prop-name helpers */
695 static inline int dev_pm_opp_set_prop_name(struct device *dev, const char *name)
697 struct dev_pm_opp_config config = {
701 return dev_pm_opp_set_config(dev, &config);
704 static inline void dev_pm_opp_put_prop_name(int token)
706 dev_pm_opp_clear_config(token);
709 static inline unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp)
711 return dev_pm_opp_get_freq_indexed(opp, 0);
714 #endif /* __LINUX_OPP_H__ */