blob: 395dc9ce492e775336b2a17856dc4392981b4bf8 [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.
Philipp Zabel61fc4132012-11-19 17:23:13 +0100138 */
139int reset_control_reset(struct reset_control *rstc)
140{
Hans de Goede0b522972016-02-23 18:46:26 +0100141 if (WARN_ON(rstc->shared))
142 return -EINVAL;
143
Philipp Zabel61fc4132012-11-19 17:23:13 +0100144 if (rstc->rcdev->ops->reset)
145 return rstc->rcdev->ops->reset(rstc->rcdev, rstc->id);
146
Philipp Zabel39b4da72015-10-29 09:55:00 +0100147 return -ENOTSUPP;
Philipp Zabel61fc4132012-11-19 17:23:13 +0100148}
149EXPORT_SYMBOL_GPL(reset_control_reset);
150
151/**
152 * reset_control_assert - asserts the reset line
153 * @rstc: reset controller
Hans de Goede0b522972016-02-23 18:46:26 +0100154 *
155 * Calling this on an exclusive reset controller guarantees that the reset
156 * will be asserted. When called on a shared reset controller the line may
157 * still be deasserted, as long as other users keep it so.
158 *
159 * For shared reset controls a driver cannot expect the hw's registers and
160 * internal state to be reset, but must be prepared for this to happen.
Philipp Zabel61fc4132012-11-19 17:23:13 +0100161 */
162int reset_control_assert(struct reset_control *rstc)
163{
Hans de Goede0b522972016-02-23 18:46:26 +0100164 if (!rstc->rcdev->ops->assert)
165 return -ENOTSUPP;
Philipp Zabel61fc4132012-11-19 17:23:13 +0100166
Hans de Goede0b522972016-02-23 18:46:26 +0100167 if (rstc->shared) {
168 if (WARN_ON(atomic_read(&rstc->deassert_count) == 0))
169 return -EINVAL;
170
171 if (atomic_dec_return(&rstc->deassert_count) != 0)
172 return 0;
173 }
174
175 return rstc->rcdev->ops->assert(rstc->rcdev, rstc->id);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100176}
177EXPORT_SYMBOL_GPL(reset_control_assert);
178
179/**
180 * reset_control_deassert - deasserts the reset line
181 * @rstc: reset controller
Hans de Goede0b522972016-02-23 18:46:26 +0100182 *
183 * After calling this function, the reset is guaranteed to be deasserted.
Philipp Zabel61fc4132012-11-19 17:23:13 +0100184 */
185int reset_control_deassert(struct reset_control *rstc)
186{
Hans de Goede0b522972016-02-23 18:46:26 +0100187 if (!rstc->rcdev->ops->deassert)
188 return -ENOTSUPP;
Philipp Zabel61fc4132012-11-19 17:23:13 +0100189
Hans de Goede0b522972016-02-23 18:46:26 +0100190 if (rstc->shared) {
191 if (atomic_inc_return(&rstc->deassert_count) != 1)
192 return 0;
193 }
194
195 return rstc->rcdev->ops->deassert(rstc->rcdev, rstc->id);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100196}
197EXPORT_SYMBOL_GPL(reset_control_deassert);
198
199/**
Dinh Nguyen729de412014-10-10 10:21:14 -0500200 * reset_control_status - returns a negative errno if not supported, a
201 * positive value if the reset line is asserted, or zero if the reset
202 * line is not asserted.
203 * @rstc: reset controller
204 */
205int reset_control_status(struct reset_control *rstc)
206{
207 if (rstc->rcdev->ops->status)
208 return rstc->rcdev->ops->status(rstc->rcdev, rstc->id);
209
Philipp Zabel39b4da72015-10-29 09:55:00 +0100210 return -ENOTSUPP;
Dinh Nguyen729de412014-10-10 10:21:14 -0500211}
212EXPORT_SYMBOL_GPL(reset_control_status);
213
Hans de Goedec15ddec2016-02-23 18:46:25 +0100214static struct reset_control *__reset_control_get(
215 struct reset_controller_dev *rcdev,
Hans de Goede0b522972016-02-23 18:46:26 +0100216 unsigned int index, int shared)
Hans de Goedec15ddec2016-02-23 18:46:25 +0100217{
218 struct reset_control *rstc;
219
220 lockdep_assert_held(&reset_list_mutex);
221
222 list_for_each_entry(rstc, &rcdev->reset_control_head, list) {
223 if (rstc->id == index) {
Hans de Goede0b522972016-02-23 18:46:26 +0100224 if (WARN_ON(!rstc->shared || !shared))
225 return ERR_PTR(-EBUSY);
226
Hans de Goedec15ddec2016-02-23 18:46:25 +0100227 rstc->refcnt++;
228 return rstc;
229 }
230 }
231
232 rstc = kzalloc(sizeof(*rstc), GFP_KERNEL);
233 if (!rstc)
234 return ERR_PTR(-ENOMEM);
235
236 try_module_get(rcdev->owner);
237
238 rstc->rcdev = rcdev;
239 list_add(&rstc->list, &rcdev->reset_control_head);
240 rstc->id = index;
241 rstc->refcnt = 1;
Hans de Goede0b522972016-02-23 18:46:26 +0100242 rstc->shared = shared;
Hans de Goedec15ddec2016-02-23 18:46:25 +0100243
244 return rstc;
245}
246
247static void __reset_control_put(struct reset_control *rstc)
248{
249 lockdep_assert_held(&reset_list_mutex);
250
251 if (--rstc->refcnt)
252 return;
253
254 module_put(rstc->rcdev->owner);
255
256 list_del(&rstc->list);
257 kfree(rstc);
258}
259
Hans de Goede6c96f052016-02-23 18:46:24 +0100260struct reset_control *__of_reset_control_get(struct device_node *node,
Hans de Goede0b522972016-02-23 18:46:26 +0100261 const char *id, int index, int shared)
Philipp Zabel61fc4132012-11-19 17:23:13 +0100262{
Philipp Zabeld056c9b2015-12-08 18:49:53 +0100263 struct reset_control *rstc;
Philipp Zabel61fc4132012-11-19 17:23:13 +0100264 struct reset_controller_dev *r, *rcdev;
265 struct of_phandle_args args;
Philipp Zabel61fc4132012-11-19 17:23:13 +0100266 int rstc_id;
267 int ret;
268
Hans de Goede6c96f052016-02-23 18:46:24 +0100269 if (!node)
270 return ERR_PTR(-EINVAL);
271
272 if (id) {
273 index = of_property_match_string(node,
274 "reset-names", id);
275 if (index < 0)
276 return ERR_PTR(-ENOENT);
277 }
278
Maxime Ripardfc0a5922013-12-20 22:41:07 +0100279 ret = of_parse_phandle_with_args(node, "resets", "#reset-cells",
Philipp Zabel61fc4132012-11-19 17:23:13 +0100280 index, &args);
281 if (ret)
282 return ERR_PTR(ret);
283
Hans de Goedec15ddec2016-02-23 18:46:25 +0100284 mutex_lock(&reset_list_mutex);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100285 rcdev = NULL;
286 list_for_each_entry(r, &reset_controller_list, list) {
287 if (args.np == r->of_node) {
288 rcdev = r;
289 break;
290 }
291 }
292 of_node_put(args.np);
293
294 if (!rcdev) {
Hans de Goedec15ddec2016-02-23 18:46:25 +0100295 mutex_unlock(&reset_list_mutex);
Philipp Zabel3d103022013-07-18 13:55:22 +0200296 return ERR_PTR(-EPROBE_DEFER);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100297 }
298
Maxime Riparde6777742016-01-14 16:24:44 +0100299 if (WARN_ON(args.args_count != rcdev->of_reset_n_cells)) {
Hans de Goedec15ddec2016-02-23 18:46:25 +0100300 mutex_unlock(&reset_list_mutex);
Maxime Riparde6777742016-01-14 16:24:44 +0100301 return ERR_PTR(-EINVAL);
302 }
303
Philipp Zabel61fc4132012-11-19 17:23:13 +0100304 rstc_id = rcdev->of_xlate(rcdev, &args);
305 if (rstc_id < 0) {
Hans de Goedec15ddec2016-02-23 18:46:25 +0100306 mutex_unlock(&reset_list_mutex);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100307 return ERR_PTR(rstc_id);
308 }
309
Hans de Goedec15ddec2016-02-23 18:46:25 +0100310 /* reset_list_mutex also protects the rcdev's reset_control list */
Hans de Goede0b522972016-02-23 18:46:26 +0100311 rstc = __reset_control_get(rcdev, rstc_id, shared);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100312
Hans de Goedec15ddec2016-02-23 18:46:25 +0100313 mutex_unlock(&reset_list_mutex);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100314
315 return rstc;
316}
Hans de Goede6c96f052016-02-23 18:46:24 +0100317EXPORT_SYMBOL_GPL(__of_reset_control_get);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100318
319/**
320 * reset_control_put - free the reset controller
321 * @rstc: reset controller
322 */
323
324void reset_control_put(struct reset_control *rstc)
325{
326 if (IS_ERR(rstc))
327 return;
328
Hans de Goedec15ddec2016-02-23 18:46:25 +0100329 mutex_lock(&reset_list_mutex);
330 __reset_control_put(rstc);
331 mutex_unlock(&reset_list_mutex);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100332}
333EXPORT_SYMBOL_GPL(reset_control_put);
334
335static void devm_reset_control_release(struct device *dev, void *res)
336{
337 reset_control_put(*(struct reset_control **)res);
338}
339
Hans de Goede6c96f052016-02-23 18:46:24 +0100340struct reset_control *__devm_reset_control_get(struct device *dev,
Hans de Goede0b522972016-02-23 18:46:26 +0100341 const char *id, int index, int shared)
Philipp Zabel61fc4132012-11-19 17:23:13 +0100342{
343 struct reset_control **ptr, *rstc;
344
345 ptr = devres_alloc(devm_reset_control_release, sizeof(*ptr),
346 GFP_KERNEL);
347 if (!ptr)
348 return ERR_PTR(-ENOMEM);
349
Hans de Goede0b522972016-02-23 18:46:26 +0100350 rstc = __of_reset_control_get(dev ? dev->of_node : NULL,
351 id, index, shared);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100352 if (!IS_ERR(rstc)) {
353 *ptr = rstc;
354 devres_add(dev, ptr);
355 } else {
356 devres_free(ptr);
357 }
358
359 return rstc;
360}
Hans de Goede6c96f052016-02-23 18:46:24 +0100361EXPORT_SYMBOL_GPL(__devm_reset_control_get);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100362
Philipp Zabel61fc4132012-11-19 17:23:13 +0100363/**
364 * device_reset - find reset controller associated with the device
365 * and perform reset
366 * @dev: device to be reset by the controller
367 *
368 * Convenience wrapper for reset_control_get() and reset_control_reset().
369 * This is useful for the common case of devices with single, dedicated reset
370 * lines.
371 */
372int device_reset(struct device *dev)
373{
374 struct reset_control *rstc;
375 int ret;
376
377 rstc = reset_control_get(dev, NULL);
378 if (IS_ERR(rstc))
379 return PTR_ERR(rstc);
380
381 ret = reset_control_reset(rstc);
382
383 reset_control_put(rstc);
384
385 return ret;
386}
387EXPORT_SYMBOL_GPL(device_reset);