Merge tag 'arc-last-minute-fixes-for-2020.04' of https://gitlab.denx.de/u-boot/custod...
[oweals/u-boot.git] / drivers / clk / clk-ti-sci.c
1 // 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 <malloc.h>
16 #include <dm/device_compat.h>
17 #include <linux/err.h>
18 #include <linux/soc/ti/ti_sci_protocol.h>
19 #include <k3-avs.h>
20
21 /**
22  * struct ti_sci_clk_data - clock controller information structure
23  * @sci: TI SCI handle used for communication with system controller
24  */
25 struct ti_sci_clk_data {
26         const struct ti_sci_handle *sci;
27 };
28
29 static 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
46 static 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
66 static int ti_sci_clk_request(struct clk *clk)
67 {
68         debug("%s(clk=%p)\n", __func__, clk);
69         return 0;
70 }
71
72 static int ti_sci_clk_free(struct clk *clk)
73 {
74         debug("%s(clk=%p)\n", __func__, clk);
75         return 0;
76 }
77
78 static ulong ti_sci_clk_get_rate(struct clk *clk)
79 {
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;
83         u64 current_freq;
84         int ret;
85
86         debug("%s(clk=%p)\n", __func__, clk);
87
88         ret = cops->get_freq(sci, clk->id, clk->data, &current_freq);
89         if (ret) {
90                 dev_err(clk->dev, "%s: get_freq failed (%d)\n", __func__, ret);
91                 return ret;
92         }
93
94         debug("%s(current_freq=%llu)\n", __func__, current_freq);
95
96         return current_freq;
97 }
98
99 static ulong ti_sci_clk_set_rate(struct clk *clk, ulong rate)
100 {
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;
104         int ret;
105
106         debug("%s(clk=%p, rate=%lu)\n", __func__, clk, rate);
107
108 #ifdef CONFIG_K3_AVS0
109         k3_avs_notify_freq(clk->id, clk->data, rate);
110 #endif
111
112         ret = cops->set_freq(sci, clk->id, clk->data, 0, rate, ULONG_MAX);
113         if (ret)
114                 dev_err(clk->dev, "%s: set_freq failed (%d)\n", __func__, ret);
115
116         return ret;
117 }
118
119 static int ti_sci_clk_set_parent(struct clk *clk, struct clk *parent)
120 {
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;
124         u8 num_parents;
125         u8 parent_cid;
126         int ret;
127
128         debug("%s(clk=%p, parent=%p)\n", __func__, clk, parent);
129
130         /* Make sure the clock parent is valid for a given device ID */
131         if (clk->id != parent->id)
132                 return -EINVAL;
133
134         /* Make sure clock has parents that can be set */
135         ret = cops->get_num_parents(sci, clk->id, clk->data, &num_parents);
136         if (ret) {
137                 dev_err(clk->dev, "%s: get_num_parents failed (%d)\n",
138                         __func__, ret);
139                 return ret;
140         }
141         if (num_parents < 2) {
142                 dev_err(clk->dev, "%s: clock has no settable parents!\n",
143                         __func__);
144                 return -EINVAL;
145         }
146
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__);
151                 return -EINVAL;
152         }
153
154         /* Ready to proceed to configure the new clock parent */
155         ret = cops->set_parent(sci, clk->id, clk->data, parent->data);
156         if (ret)
157                 dev_err(clk->dev, "%s: set_parent failed (%d)\n", __func__,
158                         ret);
159
160         return ret;
161 }
162
163 static int ti_sci_clk_enable(struct clk *clk)
164 {
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;
168         int ret;
169
170         debug("%s(clk=%p)\n", __func__, clk);
171
172         /*
173          * Allow the System Controller to automatically manage the state of
174          * this clock. If the device is enabled, then the clock is enabled.
175          */
176         ret = cops->put_clock(sci, clk->id, clk->data);
177         if (ret)
178                 dev_err(clk->dev, "%s: put_clock failed (%d)\n", __func__, ret);
179
180         return ret;
181 }
182
183 static int ti_sci_clk_disable(struct clk *clk)
184 {
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;
188         int ret;
189
190         debug("%s(clk=%p)\n", __func__, clk);
191
192         /* Unconditionally disable clock, regardless of state of the device */
193         ret = cops->idle_clock(sci, clk->id, clk->data);
194         if (ret)
195                 dev_err(clk->dev, "%s: idle_clock failed (%d)\n", __func__,
196                         ret);
197
198         return ret;
199 }
200
201 static const struct udevice_id ti_sci_clk_of_match[] = {
202         { .compatible = "ti,k2g-sci-clk" },
203         { /* sentinel */ },
204 };
205
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,
215 };
216
217 U_BOOT_DRIVER(ti_sci_clk) = {
218         .name = "ti-sci-clk",
219         .id = UCLASS_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,
224 };