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