Merge tag 'ceph-for-4.9-rc2' of git://github.com/ceph/ceph-client
[linux-2.6-block.git] / drivers / reset / core.c
CommitLineData
61fc4131
PZ
1/*
2 * Reset Controller framework
3 *
4 * Copyright 2013 Philipp Zabel, Pengutronix
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 */
0b52297f 11#include <linux/atomic.h>
61fc4131
PZ
12#include <linux/device.h>
13#include <linux/err.h>
14#include <linux/export.h>
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/of.h>
18#include <linux/reset.h>
19#include <linux/reset-controller.h>
20#include <linux/slab.h>
21
c15ddec2 22static DEFINE_MUTEX(reset_list_mutex);
61fc4131
PZ
23static LIST_HEAD(reset_controller_list);
24
25/**
26 * struct reset_control - a reset control
27 * @rcdev: a pointer to the reset controller device
28 * this reset control belongs to
c15ddec2 29 * @list: list entry for the rcdev's reset controller list
61fc4131
PZ
30 * @id: ID of the reset controller in the reset
31 * controller device
c15ddec2 32 * @refcnt: Number of gets of this reset_control
0b52297f
HG
33 * @shared: Is this a shared (1), or an exclusive (0) reset_control?
34 * @deassert_cnt: Number of times this reset line has been deasserted
61fc4131
PZ
35 */
36struct reset_control {
37 struct reset_controller_dev *rcdev;
c15ddec2 38 struct list_head list;
61fc4131 39 unsigned int id;
c15ddec2 40 unsigned int refcnt;
0b52297f
HG
41 int shared;
42 atomic_t deassert_count;
61fc4131
PZ
43};
44
45/**
46 * of_reset_simple_xlate - translate reset_spec to the reset line number
47 * @rcdev: a pointer to the reset controller device
48 * @reset_spec: reset line specifier as found in the device tree
49 * @flags: a flags pointer to fill in (optional)
50 *
51 * This simple translation function should be used for reset controllers
52 * with 1:1 mapping, where reset lines can be indexed by number without gaps.
53 */
0c5b2b91 54static int of_reset_simple_xlate(struct reset_controller_dev *rcdev,
61fc4131
PZ
55 const struct of_phandle_args *reset_spec)
56{
61fc4131
PZ
57 if (reset_spec->args[0] >= rcdev->nr_resets)
58 return -EINVAL;
59
60 return reset_spec->args[0];
61}
61fc4131
PZ
62
63/**
64 * reset_controller_register - register a reset controller device
65 * @rcdev: a pointer to the initialized reset controller device
66 */
67int reset_controller_register(struct reset_controller_dev *rcdev)
68{
69 if (!rcdev->of_xlate) {
70 rcdev->of_reset_n_cells = 1;
71 rcdev->of_xlate = of_reset_simple_xlate;
72 }
73
c15ddec2
HG
74 INIT_LIST_HEAD(&rcdev->reset_control_head);
75
76 mutex_lock(&reset_list_mutex);
61fc4131 77 list_add(&rcdev->list, &reset_controller_list);
c15ddec2 78 mutex_unlock(&reset_list_mutex);
61fc4131
PZ
79
80 return 0;
81}
82EXPORT_SYMBOL_GPL(reset_controller_register);
83
84/**
85 * reset_controller_unregister - unregister a reset controller device
86 * @rcdev: a pointer to the reset controller device
87 */
88void reset_controller_unregister(struct reset_controller_dev *rcdev)
89{
c15ddec2 90 mutex_lock(&reset_list_mutex);
61fc4131 91 list_del(&rcdev->list);
c15ddec2 92 mutex_unlock(&reset_list_mutex);
61fc4131
PZ
93}
94EXPORT_SYMBOL_GPL(reset_controller_unregister);
95
8d5b5d5c
MY
96static void devm_reset_controller_release(struct device *dev, void *res)
97{
98 reset_controller_unregister(*(struct reset_controller_dev **)res);
99}
100
101/**
102 * devm_reset_controller_register - resource managed reset_controller_register()
103 * @dev: device that is registering this reset controller
104 * @rcdev: a pointer to the initialized reset controller device
105 *
106 * Managed reset_controller_register(). For reset controllers registered by
107 * this function, reset_controller_unregister() is automatically called on
108 * driver detach. See reset_controller_register() for more information.
109 */
110int devm_reset_controller_register(struct device *dev,
111 struct reset_controller_dev *rcdev)
112{
113 struct reset_controller_dev **rcdevp;
114 int ret;
115
116 rcdevp = devres_alloc(devm_reset_controller_release, sizeof(*rcdevp),
117 GFP_KERNEL);
118 if (!rcdevp)
119 return -ENOMEM;
120
121 ret = reset_controller_register(rcdev);
122 if (!ret) {
123 *rcdevp = rcdev;
124 devres_add(dev, rcdevp);
125 } else {
126 devres_free(rcdevp);
127 }
128
129 return ret;
130}
131EXPORT_SYMBOL_GPL(devm_reset_controller_register);
132
61fc4131
PZ
133/**
134 * reset_control_reset - reset the controlled device
135 * @rstc: reset controller
0b52297f
HG
136 *
137 * Calling this on a shared reset controller is an error.
61fc4131
PZ
138 */
139int reset_control_reset(struct reset_control *rstc)
140{
a3774e14
PZ
141 if (WARN_ON(IS_ERR_OR_NULL(rstc)) ||
142 WARN_ON(rstc->shared))
0b52297f
HG
143 return -EINVAL;
144
61fc4131
PZ
145 if (rstc->rcdev->ops->reset)
146 return rstc->rcdev->ops->reset(rstc->rcdev, rstc->id);
147
39b4da71 148 return -ENOTSUPP;
61fc4131
PZ
149}
150EXPORT_SYMBOL_GPL(reset_control_reset);
151
152/**
153 * reset_control_assert - asserts the reset line
154 * @rstc: reset controller
0b52297f
HG
155 *
156 * Calling this on an exclusive reset controller guarantees that the reset
157 * will be asserted. When called on a shared reset controller the line may
158 * still be deasserted, as long as other users keep it so.
159 *
160 * For shared reset controls a driver cannot expect the hw's registers and
161 * internal state to be reset, but must be prepared for this to happen.
61fc4131
PZ
162 */
163int reset_control_assert(struct reset_control *rstc)
164{
a3774e14
PZ
165 if (WARN_ON(IS_ERR_OR_NULL(rstc)))
166 return -EINVAL;
167
0b52297f
HG
168 if (!rstc->rcdev->ops->assert)
169 return -ENOTSUPP;
61fc4131 170
0b52297f
HG
171 if (rstc->shared) {
172 if (WARN_ON(atomic_read(&rstc->deassert_count) == 0))
173 return -EINVAL;
174
175 if (atomic_dec_return(&rstc->deassert_count) != 0)
176 return 0;
177 }
178
179 return rstc->rcdev->ops->assert(rstc->rcdev, rstc->id);
61fc4131
PZ
180}
181EXPORT_SYMBOL_GPL(reset_control_assert);
182
183/**
184 * reset_control_deassert - deasserts the reset line
185 * @rstc: reset controller
0b52297f
HG
186 *
187 * After calling this function, the reset is guaranteed to be deasserted.
61fc4131
PZ
188 */
189int reset_control_deassert(struct reset_control *rstc)
190{
a3774e14
PZ
191 if (WARN_ON(IS_ERR_OR_NULL(rstc)))
192 return -EINVAL;
193
0b52297f
HG
194 if (!rstc->rcdev->ops->deassert)
195 return -ENOTSUPP;
61fc4131 196
0b52297f
HG
197 if (rstc->shared) {
198 if (atomic_inc_return(&rstc->deassert_count) != 1)
199 return 0;
200 }
201
202 return rstc->rcdev->ops->deassert(rstc->rcdev, rstc->id);
61fc4131
PZ
203}
204EXPORT_SYMBOL_GPL(reset_control_deassert);
205
729de41b
DN
206/**
207 * reset_control_status - returns a negative errno if not supported, a
208 * positive value if the reset line is asserted, or zero if the reset
209 * line is not asserted.
210 * @rstc: reset controller
211 */
212int reset_control_status(struct reset_control *rstc)
213{
a3774e14
PZ
214 if (WARN_ON(IS_ERR_OR_NULL(rstc)))
215 return -EINVAL;
216
729de41b
DN
217 if (rstc->rcdev->ops->status)
218 return rstc->rcdev->ops->status(rstc->rcdev, rstc->id);
219
39b4da71 220 return -ENOTSUPP;
729de41b
DN
221}
222EXPORT_SYMBOL_GPL(reset_control_status);
223
c15ddec2
HG
224static struct reset_control *__reset_control_get(
225 struct reset_controller_dev *rcdev,
0b52297f 226 unsigned int index, int shared)
c15ddec2
HG
227{
228 struct reset_control *rstc;
229
230 lockdep_assert_held(&reset_list_mutex);
231
232 list_for_each_entry(rstc, &rcdev->reset_control_head, list) {
233 if (rstc->id == index) {
0b52297f
HG
234 if (WARN_ON(!rstc->shared || !shared))
235 return ERR_PTR(-EBUSY);
236
c15ddec2
HG
237 rstc->refcnt++;
238 return rstc;
239 }
240 }
241
242 rstc = kzalloc(sizeof(*rstc), GFP_KERNEL);
243 if (!rstc)
244 return ERR_PTR(-ENOMEM);
245
246 try_module_get(rcdev->owner);
247
248 rstc->rcdev = rcdev;
249 list_add(&rstc->list, &rcdev->reset_control_head);
250 rstc->id = index;
251 rstc->refcnt = 1;
0b52297f 252 rstc->shared = shared;
c15ddec2
HG
253
254 return rstc;
255}
256
257static void __reset_control_put(struct reset_control *rstc)
258{
259 lockdep_assert_held(&reset_list_mutex);
260
261 if (--rstc->refcnt)
262 return;
263
264 module_put(rstc->rcdev->owner);
265
266 list_del(&rstc->list);
267 kfree(rstc);
268}
269
6c96f05c 270struct reset_control *__of_reset_control_get(struct device_node *node,
0b52297f 271 const char *id, int index, int shared)
61fc4131 272{
d056c9b8 273 struct reset_control *rstc;
61fc4131
PZ
274 struct reset_controller_dev *r, *rcdev;
275 struct of_phandle_args args;
61fc4131
PZ
276 int rstc_id;
277 int ret;
278
6c96f05c
HG
279 if (!node)
280 return ERR_PTR(-EINVAL);
281
282 if (id) {
283 index = of_property_match_string(node,
284 "reset-names", id);
285 if (index < 0)
286 return ERR_PTR(-ENOENT);
287 }
288
fc0a5921 289 ret = of_parse_phandle_with_args(node, "resets", "#reset-cells",
61fc4131
PZ
290 index, &args);
291 if (ret)
292 return ERR_PTR(ret);
293
c15ddec2 294 mutex_lock(&reset_list_mutex);
61fc4131
PZ
295 rcdev = NULL;
296 list_for_each_entry(r, &reset_controller_list, list) {
297 if (args.np == r->of_node) {
298 rcdev = r;
299 break;
300 }
301 }
302 of_node_put(args.np);
303
304 if (!rcdev) {
c15ddec2 305 mutex_unlock(&reset_list_mutex);
3d103020 306 return ERR_PTR(-EPROBE_DEFER);
61fc4131
PZ
307 }
308
e677774f 309 if (WARN_ON(args.args_count != rcdev->of_reset_n_cells)) {
c15ddec2 310 mutex_unlock(&reset_list_mutex);
e677774f
MR
311 return ERR_PTR(-EINVAL);
312 }
313
61fc4131
PZ
314 rstc_id = rcdev->of_xlate(rcdev, &args);
315 if (rstc_id < 0) {
c15ddec2 316 mutex_unlock(&reset_list_mutex);
61fc4131
PZ
317 return ERR_PTR(rstc_id);
318 }
319
c15ddec2 320 /* reset_list_mutex also protects the rcdev's reset_control list */
0b52297f 321 rstc = __reset_control_get(rcdev, rstc_id, shared);
61fc4131 322
c15ddec2 323 mutex_unlock(&reset_list_mutex);
61fc4131
PZ
324
325 return rstc;
326}
6c96f05c 327EXPORT_SYMBOL_GPL(__of_reset_control_get);
61fc4131
PZ
328
329/**
330 * reset_control_put - free the reset controller
331 * @rstc: reset controller
332 */
333
334void reset_control_put(struct reset_control *rstc)
335{
336 if (IS_ERR(rstc))
337 return;
338
c15ddec2
HG
339 mutex_lock(&reset_list_mutex);
340 __reset_control_put(rstc);
341 mutex_unlock(&reset_list_mutex);
61fc4131
PZ
342}
343EXPORT_SYMBOL_GPL(reset_control_put);
344
345static void devm_reset_control_release(struct device *dev, void *res)
346{
347 reset_control_put(*(struct reset_control **)res);
348}
349
6c96f05c 350struct reset_control *__devm_reset_control_get(struct device *dev,
0b52297f 351 const char *id, int index, int shared)
61fc4131
PZ
352{
353 struct reset_control **ptr, *rstc;
354
355 ptr = devres_alloc(devm_reset_control_release, sizeof(*ptr),
356 GFP_KERNEL);
357 if (!ptr)
358 return ERR_PTR(-ENOMEM);
359
0b52297f
HG
360 rstc = __of_reset_control_get(dev ? dev->of_node : NULL,
361 id, index, shared);
61fc4131
PZ
362 if (!IS_ERR(rstc)) {
363 *ptr = rstc;
364 devres_add(dev, ptr);
365 } else {
366 devres_free(ptr);
367 }
368
369 return rstc;
370}
6c96f05c 371EXPORT_SYMBOL_GPL(__devm_reset_control_get);
61fc4131 372
61fc4131
PZ
373/**
374 * device_reset - find reset controller associated with the device
375 * and perform reset
376 * @dev: device to be reset by the controller
377 *
378 * Convenience wrapper for reset_control_get() and reset_control_reset().
379 * This is useful for the common case of devices with single, dedicated reset
380 * lines.
381 */
382int device_reset(struct device *dev)
383{
384 struct reset_control *rstc;
385 int ret;
386
387 rstc = reset_control_get(dev, NULL);
388 if (IS_ERR(rstc))
389 return PTR_ERR(rstc);
390
391 ret = reset_control_reset(rstc);
392
393 reset_control_put(rstc);
394
395 return ret;
396}
397EXPORT_SYMBOL_GPL(device_reset);