hc
2024-05-10 61598093bbdd283a7edc367d900f223070ead8d2
kernel/drivers/devfreq/exynos-bus.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * Generic Exynos Bus frequency driver with DEVFREQ Framework
34 *
....@@ -6,10 +7,6 @@
67 *
78 * This driver support Exynos Bus frequency feature by using
89 * DEVFREQ framework and is based on drivers/devfreq/exynos/exynos4_bus.c.
9
- *
10
- * This program is free software; you can redistribute it and/or modify
11
- * it under the terms of the GNU General Public License version 2 as
12
- * published by the Free Software Foundation.
1310 */
1411
1512 #include <linux/clk.h>
....@@ -18,14 +15,12 @@
1815 #include <linux/device.h>
1916 #include <linux/export.h>
2017 #include <linux/module.h>
21
-#include <linux/of_device.h>
18
+#include <linux/of.h>
2219 #include <linux/pm_opp.h>
2320 #include <linux/platform_device.h>
2421 #include <linux/regulator/consumer.h>
25
-#include <linux/slab.h>
2622
2723 #define DEFAULT_SATURATION_RATIO 40
28
-#define DEFAULT_VOLTAGE_TOLERANCE 2
2924
3025 struct exynos_bus {
3126 struct device *dev;
....@@ -37,9 +32,8 @@
3732
3833 unsigned long curr_freq;
3934
40
- struct regulator *regulator;
35
+ struct opp_table *opp_table;
4136 struct clk *clk;
42
- unsigned int voltage_tolerance;
4337 unsigned int ratio;
4438 };
4539
....@@ -93,62 +87,29 @@
9387 }
9488
9589 /*
96
- * Must necessary function for devfreq simple-ondemand governor
90
+ * devfreq function for both simple-ondemand and passive governor
9791 */
9892 static int exynos_bus_target(struct device *dev, unsigned long *freq, u32 flags)
9993 {
10094 struct exynos_bus *bus = dev_get_drvdata(dev);
10195 struct dev_pm_opp *new_opp;
102
- unsigned long old_freq, new_freq, new_volt, tol;
10396 int ret = 0;
10497
105
- /* Get new opp-bus instance according to new bus clock */
98
+ /* Get correct frequency for bus. */
10699 new_opp = devfreq_recommended_opp(dev, freq, flags);
107100 if (IS_ERR(new_opp)) {
108101 dev_err(dev, "failed to get recommended opp instance\n");
109102 return PTR_ERR(new_opp);
110103 }
111104
112
- new_freq = dev_pm_opp_get_freq(new_opp);
113
- new_volt = dev_pm_opp_get_voltage(new_opp);
114105 dev_pm_opp_put(new_opp);
115
-
116
- old_freq = bus->curr_freq;
117
-
118
- if (old_freq == new_freq)
119
- return 0;
120
- tol = new_volt * bus->voltage_tolerance / 100;
121106
122107 /* Change voltage and frequency according to new OPP level */
123108 mutex_lock(&bus->lock);
109
+ ret = dev_pm_opp_set_rate(dev, *freq);
110
+ if (!ret)
111
+ bus->curr_freq = *freq;
124112
125
- if (old_freq < new_freq) {
126
- ret = regulator_set_voltage_tol(bus->regulator, new_volt, tol);
127
- if (ret < 0) {
128
- dev_err(bus->dev, "failed to set voltage\n");
129
- goto out;
130
- }
131
- }
132
-
133
- ret = clk_set_rate(bus->clk, new_freq);
134
- if (ret < 0) {
135
- dev_err(dev, "failed to change clock of bus\n");
136
- clk_set_rate(bus->clk, old_freq);
137
- goto out;
138
- }
139
-
140
- if (old_freq > new_freq) {
141
- ret = regulator_set_voltage_tol(bus->regulator, new_volt, tol);
142
- if (ret < 0) {
143
- dev_err(bus->dev, "failed to set voltage\n");
144
- goto out;
145
- }
146
- }
147
- bus->curr_freq = new_freq;
148
-
149
- dev_dbg(dev, "Set the frequency of bus (%luHz -> %luHz, %luHz)\n",
150
- old_freq, new_freq, clk_get_rate(bus->clk));
151
-out:
152113 mutex_unlock(&bus->lock);
153114
154115 return ret;
....@@ -165,6 +126,7 @@
165126
166127 ret = exynos_bus_get_event(bus, &edata);
167128 if (ret < 0) {
129
+ dev_err(dev, "failed to get event from devfreq-event devices\n");
168130 stat->total_time = stat->busy_time = 0;
169131 goto err;
170132 }
....@@ -196,54 +158,10 @@
196158
197159 dev_pm_opp_of_remove_table(dev);
198160 clk_disable_unprepare(bus->clk);
199
- if (bus->regulator)
200
- regulator_disable(bus->regulator);
201
-}
202
-
203
-/*
204
- * Must necessary function for devfreq passive governor
205
- */
206
-static int exynos_bus_passive_target(struct device *dev, unsigned long *freq,
207
- u32 flags)
208
-{
209
- struct exynos_bus *bus = dev_get_drvdata(dev);
210
- struct dev_pm_opp *new_opp;
211
- unsigned long old_freq, new_freq;
212
- int ret = 0;
213
-
214
- /* Get new opp-bus instance according to new bus clock */
215
- new_opp = devfreq_recommended_opp(dev, freq, flags);
216
- if (IS_ERR(new_opp)) {
217
- dev_err(dev, "failed to get recommended opp instance\n");
218
- return PTR_ERR(new_opp);
161
+ if (bus->opp_table) {
162
+ dev_pm_opp_put_regulators(bus->opp_table);
163
+ bus->opp_table = NULL;
219164 }
220
-
221
- new_freq = dev_pm_opp_get_freq(new_opp);
222
- dev_pm_opp_put(new_opp);
223
-
224
- old_freq = bus->curr_freq;
225
-
226
- if (old_freq == new_freq)
227
- return 0;
228
-
229
- /* Change the frequency according to new OPP level */
230
- mutex_lock(&bus->lock);
231
-
232
- ret = clk_set_rate(bus->clk, new_freq);
233
- if (ret < 0) {
234
- dev_err(dev, "failed to set the clock of bus\n");
235
- goto out;
236
- }
237
-
238
- *freq = new_freq;
239
- bus->curr_freq = new_freq;
240
-
241
- dev_dbg(dev, "Set the frequency of bus (%luHz -> %luHz, %luHz)\n",
242
- old_freq, new_freq, clk_get_rate(bus->clk));
243
-out:
244
- mutex_unlock(&bus->lock);
245
-
246
- return ret;
247165 }
248166
249167 static void exynos_bus_passive_exit(struct device *dev)
....@@ -258,26 +176,24 @@
258176 struct exynos_bus *bus)
259177 {
260178 struct device *dev = bus->dev;
179
+ struct opp_table *opp_table;
180
+ const char *vdd = "vdd";
261181 int i, ret, count, size;
262182
263
- /* Get the regulator to provide each bus with the power */
264
- bus->regulator = devm_regulator_get(dev, "vdd");
265
- if (IS_ERR(bus->regulator)) {
266
- dev_err(dev, "failed to get VDD regulator\n");
267
- return PTR_ERR(bus->regulator);
268
- }
269
-
270
- ret = regulator_enable(bus->regulator);
271
- if (ret < 0) {
272
- dev_err(dev, "failed to enable VDD regulator\n");
183
+ opp_table = dev_pm_opp_set_regulators(dev, &vdd, 1);
184
+ if (IS_ERR(opp_table)) {
185
+ ret = PTR_ERR(opp_table);
186
+ dev_err(dev, "failed to set regulators %d\n", ret);
273187 return ret;
274188 }
189
+
190
+ bus->opp_table = opp_table;
275191
276192 /*
277193 * Get the devfreq-event devices to get the current utilization of
278194 * buses. This raw data will be used in devfreq ondemand governor.
279195 */
280
- count = devfreq_event_get_edev_count(dev);
196
+ count = devfreq_event_get_edev_count(dev, "devfreq-events");
281197 if (count < 0) {
282198 dev_err(dev, "failed to get the count of devfreq-event dev\n");
283199 ret = count;
....@@ -293,7 +209,8 @@
293209 }
294210
295211 for (i = 0; i < count; i++) {
296
- bus->edev[i] = devfreq_event_get_edev_by_phandle(dev, i);
212
+ bus->edev[i] = devfreq_event_get_edev_by_phandle(dev,
213
+ "devfreq-events", i);
297214 if (IS_ERR(bus->edev[i])) {
298215 ret = -EPROBE_DEFER;
299216 goto err_regulator;
....@@ -313,14 +230,11 @@
313230 if (of_property_read_u32(np, "exynos,saturation-ratio", &bus->ratio))
314231 bus->ratio = DEFAULT_SATURATION_RATIO;
315232
316
- if (of_property_read_u32(np, "exynos,voltage-tolerance",
317
- &bus->voltage_tolerance))
318
- bus->voltage_tolerance = DEFAULT_VOLTAGE_TOLERANCE;
319
-
320233 return 0;
321234
322235 err_regulator:
323
- regulator_disable(bus->regulator);
236
+ dev_pm_opp_put_regulators(bus->opp_table);
237
+ bus->opp_table = NULL;
324238
325239 return ret;
326240 }
....@@ -374,14 +288,106 @@
374288 return ret;
375289 }
376290
291
+static int exynos_bus_profile_init(struct exynos_bus *bus,
292
+ struct devfreq_dev_profile *profile)
293
+{
294
+ struct device *dev = bus->dev;
295
+ struct devfreq_simple_ondemand_data *ondemand_data;
296
+ int ret;
297
+
298
+ /* Initialize the struct profile and governor data for parent device */
299
+ profile->polling_ms = 50;
300
+ profile->target = exynos_bus_target;
301
+ profile->get_dev_status = exynos_bus_get_dev_status;
302
+ profile->exit = exynos_bus_exit;
303
+
304
+ ondemand_data = devm_kzalloc(dev, sizeof(*ondemand_data), GFP_KERNEL);
305
+ if (!ondemand_data)
306
+ return -ENOMEM;
307
+
308
+ ondemand_data->upthreshold = 40;
309
+ ondemand_data->downdifferential = 5;
310
+
311
+ /* Add devfreq device to monitor and handle the exynos bus */
312
+ bus->devfreq = devm_devfreq_add_device(dev, profile,
313
+ DEVFREQ_GOV_SIMPLE_ONDEMAND,
314
+ ondemand_data);
315
+ if (IS_ERR(bus->devfreq)) {
316
+ dev_err(dev, "failed to add devfreq device\n");
317
+ return PTR_ERR(bus->devfreq);
318
+ }
319
+
320
+ /* Register opp_notifier to catch the change of OPP */
321
+ ret = devm_devfreq_register_opp_notifier(dev, bus->devfreq);
322
+ if (ret < 0) {
323
+ dev_err(dev, "failed to register opp notifier\n");
324
+ return ret;
325
+ }
326
+
327
+ /*
328
+ * Enable devfreq-event to get raw data which is used to determine
329
+ * current bus load.
330
+ */
331
+ ret = exynos_bus_enable_edev(bus);
332
+ if (ret < 0) {
333
+ dev_err(dev, "failed to enable devfreq-event devices\n");
334
+ return ret;
335
+ }
336
+
337
+ ret = exynos_bus_set_event(bus);
338
+ if (ret < 0) {
339
+ dev_err(dev, "failed to set event to devfreq-event devices\n");
340
+ goto err_edev;
341
+ }
342
+
343
+ return 0;
344
+
345
+err_edev:
346
+ if (exynos_bus_disable_edev(bus))
347
+ dev_warn(dev, "failed to disable the devfreq-event devices\n");
348
+
349
+ return ret;
350
+}
351
+
352
+static int exynos_bus_profile_init_passive(struct exynos_bus *bus,
353
+ struct devfreq_dev_profile *profile)
354
+{
355
+ struct device *dev = bus->dev;
356
+ struct devfreq_passive_data *passive_data;
357
+ struct devfreq *parent_devfreq;
358
+
359
+ /* Initialize the struct profile and governor data for passive device */
360
+ profile->target = exynos_bus_target;
361
+ profile->exit = exynos_bus_passive_exit;
362
+
363
+ /* Get the instance of parent devfreq device */
364
+ parent_devfreq = devfreq_get_devfreq_by_phandle(dev, "devfreq", 0);
365
+ if (IS_ERR(parent_devfreq))
366
+ return -EPROBE_DEFER;
367
+
368
+ passive_data = devm_kzalloc(dev, sizeof(*passive_data), GFP_KERNEL);
369
+ if (!passive_data)
370
+ return -ENOMEM;
371
+
372
+ passive_data->parent = parent_devfreq;
373
+
374
+ /* Add devfreq device for exynos bus with passive governor */
375
+ bus->devfreq = devm_devfreq_add_device(dev, profile, DEVFREQ_GOV_PASSIVE,
376
+ passive_data);
377
+ if (IS_ERR(bus->devfreq)) {
378
+ dev_err(dev,
379
+ "failed to add devfreq dev with passive governor\n");
380
+ return PTR_ERR(bus->devfreq);
381
+ }
382
+
383
+ return 0;
384
+}
385
+
377386 static int exynos_bus_probe(struct platform_device *pdev)
378387 {
379388 struct device *dev = &pdev->dev;
380389 struct device_node *np = dev->of_node, *node;
381390 struct devfreq_dev_profile *profile;
382
- struct devfreq_simple_ondemand_data *ondemand_data;
383
- struct devfreq_passive_data *passive_data;
384
- struct devfreq *parent_devfreq;
385391 struct exynos_bus *bus;
386392 int ret, max_state;
387393 unsigned long min_freq, max_freq;
....@@ -419,86 +425,13 @@
419425 goto err_reg;
420426
421427 if (passive)
422
- goto passive;
428
+ ret = exynos_bus_profile_init_passive(bus, profile);
429
+ else
430
+ ret = exynos_bus_profile_init(bus, profile);
423431
424
- /* Initialize the struct profile and governor data for parent device */
425
- profile->polling_ms = 50;
426
- profile->target = exynos_bus_target;
427
- profile->get_dev_status = exynos_bus_get_dev_status;
428
- profile->exit = exynos_bus_exit;
429
-
430
- ondemand_data = devm_kzalloc(dev, sizeof(*ondemand_data), GFP_KERNEL);
431
- if (!ondemand_data) {
432
- ret = -ENOMEM;
432
+ if (ret < 0)
433433 goto err;
434
- }
435
- ondemand_data->upthreshold = 40;
436
- ondemand_data->downdifferential = 5;
437434
438
- /* Add devfreq device to monitor and handle the exynos bus */
439
- bus->devfreq = devm_devfreq_add_device(dev, profile,
440
- DEVFREQ_GOV_SIMPLE_ONDEMAND,
441
- ondemand_data);
442
- if (IS_ERR(bus->devfreq)) {
443
- dev_err(dev, "failed to add devfreq device\n");
444
- ret = PTR_ERR(bus->devfreq);
445
- goto err;
446
- }
447
-
448
- /* Register opp_notifier to catch the change of OPP */
449
- ret = devm_devfreq_register_opp_notifier(dev, bus->devfreq);
450
- if (ret < 0) {
451
- dev_err(dev, "failed to register opp notifier\n");
452
- goto err;
453
- }
454
-
455
- /*
456
- * Enable devfreq-event to get raw data which is used to determine
457
- * current bus load.
458
- */
459
- ret = exynos_bus_enable_edev(bus);
460
- if (ret < 0) {
461
- dev_err(dev, "failed to enable devfreq-event devices\n");
462
- goto err;
463
- }
464
-
465
- ret = exynos_bus_set_event(bus);
466
- if (ret < 0) {
467
- dev_err(dev, "failed to set event to devfreq-event devices\n");
468
- goto err;
469
- }
470
-
471
- goto out;
472
-passive:
473
- /* Initialize the struct profile and governor data for passive device */
474
- profile->target = exynos_bus_passive_target;
475
- profile->exit = exynos_bus_passive_exit;
476
-
477
- /* Get the instance of parent devfreq device */
478
- parent_devfreq = devfreq_get_devfreq_by_phandle(dev, 0);
479
- if (IS_ERR(parent_devfreq)) {
480
- ret = -EPROBE_DEFER;
481
- goto err;
482
- }
483
-
484
- passive_data = devm_kzalloc(dev, sizeof(*passive_data), GFP_KERNEL);
485
- if (!passive_data) {
486
- ret = -ENOMEM;
487
- goto err;
488
- }
489
- passive_data->parent = parent_devfreq;
490
-
491
- /* Add devfreq device for exynos bus with passive governor */
492
- bus->devfreq = devm_devfreq_add_device(dev, profile, DEVFREQ_GOV_PASSIVE,
493
- passive_data);
494
- if (IS_ERR(bus->devfreq)) {
495
- dev_err(dev,
496
- "failed to add devfreq dev with passive governor\n");
497
- ret = PTR_ERR(bus->devfreq);
498
- goto err;
499
- }
500
-
501
-out:
502435 max_state = bus->devfreq->profile->max_state;
503436 min_freq = (bus->devfreq->profile->freq_table[0] / 1000);
504437 max_freq = (bus->devfreq->profile->freq_table[max_state - 1] / 1000);
....@@ -511,10 +444,19 @@
511444 dev_pm_opp_of_remove_table(dev);
512445 clk_disable_unprepare(bus->clk);
513446 err_reg:
514
- if (!passive)
515
- regulator_disable(bus->regulator);
447
+ if (!passive) {
448
+ dev_pm_opp_put_regulators(bus->opp_table);
449
+ bus->opp_table = NULL;
450
+ }
516451
517452 return ret;
453
+}
454
+
455
+static void exynos_bus_shutdown(struct platform_device *pdev)
456
+{
457
+ struct exynos_bus *bus = dev_get_drvdata(&pdev->dev);
458
+
459
+ devfreq_suspend_device(bus->devfreq);
518460 }
519461
520462 #ifdef CONFIG_PM_SLEEP
....@@ -559,6 +501,7 @@
559501
560502 static struct platform_driver exynos_bus_platdrv = {
561503 .probe = exynos_bus_probe,
504
+ .shutdown = exynos_bus_shutdown,
562505 .driver = {
563506 .name = "exynos-bus",
564507 .pm = &exynos_bus_pm,