gpio-max730x.c 5.9 KB

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