gpio-pisosr.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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/bitmap.h>
  15. #include <linux/bitops.h>
  16. #include <linux/delay.h>
  17. #include <linux/gpio/consumer.h>
  18. #include <linux/gpio/driver.h>
  19. #include <linux/module.h>
  20. #include <linux/mutex.h>
  21. #include <linux/spi/spi.h>
  22. #define DEFAULT_NGPIO 8
  23. /**
  24. * struct pisosr_gpio - GPIO driver data
  25. * @chip: GPIO controller chip
  26. * @spi: SPI device pointer
  27. * @buffer: Buffer for device reads
  28. * @buffer_size: Size of buffer
  29. * @load_gpio: GPIO pin used to load input into device
  30. * @lock: Protects read sequences
  31. */
  32. struct pisosr_gpio {
  33. struct gpio_chip chip;
  34. struct spi_device *spi;
  35. u8 *buffer;
  36. size_t buffer_size;
  37. struct gpio_desc *load_gpio;
  38. struct mutex lock;
  39. };
  40. static int pisosr_gpio_refresh(struct pisosr_gpio *gpio)
  41. {
  42. int ret;
  43. mutex_lock(&gpio->lock);
  44. if (gpio->load_gpio) {
  45. gpiod_set_value_cansleep(gpio->load_gpio, 1);
  46. udelay(1); /* registers load time (~10ns) */
  47. gpiod_set_value_cansleep(gpio->load_gpio, 0);
  48. udelay(1); /* registers recovery time (~5ns) */
  49. }
  50. ret = spi_read(gpio->spi, gpio->buffer, gpio->buffer_size);
  51. mutex_unlock(&gpio->lock);
  52. return ret;
  53. }
  54. static int pisosr_gpio_get_direction(struct gpio_chip *chip,
  55. unsigned offset)
  56. {
  57. /* This device always input */
  58. return GPIO_LINE_DIRECTION_IN;
  59. }
  60. static int pisosr_gpio_direction_input(struct gpio_chip *chip,
  61. unsigned offset)
  62. {
  63. /* This device always input */
  64. return 0;
  65. }
  66. static int pisosr_gpio_direction_output(struct gpio_chip *chip,
  67. unsigned offset, int value)
  68. {
  69. /* This device is input only */
  70. return -EINVAL;
  71. }
  72. static int pisosr_gpio_get(struct gpio_chip *chip, unsigned offset)
  73. {
  74. struct pisosr_gpio *gpio = gpiochip_get_data(chip);
  75. /* Refresh may not always be needed */
  76. pisosr_gpio_refresh(gpio);
  77. return (gpio->buffer[offset / 8] >> (offset % 8)) & 0x1;
  78. }
  79. static int pisosr_gpio_get_multiple(struct gpio_chip *chip,
  80. unsigned long *mask, unsigned long *bits)
  81. {
  82. struct pisosr_gpio *gpio = gpiochip_get_data(chip);
  83. unsigned long offset;
  84. unsigned long gpio_mask;
  85. unsigned long buffer_state;
  86. pisosr_gpio_refresh(gpio);
  87. bitmap_zero(bits, chip->ngpio);
  88. for_each_set_clump8(offset, gpio_mask, mask, chip->ngpio) {
  89. buffer_state = gpio->buffer[offset / 8] & gpio_mask;
  90. bitmap_set_value8(bits, buffer_state, offset);
  91. }
  92. return 0;
  93. }
  94. static const struct gpio_chip template_chip = {
  95. .label = "pisosr-gpio",
  96. .owner = THIS_MODULE,
  97. .get_direction = pisosr_gpio_get_direction,
  98. .direction_input = pisosr_gpio_direction_input,
  99. .direction_output = pisosr_gpio_direction_output,
  100. .get = pisosr_gpio_get,
  101. .get_multiple = pisosr_gpio_get_multiple,
  102. .base = -1,
  103. .ngpio = DEFAULT_NGPIO,
  104. .can_sleep = true,
  105. };
  106. static int pisosr_gpio_probe(struct spi_device *spi)
  107. {
  108. struct device *dev = &spi->dev;
  109. struct pisosr_gpio *gpio;
  110. int ret;
  111. gpio = devm_kzalloc(dev, sizeof(*gpio), GFP_KERNEL);
  112. if (!gpio)
  113. return -ENOMEM;
  114. spi_set_drvdata(spi, gpio);
  115. gpio->chip = template_chip;
  116. gpio->chip.parent = dev;
  117. of_property_read_u16(dev->of_node, "ngpios", &gpio->chip.ngpio);
  118. gpio->spi = spi;
  119. gpio->buffer_size = DIV_ROUND_UP(gpio->chip.ngpio, 8);
  120. gpio->buffer = devm_kzalloc(dev, gpio->buffer_size, GFP_KERNEL);
  121. if (!gpio->buffer)
  122. return -ENOMEM;
  123. gpio->load_gpio = devm_gpiod_get_optional(dev, "load", GPIOD_OUT_LOW);
  124. if (IS_ERR(gpio->load_gpio))
  125. return dev_err_probe(dev, PTR_ERR(gpio->load_gpio),
  126. "Unable to allocate load GPIO\n");
  127. mutex_init(&gpio->lock);
  128. ret = gpiochip_add_data(&gpio->chip, gpio);
  129. if (ret < 0) {
  130. dev_err(dev, "Unable to register gpiochip\n");
  131. return ret;
  132. }
  133. return 0;
  134. }
  135. static int pisosr_gpio_remove(struct spi_device *spi)
  136. {
  137. struct pisosr_gpio *gpio = spi_get_drvdata(spi);
  138. gpiochip_remove(&gpio->chip);
  139. mutex_destroy(&gpio->lock);
  140. return 0;
  141. }
  142. static const struct spi_device_id pisosr_gpio_id_table[] = {
  143. { "pisosr-gpio", },
  144. { /* sentinel */ }
  145. };
  146. MODULE_DEVICE_TABLE(spi, pisosr_gpio_id_table);
  147. static const struct of_device_id pisosr_gpio_of_match_table[] = {
  148. { .compatible = "pisosr-gpio", },
  149. { /* sentinel */ }
  150. };
  151. MODULE_DEVICE_TABLE(of, pisosr_gpio_of_match_table);
  152. static struct spi_driver pisosr_gpio_driver = {
  153. .driver = {
  154. .name = "pisosr-gpio",
  155. .of_match_table = pisosr_gpio_of_match_table,
  156. },
  157. .probe = pisosr_gpio_probe,
  158. .remove = pisosr_gpio_remove,
  159. .id_table = pisosr_gpio_id_table,
  160. };
  161. module_spi_driver(pisosr_gpio_driver);
  162. MODULE_AUTHOR("Andrew F. Davis <afd@ti.com>");
  163. MODULE_DESCRIPTION("SPI Compatible PISO Shift Register GPIO Driver");
  164. MODULE_LICENSE("GPL v2");