ms5611_core.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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 <linux/regulator/consumer.h>
  19. #include <linux/iio/sysfs.h>
  20. #include <linux/iio/buffer.h>
  21. #include <linux/iio/triggered_buffer.h>
  22. #include <linux/iio/trigger_consumer.h>
  23. #include "ms5611.h"
  24. #define MS5611_INIT_OSR(_cmd, _conv_usec, _rate) \
  25. { .cmd = _cmd, .conv_usec = _conv_usec, .rate = _rate }
  26. static const struct ms5611_osr ms5611_avail_pressure_osr[] = {
  27. MS5611_INIT_OSR(0x40, 600, 256),
  28. MS5611_INIT_OSR(0x42, 1170, 512),
  29. MS5611_INIT_OSR(0x44, 2280, 1024),
  30. MS5611_INIT_OSR(0x46, 4540, 2048),
  31. MS5611_INIT_OSR(0x48, 9040, 4096)
  32. };
  33. static const struct ms5611_osr ms5611_avail_temp_osr[] = {
  34. MS5611_INIT_OSR(0x50, 600, 256),
  35. MS5611_INIT_OSR(0x52, 1170, 512),
  36. MS5611_INIT_OSR(0x54, 2280, 1024),
  37. MS5611_INIT_OSR(0x56, 4540, 2048),
  38. MS5611_INIT_OSR(0x58, 9040, 4096)
  39. };
  40. static const char ms5611_show_osr[] = "256 512 1024 2048 4096";
  41. static IIO_CONST_ATTR(oversampling_ratio_available, ms5611_show_osr);
  42. static struct attribute *ms5611_attributes[] = {
  43. &iio_const_attr_oversampling_ratio_available.dev_attr.attr,
  44. NULL,
  45. };
  46. static const struct attribute_group ms5611_attribute_group = {
  47. .attrs = ms5611_attributes,
  48. };
  49. static bool ms5611_prom_is_valid(u16 *prom, size_t len)
  50. {
  51. int i, j;
  52. uint16_t crc = 0, crc_orig = prom[7] & 0x000F;
  53. prom[7] &= 0xFF00;
  54. for (i = 0; i < len * 2; i++) {
  55. if (i % 2 == 1)
  56. crc ^= prom[i >> 1] & 0x00FF;
  57. else
  58. crc ^= prom[i >> 1] >> 8;
  59. for (j = 0; j < 8; j++) {
  60. if (crc & 0x8000)
  61. crc = (crc << 1) ^ 0x3000;
  62. else
  63. crc <<= 1;
  64. }
  65. }
  66. crc = (crc >> 12) & 0x000F;
  67. return crc_orig != 0x0000 && crc == crc_orig;
  68. }
  69. static int ms5611_read_prom(struct iio_dev *indio_dev)
  70. {
  71. int ret, i;
  72. struct ms5611_state *st = iio_priv(indio_dev);
  73. for (i = 0; i < MS5611_PROM_WORDS_NB; i++) {
  74. ret = st->read_prom_word(&indio_dev->dev,
  75. i, &st->chip_info->prom[i]);
  76. if (ret < 0) {
  77. dev_err(&indio_dev->dev,
  78. "failed to read prom at %d\n", i);
  79. return ret;
  80. }
  81. }
  82. if (!ms5611_prom_is_valid(st->chip_info->prom, MS5611_PROM_WORDS_NB)) {
  83. dev_err(&indio_dev->dev, "PROM integrity check failed\n");
  84. return -ENODEV;
  85. }
  86. return 0;
  87. }
  88. static int ms5611_read_temp_and_pressure(struct iio_dev *indio_dev,
  89. s32 *temp, s32 *pressure)
  90. {
  91. int ret;
  92. struct ms5611_state *st = iio_priv(indio_dev);
  93. ret = st->read_adc_temp_and_pressure(&indio_dev->dev, temp, pressure);
  94. if (ret < 0) {
  95. dev_err(&indio_dev->dev,
  96. "failed to read temperature and pressure\n");
  97. return ret;
  98. }
  99. return st->chip_info->temp_and_pressure_compensate(st->chip_info,
  100. temp, pressure);
  101. }
  102. static int ms5611_temp_and_pressure_compensate(struct ms5611_chip_info *chip_info,
  103. s32 *temp, s32 *pressure)
  104. {
  105. s32 t = *temp, p = *pressure;
  106. s64 off, sens, dt;
  107. dt = t - (chip_info->prom[5] << 8);
  108. off = ((s64)chip_info->prom[2] << 16) + ((chip_info->prom[4] * dt) >> 7);
  109. sens = ((s64)chip_info->prom[1] << 15) + ((chip_info->prom[3] * dt) >> 8);
  110. t = 2000 + ((chip_info->prom[6] * dt) >> 23);
  111. if (t < 2000) {
  112. s64 off2, sens2, t2;
  113. t2 = (dt * dt) >> 31;
  114. off2 = (5 * (t - 2000) * (t - 2000)) >> 1;
  115. sens2 = off2 >> 1;
  116. if (t < -1500) {
  117. s64 tmp = (t + 1500) * (t + 1500);
  118. off2 += 7 * tmp;
  119. sens2 += (11 * tmp) >> 1;
  120. }
  121. t -= t2;
  122. off -= off2;
  123. sens -= sens2;
  124. }
  125. *temp = t;
  126. *pressure = (((p * sens) >> 21) - off) >> 15;
  127. return 0;
  128. }
  129. static int ms5607_temp_and_pressure_compensate(struct ms5611_chip_info *chip_info,
  130. s32 *temp, s32 *pressure)
  131. {
  132. s32 t = *temp, p = *pressure;
  133. s64 off, sens, dt;
  134. dt = t - (chip_info->prom[5] << 8);
  135. off = ((s64)chip_info->prom[2] << 17) + ((chip_info->prom[4] * dt) >> 6);
  136. sens = ((s64)chip_info->prom[1] << 16) + ((chip_info->prom[3] * dt) >> 7);
  137. t = 2000 + ((chip_info->prom[6] * dt) >> 23);
  138. if (t < 2000) {
  139. s64 off2, sens2, t2, tmp;
  140. t2 = (dt * dt) >> 31;
  141. tmp = (t - 2000) * (t - 2000);
  142. off2 = (61 * tmp) >> 4;
  143. sens2 = tmp << 1;
  144. if (t < -1500) {
  145. tmp = (t + 1500) * (t + 1500);
  146. off2 += 15 * tmp;
  147. sens2 += 8 * tmp;
  148. }
  149. t -= t2;
  150. off -= off2;
  151. sens -= sens2;
  152. }
  153. *temp = t;
  154. *pressure = (((p * sens) >> 21) - off) >> 15;
  155. return 0;
  156. }
  157. static int ms5611_reset(struct iio_dev *indio_dev)
  158. {
  159. int ret;
  160. struct ms5611_state *st = iio_priv(indio_dev);
  161. ret = st->reset(&indio_dev->dev);
  162. if (ret < 0) {
  163. dev_err(&indio_dev->dev, "failed to reset device\n");
  164. return ret;
  165. }
  166. usleep_range(3000, 4000);
  167. return 0;
  168. }
  169. static irqreturn_t ms5611_trigger_handler(int irq, void *p)
  170. {
  171. struct iio_poll_func *pf = p;
  172. struct iio_dev *indio_dev = pf->indio_dev;
  173. struct ms5611_state *st = iio_priv(indio_dev);
  174. s32 buf[4]; /* s32 (pressure) + s32 (temp) + 2 * s32 (timestamp) */
  175. int ret;
  176. mutex_lock(&st->lock);
  177. ret = ms5611_read_temp_and_pressure(indio_dev, &buf[1], &buf[0]);
  178. mutex_unlock(&st->lock);
  179. if (ret < 0)
  180. goto err;
  181. iio_push_to_buffers_with_timestamp(indio_dev, buf,
  182. iio_get_time_ns(indio_dev));
  183. err:
  184. iio_trigger_notify_done(indio_dev->trig);
  185. return IRQ_HANDLED;
  186. }
  187. static int ms5611_read_raw(struct iio_dev *indio_dev,
  188. struct iio_chan_spec const *chan,
  189. int *val, int *val2, long mask)
  190. {
  191. int ret;
  192. s32 temp, pressure;
  193. struct ms5611_state *st = iio_priv(indio_dev);
  194. switch (mask) {
  195. case IIO_CHAN_INFO_PROCESSED:
  196. mutex_lock(&st->lock);
  197. ret = ms5611_read_temp_and_pressure(indio_dev,
  198. &temp, &pressure);
  199. mutex_unlock(&st->lock);
  200. if (ret < 0)
  201. return ret;
  202. switch (chan->type) {
  203. case IIO_TEMP:
  204. *val = temp * 10;
  205. return IIO_VAL_INT;
  206. case IIO_PRESSURE:
  207. *val = pressure / 1000;
  208. *val2 = (pressure % 1000) * 1000;
  209. return IIO_VAL_INT_PLUS_MICRO;
  210. default:
  211. return -EINVAL;
  212. }
  213. case IIO_CHAN_INFO_SCALE:
  214. switch (chan->type) {
  215. case IIO_TEMP:
  216. *val = 10;
  217. return IIO_VAL_INT;
  218. case IIO_PRESSURE:
  219. *val = 0;
  220. *val2 = 1000;
  221. return IIO_VAL_INT_PLUS_MICRO;
  222. default:
  223. return -EINVAL;
  224. }
  225. case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
  226. if (chan->type != IIO_TEMP && chan->type != IIO_PRESSURE)
  227. break;
  228. mutex_lock(&st->lock);
  229. if (chan->type == IIO_TEMP)
  230. *val = (int)st->temp_osr->rate;
  231. else
  232. *val = (int)st->pressure_osr->rate;
  233. mutex_unlock(&st->lock);
  234. return IIO_VAL_INT;
  235. }
  236. return -EINVAL;
  237. }
  238. static const struct ms5611_osr *ms5611_find_osr(int rate,
  239. const struct ms5611_osr *osr,
  240. size_t count)
  241. {
  242. unsigned int r;
  243. for (r = 0; r < count; r++)
  244. if ((unsigned short)rate == osr[r].rate)
  245. break;
  246. if (r >= count)
  247. return NULL;
  248. return &osr[r];
  249. }
  250. static int ms5611_write_raw(struct iio_dev *indio_dev,
  251. struct iio_chan_spec const *chan,
  252. int val, int val2, long mask)
  253. {
  254. struct ms5611_state *st = iio_priv(indio_dev);
  255. const struct ms5611_osr *osr = NULL;
  256. int ret;
  257. if (mask != IIO_CHAN_INFO_OVERSAMPLING_RATIO)
  258. return -EINVAL;
  259. if (chan->type == IIO_TEMP)
  260. osr = ms5611_find_osr(val, ms5611_avail_temp_osr,
  261. ARRAY_SIZE(ms5611_avail_temp_osr));
  262. else if (chan->type == IIO_PRESSURE)
  263. osr = ms5611_find_osr(val, ms5611_avail_pressure_osr,
  264. ARRAY_SIZE(ms5611_avail_pressure_osr));
  265. if (!osr)
  266. return -EINVAL;
  267. ret = iio_device_claim_direct_mode(indio_dev);
  268. if (ret)
  269. return ret;
  270. mutex_lock(&st->lock);
  271. if (chan->type == IIO_TEMP)
  272. st->temp_osr = osr;
  273. else
  274. st->pressure_osr = osr;
  275. mutex_unlock(&st->lock);
  276. iio_device_release_direct_mode(indio_dev);
  277. return 0;
  278. }
  279. static const unsigned long ms5611_scan_masks[] = {0x3, 0};
  280. static struct ms5611_chip_info chip_info_tbl[] = {
  281. [MS5611] = {
  282. .temp_and_pressure_compensate = ms5611_temp_and_pressure_compensate,
  283. },
  284. [MS5607] = {
  285. .temp_and_pressure_compensate = ms5607_temp_and_pressure_compensate,
  286. }
  287. };
  288. static const struct iio_chan_spec ms5611_channels[] = {
  289. {
  290. .type = IIO_PRESSURE,
  291. .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
  292. BIT(IIO_CHAN_INFO_SCALE) |
  293. BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
  294. .scan_index = 0,
  295. .scan_type = {
  296. .sign = 's',
  297. .realbits = 32,
  298. .storagebits = 32,
  299. .endianness = IIO_CPU,
  300. },
  301. },
  302. {
  303. .type = IIO_TEMP,
  304. .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
  305. BIT(IIO_CHAN_INFO_SCALE) |
  306. BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
  307. .scan_index = 1,
  308. .scan_type = {
  309. .sign = 's',
  310. .realbits = 32,
  311. .storagebits = 32,
  312. .endianness = IIO_CPU,
  313. },
  314. },
  315. IIO_CHAN_SOFT_TIMESTAMP(2),
  316. };
  317. static const struct iio_info ms5611_info = {
  318. .read_raw = &ms5611_read_raw,
  319. .write_raw = &ms5611_write_raw,
  320. .attrs = &ms5611_attribute_group,
  321. };
  322. static int ms5611_init(struct iio_dev *indio_dev)
  323. {
  324. int ret;
  325. struct ms5611_state *st = iio_priv(indio_dev);
  326. /* Enable attached regulator if any. */
  327. st->vdd = devm_regulator_get(indio_dev->dev.parent, "vdd");
  328. if (IS_ERR(st->vdd))
  329. return PTR_ERR(st->vdd);
  330. ret = regulator_enable(st->vdd);
  331. if (ret) {
  332. dev_err(indio_dev->dev.parent,
  333. "failed to enable Vdd supply: %d\n", ret);
  334. return ret;
  335. }
  336. ret = ms5611_reset(indio_dev);
  337. if (ret < 0)
  338. goto err_regulator_disable;
  339. ret = ms5611_read_prom(indio_dev);
  340. if (ret < 0)
  341. goto err_regulator_disable;
  342. return 0;
  343. err_regulator_disable:
  344. regulator_disable(st->vdd);
  345. return ret;
  346. }
  347. static void ms5611_fini(const struct iio_dev *indio_dev)
  348. {
  349. const struct ms5611_state *st = iio_priv(indio_dev);
  350. regulator_disable(st->vdd);
  351. }
  352. int ms5611_probe(struct iio_dev *indio_dev, struct device *dev,
  353. const char *name, int type)
  354. {
  355. int ret;
  356. struct ms5611_state *st = iio_priv(indio_dev);
  357. mutex_init(&st->lock);
  358. st->chip_info = &chip_info_tbl[type];
  359. st->temp_osr =
  360. &ms5611_avail_temp_osr[ARRAY_SIZE(ms5611_avail_temp_osr) - 1];
  361. st->pressure_osr =
  362. &ms5611_avail_pressure_osr[ARRAY_SIZE(ms5611_avail_pressure_osr)
  363. - 1];
  364. indio_dev->dev.parent = dev;
  365. indio_dev->name = name;
  366. indio_dev->info = &ms5611_info;
  367. indio_dev->channels = ms5611_channels;
  368. indio_dev->num_channels = ARRAY_SIZE(ms5611_channels);
  369. indio_dev->modes = INDIO_DIRECT_MODE;
  370. indio_dev->available_scan_masks = ms5611_scan_masks;
  371. ret = ms5611_init(indio_dev);
  372. if (ret < 0)
  373. return ret;
  374. ret = iio_triggered_buffer_setup(indio_dev, NULL,
  375. ms5611_trigger_handler, NULL);
  376. if (ret < 0) {
  377. dev_err(dev, "iio triggered buffer setup failed\n");
  378. goto err_fini;
  379. }
  380. ret = iio_device_register(indio_dev);
  381. if (ret < 0) {
  382. dev_err(dev, "unable to register iio device\n");
  383. goto err_buffer_cleanup;
  384. }
  385. return 0;
  386. err_buffer_cleanup:
  387. iio_triggered_buffer_cleanup(indio_dev);
  388. err_fini:
  389. ms5611_fini(indio_dev);
  390. return ret;
  391. }
  392. EXPORT_SYMBOL(ms5611_probe);
  393. int ms5611_remove(struct iio_dev *indio_dev)
  394. {
  395. iio_device_unregister(indio_dev);
  396. iio_triggered_buffer_cleanup(indio_dev);
  397. ms5611_fini(indio_dev);
  398. return 0;
  399. }
  400. EXPORT_SYMBOL(ms5611_remove);
  401. MODULE_AUTHOR("Tomasz Duszynski <tduszyns@gmail.com>");
  402. MODULE_DESCRIPTION("MS5611 core driver");
  403. MODULE_LICENSE("GPL v2");