enic: update enic maintainers
[linux-2.6/btrfs-unstable.git] / drivers / reset / core.c
blobf1e5e65388bb525b186f9257794afcf3564d2c3f
1 /*
2 * Reset Controller framework
4 * Copyright 2013 Philipp Zabel, Pengutronix
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.
11 #include <linux/atomic.h>
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>
22 static DEFINE_MUTEX(reset_list_mutex);
23 static LIST_HEAD(reset_controller_list);
25 /**
26 * struct reset_control - a reset control
27 * @rcdev: a pointer to the reset controller device
28 * this reset control belongs to
29 * @list: list entry for the rcdev's reset controller list
30 * @id: ID of the reset controller in the reset
31 * controller device
32 * @refcnt: Number of gets of this reset_control
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
35 * @triggered_count: Number of times this reset line has been reset. Currently
36 * only used for shared resets, which means that the value
37 * will be either 0 or 1.
39 struct reset_control {
40 struct reset_controller_dev *rcdev;
41 struct list_head list;
42 unsigned int id;
43 unsigned int refcnt;
44 bool shared;
45 atomic_t deassert_count;
46 atomic_t triggered_count;
49 /**
50 * of_reset_simple_xlate - translate reset_spec to the reset line number
51 * @rcdev: a pointer to the reset controller device
52 * @reset_spec: reset line specifier as found in the device tree
53 * @flags: a flags pointer to fill in (optional)
55 * This simple translation function should be used for reset controllers
56 * with 1:1 mapping, where reset lines can be indexed by number without gaps.
58 static int of_reset_simple_xlate(struct reset_controller_dev *rcdev,
59 const struct of_phandle_args *reset_spec)
61 if (reset_spec->args[0] >= rcdev->nr_resets)
62 return -EINVAL;
64 return reset_spec->args[0];
67 /**
68 * reset_controller_register - register a reset controller device
69 * @rcdev: a pointer to the initialized reset controller device
71 int reset_controller_register(struct reset_controller_dev *rcdev)
73 if (!rcdev->of_xlate) {
74 rcdev->of_reset_n_cells = 1;
75 rcdev->of_xlate = of_reset_simple_xlate;
78 INIT_LIST_HEAD(&rcdev->reset_control_head);
80 mutex_lock(&reset_list_mutex);
81 list_add(&rcdev->list, &reset_controller_list);
82 mutex_unlock(&reset_list_mutex);
84 return 0;
86 EXPORT_SYMBOL_GPL(reset_controller_register);
88 /**
89 * reset_controller_unregister - unregister a reset controller device
90 * @rcdev: a pointer to the reset controller device
92 void reset_controller_unregister(struct reset_controller_dev *rcdev)
94 mutex_lock(&reset_list_mutex);
95 list_del(&rcdev->list);
96 mutex_unlock(&reset_list_mutex);
98 EXPORT_SYMBOL_GPL(reset_controller_unregister);
100 static void devm_reset_controller_release(struct device *dev, void *res)
102 reset_controller_unregister(*(struct reset_controller_dev **)res);
106 * devm_reset_controller_register - resource managed reset_controller_register()
107 * @dev: device that is registering this reset controller
108 * @rcdev: a pointer to the initialized reset controller device
110 * Managed reset_controller_register(). For reset controllers registered by
111 * this function, reset_controller_unregister() is automatically called on
112 * driver detach. See reset_controller_register() for more information.
114 int devm_reset_controller_register(struct device *dev,
115 struct reset_controller_dev *rcdev)
117 struct reset_controller_dev **rcdevp;
118 int ret;
120 rcdevp = devres_alloc(devm_reset_controller_release, sizeof(*rcdevp),
121 GFP_KERNEL);
122 if (!rcdevp)
123 return -ENOMEM;
125 ret = reset_controller_register(rcdev);
126 if (!ret) {
127 *rcdevp = rcdev;
128 devres_add(dev, rcdevp);
129 } else {
130 devres_free(rcdevp);
133 return ret;
135 EXPORT_SYMBOL_GPL(devm_reset_controller_register);
138 * reset_control_reset - reset the controlled device
139 * @rstc: reset controller
141 * On a shared reset line the actual reset pulse is only triggered once for the
142 * lifetime of the reset_control instance: for all but the first caller this is
143 * a no-op.
144 * Consumers must not use reset_control_(de)assert on shared reset lines when
145 * reset_control_reset has been used.
147 * If rstc is NULL it is an optional reset and the function will just
148 * return 0.
150 int reset_control_reset(struct reset_control *rstc)
152 int ret;
154 if (!rstc)
155 return 0;
157 if (WARN_ON(IS_ERR(rstc)))
158 return -EINVAL;
160 if (!rstc->rcdev->ops->reset)
161 return -ENOTSUPP;
163 if (rstc->shared) {
164 if (WARN_ON(atomic_read(&rstc->deassert_count) != 0))
165 return -EINVAL;
167 if (atomic_inc_return(&rstc->triggered_count) != 1)
168 return 0;
171 ret = rstc->rcdev->ops->reset(rstc->rcdev, rstc->id);
172 if (rstc->shared && ret)
173 atomic_dec(&rstc->triggered_count);
175 return ret;
177 EXPORT_SYMBOL_GPL(reset_control_reset);
180 * reset_control_assert - asserts the reset line
181 * @rstc: reset controller
183 * Calling this on an exclusive reset controller guarantees that the reset
184 * will be asserted. When called on a shared reset controller the line may
185 * still be deasserted, as long as other users keep it so.
187 * For shared reset controls a driver cannot expect the hw's registers and
188 * internal state to be reset, but must be prepared for this to happen.
189 * Consumers must not use reset_control_reset on shared reset lines when
190 * reset_control_(de)assert has been used.
191 * return 0.
193 * If rstc is NULL it is an optional reset and the function will just
194 * return 0.
196 int reset_control_assert(struct reset_control *rstc)
198 if (!rstc)
199 return 0;
201 if (WARN_ON(IS_ERR(rstc)))
202 return -EINVAL;
204 if (!rstc->rcdev->ops->assert)
205 return -ENOTSUPP;
207 if (rstc->shared) {
208 if (WARN_ON(atomic_read(&rstc->triggered_count) != 0))
209 return -EINVAL;
211 if (WARN_ON(atomic_read(&rstc->deassert_count) == 0))
212 return -EINVAL;
214 if (atomic_dec_return(&rstc->deassert_count) != 0)
215 return 0;
218 return rstc->rcdev->ops->assert(rstc->rcdev, rstc->id);
220 EXPORT_SYMBOL_GPL(reset_control_assert);
223 * reset_control_deassert - deasserts the reset line
224 * @rstc: reset controller
226 * After calling this function, the reset is guaranteed to be deasserted.
227 * Consumers must not use reset_control_reset on shared reset lines when
228 * reset_control_(de)assert has been used.
229 * return 0.
231 * If rstc is NULL it is an optional reset and the function will just
232 * return 0.
234 int reset_control_deassert(struct reset_control *rstc)
236 if (!rstc)
237 return 0;
239 if (WARN_ON(IS_ERR(rstc)))
240 return -EINVAL;
242 if (!rstc->rcdev->ops->deassert)
243 return -ENOTSUPP;
245 if (rstc->shared) {
246 if (WARN_ON(atomic_read(&rstc->triggered_count) != 0))
247 return -EINVAL;
249 if (atomic_inc_return(&rstc->deassert_count) != 1)
250 return 0;
253 return rstc->rcdev->ops->deassert(rstc->rcdev, rstc->id);
255 EXPORT_SYMBOL_GPL(reset_control_deassert);
258 * reset_control_status - returns a negative errno if not supported, a
259 * positive value if the reset line is asserted, or zero if the reset
260 * line is not asserted or if the desc is NULL (optional reset).
261 * @rstc: reset controller
263 int reset_control_status(struct reset_control *rstc)
265 if (!rstc)
266 return 0;
268 if (WARN_ON(IS_ERR(rstc)))
269 return -EINVAL;
271 if (rstc->rcdev->ops->status)
272 return rstc->rcdev->ops->status(rstc->rcdev, rstc->id);
274 return -ENOTSUPP;
276 EXPORT_SYMBOL_GPL(reset_control_status);
278 static struct reset_control *__reset_control_get(
279 struct reset_controller_dev *rcdev,
280 unsigned int index, bool shared)
282 struct reset_control *rstc;
284 lockdep_assert_held(&reset_list_mutex);
286 list_for_each_entry(rstc, &rcdev->reset_control_head, list) {
287 if (rstc->id == index) {
288 if (WARN_ON(!rstc->shared || !shared))
289 return ERR_PTR(-EBUSY);
291 rstc->refcnt++;
292 return rstc;
296 rstc = kzalloc(sizeof(*rstc), GFP_KERNEL);
297 if (!rstc)
298 return ERR_PTR(-ENOMEM);
300 try_module_get(rcdev->owner);
302 rstc->rcdev = rcdev;
303 list_add(&rstc->list, &rcdev->reset_control_head);
304 rstc->id = index;
305 rstc->refcnt = 1;
306 rstc->shared = shared;
308 return rstc;
311 static void __reset_control_put(struct reset_control *rstc)
313 lockdep_assert_held(&reset_list_mutex);
315 if (--rstc->refcnt)
316 return;
318 module_put(rstc->rcdev->owner);
320 list_del(&rstc->list);
321 kfree(rstc);
324 struct reset_control *__of_reset_control_get(struct device_node *node,
325 const char *id, int index, bool shared,
326 bool optional)
328 struct reset_control *rstc;
329 struct reset_controller_dev *r, *rcdev;
330 struct of_phandle_args args;
331 int rstc_id;
332 int ret;
334 if (!node)
335 return ERR_PTR(-EINVAL);
337 if (id) {
338 index = of_property_match_string(node,
339 "reset-names", id);
340 if (index == -EILSEQ)
341 return ERR_PTR(index);
342 if (index < 0)
343 return optional ? NULL : ERR_PTR(-ENOENT);
346 ret = of_parse_phandle_with_args(node, "resets", "#reset-cells",
347 index, &args);
348 if (ret == -EINVAL)
349 return ERR_PTR(ret);
350 if (ret)
351 return optional ? NULL : ERR_PTR(ret);
353 mutex_lock(&reset_list_mutex);
354 rcdev = NULL;
355 list_for_each_entry(r, &reset_controller_list, list) {
356 if (args.np == r->of_node) {
357 rcdev = r;
358 break;
361 of_node_put(args.np);
363 if (!rcdev) {
364 mutex_unlock(&reset_list_mutex);
365 return ERR_PTR(-EPROBE_DEFER);
368 if (WARN_ON(args.args_count != rcdev->of_reset_n_cells)) {
369 mutex_unlock(&reset_list_mutex);
370 return ERR_PTR(-EINVAL);
373 rstc_id = rcdev->of_xlate(rcdev, &args);
374 if (rstc_id < 0) {
375 mutex_unlock(&reset_list_mutex);
376 return ERR_PTR(rstc_id);
379 /* reset_list_mutex also protects the rcdev's reset_control list */
380 rstc = __reset_control_get(rcdev, rstc_id, shared);
382 mutex_unlock(&reset_list_mutex);
384 return rstc;
386 EXPORT_SYMBOL_GPL(__of_reset_control_get);
389 * reset_control_put - free the reset controller
390 * @rstc: reset controller
393 void reset_control_put(struct reset_control *rstc)
395 if (IS_ERR_OR_NULL(rstc))
396 return;
398 mutex_lock(&reset_list_mutex);
399 __reset_control_put(rstc);
400 mutex_unlock(&reset_list_mutex);
402 EXPORT_SYMBOL_GPL(reset_control_put);
404 static void devm_reset_control_release(struct device *dev, void *res)
406 reset_control_put(*(struct reset_control **)res);
409 struct reset_control *__devm_reset_control_get(struct device *dev,
410 const char *id, int index, bool shared,
411 bool optional)
413 struct reset_control **ptr, *rstc;
415 ptr = devres_alloc(devm_reset_control_release, sizeof(*ptr),
416 GFP_KERNEL);
417 if (!ptr)
418 return ERR_PTR(-ENOMEM);
420 rstc = __of_reset_control_get(dev ? dev->of_node : NULL,
421 id, index, shared, optional);
422 if (!IS_ERR(rstc)) {
423 *ptr = rstc;
424 devres_add(dev, ptr);
425 } else {
426 devres_free(ptr);
429 return rstc;
431 EXPORT_SYMBOL_GPL(__devm_reset_control_get);
434 * device_reset - find reset controller associated with the device
435 * and perform reset
436 * @dev: device to be reset by the controller
438 * Convenience wrapper for reset_control_get() and reset_control_reset().
439 * This is useful for the common case of devices with single, dedicated reset
440 * lines.
442 int device_reset(struct device *dev)
444 struct reset_control *rstc;
445 int ret;
447 rstc = reset_control_get(dev, NULL);
448 if (IS_ERR(rstc))
449 return PTR_ERR(rstc);
451 ret = reset_control_reset(rstc);
453 reset_control_put(rstc);
455 return ret;
457 EXPORT_SYMBOL_GPL(device_reset);