st_sensors_spi.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * STMicroelectronics sensors spi library driver
  3. *
  4. * Copyright 2012-2013 STMicroelectronics Inc.
  5. *
  6. * Denis Ciocca <denis.ciocca@st.com>
  7. *
  8. * Licensed under the GPL-2.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/slab.h>
  13. #include <linux/iio/iio.h>
  14. #include <linux/iio/common/st_sensors_spi.h>
  15. #define ST_SENSORS_SPI_MULTIREAD 0xc0
  16. #define ST_SENSORS_SPI_READ 0x80
  17. static unsigned int st_sensors_spi_get_irq(struct iio_dev *indio_dev)
  18. {
  19. struct st_sensor_data *sdata = iio_priv(indio_dev);
  20. return to_spi_device(sdata->dev)->irq;
  21. }
  22. static int st_sensors_spi_read(struct st_sensor_transfer_buffer *tb,
  23. struct device *dev, u8 reg_addr, int len, u8 *data, bool multiread_bit)
  24. {
  25. int err;
  26. struct spi_transfer xfers[] = {
  27. {
  28. .tx_buf = tb->tx_buf,
  29. .bits_per_word = 8,
  30. .len = 1,
  31. },
  32. {
  33. .rx_buf = tb->rx_buf,
  34. .bits_per_word = 8,
  35. .len = len,
  36. }
  37. };
  38. mutex_lock(&tb->buf_lock);
  39. if ((multiread_bit) && (len > 1))
  40. tb->tx_buf[0] = reg_addr | ST_SENSORS_SPI_MULTIREAD;
  41. else
  42. tb->tx_buf[0] = reg_addr | ST_SENSORS_SPI_READ;
  43. err = spi_sync_transfer(to_spi_device(dev), xfers, ARRAY_SIZE(xfers));
  44. if (err)
  45. goto acc_spi_read_error;
  46. memcpy(data, tb->rx_buf, len);
  47. mutex_unlock(&tb->buf_lock);
  48. return len;
  49. acc_spi_read_error:
  50. mutex_unlock(&tb->buf_lock);
  51. return err;
  52. }
  53. static int st_sensors_spi_read_byte(struct st_sensor_transfer_buffer *tb,
  54. struct device *dev, u8 reg_addr, u8 *res_byte)
  55. {
  56. return st_sensors_spi_read(tb, dev, reg_addr, 1, res_byte, false);
  57. }
  58. static int st_sensors_spi_read_multiple_byte(
  59. struct st_sensor_transfer_buffer *tb, struct device *dev,
  60. u8 reg_addr, int len, u8 *data, bool multiread_bit)
  61. {
  62. return st_sensors_spi_read(tb, dev, reg_addr, len, data, multiread_bit);
  63. }
  64. static int st_sensors_spi_write_byte(struct st_sensor_transfer_buffer *tb,
  65. struct device *dev, u8 reg_addr, u8 data)
  66. {
  67. int err;
  68. struct spi_transfer xfers = {
  69. .tx_buf = tb->tx_buf,
  70. .bits_per_word = 8,
  71. .len = 2,
  72. };
  73. mutex_lock(&tb->buf_lock);
  74. tb->tx_buf[0] = reg_addr;
  75. tb->tx_buf[1] = data;
  76. err = spi_sync_transfer(to_spi_device(dev), &xfers, 1);
  77. mutex_unlock(&tb->buf_lock);
  78. return err;
  79. }
  80. static const struct st_sensor_transfer_function st_sensors_tf_spi = {
  81. .read_byte = st_sensors_spi_read_byte,
  82. .write_byte = st_sensors_spi_write_byte,
  83. .read_multiple_byte = st_sensors_spi_read_multiple_byte,
  84. };
  85. void st_sensors_spi_configure(struct iio_dev *indio_dev,
  86. struct spi_device *spi, struct st_sensor_data *sdata)
  87. {
  88. spi_set_drvdata(spi, indio_dev);
  89. indio_dev->dev.parent = &spi->dev;
  90. indio_dev->name = spi->modalias;
  91. sdata->dev = &spi->dev;
  92. sdata->tf = &st_sensors_tf_spi;
  93. sdata->get_irq_data_ready = st_sensors_spi_get_irq;
  94. }
  95. EXPORT_SYMBOL(st_sensors_spi_configure);
  96. MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
  97. MODULE_DESCRIPTION("STMicroelectronics ST-sensors spi driver");
  98. MODULE_LICENSE("GPL v2");