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