htu21.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * htu21.c - Support for Measurement-Specialties
  3. * htu21 temperature & humidity sensor
  4. * and humidity part of MS8607 sensor
  5. *
  6. * Copyright (c) 2014 Measurement-Specialties
  7. *
  8. * Licensed under the GPL-2.
  9. *
  10. * (7-bit I2C slave address 0x40)
  11. *
  12. * Datasheet:
  13. * http://www.meas-spec.com/downloads/HTU21D.pdf
  14. * Datasheet:
  15. * http://www.meas-spec.com/downloads/MS8607-02BA01.pdf
  16. */
  17. #include <linux/init.h>
  18. #include <linux/device.h>
  19. #include <linux/kernel.h>
  20. #include <linux/stat.h>
  21. #include <linux/module.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. .driver_module = THIS_MODULE,
  154. };
  155. static int htu21_probe(struct i2c_client *client,
  156. const struct i2c_device_id *id)
  157. {
  158. struct ms_ht_dev *dev_data;
  159. struct iio_dev *indio_dev;
  160. int ret;
  161. u64 serial_number;
  162. if (!i2c_check_functionality(client->adapter,
  163. I2C_FUNC_SMBUS_WRITE_BYTE_DATA |
  164. I2C_FUNC_SMBUS_WRITE_BYTE |
  165. I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
  166. dev_err(&client->dev,
  167. "Adapter does not support some i2c transaction\n");
  168. return -EOPNOTSUPP;
  169. }
  170. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*dev_data));
  171. if (!indio_dev)
  172. return -ENOMEM;
  173. dev_data = iio_priv(indio_dev);
  174. dev_data->client = client;
  175. dev_data->res_index = 0;
  176. mutex_init(&dev_data->lock);
  177. indio_dev->info = &htu21_info;
  178. indio_dev->name = id->name;
  179. indio_dev->dev.parent = &client->dev;
  180. indio_dev->modes = INDIO_DIRECT_MODE;
  181. if (id->driver_data == MS8607) {
  182. indio_dev->channels = ms8607_channels;
  183. indio_dev->num_channels = ARRAY_SIZE(ms8607_channels);
  184. } else {
  185. indio_dev->channels = htu21_channels;
  186. indio_dev->num_channels = ARRAY_SIZE(htu21_channels);
  187. }
  188. i2c_set_clientdata(client, indio_dev);
  189. ret = ms_sensors_reset(client, HTU21_RESET, 15000);
  190. if (ret)
  191. return ret;
  192. ret = ms_sensors_read_serial(client, &serial_number);
  193. if (ret)
  194. return ret;
  195. dev_info(&client->dev, "Serial number : %llx", serial_number);
  196. return devm_iio_device_register(&client->dev, indio_dev);
  197. }
  198. static const struct i2c_device_id htu21_id[] = {
  199. {"htu21", HTU21},
  200. {"ms8607-humidity", MS8607},
  201. {}
  202. };
  203. MODULE_DEVICE_TABLE(i2c, htu21_id);
  204. static const struct of_device_id htu21_of_match[] = {
  205. { .compatible = "meas,htu21", },
  206. { .compatible = "meas,ms8607-humidity", },
  207. { },
  208. };
  209. MODULE_DEVICE_TABLE(of, htu21_of_match);
  210. static struct i2c_driver htu21_driver = {
  211. .probe = htu21_probe,
  212. .id_table = htu21_id,
  213. .driver = {
  214. .name = "htu21",
  215. .of_match_table = of_match_ptr(htu21_of_match),
  216. },
  217. };
  218. module_i2c_driver(htu21_driver);
  219. MODULE_DESCRIPTION("Measurement-Specialties htu21 temperature and humidity driver");
  220. MODULE_AUTHOR("William Markezana <william.markezana@meas-spec.com>");
  221. MODULE_AUTHOR("Ludovic Tancerel <ludovic.tancerel@maplehightech.com>");
  222. MODULE_LICENSE("GPL v2");