| 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * Reset Controller framework |
| 4 | * |
| 5 | * Copyright 2013 Philipp Zabel, Pengutronix |
| 6 | */ |
| 7 | |
| 8 | #include <linux/acpi.h> |
| 9 | #include <linux/atomic.h> |
| 10 | #include <linux/auxiliary_bus.h> |
| 11 | #include <linux/cleanup.h> |
| 12 | #include <linux/device.h> |
| 13 | #include <linux/err.h> |
| 14 | #include <linux/export.h> |
| 15 | #include <linux/gpio/driver.h> |
| 16 | #include <linux/gpio/machine.h> |
| 17 | #include <linux/gpio/property.h> |
| 18 | #include <linux/idr.h> |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/kref.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/of.h> |
| 23 | #include <linux/reset.h> |
| 24 | #include <linux/reset-controller.h> |
| 25 | #include <linux/slab.h> |
| 26 | |
| 27 | static DEFINE_MUTEX(reset_list_mutex); |
| 28 | static LIST_HEAD(reset_controller_list); |
| 29 | |
| 30 | /* Protects reset_gpio_lookup_list */ |
| 31 | static DEFINE_MUTEX(reset_gpio_lookup_mutex); |
| 32 | static LIST_HEAD(reset_gpio_lookup_list); |
| 33 | static DEFINE_IDA(reset_gpio_ida); |
| 34 | |
| 35 | /** |
| 36 | * struct reset_control - a reset control |
| 37 | * @rcdev: a pointer to the reset controller device |
| 38 | * this reset control belongs to |
| 39 | * @list: list entry for the rcdev's reset controller list |
| 40 | * @id: ID of the reset controller in the reset |
| 41 | * controller device |
| 42 | * @refcnt: Number of gets of this reset_control |
| 43 | * @acquired: Only one reset_control may be acquired for a given rcdev and id. |
| 44 | * @shared: Is this a shared (1), or an exclusive (0) reset_control? |
| 45 | * @array: Is this an array of reset controls (1)? |
| 46 | * @deassert_count: Number of times this reset line has been deasserted |
| 47 | * @triggered_count: Number of times this reset line has been reset. Currently |
| 48 | * only used for shared resets, which means that the value |
| 49 | * will be either 0 or 1. |
| 50 | */ |
| 51 | struct reset_control { |
| 52 | struct reset_controller_dev *rcdev; |
| 53 | struct list_head list; |
| 54 | unsigned int id; |
| 55 | struct kref refcnt; |
| 56 | bool acquired; |
| 57 | bool shared; |
| 58 | bool array; |
| 59 | atomic_t deassert_count; |
| 60 | atomic_t triggered_count; |
| 61 | }; |
| 62 | |
| 63 | /** |
| 64 | * struct reset_control_array - an array of reset controls |
| 65 | * @base: reset control for compatibility with reset control API functions |
| 66 | * @num_rstcs: number of reset controls |
| 67 | * @rstc: array of reset controls |
| 68 | */ |
| 69 | struct reset_control_array { |
| 70 | struct reset_control base; |
| 71 | unsigned int num_rstcs; |
| 72 | struct reset_control *rstc[] __counted_by(num_rstcs); |
| 73 | }; |
| 74 | |
| 75 | /** |
| 76 | * struct reset_gpio_lookup - lookup key for ad-hoc created reset-gpio devices |
| 77 | * @of_args: phandle to the reset controller with all the args like GPIO number |
| 78 | * @swnode: Software node containing the reference to the GPIO provider |
| 79 | * @list: list entry for the reset_gpio_lookup_list |
| 80 | */ |
| 81 | struct reset_gpio_lookup { |
| 82 | struct of_phandle_args of_args; |
| 83 | struct fwnode_handle *swnode; |
| 84 | struct list_head list; |
| 85 | }; |
| 86 | |
| 87 | static const char *rcdev_name(struct reset_controller_dev *rcdev) |
| 88 | { |
| 89 | if (rcdev->dev) |
| 90 | return dev_name(dev: rcdev->dev); |
| 91 | |
| 92 | if (rcdev->of_node) |
| 93 | return rcdev->of_node->full_name; |
| 94 | |
| 95 | if (rcdev->of_args) |
| 96 | return rcdev->of_args->np->full_name; |
| 97 | |
| 98 | return NULL; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * of_reset_simple_xlate - translate reset_spec to the reset line number |
| 103 | * @rcdev: a pointer to the reset controller device |
| 104 | * @reset_spec: reset line specifier as found in the device tree |
| 105 | * |
| 106 | * This static translation function is used by default if of_xlate in |
| 107 | * :c:type:`reset_controller_dev` is not set. It is useful for all reset |
| 108 | * controllers with 1:1 mapping, where reset lines can be indexed by number |
| 109 | * without gaps. |
| 110 | */ |
| 111 | static int of_reset_simple_xlate(struct reset_controller_dev *rcdev, |
| 112 | const struct of_phandle_args *reset_spec) |
| 113 | { |
| 114 | if (reset_spec->args[0] >= rcdev->nr_resets) |
| 115 | return -EINVAL; |
| 116 | |
| 117 | return reset_spec->args[0]; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * reset_controller_register - register a reset controller device |
| 122 | * @rcdev: a pointer to the initialized reset controller device |
| 123 | */ |
| 124 | int reset_controller_register(struct reset_controller_dev *rcdev) |
| 125 | { |
| 126 | if (rcdev->of_node && rcdev->of_args) |
| 127 | return -EINVAL; |
| 128 | |
| 129 | if (!rcdev->of_xlate) { |
| 130 | rcdev->of_reset_n_cells = 1; |
| 131 | rcdev->of_xlate = of_reset_simple_xlate; |
| 132 | } |
| 133 | |
| 134 | INIT_LIST_HEAD(list: &rcdev->reset_control_head); |
| 135 | |
| 136 | mutex_lock(&reset_list_mutex); |
| 137 | list_add(new: &rcdev->list, head: &reset_controller_list); |
| 138 | mutex_unlock(lock: &reset_list_mutex); |
| 139 | |
| 140 | return 0; |
| 141 | } |
| 142 | EXPORT_SYMBOL_GPL(reset_controller_register); |
| 143 | |
| 144 | /** |
| 145 | * reset_controller_unregister - unregister a reset controller device |
| 146 | * @rcdev: a pointer to the reset controller device |
| 147 | */ |
| 148 | void reset_controller_unregister(struct reset_controller_dev *rcdev) |
| 149 | { |
| 150 | mutex_lock(&reset_list_mutex); |
| 151 | list_del(entry: &rcdev->list); |
| 152 | mutex_unlock(lock: &reset_list_mutex); |
| 153 | } |
| 154 | EXPORT_SYMBOL_GPL(reset_controller_unregister); |
| 155 | |
| 156 | static void devm_reset_controller_release(struct device *dev, void *res) |
| 157 | { |
| 158 | reset_controller_unregister(*(struct reset_controller_dev **)res); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * devm_reset_controller_register - resource managed reset_controller_register() |
| 163 | * @dev: device that is registering this reset controller |
| 164 | * @rcdev: a pointer to the initialized reset controller device |
| 165 | * |
| 166 | * Managed reset_controller_register(). For reset controllers registered by |
| 167 | * this function, reset_controller_unregister() is automatically called on |
| 168 | * driver detach. See reset_controller_register() for more information. |
| 169 | */ |
| 170 | int devm_reset_controller_register(struct device *dev, |
| 171 | struct reset_controller_dev *rcdev) |
| 172 | { |
| 173 | struct reset_controller_dev **rcdevp; |
| 174 | int ret; |
| 175 | |
| 176 | rcdevp = devres_alloc(devm_reset_controller_release, sizeof(*rcdevp), |
| 177 | GFP_KERNEL); |
| 178 | if (!rcdevp) |
| 179 | return -ENOMEM; |
| 180 | |
| 181 | ret = reset_controller_register(rcdev); |
| 182 | if (ret) { |
| 183 | devres_free(res: rcdevp); |
| 184 | return ret; |
| 185 | } |
| 186 | |
| 187 | *rcdevp = rcdev; |
| 188 | devres_add(dev, res: rcdevp); |
| 189 | |
| 190 | return ret; |
| 191 | } |
| 192 | EXPORT_SYMBOL_GPL(devm_reset_controller_register); |
| 193 | |
| 194 | static inline struct reset_control_array * |
| 195 | rstc_to_array(struct reset_control *rstc) { |
| 196 | return container_of(rstc, struct reset_control_array, base); |
| 197 | } |
| 198 | |
| 199 | static 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(rstc: resets->rstc[i]); |
| 205 | if (ret) |
| 206 | return ret; |
| 207 | } |
| 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | static int reset_control_array_rearm(struct reset_control_array *resets) |
| 213 | { |
| 214 | struct reset_control *rstc; |
| 215 | int i; |
| 216 | |
| 217 | for (i = 0; i < resets->num_rstcs; i++) { |
| 218 | rstc = resets->rstc[i]; |
| 219 | |
| 220 | if (!rstc) |
| 221 | continue; |
| 222 | |
| 223 | if (WARN_ON(IS_ERR(rstc))) |
| 224 | return -EINVAL; |
| 225 | |
| 226 | if (rstc->shared) { |
| 227 | if (WARN_ON(atomic_read(&rstc->deassert_count) != 0)) |
| 228 | return -EINVAL; |
| 229 | } else { |
| 230 | if (!rstc->acquired) |
| 231 | return -EPERM; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | for (i = 0; i < resets->num_rstcs; i++) { |
| 236 | rstc = resets->rstc[i]; |
| 237 | |
| 238 | if (rstc && rstc->shared) |
| 239 | WARN_ON(atomic_dec_return(&rstc->triggered_count) < 0); |
| 240 | } |
| 241 | |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | static int reset_control_array_assert(struct reset_control_array *resets) |
| 246 | { |
| 247 | int ret, i; |
| 248 | |
| 249 | for (i = 0; i < resets->num_rstcs; i++) { |
| 250 | ret = reset_control_assert(rstc: resets->rstc[i]); |
| 251 | if (ret) |
| 252 | goto err; |
| 253 | } |
| 254 | |
| 255 | return 0; |
| 256 | |
| 257 | err: |
| 258 | while (i--) |
| 259 | reset_control_deassert(rstc: resets->rstc[i]); |
| 260 | return ret; |
| 261 | } |
| 262 | |
| 263 | static int reset_control_array_deassert(struct reset_control_array *resets) |
| 264 | { |
| 265 | int ret, i; |
| 266 | |
| 267 | for (i = 0; i < resets->num_rstcs; i++) { |
| 268 | ret = reset_control_deassert(rstc: resets->rstc[i]); |
| 269 | if (ret) |
| 270 | goto err; |
| 271 | } |
| 272 | |
| 273 | return 0; |
| 274 | |
| 275 | err: |
| 276 | while (i--) |
| 277 | reset_control_assert(rstc: resets->rstc[i]); |
| 278 | return ret; |
| 279 | } |
| 280 | |
| 281 | static int reset_control_array_acquire(struct reset_control_array *resets) |
| 282 | { |
| 283 | unsigned int i; |
| 284 | int err; |
| 285 | |
| 286 | for (i = 0; i < resets->num_rstcs; i++) { |
| 287 | err = reset_control_acquire(rstc: resets->rstc[i]); |
| 288 | if (err < 0) |
| 289 | goto release; |
| 290 | } |
| 291 | |
| 292 | return 0; |
| 293 | |
| 294 | release: |
| 295 | while (i--) |
| 296 | reset_control_release(rstc: resets->rstc[i]); |
| 297 | |
| 298 | return err; |
| 299 | } |
| 300 | |
| 301 | static void reset_control_array_release(struct reset_control_array *resets) |
| 302 | { |
| 303 | unsigned int i; |
| 304 | |
| 305 | for (i = 0; i < resets->num_rstcs; i++) |
| 306 | reset_control_release(rstc: resets->rstc[i]); |
| 307 | } |
| 308 | |
| 309 | static inline bool reset_control_is_array(struct reset_control *rstc) |
| 310 | { |
| 311 | return rstc->array; |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * reset_control_reset - reset the controlled device |
| 316 | * @rstc: reset controller |
| 317 | * |
| 318 | * On a shared reset line the actual reset pulse is only triggered once for the |
| 319 | * lifetime of the reset_control instance: for all but the first caller this is |
| 320 | * a no-op. |
| 321 | * Consumers must not use reset_control_(de)assert on shared reset lines when |
| 322 | * reset_control_reset has been used. |
| 323 | * |
| 324 | * If rstc is NULL it is an optional reset and the function will just |
| 325 | * return 0. |
| 326 | */ |
| 327 | int reset_control_reset(struct reset_control *rstc) |
| 328 | { |
| 329 | int ret; |
| 330 | |
| 331 | if (!rstc) |
| 332 | return 0; |
| 333 | |
| 334 | if (WARN_ON(IS_ERR(rstc))) |
| 335 | return -EINVAL; |
| 336 | |
| 337 | if (reset_control_is_array(rstc)) |
| 338 | return reset_control_array_reset(resets: rstc_to_array(rstc)); |
| 339 | |
| 340 | if (!rstc->rcdev->ops->reset) |
| 341 | return -ENOTSUPP; |
| 342 | |
| 343 | if (rstc->shared) { |
| 344 | if (WARN_ON(atomic_read(&rstc->deassert_count) != 0)) |
| 345 | return -EINVAL; |
| 346 | |
| 347 | if (atomic_inc_return(v: &rstc->triggered_count) != 1) |
| 348 | return 0; |
| 349 | } else { |
| 350 | if (!rstc->acquired) |
| 351 | return -EPERM; |
| 352 | } |
| 353 | |
| 354 | ret = rstc->rcdev->ops->reset(rstc->rcdev, rstc->id); |
| 355 | if (rstc->shared && ret) |
| 356 | atomic_dec(v: &rstc->triggered_count); |
| 357 | |
| 358 | return ret; |
| 359 | } |
| 360 | EXPORT_SYMBOL_GPL(reset_control_reset); |
| 361 | |
| 362 | /** |
| 363 | * reset_control_bulk_reset - reset the controlled devices in order |
| 364 | * @num_rstcs: number of entries in rstcs array |
| 365 | * @rstcs: array of struct reset_control_bulk_data with reset controls set |
| 366 | * |
| 367 | * Issue a reset on all provided reset controls, in order. |
| 368 | * |
| 369 | * See also: reset_control_reset() |
| 370 | */ |
| 371 | int reset_control_bulk_reset(int num_rstcs, |
| 372 | struct reset_control_bulk_data *rstcs) |
| 373 | { |
| 374 | int ret, i; |
| 375 | |
| 376 | for (i = 0; i < num_rstcs; i++) { |
| 377 | ret = reset_control_reset(rstcs[i].rstc); |
| 378 | if (ret) |
| 379 | return ret; |
| 380 | } |
| 381 | |
| 382 | return 0; |
| 383 | } |
| 384 | EXPORT_SYMBOL_GPL(reset_control_bulk_reset); |
| 385 | |
| 386 | /** |
| 387 | * reset_control_rearm - allow shared reset line to be re-triggered" |
| 388 | * @rstc: reset controller |
| 389 | * |
| 390 | * On a shared reset line the actual reset pulse is only triggered once for the |
| 391 | * lifetime of the reset_control instance, except if this call is used. |
| 392 | * |
| 393 | * Calls to this function must be balanced with calls to reset_control_reset, |
| 394 | * a warning is thrown in case triggered_count ever dips below 0. |
| 395 | * |
| 396 | * Consumers must not use reset_control_(de)assert on shared reset lines when |
| 397 | * reset_control_reset or reset_control_rearm have been used. |
| 398 | * |
| 399 | * If rstc is NULL the function will just return 0. |
| 400 | */ |
| 401 | int reset_control_rearm(struct reset_control *rstc) |
| 402 | { |
| 403 | if (!rstc) |
| 404 | return 0; |
| 405 | |
| 406 | if (WARN_ON(IS_ERR(rstc))) |
| 407 | return -EINVAL; |
| 408 | |
| 409 | if (reset_control_is_array(rstc)) |
| 410 | return reset_control_array_rearm(resets: rstc_to_array(rstc)); |
| 411 | |
| 412 | if (rstc->shared) { |
| 413 | if (WARN_ON(atomic_read(&rstc->deassert_count) != 0)) |
| 414 | return -EINVAL; |
| 415 | |
| 416 | WARN_ON(atomic_dec_return(&rstc->triggered_count) < 0); |
| 417 | } else { |
| 418 | if (!rstc->acquired) |
| 419 | return -EPERM; |
| 420 | } |
| 421 | |
| 422 | return 0; |
| 423 | } |
| 424 | EXPORT_SYMBOL_GPL(reset_control_rearm); |
| 425 | |
| 426 | /** |
| 427 | * reset_control_assert - asserts the reset line |
| 428 | * @rstc: reset controller |
| 429 | * |
| 430 | * Calling this on an exclusive reset controller guarantees that the reset |
| 431 | * will be asserted. When called on a shared reset controller the line may |
| 432 | * still be deasserted, as long as other users keep it so. |
| 433 | * |
| 434 | * For shared reset controls a driver cannot expect the hw's registers and |
| 435 | * internal state to be reset, but must be prepared for this to happen. |
| 436 | * Consumers must not use reset_control_reset on shared reset lines when |
| 437 | * reset_control_(de)assert has been used. |
| 438 | * |
| 439 | * If rstc is NULL it is an optional reset and the function will just |
| 440 | * return 0. |
| 441 | */ |
| 442 | int reset_control_assert(struct reset_control *rstc) |
| 443 | { |
| 444 | if (!rstc) |
| 445 | return 0; |
| 446 | |
| 447 | if (WARN_ON(IS_ERR(rstc))) |
| 448 | return -EINVAL; |
| 449 | |
| 450 | if (reset_control_is_array(rstc)) |
| 451 | return reset_control_array_assert(resets: rstc_to_array(rstc)); |
| 452 | |
| 453 | if (rstc->shared) { |
| 454 | if (WARN_ON(atomic_read(&rstc->triggered_count) != 0)) |
| 455 | return -EINVAL; |
| 456 | |
| 457 | if (WARN_ON(atomic_read(&rstc->deassert_count) == 0)) |
| 458 | return -EINVAL; |
| 459 | |
| 460 | if (atomic_dec_return(v: &rstc->deassert_count) != 0) |
| 461 | return 0; |
| 462 | |
| 463 | /* |
| 464 | * Shared reset controls allow the reset line to be in any state |
| 465 | * after this call, so doing nothing is a valid option. |
| 466 | */ |
| 467 | if (!rstc->rcdev->ops->assert) |
| 468 | return 0; |
| 469 | } else { |
| 470 | /* |
| 471 | * If the reset controller does not implement .assert(), there |
| 472 | * is no way to guarantee that the reset line is asserted after |
| 473 | * this call. |
| 474 | */ |
| 475 | if (!rstc->rcdev->ops->assert) |
| 476 | return -ENOTSUPP; |
| 477 | |
| 478 | if (!rstc->acquired) { |
| 479 | WARN(1, "reset %s (ID: %u) is not acquired\n" , |
| 480 | rcdev_name(rstc->rcdev), rstc->id); |
| 481 | return -EPERM; |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | return rstc->rcdev->ops->assert(rstc->rcdev, rstc->id); |
| 486 | } |
| 487 | EXPORT_SYMBOL_GPL(reset_control_assert); |
| 488 | |
| 489 | /** |
| 490 | * reset_control_bulk_assert - asserts the reset lines in order |
| 491 | * @num_rstcs: number of entries in rstcs array |
| 492 | * @rstcs: array of struct reset_control_bulk_data with reset controls set |
| 493 | * |
| 494 | * Assert the reset lines for all provided reset controls, in order. |
| 495 | * If an assertion fails, already asserted resets are deasserted again. |
| 496 | * |
| 497 | * See also: reset_control_assert() |
| 498 | */ |
| 499 | int reset_control_bulk_assert(int num_rstcs, |
| 500 | struct reset_control_bulk_data *rstcs) |
| 501 | { |
| 502 | int ret, i; |
| 503 | |
| 504 | for (i = 0; i < num_rstcs; i++) { |
| 505 | ret = reset_control_assert(rstcs[i].rstc); |
| 506 | if (ret) |
| 507 | goto err; |
| 508 | } |
| 509 | |
| 510 | return 0; |
| 511 | |
| 512 | err: |
| 513 | while (i--) |
| 514 | reset_control_deassert(rstc: rstcs[i].rstc); |
| 515 | return ret; |
| 516 | } |
| 517 | EXPORT_SYMBOL_GPL(reset_control_bulk_assert); |
| 518 | |
| 519 | /** |
| 520 | * reset_control_deassert - deasserts the reset line |
| 521 | * @rstc: reset controller |
| 522 | * |
| 523 | * After calling this function, the reset is guaranteed to be deasserted. |
| 524 | * Consumers must not use reset_control_reset on shared reset lines when |
| 525 | * reset_control_(de)assert has been used. |
| 526 | * |
| 527 | * If rstc is NULL it is an optional reset and the function will just |
| 528 | * return 0. |
| 529 | */ |
| 530 | int reset_control_deassert(struct reset_control *rstc) |
| 531 | { |
| 532 | if (!rstc) |
| 533 | return 0; |
| 534 | |
| 535 | if (WARN_ON(IS_ERR(rstc))) |
| 536 | return -EINVAL; |
| 537 | |
| 538 | if (reset_control_is_array(rstc)) |
| 539 | return reset_control_array_deassert(resets: rstc_to_array(rstc)); |
| 540 | |
| 541 | if (rstc->shared) { |
| 542 | if (WARN_ON(atomic_read(&rstc->triggered_count) != 0)) |
| 543 | return -EINVAL; |
| 544 | |
| 545 | if (atomic_inc_return(v: &rstc->deassert_count) != 1) |
| 546 | return 0; |
| 547 | } else { |
| 548 | if (!rstc->acquired) { |
| 549 | WARN(1, "reset %s (ID: %u) is not acquired\n" , |
| 550 | rcdev_name(rstc->rcdev), rstc->id); |
| 551 | return -EPERM; |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | /* |
| 556 | * If the reset controller does not implement .deassert(), we assume |
| 557 | * that it handles self-deasserting reset lines via .reset(). In that |
| 558 | * case, the reset lines are deasserted by default. If that is not the |
| 559 | * case, the reset controller driver should implement .deassert() and |
| 560 | * return -ENOTSUPP. |
| 561 | */ |
| 562 | if (!rstc->rcdev->ops->deassert) |
| 563 | return 0; |
| 564 | |
| 565 | return rstc->rcdev->ops->deassert(rstc->rcdev, rstc->id); |
| 566 | } |
| 567 | EXPORT_SYMBOL_GPL(reset_control_deassert); |
| 568 | |
| 569 | /** |
| 570 | * reset_control_bulk_deassert - deasserts the reset lines in reverse order |
| 571 | * @num_rstcs: number of entries in rstcs array |
| 572 | * @rstcs: array of struct reset_control_bulk_data with reset controls set |
| 573 | * |
| 574 | * Deassert the reset lines for all provided reset controls, in reverse order. |
| 575 | * If a deassertion fails, already deasserted resets are asserted again. |
| 576 | * |
| 577 | * See also: reset_control_deassert() |
| 578 | */ |
| 579 | int reset_control_bulk_deassert(int num_rstcs, |
| 580 | struct reset_control_bulk_data *rstcs) |
| 581 | { |
| 582 | int ret, i; |
| 583 | |
| 584 | for (i = num_rstcs - 1; i >= 0; i--) { |
| 585 | ret = reset_control_deassert(rstcs[i].rstc); |
| 586 | if (ret) |
| 587 | goto err; |
| 588 | } |
| 589 | |
| 590 | return 0; |
| 591 | |
| 592 | err: |
| 593 | while (i < num_rstcs) |
| 594 | reset_control_assert(rstcs[i++].rstc); |
| 595 | return ret; |
| 596 | } |
| 597 | EXPORT_SYMBOL_GPL(reset_control_bulk_deassert); |
| 598 | |
| 599 | /** |
| 600 | * reset_control_status - returns a negative errno if not supported, a |
| 601 | * positive value if the reset line is asserted, or zero if the reset |
| 602 | * line is not asserted or if the desc is NULL (optional reset). |
| 603 | * @rstc: reset controller |
| 604 | */ |
| 605 | int reset_control_status(struct reset_control *rstc) |
| 606 | { |
| 607 | if (!rstc) |
| 608 | return 0; |
| 609 | |
| 610 | if (WARN_ON(IS_ERR(rstc)) || reset_control_is_array(rstc)) |
| 611 | return -EINVAL; |
| 612 | |
| 613 | if (rstc->rcdev->ops->status) |
| 614 | return rstc->rcdev->ops->status(rstc->rcdev, rstc->id); |
| 615 | |
| 616 | return -ENOTSUPP; |
| 617 | } |
| 618 | EXPORT_SYMBOL_GPL(reset_control_status); |
| 619 | |
| 620 | /** |
| 621 | * reset_control_acquire() - acquires a reset control for exclusive use |
| 622 | * @rstc: reset control |
| 623 | * |
| 624 | * This is used to explicitly acquire a reset control for exclusive use. Note |
| 625 | * that exclusive resets are requested as acquired by default. In order for a |
| 626 | * second consumer to be able to control the reset, the first consumer has to |
| 627 | * release it first. Typically the easiest way to achieve this is to call the |
| 628 | * reset_control_get_exclusive_released() to obtain an instance of the reset |
| 629 | * control. Such reset controls are not acquired by default. |
| 630 | * |
| 631 | * Consumers implementing shared access to an exclusive reset need to follow |
| 632 | * a specific protocol in order to work together. Before consumers can change |
| 633 | * a reset they must acquire exclusive access using reset_control_acquire(). |
| 634 | * After they are done operating the reset, they must release exclusive access |
| 635 | * with a call to reset_control_release(). Consumers are not granted exclusive |
| 636 | * access to the reset as long as another consumer hasn't released a reset. |
| 637 | * |
| 638 | * See also: reset_control_release() |
| 639 | */ |
| 640 | int reset_control_acquire(struct reset_control *rstc) |
| 641 | { |
| 642 | struct reset_control *rc; |
| 643 | |
| 644 | if (!rstc) |
| 645 | return 0; |
| 646 | |
| 647 | if (WARN_ON(IS_ERR(rstc))) |
| 648 | return -EINVAL; |
| 649 | |
| 650 | if (reset_control_is_array(rstc)) |
| 651 | return reset_control_array_acquire(resets: rstc_to_array(rstc)); |
| 652 | |
| 653 | mutex_lock(&reset_list_mutex); |
| 654 | |
| 655 | if (rstc->acquired) { |
| 656 | mutex_unlock(lock: &reset_list_mutex); |
| 657 | return 0; |
| 658 | } |
| 659 | |
| 660 | list_for_each_entry(rc, &rstc->rcdev->reset_control_head, list) { |
| 661 | if (rstc != rc && rstc->id == rc->id) { |
| 662 | if (rc->acquired) { |
| 663 | mutex_unlock(lock: &reset_list_mutex); |
| 664 | return -EBUSY; |
| 665 | } |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | rstc->acquired = true; |
| 670 | |
| 671 | mutex_unlock(lock: &reset_list_mutex); |
| 672 | return 0; |
| 673 | } |
| 674 | EXPORT_SYMBOL_GPL(reset_control_acquire); |
| 675 | |
| 676 | /** |
| 677 | * reset_control_bulk_acquire - acquires reset controls for exclusive use |
| 678 | * @num_rstcs: number of entries in rstcs array |
| 679 | * @rstcs: array of struct reset_control_bulk_data with reset controls set |
| 680 | * |
| 681 | * This is used to explicitly acquire reset controls requested with |
| 682 | * reset_control_bulk_get_exclusive_release() for temporary exclusive use. |
| 683 | * |
| 684 | * See also: reset_control_acquire(), reset_control_bulk_release() |
| 685 | */ |
| 686 | int reset_control_bulk_acquire(int num_rstcs, |
| 687 | struct reset_control_bulk_data *rstcs) |
| 688 | { |
| 689 | int ret, i; |
| 690 | |
| 691 | for (i = 0; i < num_rstcs; i++) { |
| 692 | ret = reset_control_acquire(rstcs[i].rstc); |
| 693 | if (ret) |
| 694 | goto err; |
| 695 | } |
| 696 | |
| 697 | return 0; |
| 698 | |
| 699 | err: |
| 700 | while (i--) |
| 701 | reset_control_release(rstc: rstcs[i].rstc); |
| 702 | return ret; |
| 703 | } |
| 704 | EXPORT_SYMBOL_GPL(reset_control_bulk_acquire); |
| 705 | |
| 706 | /** |
| 707 | * reset_control_release() - releases exclusive access to a reset control |
| 708 | * @rstc: reset control |
| 709 | * |
| 710 | * Releases exclusive access right to a reset control previously obtained by a |
| 711 | * call to reset_control_acquire(). Until a consumer calls this function, no |
| 712 | * other consumers will be granted exclusive access. |
| 713 | * |
| 714 | * See also: reset_control_acquire() |
| 715 | */ |
| 716 | void reset_control_release(struct reset_control *rstc) |
| 717 | { |
| 718 | if (!rstc || WARN_ON(IS_ERR(rstc))) |
| 719 | return; |
| 720 | |
| 721 | if (reset_control_is_array(rstc)) |
| 722 | reset_control_array_release(resets: rstc_to_array(rstc)); |
| 723 | else |
| 724 | rstc->acquired = false; |
| 725 | } |
| 726 | EXPORT_SYMBOL_GPL(reset_control_release); |
| 727 | |
| 728 | /** |
| 729 | * reset_control_bulk_release() - releases exclusive access to reset controls |
| 730 | * @num_rstcs: number of entries in rstcs array |
| 731 | * @rstcs: array of struct reset_control_bulk_data with reset controls set |
| 732 | * |
| 733 | * Releases exclusive access right to reset controls previously obtained by a |
| 734 | * call to reset_control_bulk_acquire(). |
| 735 | * |
| 736 | * See also: reset_control_release(), reset_control_bulk_acquire() |
| 737 | */ |
| 738 | void reset_control_bulk_release(int num_rstcs, |
| 739 | struct reset_control_bulk_data *rstcs) |
| 740 | { |
| 741 | int i; |
| 742 | |
| 743 | for (i = 0; i < num_rstcs; i++) |
| 744 | reset_control_release(rstcs[i].rstc); |
| 745 | } |
| 746 | EXPORT_SYMBOL_GPL(reset_control_bulk_release); |
| 747 | |
| 748 | static struct reset_control * |
| 749 | __reset_control_get_internal(struct reset_controller_dev *rcdev, |
| 750 | unsigned int index, enum reset_control_flags flags) |
| 751 | { |
| 752 | bool shared = flags & RESET_CONTROL_FLAGS_BIT_SHARED; |
| 753 | bool acquired = flags & RESET_CONTROL_FLAGS_BIT_ACQUIRED; |
| 754 | struct reset_control *rstc; |
| 755 | |
| 756 | lockdep_assert_held(&reset_list_mutex); |
| 757 | |
| 758 | /* Expect callers to filter out OPTIONAL and DEASSERTED bits */ |
| 759 | if (WARN_ON(flags & ~(RESET_CONTROL_FLAGS_BIT_SHARED | |
| 760 | RESET_CONTROL_FLAGS_BIT_ACQUIRED))) |
| 761 | return ERR_PTR(error: -EINVAL); |
| 762 | |
| 763 | list_for_each_entry(rstc, &rcdev->reset_control_head, list) { |
| 764 | if (rstc->id == index) { |
| 765 | /* |
| 766 | * Allow creating a secondary exclusive reset_control |
| 767 | * that is initially not acquired for an already |
| 768 | * controlled reset line. |
| 769 | */ |
| 770 | if (!rstc->shared && !shared && !acquired) |
| 771 | break; |
| 772 | |
| 773 | if (WARN_ON(!rstc->shared || !shared)) |
| 774 | return ERR_PTR(error: -EBUSY); |
| 775 | |
| 776 | kref_get(kref: &rstc->refcnt); |
| 777 | return rstc; |
| 778 | } |
| 779 | } |
| 780 | |
| 781 | rstc = kzalloc(sizeof(*rstc), GFP_KERNEL); |
| 782 | if (!rstc) |
| 783 | return ERR_PTR(error: -ENOMEM); |
| 784 | |
| 785 | if (!try_module_get(module: rcdev->owner)) { |
| 786 | kfree(objp: rstc); |
| 787 | return ERR_PTR(error: -ENODEV); |
| 788 | } |
| 789 | |
| 790 | rstc->rcdev = rcdev; |
| 791 | list_add(new: &rstc->list, head: &rcdev->reset_control_head); |
| 792 | rstc->id = index; |
| 793 | kref_init(kref: &rstc->refcnt); |
| 794 | rstc->acquired = acquired; |
| 795 | rstc->shared = shared; |
| 796 | get_device(dev: rcdev->dev); |
| 797 | |
| 798 | return rstc; |
| 799 | } |
| 800 | |
| 801 | static void __reset_control_release(struct kref *kref) |
| 802 | { |
| 803 | struct reset_control *rstc = container_of(kref, struct reset_control, |
| 804 | refcnt); |
| 805 | |
| 806 | lockdep_assert_held(&reset_list_mutex); |
| 807 | |
| 808 | module_put(module: rstc->rcdev->owner); |
| 809 | |
| 810 | list_del(entry: &rstc->list); |
| 811 | put_device(dev: rstc->rcdev->dev); |
| 812 | kfree(objp: rstc); |
| 813 | } |
| 814 | |
| 815 | static void __reset_control_put_internal(struct reset_control *rstc) |
| 816 | { |
| 817 | lockdep_assert_held(&reset_list_mutex); |
| 818 | |
| 819 | if (IS_ERR_OR_NULL(ptr: rstc)) |
| 820 | return; |
| 821 | |
| 822 | kref_put(kref: &rstc->refcnt, release: __reset_control_release); |
| 823 | } |
| 824 | |
| 825 | static void reset_gpio_aux_device_release(struct device *dev) |
| 826 | { |
| 827 | struct auxiliary_device *adev = to_auxiliary_dev(dev); |
| 828 | |
| 829 | kfree(objp: adev); |
| 830 | } |
| 831 | |
| 832 | static int reset_add_gpio_aux_device(struct device *parent, |
| 833 | struct fwnode_handle *swnode, |
| 834 | int id, void *pdata) |
| 835 | { |
| 836 | struct auxiliary_device *adev; |
| 837 | int ret; |
| 838 | |
| 839 | adev = kzalloc(sizeof(*adev), GFP_KERNEL); |
| 840 | if (!adev) |
| 841 | return -ENOMEM; |
| 842 | |
| 843 | adev->id = id; |
| 844 | adev->name = "gpio" ; |
| 845 | adev->dev.parent = parent; |
| 846 | adev->dev.platform_data = pdata; |
| 847 | adev->dev.release = reset_gpio_aux_device_release; |
| 848 | device_set_node(dev: &adev->dev, fwnode: swnode); |
| 849 | |
| 850 | ret = auxiliary_device_init(auxdev: adev); |
| 851 | if (ret) { |
| 852 | kfree(objp: adev); |
| 853 | return ret; |
| 854 | } |
| 855 | |
| 856 | ret = __auxiliary_device_add(auxdev: adev, modname: "reset" ); |
| 857 | if (ret) { |
| 858 | auxiliary_device_uninit(auxdev: adev); |
| 859 | kfree(objp: adev); |
| 860 | return ret; |
| 861 | } |
| 862 | |
| 863 | return ret; |
| 864 | } |
| 865 | |
| 866 | /* |
| 867 | * @args: phandle to the GPIO provider with all the args like GPIO number |
| 868 | */ |
| 869 | static int __reset_add_reset_gpio_device(const struct of_phandle_args *args) |
| 870 | { |
| 871 | struct property_entry properties[2] = { }; |
| 872 | unsigned int offset, of_flags, lflags; |
| 873 | struct reset_gpio_lookup *rgpio_dev; |
| 874 | struct device *parent; |
| 875 | int id, ret; |
| 876 | |
| 877 | /* |
| 878 | * Currently only #gpio-cells=2 is supported with the meaning of: |
| 879 | * args[0]: GPIO number |
| 880 | * args[1]: GPIO flags |
| 881 | * TODO: Handle other cases. |
| 882 | */ |
| 883 | if (args->args_count != 2) |
| 884 | return -ENOENT; |
| 885 | |
| 886 | /* |
| 887 | * Registering reset-gpio device might cause immediate |
| 888 | * bind, resulting in its probe() registering new reset controller thus |
| 889 | * taking reset_list_mutex lock via reset_controller_register(). |
| 890 | */ |
| 891 | lockdep_assert_not_held(&reset_list_mutex); |
| 892 | |
| 893 | offset = args->args[0]; |
| 894 | of_flags = args->args[1]; |
| 895 | |
| 896 | /* |
| 897 | * Later we map GPIO flags between OF and Linux, however not all |
| 898 | * constants from include/dt-bindings/gpio/gpio.h and |
| 899 | * include/linux/gpio/machine.h match each other. |
| 900 | * |
| 901 | * FIXME: Find a better way of translating OF flags to GPIO lookup |
| 902 | * flags. |
| 903 | */ |
| 904 | if (of_flags > GPIO_ACTIVE_LOW) { |
| 905 | pr_err("reset-gpio code does not support GPIO flags %u for GPIO %u\n" , |
| 906 | of_flags, offset); |
| 907 | return -EINVAL; |
| 908 | } |
| 909 | |
| 910 | struct gpio_device *gdev __free(gpio_device_put) = |
| 911 | gpio_device_find_by_fwnode(of_fwnode_handle(args->np)); |
| 912 | if (!gdev) |
| 913 | return -EPROBE_DEFER; |
| 914 | |
| 915 | guard(mutex)(T: &reset_gpio_lookup_mutex); |
| 916 | |
| 917 | list_for_each_entry(rgpio_dev, &reset_gpio_lookup_list, list) { |
| 918 | if (args->np == rgpio_dev->of_args.np) { |
| 919 | if (of_phandle_args_equal(a1: args, a2: &rgpio_dev->of_args)) |
| 920 | return 0; /* Already on the list, done */ |
| 921 | } |
| 922 | } |
| 923 | |
| 924 | lflags = GPIO_PERSISTENT | (of_flags & GPIO_ACTIVE_LOW); |
| 925 | parent = gpio_device_to_device(gdev); |
| 926 | properties[0] = PROPERTY_ENTRY_GPIO("reset-gpios" , parent->fwnode, offset, lflags); |
| 927 | |
| 928 | id = ida_alloc(ida: &reset_gpio_ida, GFP_KERNEL); |
| 929 | if (id < 0) |
| 930 | return id; |
| 931 | |
| 932 | /* Not freed on success, because it is persisent subsystem data. */ |
| 933 | rgpio_dev = kzalloc(sizeof(*rgpio_dev), GFP_KERNEL); |
| 934 | if (!rgpio_dev) { |
| 935 | ret = -ENOMEM; |
| 936 | goto err_ida_free; |
| 937 | } |
| 938 | |
| 939 | rgpio_dev->of_args = *args; |
| 940 | /* |
| 941 | * We keep the device_node reference, but of_args.np is put at the end |
| 942 | * of __of_reset_control_get(), so get it one more time. |
| 943 | * Hold reference as long as rgpio_dev memory is valid. |
| 944 | */ |
| 945 | of_node_get(node: rgpio_dev->of_args.np); |
| 946 | |
| 947 | rgpio_dev->swnode = fwnode_create_software_node(properties, NULL); |
| 948 | if (IS_ERR(ptr: rgpio_dev->swnode)) { |
| 949 | ret = PTR_ERR(ptr: rgpio_dev->swnode); |
| 950 | goto err_put_of_node; |
| 951 | } |
| 952 | |
| 953 | ret = reset_add_gpio_aux_device(parent, swnode: rgpio_dev->swnode, id, |
| 954 | pdata: &rgpio_dev->of_args); |
| 955 | if (ret) |
| 956 | goto err_del_swnode; |
| 957 | |
| 958 | list_add(new: &rgpio_dev->list, head: &reset_gpio_lookup_list); |
| 959 | |
| 960 | return 0; |
| 961 | |
| 962 | err_del_swnode: |
| 963 | fwnode_remove_software_node(fwnode: rgpio_dev->swnode); |
| 964 | err_put_of_node: |
| 965 | of_node_put(node: rgpio_dev->of_args.np); |
| 966 | kfree(objp: rgpio_dev); |
| 967 | err_ida_free: |
| 968 | ida_free(&reset_gpio_ida, id); |
| 969 | |
| 970 | return ret; |
| 971 | } |
| 972 | |
| 973 | static struct reset_controller_dev *__reset_find_rcdev(const struct of_phandle_args *args, |
| 974 | bool gpio_fallback) |
| 975 | { |
| 976 | struct reset_controller_dev *rcdev; |
| 977 | |
| 978 | lockdep_assert_held(&reset_list_mutex); |
| 979 | |
| 980 | list_for_each_entry(rcdev, &reset_controller_list, list) { |
| 981 | if (gpio_fallback) { |
| 982 | if (rcdev->of_args && of_phandle_args_equal(a1: args, |
| 983 | a2: rcdev->of_args)) |
| 984 | return rcdev; |
| 985 | } else { |
| 986 | if (args->np == rcdev->of_node) |
| 987 | return rcdev; |
| 988 | } |
| 989 | } |
| 990 | |
| 991 | return NULL; |
| 992 | } |
| 993 | |
| 994 | struct reset_control * |
| 995 | __of_reset_control_get(struct device_node *node, const char *id, int index, |
| 996 | enum reset_control_flags flags) |
| 997 | { |
| 998 | bool optional = flags & RESET_CONTROL_FLAGS_BIT_OPTIONAL; |
| 999 | bool gpio_fallback = false; |
| 1000 | struct reset_control *rstc; |
| 1001 | struct reset_controller_dev *rcdev; |
| 1002 | struct of_phandle_args args; |
| 1003 | int rstc_id; |
| 1004 | int ret; |
| 1005 | |
| 1006 | if (!node) |
| 1007 | return ERR_PTR(error: -EINVAL); |
| 1008 | |
| 1009 | if (id) { |
| 1010 | index = of_property_match_string(np: node, |
| 1011 | propname: "reset-names" , string: id); |
| 1012 | if (index == -EILSEQ) |
| 1013 | return ERR_PTR(error: index); |
| 1014 | if (index < 0) |
| 1015 | return optional ? NULL : ERR_PTR(error: -ENOENT); |
| 1016 | } |
| 1017 | |
| 1018 | ret = of_parse_phandle_with_args(np: node, list_name: "resets" , cells_name: "#reset-cells" , |
| 1019 | index, out_args: &args); |
| 1020 | if (ret == -EINVAL) |
| 1021 | return ERR_PTR(error: ret); |
| 1022 | if (ret) { |
| 1023 | if (!IS_ENABLED(CONFIG_RESET_GPIO)) |
| 1024 | return optional ? NULL : ERR_PTR(error: ret); |
| 1025 | |
| 1026 | /* |
| 1027 | * There can be only one reset-gpio for regular devices, so |
| 1028 | * don't bother with the "reset-gpios" phandle index. |
| 1029 | */ |
| 1030 | ret = of_parse_phandle_with_args(np: node, list_name: "reset-gpios" , cells_name: "#gpio-cells" , |
| 1031 | index: 0, out_args: &args); |
| 1032 | if (ret) |
| 1033 | return optional ? NULL : ERR_PTR(error: ret); |
| 1034 | |
| 1035 | gpio_fallback = true; |
| 1036 | |
| 1037 | ret = __reset_add_reset_gpio_device(args: &args); |
| 1038 | if (ret) { |
| 1039 | rstc = ERR_PTR(error: ret); |
| 1040 | goto out_put; |
| 1041 | } |
| 1042 | } |
| 1043 | |
| 1044 | mutex_lock(&reset_list_mutex); |
| 1045 | rcdev = __reset_find_rcdev(args: &args, gpio_fallback); |
| 1046 | if (!rcdev) { |
| 1047 | rstc = ERR_PTR(error: -EPROBE_DEFER); |
| 1048 | goto out_unlock; |
| 1049 | } |
| 1050 | |
| 1051 | if (WARN_ON(args.args_count != rcdev->of_reset_n_cells)) { |
| 1052 | rstc = ERR_PTR(error: -EINVAL); |
| 1053 | goto out_unlock; |
| 1054 | } |
| 1055 | |
| 1056 | rstc_id = rcdev->of_xlate(rcdev, &args); |
| 1057 | if (rstc_id < 0) { |
| 1058 | rstc = ERR_PTR(error: rstc_id); |
| 1059 | goto out_unlock; |
| 1060 | } |
| 1061 | |
| 1062 | flags &= ~RESET_CONTROL_FLAGS_BIT_OPTIONAL; |
| 1063 | |
| 1064 | /* reset_list_mutex also protects the rcdev's reset_control list */ |
| 1065 | rstc = __reset_control_get_internal(rcdev, index: rstc_id, flags); |
| 1066 | |
| 1067 | out_unlock: |
| 1068 | mutex_unlock(lock: &reset_list_mutex); |
| 1069 | out_put: |
| 1070 | of_node_put(node: args.np); |
| 1071 | |
| 1072 | return rstc; |
| 1073 | } |
| 1074 | EXPORT_SYMBOL_GPL(__of_reset_control_get); |
| 1075 | |
| 1076 | struct reset_control *__reset_control_get(struct device *dev, const char *id, |
| 1077 | int index, enum reset_control_flags flags) |
| 1078 | { |
| 1079 | bool shared = flags & RESET_CONTROL_FLAGS_BIT_SHARED; |
| 1080 | bool acquired = flags & RESET_CONTROL_FLAGS_BIT_ACQUIRED; |
| 1081 | bool optional = flags & RESET_CONTROL_FLAGS_BIT_OPTIONAL; |
| 1082 | |
| 1083 | if (WARN_ON(shared && acquired)) |
| 1084 | return ERR_PTR(error: -EINVAL); |
| 1085 | |
| 1086 | if (dev->of_node) |
| 1087 | return __of_reset_control_get(dev->of_node, id, index, flags); |
| 1088 | |
| 1089 | return optional ? NULL : ERR_PTR(error: -ENOENT); |
| 1090 | } |
| 1091 | EXPORT_SYMBOL_GPL(__reset_control_get); |
| 1092 | |
| 1093 | int __reset_control_bulk_get(struct device *dev, int num_rstcs, |
| 1094 | struct reset_control_bulk_data *rstcs, |
| 1095 | enum reset_control_flags flags) |
| 1096 | { |
| 1097 | int ret, i; |
| 1098 | |
| 1099 | for (i = 0; i < num_rstcs; i++) { |
| 1100 | rstcs[i].rstc = __reset_control_get(dev, rstcs[i].id, 0, flags); |
| 1101 | if (IS_ERR(ptr: rstcs[i].rstc)) { |
| 1102 | ret = PTR_ERR(ptr: rstcs[i].rstc); |
| 1103 | goto err; |
| 1104 | } |
| 1105 | } |
| 1106 | |
| 1107 | return 0; |
| 1108 | |
| 1109 | err: |
| 1110 | mutex_lock(&reset_list_mutex); |
| 1111 | while (i--) |
| 1112 | __reset_control_put_internal(rstc: rstcs[i].rstc); |
| 1113 | mutex_unlock(lock: &reset_list_mutex); |
| 1114 | return ret; |
| 1115 | } |
| 1116 | EXPORT_SYMBOL_GPL(__reset_control_bulk_get); |
| 1117 | |
| 1118 | static void reset_control_array_put(struct reset_control_array *resets) |
| 1119 | { |
| 1120 | int i; |
| 1121 | |
| 1122 | mutex_lock(&reset_list_mutex); |
| 1123 | for (i = 0; i < resets->num_rstcs; i++) |
| 1124 | __reset_control_put_internal(rstc: resets->rstc[i]); |
| 1125 | mutex_unlock(lock: &reset_list_mutex); |
| 1126 | kfree(objp: resets); |
| 1127 | } |
| 1128 | |
| 1129 | /** |
| 1130 | * reset_control_put - free the reset controller |
| 1131 | * @rstc: reset controller |
| 1132 | */ |
| 1133 | void reset_control_put(struct reset_control *rstc) |
| 1134 | { |
| 1135 | if (IS_ERR_OR_NULL(ptr: rstc)) |
| 1136 | return; |
| 1137 | |
| 1138 | if (reset_control_is_array(rstc)) { |
| 1139 | reset_control_array_put(resets: rstc_to_array(rstc)); |
| 1140 | return; |
| 1141 | } |
| 1142 | |
| 1143 | mutex_lock(&reset_list_mutex); |
| 1144 | __reset_control_put_internal(rstc); |
| 1145 | mutex_unlock(lock: &reset_list_mutex); |
| 1146 | } |
| 1147 | EXPORT_SYMBOL_GPL(reset_control_put); |
| 1148 | |
| 1149 | /** |
| 1150 | * reset_control_bulk_put - free the reset controllers |
| 1151 | * @num_rstcs: number of entries in rstcs array |
| 1152 | * @rstcs: array of struct reset_control_bulk_data with reset controls set |
| 1153 | */ |
| 1154 | void reset_control_bulk_put(int num_rstcs, struct reset_control_bulk_data *rstcs) |
| 1155 | { |
| 1156 | mutex_lock(&reset_list_mutex); |
| 1157 | while (num_rstcs--) |
| 1158 | __reset_control_put_internal(rstc: rstcs[num_rstcs].rstc); |
| 1159 | mutex_unlock(lock: &reset_list_mutex); |
| 1160 | } |
| 1161 | EXPORT_SYMBOL_GPL(reset_control_bulk_put); |
| 1162 | |
| 1163 | static void devm_reset_control_release(struct device *dev, void *res) |
| 1164 | { |
| 1165 | reset_control_put(*(struct reset_control **)res); |
| 1166 | } |
| 1167 | |
| 1168 | static void devm_reset_control_release_deasserted(struct device *dev, void *res) |
| 1169 | { |
| 1170 | struct reset_control *rstc = *(struct reset_control **)res; |
| 1171 | |
| 1172 | reset_control_assert(rstc); |
| 1173 | reset_control_put(rstc); |
| 1174 | } |
| 1175 | |
| 1176 | struct reset_control * |
| 1177 | __devm_reset_control_get(struct device *dev, const char *id, int index, |
| 1178 | enum reset_control_flags flags) |
| 1179 | { |
| 1180 | struct reset_control **ptr, *rstc; |
| 1181 | bool deasserted = flags & RESET_CONTROL_FLAGS_BIT_DEASSERTED; |
| 1182 | |
| 1183 | ptr = devres_alloc(deasserted ? devm_reset_control_release_deasserted : |
| 1184 | devm_reset_control_release, sizeof(*ptr), |
| 1185 | GFP_KERNEL); |
| 1186 | if (!ptr) |
| 1187 | return ERR_PTR(error: -ENOMEM); |
| 1188 | |
| 1189 | flags &= ~RESET_CONTROL_FLAGS_BIT_DEASSERTED; |
| 1190 | |
| 1191 | rstc = __reset_control_get(dev, id, index, flags); |
| 1192 | if (IS_ERR_OR_NULL(ptr: rstc)) { |
| 1193 | devres_free(res: ptr); |
| 1194 | return rstc; |
| 1195 | } |
| 1196 | |
| 1197 | if (deasserted) { |
| 1198 | int ret; |
| 1199 | |
| 1200 | ret = reset_control_deassert(rstc); |
| 1201 | if (ret) { |
| 1202 | reset_control_put(rstc); |
| 1203 | devres_free(res: ptr); |
| 1204 | return ERR_PTR(error: ret); |
| 1205 | } |
| 1206 | } |
| 1207 | |
| 1208 | *ptr = rstc; |
| 1209 | devres_add(dev, res: ptr); |
| 1210 | |
| 1211 | return rstc; |
| 1212 | } |
| 1213 | EXPORT_SYMBOL_GPL(__devm_reset_control_get); |
| 1214 | |
| 1215 | struct reset_control_bulk_devres { |
| 1216 | int num_rstcs; |
| 1217 | struct reset_control_bulk_data *rstcs; |
| 1218 | }; |
| 1219 | |
| 1220 | static void devm_reset_control_bulk_release(struct device *dev, void *res) |
| 1221 | { |
| 1222 | struct reset_control_bulk_devres *devres = res; |
| 1223 | |
| 1224 | reset_control_bulk_put(devres->num_rstcs, devres->rstcs); |
| 1225 | } |
| 1226 | |
| 1227 | static void devm_reset_control_bulk_release_deasserted(struct device *dev, void *res) |
| 1228 | { |
| 1229 | struct reset_control_bulk_devres *devres = res; |
| 1230 | |
| 1231 | reset_control_bulk_assert(devres->num_rstcs, devres->rstcs); |
| 1232 | reset_control_bulk_put(devres->num_rstcs, devres->rstcs); |
| 1233 | } |
| 1234 | |
| 1235 | int __devm_reset_control_bulk_get(struct device *dev, int num_rstcs, |
| 1236 | struct reset_control_bulk_data *rstcs, |
| 1237 | enum reset_control_flags flags) |
| 1238 | { |
| 1239 | struct reset_control_bulk_devres *ptr; |
| 1240 | bool deasserted = flags & RESET_CONTROL_FLAGS_BIT_DEASSERTED; |
| 1241 | int ret; |
| 1242 | |
| 1243 | ptr = devres_alloc(deasserted ? devm_reset_control_bulk_release_deasserted : |
| 1244 | devm_reset_control_bulk_release, sizeof(*ptr), |
| 1245 | GFP_KERNEL); |
| 1246 | if (!ptr) |
| 1247 | return -ENOMEM; |
| 1248 | |
| 1249 | flags &= ~RESET_CONTROL_FLAGS_BIT_DEASSERTED; |
| 1250 | |
| 1251 | ret = __reset_control_bulk_get(dev, num_rstcs, rstcs, flags); |
| 1252 | if (ret < 0) { |
| 1253 | devres_free(res: ptr); |
| 1254 | return ret; |
| 1255 | } |
| 1256 | |
| 1257 | if (deasserted) { |
| 1258 | ret = reset_control_bulk_deassert(num_rstcs, rstcs); |
| 1259 | if (ret) { |
| 1260 | reset_control_bulk_put(num_rstcs, rstcs); |
| 1261 | devres_free(res: ptr); |
| 1262 | return ret; |
| 1263 | } |
| 1264 | } |
| 1265 | |
| 1266 | ptr->num_rstcs = num_rstcs; |
| 1267 | ptr->rstcs = rstcs; |
| 1268 | devres_add(dev, res: ptr); |
| 1269 | |
| 1270 | return 0; |
| 1271 | } |
| 1272 | EXPORT_SYMBOL_GPL(__devm_reset_control_bulk_get); |
| 1273 | |
| 1274 | /** |
| 1275 | * __device_reset - find reset controller associated with the device |
| 1276 | * and perform reset |
| 1277 | * @dev: device to be reset by the controller |
| 1278 | * @optional: whether it is optional to reset the device |
| 1279 | * |
| 1280 | * Convenience wrapper for __reset_control_get() and reset_control_reset(). |
| 1281 | * This is useful for the common case of devices with single, dedicated reset |
| 1282 | * lines. _RST firmware method will be called for devices with ACPI. |
| 1283 | */ |
| 1284 | int __device_reset(struct device *dev, bool optional) |
| 1285 | { |
| 1286 | enum reset_control_flags flags; |
| 1287 | struct reset_control *rstc; |
| 1288 | int ret; |
| 1289 | |
| 1290 | #ifdef CONFIG_ACPI |
| 1291 | acpi_handle handle = ACPI_HANDLE(dev); |
| 1292 | |
| 1293 | if (handle) { |
| 1294 | if (!acpi_has_method(handle, name: "_RST" )) |
| 1295 | return optional ? 0 : -ENOENT; |
| 1296 | if (ACPI_FAILURE(acpi_evaluate_object(handle, "_RST" , NULL, |
| 1297 | NULL))) |
| 1298 | return -EIO; |
| 1299 | } |
| 1300 | #endif |
| 1301 | |
| 1302 | flags = optional ? RESET_CONTROL_OPTIONAL_EXCLUSIVE : RESET_CONTROL_EXCLUSIVE; |
| 1303 | rstc = __reset_control_get(dev, NULL, 0, flags); |
| 1304 | if (IS_ERR(ptr: rstc)) |
| 1305 | return PTR_ERR(ptr: rstc); |
| 1306 | |
| 1307 | ret = reset_control_reset(rstc); |
| 1308 | |
| 1309 | reset_control_put(rstc); |
| 1310 | |
| 1311 | return ret; |
| 1312 | } |
| 1313 | EXPORT_SYMBOL_GPL(__device_reset); |
| 1314 | |
| 1315 | /* |
| 1316 | * APIs to manage an array of reset controls. |
| 1317 | */ |
| 1318 | |
| 1319 | /** |
| 1320 | * of_reset_control_get_count - Count number of resets available with a device |
| 1321 | * |
| 1322 | * @node: device node that contains 'resets'. |
| 1323 | * |
| 1324 | * Returns positive reset count on success, or error number on failure and |
| 1325 | * on count being zero. |
| 1326 | */ |
| 1327 | static int of_reset_control_get_count(struct device_node *node) |
| 1328 | { |
| 1329 | int count; |
| 1330 | |
| 1331 | if (!node) |
| 1332 | return -EINVAL; |
| 1333 | |
| 1334 | count = of_count_phandle_with_args(np: node, list_name: "resets" , cells_name: "#reset-cells" ); |
| 1335 | if (count == 0) |
| 1336 | count = -ENOENT; |
| 1337 | |
| 1338 | return count; |
| 1339 | } |
| 1340 | |
| 1341 | /** |
| 1342 | * of_reset_control_array_get - Get a list of reset controls using |
| 1343 | * device node. |
| 1344 | * |
| 1345 | * @np: device node for the device that requests the reset controls array |
| 1346 | * @flags: whether reset controls are shared, optional, acquired |
| 1347 | * |
| 1348 | * Returns pointer to allocated reset_control on success or error on failure |
| 1349 | */ |
| 1350 | struct reset_control * |
| 1351 | of_reset_control_array_get(struct device_node *np, enum reset_control_flags flags) |
| 1352 | { |
| 1353 | bool optional = flags & RESET_CONTROL_FLAGS_BIT_OPTIONAL; |
| 1354 | struct reset_control_array *resets; |
| 1355 | struct reset_control *rstc; |
| 1356 | int num, i; |
| 1357 | |
| 1358 | num = of_reset_control_get_count(node: np); |
| 1359 | if (num < 0) |
| 1360 | return optional ? NULL : ERR_PTR(error: num); |
| 1361 | |
| 1362 | resets = kzalloc(struct_size(resets, rstc, num), GFP_KERNEL); |
| 1363 | if (!resets) |
| 1364 | return ERR_PTR(error: -ENOMEM); |
| 1365 | resets->num_rstcs = num; |
| 1366 | |
| 1367 | for (i = 0; i < num; i++) { |
| 1368 | rstc = __of_reset_control_get(np, NULL, i, flags); |
| 1369 | if (IS_ERR(ptr: rstc)) |
| 1370 | goto err_rst; |
| 1371 | resets->rstc[i] = rstc; |
| 1372 | } |
| 1373 | resets->base.array = true; |
| 1374 | |
| 1375 | return &resets->base; |
| 1376 | |
| 1377 | err_rst: |
| 1378 | mutex_lock(&reset_list_mutex); |
| 1379 | while (--i >= 0) |
| 1380 | __reset_control_put_internal(rstc: resets->rstc[i]); |
| 1381 | mutex_unlock(lock: &reset_list_mutex); |
| 1382 | |
| 1383 | kfree(objp: resets); |
| 1384 | |
| 1385 | return rstc; |
| 1386 | } |
| 1387 | EXPORT_SYMBOL_GPL(of_reset_control_array_get); |
| 1388 | |
| 1389 | /** |
| 1390 | * devm_reset_control_array_get - Resource managed reset control array get |
| 1391 | * |
| 1392 | * @dev: device that requests the list of reset controls |
| 1393 | * @flags: whether reset controls are shared, optional, acquired |
| 1394 | * |
| 1395 | * The reset control array APIs are intended for a list of resets |
| 1396 | * that just have to be asserted or deasserted, without any |
| 1397 | * requirements on the order. |
| 1398 | * |
| 1399 | * Returns pointer to allocated reset_control on success or error on failure |
| 1400 | */ |
| 1401 | struct reset_control * |
| 1402 | devm_reset_control_array_get(struct device *dev, enum reset_control_flags flags) |
| 1403 | { |
| 1404 | struct reset_control **ptr, *rstc; |
| 1405 | |
| 1406 | ptr = devres_alloc(devm_reset_control_release, sizeof(*ptr), |
| 1407 | GFP_KERNEL); |
| 1408 | if (!ptr) |
| 1409 | return ERR_PTR(error: -ENOMEM); |
| 1410 | |
| 1411 | rstc = of_reset_control_array_get(dev->of_node, flags); |
| 1412 | if (IS_ERR_OR_NULL(ptr: rstc)) { |
| 1413 | devres_free(res: ptr); |
| 1414 | return rstc; |
| 1415 | } |
| 1416 | |
| 1417 | *ptr = rstc; |
| 1418 | devres_add(dev, res: ptr); |
| 1419 | |
| 1420 | return rstc; |
| 1421 | } |
| 1422 | EXPORT_SYMBOL_GPL(devm_reset_control_array_get); |
| 1423 | |
| 1424 | /** |
| 1425 | * reset_control_get_count - Count number of resets available with a device |
| 1426 | * |
| 1427 | * @dev: device for which to return the number of resets |
| 1428 | * |
| 1429 | * Returns positive reset count on success, or error number on failure and |
| 1430 | * on count being zero. |
| 1431 | */ |
| 1432 | int reset_control_get_count(struct device *dev) |
| 1433 | { |
| 1434 | if (dev->of_node) |
| 1435 | return of_reset_control_get_count(node: dev->of_node); |
| 1436 | |
| 1437 | return -ENOENT; |
| 1438 | } |
| 1439 | EXPORT_SYMBOL_GPL(reset_control_get_count); |
| 1440 | |