ms5637.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ms5637.c - Support for Measurement-Specialties MS5637, MS5805
  4. * MS5837 and MS8607 pressure & temperature sensor
  5. *
  6. * Copyright (c) 2015 Measurement-Specialties
  7. *
  8. * (7-bit I2C slave address 0x76)
  9. *
  10. * Datasheet:
  11. * http://www.meas-spec.com/downloads/MS5637-02BA03.pdf
  12. * Datasheet:
  13. * http://www.meas-spec.com/downloads/MS5805-02BA01.pdf
  14. * Datasheet:
  15. * http://www.meas-spec.com/downloads/MS5837-30BA.pdf
  16. * Datasheet:
  17. * http://www.meas-spec.com/downloads/MS8607-02BA01.pdf
  18. */
  19. #include <linux/init.h>
  20. #include <linux/device.h>
  21. #include <linux/kernel.h>
  22. #include <linux/stat.h>
  23. #include <linux/module.h>
  24. #include <linux/mod_devicetable.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. struct ms_tp_data {
  31. const char *name;
  32. const struct ms_tp_hw_data *hw;
  33. };
  34. static const int ms5637_samp_freq[6] = { 960, 480, 240, 120, 60, 30 };
  35. static ssize_t ms5637_show_samp_freq(struct device *dev, struct device_attribute *attr, char *buf)
  36. {
  37. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  38. struct ms_tp_dev *dev_data = iio_priv(indio_dev);
  39. int i, len = 0;
  40. for (i = 0; i <= dev_data->hw->max_res_index; i++)
  41. len += sysfs_emit_at(buf, len, "%u ", ms5637_samp_freq[i]);
  42. sysfs_emit_at(buf, len - 1, "\n");
  43. return len;
  44. }
  45. static int ms5637_read_raw(struct iio_dev *indio_dev,
  46. struct iio_chan_spec const *channel, int *val,
  47. int *val2, long mask)
  48. {
  49. int ret;
  50. int temperature;
  51. unsigned int pressure;
  52. struct ms_tp_dev *dev_data = iio_priv(indio_dev);
  53. switch (mask) {
  54. case IIO_CHAN_INFO_PROCESSED:
  55. ret = ms_sensors_read_temp_and_pressure(dev_data,
  56. &temperature,
  57. &pressure);
  58. if (ret)
  59. return ret;
  60. switch (channel->type) {
  61. case IIO_TEMP: /* in milli °C */
  62. *val = temperature;
  63. return IIO_VAL_INT;
  64. case IIO_PRESSURE: /* in kPa */
  65. *val = pressure / 1000;
  66. *val2 = (pressure % 1000) * 1000;
  67. return IIO_VAL_INT_PLUS_MICRO;
  68. default:
  69. return -EINVAL;
  70. }
  71. case IIO_CHAN_INFO_SAMP_FREQ:
  72. *val = ms5637_samp_freq[dev_data->res_index];
  73. return IIO_VAL_INT;
  74. default:
  75. return -EINVAL;
  76. }
  77. }
  78. static int ms5637_write_raw(struct iio_dev *indio_dev,
  79. struct iio_chan_spec const *chan,
  80. int val, int val2, long mask)
  81. {
  82. struct ms_tp_dev *dev_data = iio_priv(indio_dev);
  83. int i;
  84. switch (mask) {
  85. case IIO_CHAN_INFO_SAMP_FREQ:
  86. i = ARRAY_SIZE(ms5637_samp_freq);
  87. while (i-- > 0)
  88. if (val == ms5637_samp_freq[i])
  89. break;
  90. if (i < 0)
  91. return -EINVAL;
  92. dev_data->res_index = i;
  93. return 0;
  94. default:
  95. return -EINVAL;
  96. }
  97. }
  98. static const struct iio_chan_spec ms5637_channels[] = {
  99. {
  100. .type = IIO_TEMP,
  101. .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
  102. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
  103. },
  104. {
  105. .type = IIO_PRESSURE,
  106. .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
  107. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
  108. }
  109. };
  110. static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(ms5637_show_samp_freq);
  111. static struct attribute *ms5637_attributes[] = {
  112. &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
  113. NULL,
  114. };
  115. static const struct attribute_group ms5637_attribute_group = {
  116. .attrs = ms5637_attributes,
  117. };
  118. static const struct iio_info ms5637_info = {
  119. .read_raw = ms5637_read_raw,
  120. .write_raw = ms5637_write_raw,
  121. .attrs = &ms5637_attribute_group,
  122. };
  123. static int ms5637_probe(struct i2c_client *client,
  124. const struct i2c_device_id *id)
  125. {
  126. const struct ms_tp_data *data;
  127. struct ms_tp_dev *dev_data;
  128. struct iio_dev *indio_dev;
  129. int ret;
  130. if (!i2c_check_functionality(client->adapter,
  131. I2C_FUNC_SMBUS_READ_WORD_DATA |
  132. I2C_FUNC_SMBUS_WRITE_BYTE |
  133. I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
  134. dev_err(&client->dev,
  135. "Adapter does not support some i2c transaction\n");
  136. return -EOPNOTSUPP;
  137. }
  138. if (id)
  139. data = (const struct ms_tp_data *)id->driver_data;
  140. else
  141. data = device_get_match_data(&client->dev);
  142. if (!data)
  143. return -EINVAL;
  144. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*dev_data));
  145. if (!indio_dev)
  146. return -ENOMEM;
  147. dev_data = iio_priv(indio_dev);
  148. dev_data->client = client;
  149. dev_data->res_index = data->hw->max_res_index;
  150. dev_data->hw = data->hw;
  151. mutex_init(&dev_data->lock);
  152. indio_dev->info = &ms5637_info;
  153. indio_dev->name = data->name;
  154. indio_dev->modes = INDIO_DIRECT_MODE;
  155. indio_dev->channels = ms5637_channels;
  156. indio_dev->num_channels = ARRAY_SIZE(ms5637_channels);
  157. i2c_set_clientdata(client, indio_dev);
  158. ret = ms_sensors_reset(client, 0x1E, 3000);
  159. if (ret)
  160. return ret;
  161. ret = ms_sensors_tp_read_prom(dev_data);
  162. if (ret)
  163. return ret;
  164. return devm_iio_device_register(&client->dev, indio_dev);
  165. }
  166. static const struct ms_tp_hw_data ms5637_hw_data = {
  167. .prom_len = 7,
  168. .max_res_index = 5
  169. };
  170. static const struct ms_tp_hw_data ms5803_hw_data = {
  171. .prom_len = 8,
  172. .max_res_index = 4
  173. };
  174. static const struct ms_tp_data ms5637_data = { .name = "ms5637", .hw = &ms5637_hw_data };
  175. static const struct ms_tp_data ms5803_data = { .name = "ms5803", .hw = &ms5803_hw_data };
  176. static const struct ms_tp_data ms5805_data = { .name = "ms5805", .hw = &ms5637_hw_data };
  177. static const struct ms_tp_data ms5837_data = { .name = "ms5837", .hw = &ms5637_hw_data };
  178. static const struct ms_tp_data ms8607_data = {
  179. .name = "ms8607-temppressure",
  180. .hw = &ms5637_hw_data,
  181. };
  182. static const struct i2c_device_id ms5637_id[] = {
  183. {"ms5637", (kernel_ulong_t)&ms5637_data },
  184. {"ms5805", (kernel_ulong_t)&ms5805_data },
  185. {"ms5837", (kernel_ulong_t)&ms5837_data },
  186. {"ms8607-temppressure", (kernel_ulong_t)&ms8607_data },
  187. {}
  188. };
  189. MODULE_DEVICE_TABLE(i2c, ms5637_id);
  190. static const struct of_device_id ms5637_of_match[] = {
  191. { .compatible = "meas,ms5637", .data = &ms5637_data },
  192. { .compatible = "meas,ms5803", .data = &ms5803_data },
  193. { .compatible = "meas,ms5805", .data = &ms5805_data },
  194. { .compatible = "meas,ms5837", .data = &ms5837_data },
  195. { .compatible = "meas,ms8607-temppressure", .data = &ms8607_data },
  196. { },
  197. };
  198. MODULE_DEVICE_TABLE(of, ms5637_of_match);
  199. static struct i2c_driver ms5637_driver = {
  200. .probe = ms5637_probe,
  201. .id_table = ms5637_id,
  202. .driver = {
  203. .name = "ms5637",
  204. .of_match_table = ms5637_of_match,
  205. },
  206. };
  207. module_i2c_driver(ms5637_driver);
  208. MODULE_DESCRIPTION("Measurement-Specialties ms5637 temperature & pressure driver");
  209. MODULE_AUTHOR("William Markezana <[email protected]>");
  210. MODULE_AUTHOR("Ludovic Tancerel <[email protected]>");
  211. MODULE_LICENSE("GPL v2");
  212. MODULE_IMPORT_NS(IIO_MEAS_SPEC_SENSORS);