gpio.c 2.7 KB

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