Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Generic Exynos Bus frequency driver with DEVFREQ Framework
  4 *
  5 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
  6 * Author : Chanwoo Choi <cw00.choi@samsung.com>
  7 *
  8 * This driver support Exynos Bus frequency feature by using
  9 * DEVFREQ framework and is based on drivers/devfreq/exynos/exynos4_bus.c.
 
 
 
 
 10 */
 11
 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.h>
 19#include <linux/pm_opp.h>
 20#include <linux/platform_device.h>
 21#include <linux/regulator/consumer.h>
 
 22
 23#define DEFAULT_SATURATION_RATIO	40
 
 24
 25struct exynos_bus {
 26	struct device *dev;
 27	struct platform_device *icc_pdev;
 28
 29	struct devfreq *devfreq;
 30	struct devfreq_event_dev **edev;
 31	unsigned int edev_count;
 32	struct mutex lock;
 33
 34	unsigned long curr_freq;
 35
 36	int opp_token;
 37	struct clk *clk;
 
 38	unsigned int ratio;
 39};
 40
 41/*
 42 * Control the devfreq-event device to get the current state of bus
 43 */
 44#define exynos_bus_ops_edev(ops)				\
 45static int exynos_bus_##ops(struct exynos_bus *bus)		\
 46{								\
 47	int i, ret;						\
 48								\
 49	for (i = 0; i < bus->edev_count; i++) {			\
 50		if (!bus->edev[i])				\
 51			continue;				\
 52		ret = devfreq_event_##ops(bus->edev[i]);	\
 53		if (ret < 0)					\
 54			return ret;				\
 55	}							\
 56								\
 57	return 0;						\
 58}
 59exynos_bus_ops_edev(enable_edev);
 60exynos_bus_ops_edev(disable_edev);
 61exynos_bus_ops_edev(set_event);
 62
 63static int exynos_bus_get_event(struct exynos_bus *bus,
 64				struct devfreq_event_data *edata)
 65{
 66	struct devfreq_event_data event_data;
 67	unsigned long load_count = 0, total_count = 0;
 68	int i, ret = 0;
 69
 70	for (i = 0; i < bus->edev_count; i++) {
 71		if (!bus->edev[i])
 72			continue;
 73
 74		ret = devfreq_event_get_event(bus->edev[i], &event_data);
 75		if (ret < 0)
 76			return ret;
 77
 78		if (i == 0 || event_data.load_count > load_count) {
 79			load_count = event_data.load_count;
 80			total_count = event_data.total_count;
 81		}
 82	}
 83
 84	edata->load_count = load_count;
 85	edata->total_count = total_count;
 86
 87	return ret;
 88}
 89
 90/*
 91 * devfreq function for both simple-ondemand and passive governor
 92 */
 93static int exynos_bus_target(struct device *dev, unsigned long *freq, u32 flags)
 94{
 95	struct exynos_bus *bus = dev_get_drvdata(dev);
 96	struct dev_pm_opp *new_opp;
 
 97	int ret = 0;
 98
 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);
104	}
105
106	dev_pm_opp_put(new_opp);
 
 
 
 
 
 
 
107
108	/* Change voltage and frequency according to new OPP level */
109	mutex_lock(&bus->lock);
110	ret = dev_pm_opp_set_rate(dev, *freq);
111	if (!ret)
112		bus->curr_freq = *freq;
113
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
114	mutex_unlock(&bus->lock);
115
116	return ret;
117}
118
119static int exynos_bus_get_dev_status(struct device *dev,
120				     struct devfreq_dev_status *stat)
121{
122	struct exynos_bus *bus = dev_get_drvdata(dev);
123	struct devfreq_event_data edata;
124	int ret;
125
126	stat->current_frequency = bus->curr_freq;
127
128	ret = exynos_bus_get_event(bus, &edata);
129	if (ret < 0) {
130		dev_err(dev, "failed to get event from devfreq-event devices\n");
131		stat->total_time = stat->busy_time = 0;
132		goto err;
133	}
134
135	stat->busy_time = (edata.load_count * 100) / bus->ratio;
136	stat->total_time = edata.total_count;
137
138	dev_dbg(dev, "Usage of devfreq-event : %lu/%lu\n", stat->busy_time,
139							stat->total_time);
140
141err:
142	ret = exynos_bus_set_event(bus);
143	if (ret < 0) {
144		dev_err(dev, "failed to set event to devfreq-event devices\n");
145		return ret;
146	}
147
148	return ret;
149}
150
151static void exynos_bus_exit(struct device *dev)
152{
153	struct exynos_bus *bus = dev_get_drvdata(dev);
154	int ret;
155
156	ret = exynos_bus_disable_edev(bus);
157	if (ret < 0)
158		dev_warn(dev, "failed to disable the devfreq-event devices\n");
159
160	platform_device_unregister(bus->icc_pdev);
 
161
162	dev_pm_opp_of_remove_table(dev);
163	clk_disable_unprepare(bus->clk);
164	dev_pm_opp_put_regulators(bus->opp_token);
165}
166
167static void exynos_bus_passive_exit(struct device *dev)
 
 
 
 
168{
169	struct exynos_bus *bus = dev_get_drvdata(dev);
 
 
 
 
 
 
 
 
 
 
 
 
170
171	platform_device_unregister(bus->icc_pdev);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
172
173	dev_pm_opp_of_remove_table(dev);
174	clk_disable_unprepare(bus->clk);
175}
176
177static int exynos_bus_parent_parse_of(struct device_node *np,
178					struct exynos_bus *bus)
179{
180	struct device *dev = bus->dev;
181	const char *supplies[] = { "vdd", NULL };
182	int i, ret, count, size;
183
184	ret = dev_pm_opp_set_regulators(dev, supplies);
 
 
 
 
 
 
 
185	if (ret < 0) {
186		dev_err(dev, "failed to set regulators %d\n", ret);
187		return ret;
188	}
189
190	bus->opp_token = ret;
191
192	/*
193	 * Get the devfreq-event devices to get the current utilization of
194	 * buses. This raw data will be used in devfreq ondemand governor.
195	 */
196	count = devfreq_event_get_edev_count(dev, "devfreq-events");
197	if (count < 0) {
198		dev_err(dev, "failed to get the count of devfreq-event dev\n");
199		ret = count;
200		goto err_regulator;
201	}
202	bus->edev_count = count;
203
204	size = sizeof(*bus->edev) * count;
205	bus->edev = devm_kzalloc(dev, size, GFP_KERNEL);
206	if (!bus->edev) {
207		ret = -ENOMEM;
208		goto err_regulator;
209	}
210
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])) {
215			ret = -EPROBE_DEFER;
216			goto err_regulator;
217		}
218	}
219
220	/*
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().
229	 */
230	if (of_property_read_u32(np, "exynos,saturation-ratio", &bus->ratio))
231		bus->ratio = DEFAULT_SATURATION_RATIO;
232
 
 
 
 
233	return 0;
234
235err_regulator:
236	dev_pm_opp_put_regulators(bus->opp_token);
237
238	return ret;
239}
240
241static int exynos_bus_parse_of(struct device_node *np,
242			      struct exynos_bus *bus)
243{
244	struct device *dev = bus->dev;
245	struct dev_pm_opp *opp;
246	unsigned long rate;
247	int ret;
248
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);
254	}
255
256	ret = clk_prepare_enable(bus->clk);
257	if (ret < 0) {
258		dev_err(dev, "failed to get enable clock\n");
259		return ret;
260	}
261
262	/* Get the freq and voltage from OPP table to scale the bus freq */
263	ret = dev_pm_opp_of_add_table(dev);
264	if (ret < 0) {
265		dev_err(dev, "failed to get OPP table\n");
266		goto err_clk;
267	}
268
269	rate = clk_get_rate(bus->clk);
270
 
271	opp = devfreq_recommended_opp(dev, &rate, 0);
272	if (IS_ERR(opp)) {
273		dev_err(dev, "failed to find dev_pm_opp\n");
 
274		ret = PTR_ERR(opp);
275		goto err_opp;
276	}
277	bus->curr_freq = dev_pm_opp_get_freq(opp);
278	dev_pm_opp_put(opp);
279
280	return 0;
281
282err_opp:
283	dev_pm_opp_of_remove_table(dev);
284err_clk:
285	clk_disable_unprepare(bus->clk);
286
287	return ret;
288}
289
290static int exynos_bus_profile_init(struct exynos_bus *bus,
291				   struct devfreq_dev_profile *profile)
292{
293	struct device *dev = bus->dev;
 
 
294	struct devfreq_simple_ondemand_data *ondemand_data;
295	int ret;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
296
297	/* Initialize the struct profile and governor data for parent device */
298	profile->polling_ms = 50;
299	profile->target = exynos_bus_target;
300	profile->get_dev_status = exynos_bus_get_dev_status;
301	profile->exit = exynos_bus_exit;
302
303	ondemand_data = devm_kzalloc(dev, sizeof(*ondemand_data), GFP_KERNEL);
304	if (!ondemand_data)
305		return -ENOMEM;
306
 
307	ondemand_data->upthreshold = 40;
308	ondemand_data->downdifferential = 5;
309
310	/* Add devfreq device to monitor and handle the exynos bus */
311	bus->devfreq = devm_devfreq_add_device(dev, profile,
312						DEVFREQ_GOV_SIMPLE_ONDEMAND,
313						ondemand_data);
314	if (IS_ERR(bus->devfreq)) {
315		dev_err(dev, "failed to add devfreq device\n");
316		return PTR_ERR(bus->devfreq);
 
317	}
318
319	/* Register opp_notifier to catch the change of OPP  */
320	ret = devm_devfreq_register_opp_notifier(dev, bus->devfreq);
321	if (ret < 0) {
322		dev_err(dev, "failed to register opp notifier\n");
323		return ret;
324	}
325
326	/*
327	 * Enable devfreq-event to get raw data which is used to determine
328	 * current bus load.
329	 */
330	ret = exynos_bus_enable_edev(bus);
331	if (ret < 0) {
332		dev_err(dev, "failed to enable devfreq-event devices\n");
333		return ret;
334	}
335
336	ret = exynos_bus_set_event(bus);
337	if (ret < 0) {
338		dev_err(dev, "failed to set event to devfreq-event devices\n");
339		goto err_edev;
340	}
341
342	return 0;
343
344err_edev:
345	if (exynos_bus_disable_edev(bus))
346		dev_warn(dev, "failed to disable the devfreq-event devices\n");
347
348	return ret;
349}
350
351static int exynos_bus_profile_init_passive(struct exynos_bus *bus,
352					   struct devfreq_dev_profile *profile)
353{
354	struct device *dev = bus->dev;
355	struct devfreq_passive_data *passive_data;
356	struct devfreq *parent_devfreq;
357
358	/* Initialize the struct profile and governor data for passive device */
359	profile->target = exynos_bus_target;
360	profile->exit = exynos_bus_passive_exit;
361
362	/* Get the instance of parent devfreq device */
363	parent_devfreq = devfreq_get_devfreq_by_phandle(dev, "devfreq", 0);
364	if (IS_ERR(parent_devfreq))
365		return -EPROBE_DEFER;
 
 
366
367	passive_data = devm_kzalloc(dev, sizeof(*passive_data), GFP_KERNEL);
368	if (!passive_data)
369		return -ENOMEM;
370
 
371	passive_data->parent = parent_devfreq;
372
373	/* Add devfreq device for exynos bus with passive governor */
374	bus->devfreq = devm_devfreq_add_device(dev, profile, DEVFREQ_GOV_PASSIVE,
375						passive_data);
376	if (IS_ERR(bus->devfreq)) {
377		dev_err(dev,
378			"failed to add devfreq dev with passive governor\n");
379		return PTR_ERR(bus->devfreq);
380	}
381
382	return 0;
383}
384
385static int exynos_bus_probe(struct platform_device *pdev)
386{
387	struct device *dev = &pdev->dev;
388	struct device_node *np = dev->of_node, *node;
389	struct devfreq_dev_profile *profile;
390	struct exynos_bus *bus;
391	int ret, max_state;
392	unsigned long min_freq, max_freq;
393	bool passive = false;
394
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
407	profile = devm_kzalloc(dev, sizeof(*profile), GFP_KERNEL);
408	if (!profile)
409		return -ENOMEM;
410
411	node = of_parse_phandle(dev->of_node, "devfreq", 0);
412	if (node) {
413		of_node_put(node);
414		passive = true;
415	} else {
416		ret = exynos_bus_parent_parse_of(np, bus);
417		if (ret < 0)
418			return ret;
419	}
420
421	/* Parse the device-tree to get the resource information */
422	ret = exynos_bus_parse_of(np, bus);
423	if (ret < 0)
424		goto err_reg;
425
426	if (passive)
427		ret = exynos_bus_profile_init_passive(bus, profile);
428	else
429		ret = exynos_bus_profile_init(bus, profile);
430
431	if (ret < 0)
432		goto err;
433
434	/* Create child platform device for the interconnect provider */
435	if (of_property_present(dev->of_node, "#interconnect-cells")) {
436		bus->icc_pdev = platform_device_register_data(
437						dev, "exynos-generic-icc",
438						PLATFORM_DEVID_AUTO, NULL, 0);
439
440		if (IS_ERR(bus->icc_pdev)) {
441			ret = PTR_ERR(bus->icc_pdev);
442			goto err;
443		}
444	}
445
446	max_state = bus->devfreq->max_state;
447	min_freq = (bus->devfreq->freq_table[0] / 1000);
448	max_freq = (bus->devfreq->freq_table[max_state - 1] / 1000);
 
449	pr_info("exynos-bus: new bus device registered: %s (%6ld KHz ~ %6ld KHz)\n",
450			dev_name(dev), min_freq, max_freq);
451
452	return 0;
453
454err:
455	dev_pm_opp_of_remove_table(dev);
456	clk_disable_unprepare(bus->clk);
457err_reg:
458	dev_pm_opp_put_regulators(bus->opp_token);
459
460	return ret;
461}
462
463static void exynos_bus_shutdown(struct platform_device *pdev)
464{
465	struct exynos_bus *bus = dev_get_drvdata(&pdev->dev);
466
467	devfreq_suspend_device(bus->devfreq);
468}
469
470#ifdef CONFIG_PM_SLEEP
471static int exynos_bus_resume(struct device *dev)
472{
473	struct exynos_bus *bus = dev_get_drvdata(dev);
474	int ret;
475
476	ret = exynos_bus_enable_edev(bus);
477	if (ret < 0) {
478		dev_err(dev, "failed to enable the devfreq-event devices\n");
479		return ret;
480	}
481
482	return 0;
483}
484
485static int exynos_bus_suspend(struct device *dev)
486{
487	struct exynos_bus *bus = dev_get_drvdata(dev);
488	int ret;
489
490	ret = exynos_bus_disable_edev(bus);
491	if (ret < 0) {
492		dev_err(dev, "failed to disable the devfreq-event devices\n");
493		return ret;
494	}
495
496	return 0;
497}
498#endif
499
500static const struct dev_pm_ops exynos_bus_pm = {
501	SET_SYSTEM_SLEEP_PM_OPS(exynos_bus_suspend, exynos_bus_resume)
502};
503
504static const struct of_device_id exynos_bus_of_match[] = {
505	{ .compatible = "samsung,exynos-bus", },
506	{ /* sentinel */ },
507};
508MODULE_DEVICE_TABLE(of, exynos_bus_of_match);
509
510static struct platform_driver exynos_bus_platdrv = {
511	.probe		= exynos_bus_probe,
512	.shutdown	= exynos_bus_shutdown,
513	.driver = {
514		.name	= "exynos-bus",
515		.pm	= &exynos_bus_pm,
516		.of_match_table = exynos_bus_of_match,
517	},
518};
519module_platform_driver(exynos_bus_platdrv);
520
521MODULE_SOFTDEP("pre: exynos_ppmu");
522MODULE_DESCRIPTION("Generic Exynos Bus frequency driver");
523MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
524MODULE_LICENSE("GPL v2");
v4.10.11
 
  1/*
  2 * Generic Exynos Bus frequency driver with DEVFREQ Framework
  3 *
  4 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
  5 * 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	unsigned long curr_freq;
 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/*
 96 * Must necessary function for devfreq simple-ondemand governor
 97 */
 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, 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 = bus->curr_freq;
117	rcu_read_unlock();
118
119	if (old_freq == new_freq)
120		return 0;
121	tol = new_volt * bus->voltage_tolerance / 100;
122
123	/* Change voltage and frequency according to new OPP level */
124	mutex_lock(&bus->lock);
 
 
 
125
126	if (old_freq < new_freq) {
127		ret = regulator_set_voltage_tol(bus->regulator, new_volt, tol);
128		if (ret < 0) {
129			dev_err(bus->dev, "failed to set voltage\n");
130			goto out;
131		}
132	}
133
134	ret = clk_set_rate(bus->clk, new_freq);
135	if (ret < 0) {
136		dev_err(dev, "failed to change clock of bus\n");
137		clk_set_rate(bus->clk, old_freq);
138		goto out;
139	}
140
141	if (old_freq > new_freq) {
142		ret = regulator_set_voltage_tol(bus->regulator, new_volt, tol);
143		if (ret < 0) {
144			dev_err(bus->dev, "failed to set voltage\n");
145			goto out;
146		}
147	}
148	bus->curr_freq = new_freq;
149
150	dev_dbg(dev, "Set the frequency of bus (%lukHz -> %lukHz)\n",
151			old_freq/1000, new_freq/1000);
152out:
153	mutex_unlock(&bus->lock);
154
155	return ret;
156}
157
158static int exynos_bus_get_dev_status(struct device *dev,
159				     struct devfreq_dev_status *stat)
160{
161	struct exynos_bus *bus = dev_get_drvdata(dev);
162	struct devfreq_event_data edata;
163	int ret;
164
165	stat->current_frequency = bus->curr_freq;
166
167	ret = exynos_bus_get_event(bus, &edata);
168	if (ret < 0) {
 
169		stat->total_time = stat->busy_time = 0;
170		goto err;
171	}
172
173	stat->busy_time = (edata.load_count * 100) / bus->ratio;
174	stat->total_time = edata.total_count;
175
176	dev_dbg(dev, "Usage of devfreq-event : %lu/%lu\n", stat->busy_time,
177							stat->total_time);
178
179err:
180	ret = exynos_bus_set_event(bus);
181	if (ret < 0) {
182		dev_err(dev, "failed to set event to devfreq-event devices\n");
183		return ret;
184	}
185
186	return ret;
187}
188
189static void exynos_bus_exit(struct device *dev)
190{
191	struct exynos_bus *bus = dev_get_drvdata(dev);
192	int ret;
193
194	ret = exynos_bus_disable_edev(bus);
195	if (ret < 0)
196		dev_warn(dev, "failed to disable the devfreq-event devices\n");
197
198	if (bus->regulator)
199		regulator_disable(bus->regulator);
200
201	dev_pm_opp_of_remove_table(dev);
202	clk_disable_unprepare(bus->clk);
 
203}
204
205/*
206 * Must necessary function for devfreq passive governor
207 */
208static int exynos_bus_passive_target(struct device *dev, unsigned long *freq,
209					u32 flags)
210{
211	struct exynos_bus *bus = dev_get_drvdata(dev);
212	struct dev_pm_opp *new_opp;
213	unsigned long old_freq, new_freq;
214	int ret = 0;
215
216	/* Get new opp-bus instance according to new bus clock */
217	rcu_read_lock();
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");
221		rcu_read_unlock();
222		return PTR_ERR(new_opp);
223	}
224
225	new_freq = dev_pm_opp_get_freq(new_opp);
226	old_freq = bus->curr_freq;
227	rcu_read_unlock();
228
229	if (old_freq == new_freq)
230		return 0;
231
232	/* Change the frequency according to new OPP level */
233	mutex_lock(&bus->lock);
234
235	ret = clk_set_rate(bus->clk, new_freq);
236	if (ret < 0) {
237		dev_err(dev, "failed to set the clock of bus\n");
238		goto out;
239	}
240
241	*freq = new_freq;
242	bus->curr_freq = new_freq;
243
244	dev_dbg(dev, "Set the frequency of bus (%lukHz -> %lukHz)\n",
245			old_freq/1000, new_freq/1000);
246out:
247	mutex_unlock(&bus->lock);
248
249	return ret;
250}
251
252static void exynos_bus_passive_exit(struct device *dev)
253{
254	struct exynos_bus *bus = dev_get_drvdata(dev);
255
256	dev_pm_opp_of_remove_table(dev);
257	clk_disable_unprepare(bus->clk);
258}
259
260static int exynos_bus_parent_parse_of(struct device_node *np,
261					struct exynos_bus *bus)
262{
263	struct device *dev = bus->dev;
 
264	int i, ret, count, size;
265
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);
271	}
272
273	ret = regulator_enable(bus->regulator);
274	if (ret < 0) {
275		dev_err(dev, "failed to enable VDD regulator\n");
276		return ret;
277	}
278
 
 
279	/*
280	 * Get the devfreq-event devices to get the current utilization of
281	 * buses. This raw data will be used in devfreq ondemand governor.
282	 */
283	count = devfreq_event_get_edev_count(dev);
284	if (count < 0) {
285		dev_err(dev, "failed to get the count of devfreq-event dev\n");
286		ret = count;
287		goto err_regulator;
288	}
289	bus->edev_count = count;
290
291	size = sizeof(*bus->edev) * count;
292	bus->edev = devm_kzalloc(dev, size, GFP_KERNEL);
293	if (!bus->edev) {
294		ret = -ENOMEM;
295		goto err_regulator;
296	}
297
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])) {
301			ret = -EPROBE_DEFER;
302			goto err_regulator;
303		}
304	}
305
306	/*
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().
315	 */
316	if (of_property_read_u32(np, "exynos,saturation-ratio", &bus->ratio))
317		bus->ratio = DEFAULT_SATURATION_RATIO;
318
319	if (of_property_read_u32(np, "exynos,voltage-tolerance",
320					&bus->voltage_tolerance))
321		bus->voltage_tolerance = DEFAULT_VOLTAGE_TOLERANCE;
322
323	return 0;
324
325err_regulator:
326	regulator_disable(bus->regulator);
327
328	return ret;
329}
330
331static int exynos_bus_parse_of(struct device_node *np,
332			      struct exynos_bus *bus)
333{
334	struct device *dev = bus->dev;
335	struct dev_pm_opp *opp;
336	unsigned long rate;
337	int ret;
338
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);
344	}
345
346	ret = clk_prepare_enable(bus->clk);
347	if (ret < 0) {
348		dev_err(dev, "failed to get enable clock\n");
349		return ret;
350	}
351
352	/* Get the freq and voltage from OPP table to scale the bus freq */
353	ret = dev_pm_opp_of_add_table(dev);
354	if (ret < 0) {
355		dev_err(dev, "failed to get OPP table\n");
356		goto err_clk;
357	}
358
359	rate = clk_get_rate(bus->clk);
360
361	rcu_read_lock();
362	opp = devfreq_recommended_opp(dev, &rate, 0);
363	if (IS_ERR(opp)) {
364		dev_err(dev, "failed to find dev_pm_opp\n");
365		rcu_read_unlock();
366		ret = PTR_ERR(opp);
367		goto err_opp;
368	}
369	bus->curr_freq = dev_pm_opp_get_freq(opp);
370	rcu_read_unlock();
371
372	return 0;
373
374err_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;
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;
391	int ret, max_state;
392	unsigned long min_freq, max_freq;
393
394	if (!np) {
395		dev_err(dev, "failed to find devicetree node\n");
396		return -EINVAL;
397	}
398
399	bus = devm_kzalloc(&pdev->dev, sizeof(*bus), GFP_KERNEL);
400	if (!bus)
401		return -ENOMEM;
402	mutex_init(&bus->lock);
403	bus->dev = &pdev->dev;
404	platform_set_drvdata(pdev, bus);
405
406	/* Parse the device-tree to get the resource information */
407	ret = exynos_bus_parse_of(np, bus);
408	if (ret < 0)
409		return ret;
410
411	profile = devm_kzalloc(dev, sizeof(*profile), GFP_KERNEL);
412	if (!profile) {
413		ret = -ENOMEM;
414		goto err;
415	}
416
417	node = of_parse_phandle(dev->of_node, "devfreq", 0);
418	if (node) {
419		of_node_put(node);
420		goto passive;
421	} else {
422		ret = exynos_bus_parent_parse_of(np, bus);
423	}
424
425	if (ret < 0)
426		goto err;
427
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;
433
434	ondemand_data = devm_kzalloc(dev, sizeof(*ondemand_data), GFP_KERNEL);
435	if (!ondemand_data) {
436		ret = -ENOMEM;
437		goto err;
438	}
439	ondemand_data->upthreshold = 40;
440	ondemand_data->downdifferential = 5;
441
442	/* Add devfreq device to monitor and handle the exynos bus */
443	bus->devfreq = devm_devfreq_add_device(dev, profile, "simple_ondemand",
 
444						ondemand_data);
445	if (IS_ERR(bus->devfreq)) {
446		dev_err(dev, "failed to add devfreq device\n");
447		ret = PTR_ERR(bus->devfreq);
448		goto err;
449	}
450
451	/* Register opp_notifier to catch the change of OPP  */
452	ret = devm_devfreq_register_opp_notifier(dev, bus->devfreq);
453	if (ret < 0) {
454		dev_err(dev, "failed to register opp notifier\n");
455		goto err;
456	}
457
458	/*
459	 * Enable devfreq-event to get raw data which is used to determine
460	 * current bus load.
461	 */
462	ret = exynos_bus_enable_edev(bus);
463	if (ret < 0) {
464		dev_err(dev, "failed to enable devfreq-event devices\n");
465		goto err;
466	}
467
468	ret = exynos_bus_set_event(bus);
469	if (ret < 0) {
470		dev_err(dev, "failed to set event to devfreq-event devices\n");
471		goto err;
472	}
473
474	goto out;
475passive:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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;
479
480	/* Get the instance of parent devfreq device */
481	parent_devfreq = devfreq_get_devfreq_by_phandle(dev, 0);
482	if (IS_ERR(parent_devfreq)) {
483		ret = -EPROBE_DEFER;
484		goto err;
485	}
486
487	passive_data = devm_kzalloc(dev, sizeof(*passive_data), GFP_KERNEL);
488	if (!passive_data) {
489		ret = -ENOMEM;
490		goto err;
491	}
492	passive_data->parent = parent_devfreq;
493
494	/* Add devfreq device for exynos bus with passive governor */
495	bus->devfreq = devm_devfreq_add_device(dev, profile, "passive",
496						passive_data);
497	if (IS_ERR(bus->devfreq)) {
498		dev_err(dev,
499			"failed to add devfreq dev with passive governor\n");
500		ret = PTR_ERR(bus->devfreq);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
501		goto err;
 
 
 
 
 
 
 
 
 
 
 
502	}
503
504out:
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);
510
511	return 0;
512
513err:
514	dev_pm_opp_of_remove_table(dev);
515	clk_disable_unprepare(bus->clk);
 
 
516
517	return ret;
518}
519
 
 
 
 
 
 
 
520#ifdef CONFIG_PM_SLEEP
521static int exynos_bus_resume(struct device *dev)
522{
523	struct exynos_bus *bus = dev_get_drvdata(dev);
524	int ret;
525
526	ret = exynos_bus_enable_edev(bus);
527	if (ret < 0) {
528		dev_err(dev, "failed to enable the devfreq-event devices\n");
529		return ret;
530	}
531
532	return 0;
533}
534
535static int exynos_bus_suspend(struct device *dev)
536{
537	struct exynos_bus *bus = dev_get_drvdata(dev);
538	int ret;
539
540	ret = exynos_bus_disable_edev(bus);
541	if (ret < 0) {
542		dev_err(dev, "failed to disable the devfreq-event devices\n");
543		return ret;
544	}
545
546	return 0;
547}
548#endif
549
550static const struct dev_pm_ops exynos_bus_pm = {
551	SET_SYSTEM_SLEEP_PM_OPS(exynos_bus_suspend, exynos_bus_resume)
552};
553
554static const struct of_device_id exynos_bus_of_match[] = {
555	{ .compatible = "samsung,exynos-bus", },
556	{ /* sentinel */ },
557};
558MODULE_DEVICE_TABLE(of, exynos_bus_of_match);
559
560static struct platform_driver exynos_bus_platdrv = {
561	.probe		= exynos_bus_probe,
 
562	.driver = {
563		.name	= "exynos-bus",
564		.pm	= &exynos_bus_pm,
565		.of_match_table = of_match_ptr(exynos_bus_of_match),
566	},
567};
568module_platform_driver(exynos_bus_platdrv);
569
 
570MODULE_DESCRIPTION("Generic Exynos Bus frequency driver");
571MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
572MODULE_LICENSE("GPL v2");