blob: 2c8f41fbe94fb71c00e3471b6b8719b6d49547fc [file] [log] [blame]
Chanwoo Choi07222492015-11-03 19:04:16 +09001/*
2 * Generic Exynos Bus frequency driver with DEVFREQ Framework
3 *
Chanwoo Choi403e0682015-11-05 18:29:27 +09004 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
Chanwoo Choi07222492015-11-03 19:04:16 +09005 * Author : Chanwoo Choi <cw00.choi@samsung.com>
6 *
7 * This driver support Exynos Bus frequency feature by using
8 * DEVFREQ framework and is based on drivers/devfreq/exynos/exynos4_bus.c.
9 *
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.
13 */
14
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>
26
27#define DEFAULT_SATURATION_RATIO 40
28#define DEFAULT_VOLTAGE_TOLERANCE 2
29
30struct exynos_bus {
31 struct device *dev;
32
33 struct devfreq *devfreq;
34 struct devfreq_event_dev **edev;
35 unsigned int edev_count;
36 struct mutex lock;
37
38 struct dev_pm_opp *curr_opp;
39
40 struct regulator *regulator;
41 struct clk *clk;
42 unsigned int voltage_tolerance;
43 unsigned int ratio;
44};
45
46/*
47 * Control the devfreq-event device to get the current state of bus
48 */
49#define exynos_bus_ops_edev(ops) \
50static int exynos_bus_##ops(struct exynos_bus *bus) \
51{ \
52 int i, ret; \
53 \
54 for (i = 0; i < bus->edev_count; i++) { \
55 if (!bus->edev[i]) \
56 continue; \
57 ret = devfreq_event_##ops(bus->edev[i]); \
58 if (ret < 0) \
59 return ret; \
60 } \
61 \
62 return 0; \
63}
64exynos_bus_ops_edev(enable_edev);
65exynos_bus_ops_edev(disable_edev);
66exynos_bus_ops_edev(set_event);
67
68static int exynos_bus_get_event(struct exynos_bus *bus,
69 struct devfreq_event_data *edata)
70{
71 struct devfreq_event_data event_data;
72 unsigned long load_count = 0, total_count = 0;
73 int i, ret = 0;
74
75 for (i = 0; i < bus->edev_count; i++) {
76 if (!bus->edev[i])
77 continue;
78
79 ret = devfreq_event_get_event(bus->edev[i], &event_data);
80 if (ret < 0)
81 return ret;
82
83 if (i == 0 || event_data.load_count > load_count) {
84 load_count = event_data.load_count;
85 total_count = event_data.total_count;
86 }
87 }
88
89 edata->load_count = load_count;
90 edata->total_count = total_count;
91
92 return ret;
93}
94
95/*
Chanwoo Choi403e0682015-11-05 18:29:27 +090096 * Must necessary function for devfreq simple-ondemand governor
Chanwoo Choi07222492015-11-03 19:04:16 +090097 */
98static int exynos_bus_target(struct device *dev, unsigned long *freq, u32 flags)
99{
100 struct exynos_bus *bus = dev_get_drvdata(dev);
101 struct dev_pm_opp *new_opp;
102 unsigned long old_freq, new_freq, old_volt, new_volt, tol;
103 int ret = 0;
104
105 /* Get new opp-bus instance according to new bus clock */
106 rcu_read_lock();
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");
110 rcu_read_unlock();
111 return PTR_ERR(new_opp);
112 }
113
114 new_freq = dev_pm_opp_get_freq(new_opp);
115 new_volt = dev_pm_opp_get_voltage(new_opp);
116 old_freq = dev_pm_opp_get_freq(bus->curr_opp);
117 old_volt = dev_pm_opp_get_voltage(bus->curr_opp);
118 rcu_read_unlock();
119
120 if (old_freq == new_freq)
121 return 0;
122 tol = new_volt * bus->voltage_tolerance / 100;
123
124 /* Change voltage and frequency according to new OPP level */
125 mutex_lock(&bus->lock);
126
127 if (old_freq < new_freq) {
128 ret = regulator_set_voltage_tol(bus->regulator, new_volt, tol);
129 if (ret < 0) {
130 dev_err(bus->dev, "failed to set voltage\n");
131 goto out;
132 }
133 }
134
135 ret = clk_set_rate(bus->clk, new_freq);
136 if (ret < 0) {
137 dev_err(dev, "failed to change clock of bus\n");
138 clk_set_rate(bus->clk, old_freq);
139 goto out;
140 }
141
142 if (old_freq > new_freq) {
143 ret = regulator_set_voltage_tol(bus->regulator, new_volt, tol);
144 if (ret < 0) {
145 dev_err(bus->dev, "failed to set voltage\n");
146 goto out;
147 }
148 }
149 bus->curr_opp = new_opp;
150
151 dev_dbg(dev, "Set the frequency of bus (%lukHz -> %lukHz)\n",
152 old_freq/1000, new_freq/1000);
153out:
154 mutex_unlock(&bus->lock);
155
156 return ret;
157}
158
159static int exynos_bus_get_dev_status(struct device *dev,
160 struct devfreq_dev_status *stat)
161{
162 struct exynos_bus *bus = dev_get_drvdata(dev);
163 struct devfreq_event_data edata;
164 int ret;
165
166 rcu_read_lock();
167 stat->current_frequency = dev_pm_opp_get_freq(bus->curr_opp);
168 rcu_read_unlock();
169
170 ret = exynos_bus_get_event(bus, &edata);
171 if (ret < 0) {
172 stat->total_time = stat->busy_time = 0;
173 goto err;
174 }
175
176 stat->busy_time = (edata.load_count * 100) / bus->ratio;
177 stat->total_time = edata.total_count;
178
179 dev_dbg(dev, "Usage of devfreq-event : %lu/%lu\n", stat->busy_time,
180 stat->total_time);
181
182err:
183 ret = exynos_bus_set_event(bus);
184 if (ret < 0) {
185 dev_err(dev, "failed to set event to devfreq-event devices\n");
186 return ret;
187 }
188
189 return ret;
190}
191
192static void exynos_bus_exit(struct device *dev)
193{
194 struct exynos_bus *bus = dev_get_drvdata(dev);
195 int ret;
196
197 ret = exynos_bus_disable_edev(bus);
198 if (ret < 0)
199 dev_warn(dev, "failed to disable the devfreq-event devices\n");
200
Chanwoo Choi07222492015-11-03 19:04:16 +0900201 dev_pm_opp_of_remove_table(dev);
Chanwoo Choi403e0682015-11-05 18:29:27 +0900202 clk_disable_unprepare(bus->clk);
Kamil Konieczny4ebe3a82019-08-07 15:38:35 +0200203 if (bus->regulator)
204 regulator_disable(bus->regulator);
Chanwoo Choi07222492015-11-03 19:04:16 +0900205}
206
Chanwoo Choi403e0682015-11-05 18:29:27 +0900207/*
208 * Must necessary function for devfreq passive governor
209 */
210static int exynos_bus_passive_target(struct device *dev, unsigned long *freq,
211 u32 flags)
212{
213 struct exynos_bus *bus = dev_get_drvdata(dev);
214 struct dev_pm_opp *new_opp;
215 unsigned long old_freq, new_freq;
216 int ret = 0;
217
218 /* Get new opp-bus instance according to new bus clock */
219 rcu_read_lock();
220 new_opp = devfreq_recommended_opp(dev, freq, flags);
221 if (IS_ERR(new_opp)) {
222 dev_err(dev, "failed to get recommended opp instance\n");
223 rcu_read_unlock();
224 return PTR_ERR(new_opp);
225 }
226
227 new_freq = dev_pm_opp_get_freq(new_opp);
228 old_freq = dev_pm_opp_get_freq(bus->curr_opp);
229 rcu_read_unlock();
230
231 if (old_freq == new_freq)
232 return 0;
233
234 /* Change the frequency according to new OPP level */
235 mutex_lock(&bus->lock);
236
237 ret = clk_set_rate(bus->clk, new_freq);
238 if (ret < 0) {
239 dev_err(dev, "failed to set the clock of bus\n");
240 goto out;
241 }
242
243 *freq = new_freq;
244 bus->curr_opp = new_opp;
245
246 dev_dbg(dev, "Set the frequency of bus (%lukHz -> %lukHz)\n",
247 old_freq/1000, new_freq/1000);
248out:
249 mutex_unlock(&bus->lock);
250
251 return ret;
252}
253
254static void exynos_bus_passive_exit(struct device *dev)
255{
256 struct exynos_bus *bus = dev_get_drvdata(dev);
257
258 dev_pm_opp_of_remove_table(dev);
259 clk_disable_unprepare(bus->clk);
260}
261
262static int exynos_bus_parent_parse_of(struct device_node *np,
263 struct exynos_bus *bus)
Chanwoo Choi07222492015-11-03 19:04:16 +0900264{
265 struct device *dev = bus->dev;
Chanwoo Choi07222492015-11-03 19:04:16 +0900266 int i, ret, count, size;
267
Chanwoo Choi07222492015-11-03 19:04:16 +0900268 /* Get the regulator to provide each bus with the power */
269 bus->regulator = devm_regulator_get(dev, "vdd");
270 if (IS_ERR(bus->regulator)) {
271 dev_err(dev, "failed to get VDD regulator\n");
Chanwoo Choi403e0682015-11-05 18:29:27 +0900272 return PTR_ERR(bus->regulator);
Chanwoo Choi07222492015-11-03 19:04:16 +0900273 }
274
275 ret = regulator_enable(bus->regulator);
276 if (ret < 0) {
277 dev_err(dev, "failed to enable VDD regulator\n");
Chanwoo Choi403e0682015-11-05 18:29:27 +0900278 return ret;
Chanwoo Choi07222492015-11-03 19:04:16 +0900279 }
280
281 /*
282 * Get the devfreq-event devices to get the current utilization of
283 * buses. This raw data will be used in devfreq ondemand governor.
284 */
285 count = devfreq_event_get_edev_count(dev);
286 if (count < 0) {
287 dev_err(dev, "failed to get the count of devfreq-event dev\n");
288 ret = count;
289 goto err_regulator;
290 }
291 bus->edev_count = count;
292
293 size = sizeof(*bus->edev) * count;
294 bus->edev = devm_kzalloc(dev, size, GFP_KERNEL);
295 if (!bus->edev) {
296 ret = -ENOMEM;
297 goto err_regulator;
298 }
299
300 for (i = 0; i < count; i++) {
301 bus->edev[i] = devfreq_event_get_edev_by_phandle(dev, i);
302 if (IS_ERR(bus->edev[i])) {
303 ret = -EPROBE_DEFER;
304 goto err_regulator;
305 }
306 }
307
308 /*
309 * Optionally, Get the saturation ratio according to Exynos SoC
310 * When measuring the utilization of each AXI bus with devfreq-event
311 * devices, the measured real cycle might be much lower than the
312 * total cycle of bus during sampling rate. In result, the devfreq
313 * simple-ondemand governor might not decide to change the current
314 * frequency due to too utilization (= real cycle/total cycle).
315 * So, this property is used to adjust the utilization when calculating
316 * the busy_time in exynos_bus_get_dev_status().
317 */
318 if (of_property_read_u32(np, "exynos,saturation-ratio", &bus->ratio))
319 bus->ratio = DEFAULT_SATURATION_RATIO;
320
321 if (of_property_read_u32(np, "exynos,voltage-tolerance",
322 &bus->voltage_tolerance))
323 bus->voltage_tolerance = DEFAULT_VOLTAGE_TOLERANCE;
324
325 return 0;
326
327err_regulator:
328 regulator_disable(bus->regulator);
Chanwoo Choi403e0682015-11-05 18:29:27 +0900329
330 return ret;
331}
332
333static int exynos_bus_parse_of(struct device_node *np,
334 struct exynos_bus *bus)
335{
336 struct device *dev = bus->dev;
337 unsigned long rate;
338 int ret;
339
340 /* Get the clock to provide each bus with source clock */
341 bus->clk = devm_clk_get(dev, "bus");
342 if (IS_ERR(bus->clk)) {
343 dev_err(dev, "failed to get bus clock\n");
344 return PTR_ERR(bus->clk);
345 }
346
347 ret = clk_prepare_enable(bus->clk);
348 if (ret < 0) {
349 dev_err(dev, "failed to get enable clock\n");
350 return ret;
351 }
352
353 /* Get the freq and voltage from OPP table to scale the bus freq */
354 rcu_read_lock();
355 ret = dev_pm_opp_of_add_table(dev);
356 if (ret < 0) {
357 dev_err(dev, "failed to get OPP table\n");
358 rcu_read_unlock();
359 goto err_clk;
360 }
361
362 rate = clk_get_rate(bus->clk);
363 bus->curr_opp = devfreq_recommended_opp(dev, &rate, 0);
364 if (IS_ERR(bus->curr_opp)) {
365 dev_err(dev, "failed to find dev_pm_opp\n");
366 rcu_read_unlock();
367 ret = PTR_ERR(bus->curr_opp);
368 goto err_opp;
369 }
370 rcu_read_unlock();
371
372 return 0;
373
Chanwoo Choi07222492015-11-03 19:04:16 +0900374err_opp:
375 dev_pm_opp_of_remove_table(dev);
376err_clk:
377 clk_disable_unprepare(bus->clk);
378
379 return ret;
380}
381
382static int exynos_bus_probe(struct platform_device *pdev)
383{
384 struct device *dev = &pdev->dev;
Peter Chend8150d12016-07-01 17:42:01 +0800385 struct device_node *np = dev->of_node, *node;
Chanwoo Choi07222492015-11-03 19:04:16 +0900386 struct devfreq_dev_profile *profile;
387 struct devfreq_simple_ondemand_data *ondemand_data;
Chanwoo Choi403e0682015-11-05 18:29:27 +0900388 struct devfreq_passive_data *passive_data;
389 struct devfreq *parent_devfreq;
Chanwoo Choi07222492015-11-03 19:04:16 +0900390 struct exynos_bus *bus;
Chanwoo Choi403e0682015-11-05 18:29:27 +0900391 int ret, max_state;
392 unsigned long min_freq, max_freq;
Kamil Konieczny4ebe3a82019-08-07 15:38:35 +0200393 bool passive = false;
Chanwoo Choi07222492015-11-03 19:04:16 +0900394
395 if (!np) {
396 dev_err(dev, "failed to find devicetree node\n");
397 return -EINVAL;
398 }
399
400 bus = devm_kzalloc(&pdev->dev, sizeof(*bus), GFP_KERNEL);
401 if (!bus)
402 return -ENOMEM;
403 mutex_init(&bus->lock);
404 bus->dev = &pdev->dev;
405 platform_set_drvdata(pdev, bus);
406
Chanwoo Choi07222492015-11-03 19:04:16 +0900407 profile = devm_kzalloc(dev, sizeof(*profile), GFP_KERNEL);
Kamil Konieczny4ebe3a82019-08-07 15:38:35 +0200408 if (!profile)
409 return -ENOMEM;
Chanwoo Choi403e0682015-11-05 18:29:27 +0900410
Peter Chend8150d12016-07-01 17:42:01 +0800411 node = of_parse_phandle(dev->of_node, "devfreq", 0);
412 if (node) {
413 of_node_put(node);
Kamil Konieczny4ebe3a82019-08-07 15:38:35 +0200414 passive = true;
Peter Chend8150d12016-07-01 17:42:01 +0800415 } else {
Chanwoo Choi403e0682015-11-05 18:29:27 +0900416 ret = exynos_bus_parent_parse_of(np, bus);
Kamil Konieczny4ebe3a82019-08-07 15:38:35 +0200417 if (ret < 0)
418 return ret;
Peter Chend8150d12016-07-01 17:42:01 +0800419 }
Chanwoo Choi403e0682015-11-05 18:29:27 +0900420
Kamil Konieczny4ebe3a82019-08-07 15:38:35 +0200421 /* Parse the device-tree to get the resource information */
422 ret = exynos_bus_parse_of(np, bus);
Chanwoo Choi403e0682015-11-05 18:29:27 +0900423 if (ret < 0)
Kamil Konieczny4ebe3a82019-08-07 15:38:35 +0200424 goto err_reg;
425
426 if (passive)
427 goto passive;
Chanwoo Choi403e0682015-11-05 18:29:27 +0900428
MyungJoo Ham83cb0e42016-04-29 16:13:03 +0900429 /* Initialize the struct profile and governor data for parent device */
Chanwoo Choi07222492015-11-03 19:04:16 +0900430 profile->polling_ms = 50;
431 profile->target = exynos_bus_target;
432 profile->get_dev_status = exynos_bus_get_dev_status;
433 profile->exit = exynos_bus_exit;
434
435 ondemand_data = devm_kzalloc(dev, sizeof(*ondemand_data), GFP_KERNEL);
Chanwoo Choi403e0682015-11-05 18:29:27 +0900436 if (!ondemand_data) {
437 ret = -ENOMEM;
438 goto err;
439 }
Chanwoo Choi07222492015-11-03 19:04:16 +0900440 ondemand_data->upthreshold = 40;
441 ondemand_data->downdifferential = 5;
442
443 /* Add devfreq device to monitor and handle the exynos bus */
444 bus->devfreq = devm_devfreq_add_device(dev, profile, "simple_ondemand",
445 ondemand_data);
446 if (IS_ERR(bus->devfreq)) {
447 dev_err(dev, "failed to add devfreq device\n");
Chanwoo Choi403e0682015-11-05 18:29:27 +0900448 ret = PTR_ERR(bus->devfreq);
449 goto err;
Chanwoo Choi07222492015-11-03 19:04:16 +0900450 }
451
452 /* Register opp_notifier to catch the change of OPP */
453 ret = devm_devfreq_register_opp_notifier(dev, bus->devfreq);
454 if (ret < 0) {
455 dev_err(dev, "failed to register opp notifier\n");
Chanwoo Choi403e0682015-11-05 18:29:27 +0900456 goto err;
Chanwoo Choi07222492015-11-03 19:04:16 +0900457 }
458
459 /*
460 * Enable devfreq-event to get raw data which is used to determine
461 * current bus load.
462 */
463 ret = exynos_bus_enable_edev(bus);
464 if (ret < 0) {
465 dev_err(dev, "failed to enable devfreq-event devices\n");
Chanwoo Choi403e0682015-11-05 18:29:27 +0900466 goto err;
Chanwoo Choi07222492015-11-03 19:04:16 +0900467 }
468
469 ret = exynos_bus_set_event(bus);
470 if (ret < 0) {
471 dev_err(dev, "failed to set event to devfreq-event devices\n");
Chanwoo Choi403e0682015-11-05 18:29:27 +0900472 goto err;
Chanwoo Choi07222492015-11-03 19:04:16 +0900473 }
474
Chanwoo Choi403e0682015-11-05 18:29:27 +0900475 goto out;
476passive:
MyungJoo Ham83cb0e42016-04-29 16:13:03 +0900477 /* Initialize the struct profile and governor data for passive device */
Chanwoo Choi403e0682015-11-05 18:29:27 +0900478 profile->target = exynos_bus_passive_target;
479 profile->exit = exynos_bus_passive_exit;
480
481 /* Get the instance of parent devfreq device */
482 parent_devfreq = devfreq_get_devfreq_by_phandle(dev, 0);
483 if (IS_ERR(parent_devfreq)) {
484 ret = -EPROBE_DEFER;
485 goto err;
486 }
487
488 passive_data = devm_kzalloc(dev, sizeof(*passive_data), GFP_KERNEL);
489 if (!passive_data) {
490 ret = -ENOMEM;
491 goto err;
492 }
493 passive_data->parent = parent_devfreq;
494
495 /* Add devfreq device for exynos bus with passive governor */
496 bus->devfreq = devm_devfreq_add_device(dev, profile, "passive",
497 passive_data);
498 if (IS_ERR(bus->devfreq)) {
499 dev_err(dev,
500 "failed to add devfreq dev with passive governor\n");
Chanwoo Choi79598352016-12-28 20:52:36 +0900501 ret = PTR_ERR(bus->devfreq);
Chanwoo Choi403e0682015-11-05 18:29:27 +0900502 goto err;
503 }
504
505out:
506 max_state = bus->devfreq->profile->max_state;
507 min_freq = (bus->devfreq->profile->freq_table[0] / 1000);
508 max_freq = (bus->devfreq->profile->freq_table[max_state - 1] / 1000);
509 pr_info("exynos-bus: new bus device registered: %s (%6ld KHz ~ %6ld KHz)\n",
510 dev_name(dev), min_freq, max_freq);
511
Chanwoo Choi07222492015-11-03 19:04:16 +0900512 return 0;
Chanwoo Choi403e0682015-11-05 18:29:27 +0900513
514err:
515 dev_pm_opp_of_remove_table(dev);
516 clk_disable_unprepare(bus->clk);
Kamil Konieczny4ebe3a82019-08-07 15:38:35 +0200517err_reg:
518 if (!passive)
519 regulator_disable(bus->regulator);
Chanwoo Choi403e0682015-11-05 18:29:27 +0900520
521 return ret;
Chanwoo Choi07222492015-11-03 19:04:16 +0900522}
523
524#ifdef CONFIG_PM_SLEEP
525static int exynos_bus_resume(struct device *dev)
526{
527 struct exynos_bus *bus = dev_get_drvdata(dev);
528 int ret;
529
530 ret = exynos_bus_enable_edev(bus);
531 if (ret < 0) {
532 dev_err(dev, "failed to enable the devfreq-event devices\n");
533 return ret;
534 }
535
536 return 0;
537}
538
539static int exynos_bus_suspend(struct device *dev)
540{
541 struct exynos_bus *bus = dev_get_drvdata(dev);
542 int ret;
543
544 ret = exynos_bus_disable_edev(bus);
545 if (ret < 0) {
546 dev_err(dev, "failed to disable the devfreq-event devices\n");
547 return ret;
548 }
549
550 return 0;
551}
552#endif
553
554static const struct dev_pm_ops exynos_bus_pm = {
555 SET_SYSTEM_SLEEP_PM_OPS(exynos_bus_suspend, exynos_bus_resume)
556};
557
558static const struct of_device_id exynos_bus_of_match[] = {
559 { .compatible = "samsung,exynos-bus", },
560 { /* sentinel */ },
561};
562MODULE_DEVICE_TABLE(of, exynos_bus_of_match);
563
564static struct platform_driver exynos_bus_platdrv = {
565 .probe = exynos_bus_probe,
566 .driver = {
567 .name = "exynos-bus",
568 .pm = &exynos_bus_pm,
569 .of_match_table = of_match_ptr(exynos_bus_of_match),
570 },
571};
572module_platform_driver(exynos_bus_platdrv);
573
574MODULE_DESCRIPTION("Generic Exynos Bus frequency driver");
575MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
576MODULE_LICENSE("GPL v2");