ms5637.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * ms5637.c - Support for Measurement-Specialties MS5637, MS5805
  3. * MS5837 and MS8607 pressure & temperature sensor
  4. *
  5. * Copyright (c) 2015 Measurement-Specialties
  6. *
  7. * Licensed under the GPL-2.
  8. *
  9. * (7-bit I2C slave address 0x76)
  10. *
  11. * Datasheet:
  12. * http://www.meas-spec.com/downloads/MS5637-02BA03.pdf
  13. * Datasheet:
  14. * http://www.meas-spec.com/downloads/MS5805-02BA01.pdf
  15. * Datasheet:
  16. * http://www.meas-spec.com/downloads/MS5837-30BA.pdf
  17. * Datasheet:
  18. * http://www.meas-spec.com/downloads/MS8607-02BA01.pdf
  19. */
  20. #include <linux/init.h>
  21. #include <linux/device.h>
  22. #include <linux/kernel.h>
  23. #include <linux/stat.h>
  24. #include <linux/module.h>
  25. #include <linux/i2c.h>
  26. #include <linux/iio/iio.h>
  27. #include <linux/iio/sysfs.h>
  28. #include <linux/mutex.h>
  29. #include "../common/ms_sensors/ms_sensors_i2c.h"
  30. static const int ms5637_samp_freq[6] = { 960, 480, 240, 120, 60, 30 };
  31. /* String copy of the above const for readability purpose */
  32. static const char ms5637_show_samp_freq[] = "960 480 240 120 60 30";
  33. static int ms5637_read_raw(struct iio_dev *indio_dev,
  34. struct iio_chan_spec const *channel, int *val,
  35. int *val2, long mask)
  36. {
  37. int ret;
  38. int temperature;
  39. unsigned int pressure;
  40. struct ms_tp_dev *dev_data = iio_priv(indio_dev);
  41. switch (mask) {
  42. case IIO_CHAN_INFO_PROCESSED:
  43. ret = ms_sensors_read_temp_and_pressure(dev_data,
  44. &temperature,
  45. &pressure);
  46. if (ret)
  47. return ret;
  48. switch (channel->type) {
  49. case IIO_TEMP: /* in milli °C */
  50. *val = temperature;
  51. return IIO_VAL_INT;
  52. case IIO_PRESSURE: /* in kPa */
  53. *val = pressure / 1000;
  54. *val2 = (pressure % 1000) * 1000;
  55. return IIO_VAL_INT_PLUS_MICRO;
  56. default:
  57. return -EINVAL;
  58. }
  59. case IIO_CHAN_INFO_SAMP_FREQ:
  60. *val = ms5637_samp_freq[dev_data->res_index];
  61. return IIO_VAL_INT;
  62. default:
  63. return -EINVAL;
  64. }
  65. }
  66. static int ms5637_write_raw(struct iio_dev *indio_dev,
  67. struct iio_chan_spec const *chan,
  68. int val, int val2, long mask)
  69. {
  70. struct ms_tp_dev *dev_data = iio_priv(indio_dev);
  71. int i;
  72. switch (mask) {
  73. case IIO_CHAN_INFO_SAMP_FREQ:
  74. i = ARRAY_SIZE(ms5637_samp_freq);
  75. while (i-- > 0)
  76. if (val == ms5637_samp_freq[i])
  77. break;
  78. if (i < 0)
  79. return -EINVAL;
  80. dev_data->res_index = i;
  81. return 0;
  82. default:
  83. return -EINVAL;
  84. }
  85. }
  86. static const struct iio_chan_spec ms5637_channels[] = {
  87. {
  88. .type = IIO_TEMP,
  89. .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
  90. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
  91. },
  92. {
  93. .type = IIO_PRESSURE,
  94. .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
  95. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
  96. }
  97. };
  98. static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(ms5637_show_samp_freq);
  99. static struct attribute *ms5637_attributes[] = {
  100. &iio_const_attr_sampling_frequency_available.dev_attr.attr,
  101. NULL,
  102. };
  103. static const struct attribute_group ms5637_attribute_group = {
  104. .attrs = ms5637_attributes,
  105. };
  106. static const struct iio_info ms5637_info = {
  107. .read_raw = ms5637_read_raw,
  108. .write_raw = ms5637_write_raw,
  109. .attrs = &ms5637_attribute_group,
  110. };
  111. static int ms5637_probe(struct i2c_client *client,
  112. const struct i2c_device_id *id)
  113. {
  114. struct ms_tp_dev *dev_data;
  115. struct iio_dev *indio_dev;
  116. int ret;
  117. if (!i2c_check_functionality(client->adapter,
  118. I2C_FUNC_SMBUS_READ_WORD_DATA |
  119. I2C_FUNC_SMBUS_WRITE_BYTE |
  120. I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
  121. dev_err(&client->dev,
  122. "Adapter does not support some i2c transaction\n");
  123. return -EOPNOTSUPP;
  124. }
  125. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*dev_data));
  126. if (!indio_dev)
  127. return -ENOMEM;
  128. dev_data = iio_priv(indio_dev);
  129. dev_data->client = client;
  130. dev_data->res_index = 5;
  131. mutex_init(&dev_data->lock);
  132. indio_dev->info = &ms5637_info;
  133. indio_dev->name = id->name;
  134. indio_dev->dev.parent = &client->dev;
  135. indio_dev->modes = INDIO_DIRECT_MODE;
  136. indio_dev->channels = ms5637_channels;
  137. indio_dev->num_channels = ARRAY_SIZE(ms5637_channels);
  138. i2c_set_clientdata(client, indio_dev);
  139. ret = ms_sensors_reset(client, 0x1E, 3000);
  140. if (ret)
  141. return ret;
  142. ret = ms_sensors_tp_read_prom(dev_data);
  143. if (ret)
  144. return ret;
  145. return devm_iio_device_register(&client->dev, indio_dev);
  146. }
  147. static const struct i2c_device_id ms5637_id[] = {
  148. {"ms5637", 0},
  149. {"ms5805", 0},
  150. {"ms5837", 0},
  151. {"ms8607-temppressure", 0},
  152. {}
  153. };
  154. MODULE_DEVICE_TABLE(i2c, ms5637_id);
  155. static const struct of_device_id ms5637_of_match[] = {
  156. { .compatible = "meas,ms5637", },
  157. { .compatible = "meas,ms5805", },
  158. { .compatible = "meas,ms5837", },
  159. { .compatible = "meas,ms8607-temppressure", },
  160. { },
  161. };
  162. MODULE_DEVICE_TABLE(of, ms5637_of_match);
  163. static struct i2c_driver ms5637_driver = {
  164. .probe = ms5637_probe,
  165. .id_table = ms5637_id,
  166. .driver = {
  167. .name = "ms5637",
  168. .of_match_table = of_match_ptr(ms5637_of_match),
  169. },
  170. };
  171. module_i2c_driver(ms5637_driver);
  172. MODULE_DESCRIPTION("Measurement-Specialties ms5637 temperature & pressure driver");
  173. MODULE_AUTHOR("William Markezana <william.markezana@meas-spec.com>");
  174. MODULE_AUTHOR("Ludovic Tancerel <ludovic.tancerel@maplehightech.com>");
  175. MODULE_LICENSE("GPL v2");