1 // SPDX-License-Identifier: GPL-2.0+
3 * Texas Instruments System Control Interface (TI SCI) clock driver
5 * Copyright (C) 2018 Texas Instruments Incorporated - http://www.ti.com/
6 * Andreas Dannenberg <dannenberg@ti.com>
8 * Loosely based on Linux kernel sci-clk.c...
14 #include <clk-uclass.h>
15 #include <linux/soc/ti/ti_sci_protocol.h>
19 * struct ti_sci_clk_data - clock controller information structure
20 * @sci: TI SCI handle used for communication with system controller
22 struct ti_sci_clk_data {
23 const struct ti_sci_handle *sci;
26 static int ti_sci_clk_probe(struct udevice *dev)
28 struct ti_sci_clk_data *data = dev_get_priv(dev);
30 debug("%s(dev=%p)\n", __func__, dev);
35 /* Store handle for communication with the system controller */
36 data->sci = ti_sci_get_handle(dev);
37 if (IS_ERR(data->sci))
38 return PTR_ERR(data->sci);
43 static int ti_sci_clk_of_xlate(struct clk *clk,
44 struct ofnode_phandle_args *args)
46 debug("%s(clk=%p, args_count=%d)\n", __func__, clk, args->args_count);
48 if (args->args_count != 2) {
49 debug("Invalid args_count: %d\n", args->args_count);
54 * On TI SCI-based devices, the clock provider id field is used as a
55 * device ID, and the data field is used as the associated sub-ID.
57 clk->id = args->args[0];
58 clk->data = args->args[1];
63 static int ti_sci_clk_request(struct clk *clk)
65 debug("%s(clk=%p)\n", __func__, clk);
69 static int ti_sci_clk_free(struct clk *clk)
71 debug("%s(clk=%p)\n", __func__, clk);
75 static ulong ti_sci_clk_get_rate(struct clk *clk)
77 struct ti_sci_clk_data *data = dev_get_priv(clk->dev);
78 const struct ti_sci_handle *sci = data->sci;
79 const struct ti_sci_clk_ops *cops = &sci->ops.clk_ops;
83 debug("%s(clk=%p)\n", __func__, clk);
85 ret = cops->get_freq(sci, clk->id, clk->data, ¤t_freq);
87 dev_err(clk->dev, "%s: get_freq failed (%d)\n", __func__, ret);
91 debug("%s(current_freq=%llu)\n", __func__, current_freq);
96 static ulong ti_sci_clk_set_rate(struct clk *clk, ulong rate)
98 struct ti_sci_clk_data *data = dev_get_priv(clk->dev);
99 const struct ti_sci_handle *sci = data->sci;
100 const struct ti_sci_clk_ops *cops = &sci->ops.clk_ops;
103 debug("%s(clk=%p, rate=%lu)\n", __func__, clk, rate);
105 #ifdef CONFIG_K3_AVS0
106 k3_avs_notify_freq(clk->id, clk->data, rate);
109 /* Ask for exact frequency by using same value for min/target/max */
110 ret = cops->set_freq(sci, clk->id, clk->data, rate, rate, rate);
112 dev_err(clk->dev, "%s: set_freq failed (%d)\n", __func__, ret);
117 static int ti_sci_clk_set_parent(struct clk *clk, struct clk *parent)
119 struct ti_sci_clk_data *data = dev_get_priv(clk->dev);
120 const struct ti_sci_handle *sci = data->sci;
121 const struct ti_sci_clk_ops *cops = &sci->ops.clk_ops;
126 debug("%s(clk=%p, parent=%p)\n", __func__, clk, parent);
128 /* Make sure the clock parent is valid for a given device ID */
129 if (clk->id != parent->id)
132 /* Make sure clock has parents that can be set */
133 ret = cops->get_num_parents(sci, clk->id, clk->data, &num_parents);
135 dev_err(clk->dev, "%s: get_num_parents failed (%d)\n",
139 if (num_parents < 2) {
140 dev_err(clk->dev, "%s: clock has no settable parents!\n",
145 /* Make sure parent clock ID is valid */
146 parent_cid = parent->data - clk->data - 1;
147 if (parent_cid >= num_parents) {
148 dev_err(clk->dev, "%s: invalid parent clock!\n", __func__);
152 /* Ready to proceed to configure the new clock parent */
153 ret = cops->set_parent(sci, clk->id, clk->data, parent->data);
155 dev_err(clk->dev, "%s: set_parent failed (%d)\n", __func__,
161 static int ti_sci_clk_enable(struct clk *clk)
163 struct ti_sci_clk_data *data = dev_get_priv(clk->dev);
164 const struct ti_sci_handle *sci = data->sci;
165 const struct ti_sci_clk_ops *cops = &sci->ops.clk_ops;
168 debug("%s(clk=%p)\n", __func__, clk);
171 * Allow the System Controller to automatically manage the state of
172 * this clock. If the device is enabled, then the clock is enabled.
174 ret = cops->put_clock(sci, clk->id, clk->data);
176 dev_err(clk->dev, "%s: put_clock failed (%d)\n", __func__, ret);
181 static int ti_sci_clk_disable(struct clk *clk)
183 struct ti_sci_clk_data *data = dev_get_priv(clk->dev);
184 const struct ti_sci_handle *sci = data->sci;
185 const struct ti_sci_clk_ops *cops = &sci->ops.clk_ops;
188 debug("%s(clk=%p)\n", __func__, clk);
190 /* Unconditionally disable clock, regardless of state of the device */
191 ret = cops->idle_clock(sci, clk->id, clk->data);
193 dev_err(clk->dev, "%s: idle_clock failed (%d)\n", __func__,
199 static const struct udevice_id ti_sci_clk_of_match[] = {
200 { .compatible = "ti,k2g-sci-clk" },
204 static struct clk_ops ti_sci_clk_ops = {
205 .of_xlate = ti_sci_clk_of_xlate,
206 .request = ti_sci_clk_request,
207 .free = ti_sci_clk_free,
208 .get_rate = ti_sci_clk_get_rate,
209 .set_rate = ti_sci_clk_set_rate,
210 .set_parent = ti_sci_clk_set_parent,
211 .enable = ti_sci_clk_enable,
212 .disable = ti_sci_clk_disable,
215 U_BOOT_DRIVER(ti_sci_clk) = {
216 .name = "ti-sci-clk",
218 .of_match = ti_sci_clk_of_match,
219 .probe = ti_sci_clk_probe,
220 .priv_auto_alloc_size = sizeof(struct ti_sci_clk_data),
221 .ops = &ti_sci_clk_ops,