[media] media: mt9m111: add device-tree documentation
[linux-2.6/btrfs-unstable.git] / drivers / reset / core.c
blobbaeaf82d40d9f1982761d68ee1cf0c56cd6c346e
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/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>
21 static DEFINE_MUTEX(reset_controller_list_mutex);
22 static LIST_HEAD(reset_controller_list);
24 /**
25 * struct reset_control - a reset control
26 * @rcdev: a pointer to the reset controller device
27 * this reset control belongs to
28 * @id: ID of the reset controller in the reset
29 * controller device
31 struct reset_control {
32 struct reset_controller_dev *rcdev;
33 struct device *dev;
34 unsigned int id;
37 /**
38 * of_reset_simple_xlate - translate reset_spec to the reset line number
39 * @rcdev: a pointer to the reset controller device
40 * @reset_spec: reset line specifier as found in the device tree
41 * @flags: a flags pointer to fill in (optional)
43 * This simple translation function should be used for reset controllers
44 * with 1:1 mapping, where reset lines can be indexed by number without gaps.
46 static int of_reset_simple_xlate(struct reset_controller_dev *rcdev,
47 const struct of_phandle_args *reset_spec)
49 if (WARN_ON(reset_spec->args_count != rcdev->of_reset_n_cells))
50 return -EINVAL;
52 if (reset_spec->args[0] >= rcdev->nr_resets)
53 return -EINVAL;
55 return reset_spec->args[0];
58 /**
59 * reset_controller_register - register a reset controller device
60 * @rcdev: a pointer to the initialized reset controller device
62 int reset_controller_register(struct reset_controller_dev *rcdev)
64 if (!rcdev->of_xlate) {
65 rcdev->of_reset_n_cells = 1;
66 rcdev->of_xlate = of_reset_simple_xlate;
69 mutex_lock(&reset_controller_list_mutex);
70 list_add(&rcdev->list, &reset_controller_list);
71 mutex_unlock(&reset_controller_list_mutex);
73 return 0;
75 EXPORT_SYMBOL_GPL(reset_controller_register);
77 /**
78 * reset_controller_unregister - unregister a reset controller device
79 * @rcdev: a pointer to the reset controller device
81 void reset_controller_unregister(struct reset_controller_dev *rcdev)
83 mutex_lock(&reset_controller_list_mutex);
84 list_del(&rcdev->list);
85 mutex_unlock(&reset_controller_list_mutex);
87 EXPORT_SYMBOL_GPL(reset_controller_unregister);
89 /**
90 * reset_control_reset - reset the controlled device
91 * @rstc: reset controller
93 int reset_control_reset(struct reset_control *rstc)
95 if (rstc->rcdev->ops->reset)
96 return rstc->rcdev->ops->reset(rstc->rcdev, rstc->id);
98 return -ENOSYS;
100 EXPORT_SYMBOL_GPL(reset_control_reset);
103 * reset_control_assert - asserts the reset line
104 * @rstc: reset controller
106 int reset_control_assert(struct reset_control *rstc)
108 if (rstc->rcdev->ops->assert)
109 return rstc->rcdev->ops->assert(rstc->rcdev, rstc->id);
111 return -ENOSYS;
113 EXPORT_SYMBOL_GPL(reset_control_assert);
116 * reset_control_deassert - deasserts the reset line
117 * @rstc: reset controller
119 int reset_control_deassert(struct reset_control *rstc)
121 if (rstc->rcdev->ops->deassert)
122 return rstc->rcdev->ops->deassert(rstc->rcdev, rstc->id);
124 return -ENOSYS;
126 EXPORT_SYMBOL_GPL(reset_control_deassert);
129 * of_reset_control_get - Lookup and obtain a reference to a reset controller.
130 * @node: device to be reset by the controller
131 * @id: reset line name
133 * Returns a struct reset_control or IS_ERR() condition containing errno.
135 * Use of id names is optional.
137 struct reset_control *of_reset_control_get(struct device_node *node,
138 const char *id)
140 struct reset_control *rstc = ERR_PTR(-EPROBE_DEFER);
141 struct reset_controller_dev *r, *rcdev;
142 struct of_phandle_args args;
143 int index = 0;
144 int rstc_id;
145 int ret;
147 if (id)
148 index = of_property_match_string(node,
149 "reset-names", id);
150 ret = of_parse_phandle_with_args(node, "resets", "#reset-cells",
151 index, &args);
152 if (ret)
153 return ERR_PTR(ret);
155 mutex_lock(&reset_controller_list_mutex);
156 rcdev = NULL;
157 list_for_each_entry(r, &reset_controller_list, list) {
158 if (args.np == r->of_node) {
159 rcdev = r;
160 break;
163 of_node_put(args.np);
165 if (!rcdev) {
166 mutex_unlock(&reset_controller_list_mutex);
167 return ERR_PTR(-EPROBE_DEFER);
170 rstc_id = rcdev->of_xlate(rcdev, &args);
171 if (rstc_id < 0) {
172 mutex_unlock(&reset_controller_list_mutex);
173 return ERR_PTR(rstc_id);
176 try_module_get(rcdev->owner);
177 mutex_unlock(&reset_controller_list_mutex);
179 rstc = kzalloc(sizeof(*rstc), GFP_KERNEL);
180 if (!rstc) {
181 module_put(rcdev->owner);
182 return ERR_PTR(-ENOMEM);
185 rstc->rcdev = rcdev;
186 rstc->id = rstc_id;
188 return rstc;
190 EXPORT_SYMBOL_GPL(of_reset_control_get);
193 * reset_control_get - Lookup and obtain a reference to a reset controller.
194 * @dev: device to be reset by the controller
195 * @id: reset line name
197 * Returns a struct reset_control or IS_ERR() condition containing errno.
199 * Use of id names is optional.
201 struct reset_control *reset_control_get(struct device *dev, const char *id)
203 struct reset_control *rstc;
205 if (!dev)
206 return ERR_PTR(-EINVAL);
208 rstc = of_reset_control_get(dev->of_node, id);
209 if (!IS_ERR(rstc))
210 rstc->dev = dev;
212 return rstc;
214 EXPORT_SYMBOL_GPL(reset_control_get);
217 * reset_control_put - free the reset controller
218 * @rstc: reset controller
221 void reset_control_put(struct reset_control *rstc)
223 if (IS_ERR(rstc))
224 return;
226 module_put(rstc->rcdev->owner);
227 kfree(rstc);
229 EXPORT_SYMBOL_GPL(reset_control_put);
231 static void devm_reset_control_release(struct device *dev, void *res)
233 reset_control_put(*(struct reset_control **)res);
237 * devm_reset_control_get - resource managed reset_control_get()
238 * @dev: device to be reset by the controller
239 * @id: reset line name
241 * Managed reset_control_get(). For reset controllers returned from this
242 * function, reset_control_put() is called automatically on driver detach.
243 * See reset_control_get() for more information.
245 struct reset_control *devm_reset_control_get(struct device *dev, const char *id)
247 struct reset_control **ptr, *rstc;
249 ptr = devres_alloc(devm_reset_control_release, sizeof(*ptr),
250 GFP_KERNEL);
251 if (!ptr)
252 return ERR_PTR(-ENOMEM);
254 rstc = reset_control_get(dev, id);
255 if (!IS_ERR(rstc)) {
256 *ptr = rstc;
257 devres_add(dev, ptr);
258 } else {
259 devres_free(ptr);
262 return rstc;
264 EXPORT_SYMBOL_GPL(devm_reset_control_get);
267 * device_reset - find reset controller associated with the device
268 * and perform reset
269 * @dev: device to be reset by the controller
271 * Convenience wrapper for reset_control_get() and reset_control_reset().
272 * This is useful for the common case of devices with single, dedicated reset
273 * lines.
275 int device_reset(struct device *dev)
277 struct reset_control *rstc;
278 int ret;
280 rstc = reset_control_get(dev, NULL);
281 if (IS_ERR(rstc))
282 return PTR_ERR(rstc);
284 ret = reset_control_reset(rstc);
286 reset_control_put(rstc);
288 return ret;
290 EXPORT_SYMBOL_GPL(device_reset);