| 1 | |
| 2 | |
| 3 | * Multiplexer subsystem
|
| 4 | *
|
| 5 | * Copyright (C) 2017 Axentia Technologies AB
|
| 6 | *
|
| 7 | * Author: Peter Rosin <[email protected]>
|
| 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 |
|
| 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 |
|
| 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 | * @node: the device nodes, use dev->of_node if it is NULL.
|
| 537 | *
|
| 538 | * Return: Pointer to the mux-control on success, an ERR_PTR with a negative
|
| 539 | * errno on error, or NULL if optional is true and mux doesn't exist.
|
| 540 |
|
| 541 | static struct mux_control *mux_get(struct device *dev, const char *mux_name,
|
| 542 | unsigned int *state, bool optional,
|
| 543 | struct device_node *node)
|
| 544 | {
|
| 545 | struct device_node *np = node ? node : dev->of_node;
|
| 546 | struct of_phandle_args args;
|
| 547 | struct mux_chip *mux_chip;
|
| 548 | unsigned int controller;
|
| 549 | int index = 0;
|
| 550 | int ret;
|
| 551 |
|
| 552 | if (mux_name) {
|
| 553 | if (state)
|
| 554 | index = of_property_match_string(np, "mux-state-names",
|
| 555 | mux_name);
|
| 556 | else
|
| 557 | index = of_property_match_string(np, "mux-control-names",
|
| 558 | mux_name);
|
| 559 | if (index < 0 && optional) {
|
| 560 | return NULL;
|
| 561 | } else if (index < 0) {
|
| 562 | dev_err(dev, "mux controller '%s' not found\n",
|
| 563 | mux_name);
|
| 564 | return ERR_PTR(index);
|
| 565 | }
|
| 566 | }
|
| 567 |
|
| 568 | if (state)
|
| 569 | ret = of_parse_phandle_with_args(np,
|
| 570 | "mux-states", "#mux-state-cells",
|
| 571 | index, &args);
|
| 572 | else
|
| 573 | ret = of_parse_phandle_with_args(np,
|
| 574 | "mux-controls", "#mux-control-cells",
|
| 575 | index, &args);
|
| 576 | if (ret) {
|
| 577 | if (optional && ret == -ENOENT)
|
| 578 | return NULL;
|
| 579 |
|
| 580 | dev_err(dev, "%pOF: failed to get mux-%s %s(%i)\n",
|
| 581 | np, state ? "state" : "control",
|
| 582 | mux_name ?: "", index);
|
| 583 | return ERR_PTR(ret);
|
| 584 | }
|
| 585 |
|
| 586 | mux_chip = of_find_mux_chip_by_node(args.np);
|
| 587 | of_node_put(args.np);
|
| 588 | if (!mux_chip)
|
| 589 | return ERR_PTR(-EPROBE_DEFER);
|
| 590 |
|
| 591 | controller = 0;
|
| 592 | if (state) {
|
| 593 | if (args.args_count > 2 || args.args_count == 0 ||
|
| 594 | (args.args_count < 2 && mux_chip->controllers > 1)) {
|
| 595 | dev_err(dev, "%pOF: wrong #mux-state-cells for %pOF\n",
|
| 596 | np, args.np);
|
| 597 | put_device(&mux_chip->dev);
|
| 598 | return ERR_PTR(-EINVAL);
|
| 599 | }
|
| 600 |
|
| 601 | if (args.args_count == 2) {
|
| 602 | controller = args.args[0];
|
| 603 | *state = args.args[1];
|
| 604 | } else {
|
| 605 | *state = args.args[0];
|
| 606 | }
|
| 607 |
|
| 608 | } else {
|
| 609 | if (args.args_count > 1 ||
|
| 610 | (!args.args_count && mux_chip->controllers > 1)) {
|
| 611 | dev_err(dev, "%pOF: wrong #mux-control-cells for %pOF\n",
|
| 612 | np, args.np);
|
| 613 | put_device(&mux_chip->dev);
|
| 614 | return ERR_PTR(-EINVAL);
|
| 615 | }
|
| 616 |
|
| 617 | if (args.args_count)
|
| 618 | controller = args.args[0];
|
| 619 | }
|
| 620 |
|
| 621 | if (controller >= mux_chip->controllers) {
|
| 622 | dev_err(dev, "%pOF: bad mux controller %u specified in %pOF\n",
|
| 623 | np, controller, args.np);
|
| 624 | put_device(&mux_chip->dev);
|
| 625 | return ERR_PTR(-EINVAL);
|
| 626 | }
|
| 627 |
|
| 628 | return &mux_chip->mux[controller];
|
| 629 | }
|
| 630 |
|
| 631 | |
| 632 | * mux_control_get() - Get the mux-control for a device.
|
| 633 | * @dev: The device that needs a mux-control.
|
| 634 | * @mux_name: The name identifying the mux-control.
|
| 635 | *
|
| 636 | * Return: A pointer to the mux-control, or an ERR_PTR with a negative errno.
|
| 637 |
|
| 638 | struct mux_control *mux_control_get(struct device *dev, const char *mux_name)
|
| 639 | {
|
| 640 | struct mux_control *mux = mux_get(dev, mux_name, NULL, false, NULL);
|
| 641 |
|
| 642 | if (!mux)
|
| 643 | return ERR_PTR(-ENOENT);
|
| 644 |
|
| 645 | return mux;
|
| 646 | }
|
| 647 | EXPORT_SYMBOL_GPL(mux_control_get);
|
| 648 |
|
| 649 | |
| 650 | * mux_control_get_optional() - Get the optional mux-control for a device.
|
| 651 | * @dev: The device that needs a mux-control.
|
| 652 | * @mux_name: The name identifying the mux-control.
|
| 653 | *
|
| 654 | * Return: Pointer to the mux-control on success, an ERR_PTR with a negative
|
| 655 | * errno on error, or NULL if mux doesn't exist.
|
| 656 |
|
| 657 | struct mux_control *mux_control_get_optional(struct device *dev, const char *mux_name)
|
| 658 | {
|
| 659 | return mux_get(dev, mux_name, NULL, true, NULL);
|
| 660 | }
|
| 661 | EXPORT_SYMBOL_GPL(mux_control_get_optional);
|
| 662 |
|
| 663 | |
| 664 | * mux_control_put() - Put away the mux-control for good.
|
| 665 | * @mux: The mux-control to put away.
|
| 666 | *
|
| 667 | * mux_control_put() reverses the effects of mux_control_get().
|
| 668 |
|
| 669 | void mux_control_put(struct mux_control *mux)
|
| 670 | {
|
| 671 | put_device(&mux->chip->dev);
|
| 672 | }
|
| 673 | EXPORT_SYMBOL_GPL(mux_control_put);
|
| 674 |
|
| 675 | static void devm_mux_control_release(struct device *dev, void *res)
|
| 676 | {
|
| 677 | struct mux_control *mux = *(struct mux_control **)res;
|
| 678 |
|
| 679 | mux_control_put(mux);
|
| 680 | }
|
| 681 |
|
| 682 | |
| 683 | * devm_mux_control_get() - Get the mux-control for a device, with resource
|
| 684 | * management.
|
| 685 | * @dev: The device that needs a mux-control.
|
| 686 | * @mux_name: The name identifying the mux-control.
|
| 687 | *
|
| 688 | * Return: Pointer to the mux-control, or an ERR_PTR with a negative errno.
|
| 689 |
|
| 690 | struct mux_control *devm_mux_control_get(struct device *dev,
|
| 691 | const char *mux_name)
|
| 692 | {
|
| 693 | struct mux_control **ptr, *mux;
|
| 694 |
|
| 695 | ptr = devres_alloc(devm_mux_control_release, sizeof(*ptr), GFP_KERNEL);
|
| 696 | if (!ptr)
|
| 697 | return ERR_PTR(-ENOMEM);
|
| 698 |
|
| 699 | mux = mux_control_get(dev, mux_name);
|
| 700 | if (IS_ERR(mux)) {
|
| 701 | devres_free(ptr);
|
| 702 | return mux;
|
| 703 | }
|
| 704 |
|
| 705 | *ptr = mux;
|
| 706 | devres_add(dev, ptr);
|
| 707 |
|
| 708 | return mux;
|
| 709 | }
|
| 710 | EXPORT_SYMBOL_GPL(devm_mux_control_get);
|
| 711 |
|
| 712 | |
| 713 | * mux_state_get() - Get the mux-state for a device.
|
| 714 | * @dev: The device that needs a mux-state.
|
| 715 | * @mux_name: The name identifying the mux-state.
|
| 716 | * @optional: Whether to return NULL and silence errors when mux doesn't exist.
|
| 717 | * @np: the device nodes, use dev->of_node if it is NULL.
|
| 718 | *
|
| 719 | * Return: Pointer to the mux-state on success, an ERR_PTR with a negative
|
| 720 | * errno on error, or NULL if optional is true and mux doesn't exist.
|
| 721 |
|
| 722 | static struct mux_state *
|
| 723 | mux_state_get(struct device *dev, const char *mux_name, bool optional,
|
| 724 | struct device_node *np)
|
| 725 | {
|
| 726 | struct mux_state *mstate;
|
| 727 |
|
| 728 | mstate = kzalloc_obj(*mstate);
|
| 729 | if (!mstate)
|
| 730 | return ERR_PTR(-ENOMEM);
|
| 731 |
|
| 732 | mstate->mux = mux_get(dev, mux_name, &mstate->state, optional, np);
|
| 733 | if (IS_ERR(mstate->mux)) {
|
| 734 | int err = PTR_ERR(mstate->mux);
|
| 735 |
|
| 736 | kfree(mstate);
|
| 737 | return ERR_PTR(err);
|
| 738 | } else if (!mstate->mux) {
|
| 739 | kfree(mstate);
|
| 740 | return optional ? NULL : ERR_PTR(-ENOENT);
|
| 741 | }
|
| 742 |
|
| 743 | return mstate;
|
| 744 | }
|
| 745 |
|
| 746 | |
| 747 | * mux_state_put() - Put away the mux-state for good.
|
| 748 | * @mstate: The mux-state to put away.
|
| 749 | *
|
| 750 | * mux_state_put() reverses the effects of mux_state_get().
|
| 751 |
|
| 752 | static void mux_state_put(struct mux_state *mstate)
|
| 753 | {
|
| 754 | mux_control_put(mstate->mux);
|
| 755 | kfree(mstate);
|
| 756 | }
|
| 757 |
|
| 758 | static void devm_mux_state_release(struct device *dev, void *res)
|
| 759 | {
|
| 760 | struct devm_mux_state_state *devm_state = res;
|
| 761 |
|
| 762 | if (devm_state->exit)
|
| 763 | devm_state->exit(devm_state->mstate);
|
| 764 |
|
| 765 | mux_state_put(devm_state->mstate);
|
| 766 | }
|
| 767 |
|
| 768 | |
| 769 | * __devm_mux_state_get() - Get the optional mux-state for a device,
|
| 770 | * with resource management.
|
| 771 | * @dev: The device that needs a mux-state.
|
| 772 | * @mux_name: The name identifying the mux-state.
|
| 773 | * @optional: Whether to return NULL and silence errors when mux doesn't exist.
|
| 774 | * @np: The device nodes, use dev->of_node if it is NULL.
|
| 775 | * @init: Optional function pointer for mux-state object initialisation.
|
| 776 | * @exit: Optional function pointer for mux-state object cleanup on release.
|
| 777 | *
|
| 778 | * Return: Pointer to the mux-state on success, an ERR_PTR with a negative
|
| 779 | * errno on error, or NULL if optional is true and mux doesn't exist.
|
| 780 |
|
| 781 | static struct mux_state *__devm_mux_state_get(struct device *dev, const char *mux_name,
|
| 782 | bool optional, struct device_node *np,
|
| 783 | int (*init)(struct mux_state *mstate),
|
| 784 | int (*exit)(struct mux_state *mstate))
|
| 785 | {
|
| 786 | struct devm_mux_state_state *devm_state;
|
| 787 | struct mux_state *mstate;
|
| 788 | int ret;
|
| 789 |
|
| 790 | mstate = mux_state_get(dev, mux_name, optional, np);
|
| 791 | if (IS_ERR(mstate))
|
| 792 | return ERR_CAST(mstate);
|
| 793 | else if (optional && !mstate)
|
| 794 | return NULL;
|
| 795 | else if (!mstate)
|
| 796 | return ERR_PTR(-ENOENT);
|
| 797 |
|
| 798 | devm_state = devres_alloc(devm_mux_state_release, sizeof(*devm_state), GFP_KERNEL);
|
| 799 | if (!devm_state) {
|
| 800 | ret = -ENOMEM;
|
| 801 | goto err_devres_alloc;
|
| 802 | }
|
| 803 |
|
| 804 | if (init) {
|
| 805 | ret = init(mstate);
|
| 806 | if (ret)
|
| 807 | goto err_mux_state_init;
|
| 808 | }
|
| 809 |
|
| 810 | devm_state->mstate = mstate;
|
| 811 | devm_state->exit = exit;
|
| 812 | devres_add(dev, devm_state);
|
| 813 |
|
| 814 | return mstate;
|
| 815 |
|
| 816 | err_mux_state_init:
|
| 817 | devres_free(devm_state);
|
| 818 | err_devres_alloc:
|
| 819 | mux_state_put(mstate);
|
| 820 | return ERR_PTR(ret);
|
| 821 | }
|
| 822 |
|
| 823 | |
| 824 | * devm_mux_state_get_from_np() - Get the mux-state for a device, with resource
|
| 825 | * management.
|
| 826 | * @dev: The device that needs a mux-control.
|
| 827 | * @mux_name: The name identifying the mux-control.
|
| 828 | * @np: the device nodes, use dev->of_node if it is NULL.
|
| 829 | *
|
| 830 | * Return: Pointer to the mux-state, or an ERR_PTR with a negative errno.
|
| 831 | *
|
| 832 | * The mux-state will automatically be freed on release.
|
| 833 |
|
| 834 | struct mux_state *
|
| 835 | devm_mux_state_get_from_np(struct device *dev, const char *mux_name,
|
| 836 | struct device_node *np)
|
| 837 | {
|
| 838 | return __devm_mux_state_get(dev, mux_name, false, np, NULL, NULL);
|
| 839 | }
|
| 840 | EXPORT_SYMBOL_GPL(devm_mux_state_get_from_np);
|
| 841 |
|
| 842 | |
| 843 | * devm_mux_state_get_optional() - Get the optional mux-state for a device,
|
| 844 | * with resource management.
|
| 845 | * @dev: The device that needs a mux-state.
|
| 846 | * @mux_name: The name identifying the mux-state.
|
| 847 | *
|
| 848 | * Return: Pointer to the mux-state on success, an ERR_PTR with a negative
|
| 849 | * errno on error, or NULL if mux doesn't exist.
|
| 850 | *
|
| 851 | * The mux-state will automatically be freed on release.
|
| 852 |
|
| 853 | struct mux_state *devm_mux_state_get_optional(struct device *dev, const char *mux_name)
|
| 854 | {
|
| 855 | return __devm_mux_state_get(dev, mux_name, true, NULL, NULL, NULL);
|
| 856 | }
|
| 857 | EXPORT_SYMBOL_GPL(devm_mux_state_get_optional);
|
| 858 |
|
| 859 | |
| 860 | * devm_mux_state_get_selected() - Get the mux-state for a device, with
|
| 861 | * resource management.
|
| 862 | * @dev: The device that needs a mux-state.
|
| 863 | * @mux_name: The name identifying the mux-state.
|
| 864 | *
|
| 865 | * Return: Pointer to the mux-state, or an ERR_PTR with a negative errno.
|
| 866 | *
|
| 867 | * The returned mux-state (if valid) is already selected.
|
| 868 | *
|
| 869 | * The mux-state will automatically be deselected and freed on release.
|
| 870 |
|
| 871 | struct mux_state *devm_mux_state_get_selected(struct device *dev, const char *mux_name)
|
| 872 | {
|
| 873 | return __devm_mux_state_get(dev, mux_name, false, NULL,
|
| 874 | mux_state_select, mux_state_deselect);
|
| 875 | }
|
| 876 | EXPORT_SYMBOL_GPL(devm_mux_state_get_selected);
|
| 877 |
|
| 878 | |
| 879 | * devm_mux_state_get_optional_selected() - Get the optional mux-state for
|
| 880 | * a device, with resource management.
|
| 881 | * @dev: The device that needs a mux-state.
|
| 882 | * @mux_name: The name identifying the mux-state.
|
| 883 | *
|
| 884 | * Return: Pointer to the mux-state on success, an ERR_PTR with a negative
|
| 885 | * errno on error, or NULL if mux doesn't exist.
|
| 886 | *
|
| 887 | * The returned mux-state (if valid) is already selected.
|
| 888 | *
|
| 889 | * The mux-state will automatically be deselected and freed on release.
|
| 890 |
|
| 891 | struct mux_state *devm_mux_state_get_optional_selected(struct device *dev,
|
| 892 | const char *mux_name)
|
| 893 | {
|
| 894 | return __devm_mux_state_get(dev, mux_name, true, NULL,
|
| 895 | mux_state_select, mux_state_deselect);
|
| 896 | }
|
| 897 | EXPORT_SYMBOL_GPL(devm_mux_state_get_optional_selected);
|
| 898 |
|
| 899 | |
| 900 | * Using subsys_initcall instead of module_init here to try to ensure - for
|
| 901 | * the non-modular case - that the subsystem is initialized when mux consumers
|
| 902 | * and mux controllers start to use it.
|
| 903 | * For the modular case, the ordering is ensured with module dependencies.
|
| 904 |
|
| 905 | subsys_initcall(mux_init);
|
| 906 | module_exit(mux_exit);
|
| 907 |
|
| 908 | MODULE_DESCRIPTION("Multiplexer subsystem");
|
| 909 | MODULE_AUTHOR("Peter Rosin <[email protected]>");
|
| 910 | MODULE_LICENSE("GPL v2");
|