gpio-74x164.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. u8 *buffer;
  21. struct gpio_chip gpio_chip;
  22. struct mutex lock;
  23. u32 registers;
  24. };
  25. static struct gen_74x164_chip *gpio_to_74x164_chip(struct gpio_chip *gc)
  26. {
  27. return container_of(gc, struct gen_74x164_chip, gpio_chip);
  28. }
  29. static int __gen_74x164_write_config(struct gen_74x164_chip *chip)
  30. {
  31. struct spi_device *spi = to_spi_device(chip->gpio_chip.dev);
  32. struct spi_message message;
  33. struct spi_transfer *msg_buf;
  34. int i, ret = 0;
  35. msg_buf = kzalloc(chip->registers * sizeof(struct spi_transfer),
  36. GFP_KERNEL);
  37. if (!msg_buf)
  38. return -ENOMEM;
  39. spi_message_init(&message);
  40. /*
  41. * Since the registers are chained, every byte sent will make
  42. * the previous byte shift to the next register in the
  43. * chain. Thus, the first byte send will end up in the last
  44. * register at the end of the transfer. So, to have a logical
  45. * numbering, send the bytes in reverse order so that the last
  46. * byte of the buffer will end up in the last register.
  47. */
  48. for (i = chip->registers - 1; i >= 0; i--) {
  49. msg_buf[i].tx_buf = chip->buffer + i;
  50. msg_buf[i].len = sizeof(u8);
  51. spi_message_add_tail(msg_buf + i, &message);
  52. }
  53. ret = spi_sync(spi, &message);
  54. kfree(msg_buf);
  55. return ret;
  56. }
  57. static int gen_74x164_get_value(struct gpio_chip *gc, unsigned offset)
  58. {
  59. struct gen_74x164_chip *chip = gpio_to_74x164_chip(gc);
  60. u8 bank = offset / 8;
  61. u8 pin = offset % 8;
  62. int ret;
  63. mutex_lock(&chip->lock);
  64. ret = (chip->buffer[bank] >> pin) & 0x1;
  65. mutex_unlock(&chip->lock);
  66. return ret;
  67. }
  68. static void gen_74x164_set_value(struct gpio_chip *gc,
  69. unsigned offset, int val)
  70. {
  71. struct gen_74x164_chip *chip = gpio_to_74x164_chip(gc);
  72. u8 bank = offset / 8;
  73. u8 pin = offset % 8;
  74. mutex_lock(&chip->lock);
  75. if (val)
  76. chip->buffer[bank] |= (1 << pin);
  77. else
  78. chip->buffer[bank] &= ~(1 << pin);
  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. int ret;
  92. /*
  93. * bits_per_word cannot be configured in platform data
  94. */
  95. spi->bits_per_word = 8;
  96. ret = spi_setup(spi);
  97. if (ret < 0)
  98. return ret;
  99. chip = devm_kzalloc(&spi->dev, sizeof(*chip), GFP_KERNEL);
  100. if (!chip)
  101. return -ENOMEM;
  102. spi_set_drvdata(spi, chip);
  103. chip->gpio_chip.label = spi->modalias;
  104. chip->gpio_chip.direction_output = gen_74x164_direction_output;
  105. chip->gpio_chip.get = gen_74x164_get_value;
  106. chip->gpio_chip.set = gen_74x164_set_value;
  107. chip->gpio_chip.base = -1;
  108. if (of_property_read_u32(spi->dev.of_node, "registers-number",
  109. &chip->registers)) {
  110. dev_err(&spi->dev,
  111. "Missing registers-number property in the DT.\n");
  112. return -EINVAL;
  113. }
  114. chip->gpio_chip.ngpio = GEN_74X164_NUMBER_GPIOS * chip->registers;
  115. chip->buffer = devm_kzalloc(&spi->dev, chip->registers, GFP_KERNEL);
  116. if (!chip->buffer)
  117. return -ENOMEM;
  118. chip->gpio_chip.can_sleep = true;
  119. chip->gpio_chip.dev = &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(&chip->gpio_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. {},
  144. };
  145. MODULE_DEVICE_TABLE(of, gen_74x164_dt_ids);
  146. static struct spi_driver gen_74x164_driver = {
  147. .driver = {
  148. .name = "74x164",
  149. .owner = THIS_MODULE,
  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");