gpio-74x164.c 4.0 KB

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