gpio-74x164.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. struct spi_transfer xfer = {
  35. .tx_buf = chip->buffer,
  36. .len = chip->registers,
  37. };
  38. return spi_sync_transfer(to_spi_device(chip->gpio_chip.parent),
  39. &xfer, 1);
  40. }
  41. static int gen_74x164_get_value(struct gpio_chip *gc, unsigned offset)
  42. {
  43. struct gen_74x164_chip *chip = gpiochip_get_data(gc);
  44. u8 bank = chip->registers - 1 - offset / 8;
  45. u8 pin = offset % 8;
  46. int ret;
  47. mutex_lock(&chip->lock);
  48. ret = (chip->buffer[bank] >> pin) & 0x1;
  49. mutex_unlock(&chip->lock);
  50. return ret;
  51. }
  52. static void gen_74x164_set_value(struct gpio_chip *gc,
  53. unsigned offset, int val)
  54. {
  55. struct gen_74x164_chip *chip = gpiochip_get_data(gc);
  56. u8 bank = chip->registers - 1 - offset / 8;
  57. u8 pin = offset % 8;
  58. mutex_lock(&chip->lock);
  59. if (val)
  60. chip->buffer[bank] |= (1 << pin);
  61. else
  62. chip->buffer[bank] &= ~(1 << pin);
  63. __gen_74x164_write_config(chip);
  64. mutex_unlock(&chip->lock);
  65. }
  66. static int gen_74x164_direction_output(struct gpio_chip *gc,
  67. unsigned offset, int val)
  68. {
  69. gen_74x164_set_value(gc, offset, val);
  70. return 0;
  71. }
  72. static int gen_74x164_probe(struct spi_device *spi)
  73. {
  74. struct gen_74x164_chip *chip;
  75. u32 nregs;
  76. int ret;
  77. /*
  78. * bits_per_word cannot be configured in platform data
  79. */
  80. spi->bits_per_word = 8;
  81. ret = spi_setup(spi);
  82. if (ret < 0)
  83. return ret;
  84. if (of_property_read_u32(spi->dev.of_node, "registers-number",
  85. &nregs)) {
  86. dev_err(&spi->dev,
  87. "Missing registers-number property in the DT.\n");
  88. return -EINVAL;
  89. }
  90. chip = devm_kzalloc(&spi->dev, sizeof(*chip) + nregs, GFP_KERNEL);
  91. if (!chip)
  92. return -ENOMEM;
  93. spi_set_drvdata(spi, chip);
  94. chip->gpio_chip.label = spi->modalias;
  95. chip->gpio_chip.direction_output = gen_74x164_direction_output;
  96. chip->gpio_chip.get = gen_74x164_get_value;
  97. chip->gpio_chip.set = gen_74x164_set_value;
  98. chip->gpio_chip.base = -1;
  99. chip->registers = nregs;
  100. chip->gpio_chip.ngpio = GEN_74X164_NUMBER_GPIOS * chip->registers;
  101. chip->gpio_chip.can_sleep = true;
  102. chip->gpio_chip.parent = &spi->dev;
  103. chip->gpio_chip.owner = THIS_MODULE;
  104. mutex_init(&chip->lock);
  105. ret = __gen_74x164_write_config(chip);
  106. if (ret) {
  107. dev_err(&spi->dev, "Failed writing: %d\n", ret);
  108. goto exit_destroy;
  109. }
  110. ret = gpiochip_add_data(&chip->gpio_chip, chip);
  111. if (!ret)
  112. return 0;
  113. exit_destroy:
  114. mutex_destroy(&chip->lock);
  115. return ret;
  116. }
  117. static int gen_74x164_remove(struct spi_device *spi)
  118. {
  119. struct gen_74x164_chip *chip = spi_get_drvdata(spi);
  120. gpiochip_remove(&chip->gpio_chip);
  121. mutex_destroy(&chip->lock);
  122. return 0;
  123. }
  124. static const struct of_device_id gen_74x164_dt_ids[] = {
  125. { .compatible = "fairchild,74hc595" },
  126. {},
  127. };
  128. MODULE_DEVICE_TABLE(of, gen_74x164_dt_ids);
  129. static struct spi_driver gen_74x164_driver = {
  130. .driver = {
  131. .name = "74x164",
  132. .of_match_table = gen_74x164_dt_ids,
  133. },
  134. .probe = gen_74x164_probe,
  135. .remove = gen_74x164_remove,
  136. };
  137. module_spi_driver(gen_74x164_driver);
  138. MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
  139. MODULE_AUTHOR("Miguel Gaio <miguel.gaio@efixo.com>");
  140. MODULE_DESCRIPTION("GPIO expander driver for 74X164 8-bits shift register");
  141. MODULE_LICENSE("GPL v2");