blob: 5f114cd3d88c4aa49cf86b1e82dff9e782fa0b60 [file] [log] [blame]
Viresh Kumarf47b72a2016-05-05 16:20:33 +05301/*
2 * Generic OPP OF helpers
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/cpu.h>
17#include <linux/errno.h>
18#include <linux/device.h>
Sudeep Holla98679992017-10-12 11:32:23 +010019#include <linux/of_device.h>
Viresh Kumar3ba98322016-11-18 15:47:46 +053020#include <linux/pm_domain.h>
Viresh Kumardfbe4672016-12-01 16:28:19 +053021#include <linux/slab.h>
Viresh Kumarf47b72a2016-05-05 16:20:33 +053022#include <linux/export.h>
23
24#include "opp.h"
25
Viresh Kumarf06ed902018-06-14 12:04:43 +053026/*
27 * Returns opp descriptor node for a device node, caller must
28 * do of_node_put().
29 */
30static struct device_node *_opp_of_get_opp_desc_node(struct device_node *np,
31 int index)
32{
33 /* "operating-points-v2" can be an array for power domain providers */
34 return of_parse_phandle(np, "operating-points-v2", index);
35}
36
37/* Returns opp descriptor node for a device, caller must do of_node_put() */
38struct device_node *dev_pm_opp_of_get_opp_desc_node(struct device *dev)
39{
40 return _opp_of_get_opp_desc_node(dev->of_node, 0);
41}
42EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_opp_desc_node);
43
Viresh Kumar283d55e2018-09-07 09:01:54 +053044struct opp_table *_managed_opp(struct device *dev, int index)
Viresh Kumarf47b72a2016-05-05 16:20:33 +053045{
Viresh Kumarb83c1892017-01-23 10:11:45 +053046 struct opp_table *opp_table, *managed_table = NULL;
Viresh Kumar283d55e2018-09-07 09:01:54 +053047 struct device_node *np;
Viresh Kumarb83c1892017-01-23 10:11:45 +053048
Viresh Kumar283d55e2018-09-07 09:01:54 +053049 np = _opp_of_get_opp_desc_node(dev->of_node, index);
50 if (!np)
51 return NULL;
Viresh Kumarf47b72a2016-05-05 16:20:33 +053052
Viresh Kumar052c6f12017-01-23 10:11:49 +053053 list_for_each_entry(opp_table, &opp_tables, node) {
Viresh Kumarf47b72a2016-05-05 16:20:33 +053054 if (opp_table->np == np) {
55 /*
56 * Multiple devices can point to the same OPP table and
57 * so will have same node-pointer, np.
58 *
59 * But the OPPs will be considered as shared only if the
60 * OPP table contains a "opp-shared" property.
61 */
Viresh Kumarb83c1892017-01-23 10:11:45 +053062 if (opp_table->shared_opp == OPP_TABLE_ACCESS_SHARED) {
63 _get_opp_table_kref(opp_table);
64 managed_table = opp_table;
65 }
Viresh Kumar79ee2e82016-06-16 19:03:11 +053066
Viresh Kumarb83c1892017-01-23 10:11:45 +053067 break;
Viresh Kumarf47b72a2016-05-05 16:20:33 +053068 }
69 }
70
Viresh Kumar283d55e2018-09-07 09:01:54 +053071 of_node_put(np);
Viresh Kumarb83c1892017-01-23 10:11:45 +053072
73 return managed_table;
Viresh Kumarf47b72a2016-05-05 16:20:33 +053074}
75
Viresh Kumareb7c87432018-09-05 16:17:14 +053076void _of_init_opp_table(struct opp_table *opp_table, struct device *dev,
77 int index)
Viresh Kumarf47b72a2016-05-05 16:20:33 +053078{
Viresh Kumarf06ed902018-06-14 12:04:43 +053079 struct device_node *np, *opp_np;
80 u32 val;
Viresh Kumarf47b72a2016-05-05 16:20:33 +053081
82 /*
83 * Only required for backward compatibility with v1 bindings, but isn't
84 * harmful for other cases. And so we do it unconditionally.
85 */
86 np = of_node_get(dev->of_node);
Viresh Kumarf06ed902018-06-14 12:04:43 +053087 if (!np)
88 return;
Viresh Kumarf47b72a2016-05-05 16:20:33 +053089
Viresh Kumarf06ed902018-06-14 12:04:43 +053090 if (!of_property_read_u32(np, "clock-latency", &val))
91 opp_table->clock_latency_ns_max = val;
92 of_property_read_u32(np, "voltage-tolerance",
93 &opp_table->voltage_tolerance_v1);
94
Viresh Kumar61d8e7c2018-06-13 16:00:11 +053095 if (of_find_property(np, "#power-domain-cells", NULL))
96 opp_table->is_genpd = true;
97
Viresh Kumarf06ed902018-06-14 12:04:43 +053098 /* Get OPP table node */
99 opp_np = _opp_of_get_opp_desc_node(np, index);
100 of_node_put(np);
101
102 if (!opp_np)
103 return;
104
105 if (of_property_read_bool(opp_np, "opp-shared"))
106 opp_table->shared_opp = OPP_TABLE_ACCESS_SHARED;
107 else
108 opp_table->shared_opp = OPP_TABLE_ACCESS_EXCLUSIVE;
109
110 opp_table->np = opp_np;
111
112 of_node_put(opp_np);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530113}
114
115static bool _opp_is_supported(struct device *dev, struct opp_table *opp_table,
116 struct device_node *np)
117{
118 unsigned int count = opp_table->supported_hw_count;
119 u32 version;
120 int ret;
121
Dave Gerlacha4ee4542016-09-23 15:07:47 -0500122 if (!opp_table->supported_hw) {
123 /*
124 * In the case that no supported_hw has been set by the
125 * platform but there is an opp-supported-hw value set for
126 * an OPP then the OPP should not be enabled as there is
127 * no way to see if the hardware supports it.
128 */
129 if (of_find_property(np, "opp-supported-hw", NULL))
130 return false;
131 else
132 return true;
133 }
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530134
135 while (count--) {
136 ret = of_property_read_u32_index(np, "opp-supported-hw", count,
137 &version);
138 if (ret) {
139 dev_warn(dev, "%s: failed to read opp-supported-hw property at index %d: %d\n",
140 __func__, count, ret);
141 return false;
142 }
143
144 /* Both of these are bitwise masks of the versions */
145 if (!(version & opp_table->supported_hw[count]))
146 return false;
147 }
148
149 return true;
150}
151
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530152static int opp_parse_supplies(struct dev_pm_opp *opp, struct device *dev,
153 struct opp_table *opp_table)
154{
Viresh Kumardfbe4672016-12-01 16:28:19 +0530155 u32 *microvolt, *microamp = NULL;
156 int supplies, vcount, icount, ret, i, j;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530157 struct property *prop = NULL;
158 char name[NAME_MAX];
159
Viresh Kumardfbe4672016-12-01 16:28:19 +0530160 supplies = opp_table->regulator_count ? opp_table->regulator_count : 1;
161
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530162 /* Search for "opp-microvolt-<name>" */
163 if (opp_table->prop_name) {
164 snprintf(name, sizeof(name), "opp-microvolt-%s",
165 opp_table->prop_name);
166 prop = of_find_property(opp->np, name, NULL);
167 }
168
169 if (!prop) {
170 /* Search for "opp-microvolt" */
171 sprintf(name, "opp-microvolt");
172 prop = of_find_property(opp->np, name, NULL);
173
174 /* Missing property isn't a problem, but an invalid entry is */
Viresh Kumar688a48b2017-05-23 09:32:12 +0530175 if (!prop) {
176 if (!opp_table->regulator_count)
177 return 0;
178
179 dev_err(dev, "%s: opp-microvolt missing although OPP managing regulators\n",
180 __func__);
181 return -EINVAL;
182 }
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530183 }
184
Viresh Kumardfbe4672016-12-01 16:28:19 +0530185 vcount = of_property_count_u32_elems(opp->np, name);
186 if (vcount < 0) {
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530187 dev_err(dev, "%s: Invalid %s property (%d)\n",
Viresh Kumardfbe4672016-12-01 16:28:19 +0530188 __func__, name, vcount);
189 return vcount;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530190 }
191
Viresh Kumardfbe4672016-12-01 16:28:19 +0530192 /* There can be one or three elements per supply */
193 if (vcount != supplies && vcount != supplies * 3) {
194 dev_err(dev, "%s: Invalid number of elements in %s property (%d) with supplies (%d)\n",
195 __func__, name, vcount, supplies);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530196 return -EINVAL;
197 }
198
Viresh Kumardfbe4672016-12-01 16:28:19 +0530199 microvolt = kmalloc_array(vcount, sizeof(*microvolt), GFP_KERNEL);
200 if (!microvolt)
201 return -ENOMEM;
202
203 ret = of_property_read_u32_array(opp->np, name, microvolt, vcount);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530204 if (ret) {
205 dev_err(dev, "%s: error parsing %s: %d\n", __func__, name, ret);
Viresh Kumardfbe4672016-12-01 16:28:19 +0530206 ret = -EINVAL;
207 goto free_microvolt;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530208 }
209
210 /* Search for "opp-microamp-<name>" */
211 prop = NULL;
212 if (opp_table->prop_name) {
213 snprintf(name, sizeof(name), "opp-microamp-%s",
214 opp_table->prop_name);
215 prop = of_find_property(opp->np, name, NULL);
216 }
217
218 if (!prop) {
219 /* Search for "opp-microamp" */
220 sprintf(name, "opp-microamp");
221 prop = of_find_property(opp->np, name, NULL);
222 }
223
Viresh Kumardfbe4672016-12-01 16:28:19 +0530224 if (prop) {
225 icount = of_property_count_u32_elems(opp->np, name);
226 if (icount < 0) {
227 dev_err(dev, "%s: Invalid %s property (%d)\n", __func__,
228 name, icount);
229 ret = icount;
230 goto free_microvolt;
231 }
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530232
Viresh Kumardfbe4672016-12-01 16:28:19 +0530233 if (icount != supplies) {
234 dev_err(dev, "%s: Invalid number of elements in %s property (%d) with supplies (%d)\n",
235 __func__, name, icount, supplies);
236 ret = -EINVAL;
237 goto free_microvolt;
238 }
239
240 microamp = kmalloc_array(icount, sizeof(*microamp), GFP_KERNEL);
241 if (!microamp) {
242 ret = -EINVAL;
243 goto free_microvolt;
244 }
245
246 ret = of_property_read_u32_array(opp->np, name, microamp,
247 icount);
248 if (ret) {
249 dev_err(dev, "%s: error parsing %s: %d\n", __func__,
250 name, ret);
251 ret = -EINVAL;
252 goto free_microamp;
253 }
254 }
255
256 for (i = 0, j = 0; i < supplies; i++) {
257 opp->supplies[i].u_volt = microvolt[j++];
258
259 if (vcount == supplies) {
260 opp->supplies[i].u_volt_min = opp->supplies[i].u_volt;
261 opp->supplies[i].u_volt_max = opp->supplies[i].u_volt;
262 } else {
263 opp->supplies[i].u_volt_min = microvolt[j++];
264 opp->supplies[i].u_volt_max = microvolt[j++];
265 }
266
267 if (microamp)
268 opp->supplies[i].u_amp = microamp[i];
269 }
270
271free_microamp:
272 kfree(microamp);
273free_microvolt:
274 kfree(microvolt);
275
276 return ret;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530277}
278
279/**
280 * dev_pm_opp_of_remove_table() - Free OPP table entries created from static DT
281 * entries
282 * @dev: device pointer used to lookup OPP table.
283 *
284 * Free OPPs created using static entries present in DT.
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530285 */
286void dev_pm_opp_of_remove_table(struct device *dev)
287{
Viresh Kumar2a4eb732018-09-13 13:14:36 +0530288 _dev_pm_opp_find_and_remove_table(dev);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530289}
290EXPORT_SYMBOL_GPL(dev_pm_opp_of_remove_table);
291
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530292/**
293 * _opp_add_static_v2() - Allocate static OPPs (As per 'v2' DT bindings)
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +0530294 * @opp_table: OPP table
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530295 * @dev: device for which we do this operation
296 * @np: device node
297 *
298 * This function adds an opp definition to the opp table and returns status. The
299 * opp can be controlled using dev_pm_opp_enable/disable functions and may be
300 * removed by dev_pm_opp_remove.
301 *
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530302 * Return:
Dave Gerlachdeac8702018-10-03 16:13:15 +0530303 * Valid OPP pointer:
304 * On success
305 * NULL:
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530306 * Duplicate OPPs (both freq and volt are same) and opp->available
Dave Gerlachdeac8702018-10-03 16:13:15 +0530307 * OR if the OPP is not supported by hardware.
308 * ERR_PTR(-EEXIST):
309 * Freq are same and volt are different OR
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530310 * Duplicate OPPs (both freq and volt are same) and !opp->available
Dave Gerlachdeac8702018-10-03 16:13:15 +0530311 * ERR_PTR(-ENOMEM):
312 * Memory allocation failure
313 * ERR_PTR(-EINVAL):
314 * Failed parsing the OPP node
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530315 */
Dave Gerlachdeac8702018-10-03 16:13:15 +0530316static struct dev_pm_opp *_opp_add_static_v2(struct opp_table *opp_table,
317 struct device *dev, struct device_node *np)
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530318{
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530319 struct dev_pm_opp *new_opp;
Dan Carpenter6a89e012018-05-16 12:52:41 +0300320 u64 rate = 0;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530321 u32 val;
322 int ret;
Viresh Kumara1e8c132018-04-06 14:35:45 +0530323 bool rate_not_available = false;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530324
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +0530325 new_opp = _opp_allocate(opp_table);
326 if (!new_opp)
Dave Gerlachdeac8702018-10-03 16:13:15 +0530327 return ERR_PTR(-ENOMEM);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530328
329 ret = of_property_read_u64(np, "opp-hz", &rate);
330 if (ret < 0) {
Viresh Kumara1e8c132018-04-06 14:35:45 +0530331 /* "opp-hz" is optional for devices like power domains. */
Viresh Kumar61d8e7c2018-06-13 16:00:11 +0530332 if (!opp_table->is_genpd) {
Viresh Kumara1e8c132018-04-06 14:35:45 +0530333 dev_err(dev, "%s: opp-hz not found\n", __func__);
334 goto free_opp;
335 }
336
337 rate_not_available = true;
338 } else {
339 /*
340 * Rate is defined as an unsigned long in clk API, and so
341 * casting explicitly to its type. Must be fixed once rate is 64
342 * bit guaranteed in clk API.
343 */
344 new_opp->rate = (unsigned long)rate;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530345 }
346
347 /* Check if the OPP supports hardware's hierarchy of versions or not */
348 if (!_opp_is_supported(dev, opp_table, np)) {
349 dev_dbg(dev, "OPP not supported by hardware: %llu\n", rate);
350 goto free_opp;
351 }
352
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530353 new_opp->turbo = of_property_read_bool(np, "turbo-mode");
354
355 new_opp->np = np;
356 new_opp->dynamic = false;
357 new_opp->available = true;
358
359 if (!of_property_read_u32(np, "clock-latency-ns", &val))
360 new_opp->clock_latency_ns = val;
361
Viresh Kumar3ba98322016-11-18 15:47:46 +0530362 new_opp->pstate = of_genpd_opp_to_performance_state(dev, np);
363
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530364 ret = opp_parse_supplies(new_opp, dev, opp_table);
365 if (ret)
366 goto free_opp;
367
Viresh Kumara1e8c132018-04-06 14:35:45 +0530368 ret = _opp_add(dev, new_opp, opp_table, rate_not_available);
Viresh Kumar7f8538e2017-01-02 14:40:55 +0530369 if (ret) {
370 /* Don't return error for duplicate OPPs */
371 if (ret == -EBUSY)
372 ret = 0;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530373 goto free_opp;
Viresh Kumar7f8538e2017-01-02 14:40:55 +0530374 }
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530375
376 /* OPP to select on device suspend */
377 if (of_property_read_bool(np, "opp-suspend")) {
378 if (opp_table->suspend_opp) {
379 dev_warn(dev, "%s: Multiple suspend OPPs found (%lu %lu)\n",
380 __func__, opp_table->suspend_opp->rate,
381 new_opp->rate);
382 } else {
383 new_opp->suspend = true;
384 opp_table->suspend_opp = new_opp;
385 }
386 }
387
388 if (new_opp->clock_latency_ns > opp_table->clock_latency_ns_max)
389 opp_table->clock_latency_ns_max = new_opp->clock_latency_ns;
390
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530391 pr_debug("%s: turbo:%d rate:%lu uv:%lu uvmin:%lu uvmax:%lu latency:%lu\n",
Viresh Kumar0f0fe7e2016-12-01 16:28:17 +0530392 __func__, new_opp->turbo, new_opp->rate,
Viresh Kumardfbe4672016-12-01 16:28:19 +0530393 new_opp->supplies[0].u_volt, new_opp->supplies[0].u_volt_min,
394 new_opp->supplies[0].u_volt_max, new_opp->clock_latency_ns);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530395
396 /*
397 * Notify the changes in the availability of the operable
398 * frequency/voltage list.
399 */
Viresh Kumar052c6f12017-01-23 10:11:49 +0530400 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
Dave Gerlachdeac8702018-10-03 16:13:15 +0530401 return new_opp;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530402
403free_opp:
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +0530404 _opp_free(new_opp);
405
Dave Gerlachdeac8702018-10-03 16:13:15 +0530406 return ERR_PTR(ret);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530407}
408
409/* Initializes OPP tables based on new bindings */
Viresh Kumar5ed4cec2018-09-12 11:21:17 +0530410static int _of_add_opp_table_v2(struct device *dev, struct opp_table *opp_table)
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530411{
412 struct device_node *np;
Viresh Kumar283d55e2018-09-07 09:01:54 +0530413 int ret, count = 0, pstate_count = 0;
Viresh Kumar3ba98322016-11-18 15:47:46 +0530414 struct dev_pm_opp *opp;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530415
Viresh Kumar283d55e2018-09-07 09:01:54 +0530416 /* OPP table is already initialized for the device */
417 if (opp_table->parsed_static_opps) {
418 kref_get(&opp_table->list_kref);
419 return 0;
420 }
421
Viresh Kumard0e8ae62018-09-11 11:14:11 +0530422 kref_init(&opp_table->list_kref);
423
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530424 /* We have opp-table node now, iterate over it and add OPPs */
Viresh Kumar5ed4cec2018-09-12 11:21:17 +0530425 for_each_available_child_of_node(opp_table->np, np) {
Dave Gerlachdeac8702018-10-03 16:13:15 +0530426 opp = _opp_add_static_v2(opp_table, dev, np);
427 if (IS_ERR(opp)) {
428 ret = PTR_ERR(opp);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530429 dev_err(dev, "%s: Failed to add OPP, %d\n", __func__,
430 ret);
Tobias Jordan7978db32017-10-04 11:35:03 +0530431 of_node_put(np);
Viresh Kumarcdd6ed92018-09-12 12:35:19 +0530432 goto put_list_kref;
Dave Gerlachdeac8702018-10-03 16:13:15 +0530433 } else if (opp) {
434 count++;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530435 }
436 }
437
438 /* There should be one of more OPP defined */
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +0530439 if (WARN_ON(!count)) {
440 ret = -ENOENT;
Viresh Kumarcdd6ed92018-09-12 12:35:19 +0530441 goto put_list_kref;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530442 }
443
Viresh Kumar3ba98322016-11-18 15:47:46 +0530444 list_for_each_entry(opp, &opp_table->opp_list, node)
445 pstate_count += !!opp->pstate;
446
447 /* Either all or none of the nodes shall have performance state set */
448 if (pstate_count && pstate_count != count) {
449 dev_err(dev, "Not all nodes have performance state set (%d: %d)\n",
450 count, pstate_count);
451 ret = -ENOENT;
Viresh Kumarcdd6ed92018-09-12 12:35:19 +0530452 goto put_list_kref;
Viresh Kumar3ba98322016-11-18 15:47:46 +0530453 }
454
455 if (pstate_count)
456 opp_table->genpd_performance_state = true;
457
Viresh Kumarf06ed902018-06-14 12:04:43 +0530458 opp_table->parsed_static_opps = true;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530459
Viresh Kumarcdd6ed92018-09-12 12:35:19 +0530460 return 0;
461
462put_list_kref:
463 _put_opp_list_kref(opp_table);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530464
465 return ret;
466}
467
468/* Initializes OPP tables based on old-deprecated bindings */
Viresh Kumar5ed4cec2018-09-12 11:21:17 +0530469static int _of_add_opp_table_v1(struct device *dev, struct opp_table *opp_table)
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530470{
471 const struct property *prop;
472 const __be32 *val;
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +0530473 int nr, ret = 0;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530474
475 prop = of_find_property(dev->of_node, "operating-points", NULL);
476 if (!prop)
477 return -ENODEV;
478 if (!prop->value)
479 return -ENODATA;
480
481 /*
482 * Each OPP is a set of tuples consisting of frequency and
483 * voltage like <freq-kHz vol-uV>.
484 */
485 nr = prop->length / sizeof(u32);
486 if (nr % 2) {
487 dev_err(dev, "%s: Invalid OPP table\n", __func__);
488 return -EINVAL;
489 }
490
Viresh Kumard0e8ae62018-09-11 11:14:11 +0530491 kref_init(&opp_table->list_kref);
492
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530493 val = prop->value;
494 while (nr) {
495 unsigned long freq = be32_to_cpup(val++) * 1000;
496 unsigned long volt = be32_to_cpup(val++);
497
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +0530498 ret = _opp_add_v1(opp_table, dev, freq, volt, false);
Viresh Kumar04a86a82017-01-02 14:40:58 +0530499 if (ret) {
500 dev_err(dev, "%s: Failed to add OPP %ld (%d)\n",
501 __func__, freq, ret);
Viresh Kumarcdd6ed92018-09-12 12:35:19 +0530502 _put_opp_list_kref(opp_table);
Viresh Kumarcdd6ed92018-09-12 12:35:19 +0530503 return ret;
Viresh Kumar04a86a82017-01-02 14:40:58 +0530504 }
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530505 nr -= 2;
506 }
507
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +0530508 return ret;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530509}
510
511/**
512 * dev_pm_opp_of_add_table() - Initialize opp table from device tree
513 * @dev: device pointer used to lookup OPP table.
514 *
515 * Register the initial OPP table with the OPP library for given device.
516 *
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530517 * Return:
518 * 0 On success OR
519 * Duplicate OPPs (both freq and volt are same) and opp->available
520 * -EEXIST Freq are same and volt are different OR
521 * Duplicate OPPs (both freq and volt are same) and !opp->available
522 * -ENOMEM Memory allocation failure
523 * -ENODEV when 'operating-points' property is not found or is invalid data
524 * in device node.
525 * -ENODATA when empty 'operating-points' property is found
526 * -EINVAL when invalid entries are found in opp-v2 table
527 */
528int dev_pm_opp_of_add_table(struct device *dev)
529{
Viresh Kumar5ed4cec2018-09-12 11:21:17 +0530530 struct opp_table *opp_table;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530531 int ret;
532
Viresh Kumar5ed4cec2018-09-12 11:21:17 +0530533 opp_table = dev_pm_opp_get_opp_table_indexed(dev, 0);
534 if (!opp_table)
535 return -ENOMEM;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530536
Viresh Kumar5ed4cec2018-09-12 11:21:17 +0530537 /*
538 * OPPs have two version of bindings now. Also try the old (v1)
539 * bindings for backward compatibility with older dtbs.
540 */
541 if (opp_table->np)
542 ret = _of_add_opp_table_v2(dev, opp_table);
543 else
544 ret = _of_add_opp_table_v1(dev, opp_table);
545
546 if (ret)
547 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530548
549 return ret;
550}
551EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table);
552
Viresh Kumarfa9b2742017-04-26 10:45:46 +0530553/**
554 * dev_pm_opp_of_add_table_indexed() - Initialize indexed opp table from device tree
555 * @dev: device pointer used to lookup OPP table.
556 * @index: Index number.
557 *
558 * Register the initial OPP table with the OPP library for given device only
559 * using the "operating-points-v2" property.
560 *
561 * Return:
562 * 0 On success OR
563 * Duplicate OPPs (both freq and volt are same) and opp->available
564 * -EEXIST Freq are same and volt are different OR
565 * Duplicate OPPs (both freq and volt are same) and !opp->available
566 * -ENOMEM Memory allocation failure
567 * -ENODEV when 'operating-points' property is not found or is invalid data
568 * in device node.
569 * -ENODATA when empty 'operating-points' property is found
570 * -EINVAL when invalid entries are found in opp-v2 table
571 */
572int dev_pm_opp_of_add_table_indexed(struct device *dev, int index)
573{
Viresh Kumar5ed4cec2018-09-12 11:21:17 +0530574 struct opp_table *opp_table;
Viresh Kumar8a352fd2018-05-30 15:19:38 +0530575 int ret, count;
Viresh Kumarfa9b2742017-04-26 10:45:46 +0530576
Viresh Kumar5ed4cec2018-09-12 11:21:17 +0530577 if (index) {
Viresh Kumar8a352fd2018-05-30 15:19:38 +0530578 /*
579 * If only one phandle is present, then the same OPP table
580 * applies for all index requests.
581 */
582 count = of_count_phandle_with_args(dev->of_node,
583 "operating-points-v2", NULL);
Viresh Kumar5ed4cec2018-09-12 11:21:17 +0530584 if (count != 1)
585 return -ENODEV;
Viresh Kumar8a352fd2018-05-30 15:19:38 +0530586
Viresh Kumar5ed4cec2018-09-12 11:21:17 +0530587 index = 0;
Viresh Kumar8a352fd2018-05-30 15:19:38 +0530588 }
Viresh Kumarfa9b2742017-04-26 10:45:46 +0530589
Viresh Kumar5ed4cec2018-09-12 11:21:17 +0530590 opp_table = dev_pm_opp_get_opp_table_indexed(dev, index);
591 if (!opp_table)
592 return -ENOMEM;
593
594 ret = _of_add_opp_table_v2(dev, opp_table);
595 if (ret)
596 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumarfa9b2742017-04-26 10:45:46 +0530597
598 return ret;
599}
600EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table_indexed);
601
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530602/* CPU device specific helpers */
603
604/**
605 * dev_pm_opp_of_cpumask_remove_table() - Removes OPP table for @cpumask
606 * @cpumask: cpumask for which OPP table needs to be removed
607 *
608 * This removes the OPP tables for CPUs present in the @cpumask.
609 * This should be used only to remove static entries created from DT.
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530610 */
611void dev_pm_opp_of_cpumask_remove_table(const struct cpumask *cpumask)
612{
Viresh Kumar2a4eb732018-09-13 13:14:36 +0530613 _dev_pm_opp_cpumask_remove_table(cpumask, -1);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530614}
615EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_remove_table);
616
617/**
618 * dev_pm_opp_of_cpumask_add_table() - Adds OPP table for @cpumask
619 * @cpumask: cpumask for which OPP table needs to be added.
620 *
621 * This adds the OPP tables for CPUs present in the @cpumask.
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530622 */
623int dev_pm_opp_of_cpumask_add_table(const struct cpumask *cpumask)
624{
625 struct device *cpu_dev;
Viresh Kumar50b6b872018-10-03 15:12:06 +0530626 int cpu, ret;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530627
Viresh Kumar50b6b872018-10-03 15:12:06 +0530628 if (WARN_ON(cpumask_empty(cpumask)))
629 return -ENODEV;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530630
631 for_each_cpu(cpu, cpumask) {
632 cpu_dev = get_cpu_device(cpu);
633 if (!cpu_dev) {
634 pr_err("%s: failed to get cpu%d device\n", __func__,
635 cpu);
Viresh Kumar50b6b872018-10-03 15:12:06 +0530636 ret = -ENODEV;
637 goto remove_table;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530638 }
639
640 ret = dev_pm_opp_of_add_table(cpu_dev);
641 if (ret) {
Viresh Kumar5b606972017-07-12 09:21:49 +0530642 /*
643 * OPP may get registered dynamically, don't print error
644 * message here.
645 */
646 pr_debug("%s: couldn't find opp table for cpu:%d, %d\n",
647 __func__, cpu, ret);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530648
Viresh Kumar50b6b872018-10-03 15:12:06 +0530649 goto remove_table;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530650 }
651 }
652
Viresh Kumar50b6b872018-10-03 15:12:06 +0530653 return 0;
654
655remove_table:
656 /* Free all other OPPs */
657 _dev_pm_opp_cpumask_remove_table(cpumask, cpu);
658
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530659 return ret;
660}
661EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_add_table);
662
663/*
664 * Works only for OPP v2 bindings.
665 *
666 * Returns -ENOENT if operating-points-v2 bindings aren't supported.
667 */
668/**
669 * dev_pm_opp_of_get_sharing_cpus() - Get cpumask of CPUs sharing OPPs with
670 * @cpu_dev using operating-points-v2
671 * bindings.
672 *
673 * @cpu_dev: CPU device for which we do this operation
674 * @cpumask: cpumask to update with information of sharing CPUs
675 *
676 * This updates the @cpumask with CPUs that are sharing OPPs with @cpu_dev.
677 *
678 * Returns -ENOENT if operating-points-v2 isn't present for @cpu_dev.
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530679 */
680int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev,
681 struct cpumask *cpumask)
682{
Waldemar Rymarkiewicz76279292017-07-27 12:01:17 +0200683 struct device_node *np, *tmp_np, *cpu_np;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530684 int cpu, ret = 0;
685
686 /* Get OPP descriptor node */
Dave Gerlach0764c602017-02-03 11:29:26 -0600687 np = dev_pm_opp_of_get_opp_desc_node(cpu_dev);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530688 if (!np) {
Masahiro Yamada349aa922016-10-20 16:12:49 +0900689 dev_dbg(cpu_dev, "%s: Couldn't find opp node.\n", __func__);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530690 return -ENOENT;
691 }
692
693 cpumask_set_cpu(cpu_dev->id, cpumask);
694
695 /* OPPs are shared ? */
696 if (!of_property_read_bool(np, "opp-shared"))
697 goto put_cpu_node;
698
699 for_each_possible_cpu(cpu) {
700 if (cpu == cpu_dev->id)
701 continue;
702
Sudeep Holla98679992017-10-12 11:32:23 +0100703 cpu_np = of_cpu_device_node_get(cpu);
Waldemar Rymarkiewicz76279292017-07-27 12:01:17 +0200704 if (!cpu_np) {
705 dev_err(cpu_dev, "%s: failed to get cpu%d node\n",
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530706 __func__, cpu);
Waldemar Rymarkiewicz76279292017-07-27 12:01:17 +0200707 ret = -ENOENT;
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530708 goto put_cpu_node;
709 }
710
711 /* Get OPP descriptor node */
Viresh Kumarfa9b2742017-04-26 10:45:46 +0530712 tmp_np = _opp_of_get_opp_desc_node(cpu_np, 0);
Sudeep Holla98679992017-10-12 11:32:23 +0100713 of_node_put(cpu_np);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530714 if (!tmp_np) {
Waldemar Rymarkiewicz76279292017-07-27 12:01:17 +0200715 pr_err("%pOF: Couldn't find opp node\n", cpu_np);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530716 ret = -ENOENT;
717 goto put_cpu_node;
718 }
719
720 /* CPUs are sharing opp node */
721 if (np == tmp_np)
722 cpumask_set_cpu(cpu, cpumask);
723
724 of_node_put(tmp_np);
725 }
726
727put_cpu_node:
728 of_node_put(np);
729 return ret;
730}
731EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_sharing_cpus);
Viresh Kumara88bd2a2017-11-29 15:18:36 +0530732
733/**
734 * of_dev_pm_opp_find_required_opp() - Search for required OPP.
735 * @dev: The device whose OPP node is referenced by the 'np' DT node.
736 * @np: Node that contains the "required-opps" property.
737 *
738 * Returns the OPP of the device 'dev', whose phandle is present in the "np"
739 * node. Although the "required-opps" property supports having multiple
740 * phandles, this helper routine only parses the very first phandle in the list.
741 *
742 * Return: Matching opp, else returns ERR_PTR in case of error and should be
743 * handled using IS_ERR.
744 *
745 * The callers are required to call dev_pm_opp_put() for the returned OPP after
746 * use.
747 */
748struct dev_pm_opp *of_dev_pm_opp_find_required_opp(struct device *dev,
749 struct device_node *np)
750{
751 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ENODEV);
752 struct device_node *required_np;
753 struct opp_table *opp_table;
754
755 opp_table = _find_opp_table(dev);
756 if (IS_ERR(opp_table))
757 return ERR_CAST(opp_table);
758
759 required_np = of_parse_phandle(np, "required-opps", 0);
760 if (unlikely(!required_np)) {
761 dev_err(dev, "Unable to parse required-opps\n");
762 goto put_opp_table;
763 }
764
765 mutex_lock(&opp_table->lock);
766
767 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
768 if (temp_opp->available && temp_opp->np == required_np) {
769 opp = temp_opp;
770
771 /* Increment the reference count of OPP */
772 dev_pm_opp_get(opp);
773 break;
774 }
775 }
776
777 mutex_unlock(&opp_table->lock);
778
779 of_node_put(required_np);
780put_opp_table:
781 dev_pm_opp_put_opp_table(opp_table);
782
783 return opp;
784}
785EXPORT_SYMBOL_GPL(of_dev_pm_opp_find_required_opp);
Viresh Kumare2f4b5f2018-01-12 10:03:45 +0530786
787/**
788 * dev_pm_opp_get_of_node() - Gets the DT node corresponding to an opp
789 * @opp: opp for which DT node has to be returned for
790 *
791 * Return: DT node corresponding to the opp, else 0 on success.
792 *
793 * The caller needs to put the node with of_node_put() after using it.
794 */
795struct device_node *dev_pm_opp_get_of_node(struct dev_pm_opp *opp)
796{
797 if (IS_ERR_OR_NULL(opp)) {
798 pr_err("%s: Invalid parameters\n", __func__);
799 return NULL;
800 }
801
802 return of_node_get(opp->np);
803}
804EXPORT_SYMBOL_GPL(dev_pm_opp_get_of_node);