Mauro Carvalho Chehab | 151f4e2 | 2019-06-13 07:10:36 -0300 | [diff] [blame] | 1 | ========================================== |
Nishanth Menon | 4b87581 | 2013-02-26 23:53:02 +0000 | [diff] [blame] | 2 | Operating Performance Points (OPP) Library |
| 3 | ========================================== |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 4 | |
| 5 | (C) 2009-2010 Nishanth Menon <nm@ti.com>, Texas Instruments Incorporated |
| 6 | |
Mauro Carvalho Chehab | 151f4e2 | 2019-06-13 07:10:36 -0300 | [diff] [blame] | 7 | .. Contents |
| 8 | |
| 9 | 1. Introduction |
| 10 | 2. Initial OPP List Registration |
| 11 | 3. OPP Search Functions |
| 12 | 4. OPP Availability Control Functions |
| 13 | 5. OPP Data Retrieval Functions |
| 14 | 6. Data Structures |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 15 | |
| 16 | 1. Introduction |
| 17 | =============== |
Mauro Carvalho Chehab | 151f4e2 | 2019-06-13 07:10:36 -0300 | [diff] [blame] | 18 | |
Nishanth Menon | 4b87581 | 2013-02-26 23:53:02 +0000 | [diff] [blame] | 19 | 1.1 What is an Operating Performance Point (OPP)? |
Mauro Carvalho Chehab | 151f4e2 | 2019-06-13 07:10:36 -0300 | [diff] [blame] | 20 | ------------------------------------------------- |
Nishanth Menon | 4b87581 | 2013-02-26 23:53:02 +0000 | [diff] [blame] | 21 | |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 22 | Complex SoCs of today consists of a multiple sub-modules working in conjunction. |
| 23 | In an operational system executing varied use cases, not all modules in the SoC |
| 24 | need to function at their highest performing frequency all the time. To |
| 25 | facilitate this, sub-modules in a SoC are grouped into domains, allowing some |
Nishanth Menon | 4b87581 | 2013-02-26 23:53:02 +0000 | [diff] [blame] | 26 | domains to run at lower voltage and frequency while other domains run at |
| 27 | voltage/frequency pairs that are higher. |
| 28 | |
| 29 | The set of discrete tuples consisting of frequency and voltage pairs that |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 30 | the device will support per domain are called Operating Performance Points or |
| 31 | OPPs. |
| 32 | |
Nishanth Menon | 4b87581 | 2013-02-26 23:53:02 +0000 | [diff] [blame] | 33 | As an example: |
Mauro Carvalho Chehab | 151f4e2 | 2019-06-13 07:10:36 -0300 | [diff] [blame] | 34 | |
Nishanth Menon | 4b87581 | 2013-02-26 23:53:02 +0000 | [diff] [blame] | 35 | Let us consider an MPU device which supports the following: |
| 36 | {300MHz at minimum voltage of 1V}, {800MHz at minimum voltage of 1.2V}, |
| 37 | {1GHz at minimum voltage of 1.3V} |
| 38 | |
| 39 | We can represent these as three OPPs as the following {Hz, uV} tuples: |
Mauro Carvalho Chehab | 151f4e2 | 2019-06-13 07:10:36 -0300 | [diff] [blame] | 40 | |
| 41 | - {300000000, 1000000} |
| 42 | - {800000000, 1200000} |
| 43 | - {1000000000, 1300000} |
Nishanth Menon | 4b87581 | 2013-02-26 23:53:02 +0000 | [diff] [blame] | 44 | |
| 45 | 1.2 Operating Performance Points Library |
Mauro Carvalho Chehab | 151f4e2 | 2019-06-13 07:10:36 -0300 | [diff] [blame] | 46 | ---------------------------------------- |
Nishanth Menon | 4b87581 | 2013-02-26 23:53:02 +0000 | [diff] [blame] | 47 | |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 48 | OPP library provides a set of helper functions to organize and query the OPP |
Yue Hu | 27a84f76 | 2019-07-26 14:07:47 +0800 | [diff] [blame] | 49 | information. The library is located in drivers/opp/ directory and the header |
Nishanth Menon | e4db1c7 | 2013-09-19 16:03:52 -0500 | [diff] [blame] | 50 | is located in include/linux/pm_opp.h. OPP library can be enabled by enabling |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 51 | CONFIG_PM_OPP from power management menuconfig menu. OPP library depends on |
| 52 | CONFIG_PM as certain SoCs such as Texas Instrument's OMAP framework allows to |
| 53 | optionally boot at a certain OPP without needing cpufreq. |
| 54 | |
Mauro Carvalho Chehab | 151f4e2 | 2019-06-13 07:10:36 -0300 | [diff] [blame] | 55 | Typical usage of the OPP library is as follows:: |
| 56 | |
| 57 | (users) -> registers a set of default OPPs -> (library) |
| 58 | SoC framework -> modifies on required cases certain OPPs -> OPP layer |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 59 | -> queries to search/retrieve information -> |
| 60 | |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 61 | OPP layer expects each domain to be represented by a unique device pointer. SoC |
| 62 | framework registers a set of initial OPPs per device with the OPP layer. This |
| 63 | list is expected to be an optimally small number typically around 5 per device. |
| 64 | This initial list contains a set of OPPs that the framework expects to be safely |
| 65 | enabled by default in the system. |
| 66 | |
Mauro Carvalho Chehab | 151f4e2 | 2019-06-13 07:10:36 -0300 | [diff] [blame] | 67 | Note on OPP Availability |
| 68 | ^^^^^^^^^^^^^^^^^^^^^^^^ |
| 69 | |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 70 | As the system proceeds to operate, SoC framework may choose to make certain |
| 71 | OPPs available or not available on each device based on various external |
| 72 | factors. Example usage: Thermal management or other exceptional situations where |
| 73 | SoC framework might choose to disable a higher frequency OPP to safely continue |
| 74 | operations until that OPP could be re-enabled if possible. |
| 75 | |
Bjorn Helgaas | 1992b66 | 2019-11-19 08:09:23 -0600 | [diff] [blame] | 76 | OPP library facilitates this concept in its implementation. The following |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 77 | operational functions operate only on available opps: |
Bjorn Helgaas | 1992b66 | 2019-11-19 08:09:23 -0600 | [diff] [blame] | 78 | opp_find_freq_{ceil, floor}, dev_pm_opp_get_voltage, dev_pm_opp_get_freq, |
| 79 | dev_pm_opp_get_opp_count |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 80 | |
Bjorn Helgaas | 1992b66 | 2019-11-19 08:09:23 -0600 | [diff] [blame] | 81 | dev_pm_opp_find_freq_exact is meant to be used to find the opp pointer |
| 82 | which can then be used for dev_pm_opp_enable/disable functions to make an |
| 83 | opp available as required. |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 84 | |
| 85 | WARNING: Users of OPP library should refresh their availability count using |
Bjorn Helgaas | 1992b66 | 2019-11-19 08:09:23 -0600 | [diff] [blame] | 86 | get_opp_count if dev_pm_opp_enable/disable functions are invoked for a |
| 87 | device, the exact mechanism to trigger these or the notification mechanism |
| 88 | to other dependent subsystems such as cpufreq are left to the discretion of |
| 89 | the SoC specific framework which uses the OPP library. Similar care needs |
| 90 | to be taken care to refresh the cpufreq table in cases of these operations. |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 91 | |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 92 | 2. Initial OPP List Registration |
| 93 | ================================ |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 94 | The SoC implementation calls dev_pm_opp_add function iteratively to add OPPs per |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 95 | device. It is expected that the SoC framework will register the OPP entries |
| 96 | optimally- typical numbers range to be less than 5. The list generated by |
| 97 | registering the OPPs is maintained by OPP library throughout the device |
| 98 | operation. The SoC framework can subsequently control the availability of the |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 99 | OPPs dynamically using the dev_pm_opp_enable / disable functions. |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 100 | |
Mauro Carvalho Chehab | 151f4e2 | 2019-06-13 07:10:36 -0300 | [diff] [blame] | 101 | dev_pm_opp_add |
| 102 | Add a new OPP for a specific domain represented by the device pointer. |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 103 | The OPP is defined using the frequency and voltage. Once added, the OPP |
Bjorn Helgaas | 1992b66 | 2019-11-19 08:09:23 -0600 | [diff] [blame] | 104 | is assumed to be available and control of its availability can be done |
| 105 | with the dev_pm_opp_enable/disable functions. OPP library |
| 106 | internally stores and manages this information in the opp struct. |
| 107 | This function may be used by SoC framework to define a optimal list |
| 108 | as per the demands of SoC usage environment. |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 109 | |
Mauro Carvalho Chehab | 151f4e2 | 2019-06-13 07:10:36 -0300 | [diff] [blame] | 110 | WARNING: |
| 111 | Do not use this function in interrupt context. |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 112 | |
Mauro Carvalho Chehab | 151f4e2 | 2019-06-13 07:10:36 -0300 | [diff] [blame] | 113 | Example:: |
| 114 | |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 115 | soc_pm_init() |
| 116 | { |
| 117 | /* Do things */ |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 118 | r = dev_pm_opp_add(mpu_dev, 1000000, 900000); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 119 | if (!r) { |
| 120 | pr_err("%s: unable to register mpu opp(%d)\n", r); |
| 121 | goto no_cpufreq; |
| 122 | } |
| 123 | /* Do cpufreq things */ |
| 124 | no_cpufreq: |
| 125 | /* Do remaining things */ |
| 126 | } |
| 127 | |
| 128 | 3. OPP Search Functions |
| 129 | ======================= |
| 130 | High level framework such as cpufreq operates on frequencies. To map the |
| 131 | frequency back to the corresponding OPP, OPP library provides handy functions |
| 132 | to search the OPP list that OPP library internally manages. These search |
| 133 | functions return the matching pointer representing the opp if a match is |
| 134 | found, else returns error. These errors are expected to be handled by standard |
| 135 | error checks such as IS_ERR() and appropriate actions taken by the caller. |
| 136 | |
Viresh Kumar | 6185dea | 2017-01-23 10:11:52 +0530 | [diff] [blame] | 137 | Callers of these functions shall call dev_pm_opp_put() after they have used the |
| 138 | OPP. Otherwise the memory for the OPP will never get freed and result in |
| 139 | memleak. |
| 140 | |
Mauro Carvalho Chehab | 151f4e2 | 2019-06-13 07:10:36 -0300 | [diff] [blame] | 141 | dev_pm_opp_find_freq_exact |
| 142 | Search for an OPP based on an *exact* frequency and |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 143 | availability. This function is especially useful to enable an OPP which |
| 144 | is not available by default. |
| 145 | Example: In a case when SoC framework detects a situation where a |
| 146 | higher frequency could be made available, it can use this function to |
Mauro Carvalho Chehab | 151f4e2 | 2019-06-13 07:10:36 -0300 | [diff] [blame] | 147 | find the OPP prior to call the dev_pm_opp_enable to actually make |
| 148 | it available:: |
| 149 | |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 150 | opp = dev_pm_opp_find_freq_exact(dev, 1000000000, false); |
Viresh Kumar | 6185dea | 2017-01-23 10:11:52 +0530 | [diff] [blame] | 151 | dev_pm_opp_put(opp); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 152 | /* dont operate on the pointer.. just do a sanity check.. */ |
| 153 | if (IS_ERR(opp)) { |
| 154 | pr_err("frequency not disabled!\n"); |
| 155 | /* trigger appropriate actions.. */ |
| 156 | } else { |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 157 | dev_pm_opp_enable(dev,1000000000); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 158 | } |
| 159 | |
Mauro Carvalho Chehab | 151f4e2 | 2019-06-13 07:10:36 -0300 | [diff] [blame] | 160 | NOTE: |
| 161 | This is the only search function that operates on OPPs which are |
| 162 | not available. |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 163 | |
Mauro Carvalho Chehab | 151f4e2 | 2019-06-13 07:10:36 -0300 | [diff] [blame] | 164 | dev_pm_opp_find_freq_floor |
| 165 | Search for an available OPP which is *at most* the |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 166 | provided frequency. This function is useful while searching for a lesser |
| 167 | match OR operating on OPP information in the order of decreasing |
| 168 | frequency. |
Mauro Carvalho Chehab | 151f4e2 | 2019-06-13 07:10:36 -0300 | [diff] [blame] | 169 | Example: To find the highest opp for a device:: |
| 170 | |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 171 | freq = ULONG_MAX; |
Viresh Kumar | 6185dea | 2017-01-23 10:11:52 +0530 | [diff] [blame] | 172 | opp = dev_pm_opp_find_freq_floor(dev, &freq); |
| 173 | dev_pm_opp_put(opp); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 174 | |
Mauro Carvalho Chehab | 151f4e2 | 2019-06-13 07:10:36 -0300 | [diff] [blame] | 175 | dev_pm_opp_find_freq_ceil |
| 176 | Search for an available OPP which is *at least* the |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 177 | provided frequency. This function is useful while searching for a |
| 178 | higher match OR operating on OPP information in the order of increasing |
| 179 | frequency. |
Mauro Carvalho Chehab | 151f4e2 | 2019-06-13 07:10:36 -0300 | [diff] [blame] | 180 | Example 1: To find the lowest opp for a device:: |
| 181 | |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 182 | freq = 0; |
Viresh Kumar | 6185dea | 2017-01-23 10:11:52 +0530 | [diff] [blame] | 183 | opp = dev_pm_opp_find_freq_ceil(dev, &freq); |
| 184 | dev_pm_opp_put(opp); |
Mauro Carvalho Chehab | 151f4e2 | 2019-06-13 07:10:36 -0300 | [diff] [blame] | 185 | |
| 186 | Example 2: A simplified implementation of a SoC cpufreq_driver->target:: |
| 187 | |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 188 | soc_cpufreq_target(..) |
| 189 | { |
| 190 | /* Do stuff like policy checks etc. */ |
| 191 | /* Find the best frequency match for the req */ |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 192 | opp = dev_pm_opp_find_freq_ceil(dev, &freq); |
Viresh Kumar | 6185dea | 2017-01-23 10:11:52 +0530 | [diff] [blame] | 193 | dev_pm_opp_put(opp); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 194 | if (!IS_ERR(opp)) |
| 195 | soc_switch_to_freq_voltage(freq); |
| 196 | else |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 197 | /* do something when we can't satisfy the req */ |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 198 | /* do other stuff */ |
| 199 | } |
| 200 | |
| 201 | 4. OPP Availability Control Functions |
| 202 | ===================================== |
| 203 | A default OPP list registered with the OPP library may not cater to all possible |
| 204 | situation. The OPP library provides a set of functions to modify the |
| 205 | availability of a OPP within the OPP list. This allows SoC frameworks to have |
| 206 | fine grained dynamic control of which sets of OPPs are operationally available. |
| 207 | These functions are intended to *temporarily* remove an OPP in conditions such |
| 208 | as thermal considerations (e.g. don't use OPPx until the temperature drops). |
| 209 | |
Mauro Carvalho Chehab | 151f4e2 | 2019-06-13 07:10:36 -0300 | [diff] [blame] | 210 | WARNING: |
| 211 | Do not use these functions in interrupt context. |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 212 | |
Mauro Carvalho Chehab | 151f4e2 | 2019-06-13 07:10:36 -0300 | [diff] [blame] | 213 | dev_pm_opp_enable |
| 214 | Make a OPP available for operation. |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 215 | Example: Lets say that 1GHz OPP is to be made available only if the |
| 216 | SoC temperature is lower than a certain threshold. The SoC framework |
Mauro Carvalho Chehab | 151f4e2 | 2019-06-13 07:10:36 -0300 | [diff] [blame] | 217 | implementation might choose to do something as follows:: |
| 218 | |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 219 | if (cur_temp < temp_low_thresh) { |
| 220 | /* Enable 1GHz if it was disabled */ |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 221 | opp = dev_pm_opp_find_freq_exact(dev, 1000000000, false); |
Viresh Kumar | 6185dea | 2017-01-23 10:11:52 +0530 | [diff] [blame] | 222 | dev_pm_opp_put(opp); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 223 | /* just error check */ |
| 224 | if (!IS_ERR(opp)) |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 225 | ret = dev_pm_opp_enable(dev, 1000000000); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 226 | else |
| 227 | goto try_something_else; |
| 228 | } |
| 229 | |
Mauro Carvalho Chehab | 151f4e2 | 2019-06-13 07:10:36 -0300 | [diff] [blame] | 230 | dev_pm_opp_disable |
| 231 | Make an OPP to be not available for operation |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 232 | Example: Lets say that 1GHz OPP is to be disabled if the temperature |
| 233 | exceeds a threshold value. The SoC framework implementation might |
Mauro Carvalho Chehab | 151f4e2 | 2019-06-13 07:10:36 -0300 | [diff] [blame] | 234 | choose to do something as follows:: |
| 235 | |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 236 | if (cur_temp > temp_high_thresh) { |
| 237 | /* Disable 1GHz if it was enabled */ |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 238 | opp = dev_pm_opp_find_freq_exact(dev, 1000000000, true); |
Viresh Kumar | 6185dea | 2017-01-23 10:11:52 +0530 | [diff] [blame] | 239 | dev_pm_opp_put(opp); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 240 | /* just error check */ |
| 241 | if (!IS_ERR(opp)) |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 242 | ret = dev_pm_opp_disable(dev, 1000000000); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 243 | else |
| 244 | goto try_something_else; |
| 245 | } |
| 246 | |
| 247 | 5. OPP Data Retrieval Functions |
| 248 | =============================== |
| 249 | Since OPP library abstracts away the OPP information, a set of functions to pull |
| 250 | information from the OPP structure is necessary. Once an OPP pointer is |
| 251 | retrieved using the search functions, the following functions can be used by SoC |
| 252 | framework to retrieve the information represented inside the OPP layer. |
| 253 | |
Mauro Carvalho Chehab | 151f4e2 | 2019-06-13 07:10:36 -0300 | [diff] [blame] | 254 | dev_pm_opp_get_voltage |
| 255 | Retrieve the voltage represented by the opp pointer. |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 256 | Example: At a cpufreq transition to a different frequency, SoC |
| 257 | framework requires to set the voltage represented by the OPP using |
| 258 | the regulator framework to the Power Management chip providing the |
Mauro Carvalho Chehab | 151f4e2 | 2019-06-13 07:10:36 -0300 | [diff] [blame] | 259 | voltage:: |
| 260 | |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 261 | soc_switch_to_freq_voltage(freq) |
| 262 | { |
| 263 | /* do things */ |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 264 | opp = dev_pm_opp_find_freq_ceil(dev, &freq); |
| 265 | v = dev_pm_opp_get_voltage(opp); |
Viresh Kumar | 6185dea | 2017-01-23 10:11:52 +0530 | [diff] [blame] | 266 | dev_pm_opp_put(opp); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 267 | if (v) |
| 268 | regulator_set_voltage(.., v); |
| 269 | /* do other things */ |
| 270 | } |
| 271 | |
Mauro Carvalho Chehab | 151f4e2 | 2019-06-13 07:10:36 -0300 | [diff] [blame] | 272 | dev_pm_opp_get_freq |
| 273 | Retrieve the freq represented by the opp pointer. |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 274 | Example: Lets say the SoC framework uses a couple of helper functions |
| 275 | we could pass opp pointers instead of doing additional parameters to |
Mauro Carvalho Chehab | 151f4e2 | 2019-06-13 07:10:36 -0300 | [diff] [blame] | 276 | handle quiet a bit of data parameters:: |
| 277 | |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 278 | soc_cpufreq_target(..) |
| 279 | { |
| 280 | /* do things.. */ |
| 281 | max_freq = ULONG_MAX; |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 282 | max_opp = dev_pm_opp_find_freq_floor(dev,&max_freq); |
| 283 | requested_opp = dev_pm_opp_find_freq_ceil(dev,&freq); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 284 | if (!IS_ERR(max_opp) && !IS_ERR(requested_opp)) |
| 285 | r = soc_test_validity(max_opp, requested_opp); |
Viresh Kumar | 6185dea | 2017-01-23 10:11:52 +0530 | [diff] [blame] | 286 | dev_pm_opp_put(max_opp); |
| 287 | dev_pm_opp_put(requested_opp); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 288 | /* do other things */ |
| 289 | } |
| 290 | soc_test_validity(..) |
| 291 | { |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 292 | if(dev_pm_opp_get_voltage(max_opp) < dev_pm_opp_get_voltage(requested_opp)) |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 293 | return -EINVAL; |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 294 | if(dev_pm_opp_get_freq(max_opp) < dev_pm_opp_get_freq(requested_opp)) |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 295 | return -EINVAL; |
| 296 | /* do things.. */ |
| 297 | } |
| 298 | |
Mauro Carvalho Chehab | 151f4e2 | 2019-06-13 07:10:36 -0300 | [diff] [blame] | 299 | dev_pm_opp_get_opp_count |
| 300 | Retrieve the number of available opps for a device |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 301 | Example: Lets say a co-processor in the SoC needs to know the available |
Mauro Carvalho Chehab | 151f4e2 | 2019-06-13 07:10:36 -0300 | [diff] [blame] | 302 | frequencies in a table, the main processor can notify as following:: |
| 303 | |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 304 | soc_notify_coproc_available_frequencies() |
| 305 | { |
| 306 | /* Do things */ |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 307 | num_available = dev_pm_opp_get_opp_count(dev); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 308 | speeds = kzalloc(sizeof(u32) * num_available, GFP_KERNEL); |
| 309 | /* populate the table in increasing order */ |
| 310 | freq = 0; |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 311 | while (!IS_ERR(opp = dev_pm_opp_find_freq_ceil(dev, &freq))) { |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 312 | speeds[i] = freq; |
| 313 | freq++; |
| 314 | i++; |
Viresh Kumar | 6185dea | 2017-01-23 10:11:52 +0530 | [diff] [blame] | 315 | dev_pm_opp_put(opp); |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 316 | } |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 317 | |
| 318 | soc_notify_coproc(AVAILABLE_FREQs, speeds, num_available); |
| 319 | /* Do other things */ |
| 320 | } |
| 321 | |
Nishanth Menon | a0dd7b7 | 2014-05-05 08:33:50 -0500 | [diff] [blame] | 322 | 6. Data Structures |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 323 | ================== |
| 324 | Typically an SoC contains multiple voltage domains which are variable. Each |
| 325 | domain is represented by a device pointer. The relationship to OPP can be |
Mauro Carvalho Chehab | 151f4e2 | 2019-06-13 07:10:36 -0300 | [diff] [blame] | 326 | represented as follows:: |
| 327 | |
| 328 | SoC |
| 329 | |- device 1 |
| 330 | | |- opp 1 (availability, freq, voltage) |
| 331 | | |- opp 2 .. |
| 332 | ... ... |
| 333 | | `- opp n .. |
| 334 | |- device 2 |
| 335 | ... |
| 336 | `- device m |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 337 | |
| 338 | OPP library maintains a internal list that the SoC framework populates and |
| 339 | accessed by various functions as described above. However, the structures |
| 340 | representing the actual OPPs and domains are internal to the OPP library itself |
| 341 | to allow for suitable abstraction reusable across systems. |
| 342 | |
Mauro Carvalho Chehab | 151f4e2 | 2019-06-13 07:10:36 -0300 | [diff] [blame] | 343 | struct dev_pm_opp |
| 344 | The internal data structure of OPP library which is used to |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 345 | represent an OPP. In addition to the freq, voltage, availability |
| 346 | information, it also contains internal book keeping information required |
| 347 | for the OPP library to operate on. Pointer to this structure is |
| 348 | provided back to the users such as SoC framework to be used as a |
| 349 | identifier for OPP in the interactions with OPP layer. |
| 350 | |
Mauro Carvalho Chehab | 151f4e2 | 2019-06-13 07:10:36 -0300 | [diff] [blame] | 351 | WARNING: |
| 352 | The struct dev_pm_opp pointer should not be parsed or modified by the |
| 353 | users. The defaults of for an instance is populated by |
| 354 | dev_pm_opp_add, but the availability of the OPP can be modified |
| 355 | by dev_pm_opp_enable/disable functions. |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 356 | |
Mauro Carvalho Chehab | 151f4e2 | 2019-06-13 07:10:36 -0300 | [diff] [blame] | 357 | struct device |
| 358 | This is used to identify a domain to the OPP layer. The |
Bjorn Helgaas | 1992b66 | 2019-11-19 08:09:23 -0600 | [diff] [blame] | 359 | nature of the device and its implementation is left to the user of |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 360 | OPP library such as the SoC framework. |
| 361 | |
| 362 | Overall, in a simplistic view, the data structure operations is represented as |
Mauro Carvalho Chehab | 151f4e2 | 2019-06-13 07:10:36 -0300 | [diff] [blame] | 363 | following:: |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 364 | |
Mauro Carvalho Chehab | 151f4e2 | 2019-06-13 07:10:36 -0300 | [diff] [blame] | 365 | Initialization / modification: |
| 366 | +-----+ /- dev_pm_opp_enable |
| 367 | dev_pm_opp_add --> | opp | <------- |
| 368 | | +-----+ \- dev_pm_opp_disable |
| 369 | \-------> domain_info(device) |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 370 | |
Mauro Carvalho Chehab | 151f4e2 | 2019-06-13 07:10:36 -0300 | [diff] [blame] | 371 | Search functions: |
| 372 | /-- dev_pm_opp_find_freq_ceil ---\ +-----+ |
| 373 | domain_info<---- dev_pm_opp_find_freq_exact -----> | opp | |
| 374 | \-- dev_pm_opp_find_freq_floor ---/ +-----+ |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 375 | |
Mauro Carvalho Chehab | 151f4e2 | 2019-06-13 07:10:36 -0300 | [diff] [blame] | 376 | Retrieval functions: |
| 377 | +-----+ /- dev_pm_opp_get_voltage |
| 378 | | opp | <--- |
| 379 | +-----+ \- dev_pm_opp_get_freq |
Nishanth Menon | e1f60b2 | 2010-10-13 00:13:10 +0200 | [diff] [blame] | 380 | |
Mauro Carvalho Chehab | 151f4e2 | 2019-06-13 07:10:36 -0300 | [diff] [blame] | 381 | domain_info <- dev_pm_opp_get_opp_count |