gpio-74x164.c 4.7 KB

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