1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#include <linux/idr.h>
18#include <linux/module.h>
19#include <linux/mutex.h>
20#include <linux/of.h>
21#include <linux/platform_device.h>
22#include <linux/reset-controller.h>
23#include <linux/soc/ti/ti_sci_protocol.h>
24
25
26
27
28
29
30
31struct ti_sci_reset_control {
32 u32 dev_id;
33 u32 reset_mask;
34 struct mutex lock;
35};
36
37
38
39
40
41
42
43
44struct ti_sci_reset_data {
45 struct reset_controller_dev rcdev;
46 struct device *dev;
47 const struct ti_sci_handle *sci;
48 struct idr idr;
49};
50
51#define to_ti_sci_reset_data(p) \
52 container_of((p), struct ti_sci_reset_data, rcdev)
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70static int ti_sci_reset_set(struct reset_controller_dev *rcdev,
71 unsigned long id, bool assert)
72{
73 struct ti_sci_reset_data *data = to_ti_sci_reset_data(rcdev);
74 const struct ti_sci_handle *sci = data->sci;
75 const struct ti_sci_dev_ops *dev_ops = &sci->ops.dev_ops;
76 struct ti_sci_reset_control *control;
77 u32 reset_state;
78 int ret;
79
80 control = idr_find(&data->idr, id);
81 if (!control)
82 return -EINVAL;
83
84 mutex_lock(&control->lock);
85
86 ret = dev_ops->get_device_resets(sci, control->dev_id, &reset_state);
87 if (ret)
88 goto out;
89
90 if (assert)
91 reset_state |= control->reset_mask;
92 else
93 reset_state &= ~control->reset_mask;
94
95 ret = dev_ops->set_device_resets(sci, control->dev_id, reset_state);
96out:
97 mutex_unlock(&control->lock);
98
99 return ret;
100}
101
102
103
104
105
106
107
108
109
110
111
112
113
114static int ti_sci_reset_assert(struct reset_controller_dev *rcdev,
115 unsigned long id)
116{
117 return ti_sci_reset_set(rcdev, id, true);
118}
119
120
121
122
123
124
125
126
127
128
129
130
131
132static int ti_sci_reset_deassert(struct reset_controller_dev *rcdev,
133 unsigned long id)
134{
135 return ti_sci_reset_set(rcdev, id, false);
136}
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151static int ti_sci_reset_status(struct reset_controller_dev *rcdev,
152 unsigned long id)
153{
154 struct ti_sci_reset_data *data = to_ti_sci_reset_data(rcdev);
155 const struct ti_sci_handle *sci = data->sci;
156 const struct ti_sci_dev_ops *dev_ops = &sci->ops.dev_ops;
157 struct ti_sci_reset_control *control;
158 u32 reset_state;
159 int ret;
160
161 control = idr_find(&data->idr, id);
162 if (!control)
163 return -EINVAL;
164
165 ret = dev_ops->get_device_resets(sci, control->dev_id, &reset_state);
166 if (ret)
167 return ret;
168
169 return reset_state & control->reset_mask;
170}
171
172static const struct reset_control_ops ti_sci_reset_ops = {
173 .assert = ti_sci_reset_assert,
174 .deassert = ti_sci_reset_deassert,
175 .status = ti_sci_reset_status,
176};
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192static int ti_sci_reset_of_xlate(struct reset_controller_dev *rcdev,
193 const struct of_phandle_args *reset_spec)
194{
195 struct ti_sci_reset_data *data = to_ti_sci_reset_data(rcdev);
196 struct ti_sci_reset_control *control;
197
198 if (WARN_ON(reset_spec->args_count != rcdev->of_reset_n_cells))
199 return -EINVAL;
200
201 control = devm_kzalloc(data->dev, sizeof(*control), GFP_KERNEL);
202 if (!control)
203 return -ENOMEM;
204
205 control->dev_id = reset_spec->args[0];
206 control->reset_mask = reset_spec->args[1];
207 mutex_init(&control->lock);
208
209 return idr_alloc(&data->idr, control, 0, 0, GFP_KERNEL);
210}
211
212static const struct of_device_id ti_sci_reset_of_match[] = {
213 { .compatible = "ti,sci-reset", },
214 { },
215};
216MODULE_DEVICE_TABLE(of, ti_sci_reset_of_match);
217
218static int ti_sci_reset_probe(struct platform_device *pdev)
219{
220 struct ti_sci_reset_data *data;
221
222 if (!pdev->dev.of_node)
223 return -ENODEV;
224
225 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
226 if (!data)
227 return -ENOMEM;
228
229 data->sci = devm_ti_sci_get_handle(&pdev->dev);
230 if (IS_ERR(data->sci))
231 return PTR_ERR(data->sci);
232
233 data->rcdev.ops = &ti_sci_reset_ops;
234 data->rcdev.owner = THIS_MODULE;
235 data->rcdev.of_node = pdev->dev.of_node;
236 data->rcdev.of_reset_n_cells = 2;
237 data->rcdev.of_xlate = ti_sci_reset_of_xlate;
238 data->dev = &pdev->dev;
239 idr_init(&data->idr);
240
241 platform_set_drvdata(pdev, data);
242
243 return reset_controller_register(&data->rcdev);
244}
245
246static int ti_sci_reset_remove(struct platform_device *pdev)
247{
248 struct ti_sci_reset_data *data = platform_get_drvdata(pdev);
249
250 reset_controller_unregister(&data->rcdev);
251
252 idr_destroy(&data->idr);
253
254 return 0;
255}
256
257static struct platform_driver ti_sci_reset_driver = {
258 .probe = ti_sci_reset_probe,
259 .remove = ti_sci_reset_remove,
260 .driver = {
261 .name = "ti-sci-reset",
262 .of_match_table = ti_sci_reset_of_match,
263 },
264};
265module_platform_driver(ti_sci_reset_driver);
266
267MODULE_AUTHOR("Andrew F. Davis <afd@ti.com>");
268MODULE_DESCRIPTION("TI System Control Interface (TI SCI) Reset driver");
269MODULE_LICENSE("GPL v2");
270