ms5611_core.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * MS5611 pressure and temperature sensor driver
  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. * Data sheet:
  11. * http://www.meas-spec.com/downloads/MS5611-01BA03.pdf
  12. * http://www.meas-spec.com/downloads/MS5607-02BA03.pdf
  13. *
  14. */
  15. #include <linux/module.h>
  16. #include <linux/iio/iio.h>
  17. #include <linux/delay.h>
  18. #include "ms5611.h"
  19. static bool ms5611_prom_is_valid(u16 *prom, size_t len)
  20. {
  21. int i, j;
  22. uint16_t crc = 0, crc_orig = prom[7] & 0x000F;
  23. prom[7] &= 0xFF00;
  24. for (i = 0; i < len * 2; i++) {
  25. if (i % 2 == 1)
  26. crc ^= prom[i >> 1] & 0x00FF;
  27. else
  28. crc ^= prom[i >> 1] >> 8;
  29. for (j = 0; j < 8; j++) {
  30. if (crc & 0x8000)
  31. crc = (crc << 1) ^ 0x3000;
  32. else
  33. crc <<= 1;
  34. }
  35. }
  36. crc = (crc >> 12) & 0x000F;
  37. return crc_orig != 0x0000 && crc == crc_orig;
  38. }
  39. static int ms5611_read_prom(struct iio_dev *indio_dev)
  40. {
  41. int ret, i;
  42. struct ms5611_state *st = iio_priv(indio_dev);
  43. for (i = 0; i < MS5611_PROM_WORDS_NB; i++) {
  44. ret = st->read_prom_word(&indio_dev->dev,
  45. i, &st->chip_info->prom[i]);
  46. if (ret < 0) {
  47. dev_err(&indio_dev->dev,
  48. "failed to read prom at %d\n", i);
  49. return ret;
  50. }
  51. }
  52. if (!ms5611_prom_is_valid(st->chip_info->prom, MS5611_PROM_WORDS_NB)) {
  53. dev_err(&indio_dev->dev, "PROM integrity check failed\n");
  54. return -ENODEV;
  55. }
  56. return 0;
  57. }
  58. static int ms5611_read_temp_and_pressure(struct iio_dev *indio_dev,
  59. s32 *temp, s32 *pressure)
  60. {
  61. int ret;
  62. struct ms5611_state *st = iio_priv(indio_dev);
  63. ret = st->read_adc_temp_and_pressure(&indio_dev->dev, temp, pressure);
  64. if (ret < 0) {
  65. dev_err(&indio_dev->dev,
  66. "failed to read temperature and pressure\n");
  67. return ret;
  68. }
  69. return st->chip_info->temp_and_pressure_compensate(st->chip_info,
  70. temp, pressure);
  71. }
  72. static int ms5611_temp_and_pressure_compensate(struct ms5611_chip_info *chip_info,
  73. s32 *temp, s32 *pressure)
  74. {
  75. s32 t = *temp, p = *pressure;
  76. s64 off, sens, dt;
  77. dt = t - (chip_info->prom[5] << 8);
  78. off = ((s64)chip_info->prom[2] << 16) + ((chip_info->prom[4] * dt) >> 7);
  79. sens = ((s64)chip_info->prom[1] << 15) + ((chip_info->prom[3] * dt) >> 8);
  80. t = 2000 + ((chip_info->prom[6] * dt) >> 23);
  81. if (t < 2000) {
  82. s64 off2, sens2, t2;
  83. t2 = (dt * dt) >> 31;
  84. off2 = (5 * (t - 2000) * (t - 2000)) >> 1;
  85. sens2 = off2 >> 1;
  86. if (t < -1500) {
  87. s64 tmp = (t + 1500) * (t + 1500);
  88. off2 += 7 * tmp;
  89. sens2 += (11 * tmp) >> 1;
  90. }
  91. t -= t2;
  92. off -= off2;
  93. sens -= sens2;
  94. }
  95. *temp = t;
  96. *pressure = (((p * sens) >> 21) - off) >> 15;
  97. return 0;
  98. }
  99. static int ms5607_temp_and_pressure_compensate(struct ms5611_chip_info *chip_info,
  100. s32 *temp, s32 *pressure)
  101. {
  102. s32 t = *temp, p = *pressure;
  103. s64 off, sens, dt;
  104. dt = t - (chip_info->prom[5] << 8);
  105. off = ((s64)chip_info->prom[2] << 17) + ((chip_info->prom[4] * dt) >> 6);
  106. sens = ((s64)chip_info->prom[1] << 16) + ((chip_info->prom[3] * dt) >> 7);
  107. t = 2000 + ((chip_info->prom[6] * dt) >> 23);
  108. if (t < 2000) {
  109. s64 off2, sens2, t2;
  110. t2 = (dt * dt) >> 31;
  111. off2 = (61 * (t - 2000) * (t - 2000)) >> 4;
  112. sens2 = off2 << 1;
  113. if (t < -1500) {
  114. s64 tmp = (t + 1500) * (t + 1500);
  115. off2 += 15 * tmp;
  116. sens2 += (8 * tmp);
  117. }
  118. t -= t2;
  119. off -= off2;
  120. sens -= sens2;
  121. }
  122. *temp = t;
  123. *pressure = (((p * sens) >> 21) - off) >> 15;
  124. return 0;
  125. }
  126. static int ms5611_reset(struct iio_dev *indio_dev)
  127. {
  128. int ret;
  129. struct ms5611_state *st = iio_priv(indio_dev);
  130. ret = st->reset(&indio_dev->dev);
  131. if (ret < 0) {
  132. dev_err(&indio_dev->dev, "failed to reset device\n");
  133. return ret;
  134. }
  135. usleep_range(3000, 4000);
  136. return 0;
  137. }
  138. static int ms5611_read_raw(struct iio_dev *indio_dev,
  139. struct iio_chan_spec const *chan,
  140. int *val, int *val2, long mask)
  141. {
  142. int ret;
  143. s32 temp, pressure;
  144. struct ms5611_state *st = iio_priv(indio_dev);
  145. switch (mask) {
  146. case IIO_CHAN_INFO_PROCESSED:
  147. mutex_lock(&st->lock);
  148. ret = ms5611_read_temp_and_pressure(indio_dev,
  149. &temp, &pressure);
  150. mutex_unlock(&st->lock);
  151. if (ret < 0)
  152. return ret;
  153. switch (chan->type) {
  154. case IIO_TEMP:
  155. *val = temp * 10;
  156. return IIO_VAL_INT;
  157. case IIO_PRESSURE:
  158. *val = pressure / 1000;
  159. *val2 = (pressure % 1000) * 1000;
  160. return IIO_VAL_INT_PLUS_MICRO;
  161. default:
  162. return -EINVAL;
  163. }
  164. }
  165. return -EINVAL;
  166. }
  167. static struct ms5611_chip_info chip_info_tbl[] = {
  168. [MS5611] = {
  169. .temp_and_pressure_compensate = ms5611_temp_and_pressure_compensate,
  170. },
  171. [MS5607] = {
  172. .temp_and_pressure_compensate = ms5607_temp_and_pressure_compensate,
  173. }
  174. };
  175. static const struct iio_chan_spec ms5611_channels[] = {
  176. {
  177. .type = IIO_PRESSURE,
  178. .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
  179. },
  180. {
  181. .type = IIO_TEMP,
  182. .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
  183. }
  184. };
  185. static const struct iio_info ms5611_info = {
  186. .read_raw = &ms5611_read_raw,
  187. .driver_module = THIS_MODULE,
  188. };
  189. static int ms5611_init(struct iio_dev *indio_dev)
  190. {
  191. int ret;
  192. ret = ms5611_reset(indio_dev);
  193. if (ret < 0)
  194. return ret;
  195. return ms5611_read_prom(indio_dev);
  196. }
  197. int ms5611_probe(struct iio_dev *indio_dev, struct device *dev, int type)
  198. {
  199. int ret;
  200. struct ms5611_state *st = iio_priv(indio_dev);
  201. mutex_init(&st->lock);
  202. st->chip_info = &chip_info_tbl[type];
  203. indio_dev->dev.parent = dev;
  204. indio_dev->name = dev->driver->name;
  205. indio_dev->info = &ms5611_info;
  206. indio_dev->channels = ms5611_channels;
  207. indio_dev->num_channels = ARRAY_SIZE(ms5611_channels);
  208. indio_dev->modes = INDIO_DIRECT_MODE;
  209. ret = ms5611_init(indio_dev);
  210. if (ret < 0)
  211. return ret;
  212. return devm_iio_device_register(dev, indio_dev);
  213. }
  214. EXPORT_SYMBOL(ms5611_probe);
  215. MODULE_AUTHOR("Tomasz Duszynski <tduszyns@gmail.com>");
  216. MODULE_DESCRIPTION("MS5611 core driver");
  217. MODULE_LICENSE("GPL v2");