1 From 8951e08252c6c254d68c350468c40f88a12bcc1d Mon Sep 17 00:00:00 2001
2 From: John Crispin <blogic@openwrt.org>
3 Date: Mon, 20 May 2013 15:42:01 +0200
4 Subject: [PATCH 67/79] reset: Add reset controller API
6 backport from v3.10-rc1
7 61fc41317666be400802ac793f47de816ef7bd57
8 6034bb22d8387708075c083385e5d2e1072a4f33
9 4e11f848c65b1c87782cb232a6e3b47a9d4c1f98
11 This adds a simple API for devices to request being reset
12 by separate reset controller hardware and implements the
13 reset signal device tree binding.
15 Signed-off-by: Philipp Zabel <p.zabel@pengutronix.de>
16 Reviewed-by: Stephen Warren <swarren@nvidia.com>
17 Reviewed-by: Shawn Guo <shawn.guo@linaro.org>
18 Reviewed-by: Marek Vasut <marex@denx.de>
19 Reviewed-by: Pavel Machek <pavel@ucw.cz>
21 Documentation/devicetree/bindings/reset/reset.txt | 75 ++++++
23 drivers/Makefile | 3 +
24 drivers/reset/Kconfig | 13 +
25 drivers/reset/Makefile | 1 +
26 drivers/reset/core.c | 297 +++++++++++++++++++++
27 include/linux/reset-controller.h | 51 ++++
28 include/linux/reset.h | 17 ++
29 8 files changed, 459 insertions(+)
30 create mode 100644 Documentation/devicetree/bindings/reset/reset.txt
31 create mode 100644 drivers/reset/Kconfig
32 create mode 100644 drivers/reset/Makefile
33 create mode 100644 drivers/reset/core.c
34 create mode 100644 include/linux/reset-controller.h
35 create mode 100644 include/linux/reset.h
37 diff --git a/Documentation/devicetree/bindings/reset/reset.txt b/Documentation/devicetree/bindings/reset/reset.txt
39 index 0000000..31db6ff
41 +++ b/Documentation/devicetree/bindings/reset/reset.txt
43 += Reset Signal Device Tree Bindings =
45 +This binding is intended to represent the hardware reset signals present
46 +internally in most IC (SoC, FPGA, ...) designs. Reset signals for whole
47 +standalone chips are most likely better represented as GPIOs, although there
48 +are likely to be exceptions to this rule.
50 +Hardware blocks typically receive a reset signal. This signal is generated by
51 +a reset provider (e.g. power management or clock module) and received by a
52 +reset consumer (the module being reset, or a module managing when a sub-
53 +ordinate module is reset). This binding exists to represent the provider and
54 +consumer, and provide a way to couple the two together.
56 +A reset signal is represented by the phandle of the provider, plus a reset
57 +specifier - a list of DT cells that represents the reset signal within the
58 +provider. The length (number of cells) and semantics of the reset specifier
59 +are dictated by the binding of the reset provider, although common schemes
62 +A word on where to place reset signal consumers in device tree: It is possible
63 +in hardware for a reset signal to affect multiple logically separate HW blocks
64 +at once. In this case, it would be unwise to represent this reset signal in
65 +the DT node of each affected HW block, since if activated, an unrelated block
66 +may be reset. Instead, reset signals should be represented in the DT node
67 +where it makes most sense to control it; this may be a bus node if all
68 +children of the bus are affected by the reset signal, or an individual HW
69 +block node for dedicated reset signals. The intent of this binding is to give
70 +appropriate software access to the reset signals in order to manage the HW,
71 +rather than to slavishly enumerate the reset signal that affects each HW
77 +#reset-cells: Number of cells in a reset specifier; Typically 0 for nodes
78 + with a single reset output and 1 for nodes with multiple
83 + rst: reset-controller {
90 +resets: List of phandle and reset specifier pairs, one pair
91 + for each reset signal that affects the device, or that the
92 + device manages. Note: if the reset provider specifies '0' for
93 + #reset-cells, then only the phandle portion of the pair will
97 +reset-names: List of reset signal name strings sorted in the same order as
98 + the resets property. Consumers drivers will use reset-names to
99 + match reset signal names with reset specifiers.
104 + resets = <&rst 20>;
105 + reset-names = "reset";
108 +This represents a device with a single reset signal named "reset".
111 + resets = <&rst 10> <&rst 11> <&rst 12> <&rst 11>;
112 + reset-names = "i2s1", "i2s2", "dma", "mixer";
115 +This represents a bus that controls the reset signal of each of four sub-
116 +ordinate devices. Consider for example a bus that fails to operate unless no
117 +child device has reset asserted.
118 diff --git a/drivers/Kconfig b/drivers/Kconfig
119 index f5fb072..51f73ae 100644
120 --- a/drivers/Kconfig
121 +++ b/drivers/Kconfig
122 @@ -158,4 +158,6 @@ source "drivers/irqchip/Kconfig"
124 source "drivers/ipack/Kconfig"
126 +source "drivers/reset/Kconfig"
129 diff --git a/drivers/Makefile b/drivers/Makefile
130 index 7863b9f..2819ac0 100644
131 --- a/drivers/Makefile
132 +++ b/drivers/Makefile
133 @@ -37,6 +37,9 @@ obj-$(CONFIG_XEN) += xen/
134 # regulators early, since some subsystems rely on them to initialize
135 obj-$(CONFIG_REGULATOR) += regulator/
137 +# reset controllers early, since gpu drivers might rely on them to initialize
138 +obj-$(CONFIG_RESET_CONTROLLER) += reset/
140 # tty/ comes before char/ so that the VT console is the boot-time
143 diff --git a/drivers/reset/Kconfig b/drivers/reset/Kconfig
145 index 0000000..c9d04f7
147 +++ b/drivers/reset/Kconfig
149 +config ARCH_HAS_RESET_CONTROLLER
152 +menuconfig RESET_CONTROLLER
153 + bool "Reset Controller Support"
154 + default y if ARCH_HAS_RESET_CONTROLLER
156 + Generic Reset Controller support.
158 + This framework is designed to abstract reset handling of devices
159 + via GPIOs or SoC-internal reset controller modules.
162 diff --git a/drivers/reset/Makefile b/drivers/reset/Makefile
164 index 0000000..1e2d83f
166 +++ b/drivers/reset/Makefile
168 +obj-$(CONFIG_RESET_CONTROLLER) += core.o
169 diff --git a/drivers/reset/core.c b/drivers/reset/core.c
171 index 0000000..d1b6089
173 +++ b/drivers/reset/core.c
176 + * Reset Controller framework
178 + * Copyright 2013 Philipp Zabel, Pengutronix
180 + * This program is free software; you can redistribute it and/or modify
181 + * it under the terms of the GNU General Public License as published by
182 + * the Free Software Foundation; either version 2 of the License, or
183 + * (at your option) any later version.
185 +#include <linux/device.h>
186 +#include <linux/err.h>
187 +#include <linux/export.h>
188 +#include <linux/kernel.h>
189 +#include <linux/module.h>
190 +#include <linux/of.h>
191 +#include <linux/reset.h>
192 +#include <linux/reset-controller.h>
193 +#include <linux/slab.h>
195 +static DEFINE_MUTEX(reset_controller_list_mutex);
196 +static LIST_HEAD(reset_controller_list);
199 + * struct reset_control - a reset control
200 + * @rcdev: a pointer to the reset controller device
201 + * this reset control belongs to
202 + * @id: ID of the reset controller in the reset
203 + * controller device
205 +struct reset_control {
206 + struct reset_controller_dev *rcdev;
207 + struct device *dev;
212 + * of_reset_simple_xlate - translate reset_spec to the reset line number
213 + * @rcdev: a pointer to the reset controller device
214 + * @reset_spec: reset line specifier as found in the device tree
215 + * @flags: a flags pointer to fill in (optional)
217 + * This simple translation function should be used for reset controllers
218 + * with 1:1 mapping, where reset lines can be indexed by number without gaps.
220 +int of_reset_simple_xlate(struct reset_controller_dev *rcdev,
221 + const struct of_phandle_args *reset_spec)
223 + if (WARN_ON(reset_spec->args_count != rcdev->of_reset_n_cells))
226 + if (reset_spec->args[0] >= rcdev->nr_resets)
229 + return reset_spec->args[0];
231 +EXPORT_SYMBOL_GPL(of_reset_simple_xlate);
234 + * reset_controller_register - register a reset controller device
235 + * @rcdev: a pointer to the initialized reset controller device
237 +int reset_controller_register(struct reset_controller_dev *rcdev)
239 + if (!rcdev->of_xlate) {
240 + rcdev->of_reset_n_cells = 1;
241 + rcdev->of_xlate = of_reset_simple_xlate;
244 + mutex_lock(&reset_controller_list_mutex);
245 + list_add(&rcdev->list, &reset_controller_list);
246 + mutex_unlock(&reset_controller_list_mutex);
250 +EXPORT_SYMBOL_GPL(reset_controller_register);
253 + * reset_controller_unregister - unregister a reset controller device
254 + * @rcdev: a pointer to the reset controller device
256 +void reset_controller_unregister(struct reset_controller_dev *rcdev)
258 + mutex_lock(&reset_controller_list_mutex);
259 + list_del(&rcdev->list);
260 + mutex_unlock(&reset_controller_list_mutex);
262 +EXPORT_SYMBOL_GPL(reset_controller_unregister);
265 + * reset_control_reset - reset the controlled device
266 + * @rstc: reset controller
268 +int reset_control_reset(struct reset_control *rstc)
270 + if (rstc->rcdev->ops->reset)
271 + return rstc->rcdev->ops->reset(rstc->rcdev, rstc->id);
275 +EXPORT_SYMBOL_GPL(reset_control_reset);
278 + * reset_control_assert - asserts the reset line
279 + * @rstc: reset controller
281 +int reset_control_assert(struct reset_control *rstc)
283 + if (rstc->rcdev->ops->assert)
284 + return rstc->rcdev->ops->assert(rstc->rcdev, rstc->id);
288 +EXPORT_SYMBOL_GPL(reset_control_assert);
291 + * reset_control_deassert - deasserts the reset line
292 + * @rstc: reset controller
294 +int reset_control_deassert(struct reset_control *rstc)
296 + if (rstc->rcdev->ops->deassert)
297 + return rstc->rcdev->ops->deassert(rstc->rcdev, rstc->id);
301 +EXPORT_SYMBOL_GPL(reset_control_deassert);
304 + * reset_control_get - Lookup and obtain a reference to a reset controller.
305 + * @dev: device to be reset by the controller
306 + * @id: reset line name
308 + * Returns a struct reset_control or IS_ERR() condition containing errno.
310 + * Use of id names is optional.
312 +struct reset_control *reset_control_get(struct device *dev, const char *id)
314 + struct reset_control *rstc = ERR_PTR(-EPROBE_DEFER);
315 + struct reset_controller_dev *r, *rcdev;
316 + struct of_phandle_args args;
322 + return ERR_PTR(-EINVAL);
325 + index = of_property_match_string(dev->of_node,
326 + "reset-names", id);
327 + ret = of_parse_phandle_with_args(dev->of_node, "resets", "#reset-cells",
330 + return ERR_PTR(ret);
332 + mutex_lock(&reset_controller_list_mutex);
334 + list_for_each_entry(r, &reset_controller_list, list) {
335 + if (args.np == r->of_node) {
340 + of_node_put(args.np);
343 + mutex_unlock(&reset_controller_list_mutex);
344 + return ERR_PTR(-ENODEV);
347 + rstc_id = rcdev->of_xlate(rcdev, &args);
349 + mutex_unlock(&reset_controller_list_mutex);
350 + return ERR_PTR(rstc_id);
353 + try_module_get(rcdev->owner);
354 + mutex_unlock(&reset_controller_list_mutex);
356 + rstc = kzalloc(sizeof(*rstc), GFP_KERNEL);
358 + module_put(rcdev->owner);
359 + return ERR_PTR(-ENOMEM);
363 + rstc->rcdev = rcdev;
364 + rstc->id = rstc_id;
368 +EXPORT_SYMBOL_GPL(reset_control_get);
371 + * reset_control_put - free the reset controller
372 + * @rstc: reset controller
375 +void reset_control_put(struct reset_control *rstc)
380 + module_put(rstc->rcdev->owner);
383 +EXPORT_SYMBOL_GPL(reset_control_put);
385 +static void devm_reset_control_release(struct device *dev, void *res)
387 + reset_control_put(*(struct reset_control **)res);
391 + * devm_reset_control_get - resource managed reset_control_get()
392 + * @dev: device to be reset by the controller
393 + * @id: reset line name
395 + * Managed reset_control_get(). For reset controllers returned from this
396 + * function, reset_control_put() is called automatically on driver detach.
397 + * See reset_control_get() for more information.
399 +struct reset_control *devm_reset_control_get(struct device *dev, const char *id)
401 + struct reset_control **ptr, *rstc;
403 + ptr = devres_alloc(devm_reset_control_release, sizeof(*ptr),
406 + return ERR_PTR(-ENOMEM);
408 + rstc = reset_control_get(dev, id);
409 + if (!IS_ERR(rstc)) {
411 + devres_add(dev, ptr);
418 +EXPORT_SYMBOL_GPL(devm_reset_control_get);
420 +static int devm_reset_control_match(struct device *dev, void *res, void *data)
422 + struct reset_control **rstc = res;
423 + if (WARN_ON(!rstc || !*rstc))
425 + return *rstc == data;
429 + * devm_reset_control_put - resource managed reset_control_put()
430 + * @rstc: reset controller to free
432 + * Deallocate a reset control allocated withd devm_reset_control_get().
433 + * This function will not need to be called normally, as devres will take
434 + * care of freeing the resource.
436 +void devm_reset_control_put(struct reset_control *rstc)
440 + ret = devres_release(rstc->dev, devm_reset_control_release,
441 + devm_reset_control_match, rstc);
445 +EXPORT_SYMBOL_GPL(devm_reset_control_put);
448 + * device_reset - find reset controller associated with the device
449 + * and perform reset
450 + * @dev: device to be reset by the controller
452 + * Convenience wrapper for reset_control_get() and reset_control_reset().
453 + * This is useful for the common case of devices with single, dedicated reset
456 +int device_reset(struct device *dev)
458 + struct reset_control *rstc;
461 + rstc = reset_control_get(dev, NULL);
463 + return PTR_ERR(rstc);
465 + ret = reset_control_reset(rstc);
467 + reset_control_put(rstc);
471 +EXPORT_SYMBOL_GPL(device_reset);
472 diff --git a/include/linux/reset-controller.h b/include/linux/reset-controller.h
474 index 0000000..2f61311
476 +++ b/include/linux/reset-controller.h
478 +#ifndef _LINUX_RESET_CONTROLLER_H_
479 +#define _LINUX_RESET_CONTROLLER_H_
481 +#include <linux/list.h>
483 +struct reset_controller_dev;
486 + * struct reset_control_ops
488 + * @reset: for self-deasserting resets, does all necessary
489 + * things to reset the device
490 + * @assert: manually assert the reset line, if supported
491 + * @deassert: manually deassert the reset line, if supported
493 +struct reset_control_ops {
494 + int (*reset)(struct reset_controller_dev *rcdev, unsigned long id);
495 + int (*assert)(struct reset_controller_dev *rcdev, unsigned long id);
496 + int (*deassert)(struct reset_controller_dev *rcdev, unsigned long id);
503 + * struct reset_controller_dev - reset controller entity that might
504 + * provide multiple reset controls
505 + * @ops: a pointer to device specific struct reset_control_ops
506 + * @owner: kernel module of the reset controller driver
507 + * @list: internal list of reset controller devices
508 + * @of_node: corresponding device tree node as phandle target
509 + * @of_reset_n_cells: number of cells in reset line specifiers
510 + * @of_xlate: translation function to translate from specifier as found in the
511 + * device tree to id as given to the reset control ops
512 + * @nr_resets: number of reset controls in this reset controller device
514 +struct reset_controller_dev {
515 + struct reset_control_ops *ops;
516 + struct module *owner;
517 + struct list_head list;
518 + struct device_node *of_node;
519 + int of_reset_n_cells;
520 + int (*of_xlate)(struct reset_controller_dev *rcdev,
521 + const struct of_phandle_args *reset_spec);
522 + unsigned int nr_resets;
525 +int reset_controller_register(struct reset_controller_dev *rcdev);
526 +void reset_controller_unregister(struct reset_controller_dev *rcdev);
529 diff --git a/include/linux/reset.h b/include/linux/reset.h
531 index 0000000..6082247
533 +++ b/include/linux/reset.h
535 +#ifndef _LINUX_RESET_H_
536 +#define _LINUX_RESET_H_
539 +struct reset_control;
541 +int reset_control_reset(struct reset_control *rstc);
542 +int reset_control_assert(struct reset_control *rstc);
543 +int reset_control_deassert(struct reset_control *rstc);
545 +struct reset_control *reset_control_get(struct device *dev, const char *id);
546 +void reset_control_put(struct reset_control *rstc);
547 +struct reset_control *devm_reset_control_get(struct device *dev, const char *id);
549 +int device_reset(struct device *dev);