ms5611_spi.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * MS5611 pressure and temperature sensor driver (SPI bus)
  3. *
  4. * Copyright (c) Tomasz Duszynski <tduszyns@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. */
  11. #include <linux/delay.h>
  12. #include <linux/module.h>
  13. #include <linux/spi/spi.h>
  14. #include "ms5611.h"
  15. static int ms5611_spi_reset(struct device *dev)
  16. {
  17. u8 cmd = MS5611_RESET;
  18. struct ms5611_state *st = iio_priv(dev_to_iio_dev(dev));
  19. return spi_write_then_read(st->client, &cmd, 1, NULL, 0);
  20. }
  21. static int ms5611_spi_read_prom_word(struct device *dev, int index, u16 *word)
  22. {
  23. int ret;
  24. struct ms5611_state *st = iio_priv(dev_to_iio_dev(dev));
  25. ret = spi_w8r16be(st->client, MS5611_READ_PROM_WORD + (index << 1));
  26. if (ret < 0)
  27. return ret;
  28. *word = ret;
  29. return 0;
  30. }
  31. static int ms5611_spi_read_adc(struct device *dev, s32 *val)
  32. {
  33. int ret;
  34. u8 buf[3] = { MS5611_READ_ADC };
  35. struct ms5611_state *st = iio_priv(dev_to_iio_dev(dev));
  36. ret = spi_write_then_read(st->client, buf, 1, buf, 3);
  37. if (ret < 0)
  38. return ret;
  39. *val = (buf[0] << 16) | (buf[1] << 8) | buf[2];
  40. return 0;
  41. }
  42. static int ms5611_spi_read_adc_temp_and_pressure(struct device *dev,
  43. s32 *temp, s32 *pressure)
  44. {
  45. u8 cmd;
  46. int ret;
  47. struct ms5611_state *st = iio_priv(dev_to_iio_dev(dev));
  48. cmd = MS5611_START_TEMP_CONV;
  49. ret = spi_write_then_read(st->client, &cmd, 1, NULL, 0);
  50. if (ret < 0)
  51. return ret;
  52. usleep_range(MS5611_CONV_TIME_MIN, MS5611_CONV_TIME_MAX);
  53. ret = ms5611_spi_read_adc(dev, temp);
  54. if (ret < 0)
  55. return ret;
  56. cmd = MS5611_START_PRESSURE_CONV;
  57. ret = spi_write_then_read(st->client, &cmd, 1, NULL, 0);
  58. if (ret < 0)
  59. return ret;
  60. usleep_range(MS5611_CONV_TIME_MIN, MS5611_CONV_TIME_MAX);
  61. return ms5611_spi_read_adc(dev, pressure);
  62. }
  63. static int ms5611_spi_probe(struct spi_device *spi)
  64. {
  65. int ret;
  66. struct ms5611_state *st;
  67. struct iio_dev *indio_dev;
  68. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
  69. if (!indio_dev)
  70. return -ENOMEM;
  71. spi->mode = SPI_MODE_0;
  72. spi->max_speed_hz = 20000000;
  73. spi->bits_per_word = 8;
  74. ret = spi_setup(spi);
  75. if (ret < 0)
  76. return ret;
  77. st = iio_priv(indio_dev);
  78. st->reset = ms5611_spi_reset;
  79. st->read_prom_word = ms5611_spi_read_prom_word;
  80. st->read_adc_temp_and_pressure = ms5611_spi_read_adc_temp_and_pressure;
  81. st->client = spi;
  82. return ms5611_probe(indio_dev, &spi->dev);
  83. }
  84. static const struct spi_device_id ms5611_id[] = {
  85. { "ms5611", 0 },
  86. { }
  87. };
  88. MODULE_DEVICE_TABLE(spi, ms5611_id);
  89. static struct spi_driver ms5611_driver = {
  90. .driver = {
  91. .name = "ms5611",
  92. .owner = THIS_MODULE,
  93. },
  94. .id_table = ms5611_id,
  95. .probe = ms5611_spi_probe,
  96. };
  97. module_spi_driver(ms5611_driver);
  98. MODULE_AUTHOR("Tomasz Duszynski <tduszyns@gmail.com>");
  99. MODULE_DESCRIPTION("MS5611 spi driver");
  100. MODULE_LICENSE("GPL v2");