max730x.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. #include <linux/slab.h>
  41. /*
  42. * Pin configurations, see MAX7301 datasheet page 6
  43. */
  44. #define PIN_CONFIG_MASK 0x03
  45. #define PIN_CONFIG_IN_PULLUP 0x03
  46. #define PIN_CONFIG_IN_WO_PULLUP 0x02
  47. #define PIN_CONFIG_OUT 0x01
  48. #define PIN_NUMBER 28
  49. static int max7301_direction_input(struct gpio_chip *chip, unsigned offset)
  50. {
  51. struct max7301 *ts = container_of(chip, struct max7301, chip);
  52. u8 *config;
  53. u8 offset_bits;
  54. int ret;
  55. /* First 4 pins are unused in the controller */
  56. offset += 4;
  57. offset_bits = (offset & 3) << 1;
  58. config = &ts->port_config[offset >> 2];
  59. mutex_lock(&ts->lock);
  60. /* Standard GPIO API doesn't support pull-ups, has to be extended.
  61. * Hard-coding no pollup for now. */
  62. *config = (*config & ~(PIN_CONFIG_MASK << offset_bits))
  63. | (PIN_CONFIG_IN_WO_PULLUP << 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 __devinit __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->platform_data;
  135. if (!pdata || !pdata->base) {
  136. dev_err(dev, "incorrect or missing platform data\n");
  137. return -EINVAL;
  138. }
  139. mutex_init(&ts->lock);
  140. dev_set_drvdata(dev, ts);
  141. /* Power up the chip and disable IRQ output */
  142. ts->write(dev, 0x04, 0x01);
  143. ts->chip.label = dev->driver->name;
  144. ts->chip.direction_input = max7301_direction_input;
  145. ts->chip.get = max7301_get;
  146. ts->chip.direction_output = max7301_direction_output;
  147. ts->chip.set = max7301_set;
  148. ts->chip.base = pdata->base;
  149. ts->chip.ngpio = PIN_NUMBER;
  150. ts->chip.can_sleep = 1;
  151. ts->chip.dev = dev;
  152. ts->chip.owner = THIS_MODULE;
  153. /*
  154. * tristate all pins in hardware and cache the
  155. * register values for later use.
  156. */
  157. for (i = 1; i < 8; i++) {
  158. int j;
  159. /* 0xAA means input with internal pullup disabled */
  160. ts->write(dev, 0x08 + i, 0xAA);
  161. ts->port_config[i] = 0xAA;
  162. for (j = 0; j < 4; j++) {
  163. int offset = (i - 1) * 4 + j;
  164. ret = max7301_direction_input(&ts->chip, offset);
  165. if (ret)
  166. goto exit_destroy;
  167. }
  168. }
  169. ret = gpiochip_add(&ts->chip);
  170. if (ret)
  171. goto exit_destroy;
  172. return ret;
  173. exit_destroy:
  174. dev_set_drvdata(dev, NULL);
  175. mutex_destroy(&ts->lock);
  176. return ret;
  177. }
  178. EXPORT_SYMBOL_GPL(__max730x_probe);
  179. int __devexit __max730x_remove(struct device *dev)
  180. {
  181. struct max7301 *ts = dev_get_drvdata(dev);
  182. int ret;
  183. if (ts == NULL)
  184. return -ENODEV;
  185. dev_set_drvdata(dev, NULL);
  186. /* Power down the chip and disable IRQ output */
  187. ts->write(dev, 0x04, 0x00);
  188. ret = gpiochip_remove(&ts->chip);
  189. if (!ret) {
  190. mutex_destroy(&ts->lock);
  191. kfree(ts);
  192. } else
  193. dev_err(dev, "Failed to remove GPIO controller: %d\n", ret);
  194. return ret;
  195. }
  196. EXPORT_SYMBOL_GPL(__max730x_remove);
  197. MODULE_AUTHOR("Juergen Beisert, Wolfram Sang");
  198. MODULE_LICENSE("GPL v2");
  199. MODULE_DESCRIPTION("MAX730x GPIO-Expanders, generic parts");