blob: acb9eadf03c3148d202400488227f8b5efd124b1 [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>
Simon Glassf7ae49f2020-05-10 11:40:05 -060015#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070016#include <malloc.h>
17#include <dm/device_compat.h>
Simon Glass61b29b82020-02-03 07:36:15 -070018#include <linux/err.h>
Andreas Dannenberge585bef2018-08-27 15:57:43 +053019#include <linux/soc/ti/ti_sci_protocol.h>
Keerthye0aa8732019-10-24 15:00:47 +053020#include <k3-avs.h>
Andreas Dannenberge585bef2018-08-27 15:57:43 +053021
22/**
23 * struct ti_sci_clk_data - clock controller information structure
24 * @sci: TI SCI handle used for communication with system controller
25 */
26struct ti_sci_clk_data {
27 const struct ti_sci_handle *sci;
28};
29
30static int ti_sci_clk_probe(struct udevice *dev)
31{
32 struct ti_sci_clk_data *data = dev_get_priv(dev);
33
34 debug("%s(dev=%p)\n", __func__, dev);
35
36 if (!data)
37 return -ENOMEM;
38
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);
43
44 return 0;
45}
46
47static int ti_sci_clk_of_xlate(struct clk *clk,
48 struct ofnode_phandle_args *args)
49{
50 debug("%s(clk=%p, args_count=%d)\n", __func__, clk, args->args_count);
51
52 if (args->args_count != 2) {
53 debug("Invalid args_count: %d\n", args->args_count);
54 return -EINVAL;
55 }
56
57 /*
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.
60 */
61 clk->id = args->args[0];
62 clk->data = args->args[1];
63
64 return 0;
65}
66
67static int ti_sci_clk_request(struct clk *clk)
68{
69 debug("%s(clk=%p)\n", __func__, clk);
70 return 0;
71}
72
73static int ti_sci_clk_free(struct clk *clk)
74{
75 debug("%s(clk=%p)\n", __func__, clk);
76 return 0;
77}
78
79static ulong ti_sci_clk_get_rate(struct clk *clk)
80{
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;
84 u64 current_freq;
85 int ret;
86
87 debug("%s(clk=%p)\n", __func__, clk);
88
89 ret = cops->get_freq(sci, clk->id, clk->data, &current_freq);
90 if (ret) {
91 dev_err(clk->dev, "%s: get_freq failed (%d)\n", __func__, ret);
92 return ret;
93 }
94
95 debug("%s(current_freq=%llu)\n", __func__, current_freq);
96
97 return current_freq;
98}
99
100static ulong ti_sci_clk_set_rate(struct clk *clk, ulong rate)
101{
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;
105 int ret;
106
107 debug("%s(clk=%p, rate=%lu)\n", __func__, clk, rate);
108
Keerthye0aa8732019-10-24 15:00:47 +0530109#ifdef CONFIG_K3_AVS0
110 k3_avs_notify_freq(clk->id, clk->data, rate);
111#endif
112
Lokesh Vutlaea67b262020-01-17 11:57:30 +0530113 ret = cops->set_freq(sci, clk->id, clk->data, 0, rate, ULONG_MAX);
Tero Kristoc3195722021-06-11 11:45:10 +0300114 if (ret) {
Andreas Dannenberge585bef2018-08-27 15:57:43 +0530115 dev_err(clk->dev, "%s: set_freq failed (%d)\n", __func__, ret);
Tero Kristoc3195722021-06-11 11:45:10 +0300116 return ret;
117 }
Andreas Dannenberge585bef2018-08-27 15:57:43 +0530118
Tero Kristoc3195722021-06-11 11:45:10 +0300119 return rate;
Andreas Dannenberge585bef2018-08-27 15:57:43 +0530120}
121
122static int ti_sci_clk_set_parent(struct clk *clk, struct clk *parent)
123{
124 struct ti_sci_clk_data *data = dev_get_priv(clk->dev);
125 const struct ti_sci_handle *sci = data->sci;
126 const struct ti_sci_clk_ops *cops = &sci->ops.clk_ops;
127 u8 num_parents;
128 u8 parent_cid;
129 int ret;
130
131 debug("%s(clk=%p, parent=%p)\n", __func__, clk, parent);
132
133 /* Make sure the clock parent is valid for a given device ID */
134 if (clk->id != parent->id)
135 return -EINVAL;
136
137 /* Make sure clock has parents that can be set */
138 ret = cops->get_num_parents(sci, clk->id, clk->data, &num_parents);
139 if (ret) {
140 dev_err(clk->dev, "%s: get_num_parents failed (%d)\n",
141 __func__, ret);
142 return ret;
143 }
144 if (num_parents < 2) {
145 dev_err(clk->dev, "%s: clock has no settable parents!\n",
146 __func__);
147 return -EINVAL;
148 }
149
150 /* Make sure parent clock ID is valid */
151 parent_cid = parent->data - clk->data - 1;
152 if (parent_cid >= num_parents) {
153 dev_err(clk->dev, "%s: invalid parent clock!\n", __func__);
154 return -EINVAL;
155 }
156
157 /* Ready to proceed to configure the new clock parent */
158 ret = cops->set_parent(sci, clk->id, clk->data, parent->data);
159 if (ret)
160 dev_err(clk->dev, "%s: set_parent failed (%d)\n", __func__,
161 ret);
162
163 return ret;
164}
165
166static int ti_sci_clk_enable(struct clk *clk)
167{
168 struct ti_sci_clk_data *data = dev_get_priv(clk->dev);
169 const struct ti_sci_handle *sci = data->sci;
170 const struct ti_sci_clk_ops *cops = &sci->ops.clk_ops;
171 int ret;
172
173 debug("%s(clk=%p)\n", __func__, clk);
174
175 /*
176 * Allow the System Controller to automatically manage the state of
177 * this clock. If the device is enabled, then the clock is enabled.
178 */
179 ret = cops->put_clock(sci, clk->id, clk->data);
180 if (ret)
181 dev_err(clk->dev, "%s: put_clock failed (%d)\n", __func__, ret);
182
183 return ret;
184}
185
186static int ti_sci_clk_disable(struct clk *clk)
187{
188 struct ti_sci_clk_data *data = dev_get_priv(clk->dev);
189 const struct ti_sci_handle *sci = data->sci;
190 const struct ti_sci_clk_ops *cops = &sci->ops.clk_ops;
191 int ret;
192
193 debug("%s(clk=%p)\n", __func__, clk);
194
195 /* Unconditionally disable clock, regardless of state of the device */
196 ret = cops->idle_clock(sci, clk->id, clk->data);
197 if (ret)
198 dev_err(clk->dev, "%s: idle_clock failed (%d)\n", __func__,
199 ret);
200
201 return ret;
202}
203
204static const struct udevice_id ti_sci_clk_of_match[] = {
205 { .compatible = "ti,k2g-sci-clk" },
206 { /* sentinel */ },
207};
208
209static struct clk_ops ti_sci_clk_ops = {
210 .of_xlate = ti_sci_clk_of_xlate,
211 .request = ti_sci_clk_request,
Simon Glassfb8c0d52020-02-03 07:35:54 -0700212 .rfree = ti_sci_clk_free,
Andreas Dannenberge585bef2018-08-27 15:57:43 +0530213 .get_rate = ti_sci_clk_get_rate,
214 .set_rate = ti_sci_clk_set_rate,
215 .set_parent = ti_sci_clk_set_parent,
216 .enable = ti_sci_clk_enable,
217 .disable = ti_sci_clk_disable,
218};
219
220U_BOOT_DRIVER(ti_sci_clk) = {
221 .name = "ti-sci-clk",
222 .id = UCLASS_CLK,
223 .of_match = ti_sci_clk_of_match,
224 .probe = ti_sci_clk_probe,
Simon Glass41575d82020-12-03 16:55:17 -0700225 .priv_auto = sizeof(struct ti_sci_clk_data),
Andreas Dannenberge585bef2018-08-27 15:57:43 +0530226 .ops = &ti_sci_clk_ops,
227};