1 // SPDX-License-Identifier: GPL-2.0-only
3 * Generic Exynos Bus frequency driver with DEVFREQ Framework
5 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
6 * Author : Chanwoo Choi <cw00.choi@samsung.com>
8 * This driver support Exynos Bus frequency feature by using
9 * DEVFREQ framework and is based on drivers/devfreq/exynos/exynos4_bus.c.
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>
19 #include <linux/pm_opp.h>
20 #include <linux/platform_device.h>
21 #include <linux/regulator/consumer.h>
23 #define DEFAULT_SATURATION_RATIO 40
27 struct platform_device
*icc_pdev
;
29 struct devfreq
*devfreq
;
30 struct devfreq_event_dev
**edev
;
31 unsigned int edev_count
;
34 unsigned long curr_freq
;
36 struct opp_table
*opp_table
;
42 * Control the devfreq-event device to get the current state of bus
44 #define exynos_bus_ops_edev(ops) \
45 static int exynos_bus_##ops(struct exynos_bus *bus) \
49 for (i = 0; i < bus->edev_count; i++) { \
52 ret = devfreq_event_##ops(bus->edev[i]); \
59 exynos_bus_ops_edev(enable_edev
);
60 exynos_bus_ops_edev(disable_edev
);
61 exynos_bus_ops_edev(set_event
);
63 static int exynos_bus_get_event(struct exynos_bus
*bus
,
64 struct devfreq_event_data
*edata
)
66 struct devfreq_event_data event_data
;
67 unsigned long load_count
= 0, total_count
= 0;
70 for (i
= 0; i
< bus
->edev_count
; i
++) {
74 ret
= devfreq_event_get_event(bus
->edev
[i
], &event_data
);
78 if (i
== 0 || event_data
.load_count
> load_count
) {
79 load_count
= event_data
.load_count
;
80 total_count
= event_data
.total_count
;
84 edata
->load_count
= load_count
;
85 edata
->total_count
= total_count
;
91 * devfreq function for both simple-ondemand and passive governor
93 static int exynos_bus_target(struct device
*dev
, unsigned long *freq
, u32 flags
)
95 struct exynos_bus
*bus
= dev_get_drvdata(dev
);
96 struct dev_pm_opp
*new_opp
;
99 /* Get correct frequency for bus. */
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");
103 return PTR_ERR(new_opp
);
106 dev_pm_opp_put(new_opp
);
108 /* Change voltage and frequency according to new OPP level */
109 mutex_lock(&bus
->lock
);
110 ret
= dev_pm_opp_set_rate(dev
, *freq
);
112 bus
->curr_freq
= *freq
;
114 mutex_unlock(&bus
->lock
);
119 static int exynos_bus_get_dev_status(struct device
*dev
,
120 struct devfreq_dev_status
*stat
)
122 struct exynos_bus
*bus
= dev_get_drvdata(dev
);
123 struct devfreq_event_data edata
;
126 stat
->current_frequency
= bus
->curr_freq
;
128 ret
= exynos_bus_get_event(bus
, &edata
);
130 dev_err(dev
, "failed to get event from devfreq-event devices\n");
131 stat
->total_time
= stat
->busy_time
= 0;
135 stat
->busy_time
= (edata
.load_count
* 100) / bus
->ratio
;
136 stat
->total_time
= edata
.total_count
;
138 dev_dbg(dev
, "Usage of devfreq-event : %lu/%lu\n", stat
->busy_time
,
142 ret
= exynos_bus_set_event(bus
);
144 dev_err(dev
, "failed to set event to devfreq-event devices\n");
151 static void exynos_bus_exit(struct device
*dev
)
153 struct exynos_bus
*bus
= dev_get_drvdata(dev
);
156 ret
= exynos_bus_disable_edev(bus
);
158 dev_warn(dev
, "failed to disable the devfreq-event devices\n");
160 platform_device_unregister(bus
->icc_pdev
);
162 dev_pm_opp_of_remove_table(dev
);
163 clk_disable_unprepare(bus
->clk
);
164 dev_pm_opp_put_regulators(bus
->opp_table
);
165 bus
->opp_table
= NULL
;
168 static void exynos_bus_passive_exit(struct device
*dev
)
170 struct exynos_bus
*bus
= dev_get_drvdata(dev
);
172 platform_device_unregister(bus
->icc_pdev
);
174 dev_pm_opp_of_remove_table(dev
);
175 clk_disable_unprepare(bus
->clk
);
178 static int exynos_bus_parent_parse_of(struct device_node
*np
,
179 struct exynos_bus
*bus
)
181 struct device
*dev
= bus
->dev
;
182 struct opp_table
*opp_table
;
183 const char *vdd
= "vdd";
184 int i
, ret
, count
, size
;
186 opp_table
= dev_pm_opp_set_regulators(dev
, &vdd
, 1);
187 if (IS_ERR(opp_table
)) {
188 ret
= PTR_ERR(opp_table
);
189 dev_err(dev
, "failed to set regulators %d\n", ret
);
193 bus
->opp_table
= opp_table
;
196 * Get the devfreq-event devices to get the current utilization of
197 * buses. This raw data will be used in devfreq ondemand governor.
199 count
= devfreq_event_get_edev_count(dev
, "devfreq-events");
201 dev_err(dev
, "failed to get the count of devfreq-event dev\n");
205 bus
->edev_count
= count
;
207 size
= sizeof(*bus
->edev
) * count
;
208 bus
->edev
= devm_kzalloc(dev
, size
, GFP_KERNEL
);
214 for (i
= 0; i
< count
; i
++) {
215 bus
->edev
[i
] = devfreq_event_get_edev_by_phandle(dev
,
216 "devfreq-events", i
);
217 if (IS_ERR(bus
->edev
[i
])) {
224 * Optionally, Get the saturation ratio according to Exynos SoC
225 * When measuring the utilization of each AXI bus with devfreq-event
226 * devices, the measured real cycle might be much lower than the
227 * total cycle of bus during sampling rate. In result, the devfreq
228 * simple-ondemand governor might not decide to change the current
229 * frequency due to too utilization (= real cycle/total cycle).
230 * So, this property is used to adjust the utilization when calculating
231 * the busy_time in exynos_bus_get_dev_status().
233 if (of_property_read_u32(np
, "exynos,saturation-ratio", &bus
->ratio
))
234 bus
->ratio
= DEFAULT_SATURATION_RATIO
;
239 dev_pm_opp_put_regulators(bus
->opp_table
);
240 bus
->opp_table
= NULL
;
245 static int exynos_bus_parse_of(struct device_node
*np
,
246 struct exynos_bus
*bus
)
248 struct device
*dev
= bus
->dev
;
249 struct dev_pm_opp
*opp
;
253 /* Get the clock to provide each bus with source clock */
254 bus
->clk
= devm_clk_get(dev
, "bus");
255 if (IS_ERR(bus
->clk
)) {
256 dev_err(dev
, "failed to get bus clock\n");
257 return PTR_ERR(bus
->clk
);
260 ret
= clk_prepare_enable(bus
->clk
);
262 dev_err(dev
, "failed to get enable clock\n");
266 /* Get the freq and voltage from OPP table to scale the bus freq */
267 ret
= dev_pm_opp_of_add_table(dev
);
269 dev_err(dev
, "failed to get OPP table\n");
273 rate
= clk_get_rate(bus
->clk
);
275 opp
= devfreq_recommended_opp(dev
, &rate
, 0);
277 dev_err(dev
, "failed to find dev_pm_opp\n");
281 bus
->curr_freq
= dev_pm_opp_get_freq(opp
);
287 dev_pm_opp_of_remove_table(dev
);
289 clk_disable_unprepare(bus
->clk
);
294 static int exynos_bus_profile_init(struct exynos_bus
*bus
,
295 struct devfreq_dev_profile
*profile
)
297 struct device
*dev
= bus
->dev
;
298 struct devfreq_simple_ondemand_data
*ondemand_data
;
301 /* Initialize the struct profile and governor data for parent device */
302 profile
->polling_ms
= 50;
303 profile
->target
= exynos_bus_target
;
304 profile
->get_dev_status
= exynos_bus_get_dev_status
;
305 profile
->exit
= exynos_bus_exit
;
307 ondemand_data
= devm_kzalloc(dev
, sizeof(*ondemand_data
), GFP_KERNEL
);
311 ondemand_data
->upthreshold
= 40;
312 ondemand_data
->downdifferential
= 5;
314 /* Add devfreq device to monitor and handle the exynos bus */
315 bus
->devfreq
= devm_devfreq_add_device(dev
, profile
,
316 DEVFREQ_GOV_SIMPLE_ONDEMAND
,
318 if (IS_ERR(bus
->devfreq
)) {
319 dev_err(dev
, "failed to add devfreq device\n");
320 return PTR_ERR(bus
->devfreq
);
323 /* Register opp_notifier to catch the change of OPP */
324 ret
= devm_devfreq_register_opp_notifier(dev
, bus
->devfreq
);
326 dev_err(dev
, "failed to register opp notifier\n");
331 * Enable devfreq-event to get raw data which is used to determine
334 ret
= exynos_bus_enable_edev(bus
);
336 dev_err(dev
, "failed to enable devfreq-event devices\n");
340 ret
= exynos_bus_set_event(bus
);
342 dev_err(dev
, "failed to set event to devfreq-event devices\n");
349 if (exynos_bus_disable_edev(bus
))
350 dev_warn(dev
, "failed to disable the devfreq-event devices\n");
355 static int exynos_bus_profile_init_passive(struct exynos_bus
*bus
,
356 struct devfreq_dev_profile
*profile
)
358 struct device
*dev
= bus
->dev
;
359 struct devfreq_passive_data
*passive_data
;
360 struct devfreq
*parent_devfreq
;
362 /* Initialize the struct profile and governor data for passive device */
363 profile
->target
= exynos_bus_target
;
364 profile
->exit
= exynos_bus_passive_exit
;
366 /* Get the instance of parent devfreq device */
367 parent_devfreq
= devfreq_get_devfreq_by_phandle(dev
, "devfreq", 0);
368 if (IS_ERR(parent_devfreq
))
369 return -EPROBE_DEFER
;
371 passive_data
= devm_kzalloc(dev
, sizeof(*passive_data
), GFP_KERNEL
);
375 passive_data
->parent
= parent_devfreq
;
377 /* Add devfreq device for exynos bus with passive governor */
378 bus
->devfreq
= devm_devfreq_add_device(dev
, profile
, DEVFREQ_GOV_PASSIVE
,
380 if (IS_ERR(bus
->devfreq
)) {
382 "failed to add devfreq dev with passive governor\n");
383 return PTR_ERR(bus
->devfreq
);
389 static int exynos_bus_probe(struct platform_device
*pdev
)
391 struct device
*dev
= &pdev
->dev
;
392 struct device_node
*np
= dev
->of_node
, *node
;
393 struct devfreq_dev_profile
*profile
;
394 struct exynos_bus
*bus
;
396 unsigned long min_freq
, max_freq
;
397 bool passive
= false;
400 dev_err(dev
, "failed to find devicetree node\n");
404 bus
= devm_kzalloc(&pdev
->dev
, sizeof(*bus
), GFP_KERNEL
);
407 mutex_init(&bus
->lock
);
408 bus
->dev
= &pdev
->dev
;
409 platform_set_drvdata(pdev
, bus
);
411 profile
= devm_kzalloc(dev
, sizeof(*profile
), GFP_KERNEL
);
415 node
= of_parse_phandle(dev
->of_node
, "devfreq", 0);
420 ret
= exynos_bus_parent_parse_of(np
, bus
);
425 /* Parse the device-tree to get the resource information */
426 ret
= exynos_bus_parse_of(np
, bus
);
431 ret
= exynos_bus_profile_init_passive(bus
, profile
);
433 ret
= exynos_bus_profile_init(bus
, profile
);
438 /* Create child platform device for the interconnect provider */
439 if (of_get_property(dev
->of_node
, "#interconnect-cells", NULL
)) {
440 bus
->icc_pdev
= platform_device_register_data(
441 dev
, "exynos-generic-icc",
442 PLATFORM_DEVID_AUTO
, NULL
, 0);
444 if (IS_ERR(bus
->icc_pdev
)) {
445 ret
= PTR_ERR(bus
->icc_pdev
);
450 max_state
= bus
->devfreq
->profile
->max_state
;
451 min_freq
= (bus
->devfreq
->profile
->freq_table
[0] / 1000);
452 max_freq
= (bus
->devfreq
->profile
->freq_table
[max_state
- 1] / 1000);
453 pr_info("exynos-bus: new bus device registered: %s (%6ld KHz ~ %6ld KHz)\n",
454 dev_name(dev
), min_freq
, max_freq
);
459 dev_pm_opp_of_remove_table(dev
);
460 clk_disable_unprepare(bus
->clk
);
462 dev_pm_opp_put_regulators(bus
->opp_table
);
463 bus
->opp_table
= NULL
;
468 static void exynos_bus_shutdown(struct platform_device
*pdev
)
470 struct exynos_bus
*bus
= dev_get_drvdata(&pdev
->dev
);
472 devfreq_suspend_device(bus
->devfreq
);
475 #ifdef CONFIG_PM_SLEEP
476 static int exynos_bus_resume(struct device
*dev
)
478 struct exynos_bus
*bus
= dev_get_drvdata(dev
);
481 ret
= exynos_bus_enable_edev(bus
);
483 dev_err(dev
, "failed to enable the devfreq-event devices\n");
490 static int exynos_bus_suspend(struct device
*dev
)
492 struct exynos_bus
*bus
= dev_get_drvdata(dev
);
495 ret
= exynos_bus_disable_edev(bus
);
497 dev_err(dev
, "failed to disable the devfreq-event devices\n");
505 static const struct dev_pm_ops exynos_bus_pm
= {
506 SET_SYSTEM_SLEEP_PM_OPS(exynos_bus_suspend
, exynos_bus_resume
)
509 static const struct of_device_id exynos_bus_of_match
[] = {
510 { .compatible
= "samsung,exynos-bus", },
513 MODULE_DEVICE_TABLE(of
, exynos_bus_of_match
);
515 static struct platform_driver exynos_bus_platdrv
= {
516 .probe
= exynos_bus_probe
,
517 .shutdown
= exynos_bus_shutdown
,
519 .name
= "exynos-bus",
520 .pm
= &exynos_bus_pm
,
521 .of_match_table
= of_match_ptr(exynos_bus_of_match
),
524 module_platform_driver(exynos_bus_platdrv
);
526 MODULE_DESCRIPTION("Generic Exynos Bus frequency driver");
527 MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
528 MODULE_LICENSE("GPL v2");