2 * Generic Exynos Bus frequency driver with DEVFREQ Framework
4 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
5 * Author : Chanwoo Choi <cw00.choi@samsung.com>
7 * This driver support Exynos Bus frequency feature by using
8 * DEVFREQ framework and is based on drivers/devfreq/exynos/exynos4_bus.c.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
15 #include <linux/clk.h>
16 #include <linux/devfreq.h>
17 #include <linux/devfreq-event.h>
18 #include <linux/device.h>
19 #include <linux/export.h>
20 #include <linux/module.h>
21 #include <linux/of_device.h>
22 #include <linux/pm_opp.h>
23 #include <linux/platform_device.h>
24 #include <linux/regulator/consumer.h>
25 #include <linux/slab.h>
27 #define DEFAULT_SATURATION_RATIO 40
28 #define DEFAULT_VOLTAGE_TOLERANCE 2
33 struct devfreq *devfreq;
34 struct devfreq_event_dev **edev;
35 unsigned int edev_count;
38 unsigned long curr_freq;
40 struct regulator *regulator;
42 unsigned int voltage_tolerance;
47 * Control the devfreq-event device to get the current state of bus
49 #define exynos_bus_ops_edev(ops) \
50 static int exynos_bus_##ops(struct exynos_bus *bus) \
54 for (i = 0; i < bus->edev_count; i++) { \
57 ret = devfreq_event_##ops(bus->edev[i]); \
64 exynos_bus_ops_edev(enable_edev);
65 exynos_bus_ops_edev(disable_edev);
66 exynos_bus_ops_edev(set_event);
68 static int exynos_bus_get_event(struct exynos_bus *bus,
69 struct devfreq_event_data *edata)
71 struct devfreq_event_data event_data;
72 unsigned long load_count = 0, total_count = 0;
75 for (i = 0; i < bus->edev_count; i++) {
79 ret = devfreq_event_get_event(bus->edev[i], &event_data);
83 if (i == 0 || event_data.load_count > load_count) {
84 load_count = event_data.load_count;
85 total_count = event_data.total_count;
89 edata->load_count = load_count;
90 edata->total_count = total_count;
96 * Must necessary function for devfreq simple-ondemand governor
98 static int exynos_bus_target(struct device *dev, unsigned long *freq, u32 flags)
100 struct exynos_bus *bus = dev_get_drvdata(dev);
101 struct dev_pm_opp *new_opp;
102 unsigned long old_freq, new_freq, new_volt, tol;
105 /* Get new opp-bus instance according to new bus clock */
107 new_opp = devfreq_recommended_opp(dev, freq, flags);
108 if (IS_ERR(new_opp)) {
109 dev_err(dev, "failed to get recommended opp instance\n");
111 return PTR_ERR(new_opp);
114 new_freq = dev_pm_opp_get_freq(new_opp);
115 new_volt = dev_pm_opp_get_voltage(new_opp);
116 old_freq = bus->curr_freq;
119 if (old_freq == new_freq)
121 tol = new_volt * bus->voltage_tolerance / 100;
123 /* Change voltage and frequency according to new OPP level */
124 mutex_lock(&bus->lock);
126 if (old_freq < new_freq) {
127 ret = regulator_set_voltage_tol(bus->regulator, new_volt, tol);
129 dev_err(bus->dev, "failed to set voltage\n");
134 ret = clk_set_rate(bus->clk, new_freq);
136 dev_err(dev, "failed to change clock of bus\n");
137 clk_set_rate(bus->clk, old_freq);
141 if (old_freq > new_freq) {
142 ret = regulator_set_voltage_tol(bus->regulator, new_volt, tol);
144 dev_err(bus->dev, "failed to set voltage\n");
148 bus->curr_freq = new_freq;
150 dev_dbg(dev, "Set the frequency of bus (%lukHz -> %lukHz)\n",
151 old_freq/1000, new_freq/1000);
153 mutex_unlock(&bus->lock);
158 static int exynos_bus_get_dev_status(struct device *dev,
159 struct devfreq_dev_status *stat)
161 struct exynos_bus *bus = dev_get_drvdata(dev);
162 struct devfreq_event_data edata;
165 stat->current_frequency = bus->curr_freq;
167 ret = exynos_bus_get_event(bus, &edata);
169 stat->total_time = stat->busy_time = 0;
173 stat->busy_time = (edata.load_count * 100) / bus->ratio;
174 stat->total_time = edata.total_count;
176 dev_dbg(dev, "Usage of devfreq-event : %lu/%lu\n", stat->busy_time,
180 ret = exynos_bus_set_event(bus);
182 dev_err(dev, "failed to set event to devfreq-event devices\n");
189 static void exynos_bus_exit(struct device *dev)
191 struct exynos_bus *bus = dev_get_drvdata(dev);
194 ret = exynos_bus_disable_edev(bus);
196 dev_warn(dev, "failed to disable the devfreq-event devices\n");
199 regulator_disable(bus->regulator);
201 dev_pm_opp_of_remove_table(dev);
202 clk_disable_unprepare(bus->clk);
206 * Must necessary function for devfreq passive governor
208 static int exynos_bus_passive_target(struct device *dev, unsigned long *freq,
211 struct exynos_bus *bus = dev_get_drvdata(dev);
212 struct dev_pm_opp *new_opp;
213 unsigned long old_freq, new_freq;
216 /* Get new opp-bus instance according to new bus clock */
218 new_opp = devfreq_recommended_opp(dev, freq, flags);
219 if (IS_ERR(new_opp)) {
220 dev_err(dev, "failed to get recommended opp instance\n");
222 return PTR_ERR(new_opp);
225 new_freq = dev_pm_opp_get_freq(new_opp);
226 old_freq = bus->curr_freq;
229 if (old_freq == new_freq)
232 /* Change the frequency according to new OPP level */
233 mutex_lock(&bus->lock);
235 ret = clk_set_rate(bus->clk, new_freq);
237 dev_err(dev, "failed to set the clock of bus\n");
242 bus->curr_freq = new_freq;
244 dev_dbg(dev, "Set the frequency of bus (%lukHz -> %lukHz)\n",
245 old_freq/1000, new_freq/1000);
247 mutex_unlock(&bus->lock);
252 static void exynos_bus_passive_exit(struct device *dev)
254 struct exynos_bus *bus = dev_get_drvdata(dev);
256 dev_pm_opp_of_remove_table(dev);
257 clk_disable_unprepare(bus->clk);
260 static int exynos_bus_parent_parse_of(struct device_node *np,
261 struct exynos_bus *bus)
263 struct device *dev = bus->dev;
264 int i, ret, count, size;
266 /* Get the regulator to provide each bus with the power */
267 bus->regulator = devm_regulator_get(dev, "vdd");
268 if (IS_ERR(bus->regulator)) {
269 dev_err(dev, "failed to get VDD regulator\n");
270 return PTR_ERR(bus->regulator);
273 ret = regulator_enable(bus->regulator);
275 dev_err(dev, "failed to enable VDD regulator\n");
280 * Get the devfreq-event devices to get the current utilization of
281 * buses. This raw data will be used in devfreq ondemand governor.
283 count = devfreq_event_get_edev_count(dev);
285 dev_err(dev, "failed to get the count of devfreq-event dev\n");
289 bus->edev_count = count;
291 size = sizeof(*bus->edev) * count;
292 bus->edev = devm_kzalloc(dev, size, GFP_KERNEL);
298 for (i = 0; i < count; i++) {
299 bus->edev[i] = devfreq_event_get_edev_by_phandle(dev, i);
300 if (IS_ERR(bus->edev[i])) {
307 * Optionally, Get the saturation ratio according to Exynos SoC
308 * When measuring the utilization of each AXI bus with devfreq-event
309 * devices, the measured real cycle might be much lower than the
310 * total cycle of bus during sampling rate. In result, the devfreq
311 * simple-ondemand governor might not decide to change the current
312 * frequency due to too utilization (= real cycle/total cycle).
313 * So, this property is used to adjust the utilization when calculating
314 * the busy_time in exynos_bus_get_dev_status().
316 if (of_property_read_u32(np, "exynos,saturation-ratio", &bus->ratio))
317 bus->ratio = DEFAULT_SATURATION_RATIO;
319 if (of_property_read_u32(np, "exynos,voltage-tolerance",
320 &bus->voltage_tolerance))
321 bus->voltage_tolerance = DEFAULT_VOLTAGE_TOLERANCE;
326 regulator_disable(bus->regulator);
331 static int exynos_bus_parse_of(struct device_node *np,
332 struct exynos_bus *bus)
334 struct device *dev = bus->dev;
335 struct dev_pm_opp *opp;
339 /* Get the clock to provide each bus with source clock */
340 bus->clk = devm_clk_get(dev, "bus");
341 if (IS_ERR(bus->clk)) {
342 dev_err(dev, "failed to get bus clock\n");
343 return PTR_ERR(bus->clk);
346 ret = clk_prepare_enable(bus->clk);
348 dev_err(dev, "failed to get enable clock\n");
352 /* Get the freq and voltage from OPP table to scale the bus freq */
353 ret = dev_pm_opp_of_add_table(dev);
355 dev_err(dev, "failed to get OPP table\n");
359 rate = clk_get_rate(bus->clk);
362 opp = devfreq_recommended_opp(dev, &rate, 0);
364 dev_err(dev, "failed to find dev_pm_opp\n");
369 bus->curr_freq = dev_pm_opp_get_freq(opp);
375 dev_pm_opp_of_remove_table(dev);
377 clk_disable_unprepare(bus->clk);
382 static int exynos_bus_probe(struct platform_device *pdev)
384 struct device *dev = &pdev->dev;
385 struct device_node *np = dev->of_node, *node;
386 struct devfreq_dev_profile *profile;
387 struct devfreq_simple_ondemand_data *ondemand_data;
388 struct devfreq_passive_data *passive_data;
389 struct devfreq *parent_devfreq;
390 struct exynos_bus *bus;
392 unsigned long min_freq, max_freq;
395 dev_err(dev, "failed to find devicetree node\n");
399 bus = devm_kzalloc(&pdev->dev, sizeof(*bus), GFP_KERNEL);
402 mutex_init(&bus->lock);
403 bus->dev = &pdev->dev;
404 platform_set_drvdata(pdev, bus);
406 /* Parse the device-tree to get the resource information */
407 ret = exynos_bus_parse_of(np, bus);
411 profile = devm_kzalloc(dev, sizeof(*profile), GFP_KERNEL);
417 node = of_parse_phandle(dev->of_node, "devfreq", 0);
422 ret = exynos_bus_parent_parse_of(np, bus);
428 /* Initialize the struct profile and governor data for parent device */
429 profile->polling_ms = 50;
430 profile->target = exynos_bus_target;
431 profile->get_dev_status = exynos_bus_get_dev_status;
432 profile->exit = exynos_bus_exit;
434 ondemand_data = devm_kzalloc(dev, sizeof(*ondemand_data), GFP_KERNEL);
435 if (!ondemand_data) {
439 ondemand_data->upthreshold = 40;
440 ondemand_data->downdifferential = 5;
442 /* Add devfreq device to monitor and handle the exynos bus */
443 bus->devfreq = devm_devfreq_add_device(dev, profile, "simple_ondemand",
445 if (IS_ERR(bus->devfreq)) {
446 dev_err(dev, "failed to add devfreq device\n");
447 ret = PTR_ERR(bus->devfreq);
451 /* Register opp_notifier to catch the change of OPP */
452 ret = devm_devfreq_register_opp_notifier(dev, bus->devfreq);
454 dev_err(dev, "failed to register opp notifier\n");
459 * Enable devfreq-event to get raw data which is used to determine
462 ret = exynos_bus_enable_edev(bus);
464 dev_err(dev, "failed to enable devfreq-event devices\n");
468 ret = exynos_bus_set_event(bus);
470 dev_err(dev, "failed to set event to devfreq-event devices\n");
476 /* Initialize the struct profile and governor data for passive device */
477 profile->target = exynos_bus_passive_target;
478 profile->exit = exynos_bus_passive_exit;
480 /* Get the instance of parent devfreq device */
481 parent_devfreq = devfreq_get_devfreq_by_phandle(dev, 0);
482 if (IS_ERR(parent_devfreq)) {
487 passive_data = devm_kzalloc(dev, sizeof(*passive_data), GFP_KERNEL);
492 passive_data->parent = parent_devfreq;
494 /* Add devfreq device for exynos bus with passive governor */
495 bus->devfreq = devm_devfreq_add_device(dev, profile, "passive",
497 if (IS_ERR(bus->devfreq)) {
499 "failed to add devfreq dev with passive governor\n");
505 max_state = bus->devfreq->profile->max_state;
506 min_freq = (bus->devfreq->profile->freq_table[0] / 1000);
507 max_freq = (bus->devfreq->profile->freq_table[max_state - 1] / 1000);
508 pr_info("exynos-bus: new bus device registered: %s (%6ld KHz ~ %6ld KHz)\n",
509 dev_name(dev), min_freq, max_freq);
514 dev_pm_opp_of_remove_table(dev);
515 clk_disable_unprepare(bus->clk);
520 #ifdef CONFIG_PM_SLEEP
521 static int exynos_bus_resume(struct device *dev)
523 struct exynos_bus *bus = dev_get_drvdata(dev);
526 ret = exynos_bus_enable_edev(bus);
528 dev_err(dev, "failed to enable the devfreq-event devices\n");
535 static int exynos_bus_suspend(struct device *dev)
537 struct exynos_bus *bus = dev_get_drvdata(dev);
540 ret = exynos_bus_disable_edev(bus);
542 dev_err(dev, "failed to disable the devfreq-event devices\n");
550 static const struct dev_pm_ops exynos_bus_pm = {
551 SET_SYSTEM_SLEEP_PM_OPS(exynos_bus_suspend, exynos_bus_resume)
554 static const struct of_device_id exynos_bus_of_match[] = {
555 { .compatible = "samsung,exynos-bus", },
558 MODULE_DEVICE_TABLE(of, exynos_bus_of_match);
560 static struct platform_driver exynos_bus_platdrv = {
561 .probe = exynos_bus_probe,
563 .name = "exynos-bus",
564 .pm = &exynos_bus_pm,
565 .of_match_table = of_match_ptr(exynos_bus_of_match),
568 module_platform_driver(exynos_bus_platdrv);
570 MODULE_DESCRIPTION("Generic Exynos Bus frequency driver");
571 MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
572 MODULE_LICENSE("GPL v2");