blob: 272c1e4ecb5ca3249ebba2ee8edc9d3354cb6dc8 [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 */
Hans de Goede0b522972016-02-23 18:46:26 +010011#include <linux/atomic.h>
Philipp Zabel61fc4132012-11-19 17:23:13 +010012#include <linux/device.h>
13#include <linux/err.h>
14#include <linux/export.h>
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/of.h>
18#include <linux/reset.h>
19#include <linux/reset-controller.h>
20#include <linux/slab.h>
21
Hans de Goedec15ddec2016-02-23 18:46:25 +010022static DEFINE_MUTEX(reset_list_mutex);
Philipp Zabel61fc4132012-11-19 17:23:13 +010023static LIST_HEAD(reset_controller_list);
24
25/**
26 * struct reset_control - a reset control
27 * @rcdev: a pointer to the reset controller device
28 * this reset control belongs to
Hans de Goedec15ddec2016-02-23 18:46:25 +010029 * @list: list entry for the rcdev's reset controller list
Philipp Zabel61fc4132012-11-19 17:23:13 +010030 * @id: ID of the reset controller in the reset
31 * controller device
Hans de Goedec15ddec2016-02-23 18:46:25 +010032 * @refcnt: Number of gets of this reset_control
Hans de Goede0b522972016-02-23 18:46:26 +010033 * @shared: Is this a shared (1), or an exclusive (0) reset_control?
34 * @deassert_cnt: Number of times this reset line has been deasserted
Martin Blumenstingl7da33a32016-11-12 14:13:03 +010035 * @triggered_count: Number of times this reset line has been reset. Currently
36 * only used for shared resets, which means that the value
37 * will be either 0 or 1.
Philipp Zabel61fc4132012-11-19 17:23:13 +010038 */
39struct reset_control {
40 struct reset_controller_dev *rcdev;
Hans de Goedec15ddec2016-02-23 18:46:25 +010041 struct list_head list;
Philipp Zabel61fc4132012-11-19 17:23:13 +010042 unsigned int id;
Hans de Goedec15ddec2016-02-23 18:46:25 +010043 unsigned int refcnt;
Ramiro Oliveiraee48c722017-01-13 17:57:40 +000044 bool shared;
Hans de Goede0b522972016-02-23 18:46:26 +010045 atomic_t deassert_count;
Martin Blumenstingl7da33a32016-11-12 14:13:03 +010046 atomic_t triggered_count;
Philipp Zabel61fc4132012-11-19 17:23:13 +010047};
48
49/**
50 * of_reset_simple_xlate - translate reset_spec to the reset line number
51 * @rcdev: a pointer to the reset controller device
52 * @reset_spec: reset line specifier as found in the device tree
53 * @flags: a flags pointer to fill in (optional)
54 *
55 * This simple translation function should be used for reset controllers
56 * with 1:1 mapping, where reset lines can be indexed by number without gaps.
57 */
Rashika Kheria0c5b2b92013-12-19 14:11:10 +053058static int of_reset_simple_xlate(struct reset_controller_dev *rcdev,
Philipp Zabel61fc4132012-11-19 17:23:13 +010059 const struct of_phandle_args *reset_spec)
60{
Philipp Zabel61fc4132012-11-19 17:23:13 +010061 if (reset_spec->args[0] >= rcdev->nr_resets)
62 return -EINVAL;
63
64 return reset_spec->args[0];
65}
Philipp Zabel61fc4132012-11-19 17:23:13 +010066
67/**
68 * reset_controller_register - register a reset controller device
69 * @rcdev: a pointer to the initialized reset controller device
70 */
71int reset_controller_register(struct reset_controller_dev *rcdev)
72{
73 if (!rcdev->of_xlate) {
74 rcdev->of_reset_n_cells = 1;
75 rcdev->of_xlate = of_reset_simple_xlate;
76 }
77
Hans de Goedec15ddec2016-02-23 18:46:25 +010078 INIT_LIST_HEAD(&rcdev->reset_control_head);
79
80 mutex_lock(&reset_list_mutex);
Philipp Zabel61fc4132012-11-19 17:23:13 +010081 list_add(&rcdev->list, &reset_controller_list);
Hans de Goedec15ddec2016-02-23 18:46:25 +010082 mutex_unlock(&reset_list_mutex);
Philipp Zabel61fc4132012-11-19 17:23:13 +010083
84 return 0;
85}
86EXPORT_SYMBOL_GPL(reset_controller_register);
87
88/**
89 * reset_controller_unregister - unregister a reset controller device
90 * @rcdev: a pointer to the reset controller device
91 */
92void reset_controller_unregister(struct reset_controller_dev *rcdev)
93{
Hans de Goedec15ddec2016-02-23 18:46:25 +010094 mutex_lock(&reset_list_mutex);
Philipp Zabel61fc4132012-11-19 17:23:13 +010095 list_del(&rcdev->list);
Hans de Goedec15ddec2016-02-23 18:46:25 +010096 mutex_unlock(&reset_list_mutex);
Philipp Zabel61fc4132012-11-19 17:23:13 +010097}
98EXPORT_SYMBOL_GPL(reset_controller_unregister);
99
Masahiro Yamada8d5b5d52016-05-01 19:36:57 +0900100static void devm_reset_controller_release(struct device *dev, void *res)
101{
102 reset_controller_unregister(*(struct reset_controller_dev **)res);
103}
104
105/**
106 * devm_reset_controller_register - resource managed reset_controller_register()
107 * @dev: device that is registering this reset controller
108 * @rcdev: a pointer to the initialized reset controller device
109 *
110 * Managed reset_controller_register(). For reset controllers registered by
111 * this function, reset_controller_unregister() is automatically called on
112 * driver detach. See reset_controller_register() for more information.
113 */
114int devm_reset_controller_register(struct device *dev,
115 struct reset_controller_dev *rcdev)
116{
117 struct reset_controller_dev **rcdevp;
118 int ret;
119
120 rcdevp = devres_alloc(devm_reset_controller_release, sizeof(*rcdevp),
121 GFP_KERNEL);
122 if (!rcdevp)
123 return -ENOMEM;
124
125 ret = reset_controller_register(rcdev);
126 if (!ret) {
127 *rcdevp = rcdev;
128 devres_add(dev, rcdevp);
129 } else {
130 devres_free(rcdevp);
131 }
132
133 return ret;
134}
135EXPORT_SYMBOL_GPL(devm_reset_controller_register);
136
Philipp Zabel61fc4132012-11-19 17:23:13 +0100137/**
138 * reset_control_reset - reset the controlled device
139 * @rstc: reset controller
Hans de Goede0b522972016-02-23 18:46:26 +0100140 *
Martin Blumenstingl7da33a32016-11-12 14:13:03 +0100141 * On a shared reset line the actual reset pulse is only triggered once for the
142 * lifetime of the reset_control instance: for all but the first caller this is
143 * a no-op.
144 * Consumers must not use reset_control_(de)assert on shared reset lines when
145 * reset_control_reset has been used.
Philipp Zabel61fc4132012-11-19 17:23:13 +0100146 */
147int reset_control_reset(struct reset_control *rstc)
148{
Martin Blumenstingl7da33a32016-11-12 14:13:03 +0100149 int ret;
150
151 if (WARN_ON(IS_ERR_OR_NULL(rstc)))
Hans de Goede0b522972016-02-23 18:46:26 +0100152 return -EINVAL;
153
Martin Blumenstingl7da33a32016-11-12 14:13:03 +0100154 if (!rstc->rcdev->ops->reset)
155 return -ENOTSUPP;
Philipp Zabel61fc4132012-11-19 17:23:13 +0100156
Martin Blumenstingl7da33a32016-11-12 14:13:03 +0100157 if (rstc->shared) {
158 if (WARN_ON(atomic_read(&rstc->deassert_count) != 0))
159 return -EINVAL;
160
161 if (atomic_inc_return(&rstc->triggered_count) != 1)
162 return 0;
163 }
164
165 ret = rstc->rcdev->ops->reset(rstc->rcdev, rstc->id);
166 if (rstc->shared && !ret)
167 atomic_dec(&rstc->triggered_count);
168
169 return ret;
Philipp Zabel61fc4132012-11-19 17:23:13 +0100170}
171EXPORT_SYMBOL_GPL(reset_control_reset);
172
173/**
174 * reset_control_assert - asserts the reset line
175 * @rstc: reset controller
Hans de Goede0b522972016-02-23 18:46:26 +0100176 *
177 * Calling this on an exclusive reset controller guarantees that the reset
178 * will be asserted. When called on a shared reset controller the line may
179 * still be deasserted, as long as other users keep it so.
180 *
181 * For shared reset controls a driver cannot expect the hw's registers and
182 * internal state to be reset, but must be prepared for this to happen.
Martin Blumenstingl7da33a32016-11-12 14:13:03 +0100183 * Consumers must not use reset_control_reset on shared reset lines when
184 * reset_control_(de)assert has been used.
Philipp Zabel61fc4132012-11-19 17:23:13 +0100185 */
186int reset_control_assert(struct reset_control *rstc)
187{
Philipp Zabela3774e12016-06-20 13:05:14 +0200188 if (WARN_ON(IS_ERR_OR_NULL(rstc)))
189 return -EINVAL;
190
Hans de Goede0b522972016-02-23 18:46:26 +0100191 if (!rstc->rcdev->ops->assert)
192 return -ENOTSUPP;
Philipp Zabel61fc4132012-11-19 17:23:13 +0100193
Hans de Goede0b522972016-02-23 18:46:26 +0100194 if (rstc->shared) {
Martin Blumenstingl7da33a32016-11-12 14:13:03 +0100195 if (WARN_ON(atomic_read(&rstc->triggered_count) != 0))
196 return -EINVAL;
197
Hans de Goede0b522972016-02-23 18:46:26 +0100198 if (WARN_ON(atomic_read(&rstc->deassert_count) == 0))
199 return -EINVAL;
200
201 if (atomic_dec_return(&rstc->deassert_count) != 0)
202 return 0;
203 }
204
205 return rstc->rcdev->ops->assert(rstc->rcdev, rstc->id);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100206}
207EXPORT_SYMBOL_GPL(reset_control_assert);
208
209/**
210 * reset_control_deassert - deasserts the reset line
211 * @rstc: reset controller
Hans de Goede0b522972016-02-23 18:46:26 +0100212 *
213 * After calling this function, the reset is guaranteed to be deasserted.
Martin Blumenstingl7da33a32016-11-12 14:13:03 +0100214 * Consumers must not use reset_control_reset on shared reset lines when
215 * reset_control_(de)assert has been used.
Philipp Zabel61fc4132012-11-19 17:23:13 +0100216 */
217int reset_control_deassert(struct reset_control *rstc)
218{
Philipp Zabela3774e12016-06-20 13:05:14 +0200219 if (WARN_ON(IS_ERR_OR_NULL(rstc)))
220 return -EINVAL;
221
Hans de Goede0b522972016-02-23 18:46:26 +0100222 if (!rstc->rcdev->ops->deassert)
223 return -ENOTSUPP;
Philipp Zabel61fc4132012-11-19 17:23:13 +0100224
Hans de Goede0b522972016-02-23 18:46:26 +0100225 if (rstc->shared) {
Martin Blumenstingl7da33a32016-11-12 14:13:03 +0100226 if (WARN_ON(atomic_read(&rstc->triggered_count) != 0))
227 return -EINVAL;
228
Hans de Goede0b522972016-02-23 18:46:26 +0100229 if (atomic_inc_return(&rstc->deassert_count) != 1)
230 return 0;
231 }
232
233 return rstc->rcdev->ops->deassert(rstc->rcdev, rstc->id);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100234}
235EXPORT_SYMBOL_GPL(reset_control_deassert);
236
237/**
Dinh Nguyen729de412014-10-10 10:21:14 -0500238 * reset_control_status - returns a negative errno if not supported, a
239 * positive value if the reset line is asserted, or zero if the reset
240 * line is not asserted.
241 * @rstc: reset controller
242 */
243int reset_control_status(struct reset_control *rstc)
244{
Philipp Zabela3774e12016-06-20 13:05:14 +0200245 if (WARN_ON(IS_ERR_OR_NULL(rstc)))
246 return -EINVAL;
247
Dinh Nguyen729de412014-10-10 10:21:14 -0500248 if (rstc->rcdev->ops->status)
249 return rstc->rcdev->ops->status(rstc->rcdev, rstc->id);
250
Philipp Zabel39b4da72015-10-29 09:55:00 +0100251 return -ENOTSUPP;
Dinh Nguyen729de412014-10-10 10:21:14 -0500252}
253EXPORT_SYMBOL_GPL(reset_control_status);
254
Hans de Goedec15ddec2016-02-23 18:46:25 +0100255static struct reset_control *__reset_control_get(
256 struct reset_controller_dev *rcdev,
Ramiro Oliveiraee48c722017-01-13 17:57:40 +0000257 unsigned int index, bool shared)
Hans de Goedec15ddec2016-02-23 18:46:25 +0100258{
259 struct reset_control *rstc;
260
261 lockdep_assert_held(&reset_list_mutex);
262
263 list_for_each_entry(rstc, &rcdev->reset_control_head, list) {
264 if (rstc->id == index) {
Hans de Goede0b522972016-02-23 18:46:26 +0100265 if (WARN_ON(!rstc->shared || !shared))
266 return ERR_PTR(-EBUSY);
267
Hans de Goedec15ddec2016-02-23 18:46:25 +0100268 rstc->refcnt++;
269 return rstc;
270 }
271 }
272
273 rstc = kzalloc(sizeof(*rstc), GFP_KERNEL);
274 if (!rstc)
275 return ERR_PTR(-ENOMEM);
276
277 try_module_get(rcdev->owner);
278
279 rstc->rcdev = rcdev;
280 list_add(&rstc->list, &rcdev->reset_control_head);
281 rstc->id = index;
282 rstc->refcnt = 1;
Hans de Goede0b522972016-02-23 18:46:26 +0100283 rstc->shared = shared;
Hans de Goedec15ddec2016-02-23 18:46:25 +0100284
285 return rstc;
286}
287
288static void __reset_control_put(struct reset_control *rstc)
289{
290 lockdep_assert_held(&reset_list_mutex);
291
292 if (--rstc->refcnt)
293 return;
294
295 module_put(rstc->rcdev->owner);
296
297 list_del(&rstc->list);
298 kfree(rstc);
299}
300
Hans de Goede6c96f052016-02-23 18:46:24 +0100301struct reset_control *__of_reset_control_get(struct device_node *node,
Ramiro Oliveiraee48c722017-01-13 17:57:40 +0000302 const char *id, int index, bool shared)
Philipp Zabel61fc4132012-11-19 17:23:13 +0100303{
Philipp Zabeld056c9b2015-12-08 18:49:53 +0100304 struct reset_control *rstc;
Philipp Zabel61fc4132012-11-19 17:23:13 +0100305 struct reset_controller_dev *r, *rcdev;
306 struct of_phandle_args args;
Philipp Zabel61fc4132012-11-19 17:23:13 +0100307 int rstc_id;
308 int ret;
309
Hans de Goede6c96f052016-02-23 18:46:24 +0100310 if (!node)
311 return ERR_PTR(-EINVAL);
312
313 if (id) {
314 index = of_property_match_string(node,
315 "reset-names", id);
316 if (index < 0)
317 return ERR_PTR(-ENOENT);
318 }
319
Maxime Ripardfc0a5922013-12-20 22:41:07 +0100320 ret = of_parse_phandle_with_args(node, "resets", "#reset-cells",
Philipp Zabel61fc4132012-11-19 17:23:13 +0100321 index, &args);
322 if (ret)
323 return ERR_PTR(ret);
324
Hans de Goedec15ddec2016-02-23 18:46:25 +0100325 mutex_lock(&reset_list_mutex);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100326 rcdev = NULL;
327 list_for_each_entry(r, &reset_controller_list, list) {
328 if (args.np == r->of_node) {
329 rcdev = r;
330 break;
331 }
332 }
333 of_node_put(args.np);
334
335 if (!rcdev) {
Hans de Goedec15ddec2016-02-23 18:46:25 +0100336 mutex_unlock(&reset_list_mutex);
Philipp Zabel3d103022013-07-18 13:55:22 +0200337 return ERR_PTR(-EPROBE_DEFER);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100338 }
339
Maxime Riparde6777742016-01-14 16:24:44 +0100340 if (WARN_ON(args.args_count != rcdev->of_reset_n_cells)) {
Hans de Goedec15ddec2016-02-23 18:46:25 +0100341 mutex_unlock(&reset_list_mutex);
Maxime Riparde6777742016-01-14 16:24:44 +0100342 return ERR_PTR(-EINVAL);
343 }
344
Philipp Zabel61fc4132012-11-19 17:23:13 +0100345 rstc_id = rcdev->of_xlate(rcdev, &args);
346 if (rstc_id < 0) {
Hans de Goedec15ddec2016-02-23 18:46:25 +0100347 mutex_unlock(&reset_list_mutex);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100348 return ERR_PTR(rstc_id);
349 }
350
Hans de Goedec15ddec2016-02-23 18:46:25 +0100351 /* reset_list_mutex also protects the rcdev's reset_control list */
Hans de Goede0b522972016-02-23 18:46:26 +0100352 rstc = __reset_control_get(rcdev, rstc_id, shared);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100353
Hans de Goedec15ddec2016-02-23 18:46:25 +0100354 mutex_unlock(&reset_list_mutex);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100355
356 return rstc;
357}
Hans de Goede6c96f052016-02-23 18:46:24 +0100358EXPORT_SYMBOL_GPL(__of_reset_control_get);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100359
360/**
361 * reset_control_put - free the reset controller
362 * @rstc: reset controller
363 */
364
365void reset_control_put(struct reset_control *rstc)
366{
367 if (IS_ERR(rstc))
368 return;
369
Hans de Goedec15ddec2016-02-23 18:46:25 +0100370 mutex_lock(&reset_list_mutex);
371 __reset_control_put(rstc);
372 mutex_unlock(&reset_list_mutex);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100373}
374EXPORT_SYMBOL_GPL(reset_control_put);
375
376static void devm_reset_control_release(struct device *dev, void *res)
377{
378 reset_control_put(*(struct reset_control **)res);
379}
380
Hans de Goede6c96f052016-02-23 18:46:24 +0100381struct reset_control *__devm_reset_control_get(struct device *dev,
Ramiro Oliveiraee48c722017-01-13 17:57:40 +0000382 const char *id, int index, bool shared)
Philipp Zabel61fc4132012-11-19 17:23:13 +0100383{
384 struct reset_control **ptr, *rstc;
385
386 ptr = devres_alloc(devm_reset_control_release, sizeof(*ptr),
387 GFP_KERNEL);
388 if (!ptr)
389 return ERR_PTR(-ENOMEM);
390
Hans de Goede0b522972016-02-23 18:46:26 +0100391 rstc = __of_reset_control_get(dev ? dev->of_node : NULL,
392 id, index, shared);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100393 if (!IS_ERR(rstc)) {
394 *ptr = rstc;
395 devres_add(dev, ptr);
396 } else {
397 devres_free(ptr);
398 }
399
400 return rstc;
401}
Hans de Goede6c96f052016-02-23 18:46:24 +0100402EXPORT_SYMBOL_GPL(__devm_reset_control_get);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100403
Philipp Zabel61fc4132012-11-19 17:23:13 +0100404/**
405 * device_reset - find reset controller associated with the device
406 * and perform reset
407 * @dev: device to be reset by the controller
408 *
409 * Convenience wrapper for reset_control_get() and reset_control_reset().
410 * This is useful for the common case of devices with single, dedicated reset
411 * lines.
412 */
413int device_reset(struct device *dev)
414{
415 struct reset_control *rstc;
416 int ret;
417
418 rstc = reset_control_get(dev, NULL);
419 if (IS_ERR(rstc))
420 return PTR_ERR(rstc);
421
422 ret = reset_control_reset(rstc);
423
424 reset_control_put(rstc);
425
426 return ret;
427}
428EXPORT_SYMBOL_GPL(device_reset);