gpio-74x164.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. /*
  66. * bits_per_word cannot be configured in platform data
  67. */
  68. spi->bits_per_word = 8;
  69. ret = spi_setup(spi);
  70. if (ret < 0)
  71. return ret;
  72. chip = devm_kzalloc(&spi->dev, sizeof(*chip), GFP_KERNEL);
  73. if (!chip)
  74. return -ENOMEM;
  75. pdata = spi->dev.platform_data;
  76. if (pdata && pdata->base)
  77. chip->gpio_chip.base = pdata->base;
  78. else
  79. chip->gpio_chip.base = -1;
  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.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. return ret;
  104. }
  105. static int __devexit gen_74x164_remove(struct spi_device *spi)
  106. {
  107. struct gen_74x164_chip *chip;
  108. int ret;
  109. chip = dev_get_drvdata(&spi->dev);
  110. if (chip == NULL)
  111. return -ENODEV;
  112. dev_set_drvdata(&spi->dev, NULL);
  113. ret = gpiochip_remove(&chip->gpio_chip);
  114. if (!ret)
  115. mutex_destroy(&chip->lock);
  116. else
  117. dev_err(&spi->dev, "Failed to remove the GPIO controller: %d\n",
  118. ret);
  119. return ret;
  120. }
  121. static struct spi_driver gen_74x164_driver = {
  122. .driver = {
  123. .name = "74x164",
  124. .owner = THIS_MODULE,
  125. },
  126. .probe = gen_74x164_probe,
  127. .remove = __devexit_p(gen_74x164_remove),
  128. };
  129. module_spi_driver(gen_74x164_driver);
  130. MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
  131. MODULE_AUTHOR("Miguel Gaio <miguel.gaio@efixo.com>");
  132. MODULE_DESCRIPTION("GPIO expander driver for 74X164 8-bits shift register");
  133. MODULE_LICENSE("GPL v2");