pm_opp.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Generic OPP Interface
  4. *
  5. * Copyright (C) 2009-2010 Texas Instruments Incorporated.
  6. * Nishanth Menon
  7. * Romit Dasgupta
  8. * Kevin Hilman
  9. */
  10. #ifndef __LINUX_OPP_H__
  11. #define __LINUX_OPP_H__
  12. #include <linux/energy_model.h>
  13. #include <linux/err.h>
  14. #include <linux/notifier.h>
  15. struct clk;
  16. struct regulator;
  17. struct dev_pm_opp;
  18. struct device;
  19. struct opp_table;
  20. enum dev_pm_opp_event {
  21. OPP_EVENT_ADD, OPP_EVENT_REMOVE, OPP_EVENT_ENABLE, OPP_EVENT_DISABLE,
  22. OPP_EVENT_ADJUST_VOLTAGE,
  23. };
  24. /**
  25. * struct dev_pm_opp_supply - Power supply voltage/current values
  26. * @u_volt: Target voltage in microvolts corresponding to this OPP
  27. * @u_volt_min: Minimum voltage in microvolts corresponding to this OPP
  28. * @u_volt_max: Maximum voltage in microvolts corresponding to this OPP
  29. * @u_amp: Maximum current drawn by the device in microamperes
  30. * @u_watt: Power used by the device in microwatts
  31. *
  32. * This structure stores the voltage/current/power values for a single power
  33. * supply.
  34. */
  35. struct dev_pm_opp_supply {
  36. unsigned long u_volt;
  37. unsigned long u_volt_min;
  38. unsigned long u_volt_max;
  39. unsigned long u_amp;
  40. unsigned long u_watt;
  41. };
  42. /**
  43. * struct dev_pm_opp_icc_bw - Interconnect bandwidth values
  44. * @avg: Average bandwidth corresponding to this OPP (in icc units)
  45. * @peak: Peak bandwidth corresponding to this OPP (in icc units)
  46. *
  47. * This structure stores the bandwidth values for a single interconnect path.
  48. */
  49. struct dev_pm_opp_icc_bw {
  50. u32 avg;
  51. u32 peak;
  52. };
  53. typedef int (*config_regulators_t)(struct device *dev,
  54. struct dev_pm_opp *old_opp, struct dev_pm_opp *new_opp,
  55. struct regulator **regulators, unsigned int count);
  56. typedef int (*config_clks_t)(struct device *dev, struct opp_table *opp_table,
  57. struct dev_pm_opp *opp, void *data, bool scaling_down);
  58. /**
  59. * struct dev_pm_opp_config - Device OPP configuration values
  60. * @clk_names: Clk names, NULL terminated array.
  61. * @config_clks: Custom set clk helper.
  62. * @prop_name: Name to postfix to properties.
  63. * @config_regulators: Custom set regulator helper.
  64. * @supported_hw: Array of hierarchy of versions to match.
  65. * @supported_hw_count: Number of elements in the array.
  66. * @regulator_names: Array of pointers to the names of the regulator, NULL terminated.
  67. * @genpd_names: Null terminated array of pointers containing names of genpd to
  68. * attach.
  69. * @virt_devs: Pointer to return the array of virtual devices.
  70. *
  71. * This structure contains platform specific OPP configurations for the device.
  72. */
  73. struct dev_pm_opp_config {
  74. /* NULL terminated */
  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. };
  85. #if defined(CONFIG_PM_OPP)
  86. struct opp_table *dev_pm_opp_get_opp_table(struct device *dev);
  87. void dev_pm_opp_put_opp_table(struct opp_table *opp_table);
  88. unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp);
  89. int dev_pm_opp_get_supplies(struct dev_pm_opp *opp, struct dev_pm_opp_supply *supplies);
  90. unsigned long dev_pm_opp_get_power(struct dev_pm_opp *opp);
  91. unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp);
  92. unsigned int dev_pm_opp_get_level(struct dev_pm_opp *opp);
  93. unsigned int dev_pm_opp_get_required_pstate(struct dev_pm_opp *opp,
  94. unsigned int index);
  95. bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp);
  96. int dev_pm_opp_get_opp_count(struct device *dev);
  97. unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev);
  98. unsigned long dev_pm_opp_get_max_volt_latency(struct device *dev);
  99. unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev);
  100. unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev);
  101. struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
  102. unsigned long freq,
  103. bool available);
  104. struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
  105. unsigned long *freq);
  106. struct dev_pm_opp *dev_pm_opp_find_level_exact(struct device *dev,
  107. unsigned int level);
  108. struct dev_pm_opp *dev_pm_opp_find_level_ceil(struct device *dev,
  109. unsigned int *level);
  110. struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
  111. unsigned long *freq);
  112. struct dev_pm_opp *dev_pm_opp_find_bw_ceil(struct device *dev,
  113. unsigned int *bw, int index);
  114. struct dev_pm_opp *dev_pm_opp_find_bw_floor(struct device *dev,
  115. unsigned int *bw, int index);
  116. void dev_pm_opp_put(struct dev_pm_opp *opp);
  117. int dev_pm_opp_add(struct device *dev, unsigned long freq,
  118. unsigned long u_volt);
  119. void dev_pm_opp_remove(struct device *dev, unsigned long freq);
  120. void dev_pm_opp_remove_all_dynamic(struct device *dev);
  121. int dev_pm_opp_adjust_voltage(struct device *dev, unsigned long freq,
  122. unsigned long u_volt, unsigned long u_volt_min,
  123. unsigned long u_volt_max);
  124. int dev_pm_opp_enable(struct device *dev, unsigned long freq);
  125. int dev_pm_opp_disable(struct device *dev, unsigned long freq);
  126. int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb);
  127. int dev_pm_opp_unregister_notifier(struct device *dev, struct notifier_block *nb);
  128. int dev_pm_opp_set_config(struct device *dev, struct dev_pm_opp_config *config);
  129. int devm_pm_opp_set_config(struct device *dev, struct dev_pm_opp_config *config);
  130. void dev_pm_opp_clear_config(int token);
  131. int dev_pm_opp_config_clks_simple(struct device *dev,
  132. struct opp_table *opp_table, struct dev_pm_opp *opp, void *data,
  133. bool scaling_down);
  134. 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);
  135. int dev_pm_opp_xlate_performance_state(struct opp_table *src_table, struct opp_table *dst_table, unsigned int pstate);
  136. int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq);
  137. int dev_pm_opp_set_opp(struct device *dev, struct dev_pm_opp *opp);
  138. int dev_pm_opp_set_sharing_cpus(struct device *cpu_dev, const struct cpumask *cpumask);
  139. int dev_pm_opp_get_sharing_cpus(struct device *cpu_dev, struct cpumask *cpumask);
  140. void dev_pm_opp_remove_table(struct device *dev);
  141. void dev_pm_opp_cpumask_remove_table(const struct cpumask *cpumask);
  142. int dev_pm_opp_sync_regulators(struct device *dev);
  143. #else
  144. static inline struct opp_table *dev_pm_opp_get_opp_table(struct device *dev)
  145. {
  146. return ERR_PTR(-EOPNOTSUPP);
  147. }
  148. static inline struct opp_table *dev_pm_opp_get_opp_table_indexed(struct device *dev, int index)
  149. {
  150. return ERR_PTR(-EOPNOTSUPP);
  151. }
  152. static inline void dev_pm_opp_put_opp_table(struct opp_table *opp_table) {}
  153. static inline unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp)
  154. {
  155. return 0;
  156. }
  157. static inline int dev_pm_opp_get_supplies(struct dev_pm_opp *opp, struct dev_pm_opp_supply *supplies)
  158. {
  159. return -EOPNOTSUPP;
  160. }
  161. static inline unsigned long dev_pm_opp_get_power(struct dev_pm_opp *opp)
  162. {
  163. return 0;
  164. }
  165. static inline unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp)
  166. {
  167. return 0;
  168. }
  169. static inline unsigned int dev_pm_opp_get_level(struct dev_pm_opp *opp)
  170. {
  171. return 0;
  172. }
  173. static inline
  174. unsigned int dev_pm_opp_get_required_pstate(struct dev_pm_opp *opp,
  175. unsigned int index)
  176. {
  177. return 0;
  178. }
  179. static inline bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp)
  180. {
  181. return false;
  182. }
  183. static inline int dev_pm_opp_get_opp_count(struct device *dev)
  184. {
  185. return 0;
  186. }
  187. static inline unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev)
  188. {
  189. return 0;
  190. }
  191. static inline unsigned long dev_pm_opp_get_max_volt_latency(struct device *dev)
  192. {
  193. return 0;
  194. }
  195. static inline unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev)
  196. {
  197. return 0;
  198. }
  199. static inline unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev)
  200. {
  201. return 0;
  202. }
  203. static inline struct dev_pm_opp *dev_pm_opp_find_level_exact(struct device *dev,
  204. unsigned int level)
  205. {
  206. return ERR_PTR(-EOPNOTSUPP);
  207. }
  208. static inline struct dev_pm_opp *dev_pm_opp_find_level_ceil(struct device *dev,
  209. unsigned int *level)
  210. {
  211. return ERR_PTR(-EOPNOTSUPP);
  212. }
  213. static inline struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
  214. unsigned long freq, bool available)
  215. {
  216. return ERR_PTR(-EOPNOTSUPP);
  217. }
  218. static inline struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
  219. unsigned long *freq)
  220. {
  221. return ERR_PTR(-EOPNOTSUPP);
  222. }
  223. static inline struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
  224. unsigned long *freq)
  225. {
  226. return ERR_PTR(-EOPNOTSUPP);
  227. }
  228. static inline struct dev_pm_opp *dev_pm_opp_find_bw_ceil(struct device *dev,
  229. unsigned int *bw, int index)
  230. {
  231. return ERR_PTR(-EOPNOTSUPP);
  232. }
  233. static inline struct dev_pm_opp *dev_pm_opp_find_bw_floor(struct device *dev,
  234. unsigned int *bw, int index)
  235. {
  236. return ERR_PTR(-EOPNOTSUPP);
  237. }
  238. static inline void dev_pm_opp_put(struct dev_pm_opp *opp) {}
  239. static inline int dev_pm_opp_add(struct device *dev, unsigned long freq,
  240. unsigned long u_volt)
  241. {
  242. return -EOPNOTSUPP;
  243. }
  244. static inline void dev_pm_opp_remove(struct device *dev, unsigned long freq)
  245. {
  246. }
  247. static inline void dev_pm_opp_remove_all_dynamic(struct device *dev)
  248. {
  249. }
  250. static inline int
  251. dev_pm_opp_adjust_voltage(struct device *dev, unsigned long freq,
  252. unsigned long u_volt, unsigned long u_volt_min,
  253. unsigned long u_volt_max)
  254. {
  255. return 0;
  256. }
  257. static inline int dev_pm_opp_enable(struct device *dev, unsigned long freq)
  258. {
  259. return 0;
  260. }
  261. static inline int dev_pm_opp_disable(struct device *dev, unsigned long freq)
  262. {
  263. return 0;
  264. }
  265. static inline int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb)
  266. {
  267. return -EOPNOTSUPP;
  268. }
  269. static inline int dev_pm_opp_unregister_notifier(struct device *dev, struct notifier_block *nb)
  270. {
  271. return -EOPNOTSUPP;
  272. }
  273. static inline int dev_pm_opp_set_config(struct device *dev, struct dev_pm_opp_config *config)
  274. {
  275. return -EOPNOTSUPP;
  276. }
  277. static inline int devm_pm_opp_set_config(struct device *dev, struct dev_pm_opp_config *config)
  278. {
  279. return -EOPNOTSUPP;
  280. }
  281. static inline void dev_pm_opp_clear_config(int token) {}
  282. static inline int dev_pm_opp_config_clks_simple(struct device *dev,
  283. struct opp_table *opp_table, struct dev_pm_opp *opp, void *data,
  284. bool scaling_down)
  285. {
  286. return -EOPNOTSUPP;
  287. }
  288. static inline struct dev_pm_opp *dev_pm_opp_xlate_required_opp(struct opp_table *src_table,
  289. struct opp_table *dst_table, struct dev_pm_opp *src_opp)
  290. {
  291. return ERR_PTR(-EOPNOTSUPP);
  292. }
  293. static inline int dev_pm_opp_xlate_performance_state(struct opp_table *src_table, struct opp_table *dst_table, unsigned int pstate)
  294. {
  295. return -EOPNOTSUPP;
  296. }
  297. static inline int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq)
  298. {
  299. return -EOPNOTSUPP;
  300. }
  301. static inline int dev_pm_opp_set_opp(struct device *dev, struct dev_pm_opp *opp)
  302. {
  303. return -EOPNOTSUPP;
  304. }
  305. static inline int dev_pm_opp_set_sharing_cpus(struct device *cpu_dev, const struct cpumask *cpumask)
  306. {
  307. return -EOPNOTSUPP;
  308. }
  309. static inline int dev_pm_opp_get_sharing_cpus(struct device *cpu_dev, struct cpumask *cpumask)
  310. {
  311. return -EINVAL;
  312. }
  313. static inline void dev_pm_opp_remove_table(struct device *dev)
  314. {
  315. }
  316. static inline void dev_pm_opp_cpumask_remove_table(const struct cpumask *cpumask)
  317. {
  318. }
  319. static inline int dev_pm_opp_sync_regulators(struct device *dev)
  320. {
  321. return -EOPNOTSUPP;
  322. }
  323. #endif /* CONFIG_PM_OPP */
  324. #if defined(CONFIG_PM_OPP) && defined(CONFIG_OF)
  325. int dev_pm_opp_of_add_table(struct device *dev);
  326. int dev_pm_opp_of_add_table_indexed(struct device *dev, int index);
  327. int devm_pm_opp_of_add_table_indexed(struct device *dev, int index);
  328. void dev_pm_opp_of_remove_table(struct device *dev);
  329. int devm_pm_opp_of_add_table(struct device *dev);
  330. int dev_pm_opp_of_cpumask_add_table(const struct cpumask *cpumask);
  331. void dev_pm_opp_of_cpumask_remove_table(const struct cpumask *cpumask);
  332. int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev, struct cpumask *cpumask);
  333. struct device_node *dev_pm_opp_of_get_opp_desc_node(struct device *dev);
  334. struct device_node *dev_pm_opp_get_of_node(struct dev_pm_opp *opp);
  335. int of_get_required_opp_performance_state(struct device_node *np, int index);
  336. int dev_pm_opp_of_find_icc_paths(struct device *dev, struct opp_table *opp_table);
  337. int dev_pm_opp_of_register_em(struct device *dev, struct cpumask *cpus);
  338. static inline void dev_pm_opp_of_unregister_em(struct device *dev)
  339. {
  340. em_dev_unregister_perf_domain(dev);
  341. }
  342. #else
  343. static inline int dev_pm_opp_of_add_table(struct device *dev)
  344. {
  345. return -EOPNOTSUPP;
  346. }
  347. static inline int dev_pm_opp_of_add_table_indexed(struct device *dev, int index)
  348. {
  349. return -EOPNOTSUPP;
  350. }
  351. static inline int devm_pm_opp_of_add_table_indexed(struct device *dev, int index)
  352. {
  353. return -EOPNOTSUPP;
  354. }
  355. static inline void dev_pm_opp_of_remove_table(struct device *dev)
  356. {
  357. }
  358. static inline int devm_pm_opp_of_add_table(struct device *dev)
  359. {
  360. return -EOPNOTSUPP;
  361. }
  362. static inline int dev_pm_opp_of_cpumask_add_table(const struct cpumask *cpumask)
  363. {
  364. return -EOPNOTSUPP;
  365. }
  366. static inline void dev_pm_opp_of_cpumask_remove_table(const struct cpumask *cpumask)
  367. {
  368. }
  369. static inline int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev, struct cpumask *cpumask)
  370. {
  371. return -EOPNOTSUPP;
  372. }
  373. static inline struct device_node *dev_pm_opp_of_get_opp_desc_node(struct device *dev)
  374. {
  375. return NULL;
  376. }
  377. static inline struct device_node *dev_pm_opp_get_of_node(struct dev_pm_opp *opp)
  378. {
  379. return NULL;
  380. }
  381. static inline int dev_pm_opp_of_register_em(struct device *dev,
  382. struct cpumask *cpus)
  383. {
  384. return -EOPNOTSUPP;
  385. }
  386. static inline void dev_pm_opp_of_unregister_em(struct device *dev)
  387. {
  388. }
  389. static inline int of_get_required_opp_performance_state(struct device_node *np, int index)
  390. {
  391. return -EOPNOTSUPP;
  392. }
  393. static inline int dev_pm_opp_of_find_icc_paths(struct device *dev, struct opp_table *opp_table)
  394. {
  395. return -EOPNOTSUPP;
  396. }
  397. #endif
  398. /* OPP Configuration helpers */
  399. /* Regulators helpers */
  400. static inline int dev_pm_opp_set_regulators(struct device *dev,
  401. const char * const names[])
  402. {
  403. struct dev_pm_opp_config config = {
  404. .regulator_names = names,
  405. };
  406. return dev_pm_opp_set_config(dev, &config);
  407. }
  408. static inline void dev_pm_opp_put_regulators(int token)
  409. {
  410. dev_pm_opp_clear_config(token);
  411. }
  412. static inline int devm_pm_opp_set_regulators(struct device *dev,
  413. const char * const names[])
  414. {
  415. struct dev_pm_opp_config config = {
  416. .regulator_names = names,
  417. };
  418. return devm_pm_opp_set_config(dev, &config);
  419. }
  420. /* Supported-hw helpers */
  421. static inline int dev_pm_opp_set_supported_hw(struct device *dev,
  422. const u32 *versions,
  423. unsigned int count)
  424. {
  425. struct dev_pm_opp_config config = {
  426. .supported_hw = versions,
  427. .supported_hw_count = count,
  428. };
  429. return dev_pm_opp_set_config(dev, &config);
  430. }
  431. static inline void dev_pm_opp_put_supported_hw(int token)
  432. {
  433. dev_pm_opp_clear_config(token);
  434. }
  435. static inline int devm_pm_opp_set_supported_hw(struct device *dev,
  436. const u32 *versions,
  437. unsigned int count)
  438. {
  439. struct dev_pm_opp_config config = {
  440. .supported_hw = versions,
  441. .supported_hw_count = count,
  442. };
  443. return devm_pm_opp_set_config(dev, &config);
  444. }
  445. /* clkname helpers */
  446. static inline int dev_pm_opp_set_clkname(struct device *dev, const char *name)
  447. {
  448. const char *names[] = { name, NULL };
  449. struct dev_pm_opp_config config = {
  450. .clk_names = names,
  451. };
  452. return dev_pm_opp_set_config(dev, &config);
  453. }
  454. static inline void dev_pm_opp_put_clkname(int token)
  455. {
  456. dev_pm_opp_clear_config(token);
  457. }
  458. static inline int devm_pm_opp_set_clkname(struct device *dev, const char *name)
  459. {
  460. const char *names[] = { name, NULL };
  461. struct dev_pm_opp_config config = {
  462. .clk_names = names,
  463. };
  464. return devm_pm_opp_set_config(dev, &config);
  465. }
  466. /* config-regulators helpers */
  467. static inline int dev_pm_opp_set_config_regulators(struct device *dev,
  468. config_regulators_t helper)
  469. {
  470. struct dev_pm_opp_config config = {
  471. .config_regulators = helper,
  472. };
  473. return dev_pm_opp_set_config(dev, &config);
  474. }
  475. static inline void dev_pm_opp_put_config_regulators(int token)
  476. {
  477. dev_pm_opp_clear_config(token);
  478. }
  479. /* genpd helpers */
  480. static inline int dev_pm_opp_attach_genpd(struct device *dev,
  481. const char * const *names,
  482. struct device ***virt_devs)
  483. {
  484. struct dev_pm_opp_config config = {
  485. .genpd_names = names,
  486. .virt_devs = virt_devs,
  487. };
  488. return dev_pm_opp_set_config(dev, &config);
  489. }
  490. static inline void dev_pm_opp_detach_genpd(int token)
  491. {
  492. dev_pm_opp_clear_config(token);
  493. }
  494. static inline int devm_pm_opp_attach_genpd(struct device *dev,
  495. const char * const *names,
  496. struct device ***virt_devs)
  497. {
  498. struct dev_pm_opp_config config = {
  499. .genpd_names = names,
  500. .virt_devs = virt_devs,
  501. };
  502. return devm_pm_opp_set_config(dev, &config);
  503. }
  504. /* prop-name helpers */
  505. static inline int dev_pm_opp_set_prop_name(struct device *dev, const char *name)
  506. {
  507. struct dev_pm_opp_config config = {
  508. .prop_name = name,
  509. };
  510. return dev_pm_opp_set_config(dev, &config);
  511. }
  512. static inline void dev_pm_opp_put_prop_name(int token)
  513. {
  514. dev_pm_opp_clear_config(token);
  515. }
  516. #endif /* __LINUX_OPP_H__ */