aboutsummaryrefslogtreecommitdiff
path: root/drivers/clk/clk-scpi.c
blob: 2ced0ceff36b56daff72715a212c8b0bd8dff5db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
 * System Control and Power Interface (SCPI) Protocol based clock driver
 *
 * Copyright (C) 2015 ARM Ltd.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include <linux/clkdev.h>
#include <linux/clk-provider.h>
#include <linux/device.h>
#include <linux/err.h>
#include <linux/of.h>
#include <linux/module.h>
#include <linux/of_platform.h>
#include <linux/scpi_protocol.h>

struct scpi_clk {
	u32 id;
	const char *name;
	struct clk_hw hw;
	struct scpi_dvfs_info *info;
	unsigned long rate_min;
	unsigned long rate_max;
};

#define to_scpi_clk(clk) container_of(clk, struct scpi_clk, hw)

static struct scpi_ops *scpi_ops;
static struct platform_device *cpufreq_dev;

static unsigned long scpi_clk_recalc_rate(struct clk_hw *hw,
					  unsigned long parent_rate)
{
	struct scpi_clk *clk = to_scpi_clk(hw);

	return scpi_ops->clk_get_val(clk->id);
}

static long scpi_clk_round_rate(struct clk_hw *hw, unsigned long rate,
				unsigned long *parent_rate)
{
	struct scpi_clk *clk = to_scpi_clk(hw);

	if (clk->rate_min && rate < clk->rate_min)
		rate = clk->rate_min;
	if (clk->rate_max && rate > clk->rate_max)
		rate = clk->rate_max;

	return rate;
}

static int scpi_clk_set_rate(struct clk_hw *hw, unsigned long rate,
			     unsigned long parent_rate)
{
	struct scpi_clk *clk = to_scpi_clk(hw);

	return scpi_ops->clk_set_val(clk->id, rate);
}

static struct clk_ops scpi_clk_ops = {
	.recalc_rate = scpi_clk_recalc_rate,
	.round_rate = scpi_clk_round_rate,
	.set_rate = scpi_clk_set_rate,
};

/* find closest match to given frequency in OPP table */
static int __scpi_dvfs_round_rate(struct scpi_clk *clk, unsigned long rate)
{
	int idx;
	u32 fmin = 0, fmax = ~0, ftmp;
	struct scpi_opp *opp = clk->info->opps;

	for (idx = 0; idx < clk->info->count; idx++, opp++) {
		ftmp = opp->freq;
		if (ftmp >= (u32)rate) {
			if (ftmp <= fmax)
				fmax = ftmp;
		} else {
			if (ftmp >= fmin)
				fmin = ftmp;
		}
	}
	if (fmax != ~0)
		return fmax;
	return fmin;
}

static unsigned long scpi_dvfs_recalc_rate(struct clk_hw *hw,
					   unsigned long parent_rate)
{
	struct scpi_clk *clk = to_scpi_clk(hw);
	int idx = scpi_ops->dvfs_get_idx(clk->id);
	struct scpi_opp *opp;

	if (idx < 0)
		return 0;

	opp = clk->info->opps + idx;
	return opp->freq;
}

static long scpi_dvfs_round_rate(struct clk_hw *hw, unsigned long rate,
				 unsigned long *parent_rate)
{
	struct scpi_clk *clk = to_scpi_clk(hw);

	return __scpi_dvfs_round_rate(clk, rate);
}

static int __scpi_find_dvfs_index(struct scpi_clk *clk, unsigned long rate)
{
	int idx, max_opp = clk->info->count;
	struct scpi_opp *opp = clk->info->opps;

	for (idx = 0; idx < max_opp; idx++, opp++)
		if (opp->freq == rate)
			break;
	return (idx == max_opp) ? -EINVAL : idx;
}

static int scpi_dvfs_set_rate(struct clk_hw *hw, unsigned long rate,
			      unsigned long parent_rate)
{
	struct scpi_clk *clk = to_scpi_clk(hw);
	int ret = __scpi_find_dvfs_index(clk, rate);

	if (ret < 0)
		return ret;
	return scpi_ops->dvfs_set_idx(clk->id, (u8)ret);
}

static struct clk_ops scpi_dvfs_ops = {
	.recalc_rate = scpi_dvfs_recalc_rate,
	.round_rate = scpi_dvfs_round_rate,
	.set_rate = scpi_dvfs_set_rate,
};

static struct clk *
scpi_dvfs_ops_init(struct device *dev, struct device_node *np,
		   struct scpi_clk *sclk)
{
	struct clk_init_data init;
	struct scpi_dvfs_info *info;

	init.name = sclk->name;
	init.flags = CLK_IS_ROOT;
	init.num_parents = 0;
	init.ops = &scpi_dvfs_ops;
	sclk->hw.init = &init;

	info = scpi_ops->dvfs_get_info(sclk->id);
	if (IS_ERR(info))
		return (struct clk *)info;

	sclk->info = info;

	return devm_clk_register(dev, &sclk->hw);
}

static struct clk *
scpi_clk_ops_init(struct device *dev, struct device_node *np,
		  struct scpi_clk *sclk)
{
	struct clk_init_data init;
	int ret;

	init.name = sclk->name;
	init.flags = CLK_IS_ROOT;
	init.num_parents = 0;
	init.ops = &scpi_clk_ops;
	sclk->hw.init = &init;

	ret = scpi_ops->clk_get_range(sclk->id, &sclk->rate_min,
				      &sclk->rate_max);
	if (!sclk->rate_max)
		ret = -EINVAL;
	if (ret)
		return ERR_PTR(ret);

	return devm_clk_register(dev, &sclk->hw);
}

static const struct of_device_id scpi_clk_match[] = {
	{ .compatible = "arm,scpi-dvfs", .data = scpi_dvfs_ops_init, },
	{ .compatible = "arm,scpi-clk", .data = scpi_clk_ops_init, },
	{}
};

static int scpi_clk_probe(struct platform_device *pdev)
{
	struct clk **clks;
	int idx, count;
	struct clk_onecell_data *clk_data;
	struct device *dev = &pdev->dev;
	struct device_node *np = dev->of_node;
	struct clk *(*clk_ops_init)(struct device *, struct device_node *,
				    struct scpi_clk *);

	if (!of_device_is_available(np))
		return -ENODEV;

	clk_ops_init = of_match_node(scpi_clk_match, np)->data;
	if (!clk_ops_init)
		return -ENODEV;

	count = of_property_count_strings(np, "clock-output-names");
	if (count < 0) {
		dev_err(dev, "%s: invalid clock output count\n", np->name);
		return -EINVAL;
	}

	clk_data = devm_kmalloc(dev, sizeof(*clk_data), GFP_KERNEL);
	if (!clk_data) {
		dev_err(dev, "failed to allocate clock provider data\n");
		return -ENOMEM;
	}

	clks = devm_kmalloc(dev, count * sizeof(*clks), GFP_KERNEL);
	if (!clks) {
		dev_err(dev, "failed to allocate clock providers\n");
		return -ENOMEM;
	}

	for (idx = 0; idx < count; idx++) {
		struct scpi_clk *sclk;
		u32 val;

		sclk = devm_kzalloc(dev, sizeof(*sclk), GFP_KERNEL);
		if (!sclk) {
			dev_err(dev, "failed to allocate scpi clocks\n");
			return -ENOMEM;
		}

		if (of_property_read_string_index(np, "clock-output-names",
						  idx, &sclk->name)) {
			dev_err(dev, "invalid clock name @ %s\n", np->name);
			return -EINVAL;
		}

		if (of_property_read_u32_index(np, "clock-indices",
					       idx, &val)) {
			dev_err(dev, "invalid clock index @ %s\n", np->name);
			return -EINVAL;
		}

		sclk->id = val;

		clks[idx] = clk_ops_init(dev, np, sclk);
		if (IS_ERR(clks[idx])) {
			dev_err(dev, "failed to register clock '%s'\n",
				sclk->name);
		}

		dev_dbg(dev, "Registered clock '%s'\n", sclk->name);
	}

	clk_data->clks = clks;
	clk_data->clk_num = idx;
	of_clk_add_provider(np, of_clk_src_onecell_get, clk_data);

	platform_set_drvdata(pdev, clk_data);

	if (clk_ops_init == scpi_dvfs_ops_init) {
		/* Add virtual cpufreq device depending SCPI clock */
		cpufreq_dev = platform_device_register_simple("scpi-cpufreq",
							      -1, NULL, 0);
		if (!cpufreq_dev)
			pr_warn("unable to register cpufreq device");
	}
	return 0;
}

static int scpi_clk_remove(struct platform_device *pdev)
{
	if (cpufreq_dev) {
		platform_device_unregister(cpufreq_dev);
		cpufreq_dev = NULL;
	}

	of_clk_del_provider(pdev->dev.of_node);
	platform_set_drvdata(pdev, NULL);
	return 0;
}

static struct platform_driver scpi_clk_driver = {
	.driver	= {
		.name = "scpi_clk",
		.of_match_table = scpi_clk_match,
	},
	.probe = scpi_clk_probe,
	.remove = scpi_clk_remove,
};

static int scpi_clocks_probe(struct platform_device *pdev)
{
	scpi_ops = get_scpi_ops();
	if (!scpi_ops)
		return -ENXIO;

	return of_platform_populate(pdev->dev.of_node, scpi_clk_match,
				    NULL, &pdev->dev);
}

static int scpi_clocks_remove(struct platform_device *pdev)
{
	of_platform_depopulate(&pdev->dev);
	scpi_ops = NULL;
	return 0;
}

static const struct of_device_id scpi_clocks_ids[] = {
	{ .compatible = "arm,scpi-clocks", },
	{}
};

static struct platform_driver scpi_clocks_driver = {
	.driver	= {
		.name = "scpi_clocks",
		.of_match_table = scpi_clocks_ids,
	},
	.probe = scpi_clocks_probe,
	.remove = scpi_clocks_remove,
};

static int __init scpi_clocks_init(void)
{
	int ret;

	ret = platform_driver_register(&scpi_clk_driver);
	if (ret)
		return ret;

	return platform_driver_register(&scpi_clocks_driver);
}
module_init(scpi_clocks_init);

static void __exit scpi_clocks_exit(void)
{
	platform_driver_unregister(&scpi_clk_driver);
	platform_driver_unregister(&scpi_clocks_driver);
}
module_exit(scpi_clocks_exit);

MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
MODULE_DESCRIPTION("ARM SCPI clock driver");
MODULE_LICENSE("GPL");