gpio-74x164.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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/spi/74x164.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 spi_device *spi;
  22. u8 *buffer;
  23. struct gpio_chip gpio_chip;
  24. struct mutex lock;
  25. u32 registers;
  26. };
  27. static struct gen_74x164_chip *gpio_to_74x164_chip(struct gpio_chip *gc)
  28. {
  29. return container_of(gc, struct gen_74x164_chip, gpio_chip);
  30. }
  31. static int __gen_74x164_write_config(struct gen_74x164_chip *chip)
  32. {
  33. struct spi_message message;
  34. struct spi_transfer *msg_buf;
  35. int i, ret = 0;
  36. msg_buf = kzalloc(chip->registers * sizeof(struct spi_transfer),
  37. GFP_KERNEL);
  38. if (!msg_buf)
  39. return -ENOMEM;
  40. spi_message_init(&message);
  41. /*
  42. * Since the registers are chained, every byte sent will make
  43. * the previous byte shift to the next register in the
  44. * chain. Thus, the first byte send will end up in the last
  45. * register at the end of the transfer. So, to have a logical
  46. * numbering, send the bytes in reverse order so that the last
  47. * byte of the buffer will end up in the last register.
  48. */
  49. for (i = chip->registers - 1; i >= 0; i--) {
  50. msg_buf[i].tx_buf = chip->buffer +i;
  51. msg_buf[i].len = sizeof(u8);
  52. spi_message_add_tail(msg_buf + i, &message);
  53. }
  54. ret = spi_sync(chip->spi, &message);
  55. kfree(msg_buf);
  56. return ret;
  57. }
  58. static int gen_74x164_get_value(struct gpio_chip *gc, unsigned offset)
  59. {
  60. struct gen_74x164_chip *chip = gpio_to_74x164_chip(gc);
  61. u8 bank = offset / 8;
  62. u8 pin = offset % 8;
  63. int ret;
  64. mutex_lock(&chip->lock);
  65. ret = (chip->buffer[bank] >> pin) & 0x1;
  66. mutex_unlock(&chip->lock);
  67. return ret;
  68. }
  69. static void gen_74x164_set_value(struct gpio_chip *gc,
  70. unsigned offset, int val)
  71. {
  72. struct gen_74x164_chip *chip = gpio_to_74x164_chip(gc);
  73. u8 bank = offset / 8;
  74. u8 pin = offset % 8;
  75. mutex_lock(&chip->lock);
  76. if (val)
  77. chip->buffer[bank] |= (1 << pin);
  78. else
  79. chip->buffer[bank] &= ~(1 << pin);
  80. __gen_74x164_write_config(chip);
  81. mutex_unlock(&chip->lock);
  82. }
  83. static int gen_74x164_direction_output(struct gpio_chip *gc,
  84. unsigned offset, int val)
  85. {
  86. gen_74x164_set_value(gc, offset, val);
  87. return 0;
  88. }
  89. static int __devinit gen_74x164_probe(struct spi_device *spi)
  90. {
  91. struct gen_74x164_chip *chip;
  92. struct gen_74x164_chip_platform_data *pdata;
  93. int ret;
  94. if (!spi->dev.of_node) {
  95. dev_err(&spi->dev, "No device tree data available.\n");
  96. return -EINVAL;
  97. }
  98. /*
  99. * bits_per_word cannot be configured in platform data
  100. */
  101. spi->bits_per_word = 8;
  102. ret = spi_setup(spi);
  103. if (ret < 0)
  104. return ret;
  105. chip = devm_kzalloc(&spi->dev, sizeof(*chip), GFP_KERNEL);
  106. if (!chip)
  107. return -ENOMEM;
  108. pdata = spi->dev.platform_data;
  109. if (pdata && pdata->base)
  110. chip->gpio_chip.base = pdata->base;
  111. else
  112. chip->gpio_chip.base = -1;
  113. mutex_init(&chip->lock);
  114. dev_set_drvdata(&spi->dev, chip);
  115. chip->spi = spi;
  116. chip->gpio_chip.label = spi->modalias;
  117. chip->gpio_chip.direction_output = gen_74x164_direction_output;
  118. chip->gpio_chip.get = gen_74x164_get_value;
  119. chip->gpio_chip.set = gen_74x164_set_value;
  120. if (of_property_read_u32(spi->dev.of_node, "registers-number", &chip->registers)) {
  121. dev_err(&spi->dev, "Missing registers-number property in the DT.\n");
  122. ret = -EINVAL;
  123. goto exit_destroy;
  124. }
  125. chip->gpio_chip.ngpio = GEN_74X164_NUMBER_GPIOS * chip->registers;
  126. chip->buffer = devm_kzalloc(&spi->dev, chip->registers, GFP_KERNEL);
  127. if (!chip->buffer) {
  128. ret = -ENOMEM;
  129. goto exit_destroy;
  130. }
  131. chip->gpio_chip.can_sleep = 1;
  132. chip->gpio_chip.dev = &spi->dev;
  133. chip->gpio_chip.owner = THIS_MODULE;
  134. ret = __gen_74x164_write_config(chip);
  135. if (ret) {
  136. dev_err(&spi->dev, "Failed writing: %d\n", ret);
  137. goto exit_destroy;
  138. }
  139. ret = gpiochip_add(&chip->gpio_chip);
  140. if (ret)
  141. goto exit_destroy;
  142. return ret;
  143. exit_destroy:
  144. dev_set_drvdata(&spi->dev, NULL);
  145. mutex_destroy(&chip->lock);
  146. return ret;
  147. }
  148. static int __devexit gen_74x164_remove(struct spi_device *spi)
  149. {
  150. struct gen_74x164_chip *chip;
  151. int ret;
  152. chip = dev_get_drvdata(&spi->dev);
  153. if (chip == NULL)
  154. return -ENODEV;
  155. dev_set_drvdata(&spi->dev, NULL);
  156. ret = gpiochip_remove(&chip->gpio_chip);
  157. if (!ret)
  158. mutex_destroy(&chip->lock);
  159. else
  160. dev_err(&spi->dev, "Failed to remove the GPIO controller: %d\n",
  161. ret);
  162. return ret;
  163. }
  164. static const struct of_device_id gen_74x164_dt_ids[] = {
  165. { .compatible = "fairchild,74hc595" },
  166. {},
  167. };
  168. MODULE_DEVICE_TABLE(of, gen_74x164_dt_ids);
  169. static struct spi_driver gen_74x164_driver = {
  170. .driver = {
  171. .name = "74x164",
  172. .owner = THIS_MODULE,
  173. .of_match_table = of_match_ptr(gen_74x164_dt_ids),
  174. },
  175. .probe = gen_74x164_probe,
  176. .remove = __devexit_p(gen_74x164_remove),
  177. };
  178. module_spi_driver(gen_74x164_driver);
  179. MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
  180. MODULE_AUTHOR("Miguel Gaio <miguel.gaio@efixo.com>");
  181. MODULE_DESCRIPTION("GPIO expander driver for 74X164 8-bits shift register");
  182. MODULE_LICENSE("GPL v2");