adxl34x-spi.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * ADLX345/346 Three-Axis Digital Accelerometers (SPI Interface)
  3. *
  4. * Enter bugs at http://blackfin.uclinux.org/
  5. *
  6. * Copyright (C) 2009 Michael Hennerich, Analog Devices Inc.
  7. * Licensed under the GPL-2 or later.
  8. */
  9. #include <linux/input.h> /* BUS_SPI */
  10. #include <linux/module.h>
  11. #include <linux/spi/spi.h>
  12. #include <linux/pm.h>
  13. #include <linux/types.h>
  14. #include "adxl34x.h"
  15. #define MAX_SPI_FREQ_HZ 5000000
  16. #define MAX_FREQ_NO_FIFODELAY 1500000
  17. #define ADXL34X_CMD_MULTB (1 << 6)
  18. #define ADXL34X_CMD_READ (1 << 7)
  19. #define ADXL34X_WRITECMD(reg) (reg & 0x3F)
  20. #define ADXL34X_READCMD(reg) (ADXL34X_CMD_READ | (reg & 0x3F))
  21. #define ADXL34X_READMB_CMD(reg) (ADXL34X_CMD_READ | ADXL34X_CMD_MULTB \
  22. | (reg & 0x3F))
  23. static int adxl34x_spi_read(struct device *dev, unsigned char reg)
  24. {
  25. struct spi_device *spi = to_spi_device(dev);
  26. unsigned char cmd;
  27. cmd = ADXL34X_READCMD(reg);
  28. return spi_w8r8(spi, cmd);
  29. }
  30. static int adxl34x_spi_write(struct device *dev,
  31. unsigned char reg, unsigned char val)
  32. {
  33. struct spi_device *spi = to_spi_device(dev);
  34. unsigned char buf[2];
  35. buf[0] = ADXL34X_WRITECMD(reg);
  36. buf[1] = val;
  37. return spi_write(spi, buf, sizeof(buf));
  38. }
  39. static int adxl34x_spi_read_block(struct device *dev,
  40. unsigned char reg, int count,
  41. void *buf)
  42. {
  43. struct spi_device *spi = to_spi_device(dev);
  44. ssize_t status;
  45. reg = ADXL34X_READMB_CMD(reg);
  46. status = spi_write_then_read(spi, &reg, 1, buf, count);
  47. return (status < 0) ? status : 0;
  48. }
  49. static const struct adxl34x_bus_ops adxl34x_spi_bops = {
  50. .bustype = BUS_SPI,
  51. .write = adxl34x_spi_write,
  52. .read = adxl34x_spi_read,
  53. .read_block = adxl34x_spi_read_block,
  54. };
  55. static int adxl34x_spi_probe(struct spi_device *spi)
  56. {
  57. struct adxl34x *ac;
  58. /* don't exceed max specified SPI CLK frequency */
  59. if (spi->max_speed_hz > MAX_SPI_FREQ_HZ) {
  60. dev_err(&spi->dev, "SPI CLK %d Hz too fast\n", spi->max_speed_hz);
  61. return -EINVAL;
  62. }
  63. ac = adxl34x_probe(&spi->dev, spi->irq,
  64. spi->max_speed_hz > MAX_FREQ_NO_FIFODELAY,
  65. &adxl34x_spi_bops);
  66. if (IS_ERR(ac))
  67. return PTR_ERR(ac);
  68. spi_set_drvdata(spi, ac);
  69. return 0;
  70. }
  71. static int adxl34x_spi_remove(struct spi_device *spi)
  72. {
  73. struct adxl34x *ac = spi_get_drvdata(spi);
  74. return adxl34x_remove(ac);
  75. }
  76. static int __maybe_unused adxl34x_spi_suspend(struct device *dev)
  77. {
  78. struct spi_device *spi = to_spi_device(dev);
  79. struct adxl34x *ac = spi_get_drvdata(spi);
  80. adxl34x_suspend(ac);
  81. return 0;
  82. }
  83. static int __maybe_unused adxl34x_spi_resume(struct device *dev)
  84. {
  85. struct spi_device *spi = to_spi_device(dev);
  86. struct adxl34x *ac = spi_get_drvdata(spi);
  87. adxl34x_resume(ac);
  88. return 0;
  89. }
  90. static SIMPLE_DEV_PM_OPS(adxl34x_spi_pm, adxl34x_spi_suspend,
  91. adxl34x_spi_resume);
  92. static struct spi_driver adxl34x_driver = {
  93. .driver = {
  94. .name = "adxl34x",
  95. .pm = &adxl34x_spi_pm,
  96. },
  97. .probe = adxl34x_spi_probe,
  98. .remove = adxl34x_spi_remove,
  99. };
  100. module_spi_driver(adxl34x_driver);
  101. MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
  102. MODULE_DESCRIPTION("ADXL345/346 Three-Axis Digital Accelerometer SPI Bus Driver");
  103. MODULE_LICENSE("GPL");