ad7879-spi.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * AD7879/AD7889 touchscreen (SPI bus)
  3. *
  4. * Copyright (C) 2008-2010 Michael Hennerich, Analog Devices Inc.
  5. *
  6. * Licensed under the GPL-2 or later.
  7. */
  8. #include <linux/input.h> /* BUS_SPI */
  9. #include <linux/spi/spi.h>
  10. #include "ad7879.h"
  11. #define AD7879_DEVID 0x7A /* AD7879/AD7889 */
  12. #define MAX_SPI_FREQ_HZ 5000000
  13. #define AD7879_CMD_MAGIC 0xE000
  14. #define AD7879_CMD_READ (1 << 10)
  15. #define AD7879_CMD(reg) (AD7879_CMD_MAGIC | ((reg) & 0xF))
  16. #define AD7879_WRITECMD(reg) (AD7879_CMD(reg))
  17. #define AD7879_READCMD(reg) (AD7879_CMD(reg) | AD7879_CMD_READ)
  18. #ifdef CONFIG_PM
  19. static int ad7879_spi_suspend(struct spi_device *spi, pm_message_t message)
  20. {
  21. struct ad7879 *ts = spi_get_drvdata(spi);
  22. ad7879_suspend(ts);
  23. return 0;
  24. }
  25. static int ad7879_spi_resume(struct spi_device *spi)
  26. {
  27. struct ad7879 *ts = spi_get_drvdata(spi);
  28. ad7879_resume(ts);
  29. return 0;
  30. }
  31. #else
  32. # define ad7879_spi_suspend NULL
  33. # define ad7879_spi_resume NULL
  34. #endif
  35. /*
  36. * ad7879_read/write are only used for initial setup and for sysfs controls.
  37. * The main traffic is done in ad7879_collect().
  38. */
  39. static int ad7879_spi_xfer(struct spi_device *spi,
  40. u16 cmd, u8 count, u16 *tx_buf, u16 *rx_buf)
  41. {
  42. struct spi_message msg;
  43. struct spi_transfer *xfers;
  44. void *spi_data;
  45. u16 *command;
  46. u16 *_rx_buf = _rx_buf; /* shut gcc up */
  47. u8 idx;
  48. int ret;
  49. xfers = spi_data = kzalloc(sizeof(*xfers) * (count + 2), GFP_KERNEL);
  50. if (!spi_data)
  51. return -ENOMEM;
  52. spi_message_init(&msg);
  53. command = spi_data;
  54. command[0] = cmd;
  55. if (count == 1) {
  56. /* ad7879_spi_{read,write} gave us buf on stack */
  57. command[1] = *tx_buf;
  58. tx_buf = &command[1];
  59. _rx_buf = rx_buf;
  60. rx_buf = &command[2];
  61. }
  62. ++xfers;
  63. xfers[0].tx_buf = command;
  64. xfers[0].len = 2;
  65. spi_message_add_tail(&xfers[0], &msg);
  66. ++xfers;
  67. for (idx = 0; idx < count; ++idx) {
  68. if (rx_buf)
  69. xfers[idx].rx_buf = &rx_buf[idx];
  70. if (tx_buf)
  71. xfers[idx].tx_buf = &tx_buf[idx];
  72. xfers[idx].len = 2;
  73. spi_message_add_tail(&xfers[idx], &msg);
  74. }
  75. ret = spi_sync(spi, &msg);
  76. if (count == 1)
  77. _rx_buf[0] = command[2];
  78. kfree(spi_data);
  79. return ret;
  80. }
  81. static int ad7879_spi_multi_read(struct device *dev,
  82. u8 first_reg, u8 count, u16 *buf)
  83. {
  84. struct spi_device *spi = to_spi_device(dev);
  85. return ad7879_spi_xfer(spi, AD7879_READCMD(first_reg), count, NULL, buf);
  86. }
  87. static int ad7879_spi_read(struct device *dev, u8 reg)
  88. {
  89. struct spi_device *spi = to_spi_device(dev);
  90. u16 ret, dummy;
  91. return ad7879_spi_xfer(spi, AD7879_READCMD(reg), 1, &dummy, &ret) ? : ret;
  92. }
  93. static int ad7879_spi_write(struct device *dev, u8 reg, u16 val)
  94. {
  95. struct spi_device *spi = to_spi_device(dev);
  96. u16 dummy;
  97. return ad7879_spi_xfer(spi, AD7879_WRITECMD(reg), 1, &val, &dummy);
  98. }
  99. static const struct ad7879_bus_ops ad7879_spi_bus_ops = {
  100. .bustype = BUS_SPI,
  101. .read = ad7879_spi_read,
  102. .multi_read = ad7879_spi_multi_read,
  103. .write = ad7879_spi_write,
  104. };
  105. static int __devinit ad7879_spi_probe(struct spi_device *spi)
  106. {
  107. struct ad7879 *ts;
  108. int err;
  109. /* don't exceed max specified SPI CLK frequency */
  110. if (spi->max_speed_hz > MAX_SPI_FREQ_HZ) {
  111. dev_err(&spi->dev, "SPI CLK %d Hz?\n", spi->max_speed_hz);
  112. return -EINVAL;
  113. }
  114. spi->bits_per_word = 16;
  115. err = spi_setup(spi);
  116. if (err) {
  117. dev_dbg(&spi->dev, "spi master doesn't support 16 bits/word\n");
  118. return err;
  119. }
  120. ts = ad7879_probe(&spi->dev, AD7879_DEVID, spi->irq, &ad7879_spi_bus_ops);
  121. if (IS_ERR(ts))
  122. return PTR_ERR(ts);
  123. spi_set_drvdata(spi, ts);
  124. return 0;
  125. }
  126. static int __devexit ad7879_spi_remove(struct spi_device *spi)
  127. {
  128. struct ad7879 *ts = spi_get_drvdata(spi);
  129. ad7879_remove(ts);
  130. spi_set_drvdata(spi, NULL);
  131. return 0;
  132. }
  133. static struct spi_driver ad7879_spi_driver = {
  134. .driver = {
  135. .name = "ad7879",
  136. .bus = &spi_bus_type,
  137. .owner = THIS_MODULE,
  138. },
  139. .probe = ad7879_spi_probe,
  140. .remove = __devexit_p(ad7879_spi_remove),
  141. .suspend = ad7879_spi_suspend,
  142. .resume = ad7879_spi_resume,
  143. };
  144. static int __init ad7879_spi_init(void)
  145. {
  146. return spi_register_driver(&ad7879_spi_driver);
  147. }
  148. module_init(ad7879_spi_init);
  149. static void __exit ad7879_spi_exit(void)
  150. {
  151. spi_unregister_driver(&ad7879_spi_driver);
  152. }
  153. module_exit(ad7879_spi_exit);
  154. MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
  155. MODULE_DESCRIPTION("AD7879(-1) touchscreen SPI bus driver");
  156. MODULE_LICENSE("GPL");
  157. MODULE_ALIAS("spi:ad7879");