gpio-74x164.c 4.0 KB

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