xref: /openbmc/linux/drivers/clk/clk-scmi.c (revision 9a29ad52)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * System Control and Power Interface (SCMI) Protocol based clock driver
4  *
5  * Copyright (C) 2018 ARM Ltd.
6  */
7 
8 #include <linux/clk-provider.h>
9 #include <linux/device.h>
10 #include <linux/err.h>
11 #include <linux/of.h>
12 #include <linux/module.h>
13 #include <linux/scmi_protocol.h>
14 #include <asm/div64.h>
15 
16 struct scmi_clk {
17 	u32 id;
18 	struct clk_hw hw;
19 	const struct scmi_clock_info *info;
20 	const struct scmi_handle *handle;
21 };
22 
23 #define to_scmi_clk(clk) container_of(clk, struct scmi_clk, hw)
24 
25 static unsigned long scmi_clk_recalc_rate(struct clk_hw *hw,
26 					  unsigned long parent_rate)
27 {
28 	int ret;
29 	u64 rate;
30 	struct scmi_clk *clk = to_scmi_clk(hw);
31 
32 	ret = clk->handle->clk_ops->rate_get(clk->handle, clk->id, &rate);
33 	if (ret)
34 		return 0;
35 	return rate;
36 }
37 
38 static long scmi_clk_round_rate(struct clk_hw *hw, unsigned long rate,
39 				unsigned long *parent_rate)
40 {
41 	int step;
42 	u64 fmin, fmax, ftmp;
43 	struct scmi_clk *clk = to_scmi_clk(hw);
44 
45 	/*
46 	 * We can't figure out what rate it will be, so just return the
47 	 * rate back to the caller. scmi_clk_recalc_rate() will be called
48 	 * after the rate is set and we'll know what rate the clock is
49 	 * running at then.
50 	 */
51 	if (clk->info->rate_discrete)
52 		return rate;
53 
54 	fmin = clk->info->range.min_rate;
55 	fmax = clk->info->range.max_rate;
56 	if (rate <= fmin)
57 		return fmin;
58 	else if (rate >= fmax)
59 		return fmax;
60 
61 	ftmp = rate - fmin;
62 	ftmp += clk->info->range.step_size - 1; /* to round up */
63 	step = do_div(ftmp, clk->info->range.step_size);
64 
65 	return step * clk->info->range.step_size + fmin;
66 }
67 
68 static int scmi_clk_set_rate(struct clk_hw *hw, unsigned long rate,
69 			     unsigned long parent_rate)
70 {
71 	struct scmi_clk *clk = to_scmi_clk(hw);
72 
73 	return clk->handle->clk_ops->rate_set(clk->handle, clk->id, 0, rate);
74 }
75 
76 static int scmi_clk_enable(struct clk_hw *hw)
77 {
78 	struct scmi_clk *clk = to_scmi_clk(hw);
79 
80 	return clk->handle->clk_ops->enable(clk->handle, clk->id);
81 }
82 
83 static void scmi_clk_disable(struct clk_hw *hw)
84 {
85 	struct scmi_clk *clk = to_scmi_clk(hw);
86 
87 	clk->handle->clk_ops->disable(clk->handle, clk->id);
88 }
89 
90 static const struct clk_ops scmi_clk_ops = {
91 	.recalc_rate = scmi_clk_recalc_rate,
92 	.round_rate = scmi_clk_round_rate,
93 	.set_rate = scmi_clk_set_rate,
94 	/*
95 	 * We can't provide enable/disable callback as we can't perform the same
96 	 * in atomic context. Since the clock framework provides standard API
97 	 * clk_prepare_enable that helps cases using clk_enable in non-atomic
98 	 * context, it should be fine providing prepare/unprepare.
99 	 */
100 	.prepare = scmi_clk_enable,
101 	.unprepare = scmi_clk_disable,
102 };
103 
104 static int scmi_clk_ops_init(struct device *dev, struct scmi_clk *sclk)
105 {
106 	int ret;
107 	struct clk_init_data init = {
108 		.flags = CLK_GET_RATE_NOCACHE,
109 		.num_parents = 0,
110 		.ops = &scmi_clk_ops,
111 		.name = sclk->info->name,
112 	};
113 
114 	sclk->hw.init = &init;
115 	ret = devm_clk_hw_register(dev, &sclk->hw);
116 	if (!ret)
117 		clk_hw_set_rate_range(&sclk->hw, sclk->info->range.min_rate,
118 				      sclk->info->range.max_rate);
119 	return ret;
120 }
121 
122 static int scmi_clocks_probe(struct scmi_device *sdev)
123 {
124 	int idx, count, err;
125 	struct clk_hw **hws;
126 	struct clk_hw_onecell_data *clk_data;
127 	struct device *dev = &sdev->dev;
128 	struct device_node *np = dev->of_node;
129 	const struct scmi_handle *handle = sdev->handle;
130 
131 	if (!handle || !handle->clk_ops)
132 		return -ENODEV;
133 
134 	count = handle->clk_ops->count_get(handle);
135 	if (count < 0) {
136 		dev_err(dev, "%s: invalid clock output count\n", np->name);
137 		return -EINVAL;
138 	}
139 
140 	clk_data = devm_kzalloc(dev, struct_size(clk_data, hws, count),
141 				GFP_KERNEL);
142 	if (!clk_data)
143 		return -ENOMEM;
144 
145 	clk_data->num = count;
146 	hws = clk_data->hws;
147 
148 	for (idx = 0; idx < count; idx++) {
149 		struct scmi_clk *sclk;
150 
151 		sclk = devm_kzalloc(dev, sizeof(*sclk), GFP_KERNEL);
152 		if (!sclk)
153 			return -ENOMEM;
154 
155 		sclk->info = handle->clk_ops->info_get(handle, idx);
156 		if (!sclk->info) {
157 			dev_dbg(dev, "invalid clock info for idx %d\n", idx);
158 			continue;
159 		}
160 
161 		sclk->id = idx;
162 		sclk->handle = handle;
163 
164 		err = scmi_clk_ops_init(dev, sclk);
165 		if (err) {
166 			dev_err(dev, "failed to register clock %d\n", idx);
167 			devm_kfree(dev, sclk);
168 			hws[idx] = NULL;
169 		} else {
170 			dev_dbg(dev, "Registered clock:%s\n", sclk->info->name);
171 			hws[idx] = &sclk->hw;
172 		}
173 	}
174 
175 	return devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get,
176 					   clk_data);
177 }
178 
179 static const struct scmi_device_id scmi_id_table[] = {
180 	{ SCMI_PROTOCOL_CLOCK },
181 	{ },
182 };
183 MODULE_DEVICE_TABLE(scmi, scmi_id_table);
184 
185 static struct scmi_driver scmi_clocks_driver = {
186 	.name = "scmi-clocks",
187 	.probe = scmi_clocks_probe,
188 	.id_table = scmi_id_table,
189 };
190 module_scmi_driver(scmi_clocks_driver);
191 
192 MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
193 MODULE_DESCRIPTION("ARM SCMI clock driver");
194 MODULE_LICENSE("GPL v2");
195