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