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