htu21.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * htu21.c - Support for Measurement-Specialties
  4. * htu21 temperature & humidity sensor
  5. * and humidity part of MS8607 sensor
  6. *
  7. * Copyright (c) 2014 Measurement-Specialties
  8. *
  9. * (7-bit I2C slave address 0x40)
  10. *
  11. * Datasheet:
  12. * http://www.meas-spec.com/downloads/HTU21D.pdf
  13. * Datasheet:
  14. * http://www.meas-spec.com/downloads/MS8607-02BA01.pdf
  15. */
  16. #include <linux/init.h>
  17. #include <linux/device.h>
  18. #include <linux/kernel.h>
  19. #include <linux/stat.h>
  20. #include <linux/module.h>
  21. #include <linux/mod_devicetable.h>
  22. #include <linux/iio/iio.h>
  23. #include <linux/iio/sysfs.h>
  24. #include "../common/ms_sensors/ms_sensors_i2c.h"
  25. #define HTU21_RESET 0xFE
  26. enum {
  27. HTU21,
  28. MS8607
  29. };
  30. static const int htu21_samp_freq[4] = { 20, 40, 70, 120 };
  31. /* String copy of the above const for readability purpose */
  32. static const char htu21_show_samp_freq[] = "20 40 70 120";
  33. static int htu21_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, temperature;
  38. unsigned int humidity;
  39. struct ms_ht_dev *dev_data = iio_priv(indio_dev);
  40. switch (mask) {
  41. case IIO_CHAN_INFO_PROCESSED:
  42. switch (channel->type) {
  43. case IIO_TEMP: /* in milli °C */
  44. ret = ms_sensors_ht_read_temperature(dev_data,
  45. &temperature);
  46. if (ret)
  47. return ret;
  48. *val = temperature;
  49. return IIO_VAL_INT;
  50. case IIO_HUMIDITYRELATIVE: /* in milli %RH */
  51. ret = ms_sensors_ht_read_humidity(dev_data,
  52. &humidity);
  53. if (ret)
  54. return ret;
  55. *val = humidity;
  56. return IIO_VAL_INT;
  57. default:
  58. return -EINVAL;
  59. }
  60. case IIO_CHAN_INFO_SAMP_FREQ:
  61. *val = htu21_samp_freq[dev_data->res_index];
  62. return IIO_VAL_INT;
  63. default:
  64. return -EINVAL;
  65. }
  66. }
  67. static int htu21_write_raw(struct iio_dev *indio_dev,
  68. struct iio_chan_spec const *chan,
  69. int val, int val2, long mask)
  70. {
  71. struct ms_ht_dev *dev_data = iio_priv(indio_dev);
  72. int i, ret;
  73. switch (mask) {
  74. case IIO_CHAN_INFO_SAMP_FREQ:
  75. i = ARRAY_SIZE(htu21_samp_freq);
  76. while (i-- > 0)
  77. if (val == htu21_samp_freq[i])
  78. break;
  79. if (i < 0)
  80. return -EINVAL;
  81. mutex_lock(&dev_data->lock);
  82. dev_data->res_index = i;
  83. ret = ms_sensors_write_resolution(dev_data, i);
  84. mutex_unlock(&dev_data->lock);
  85. return ret;
  86. default:
  87. return -EINVAL;
  88. }
  89. }
  90. static const struct iio_chan_spec htu21_channels[] = {
  91. {
  92. .type = IIO_TEMP,
  93. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_PROCESSED),
  94. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
  95. },
  96. {
  97. .type = IIO_HUMIDITYRELATIVE,
  98. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_PROCESSED),
  99. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
  100. }
  101. };
  102. /*
  103. * Meas Spec recommendation is to not read temperature
  104. * on this driver part for MS8607
  105. */
  106. static const struct iio_chan_spec ms8607_channels[] = {
  107. {
  108. .type = IIO_HUMIDITYRELATIVE,
  109. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_PROCESSED),
  110. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
  111. }
  112. };
  113. static ssize_t htu21_show_battery_low(struct device *dev,
  114. struct device_attribute *attr, char *buf)
  115. {
  116. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  117. struct ms_ht_dev *dev_data = iio_priv(indio_dev);
  118. return ms_sensors_show_battery_low(dev_data, buf);
  119. }
  120. static ssize_t htu21_show_heater(struct device *dev,
  121. struct device_attribute *attr, char *buf)
  122. {
  123. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  124. struct ms_ht_dev *dev_data = iio_priv(indio_dev);
  125. return ms_sensors_show_heater(dev_data, buf);
  126. }
  127. static ssize_t htu21_write_heater(struct device *dev,
  128. struct device_attribute *attr,
  129. const char *buf, size_t len)
  130. {
  131. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  132. struct ms_ht_dev *dev_data = iio_priv(indio_dev);
  133. return ms_sensors_write_heater(dev_data, buf, len);
  134. }
  135. static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(htu21_show_samp_freq);
  136. static IIO_DEVICE_ATTR(battery_low, S_IRUGO,
  137. htu21_show_battery_low, NULL, 0);
  138. static IIO_DEVICE_ATTR(heater_enable, S_IRUGO | S_IWUSR,
  139. htu21_show_heater, htu21_write_heater, 0);
  140. static struct attribute *htu21_attributes[] = {
  141. &iio_const_attr_sampling_frequency_available.dev_attr.attr,
  142. &iio_dev_attr_battery_low.dev_attr.attr,
  143. &iio_dev_attr_heater_enable.dev_attr.attr,
  144. NULL,
  145. };
  146. static const struct attribute_group htu21_attribute_group = {
  147. .attrs = htu21_attributes,
  148. };
  149. static const struct iio_info htu21_info = {
  150. .read_raw = htu21_read_raw,
  151. .write_raw = htu21_write_raw,
  152. .attrs = &htu21_attribute_group,
  153. };
  154. static int htu21_probe(struct i2c_client *client)
  155. {
  156. const struct i2c_device_id *id = i2c_client_get_device_id(client);
  157. struct ms_ht_dev *dev_data;
  158. struct iio_dev *indio_dev;
  159. int ret;
  160. u64 serial_number;
  161. if (!i2c_check_functionality(client->adapter,
  162. I2C_FUNC_SMBUS_WRITE_BYTE_DATA |
  163. I2C_FUNC_SMBUS_WRITE_BYTE |
  164. I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
  165. dev_err(&client->dev,
  166. "Adapter does not support some i2c transaction\n");
  167. return -EOPNOTSUPP;
  168. }
  169. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*dev_data));
  170. if (!indio_dev)
  171. return -ENOMEM;
  172. dev_data = iio_priv(indio_dev);
  173. dev_data->client = client;
  174. dev_data->res_index = 0;
  175. mutex_init(&dev_data->lock);
  176. indio_dev->info = &htu21_info;
  177. indio_dev->name = id->name;
  178. indio_dev->modes = INDIO_DIRECT_MODE;
  179. if (id->driver_data == MS8607) {
  180. indio_dev->channels = ms8607_channels;
  181. indio_dev->num_channels = ARRAY_SIZE(ms8607_channels);
  182. } else {
  183. indio_dev->channels = htu21_channels;
  184. indio_dev->num_channels = ARRAY_SIZE(htu21_channels);
  185. }
  186. i2c_set_clientdata(client, indio_dev);
  187. ret = ms_sensors_reset(client, HTU21_RESET, 15000);
  188. if (ret)
  189. return ret;
  190. ret = ms_sensors_read_serial(client, &serial_number);
  191. if (ret)
  192. return ret;
  193. dev_info(&client->dev, "Serial number : %llx", serial_number);
  194. return devm_iio_device_register(&client->dev, indio_dev);
  195. }
  196. static const struct i2c_device_id htu21_id[] = {
  197. {"htu21", HTU21},
  198. {"ms8607-humidity", MS8607},
  199. { }
  200. };
  201. MODULE_DEVICE_TABLE(i2c, htu21_id);
  202. static const struct of_device_id htu21_of_match[] = {
  203. { .compatible = "meas,htu21", },
  204. { .compatible = "meas,ms8607-humidity", },
  205. { }
  206. };
  207. MODULE_DEVICE_TABLE(of, htu21_of_match);
  208. static struct i2c_driver htu21_driver = {
  209. .probe = htu21_probe,
  210. .id_table = htu21_id,
  211. .driver = {
  212. .name = "htu21",
  213. .of_match_table = htu21_of_match,
  214. },
  215. };
  216. module_i2c_driver(htu21_driver);
  217. MODULE_DESCRIPTION("Measurement-Specialties htu21 temperature and humidity driver");
  218. MODULE_AUTHOR("William Markezana <william.markezana@meas-spec.com>");
  219. MODULE_AUTHOR("Ludovic Tancerel <ludovic.tancerel@maplehightech.com>");
  220. MODULE_LICENSE("GPL v2");
  221. MODULE_IMPORT_NS("IIO_MEAS_SPEC_SENSORS");