gpio-74x164.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * 74Hx164 - Generic serial-in/parallel-out 8-bits shift register GPIO driver
  4. *
  5. * Copyright (C) 2010 Gabor Juhos <juhosg@openwrt.org>
  6. * Copyright (C) 2010 Miguel Gaio <miguel.gaio@efixo.com>
  7. */
  8. #include <linux/bitops.h>
  9. #include <linux/gpio/consumer.h>
  10. #include <linux/gpio/driver.h>
  11. #include <linux/module.h>
  12. #include <linux/mutex.h>
  13. #include <linux/property.h>
  14. #include <linux/slab.h>
  15. #include <linux/spi/spi.h>
  16. #define GEN_74X164_NUMBER_GPIOS 8
  17. struct gen_74x164_chip {
  18. struct gpio_chip gpio_chip;
  19. struct mutex lock;
  20. struct gpio_desc *gpiod_oe;
  21. u32 registers;
  22. /*
  23. * Since the registers are chained, every byte sent will make
  24. * the previous byte shift to the next register in the
  25. * chain. Thus, the first byte sent will end up in the last
  26. * register at the end of the transfer. So, to have a logical
  27. * numbering, store the bytes in reverse order.
  28. */
  29. u8 buffer[];
  30. };
  31. static int __gen_74x164_write_config(struct gen_74x164_chip *chip)
  32. {
  33. return spi_write(to_spi_device(chip->gpio_chip.parent), chip->buffer,
  34. chip->registers);
  35. }
  36. static int gen_74x164_get_value(struct gpio_chip *gc, unsigned offset)
  37. {
  38. struct gen_74x164_chip *chip = gpiochip_get_data(gc);
  39. u8 bank = chip->registers - 1 - offset / 8;
  40. u8 pin = offset % 8;
  41. int ret;
  42. mutex_lock(&chip->lock);
  43. ret = (chip->buffer[bank] >> pin) & 0x1;
  44. mutex_unlock(&chip->lock);
  45. return ret;
  46. }
  47. static void gen_74x164_set_value(struct gpio_chip *gc,
  48. unsigned offset, int val)
  49. {
  50. struct gen_74x164_chip *chip = gpiochip_get_data(gc);
  51. u8 bank = chip->registers - 1 - offset / 8;
  52. u8 pin = offset % 8;
  53. mutex_lock(&chip->lock);
  54. if (val)
  55. chip->buffer[bank] |= (1 << pin);
  56. else
  57. chip->buffer[bank] &= ~(1 << pin);
  58. __gen_74x164_write_config(chip);
  59. mutex_unlock(&chip->lock);
  60. }
  61. static void gen_74x164_set_multiple(struct gpio_chip *gc, unsigned long *mask,
  62. unsigned long *bits)
  63. {
  64. struct gen_74x164_chip *chip = gpiochip_get_data(gc);
  65. unsigned long offset;
  66. unsigned long bankmask;
  67. size_t bank;
  68. unsigned long bitmask;
  69. mutex_lock(&chip->lock);
  70. for_each_set_clump8(offset, bankmask, mask, chip->registers * 8) {
  71. bank = chip->registers - 1 - offset / 8;
  72. bitmask = bitmap_get_value8(bits, offset) & bankmask;
  73. chip->buffer[bank] &= ~bankmask;
  74. chip->buffer[bank] |= bitmask;
  75. }
  76. __gen_74x164_write_config(chip);
  77. mutex_unlock(&chip->lock);
  78. }
  79. static int gen_74x164_direction_output(struct gpio_chip *gc,
  80. unsigned offset, int val)
  81. {
  82. gen_74x164_set_value(gc, offset, val);
  83. return 0;
  84. }
  85. static int gen_74x164_probe(struct spi_device *spi)
  86. {
  87. struct gen_74x164_chip *chip;
  88. u32 nregs;
  89. int ret;
  90. /*
  91. * bits_per_word cannot be configured in platform data
  92. */
  93. spi->bits_per_word = 8;
  94. ret = spi_setup(spi);
  95. if (ret < 0)
  96. return ret;
  97. ret = device_property_read_u32(&spi->dev, "registers-number", &nregs);
  98. if (ret) {
  99. dev_err(&spi->dev, "Missing 'registers-number' property.\n");
  100. return -EINVAL;
  101. }
  102. chip = devm_kzalloc(&spi->dev, sizeof(*chip) + nregs, GFP_KERNEL);
  103. if (!chip)
  104. return -ENOMEM;
  105. chip->gpiod_oe = devm_gpiod_get_optional(&spi->dev, "enable",
  106. GPIOD_OUT_LOW);
  107. if (IS_ERR(chip->gpiod_oe))
  108. return PTR_ERR(chip->gpiod_oe);
  109. gpiod_set_value_cansleep(chip->gpiod_oe, 1);
  110. spi_set_drvdata(spi, chip);
  111. chip->gpio_chip.label = spi->modalias;
  112. chip->gpio_chip.direction_output = gen_74x164_direction_output;
  113. chip->gpio_chip.get = gen_74x164_get_value;
  114. chip->gpio_chip.set = gen_74x164_set_value;
  115. chip->gpio_chip.set_multiple = gen_74x164_set_multiple;
  116. chip->gpio_chip.base = -1;
  117. chip->registers = nregs;
  118. chip->gpio_chip.ngpio = GEN_74X164_NUMBER_GPIOS * chip->registers;
  119. chip->gpio_chip.can_sleep = true;
  120. chip->gpio_chip.parent = &spi->dev;
  121. chip->gpio_chip.owner = THIS_MODULE;
  122. mutex_init(&chip->lock);
  123. ret = __gen_74x164_write_config(chip);
  124. if (ret) {
  125. dev_err(&spi->dev, "Failed writing: %d\n", ret);
  126. goto exit_destroy;
  127. }
  128. ret = gpiochip_add_data(&chip->gpio_chip, chip);
  129. if (!ret)
  130. return 0;
  131. exit_destroy:
  132. mutex_destroy(&chip->lock);
  133. return ret;
  134. }
  135. static int gen_74x164_remove(struct spi_device *spi)
  136. {
  137. struct gen_74x164_chip *chip = spi_get_drvdata(spi);
  138. gpiod_set_value_cansleep(chip->gpiod_oe, 0);
  139. gpiochip_remove(&chip->gpio_chip);
  140. mutex_destroy(&chip->lock);
  141. return 0;
  142. }
  143. static const struct of_device_id gen_74x164_dt_ids[] = {
  144. { .compatible = "fairchild,74hc595" },
  145. { .compatible = "nxp,74lvc594" },
  146. {},
  147. };
  148. MODULE_DEVICE_TABLE(of, gen_74x164_dt_ids);
  149. static struct spi_driver gen_74x164_driver = {
  150. .driver = {
  151. .name = "74x164",
  152. .of_match_table = gen_74x164_dt_ids,
  153. },
  154. .probe = gen_74x164_probe,
  155. .remove = gen_74x164_remove,
  156. };
  157. module_spi_driver(gen_74x164_driver);
  158. MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
  159. MODULE_AUTHOR("Miguel Gaio <miguel.gaio@efixo.com>");
  160. MODULE_DESCRIPTION("GPIO expander driver for 74X164 8-bits shift register");
  161. MODULE_LICENSE("GPL v2");