gpio-max730x.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /**
  3. * Copyright (C) 2006 Juergen Beisert, Pengutronix
  4. * Copyright (C) 2008 Guennadi Liakhovetski, Pengutronix
  5. * Copyright (C) 2009 Wolfram Sang, Pengutronix
  6. *
  7. * The Maxim MAX7300/1 device is an I2C/SPI driven GPIO expander. There are
  8. * 28 GPIOs. 8 of them can trigger an interrupt. See datasheet for more
  9. * details
  10. * Note:
  11. * - DIN must be stable at the rising edge of clock.
  12. * - when writing:
  13. * - always clock in 16 clocks at once
  14. * - at DIN: D15 first, D0 last
  15. * - D0..D7 = databyte, D8..D14 = commandbyte
  16. * - D15 = low -> write command
  17. * - when reading
  18. * - always clock in 16 clocks at once
  19. * - at DIN: D15 first, D0 last
  20. * - D0..D7 = dummy, D8..D14 = register address
  21. * - D15 = high -> read command
  22. * - raise CS and assert it again
  23. * - always clock in 16 clocks at once
  24. * - at DOUT: D15 first, D0 last
  25. * - D0..D7 contains the data from the first cycle
  26. *
  27. * The driver exports a standard gpiochip interface
  28. */
  29. #include <linux/module.h>
  30. #include <linux/init.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/mutex.h>
  33. #include <linux/spi/max7301.h>
  34. #include <linux/gpio/driver.h>
  35. #include <linux/slab.h>
  36. /*
  37. * Pin configurations, see MAX7301 datasheet page 6
  38. */
  39. #define PIN_CONFIG_MASK 0x03
  40. #define PIN_CONFIG_IN_PULLUP 0x03
  41. #define PIN_CONFIG_IN_WO_PULLUP 0x02
  42. #define PIN_CONFIG_OUT 0x01
  43. #define PIN_NUMBER 28
  44. static int max7301_direction_input(struct gpio_chip *chip, unsigned offset)
  45. {
  46. struct max7301 *ts = container_of(chip, struct max7301, chip);
  47. u8 *config;
  48. u8 offset_bits, pin_config;
  49. int ret;
  50. /* First 4 pins are unused in the controller */
  51. offset += 4;
  52. offset_bits = (offset & 3) << 1;
  53. config = &ts->port_config[offset >> 2];
  54. if (ts->input_pullup_active & BIT(offset))
  55. pin_config = PIN_CONFIG_IN_PULLUP;
  56. else
  57. pin_config = PIN_CONFIG_IN_WO_PULLUP;
  58. mutex_lock(&ts->lock);
  59. *config = (*config & ~(PIN_CONFIG_MASK << offset_bits))
  60. | (pin_config << offset_bits);
  61. ret = ts->write(ts->dev, 0x08 + (offset >> 2), *config);
  62. mutex_unlock(&ts->lock);
  63. return ret;
  64. }
  65. static int __max7301_set(struct max7301 *ts, unsigned offset, int value)
  66. {
  67. if (value) {
  68. ts->out_level |= 1 << offset;
  69. return ts->write(ts->dev, 0x20 + offset, 0x01);
  70. } else {
  71. ts->out_level &= ~(1 << offset);
  72. return ts->write(ts->dev, 0x20 + offset, 0x00);
  73. }
  74. }
  75. static int max7301_direction_output(struct gpio_chip *chip, unsigned offset,
  76. int value)
  77. {
  78. struct max7301 *ts = container_of(chip, struct max7301, chip);
  79. u8 *config;
  80. u8 offset_bits;
  81. int ret;
  82. /* First 4 pins are unused in the controller */
  83. offset += 4;
  84. offset_bits = (offset & 3) << 1;
  85. config = &ts->port_config[offset >> 2];
  86. mutex_lock(&ts->lock);
  87. *config = (*config & ~(PIN_CONFIG_MASK << offset_bits))
  88. | (PIN_CONFIG_OUT << offset_bits);
  89. ret = __max7301_set(ts, offset, value);
  90. if (!ret)
  91. ret = ts->write(ts->dev, 0x08 + (offset >> 2), *config);
  92. mutex_unlock(&ts->lock);
  93. return ret;
  94. }
  95. static int max7301_get(struct gpio_chip *chip, unsigned offset)
  96. {
  97. struct max7301 *ts = gpiochip_get_data(chip);
  98. int config, level = -EINVAL;
  99. /* First 4 pins are unused in the controller */
  100. offset += 4;
  101. mutex_lock(&ts->lock);
  102. config = (ts->port_config[offset >> 2] >> ((offset & 3) << 1))
  103. & PIN_CONFIG_MASK;
  104. switch (config) {
  105. case PIN_CONFIG_OUT:
  106. /* Output: return cached level */
  107. level = !!(ts->out_level & (1 << offset));
  108. break;
  109. case PIN_CONFIG_IN_WO_PULLUP:
  110. case PIN_CONFIG_IN_PULLUP:
  111. /* Input: read out */
  112. level = ts->read(ts->dev, 0x20 + offset) & 0x01;
  113. }
  114. mutex_unlock(&ts->lock);
  115. return level;
  116. }
  117. static void max7301_set(struct gpio_chip *chip, unsigned offset, int value)
  118. {
  119. struct max7301 *ts = gpiochip_get_data(chip);
  120. /* First 4 pins are unused in the controller */
  121. offset += 4;
  122. mutex_lock(&ts->lock);
  123. __max7301_set(ts, offset, value);
  124. mutex_unlock(&ts->lock);
  125. }
  126. int __max730x_probe(struct max7301 *ts)
  127. {
  128. struct device *dev = ts->dev;
  129. struct max7301_platform_data *pdata;
  130. int i, ret;
  131. pdata = dev_get_platdata(dev);
  132. mutex_init(&ts->lock);
  133. dev_set_drvdata(dev, ts);
  134. /* Power up the chip and disable IRQ output */
  135. ts->write(dev, 0x04, 0x01);
  136. if (pdata) {
  137. ts->input_pullup_active = pdata->input_pullup_active;
  138. ts->chip.base = pdata->base;
  139. } else {
  140. ts->chip.base = -1;
  141. }
  142. ts->chip.label = dev->driver->name;
  143. ts->chip.direction_input = max7301_direction_input;
  144. ts->chip.get = max7301_get;
  145. ts->chip.direction_output = max7301_direction_output;
  146. ts->chip.set = max7301_set;
  147. ts->chip.ngpio = PIN_NUMBER;
  148. ts->chip.can_sleep = true;
  149. ts->chip.parent = dev;
  150. ts->chip.owner = THIS_MODULE;
  151. /*
  152. * initialize pullups according to platform data and cache the
  153. * register values for later use.
  154. */
  155. for (i = 1; i < 8; i++) {
  156. int j;
  157. /*
  158. * initialize port_config with "0xAA", which means
  159. * input with internal pullup disabled. This is needed
  160. * to avoid writing zeros (in the inner for loop),
  161. * which is not allowed according to the datasheet.
  162. */
  163. ts->port_config[i] = 0xAA;
  164. for (j = 0; j < 4; j++) {
  165. int offset = (i - 1) * 4 + j;
  166. ret = max7301_direction_input(&ts->chip, offset);
  167. if (ret)
  168. goto exit_destroy;
  169. }
  170. }
  171. ret = gpiochip_add_data(&ts->chip, ts);
  172. if (!ret)
  173. return ret;
  174. exit_destroy:
  175. mutex_destroy(&ts->lock);
  176. return ret;
  177. }
  178. EXPORT_SYMBOL_GPL(__max730x_probe);
  179. int __max730x_remove(struct device *dev)
  180. {
  181. struct max7301 *ts = dev_get_drvdata(dev);
  182. if (ts == NULL)
  183. return -ENODEV;
  184. /* Power down the chip and disable IRQ output */
  185. ts->write(dev, 0x04, 0x00);
  186. gpiochip_remove(&ts->chip);
  187. mutex_destroy(&ts->lock);
  188. return 0;
  189. }
  190. EXPORT_SYMBOL_GPL(__max730x_remove);
  191. MODULE_AUTHOR("Juergen Beisert, Wolfram Sang");
  192. MODULE_LICENSE("GPL v2");
  193. MODULE_DESCRIPTION("MAX730x GPIO-Expanders, generic parts");