ms5611_spi.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 <linux/of_device.h>
  15. #include "ms5611.h"
  16. static int ms5611_spi_reset(struct device *dev)
  17. {
  18. u8 cmd = MS5611_RESET;
  19. struct ms5611_state *st = iio_priv(dev_to_iio_dev(dev));
  20. return spi_write_then_read(st->client, &cmd, 1, NULL, 0);
  21. }
  22. static int ms5611_spi_read_prom_word(struct device *dev, int index, u16 *word)
  23. {
  24. int ret;
  25. struct ms5611_state *st = iio_priv(dev_to_iio_dev(dev));
  26. ret = spi_w8r16be(st->client, MS5611_READ_PROM_WORD + (index << 1));
  27. if (ret < 0)
  28. return ret;
  29. *word = ret;
  30. return 0;
  31. }
  32. static int ms5611_spi_read_adc(struct device *dev, s32 *val)
  33. {
  34. int ret;
  35. u8 buf[3] = { MS5611_READ_ADC };
  36. struct ms5611_state *st = iio_priv(dev_to_iio_dev(dev));
  37. ret = spi_write_then_read(st->client, buf, 1, buf, 3);
  38. if (ret < 0)
  39. return ret;
  40. *val = (buf[0] << 16) | (buf[1] << 8) | buf[2];
  41. return 0;
  42. }
  43. static int ms5611_spi_read_adc_temp_and_pressure(struct device *dev,
  44. s32 *temp, s32 *pressure)
  45. {
  46. int ret;
  47. struct ms5611_state *st = iio_priv(dev_to_iio_dev(dev));
  48. const struct ms5611_osr *osr = st->temp_osr;
  49. /*
  50. * Warning: &osr->cmd MUST be aligned on a word boundary since used as
  51. * 2nd argument (void*) of spi_write_then_read.
  52. */
  53. ret = spi_write_then_read(st->client, &osr->cmd, 1, NULL, 0);
  54. if (ret < 0)
  55. return ret;
  56. usleep_range(osr->conv_usec, osr->conv_usec + (osr->conv_usec / 10UL));
  57. ret = ms5611_spi_read_adc(dev, temp);
  58. if (ret < 0)
  59. return ret;
  60. osr = st->pressure_osr;
  61. ret = spi_write_then_read(st->client, &osr->cmd, 1, NULL, 0);
  62. if (ret < 0)
  63. return ret;
  64. usleep_range(osr->conv_usec, osr->conv_usec + (osr->conv_usec / 10UL));
  65. return ms5611_spi_read_adc(dev, pressure);
  66. }
  67. static int ms5611_spi_probe(struct spi_device *spi)
  68. {
  69. int ret;
  70. struct ms5611_state *st;
  71. struct iio_dev *indio_dev;
  72. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
  73. if (!indio_dev)
  74. return -ENOMEM;
  75. spi_set_drvdata(spi, indio_dev);
  76. spi->mode = SPI_MODE_0;
  77. spi->max_speed_hz = 20000000;
  78. spi->bits_per_word = 8;
  79. ret = spi_setup(spi);
  80. if (ret < 0)
  81. return ret;
  82. st = iio_priv(indio_dev);
  83. st->reset = ms5611_spi_reset;
  84. st->read_prom_word = ms5611_spi_read_prom_word;
  85. st->read_adc_temp_and_pressure = ms5611_spi_read_adc_temp_and_pressure;
  86. st->client = spi;
  87. return ms5611_probe(indio_dev, &spi->dev, spi_get_device_id(spi)->name,
  88. spi_get_device_id(spi)->driver_data);
  89. }
  90. static int ms5611_spi_remove(struct spi_device *spi)
  91. {
  92. return ms5611_remove(spi_get_drvdata(spi));
  93. }
  94. #if defined(CONFIG_OF)
  95. static const struct of_device_id ms5611_spi_matches[] = {
  96. { .compatible = "meas,ms5611" },
  97. { .compatible = "ms5611" },
  98. { .compatible = "meas,ms5607" },
  99. { .compatible = "ms5607" },
  100. { }
  101. };
  102. MODULE_DEVICE_TABLE(of, ms5611_spi_matches);
  103. #endif
  104. static const struct spi_device_id ms5611_id[] = {
  105. { "ms5611", MS5611 },
  106. { "ms5607", MS5607 },
  107. { }
  108. };
  109. MODULE_DEVICE_TABLE(spi, ms5611_id);
  110. static struct spi_driver ms5611_driver = {
  111. .driver = {
  112. .name = "ms5611",
  113. .of_match_table = of_match_ptr(ms5611_spi_matches)
  114. },
  115. .id_table = ms5611_id,
  116. .probe = ms5611_spi_probe,
  117. .remove = ms5611_spi_remove,
  118. };
  119. module_spi_driver(ms5611_driver);
  120. MODULE_AUTHOR("Tomasz Duszynski <tduszyns@gmail.com>");
  121. MODULE_DESCRIPTION("MS5611 spi driver");
  122. MODULE_LICENSE("GPL v2");