blob: d0ebca301afc93e7724e4ea61f35cea841a641aa [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
Philipp Zabel61fc4132012-11-19 17:23:13 +010035 */
36struct reset_control {
37 struct reset_controller_dev *rcdev;
Hans de Goedec15ddec2016-02-23 18:46:25 +010038 struct list_head list;
Philipp Zabel61fc4132012-11-19 17:23:13 +010039 unsigned int id;
Hans de Goedec15ddec2016-02-23 18:46:25 +010040 unsigned int refcnt;
Hans de Goede0b522972016-02-23 18:46:26 +010041 int shared;
42 atomic_t deassert_count;
Philipp Zabel61fc4132012-11-19 17:23:13 +010043};
44
45/**
46 * of_reset_simple_xlate - translate reset_spec to the reset line number
47 * @rcdev: a pointer to the reset controller device
48 * @reset_spec: reset line specifier as found in the device tree
49 * @flags: a flags pointer to fill in (optional)
50 *
51 * This simple translation function should be used for reset controllers
52 * with 1:1 mapping, where reset lines can be indexed by number without gaps.
53 */
Rashika Kheria0c5b2b92013-12-19 14:11:10 +053054static int of_reset_simple_xlate(struct reset_controller_dev *rcdev,
Philipp Zabel61fc4132012-11-19 17:23:13 +010055 const struct of_phandle_args *reset_spec)
56{
Philipp Zabel61fc4132012-11-19 17:23:13 +010057 if (reset_spec->args[0] >= rcdev->nr_resets)
58 return -EINVAL;
59
60 return reset_spec->args[0];
61}
Philipp Zabel61fc4132012-11-19 17:23:13 +010062
63/**
64 * reset_controller_register - register a reset controller device
65 * @rcdev: a pointer to the initialized reset controller device
66 */
67int reset_controller_register(struct reset_controller_dev *rcdev)
68{
69 if (!rcdev->of_xlate) {
70 rcdev->of_reset_n_cells = 1;
71 rcdev->of_xlate = of_reset_simple_xlate;
72 }
73
Hans de Goedec15ddec2016-02-23 18:46:25 +010074 INIT_LIST_HEAD(&rcdev->reset_control_head);
75
76 mutex_lock(&reset_list_mutex);
Philipp Zabel61fc4132012-11-19 17:23:13 +010077 list_add(&rcdev->list, &reset_controller_list);
Hans de Goedec15ddec2016-02-23 18:46:25 +010078 mutex_unlock(&reset_list_mutex);
Philipp Zabel61fc4132012-11-19 17:23:13 +010079
80 return 0;
81}
82EXPORT_SYMBOL_GPL(reset_controller_register);
83
84/**
85 * reset_controller_unregister - unregister a reset controller device
86 * @rcdev: a pointer to the reset controller device
87 */
88void reset_controller_unregister(struct reset_controller_dev *rcdev)
89{
Hans de Goedec15ddec2016-02-23 18:46:25 +010090 mutex_lock(&reset_list_mutex);
Philipp Zabel61fc4132012-11-19 17:23:13 +010091 list_del(&rcdev->list);
Hans de Goedec15ddec2016-02-23 18:46:25 +010092 mutex_unlock(&reset_list_mutex);
Philipp Zabel61fc4132012-11-19 17:23:13 +010093}
94EXPORT_SYMBOL_GPL(reset_controller_unregister);
95
Masahiro Yamada8d5b5d52016-05-01 19:36:57 +090096static void devm_reset_controller_release(struct device *dev, void *res)
97{
98 reset_controller_unregister(*(struct reset_controller_dev **)res);
99}
100
101/**
102 * devm_reset_controller_register - resource managed reset_controller_register()
103 * @dev: device that is registering this reset controller
104 * @rcdev: a pointer to the initialized reset controller device
105 *
106 * Managed reset_controller_register(). For reset controllers registered by
107 * this function, reset_controller_unregister() is automatically called on
108 * driver detach. See reset_controller_register() for more information.
109 */
110int devm_reset_controller_register(struct device *dev,
111 struct reset_controller_dev *rcdev)
112{
113 struct reset_controller_dev **rcdevp;
114 int ret;
115
116 rcdevp = devres_alloc(devm_reset_controller_release, sizeof(*rcdevp),
117 GFP_KERNEL);
118 if (!rcdevp)
119 return -ENOMEM;
120
121 ret = reset_controller_register(rcdev);
122 if (!ret) {
123 *rcdevp = rcdev;
124 devres_add(dev, rcdevp);
125 } else {
126 devres_free(rcdevp);
127 }
128
129 return ret;
130}
131EXPORT_SYMBOL_GPL(devm_reset_controller_register);
132
Philipp Zabel61fc4132012-11-19 17:23:13 +0100133/**
134 * reset_control_reset - reset the controlled device
135 * @rstc: reset controller
Hans de Goede0b522972016-02-23 18:46:26 +0100136 *
137 * Calling this on a shared reset controller is an error.
Ramiro Oliveiraca58e3b2017-01-13 17:57:41 +0000138 *
139 * If rstc is NULL it is an optional reset and the function will just
140 * return 0.
Philipp Zabel61fc4132012-11-19 17:23:13 +0100141 */
142int reset_control_reset(struct reset_control *rstc)
143{
Ramiro Oliveiraca58e3b2017-01-13 17:57:41 +0000144 if (!rstc)
145 return 0;
146
147 if (WARN_ON(IS_ERR(rstc)))
Hans de Goede0b522972016-02-23 18:46:26 +0100148 return -EINVAL;
149
Philipp Zabel61fc4132012-11-19 17:23:13 +0100150 if (rstc->rcdev->ops->reset)
151 return rstc->rcdev->ops->reset(rstc->rcdev, rstc->id);
152
Philipp Zabel39b4da72015-10-29 09:55:00 +0100153 return -ENOTSUPP;
Philipp Zabel61fc4132012-11-19 17:23:13 +0100154}
155EXPORT_SYMBOL_GPL(reset_control_reset);
156
157/**
158 * reset_control_assert - asserts the reset line
159 * @rstc: reset controller
Hans de Goede0b522972016-02-23 18:46:26 +0100160 *
161 * Calling this on an exclusive reset controller guarantees that the reset
162 * will be asserted. When called on a shared reset controller the line may
163 * still be deasserted, as long as other users keep it so.
164 *
165 * For shared reset controls a driver cannot expect the hw's registers and
166 * internal state to be reset, but must be prepared for this to happen.
Ramiro Oliveiraca58e3b2017-01-13 17:57:41 +0000167 *
168 * If rstc is NULL it is an optional reset and the function will just
169 * return 0.
Philipp Zabel61fc4132012-11-19 17:23:13 +0100170 */
171int reset_control_assert(struct reset_control *rstc)
172{
Ramiro Oliveiraca58e3b2017-01-13 17:57:41 +0000173 if (!rstc)
174 return 0;
175
176 if (WARN_ON(IS_ERR(rstc)))
Philipp Zabela3774e12016-06-20 13:05:14 +0200177 return -EINVAL;
178
Hans de Goede0b522972016-02-23 18:46:26 +0100179 if (!rstc->rcdev->ops->assert)
180 return -ENOTSUPP;
Philipp Zabel61fc4132012-11-19 17:23:13 +0100181
Hans de Goede0b522972016-02-23 18:46:26 +0100182 if (rstc->shared) {
183 if (WARN_ON(atomic_read(&rstc->deassert_count) == 0))
184 return -EINVAL;
185
186 if (atomic_dec_return(&rstc->deassert_count) != 0)
187 return 0;
188 }
189
190 return rstc->rcdev->ops->assert(rstc->rcdev, rstc->id);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100191}
192EXPORT_SYMBOL_GPL(reset_control_assert);
193
194/**
195 * reset_control_deassert - deasserts the reset line
196 * @rstc: reset controller
Hans de Goede0b522972016-02-23 18:46:26 +0100197 *
198 * After calling this function, the reset is guaranteed to be deasserted.
Ramiro Oliveiraca58e3b2017-01-13 17:57:41 +0000199 *
200 * If rstc is NULL it is an optional reset and the function will just
201 * return 0.
Philipp Zabel61fc4132012-11-19 17:23:13 +0100202 */
203int reset_control_deassert(struct reset_control *rstc)
204{
Ramiro Oliveiraca58e3b2017-01-13 17:57:41 +0000205 if (!rstc)
206 return 0;
207
208 if (WARN_ON(IS_ERR(rstc)))
Philipp Zabela3774e12016-06-20 13:05:14 +0200209 return -EINVAL;
210
Hans de Goede0b522972016-02-23 18:46:26 +0100211 if (!rstc->rcdev->ops->deassert)
212 return -ENOTSUPP;
Philipp Zabel61fc4132012-11-19 17:23:13 +0100213
Hans de Goede0b522972016-02-23 18:46:26 +0100214 if (rstc->shared) {
215 if (atomic_inc_return(&rstc->deassert_count) != 1)
216 return 0;
217 }
218
219 return rstc->rcdev->ops->deassert(rstc->rcdev, rstc->id);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100220}
221EXPORT_SYMBOL_GPL(reset_control_deassert);
222
223/**
Dinh Nguyen729de412014-10-10 10:21:14 -0500224 * reset_control_status - returns a negative errno if not supported, a
225 * positive value if the reset line is asserted, or zero if the reset
Ramiro Oliveiraca58e3b2017-01-13 17:57:41 +0000226 * line is not asserted or if the desc is NULL (optional reset).
Dinh Nguyen729de412014-10-10 10:21:14 -0500227 * @rstc: reset controller
228 */
229int reset_control_status(struct reset_control *rstc)
230{
Ramiro Oliveiraca58e3b2017-01-13 17:57:41 +0000231 if (!rstc)
232 return 0;
233
234 if (WARN_ON(IS_ERR(rstc)))
Philipp Zabela3774e12016-06-20 13:05:14 +0200235 return -EINVAL;
236
Dinh Nguyen729de412014-10-10 10:21:14 -0500237 if (rstc->rcdev->ops->status)
238 return rstc->rcdev->ops->status(rstc->rcdev, rstc->id);
239
Philipp Zabel39b4da72015-10-29 09:55:00 +0100240 return -ENOTSUPP;
Dinh Nguyen729de412014-10-10 10:21:14 -0500241}
242EXPORT_SYMBOL_GPL(reset_control_status);
243
Philipp Zabelee6e7fb2016-02-05 13:41:39 +0100244static struct reset_control *__reset_control_get_internal(
Hans de Goedec15ddec2016-02-23 18:46:25 +0100245 struct reset_controller_dev *rcdev,
Hans de Goede0b522972016-02-23 18:46:26 +0100246 unsigned int index, int shared)
Hans de Goedec15ddec2016-02-23 18:46:25 +0100247{
248 struct reset_control *rstc;
249
250 lockdep_assert_held(&reset_list_mutex);
251
252 list_for_each_entry(rstc, &rcdev->reset_control_head, list) {
253 if (rstc->id == index) {
Hans de Goede0b522972016-02-23 18:46:26 +0100254 if (WARN_ON(!rstc->shared || !shared))
255 return ERR_PTR(-EBUSY);
256
Hans de Goedec15ddec2016-02-23 18:46:25 +0100257 rstc->refcnt++;
258 return rstc;
259 }
260 }
261
262 rstc = kzalloc(sizeof(*rstc), GFP_KERNEL);
263 if (!rstc)
264 return ERR_PTR(-ENOMEM);
265
266 try_module_get(rcdev->owner);
267
268 rstc->rcdev = rcdev;
269 list_add(&rstc->list, &rcdev->reset_control_head);
270 rstc->id = index;
271 rstc->refcnt = 1;
Hans de Goede0b522972016-02-23 18:46:26 +0100272 rstc->shared = shared;
Hans de Goedec15ddec2016-02-23 18:46:25 +0100273
274 return rstc;
275}
276
Philipp Zabelee6e7fb2016-02-05 13:41:39 +0100277static void __reset_control_put_internal(struct reset_control *rstc)
Hans de Goedec15ddec2016-02-23 18:46:25 +0100278{
279 lockdep_assert_held(&reset_list_mutex);
280
281 if (--rstc->refcnt)
282 return;
283
284 module_put(rstc->rcdev->owner);
285
286 list_del(&rstc->list);
287 kfree(rstc);
288}
289
Hans de Goede6c96f052016-02-23 18:46:24 +0100290struct reset_control *__of_reset_control_get(struct device_node *node,
Ramiro Oliveiraca58e3b2017-01-13 17:57:41 +0000291 const char *id, int index, bool shared,
292 bool optional)
Philipp Zabel61fc4132012-11-19 17:23:13 +0100293{
Philipp Zabeld056c9b2015-12-08 18:49:53 +0100294 struct reset_control *rstc;
Philipp Zabel61fc4132012-11-19 17:23:13 +0100295 struct reset_controller_dev *r, *rcdev;
296 struct of_phandle_args args;
Philipp Zabel61fc4132012-11-19 17:23:13 +0100297 int rstc_id;
298 int ret;
299
Hans de Goede6c96f052016-02-23 18:46:24 +0100300 if (!node)
301 return ERR_PTR(-EINVAL);
302
303 if (id) {
304 index = of_property_match_string(node,
305 "reset-names", id);
Ramiro Oliveiraca58e3b2017-01-13 17:57:41 +0000306 if (index == -EILSEQ)
307 return ERR_PTR(index);
Hans de Goede6c96f052016-02-23 18:46:24 +0100308 if (index < 0)
Ramiro Oliveiraca58e3b2017-01-13 17:57:41 +0000309 return optional ? NULL : ERR_PTR(-ENOENT);
Hans de Goede6c96f052016-02-23 18:46:24 +0100310 }
311
Maxime Ripardfc0a5922013-12-20 22:41:07 +0100312 ret = of_parse_phandle_with_args(node, "resets", "#reset-cells",
Philipp Zabel61fc4132012-11-19 17:23:13 +0100313 index, &args);
Ramiro Oliveiraca58e3b2017-01-13 17:57:41 +0000314 if (ret == -EINVAL)
Philipp Zabel61fc4132012-11-19 17:23:13 +0100315 return ERR_PTR(ret);
Ramiro Oliveiraca58e3b2017-01-13 17:57:41 +0000316 if (ret)
317 return optional ? NULL : ERR_PTR(ret);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100318
Hans de Goedec15ddec2016-02-23 18:46:25 +0100319 mutex_lock(&reset_list_mutex);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100320 rcdev = NULL;
321 list_for_each_entry(r, &reset_controller_list, list) {
322 if (args.np == r->of_node) {
323 rcdev = r;
324 break;
325 }
326 }
Philipp Zabel61fc4132012-11-19 17:23:13 +0100327
328 if (!rcdev) {
Geert Uytterhoeven1012e972018-10-08 13:14:35 +0200329 rstc = ERR_PTR(-EPROBE_DEFER);
330 goto out;
Philipp Zabel61fc4132012-11-19 17:23:13 +0100331 }
332
Maxime Riparde6777742016-01-14 16:24:44 +0100333 if (WARN_ON(args.args_count != rcdev->of_reset_n_cells)) {
Geert Uytterhoeven1012e972018-10-08 13:14:35 +0200334 rstc = ERR_PTR(-EINVAL);
335 goto out;
Maxime Riparde6777742016-01-14 16:24:44 +0100336 }
337
Philipp Zabel61fc4132012-11-19 17:23:13 +0100338 rstc_id = rcdev->of_xlate(rcdev, &args);
339 if (rstc_id < 0) {
Geert Uytterhoeven1012e972018-10-08 13:14:35 +0200340 rstc = ERR_PTR(rstc_id);
341 goto out;
Philipp Zabel61fc4132012-11-19 17:23:13 +0100342 }
343
Hans de Goedec15ddec2016-02-23 18:46:25 +0100344 /* reset_list_mutex also protects the rcdev's reset_control list */
Philipp Zabelee6e7fb2016-02-05 13:41:39 +0100345 rstc = __reset_control_get_internal(rcdev, rstc_id, shared);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100346
Geert Uytterhoeven1012e972018-10-08 13:14:35 +0200347out:
Hans de Goedec15ddec2016-02-23 18:46:25 +0100348 mutex_unlock(&reset_list_mutex);
Geert Uytterhoeven1012e972018-10-08 13:14:35 +0200349 of_node_put(args.np);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100350
351 return rstc;
352}
Hans de Goede6c96f052016-02-23 18:46:24 +0100353EXPORT_SYMBOL_GPL(__of_reset_control_get);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100354
Philipp Zabelee6e7fb2016-02-05 13:41:39 +0100355struct reset_control *__reset_control_get(struct device *dev, const char *id,
356 int index, bool shared, bool optional)
357{
358 if (dev->of_node)
359 return __of_reset_control_get(dev->of_node, id, index, shared,
360 optional);
361
362 return optional ? NULL : ERR_PTR(-EINVAL);
363}
364EXPORT_SYMBOL_GPL(__reset_control_get);
365
Philipp Zabel61fc4132012-11-19 17:23:13 +0100366/**
367 * reset_control_put - free the reset controller
368 * @rstc: reset controller
369 */
370
371void reset_control_put(struct reset_control *rstc)
372{
Heiner Kallweitd4b89ec2017-02-01 08:05:22 +0100373 if (IS_ERR_OR_NULL(rstc))
Philipp Zabel61fc4132012-11-19 17:23:13 +0100374 return;
375
Hans de Goedec15ddec2016-02-23 18:46:25 +0100376 mutex_lock(&reset_list_mutex);
Philipp Zabelee6e7fb2016-02-05 13:41:39 +0100377 __reset_control_put_internal(rstc);
Hans de Goedec15ddec2016-02-23 18:46:25 +0100378 mutex_unlock(&reset_list_mutex);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100379}
380EXPORT_SYMBOL_GPL(reset_control_put);
381
382static void devm_reset_control_release(struct device *dev, void *res)
383{
384 reset_control_put(*(struct reset_control **)res);
385}
386
Hans de Goede6c96f052016-02-23 18:46:24 +0100387struct reset_control *__devm_reset_control_get(struct device *dev,
Ramiro Oliveiraca58e3b2017-01-13 17:57:41 +0000388 const char *id, int index, bool shared,
389 bool optional)
Philipp Zabel61fc4132012-11-19 17:23:13 +0100390{
391 struct reset_control **ptr, *rstc;
392
393 ptr = devres_alloc(devm_reset_control_release, sizeof(*ptr),
394 GFP_KERNEL);
395 if (!ptr)
396 return ERR_PTR(-ENOMEM);
397
Philipp Zabelee6e7fb2016-02-05 13:41:39 +0100398 rstc = __reset_control_get(dev, id, index, shared, optional);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100399 if (!IS_ERR(rstc)) {
400 *ptr = rstc;
401 devres_add(dev, ptr);
402 } else {
403 devres_free(ptr);
404 }
405
406 return rstc;
407}
Hans de Goede6c96f052016-02-23 18:46:24 +0100408EXPORT_SYMBOL_GPL(__devm_reset_control_get);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100409
Philipp Zabel61fc4132012-11-19 17:23:13 +0100410/**
411 * device_reset - find reset controller associated with the device
412 * and perform reset
413 * @dev: device to be reset by the controller
Masahiro Yamadaf109deb2017-10-29 01:50:06 +0900414 * @optional: whether it is optional to reset the device
Philipp Zabel61fc4132012-11-19 17:23:13 +0100415 *
Masahiro Yamadaf109deb2017-10-29 01:50:06 +0900416 * Convenience wrapper for __reset_control_get() and reset_control_reset().
Philipp Zabel61fc4132012-11-19 17:23:13 +0100417 * This is useful for the common case of devices with single, dedicated reset
418 * lines.
419 */
Masahiro Yamadaf109deb2017-10-29 01:50:06 +0900420int __device_reset(struct device *dev, bool optional)
Philipp Zabel61fc4132012-11-19 17:23:13 +0100421{
422 struct reset_control *rstc;
423 int ret;
424
Masahiro Yamadaf109deb2017-10-29 01:50:06 +0900425 rstc = __reset_control_get(dev, NULL, 0, 0, optional);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100426 if (IS_ERR(rstc))
427 return PTR_ERR(rstc);
428
429 ret = reset_control_reset(rstc);
430
431 reset_control_put(rstc);
432
433 return ret;
434}
Masahiro Yamadaf109deb2017-10-29 01:50:06 +0900435EXPORT_SYMBOL_GPL(__device_reset);