ad7879-spi.c 4.4 KB

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