mc33880.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * mc33880.c 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. #define DRIVER_NAME "mc33880"
  28. /*
  29. * Pin configurations, see MAX7301 datasheet page 6
  30. */
  31. #define PIN_CONFIG_MASK 0x03
  32. #define PIN_CONFIG_IN_PULLUP 0x03
  33. #define PIN_CONFIG_IN_WO_PULLUP 0x02
  34. #define PIN_CONFIG_OUT 0x01
  35. #define PIN_NUMBER 8
  36. /*
  37. * Some registers must be read back to modify.
  38. * To save time we cache them here in memory
  39. */
  40. struct mc33880 {
  41. struct mutex lock; /* protect from simultaneous accesses */
  42. u8 port_config;
  43. struct gpio_chip chip;
  44. struct spi_device *spi;
  45. };
  46. static int mc33880_write_config(struct mc33880 *mc)
  47. {
  48. return spi_write(mc->spi, &mc->port_config, sizeof(mc->port_config));
  49. }
  50. static int __mc33880_set(struct mc33880 *mc, unsigned offset, int value)
  51. {
  52. if (value)
  53. mc->port_config |= 1 << offset;
  54. else
  55. mc->port_config &= ~(1 << offset);
  56. return mc33880_write_config(mc);
  57. }
  58. static void mc33880_set(struct gpio_chip *chip, unsigned offset, int value)
  59. {
  60. struct mc33880 *mc = container_of(chip, struct mc33880, chip);
  61. mutex_lock(&mc->lock);
  62. __mc33880_set(mc, offset, value);
  63. mutex_unlock(&mc->lock);
  64. }
  65. static int __devinit mc33880_probe(struct spi_device *spi)
  66. {
  67. struct mc33880 *mc;
  68. struct mc33880_platform_data *pdata;
  69. int ret;
  70. pdata = spi->dev.platform_data;
  71. if (!pdata || !pdata->base) {
  72. dev_dbg(&spi->dev, "incorrect or missing platform data\n");
  73. return -EINVAL;
  74. }
  75. /*
  76. * bits_per_word cannot be configured in platform data
  77. */
  78. spi->bits_per_word = 8;
  79. ret = spi_setup(spi);
  80. if (ret < 0)
  81. return ret;
  82. mc = kzalloc(sizeof(struct mc33880), GFP_KERNEL);
  83. if (!mc)
  84. return -ENOMEM;
  85. mutex_init(&mc->lock);
  86. dev_set_drvdata(&spi->dev, mc);
  87. mc->spi = spi;
  88. mc->chip.label = DRIVER_NAME,
  89. mc->chip.set = mc33880_set;
  90. mc->chip.base = pdata->base;
  91. mc->chip.ngpio = PIN_NUMBER;
  92. mc->chip.can_sleep = 1;
  93. mc->chip.dev = &spi->dev;
  94. mc->chip.owner = THIS_MODULE;
  95. mc->port_config = 0x00;
  96. /* write twice, because during initialisation the first setting
  97. * is just for testing SPI communication, and the second is the
  98. * "real" configuration
  99. */
  100. ret = mc33880_write_config(mc);
  101. mc->port_config = 0x00;
  102. if (!ret)
  103. ret = mc33880_write_config(mc);
  104. if (ret) {
  105. printk(KERN_ERR "Failed writing to " DRIVER_NAME ": %d\n", ret);
  106. goto exit_destroy;
  107. }
  108. ret = gpiochip_add(&mc->chip);
  109. if (ret)
  110. goto exit_destroy;
  111. return ret;
  112. exit_destroy:
  113. dev_set_drvdata(&spi->dev, NULL);
  114. mutex_destroy(&mc->lock);
  115. kfree(mc);
  116. return ret;
  117. }
  118. static int __devexit mc33880_remove(struct spi_device *spi)
  119. {
  120. struct mc33880 *mc;
  121. int ret;
  122. mc = dev_get_drvdata(&spi->dev);
  123. if (mc == NULL)
  124. return -ENODEV;
  125. dev_set_drvdata(&spi->dev, NULL);
  126. ret = gpiochip_remove(&mc->chip);
  127. if (!ret) {
  128. mutex_destroy(&mc->lock);
  129. kfree(mc);
  130. } else
  131. dev_err(&spi->dev, "Failed to remove the GPIO controller: %d\n",
  132. ret);
  133. return ret;
  134. }
  135. static struct spi_driver mc33880_driver = {
  136. .driver = {
  137. .name = DRIVER_NAME,
  138. .owner = THIS_MODULE,
  139. },
  140. .probe = mc33880_probe,
  141. .remove = __devexit_p(mc33880_remove),
  142. };
  143. static int __init mc33880_init(void)
  144. {
  145. return spi_register_driver(&mc33880_driver);
  146. }
  147. /* register after spi postcore initcall and before
  148. * subsys initcalls that may rely on these GPIOs
  149. */
  150. subsys_initcall(mc33880_init);
  151. static void __exit mc33880_exit(void)
  152. {
  153. spi_unregister_driver(&mc33880_driver);
  154. }
  155. module_exit(mc33880_exit);
  156. MODULE_AUTHOR("Mocean Laboratories <info@mocean-labs.com>");
  157. MODULE_LICENSE("GPL v2");