max730x.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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, pin_config;
  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. if (ts->input_pullup_active & BIT(offset))
  60. pin_config = PIN_CONFIG_IN_PULLUP;
  61. else
  62. pin_config = PIN_CONFIG_IN_WO_PULLUP;
  63. mutex_lock(&ts->lock);
  64. *config = (*config & ~(PIN_CONFIG_MASK << offset_bits))
  65. | (pin_config << offset_bits);
  66. ret = ts->write(ts->dev, 0x08 + (offset >> 2), *config);
  67. mutex_unlock(&ts->lock);
  68. return ret;
  69. }
  70. static int __max7301_set(struct max7301 *ts, unsigned offset, int value)
  71. {
  72. if (value) {
  73. ts->out_level |= 1 << offset;
  74. return ts->write(ts->dev, 0x20 + offset, 0x01);
  75. } else {
  76. ts->out_level &= ~(1 << offset);
  77. return ts->write(ts->dev, 0x20 + offset, 0x00);
  78. }
  79. }
  80. static int max7301_direction_output(struct gpio_chip *chip, unsigned offset,
  81. int value)
  82. {
  83. struct max7301 *ts = container_of(chip, struct max7301, chip);
  84. u8 *config;
  85. u8 offset_bits;
  86. int ret;
  87. /* First 4 pins are unused in the controller */
  88. offset += 4;
  89. offset_bits = (offset & 3) << 1;
  90. config = &ts->port_config[offset >> 2];
  91. mutex_lock(&ts->lock);
  92. *config = (*config & ~(PIN_CONFIG_MASK << offset_bits))
  93. | (PIN_CONFIG_OUT << offset_bits);
  94. ret = __max7301_set(ts, offset, value);
  95. if (!ret)
  96. ret = ts->write(ts->dev, 0x08 + (offset >> 2), *config);
  97. mutex_unlock(&ts->lock);
  98. return ret;
  99. }
  100. static int max7301_get(struct gpio_chip *chip, unsigned offset)
  101. {
  102. struct max7301 *ts = container_of(chip, struct max7301, chip);
  103. int config, level = -EINVAL;
  104. /* First 4 pins are unused in the controller */
  105. offset += 4;
  106. mutex_lock(&ts->lock);
  107. config = (ts->port_config[offset >> 2] >> ((offset & 3) << 1))
  108. & PIN_CONFIG_MASK;
  109. switch (config) {
  110. case PIN_CONFIG_OUT:
  111. /* Output: return cached level */
  112. level = !!(ts->out_level & (1 << offset));
  113. break;
  114. case PIN_CONFIG_IN_WO_PULLUP:
  115. case PIN_CONFIG_IN_PULLUP:
  116. /* Input: read out */
  117. level = ts->read(ts->dev, 0x20 + offset) & 0x01;
  118. }
  119. mutex_unlock(&ts->lock);
  120. return level;
  121. }
  122. static void max7301_set(struct gpio_chip *chip, unsigned offset, int value)
  123. {
  124. struct max7301 *ts = container_of(chip, struct max7301, chip);
  125. /* First 4 pins are unused in the controller */
  126. offset += 4;
  127. mutex_lock(&ts->lock);
  128. __max7301_set(ts, offset, value);
  129. mutex_unlock(&ts->lock);
  130. }
  131. int __devinit __max730x_probe(struct max7301 *ts)
  132. {
  133. struct device *dev = ts->dev;
  134. struct max7301_platform_data *pdata;
  135. int i, ret;
  136. pdata = dev->platform_data;
  137. if (!pdata || !pdata->base) {
  138. dev_err(dev, "incorrect or missing platform data\n");
  139. return -EINVAL;
  140. }
  141. mutex_init(&ts->lock);
  142. dev_set_drvdata(dev, ts);
  143. /* Power up the chip and disable IRQ output */
  144. ts->write(dev, 0x04, 0x01);
  145. ts->input_pullup_active = pdata->input_pullup_active;
  146. ts->chip.label = dev->driver->name;
  147. ts->chip.direction_input = max7301_direction_input;
  148. ts->chip.get = max7301_get;
  149. ts->chip.direction_output = max7301_direction_output;
  150. ts->chip.set = max7301_set;
  151. ts->chip.base = pdata->base;
  152. ts->chip.ngpio = PIN_NUMBER;
  153. ts->chip.can_sleep = 1;
  154. ts->chip.dev = dev;
  155. ts->chip.owner = THIS_MODULE;
  156. /*
  157. * initialize pullups according to platform data and cache the
  158. * register values for later use.
  159. */
  160. for (i = 1; i < 8; i++) {
  161. int j;
  162. /*
  163. * initialize port_config with "0xAA", which means
  164. * input with internal pullup disabled. This is needed
  165. * to avoid writing zeros (in the inner for loop),
  166. * which is not allowed according to the datasheet.
  167. */
  168. ts->port_config[i] = 0xAA;
  169. for (j = 0; j < 4; j++) {
  170. int offset = (i - 1) * 4 + j;
  171. ret = max7301_direction_input(&ts->chip, offset);
  172. if (ret)
  173. goto exit_destroy;
  174. }
  175. }
  176. ret = gpiochip_add(&ts->chip);
  177. if (ret)
  178. goto exit_destroy;
  179. return ret;
  180. exit_destroy:
  181. dev_set_drvdata(dev, NULL);
  182. mutex_destroy(&ts->lock);
  183. return ret;
  184. }
  185. EXPORT_SYMBOL_GPL(__max730x_probe);
  186. int __devexit __max730x_remove(struct device *dev)
  187. {
  188. struct max7301 *ts = dev_get_drvdata(dev);
  189. int ret;
  190. if (ts == NULL)
  191. return -ENODEV;
  192. dev_set_drvdata(dev, NULL);
  193. /* Power down the chip and disable IRQ output */
  194. ts->write(dev, 0x04, 0x00);
  195. ret = gpiochip_remove(&ts->chip);
  196. if (!ret) {
  197. mutex_destroy(&ts->lock);
  198. kfree(ts);
  199. } else
  200. dev_err(dev, "Failed to remove GPIO controller: %d\n", ret);
  201. return ret;
  202. }
  203. EXPORT_SYMBOL_GPL(__max730x_remove);
  204. MODULE_AUTHOR("Juergen Beisert, Wolfram Sang");
  205. MODULE_LICENSE("GPL v2");
  206. MODULE_DESCRIPTION("MAX730x GPIO-Expanders, generic parts");