max730x.c 5.7 KB

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