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>
16 #include <dm/device_compat.h>
17 #include <linux/err.h>
18 #include <linux/soc/ti/ti_sci_protocol.h>
22 * struct ti_sci_clk_data - clock controller information structure
23 * @sci: TI SCI handle used for communication with system controller
25 struct ti_sci_clk_data {
26 const struct ti_sci_handle *sci;
29 static int ti_sci_clk_probe(struct udevice *dev)
31 struct ti_sci_clk_data *data = dev_get_priv(dev);
33 debug("%s(dev=%p)\n", __func__, dev);
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);
46 static int ti_sci_clk_of_xlate(struct clk *clk,
47 struct ofnode_phandle_args *args)
49 debug("%s(clk=%p, args_count=%d)\n", __func__, clk, args->args_count);
51 if (args->args_count != 2) {
52 debug("Invalid args_count: %d\n", args->args_count);
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.
60 clk->id = args->args[0];
61 clk->data = args->args[1];
66 static int ti_sci_clk_request(struct clk *clk)
68 debug("%s(clk=%p)\n", __func__, clk);
72 static int ti_sci_clk_free(struct clk *clk)
74 debug("%s(clk=%p)\n", __func__, clk);
78 static ulong ti_sci_clk_get_rate(struct clk *clk)
80 struct ti_sci_clk_data *data = dev_get_priv(clk->dev);
81 const struct ti_sci_handle *sci = data->sci;
82 const struct ti_sci_clk_ops *cops = &sci->ops.clk_ops;
86 debug("%s(clk=%p)\n", __func__, clk);
88 ret = cops->get_freq(sci, clk->id, clk->data, ¤t_freq);
90 dev_err(clk->dev, "%s: get_freq failed (%d)\n", __func__, ret);
94 debug("%s(current_freq=%llu)\n", __func__, current_freq);
99 static ulong ti_sci_clk_set_rate(struct clk *clk, ulong rate)
101 struct ti_sci_clk_data *data = dev_get_priv(clk->dev);
102 const struct ti_sci_handle *sci = data->sci;
103 const struct ti_sci_clk_ops *cops = &sci->ops.clk_ops;
106 debug("%s(clk=%p, rate=%lu)\n", __func__, clk, rate);
108 #ifdef CONFIG_K3_AVS0
109 k3_avs_notify_freq(clk->id, clk->data, rate);
112 ret = cops->set_freq(sci, clk->id, clk->data, 0, rate, ULONG_MAX);
114 dev_err(clk->dev, "%s: set_freq failed (%d)\n", __func__, ret);
119 static int ti_sci_clk_set_parent(struct clk *clk, struct clk *parent)
121 struct ti_sci_clk_data *data = dev_get_priv(clk->dev);
122 const struct ti_sci_handle *sci = data->sci;
123 const struct ti_sci_clk_ops *cops = &sci->ops.clk_ops;
128 debug("%s(clk=%p, parent=%p)\n", __func__, clk, parent);
130 /* Make sure the clock parent is valid for a given device ID */
131 if (clk->id != parent->id)
134 /* Make sure clock has parents that can be set */
135 ret = cops->get_num_parents(sci, clk->id, clk->data, &num_parents);
137 dev_err(clk->dev, "%s: get_num_parents failed (%d)\n",
141 if (num_parents < 2) {
142 dev_err(clk->dev, "%s: clock has no settable parents!\n",
147 /* Make sure parent clock ID is valid */
148 parent_cid = parent->data - clk->data - 1;
149 if (parent_cid >= num_parents) {
150 dev_err(clk->dev, "%s: invalid parent clock!\n", __func__);
154 /* Ready to proceed to configure the new clock parent */
155 ret = cops->set_parent(sci, clk->id, clk->data, parent->data);
157 dev_err(clk->dev, "%s: set_parent failed (%d)\n", __func__,
163 static int ti_sci_clk_enable(struct clk *clk)
165 struct ti_sci_clk_data *data = dev_get_priv(clk->dev);
166 const struct ti_sci_handle *sci = data->sci;
167 const struct ti_sci_clk_ops *cops = &sci->ops.clk_ops;
170 debug("%s(clk=%p)\n", __func__, clk);
173 * Allow the System Controller to automatically manage the state of
174 * this clock. If the device is enabled, then the clock is enabled.
176 ret = cops->put_clock(sci, clk->id, clk->data);
178 dev_err(clk->dev, "%s: put_clock failed (%d)\n", __func__, ret);
183 static int ti_sci_clk_disable(struct clk *clk)
185 struct ti_sci_clk_data *data = dev_get_priv(clk->dev);
186 const struct ti_sci_handle *sci = data->sci;
187 const struct ti_sci_clk_ops *cops = &sci->ops.clk_ops;
190 debug("%s(clk=%p)\n", __func__, clk);
192 /* Unconditionally disable clock, regardless of state of the device */
193 ret = cops->idle_clock(sci, clk->id, clk->data);
195 dev_err(clk->dev, "%s: idle_clock failed (%d)\n", __func__,
201 static const struct udevice_id ti_sci_clk_of_match[] = {
202 { .compatible = "ti,k2g-sci-clk" },
206 static struct clk_ops ti_sci_clk_ops = {
207 .of_xlate = ti_sci_clk_of_xlate,
208 .request = ti_sci_clk_request,
209 .rfree = ti_sci_clk_free,
210 .get_rate = ti_sci_clk_get_rate,
211 .set_rate = ti_sci_clk_set_rate,
212 .set_parent = ti_sci_clk_set_parent,
213 .enable = ti_sci_clk_enable,
214 .disable = ti_sci_clk_disable,
217 U_BOOT_DRIVER(ti_sci_clk) = {
218 .name = "ti-sci-clk",
220 .of_match = ti_sci_clk_of_match,
221 .probe = ti_sci_clk_probe,
222 .priv_auto_alloc_size = sizeof(struct ti_sci_clk_data),
223 .ops = &ti_sci_clk_ops,