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