0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #define pr_fmt(fmt) "mux-core: " fmt
0011
0012 #include <linux/delay.h>
0013 #include <linux/device.h>
0014 #include <linux/err.h>
0015 #include <linux/export.h>
0016 #include <linux/idr.h>
0017 #include <linux/init.h>
0018 #include <linux/module.h>
0019 #include <linux/mux/consumer.h>
0020 #include <linux/mux/driver.h>
0021 #include <linux/of.h>
0022 #include <linux/of_platform.h>
0023 #include <linux/slab.h>
0024
0025
0026
0027
0028
0029
0030 #define MUX_CACHE_UNKNOWN MUX_IDLE_AS_IS
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041 struct mux_state {
0042 struct mux_control *mux;
0043 unsigned int state;
0044 };
0045
0046 static struct class mux_class = {
0047 .name = "mux",
0048 .owner = THIS_MODULE,
0049 };
0050
0051 static DEFINE_IDA(mux_ida);
0052
0053 static int __init mux_init(void)
0054 {
0055 ida_init(&mux_ida);
0056 return class_register(&mux_class);
0057 }
0058
0059 static void __exit mux_exit(void)
0060 {
0061 class_unregister(&mux_class);
0062 ida_destroy(&mux_ida);
0063 }
0064
0065 static void mux_chip_release(struct device *dev)
0066 {
0067 struct mux_chip *mux_chip = to_mux_chip(dev);
0068
0069 ida_simple_remove(&mux_ida, mux_chip->id);
0070 kfree(mux_chip);
0071 }
0072
0073 static const struct device_type mux_type = {
0074 .name = "mux-chip",
0075 .release = mux_chip_release,
0076 };
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094 struct mux_chip *mux_chip_alloc(struct device *dev,
0095 unsigned int controllers, size_t sizeof_priv)
0096 {
0097 struct mux_chip *mux_chip;
0098 int i;
0099
0100 if (WARN_ON(!dev || !controllers))
0101 return ERR_PTR(-EINVAL);
0102
0103 mux_chip = kzalloc(sizeof(*mux_chip) +
0104 controllers * sizeof(*mux_chip->mux) +
0105 sizeof_priv, GFP_KERNEL);
0106 if (!mux_chip)
0107 return ERR_PTR(-ENOMEM);
0108
0109 mux_chip->mux = (struct mux_control *)(mux_chip + 1);
0110 mux_chip->dev.class = &mux_class;
0111 mux_chip->dev.type = &mux_type;
0112 mux_chip->dev.parent = dev;
0113 mux_chip->dev.of_node = dev->of_node;
0114 dev_set_drvdata(&mux_chip->dev, mux_chip);
0115
0116 mux_chip->id = ida_simple_get(&mux_ida, 0, 0, GFP_KERNEL);
0117 if (mux_chip->id < 0) {
0118 int err = mux_chip->id;
0119
0120 pr_err("muxchipX failed to get a device id\n");
0121 kfree(mux_chip);
0122 return ERR_PTR(err);
0123 }
0124 dev_set_name(&mux_chip->dev, "muxchip%d", mux_chip->id);
0125
0126 mux_chip->controllers = controllers;
0127 for (i = 0; i < controllers; ++i) {
0128 struct mux_control *mux = &mux_chip->mux[i];
0129
0130 mux->chip = mux_chip;
0131 sema_init(&mux->lock, 1);
0132 mux->cached_state = MUX_CACHE_UNKNOWN;
0133 mux->idle_state = MUX_IDLE_AS_IS;
0134 mux->last_change = ktime_get();
0135 }
0136
0137 device_initialize(&mux_chip->dev);
0138
0139 return mux_chip;
0140 }
0141 EXPORT_SYMBOL_GPL(mux_chip_alloc);
0142
0143 static int mux_control_set(struct mux_control *mux, int state)
0144 {
0145 int ret = mux->chip->ops->set(mux, state);
0146
0147 mux->cached_state = ret < 0 ? MUX_CACHE_UNKNOWN : state;
0148 if (ret >= 0)
0149 mux->last_change = ktime_get();
0150
0151 return ret;
0152 }
0153
0154
0155
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165 int mux_chip_register(struct mux_chip *mux_chip)
0166 {
0167 int i;
0168 int ret;
0169
0170 for (i = 0; i < mux_chip->controllers; ++i) {
0171 struct mux_control *mux = &mux_chip->mux[i];
0172
0173 if (mux->idle_state == mux->cached_state)
0174 continue;
0175
0176 ret = mux_control_set(mux, mux->idle_state);
0177 if (ret < 0) {
0178 dev_err(&mux_chip->dev, "unable to set idle state\n");
0179 return ret;
0180 }
0181 }
0182
0183 ret = device_add(&mux_chip->dev);
0184 if (ret < 0)
0185 dev_err(&mux_chip->dev,
0186 "device_add failed in %s: %d\n", __func__, ret);
0187 return ret;
0188 }
0189 EXPORT_SYMBOL_GPL(mux_chip_register);
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199 void mux_chip_unregister(struct mux_chip *mux_chip)
0200 {
0201 device_del(&mux_chip->dev);
0202 }
0203 EXPORT_SYMBOL_GPL(mux_chip_unregister);
0204
0205
0206
0207
0208
0209
0210
0211 void mux_chip_free(struct mux_chip *mux_chip)
0212 {
0213 if (!mux_chip)
0214 return;
0215
0216 put_device(&mux_chip->dev);
0217 }
0218 EXPORT_SYMBOL_GPL(mux_chip_free);
0219
0220 static void devm_mux_chip_release(struct device *dev, void *res)
0221 {
0222 struct mux_chip *mux_chip = *(struct mux_chip **)res;
0223
0224 mux_chip_free(mux_chip);
0225 }
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237 struct mux_chip *devm_mux_chip_alloc(struct device *dev,
0238 unsigned int controllers,
0239 size_t sizeof_priv)
0240 {
0241 struct mux_chip **ptr, *mux_chip;
0242
0243 ptr = devres_alloc(devm_mux_chip_release, sizeof(*ptr), GFP_KERNEL);
0244 if (!ptr)
0245 return ERR_PTR(-ENOMEM);
0246
0247 mux_chip = mux_chip_alloc(dev, controllers, sizeof_priv);
0248 if (IS_ERR(mux_chip)) {
0249 devres_free(ptr);
0250 return mux_chip;
0251 }
0252
0253 *ptr = mux_chip;
0254 devres_add(dev, ptr);
0255
0256 return mux_chip;
0257 }
0258 EXPORT_SYMBOL_GPL(devm_mux_chip_alloc);
0259
0260 static void devm_mux_chip_reg_release(struct device *dev, void *res)
0261 {
0262 struct mux_chip *mux_chip = *(struct mux_chip **)res;
0263
0264 mux_chip_unregister(mux_chip);
0265 }
0266
0267
0268
0269
0270
0271
0272
0273
0274
0275
0276 int devm_mux_chip_register(struct device *dev,
0277 struct mux_chip *mux_chip)
0278 {
0279 struct mux_chip **ptr;
0280 int res;
0281
0282 ptr = devres_alloc(devm_mux_chip_reg_release, sizeof(*ptr), GFP_KERNEL);
0283 if (!ptr)
0284 return -ENOMEM;
0285
0286 res = mux_chip_register(mux_chip);
0287 if (res) {
0288 devres_free(ptr);
0289 return res;
0290 }
0291
0292 *ptr = mux_chip;
0293 devres_add(dev, ptr);
0294
0295 return res;
0296 }
0297 EXPORT_SYMBOL_GPL(devm_mux_chip_register);
0298
0299
0300
0301
0302
0303
0304
0305 unsigned int mux_control_states(struct mux_control *mux)
0306 {
0307 return mux->states;
0308 }
0309 EXPORT_SYMBOL_GPL(mux_control_states);
0310
0311
0312
0313
0314 static int __mux_control_select(struct mux_control *mux, int state)
0315 {
0316 int ret;
0317
0318 if (WARN_ON(state < 0 || state >= mux->states))
0319 return -EINVAL;
0320
0321 if (mux->cached_state == state)
0322 return 0;
0323
0324 ret = mux_control_set(mux, state);
0325 if (ret >= 0)
0326 return 0;
0327
0328
0329 if (mux->idle_state != MUX_IDLE_AS_IS)
0330 mux_control_set(mux, mux->idle_state);
0331
0332 return ret;
0333 }
0334
0335 static void mux_control_delay(struct mux_control *mux, unsigned int delay_us)
0336 {
0337 ktime_t delayend;
0338 s64 remaining;
0339
0340 if (!delay_us)
0341 return;
0342
0343 delayend = ktime_add_us(mux->last_change, delay_us);
0344 remaining = ktime_us_delta(delayend, ktime_get());
0345 if (remaining > 0)
0346 fsleep(remaining);
0347 }
0348
0349
0350
0351
0352
0353
0354
0355
0356
0357
0358
0359
0360
0361
0362
0363
0364
0365
0366
0367
0368 int mux_control_select_delay(struct mux_control *mux, unsigned int state,
0369 unsigned int delay_us)
0370 {
0371 int ret;
0372
0373 ret = down_killable(&mux->lock);
0374 if (ret < 0)
0375 return ret;
0376
0377 ret = __mux_control_select(mux, state);
0378 if (ret >= 0)
0379 mux_control_delay(mux, delay_us);
0380
0381 if (ret < 0)
0382 up(&mux->lock);
0383
0384 return ret;
0385 }
0386 EXPORT_SYMBOL_GPL(mux_control_select_delay);
0387
0388
0389
0390
0391
0392
0393
0394
0395
0396
0397
0398
0399
0400
0401
0402
0403
0404
0405
0406 int mux_state_select_delay(struct mux_state *mstate, unsigned int delay_us)
0407 {
0408 return mux_control_select_delay(mstate->mux, mstate->state, delay_us);
0409 }
0410 EXPORT_SYMBOL_GPL(mux_state_select_delay);
0411
0412
0413
0414
0415
0416
0417
0418
0419
0420
0421
0422
0423
0424
0425
0426
0427
0428 int mux_control_try_select_delay(struct mux_control *mux, unsigned int state,
0429 unsigned int delay_us)
0430 {
0431 int ret;
0432
0433 if (down_trylock(&mux->lock))
0434 return -EBUSY;
0435
0436 ret = __mux_control_select(mux, state);
0437 if (ret >= 0)
0438 mux_control_delay(mux, delay_us);
0439
0440 if (ret < 0)
0441 up(&mux->lock);
0442
0443 return ret;
0444 }
0445 EXPORT_SYMBOL_GPL(mux_control_try_select_delay);
0446
0447
0448
0449
0450
0451
0452
0453
0454
0455
0456
0457
0458
0459
0460
0461
0462 int mux_state_try_select_delay(struct mux_state *mstate, unsigned int delay_us)
0463 {
0464 return mux_control_try_select_delay(mstate->mux, mstate->state, delay_us);
0465 }
0466 EXPORT_SYMBOL_GPL(mux_state_try_select_delay);
0467
0468
0469
0470
0471
0472
0473
0474
0475
0476
0477
0478
0479
0480 int mux_control_deselect(struct mux_control *mux)
0481 {
0482 int ret = 0;
0483
0484 if (mux->idle_state != MUX_IDLE_AS_IS &&
0485 mux->idle_state != mux->cached_state)
0486 ret = mux_control_set(mux, mux->idle_state);
0487
0488 up(&mux->lock);
0489
0490 return ret;
0491 }
0492 EXPORT_SYMBOL_GPL(mux_control_deselect);
0493
0494
0495
0496
0497
0498
0499
0500
0501
0502
0503
0504
0505
0506 int mux_state_deselect(struct mux_state *mstate)
0507 {
0508 return mux_control_deselect(mstate->mux);
0509 }
0510 EXPORT_SYMBOL_GPL(mux_state_deselect);
0511
0512
0513 static struct mux_chip *of_find_mux_chip_by_node(struct device_node *np)
0514 {
0515 struct device *dev;
0516
0517 dev = class_find_device_by_of_node(&mux_class, np);
0518
0519 return dev ? to_mux_chip(dev) : NULL;
0520 }
0521
0522
0523
0524
0525
0526
0527
0528
0529
0530
0531 static struct mux_control *mux_get(struct device *dev, const char *mux_name,
0532 unsigned int *state)
0533 {
0534 struct device_node *np = dev->of_node;
0535 struct of_phandle_args args;
0536 struct mux_chip *mux_chip;
0537 unsigned int controller;
0538 int index = 0;
0539 int ret;
0540
0541 if (mux_name) {
0542 if (state)
0543 index = of_property_match_string(np, "mux-state-names",
0544 mux_name);
0545 else
0546 index = of_property_match_string(np, "mux-control-names",
0547 mux_name);
0548 if (index < 0) {
0549 dev_err(dev, "mux controller '%s' not found\n",
0550 mux_name);
0551 return ERR_PTR(index);
0552 }
0553 }
0554
0555 if (state)
0556 ret = of_parse_phandle_with_args(np,
0557 "mux-states", "#mux-state-cells",
0558 index, &args);
0559 else
0560 ret = of_parse_phandle_with_args(np,
0561 "mux-controls", "#mux-control-cells",
0562 index, &args);
0563 if (ret) {
0564 dev_err(dev, "%pOF: failed to get mux-%s %s(%i)\n",
0565 np, state ? "state" : "control", mux_name ?: "", index);
0566 return ERR_PTR(ret);
0567 }
0568
0569 mux_chip = of_find_mux_chip_by_node(args.np);
0570 of_node_put(args.np);
0571 if (!mux_chip)
0572 return ERR_PTR(-EPROBE_DEFER);
0573
0574 controller = 0;
0575 if (state) {
0576 if (args.args_count > 2 || args.args_count == 0 ||
0577 (args.args_count < 2 && mux_chip->controllers > 1)) {
0578 dev_err(dev, "%pOF: wrong #mux-state-cells for %pOF\n",
0579 np, args.np);
0580 put_device(&mux_chip->dev);
0581 return ERR_PTR(-EINVAL);
0582 }
0583
0584 if (args.args_count == 2) {
0585 controller = args.args[0];
0586 *state = args.args[1];
0587 } else {
0588 *state = args.args[0];
0589 }
0590
0591 } else {
0592 if (args.args_count > 1 ||
0593 (!args.args_count && mux_chip->controllers > 1)) {
0594 dev_err(dev, "%pOF: wrong #mux-control-cells for %pOF\n",
0595 np, args.np);
0596 put_device(&mux_chip->dev);
0597 return ERR_PTR(-EINVAL);
0598 }
0599
0600 if (args.args_count)
0601 controller = args.args[0];
0602 }
0603
0604 if (controller >= mux_chip->controllers) {
0605 dev_err(dev, "%pOF: bad mux controller %u specified in %pOF\n",
0606 np, controller, args.np);
0607 put_device(&mux_chip->dev);
0608 return ERR_PTR(-EINVAL);
0609 }
0610
0611 return &mux_chip->mux[controller];
0612 }
0613
0614
0615
0616
0617
0618
0619
0620
0621 struct mux_control *mux_control_get(struct device *dev, const char *mux_name)
0622 {
0623 return mux_get(dev, mux_name, NULL);
0624 }
0625 EXPORT_SYMBOL_GPL(mux_control_get);
0626
0627
0628
0629
0630
0631
0632
0633 void mux_control_put(struct mux_control *mux)
0634 {
0635 put_device(&mux->chip->dev);
0636 }
0637 EXPORT_SYMBOL_GPL(mux_control_put);
0638
0639 static void devm_mux_control_release(struct device *dev, void *res)
0640 {
0641 struct mux_control *mux = *(struct mux_control **)res;
0642
0643 mux_control_put(mux);
0644 }
0645
0646
0647
0648
0649
0650
0651
0652
0653
0654 struct mux_control *devm_mux_control_get(struct device *dev,
0655 const char *mux_name)
0656 {
0657 struct mux_control **ptr, *mux;
0658
0659 ptr = devres_alloc(devm_mux_control_release, sizeof(*ptr), GFP_KERNEL);
0660 if (!ptr)
0661 return ERR_PTR(-ENOMEM);
0662
0663 mux = mux_control_get(dev, mux_name);
0664 if (IS_ERR(mux)) {
0665 devres_free(ptr);
0666 return mux;
0667 }
0668
0669 *ptr = mux;
0670 devres_add(dev, ptr);
0671
0672 return mux;
0673 }
0674 EXPORT_SYMBOL_GPL(devm_mux_control_get);
0675
0676
0677
0678
0679
0680
0681
0682
0683 static struct mux_state *mux_state_get(struct device *dev, const char *mux_name)
0684 {
0685 struct mux_state *mstate;
0686
0687 mstate = kzalloc(sizeof(*mstate), GFP_KERNEL);
0688 if (!mstate)
0689 return ERR_PTR(-ENOMEM);
0690
0691 mstate->mux = mux_get(dev, mux_name, &mstate->state);
0692 if (IS_ERR(mstate->mux)) {
0693 int err = PTR_ERR(mstate->mux);
0694
0695 kfree(mstate);
0696 return ERR_PTR(err);
0697 }
0698
0699 return mstate;
0700 }
0701
0702
0703
0704
0705
0706
0707
0708 static void mux_state_put(struct mux_state *mstate)
0709 {
0710 mux_control_put(mstate->mux);
0711 kfree(mstate);
0712 }
0713
0714 static void devm_mux_state_release(struct device *dev, void *res)
0715 {
0716 struct mux_state *mstate = *(struct mux_state **)res;
0717
0718 mux_state_put(mstate);
0719 }
0720
0721
0722
0723
0724
0725
0726
0727
0728
0729 struct mux_state *devm_mux_state_get(struct device *dev,
0730 const char *mux_name)
0731 {
0732 struct mux_state **ptr, *mstate;
0733
0734 ptr = devres_alloc(devm_mux_state_release, sizeof(*ptr), GFP_KERNEL);
0735 if (!ptr)
0736 return ERR_PTR(-ENOMEM);
0737
0738 mstate = mux_state_get(dev, mux_name);
0739 if (IS_ERR(mstate)) {
0740 devres_free(ptr);
0741 return mstate;
0742 }
0743
0744 *ptr = mstate;
0745 devres_add(dev, ptr);
0746
0747 return mstate;
0748 }
0749 EXPORT_SYMBOL_GPL(devm_mux_state_get);
0750
0751
0752
0753
0754
0755
0756
0757 subsys_initcall(mux_init);
0758 module_exit(mux_exit);
0759
0760 MODULE_DESCRIPTION("Multiplexer subsystem");
0761 MODULE_AUTHOR("Peter Rosin <peda@axentia.se>");
0762 MODULE_LICENSE("GPL v2");