1
2
3
4
5
6
7
8
9
10
11#include <linux/device.h>
12#include <linux/err.h>
13#include <linux/export.h>
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/of.h>
17#include <linux/reset.h>
18#include <linux/reset-controller.h>
19#include <linux/slab.h>
20
21static DEFINE_MUTEX(reset_controller_list_mutex);
22static LIST_HEAD(reset_controller_list);
23
24
25
26
27
28
29
30
31struct reset_control {
32 struct reset_controller_dev *rcdev;
33 struct device *dev;
34 unsigned int id;
35};
36
37
38
39
40
41
42
43
44
45
46static int of_reset_simple_xlate(struct reset_controller_dev *rcdev,
47 const struct of_phandle_args *reset_spec)
48{
49 if (WARN_ON(reset_spec->args_count != rcdev->of_reset_n_cells))
50 return -EINVAL;
51
52 if (reset_spec->args[0] >= rcdev->nr_resets)
53 return -EINVAL;
54
55 return reset_spec->args[0];
56}
57
58
59
60
61
62int reset_controller_register(struct reset_controller_dev *rcdev)
63{
64 if (!rcdev->of_xlate) {
65 rcdev->of_reset_n_cells = 1;
66 rcdev->of_xlate = of_reset_simple_xlate;
67 }
68
69 mutex_lock(&reset_controller_list_mutex);
70 list_add(&rcdev->list, &reset_controller_list);
71 mutex_unlock(&reset_controller_list_mutex);
72
73 return 0;
74}
75EXPORT_SYMBOL_GPL(reset_controller_register);
76
77
78
79
80
81void reset_controller_unregister(struct reset_controller_dev *rcdev)
82{
83 mutex_lock(&reset_controller_list_mutex);
84 list_del(&rcdev->list);
85 mutex_unlock(&reset_controller_list_mutex);
86}
87EXPORT_SYMBOL_GPL(reset_controller_unregister);
88
89
90
91
92
93int reset_control_reset(struct reset_control *rstc)
94{
95 if (rstc->rcdev->ops->reset)
96 return rstc->rcdev->ops->reset(rstc->rcdev, rstc->id);
97
98 return -ENOSYS;
99}
100EXPORT_SYMBOL_GPL(reset_control_reset);
101
102
103
104
105
106int reset_control_assert(struct reset_control *rstc)
107{
108 if (rstc->rcdev->ops->assert)
109 return rstc->rcdev->ops->assert(rstc->rcdev, rstc->id);
110
111 return -ENOSYS;
112}
113EXPORT_SYMBOL_GPL(reset_control_assert);
114
115
116
117
118
119int reset_control_deassert(struct reset_control *rstc)
120{
121 if (rstc->rcdev->ops->deassert)
122 return rstc->rcdev->ops->deassert(rstc->rcdev, rstc->id);
123
124 return -ENOSYS;
125}
126EXPORT_SYMBOL_GPL(reset_control_deassert);
127
128
129
130
131
132
133
134int reset_control_status(struct reset_control *rstc)
135{
136 if (rstc->rcdev->ops->status)
137 return rstc->rcdev->ops->status(rstc->rcdev, rstc->id);
138
139 return -ENOSYS;
140}
141EXPORT_SYMBOL_GPL(reset_control_status);
142
143
144
145
146
147
148
149
150
151
152struct reset_control *of_reset_control_get(struct device_node *node,
153 const char *id)
154{
155 struct reset_control *rstc = ERR_PTR(-EPROBE_DEFER);
156 struct reset_controller_dev *r, *rcdev;
157 struct of_phandle_args args;
158 int index = 0;
159 int rstc_id;
160 int ret;
161
162 if (id)
163 index = of_property_match_string(node,
164 "reset-names", id);
165 ret = of_parse_phandle_with_args(node, "resets", "#reset-cells",
166 index, &args);
167 if (ret)
168 return ERR_PTR(ret);
169
170 mutex_lock(&reset_controller_list_mutex);
171 rcdev = NULL;
172 list_for_each_entry(r, &reset_controller_list, list) {
173 if (args.np == r->of_node) {
174 rcdev = r;
175 break;
176 }
177 }
178 of_node_put(args.np);
179
180 if (!rcdev) {
181 mutex_unlock(&reset_controller_list_mutex);
182 return ERR_PTR(-EPROBE_DEFER);
183 }
184
185 rstc_id = rcdev->of_xlate(rcdev, &args);
186 if (rstc_id < 0) {
187 mutex_unlock(&reset_controller_list_mutex);
188 return ERR_PTR(rstc_id);
189 }
190
191 try_module_get(rcdev->owner);
192 mutex_unlock(&reset_controller_list_mutex);
193
194 rstc = kzalloc(sizeof(*rstc), GFP_KERNEL);
195 if (!rstc) {
196 module_put(rcdev->owner);
197 return ERR_PTR(-ENOMEM);
198 }
199
200 rstc->rcdev = rcdev;
201 rstc->id = rstc_id;
202
203 return rstc;
204}
205EXPORT_SYMBOL_GPL(of_reset_control_get);
206
207
208
209
210
211
212
213
214
215
216struct reset_control *reset_control_get(struct device *dev, const char *id)
217{
218 struct reset_control *rstc;
219
220 if (!dev)
221 return ERR_PTR(-EINVAL);
222
223 rstc = of_reset_control_get(dev->of_node, id);
224 if (!IS_ERR(rstc))
225 rstc->dev = dev;
226
227 return rstc;
228}
229EXPORT_SYMBOL_GPL(reset_control_get);
230
231
232
233
234
235
236void reset_control_put(struct reset_control *rstc)
237{
238 if (IS_ERR(rstc))
239 return;
240
241 module_put(rstc->rcdev->owner);
242 kfree(rstc);
243}
244EXPORT_SYMBOL_GPL(reset_control_put);
245
246static void devm_reset_control_release(struct device *dev, void *res)
247{
248 reset_control_put(*(struct reset_control **)res);
249}
250
251
252
253
254
255
256
257
258
259
260struct reset_control *devm_reset_control_get(struct device *dev, const char *id)
261{
262 struct reset_control **ptr, *rstc;
263
264 ptr = devres_alloc(devm_reset_control_release, sizeof(*ptr),
265 GFP_KERNEL);
266 if (!ptr)
267 return ERR_PTR(-ENOMEM);
268
269 rstc = reset_control_get(dev, id);
270 if (!IS_ERR(rstc)) {
271 *ptr = rstc;
272 devres_add(dev, ptr);
273 } else {
274 devres_free(ptr);
275 }
276
277 return rstc;
278}
279EXPORT_SYMBOL_GPL(devm_reset_control_get);
280
281
282
283
284
285
286
287
288
289
290int device_reset(struct device *dev)
291{
292 struct reset_control *rstc;
293 int ret;
294
295 rstc = reset_control_get(dev, NULL);
296 if (IS_ERR(rstc))
297 return PTR_ERR(rstc);
298
299 ret = reset_control_reset(rstc);
300
301 reset_control_put(rstc);
302
303 return ret;
304}
305EXPORT_SYMBOL_GPL(device_reset);
306