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 * @required_dev: The required OPP device.
66 * @required_dev_index: The index of the required OPP for the @required_dev.
68 * This structure contains platform specific OPP configurations for the device.
70 struct dev_pm_opp_config
{
72 const char * const *clk_names
;
73 config_clks_t config_clks
;
74 const char *prop_name
;
75 config_regulators_t config_regulators
;
76 const unsigned int *supported_hw
;
77 unsigned int supported_hw_count
;
78 const char * const *regulator_names
;
79 struct device
*required_dev
;
80 unsigned int required_dev_index
;
83 #define OPP_LEVEL_UNSET U32_MAX
86 * struct dev_pm_opp_data - The data to use to initialize an OPP.
87 * @turbo: Flag to indicate whether the OPP is to be marked turbo or not.
88 * @level: The performance level for the OPP. Set level to OPP_LEVEL_UNSET if
89 * level field isn't used.
90 * @freq: The clock rate in Hz for the OPP.
91 * @u_volt: The voltage in uV for the OPP.
93 struct dev_pm_opp_data
{
100 #if defined(CONFIG_PM_OPP)
102 struct opp_table
*dev_pm_opp_get_opp_table(struct device
*dev
);
103 void dev_pm_opp_put_opp_table(struct opp_table
*opp_table
);
105 unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp
*opp
);
107 int dev_pm_opp_get_supplies(struct dev_pm_opp
*opp
, struct dev_pm_opp_supply
*supplies
);
109 unsigned long dev_pm_opp_get_power(struct dev_pm_opp
*opp
);
111 unsigned long dev_pm_opp_get_freq_indexed(struct dev_pm_opp
*opp
, u32 index
);
113 unsigned int dev_pm_opp_get_level(struct dev_pm_opp
*opp
);
115 unsigned int dev_pm_opp_get_required_pstate(struct dev_pm_opp
*opp
,
118 bool dev_pm_opp_is_turbo(struct dev_pm_opp
*opp
);
120 int dev_pm_opp_get_opp_count(struct device
*dev
);
121 unsigned long dev_pm_opp_get_max_clock_latency(struct device
*dev
);
122 unsigned long dev_pm_opp_get_max_volt_latency(struct device
*dev
);
123 unsigned long dev_pm_opp_get_max_transition_latency(struct device
*dev
);
124 unsigned long dev_pm_opp_get_suspend_opp_freq(struct device
*dev
);
126 struct dev_pm_opp
*dev_pm_opp_find_freq_exact(struct device
*dev
,
131 dev_pm_opp_find_freq_exact_indexed(struct device
*dev
, unsigned long freq
,
132 u32 index
, bool available
);
134 struct dev_pm_opp
*dev_pm_opp_find_freq_floor(struct device
*dev
,
135 unsigned long *freq
);
137 struct dev_pm_opp
*dev_pm_opp_find_freq_floor_indexed(struct device
*dev
,
138 unsigned long *freq
, u32 index
);
140 struct dev_pm_opp
*dev_pm_opp_find_freq_ceil(struct device
*dev
,
141 unsigned long *freq
);
143 struct dev_pm_opp
*dev_pm_opp_find_freq_ceil_indexed(struct device
*dev
,
144 unsigned long *freq
, u32 index
);
146 struct dev_pm_opp
*dev_pm_opp_find_level_exact(struct device
*dev
,
149 struct dev_pm_opp
*dev_pm_opp_find_level_ceil(struct device
*dev
,
150 unsigned int *level
);
152 struct dev_pm_opp
*dev_pm_opp_find_level_floor(struct device
*dev
,
153 unsigned int *level
);
155 struct dev_pm_opp
*dev_pm_opp_find_bw_ceil(struct device
*dev
,
156 unsigned int *bw
, int index
);
158 struct dev_pm_opp
*dev_pm_opp_find_bw_floor(struct device
*dev
,
159 unsigned int *bw
, int index
);
161 void dev_pm_opp_put(struct dev_pm_opp
*opp
);
163 int dev_pm_opp_add_dynamic(struct device
*dev
, struct dev_pm_opp_data
*opp
);
165 void dev_pm_opp_remove(struct device
*dev
, unsigned long freq
);
166 void dev_pm_opp_remove_all_dynamic(struct device
*dev
);
168 int dev_pm_opp_adjust_voltage(struct device
*dev
, unsigned long freq
,
169 unsigned long u_volt
, unsigned long u_volt_min
,
170 unsigned long u_volt_max
);
172 int dev_pm_opp_enable(struct device
*dev
, unsigned long freq
);
174 int dev_pm_opp_disable(struct device
*dev
, unsigned long freq
);
176 int dev_pm_opp_register_notifier(struct device
*dev
, struct notifier_block
*nb
);
177 int dev_pm_opp_unregister_notifier(struct device
*dev
, struct notifier_block
*nb
);
179 int dev_pm_opp_set_config(struct device
*dev
, struct dev_pm_opp_config
*config
);
180 int devm_pm_opp_set_config(struct device
*dev
, struct dev_pm_opp_config
*config
);
181 void dev_pm_opp_clear_config(int token
);
182 int dev_pm_opp_config_clks_simple(struct device
*dev
,
183 struct opp_table
*opp_table
, struct dev_pm_opp
*opp
, void *data
,
186 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
);
187 int dev_pm_opp_xlate_performance_state(struct opp_table
*src_table
, struct opp_table
*dst_table
, unsigned int pstate
);
188 int dev_pm_opp_set_rate(struct device
*dev
, unsigned long target_freq
);
189 int dev_pm_opp_set_opp(struct device
*dev
, struct dev_pm_opp
*opp
);
190 int dev_pm_opp_set_sharing_cpus(struct device
*cpu_dev
, const struct cpumask
*cpumask
);
191 int dev_pm_opp_get_sharing_cpus(struct device
*cpu_dev
, struct cpumask
*cpumask
);
192 void dev_pm_opp_remove_table(struct device
*dev
);
193 void dev_pm_opp_cpumask_remove_table(const struct cpumask
*cpumask
);
194 int dev_pm_opp_sync_regulators(struct device
*dev
);
196 static inline struct opp_table
*dev_pm_opp_get_opp_table(struct device
*dev
)
198 return ERR_PTR(-EOPNOTSUPP
);
201 static inline struct opp_table
*dev_pm_opp_get_opp_table_indexed(struct device
*dev
, int index
)
203 return ERR_PTR(-EOPNOTSUPP
);
206 static inline void dev_pm_opp_put_opp_table(struct opp_table
*opp_table
) {}
208 static inline unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp
*opp
)
213 static inline int dev_pm_opp_get_supplies(struct dev_pm_opp
*opp
, struct dev_pm_opp_supply
*supplies
)
218 static inline unsigned long dev_pm_opp_get_power(struct dev_pm_opp
*opp
)
223 static inline unsigned long dev_pm_opp_get_freq_indexed(struct dev_pm_opp
*opp
, u32 index
)
228 static inline unsigned int dev_pm_opp_get_level(struct dev_pm_opp
*opp
)
234 unsigned int dev_pm_opp_get_required_pstate(struct dev_pm_opp
*opp
,
240 static inline bool dev_pm_opp_is_turbo(struct dev_pm_opp
*opp
)
245 static inline int dev_pm_opp_get_opp_count(struct device
*dev
)
250 static inline unsigned long dev_pm_opp_get_max_clock_latency(struct device
*dev
)
255 static inline unsigned long dev_pm_opp_get_max_volt_latency(struct device
*dev
)
260 static inline unsigned long dev_pm_opp_get_max_transition_latency(struct device
*dev
)
265 static inline unsigned long dev_pm_opp_get_suspend_opp_freq(struct device
*dev
)
270 static inline struct dev_pm_opp
*dev_pm_opp_find_freq_exact(struct device
*dev
,
271 unsigned long freq
, bool available
)
273 return ERR_PTR(-EOPNOTSUPP
);
276 static inline struct dev_pm_opp
*
277 dev_pm_opp_find_freq_exact_indexed(struct device
*dev
, unsigned long freq
,
278 u32 index
, bool available
)
280 return ERR_PTR(-EOPNOTSUPP
);
283 static inline struct dev_pm_opp
*dev_pm_opp_find_freq_floor(struct device
*dev
,
286 return ERR_PTR(-EOPNOTSUPP
);
289 static inline struct dev_pm_opp
*
290 dev_pm_opp_find_freq_floor_indexed(struct device
*dev
, unsigned long *freq
, u32 index
)
292 return ERR_PTR(-EOPNOTSUPP
);
295 static inline struct dev_pm_opp
*dev_pm_opp_find_freq_ceil(struct device
*dev
,
298 return ERR_PTR(-EOPNOTSUPP
);
301 static inline struct dev_pm_opp
*
302 dev_pm_opp_find_freq_ceil_indexed(struct device
*dev
, unsigned long *freq
, u32 index
)
304 return ERR_PTR(-EOPNOTSUPP
);
307 static inline struct dev_pm_opp
*dev_pm_opp_find_level_exact(struct device
*dev
,
310 return ERR_PTR(-EOPNOTSUPP
);
313 static inline struct dev_pm_opp
*dev_pm_opp_find_level_ceil(struct device
*dev
,
316 return ERR_PTR(-EOPNOTSUPP
);
319 static inline struct dev_pm_opp
*dev_pm_opp_find_level_floor(struct device
*dev
,
322 return ERR_PTR(-EOPNOTSUPP
);
325 static inline struct dev_pm_opp
*dev_pm_opp_find_bw_ceil(struct device
*dev
,
326 unsigned int *bw
, int index
)
328 return ERR_PTR(-EOPNOTSUPP
);
331 static inline struct dev_pm_opp
*dev_pm_opp_find_bw_floor(struct device
*dev
,
332 unsigned int *bw
, int index
)
334 return ERR_PTR(-EOPNOTSUPP
);
337 static inline void dev_pm_opp_put(struct dev_pm_opp
*opp
) {}
340 dev_pm_opp_add_dynamic(struct device
*dev
, struct dev_pm_opp_data
*opp
)
345 static inline void dev_pm_opp_remove(struct device
*dev
, unsigned long freq
)
349 static inline void dev_pm_opp_remove_all_dynamic(struct device
*dev
)
354 dev_pm_opp_adjust_voltage(struct device
*dev
, unsigned long freq
,
355 unsigned long u_volt
, unsigned long u_volt_min
,
356 unsigned long u_volt_max
)
361 static inline int dev_pm_opp_enable(struct device
*dev
, unsigned long freq
)
366 static inline int dev_pm_opp_disable(struct device
*dev
, unsigned long freq
)
371 static inline int dev_pm_opp_register_notifier(struct device
*dev
, struct notifier_block
*nb
)
376 static inline int dev_pm_opp_unregister_notifier(struct device
*dev
, struct notifier_block
*nb
)
381 static inline int dev_pm_opp_set_config(struct device
*dev
, struct dev_pm_opp_config
*config
)
386 static inline int devm_pm_opp_set_config(struct device
*dev
, struct dev_pm_opp_config
*config
)
391 static inline void dev_pm_opp_clear_config(int token
) {}
393 static inline int dev_pm_opp_config_clks_simple(struct device
*dev
,
394 struct opp_table
*opp_table
, struct dev_pm_opp
*opp
, void *data
,
400 static inline struct dev_pm_opp
*dev_pm_opp_xlate_required_opp(struct opp_table
*src_table
,
401 struct opp_table
*dst_table
, struct dev_pm_opp
*src_opp
)
403 return ERR_PTR(-EOPNOTSUPP
);
406 static inline int dev_pm_opp_xlate_performance_state(struct opp_table
*src_table
, struct opp_table
*dst_table
, unsigned int pstate
)
411 static inline int dev_pm_opp_set_rate(struct device
*dev
, unsigned long target_freq
)
416 static inline int dev_pm_opp_set_opp(struct device
*dev
, struct dev_pm_opp
*opp
)
421 static inline int dev_pm_opp_set_sharing_cpus(struct device
*cpu_dev
, const struct cpumask
*cpumask
)
426 static inline int dev_pm_opp_get_sharing_cpus(struct device
*cpu_dev
, struct cpumask
*cpumask
)
431 static inline void dev_pm_opp_remove_table(struct device
*dev
)
435 static inline void dev_pm_opp_cpumask_remove_table(const struct cpumask
*cpumask
)
439 static inline int dev_pm_opp_sync_regulators(struct device
*dev
)
444 #endif /* CONFIG_PM_OPP */
446 #if defined(CONFIG_CPU_FREQ) && defined(CONFIG_PM_OPP)
447 int dev_pm_opp_init_cpufreq_table(struct device
*dev
, struct cpufreq_frequency_table
**table
);
448 void dev_pm_opp_free_cpufreq_table(struct device
*dev
, struct cpufreq_frequency_table
**table
);
450 static inline int dev_pm_opp_init_cpufreq_table(struct device
*dev
, struct cpufreq_frequency_table
**table
)
455 static inline void dev_pm_opp_free_cpufreq_table(struct device
*dev
, struct cpufreq_frequency_table
**table
)
461 #if defined(CONFIG_PM_OPP) && defined(CONFIG_OF)
462 int dev_pm_opp_of_add_table(struct device
*dev
);
463 int dev_pm_opp_of_add_table_indexed(struct device
*dev
, int index
);
464 int devm_pm_opp_of_add_table_indexed(struct device
*dev
, int index
);
465 void dev_pm_opp_of_remove_table(struct device
*dev
);
466 int devm_pm_opp_of_add_table(struct device
*dev
);
467 int dev_pm_opp_of_cpumask_add_table(const struct cpumask
*cpumask
);
468 void dev_pm_opp_of_cpumask_remove_table(const struct cpumask
*cpumask
);
469 int dev_pm_opp_of_get_sharing_cpus(struct device
*cpu_dev
, struct cpumask
*cpumask
);
470 struct device_node
*dev_pm_opp_of_get_opp_desc_node(struct device
*dev
);
471 struct device_node
*dev_pm_opp_get_of_node(struct dev_pm_opp
*opp
);
472 int of_get_required_opp_performance_state(struct device_node
*np
, int index
);
473 bool dev_pm_opp_of_has_required_opp(struct device
*dev
);
474 int dev_pm_opp_of_find_icc_paths(struct device
*dev
, struct opp_table
*opp_table
);
475 int dev_pm_opp_of_register_em(struct device
*dev
, struct cpumask
*cpus
);
476 int dev_pm_opp_calc_power(struct device
*dev
, unsigned long *uW
,
478 static inline void dev_pm_opp_of_unregister_em(struct device
*dev
)
480 em_dev_unregister_perf_domain(dev
);
483 static inline int dev_pm_opp_of_add_table(struct device
*dev
)
488 static inline int dev_pm_opp_of_add_table_indexed(struct device
*dev
, int index
)
493 static inline int devm_pm_opp_of_add_table_indexed(struct device
*dev
, int index
)
498 static inline void dev_pm_opp_of_remove_table(struct device
*dev
)
502 static inline int devm_pm_opp_of_add_table(struct device
*dev
)
507 static inline int dev_pm_opp_of_cpumask_add_table(const struct cpumask
*cpumask
)
512 static inline void dev_pm_opp_of_cpumask_remove_table(const struct cpumask
*cpumask
)
516 static inline int dev_pm_opp_of_get_sharing_cpus(struct device
*cpu_dev
, struct cpumask
*cpumask
)
521 static inline struct device_node
*dev_pm_opp_of_get_opp_desc_node(struct device
*dev
)
526 static inline struct device_node
*dev_pm_opp_get_of_node(struct dev_pm_opp
*opp
)
531 static inline int dev_pm_opp_of_register_em(struct device
*dev
,
532 struct cpumask
*cpus
)
537 static inline void dev_pm_opp_of_unregister_em(struct device
*dev
)
541 static inline int dev_pm_opp_calc_power(struct device
*dev
, unsigned long *uW
,
547 static inline int of_get_required_opp_performance_state(struct device_node
*np
, int index
)
552 static inline bool dev_pm_opp_of_has_required_opp(struct device
*dev
)
557 static inline int dev_pm_opp_of_find_icc_paths(struct device
*dev
, struct opp_table
*opp_table
)
563 /* OPP Configuration helpers */
565 static inline int dev_pm_opp_add(struct device
*dev
, unsigned long freq
,
566 unsigned long u_volt
)
568 struct dev_pm_opp_data data
= {
573 return dev_pm_opp_add_dynamic(dev
, &data
);
576 /* Regulators helpers */
577 static inline int dev_pm_opp_set_regulators(struct device
*dev
,
578 const char * const names
[])
580 struct dev_pm_opp_config config
= {
581 .regulator_names
= names
,
584 return dev_pm_opp_set_config(dev
, &config
);
587 static inline void dev_pm_opp_put_regulators(int token
)
589 dev_pm_opp_clear_config(token
);
592 static inline int devm_pm_opp_set_regulators(struct device
*dev
,
593 const char * const names
[])
595 struct dev_pm_opp_config config
= {
596 .regulator_names
= names
,
599 return devm_pm_opp_set_config(dev
, &config
);
602 /* Supported-hw helpers */
603 static inline int dev_pm_opp_set_supported_hw(struct device
*dev
,
607 struct dev_pm_opp_config config
= {
608 .supported_hw
= versions
,
609 .supported_hw_count
= count
,
612 return dev_pm_opp_set_config(dev
, &config
);
615 static inline void dev_pm_opp_put_supported_hw(int token
)
617 dev_pm_opp_clear_config(token
);
620 static inline int devm_pm_opp_set_supported_hw(struct device
*dev
,
624 struct dev_pm_opp_config config
= {
625 .supported_hw
= versions
,
626 .supported_hw_count
= count
,
629 return devm_pm_opp_set_config(dev
, &config
);
632 /* clkname helpers */
633 static inline int dev_pm_opp_set_clkname(struct device
*dev
, const char *name
)
635 const char *names
[] = { name
, NULL
};
636 struct dev_pm_opp_config config
= {
640 return dev_pm_opp_set_config(dev
, &config
);
643 static inline void dev_pm_opp_put_clkname(int token
)
645 dev_pm_opp_clear_config(token
);
648 static inline int devm_pm_opp_set_clkname(struct device
*dev
, const char *name
)
650 const char *names
[] = { name
, NULL
};
651 struct dev_pm_opp_config config
= {
655 return devm_pm_opp_set_config(dev
, &config
);
658 /* config-regulators helpers */
659 static inline int dev_pm_opp_set_config_regulators(struct device
*dev
,
660 config_regulators_t helper
)
662 struct dev_pm_opp_config config
= {
663 .config_regulators
= helper
,
666 return dev_pm_opp_set_config(dev
, &config
);
669 static inline void dev_pm_opp_put_config_regulators(int token
)
671 dev_pm_opp_clear_config(token
);
674 /* prop-name helpers */
675 static inline int dev_pm_opp_set_prop_name(struct device
*dev
, const char *name
)
677 struct dev_pm_opp_config config
= {
681 return dev_pm_opp_set_config(dev
, &config
);
684 static inline void dev_pm_opp_put_prop_name(int token
)
686 dev_pm_opp_clear_config(token
);
689 static inline unsigned long dev_pm_opp_get_freq(struct dev_pm_opp
*opp
)
691 return dev_pm_opp_get_freq_indexed(opp
, 0);
694 #endif /* __LINUX_OPP_H__ */