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.
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
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 stat->total_time = stat->busy_time = 0;
134 stat->busy_time = (edata.load_count * 100) / bus->ratio;
135 stat->total_time = edata.total_count;
137 dev_dbg(dev, "Usage of devfreq-event : %lu/%lu\n", stat->busy_time,
141 ret = exynos_bus_set_event(bus);
143 dev_err(dev, "failed to set event to devfreq-event devices\n");
150 static void exynos_bus_exit(struct device *dev)
152 struct exynos_bus *bus = dev_get_drvdata(dev);
155 ret = exynos_bus_disable_edev(bus);
157 dev_warn(dev, "failed to disable the devfreq-event devices\n");
159 dev_pm_opp_of_remove_table(dev);
160 clk_disable_unprepare(bus->clk);
161 if (bus->opp_table) {
162 dev_pm_opp_put_regulators(bus->opp_table);
163 bus->opp_table = NULL;
167 static void exynos_bus_passive_exit(struct device *dev)
169 struct exynos_bus *bus = dev_get_drvdata(dev);
171 dev_pm_opp_of_remove_table(dev);
172 clk_disable_unprepare(bus->clk);
175 static int exynos_bus_parent_parse_of(struct device_node *np,
176 struct exynos_bus *bus)
178 struct device *dev = bus->dev;
179 struct opp_table *opp_table;
180 const char *vdd = "vdd";
181 int i, ret, count, size;
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);
190 bus->opp_table = opp_table;
193 * Get the devfreq-event devices to get the current utilization of
194 * buses. This raw data will be used in devfreq ondemand governor.
196 count = devfreq_event_get_edev_count(dev);
198 dev_err(dev, "failed to get the count of devfreq-event dev\n");
202 bus->edev_count = count;
204 size = sizeof(*bus->edev) * count;
205 bus->edev = devm_kzalloc(dev, size, GFP_KERNEL);
211 for (i = 0; i < count; i++) {
212 bus->edev[i] = devfreq_event_get_edev_by_phandle(dev, i);
213 if (IS_ERR(bus->edev[i])) {
220 * Optionally, Get the saturation ratio according to Exynos SoC
221 * When measuring the utilization of each AXI bus with devfreq-event
222 * devices, the measured real cycle might be much lower than the
223 * total cycle of bus during sampling rate. In result, the devfreq
224 * simple-ondemand governor might not decide to change the current
225 * frequency due to too utilization (= real cycle/total cycle).
226 * So, this property is used to adjust the utilization when calculating
227 * the busy_time in exynos_bus_get_dev_status().
229 if (of_property_read_u32(np, "exynos,saturation-ratio", &bus->ratio))
230 bus->ratio = DEFAULT_SATURATION_RATIO;
235 dev_pm_opp_put_regulators(bus->opp_table);
236 bus->opp_table = NULL;
241 static int exynos_bus_parse_of(struct device_node *np,
242 struct exynos_bus *bus)
244 struct device *dev = bus->dev;
245 struct dev_pm_opp *opp;
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);
256 ret = clk_prepare_enable(bus->clk);
258 dev_err(dev, "failed to get enable clock\n");
262 /* Get the freq and voltage from OPP table to scale the bus freq */
263 ret = dev_pm_opp_of_add_table(dev);
265 dev_err(dev, "failed to get OPP table\n");
269 rate = clk_get_rate(bus->clk);
271 opp = devfreq_recommended_opp(dev, &rate, 0);
273 dev_err(dev, "failed to find dev_pm_opp\n");
277 bus->curr_freq = dev_pm_opp_get_freq(opp);
283 dev_pm_opp_of_remove_table(dev);
285 clk_disable_unprepare(bus->clk);
290 static int exynos_bus_probe(struct platform_device *pdev)
292 struct device *dev = &pdev->dev;
293 struct device_node *np = dev->of_node, *node;
294 struct devfreq_dev_profile *profile;
295 struct devfreq_simple_ondemand_data *ondemand_data;
296 struct devfreq_passive_data *passive_data;
297 struct devfreq *parent_devfreq;
298 struct exynos_bus *bus;
300 unsigned long min_freq, max_freq;
301 bool passive = false;
304 dev_err(dev, "failed to find devicetree node\n");
308 bus = devm_kzalloc(&pdev->dev, sizeof(*bus), GFP_KERNEL);
311 mutex_init(&bus->lock);
312 bus->dev = &pdev->dev;
313 platform_set_drvdata(pdev, bus);
315 profile = devm_kzalloc(dev, sizeof(*profile), GFP_KERNEL);
319 node = of_parse_phandle(dev->of_node, "devfreq", 0);
324 ret = exynos_bus_parent_parse_of(np, bus);
329 /* Parse the device-tree to get the resource information */
330 ret = exynos_bus_parse_of(np, bus);
337 /* Initialize the struct profile and governor data for parent device */
338 profile->polling_ms = 50;
339 profile->target = exynos_bus_target;
340 profile->get_dev_status = exynos_bus_get_dev_status;
341 profile->exit = exynos_bus_exit;
343 ondemand_data = devm_kzalloc(dev, sizeof(*ondemand_data), GFP_KERNEL);
344 if (!ondemand_data) {
348 ondemand_data->upthreshold = 40;
349 ondemand_data->downdifferential = 5;
351 /* Add devfreq device to monitor and handle the exynos bus */
352 bus->devfreq = devm_devfreq_add_device(dev, profile,
353 DEVFREQ_GOV_SIMPLE_ONDEMAND,
355 if (IS_ERR(bus->devfreq)) {
356 dev_err(dev, "failed to add devfreq device\n");
357 ret = PTR_ERR(bus->devfreq);
361 /* Register opp_notifier to catch the change of OPP */
362 ret = devm_devfreq_register_opp_notifier(dev, bus->devfreq);
364 dev_err(dev, "failed to register opp notifier\n");
369 * Enable devfreq-event to get raw data which is used to determine
372 ret = exynos_bus_enable_edev(bus);
374 dev_err(dev, "failed to enable devfreq-event devices\n");
378 ret = exynos_bus_set_event(bus);
380 dev_err(dev, "failed to set event to devfreq-event devices\n");
386 /* Initialize the struct profile and governor data for passive device */
387 profile->target = exynos_bus_target;
388 profile->exit = exynos_bus_passive_exit;
390 /* Get the instance of parent devfreq device */
391 parent_devfreq = devfreq_get_devfreq_by_phandle(dev, 0);
392 if (IS_ERR(parent_devfreq)) {
397 passive_data = devm_kzalloc(dev, sizeof(*passive_data), GFP_KERNEL);
402 passive_data->parent = parent_devfreq;
404 /* Add devfreq device for exynos bus with passive governor */
405 bus->devfreq = devm_devfreq_add_device(dev, profile, DEVFREQ_GOV_PASSIVE,
407 if (IS_ERR(bus->devfreq)) {
409 "failed to add devfreq dev with passive governor\n");
410 ret = PTR_ERR(bus->devfreq);
415 max_state = bus->devfreq->profile->max_state;
416 min_freq = (bus->devfreq->profile->freq_table[0] / 1000);
417 max_freq = (bus->devfreq->profile->freq_table[max_state - 1] / 1000);
418 pr_info("exynos-bus: new bus device registered: %s (%6ld KHz ~ %6ld KHz)\n",
419 dev_name(dev), min_freq, max_freq);
424 dev_pm_opp_of_remove_table(dev);
425 clk_disable_unprepare(bus->clk);
428 dev_pm_opp_put_regulators(bus->opp_table);
429 bus->opp_table = NULL;
435 static void exynos_bus_shutdown(struct platform_device *pdev)
437 struct exynos_bus *bus = dev_get_drvdata(&pdev->dev);
439 devfreq_suspend_device(bus->devfreq);
442 #ifdef CONFIG_PM_SLEEP
443 static int exynos_bus_resume(struct device *dev)
445 struct exynos_bus *bus = dev_get_drvdata(dev);
448 ret = exynos_bus_enable_edev(bus);
450 dev_err(dev, "failed to enable the devfreq-event devices\n");
457 static int exynos_bus_suspend(struct device *dev)
459 struct exynos_bus *bus = dev_get_drvdata(dev);
462 ret = exynos_bus_disable_edev(bus);
464 dev_err(dev, "failed to disable the devfreq-event devices\n");
472 static const struct dev_pm_ops exynos_bus_pm = {
473 SET_SYSTEM_SLEEP_PM_OPS(exynos_bus_suspend, exynos_bus_resume)
476 static const struct of_device_id exynos_bus_of_match[] = {
477 { .compatible = "samsung,exynos-bus", },
480 MODULE_DEVICE_TABLE(of, exynos_bus_of_match);
482 static struct platform_driver exynos_bus_platdrv = {
483 .probe = exynos_bus_probe,
484 .shutdown = exynos_bus_shutdown,
486 .name = "exynos-bus",
487 .pm = &exynos_bus_pm,
488 .of_match_table = of_match_ptr(exynos_bus_of_match),
491 module_platform_driver(exynos_bus_platdrv);
493 MODULE_DESCRIPTION("Generic Exynos Bus frequency driver");
495 MODULE_LICENSE("GPL v2");