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