ad7879-spi.c 3.6 KB

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