18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Generic Exynos Bus frequency driver with DEVFREQ Framework
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2016 Samsung Electronics Co., Ltd.
68c2ecf20Sopenharmony_ci * Author : Chanwoo Choi <cw00.choi@samsung.com>
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * This driver support Exynos Bus frequency feature by using
98c2ecf20Sopenharmony_ci * DEVFREQ framework and is based on drivers/devfreq/exynos/exynos4_bus.c.
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include <linux/clk.h>
138c2ecf20Sopenharmony_ci#include <linux/devfreq.h>
148c2ecf20Sopenharmony_ci#include <linux/devfreq-event.h>
158c2ecf20Sopenharmony_ci#include <linux/device.h>
168c2ecf20Sopenharmony_ci#include <linux/export.h>
178c2ecf20Sopenharmony_ci#include <linux/module.h>
188c2ecf20Sopenharmony_ci#include <linux/of.h>
198c2ecf20Sopenharmony_ci#include <linux/pm_opp.h>
208c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
218c2ecf20Sopenharmony_ci#include <linux/regulator/consumer.h>
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#define DEFAULT_SATURATION_RATIO	40
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_cistruct exynos_bus {
268c2ecf20Sopenharmony_ci	struct device *dev;
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci	struct devfreq *devfreq;
298c2ecf20Sopenharmony_ci	struct devfreq_event_dev **edev;
308c2ecf20Sopenharmony_ci	unsigned int edev_count;
318c2ecf20Sopenharmony_ci	struct mutex lock;
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci	unsigned long curr_freq;
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci	struct opp_table *opp_table;
368c2ecf20Sopenharmony_ci	struct clk *clk;
378c2ecf20Sopenharmony_ci	unsigned int ratio;
388c2ecf20Sopenharmony_ci};
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci/*
418c2ecf20Sopenharmony_ci * Control the devfreq-event device to get the current state of bus
428c2ecf20Sopenharmony_ci */
438c2ecf20Sopenharmony_ci#define exynos_bus_ops_edev(ops)				\
448c2ecf20Sopenharmony_cistatic int exynos_bus_##ops(struct exynos_bus *bus)		\
458c2ecf20Sopenharmony_ci{								\
468c2ecf20Sopenharmony_ci	int i, ret;						\
478c2ecf20Sopenharmony_ci								\
488c2ecf20Sopenharmony_ci	for (i = 0; i < bus->edev_count; i++) {			\
498c2ecf20Sopenharmony_ci		if (!bus->edev[i])				\
508c2ecf20Sopenharmony_ci			continue;				\
518c2ecf20Sopenharmony_ci		ret = devfreq_event_##ops(bus->edev[i]);	\
528c2ecf20Sopenharmony_ci		if (ret < 0)					\
538c2ecf20Sopenharmony_ci			return ret;				\
548c2ecf20Sopenharmony_ci	}							\
558c2ecf20Sopenharmony_ci								\
568c2ecf20Sopenharmony_ci	return 0;						\
578c2ecf20Sopenharmony_ci}
588c2ecf20Sopenharmony_ciexynos_bus_ops_edev(enable_edev);
598c2ecf20Sopenharmony_ciexynos_bus_ops_edev(disable_edev);
608c2ecf20Sopenharmony_ciexynos_bus_ops_edev(set_event);
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_cistatic int exynos_bus_get_event(struct exynos_bus *bus,
638c2ecf20Sopenharmony_ci				struct devfreq_event_data *edata)
648c2ecf20Sopenharmony_ci{
658c2ecf20Sopenharmony_ci	struct devfreq_event_data event_data;
668c2ecf20Sopenharmony_ci	unsigned long load_count = 0, total_count = 0;
678c2ecf20Sopenharmony_ci	int i, ret = 0;
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	for (i = 0; i < bus->edev_count; i++) {
708c2ecf20Sopenharmony_ci		if (!bus->edev[i])
718c2ecf20Sopenharmony_ci			continue;
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci		ret = devfreq_event_get_event(bus->edev[i], &event_data);
748c2ecf20Sopenharmony_ci		if (ret < 0)
758c2ecf20Sopenharmony_ci			return ret;
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci		if (i == 0 || event_data.load_count > load_count) {
788c2ecf20Sopenharmony_ci			load_count = event_data.load_count;
798c2ecf20Sopenharmony_ci			total_count = event_data.total_count;
808c2ecf20Sopenharmony_ci		}
818c2ecf20Sopenharmony_ci	}
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	edata->load_count = load_count;
848c2ecf20Sopenharmony_ci	edata->total_count = total_count;
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	return ret;
878c2ecf20Sopenharmony_ci}
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci/*
908c2ecf20Sopenharmony_ci * devfreq function for both simple-ondemand and passive governor
918c2ecf20Sopenharmony_ci */
928c2ecf20Sopenharmony_cistatic int exynos_bus_target(struct device *dev, unsigned long *freq, u32 flags)
938c2ecf20Sopenharmony_ci{
948c2ecf20Sopenharmony_ci	struct exynos_bus *bus = dev_get_drvdata(dev);
958c2ecf20Sopenharmony_ci	struct dev_pm_opp *new_opp;
968c2ecf20Sopenharmony_ci	int ret = 0;
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	/* Get correct frequency for bus. */
998c2ecf20Sopenharmony_ci	new_opp = devfreq_recommended_opp(dev, freq, flags);
1008c2ecf20Sopenharmony_ci	if (IS_ERR(new_opp)) {
1018c2ecf20Sopenharmony_ci		dev_err(dev, "failed to get recommended opp instance\n");
1028c2ecf20Sopenharmony_ci		return PTR_ERR(new_opp);
1038c2ecf20Sopenharmony_ci	}
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	dev_pm_opp_put(new_opp);
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	/* Change voltage and frequency according to new OPP level */
1088c2ecf20Sopenharmony_ci	mutex_lock(&bus->lock);
1098c2ecf20Sopenharmony_ci	ret = dev_pm_opp_set_rate(dev, *freq);
1108c2ecf20Sopenharmony_ci	if (!ret)
1118c2ecf20Sopenharmony_ci		bus->curr_freq = *freq;
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	mutex_unlock(&bus->lock);
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	return ret;
1168c2ecf20Sopenharmony_ci}
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_cistatic int exynos_bus_get_dev_status(struct device *dev,
1198c2ecf20Sopenharmony_ci				     struct devfreq_dev_status *stat)
1208c2ecf20Sopenharmony_ci{
1218c2ecf20Sopenharmony_ci	struct exynos_bus *bus = dev_get_drvdata(dev);
1228c2ecf20Sopenharmony_ci	struct devfreq_event_data edata;
1238c2ecf20Sopenharmony_ci	int ret;
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	stat->current_frequency = bus->curr_freq;
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	ret = exynos_bus_get_event(bus, &edata);
1288c2ecf20Sopenharmony_ci	if (ret < 0) {
1298c2ecf20Sopenharmony_ci		dev_err(dev, "failed to get event from devfreq-event devices\n");
1308c2ecf20Sopenharmony_ci		stat->total_time = stat->busy_time = 0;
1318c2ecf20Sopenharmony_ci		goto err;
1328c2ecf20Sopenharmony_ci	}
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	stat->busy_time = (edata.load_count * 100) / bus->ratio;
1358c2ecf20Sopenharmony_ci	stat->total_time = edata.total_count;
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci	dev_dbg(dev, "Usage of devfreq-event : %lu/%lu\n", stat->busy_time,
1388c2ecf20Sopenharmony_ci							stat->total_time);
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_cierr:
1418c2ecf20Sopenharmony_ci	ret = exynos_bus_set_event(bus);
1428c2ecf20Sopenharmony_ci	if (ret < 0) {
1438c2ecf20Sopenharmony_ci		dev_err(dev, "failed to set event to devfreq-event devices\n");
1448c2ecf20Sopenharmony_ci		return ret;
1458c2ecf20Sopenharmony_ci	}
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	return ret;
1488c2ecf20Sopenharmony_ci}
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_cistatic void exynos_bus_exit(struct device *dev)
1518c2ecf20Sopenharmony_ci{
1528c2ecf20Sopenharmony_ci	struct exynos_bus *bus = dev_get_drvdata(dev);
1538c2ecf20Sopenharmony_ci	int ret;
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	ret = exynos_bus_disable_edev(bus);
1568c2ecf20Sopenharmony_ci	if (ret < 0)
1578c2ecf20Sopenharmony_ci		dev_warn(dev, "failed to disable the devfreq-event devices\n");
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	dev_pm_opp_of_remove_table(dev);
1608c2ecf20Sopenharmony_ci	clk_disable_unprepare(bus->clk);
1618c2ecf20Sopenharmony_ci	if (bus->opp_table) {
1628c2ecf20Sopenharmony_ci		dev_pm_opp_put_regulators(bus->opp_table);
1638c2ecf20Sopenharmony_ci		bus->opp_table = NULL;
1648c2ecf20Sopenharmony_ci	}
1658c2ecf20Sopenharmony_ci}
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_cistatic void exynos_bus_passive_exit(struct device *dev)
1688c2ecf20Sopenharmony_ci{
1698c2ecf20Sopenharmony_ci	struct exynos_bus *bus = dev_get_drvdata(dev);
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	dev_pm_opp_of_remove_table(dev);
1728c2ecf20Sopenharmony_ci	clk_disable_unprepare(bus->clk);
1738c2ecf20Sopenharmony_ci}
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_cistatic int exynos_bus_parent_parse_of(struct device_node *np,
1768c2ecf20Sopenharmony_ci					struct exynos_bus *bus)
1778c2ecf20Sopenharmony_ci{
1788c2ecf20Sopenharmony_ci	struct device *dev = bus->dev;
1798c2ecf20Sopenharmony_ci	struct opp_table *opp_table;
1808c2ecf20Sopenharmony_ci	const char *vdd = "vdd";
1818c2ecf20Sopenharmony_ci	int i, ret, count, size;
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	opp_table = dev_pm_opp_set_regulators(dev, &vdd, 1);
1848c2ecf20Sopenharmony_ci	if (IS_ERR(opp_table)) {
1858c2ecf20Sopenharmony_ci		ret = PTR_ERR(opp_table);
1868c2ecf20Sopenharmony_ci		dev_err(dev, "failed to set regulators %d\n", ret);
1878c2ecf20Sopenharmony_ci		return ret;
1888c2ecf20Sopenharmony_ci	}
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci	bus->opp_table = opp_table;
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	/*
1938c2ecf20Sopenharmony_ci	 * Get the devfreq-event devices to get the current utilization of
1948c2ecf20Sopenharmony_ci	 * buses. This raw data will be used in devfreq ondemand governor.
1958c2ecf20Sopenharmony_ci	 */
1968c2ecf20Sopenharmony_ci	count = devfreq_event_get_edev_count(dev, "devfreq-events");
1978c2ecf20Sopenharmony_ci	if (count < 0) {
1988c2ecf20Sopenharmony_ci		dev_err(dev, "failed to get the count of devfreq-event dev\n");
1998c2ecf20Sopenharmony_ci		ret = count;
2008c2ecf20Sopenharmony_ci		goto err_regulator;
2018c2ecf20Sopenharmony_ci	}
2028c2ecf20Sopenharmony_ci	bus->edev_count = count;
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci	size = sizeof(*bus->edev) * count;
2058c2ecf20Sopenharmony_ci	bus->edev = devm_kzalloc(dev, size, GFP_KERNEL);
2068c2ecf20Sopenharmony_ci	if (!bus->edev) {
2078c2ecf20Sopenharmony_ci		ret = -ENOMEM;
2088c2ecf20Sopenharmony_ci		goto err_regulator;
2098c2ecf20Sopenharmony_ci	}
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	for (i = 0; i < count; i++) {
2128c2ecf20Sopenharmony_ci		bus->edev[i] = devfreq_event_get_edev_by_phandle(dev,
2138c2ecf20Sopenharmony_ci							"devfreq-events", i);
2148c2ecf20Sopenharmony_ci		if (IS_ERR(bus->edev[i])) {
2158c2ecf20Sopenharmony_ci			ret = -EPROBE_DEFER;
2168c2ecf20Sopenharmony_ci			goto err_regulator;
2178c2ecf20Sopenharmony_ci		}
2188c2ecf20Sopenharmony_ci	}
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci	/*
2218c2ecf20Sopenharmony_ci	 * Optionally, Get the saturation ratio according to Exynos SoC
2228c2ecf20Sopenharmony_ci	 * When measuring the utilization of each AXI bus with devfreq-event
2238c2ecf20Sopenharmony_ci	 * devices, the measured real cycle might be much lower than the
2248c2ecf20Sopenharmony_ci	 * total cycle of bus during sampling rate. In result, the devfreq
2258c2ecf20Sopenharmony_ci	 * simple-ondemand governor might not decide to change the current
2268c2ecf20Sopenharmony_ci	 * frequency due to too utilization (= real cycle/total cycle).
2278c2ecf20Sopenharmony_ci	 * So, this property is used to adjust the utilization when calculating
2288c2ecf20Sopenharmony_ci	 * the busy_time in exynos_bus_get_dev_status().
2298c2ecf20Sopenharmony_ci	 */
2308c2ecf20Sopenharmony_ci	if (of_property_read_u32(np, "exynos,saturation-ratio", &bus->ratio))
2318c2ecf20Sopenharmony_ci		bus->ratio = DEFAULT_SATURATION_RATIO;
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci	return 0;
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_cierr_regulator:
2368c2ecf20Sopenharmony_ci	dev_pm_opp_put_regulators(bus->opp_table);
2378c2ecf20Sopenharmony_ci	bus->opp_table = NULL;
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	return ret;
2408c2ecf20Sopenharmony_ci}
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_cistatic int exynos_bus_parse_of(struct device_node *np,
2438c2ecf20Sopenharmony_ci			      struct exynos_bus *bus)
2448c2ecf20Sopenharmony_ci{
2458c2ecf20Sopenharmony_ci	struct device *dev = bus->dev;
2468c2ecf20Sopenharmony_ci	struct dev_pm_opp *opp;
2478c2ecf20Sopenharmony_ci	unsigned long rate;
2488c2ecf20Sopenharmony_ci	int ret;
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	/* Get the clock to provide each bus with source clock */
2518c2ecf20Sopenharmony_ci	bus->clk = devm_clk_get(dev, "bus");
2528c2ecf20Sopenharmony_ci	if (IS_ERR(bus->clk)) {
2538c2ecf20Sopenharmony_ci		dev_err(dev, "failed to get bus clock\n");
2548c2ecf20Sopenharmony_ci		return PTR_ERR(bus->clk);
2558c2ecf20Sopenharmony_ci	}
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci	ret = clk_prepare_enable(bus->clk);
2588c2ecf20Sopenharmony_ci	if (ret < 0) {
2598c2ecf20Sopenharmony_ci		dev_err(dev, "failed to get enable clock\n");
2608c2ecf20Sopenharmony_ci		return ret;
2618c2ecf20Sopenharmony_ci	}
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci	/* Get the freq and voltage from OPP table to scale the bus freq */
2648c2ecf20Sopenharmony_ci	ret = dev_pm_opp_of_add_table(dev);
2658c2ecf20Sopenharmony_ci	if (ret < 0) {
2668c2ecf20Sopenharmony_ci		dev_err(dev, "failed to get OPP table\n");
2678c2ecf20Sopenharmony_ci		goto err_clk;
2688c2ecf20Sopenharmony_ci	}
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci	rate = clk_get_rate(bus->clk);
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci	opp = devfreq_recommended_opp(dev, &rate, 0);
2738c2ecf20Sopenharmony_ci	if (IS_ERR(opp)) {
2748c2ecf20Sopenharmony_ci		dev_err(dev, "failed to find dev_pm_opp\n");
2758c2ecf20Sopenharmony_ci		ret = PTR_ERR(opp);
2768c2ecf20Sopenharmony_ci		goto err_opp;
2778c2ecf20Sopenharmony_ci	}
2788c2ecf20Sopenharmony_ci	bus->curr_freq = dev_pm_opp_get_freq(opp);
2798c2ecf20Sopenharmony_ci	dev_pm_opp_put(opp);
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci	return 0;
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_cierr_opp:
2848c2ecf20Sopenharmony_ci	dev_pm_opp_of_remove_table(dev);
2858c2ecf20Sopenharmony_cierr_clk:
2868c2ecf20Sopenharmony_ci	clk_disable_unprepare(bus->clk);
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci	return ret;
2898c2ecf20Sopenharmony_ci}
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_cistatic int exynos_bus_profile_init(struct exynos_bus *bus,
2928c2ecf20Sopenharmony_ci				   struct devfreq_dev_profile *profile)
2938c2ecf20Sopenharmony_ci{
2948c2ecf20Sopenharmony_ci	struct device *dev = bus->dev;
2958c2ecf20Sopenharmony_ci	struct devfreq_simple_ondemand_data *ondemand_data;
2968c2ecf20Sopenharmony_ci	int ret;
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci	/* Initialize the struct profile and governor data for parent device */
2998c2ecf20Sopenharmony_ci	profile->polling_ms = 50;
3008c2ecf20Sopenharmony_ci	profile->target = exynos_bus_target;
3018c2ecf20Sopenharmony_ci	profile->get_dev_status = exynos_bus_get_dev_status;
3028c2ecf20Sopenharmony_ci	profile->exit = exynos_bus_exit;
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci	ondemand_data = devm_kzalloc(dev, sizeof(*ondemand_data), GFP_KERNEL);
3058c2ecf20Sopenharmony_ci	if (!ondemand_data)
3068c2ecf20Sopenharmony_ci		return -ENOMEM;
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci	ondemand_data->upthreshold = 40;
3098c2ecf20Sopenharmony_ci	ondemand_data->downdifferential = 5;
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci	/* Add devfreq device to monitor and handle the exynos bus */
3128c2ecf20Sopenharmony_ci	bus->devfreq = devm_devfreq_add_device(dev, profile,
3138c2ecf20Sopenharmony_ci						DEVFREQ_GOV_SIMPLE_ONDEMAND,
3148c2ecf20Sopenharmony_ci						ondemand_data);
3158c2ecf20Sopenharmony_ci	if (IS_ERR(bus->devfreq)) {
3168c2ecf20Sopenharmony_ci		dev_err(dev, "failed to add devfreq device\n");
3178c2ecf20Sopenharmony_ci		return PTR_ERR(bus->devfreq);
3188c2ecf20Sopenharmony_ci	}
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci	/* Register opp_notifier to catch the change of OPP  */
3218c2ecf20Sopenharmony_ci	ret = devm_devfreq_register_opp_notifier(dev, bus->devfreq);
3228c2ecf20Sopenharmony_ci	if (ret < 0) {
3238c2ecf20Sopenharmony_ci		dev_err(dev, "failed to register opp notifier\n");
3248c2ecf20Sopenharmony_ci		return ret;
3258c2ecf20Sopenharmony_ci	}
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci	/*
3288c2ecf20Sopenharmony_ci	 * Enable devfreq-event to get raw data which is used to determine
3298c2ecf20Sopenharmony_ci	 * current bus load.
3308c2ecf20Sopenharmony_ci	 */
3318c2ecf20Sopenharmony_ci	ret = exynos_bus_enable_edev(bus);
3328c2ecf20Sopenharmony_ci	if (ret < 0) {
3338c2ecf20Sopenharmony_ci		dev_err(dev, "failed to enable devfreq-event devices\n");
3348c2ecf20Sopenharmony_ci		return ret;
3358c2ecf20Sopenharmony_ci	}
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci	ret = exynos_bus_set_event(bus);
3388c2ecf20Sopenharmony_ci	if (ret < 0) {
3398c2ecf20Sopenharmony_ci		dev_err(dev, "failed to set event to devfreq-event devices\n");
3408c2ecf20Sopenharmony_ci		goto err_edev;
3418c2ecf20Sopenharmony_ci	}
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci	return 0;
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_cierr_edev:
3468c2ecf20Sopenharmony_ci	if (exynos_bus_disable_edev(bus))
3478c2ecf20Sopenharmony_ci		dev_warn(dev, "failed to disable the devfreq-event devices\n");
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci	return ret;
3508c2ecf20Sopenharmony_ci}
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_cistatic int exynos_bus_profile_init_passive(struct exynos_bus *bus,
3538c2ecf20Sopenharmony_ci					   struct devfreq_dev_profile *profile)
3548c2ecf20Sopenharmony_ci{
3558c2ecf20Sopenharmony_ci	struct device *dev = bus->dev;
3568c2ecf20Sopenharmony_ci	struct devfreq_passive_data *passive_data;
3578c2ecf20Sopenharmony_ci	struct devfreq *parent_devfreq;
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_ci	/* Initialize the struct profile and governor data for passive device */
3608c2ecf20Sopenharmony_ci	profile->target = exynos_bus_target;
3618c2ecf20Sopenharmony_ci	profile->exit = exynos_bus_passive_exit;
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci	/* Get the instance of parent devfreq device */
3648c2ecf20Sopenharmony_ci	parent_devfreq = devfreq_get_devfreq_by_phandle(dev, "devfreq", 0);
3658c2ecf20Sopenharmony_ci	if (IS_ERR(parent_devfreq))
3668c2ecf20Sopenharmony_ci		return -EPROBE_DEFER;
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci	passive_data = devm_kzalloc(dev, sizeof(*passive_data), GFP_KERNEL);
3698c2ecf20Sopenharmony_ci	if (!passive_data)
3708c2ecf20Sopenharmony_ci		return -ENOMEM;
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci	passive_data->parent = parent_devfreq;
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci	/* Add devfreq device for exynos bus with passive governor */
3758c2ecf20Sopenharmony_ci	bus->devfreq = devm_devfreq_add_device(dev, profile, DEVFREQ_GOV_PASSIVE,
3768c2ecf20Sopenharmony_ci						passive_data);
3778c2ecf20Sopenharmony_ci	if (IS_ERR(bus->devfreq)) {
3788c2ecf20Sopenharmony_ci		dev_err(dev,
3798c2ecf20Sopenharmony_ci			"failed to add devfreq dev with passive governor\n");
3808c2ecf20Sopenharmony_ci		return PTR_ERR(bus->devfreq);
3818c2ecf20Sopenharmony_ci	}
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci	return 0;
3848c2ecf20Sopenharmony_ci}
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_cistatic int exynos_bus_probe(struct platform_device *pdev)
3878c2ecf20Sopenharmony_ci{
3888c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
3898c2ecf20Sopenharmony_ci	struct device_node *np = dev->of_node, *node;
3908c2ecf20Sopenharmony_ci	struct devfreq_dev_profile *profile;
3918c2ecf20Sopenharmony_ci	struct exynos_bus *bus;
3928c2ecf20Sopenharmony_ci	int ret, max_state;
3938c2ecf20Sopenharmony_ci	unsigned long min_freq, max_freq;
3948c2ecf20Sopenharmony_ci	bool passive = false;
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci	if (!np) {
3978c2ecf20Sopenharmony_ci		dev_err(dev, "failed to find devicetree node\n");
3988c2ecf20Sopenharmony_ci		return -EINVAL;
3998c2ecf20Sopenharmony_ci	}
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_ci	bus = devm_kzalloc(&pdev->dev, sizeof(*bus), GFP_KERNEL);
4028c2ecf20Sopenharmony_ci	if (!bus)
4038c2ecf20Sopenharmony_ci		return -ENOMEM;
4048c2ecf20Sopenharmony_ci	mutex_init(&bus->lock);
4058c2ecf20Sopenharmony_ci	bus->dev = &pdev->dev;
4068c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, bus);
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_ci	profile = devm_kzalloc(dev, sizeof(*profile), GFP_KERNEL);
4098c2ecf20Sopenharmony_ci	if (!profile)
4108c2ecf20Sopenharmony_ci		return -ENOMEM;
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci	node = of_parse_phandle(dev->of_node, "devfreq", 0);
4138c2ecf20Sopenharmony_ci	if (node) {
4148c2ecf20Sopenharmony_ci		of_node_put(node);
4158c2ecf20Sopenharmony_ci		passive = true;
4168c2ecf20Sopenharmony_ci	} else {
4178c2ecf20Sopenharmony_ci		ret = exynos_bus_parent_parse_of(np, bus);
4188c2ecf20Sopenharmony_ci		if (ret < 0)
4198c2ecf20Sopenharmony_ci			return ret;
4208c2ecf20Sopenharmony_ci	}
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_ci	/* Parse the device-tree to get the resource information */
4238c2ecf20Sopenharmony_ci	ret = exynos_bus_parse_of(np, bus);
4248c2ecf20Sopenharmony_ci	if (ret < 0)
4258c2ecf20Sopenharmony_ci		goto err_reg;
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_ci	if (passive)
4288c2ecf20Sopenharmony_ci		ret = exynos_bus_profile_init_passive(bus, profile);
4298c2ecf20Sopenharmony_ci	else
4308c2ecf20Sopenharmony_ci		ret = exynos_bus_profile_init(bus, profile);
4318c2ecf20Sopenharmony_ci
4328c2ecf20Sopenharmony_ci	if (ret < 0)
4338c2ecf20Sopenharmony_ci		goto err;
4348c2ecf20Sopenharmony_ci
4358c2ecf20Sopenharmony_ci	max_state = bus->devfreq->profile->max_state;
4368c2ecf20Sopenharmony_ci	min_freq = (bus->devfreq->profile->freq_table[0] / 1000);
4378c2ecf20Sopenharmony_ci	max_freq = (bus->devfreq->profile->freq_table[max_state - 1] / 1000);
4388c2ecf20Sopenharmony_ci	pr_info("exynos-bus: new bus device registered: %s (%6ld KHz ~ %6ld KHz)\n",
4398c2ecf20Sopenharmony_ci			dev_name(dev), min_freq, max_freq);
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_ci	return 0;
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_cierr:
4448c2ecf20Sopenharmony_ci	dev_pm_opp_of_remove_table(dev);
4458c2ecf20Sopenharmony_ci	clk_disable_unprepare(bus->clk);
4468c2ecf20Sopenharmony_cierr_reg:
4478c2ecf20Sopenharmony_ci	if (!passive) {
4488c2ecf20Sopenharmony_ci		dev_pm_opp_put_regulators(bus->opp_table);
4498c2ecf20Sopenharmony_ci		bus->opp_table = NULL;
4508c2ecf20Sopenharmony_ci	}
4518c2ecf20Sopenharmony_ci
4528c2ecf20Sopenharmony_ci	return ret;
4538c2ecf20Sopenharmony_ci}
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_cistatic void exynos_bus_shutdown(struct platform_device *pdev)
4568c2ecf20Sopenharmony_ci{
4578c2ecf20Sopenharmony_ci	struct exynos_bus *bus = dev_get_drvdata(&pdev->dev);
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_ci	devfreq_suspend_device(bus->devfreq);
4608c2ecf20Sopenharmony_ci}
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_ci#ifdef CONFIG_PM_SLEEP
4638c2ecf20Sopenharmony_cistatic int exynos_bus_resume(struct device *dev)
4648c2ecf20Sopenharmony_ci{
4658c2ecf20Sopenharmony_ci	struct exynos_bus *bus = dev_get_drvdata(dev);
4668c2ecf20Sopenharmony_ci	int ret;
4678c2ecf20Sopenharmony_ci
4688c2ecf20Sopenharmony_ci	ret = exynos_bus_enable_edev(bus);
4698c2ecf20Sopenharmony_ci	if (ret < 0) {
4708c2ecf20Sopenharmony_ci		dev_err(dev, "failed to enable the devfreq-event devices\n");
4718c2ecf20Sopenharmony_ci		return ret;
4728c2ecf20Sopenharmony_ci	}
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_ci	return 0;
4758c2ecf20Sopenharmony_ci}
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_cistatic int exynos_bus_suspend(struct device *dev)
4788c2ecf20Sopenharmony_ci{
4798c2ecf20Sopenharmony_ci	struct exynos_bus *bus = dev_get_drvdata(dev);
4808c2ecf20Sopenharmony_ci	int ret;
4818c2ecf20Sopenharmony_ci
4828c2ecf20Sopenharmony_ci	ret = exynos_bus_disable_edev(bus);
4838c2ecf20Sopenharmony_ci	if (ret < 0) {
4848c2ecf20Sopenharmony_ci		dev_err(dev, "failed to disable the devfreq-event devices\n");
4858c2ecf20Sopenharmony_ci		return ret;
4868c2ecf20Sopenharmony_ci	}
4878c2ecf20Sopenharmony_ci
4888c2ecf20Sopenharmony_ci	return 0;
4898c2ecf20Sopenharmony_ci}
4908c2ecf20Sopenharmony_ci#endif
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_cistatic const struct dev_pm_ops exynos_bus_pm = {
4938c2ecf20Sopenharmony_ci	SET_SYSTEM_SLEEP_PM_OPS(exynos_bus_suspend, exynos_bus_resume)
4948c2ecf20Sopenharmony_ci};
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_cistatic const struct of_device_id exynos_bus_of_match[] = {
4978c2ecf20Sopenharmony_ci	{ .compatible = "samsung,exynos-bus", },
4988c2ecf20Sopenharmony_ci	{ /* sentinel */ },
4998c2ecf20Sopenharmony_ci};
5008c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, exynos_bus_of_match);
5018c2ecf20Sopenharmony_ci
5028c2ecf20Sopenharmony_cistatic struct platform_driver exynos_bus_platdrv = {
5038c2ecf20Sopenharmony_ci	.probe		= exynos_bus_probe,
5048c2ecf20Sopenharmony_ci	.shutdown	= exynos_bus_shutdown,
5058c2ecf20Sopenharmony_ci	.driver = {
5068c2ecf20Sopenharmony_ci		.name	= "exynos-bus",
5078c2ecf20Sopenharmony_ci		.pm	= &exynos_bus_pm,
5088c2ecf20Sopenharmony_ci		.of_match_table = of_match_ptr(exynos_bus_of_match),
5098c2ecf20Sopenharmony_ci	},
5108c2ecf20Sopenharmony_ci};
5118c2ecf20Sopenharmony_cimodule_platform_driver(exynos_bus_platdrv);
5128c2ecf20Sopenharmony_ci
5138c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Generic Exynos Bus frequency driver");
5148c2ecf20Sopenharmony_ciMODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
5158c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
516