gpio-74x164.c 5.0 KB

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