Merge tag 'for-linus-20190706' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / devfreq / exynos-bus.c
blobd9f377912c1044adb81015315f8f7dd63f07f7d1
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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
27 struct exynos_bus {
28 struct device *dev;
30 struct devfreq *devfreq;
31 struct devfreq_event_dev **edev;
32 unsigned int edev_count;
33 struct mutex lock;
35 unsigned long curr_freq;
37 struct regulator *regulator;
38 struct clk *clk;
39 unsigned int voltage_tolerance;
40 unsigned int ratio;
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) \
48 { \
49 int i, ret; \
51 for (i = 0; i < bus->edev_count; i++) { \
52 if (!bus->edev[i]) \
53 continue; \
54 ret = devfreq_event_##ops(bus->edev[i]); \
55 if (ret < 0) \
56 return ret; \
57 } \
59 return 0; \
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;
70 int i, ret = 0;
72 for (i = 0; i < bus->edev_count; i++) {
73 if (!bus->edev[i])
74 continue;
76 ret = devfreq_event_get_event(bus->edev[i], &event_data);
77 if (ret < 0)
78 return ret;
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;
89 return ret;
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;
100 int ret = 0;
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)
116 return 0;
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);
124 if (ret < 0) {
125 dev_err(bus->dev, "failed to set voltage\n");
126 goto out;
130 ret = clk_set_rate(bus->clk, new_freq);
131 if (ret < 0) {
132 dev_err(dev, "failed to change clock of bus\n");
133 clk_set_rate(bus->clk, old_freq);
134 goto out;
137 if (old_freq > new_freq) {
138 ret = regulator_set_voltage_tol(bus->regulator, new_volt, tol);
139 if (ret < 0) {
140 dev_err(bus->dev, "failed to set voltage\n");
141 goto out;
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));
148 out:
149 mutex_unlock(&bus->lock);
151 return ret;
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;
159 int ret;
161 stat->current_frequency = bus->curr_freq;
163 ret = exynos_bus_get_event(bus, &edata);
164 if (ret < 0) {
165 stat->total_time = stat->busy_time = 0;
166 goto err;
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,
173 stat->total_time);
175 err:
176 ret = exynos_bus_set_event(bus);
177 if (ret < 0) {
178 dev_err(dev, "failed to set event to devfreq-event devices\n");
179 return ret;
182 return ret;
185 static void exynos_bus_exit(struct device *dev)
187 struct exynos_bus *bus = dev_get_drvdata(dev);
188 int ret;
190 ret = exynos_bus_disable_edev(bus);
191 if (ret < 0)
192 dev_warn(dev, "failed to disable the devfreq-event devices\n");
194 if (bus->regulator)
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,
205 u32 flags)
207 struct exynos_bus *bus = dev_get_drvdata(dev);
208 struct dev_pm_opp *new_opp;
209 unsigned long old_freq, new_freq;
210 int ret = 0;
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)
225 return 0;
227 /* Change the frequency according to new OPP level */
228 mutex_lock(&bus->lock);
230 ret = clk_set_rate(bus->clk, new_freq);
231 if (ret < 0) {
232 dev_err(dev, "failed to set the clock of bus\n");
233 goto out;
236 *freq = new_freq;
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));
241 out:
242 mutex_unlock(&bus->lock);
244 return ret;
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);
269 if (ret < 0) {
270 dev_err(dev, "failed to enable VDD regulator\n");
271 return ret;
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);
279 if (count < 0) {
280 dev_err(dev, "failed to get the count of devfreq-event dev\n");
281 ret = count;
282 goto err_regulator;
284 bus->edev_count = count;
286 size = sizeof(*bus->edev) * count;
287 bus->edev = devm_kzalloc(dev, size, GFP_KERNEL);
288 if (!bus->edev) {
289 ret = -ENOMEM;
290 goto err_regulator;
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])) {
296 ret = -EPROBE_DEFER;
297 goto err_regulator;
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;
318 return 0;
320 err_regulator:
321 regulator_disable(bus->regulator);
323 return ret;
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;
331 unsigned long rate;
332 int ret;
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);
342 if (ret < 0) {
343 dev_err(dev, "failed to get enable clock\n");
344 return ret;
347 /* Get the freq and voltage from OPP table to scale the bus freq */
348 ret = dev_pm_opp_of_add_table(dev);
349 if (ret < 0) {
350 dev_err(dev, "failed to get OPP table\n");
351 goto err_clk;
354 rate = clk_get_rate(bus->clk);
356 opp = devfreq_recommended_opp(dev, &rate, 0);
357 if (IS_ERR(opp)) {
358 dev_err(dev, "failed to find dev_pm_opp\n");
359 ret = PTR_ERR(opp);
360 goto err_opp;
362 bus->curr_freq = dev_pm_opp_get_freq(opp);
363 dev_pm_opp_put(opp);
365 return 0;
367 err_opp:
368 dev_pm_opp_of_remove_table(dev);
369 err_clk:
370 clk_disable_unprepare(bus->clk);
372 return ret;
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;
384 int ret, max_state;
385 unsigned long min_freq, max_freq;
387 if (!np) {
388 dev_err(dev, "failed to find devicetree node\n");
389 return -EINVAL;
392 bus = devm_kzalloc(&pdev->dev, sizeof(*bus), GFP_KERNEL);
393 if (!bus)
394 return -ENOMEM;
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);
401 if (ret < 0)
402 return ret;
404 profile = devm_kzalloc(dev, sizeof(*profile), GFP_KERNEL);
405 if (!profile) {
406 ret = -ENOMEM;
407 goto err;
410 node = of_parse_phandle(dev->of_node, "devfreq", 0);
411 if (node) {
412 of_node_put(node);
413 goto passive;
414 } else {
415 ret = exynos_bus_parent_parse_of(np, bus);
418 if (ret < 0)
419 goto err;
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) {
429 ret = -ENOMEM;
430 goto err;
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,
438 ondemand_data);
439 if (IS_ERR(bus->devfreq)) {
440 dev_err(dev, "failed to add devfreq device\n");
441 ret = PTR_ERR(bus->devfreq);
442 goto err;
445 /* Register opp_notifier to catch the change of OPP */
446 ret = devm_devfreq_register_opp_notifier(dev, bus->devfreq);
447 if (ret < 0) {
448 dev_err(dev, "failed to register opp notifier\n");
449 goto err;
453 * Enable devfreq-event to get raw data which is used to determine
454 * current bus load.
456 ret = exynos_bus_enable_edev(bus);
457 if (ret < 0) {
458 dev_err(dev, "failed to enable devfreq-event devices\n");
459 goto err;
462 ret = exynos_bus_set_event(bus);
463 if (ret < 0) {
464 dev_err(dev, "failed to set event to devfreq-event devices\n");
465 goto err;
468 goto out;
469 passive:
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)) {
477 ret = -EPROBE_DEFER;
478 goto err;
481 passive_data = devm_kzalloc(dev, sizeof(*passive_data), GFP_KERNEL);
482 if (!passive_data) {
483 ret = -ENOMEM;
484 goto err;
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,
490 passive_data);
491 if (IS_ERR(bus->devfreq)) {
492 dev_err(dev,
493 "failed to add devfreq dev with passive governor\n");
494 ret = PTR_ERR(bus->devfreq);
495 goto err;
498 out:
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);
505 return 0;
507 err:
508 dev_pm_opp_of_remove_table(dev);
509 clk_disable_unprepare(bus->clk);
511 return ret;
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);
525 int ret;
527 ret = exynos_bus_enable_edev(bus);
528 if (ret < 0) {
529 dev_err(dev, "failed to enable the devfreq-event devices\n");
530 return ret;
533 return 0;
536 static int exynos_bus_suspend(struct device *dev)
538 struct exynos_bus *bus = dev_get_drvdata(dev);
539 int ret;
541 ret = exynos_bus_disable_edev(bus);
542 if (ret < 0) {
543 dev_err(dev, "failed to disable the devfreq-event devices\n");
544 return ret;
547 return 0;
549 #endif
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", },
557 { /* sentinel */ },
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,
564 .driver = {
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");