ms5637.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. .driver_module = THIS_MODULE,
  111. };
  112. static int ms5637_probe(struct i2c_client *client,
  113. const struct i2c_device_id *id)
  114. {
  115. struct ms_tp_dev *dev_data;
  116. struct iio_dev *indio_dev;
  117. int ret;
  118. if (!i2c_check_functionality(client->adapter,
  119. I2C_FUNC_SMBUS_READ_WORD_DATA |
  120. I2C_FUNC_SMBUS_WRITE_BYTE |
  121. I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
  122. dev_err(&client->dev,
  123. "Adapter does not support some i2c transaction\n");
  124. return -EOPNOTSUPP;
  125. }
  126. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*dev_data));
  127. if (!indio_dev)
  128. return -ENOMEM;
  129. dev_data = iio_priv(indio_dev);
  130. dev_data->client = client;
  131. dev_data->res_index = 5;
  132. mutex_init(&dev_data->lock);
  133. indio_dev->info = &ms5637_info;
  134. indio_dev->name = id->name;
  135. indio_dev->dev.parent = &client->dev;
  136. indio_dev->modes = INDIO_DIRECT_MODE;
  137. indio_dev->channels = ms5637_channels;
  138. indio_dev->num_channels = ARRAY_SIZE(ms5637_channels);
  139. i2c_set_clientdata(client, indio_dev);
  140. ret = ms_sensors_reset(client, 0x1E, 3000);
  141. if (ret)
  142. return ret;
  143. ret = ms_sensors_tp_read_prom(dev_data);
  144. if (ret)
  145. return ret;
  146. return devm_iio_device_register(&client->dev, indio_dev);
  147. }
  148. static const struct i2c_device_id ms5637_id[] = {
  149. {"ms5637", 0},
  150. {"ms5805", 0},
  151. {"ms5837", 0},
  152. {"ms8607-temppressure", 0},
  153. {}
  154. };
  155. MODULE_DEVICE_TABLE(i2c, ms5637_id);
  156. static struct i2c_driver ms5637_driver = {
  157. .probe = ms5637_probe,
  158. .id_table = ms5637_id,
  159. .driver = {
  160. .name = "ms5637"
  161. },
  162. };
  163. module_i2c_driver(ms5637_driver);
  164. MODULE_DESCRIPTION("Measurement-Specialties ms5637 temperature & pressure driver");
  165. MODULE_AUTHOR("William Markezana <william.markezana@meas-spec.com>");
  166. MODULE_AUTHOR("Ludovic Tancerel <ludovic.tancerel@maplehightech.com>");
  167. MODULE_LICENSE("GPL v2");