gpio-mc33880.c 3.5 KB

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