gpio.c 2.6 KB

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