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
28 struct devfreq *devfreq;
29 struct devfreq_event_dev **edev;
30 unsigned int edev_count;
33 unsigned long curr_freq;
35 struct opp_table *opp_table;
41 * Control the devfreq-event device to get the current state of bus
43 #define exynos_bus_ops_edev(ops) \
44 static int exynos_bus_##ops(struct exynos_bus *bus) \
48 for (i = 0; i < bus->edev_count; i++) { \
51 ret = devfreq_event_##ops(bus->edev[i]); \
58 exynos_bus_ops_edev(enable_edev);
59 exynos_bus_ops_edev(disable_edev);
60 exynos_bus_ops_edev(set_event);
62 static int exynos_bus_get_event(struct exynos_bus *bus,
63 struct devfreq_event_data *edata)
65 struct devfreq_event_data event_data;
66 unsigned long load_count = 0, total_count = 0;
69 for (i = 0; i < bus->edev_count; i++) {
73 ret = devfreq_event_get_event(bus->edev[i], &event_data);
77 if (i == 0 || event_data.load_count > load_count) {
78 load_count = event_data.load_count;
79 total_count = event_data.total_count;
83 edata->load_count = load_count;
84 edata->total_count = total_count;
90 * devfreq function for both simple-ondemand and passive governor
92 static int exynos_bus_target(struct device *dev, unsigned long *freq, u32 flags)
94 struct exynos_bus *bus = dev_get_drvdata(dev);
95 struct dev_pm_opp *new_opp;
98 /* Get correct frequency for bus. */
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");
102 return PTR_ERR(new_opp);
105 dev_pm_opp_put(new_opp);
107 /* Change voltage and frequency according to new OPP level */
108 mutex_lock(&bus->lock);
109 ret = dev_pm_opp_set_rate(dev, *freq);
111 bus->curr_freq = *freq;
113 mutex_unlock(&bus->lock);
118 static int exynos_bus_get_dev_status(struct device *dev,
119 struct devfreq_dev_status *stat)
121 struct exynos_bus *bus = dev_get_drvdata(dev);
122 struct devfreq_event_data edata;
125 stat->current_frequency = bus->curr_freq;
127 ret = exynos_bus_get_event(bus, &edata);
129 dev_err(dev, "failed to get event from devfreq-event devices\n");
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, "devfreq-events");
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,
213 "devfreq-events", i);
214 if (IS_ERR(bus->edev[i])) {
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().
230 if (of_property_read_u32(np, "exynos,saturation-ratio", &bus->ratio))
231 bus->ratio = DEFAULT_SATURATION_RATIO;
236 dev_pm_opp_put_regulators(bus->opp_table);
237 bus->opp_table = NULL;
242 static int exynos_bus_parse_of(struct device_node *np,
243 struct exynos_bus *bus)
245 struct device *dev = bus->dev;
246 struct dev_pm_opp *opp;
250 /* Get the clock to provide each bus with source clock */
251 bus->clk = devm_clk_get(dev, "bus");
252 if (IS_ERR(bus->clk)) {
253 dev_err(dev, "failed to get bus clock\n");
254 return PTR_ERR(bus->clk);
257 ret = clk_prepare_enable(bus->clk);
259 dev_err(dev, "failed to get enable clock\n");
263 /* Get the freq and voltage from OPP table to scale the bus freq */
264 ret = dev_pm_opp_of_add_table(dev);
266 dev_err(dev, "failed to get OPP table\n");
270 rate = clk_get_rate(bus->clk);
272 opp = devfreq_recommended_opp(dev, &rate, 0);
274 dev_err(dev, "failed to find dev_pm_opp\n");
278 bus->curr_freq = dev_pm_opp_get_freq(opp);
284 dev_pm_opp_of_remove_table(dev);
286 clk_disable_unprepare(bus->clk);
291 static int exynos_bus_profile_init(struct exynos_bus *bus,
292 struct devfreq_dev_profile *profile)
294 struct device *dev = bus->dev;
295 struct devfreq_simple_ondemand_data *ondemand_data;
298 /* Initialize the struct profile and governor data for parent device */
299 profile->polling_ms = 50;
300 profile->target = exynos_bus_target;
301 profile->get_dev_status = exynos_bus_get_dev_status;
302 profile->exit = exynos_bus_exit;
304 ondemand_data = devm_kzalloc(dev, sizeof(*ondemand_data), GFP_KERNEL);
308 ondemand_data->upthreshold = 40;
309 ondemand_data->downdifferential = 5;
311 /* Add devfreq device to monitor and handle the exynos bus */
312 bus->devfreq = devm_devfreq_add_device(dev, profile,
313 DEVFREQ_GOV_SIMPLE_ONDEMAND,
315 if (IS_ERR(bus->devfreq)) {
316 dev_err(dev, "failed to add devfreq device\n");
317 return PTR_ERR(bus->devfreq);
320 /* Register opp_notifier to catch the change of OPP */
321 ret = devm_devfreq_register_opp_notifier(dev, bus->devfreq);
323 dev_err(dev, "failed to register opp notifier\n");
328 * Enable devfreq-event to get raw data which is used to determine
331 ret = exynos_bus_enable_edev(bus);
333 dev_err(dev, "failed to enable devfreq-event devices\n");
337 ret = exynos_bus_set_event(bus);
339 dev_err(dev, "failed to set event to devfreq-event devices\n");
346 if (exynos_bus_disable_edev(bus))
347 dev_warn(dev, "failed to disable the devfreq-event devices\n");
352 static int exynos_bus_profile_init_passive(struct exynos_bus *bus,
353 struct devfreq_dev_profile *profile)
355 struct device *dev = bus->dev;
356 struct devfreq_passive_data *passive_data;
357 struct devfreq *parent_devfreq;
359 /* Initialize the struct profile and governor data for passive device */
360 profile->target = exynos_bus_target;
361 profile->exit = exynos_bus_passive_exit;
363 /* Get the instance of parent devfreq device */
364 parent_devfreq = devfreq_get_devfreq_by_phandle(dev, "devfreq", 0);
365 if (IS_ERR(parent_devfreq))
366 return -EPROBE_DEFER;
368 passive_data = devm_kzalloc(dev, sizeof(*passive_data), GFP_KERNEL);
372 passive_data->parent = parent_devfreq;
374 /* Add devfreq device for exynos bus with passive governor */
375 bus->devfreq = devm_devfreq_add_device(dev, profile, DEVFREQ_GOV_PASSIVE,
377 if (IS_ERR(bus->devfreq)) {
379 "failed to add devfreq dev with passive governor\n");
380 return PTR_ERR(bus->devfreq);
386 static int exynos_bus_probe(struct platform_device *pdev)
388 struct device *dev = &pdev->dev;
389 struct device_node *np = dev->of_node, *node;
390 struct devfreq_dev_profile *profile;
391 struct exynos_bus *bus;
393 unsigned long min_freq, max_freq;
394 bool passive = false;
397 dev_err(dev, "failed to find devicetree node\n");
401 bus = devm_kzalloc(&pdev->dev, sizeof(*bus), GFP_KERNEL);
404 mutex_init(&bus->lock);
405 bus->dev = &pdev->dev;
406 platform_set_drvdata(pdev, bus);
408 profile = devm_kzalloc(dev, sizeof(*profile), GFP_KERNEL);
412 node = of_parse_phandle(dev->of_node, "devfreq", 0);
417 ret = exynos_bus_parent_parse_of(np, bus);
422 /* Parse the device-tree to get the resource information */
423 ret = exynos_bus_parse_of(np, bus);
428 ret = exynos_bus_profile_init_passive(bus, profile);
430 ret = exynos_bus_profile_init(bus, profile);
435 max_state = bus->devfreq->profile->max_state;
436 min_freq = (bus->devfreq->profile->freq_table[0] / 1000);
437 max_freq = (bus->devfreq->profile->freq_table[max_state - 1] / 1000);
438 pr_info("exynos-bus: new bus device registered: %s (%6ld KHz ~ %6ld KHz)\n",
439 dev_name(dev), min_freq, max_freq);
444 dev_pm_opp_of_remove_table(dev);
445 clk_disable_unprepare(bus->clk);
448 dev_pm_opp_put_regulators(bus->opp_table);
449 bus->opp_table = NULL;
455 static void exynos_bus_shutdown(struct platform_device *pdev)
457 struct exynos_bus *bus = dev_get_drvdata(&pdev->dev);
459 devfreq_suspend_device(bus->devfreq);
462 #ifdef CONFIG_PM_SLEEP
463 static int exynos_bus_resume(struct device *dev)
465 struct exynos_bus *bus = dev_get_drvdata(dev);
468 ret = exynos_bus_enable_edev(bus);
470 dev_err(dev, "failed to enable the devfreq-event devices\n");
477 static int exynos_bus_suspend(struct device *dev)
479 struct exynos_bus *bus = dev_get_drvdata(dev);
482 ret = exynos_bus_disable_edev(bus);
484 dev_err(dev, "failed to disable the devfreq-event devices\n");
492 static const struct dev_pm_ops exynos_bus_pm = {
493 SET_SYSTEM_SLEEP_PM_OPS(exynos_bus_suspend, exynos_bus_resume)
496 static const struct of_device_id exynos_bus_of_match[] = {
497 { .compatible = "samsung,exynos-bus", },
500 MODULE_DEVICE_TABLE(of, exynos_bus_of_match);
502 static struct platform_driver exynos_bus_platdrv = {
503 .probe = exynos_bus_probe,
504 .shutdown = exynos_bus_shutdown,
506 .name = "exynos-bus",
507 .pm = &exynos_bus_pm,
508 .of_match_table = of_match_ptr(exynos_bus_of_match),
511 module_platform_driver(exynos_bus_platdrv);
513 MODULE_DESCRIPTION("Generic Exynos Bus frequency driver");
514 MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
515 MODULE_LICENSE("GPL v2");