1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Texas Instruments System Control Interface (TI SCI) clock driver
4  *
5  * Copyright (C) 2018 Texas Instruments Incorporated - https://www.ti.com/
6  *	Andreas Dannenberg <dannenberg@ti.com>
7  *
8  * Loosely based on Linux kernel sci-clk.c...
9  */
10 
11 #include <dm.h>
12 #include <errno.h>
13 #include <clk-uclass.h>
14 #include <log.h>
15 #include <malloc.h>
16 #include <dm/device_compat.h>
17 #include <linux/err.h>
18 #include <linux/soc/ti/ti_sci_protocol.h>
19 #include <k3-avs.h>
20 
21 /**
22  * struct ti_sci_clk_data - clock controller information structure
23  * @sci: TI SCI handle used for communication with system controller
24  */
25 struct ti_sci_clk_data {
26 	const struct ti_sci_handle *sci;
27 };
28 
ti_sci_clk_probe(struct udevice * dev)29 static int ti_sci_clk_probe(struct udevice *dev)
30 {
31 	struct ti_sci_clk_data *data = dev_get_priv(dev);
32 
33 	debug("%s(dev=%p)\n", __func__, dev);
34 
35 	if (!data)
36 		return -ENOMEM;
37 
38 	/* Store handle for communication with the system controller */
39 	data->sci = ti_sci_get_handle(dev);
40 	if (IS_ERR(data->sci))
41 		return PTR_ERR(data->sci);
42 
43 	return 0;
44 }
45 
ti_sci_clk_of_xlate(struct clk * clk,struct ofnode_phandle_args * args)46 static int ti_sci_clk_of_xlate(struct clk *clk,
47 			       struct ofnode_phandle_args *args)
48 {
49 	debug("%s(clk=%p, args_count=%d)\n", __func__, clk, args->args_count);
50 
51 	if (args->args_count != 2) {
52 		debug("Invalid args_count: %d\n", args->args_count);
53 		return -EINVAL;
54 	}
55 
56 	/*
57 	 * On TI SCI-based devices, the clock provider id field is used as a
58 	 * device ID, and the data field is used as the associated sub-ID.
59 	 */
60 	clk->id = args->args[0];
61 	clk->data = args->args[1];
62 
63 	return 0;
64 }
65 
ti_sci_clk_get_rate(struct clk * clk)66 static ulong ti_sci_clk_get_rate(struct clk *clk)
67 {
68 	struct ti_sci_clk_data *data = dev_get_priv(clk->dev);
69 	const struct ti_sci_handle *sci = data->sci;
70 	const struct ti_sci_clk_ops *cops = &sci->ops.clk_ops;
71 	u64 current_freq;
72 	int ret;
73 
74 	debug("%s(clk=%p)\n", __func__, clk);
75 
76 	ret = cops->get_freq(sci, clk->id, clk->data, &current_freq);
77 	if (ret) {
78 		dev_err(clk->dev, "%s: get_freq failed (%d)\n", __func__, ret);
79 		return ret;
80 	}
81 
82 	debug("%s(current_freq=%llu)\n", __func__, current_freq);
83 
84 	return current_freq;
85 }
86 
ti_sci_clk_set_rate(struct clk * clk,ulong rate)87 static ulong ti_sci_clk_set_rate(struct clk *clk, ulong rate)
88 {
89 	struct ti_sci_clk_data *data = dev_get_priv(clk->dev);
90 	const struct ti_sci_handle *sci = data->sci;
91 	const struct ti_sci_clk_ops *cops = &sci->ops.clk_ops;
92 	int ret;
93 	int freq_scale_up = rate >= ti_sci_clk_get_rate(clk) ? 1 : 0;
94 
95 	debug("%s(clk=%p, rate=%lu)\n", __func__, clk, rate);
96 
97 	if (IS_ENABLED(CONFIG_K3_AVS0) && freq_scale_up)
98 		k3_avs_notify_freq(clk->id, clk->data, rate);
99 
100 	ret = cops->set_freq(sci, clk->id, clk->data, 0, rate, ULONG_MAX);
101 	if (ret) {
102 		dev_err(clk->dev, "%s: set_freq failed (%d)\n", __func__, ret);
103 		return ret;
104 	}
105 
106 	if (IS_ENABLED(CONFIG_K3_AVS0) && !freq_scale_up)
107 		k3_avs_notify_freq(clk->id, clk->data, rate);
108 
109 	return rate;
110 }
111 
ti_sci_clk_set_parent(struct clk * clk,struct clk * parent)112 static int ti_sci_clk_set_parent(struct clk *clk, struct clk *parent)
113 {
114 	struct ti_sci_clk_data *data = dev_get_priv(clk->dev);
115 	const struct ti_sci_handle *sci = data->sci;
116 	const struct ti_sci_clk_ops *cops = &sci->ops.clk_ops;
117 	u8 num_parents;
118 	u8 parent_cid;
119 	int ret;
120 
121 	debug("%s(clk=%p, parent=%p)\n", __func__, clk, parent);
122 
123 	/* Make sure the clock parent is valid for a given device ID */
124 	if (clk->id != parent->id)
125 		return -EINVAL;
126 
127 	/* Make sure clock has parents that can be set */
128 	ret = cops->get_num_parents(sci, clk->id, clk->data, &num_parents);
129 	if (ret) {
130 		dev_err(clk->dev, "%s: get_num_parents failed (%d)\n",
131 			__func__, ret);
132 		return ret;
133 	}
134 	if (num_parents < 2) {
135 		dev_err(clk->dev, "%s: clock has no settable parents!\n",
136 			__func__);
137 		return -EINVAL;
138 	}
139 
140 	/* Make sure parent clock ID is valid */
141 	parent_cid = parent->data - clk->data - 1;
142 	if (parent_cid >= num_parents) {
143 		dev_err(clk->dev, "%s: invalid parent clock!\n", __func__);
144 		return -EINVAL;
145 	}
146 
147 	/* Ready to proceed to configure the new clock parent */
148 	ret = cops->set_parent(sci, clk->id, clk->data, parent->data);
149 	if (ret)
150 		dev_err(clk->dev, "%s: set_parent failed (%d)\n", __func__,
151 			ret);
152 
153 	return ret;
154 }
155 
ti_sci_clk_enable(struct clk * clk)156 static int ti_sci_clk_enable(struct clk *clk)
157 {
158 	struct ti_sci_clk_data *data = dev_get_priv(clk->dev);
159 	const struct ti_sci_handle *sci = data->sci;
160 	const struct ti_sci_clk_ops *cops = &sci->ops.clk_ops;
161 	int ret;
162 
163 	debug("%s(clk=%p)\n", __func__, clk);
164 
165 	/*
166 	 * Allow the System Controller to automatically manage the state of
167 	 * this clock. If the device is enabled, then the clock is enabled.
168 	 */
169 	ret = cops->put_clock(sci, clk->id, clk->data);
170 	if (ret)
171 		dev_err(clk->dev, "%s: put_clock failed (%d)\n", __func__, ret);
172 
173 	return ret;
174 }
175 
ti_sci_clk_disable(struct clk * clk)176 static int ti_sci_clk_disable(struct clk *clk)
177 {
178 	struct ti_sci_clk_data *data = dev_get_priv(clk->dev);
179 	const struct ti_sci_handle *sci = data->sci;
180 	const struct ti_sci_clk_ops *cops = &sci->ops.clk_ops;
181 	int ret;
182 
183 	debug("%s(clk=%p)\n", __func__, clk);
184 
185 	/* Unconditionally disable clock, regardless of state of the device */
186 	ret = cops->idle_clock(sci, clk->id, clk->data);
187 	if (ret)
188 		dev_err(clk->dev, "%s: idle_clock failed (%d)\n", __func__,
189 			ret);
190 
191 	return ret;
192 }
193 
194 static const struct udevice_id ti_sci_clk_of_match[] = {
195 	{ .compatible = "ti,k2g-sci-clk" },
196 	{ /* sentinel */ },
197 };
198 
199 static struct clk_ops ti_sci_clk_ops = {
200 	.of_xlate = ti_sci_clk_of_xlate,
201 	.get_rate = ti_sci_clk_get_rate,
202 	.set_rate = ti_sci_clk_set_rate,
203 	.set_parent = ti_sci_clk_set_parent,
204 	.enable = ti_sci_clk_enable,
205 	.disable = ti_sci_clk_disable,
206 };
207 
208 U_BOOT_DRIVER(ti_sci_clk) = {
209 	.name = "ti-sci-clk",
210 	.id = UCLASS_CLK,
211 	.of_match = ti_sci_clk_of_match,
212 	.probe = ti_sci_clk_probe,
213 	.priv_auto	= sizeof(struct ti_sci_clk_data),
214 	.ops = &ti_sci_clk_ops,
215 };
216