1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Multiplexer subsystem 4 * 5 * Copyright (C) 2017 Axentia Technologies AB 6 * 7 * Author: Peter Rosin <peda@axentia.se> 8 */ 9 10 #define pr_fmt(fmt) "mux-core: " fmt 11 12 #include <linux/delay.h> 13 #include <linux/device.h> 14 #include <linux/err.h> 15 #include <linux/export.h> 16 #include <linux/idr.h> 17 #include <linux/init.h> 18 #include <linux/module.h> 19 #include <linux/mux/consumer.h> 20 #include <linux/mux/driver.h> 21 #include <linux/of.h> 22 #include <linux/slab.h> 23 24 /* 25 * The idle-as-is "state" is not an actual state that may be selected, it 26 * only implies that the state should not be changed. So, use that state 27 * as indication that the cached state of the multiplexer is unknown. 28 */ 29 #define MUX_CACHE_UNKNOWN MUX_IDLE_AS_IS 30 31 /** 32 * struct mux_state - Represents a mux controller state specific to a given 33 * consumer. 34 * @mux: Pointer to a mux controller. 35 * @state: State of the mux to be selected. 36 * 37 * This structure is specific to the consumer that acquires it and has 38 * information specific to that consumer. 39 */ 40 struct mux_state { 41 struct mux_control *mux; 42 unsigned int state; 43 }; 44 45 static const struct class mux_class = { 46 .name = "mux", 47 }; 48 49 /** 50 * struct devm_mux_state_state - Tracks managed resources for mux-state objects. 51 * @mstate: Pointer to a mux state. 52 * @exit: An optional callback to execute before free. 53 */ 54 struct devm_mux_state_state { 55 struct mux_state *mstate; 56 int (*exit)(struct mux_state *mstate); 57 }; 58 59 static DEFINE_IDA(mux_ida); 60 61 static int __init mux_init(void) 62 { 63 ida_init(&mux_ida); 64 return class_register(&mux_class); 65 } 66 67 static void __exit mux_exit(void) 68 { 69 class_unregister(&mux_class); 70 ida_destroy(&mux_ida); 71 } 72 73 static void mux_chip_release(struct device *dev) 74 { 75 struct mux_chip *mux_chip = to_mux_chip(dev); 76 77 ida_free(&mux_ida, mux_chip->id); 78 kfree(mux_chip); 79 } 80 81 static const struct device_type mux_type = { 82 .name = "mux-chip", 83 .release = mux_chip_release, 84 }; 85 86 /** 87 * mux_chip_alloc() - Allocate a mux-chip. 88 * @dev: The parent device implementing the mux interface. 89 * @controllers: The number of mux controllers to allocate for this chip. 90 * @sizeof_priv: Size of extra memory area for private use by the caller. 91 * 92 * After allocating the mux-chip with the desired number of mux controllers 93 * but before registering the chip, the mux driver is required to configure 94 * the number of valid mux states in the mux_chip->mux[N].states members and 95 * the desired idle state in the returned mux_chip->mux[N].idle_state members. 96 * The default idle state is MUX_IDLE_AS_IS. The mux driver also needs to 97 * provide a pointer to the operations struct in the mux_chip->ops member 98 * before registering the mux-chip with mux_chip_register. 99 * 100 * Return: A pointer to the new mux-chip, or an ERR_PTR with a negative errno. 101 */ 102 struct mux_chip *mux_chip_alloc(struct device *dev, 103 unsigned int controllers, size_t sizeof_priv) 104 { 105 struct mux_chip *mux_chip; 106 int i; 107 108 if (WARN_ON(!dev || !controllers)) 109 return ERR_PTR(-EINVAL); 110 111 mux_chip = kzalloc(size_add(struct_size(mux_chip, mux, controllers), 112 sizeof_priv), 113 GFP_KERNEL); 114 if (!mux_chip) 115 return ERR_PTR(-ENOMEM); 116 117 mux_chip->dev.class = &mux_class; 118 mux_chip->dev.type = &mux_type; 119 mux_chip->dev.parent = dev; 120 mux_chip->dev.of_node = dev->of_node; 121 dev_set_drvdata(&mux_chip->dev, mux_chip); 122 123 mux_chip->id = ida_alloc(&mux_ida, GFP_KERNEL); 124 if (mux_chip->id < 0) { 125 int err = mux_chip->id; 126 127 pr_err("muxchipX failed to get a device id\n"); 128 kfree(mux_chip); 129 return ERR_PTR(err); 130 } 131 dev_set_name(&mux_chip->dev, "muxchip%d", mux_chip->id); 132 133 mux_chip->controllers = controllers; 134 for (i = 0; i < controllers; ++i) { 135 struct mux_control *mux = &mux_chip->mux[i]; 136 137 mux->chip = mux_chip; 138 sema_init(&mux->lock, 1); 139 mux->cached_state = MUX_CACHE_UNKNOWN; 140 mux->idle_state = MUX_IDLE_AS_IS; 141 mux->last_change = ktime_get(); 142 } 143 144 device_initialize(&mux_chip->dev); 145 146 return mux_chip; 147 } 148 EXPORT_SYMBOL_GPL(mux_chip_alloc); 149 150 static int mux_control_set(struct mux_control *mux, int state) 151 { 152 int ret = mux->chip->ops->set(mux, state); 153 154 mux->cached_state = ret < 0 ? MUX_CACHE_UNKNOWN : state; 155 if (ret >= 0) 156 mux->last_change = ktime_get(); 157 158 return ret; 159 } 160 161 /** 162 * mux_chip_register() - Register a mux-chip, thus readying the controllers 163 * for use. 164 * @mux_chip: The mux-chip to register. 165 * 166 * Do not retry registration of the same mux-chip on failure. You should 167 * instead put it away with mux_chip_free() and allocate a new one, if you 168 * for some reason would like to retry registration. 169 * 170 * Return: Zero on success or a negative errno on error. 171 */ 172 int mux_chip_register(struct mux_chip *mux_chip) 173 { 174 int i; 175 int ret; 176 177 for (i = 0; i < mux_chip->controllers; ++i) { 178 struct mux_control *mux = &mux_chip->mux[i]; 179 180 if (mux->idle_state == mux->cached_state) 181 continue; 182 183 ret = mux_control_set(mux, mux->idle_state); 184 if (ret < 0) { 185 dev_err(&mux_chip->dev, "unable to set idle state\n"); 186 return ret; 187 } 188 } 189 190 ret = device_add(&mux_chip->dev); 191 if (ret < 0) 192 dev_err(&mux_chip->dev, 193 "device_add failed in %s: %d\n", __func__, ret); 194 return ret; 195 } 196 EXPORT_SYMBOL_GPL(mux_chip_register); 197 198 /** 199 * mux_chip_unregister() - Take the mux-chip off-line. 200 * @mux_chip: The mux-chip to unregister. 201 * 202 * mux_chip_unregister() reverses the effects of mux_chip_register(). 203 * But not completely, you should not try to call mux_chip_register() 204 * on a mux-chip that has been registered before. 205 */ 206 void mux_chip_unregister(struct mux_chip *mux_chip) 207 { 208 device_del(&mux_chip->dev); 209 } 210 EXPORT_SYMBOL_GPL(mux_chip_unregister); 211 212 /** 213 * mux_chip_free() - Free the mux-chip for good. 214 * @mux_chip: The mux-chip to free. 215 * 216 * mux_chip_free() reverses the effects of mux_chip_alloc(). 217 */ 218 void mux_chip_free(struct mux_chip *mux_chip) 219 { 220 if (!mux_chip) 221 return; 222 223 put_device(&mux_chip->dev); 224 } 225 EXPORT_SYMBOL_GPL(mux_chip_free); 226 227 static void devm_mux_chip_release(struct device *dev, void *res) 228 { 229 struct mux_chip *mux_chip = *(struct mux_chip **)res; 230 231 mux_chip_free(mux_chip); 232 } 233 234 /** 235 * devm_mux_chip_alloc() - Resource-managed version of mux_chip_alloc(). 236 * @dev: The parent device implementing the mux interface. 237 * @controllers: The number of mux controllers to allocate for this chip. 238 * @sizeof_priv: Size of extra memory area for private use by the caller. 239 * 240 * See mux_chip_alloc() for more details. 241 * 242 * Return: A pointer to the new mux-chip, or an ERR_PTR with a negative errno. 243 */ 244 struct mux_chip *devm_mux_chip_alloc(struct device *dev, 245 unsigned int controllers, 246 size_t sizeof_priv) 247 { 248 struct mux_chip **ptr, *mux_chip; 249 250 ptr = devres_alloc(devm_mux_chip_release, sizeof(*ptr), GFP_KERNEL); 251 if (!ptr) 252 return ERR_PTR(-ENOMEM); 253 254 mux_chip = mux_chip_alloc(dev, controllers, sizeof_priv); 255 if (IS_ERR(mux_chip)) { 256 devres_free(ptr); 257 return mux_chip; 258 } 259 260 *ptr = mux_chip; 261 devres_add(dev, ptr); 262 263 return mux_chip; 264 } 265 EXPORT_SYMBOL_GPL(devm_mux_chip_alloc); 266 267 static void devm_mux_chip_reg_release(struct device *dev, void *res) 268 { 269 struct mux_chip *mux_chip = *(struct mux_chip **)res; 270 271 mux_chip_unregister(mux_chip); 272 } 273 274 /** 275 * devm_mux_chip_register() - Resource-managed version mux_chip_register(). 276 * @dev: The parent device implementing the mux interface. 277 * @mux_chip: The mux-chip to register. 278 * 279 * See mux_chip_register() for more details. 280 * 281 * Return: Zero on success or a negative errno on error. 282 */ 283 int devm_mux_chip_register(struct device *dev, 284 struct mux_chip *mux_chip) 285 { 286 struct mux_chip **ptr; 287 int res; 288 289 ptr = devres_alloc(devm_mux_chip_reg_release, sizeof(*ptr), GFP_KERNEL); 290 if (!ptr) 291 return -ENOMEM; 292 293 res = mux_chip_register(mux_chip); 294 if (res) { 295 devres_free(ptr); 296 return res; 297 } 298 299 *ptr = mux_chip; 300 devres_add(dev, ptr); 301 302 return res; 303 } 304 EXPORT_SYMBOL_GPL(devm_mux_chip_register); 305 306 /** 307 * mux_control_states() - Query the number of multiplexer states. 308 * @mux: The mux-control to query. 309 * 310 * Return: The number of multiplexer states. 311 */ 312 unsigned int mux_control_states(struct mux_control *mux) 313 { 314 return mux->states; 315 } 316 EXPORT_SYMBOL_GPL(mux_control_states); 317 318 /* 319 * The mux->lock must be down when calling this function. 320 */ 321 static int __mux_control_select(struct mux_control *mux, int state) 322 { 323 int ret; 324 325 if (WARN_ON(state < 0 || state >= mux->states)) 326 return -EINVAL; 327 328 if (mux->cached_state == state) 329 return 0; 330 331 ret = mux_control_set(mux, state); 332 if (ret >= 0) 333 return 0; 334 335 /* The mux update failed, try to revert if appropriate... */ 336 if (mux->idle_state != MUX_IDLE_AS_IS) 337 mux_control_set(mux, mux->idle_state); 338 339 return ret; 340 } 341 342 static void mux_control_delay(struct mux_control *mux, unsigned int delay_us) 343 { 344 ktime_t delayend; 345 s64 remaining; 346 347 if (!delay_us) 348 return; 349 350 delayend = ktime_add_us(mux->last_change, delay_us); 351 remaining = ktime_us_delta(delayend, ktime_get()); 352 if (remaining > 0) 353 fsleep(remaining); 354 } 355 356 /** 357 * mux_control_select_delay() - Select the given multiplexer state. 358 * @mux: The mux-control to request a change of state from. 359 * @state: The new requested state. 360 * @delay_us: The time to delay (in microseconds) if the mux state is changed. 361 * 362 * On successfully selecting the mux-control state, it will be locked until 363 * there is a call to mux_control_deselect(). If the mux-control is already 364 * selected when mux_control_select() is called, the caller will be blocked 365 * until mux_control_deselect() or mux_state_deselect() is called (by someone 366 * else). 367 * 368 * Therefore, make sure to call mux_control_deselect() when the operation is 369 * complete and the mux-control is free for others to use, but do not call 370 * mux_control_deselect() if mux_control_select() fails. 371 * 372 * Return: 0 when the mux-control state has the requested state or a negative 373 * errno on error. 374 */ 375 int mux_control_select_delay(struct mux_control *mux, unsigned int state, 376 unsigned int delay_us) 377 { 378 int ret; 379 380 ret = down_killable(&mux->lock); 381 if (ret < 0) 382 return ret; 383 384 ret = __mux_control_select(mux, state); 385 if (ret >= 0) 386 mux_control_delay(mux, delay_us); 387 388 if (ret < 0) 389 up(&mux->lock); 390 391 return ret; 392 } 393 EXPORT_SYMBOL_GPL(mux_control_select_delay); 394 395 /** 396 * mux_state_select_delay() - Select the given multiplexer state. 397 * @mstate: The mux-state to select. 398 * @delay_us: The time to delay (in microseconds) if the mux state is changed. 399 * 400 * On successfully selecting the mux-state, its mux-control will be locked 401 * until there is a call to mux_state_deselect(). If the mux-control is already 402 * selected when mux_state_select() is called, the caller will be blocked 403 * until mux_state_deselect() or mux_control_deselect() is called (by someone 404 * else). 405 * 406 * Therefore, make sure to call mux_state_deselect() when the operation is 407 * complete and the mux-control is free for others to use, but do not call 408 * mux_state_deselect() if mux_state_select() fails. 409 * 410 * Return: 0 when the mux-state has been selected or a negative 411 * errno on error. 412 */ 413 int mux_state_select_delay(struct mux_state *mstate, unsigned int delay_us) 414 { 415 return mux_control_select_delay(mstate->mux, mstate->state, delay_us); 416 } 417 EXPORT_SYMBOL_GPL(mux_state_select_delay); 418 419 /** 420 * mux_control_try_select_delay() - Try to select the given multiplexer state. 421 * @mux: The mux-control to request a change of state from. 422 * @state: The new requested state. 423 * @delay_us: The time to delay (in microseconds) if the mux state is changed. 424 * 425 * On successfully selecting the mux-control state, it will be locked until 426 * mux_control_deselect() is called. 427 * 428 * Therefore, make sure to call mux_control_deselect() when the operation is 429 * complete and the mux-control is free for others to use, but do not call 430 * mux_control_deselect() if mux_control_try_select() fails. 431 * 432 * Return: 0 when the mux-control state has the requested state or a negative 433 * errno on error. Specifically -EBUSY if the mux-control is contended. 434 */ 435 int mux_control_try_select_delay(struct mux_control *mux, unsigned int state, 436 unsigned int delay_us) 437 { 438 int ret; 439 440 if (down_trylock(&mux->lock)) 441 return -EBUSY; 442 443 ret = __mux_control_select(mux, state); 444 if (ret >= 0) 445 mux_control_delay(mux, delay_us); 446 447 if (ret < 0) 448 up(&mux->lock); 449 450 return ret; 451 } 452 EXPORT_SYMBOL_GPL(mux_control_try_select_delay); 453 454 /** 455 * mux_state_try_select_delay() - Try to select the given multiplexer state. 456 * @mstate: The mux-state to select. 457 * @delay_us: The time to delay (in microseconds) if the mux state is changed. 458 * 459 * On successfully selecting the mux-state, its mux-control will be locked 460 * until mux_state_deselect() is called. 461 * 462 * Therefore, make sure to call mux_state_deselect() when the operation is 463 * complete and the mux-control is free for others to use, but do not call 464 * mux_state_deselect() if mux_state_try_select() fails. 465 * 466 * Return: 0 when the mux-state has been selected or a negative errno on 467 * error. Specifically -EBUSY if the mux-control is contended. 468 */ 469 int mux_state_try_select_delay(struct mux_state *mstate, unsigned int delay_us) 470 { 471 return mux_control_try_select_delay(mstate->mux, mstate->state, delay_us); 472 } 473 EXPORT_SYMBOL_GPL(mux_state_try_select_delay); 474 475 /** 476 * mux_control_deselect() - Deselect the previously selected multiplexer state. 477 * @mux: The mux-control to deselect. 478 * 479 * It is required that a single call is made to mux_control_deselect() for 480 * each and every successful call made to either of mux_control_select() or 481 * mux_control_try_select(). 482 * 483 * Return: 0 on success and a negative errno on error. An error can only 484 * occur if the mux has an idle state. Note that even if an error occurs, the 485 * mux-control is unlocked and is thus free for the next access. 486 */ 487 int mux_control_deselect(struct mux_control *mux) 488 { 489 int ret = 0; 490 491 if (mux->idle_state != MUX_IDLE_AS_IS && 492 mux->idle_state != mux->cached_state) 493 ret = mux_control_set(mux, mux->idle_state); 494 495 up(&mux->lock); 496 497 return ret; 498 } 499 EXPORT_SYMBOL_GPL(mux_control_deselect); 500 501 /** 502 * mux_state_deselect() - Deselect the previously selected multiplexer state. 503 * @mstate: The mux-state to deselect. 504 * 505 * It is required that a single call is made to mux_state_deselect() for 506 * each and every successful call made to either of mux_state_select() or 507 * mux_state_try_select(). 508 * 509 * Return: 0 on success and a negative errno on error. An error can only 510 * occur if the mux has an idle state. Note that even if an error occurs, the 511 * mux-control is unlocked and is thus free for the next access. 512 */ 513 int mux_state_deselect(struct mux_state *mstate) 514 { 515 return mux_control_deselect(mstate->mux); 516 } 517 EXPORT_SYMBOL_GPL(mux_state_deselect); 518 519 /* Note this function returns a reference to the mux_chip dev. */ 520 static struct mux_chip *of_find_mux_chip_by_node(struct device_node *np) 521 { 522 struct device *dev; 523 524 dev = class_find_device_by_of_node(&mux_class, np); 525 526 return dev ? to_mux_chip(dev) : NULL; 527 } 528 529 /** 530 * mux_get() - Get the mux-control for a device. 531 * @dev: The device that needs a mux-control. 532 * @mux_name: The name identifying the mux-control. 533 * @state: Pointer to where the requested state is returned, or NULL when 534 * the required multiplexer states are handled by other means. 535 * @optional: Whether to return NULL and silence errors when mux doesn't exist. 536 * 537 * Return: Pointer to the mux-control on success, an ERR_PTR with a negative 538 * errno on error, or NULL if optional is true and mux doesn't exist. 539 */ 540 static struct mux_control *mux_get(struct device *dev, const char *mux_name, 541 unsigned int *state, bool optional) 542 { 543 struct device_node *np = dev->of_node; 544 struct of_phandle_args args; 545 struct mux_chip *mux_chip; 546 unsigned int controller; 547 int index = 0; 548 int ret; 549 550 if (mux_name) { 551 if (state) 552 index = of_property_match_string(np, "mux-state-names", 553 mux_name); 554 else 555 index = of_property_match_string(np, "mux-control-names", 556 mux_name); 557 if (index < 0 && optional) { 558 return NULL; 559 } else if (index < 0) { 560 dev_err(dev, "mux controller '%s' not found\n", 561 mux_name); 562 return ERR_PTR(index); 563 } 564 } 565 566 if (state) 567 ret = of_parse_phandle_with_args(np, 568 "mux-states", "#mux-state-cells", 569 index, &args); 570 else 571 ret = of_parse_phandle_with_args(np, 572 "mux-controls", "#mux-control-cells", 573 index, &args); 574 if (ret) { 575 if (optional && ret == -ENOENT) 576 return NULL; 577 578 dev_err(dev, "%pOF: failed to get mux-%s %s(%i)\n", 579 np, state ? "state" : "control", 580 mux_name ?: "", index); 581 return ERR_PTR(ret); 582 } 583 584 mux_chip = of_find_mux_chip_by_node(args.np); 585 of_node_put(args.np); 586 if (!mux_chip) 587 return ERR_PTR(-EPROBE_DEFER); 588 589 controller = 0; 590 if (state) { 591 if (args.args_count > 2 || args.args_count == 0 || 592 (args.args_count < 2 && mux_chip->controllers > 1)) { 593 dev_err(dev, "%pOF: wrong #mux-state-cells for %pOF\n", 594 np, args.np); 595 put_device(&mux_chip->dev); 596 return ERR_PTR(-EINVAL); 597 } 598 599 if (args.args_count == 2) { 600 controller = args.args[0]; 601 *state = args.args[1]; 602 } else { 603 *state = args.args[0]; 604 } 605 606 } else { 607 if (args.args_count > 1 || 608 (!args.args_count && mux_chip->controllers > 1)) { 609 dev_err(dev, "%pOF: wrong #mux-control-cells for %pOF\n", 610 np, args.np); 611 put_device(&mux_chip->dev); 612 return ERR_PTR(-EINVAL); 613 } 614 615 if (args.args_count) 616 controller = args.args[0]; 617 } 618 619 if (controller >= mux_chip->controllers) { 620 dev_err(dev, "%pOF: bad mux controller %u specified in %pOF\n", 621 np, controller, args.np); 622 put_device(&mux_chip->dev); 623 return ERR_PTR(-EINVAL); 624 } 625 626 return &mux_chip->mux[controller]; 627 } 628 629 /** 630 * mux_control_get() - Get the mux-control for a device. 631 * @dev: The device that needs a mux-control. 632 * @mux_name: The name identifying the mux-control. 633 * 634 * Return: A pointer to the mux-control, or an ERR_PTR with a negative errno. 635 */ 636 struct mux_control *mux_control_get(struct device *dev, const char *mux_name) 637 { 638 struct mux_control *mux = mux_get(dev, mux_name, NULL, false); 639 640 if (!mux) 641 return ERR_PTR(-ENOENT); 642 643 return mux; 644 } 645 EXPORT_SYMBOL_GPL(mux_control_get); 646 647 /** 648 * mux_control_get_optional() - Get the optional mux-control for a device. 649 * @dev: The device that needs a mux-control. 650 * @mux_name: The name identifying the mux-control. 651 * 652 * Return: Pointer to the mux-control on success, an ERR_PTR with a negative 653 * errno on error, or NULL if mux doesn't exist. 654 */ 655 struct mux_control *mux_control_get_optional(struct device *dev, const char *mux_name) 656 { 657 return mux_get(dev, mux_name, NULL, true); 658 } 659 EXPORT_SYMBOL_GPL(mux_control_get_optional); 660 661 /** 662 * mux_control_put() - Put away the mux-control for good. 663 * @mux: The mux-control to put away. 664 * 665 * mux_control_put() reverses the effects of mux_control_get(). 666 */ 667 void mux_control_put(struct mux_control *mux) 668 { 669 put_device(&mux->chip->dev); 670 } 671 EXPORT_SYMBOL_GPL(mux_control_put); 672 673 static void devm_mux_control_release(struct device *dev, void *res) 674 { 675 struct mux_control *mux = *(struct mux_control **)res; 676 677 mux_control_put(mux); 678 } 679 680 /** 681 * devm_mux_control_get() - Get the mux-control for a device, with resource 682 * management. 683 * @dev: The device that needs a mux-control. 684 * @mux_name: The name identifying the mux-control. 685 * 686 * Return: Pointer to the mux-control, or an ERR_PTR with a negative errno. 687 */ 688 struct mux_control *devm_mux_control_get(struct device *dev, 689 const char *mux_name) 690 { 691 struct mux_control **ptr, *mux; 692 693 ptr = devres_alloc(devm_mux_control_release, sizeof(*ptr), GFP_KERNEL); 694 if (!ptr) 695 return ERR_PTR(-ENOMEM); 696 697 mux = mux_control_get(dev, mux_name); 698 if (IS_ERR(mux)) { 699 devres_free(ptr); 700 return mux; 701 } 702 703 *ptr = mux; 704 devres_add(dev, ptr); 705 706 return mux; 707 } 708 EXPORT_SYMBOL_GPL(devm_mux_control_get); 709 710 /** 711 * mux_state_get() - Get the mux-state for a device. 712 * @dev: The device that needs a mux-state. 713 * @mux_name: The name identifying the mux-state. 714 * @optional: Whether to return NULL and silence errors when mux doesn't exist. 715 * 716 * Return: Pointer to the mux-state on success, an ERR_PTR with a negative 717 * errno on error, or NULL if optional is true and mux doesn't exist. 718 */ 719 static struct mux_state *mux_state_get(struct device *dev, const char *mux_name, bool optional) 720 { 721 struct mux_state *mstate; 722 723 mstate = kzalloc_obj(*mstate); 724 if (!mstate) 725 return ERR_PTR(-ENOMEM); 726 727 mstate->mux = mux_get(dev, mux_name, &mstate->state, optional); 728 if (IS_ERR(mstate->mux)) { 729 int err = PTR_ERR(mstate->mux); 730 731 kfree(mstate); 732 return ERR_PTR(err); 733 } else if (!mstate->mux) { 734 kfree(mstate); 735 return optional ? NULL : ERR_PTR(-ENOENT); 736 } 737 738 return mstate; 739 } 740 741 /* 742 * mux_state_put() - Put away the mux-state for good. 743 * @mstate: The mux-state to put away. 744 * 745 * mux_state_put() reverses the effects of mux_state_get(). 746 */ 747 static void mux_state_put(struct mux_state *mstate) 748 { 749 mux_control_put(mstate->mux); 750 kfree(mstate); 751 } 752 753 static void devm_mux_state_release(struct device *dev, void *res) 754 { 755 struct devm_mux_state_state *devm_state = res; 756 757 if (devm_state->exit) 758 devm_state->exit(devm_state->mstate); 759 760 mux_state_put(devm_state->mstate); 761 } 762 763 /** 764 * __devm_mux_state_get() - Get the optional mux-state for a device, 765 * with resource management. 766 * @dev: The device that needs a mux-state. 767 * @mux_name: The name identifying the mux-state. 768 * @optional: Whether to return NULL and silence errors when mux doesn't exist. 769 * @init: Optional function pointer for mux-state object initialisation. 770 * @exit: Optional function pointer for mux-state object cleanup on release. 771 * 772 * Return: Pointer to the mux-state on success, an ERR_PTR with a negative 773 * errno on error, or NULL if optional is true and mux doesn't exist. 774 */ 775 static struct mux_state *__devm_mux_state_get(struct device *dev, const char *mux_name, 776 bool optional, 777 int (*init)(struct mux_state *mstate), 778 int (*exit)(struct mux_state *mstate)) 779 { 780 struct devm_mux_state_state *devm_state; 781 struct mux_state *mstate; 782 int ret; 783 784 mstate = mux_state_get(dev, mux_name, optional); 785 if (IS_ERR(mstate)) 786 return ERR_CAST(mstate); 787 else if (optional && !mstate) 788 return NULL; 789 else if (!mstate) 790 return ERR_PTR(-ENOENT); 791 792 devm_state = devres_alloc(devm_mux_state_release, sizeof(*devm_state), GFP_KERNEL); 793 if (!devm_state) { 794 ret = -ENOMEM; 795 goto err_devres_alloc; 796 } 797 798 if (init) { 799 ret = init(mstate); 800 if (ret) 801 goto err_mux_state_init; 802 } 803 804 devm_state->mstate = mstate; 805 devm_state->exit = exit; 806 devres_add(dev, devm_state); 807 808 return mstate; 809 810 err_mux_state_init: 811 devres_free(devm_state); 812 err_devres_alloc: 813 mux_state_put(mstate); 814 return ERR_PTR(ret); 815 } 816 817 /** 818 * devm_mux_state_get() - Get the mux-state for a device, with resource 819 * management. 820 * @dev: The device that needs a mux-control. 821 * @mux_name: The name identifying the mux-control. 822 * 823 * Return: Pointer to the mux-state, or an ERR_PTR with a negative errno. 824 * 825 * The mux-state will automatically be freed on release. 826 */ 827 struct mux_state *devm_mux_state_get(struct device *dev, const char *mux_name) 828 { 829 return __devm_mux_state_get(dev, mux_name, false, NULL, NULL); 830 } 831 EXPORT_SYMBOL_GPL(devm_mux_state_get); 832 833 /** 834 * devm_mux_state_get_optional() - Get the optional mux-state for a device, 835 * with resource management. 836 * @dev: The device that needs a mux-state. 837 * @mux_name: The name identifying the mux-state. 838 * 839 * Return: Pointer to the mux-state on success, an ERR_PTR with a negative 840 * errno on error, or NULL if mux doesn't exist. 841 * 842 * The mux-state will automatically be freed on release. 843 */ 844 struct mux_state *devm_mux_state_get_optional(struct device *dev, const char *mux_name) 845 { 846 return __devm_mux_state_get(dev, mux_name, true, NULL, NULL); 847 } 848 EXPORT_SYMBOL_GPL(devm_mux_state_get_optional); 849 850 /** 851 * devm_mux_state_get_selected() - Get the mux-state for a device, with 852 * resource management. 853 * @dev: The device that needs a mux-state. 854 * @mux_name: The name identifying the mux-state. 855 * 856 * Return: Pointer to the mux-state, or an ERR_PTR with a negative errno. 857 * 858 * The returned mux-state (if valid) is already selected. 859 * 860 * The mux-state will automatically be deselected and freed on release. 861 */ 862 struct mux_state *devm_mux_state_get_selected(struct device *dev, const char *mux_name) 863 { 864 return __devm_mux_state_get(dev, mux_name, false, mux_state_select, mux_state_deselect); 865 } 866 EXPORT_SYMBOL_GPL(devm_mux_state_get_selected); 867 868 /** 869 * devm_mux_state_get_optional_selected() - Get the optional mux-state for 870 * a device, with resource management. 871 * @dev: The device that needs a mux-state. 872 * @mux_name: The name identifying the mux-state. 873 * 874 * Return: Pointer to the mux-state on success, an ERR_PTR with a negative 875 * errno on error, or NULL if mux doesn't exist. 876 * 877 * The returned mux-state (if valid) is already selected. 878 * 879 * The mux-state will automatically be deselected and freed on release. 880 */ 881 struct mux_state *devm_mux_state_get_optional_selected(struct device *dev, 882 const char *mux_name) 883 { 884 return __devm_mux_state_get(dev, mux_name, true, mux_state_select, mux_state_deselect); 885 } 886 EXPORT_SYMBOL_GPL(devm_mux_state_get_optional_selected); 887 888 /* 889 * Using subsys_initcall instead of module_init here to try to ensure - for 890 * the non-modular case - that the subsystem is initialized when mux consumers 891 * and mux controllers start to use it. 892 * For the modular case, the ordering is ensured with module dependencies. 893 */ 894 subsys_initcall(mux_init); 895 module_exit(mux_exit); 896 897 MODULE_DESCRIPTION("Multiplexer subsystem"); 898 MODULE_AUTHOR("Peter Rosin <peda@axentia.se>"); 899 MODULE_LICENSE("GPL v2"); 900