gpio.c 2.6 KB

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