Merge patch series "RISC-V Hibernation Support"
[linux-block.git] / drivers / devfreq / exynos-bus.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
0722249a
CC
2/*
3 * Generic Exynos Bus frequency driver with DEVFREQ Framework
4 *
403e0689 5 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
0722249a
CC
6 * Author : Chanwoo Choi <cw00.choi@samsung.com>
7 *
8 * This driver support Exynos Bus frequency feature by using
9 * DEVFREQ framework and is based on drivers/devfreq/exynos/exynos4_bus.c.
0722249a
CC
10 */
11
12#include <linux/clk.h>
13#include <linux/devfreq.h>
14#include <linux/devfreq-event.h>
15#include <linux/device.h>
16#include <linux/export.h>
17#include <linux/module.h>
a4408921 18#include <linux/of.h>
0722249a
CC
19#include <linux/pm_opp.h>
20#include <linux/platform_device.h>
21#include <linux/regulator/consumer.h>
0722249a
CC
22
23#define DEFAULT_SATURATION_RATIO 40
0722249a
CC
24
25struct exynos_bus {
26 struct device *dev;
404d59c5 27 struct platform_device *icc_pdev;
0722249a
CC
28
29 struct devfreq *devfreq;
30 struct devfreq_event_dev **edev;
31 unsigned int edev_count;
32 struct mutex lock;
33
c8ce82b9 34 unsigned long curr_freq;
0722249a 35
b0ec0942 36 int opp_token;
0722249a 37 struct clk *clk;
0722249a
CC
38 unsigned int ratio;
39};
40
41/*
42 * Control the devfreq-event device to get the current state of bus
43 */
44#define exynos_bus_ops_edev(ops) \
45static int exynos_bus_##ops(struct exynos_bus *bus) \
46{ \
47 int i, ret; \
48 \
49 for (i = 0; i < bus->edev_count; i++) { \
50 if (!bus->edev[i]) \
51 continue; \
52 ret = devfreq_event_##ops(bus->edev[i]); \
53 if (ret < 0) \
54 return ret; \
55 } \
56 \
57 return 0; \
58}
59exynos_bus_ops_edev(enable_edev);
60exynos_bus_ops_edev(disable_edev);
61exynos_bus_ops_edev(set_event);
62
63static int exynos_bus_get_event(struct exynos_bus *bus,
64 struct devfreq_event_data *edata)
65{
66 struct devfreq_event_data event_data;
67 unsigned long load_count = 0, total_count = 0;
68 int i, ret = 0;
69
70 for (i = 0; i < bus->edev_count; i++) {
71 if (!bus->edev[i])
72 continue;
73
74 ret = devfreq_event_get_event(bus->edev[i], &event_data);
75 if (ret < 0)
76 return ret;
77
78 if (i == 0 || event_data.load_count > load_count) {
79 load_count = event_data.load_count;
80 total_count = event_data.total_count;
81 }
82 }
83
84 edata->load_count = load_count;
85 edata->total_count = total_count;
86
87 return ret;
88}
89
90/*
4294a779 91 * devfreq function for both simple-ondemand and passive governor
0722249a
CC
92 */
93static int exynos_bus_target(struct device *dev, unsigned long *freq, u32 flags)
94{
95 struct exynos_bus *bus = dev_get_drvdata(dev);
96 struct dev_pm_opp *new_opp;
0722249a
CC
97 int ret = 0;
98
4294a779 99 /* Get correct frequency for bus. */
0722249a
CC
100 new_opp = devfreq_recommended_opp(dev, freq, flags);
101 if (IS_ERR(new_opp)) {
102 dev_err(dev, "failed to get recommended opp instance\n");
0722249a
CC
103 return PTR_ERR(new_opp);
104 }
105
8a31d9d9
VK
106 dev_pm_opp_put(new_opp);
107
0722249a
CC
108 /* Change voltage and frequency according to new OPP level */
109 mutex_lock(&bus->lock);
4294a779
KK
110 ret = dev_pm_opp_set_rate(dev, *freq);
111 if (!ret)
112 bus->curr_freq = *freq;
0722249a 113
0722249a
CC
114 mutex_unlock(&bus->lock);
115
116 return ret;
117}
118
119static int exynos_bus_get_dev_status(struct device *dev,
120 struct devfreq_dev_status *stat)
121{
122 struct exynos_bus *bus = dev_get_drvdata(dev);
123 struct devfreq_event_data edata;
124 int ret;
125
c8ce82b9 126 stat->current_frequency = bus->curr_freq;
0722249a
CC
127
128 ret = exynos_bus_get_event(bus, &edata);
129 if (ret < 0) {
28135762 130 dev_err(dev, "failed to get event from devfreq-event devices\n");
0722249a
CC
131 stat->total_time = stat->busy_time = 0;
132 goto err;
133 }
134
135 stat->busy_time = (edata.load_count * 100) / bus->ratio;
136 stat->total_time = edata.total_count;
137
138 dev_dbg(dev, "Usage of devfreq-event : %lu/%lu\n", stat->busy_time,
139 stat->total_time);
140
141err:
142 ret = exynos_bus_set_event(bus);
143 if (ret < 0) {
144 dev_err(dev, "failed to set event to devfreq-event devices\n");
145 return ret;
146 }
147
148 return ret;
149}
150
151static void exynos_bus_exit(struct device *dev)
152{
153 struct exynos_bus *bus = dev_get_drvdata(dev);
154 int ret;
155
156 ret = exynos_bus_disable_edev(bus);
157 if (ret < 0)
158 dev_warn(dev, "failed to disable the devfreq-event devices\n");
159
404d59c5
SN
160 platform_device_unregister(bus->icc_pdev);
161
0722249a 162 dev_pm_opp_of_remove_table(dev);
403e0689 163 clk_disable_unprepare(bus->clk);
b0ec0942 164 dev_pm_opp_put_regulators(bus->opp_token);
403e0689
CC
165}
166
167static void exynos_bus_passive_exit(struct device *dev)
168{
169 struct exynos_bus *bus = dev_get_drvdata(dev);
170
404d59c5
SN
171 platform_device_unregister(bus->icc_pdev);
172
403e0689
CC
173 dev_pm_opp_of_remove_table(dev);
174 clk_disable_unprepare(bus->clk);
175}
176
177static int exynos_bus_parent_parse_of(struct device_node *np,
178 struct exynos_bus *bus)
179{
180 struct device *dev = bus->dev;
87686cc8 181 const char *supplies[] = { "vdd", NULL };
403e0689 182 int i, ret, count, size;
0722249a 183
b0ec0942
VK
184 ret = dev_pm_opp_set_regulators(dev, supplies);
185 if (ret < 0) {
4294a779 186 dev_err(dev, "failed to set regulators %d\n", ret);
403e0689 187 return ret;
0722249a
CC
188 }
189
b0ec0942 190 bus->opp_token = ret;
4294a779 191
0722249a
CC
192 /*
193 * Get the devfreq-event devices to get the current utilization of
194 * buses. This raw data will be used in devfreq ondemand governor.
195 */
02bdbf7d 196 count = devfreq_event_get_edev_count(dev, "devfreq-events");
0722249a
CC
197 if (count < 0) {
198 dev_err(dev, "failed to get the count of devfreq-event dev\n");
199 ret = count;
200 goto err_regulator;
201 }
202 bus->edev_count = count;
203
204 size = sizeof(*bus->edev) * count;
205 bus->edev = devm_kzalloc(dev, size, GFP_KERNEL);
206 if (!bus->edev) {
207 ret = -ENOMEM;
208 goto err_regulator;
209 }
210
211 for (i = 0; i < count; i++) {
02bdbf7d
CC
212 bus->edev[i] = devfreq_event_get_edev_by_phandle(dev,
213 "devfreq-events", i);
0722249a
CC
214 if (IS_ERR(bus->edev[i])) {
215 ret = -EPROBE_DEFER;
216 goto err_regulator;
217 }
218 }
219
220 /*
221 * Optionally, Get the saturation ratio according to Exynos SoC
222 * When measuring the utilization of each AXI bus with devfreq-event
223 * devices, the measured real cycle might be much lower than the
224 * total cycle of bus during sampling rate. In result, the devfreq
225 * simple-ondemand governor might not decide to change the current
226 * frequency due to too utilization (= real cycle/total cycle).
227 * So, this property is used to adjust the utilization when calculating
228 * the busy_time in exynos_bus_get_dev_status().
229 */
230 if (of_property_read_u32(np, "exynos,saturation-ratio", &bus->ratio))
231 bus->ratio = DEFAULT_SATURATION_RATIO;
232
0722249a
CC
233 return 0;
234
235err_regulator:
b0ec0942 236 dev_pm_opp_put_regulators(bus->opp_token);
403e0689
CC
237
238 return ret;
239}
240
241static int exynos_bus_parse_of(struct device_node *np,
242 struct exynos_bus *bus)
243{
244 struct device *dev = bus->dev;
c8ce82b9 245 struct dev_pm_opp *opp;
403e0689
CC
246 unsigned long rate;
247 int ret;
248
249 /* Get the clock to provide each bus with source clock */
250 bus->clk = devm_clk_get(dev, "bus");
251 if (IS_ERR(bus->clk)) {
252 dev_err(dev, "failed to get bus clock\n");
253 return PTR_ERR(bus->clk);
254 }
255
256 ret = clk_prepare_enable(bus->clk);
257 if (ret < 0) {
258 dev_err(dev, "failed to get enable clock\n");
259 return ret;
260 }
261
262 /* Get the freq and voltage from OPP table to scale the bus freq */
403e0689
CC
263 ret = dev_pm_opp_of_add_table(dev);
264 if (ret < 0) {
265 dev_err(dev, "failed to get OPP table\n");
403e0689
CC
266 goto err_clk;
267 }
268
269 rate = clk_get_rate(bus->clk);
c8ce82b9 270
c8ce82b9
VK
271 opp = devfreq_recommended_opp(dev, &rate, 0);
272 if (IS_ERR(opp)) {
403e0689 273 dev_err(dev, "failed to find dev_pm_opp\n");
c8ce82b9 274 ret = PTR_ERR(opp);
403e0689
CC
275 goto err_opp;
276 }
c8ce82b9 277 bus->curr_freq = dev_pm_opp_get_freq(opp);
8a31d9d9 278 dev_pm_opp_put(opp);
403e0689
CC
279
280 return 0;
281
0722249a
CC
282err_opp:
283 dev_pm_opp_of_remove_table(dev);
284err_clk:
285 clk_disable_unprepare(bus->clk);
286
287 return ret;
288}
289
a47a97ec
290static int exynos_bus_profile_init(struct exynos_bus *bus,
291 struct devfreq_dev_profile *profile)
0722249a 292{
a47a97ec 293 struct device *dev = bus->dev;
0722249a 294 struct devfreq_simple_ondemand_data *ondemand_data;
a47a97ec 295 int ret;
403e0689 296
83cb0e4d 297 /* Initialize the struct profile and governor data for parent device */
0722249a
CC
298 profile->polling_ms = 50;
299 profile->target = exynos_bus_target;
300 profile->get_dev_status = exynos_bus_get_dev_status;
301 profile->exit = exynos_bus_exit;
302
303 ondemand_data = devm_kzalloc(dev, sizeof(*ondemand_data), GFP_KERNEL);
a4408921
304 if (!ondemand_data)
305 return -ENOMEM;
306
0722249a
CC
307 ondemand_data->upthreshold = 40;
308 ondemand_data->downdifferential = 5;
309
310 /* Add devfreq device to monitor and handle the exynos bus */
aa7c352f
CC
311 bus->devfreq = devm_devfreq_add_device(dev, profile,
312 DEVFREQ_GOV_SIMPLE_ONDEMAND,
0722249a
CC
313 ondemand_data);
314 if (IS_ERR(bus->devfreq)) {
315 dev_err(dev, "failed to add devfreq device\n");
a4408921 316 return PTR_ERR(bus->devfreq);
0722249a
CC
317 }
318
319 /* Register opp_notifier to catch the change of OPP */
320 ret = devm_devfreq_register_opp_notifier(dev, bus->devfreq);
321 if (ret < 0) {
322 dev_err(dev, "failed to register opp notifier\n");
a4408921 323 return ret;
0722249a
CC
324 }
325
326 /*
327 * Enable devfreq-event to get raw data which is used to determine
328 * current bus load.
329 */
330 ret = exynos_bus_enable_edev(bus);
331 if (ret < 0) {
332 dev_err(dev, "failed to enable devfreq-event devices\n");
a4408921 333 return ret;
0722249a
CC
334 }
335
336 ret = exynos_bus_set_event(bus);
337 if (ret < 0) {
338 dev_err(dev, "failed to set event to devfreq-event devices\n");
6c315d8f 339 goto err_edev;
0722249a
CC
340 }
341
a4408921 342 return 0;
6c315d8f
YL
343
344err_edev:
345 if (exynos_bus_disable_edev(bus))
346 dev_warn(dev, "failed to disable the devfreq-event devices\n");
347
348 return ret;
a47a97ec
349}
350
a05bb963
351static int exynos_bus_profile_init_passive(struct exynos_bus *bus,
352 struct devfreq_dev_profile *profile)
353{
354 struct device *dev = bus->dev;
355 struct devfreq_passive_data *passive_data;
356 struct devfreq *parent_devfreq;
a05bb963
357
358 /* Initialize the struct profile and governor data for passive device */
359 profile->target = exynos_bus_target;
360 profile->exit = exynos_bus_passive_exit;
361
362 /* Get the instance of parent devfreq device */
86d90fd9 363 parent_devfreq = devfreq_get_devfreq_by_phandle(dev, "devfreq", 0);
a4408921
364 if (IS_ERR(parent_devfreq))
365 return -EPROBE_DEFER;
a05bb963
366
367 passive_data = devm_kzalloc(dev, sizeof(*passive_data), GFP_KERNEL);
a4408921
368 if (!passive_data)
369 return -ENOMEM;
370
a05bb963
371 passive_data->parent = parent_devfreq;
372
373 /* Add devfreq device for exynos bus with passive governor */
374 bus->devfreq = devm_devfreq_add_device(dev, profile, DEVFREQ_GOV_PASSIVE,
375 passive_data);
376 if (IS_ERR(bus->devfreq)) {
377 dev_err(dev,
378 "failed to add devfreq dev with passive governor\n");
a4408921 379 return PTR_ERR(bus->devfreq);
a05bb963
380 }
381
a4408921 382 return 0;
a05bb963
383}
384
a47a97ec
385static int exynos_bus_probe(struct platform_device *pdev)
386{
387 struct device *dev = &pdev->dev;
388 struct device_node *np = dev->of_node, *node;
389 struct devfreq_dev_profile *profile;
a47a97ec
390 struct exynos_bus *bus;
391 int ret, max_state;
392 unsigned long min_freq, max_freq;
393 bool passive = false;
394
395 if (!np) {
396 dev_err(dev, "failed to find devicetree node\n");
397 return -EINVAL;
398 }
399
400 bus = devm_kzalloc(&pdev->dev, sizeof(*bus), GFP_KERNEL);
401 if (!bus)
402 return -ENOMEM;
403 mutex_init(&bus->lock);
404 bus->dev = &pdev->dev;
405 platform_set_drvdata(pdev, bus);
406
407 profile = devm_kzalloc(dev, sizeof(*profile), GFP_KERNEL);
408 if (!profile)
409 return -ENOMEM;
410
411 node = of_parse_phandle(dev->of_node, "devfreq", 0);
412 if (node) {
413 of_node_put(node);
414 passive = true;
415 } else {
416 ret = exynos_bus_parent_parse_of(np, bus);
417 if (ret < 0)
418 return ret;
419 }
420
421 /* Parse the device-tree to get the resource information */
422 ret = exynos_bus_parse_of(np, bus);
423 if (ret < 0)
424 goto err_reg;
425
426 if (passive)
a4408921
427 ret = exynos_bus_profile_init_passive(bus, profile);
428 else
429 ret = exynos_bus_profile_init(bus, profile);
a47a97ec 430
a05bb963 431 if (ret < 0)
403e0689 432 goto err;
403e0689 433
404d59c5 434 /* Create child platform device for the interconnect provider */
b7405e3f 435 if (of_property_present(dev->of_node, "#interconnect-cells")) {
404d59c5
SN
436 bus->icc_pdev = platform_device_register_data(
437 dev, "exynos-generic-icc",
438 PLATFORM_DEVID_AUTO, NULL, 0);
439
440 if (IS_ERR(bus->icc_pdev)) {
441 ret = PTR_ERR(bus->icc_pdev);
442 goto err;
443 }
444 }
445
c8934e4e
CM
446 max_state = bus->devfreq->max_state;
447 min_freq = (bus->devfreq->freq_table[0] / 1000);
448 max_freq = (bus->devfreq->freq_table[max_state - 1] / 1000);
403e0689
CC
449 pr_info("exynos-bus: new bus device registered: %s (%6ld KHz ~ %6ld KHz)\n",
450 dev_name(dev), min_freq, max_freq);
451
0722249a 452 return 0;
403e0689
CC
453
454err:
455 dev_pm_opp_of_remove_table(dev);
456 clk_disable_unprepare(bus->clk);
2c2b20e0 457err_reg:
b0ec0942 458 dev_pm_opp_put_regulators(bus->opp_token);
403e0689
CC
459
460 return ret;
0722249a
CC
461}
462
fbb9c3c9
MS
463static void exynos_bus_shutdown(struct platform_device *pdev)
464{
465 struct exynos_bus *bus = dev_get_drvdata(&pdev->dev);
466
467 devfreq_suspend_device(bus->devfreq);
468}
469
0722249a
CC
470#ifdef CONFIG_PM_SLEEP
471static int exynos_bus_resume(struct device *dev)
472{
473 struct exynos_bus *bus = dev_get_drvdata(dev);
474 int ret;
475
476 ret = exynos_bus_enable_edev(bus);
477 if (ret < 0) {
478 dev_err(dev, "failed to enable the devfreq-event devices\n");
479 return ret;
480 }
481
482 return 0;
483}
484
485static int exynos_bus_suspend(struct device *dev)
486{
487 struct exynos_bus *bus = dev_get_drvdata(dev);
488 int ret;
489
490 ret = exynos_bus_disable_edev(bus);
491 if (ret < 0) {
492 dev_err(dev, "failed to disable the devfreq-event devices\n");
493 return ret;
494 }
495
496 return 0;
497}
498#endif
499
500static const struct dev_pm_ops exynos_bus_pm = {
501 SET_SYSTEM_SLEEP_PM_OPS(exynos_bus_suspend, exynos_bus_resume)
502};
503
504static const struct of_device_id exynos_bus_of_match[] = {
505 { .compatible = "samsung,exynos-bus", },
506 { /* sentinel */ },
507};
508MODULE_DEVICE_TABLE(of, exynos_bus_of_match);
509
510static struct platform_driver exynos_bus_platdrv = {
511 .probe = exynos_bus_probe,
fbb9c3c9 512 .shutdown = exynos_bus_shutdown,
0722249a
CC
513 .driver = {
514 .name = "exynos-bus",
515 .pm = &exynos_bus_pm,
f9d0393a 516 .of_match_table = exynos_bus_of_match,
0722249a
CC
517 },
518};
519module_platform_driver(exynos_bus_platdrv);
520
521MODULE_DESCRIPTION("Generic Exynos Bus frequency driver");
522MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
523MODULE_LICENSE("GPL v2");