1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef _LINUX_RESET_H_ 3 #define _LINUX_RESET_H_ 4 5 #include <linux/err.h> 6 #include <linux/errno.h> 7 #include <linux/types.h> 8 9 struct device; 10 struct device_node; 11 struct reset_control; 12 13 #ifdef CONFIG_RESET_CONTROLLER 14 15 int reset_control_reset(struct reset_control *rstc); 16 int reset_control_rearm(struct reset_control *rstc); 17 int reset_control_assert(struct reset_control *rstc); 18 int reset_control_deassert(struct reset_control *rstc); 19 int reset_control_status(struct reset_control *rstc); 20 int reset_control_acquire(struct reset_control *rstc); 21 void reset_control_release(struct reset_control *rstc); 22 23 struct reset_control *__of_reset_control_get(struct device_node *node, 24 const char *id, int index, bool shared, 25 bool optional, bool acquired); 26 struct reset_control *__reset_control_get(struct device *dev, const char *id, 27 int index, bool shared, 28 bool optional, bool acquired); 29 void reset_control_put(struct reset_control *rstc); 30 int __device_reset(struct device *dev, bool optional); 31 struct reset_control *__devm_reset_control_get(struct device *dev, 32 const char *id, int index, bool shared, 33 bool optional, bool acquired); 34 35 struct reset_control *devm_reset_control_array_get(struct device *dev, 36 bool shared, bool optional); 37 struct reset_control *of_reset_control_array_get(struct device_node *np, 38 bool shared, bool optional, 39 bool acquired); 40 41 int reset_control_get_count(struct device *dev); 42 43 #else 44 45 static inline int reset_control_reset(struct reset_control *rstc) 46 { 47 return 0; 48 } 49 50 static inline int reset_control_rearm(struct reset_control *rstc) 51 { 52 return 0; 53 } 54 55 static inline int reset_control_assert(struct reset_control *rstc) 56 { 57 return 0; 58 } 59 60 static inline int reset_control_deassert(struct reset_control *rstc) 61 { 62 return 0; 63 } 64 65 static inline int reset_control_status(struct reset_control *rstc) 66 { 67 return 0; 68 } 69 70 static inline int reset_control_acquire(struct reset_control *rstc) 71 { 72 return 0; 73 } 74 75 static inline void reset_control_release(struct reset_control *rstc) 76 { 77 } 78 79 static inline void reset_control_put(struct reset_control *rstc) 80 { 81 } 82 83 static inline int __device_reset(struct device *dev, bool optional) 84 { 85 return optional ? 0 : -ENOTSUPP; 86 } 87 88 static inline struct reset_control *__of_reset_control_get( 89 struct device_node *node, 90 const char *id, int index, bool shared, 91 bool optional, bool acquired) 92 { 93 return optional ? NULL : ERR_PTR(-ENOTSUPP); 94 } 95 96 static inline struct reset_control *__reset_control_get( 97 struct device *dev, const char *id, 98 int index, bool shared, bool optional, 99 bool acquired) 100 { 101 return optional ? NULL : ERR_PTR(-ENOTSUPP); 102 } 103 104 static inline struct reset_control *__devm_reset_control_get( 105 struct device *dev, const char *id, 106 int index, bool shared, bool optional, 107 bool acquired) 108 { 109 return optional ? NULL : ERR_PTR(-ENOTSUPP); 110 } 111 112 static inline struct reset_control * 113 devm_reset_control_array_get(struct device *dev, bool shared, bool optional) 114 { 115 return optional ? NULL : ERR_PTR(-ENOTSUPP); 116 } 117 118 static inline struct reset_control * 119 of_reset_control_array_get(struct device_node *np, bool shared, bool optional, 120 bool acquired) 121 { 122 return optional ? NULL : ERR_PTR(-ENOTSUPP); 123 } 124 125 static inline int reset_control_get_count(struct device *dev) 126 { 127 return -ENOENT; 128 } 129 130 #endif /* CONFIG_RESET_CONTROLLER */ 131 132 static inline int __must_check device_reset(struct device *dev) 133 { 134 return __device_reset(dev, false); 135 } 136 137 static inline int device_reset_optional(struct device *dev) 138 { 139 return __device_reset(dev, true); 140 } 141 142 /** 143 * reset_control_get_exclusive - Lookup and obtain an exclusive reference 144 * to a reset controller. 145 * @dev: device to be reset by the controller 146 * @id: reset line name 147 * 148 * Returns a struct reset_control or IS_ERR() condition containing errno. 149 * If this function is called more than once for the same reset_control it will 150 * return -EBUSY. 151 * 152 * See reset_control_get_shared() for details on shared references to 153 * reset-controls. 154 * 155 * Use of id names is optional. 156 */ 157 static inline struct reset_control * 158 __must_check reset_control_get_exclusive(struct device *dev, const char *id) 159 { 160 return __reset_control_get(dev, id, 0, false, false, true); 161 } 162 163 /** 164 * reset_control_get_exclusive_released - Lookup and obtain a temoprarily 165 * exclusive reference to a reset 166 * controller. 167 * @dev: device to be reset by the controller 168 * @id: reset line name 169 * 170 * Returns a struct reset_control or IS_ERR() condition containing errno. 171 * reset-controls returned by this function must be acquired via 172 * reset_control_acquire() before they can be used and should be released 173 * via reset_control_release() afterwards. 174 * 175 * Use of id names is optional. 176 */ 177 static inline struct reset_control * 178 __must_check reset_control_get_exclusive_released(struct device *dev, 179 const char *id) 180 { 181 return __reset_control_get(dev, id, 0, false, false, false); 182 } 183 184 /** 185 * reset_control_get_shared - Lookup and obtain a shared reference to a 186 * reset controller. 187 * @dev: device to be reset by the controller 188 * @id: reset line name 189 * 190 * Returns a struct reset_control or IS_ERR() condition containing errno. 191 * This function is intended for use with reset-controls which are shared 192 * between hardware blocks. 193 * 194 * When a reset-control is shared, the behavior of reset_control_assert / 195 * deassert is changed, the reset-core will keep track of a deassert_count 196 * and only (re-)assert the reset after reset_control_assert has been called 197 * as many times as reset_control_deassert was called. Also see the remark 198 * about shared reset-controls in the reset_control_assert docs. 199 * 200 * Calling reset_control_assert without first calling reset_control_deassert 201 * is not allowed on a shared reset control. Calling reset_control_reset is 202 * also not allowed on a shared reset control. 203 * 204 * Use of id names is optional. 205 */ 206 static inline struct reset_control *reset_control_get_shared( 207 struct device *dev, const char *id) 208 { 209 return __reset_control_get(dev, id, 0, true, false, false); 210 } 211 212 /** 213 * reset_control_get_optional_exclusive - optional reset_control_get_exclusive() 214 * @dev: device to be reset by the controller 215 * @id: reset line name 216 * 217 * Optional variant of reset_control_get_exclusive(). If the requested reset 218 * is not specified in the device tree, this function returns NULL instead of 219 * an error. 220 * 221 * See reset_control_get_exclusive() for more information. 222 */ 223 static inline struct reset_control *reset_control_get_optional_exclusive( 224 struct device *dev, const char *id) 225 { 226 return __reset_control_get(dev, id, 0, false, true, true); 227 } 228 229 /** 230 * reset_control_get_optional_shared - optional reset_control_get_shared() 231 * @dev: device to be reset by the controller 232 * @id: reset line name 233 * 234 * Optional variant of reset_control_get_shared(). If the requested reset 235 * is not specified in the device tree, this function returns NULL instead of 236 * an error. 237 * 238 * See reset_control_get_shared() for more information. 239 */ 240 static inline struct reset_control *reset_control_get_optional_shared( 241 struct device *dev, const char *id) 242 { 243 return __reset_control_get(dev, id, 0, true, true, false); 244 } 245 246 /** 247 * of_reset_control_get_exclusive - Lookup and obtain an exclusive reference 248 * to a reset controller. 249 * @node: device to be reset by the controller 250 * @id: reset line name 251 * 252 * Returns a struct reset_control or IS_ERR() condition containing errno. 253 * 254 * Use of id names is optional. 255 */ 256 static inline struct reset_control *of_reset_control_get_exclusive( 257 struct device_node *node, const char *id) 258 { 259 return __of_reset_control_get(node, id, 0, false, false, true); 260 } 261 262 /** 263 * of_reset_control_get_shared - Lookup and obtain a shared reference 264 * to a reset controller. 265 * @node: device to be reset by the controller 266 * @id: reset line name 267 * 268 * When a reset-control is shared, the behavior of reset_control_assert / 269 * deassert is changed, the reset-core will keep track of a deassert_count 270 * and only (re-)assert the reset after reset_control_assert has been called 271 * as many times as reset_control_deassert was called. Also see the remark 272 * about shared reset-controls in the reset_control_assert docs. 273 * 274 * Calling reset_control_assert without first calling reset_control_deassert 275 * is not allowed on a shared reset control. Calling reset_control_reset is 276 * also not allowed on a shared reset control. 277 * Returns a struct reset_control or IS_ERR() condition containing errno. 278 * 279 * Use of id names is optional. 280 */ 281 static inline struct reset_control *of_reset_control_get_shared( 282 struct device_node *node, const char *id) 283 { 284 return __of_reset_control_get(node, id, 0, true, false, false); 285 } 286 287 /** 288 * of_reset_control_get_exclusive_by_index - Lookup and obtain an exclusive 289 * reference to a reset controller 290 * by index. 291 * @node: device to be reset by the controller 292 * @index: index of the reset controller 293 * 294 * This is to be used to perform a list of resets for a device or power domain 295 * in whatever order. Returns a struct reset_control or IS_ERR() condition 296 * containing errno. 297 */ 298 static inline struct reset_control *of_reset_control_get_exclusive_by_index( 299 struct device_node *node, int index) 300 { 301 return __of_reset_control_get(node, NULL, index, false, false, true); 302 } 303 304 /** 305 * of_reset_control_get_shared_by_index - Lookup and obtain a shared 306 * reference to a reset controller 307 * by index. 308 * @node: device to be reset by the controller 309 * @index: index of the reset controller 310 * 311 * When a reset-control is shared, the behavior of reset_control_assert / 312 * deassert is changed, the reset-core will keep track of a deassert_count 313 * and only (re-)assert the reset after reset_control_assert has been called 314 * as many times as reset_control_deassert was called. Also see the remark 315 * about shared reset-controls in the reset_control_assert docs. 316 * 317 * Calling reset_control_assert without first calling reset_control_deassert 318 * is not allowed on a shared reset control. Calling reset_control_reset is 319 * also not allowed on a shared reset control. 320 * Returns a struct reset_control or IS_ERR() condition containing errno. 321 * 322 * This is to be used to perform a list of resets for a device or power domain 323 * in whatever order. Returns a struct reset_control or IS_ERR() condition 324 * containing errno. 325 */ 326 static inline struct reset_control *of_reset_control_get_shared_by_index( 327 struct device_node *node, int index) 328 { 329 return __of_reset_control_get(node, NULL, index, true, false, false); 330 } 331 332 /** 333 * devm_reset_control_get_exclusive - resource managed 334 * reset_control_get_exclusive() 335 * @dev: device to be reset by the controller 336 * @id: reset line name 337 * 338 * Managed reset_control_get_exclusive(). For reset controllers returned 339 * from this function, reset_control_put() is called automatically on driver 340 * detach. 341 * 342 * See reset_control_get_exclusive() for more information. 343 */ 344 static inline struct reset_control * 345 __must_check devm_reset_control_get_exclusive(struct device *dev, 346 const char *id) 347 { 348 return __devm_reset_control_get(dev, id, 0, false, false, true); 349 } 350 351 /** 352 * devm_reset_control_get_exclusive_released - resource managed 353 * reset_control_get_exclusive_released() 354 * @dev: device to be reset by the controller 355 * @id: reset line name 356 * 357 * Managed reset_control_get_exclusive_released(). For reset controllers 358 * returned from this function, reset_control_put() is called automatically on 359 * driver detach. 360 * 361 * See reset_control_get_exclusive_released() for more information. 362 */ 363 static inline struct reset_control * 364 __must_check devm_reset_control_get_exclusive_released(struct device *dev, 365 const char *id) 366 { 367 return __devm_reset_control_get(dev, id, 0, false, false, false); 368 } 369 370 /** 371 * devm_reset_control_get_optional_exclusive_released - resource managed 372 * reset_control_get_optional_exclusive_released() 373 * @dev: device to be reset by the controller 374 * @id: reset line name 375 * 376 * Managed-and-optional variant of reset_control_get_exclusive_released(). For 377 * reset controllers returned from this function, reset_control_put() is called 378 * automatically on driver detach. 379 * 380 * See reset_control_get_exclusive_released() for more information. 381 */ 382 static inline struct reset_control * 383 __must_check devm_reset_control_get_optional_exclusive_released(struct device *dev, 384 const char *id) 385 { 386 return __devm_reset_control_get(dev, id, 0, false, true, false); 387 } 388 389 /** 390 * devm_reset_control_get_shared - resource managed reset_control_get_shared() 391 * @dev: device to be reset by the controller 392 * @id: reset line name 393 * 394 * Managed reset_control_get_shared(). For reset controllers returned from 395 * this function, reset_control_put() is called automatically on driver detach. 396 * See reset_control_get_shared() for more information. 397 */ 398 static inline struct reset_control *devm_reset_control_get_shared( 399 struct device *dev, const char *id) 400 { 401 return __devm_reset_control_get(dev, id, 0, true, false, false); 402 } 403 404 /** 405 * devm_reset_control_get_optional_exclusive - resource managed 406 * reset_control_get_optional_exclusive() 407 * @dev: device to be reset by the controller 408 * @id: reset line name 409 * 410 * Managed reset_control_get_optional_exclusive(). For reset controllers 411 * returned from this function, reset_control_put() is called automatically on 412 * driver detach. 413 * 414 * See reset_control_get_optional_exclusive() for more information. 415 */ 416 static inline struct reset_control *devm_reset_control_get_optional_exclusive( 417 struct device *dev, const char *id) 418 { 419 return __devm_reset_control_get(dev, id, 0, false, true, true); 420 } 421 422 /** 423 * devm_reset_control_get_optional_shared - resource managed 424 * reset_control_get_optional_shared() 425 * @dev: device to be reset by the controller 426 * @id: reset line name 427 * 428 * Managed reset_control_get_optional_shared(). For reset controllers returned 429 * from this function, reset_control_put() is called automatically on driver 430 * detach. 431 * 432 * See reset_control_get_optional_shared() for more information. 433 */ 434 static inline struct reset_control *devm_reset_control_get_optional_shared( 435 struct device *dev, const char *id) 436 { 437 return __devm_reset_control_get(dev, id, 0, true, true, false); 438 } 439 440 /** 441 * devm_reset_control_get_exclusive_by_index - resource managed 442 * reset_control_get_exclusive() 443 * @dev: device to be reset by the controller 444 * @index: index of the reset controller 445 * 446 * Managed reset_control_get_exclusive(). For reset controllers returned from 447 * this function, reset_control_put() is called automatically on driver 448 * detach. 449 * 450 * See reset_control_get_exclusive() for more information. 451 */ 452 static inline struct reset_control * 453 devm_reset_control_get_exclusive_by_index(struct device *dev, int index) 454 { 455 return __devm_reset_control_get(dev, NULL, index, false, false, true); 456 } 457 458 /** 459 * devm_reset_control_get_shared_by_index - resource managed 460 * reset_control_get_shared 461 * @dev: device to be reset by the controller 462 * @index: index of the reset controller 463 * 464 * Managed reset_control_get_shared(). For reset controllers returned from 465 * this function, reset_control_put() is called automatically on driver detach. 466 * See reset_control_get_shared() for more information. 467 */ 468 static inline struct reset_control * 469 devm_reset_control_get_shared_by_index(struct device *dev, int index) 470 { 471 return __devm_reset_control_get(dev, NULL, index, true, false, false); 472 } 473 474 /* 475 * TEMPORARY calls to use during transition: 476 * 477 * of_reset_control_get() => of_reset_control_get_exclusive() 478 * 479 * These inline function calls will be removed once all consumers 480 * have been moved over to the new explicit API. 481 */ 482 static inline struct reset_control *of_reset_control_get( 483 struct device_node *node, const char *id) 484 { 485 return of_reset_control_get_exclusive(node, id); 486 } 487 488 static inline struct reset_control *of_reset_control_get_by_index( 489 struct device_node *node, int index) 490 { 491 return of_reset_control_get_exclusive_by_index(node, index); 492 } 493 494 static inline struct reset_control *devm_reset_control_get( 495 struct device *dev, const char *id) 496 { 497 return devm_reset_control_get_exclusive(dev, id); 498 } 499 500 static inline struct reset_control *devm_reset_control_get_optional( 501 struct device *dev, const char *id) 502 { 503 return devm_reset_control_get_optional_exclusive(dev, id); 504 505 } 506 507 static inline struct reset_control *devm_reset_control_get_by_index( 508 struct device *dev, int index) 509 { 510 return devm_reset_control_get_exclusive_by_index(dev, index); 511 } 512 513 /* 514 * APIs to manage a list of reset controllers 515 */ 516 static inline struct reset_control * 517 devm_reset_control_array_get_exclusive(struct device *dev) 518 { 519 return devm_reset_control_array_get(dev, false, false); 520 } 521 522 static inline struct reset_control * 523 devm_reset_control_array_get_shared(struct device *dev) 524 { 525 return devm_reset_control_array_get(dev, true, false); 526 } 527 528 static inline struct reset_control * 529 devm_reset_control_array_get_optional_exclusive(struct device *dev) 530 { 531 return devm_reset_control_array_get(dev, false, true); 532 } 533 534 static inline struct reset_control * 535 devm_reset_control_array_get_optional_shared(struct device *dev) 536 { 537 return devm_reset_control_array_get(dev, true, true); 538 } 539 540 static inline struct reset_control * 541 of_reset_control_array_get_exclusive(struct device_node *node) 542 { 543 return of_reset_control_array_get(node, false, false, true); 544 } 545 546 static inline struct reset_control * 547 of_reset_control_array_get_exclusive_released(struct device_node *node) 548 { 549 return of_reset_control_array_get(node, false, false, false); 550 } 551 552 static inline struct reset_control * 553 of_reset_control_array_get_shared(struct device_node *node) 554 { 555 return of_reset_control_array_get(node, true, false, true); 556 } 557 558 static inline struct reset_control * 559 of_reset_control_array_get_optional_exclusive(struct device_node *node) 560 { 561 return of_reset_control_array_get(node, false, true, true); 562 } 563 564 static inline struct reset_control * 565 of_reset_control_array_get_optional_shared(struct device_node *node) 566 { 567 return of_reset_control_array_get(node, true, true, true); 568 } 569 #endif 570