gpio.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * GPIO-controlled multiplexer driver
  4. *
  5. * Copyright (C) 2017 Axentia Technologies AB
  6. *
  7. * Author: Peter Rosin <peda@axentia.se>
  8. */
  9. #include <linux/err.h>
  10. #include <linux/gpio/consumer.h>
  11. #include <linux/module.h>
  12. #include <linux/mux/driver.h>
  13. #include <linux/of_platform.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/property.h>
  16. struct mux_gpio {
  17. struct gpio_descs *gpios;
  18. };
  19. static int mux_gpio_set(struct mux_control *mux, int state)
  20. {
  21. struct mux_gpio *mux_gpio = mux_chip_priv(mux->chip);
  22. DECLARE_BITMAP(values, BITS_PER_TYPE(state));
  23. values[0] = state;
  24. gpiod_set_array_value_cansleep(mux_gpio->gpios->ndescs,
  25. mux_gpio->gpios->desc,
  26. mux_gpio->gpios->info, values);
  27. return 0;
  28. }
  29. static const struct mux_control_ops mux_gpio_ops = {
  30. .set = mux_gpio_set,
  31. };
  32. static const struct of_device_id mux_gpio_dt_ids[] = {
  33. { .compatible = "gpio-mux", },
  34. { /* sentinel */ }
  35. };
  36. MODULE_DEVICE_TABLE(of, mux_gpio_dt_ids);
  37. static int mux_gpio_probe(struct platform_device *pdev)
  38. {
  39. struct device *dev = &pdev->dev;
  40. struct mux_chip *mux_chip;
  41. struct mux_gpio *mux_gpio;
  42. int pins;
  43. s32 idle_state;
  44. int ret;
  45. pins = gpiod_count(dev, "mux");
  46. if (pins < 0)
  47. return pins;
  48. mux_chip = devm_mux_chip_alloc(dev, 1, sizeof(*mux_gpio));
  49. if (IS_ERR(mux_chip))
  50. return PTR_ERR(mux_chip);
  51. mux_gpio = mux_chip_priv(mux_chip);
  52. mux_chip->ops = &mux_gpio_ops;
  53. mux_gpio->gpios = devm_gpiod_get_array(dev, "mux", GPIOD_OUT_LOW);
  54. if (IS_ERR(mux_gpio->gpios)) {
  55. ret = PTR_ERR(mux_gpio->gpios);
  56. if (ret != -EPROBE_DEFER)
  57. dev_err(dev, "failed to get gpios\n");
  58. return ret;
  59. }
  60. WARN_ON(pins != mux_gpio->gpios->ndescs);
  61. mux_chip->mux->states = 1 << pins;
  62. ret = device_property_read_u32(dev, "idle-state", (u32 *)&idle_state);
  63. if (ret >= 0 && idle_state != MUX_IDLE_AS_IS) {
  64. if (idle_state < 0 || idle_state >= mux_chip->mux->states) {
  65. dev_err(dev, "invalid idle-state %u\n", idle_state);
  66. return -EINVAL;
  67. }
  68. mux_chip->mux->idle_state = idle_state;
  69. }
  70. ret = devm_mux_chip_register(dev, mux_chip);
  71. if (ret < 0)
  72. return ret;
  73. dev_info(dev, "%u-way mux-controller registered\n",
  74. mux_chip->mux->states);
  75. return 0;
  76. }
  77. static struct platform_driver mux_gpio_driver = {
  78. .driver = {
  79. .name = "gpio-mux",
  80. .of_match_table = of_match_ptr(mux_gpio_dt_ids),
  81. },
  82. .probe = mux_gpio_probe,
  83. };
  84. module_platform_driver(mux_gpio_driver);
  85. MODULE_DESCRIPTION("GPIO-controlled multiplexer driver");
  86. MODULE_AUTHOR("Peter Rosin <peda@axentia.se>");
  87. MODULE_LICENSE("GPL v2");