1 From d6cfbdfa001891894efe078a49ad82ac8a932dbb 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 154/164] 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
38 +++ b/Documentation/devicetree/bindings/reset/reset.txt
40 += Reset Signal Device Tree Bindings =
42 +This binding is intended to represent the hardware reset signals present
43 +internally in most IC (SoC, FPGA, ...) designs. Reset signals for whole
44 +standalone chips are most likely better represented as GPIOs, although there
45 +are likely to be exceptions to this rule.
47 +Hardware blocks typically receive a reset signal. This signal is generated by
48 +a reset provider (e.g. power management or clock module) and received by a
49 +reset consumer (the module being reset, or a module managing when a sub-
50 +ordinate module is reset). This binding exists to represent the provider and
51 +consumer, and provide a way to couple the two together.
53 +A reset signal is represented by the phandle of the provider, plus a reset
54 +specifier - a list of DT cells that represents the reset signal within the
55 +provider. The length (number of cells) and semantics of the reset specifier
56 +are dictated by the binding of the reset provider, although common schemes
59 +A word on where to place reset signal consumers in device tree: It is possible
60 +in hardware for a reset signal to affect multiple logically separate HW blocks
61 +at once. In this case, it would be unwise to represent this reset signal in
62 +the DT node of each affected HW block, since if activated, an unrelated block
63 +may be reset. Instead, reset signals should be represented in the DT node
64 +where it makes most sense to control it; this may be a bus node if all
65 +children of the bus are affected by the reset signal, or an individual HW
66 +block node for dedicated reset signals. The intent of this binding is to give
67 +appropriate software access to the reset signals in order to manage the HW,
68 +rather than to slavishly enumerate the reset signal that affects each HW
74 +#reset-cells: Number of cells in a reset specifier; Typically 0 for nodes
75 + with a single reset output and 1 for nodes with multiple
80 + rst: reset-controller {
87 +resets: List of phandle and reset specifier pairs, one pair
88 + for each reset signal that affects the device, or that the
89 + device manages. Note: if the reset provider specifies '0' for
90 + #reset-cells, then only the phandle portion of the pair will
94 +reset-names: List of reset signal name strings sorted in the same order as
95 + the resets property. Consumers drivers will use reset-names to
96 + match reset signal names with reset specifiers.
101 + resets = <&rst 20>;
102 + reset-names = "reset";
105 +This represents a device with a single reset signal named "reset".
108 + resets = <&rst 10> <&rst 11> <&rst 12> <&rst 11>;
109 + reset-names = "i2s1", "i2s2", "dma", "mixer";
112 +This represents a bus that controls the reset signal of each of four sub-
113 +ordinate devices. Consider for example a bus that fails to operate unless no
114 +child device has reset asserted.
115 --- a/drivers/Kconfig
116 +++ b/drivers/Kconfig
117 @@ -164,4 +164,6 @@ source "drivers/irqchip/Kconfig"
119 source "drivers/ipack/Kconfig"
121 +source "drivers/reset/Kconfig"
124 --- a/drivers/Makefile
125 +++ b/drivers/Makefile
126 @@ -38,6 +38,9 @@ obj-$(CONFIG_XEN) += xen/
127 # regulators early, since some subsystems rely on them to initialize
128 obj-$(CONFIG_REGULATOR) += regulator/
130 +# reset controllers early, since gpu drivers might rely on them to initialize
131 +obj-$(CONFIG_RESET_CONTROLLER) += reset/
133 # tty/ comes before char/ so that the VT console is the boot-time
137 +++ b/drivers/reset/Kconfig
139 +config ARCH_HAS_RESET_CONTROLLER
142 +menuconfig RESET_CONTROLLER
143 + bool "Reset Controller Support"
144 + default y if ARCH_HAS_RESET_CONTROLLER
146 + Generic Reset Controller support.
148 + This framework is designed to abstract reset handling of devices
149 + via GPIOs or SoC-internal reset controller modules.
153 +++ b/drivers/reset/Makefile
155 +obj-$(CONFIG_RESET_CONTROLLER) += core.o
157 +++ b/drivers/reset/core.c
160 + * Reset Controller framework
162 + * Copyright 2013 Philipp Zabel, Pengutronix
164 + * This program is free software; you can redistribute it and/or modify
165 + * it under the terms of the GNU General Public License as published by
166 + * the Free Software Foundation; either version 2 of the License, or
167 + * (at your option) any later version.
169 +#include <linux/device.h>
170 +#include <linux/err.h>
171 +#include <linux/export.h>
172 +#include <linux/kernel.h>
173 +#include <linux/module.h>
174 +#include <linux/of.h>
175 +#include <linux/reset.h>
176 +#include <linux/reset-controller.h>
177 +#include <linux/slab.h>
179 +static DEFINE_MUTEX(reset_controller_list_mutex);
180 +static LIST_HEAD(reset_controller_list);
183 + * struct reset_control - a reset control
184 + * @rcdev: a pointer to the reset controller device
185 + * this reset control belongs to
186 + * @id: ID of the reset controller in the reset
187 + * controller device
189 +struct reset_control {
190 + struct reset_controller_dev *rcdev;
191 + struct device *dev;
196 + * of_reset_simple_xlate - translate reset_spec to the reset line number
197 + * @rcdev: a pointer to the reset controller device
198 + * @reset_spec: reset line specifier as found in the device tree
199 + * @flags: a flags pointer to fill in (optional)
201 + * This simple translation function should be used for reset controllers
202 + * with 1:1 mapping, where reset lines can be indexed by number without gaps.
204 +int of_reset_simple_xlate(struct reset_controller_dev *rcdev,
205 + const struct of_phandle_args *reset_spec)
207 + if (WARN_ON(reset_spec->args_count != rcdev->of_reset_n_cells))
210 + if (reset_spec->args[0] >= rcdev->nr_resets)
213 + return reset_spec->args[0];
215 +EXPORT_SYMBOL_GPL(of_reset_simple_xlate);
218 + * reset_controller_register - register a reset controller device
219 + * @rcdev: a pointer to the initialized reset controller device
221 +int reset_controller_register(struct reset_controller_dev *rcdev)
223 + if (!rcdev->of_xlate) {
224 + rcdev->of_reset_n_cells = 1;
225 + rcdev->of_xlate = of_reset_simple_xlate;
228 + mutex_lock(&reset_controller_list_mutex);
229 + list_add(&rcdev->list, &reset_controller_list);
230 + mutex_unlock(&reset_controller_list_mutex);
234 +EXPORT_SYMBOL_GPL(reset_controller_register);
237 + * reset_controller_unregister - unregister a reset controller device
238 + * @rcdev: a pointer to the reset controller device
240 +void reset_controller_unregister(struct reset_controller_dev *rcdev)
242 + mutex_lock(&reset_controller_list_mutex);
243 + list_del(&rcdev->list);
244 + mutex_unlock(&reset_controller_list_mutex);
246 +EXPORT_SYMBOL_GPL(reset_controller_unregister);
249 + * reset_control_reset - reset the controlled device
250 + * @rstc: reset controller
252 +int reset_control_reset(struct reset_control *rstc)
254 + if (rstc->rcdev->ops->reset)
255 + return rstc->rcdev->ops->reset(rstc->rcdev, rstc->id);
259 +EXPORT_SYMBOL_GPL(reset_control_reset);
262 + * reset_control_assert - asserts the reset line
263 + * @rstc: reset controller
265 +int reset_control_assert(struct reset_control *rstc)
267 + if (rstc->rcdev->ops->assert)
268 + return rstc->rcdev->ops->assert(rstc->rcdev, rstc->id);
272 +EXPORT_SYMBOL_GPL(reset_control_assert);
275 + * reset_control_deassert - deasserts the reset line
276 + * @rstc: reset controller
278 +int reset_control_deassert(struct reset_control *rstc)
280 + if (rstc->rcdev->ops->deassert)
281 + return rstc->rcdev->ops->deassert(rstc->rcdev, rstc->id);
285 +EXPORT_SYMBOL_GPL(reset_control_deassert);
288 + * reset_control_get - Lookup and obtain a reference to a reset controller.
289 + * @dev: device to be reset by the controller
290 + * @id: reset line name
292 + * Returns a struct reset_control or IS_ERR() condition containing errno.
294 + * Use of id names is optional.
296 +struct reset_control *reset_control_get(struct device *dev, const char *id)
298 + struct reset_control *rstc = ERR_PTR(-EPROBE_DEFER);
299 + struct reset_controller_dev *r, *rcdev;
300 + struct of_phandle_args args;
306 + return ERR_PTR(-EINVAL);
309 + index = of_property_match_string(dev->of_node,
310 + "reset-names", id);
311 + ret = of_parse_phandle_with_args(dev->of_node, "resets", "#reset-cells",
314 + return ERR_PTR(ret);
316 + mutex_lock(&reset_controller_list_mutex);
318 + list_for_each_entry(r, &reset_controller_list, list) {
319 + if (args.np == r->of_node) {
324 + of_node_put(args.np);
327 + mutex_unlock(&reset_controller_list_mutex);
328 + return ERR_PTR(-ENODEV);
331 + rstc_id = rcdev->of_xlate(rcdev, &args);
333 + mutex_unlock(&reset_controller_list_mutex);
334 + return ERR_PTR(rstc_id);
337 + try_module_get(rcdev->owner);
338 + mutex_unlock(&reset_controller_list_mutex);
340 + rstc = kzalloc(sizeof(*rstc), GFP_KERNEL);
342 + module_put(rcdev->owner);
343 + return ERR_PTR(-ENOMEM);
347 + rstc->rcdev = rcdev;
348 + rstc->id = rstc_id;
352 +EXPORT_SYMBOL_GPL(reset_control_get);
355 + * reset_control_put - free the reset controller
356 + * @rstc: reset controller
359 +void reset_control_put(struct reset_control *rstc)
364 + module_put(rstc->rcdev->owner);
367 +EXPORT_SYMBOL_GPL(reset_control_put);
369 +static void devm_reset_control_release(struct device *dev, void *res)
371 + reset_control_put(*(struct reset_control **)res);
375 + * devm_reset_control_get - resource managed reset_control_get()
376 + * @dev: device to be reset by the controller
377 + * @id: reset line name
379 + * Managed reset_control_get(). For reset controllers returned from this
380 + * function, reset_control_put() is called automatically on driver detach.
381 + * See reset_control_get() for more information.
383 +struct reset_control *devm_reset_control_get(struct device *dev, const char *id)
385 + struct reset_control **ptr, *rstc;
387 + ptr = devres_alloc(devm_reset_control_release, sizeof(*ptr),
390 + return ERR_PTR(-ENOMEM);
392 + rstc = reset_control_get(dev, id);
393 + if (!IS_ERR(rstc)) {
395 + devres_add(dev, ptr);
402 +EXPORT_SYMBOL_GPL(devm_reset_control_get);
404 +static int devm_reset_control_match(struct device *dev, void *res, void *data)
406 + struct reset_control **rstc = res;
407 + if (WARN_ON(!rstc || !*rstc))
409 + return *rstc == data;
413 + * devm_reset_control_put - resource managed reset_control_put()
414 + * @rstc: reset controller to free
416 + * Deallocate a reset control allocated withd devm_reset_control_get().
417 + * This function will not need to be called normally, as devres will take
418 + * care of freeing the resource.
420 +void devm_reset_control_put(struct reset_control *rstc)
424 + ret = devres_release(rstc->dev, devm_reset_control_release,
425 + devm_reset_control_match, rstc);
429 +EXPORT_SYMBOL_GPL(devm_reset_control_put);
432 + * device_reset - find reset controller associated with the device
433 + * and perform reset
434 + * @dev: device to be reset by the controller
436 + * Convenience wrapper for reset_control_get() and reset_control_reset().
437 + * This is useful for the common case of devices with single, dedicated reset
440 +int device_reset(struct device *dev)
442 + struct reset_control *rstc;
445 + rstc = reset_control_get(dev, NULL);
447 + return PTR_ERR(rstc);
449 + ret = reset_control_reset(rstc);
451 + reset_control_put(rstc);
455 +EXPORT_SYMBOL_GPL(device_reset);
457 +++ b/include/linux/reset-controller.h
459 +#ifndef _LINUX_RESET_CONTROLLER_H_
460 +#define _LINUX_RESET_CONTROLLER_H_
462 +#include <linux/list.h>
464 +struct reset_controller_dev;
467 + * struct reset_control_ops
469 + * @reset: for self-deasserting resets, does all necessary
470 + * things to reset the device
471 + * @assert: manually assert the reset line, if supported
472 + * @deassert: manually deassert the reset line, if supported
474 +struct reset_control_ops {
475 + int (*reset)(struct reset_controller_dev *rcdev, unsigned long id);
476 + int (*assert)(struct reset_controller_dev *rcdev, unsigned long id);
477 + int (*deassert)(struct reset_controller_dev *rcdev, unsigned long id);
484 + * struct reset_controller_dev - reset controller entity that might
485 + * provide multiple reset controls
486 + * @ops: a pointer to device specific struct reset_control_ops
487 + * @owner: kernel module of the reset controller driver
488 + * @list: internal list of reset controller devices
489 + * @of_node: corresponding device tree node as phandle target
490 + * @of_reset_n_cells: number of cells in reset line specifiers
491 + * @of_xlate: translation function to translate from specifier as found in the
492 + * device tree to id as given to the reset control ops
493 + * @nr_resets: number of reset controls in this reset controller device
495 +struct reset_controller_dev {
496 + struct reset_control_ops *ops;
497 + struct module *owner;
498 + struct list_head list;
499 + struct device_node *of_node;
500 + int of_reset_n_cells;
501 + int (*of_xlate)(struct reset_controller_dev *rcdev,
502 + const struct of_phandle_args *reset_spec);
503 + unsigned int nr_resets;
506 +int reset_controller_register(struct reset_controller_dev *rcdev);
507 +void reset_controller_unregister(struct reset_controller_dev *rcdev);
511 +++ b/include/linux/reset.h
513 +#ifndef _LINUX_RESET_H_
514 +#define _LINUX_RESET_H_
517 +struct reset_control;
519 +int reset_control_reset(struct reset_control *rstc);
520 +int reset_control_assert(struct reset_control *rstc);
521 +int reset_control_deassert(struct reset_control *rstc);
523 +struct reset_control *reset_control_get(struct device *dev, const char *id);
524 +void reset_control_put(struct reset_control *rstc);
525 +struct reset_control *devm_reset_control_get(struct device *dev, const char *id);
527 +int device_reset(struct device *dev);