blob: 957750600ff3588f1ba21e16da1373058033738b [file] [log] [blame]
Philipp Zabel61fc4132012-11-19 17:23:13 +01001/*
2 * Reset Controller framework
3 *
4 * Copyright 2013 Philipp Zabel, Pengutronix
5 *
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.
10 */
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>
20
Hans de Goedec15ddec2016-02-23 18:46:25 +010021static DEFINE_MUTEX(reset_list_mutex);
Philipp Zabel61fc4132012-11-19 17:23:13 +010022static LIST_HEAD(reset_controller_list);
23
24/**
25 * struct reset_control - a reset control
26 * @rcdev: a pointer to the reset controller device
27 * this reset control belongs to
Hans de Goedec15ddec2016-02-23 18:46:25 +010028 * @list: list entry for the rcdev's reset controller list
Philipp Zabel61fc4132012-11-19 17:23:13 +010029 * @id: ID of the reset controller in the reset
30 * controller device
Hans de Goedec15ddec2016-02-23 18:46:25 +010031 * @refcnt: Number of gets of this reset_control
Philipp Zabel61fc4132012-11-19 17:23:13 +010032 */
33struct reset_control {
34 struct reset_controller_dev *rcdev;
Hans de Goedec15ddec2016-02-23 18:46:25 +010035 struct list_head list;
Philipp Zabel61fc4132012-11-19 17:23:13 +010036 unsigned int id;
Hans de Goedec15ddec2016-02-23 18:46:25 +010037 unsigned int refcnt;
Philipp Zabel61fc4132012-11-19 17:23:13 +010038};
39
40/**
41 * of_reset_simple_xlate - translate reset_spec to the reset line number
42 * @rcdev: a pointer to the reset controller device
43 * @reset_spec: reset line specifier as found in the device tree
44 * @flags: a flags pointer to fill in (optional)
45 *
46 * This simple translation function should be used for reset controllers
47 * with 1:1 mapping, where reset lines can be indexed by number without gaps.
48 */
Rashika Kheria0c5b2b92013-12-19 14:11:10 +053049static int of_reset_simple_xlate(struct reset_controller_dev *rcdev,
Philipp Zabel61fc4132012-11-19 17:23:13 +010050 const struct of_phandle_args *reset_spec)
51{
Philipp Zabel61fc4132012-11-19 17:23:13 +010052 if (reset_spec->args[0] >= rcdev->nr_resets)
53 return -EINVAL;
54
55 return reset_spec->args[0];
56}
Philipp Zabel61fc4132012-11-19 17:23:13 +010057
58/**
59 * reset_controller_register - register a reset controller device
60 * @rcdev: a pointer to the initialized reset controller device
61 */
62int reset_controller_register(struct reset_controller_dev *rcdev)
63{
64 if (!rcdev->of_xlate) {
65 rcdev->of_reset_n_cells = 1;
66 rcdev->of_xlate = of_reset_simple_xlate;
67 }
68
Hans de Goedec15ddec2016-02-23 18:46:25 +010069 INIT_LIST_HEAD(&rcdev->reset_control_head);
70
71 mutex_lock(&reset_list_mutex);
Philipp Zabel61fc4132012-11-19 17:23:13 +010072 list_add(&rcdev->list, &reset_controller_list);
Hans de Goedec15ddec2016-02-23 18:46:25 +010073 mutex_unlock(&reset_list_mutex);
Philipp Zabel61fc4132012-11-19 17:23:13 +010074
75 return 0;
76}
77EXPORT_SYMBOL_GPL(reset_controller_register);
78
79/**
80 * reset_controller_unregister - unregister a reset controller device
81 * @rcdev: a pointer to the reset controller device
82 */
83void reset_controller_unregister(struct reset_controller_dev *rcdev)
84{
Hans de Goedec15ddec2016-02-23 18:46:25 +010085 mutex_lock(&reset_list_mutex);
Philipp Zabel61fc4132012-11-19 17:23:13 +010086 list_del(&rcdev->list);
Hans de Goedec15ddec2016-02-23 18:46:25 +010087 mutex_unlock(&reset_list_mutex);
Philipp Zabel61fc4132012-11-19 17:23:13 +010088}
89EXPORT_SYMBOL_GPL(reset_controller_unregister);
90
91/**
92 * reset_control_reset - reset the controlled device
93 * @rstc: reset controller
94 */
95int reset_control_reset(struct reset_control *rstc)
96{
97 if (rstc->rcdev->ops->reset)
98 return rstc->rcdev->ops->reset(rstc->rcdev, rstc->id);
99
Philipp Zabel39b4da72015-10-29 09:55:00 +0100100 return -ENOTSUPP;
Philipp Zabel61fc4132012-11-19 17:23:13 +0100101}
102EXPORT_SYMBOL_GPL(reset_control_reset);
103
104/**
105 * reset_control_assert - asserts the reset line
106 * @rstc: reset controller
107 */
108int reset_control_assert(struct reset_control *rstc)
109{
110 if (rstc->rcdev->ops->assert)
111 return rstc->rcdev->ops->assert(rstc->rcdev, rstc->id);
112
Philipp Zabel39b4da72015-10-29 09:55:00 +0100113 return -ENOTSUPP;
Philipp Zabel61fc4132012-11-19 17:23:13 +0100114}
115EXPORT_SYMBOL_GPL(reset_control_assert);
116
117/**
118 * reset_control_deassert - deasserts the reset line
119 * @rstc: reset controller
120 */
121int reset_control_deassert(struct reset_control *rstc)
122{
123 if (rstc->rcdev->ops->deassert)
124 return rstc->rcdev->ops->deassert(rstc->rcdev, rstc->id);
125
Philipp Zabel39b4da72015-10-29 09:55:00 +0100126 return -ENOTSUPP;
Philipp Zabel61fc4132012-11-19 17:23:13 +0100127}
128EXPORT_SYMBOL_GPL(reset_control_deassert);
129
130/**
Dinh Nguyen729de412014-10-10 10:21:14 -0500131 * reset_control_status - returns a negative errno if not supported, a
132 * positive value if the reset line is asserted, or zero if the reset
133 * line is not asserted.
134 * @rstc: reset controller
135 */
136int reset_control_status(struct reset_control *rstc)
137{
138 if (rstc->rcdev->ops->status)
139 return rstc->rcdev->ops->status(rstc->rcdev, rstc->id);
140
Philipp Zabel39b4da72015-10-29 09:55:00 +0100141 return -ENOTSUPP;
Dinh Nguyen729de412014-10-10 10:21:14 -0500142}
143EXPORT_SYMBOL_GPL(reset_control_status);
144
Hans de Goedec15ddec2016-02-23 18:46:25 +0100145static struct reset_control *__reset_control_get(
146 struct reset_controller_dev *rcdev,
147 unsigned int index)
148{
149 struct reset_control *rstc;
150
151 lockdep_assert_held(&reset_list_mutex);
152
153 list_for_each_entry(rstc, &rcdev->reset_control_head, list) {
154 if (rstc->id == index) {
155 rstc->refcnt++;
156 return rstc;
157 }
158 }
159
160 rstc = kzalloc(sizeof(*rstc), GFP_KERNEL);
161 if (!rstc)
162 return ERR_PTR(-ENOMEM);
163
164 try_module_get(rcdev->owner);
165
166 rstc->rcdev = rcdev;
167 list_add(&rstc->list, &rcdev->reset_control_head);
168 rstc->id = index;
169 rstc->refcnt = 1;
170
171 return rstc;
172}
173
174static void __reset_control_put(struct reset_control *rstc)
175{
176 lockdep_assert_held(&reset_list_mutex);
177
178 if (--rstc->refcnt)
179 return;
180
181 module_put(rstc->rcdev->owner);
182
183 list_del(&rstc->list);
184 kfree(rstc);
185}
186
Hans de Goede6c96f052016-02-23 18:46:24 +0100187struct reset_control *__of_reset_control_get(struct device_node *node,
188 const char *id, int index)
Philipp Zabel61fc4132012-11-19 17:23:13 +0100189{
Philipp Zabeld056c9b2015-12-08 18:49:53 +0100190 struct reset_control *rstc;
Philipp Zabel61fc4132012-11-19 17:23:13 +0100191 struct reset_controller_dev *r, *rcdev;
192 struct of_phandle_args args;
Philipp Zabel61fc4132012-11-19 17:23:13 +0100193 int rstc_id;
194 int ret;
195
Hans de Goede6c96f052016-02-23 18:46:24 +0100196 if (!node)
197 return ERR_PTR(-EINVAL);
198
199 if (id) {
200 index = of_property_match_string(node,
201 "reset-names", id);
202 if (index < 0)
203 return ERR_PTR(-ENOENT);
204 }
205
Maxime Ripardfc0a5922013-12-20 22:41:07 +0100206 ret = of_parse_phandle_with_args(node, "resets", "#reset-cells",
Philipp Zabel61fc4132012-11-19 17:23:13 +0100207 index, &args);
208 if (ret)
209 return ERR_PTR(ret);
210
Hans de Goedec15ddec2016-02-23 18:46:25 +0100211 mutex_lock(&reset_list_mutex);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100212 rcdev = NULL;
213 list_for_each_entry(r, &reset_controller_list, list) {
214 if (args.np == r->of_node) {
215 rcdev = r;
216 break;
217 }
218 }
219 of_node_put(args.np);
220
221 if (!rcdev) {
Hans de Goedec15ddec2016-02-23 18:46:25 +0100222 mutex_unlock(&reset_list_mutex);
Philipp Zabel3d103022013-07-18 13:55:22 +0200223 return ERR_PTR(-EPROBE_DEFER);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100224 }
225
Maxime Riparde6777742016-01-14 16:24:44 +0100226 if (WARN_ON(args.args_count != rcdev->of_reset_n_cells)) {
Hans de Goedec15ddec2016-02-23 18:46:25 +0100227 mutex_unlock(&reset_list_mutex);
Maxime Riparde6777742016-01-14 16:24:44 +0100228 return ERR_PTR(-EINVAL);
229 }
230
Philipp Zabel61fc4132012-11-19 17:23:13 +0100231 rstc_id = rcdev->of_xlate(rcdev, &args);
232 if (rstc_id < 0) {
Hans de Goedec15ddec2016-02-23 18:46:25 +0100233 mutex_unlock(&reset_list_mutex);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100234 return ERR_PTR(rstc_id);
235 }
236
Hans de Goedec15ddec2016-02-23 18:46:25 +0100237 /* reset_list_mutex also protects the rcdev's reset_control list */
238 rstc = __reset_control_get(rcdev, rstc_id);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100239
Hans de Goedec15ddec2016-02-23 18:46:25 +0100240 mutex_unlock(&reset_list_mutex);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100241
242 return rstc;
243}
Hans de Goede6c96f052016-02-23 18:46:24 +0100244EXPORT_SYMBOL_GPL(__of_reset_control_get);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100245
246/**
247 * reset_control_put - free the reset controller
248 * @rstc: reset controller
249 */
250
251void reset_control_put(struct reset_control *rstc)
252{
253 if (IS_ERR(rstc))
254 return;
255
Hans de Goedec15ddec2016-02-23 18:46:25 +0100256 mutex_lock(&reset_list_mutex);
257 __reset_control_put(rstc);
258 mutex_unlock(&reset_list_mutex);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100259}
260EXPORT_SYMBOL_GPL(reset_control_put);
261
262static void devm_reset_control_release(struct device *dev, void *res)
263{
264 reset_control_put(*(struct reset_control **)res);
265}
266
Hans de Goede6c96f052016-02-23 18:46:24 +0100267struct reset_control *__devm_reset_control_get(struct device *dev,
268 const char *id, int index)
Philipp Zabel61fc4132012-11-19 17:23:13 +0100269{
270 struct reset_control **ptr, *rstc;
271
272 ptr = devres_alloc(devm_reset_control_release, sizeof(*ptr),
273 GFP_KERNEL);
274 if (!ptr)
275 return ERR_PTR(-ENOMEM);
276
Hans de Goede6c96f052016-02-23 18:46:24 +0100277 rstc = __of_reset_control_get(dev ? dev->of_node : NULL, id, index);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100278 if (!IS_ERR(rstc)) {
279 *ptr = rstc;
280 devres_add(dev, ptr);
281 } else {
282 devres_free(ptr);
283 }
284
285 return rstc;
286}
Hans de Goede6c96f052016-02-23 18:46:24 +0100287EXPORT_SYMBOL_GPL(__devm_reset_control_get);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100288
Philipp Zabel61fc4132012-11-19 17:23:13 +0100289/**
290 * device_reset - find reset controller associated with the device
291 * and perform reset
292 * @dev: device to be reset by the controller
293 *
294 * Convenience wrapper for reset_control_get() and reset_control_reset().
295 * This is useful for the common case of devices with single, dedicated reset
296 * lines.
297 */
298int device_reset(struct device *dev)
299{
300 struct reset_control *rstc;
301 int ret;
302
303 rstc = reset_control_get(dev, NULL);
304 if (IS_ERR(rstc))
305 return PTR_ERR(rstc);
306
307 ret = reset_control_reset(rstc);
308
309 reset_control_put(rstc);
310
311 return ret;
312}
313EXPORT_SYMBOL_GPL(device_reset);