Linux 4.2.1
[linux/fpc-iii.git] / drivers / reset / core.c
blob7955e00d04d49fca9f16d78f1fa50835c038bf46
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 * reset_control_status - returns a negative errno if not supported, a
130 * positive value if the reset line is asserted, or zero if the reset
131 * line is not asserted.
132 * @rstc: reset controller
134 int reset_control_status(struct reset_control *rstc)
136 if (rstc->rcdev->ops->status)
137 return rstc->rcdev->ops->status(rstc->rcdev, rstc->id);
139 return -ENOSYS;
141 EXPORT_SYMBOL_GPL(reset_control_status);
144 * of_reset_control_get - Lookup and obtain a reference to a reset controller.
145 * @node: device to be reset by the controller
146 * @id: reset line name
148 * Returns a struct reset_control or IS_ERR() condition containing errno.
150 * Use of id names is optional.
152 struct reset_control *of_reset_control_get(struct device_node *node,
153 const char *id)
155 struct reset_control *rstc = ERR_PTR(-EPROBE_DEFER);
156 struct reset_controller_dev *r, *rcdev;
157 struct of_phandle_args args;
158 int index = 0;
159 int rstc_id;
160 int ret;
162 if (id)
163 index = of_property_match_string(node,
164 "reset-names", id);
165 ret = of_parse_phandle_with_args(node, "resets", "#reset-cells",
166 index, &args);
167 if (ret)
168 return ERR_PTR(ret);
170 mutex_lock(&reset_controller_list_mutex);
171 rcdev = NULL;
172 list_for_each_entry(r, &reset_controller_list, list) {
173 if (args.np == r->of_node) {
174 rcdev = r;
175 break;
178 of_node_put(args.np);
180 if (!rcdev) {
181 mutex_unlock(&reset_controller_list_mutex);
182 return ERR_PTR(-EPROBE_DEFER);
185 rstc_id = rcdev->of_xlate(rcdev, &args);
186 if (rstc_id < 0) {
187 mutex_unlock(&reset_controller_list_mutex);
188 return ERR_PTR(rstc_id);
191 try_module_get(rcdev->owner);
192 mutex_unlock(&reset_controller_list_mutex);
194 rstc = kzalloc(sizeof(*rstc), GFP_KERNEL);
195 if (!rstc) {
196 module_put(rcdev->owner);
197 return ERR_PTR(-ENOMEM);
200 rstc->rcdev = rcdev;
201 rstc->id = rstc_id;
203 return rstc;
205 EXPORT_SYMBOL_GPL(of_reset_control_get);
208 * reset_control_get - Lookup and obtain a reference to a reset controller.
209 * @dev: device to be reset by the controller
210 * @id: reset line name
212 * Returns a struct reset_control or IS_ERR() condition containing errno.
214 * Use of id names is optional.
216 struct reset_control *reset_control_get(struct device *dev, const char *id)
218 struct reset_control *rstc;
220 if (!dev)
221 return ERR_PTR(-EINVAL);
223 rstc = of_reset_control_get(dev->of_node, id);
224 if (!IS_ERR(rstc))
225 rstc->dev = dev;
227 return rstc;
229 EXPORT_SYMBOL_GPL(reset_control_get);
232 * reset_control_put - free the reset controller
233 * @rstc: reset controller
236 void reset_control_put(struct reset_control *rstc)
238 if (IS_ERR(rstc))
239 return;
241 module_put(rstc->rcdev->owner);
242 kfree(rstc);
244 EXPORT_SYMBOL_GPL(reset_control_put);
246 static void devm_reset_control_release(struct device *dev, void *res)
248 reset_control_put(*(struct reset_control **)res);
252 * devm_reset_control_get - resource managed reset_control_get()
253 * @dev: device to be reset by the controller
254 * @id: reset line name
256 * Managed reset_control_get(). For reset controllers returned from this
257 * function, reset_control_put() is called automatically on driver detach.
258 * See reset_control_get() for more information.
260 struct reset_control *devm_reset_control_get(struct device *dev, const char *id)
262 struct reset_control **ptr, *rstc;
264 ptr = devres_alloc(devm_reset_control_release, sizeof(*ptr),
265 GFP_KERNEL);
266 if (!ptr)
267 return ERR_PTR(-ENOMEM);
269 rstc = reset_control_get(dev, id);
270 if (!IS_ERR(rstc)) {
271 *ptr = rstc;
272 devres_add(dev, ptr);
273 } else {
274 devres_free(ptr);
277 return rstc;
279 EXPORT_SYMBOL_GPL(devm_reset_control_get);
282 * device_reset - find reset controller associated with the device
283 * and perform reset
284 * @dev: device to be reset by the controller
286 * Convenience wrapper for reset_control_get() and reset_control_reset().
287 * This is useful for the common case of devices with single, dedicated reset
288 * lines.
290 int device_reset(struct device *dev)
292 struct reset_control *rstc;
293 int ret;
295 rstc = reset_control_get(dev, NULL);
296 if (IS_ERR(rstc))
297 return PTR_ERR(rstc);
299 ret = reset_control_reset(rstc);
301 reset_control_put(rstc);
303 return ret;
305 EXPORT_SYMBOL_GPL(device_reset);