gpio-mc33880.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * MC33880 high-side/low-side switch GPIO driver
  3. * Copyright (c) 2009 Intel Corporation
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. /* Supports:
  19. * Freescale MC33880 high-side/low-side switch
  20. */
  21. #include <linux/init.h>
  22. #include <linux/mutex.h>
  23. #include <linux/spi/spi.h>
  24. #include <linux/spi/mc33880.h>
  25. #include <linux/gpio.h>
  26. #include <linux/slab.h>
  27. #include <linux/module.h>
  28. #define DRIVER_NAME "mc33880"
  29. /*
  30. * Pin configurations, see MAX7301 datasheet page 6
  31. */
  32. #define PIN_CONFIG_MASK 0x03
  33. #define PIN_CONFIG_IN_PULLUP 0x03
  34. #define PIN_CONFIG_IN_WO_PULLUP 0x02
  35. #define PIN_CONFIG_OUT 0x01
  36. #define PIN_NUMBER 8
  37. /*
  38. * Some registers must be read back to modify.
  39. * To save time we cache them here in memory
  40. */
  41. struct mc33880 {
  42. struct mutex lock; /* protect from simultaneous accesses */
  43. u8 port_config;
  44. struct gpio_chip chip;
  45. struct spi_device *spi;
  46. };
  47. static int mc33880_write_config(struct mc33880 *mc)
  48. {
  49. return spi_write(mc->spi, &mc->port_config, sizeof(mc->port_config));
  50. }
  51. static int __mc33880_set(struct mc33880 *mc, unsigned offset, int value)
  52. {
  53. if (value)
  54. mc->port_config |= 1 << offset;
  55. else
  56. mc->port_config &= ~(1 << offset);
  57. return mc33880_write_config(mc);
  58. }
  59. static void mc33880_set(struct gpio_chip *chip, unsigned offset, int value)
  60. {
  61. struct mc33880 *mc = container_of(chip, struct mc33880, chip);
  62. mutex_lock(&mc->lock);
  63. __mc33880_set(mc, offset, value);
  64. mutex_unlock(&mc->lock);
  65. }
  66. static int mc33880_probe(struct spi_device *spi)
  67. {
  68. struct mc33880 *mc;
  69. struct mc33880_platform_data *pdata;
  70. int ret;
  71. pdata = dev_get_platdata(&spi->dev);
  72. if (!pdata || !pdata->base) {
  73. dev_dbg(&spi->dev, "incorrect or missing platform data\n");
  74. return -EINVAL;
  75. }
  76. /*
  77. * bits_per_word cannot be configured in platform data
  78. */
  79. spi->bits_per_word = 8;
  80. ret = spi_setup(spi);
  81. if (ret < 0)
  82. return ret;
  83. mc = devm_kzalloc(&spi->dev, sizeof(struct mc33880), GFP_KERNEL);
  84. if (!mc)
  85. return -ENOMEM;
  86. mutex_init(&mc->lock);
  87. spi_set_drvdata(spi, mc);
  88. mc->spi = spi;
  89. mc->chip.label = DRIVER_NAME,
  90. mc->chip.set = mc33880_set;
  91. mc->chip.base = pdata->base;
  92. mc->chip.ngpio = PIN_NUMBER;
  93. mc->chip.can_sleep = true;
  94. mc->chip.dev = &spi->dev;
  95. mc->chip.owner = THIS_MODULE;
  96. mc->port_config = 0x00;
  97. /* write twice, because during initialisation the first setting
  98. * is just for testing SPI communication, and the second is the
  99. * "real" configuration
  100. */
  101. ret = mc33880_write_config(mc);
  102. mc->port_config = 0x00;
  103. if (!ret)
  104. ret = mc33880_write_config(mc);
  105. if (ret) {
  106. dev_err(&spi->dev, "Failed writing to " DRIVER_NAME ": %d\n",
  107. ret);
  108. goto exit_destroy;
  109. }
  110. ret = gpiochip_add(&mc->chip);
  111. if (ret)
  112. goto exit_destroy;
  113. return ret;
  114. exit_destroy:
  115. mutex_destroy(&mc->lock);
  116. return ret;
  117. }
  118. static int mc33880_remove(struct spi_device *spi)
  119. {
  120. struct mc33880 *mc;
  121. mc = spi_get_drvdata(spi);
  122. if (!mc)
  123. return -ENODEV;
  124. gpiochip_remove(&mc->chip);
  125. mutex_destroy(&mc->lock);
  126. return 0;
  127. }
  128. static struct spi_driver mc33880_driver = {
  129. .driver = {
  130. .name = DRIVER_NAME,
  131. .owner = THIS_MODULE,
  132. },
  133. .probe = mc33880_probe,
  134. .remove = mc33880_remove,
  135. };
  136. static int __init mc33880_init(void)
  137. {
  138. return spi_register_driver(&mc33880_driver);
  139. }
  140. /* register after spi postcore initcall and before
  141. * subsys initcalls that may rely on these GPIOs
  142. */
  143. subsys_initcall(mc33880_init);
  144. static void __exit mc33880_exit(void)
  145. {
  146. spi_unregister_driver(&mc33880_driver);
  147. }
  148. module_exit(mc33880_exit);
  149. MODULE_AUTHOR("Mocean Laboratories <info@mocean-labs.com>");
  150. MODULE_LICENSE("GPL v2");