blob: 8212435ba81c626aeb3ee84f3a81757edf1d23aa [file] [log] [blame]
Andreas Dannenberge585bef2018-08-27 15:57:43 +05301// 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 - http://www.ti.com/
6 * Andreas Dannenberg <dannenberg@ti.com>
7 *
8 * Loosely based on Linux kernel sci-clk.c...
9 */
10
11#include <common.h>
12#include <dm.h>
13#include <errno.h>
14#include <clk-uclass.h>
15#include <linux/soc/ti/ti_sci_protocol.h>
Keerthye0aa8732019-10-24 15:00:47 +053016#include <k3-avs.h>
Andreas Dannenberge585bef2018-08-27 15:57:43 +053017
18/**
19 * struct ti_sci_clk_data - clock controller information structure
20 * @sci: TI SCI handle used for communication with system controller
21 */
22struct ti_sci_clk_data {
23 const struct ti_sci_handle *sci;
24};
25
26static int ti_sci_clk_probe(struct udevice *dev)
27{
28 struct ti_sci_clk_data *data = dev_get_priv(dev);
29
30 debug("%s(dev=%p)\n", __func__, dev);
31
32 if (!data)
33 return -ENOMEM;
34
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);
39
40 return 0;
41}
42
43static int ti_sci_clk_of_xlate(struct clk *clk,
44 struct ofnode_phandle_args *args)
45{
46 debug("%s(clk=%p, args_count=%d)\n", __func__, clk, args->args_count);
47
48 if (args->args_count != 2) {
49 debug("Invalid args_count: %d\n", args->args_count);
50 return -EINVAL;
51 }
52
53 /*
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.
56 */
57 clk->id = args->args[0];
58 clk->data = args->args[1];
59
60 return 0;
61}
62
63static int ti_sci_clk_request(struct clk *clk)
64{
65 debug("%s(clk=%p)\n", __func__, clk);
66 return 0;
67}
68
69static int ti_sci_clk_free(struct clk *clk)
70{
71 debug("%s(clk=%p)\n", __func__, clk);
72 return 0;
73}
74
75static ulong ti_sci_clk_get_rate(struct clk *clk)
76{
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;
80 u64 current_freq;
81 int ret;
82
83 debug("%s(clk=%p)\n", __func__, clk);
84
85 ret = cops->get_freq(sci, clk->id, clk->data, &current_freq);
86 if (ret) {
87 dev_err(clk->dev, "%s: get_freq failed (%d)\n", __func__, ret);
88 return ret;
89 }
90
91 debug("%s(current_freq=%llu)\n", __func__, current_freq);
92
93 return current_freq;
94}
95
96static ulong ti_sci_clk_set_rate(struct clk *clk, ulong rate)
97{
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;
101 int ret;
102
103 debug("%s(clk=%p, rate=%lu)\n", __func__, clk, rate);
104
Keerthye0aa8732019-10-24 15:00:47 +0530105#ifdef CONFIG_K3_AVS0
106 k3_avs_notify_freq(clk->id, clk->data, rate);
107#endif
108
Lokesh Vutlaea67b262020-01-17 11:57:30 +0530109 ret = cops->set_freq(sci, clk->id, clk->data, 0, rate, ULONG_MAX);
Andreas Dannenberge585bef2018-08-27 15:57:43 +0530110 if (ret)
111 dev_err(clk->dev, "%s: set_freq failed (%d)\n", __func__, ret);
112
113 return ret;
114}
115
116static int ti_sci_clk_set_parent(struct clk *clk, struct clk *parent)
117{
118 struct ti_sci_clk_data *data = dev_get_priv(clk->dev);
119 const struct ti_sci_handle *sci = data->sci;
120 const struct ti_sci_clk_ops *cops = &sci->ops.clk_ops;
121 u8 num_parents;
122 u8 parent_cid;
123 int ret;
124
125 debug("%s(clk=%p, parent=%p)\n", __func__, clk, parent);
126
127 /* Make sure the clock parent is valid for a given device ID */
128 if (clk->id != parent->id)
129 return -EINVAL;
130
131 /* Make sure clock has parents that can be set */
132 ret = cops->get_num_parents(sci, clk->id, clk->data, &num_parents);
133 if (ret) {
134 dev_err(clk->dev, "%s: get_num_parents failed (%d)\n",
135 __func__, ret);
136 return ret;
137 }
138 if (num_parents < 2) {
139 dev_err(clk->dev, "%s: clock has no settable parents!\n",
140 __func__);
141 return -EINVAL;
142 }
143
144 /* Make sure parent clock ID is valid */
145 parent_cid = parent->data - clk->data - 1;
146 if (parent_cid >= num_parents) {
147 dev_err(clk->dev, "%s: invalid parent clock!\n", __func__);
148 return -EINVAL;
149 }
150
151 /* Ready to proceed to configure the new clock parent */
152 ret = cops->set_parent(sci, clk->id, clk->data, parent->data);
153 if (ret)
154 dev_err(clk->dev, "%s: set_parent failed (%d)\n", __func__,
155 ret);
156
157 return ret;
158}
159
160static int ti_sci_clk_enable(struct clk *clk)
161{
162 struct ti_sci_clk_data *data = dev_get_priv(clk->dev);
163 const struct ti_sci_handle *sci = data->sci;
164 const struct ti_sci_clk_ops *cops = &sci->ops.clk_ops;
165 int ret;
166
167 debug("%s(clk=%p)\n", __func__, clk);
168
169 /*
170 * Allow the System Controller to automatically manage the state of
171 * this clock. If the device is enabled, then the clock is enabled.
172 */
173 ret = cops->put_clock(sci, clk->id, clk->data);
174 if (ret)
175 dev_err(clk->dev, "%s: put_clock failed (%d)\n", __func__, ret);
176
177 return ret;
178}
179
180static int ti_sci_clk_disable(struct clk *clk)
181{
182 struct ti_sci_clk_data *data = dev_get_priv(clk->dev);
183 const struct ti_sci_handle *sci = data->sci;
184 const struct ti_sci_clk_ops *cops = &sci->ops.clk_ops;
185 int ret;
186
187 debug("%s(clk=%p)\n", __func__, clk);
188
189 /* Unconditionally disable clock, regardless of state of the device */
190 ret = cops->idle_clock(sci, clk->id, clk->data);
191 if (ret)
192 dev_err(clk->dev, "%s: idle_clock failed (%d)\n", __func__,
193 ret);
194
195 return ret;
196}
197
198static const struct udevice_id ti_sci_clk_of_match[] = {
199 { .compatible = "ti,k2g-sci-clk" },
200 { /* sentinel */ },
201};
202
203static struct clk_ops ti_sci_clk_ops = {
204 .of_xlate = ti_sci_clk_of_xlate,
205 .request = ti_sci_clk_request,
Simon Glassfb8c0d52020-02-03 07:35:54 -0700206 .rfree = ti_sci_clk_free,
Andreas Dannenberge585bef2018-08-27 15:57:43 +0530207 .get_rate = ti_sci_clk_get_rate,
208 .set_rate = ti_sci_clk_set_rate,
209 .set_parent = ti_sci_clk_set_parent,
210 .enable = ti_sci_clk_enable,
211 .disable = ti_sci_clk_disable,
212};
213
214U_BOOT_DRIVER(ti_sci_clk) = {
215 .name = "ti-sci-clk",
216 .id = UCLASS_CLK,
217 .of_match = ti_sci_clk_of_match,
218 .probe = ti_sci_clk_probe,
219 .priv_auto_alloc_size = sizeof(struct ti_sci_clk_data),
220 .ops = &ti_sci_clk_ops,
221};