74x164.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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/slab.h>
  17. #define GEN_74X164_GPIO_COUNT 8
  18. struct gen_74x164_chip {
  19. struct spi_device *spi;
  20. struct gpio_chip gpio_chip;
  21. struct mutex lock;
  22. u8 port_config;
  23. };
  24. static void gen_74x164_set_value(struct gpio_chip *, unsigned, int);
  25. static struct gen_74x164_chip *gpio_to_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. return spi_write(chip->spi,
  32. &chip->port_config, sizeof(chip->port_config));
  33. }
  34. static int gen_74x164_direction_output(struct gpio_chip *gc,
  35. unsigned offset, int val)
  36. {
  37. gen_74x164_set_value(gc, offset, val);
  38. return 0;
  39. }
  40. static int gen_74x164_get_value(struct gpio_chip *gc, unsigned offset)
  41. {
  42. struct gen_74x164_chip *chip = gpio_to_chip(gc);
  43. int ret;
  44. mutex_lock(&chip->lock);
  45. ret = (chip->port_config >> offset) & 0x1;
  46. mutex_unlock(&chip->lock);
  47. return ret;
  48. }
  49. static void gen_74x164_set_value(struct gpio_chip *gc,
  50. unsigned offset, int val)
  51. {
  52. struct gen_74x164_chip *chip = gpio_to_chip(gc);
  53. mutex_lock(&chip->lock);
  54. if (val)
  55. chip->port_config |= (1 << offset);
  56. else
  57. chip->port_config &= ~(1 << offset);
  58. __gen_74x164_write_config(chip);
  59. mutex_unlock(&chip->lock);
  60. }
  61. static int __devinit gen_74x164_probe(struct spi_device *spi)
  62. {
  63. struct gen_74x164_chip *chip;
  64. struct gen_74x164_chip_platform_data *pdata;
  65. int ret;
  66. pdata = spi->dev.platform_data;
  67. if (!pdata || !pdata->base) {
  68. dev_dbg(&spi->dev, "incorrect or missing platform data\n");
  69. return -EINVAL;
  70. }
  71. /*
  72. * bits_per_word cannot be configured in platform data
  73. */
  74. spi->bits_per_word = 8;
  75. ret = spi_setup(spi);
  76. if (ret < 0)
  77. return ret;
  78. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  79. if (!chip)
  80. return -ENOMEM;
  81. mutex_init(&chip->lock);
  82. dev_set_drvdata(&spi->dev, chip);
  83. chip->spi = spi;
  84. chip->gpio_chip.label = GEN_74X164_DRIVER_NAME,
  85. chip->gpio_chip.direction_output = gen_74x164_direction_output;
  86. chip->gpio_chip.get = gen_74x164_get_value;
  87. chip->gpio_chip.set = gen_74x164_set_value;
  88. chip->gpio_chip.base = pdata->base;
  89. chip->gpio_chip.ngpio = GEN_74X164_GPIO_COUNT;
  90. chip->gpio_chip.can_sleep = 1;
  91. chip->gpio_chip.dev = &spi->dev;
  92. chip->gpio_chip.owner = THIS_MODULE;
  93. ret = __gen_74x164_write_config(chip);
  94. if (ret) {
  95. dev_err(&spi->dev, "Failed writing: %d\n", ret);
  96. goto exit_destroy;
  97. }
  98. ret = gpiochip_add(&chip->gpio_chip);
  99. if (ret)
  100. goto exit_destroy;
  101. return ret;
  102. exit_destroy:
  103. dev_set_drvdata(&spi->dev, NULL);
  104. mutex_destroy(&chip->lock);
  105. kfree(chip);
  106. return ret;
  107. }
  108. static int __devexit gen_74x164_remove(struct spi_device *spi)
  109. {
  110. struct gen_74x164_chip *chip;
  111. int ret;
  112. chip = dev_get_drvdata(&spi->dev);
  113. if (chip == NULL)
  114. return -ENODEV;
  115. dev_set_drvdata(&spi->dev, NULL);
  116. ret = gpiochip_remove(&chip->gpio_chip);
  117. if (!ret) {
  118. mutex_destroy(&chip->lock);
  119. kfree(chip);
  120. } else
  121. dev_err(&spi->dev, "Failed to remove the GPIO controller: %d\n",
  122. ret);
  123. return ret;
  124. }
  125. static struct spi_driver gen_74x164_driver = {
  126. .driver = {
  127. .name = GEN_74X164_DRIVER_NAME,
  128. .owner = THIS_MODULE,
  129. },
  130. .probe = gen_74x164_probe,
  131. .remove = __devexit_p(gen_74x164_remove),
  132. };
  133. static int __init gen_74x164_init(void)
  134. {
  135. return spi_register_driver(&gen_74x164_driver);
  136. }
  137. subsys_initcall(gen_74x164_init);
  138. static void __exit gen_74x164_exit(void)
  139. {
  140. spi_unregister_driver(&gen_74x164_driver);
  141. }
  142. module_exit(gen_74x164_exit);
  143. MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
  144. MODULE_AUTHOR("Miguel Gaio <miguel.gaio@efixo.com>");
  145. MODULE_DESCRIPTION("GPIO expander driver for 74X164 8-bits shift register");
  146. MODULE_LICENSE("GPL v2");