ad7879-spi.c 3.8 KB

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