mpl115_spi.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Freescale MPL115A1 pressure/temperature sensor
  3. *
  4. * Copyright (c) 2016 Akinobu Mita <akinobu.mita@gmail.com>
  5. *
  6. * This file is subject to the terms and conditions of version 2 of
  7. * the GNU General Public License. See the file COPYING in the main
  8. * directory of this archive for more details.
  9. *
  10. * Datasheet: http://www.nxp.com/files/sensors/doc/data_sheet/MPL115A1.pdf
  11. */
  12. #include <linux/module.h>
  13. #include <linux/spi/spi.h>
  14. #include "mpl115.h"
  15. #define MPL115_SPI_WRITE(address) ((address) << 1)
  16. #define MPL115_SPI_READ(address) (0x80 | (address) << 1)
  17. struct mpl115_spi_buf {
  18. u8 tx[4];
  19. u8 rx[4];
  20. };
  21. static int mpl115_spi_init(struct device *dev)
  22. {
  23. struct spi_device *spi = to_spi_device(dev);
  24. struct mpl115_spi_buf *buf;
  25. buf = devm_kzalloc(dev, sizeof(*buf), GFP_KERNEL);
  26. if (!buf)
  27. return -ENOMEM;
  28. spi_set_drvdata(spi, buf);
  29. return 0;
  30. }
  31. static int mpl115_spi_read(struct device *dev, u8 address)
  32. {
  33. struct spi_device *spi = to_spi_device(dev);
  34. struct mpl115_spi_buf *buf = spi_get_drvdata(spi);
  35. struct spi_transfer xfer = {
  36. .tx_buf = buf->tx,
  37. .rx_buf = buf->rx,
  38. .len = 4,
  39. };
  40. int ret;
  41. buf->tx[0] = MPL115_SPI_READ(address);
  42. buf->tx[2] = MPL115_SPI_READ(address + 1);
  43. ret = spi_sync_transfer(spi, &xfer, 1);
  44. if (ret)
  45. return ret;
  46. return (buf->rx[1] << 8) | buf->rx[3];
  47. }
  48. static int mpl115_spi_write(struct device *dev, u8 address, u8 value)
  49. {
  50. struct spi_device *spi = to_spi_device(dev);
  51. struct mpl115_spi_buf *buf = spi_get_drvdata(spi);
  52. struct spi_transfer xfer = {
  53. .tx_buf = buf->tx,
  54. .len = 2,
  55. };
  56. buf->tx[0] = MPL115_SPI_WRITE(address);
  57. buf->tx[1] = value;
  58. return spi_sync_transfer(spi, &xfer, 1);
  59. }
  60. static const struct mpl115_ops mpl115_spi_ops = {
  61. .init = mpl115_spi_init,
  62. .read = mpl115_spi_read,
  63. .write = mpl115_spi_write,
  64. };
  65. static int mpl115_spi_probe(struct spi_device *spi)
  66. {
  67. const struct spi_device_id *id = spi_get_device_id(spi);
  68. return mpl115_probe(&spi->dev, id->name, &mpl115_spi_ops);
  69. }
  70. static const struct spi_device_id mpl115_spi_ids[] = {
  71. { "mpl115", 0 },
  72. {}
  73. };
  74. MODULE_DEVICE_TABLE(spi, mpl115_spi_ids);
  75. static struct spi_driver mpl115_spi_driver = {
  76. .driver = {
  77. .name = "mpl115",
  78. },
  79. .probe = mpl115_spi_probe,
  80. .id_table = mpl115_spi_ids,
  81. };
  82. module_spi_driver(mpl115_spi_driver);
  83. MODULE_AUTHOR("Akinobu Mita <akinobu.mita@gmail.com>");
  84. MODULE_DESCRIPTION("Freescale MPL115A1 pressure/temperature driver");
  85. MODULE_LICENSE("GPL");