blob: e374bd3bcc20d140ca0e25d0ec63f3faeda06901 [file] [log] [blame]
Andreas Dannenberg1530e352018-08-27 15:57:43 +05301// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Texas Instruments System Control Interface (TI SCI) clock driver
4 *
Nishanth Menoneaa39c62023-11-01 15:56:03 -05005 * Copyright (C) 2018 Texas Instruments Incorporated - https://www.ti.com/
Andreas Dannenberg1530e352018-08-27 15:57:43 +05306 * Andreas Dannenberg <dannenberg@ti.com>
7 *
8 * Loosely based on Linux kernel sci-clk.c...
9 */
10
Andreas Dannenberg1530e352018-08-27 15:57:43 +053011#include <dm.h>
12#include <errno.h>
13#include <clk-uclass.h>
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070015#include <malloc.h>
16#include <dm/device_compat.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070017#include <linux/err.h>
Andreas Dannenberg1530e352018-08-27 15:57:43 +053018#include <linux/soc/ti/ti_sci_protocol.h>
Keerthy280d7a82019-10-24 15:00:47 +053019#include <k3-avs.h>
Andreas Dannenberg1530e352018-08-27 15:57:43 +053020
21/**
22 * struct ti_sci_clk_data - clock controller information structure
23 * @sci: TI SCI handle used for communication with system controller
24 */
25struct ti_sci_clk_data {
26 const struct ti_sci_handle *sci;
27};
28
29static int ti_sci_clk_probe(struct udevice *dev)
30{
31 struct ti_sci_clk_data *data = dev_get_priv(dev);
32
33 debug("%s(dev=%p)\n", __func__, dev);
34
35 if (!data)
36 return -ENOMEM;
37
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);
42
43 return 0;
44}
45
46static int ti_sci_clk_of_xlate(struct clk *clk,
47 struct ofnode_phandle_args *args)
48{
49 debug("%s(clk=%p, args_count=%d)\n", __func__, clk, args->args_count);
50
51 if (args->args_count != 2) {
52 debug("Invalid args_count: %d\n", args->args_count);
53 return -EINVAL;
54 }
55
56 /*
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.
59 */
60 clk->id = args->args[0];
61 clk->data = args->args[1];
62
63 return 0;
64}
65
Andreas Dannenberg1530e352018-08-27 15:57:43 +053066static ulong ti_sci_clk_get_rate(struct clk *clk)
67{
68 struct ti_sci_clk_data *data = dev_get_priv(clk->dev);
69 const struct ti_sci_handle *sci = data->sci;
70 const struct ti_sci_clk_ops *cops = &sci->ops.clk_ops;
71 u64 current_freq;
72 int ret;
73
74 debug("%s(clk=%p)\n", __func__, clk);
75
76 ret = cops->get_freq(sci, clk->id, clk->data, &current_freq);
77 if (ret) {
78 dev_err(clk->dev, "%s: get_freq failed (%d)\n", __func__, ret);
79 return ret;
80 }
81
82 debug("%s(current_freq=%llu)\n", __func__, current_freq);
83
84 return current_freq;
85}
86
87static ulong ti_sci_clk_set_rate(struct clk *clk, ulong rate)
88{
89 struct ti_sci_clk_data *data = dev_get_priv(clk->dev);
90 const struct ti_sci_handle *sci = data->sci;
91 const struct ti_sci_clk_ops *cops = &sci->ops.clk_ops;
92 int ret;
Udit Kumarc31616f2023-09-21 22:33:43 +053093 int freq_scale_up = rate >= ti_sci_clk_get_rate(clk) ? 1 : 0;
Andreas Dannenberg1530e352018-08-27 15:57:43 +053094
95 debug("%s(clk=%p, rate=%lu)\n", __func__, clk, rate);
96
Udit Kumarc31616f2023-09-21 22:33:43 +053097 if (IS_ENABLED(CONFIG_K3_AVS0) && freq_scale_up)
98 k3_avs_notify_freq(clk->id, clk->data, rate);
Keerthy280d7a82019-10-24 15:00:47 +053099
Lokesh Vutla74fc6b82020-01-17 11:57:30 +0530100 ret = cops->set_freq(sci, clk->id, clk->data, 0, rate, ULONG_MAX);
Tero Kristo17708842021-06-11 11:45:10 +0300101 if (ret) {
Andreas Dannenberg1530e352018-08-27 15:57:43 +0530102 dev_err(clk->dev, "%s: set_freq failed (%d)\n", __func__, ret);
Tero Kristo17708842021-06-11 11:45:10 +0300103 return ret;
104 }
Andreas Dannenberg1530e352018-08-27 15:57:43 +0530105
Udit Kumarc31616f2023-09-21 22:33:43 +0530106 if (IS_ENABLED(CONFIG_K3_AVS0) && !freq_scale_up)
107 k3_avs_notify_freq(clk->id, clk->data, rate);
108
Tero Kristo17708842021-06-11 11:45:10 +0300109 return rate;
Andreas Dannenberg1530e352018-08-27 15:57:43 +0530110}
111
112static int ti_sci_clk_set_parent(struct clk *clk, struct clk *parent)
113{
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;
117 u8 num_parents;
118 u8 parent_cid;
119 int ret;
120
121 debug("%s(clk=%p, parent=%p)\n", __func__, clk, parent);
122
123 /* Make sure the clock parent is valid for a given device ID */
124 if (clk->id != parent->id)
125 return -EINVAL;
126
127 /* Make sure clock has parents that can be set */
128 ret = cops->get_num_parents(sci, clk->id, clk->data, &num_parents);
129 if (ret) {
130 dev_err(clk->dev, "%s: get_num_parents failed (%d)\n",
131 __func__, ret);
132 return ret;
133 }
134 if (num_parents < 2) {
135 dev_err(clk->dev, "%s: clock has no settable parents!\n",
136 __func__);
137 return -EINVAL;
138 }
139
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__);
144 return -EINVAL;
145 }
146
147 /* Ready to proceed to configure the new clock parent */
148 ret = cops->set_parent(sci, clk->id, clk->data, parent->data);
149 if (ret)
150 dev_err(clk->dev, "%s: set_parent failed (%d)\n", __func__,
151 ret);
152
153 return ret;
154}
155
156static int ti_sci_clk_enable(struct clk *clk)
157{
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;
161 int ret;
162
163 debug("%s(clk=%p)\n", __func__, clk);
164
165 /*
166 * Allow the System Controller to automatically manage the state of
167 * this clock. If the device is enabled, then the clock is enabled.
168 */
169 ret = cops->put_clock(sci, clk->id, clk->data);
170 if (ret)
171 dev_err(clk->dev, "%s: put_clock failed (%d)\n", __func__, ret);
172
173 return ret;
174}
175
176static int ti_sci_clk_disable(struct clk *clk)
177{
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;
181 int ret;
182
183 debug("%s(clk=%p)\n", __func__, clk);
184
185 /* Unconditionally disable clock, regardless of state of the device */
186 ret = cops->idle_clock(sci, clk->id, clk->data);
187 if (ret)
188 dev_err(clk->dev, "%s: idle_clock failed (%d)\n", __func__,
189 ret);
190
191 return ret;
192}
193
194static const struct udevice_id ti_sci_clk_of_match[] = {
195 { .compatible = "ti,k2g-sci-clk" },
196 { /* sentinel */ },
197};
198
199static struct clk_ops ti_sci_clk_ops = {
200 .of_xlate = ti_sci_clk_of_xlate,
Andreas Dannenberg1530e352018-08-27 15:57:43 +0530201 .get_rate = ti_sci_clk_get_rate,
202 .set_rate = ti_sci_clk_set_rate,
203 .set_parent = ti_sci_clk_set_parent,
204 .enable = ti_sci_clk_enable,
205 .disable = ti_sci_clk_disable,
206};
207
208U_BOOT_DRIVER(ti_sci_clk) = {
209 .name = "ti-sci-clk",
210 .id = UCLASS_CLK,
211 .of_match = ti_sci_clk_of_match,
212 .probe = ti_sci_clk_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700213 .priv_auto = sizeof(struct ti_sci_clk_data),
Andreas Dannenberg1530e352018-08-27 15:57:43 +0530214 .ops = &ti_sci_clk_ops,
215};