max7301.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /**
  2. * drivers/gpio/max7301.c
  3. *
  4. * Copyright (C) 2006 Juergen Beisert, Pengutronix
  5. * Copyright (C) 2008 Guennadi Liakhovetski, Pengutronix
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * The Maxim's MAX7301 device is an SPI driven GPIO expander. There are
  12. * 28 GPIOs. 8 of them can trigger an interrupt. See datasheet for more
  13. * details
  14. * Note:
  15. * - DIN must be stable at the rising edge of clock.
  16. * - when writing:
  17. * - always clock in 16 clocks at once
  18. * - at DIN: D15 first, D0 last
  19. * - D0..D7 = databyte, D8..D14 = commandbyte
  20. * - D15 = low -> write command
  21. * - when reading
  22. * - always clock in 16 clocks at once
  23. * - at DIN: D15 first, D0 last
  24. * - D0..D7 = dummy, D8..D14 = register address
  25. * - D15 = high -> read command
  26. * - raise CS and assert it again
  27. * - always clock in 16 clocks at once
  28. * - at DOUT: D15 first, D0 last
  29. * - D0..D7 contains the data from the first cycle
  30. *
  31. * The driver exports a standard gpiochip interface
  32. */
  33. #include <linux/init.h>
  34. #include <linux/platform_device.h>
  35. #include <linux/mutex.h>
  36. #include <linux/spi/spi.h>
  37. #include <linux/spi/max7301.h>
  38. #include <linux/gpio.h>
  39. #define DRIVER_NAME "max7301"
  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. /*
  49. * Some registers must be read back to modify.
  50. * To save time we cache them here in memory
  51. */
  52. struct max7301 {
  53. struct mutex lock;
  54. u8 port_config[8]; /* field 0 is unused */
  55. u32 out_level; /* cached output levels */
  56. struct gpio_chip chip;
  57. struct spi_device *spi;
  58. };
  59. /**
  60. * max7301_write - Write a new register content
  61. * @spi: The SPI device
  62. * @reg: Register offset
  63. * @val: Value to write
  64. *
  65. * A write to the MAX7301 means one message with one transfer
  66. *
  67. * Returns 0 if successful or a negative value on error
  68. */
  69. static int max7301_write(struct spi_device *spi, unsigned int reg, unsigned int val)
  70. {
  71. u16 word = ((reg & 0x7F) << 8) | (val & 0xFF);
  72. return spi_write(spi, (const u8 *)&word, sizeof(word));
  73. }
  74. /**
  75. * max7301_read - Read back register content
  76. * @spi: The SPI device
  77. * @reg: Register offset
  78. *
  79. * A read from the MAX7301 means two transfers; here, one message each
  80. *
  81. * Returns positive 8 bit value from device if successful or a
  82. * negative value on error
  83. */
  84. static int max7301_read(struct spi_device *spi, unsigned int reg)
  85. {
  86. int ret;
  87. u16 word;
  88. word = 0x8000 | (reg << 8);
  89. ret = spi_write(spi, (const u8 *)&word, sizeof(word));
  90. if (ret)
  91. return ret;
  92. /*
  93. * This relies on the fact, that a transfer with NULL tx_buf shifts out
  94. * zero bytes (=NOOP for MAX7301)
  95. */
  96. ret = spi_read(spi, (u8 *)&word, sizeof(word));
  97. if (ret)
  98. return ret;
  99. return word & 0xff;
  100. }
  101. static int max7301_direction_input(struct gpio_chip *chip, unsigned offset)
  102. {
  103. struct max7301 *ts = container_of(chip, struct max7301, chip);
  104. u8 *config;
  105. int ret;
  106. /* First 4 pins are unused in the controller */
  107. offset += 4;
  108. config = &ts->port_config[offset >> 2];
  109. mutex_lock(&ts->lock);
  110. /* Standard GPIO API doesn't support pull-ups, has to be extended.
  111. * Hard-coding no pollup for now. */
  112. *config = (*config & ~(3 << (offset & 3))) | (1 << (offset & 3));
  113. ret = max7301_write(ts->spi, 0x08 + (offset >> 2), *config);
  114. mutex_unlock(&ts->lock);
  115. return ret;
  116. }
  117. static int __max7301_set(struct max7301 *ts, unsigned offset, int value)
  118. {
  119. if (value) {
  120. ts->out_level |= 1 << offset;
  121. return max7301_write(ts->spi, 0x20 + offset, 0x01);
  122. } else {
  123. ts->out_level &= ~(1 << offset);
  124. return max7301_write(ts->spi, 0x20 + offset, 0x00);
  125. }
  126. }
  127. static int max7301_direction_output(struct gpio_chip *chip, unsigned offset,
  128. int value)
  129. {
  130. struct max7301 *ts = container_of(chip, struct max7301, chip);
  131. u8 *config;
  132. int ret;
  133. /* First 4 pins are unused in the controller */
  134. offset += 4;
  135. config = &ts->port_config[offset >> 2];
  136. mutex_lock(&ts->lock);
  137. *config = (*config & ~(3 << (offset & 3))) | (1 << (offset & 3));
  138. ret = __max7301_set(ts, offset, value);
  139. if (!ret)
  140. ret = max7301_write(ts->spi, 0x08 + (offset >> 2), *config);
  141. mutex_unlock(&ts->lock);
  142. return ret;
  143. }
  144. static int max7301_get(struct gpio_chip *chip, unsigned offset)
  145. {
  146. struct max7301 *ts = container_of(chip, struct max7301, chip);
  147. int config, level = -EINVAL;
  148. /* First 4 pins are unused in the controller */
  149. offset += 4;
  150. mutex_lock(&ts->lock);
  151. config = (ts->port_config[offset >> 2] >> ((offset & 3) * 2)) & 3;
  152. switch (config) {
  153. case 1:
  154. /* Output: return cached level */
  155. level = !!(ts->out_level & (1 << offset));
  156. break;
  157. case 2:
  158. case 3:
  159. /* Input: read out */
  160. level = max7301_read(ts->spi, 0x20 + offset) & 0x01;
  161. }
  162. mutex_unlock(&ts->lock);
  163. return level;
  164. }
  165. static void max7301_set(struct gpio_chip *chip, unsigned offset, int value)
  166. {
  167. struct max7301 *ts = container_of(chip, struct max7301, chip);
  168. /* First 4 pins are unused in the controller */
  169. offset += 4;
  170. mutex_lock(&ts->lock);
  171. __max7301_set(ts, offset, value);
  172. mutex_unlock(&ts->lock);
  173. }
  174. static int __devinit max7301_probe(struct spi_device *spi)
  175. {
  176. struct max7301 *ts;
  177. struct max7301_platform_data *pdata;
  178. int i, ret;
  179. pdata = spi->dev.platform_data;
  180. if (!pdata || !pdata->base) {
  181. dev_dbg(&spi->dev, "incorrect or missing platform data\n");
  182. return -EINVAL;
  183. }
  184. /*
  185. * bits_per_word cannot be configured in platform data
  186. */
  187. spi->bits_per_word = 16;
  188. ret = spi_setup(spi);
  189. if (ret < 0)
  190. return ret;
  191. ts = kzalloc(sizeof(struct max7301), GFP_KERNEL);
  192. if (!ts)
  193. return -ENOMEM;
  194. mutex_init(&ts->lock);
  195. dev_set_drvdata(&spi->dev, ts);
  196. /* Power up the chip and disable IRQ output */
  197. max7301_write(spi, 0x04, 0x01);
  198. ts->spi = spi;
  199. ts->chip.label = DRIVER_NAME,
  200. ts->chip.direction_input = max7301_direction_input;
  201. ts->chip.get = max7301_get;
  202. ts->chip.direction_output = max7301_direction_output;
  203. ts->chip.set = max7301_set;
  204. ts->chip.base = pdata->base;
  205. ts->chip.ngpio = PIN_NUMBER;
  206. ts->chip.can_sleep = 1;
  207. ts->chip.dev = &spi->dev;
  208. ts->chip.owner = THIS_MODULE;
  209. /*
  210. * tristate all pins in hardware and cache the
  211. * register values for later use.
  212. */
  213. for (i = 1; i < 8; i++) {
  214. int j;
  215. /* 0xAA means input with internal pullup disabled */
  216. max7301_write(spi, 0x08 + i, 0xAA);
  217. ts->port_config[i] = 0xAA;
  218. for (j = 0; j < 4; j++) {
  219. int offset = (i - 1) * 4 + j;
  220. ret = max7301_direction_input(&ts->chip, offset);
  221. if (ret)
  222. goto exit_destroy;
  223. }
  224. }
  225. ret = gpiochip_add(&ts->chip);
  226. if (ret)
  227. goto exit_destroy;
  228. return ret;
  229. exit_destroy:
  230. dev_set_drvdata(&spi->dev, NULL);
  231. mutex_destroy(&ts->lock);
  232. kfree(ts);
  233. return ret;
  234. }
  235. static int __devexit max7301_remove(struct spi_device *spi)
  236. {
  237. struct max7301 *ts;
  238. int ret;
  239. ts = dev_get_drvdata(&spi->dev);
  240. if (ts == NULL)
  241. return -ENODEV;
  242. dev_set_drvdata(&spi->dev, NULL);
  243. /* Power down the chip and disable IRQ output */
  244. max7301_write(spi, 0x04, 0x00);
  245. ret = gpiochip_remove(&ts->chip);
  246. if (!ret) {
  247. mutex_destroy(&ts->lock);
  248. kfree(ts);
  249. } else
  250. dev_err(&spi->dev, "Failed to remove the GPIO controller: %d\n",
  251. ret);
  252. return ret;
  253. }
  254. static struct spi_driver max7301_driver = {
  255. .driver = {
  256. .name = DRIVER_NAME,
  257. .owner = THIS_MODULE,
  258. },
  259. .probe = max7301_probe,
  260. .remove = __devexit_p(max7301_remove),
  261. };
  262. static int __init max7301_init(void)
  263. {
  264. return spi_register_driver(&max7301_driver);
  265. }
  266. /* register after spi postcore initcall and before
  267. * subsys initcalls that may rely on these GPIOs
  268. */
  269. subsys_initcall(max7301_init);
  270. static void __exit max7301_exit(void)
  271. {
  272. spi_unregister_driver(&max7301_driver);
  273. }
  274. module_exit(max7301_exit);
  275. MODULE_AUTHOR("Juergen Beisert");
  276. MODULE_LICENSE("GPL v2");
  277. MODULE_DESCRIPTION("MAX7301 SPI based GPIO-Expander");