perf record: Fix segfault with --no-mmap-pages
[linux/fpc-iii.git] / drivers / reset / core.c
blobd1b6089a0ef82f57c9aece8b86168a336710b70d
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 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];
57 EXPORT_SYMBOL_GPL(of_reset_simple_xlate);
59 /**
60 * reset_controller_register - register a reset controller device
61 * @rcdev: a pointer to the initialized reset controller device
63 int reset_controller_register(struct reset_controller_dev *rcdev)
65 if (!rcdev->of_xlate) {
66 rcdev->of_reset_n_cells = 1;
67 rcdev->of_xlate = of_reset_simple_xlate;
70 mutex_lock(&reset_controller_list_mutex);
71 list_add(&rcdev->list, &reset_controller_list);
72 mutex_unlock(&reset_controller_list_mutex);
74 return 0;
76 EXPORT_SYMBOL_GPL(reset_controller_register);
78 /**
79 * reset_controller_unregister - unregister a reset controller device
80 * @rcdev: a pointer to the reset controller device
82 void reset_controller_unregister(struct reset_controller_dev *rcdev)
84 mutex_lock(&reset_controller_list_mutex);
85 list_del(&rcdev->list);
86 mutex_unlock(&reset_controller_list_mutex);
88 EXPORT_SYMBOL_GPL(reset_controller_unregister);
90 /**
91 * reset_control_reset - reset the controlled device
92 * @rstc: reset controller
94 int reset_control_reset(struct reset_control *rstc)
96 if (rstc->rcdev->ops->reset)
97 return rstc->rcdev->ops->reset(rstc->rcdev, rstc->id);
99 return -ENOSYS;
101 EXPORT_SYMBOL_GPL(reset_control_reset);
104 * reset_control_assert - asserts the reset line
105 * @rstc: reset controller
107 int reset_control_assert(struct reset_control *rstc)
109 if (rstc->rcdev->ops->assert)
110 return rstc->rcdev->ops->assert(rstc->rcdev, rstc->id);
112 return -ENOSYS;
114 EXPORT_SYMBOL_GPL(reset_control_assert);
117 * reset_control_deassert - deasserts the reset line
118 * @rstc: reset controller
120 int reset_control_deassert(struct reset_control *rstc)
122 if (rstc->rcdev->ops->deassert)
123 return rstc->rcdev->ops->deassert(rstc->rcdev, rstc->id);
125 return -ENOSYS;
127 EXPORT_SYMBOL_GPL(reset_control_deassert);
130 * reset_control_get - Lookup and obtain a reference to a reset controller.
131 * @dev: device to be reset by the controller
132 * @id: reset line name
134 * Returns a struct reset_control or IS_ERR() condition containing errno.
136 * Use of id names is optional.
138 struct reset_control *reset_control_get(struct device *dev, 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 (!dev)
148 return ERR_PTR(-EINVAL);
150 if (id)
151 index = of_property_match_string(dev->of_node,
152 "reset-names", id);
153 ret = of_parse_phandle_with_args(dev->of_node, "resets", "#reset-cells",
154 index, &args);
155 if (ret)
156 return ERR_PTR(ret);
158 mutex_lock(&reset_controller_list_mutex);
159 rcdev = NULL;
160 list_for_each_entry(r, &reset_controller_list, list) {
161 if (args.np == r->of_node) {
162 rcdev = r;
163 break;
166 of_node_put(args.np);
168 if (!rcdev) {
169 mutex_unlock(&reset_controller_list_mutex);
170 return ERR_PTR(-ENODEV);
173 rstc_id = rcdev->of_xlate(rcdev, &args);
174 if (rstc_id < 0) {
175 mutex_unlock(&reset_controller_list_mutex);
176 return ERR_PTR(rstc_id);
179 try_module_get(rcdev->owner);
180 mutex_unlock(&reset_controller_list_mutex);
182 rstc = kzalloc(sizeof(*rstc), GFP_KERNEL);
183 if (!rstc) {
184 module_put(rcdev->owner);
185 return ERR_PTR(-ENOMEM);
188 rstc->dev = dev;
189 rstc->rcdev = rcdev;
190 rstc->id = rstc_id;
192 return rstc;
194 EXPORT_SYMBOL_GPL(reset_control_get);
197 * reset_control_put - free the reset controller
198 * @rstc: reset controller
201 void reset_control_put(struct reset_control *rstc)
203 if (IS_ERR(rstc))
204 return;
206 module_put(rstc->rcdev->owner);
207 kfree(rstc);
209 EXPORT_SYMBOL_GPL(reset_control_put);
211 static void devm_reset_control_release(struct device *dev, void *res)
213 reset_control_put(*(struct reset_control **)res);
217 * devm_reset_control_get - resource managed reset_control_get()
218 * @dev: device to be reset by the controller
219 * @id: reset line name
221 * Managed reset_control_get(). For reset controllers returned from this
222 * function, reset_control_put() is called automatically on driver detach.
223 * See reset_control_get() for more information.
225 struct reset_control *devm_reset_control_get(struct device *dev, const char *id)
227 struct reset_control **ptr, *rstc;
229 ptr = devres_alloc(devm_reset_control_release, sizeof(*ptr),
230 GFP_KERNEL);
231 if (!ptr)
232 return ERR_PTR(-ENOMEM);
234 rstc = reset_control_get(dev, id);
235 if (!IS_ERR(rstc)) {
236 *ptr = rstc;
237 devres_add(dev, ptr);
238 } else {
239 devres_free(ptr);
242 return rstc;
244 EXPORT_SYMBOL_GPL(devm_reset_control_get);
246 static int devm_reset_control_match(struct device *dev, void *res, void *data)
248 struct reset_control **rstc = res;
249 if (WARN_ON(!rstc || !*rstc))
250 return 0;
251 return *rstc == data;
255 * devm_reset_control_put - resource managed reset_control_put()
256 * @rstc: reset controller to free
258 * Deallocate a reset control allocated withd devm_reset_control_get().
259 * This function will not need to be called normally, as devres will take
260 * care of freeing the resource.
262 void devm_reset_control_put(struct reset_control *rstc)
264 int ret;
266 ret = devres_release(rstc->dev, devm_reset_control_release,
267 devm_reset_control_match, rstc);
268 if (ret)
269 WARN_ON(ret);
271 EXPORT_SYMBOL_GPL(devm_reset_control_put);
274 * device_reset - find reset controller associated with the device
275 * and perform reset
276 * @dev: device to be reset by the controller
278 * Convenience wrapper for reset_control_get() and reset_control_reset().
279 * This is useful for the common case of devices with single, dedicated reset
280 * lines.
282 int device_reset(struct device *dev)
284 struct reset_control *rstc;
285 int ret;
287 rstc = reset_control_get(dev, NULL);
288 if (IS_ERR(rstc))
289 return PTR_ERR(rstc);
291 ret = reset_control_reset(rstc);
293 reset_control_put(rstc);
295 return ret;
297 EXPORT_SYMBOL_GPL(device_reset);