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