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>
17 #include <dm/device_compat.h>
18 #include <linux/err.h>
19 #include <linux/soc/ti/ti_sci_protocol.h>
23 * struct ti_sci_clk_data - clock controller information structure
24 * @sci: TI SCI handle used for communication with system controller
26 struct ti_sci_clk_data {
27 const struct ti_sci_handle *sci;
30 static int ti_sci_clk_probe(struct udevice *dev)
32 struct ti_sci_clk_data *data = dev_get_priv(dev);
34 debug("%s(dev=%p)\n", __func__, dev);
39 /* Store handle for communication with the system controller */
40 data->sci = ti_sci_get_handle(dev);
41 if (IS_ERR(data->sci))
42 return PTR_ERR(data->sci);
47 static int ti_sci_clk_of_xlate(struct clk *clk,
48 struct ofnode_phandle_args *args)
50 debug("%s(clk=%p, args_count=%d)\n", __func__, clk, args->args_count);
52 if (args->args_count != 2) {
53 debug("Invalid args_count: %d\n", args->args_count);
58 * On TI SCI-based devices, the clock provider id field is used as a
59 * device ID, and the data field is used as the associated sub-ID.
61 clk->id = args->args[0];
62 clk->data = args->args[1];
67 static int ti_sci_clk_request(struct clk *clk)
69 debug("%s(clk=%p)\n", __func__, clk);
73 static int ti_sci_clk_free(struct clk *clk)
75 debug("%s(clk=%p)\n", __func__, clk);
79 static ulong ti_sci_clk_get_rate(struct clk *clk)
81 struct ti_sci_clk_data *data = dev_get_priv(clk->dev);
82 const struct ti_sci_handle *sci = data->sci;
83 const struct ti_sci_clk_ops *cops = &sci->ops.clk_ops;
87 debug("%s(clk=%p)\n", __func__, clk);
89 ret = cops->get_freq(sci, clk->id, clk->data, ¤t_freq);
91 dev_err(clk->dev, "%s: get_freq failed (%d)\n", __func__, ret);
95 debug("%s(current_freq=%llu)\n", __func__, current_freq);
100 static ulong ti_sci_clk_set_rate(struct clk *clk, ulong rate)
102 struct ti_sci_clk_data *data = dev_get_priv(clk->dev);
103 const struct ti_sci_handle *sci = data->sci;
104 const struct ti_sci_clk_ops *cops = &sci->ops.clk_ops;
107 debug("%s(clk=%p, rate=%lu)\n", __func__, clk, rate);
109 #ifdef CONFIG_K3_AVS0
110 k3_avs_notify_freq(clk->id, clk->data, rate);
113 ret = cops->set_freq(sci, clk->id, clk->data, 0, rate, ULONG_MAX);
115 dev_err(clk->dev, "%s: set_freq failed (%d)\n", __func__, ret);
120 static int ti_sci_clk_set_parent(struct clk *clk, struct clk *parent)
122 struct ti_sci_clk_data *data = dev_get_priv(clk->dev);
123 const struct ti_sci_handle *sci = data->sci;
124 const struct ti_sci_clk_ops *cops = &sci->ops.clk_ops;
129 debug("%s(clk=%p, parent=%p)\n", __func__, clk, parent);
131 /* Make sure the clock parent is valid for a given device ID */
132 if (clk->id != parent->id)
135 /* Make sure clock has parents that can be set */
136 ret = cops->get_num_parents(sci, clk->id, clk->data, &num_parents);
138 dev_err(clk->dev, "%s: get_num_parents failed (%d)\n",
142 if (num_parents < 2) {
143 dev_err(clk->dev, "%s: clock has no settable parents!\n",
148 /* Make sure parent clock ID is valid */
149 parent_cid = parent->data - clk->data - 1;
150 if (parent_cid >= num_parents) {
151 dev_err(clk->dev, "%s: invalid parent clock!\n", __func__);
155 /* Ready to proceed to configure the new clock parent */
156 ret = cops->set_parent(sci, clk->id, clk->data, parent->data);
158 dev_err(clk->dev, "%s: set_parent failed (%d)\n", __func__,
164 static int ti_sci_clk_enable(struct clk *clk)
166 struct ti_sci_clk_data *data = dev_get_priv(clk->dev);
167 const struct ti_sci_handle *sci = data->sci;
168 const struct ti_sci_clk_ops *cops = &sci->ops.clk_ops;
171 debug("%s(clk=%p)\n", __func__, clk);
174 * Allow the System Controller to automatically manage the state of
175 * this clock. If the device is enabled, then the clock is enabled.
177 ret = cops->put_clock(sci, clk->id, clk->data);
179 dev_err(clk->dev, "%s: put_clock failed (%d)\n", __func__, ret);
184 static int ti_sci_clk_disable(struct clk *clk)
186 struct ti_sci_clk_data *data = dev_get_priv(clk->dev);
187 const struct ti_sci_handle *sci = data->sci;
188 const struct ti_sci_clk_ops *cops = &sci->ops.clk_ops;
191 debug("%s(clk=%p)\n", __func__, clk);
193 /* Unconditionally disable clock, regardless of state of the device */
194 ret = cops->idle_clock(sci, clk->id, clk->data);
196 dev_err(clk->dev, "%s: idle_clock failed (%d)\n", __func__,
202 static const struct udevice_id ti_sci_clk_of_match[] = {
203 { .compatible = "ti,k2g-sci-clk" },
207 static struct clk_ops ti_sci_clk_ops = {
208 .of_xlate = ti_sci_clk_of_xlate,
209 .request = ti_sci_clk_request,
210 .rfree = ti_sci_clk_free,
211 .get_rate = ti_sci_clk_get_rate,
212 .set_rate = ti_sci_clk_set_rate,
213 .set_parent = ti_sci_clk_set_parent,
214 .enable = ti_sci_clk_enable,
215 .disable = ti_sci_clk_disable,
218 U_BOOT_DRIVER(ti_sci_clk) = {
219 .name = "ti-sci-clk",
221 .of_match = ti_sci_clk_of_match,
222 .probe = ti_sci_clk_probe,
223 .priv_auto_alloc_size = sizeof(struct ti_sci_clk_data),
224 .ops = &ti_sci_clk_ops,