1
2
3
4
5
6
7
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/of_platform.h>
23#include <linux/slab.h>
24
25
26
27
28
29
30#define MUX_CACHE_UNKNOWN MUX_IDLE_AS_IS
31
32static struct class mux_class = {
33 .name = "mux",
34 .owner = THIS_MODULE,
35};
36
37static DEFINE_IDA(mux_ida);
38
39static int __init mux_init(void)
40{
41 ida_init(&mux_ida);
42 return class_register(&mux_class);
43}
44
45static void __exit mux_exit(void)
46{
47 class_unregister(&mux_class);
48 ida_destroy(&mux_ida);
49}
50
51static void mux_chip_release(struct device *dev)
52{
53 struct mux_chip *mux_chip = to_mux_chip(dev);
54
55 ida_simple_remove(&mux_ida, mux_chip->id);
56 kfree(mux_chip);
57}
58
59static const struct device_type mux_type = {
60 .name = "mux-chip",
61 .release = mux_chip_release,
62};
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80struct mux_chip *mux_chip_alloc(struct device *dev,
81 unsigned int controllers, size_t sizeof_priv)
82{
83 struct mux_chip *mux_chip;
84 int i;
85
86 if (WARN_ON(!dev || !controllers))
87 return ERR_PTR(-EINVAL);
88
89 mux_chip = kzalloc(sizeof(*mux_chip) +
90 controllers * sizeof(*mux_chip->mux) +
91 sizeof_priv, GFP_KERNEL);
92 if (!mux_chip)
93 return ERR_PTR(-ENOMEM);
94
95 mux_chip->mux = (struct mux_control *)(mux_chip + 1);
96 mux_chip->dev.class = &mux_class;
97 mux_chip->dev.type = &mux_type;
98 mux_chip->dev.parent = dev;
99 mux_chip->dev.of_node = dev->of_node;
100 dev_set_drvdata(&mux_chip->dev, mux_chip);
101
102 mux_chip->id = ida_simple_get(&mux_ida, 0, 0, GFP_KERNEL);
103 if (mux_chip->id < 0) {
104 int err = mux_chip->id;
105
106 pr_err("muxchipX failed to get a device id\n");
107 kfree(mux_chip);
108 return ERR_PTR(err);
109 }
110 dev_set_name(&mux_chip->dev, "muxchip%d", mux_chip->id);
111
112 mux_chip->controllers = controllers;
113 for (i = 0; i < controllers; ++i) {
114 struct mux_control *mux = &mux_chip->mux[i];
115
116 mux->chip = mux_chip;
117 sema_init(&mux->lock, 1);
118 mux->cached_state = MUX_CACHE_UNKNOWN;
119 mux->idle_state = MUX_IDLE_AS_IS;
120 mux->last_change = ktime_get();
121 }
122
123 device_initialize(&mux_chip->dev);
124
125 return mux_chip;
126}
127EXPORT_SYMBOL_GPL(mux_chip_alloc);
128
129static int mux_control_set(struct mux_control *mux, int state)
130{
131 int ret = mux->chip->ops->set(mux, state);
132
133 mux->cached_state = ret < 0 ? MUX_CACHE_UNKNOWN : state;
134 if (ret >= 0)
135 mux->last_change = ktime_get();
136
137 return ret;
138}
139
140
141
142
143
144
145
146
147
148
149
150
151int mux_chip_register(struct mux_chip *mux_chip)
152{
153 int i;
154 int ret;
155
156 for (i = 0; i < mux_chip->controllers; ++i) {
157 struct mux_control *mux = &mux_chip->mux[i];
158
159 if (mux->idle_state == mux->cached_state)
160 continue;
161
162 ret = mux_control_set(mux, mux->idle_state);
163 if (ret < 0) {
164 dev_err(&mux_chip->dev, "unable to set idle state\n");
165 return ret;
166 }
167 }
168
169 ret = device_add(&mux_chip->dev);
170 if (ret < 0)
171 dev_err(&mux_chip->dev,
172 "device_add failed in %s: %d\n", __func__, ret);
173 return ret;
174}
175EXPORT_SYMBOL_GPL(mux_chip_register);
176
177
178
179
180
181
182
183
184
185void mux_chip_unregister(struct mux_chip *mux_chip)
186{
187 device_del(&mux_chip->dev);
188}
189EXPORT_SYMBOL_GPL(mux_chip_unregister);
190
191
192
193
194
195
196
197void mux_chip_free(struct mux_chip *mux_chip)
198{
199 if (!mux_chip)
200 return;
201
202 put_device(&mux_chip->dev);
203}
204EXPORT_SYMBOL_GPL(mux_chip_free);
205
206static void devm_mux_chip_release(struct device *dev, void *res)
207{
208 struct mux_chip *mux_chip = *(struct mux_chip **)res;
209
210 mux_chip_free(mux_chip);
211}
212
213
214
215
216
217
218
219
220
221
222
223struct mux_chip *devm_mux_chip_alloc(struct device *dev,
224 unsigned int controllers,
225 size_t sizeof_priv)
226{
227 struct mux_chip **ptr, *mux_chip;
228
229 ptr = devres_alloc(devm_mux_chip_release, sizeof(*ptr), GFP_KERNEL);
230 if (!ptr)
231 return ERR_PTR(-ENOMEM);
232
233 mux_chip = mux_chip_alloc(dev, controllers, sizeof_priv);
234 if (IS_ERR(mux_chip)) {
235 devres_free(ptr);
236 return mux_chip;
237 }
238
239 *ptr = mux_chip;
240 devres_add(dev, ptr);
241
242 return mux_chip;
243}
244EXPORT_SYMBOL_GPL(devm_mux_chip_alloc);
245
246static void devm_mux_chip_reg_release(struct device *dev, void *res)
247{
248 struct mux_chip *mux_chip = *(struct mux_chip **)res;
249
250 mux_chip_unregister(mux_chip);
251}
252
253
254
255
256
257
258
259
260
261
262int devm_mux_chip_register(struct device *dev,
263 struct mux_chip *mux_chip)
264{
265 struct mux_chip **ptr;
266 int res;
267
268 ptr = devres_alloc(devm_mux_chip_reg_release, sizeof(*ptr), GFP_KERNEL);
269 if (!ptr)
270 return -ENOMEM;
271
272 res = mux_chip_register(mux_chip);
273 if (res) {
274 devres_free(ptr);
275 return res;
276 }
277
278 *ptr = mux_chip;
279 devres_add(dev, ptr);
280
281 return res;
282}
283EXPORT_SYMBOL_GPL(devm_mux_chip_register);
284
285
286
287
288
289
290
291unsigned int mux_control_states(struct mux_control *mux)
292{
293 return mux->states;
294}
295EXPORT_SYMBOL_GPL(mux_control_states);
296
297
298
299
300static int __mux_control_select(struct mux_control *mux, int state)
301{
302 int ret;
303
304 if (WARN_ON(state < 0 || state >= mux->states))
305 return -EINVAL;
306
307 if (mux->cached_state == state)
308 return 0;
309
310 ret = mux_control_set(mux, state);
311 if (ret >= 0)
312 return 0;
313
314
315 if (mux->idle_state != MUX_IDLE_AS_IS)
316 mux_control_set(mux, mux->idle_state);
317
318 return ret;
319}
320
321static void mux_control_delay(struct mux_control *mux, unsigned int delay_us)
322{
323 ktime_t delayend;
324 s64 remaining;
325
326 if (!delay_us)
327 return;
328
329 delayend = ktime_add_us(mux->last_change, delay_us);
330 remaining = ktime_us_delta(delayend, ktime_get());
331 if (remaining > 0)
332 fsleep(remaining);
333}
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353int mux_control_select_delay(struct mux_control *mux, unsigned int state,
354 unsigned int delay_us)
355{
356 int ret;
357
358 ret = down_killable(&mux->lock);
359 if (ret < 0)
360 return ret;
361
362 ret = __mux_control_select(mux, state);
363 if (ret >= 0)
364 mux_control_delay(mux, delay_us);
365
366 if (ret < 0)
367 up(&mux->lock);
368
369 return ret;
370}
371EXPORT_SYMBOL_GPL(mux_control_select_delay);
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389int mux_control_try_select_delay(struct mux_control *mux, unsigned int state,
390 unsigned int delay_us)
391{
392 int ret;
393
394 if (down_trylock(&mux->lock))
395 return -EBUSY;
396
397 ret = __mux_control_select(mux, state);
398 if (ret >= 0)
399 mux_control_delay(mux, delay_us);
400
401 if (ret < 0)
402 up(&mux->lock);
403
404 return ret;
405}
406EXPORT_SYMBOL_GPL(mux_control_try_select_delay);
407
408
409
410
411
412
413
414
415
416
417
418
419
420int mux_control_deselect(struct mux_control *mux)
421{
422 int ret = 0;
423
424 if (mux->idle_state != MUX_IDLE_AS_IS &&
425 mux->idle_state != mux->cached_state)
426 ret = mux_control_set(mux, mux->idle_state);
427
428 up(&mux->lock);
429
430 return ret;
431}
432EXPORT_SYMBOL_GPL(mux_control_deselect);
433
434
435static struct mux_chip *of_find_mux_chip_by_node(struct device_node *np)
436{
437 struct device *dev;
438
439 dev = class_find_device_by_of_node(&mux_class, np);
440
441 return dev ? to_mux_chip(dev) : NULL;
442}
443
444
445
446
447
448
449
450
451struct mux_control *mux_control_get(struct device *dev, const char *mux_name)
452{
453 struct device_node *np = dev->of_node;
454 struct of_phandle_args args;
455 struct mux_chip *mux_chip;
456 unsigned int controller;
457 int index = 0;
458 int ret;
459
460 if (mux_name) {
461 index = of_property_match_string(np, "mux-control-names",
462 mux_name);
463 if (index < 0) {
464 dev_err(dev, "mux controller '%s' not found\n",
465 mux_name);
466 return ERR_PTR(index);
467 }
468 }
469
470 ret = of_parse_phandle_with_args(np,
471 "mux-controls", "#mux-control-cells",
472 index, &args);
473 if (ret) {
474 dev_err(dev, "%pOF: failed to get mux-control %s(%i)\n",
475 np, mux_name ?: "", index);
476 return ERR_PTR(ret);
477 }
478
479 mux_chip = of_find_mux_chip_by_node(args.np);
480 of_node_put(args.np);
481 if (!mux_chip)
482 return ERR_PTR(-EPROBE_DEFER);
483
484 if (args.args_count > 1 ||
485 (!args.args_count && (mux_chip->controllers > 1))) {
486 dev_err(dev, "%pOF: wrong #mux-control-cells for %pOF\n",
487 np, args.np);
488 put_device(&mux_chip->dev);
489 return ERR_PTR(-EINVAL);
490 }
491
492 controller = 0;
493 if (args.args_count)
494 controller = args.args[0];
495
496 if (controller >= mux_chip->controllers) {
497 dev_err(dev, "%pOF: bad mux controller %u specified in %pOF\n",
498 np, controller, args.np);
499 put_device(&mux_chip->dev);
500 return ERR_PTR(-EINVAL);
501 }
502
503 return &mux_chip->mux[controller];
504}
505EXPORT_SYMBOL_GPL(mux_control_get);
506
507
508
509
510
511
512
513void mux_control_put(struct mux_control *mux)
514{
515 put_device(&mux->chip->dev);
516}
517EXPORT_SYMBOL_GPL(mux_control_put);
518
519static void devm_mux_control_release(struct device *dev, void *res)
520{
521 struct mux_control *mux = *(struct mux_control **)res;
522
523 mux_control_put(mux);
524}
525
526
527
528
529
530
531
532
533
534struct mux_control *devm_mux_control_get(struct device *dev,
535 const char *mux_name)
536{
537 struct mux_control **ptr, *mux;
538
539 ptr = devres_alloc(devm_mux_control_release, sizeof(*ptr), GFP_KERNEL);
540 if (!ptr)
541 return ERR_PTR(-ENOMEM);
542
543 mux = mux_control_get(dev, mux_name);
544 if (IS_ERR(mux)) {
545 devres_free(ptr);
546 return mux;
547 }
548
549 *ptr = mux;
550 devres_add(dev, ptr);
551
552 return mux;
553}
554EXPORT_SYMBOL_GPL(devm_mux_control_get);
555
556
557
558
559
560
561
562subsys_initcall(mux_init);
563module_exit(mux_exit);
564
565MODULE_DESCRIPTION("Multiplexer subsystem");
566MODULE_AUTHOR("Peter Rosin <peda@axentia.se>");
567MODULE_LICENSE("GPL v2");
568