xref: /linux/drivers/interconnect/samsung/exynos.c (revision 715a1284d89a740b197b3bad5eb20d36a397382f)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Exynos generic interconnect provider driver
4  *
5  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
6  *
7  * Authors: Artur Świgoń <a.swigon@samsung.com>
8  *          Sylwester Nawrocki <s.nawrocki@samsung.com>
9  */
10 #include <linux/device.h>
11 #include <linux/interconnect-provider.h>
12 #include <linux/module.h>
13 #include <linux/of.h>
14 #include <linux/platform_device.h>
15 #include <linux/pm_qos.h>
16 #include <linux/slab.h>
17 
18 #define EXYNOS_ICC_DEFAULT_BUS_CLK_RATIO	8
19 
20 struct exynos_icc_priv {
21 	struct device *dev;
22 
23 	/* One interconnect node per provider */
24 	struct icc_provider provider;
25 	struct icc_node *node;
26 
27 	struct dev_pm_qos_request qos_req;
28 	u32 bus_clk_ratio;
29 };
30 
31 static struct icc_node *exynos_icc_get_parent(struct device_node *np)
32 {
33 	struct of_phandle_args args;
34 	struct icc_node_data *icc_node_data;
35 	struct icc_node *icc_node;
36 	int num, ret;
37 
38 	num = of_count_phandle_with_args(np, "interconnects",
39 					 "#interconnect-cells");
40 	if (num < 1)
41 		return NULL; /* parent nodes are optional */
42 
43 	/* Get the interconnect target node */
44 	ret = of_parse_phandle_with_args(np, "interconnects",
45 					"#interconnect-cells", 0, &args);
46 	if (ret < 0)
47 		return ERR_PTR(ret);
48 
49 	icc_node_data = of_icc_get_from_provider(&args);
50 	of_node_put(args.np);
51 
52 	if (IS_ERR(icc_node_data))
53 		return ERR_CAST(icc_node_data);
54 
55 	icc_node = icc_node_data->node;
56 	kfree(icc_node_data);
57 
58 	return icc_node;
59 }
60 
61 static int exynos_generic_icc_set(struct icc_node *src, struct icc_node *dst)
62 {
63 	struct exynos_icc_priv *src_priv = src->data, *dst_priv = dst->data;
64 	s32 src_freq = max(src->avg_bw, src->peak_bw) / src_priv->bus_clk_ratio;
65 	s32 dst_freq = max(dst->avg_bw, dst->peak_bw) / dst_priv->bus_clk_ratio;
66 	int ret;
67 
68 	ret = dev_pm_qos_update_request(&src_priv->qos_req, src_freq);
69 	if (ret < 0) {
70 		dev_err(src_priv->dev, "failed to update PM QoS of %s (src)\n",
71 			src->name);
72 		return ret;
73 	}
74 
75 	ret = dev_pm_qos_update_request(&dst_priv->qos_req, dst_freq);
76 	if (ret < 0) {
77 		dev_err(dst_priv->dev, "failed to update PM QoS of %s (dst)\n",
78 			dst->name);
79 		return ret;
80 	}
81 
82 	return 0;
83 }
84 
85 static struct icc_node *exynos_generic_icc_xlate(struct of_phandle_args *spec,
86 						 void *data)
87 {
88 	struct exynos_icc_priv *priv = data;
89 
90 	if (spec->np != priv->dev->parent->of_node)
91 		return ERR_PTR(-EINVAL);
92 
93 	return priv->node;
94 }
95 
96 static int exynos_generic_icc_remove(struct platform_device *pdev)
97 {
98 	struct exynos_icc_priv *priv = platform_get_drvdata(pdev);
99 	struct icc_node *parent_node, *node = priv->node;
100 
101 	parent_node = exynos_icc_get_parent(priv->dev->parent->of_node);
102 	if (parent_node && !IS_ERR(parent_node))
103 		icc_link_destroy(node, parent_node);
104 
105 	icc_nodes_remove(&priv->provider);
106 	icc_provider_del(&priv->provider);
107 
108 	return 0;
109 }
110 
111 static int exynos_generic_icc_probe(struct platform_device *pdev)
112 {
113 	struct device *bus_dev = pdev->dev.parent;
114 	struct exynos_icc_priv *priv;
115 	struct icc_provider *provider;
116 	struct icc_node *icc_node, *icc_parent_node;
117 	int ret;
118 
119 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
120 	if (!priv)
121 		return -ENOMEM;
122 
123 	priv->dev = &pdev->dev;
124 	platform_set_drvdata(pdev, priv);
125 
126 	provider = &priv->provider;
127 
128 	provider->set = exynos_generic_icc_set;
129 	provider->aggregate = icc_std_aggregate;
130 	provider->xlate = exynos_generic_icc_xlate;
131 	provider->dev = bus_dev;
132 	provider->inter_set = true;
133 	provider->data = priv;
134 
135 	ret = icc_provider_add(provider);
136 	if (ret < 0)
137 		return ret;
138 
139 	icc_node = icc_node_create(pdev->id);
140 	if (IS_ERR(icc_node)) {
141 		ret = PTR_ERR(icc_node);
142 		goto err_prov_del;
143 	}
144 
145 	priv->node = icc_node;
146 	icc_node->name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%pOFn",
147 					bus_dev->of_node);
148 	if (of_property_read_u32(bus_dev->of_node, "samsung,data-clock-ratio",
149 				 &priv->bus_clk_ratio))
150 		priv->bus_clk_ratio = EXYNOS_ICC_DEFAULT_BUS_CLK_RATIO;
151 
152 	/*
153 	 * Register a PM QoS request for the parent (devfreq) device.
154 	 */
155 	ret = dev_pm_qos_add_request(bus_dev, &priv->qos_req,
156 				     DEV_PM_QOS_MIN_FREQUENCY, 0);
157 	if (ret < 0)
158 		goto err_node_del;
159 
160 	icc_node->data = priv;
161 	icc_node_add(icc_node, provider);
162 
163 	icc_parent_node = exynos_icc_get_parent(bus_dev->of_node);
164 	if (IS_ERR(icc_parent_node)) {
165 		ret = PTR_ERR(icc_parent_node);
166 		goto err_pmqos_del;
167 	}
168 	if (icc_parent_node) {
169 		ret = icc_link_create(icc_node, icc_parent_node->id);
170 		if (ret < 0)
171 			goto err_pmqos_del;
172 	}
173 
174 	return 0;
175 
176 err_pmqos_del:
177 	dev_pm_qos_remove_request(&priv->qos_req);
178 err_node_del:
179 	icc_nodes_remove(provider);
180 err_prov_del:
181 	icc_provider_del(provider);
182 	return ret;
183 }
184 
185 static struct platform_driver exynos_generic_icc_driver = {
186 	.driver = {
187 		.name = "exynos-generic-icc",
188 		.sync_state = icc_sync_state,
189 	},
190 	.probe = exynos_generic_icc_probe,
191 	.remove = exynos_generic_icc_remove,
192 };
193 module_platform_driver(exynos_generic_icc_driver);
194 
195 MODULE_DESCRIPTION("Exynos generic interconnect driver");
196 MODULE_AUTHOR("Artur Świgoń <a.swigon@samsung.com>");
197 MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
198 MODULE_LICENSE("GPL v2");
199 MODULE_ALIAS("platform:exynos-generic-icc");
200