gpio.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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, values);
  26. return 0;
  27. }
  28. static const struct mux_control_ops mux_gpio_ops = {
  29. .set = mux_gpio_set,
  30. };
  31. static const struct of_device_id mux_gpio_dt_ids[] = {
  32. { .compatible = "gpio-mux", },
  33. { /* sentinel */ }
  34. };
  35. MODULE_DEVICE_TABLE(of, mux_gpio_dt_ids);
  36. static int mux_gpio_probe(struct platform_device *pdev)
  37. {
  38. struct device *dev = &pdev->dev;
  39. struct mux_chip *mux_chip;
  40. struct mux_gpio *mux_gpio;
  41. int pins;
  42. s32 idle_state;
  43. int ret;
  44. pins = gpiod_count(dev, "mux");
  45. if (pins < 0)
  46. return pins;
  47. mux_chip = devm_mux_chip_alloc(dev, 1, sizeof(*mux_gpio));
  48. if (IS_ERR(mux_chip))
  49. return PTR_ERR(mux_chip);
  50. mux_gpio = mux_chip_priv(mux_chip);
  51. mux_chip->ops = &mux_gpio_ops;
  52. mux_gpio->gpios = devm_gpiod_get_array(dev, "mux", GPIOD_OUT_LOW);
  53. if (IS_ERR(mux_gpio->gpios)) {
  54. ret = PTR_ERR(mux_gpio->gpios);
  55. if (ret != -EPROBE_DEFER)
  56. dev_err(dev, "failed to get gpios\n");
  57. return ret;
  58. }
  59. WARN_ON(pins != mux_gpio->gpios->ndescs);
  60. mux_chip->mux->states = 1 << pins;
  61. ret = device_property_read_u32(dev, "idle-state", (u32 *)&idle_state);
  62. if (ret >= 0 && idle_state != MUX_IDLE_AS_IS) {
  63. if (idle_state < 0 || idle_state >= mux_chip->mux->states) {
  64. dev_err(dev, "invalid idle-state %u\n", idle_state);
  65. return -EINVAL;
  66. }
  67. mux_chip->mux->idle_state = idle_state;
  68. }
  69. ret = devm_mux_chip_register(dev, mux_chip);
  70. if (ret < 0)
  71. return ret;
  72. dev_info(dev, "%u-way mux-controller registered\n",
  73. mux_chip->mux->states);
  74. return 0;
  75. }
  76. static struct platform_driver mux_gpio_driver = {
  77. .driver = {
  78. .name = "gpio-mux",
  79. .of_match_table = of_match_ptr(mux_gpio_dt_ids),
  80. },
  81. .probe = mux_gpio_probe,
  82. };
  83. module_platform_driver(mux_gpio_driver);
  84. MODULE_DESCRIPTION("GPIO-controlled multiplexer driver");
  85. MODULE_AUTHOR("Peter Rosin <peda@axentia.se>");
  86. MODULE_LICENSE("GPL v2");