ad714x-spi.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * AD714X CapTouch Programmable Controller driver (SPI bus)
  3. *
  4. * Copyright 2009-2011 Analog Devices Inc.
  5. *
  6. * Licensed under the GPL-2 or later.
  7. */
  8. #include <linux/input.h> /* BUS_SPI */
  9. #include <linux/module.h>
  10. #include <linux/spi/spi.h>
  11. #include <linux/pm.h>
  12. #include <linux/types.h>
  13. #include "ad714x.h"
  14. #define AD714x_SPI_CMD_PREFIX 0xE000 /* bits 15:11 */
  15. #define AD714x_SPI_READ BIT(10)
  16. static int __maybe_unused ad714x_spi_suspend(struct device *dev)
  17. {
  18. return ad714x_disable(spi_get_drvdata(to_spi_device(dev)));
  19. }
  20. static int __maybe_unused ad714x_spi_resume(struct device *dev)
  21. {
  22. return ad714x_enable(spi_get_drvdata(to_spi_device(dev)));
  23. }
  24. static SIMPLE_DEV_PM_OPS(ad714x_spi_pm, ad714x_spi_suspend, ad714x_spi_resume);
  25. static int ad714x_spi_read(struct ad714x_chip *chip,
  26. unsigned short reg, unsigned short *data, size_t len)
  27. {
  28. struct spi_device *spi = to_spi_device(chip->dev);
  29. struct spi_message message;
  30. struct spi_transfer xfer[2];
  31. int i;
  32. int error;
  33. spi_message_init(&message);
  34. memset(xfer, 0, sizeof(xfer));
  35. chip->xfer_buf[0] = cpu_to_be16(AD714x_SPI_CMD_PREFIX |
  36. AD714x_SPI_READ | reg);
  37. xfer[0].tx_buf = &chip->xfer_buf[0];
  38. xfer[0].len = sizeof(chip->xfer_buf[0]);
  39. spi_message_add_tail(&xfer[0], &message);
  40. xfer[1].rx_buf = &chip->xfer_buf[1];
  41. xfer[1].len = sizeof(chip->xfer_buf[1]) * len;
  42. spi_message_add_tail(&xfer[1], &message);
  43. error = spi_sync(spi, &message);
  44. if (unlikely(error)) {
  45. dev_err(chip->dev, "SPI read error: %d\n", error);
  46. return error;
  47. }
  48. for (i = 0; i < len; i++)
  49. data[i] = be16_to_cpu(chip->xfer_buf[i + 1]);
  50. return 0;
  51. }
  52. static int ad714x_spi_write(struct ad714x_chip *chip,
  53. unsigned short reg, unsigned short data)
  54. {
  55. struct spi_device *spi = to_spi_device(chip->dev);
  56. int error;
  57. chip->xfer_buf[0] = cpu_to_be16(AD714x_SPI_CMD_PREFIX | reg);
  58. chip->xfer_buf[1] = cpu_to_be16(data);
  59. error = spi_write(spi, (u8 *)chip->xfer_buf,
  60. 2 * sizeof(*chip->xfer_buf));
  61. if (unlikely(error)) {
  62. dev_err(chip->dev, "SPI write error: %d\n", error);
  63. return error;
  64. }
  65. return 0;
  66. }
  67. static int ad714x_spi_probe(struct spi_device *spi)
  68. {
  69. struct ad714x_chip *chip;
  70. int err;
  71. spi->bits_per_word = 8;
  72. err = spi_setup(spi);
  73. if (err < 0)
  74. return err;
  75. chip = ad714x_probe(&spi->dev, BUS_SPI, spi->irq,
  76. ad714x_spi_read, ad714x_spi_write);
  77. if (IS_ERR(chip))
  78. return PTR_ERR(chip);
  79. spi_set_drvdata(spi, chip);
  80. return 0;
  81. }
  82. static struct spi_driver ad714x_spi_driver = {
  83. .driver = {
  84. .name = "ad714x_captouch",
  85. .pm = &ad714x_spi_pm,
  86. },
  87. .probe = ad714x_spi_probe,
  88. };
  89. module_spi_driver(ad714x_spi_driver);
  90. MODULE_DESCRIPTION("Analog Devices AD714X Capacitance Touch Sensor SPI Bus Driver");
  91. MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
  92. MODULE_LICENSE("GPL");