gpio-pisosr.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com/
  3. * Andrew F. Davis <afd@ti.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  10. * kind, whether expressed or implied; without even the implied warranty
  11. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License version 2 for more details.
  13. */
  14. #include <linux/delay.h>
  15. #include <linux/gpio/consumer.h>
  16. #include <linux/gpio/driver.h>
  17. #include <linux/module.h>
  18. #include <linux/mutex.h>
  19. #include <linux/spi/spi.h>
  20. #define DEFAULT_NGPIO 8
  21. /**
  22. * struct pisosr_gpio - GPIO driver data
  23. * @chip: GPIO controller chip
  24. * @spi: SPI device pointer
  25. * @buffer: Buffer for device reads
  26. * @buffer_size: Size of buffer
  27. * @load_gpio: GPIO pin used to load input into device
  28. * @lock: Protects read sequences
  29. */
  30. struct pisosr_gpio {
  31. struct gpio_chip chip;
  32. struct spi_device *spi;
  33. u8 *buffer;
  34. size_t buffer_size;
  35. struct gpio_desc *load_gpio;
  36. struct mutex lock;
  37. };
  38. static int pisosr_gpio_refresh(struct pisosr_gpio *gpio)
  39. {
  40. int ret;
  41. mutex_lock(&gpio->lock);
  42. if (gpio->load_gpio) {
  43. gpiod_set_value_cansleep(gpio->load_gpio, 1);
  44. udelay(1); /* registers load time (~10ns) */
  45. gpiod_set_value_cansleep(gpio->load_gpio, 0);
  46. udelay(1); /* registers recovery time (~5ns) */
  47. }
  48. ret = spi_read(gpio->spi, gpio->buffer, gpio->buffer_size);
  49. mutex_unlock(&gpio->lock);
  50. return ret;
  51. }
  52. static int pisosr_gpio_get_direction(struct gpio_chip *chip,
  53. unsigned offset)
  54. {
  55. /* This device always input */
  56. return 1;
  57. }
  58. static int pisosr_gpio_direction_input(struct gpio_chip *chip,
  59. unsigned offset)
  60. {
  61. /* This device always input */
  62. return 0;
  63. }
  64. static int pisosr_gpio_direction_output(struct gpio_chip *chip,
  65. unsigned offset, int value)
  66. {
  67. /* This device is input only */
  68. return -EINVAL;
  69. }
  70. static int pisosr_gpio_get(struct gpio_chip *chip, unsigned offset)
  71. {
  72. struct pisosr_gpio *gpio = gpiochip_get_data(chip);
  73. /* Refresh may not always be needed */
  74. pisosr_gpio_refresh(gpio);
  75. return (gpio->buffer[offset / 8] >> (offset % 8)) & 0x1;
  76. }
  77. static const struct gpio_chip template_chip = {
  78. .label = "pisosr-gpio",
  79. .owner = THIS_MODULE,
  80. .get_direction = pisosr_gpio_get_direction,
  81. .direction_input = pisosr_gpio_direction_input,
  82. .direction_output = pisosr_gpio_direction_output,
  83. .get = pisosr_gpio_get,
  84. .base = -1,
  85. .ngpio = DEFAULT_NGPIO,
  86. .can_sleep = true,
  87. };
  88. static int pisosr_gpio_probe(struct spi_device *spi)
  89. {
  90. struct device *dev = &spi->dev;
  91. struct pisosr_gpio *gpio;
  92. int ret;
  93. gpio = devm_kzalloc(dev, sizeof(*gpio), GFP_KERNEL);
  94. if (!gpio)
  95. return -ENOMEM;
  96. spi_set_drvdata(spi, gpio);
  97. gpio->chip = template_chip;
  98. gpio->chip.parent = dev;
  99. of_property_read_u16(dev->of_node, "ngpios", &gpio->chip.ngpio);
  100. gpio->spi = spi;
  101. gpio->buffer_size = DIV_ROUND_UP(gpio->chip.ngpio, 8);
  102. gpio->buffer = devm_kzalloc(dev, gpio->buffer_size, GFP_KERNEL);
  103. if (!gpio->buffer)
  104. return -ENOMEM;
  105. gpio->load_gpio = devm_gpiod_get_optional(dev, "load", GPIOD_OUT_LOW);
  106. if (IS_ERR(gpio->load_gpio)) {
  107. ret = PTR_ERR(gpio->load_gpio);
  108. if (ret != -EPROBE_DEFER)
  109. dev_err(dev, "Unable to allocate load GPIO\n");
  110. return ret;
  111. }
  112. mutex_init(&gpio->lock);
  113. ret = gpiochip_add_data(&gpio->chip, gpio);
  114. if (ret < 0) {
  115. dev_err(dev, "Unable to register gpiochip\n");
  116. return ret;
  117. }
  118. return 0;
  119. }
  120. static int pisosr_gpio_remove(struct spi_device *spi)
  121. {
  122. struct pisosr_gpio *gpio = spi_get_drvdata(spi);
  123. gpiochip_remove(&gpio->chip);
  124. mutex_destroy(&gpio->lock);
  125. return 0;
  126. }
  127. static const struct spi_device_id pisosr_gpio_id_table[] = {
  128. { "pisosr-gpio", },
  129. { /* sentinel */ }
  130. };
  131. MODULE_DEVICE_TABLE(spi, pisosr_gpio_id_table);
  132. static const struct of_device_id pisosr_gpio_of_match_table[] = {
  133. { .compatible = "pisosr-gpio", },
  134. { /* sentinel */ }
  135. };
  136. MODULE_DEVICE_TABLE(of, pisosr_gpio_of_match_table);
  137. static struct spi_driver pisosr_gpio_driver = {
  138. .driver = {
  139. .name = "pisosr-gpio",
  140. .of_match_table = pisosr_gpio_of_match_table,
  141. },
  142. .probe = pisosr_gpio_probe,
  143. .remove = pisosr_gpio_remove,
  144. .id_table = pisosr_gpio_id_table,
  145. };
  146. module_spi_driver(pisosr_gpio_driver);
  147. MODULE_AUTHOR("Andrew F. Davis <afd@ti.com>");
  148. MODULE_DESCRIPTION("SPI Compatible PISO Shift Register GPIO Driver");
  149. MODULE_LICENSE("GPL v2");