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>
18 * struct ti_sci_clk_data - clock controller information structure
19 * @sci: TI SCI handle used for communication with system controller
21 struct ti_sci_clk_data {
22 const struct ti_sci_handle *sci;
25 static int ti_sci_clk_probe(struct udevice *dev)
27 struct ti_sci_clk_data *data = dev_get_priv(dev);
29 debug("%s(dev=%p)\n", __func__, dev);
34 /* Store handle for communication with the system controller */
35 data->sci = ti_sci_get_handle(dev);
36 if (IS_ERR(data->sci))
37 return PTR_ERR(data->sci);
42 static int ti_sci_clk_of_xlate(struct clk *clk,
43 struct ofnode_phandle_args *args)
45 debug("%s(clk=%p, args_count=%d)\n", __func__, clk, args->args_count);
47 if (args->args_count != 2) {
48 debug("Invalid args_count: %d\n", args->args_count);
53 * On TI SCI-based devices, the clock provider id field is used as a
54 * device ID, and the data field is used as the associated sub-ID.
56 clk->id = args->args[0];
57 clk->data = args->args[1];
62 static int ti_sci_clk_request(struct clk *clk)
64 debug("%s(clk=%p)\n", __func__, clk);
68 static int ti_sci_clk_free(struct clk *clk)
70 debug("%s(clk=%p)\n", __func__, clk);
74 static ulong ti_sci_clk_get_rate(struct clk *clk)
76 struct ti_sci_clk_data *data = dev_get_priv(clk->dev);
77 const struct ti_sci_handle *sci = data->sci;
78 const struct ti_sci_clk_ops *cops = &sci->ops.clk_ops;
82 debug("%s(clk=%p)\n", __func__, clk);
84 ret = cops->get_freq(sci, clk->id, clk->data, ¤t_freq);
86 dev_err(clk->dev, "%s: get_freq failed (%d)\n", __func__, ret);
90 debug("%s(current_freq=%llu)\n", __func__, current_freq);
95 static ulong ti_sci_clk_set_rate(struct clk *clk, ulong rate)
97 struct ti_sci_clk_data *data = dev_get_priv(clk->dev);
98 const struct ti_sci_handle *sci = data->sci;
99 const struct ti_sci_clk_ops *cops = &sci->ops.clk_ops;
102 debug("%s(clk=%p, rate=%lu)\n", __func__, clk, rate);
104 /* Ask for exact frequency by using same value for min/target/max */
105 ret = cops->set_freq(sci, clk->id, clk->data, rate, rate, rate);
107 dev_err(clk->dev, "%s: set_freq failed (%d)\n", __func__, ret);
112 static int ti_sci_clk_set_parent(struct clk *clk, struct clk *parent)
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;
121 debug("%s(clk=%p, parent=%p)\n", __func__, clk, parent);
123 /* Make sure the clock parent is valid for a given device ID */
124 if (clk->id != parent->id)
127 /* Make sure clock has parents that can be set */
128 ret = cops->get_num_parents(sci, clk->id, clk->data, &num_parents);
130 dev_err(clk->dev, "%s: get_num_parents failed (%d)\n",
134 if (num_parents < 2) {
135 dev_err(clk->dev, "%s: clock has no settable parents!\n",
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__);
147 /* Ready to proceed to configure the new clock parent */
148 ret = cops->set_parent(sci, clk->id, clk->data, parent->data);
150 dev_err(clk->dev, "%s: set_parent failed (%d)\n", __func__,
156 static int ti_sci_clk_enable(struct clk *clk)
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;
163 debug("%s(clk=%p)\n", __func__, clk);
166 * Allow the System Controller to automatically manage the state of
167 * this clock. If the device is enabled, then the clock is enabled.
169 ret = cops->put_clock(sci, clk->id, clk->data);
171 dev_err(clk->dev, "%s: put_clock failed (%d)\n", __func__, ret);
176 static int ti_sci_clk_disable(struct clk *clk)
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;
183 debug("%s(clk=%p)\n", __func__, clk);
185 /* Unconditionally disable clock, regardless of state of the device */
186 ret = cops->idle_clock(sci, clk->id, clk->data);
188 dev_err(clk->dev, "%s: idle_clock failed (%d)\n", __func__,
194 static const struct udevice_id ti_sci_clk_of_match[] = {
195 { .compatible = "ti,k2g-sci-clk" },
199 static struct clk_ops ti_sci_clk_ops = {
200 .of_xlate = ti_sci_clk_of_xlate,
201 .request = ti_sci_clk_request,
202 .free = ti_sci_clk_free,
203 .get_rate = ti_sci_clk_get_rate,
204 .set_rate = ti_sci_clk_set_rate,
205 .set_parent = ti_sci_clk_set_parent,
206 .enable = ti_sci_clk_enable,
207 .disable = ti_sci_clk_disable,
210 U_BOOT_DRIVER(ti_sci_clk) = {
211 .name = "ti-sci-clk",
213 .of_match = ti_sci_clk_of_match,
214 .probe = ti_sci_clk_probe,
215 .priv_auto_alloc_size = sizeof(struct ti_sci_clk_data),
216 .ops = &ti_sci_clk_ops,