1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2016-2017 Texas Instruments Incorporated - http://www.ti.com/
4 * Nishanth Menon <nm@ti.com>
5 * Dave Gerlach <d-gerlach@ti.com>
7 * TI OPP supply driver that provides override into the regulator control
8 * for generic opp core to handle devices with ABB regulator and/or
11 #include <linux/clk.h>
12 #include <linux/cpufreq.h>
13 #include <linux/device.h>
15 #include <linux/module.h>
16 #include <linux/notifier.h>
17 #include <linux/of_device.h>
19 #include <linux/platform_device.h>
20 #include <linux/pm_opp.h>
21 #include <linux/regulator/consumer.h>
22 #include <linux/slab.h>
25 * struct ti_opp_supply_optimum_voltage_table - optimized voltage table
26 * @reference_uv: reference voltage (usually Nominal voltage)
27 * @optimized_uv: Optimized voltage from efuse
29 struct ti_opp_supply_optimum_voltage_table
{
30 unsigned int reference_uv
;
31 unsigned int optimized_uv
;
35 * struct ti_opp_supply_data - OMAP specific opp supply data
36 * @vdd_table: Optimized voltage mapping table
37 * @num_vdd_table: number of entries in vdd_table
38 * @vdd_absolute_max_voltage_uv: absolute maximum voltage in UV for the supply
40 struct ti_opp_supply_data
{
41 struct ti_opp_supply_optimum_voltage_table
*vdd_table
;
43 u32 vdd_absolute_max_voltage_uv
;
46 static struct ti_opp_supply_data opp_data
;
49 * struct ti_opp_supply_of_data - device tree match data
50 * @flags: specific type of opp supply
51 * @efuse_voltage_mask: mask required for efuse register representing voltage
52 * @efuse_voltage_uv: Are the efuse entries in micro-volts? if not, assume
55 struct ti_opp_supply_of_data
{
56 #define OPPDM_EFUSE_CLASS0_OPTIMIZED_VOLTAGE BIT(1)
57 #define OPPDM_HAS_NO_ABB BIT(2)
59 const u32 efuse_voltage_mask
;
60 const bool efuse_voltage_uv
;
64 * _store_optimized_voltages() - store optimized voltages
65 * @dev: ti opp supply device for which we need to store info
66 * @data: data specific to the device
68 * Picks up efuse based optimized voltages for VDD unique per device and
69 * stores it in internal data structure for use during transition requests.
71 * Return: If successful, 0, else appropriate error value.
73 static int _store_optimized_voltages(struct device
*dev
,
74 struct ti_opp_supply_data
*data
)
77 struct property
*prop
;
82 struct ti_opp_supply_optimum_voltage_table
*table
;
83 const struct ti_opp_supply_of_data
*of_data
= dev_get_drvdata(dev
);
85 /* pick up Efuse based voltages */
86 res
= platform_get_resource(to_platform_device(dev
), IORESOURCE_MEM
, 0);
88 dev_err(dev
, "Unable to get IO resource\n");
93 base
= ioremap_nocache(res
->start
, resource_size(res
));
95 dev_err(dev
, "Unable to map Efuse registers\n");
100 /* Fetch efuse-settings. */
101 prop
= of_find_property(dev
->of_node
, "ti,efuse-settings", NULL
);
103 dev_err(dev
, "No 'ti,efuse-settings' property found\n");
108 proplen
= prop
->length
/ sizeof(int);
109 data
->num_vdd_table
= proplen
/ 2;
110 /* Verify for corrupted OPP entries in dt */
111 if (data
->num_vdd_table
* 2 * sizeof(int) != prop
->length
) {
112 dev_err(dev
, "Invalid 'ti,efuse-settings'\n");
117 ret
= of_property_read_u32(dev
->of_node
, "ti,absolute-max-voltage-uv",
118 &data
->vdd_absolute_max_voltage_uv
);
120 dev_err(dev
, "ti,absolute-max-voltage-uv is missing\n");
125 table
= kzalloc(sizeof(*data
->vdd_table
) *
126 data
->num_vdd_table
, GFP_KERNEL
);
131 data
->vdd_table
= table
;
134 for (i
= 0; i
< data
->num_vdd_table
; i
++, table
++) {
138 table
->reference_uv
= be32_to_cpup(val
++);
139 efuse_offset
= be32_to_cpup(val
++);
141 tmp
= readl(base
+ efuse_offset
);
142 tmp
&= of_data
->efuse_voltage_mask
;
143 tmp
>>= __ffs(of_data
->efuse_voltage_mask
);
145 table
->optimized_uv
= of_data
->efuse_voltage_uv
? tmp
:
148 dev_dbg(dev
, "[%d] efuse=0x%08x volt_table=%d vset=%d\n",
149 i
, efuse_offset
, table
->reference_uv
,
150 table
->optimized_uv
);
153 * Some older samples might not have optimized efuse
154 * Use reference voltage for those - just add debug message
157 if (!table
->optimized_uv
) {
158 dev_dbg(dev
, "[%d] efuse=0x%08x volt_table=%d:vset0\n",
159 i
, efuse_offset
, table
->reference_uv
);
160 table
->optimized_uv
= table
->reference_uv
;
170 * _free_optimized_voltages() - free resources for optvoltages
171 * @dev: device for which we need to free info
172 * @data: data specific to the device
174 static void _free_optimized_voltages(struct device
*dev
,
175 struct ti_opp_supply_data
*data
)
177 kfree(data
->vdd_table
);
178 data
->vdd_table
= NULL
;
179 data
->num_vdd_table
= 0;
183 * _get_optimal_vdd_voltage() - Finds optimal voltage for the supply
184 * @dev: device for which we need to find info
185 * @data: data specific to the device
186 * @reference_uv: reference voltage (OPP voltage) for which we need value
188 * Return: if a match is found, return optimized voltage, else return
189 * reference_uv, also return reference_uv if no optimization is needed.
191 static int _get_optimal_vdd_voltage(struct device
*dev
,
192 struct ti_opp_supply_data
*data
,
196 struct ti_opp_supply_optimum_voltage_table
*table
;
198 if (!data
->num_vdd_table
)
201 table
= data
->vdd_table
;
205 /* Find a exact match - this list is usually very small */
206 for (i
= 0; i
< data
->num_vdd_table
; i
++, table
++)
207 if (table
->reference_uv
== reference_uv
)
208 return table
->optimized_uv
;
210 /* IF things are screwed up, we'd make a mess on console.. ratelimit */
211 dev_err_ratelimited(dev
, "%s: Failed optimized voltage match for %d\n",
212 __func__
, reference_uv
);
216 static int _opp_set_voltage(struct device
*dev
,
217 struct dev_pm_opp_supply
*supply
,
218 int new_target_uv
, struct regulator
*reg
,
222 unsigned long vdd_uv
, uv_max
;
225 vdd_uv
= new_target_uv
;
227 vdd_uv
= supply
->u_volt
;
230 * If we do have an absolute max voltage specified, then we should
231 * use that voltage instead to allow for cases where the voltage rails
232 * are ganged (example if we set the max for an opp as 1.12v, and
233 * the absolute max is 1.5v, for another rail to get 1.25v, it cannot
234 * be achieved if the regulator is constrainted to max of 1.12v, even
235 * if it can function at 1.25v
237 if (opp_data
.vdd_absolute_max_voltage_uv
)
238 uv_max
= opp_data
.vdd_absolute_max_voltage_uv
;
240 uv_max
= supply
->u_volt_max
;
242 if (vdd_uv
> uv_max
||
243 vdd_uv
< supply
->u_volt_min
||
244 supply
->u_volt_min
> uv_max
) {
246 "Invalid range voltages [Min:%lu target:%lu Max:%lu]\n",
247 supply
->u_volt_min
, vdd_uv
, uv_max
);
251 dev_dbg(dev
, "%s scaling to %luuV[min %luuV max %luuV]\n", reg_name
,
252 vdd_uv
, supply
->u_volt_min
,
255 ret
= regulator_set_voltage_triplet(reg
,
260 dev_err(dev
, "%s failed for %luuV[min %luuV max %luuV]\n",
261 reg_name
, vdd_uv
, supply
->u_volt_min
,
270 * ti_opp_supply_set_opp() - do the opp supply transition
271 * @data: information on regulators and new and old opps provided by
272 * opp core to use in transition
274 * Return: If successful, 0, else appropriate error value.
276 static int ti_opp_supply_set_opp(struct dev_pm_set_opp_data
*data
)
278 struct dev_pm_opp_supply
*old_supply_vdd
= &data
->old_opp
.supplies
[0];
279 struct dev_pm_opp_supply
*old_supply_vbb
= &data
->old_opp
.supplies
[1];
280 struct dev_pm_opp_supply
*new_supply_vdd
= &data
->new_opp
.supplies
[0];
281 struct dev_pm_opp_supply
*new_supply_vbb
= &data
->new_opp
.supplies
[1];
282 struct device
*dev
= data
->dev
;
283 unsigned long old_freq
= data
->old_opp
.rate
, freq
= data
->new_opp
.rate
;
284 struct clk
*clk
= data
->clk
;
285 struct regulator
*vdd_reg
= data
->regulators
[0];
286 struct regulator
*vbb_reg
= data
->regulators
[1];
290 vdd_uv
= _get_optimal_vdd_voltage(dev
, &opp_data
,
291 new_supply_vbb
->u_volt
);
293 /* Scaling up? Scale voltage before frequency */
294 if (freq
> old_freq
) {
295 ret
= _opp_set_voltage(dev
, new_supply_vdd
, vdd_uv
, vdd_reg
,
298 goto restore_voltage
;
300 ret
= _opp_set_voltage(dev
, new_supply_vbb
, 0, vbb_reg
, "vbb");
302 goto restore_voltage
;
305 /* Change frequency */
306 dev_dbg(dev
, "%s: switching OPP: %lu Hz --> %lu Hz\n",
307 __func__
, old_freq
, freq
);
309 ret
= clk_set_rate(clk
, freq
);
311 dev_err(dev
, "%s: failed to set clock rate: %d\n", __func__
,
313 goto restore_voltage
;
316 /* Scaling down? Scale voltage after frequency */
317 if (freq
< old_freq
) {
318 ret
= _opp_set_voltage(dev
, new_supply_vbb
, 0, vbb_reg
, "vbb");
322 ret
= _opp_set_voltage(dev
, new_supply_vdd
, vdd_uv
, vdd_reg
,
331 ret
= clk_set_rate(clk
, old_freq
);
333 dev_err(dev
, "%s: failed to restore old-freq (%lu Hz)\n",
336 /* This shouldn't harm even if the voltages weren't updated earlier */
337 if (old_supply_vdd
->u_volt
) {
338 ret
= _opp_set_voltage(dev
, old_supply_vbb
, 0, vbb_reg
, "vbb");
342 ret
= _opp_set_voltage(dev
, old_supply_vdd
, 0, vdd_reg
,
351 static const struct ti_opp_supply_of_data omap_generic_of_data
= {
354 static const struct ti_opp_supply_of_data omap_omap5_of_data
= {
355 .flags
= OPPDM_EFUSE_CLASS0_OPTIMIZED_VOLTAGE
,
356 .efuse_voltage_mask
= 0xFFF,
357 .efuse_voltage_uv
= false,
360 static const struct ti_opp_supply_of_data omap_omap5core_of_data
= {
361 .flags
= OPPDM_EFUSE_CLASS0_OPTIMIZED_VOLTAGE
| OPPDM_HAS_NO_ABB
,
362 .efuse_voltage_mask
= 0xFFF,
363 .efuse_voltage_uv
= false,
366 static const struct of_device_id ti_opp_supply_of_match
[] = {
367 {.compatible
= "ti,omap-opp-supply", .data
= &omap_generic_of_data
},
368 {.compatible
= "ti,omap5-opp-supply", .data
= &omap_omap5_of_data
},
369 {.compatible
= "ti,omap5-core-opp-supply",
370 .data
= &omap_omap5core_of_data
},
373 MODULE_DEVICE_TABLE(of
, ti_opp_supply_of_match
);
375 static int ti_opp_supply_probe(struct platform_device
*pdev
)
377 struct device
*dev
= &pdev
->dev
;
378 struct device
*cpu_dev
= get_cpu_device(0);
379 const struct of_device_id
*match
;
380 const struct ti_opp_supply_of_data
*of_data
;
383 match
= of_match_device(ti_opp_supply_of_match
, dev
);
385 /* We do not expect this to happen */
386 dev_err(dev
, "%s: Unable to match device\n", __func__
);
390 /* Again, unlikely.. but mistakes do happen */
391 dev_err(dev
, "%s: Bad data in match\n", __func__
);
394 of_data
= match
->data
;
396 dev_set_drvdata(dev
, (void *)of_data
);
398 /* If we need optimized voltage */
399 if (of_data
->flags
& OPPDM_EFUSE_CLASS0_OPTIMIZED_VOLTAGE
) {
400 ret
= _store_optimized_voltages(dev
, &opp_data
);
405 ret
= PTR_ERR_OR_ZERO(dev_pm_opp_register_set_opp_helper(cpu_dev
,
406 ti_opp_supply_set_opp
));
408 _free_optimized_voltages(dev
, &opp_data
);
413 static struct platform_driver ti_opp_supply_driver
= {
414 .probe
= ti_opp_supply_probe
,
416 .name
= "ti_opp_supply",
417 .owner
= THIS_MODULE
,
418 .of_match_table
= of_match_ptr(ti_opp_supply_of_match
),
421 module_platform_driver(ti_opp_supply_driver
);
423 MODULE_DESCRIPTION("Texas Instruments OMAP OPP Supply driver");
424 MODULE_AUTHOR("Texas Instruments Inc.");
425 MODULE_LICENSE("GPL v2");