blob: f94da91c22affccf4c81ea6e30467adbd27c7d71 [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>
Philipp Zabeld25e4332017-05-31 17:42:29 +020016#include <linux/kref.h>
Philipp Zabel61fc4132012-11-19 17:23:13 +010017#include <linux/module.h>
18#include <linux/of.h>
19#include <linux/reset.h>
20#include <linux/reset-controller.h>
21#include <linux/slab.h>
22
Hans de Goedec15ddec2016-02-23 18:46:25 +010023static DEFINE_MUTEX(reset_list_mutex);
Philipp Zabel61fc4132012-11-19 17:23:13 +010024static LIST_HEAD(reset_controller_list);
25
Bartosz Golaszewski6691dff2018-02-28 14:08:57 +010026static DEFINE_MUTEX(reset_lookup_mutex);
27static LIST_HEAD(reset_lookup_list);
28
Philipp Zabel61fc4132012-11-19 17:23:13 +010029/**
30 * struct reset_control - a reset control
31 * @rcdev: a pointer to the reset controller device
32 * this reset control belongs to
Hans de Goedec15ddec2016-02-23 18:46:25 +010033 * @list: list entry for the rcdev's reset controller list
Philipp Zabel61fc4132012-11-19 17:23:13 +010034 * @id: ID of the reset controller in the reset
35 * controller device
Hans de Goedec15ddec2016-02-23 18:46:25 +010036 * @refcnt: Number of gets of this reset_control
Philipp Zabelc84b0322019-02-21 16:25:53 +010037 * @acquired: Only one reset_control may be acquired for a given rcdev and id.
Hans de Goede0b522972016-02-23 18:46:26 +010038 * @shared: Is this a shared (1), or an exclusive (0) reset_control?
39 * @deassert_cnt: Number of times this reset line has been deasserted
Martin Blumenstingl7da33a32016-11-12 14:13:03 +010040 * @triggered_count: Number of times this reset line has been reset. Currently
41 * only used for shared resets, which means that the value
42 * will be either 0 or 1.
Philipp Zabel61fc4132012-11-19 17:23:13 +010043 */
44struct reset_control {
45 struct reset_controller_dev *rcdev;
Hans de Goedec15ddec2016-02-23 18:46:25 +010046 struct list_head list;
Philipp Zabel61fc4132012-11-19 17:23:13 +010047 unsigned int id;
Philipp Zabeld25e4332017-05-31 17:42:29 +020048 struct kref refcnt;
Philipp Zabelc84b0322019-02-21 16:25:53 +010049 bool acquired;
Ramiro Oliveiraee48c722017-01-13 17:57:40 +000050 bool shared;
Vivek Gautam17c82e22017-05-22 16:53:25 +053051 bool array;
Hans de Goede0b522972016-02-23 18:46:26 +010052 atomic_t deassert_count;
Martin Blumenstingl7da33a32016-11-12 14:13:03 +010053 atomic_t triggered_count;
Philipp Zabel61fc4132012-11-19 17:23:13 +010054};
55
56/**
Vivek Gautam17c82e22017-05-22 16:53:25 +053057 * struct reset_control_array - an array of reset controls
58 * @base: reset control for compatibility with reset control API functions
59 * @num_rstcs: number of reset controls
60 * @rstc: array of reset controls
61 */
62struct reset_control_array {
63 struct reset_control base;
64 unsigned int num_rstcs;
65 struct reset_control *rstc[];
66};
67
Philipp Zabelc84b0322019-02-21 16:25:53 +010068static const char *rcdev_name(struct reset_controller_dev *rcdev)
69{
70 if (rcdev->dev)
71 return dev_name(rcdev->dev);
72
73 if (rcdev->of_node)
74 return rcdev->of_node->full_name;
75
76 return NULL;
77}
78
Vivek Gautam17c82e22017-05-22 16:53:25 +053079/**
Philipp Zabel61fc4132012-11-19 17:23:13 +010080 * of_reset_simple_xlate - translate reset_spec to the reset line number
81 * @rcdev: a pointer to the reset controller device
82 * @reset_spec: reset line specifier as found in the device tree
83 * @flags: a flags pointer to fill in (optional)
84 *
85 * This simple translation function should be used for reset controllers
86 * with 1:1 mapping, where reset lines can be indexed by number without gaps.
87 */
Rashika Kheria0c5b2b92013-12-19 14:11:10 +053088static int of_reset_simple_xlate(struct reset_controller_dev *rcdev,
Philipp Zabel61fc4132012-11-19 17:23:13 +010089 const struct of_phandle_args *reset_spec)
90{
Philipp Zabel61fc4132012-11-19 17:23:13 +010091 if (reset_spec->args[0] >= rcdev->nr_resets)
92 return -EINVAL;
93
94 return reset_spec->args[0];
95}
Philipp Zabel61fc4132012-11-19 17:23:13 +010096
97/**
98 * reset_controller_register - register a reset controller device
99 * @rcdev: a pointer to the initialized reset controller device
100 */
101int reset_controller_register(struct reset_controller_dev *rcdev)
102{
103 if (!rcdev->of_xlate) {
104 rcdev->of_reset_n_cells = 1;
105 rcdev->of_xlate = of_reset_simple_xlate;
106 }
107
Hans de Goedec15ddec2016-02-23 18:46:25 +0100108 INIT_LIST_HEAD(&rcdev->reset_control_head);
109
110 mutex_lock(&reset_list_mutex);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100111 list_add(&rcdev->list, &reset_controller_list);
Hans de Goedec15ddec2016-02-23 18:46:25 +0100112 mutex_unlock(&reset_list_mutex);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100113
114 return 0;
115}
116EXPORT_SYMBOL_GPL(reset_controller_register);
117
118/**
119 * reset_controller_unregister - unregister a reset controller device
120 * @rcdev: a pointer to the reset controller device
121 */
122void reset_controller_unregister(struct reset_controller_dev *rcdev)
123{
Hans de Goedec15ddec2016-02-23 18:46:25 +0100124 mutex_lock(&reset_list_mutex);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100125 list_del(&rcdev->list);
Hans de Goedec15ddec2016-02-23 18:46:25 +0100126 mutex_unlock(&reset_list_mutex);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100127}
128EXPORT_SYMBOL_GPL(reset_controller_unregister);
129
Masahiro Yamada8d5b5d52016-05-01 19:36:57 +0900130static void devm_reset_controller_release(struct device *dev, void *res)
131{
132 reset_controller_unregister(*(struct reset_controller_dev **)res);
133}
134
135/**
136 * devm_reset_controller_register - resource managed reset_controller_register()
137 * @dev: device that is registering this reset controller
138 * @rcdev: a pointer to the initialized reset controller device
139 *
140 * Managed reset_controller_register(). For reset controllers registered by
141 * this function, reset_controller_unregister() is automatically called on
142 * driver detach. See reset_controller_register() for more information.
143 */
144int devm_reset_controller_register(struct device *dev,
145 struct reset_controller_dev *rcdev)
146{
147 struct reset_controller_dev **rcdevp;
148 int ret;
149
150 rcdevp = devres_alloc(devm_reset_controller_release, sizeof(*rcdevp),
151 GFP_KERNEL);
152 if (!rcdevp)
153 return -ENOMEM;
154
155 ret = reset_controller_register(rcdev);
156 if (!ret) {
157 *rcdevp = rcdev;
158 devres_add(dev, rcdevp);
159 } else {
160 devres_free(rcdevp);
161 }
162
163 return ret;
164}
165EXPORT_SYMBOL_GPL(devm_reset_controller_register);
166
Bartosz Golaszewski6691dff2018-02-28 14:08:57 +0100167/**
168 * reset_controller_add_lookup - register a set of lookup entries
Bartosz Golaszewski6691dff2018-02-28 14:08:57 +0100169 * @lookup: array of reset lookup entries
170 * @num_entries: number of entries in the lookup array
171 */
Bartosz Golaszewskie2749bb2018-03-23 14:04:48 +0100172void reset_controller_add_lookup(struct reset_control_lookup *lookup,
Bartosz Golaszewski6691dff2018-02-28 14:08:57 +0100173 unsigned int num_entries)
174{
175 struct reset_control_lookup *entry;
176 unsigned int i;
177
178 mutex_lock(&reset_lookup_mutex);
179 for (i = 0; i < num_entries; i++) {
180 entry = &lookup[i];
181
Bartosz Golaszewskie2749bb2018-03-23 14:04:48 +0100182 if (!entry->dev_id || !entry->provider) {
183 pr_warn("%s(): reset lookup entry badly specified, skipping\n",
Bartosz Golaszewski6691dff2018-02-28 14:08:57 +0100184 __func__);
185 continue;
186 }
187
Bartosz Golaszewski6691dff2018-02-28 14:08:57 +0100188 list_add_tail(&entry->list, &reset_lookup_list);
189 }
190 mutex_unlock(&reset_lookup_mutex);
191}
192EXPORT_SYMBOL_GPL(reset_controller_add_lookup);
193
Vivek Gautam17c82e22017-05-22 16:53:25 +0530194static inline struct reset_control_array *
195rstc_to_array(struct reset_control *rstc) {
196 return container_of(rstc, struct reset_control_array, base);
197}
198
199static int reset_control_array_reset(struct reset_control_array *resets)
200{
201 int ret, i;
202
203 for (i = 0; i < resets->num_rstcs; i++) {
204 ret = reset_control_reset(resets->rstc[i]);
205 if (ret)
206 return ret;
207 }
208
209 return 0;
210}
211
212static int reset_control_array_assert(struct reset_control_array *resets)
213{
214 int ret, i;
215
216 for (i = 0; i < resets->num_rstcs; i++) {
217 ret = reset_control_assert(resets->rstc[i]);
218 if (ret)
219 goto err;
220 }
221
222 return 0;
223
224err:
225 while (i--)
226 reset_control_deassert(resets->rstc[i]);
227 return ret;
228}
229
230static int reset_control_array_deassert(struct reset_control_array *resets)
231{
232 int ret, i;
233
234 for (i = 0; i < resets->num_rstcs; i++) {
235 ret = reset_control_deassert(resets->rstc[i]);
236 if (ret)
237 goto err;
238 }
239
240 return 0;
241
242err:
243 while (i--)
244 reset_control_assert(resets->rstc[i]);
245 return ret;
246}
247
248static inline bool reset_control_is_array(struct reset_control *rstc)
249{
250 return rstc->array;
251}
252
Philipp Zabel61fc4132012-11-19 17:23:13 +0100253/**
254 * reset_control_reset - reset the controlled device
255 * @rstc: reset controller
Hans de Goede0b522972016-02-23 18:46:26 +0100256 *
Martin Blumenstingl7da33a32016-11-12 14:13:03 +0100257 * On a shared reset line the actual reset pulse is only triggered once for the
258 * lifetime of the reset_control instance: for all but the first caller this is
259 * a no-op.
260 * Consumers must not use reset_control_(de)assert on shared reset lines when
261 * reset_control_reset has been used.
Ramiro Oliveirabb475232017-01-13 17:57:41 +0000262 *
263 * If rstc is NULL it is an optional reset and the function will just
264 * return 0.
Philipp Zabel61fc4132012-11-19 17:23:13 +0100265 */
266int reset_control_reset(struct reset_control *rstc)
267{
Martin Blumenstingl7da33a32016-11-12 14:13:03 +0100268 int ret;
269
Ramiro Oliveirabb475232017-01-13 17:57:41 +0000270 if (!rstc)
271 return 0;
272
273 if (WARN_ON(IS_ERR(rstc)))
Hans de Goede0b522972016-02-23 18:46:26 +0100274 return -EINVAL;
275
Vivek Gautam17c82e22017-05-22 16:53:25 +0530276 if (reset_control_is_array(rstc))
277 return reset_control_array_reset(rstc_to_array(rstc));
278
Martin Blumenstingl7da33a32016-11-12 14:13:03 +0100279 if (!rstc->rcdev->ops->reset)
280 return -ENOTSUPP;
Philipp Zabel61fc4132012-11-19 17:23:13 +0100281
Martin Blumenstingl7da33a32016-11-12 14:13:03 +0100282 if (rstc->shared) {
283 if (WARN_ON(atomic_read(&rstc->deassert_count) != 0))
284 return -EINVAL;
285
286 if (atomic_inc_return(&rstc->triggered_count) != 1)
287 return 0;
Philipp Zabelc84b0322019-02-21 16:25:53 +0100288 } else {
289 if (!rstc->acquired)
290 return -EPERM;
Martin Blumenstingl7da33a32016-11-12 14:13:03 +0100291 }
292
293 ret = rstc->rcdev->ops->reset(rstc->rcdev, rstc->id);
Jerome Brunete5a1dad2017-02-15 19:15:51 +0100294 if (rstc->shared && ret)
Martin Blumenstingl7da33a32016-11-12 14:13:03 +0100295 atomic_dec(&rstc->triggered_count);
296
297 return ret;
Philipp Zabel61fc4132012-11-19 17:23:13 +0100298}
299EXPORT_SYMBOL_GPL(reset_control_reset);
300
301/**
302 * reset_control_assert - asserts the reset line
303 * @rstc: reset controller
Hans de Goede0b522972016-02-23 18:46:26 +0100304 *
305 * Calling this on an exclusive reset controller guarantees that the reset
306 * will be asserted. When called on a shared reset controller the line may
307 * still be deasserted, as long as other users keep it so.
308 *
309 * For shared reset controls a driver cannot expect the hw's registers and
310 * internal state to be reset, but must be prepared for this to happen.
Martin Blumenstingl7da33a32016-11-12 14:13:03 +0100311 * Consumers must not use reset_control_reset on shared reset lines when
312 * reset_control_(de)assert has been used.
Ramiro Oliveirabb475232017-01-13 17:57:41 +0000313 * return 0.
314 *
315 * If rstc is NULL it is an optional reset and the function will just
316 * return 0.
Philipp Zabel61fc4132012-11-19 17:23:13 +0100317 */
318int reset_control_assert(struct reset_control *rstc)
319{
Ramiro Oliveirabb475232017-01-13 17:57:41 +0000320 if (!rstc)
321 return 0;
322
323 if (WARN_ON(IS_ERR(rstc)))
Philipp Zabela3774e12016-06-20 13:05:14 +0200324 return -EINVAL;
325
Vivek Gautam17c82e22017-05-22 16:53:25 +0530326 if (reset_control_is_array(rstc))
327 return reset_control_array_assert(rstc_to_array(rstc));
328
Hans de Goede0b522972016-02-23 18:46:26 +0100329 if (rstc->shared) {
Martin Blumenstingl7da33a32016-11-12 14:13:03 +0100330 if (WARN_ON(atomic_read(&rstc->triggered_count) != 0))
331 return -EINVAL;
332
Hans de Goede0b522972016-02-23 18:46:26 +0100333 if (WARN_ON(atomic_read(&rstc->deassert_count) == 0))
334 return -EINVAL;
335
336 if (atomic_dec_return(&rstc->deassert_count) != 0)
337 return 0;
Philipp Zabel21240eb2017-07-12 17:51:22 +0200338
339 /*
340 * Shared reset controls allow the reset line to be in any state
341 * after this call, so doing nothing is a valid option.
342 */
343 if (!rstc->rcdev->ops->assert)
344 return 0;
345 } else {
346 /*
347 * If the reset controller does not implement .assert(), there
348 * is no way to guarantee that the reset line is asserted after
349 * this call.
350 */
351 if (!rstc->rcdev->ops->assert)
352 return -ENOTSUPP;
Philipp Zabelc84b0322019-02-21 16:25:53 +0100353
354 if (!rstc->acquired) {
355 WARN(1, "reset %s (ID: %u) is not acquired\n",
356 rcdev_name(rstc->rcdev), rstc->id);
357 return -EPERM;
358 }
Hans de Goede0b522972016-02-23 18:46:26 +0100359 }
360
361 return rstc->rcdev->ops->assert(rstc->rcdev, rstc->id);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100362}
363EXPORT_SYMBOL_GPL(reset_control_assert);
364
365/**
366 * reset_control_deassert - deasserts the reset line
367 * @rstc: reset controller
Hans de Goede0b522972016-02-23 18:46:26 +0100368 *
369 * After calling this function, the reset is guaranteed to be deasserted.
Martin Blumenstingl7da33a32016-11-12 14:13:03 +0100370 * Consumers must not use reset_control_reset on shared reset lines when
371 * reset_control_(de)assert has been used.
Ramiro Oliveirabb475232017-01-13 17:57:41 +0000372 * return 0.
373 *
374 * If rstc is NULL it is an optional reset and the function will just
375 * return 0.
Philipp Zabel61fc4132012-11-19 17:23:13 +0100376 */
377int reset_control_deassert(struct reset_control *rstc)
378{
Ramiro Oliveirabb475232017-01-13 17:57:41 +0000379 if (!rstc)
380 return 0;
381
382 if (WARN_ON(IS_ERR(rstc)))
Philipp Zabela3774e12016-06-20 13:05:14 +0200383 return -EINVAL;
384
Vivek Gautam17c82e22017-05-22 16:53:25 +0530385 if (reset_control_is_array(rstc))
386 return reset_control_array_deassert(rstc_to_array(rstc));
387
Hans de Goede0b522972016-02-23 18:46:26 +0100388 if (rstc->shared) {
Martin Blumenstingl7da33a32016-11-12 14:13:03 +0100389 if (WARN_ON(atomic_read(&rstc->triggered_count) != 0))
390 return -EINVAL;
391
Hans de Goede0b522972016-02-23 18:46:26 +0100392 if (atomic_inc_return(&rstc->deassert_count) != 1)
393 return 0;
Philipp Zabelc84b0322019-02-21 16:25:53 +0100394 } else {
395 if (!rstc->acquired) {
396 WARN(1, "reset %s (ID: %u) is not acquired\n",
397 rcdev_name(rstc->rcdev), rstc->id);
398 return -EPERM;
399 }
Hans de Goede0b522972016-02-23 18:46:26 +0100400 }
401
Philipp Zabel21240eb2017-07-12 17:51:22 +0200402 /*
403 * If the reset controller does not implement .deassert(), we assume
404 * that it handles self-deasserting reset lines via .reset(). In that
405 * case, the reset lines are deasserted by default. If that is not the
406 * case, the reset controller driver should implement .deassert() and
407 * return -ENOTSUPP.
408 */
409 if (!rstc->rcdev->ops->deassert)
410 return 0;
411
Hans de Goede0b522972016-02-23 18:46:26 +0100412 return rstc->rcdev->ops->deassert(rstc->rcdev, rstc->id);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100413}
414EXPORT_SYMBOL_GPL(reset_control_deassert);
415
416/**
Dinh Nguyen729de412014-10-10 10:21:14 -0500417 * reset_control_status - returns a negative errno if not supported, a
418 * positive value if the reset line is asserted, or zero if the reset
Ramiro Oliveirabb475232017-01-13 17:57:41 +0000419 * line is not asserted or if the desc is NULL (optional reset).
Dinh Nguyen729de412014-10-10 10:21:14 -0500420 * @rstc: reset controller
421 */
422int reset_control_status(struct reset_control *rstc)
423{
Ramiro Oliveirabb475232017-01-13 17:57:41 +0000424 if (!rstc)
425 return 0;
426
Vivek Gautam17c82e22017-05-22 16:53:25 +0530427 if (WARN_ON(IS_ERR(rstc)) || reset_control_is_array(rstc))
Philipp Zabela3774e12016-06-20 13:05:14 +0200428 return -EINVAL;
429
Dinh Nguyen729de412014-10-10 10:21:14 -0500430 if (rstc->rcdev->ops->status)
431 return rstc->rcdev->ops->status(rstc->rcdev, rstc->id);
432
Philipp Zabel39b4da72015-10-29 09:55:00 +0100433 return -ENOTSUPP;
Dinh Nguyen729de412014-10-10 10:21:14 -0500434}
435EXPORT_SYMBOL_GPL(reset_control_status);
436
Philipp Zabelc84b0322019-02-21 16:25:53 +0100437/**
438 * reset_control_acquire() - acquires a reset control for exclusive use
439 * @rstc: reset control
440 *
441 * This is used to explicitly acquire a reset control for exclusive use. Note
442 * that exclusive resets are requested as acquired by default. In order for a
443 * second consumer to be able to control the reset, the first consumer has to
444 * release it first. Typically the easiest way to achieve this is to call the
445 * reset_control_get_exclusive_released() to obtain an instance of the reset
446 * control. Such reset controls are not acquired by default.
447 *
448 * Consumers implementing shared access to an exclusive reset need to follow
449 * a specific protocol in order to work together. Before consumers can change
450 * a reset they must acquire exclusive access using reset_control_acquire().
451 * After they are done operating the reset, they must release exclusive access
452 * with a call to reset_control_release(). Consumers are not granted exclusive
453 * access to the reset as long as another consumer hasn't released a reset.
454 *
455 * See also: reset_control_release()
456 */
457int reset_control_acquire(struct reset_control *rstc)
458{
459 struct reset_control *rc;
460
461 if (!rstc)
462 return 0;
463
464 if (WARN_ON(IS_ERR(rstc)))
465 return -EINVAL;
466
467 mutex_lock(&reset_list_mutex);
468
469 if (rstc->acquired) {
470 mutex_unlock(&reset_list_mutex);
471 return 0;
472 }
473
474 list_for_each_entry(rc, &rstc->rcdev->reset_control_head, list) {
475 if (rstc != rc && rstc->id == rc->id) {
476 if (rc->acquired) {
477 mutex_unlock(&reset_list_mutex);
478 return -EBUSY;
479 }
480 }
481 }
482
483 rstc->acquired = true;
484
485 mutex_unlock(&reset_list_mutex);
486 return 0;
487}
488EXPORT_SYMBOL_GPL(reset_control_acquire);
489
490/**
491 * reset_control_release() - releases exclusive access to a reset control
492 * @rstc: reset control
493 *
494 * Releases exclusive access right to a reset control previously obtained by a
495 * call to reset_control_acquire(). Until a consumer calls this function, no
496 * other consumers will be granted exclusive access.
497 *
498 * See also: reset_control_acquire()
499 */
500void reset_control_release(struct reset_control *rstc)
501{
502 if (!rstc || WARN_ON(IS_ERR(rstc)))
503 return;
504
505 rstc->acquired = false;
506}
507EXPORT_SYMBOL_GPL(reset_control_release);
508
Philipp Zabel62e24c52016-02-05 13:41:39 +0100509static struct reset_control *__reset_control_get_internal(
Hans de Goedec15ddec2016-02-23 18:46:25 +0100510 struct reset_controller_dev *rcdev,
Philipp Zabelc84b0322019-02-21 16:25:53 +0100511 unsigned int index, bool shared, bool acquired)
Hans de Goedec15ddec2016-02-23 18:46:25 +0100512{
513 struct reset_control *rstc;
514
515 lockdep_assert_held(&reset_list_mutex);
516
517 list_for_each_entry(rstc, &rcdev->reset_control_head, list) {
518 if (rstc->id == index) {
Philipp Zabelc84b0322019-02-21 16:25:53 +0100519 /*
520 * Allow creating a secondary exclusive reset_control
521 * that is initially not acquired for an already
522 * controlled reset line.
523 */
524 if (!rstc->shared && !shared && !acquired)
525 break;
526
Hans de Goede0b522972016-02-23 18:46:26 +0100527 if (WARN_ON(!rstc->shared || !shared))
528 return ERR_PTR(-EBUSY);
529
Philipp Zabeld25e4332017-05-31 17:42:29 +0200530 kref_get(&rstc->refcnt);
Hans de Goedec15ddec2016-02-23 18:46:25 +0100531 return rstc;
532 }
533 }
534
535 rstc = kzalloc(sizeof(*rstc), GFP_KERNEL);
536 if (!rstc)
537 return ERR_PTR(-ENOMEM);
538
539 try_module_get(rcdev->owner);
540
541 rstc->rcdev = rcdev;
542 list_add(&rstc->list, &rcdev->reset_control_head);
543 rstc->id = index;
Philipp Zabeld25e4332017-05-31 17:42:29 +0200544 kref_init(&rstc->refcnt);
Philipp Zabelc84b0322019-02-21 16:25:53 +0100545 rstc->acquired = acquired;
Hans de Goede0b522972016-02-23 18:46:26 +0100546 rstc->shared = shared;
Hans de Goedec15ddec2016-02-23 18:46:25 +0100547
548 return rstc;
549}
550
Philipp Zabeld25e4332017-05-31 17:42:29 +0200551static void __reset_control_release(struct kref *kref)
552{
553 struct reset_control *rstc = container_of(kref, struct reset_control,
554 refcnt);
555
556 lockdep_assert_held(&reset_list_mutex);
557
558 module_put(rstc->rcdev->owner);
559
560 list_del(&rstc->list);
561 kfree(rstc);
562}
563
Philipp Zabel62e24c52016-02-05 13:41:39 +0100564static void __reset_control_put_internal(struct reset_control *rstc)
Hans de Goedec15ddec2016-02-23 18:46:25 +0100565{
566 lockdep_assert_held(&reset_list_mutex);
567
Philipp Zabeld25e4332017-05-31 17:42:29 +0200568 kref_put(&rstc->refcnt, __reset_control_release);
Hans de Goedec15ddec2016-02-23 18:46:25 +0100569}
570
Hans de Goede6c96f052016-02-23 18:46:24 +0100571struct reset_control *__of_reset_control_get(struct device_node *node,
Ramiro Oliveirabb475232017-01-13 17:57:41 +0000572 const char *id, int index, bool shared,
Philipp Zabelc84b0322019-02-21 16:25:53 +0100573 bool optional, bool acquired)
Philipp Zabel61fc4132012-11-19 17:23:13 +0100574{
Philipp Zabeld056c9b2015-12-08 18:49:53 +0100575 struct reset_control *rstc;
Philipp Zabel61fc4132012-11-19 17:23:13 +0100576 struct reset_controller_dev *r, *rcdev;
577 struct of_phandle_args args;
Philipp Zabel61fc4132012-11-19 17:23:13 +0100578 int rstc_id;
579 int ret;
580
Hans de Goede6c96f052016-02-23 18:46:24 +0100581 if (!node)
582 return ERR_PTR(-EINVAL);
583
584 if (id) {
585 index = of_property_match_string(node,
586 "reset-names", id);
Ramiro Oliveirabb475232017-01-13 17:57:41 +0000587 if (index == -EILSEQ)
588 return ERR_PTR(index);
Hans de Goede6c96f052016-02-23 18:46:24 +0100589 if (index < 0)
Ramiro Oliveirabb475232017-01-13 17:57:41 +0000590 return optional ? NULL : ERR_PTR(-ENOENT);
Hans de Goede6c96f052016-02-23 18:46:24 +0100591 }
592
Maxime Ripardfc0a5922013-12-20 22:41:07 +0100593 ret = of_parse_phandle_with_args(node, "resets", "#reset-cells",
Philipp Zabel61fc4132012-11-19 17:23:13 +0100594 index, &args);
Ramiro Oliveirabb475232017-01-13 17:57:41 +0000595 if (ret == -EINVAL)
Philipp Zabel61fc4132012-11-19 17:23:13 +0100596 return ERR_PTR(ret);
Ramiro Oliveirabb475232017-01-13 17:57:41 +0000597 if (ret)
598 return optional ? NULL : ERR_PTR(ret);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100599
Hans de Goedec15ddec2016-02-23 18:46:25 +0100600 mutex_lock(&reset_list_mutex);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100601 rcdev = NULL;
602 list_for_each_entry(r, &reset_controller_list, list) {
603 if (args.np == r->of_node) {
604 rcdev = r;
605 break;
606 }
607 }
Philipp Zabel61fc4132012-11-19 17:23:13 +0100608
609 if (!rcdev) {
Geert Uytterhoevenb790c8e2018-10-08 13:14:35 +0200610 rstc = ERR_PTR(-EPROBE_DEFER);
611 goto out;
Philipp Zabel61fc4132012-11-19 17:23:13 +0100612 }
613
Maxime Riparde6777742016-01-14 16:24:44 +0100614 if (WARN_ON(args.args_count != rcdev->of_reset_n_cells)) {
Geert Uytterhoevenb790c8e2018-10-08 13:14:35 +0200615 rstc = ERR_PTR(-EINVAL);
616 goto out;
Maxime Riparde6777742016-01-14 16:24:44 +0100617 }
618
Philipp Zabel61fc4132012-11-19 17:23:13 +0100619 rstc_id = rcdev->of_xlate(rcdev, &args);
620 if (rstc_id < 0) {
Geert Uytterhoevenb790c8e2018-10-08 13:14:35 +0200621 rstc = ERR_PTR(rstc_id);
622 goto out;
Philipp Zabel61fc4132012-11-19 17:23:13 +0100623 }
624
Hans de Goedec15ddec2016-02-23 18:46:25 +0100625 /* reset_list_mutex also protects the rcdev's reset_control list */
Philipp Zabelc84b0322019-02-21 16:25:53 +0100626 rstc = __reset_control_get_internal(rcdev, rstc_id, shared, acquired);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100627
Geert Uytterhoevenb790c8e2018-10-08 13:14:35 +0200628out:
Hans de Goedec15ddec2016-02-23 18:46:25 +0100629 mutex_unlock(&reset_list_mutex);
Geert Uytterhoevenb790c8e2018-10-08 13:14:35 +0200630 of_node_put(args.np);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100631
632 return rstc;
633}
Hans de Goede6c96f052016-02-23 18:46:24 +0100634EXPORT_SYMBOL_GPL(__of_reset_control_get);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100635
Bartosz Golaszewskie2749bb2018-03-23 14:04:48 +0100636static struct reset_controller_dev *
637__reset_controller_by_name(const char *name)
638{
639 struct reset_controller_dev *rcdev;
640
641 lockdep_assert_held(&reset_list_mutex);
642
643 list_for_each_entry(rcdev, &reset_controller_list, list) {
644 if (!rcdev->dev)
645 continue;
646
647 if (!strcmp(name, dev_name(rcdev->dev)))
648 return rcdev;
649 }
650
651 return NULL;
652}
653
Bartosz Golaszewski6691dff2018-02-28 14:08:57 +0100654static struct reset_control *
655__reset_control_get_from_lookup(struct device *dev, const char *con_id,
Philipp Zabelc84b0322019-02-21 16:25:53 +0100656 bool shared, bool optional, bool acquired)
Bartosz Golaszewski6691dff2018-02-28 14:08:57 +0100657{
658 const struct reset_control_lookup *lookup;
Bartosz Golaszewskie2749bb2018-03-23 14:04:48 +0100659 struct reset_controller_dev *rcdev;
Bartosz Golaszewski6691dff2018-02-28 14:08:57 +0100660 const char *dev_id = dev_name(dev);
661 struct reset_control *rstc = NULL;
662
663 if (!dev)
664 return ERR_PTR(-EINVAL);
665
666 mutex_lock(&reset_lookup_mutex);
667
668 list_for_each_entry(lookup, &reset_lookup_list, list) {
669 if (strcmp(lookup->dev_id, dev_id))
670 continue;
671
672 if ((!con_id && !lookup->con_id) ||
673 ((con_id && lookup->con_id) &&
674 !strcmp(con_id, lookup->con_id))) {
675 mutex_lock(&reset_list_mutex);
Bartosz Golaszewskie2749bb2018-03-23 14:04:48 +0100676 rcdev = __reset_controller_by_name(lookup->provider);
677 if (!rcdev) {
678 mutex_unlock(&reset_list_mutex);
679 mutex_unlock(&reset_lookup_mutex);
680 /* Reset provider may not be ready yet. */
681 return ERR_PTR(-EPROBE_DEFER);
682 }
683
684 rstc = __reset_control_get_internal(rcdev,
Bartosz Golaszewski6691dff2018-02-28 14:08:57 +0100685 lookup->index,
Philipp Zabelc84b0322019-02-21 16:25:53 +0100686 shared, acquired);
Bartosz Golaszewski6691dff2018-02-28 14:08:57 +0100687 mutex_unlock(&reset_list_mutex);
688 break;
689 }
690 }
691
692 mutex_unlock(&reset_lookup_mutex);
693
694 if (!rstc)
695 return optional ? NULL : ERR_PTR(-ENOENT);
696
697 return rstc;
698}
699
Philipp Zabel62e24c52016-02-05 13:41:39 +0100700struct reset_control *__reset_control_get(struct device *dev, const char *id,
Philipp Zabelc84b0322019-02-21 16:25:53 +0100701 int index, bool shared, bool optional,
702 bool acquired)
Philipp Zabel62e24c52016-02-05 13:41:39 +0100703{
Philipp Zabelc84b0322019-02-21 16:25:53 +0100704 if (WARN_ON(shared && acquired))
705 return ERR_PTR(-EINVAL);
706
Philipp Zabel62e24c52016-02-05 13:41:39 +0100707 if (dev->of_node)
708 return __of_reset_control_get(dev->of_node, id, index, shared,
Philipp Zabelc84b0322019-02-21 16:25:53 +0100709 optional, acquired);
Philipp Zabel62e24c52016-02-05 13:41:39 +0100710
Philipp Zabelc84b0322019-02-21 16:25:53 +0100711 return __reset_control_get_from_lookup(dev, id, shared, optional,
712 acquired);
Philipp Zabel62e24c52016-02-05 13:41:39 +0100713}
714EXPORT_SYMBOL_GPL(__reset_control_get);
715
Vivek Gautam17c82e22017-05-22 16:53:25 +0530716static void reset_control_array_put(struct reset_control_array *resets)
717{
718 int i;
719
720 mutex_lock(&reset_list_mutex);
721 for (i = 0; i < resets->num_rstcs; i++)
722 __reset_control_put_internal(resets->rstc[i]);
723 mutex_unlock(&reset_list_mutex);
724}
725
Philipp Zabel61fc4132012-11-19 17:23:13 +0100726/**
727 * reset_control_put - free the reset controller
728 * @rstc: reset controller
729 */
Philipp Zabel61fc4132012-11-19 17:23:13 +0100730void reset_control_put(struct reset_control *rstc)
731{
Heiner Kallweit48914862017-02-01 08:05:22 +0100732 if (IS_ERR_OR_NULL(rstc))
Philipp Zabel61fc4132012-11-19 17:23:13 +0100733 return;
734
Vivek Gautam17c82e22017-05-22 16:53:25 +0530735 if (reset_control_is_array(rstc)) {
736 reset_control_array_put(rstc_to_array(rstc));
737 return;
738 }
739
Hans de Goedec15ddec2016-02-23 18:46:25 +0100740 mutex_lock(&reset_list_mutex);
Philipp Zabel62e24c52016-02-05 13:41:39 +0100741 __reset_control_put_internal(rstc);
Hans de Goedec15ddec2016-02-23 18:46:25 +0100742 mutex_unlock(&reset_list_mutex);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100743}
744EXPORT_SYMBOL_GPL(reset_control_put);
745
746static void devm_reset_control_release(struct device *dev, void *res)
747{
748 reset_control_put(*(struct reset_control **)res);
749}
750
Hans de Goede6c96f052016-02-23 18:46:24 +0100751struct reset_control *__devm_reset_control_get(struct device *dev,
Ramiro Oliveirabb475232017-01-13 17:57:41 +0000752 const char *id, int index, bool shared,
Philipp Zabelc84b0322019-02-21 16:25:53 +0100753 bool optional, bool acquired)
Philipp Zabel61fc4132012-11-19 17:23:13 +0100754{
755 struct reset_control **ptr, *rstc;
756
757 ptr = devres_alloc(devm_reset_control_release, sizeof(*ptr),
758 GFP_KERNEL);
759 if (!ptr)
760 return ERR_PTR(-ENOMEM);
761
Philipp Zabelc84b0322019-02-21 16:25:53 +0100762 rstc = __reset_control_get(dev, id, index, shared, optional, acquired);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100763 if (!IS_ERR(rstc)) {
764 *ptr = rstc;
765 devres_add(dev, ptr);
766 } else {
767 devres_free(ptr);
768 }
769
770 return rstc;
771}
Hans de Goede6c96f052016-02-23 18:46:24 +0100772EXPORT_SYMBOL_GPL(__devm_reset_control_get);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100773
Philipp Zabel61fc4132012-11-19 17:23:13 +0100774/**
775 * device_reset - find reset controller associated with the device
776 * and perform reset
777 * @dev: device to be reset by the controller
Masahiro Yamada1554bbd2017-10-29 01:50:06 +0900778 * @optional: whether it is optional to reset the device
Philipp Zabel61fc4132012-11-19 17:23:13 +0100779 *
Masahiro Yamada1554bbd2017-10-29 01:50:06 +0900780 * Convenience wrapper for __reset_control_get() and reset_control_reset().
Philipp Zabel61fc4132012-11-19 17:23:13 +0100781 * This is useful for the common case of devices with single, dedicated reset
782 * lines.
783 */
Masahiro Yamada1554bbd2017-10-29 01:50:06 +0900784int __device_reset(struct device *dev, bool optional)
Philipp Zabel61fc4132012-11-19 17:23:13 +0100785{
786 struct reset_control *rstc;
787 int ret;
788
Philipp Zabelc84b0322019-02-21 16:25:53 +0100789 rstc = __reset_control_get(dev, NULL, 0, 0, optional, true);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100790 if (IS_ERR(rstc))
791 return PTR_ERR(rstc);
792
793 ret = reset_control_reset(rstc);
794
795 reset_control_put(rstc);
796
797 return ret;
798}
Masahiro Yamada1554bbd2017-10-29 01:50:06 +0900799EXPORT_SYMBOL_GPL(__device_reset);
Vivek Gautam17c82e22017-05-22 16:53:25 +0530800
801/**
802 * APIs to manage an array of reset controls.
803 */
804/**
805 * of_reset_control_get_count - Count number of resets available with a device
806 *
807 * @node: device node that contains 'resets'.
808 *
809 * Returns positive reset count on success, or error number on failure and
810 * on count being zero.
811 */
812static int of_reset_control_get_count(struct device_node *node)
813{
814 int count;
815
816 if (!node)
817 return -EINVAL;
818
819 count = of_count_phandle_with_args(node, "resets", "#reset-cells");
820 if (count == 0)
821 count = -ENOENT;
822
823 return count;
824}
825
826/**
827 * of_reset_control_array_get - Get a list of reset controls using
828 * device node.
829 *
830 * @np: device node for the device that requests the reset controls array
831 * @shared: whether reset controls are shared or not
832 * @optional: whether it is optional to get the reset controls
Thierry Redingf31d5c22019-02-21 16:25:54 +0100833 * @acquired: only one reset control may be acquired for a given controller
834 * and ID
Vivek Gautam17c82e22017-05-22 16:53:25 +0530835 *
836 * Returns pointer to allocated reset_control_array on success or
837 * error on failure
838 */
839struct reset_control *
Thierry Redingf31d5c22019-02-21 16:25:54 +0100840of_reset_control_array_get(struct device_node *np, bool shared, bool optional,
841 bool acquired)
Vivek Gautam17c82e22017-05-22 16:53:25 +0530842{
843 struct reset_control_array *resets;
844 struct reset_control *rstc;
845 int num, i;
846
847 num = of_reset_control_get_count(np);
848 if (num < 0)
849 return optional ? NULL : ERR_PTR(num);
850
Kees Cookacafe7e2018-05-08 13:45:50 -0700851 resets = kzalloc(struct_size(resets, rstc, num), GFP_KERNEL);
Vivek Gautam17c82e22017-05-22 16:53:25 +0530852 if (!resets)
853 return ERR_PTR(-ENOMEM);
854
855 for (i = 0; i < num; i++) {
Philipp Zabelc84b0322019-02-21 16:25:53 +0100856 rstc = __of_reset_control_get(np, NULL, i, shared, optional,
Thierry Redingf31d5c22019-02-21 16:25:54 +0100857 acquired);
Vivek Gautam17c82e22017-05-22 16:53:25 +0530858 if (IS_ERR(rstc))
859 goto err_rst;
860 resets->rstc[i] = rstc;
861 }
862 resets->num_rstcs = num;
863 resets->base.array = true;
864
865 return &resets->base;
866
867err_rst:
868 mutex_lock(&reset_list_mutex);
869 while (--i >= 0)
870 __reset_control_put_internal(resets->rstc[i]);
871 mutex_unlock(&reset_list_mutex);
872
873 kfree(resets);
874
875 return rstc;
876}
877EXPORT_SYMBOL_GPL(of_reset_control_array_get);
878
879/**
880 * devm_reset_control_array_get - Resource managed reset control array get
881 *
882 * @dev: device that requests the list of reset controls
883 * @shared: whether reset controls are shared or not
884 * @optional: whether it is optional to get the reset controls
885 *
886 * The reset control array APIs are intended for a list of resets
887 * that just have to be asserted or deasserted, without any
888 * requirements on the order.
889 *
890 * Returns pointer to allocated reset_control_array on success or
891 * error on failure
892 */
893struct reset_control *
894devm_reset_control_array_get(struct device *dev, bool shared, bool optional)
895{
896 struct reset_control **devres;
897 struct reset_control *rstc;
898
899 devres = devres_alloc(devm_reset_control_release, sizeof(*devres),
900 GFP_KERNEL);
901 if (!devres)
902 return ERR_PTR(-ENOMEM);
903
Thierry Redingf31d5c22019-02-21 16:25:54 +0100904 rstc = of_reset_control_array_get(dev->of_node, shared, optional, true);
Vivek Gautam17c82e22017-05-22 16:53:25 +0530905 if (IS_ERR(rstc)) {
906 devres_free(devres);
907 return rstc;
908 }
909
910 *devres = rstc;
911 devres_add(dev, devres);
912
913 return rstc;
914}
915EXPORT_SYMBOL_GPL(devm_reset_control_array_get);
Geert Uytterhoeveneaf91db2018-11-13 13:47:44 +0100916
917static int reset_control_get_count_from_lookup(struct device *dev)
918{
919 const struct reset_control_lookup *lookup;
Colin Ian King151f72f2018-11-14 21:49:35 +0000920 const char *dev_id;
Geert Uytterhoeveneaf91db2018-11-13 13:47:44 +0100921 int count = 0;
922
923 if (!dev)
924 return -EINVAL;
925
Colin Ian King151f72f2018-11-14 21:49:35 +0000926 dev_id = dev_name(dev);
Geert Uytterhoeveneaf91db2018-11-13 13:47:44 +0100927 mutex_lock(&reset_lookup_mutex);
928
929 list_for_each_entry(lookup, &reset_lookup_list, list) {
930 if (!strcmp(lookup->dev_id, dev_id))
931 count++;
932 }
933
934 mutex_unlock(&reset_lookup_mutex);
935
936 if (count == 0)
937 count = -ENOENT;
938
939 return count;
940}
941
942/**
943 * reset_control_get_count - Count number of resets available with a device
944 *
945 * @dev: device for which to return the number of resets
946 *
947 * Returns positive reset count on success, or error number on failure and
948 * on count being zero.
949 */
950int reset_control_get_count(struct device *dev)
951{
952 if (dev->of_node)
953 return of_reset_control_get_count(dev->of_node);
954
955 return reset_control_get_count_from_lookup(dev);
956}
957EXPORT_SYMBOL_GPL(reset_control_get_count);