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>
18 #include <linux/of_device.h>
19 #include <linux/pm_opp.h>
20 #include <linux/platform_device.h>
21 #include <linux/regulator/consumer.h>
22 #include <linux/slab.h>
24 #define DEFAULT_SATURATION_RATIO 40
25 #define DEFAULT_VOLTAGE_TOLERANCE 2
30 struct devfreq
*devfreq
;
31 struct devfreq_event_dev
**edev
;
32 unsigned int edev_count
;
35 unsigned long curr_freq
;
37 struct regulator
*regulator
;
39 unsigned int voltage_tolerance
;
44 * Control the devfreq-event device to get the current state of bus
46 #define exynos_bus_ops_edev(ops) \
47 static int exynos_bus_##ops(struct exynos_bus *bus) \
51 for (i = 0; i < bus->edev_count; i++) { \
54 ret = devfreq_event_##ops(bus->edev[i]); \
61 exynos_bus_ops_edev(enable_edev
);
62 exynos_bus_ops_edev(disable_edev
);
63 exynos_bus_ops_edev(set_event
);
65 static int exynos_bus_get_event(struct exynos_bus
*bus
,
66 struct devfreq_event_data
*edata
)
68 struct devfreq_event_data event_data
;
69 unsigned long load_count
= 0, total_count
= 0;
72 for (i
= 0; i
< bus
->edev_count
; i
++) {
76 ret
= devfreq_event_get_event(bus
->edev
[i
], &event_data
);
80 if (i
== 0 || event_data
.load_count
> load_count
) {
81 load_count
= event_data
.load_count
;
82 total_count
= event_data
.total_count
;
86 edata
->load_count
= load_count
;
87 edata
->total_count
= total_count
;
93 * Must necessary function for devfreq simple-ondemand governor
95 static int exynos_bus_target(struct device
*dev
, unsigned long *freq
, u32 flags
)
97 struct exynos_bus
*bus
= dev_get_drvdata(dev
);
98 struct dev_pm_opp
*new_opp
;
99 unsigned long old_freq
, new_freq
, new_volt
, tol
;
102 /* Get new opp-bus instance according to new bus clock */
103 new_opp
= devfreq_recommended_opp(dev
, freq
, flags
);
104 if (IS_ERR(new_opp
)) {
105 dev_err(dev
, "failed to get recommended opp instance\n");
106 return PTR_ERR(new_opp
);
109 new_freq
= dev_pm_opp_get_freq(new_opp
);
110 new_volt
= dev_pm_opp_get_voltage(new_opp
);
111 dev_pm_opp_put(new_opp
);
113 old_freq
= bus
->curr_freq
;
115 if (old_freq
== new_freq
)
117 tol
= new_volt
* bus
->voltage_tolerance
/ 100;
119 /* Change voltage and frequency according to new OPP level */
120 mutex_lock(&bus
->lock
);
122 if (old_freq
< new_freq
) {
123 ret
= regulator_set_voltage_tol(bus
->regulator
, new_volt
, tol
);
125 dev_err(bus
->dev
, "failed to set voltage\n");
130 ret
= clk_set_rate(bus
->clk
, new_freq
);
132 dev_err(dev
, "failed to change clock of bus\n");
133 clk_set_rate(bus
->clk
, old_freq
);
137 if (old_freq
> new_freq
) {
138 ret
= regulator_set_voltage_tol(bus
->regulator
, new_volt
, tol
);
140 dev_err(bus
->dev
, "failed to set voltage\n");
144 bus
->curr_freq
= new_freq
;
146 dev_dbg(dev
, "Set the frequency of bus (%luHz -> %luHz, %luHz)\n",
147 old_freq
, new_freq
, clk_get_rate(bus
->clk
));
149 mutex_unlock(&bus
->lock
);
154 static int exynos_bus_get_dev_status(struct device
*dev
,
155 struct devfreq_dev_status
*stat
)
157 struct exynos_bus
*bus
= dev_get_drvdata(dev
);
158 struct devfreq_event_data edata
;
161 stat
->current_frequency
= bus
->curr_freq
;
163 ret
= exynos_bus_get_event(bus
, &edata
);
165 stat
->total_time
= stat
->busy_time
= 0;
169 stat
->busy_time
= (edata
.load_count
* 100) / bus
->ratio
;
170 stat
->total_time
= edata
.total_count
;
172 dev_dbg(dev
, "Usage of devfreq-event : %lu/%lu\n", stat
->busy_time
,
176 ret
= exynos_bus_set_event(bus
);
178 dev_err(dev
, "failed to set event to devfreq-event devices\n");
185 static void exynos_bus_exit(struct device
*dev
)
187 struct exynos_bus
*bus
= dev_get_drvdata(dev
);
190 ret
= exynos_bus_disable_edev(bus
);
192 dev_warn(dev
, "failed to disable the devfreq-event devices\n");
195 regulator_disable(bus
->regulator
);
197 dev_pm_opp_of_remove_table(dev
);
198 clk_disable_unprepare(bus
->clk
);
202 * Must necessary function for devfreq passive governor
204 static int exynos_bus_passive_target(struct device
*dev
, unsigned long *freq
,
207 struct exynos_bus
*bus
= dev_get_drvdata(dev
);
208 struct dev_pm_opp
*new_opp
;
209 unsigned long old_freq
, new_freq
;
212 /* Get new opp-bus instance according to new bus clock */
213 new_opp
= devfreq_recommended_opp(dev
, freq
, flags
);
214 if (IS_ERR(new_opp
)) {
215 dev_err(dev
, "failed to get recommended opp instance\n");
216 return PTR_ERR(new_opp
);
219 new_freq
= dev_pm_opp_get_freq(new_opp
);
220 dev_pm_opp_put(new_opp
);
222 old_freq
= bus
->curr_freq
;
224 if (old_freq
== new_freq
)
227 /* Change the frequency according to new OPP level */
228 mutex_lock(&bus
->lock
);
230 ret
= clk_set_rate(bus
->clk
, new_freq
);
232 dev_err(dev
, "failed to set the clock of bus\n");
237 bus
->curr_freq
= new_freq
;
239 dev_dbg(dev
, "Set the frequency of bus (%luHz -> %luHz, %luHz)\n",
240 old_freq
, new_freq
, clk_get_rate(bus
->clk
));
242 mutex_unlock(&bus
->lock
);
247 static void exynos_bus_passive_exit(struct device
*dev
)
249 struct exynos_bus
*bus
= dev_get_drvdata(dev
);
251 dev_pm_opp_of_remove_table(dev
);
252 clk_disable_unprepare(bus
->clk
);
255 static int exynos_bus_parent_parse_of(struct device_node
*np
,
256 struct exynos_bus
*bus
)
258 struct device
*dev
= bus
->dev
;
259 int i
, ret
, count
, size
;
261 /* Get the regulator to provide each bus with the power */
262 bus
->regulator
= devm_regulator_get(dev
, "vdd");
263 if (IS_ERR(bus
->regulator
)) {
264 dev_err(dev
, "failed to get VDD regulator\n");
265 return PTR_ERR(bus
->regulator
);
268 ret
= regulator_enable(bus
->regulator
);
270 dev_err(dev
, "failed to enable VDD regulator\n");
275 * Get the devfreq-event devices to get the current utilization of
276 * buses. This raw data will be used in devfreq ondemand governor.
278 count
= devfreq_event_get_edev_count(dev
);
280 dev_err(dev
, "failed to get the count of devfreq-event dev\n");
284 bus
->edev_count
= count
;
286 size
= sizeof(*bus
->edev
) * count
;
287 bus
->edev
= devm_kzalloc(dev
, size
, GFP_KERNEL
);
293 for (i
= 0; i
< count
; i
++) {
294 bus
->edev
[i
] = devfreq_event_get_edev_by_phandle(dev
, i
);
295 if (IS_ERR(bus
->edev
[i
])) {
302 * Optionally, Get the saturation ratio according to Exynos SoC
303 * When measuring the utilization of each AXI bus with devfreq-event
304 * devices, the measured real cycle might be much lower than the
305 * total cycle of bus during sampling rate. In result, the devfreq
306 * simple-ondemand governor might not decide to change the current
307 * frequency due to too utilization (= real cycle/total cycle).
308 * So, this property is used to adjust the utilization when calculating
309 * the busy_time in exynos_bus_get_dev_status().
311 if (of_property_read_u32(np
, "exynos,saturation-ratio", &bus
->ratio
))
312 bus
->ratio
= DEFAULT_SATURATION_RATIO
;
314 if (of_property_read_u32(np
, "exynos,voltage-tolerance",
315 &bus
->voltage_tolerance
))
316 bus
->voltage_tolerance
= DEFAULT_VOLTAGE_TOLERANCE
;
321 regulator_disable(bus
->regulator
);
326 static int exynos_bus_parse_of(struct device_node
*np
,
327 struct exynos_bus
*bus
)
329 struct device
*dev
= bus
->dev
;
330 struct dev_pm_opp
*opp
;
334 /* Get the clock to provide each bus with source clock */
335 bus
->clk
= devm_clk_get(dev
, "bus");
336 if (IS_ERR(bus
->clk
)) {
337 dev_err(dev
, "failed to get bus clock\n");
338 return PTR_ERR(bus
->clk
);
341 ret
= clk_prepare_enable(bus
->clk
);
343 dev_err(dev
, "failed to get enable clock\n");
347 /* Get the freq and voltage from OPP table to scale the bus freq */
348 ret
= dev_pm_opp_of_add_table(dev
);
350 dev_err(dev
, "failed to get OPP table\n");
354 rate
= clk_get_rate(bus
->clk
);
356 opp
= devfreq_recommended_opp(dev
, &rate
, 0);
358 dev_err(dev
, "failed to find dev_pm_opp\n");
362 bus
->curr_freq
= dev_pm_opp_get_freq(opp
);
368 dev_pm_opp_of_remove_table(dev
);
370 clk_disable_unprepare(bus
->clk
);
375 static int exynos_bus_probe(struct platform_device
*pdev
)
377 struct device
*dev
= &pdev
->dev
;
378 struct device_node
*np
= dev
->of_node
, *node
;
379 struct devfreq_dev_profile
*profile
;
380 struct devfreq_simple_ondemand_data
*ondemand_data
;
381 struct devfreq_passive_data
*passive_data
;
382 struct devfreq
*parent_devfreq
;
383 struct exynos_bus
*bus
;
385 unsigned long min_freq
, max_freq
;
388 dev_err(dev
, "failed to find devicetree node\n");
392 bus
= devm_kzalloc(&pdev
->dev
, sizeof(*bus
), GFP_KERNEL
);
395 mutex_init(&bus
->lock
);
396 bus
->dev
= &pdev
->dev
;
397 platform_set_drvdata(pdev
, bus
);
399 /* Parse the device-tree to get the resource information */
400 ret
= exynos_bus_parse_of(np
, bus
);
404 profile
= devm_kzalloc(dev
, sizeof(*profile
), GFP_KERNEL
);
410 node
= of_parse_phandle(dev
->of_node
, "devfreq", 0);
415 ret
= exynos_bus_parent_parse_of(np
, bus
);
421 /* Initialize the struct profile and governor data for parent device */
422 profile
->polling_ms
= 50;
423 profile
->target
= exynos_bus_target
;
424 profile
->get_dev_status
= exynos_bus_get_dev_status
;
425 profile
->exit
= exynos_bus_exit
;
427 ondemand_data
= devm_kzalloc(dev
, sizeof(*ondemand_data
), GFP_KERNEL
);
428 if (!ondemand_data
) {
432 ondemand_data
->upthreshold
= 40;
433 ondemand_data
->downdifferential
= 5;
435 /* Add devfreq device to monitor and handle the exynos bus */
436 bus
->devfreq
= devm_devfreq_add_device(dev
, profile
,
437 DEVFREQ_GOV_SIMPLE_ONDEMAND
,
439 if (IS_ERR(bus
->devfreq
)) {
440 dev_err(dev
, "failed to add devfreq device\n");
441 ret
= PTR_ERR(bus
->devfreq
);
445 /* Register opp_notifier to catch the change of OPP */
446 ret
= devm_devfreq_register_opp_notifier(dev
, bus
->devfreq
);
448 dev_err(dev
, "failed to register opp notifier\n");
453 * Enable devfreq-event to get raw data which is used to determine
456 ret
= exynos_bus_enable_edev(bus
);
458 dev_err(dev
, "failed to enable devfreq-event devices\n");
462 ret
= exynos_bus_set_event(bus
);
464 dev_err(dev
, "failed to set event to devfreq-event devices\n");
470 /* Initialize the struct profile and governor data for passive device */
471 profile
->target
= exynos_bus_passive_target
;
472 profile
->exit
= exynos_bus_passive_exit
;
474 /* Get the instance of parent devfreq device */
475 parent_devfreq
= devfreq_get_devfreq_by_phandle(dev
, 0);
476 if (IS_ERR(parent_devfreq
)) {
481 passive_data
= devm_kzalloc(dev
, sizeof(*passive_data
), GFP_KERNEL
);
486 passive_data
->parent
= parent_devfreq
;
488 /* Add devfreq device for exynos bus with passive governor */
489 bus
->devfreq
= devm_devfreq_add_device(dev
, profile
, DEVFREQ_GOV_PASSIVE
,
491 if (IS_ERR(bus
->devfreq
)) {
493 "failed to add devfreq dev with passive governor\n");
494 ret
= PTR_ERR(bus
->devfreq
);
499 max_state
= bus
->devfreq
->profile
->max_state
;
500 min_freq
= (bus
->devfreq
->profile
->freq_table
[0] / 1000);
501 max_freq
= (bus
->devfreq
->profile
->freq_table
[max_state
- 1] / 1000);
502 pr_info("exynos-bus: new bus device registered: %s (%6ld KHz ~ %6ld KHz)\n",
503 dev_name(dev
), min_freq
, max_freq
);
508 dev_pm_opp_of_remove_table(dev
);
509 clk_disable_unprepare(bus
->clk
);
514 static void exynos_bus_shutdown(struct platform_device
*pdev
)
516 struct exynos_bus
*bus
= dev_get_drvdata(&pdev
->dev
);
518 devfreq_suspend_device(bus
->devfreq
);
521 #ifdef CONFIG_PM_SLEEP
522 static int exynos_bus_resume(struct device
*dev
)
524 struct exynos_bus
*bus
= dev_get_drvdata(dev
);
527 ret
= exynos_bus_enable_edev(bus
);
529 dev_err(dev
, "failed to enable the devfreq-event devices\n");
536 static int exynos_bus_suspend(struct device
*dev
)
538 struct exynos_bus
*bus
= dev_get_drvdata(dev
);
541 ret
= exynos_bus_disable_edev(bus
);
543 dev_err(dev
, "failed to disable the devfreq-event devices\n");
551 static const struct dev_pm_ops exynos_bus_pm
= {
552 SET_SYSTEM_SLEEP_PM_OPS(exynos_bus_suspend
, exynos_bus_resume
)
555 static const struct of_device_id exynos_bus_of_match
[] = {
556 { .compatible
= "samsung,exynos-bus", },
559 MODULE_DEVICE_TABLE(of
, exynos_bus_of_match
);
561 static struct platform_driver exynos_bus_platdrv
= {
562 .probe
= exynos_bus_probe
,
563 .shutdown
= exynos_bus_shutdown
,
565 .name
= "exynos-bus",
566 .pm
= &exynos_bus_pm
,
567 .of_match_table
= of_match_ptr(exynos_bus_of_match
),
570 module_platform_driver(exynos_bus_platdrv
);
572 MODULE_DESCRIPTION("Generic Exynos Bus frequency driver");
573 MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
574 MODULE_LICENSE("GPL v2");