hdc2010.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * hdc2010.c - Support for the TI HDC2010 and HDC2080
  4. * temperature + relative humidity sensors
  5. *
  6. * Copyright (C) 2020 Norphonic AS
  7. * Author: Eugene Zaikonnikov <ez@norphonic.com>
  8. *
  9. * Datasheet: https://www.ti.com/product/HDC2010/datasheet
  10. * Datasheet: https://www.ti.com/product/HDC2080/datasheet
  11. */
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/i2c.h>
  15. #include <linux/bitops.h>
  16. #include <linux/iio/iio.h>
  17. #include <linux/iio/sysfs.h>
  18. #define HDC2010_REG_TEMP_LOW 0x00
  19. #define HDC2010_REG_TEMP_HIGH 0x01
  20. #define HDC2010_REG_HUMIDITY_LOW 0x02
  21. #define HDC2010_REG_HUMIDITY_HIGH 0x03
  22. #define HDC2010_REG_INTERRUPT_DRDY 0x04
  23. #define HDC2010_REG_TEMP_MAX 0x05
  24. #define HDC2010_REG_HUMIDITY_MAX 0x06
  25. #define HDC2010_REG_INTERRUPT_EN 0x07
  26. #define HDC2010_REG_TEMP_OFFSET_ADJ 0x08
  27. #define HDC2010_REG_HUMIDITY_OFFSET_ADJ 0x09
  28. #define HDC2010_REG_TEMP_THR_L 0x0a
  29. #define HDC2010_REG_TEMP_THR_H 0x0b
  30. #define HDC2010_REG_RH_THR_L 0x0c
  31. #define HDC2010_REG_RH_THR_H 0x0d
  32. #define HDC2010_REG_RESET_DRDY_INT_CONF 0x0e
  33. #define HDC2010_REG_MEASUREMENT_CONF 0x0f
  34. #define HDC2010_MEAS_CONF GENMASK(2, 1)
  35. #define HDC2010_MEAS_TRIG BIT(0)
  36. #define HDC2010_HEATER_EN BIT(3)
  37. #define HDC2010_AMM GENMASK(6, 4)
  38. struct hdc2010_data {
  39. struct i2c_client *client;
  40. struct mutex lock;
  41. u8 measurement_config;
  42. u8 interrupt_config;
  43. u8 drdy_config;
  44. };
  45. enum hdc2010_addr_groups {
  46. HDC2010_GROUP_TEMP = 0,
  47. HDC2010_GROUP_HUMIDITY,
  48. };
  49. struct hdc2010_reg_record {
  50. unsigned long primary;
  51. unsigned long peak;
  52. };
  53. static const struct hdc2010_reg_record hdc2010_reg_translation[] = {
  54. [HDC2010_GROUP_TEMP] = {
  55. .primary = HDC2010_REG_TEMP_LOW,
  56. .peak = HDC2010_REG_TEMP_MAX,
  57. },
  58. [HDC2010_GROUP_HUMIDITY] = {
  59. .primary = HDC2010_REG_HUMIDITY_LOW,
  60. .peak = HDC2010_REG_HUMIDITY_MAX,
  61. },
  62. };
  63. static IIO_CONST_ATTR(out_current_heater_raw_available, "0 1");
  64. static struct attribute *hdc2010_attributes[] = {
  65. &iio_const_attr_out_current_heater_raw_available.dev_attr.attr,
  66. NULL
  67. };
  68. static const struct attribute_group hdc2010_attribute_group = {
  69. .attrs = hdc2010_attributes,
  70. };
  71. static const struct iio_chan_spec hdc2010_channels[] = {
  72. {
  73. .type = IIO_TEMP,
  74. .address = HDC2010_GROUP_TEMP,
  75. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  76. BIT(IIO_CHAN_INFO_PEAK) |
  77. BIT(IIO_CHAN_INFO_OFFSET) |
  78. BIT(IIO_CHAN_INFO_SCALE),
  79. },
  80. {
  81. .type = IIO_HUMIDITYRELATIVE,
  82. .address = HDC2010_GROUP_HUMIDITY,
  83. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  84. BIT(IIO_CHAN_INFO_PEAK) |
  85. BIT(IIO_CHAN_INFO_SCALE),
  86. },
  87. {
  88. .type = IIO_CURRENT,
  89. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  90. .extend_name = "heater",
  91. .output = 1,
  92. },
  93. };
  94. static int hdc2010_update_drdy_config(struct hdc2010_data *data,
  95. char mask, char val)
  96. {
  97. u8 tmp = (~mask & data->drdy_config) | val;
  98. int ret;
  99. ret = i2c_smbus_write_byte_data(data->client,
  100. HDC2010_REG_RESET_DRDY_INT_CONF, tmp);
  101. if (ret)
  102. return ret;
  103. data->drdy_config = tmp;
  104. return 0;
  105. }
  106. static int hdc2010_get_prim_measurement_word(struct hdc2010_data *data,
  107. struct iio_chan_spec const *chan)
  108. {
  109. struct i2c_client *client = data->client;
  110. s32 ret;
  111. ret = i2c_smbus_read_word_data(client,
  112. hdc2010_reg_translation[chan->address].primary);
  113. if (ret < 0)
  114. dev_err(&client->dev, "Could not read sensor measurement word\n");
  115. return ret;
  116. }
  117. static int hdc2010_get_peak_measurement_byte(struct hdc2010_data *data,
  118. struct iio_chan_spec const *chan)
  119. {
  120. struct i2c_client *client = data->client;
  121. s32 ret;
  122. ret = i2c_smbus_read_byte_data(client,
  123. hdc2010_reg_translation[chan->address].peak);
  124. if (ret < 0)
  125. dev_err(&client->dev, "Could not read sensor measurement byte\n");
  126. return ret;
  127. }
  128. static int hdc2010_get_heater_status(struct hdc2010_data *data)
  129. {
  130. return !!(data->drdy_config & HDC2010_HEATER_EN);
  131. }
  132. static int hdc2010_read_raw(struct iio_dev *indio_dev,
  133. struct iio_chan_spec const *chan, int *val,
  134. int *val2, long mask)
  135. {
  136. struct hdc2010_data *data = iio_priv(indio_dev);
  137. switch (mask) {
  138. case IIO_CHAN_INFO_RAW: {
  139. int ret;
  140. if (chan->type == IIO_CURRENT) {
  141. *val = hdc2010_get_heater_status(data);
  142. return IIO_VAL_INT;
  143. }
  144. if (!iio_device_claim_direct(indio_dev))
  145. return -EBUSY;
  146. mutex_lock(&data->lock);
  147. ret = hdc2010_get_prim_measurement_word(data, chan);
  148. mutex_unlock(&data->lock);
  149. iio_device_release_direct(indio_dev);
  150. if (ret < 0)
  151. return ret;
  152. *val = ret;
  153. return IIO_VAL_INT;
  154. }
  155. case IIO_CHAN_INFO_PEAK: {
  156. int ret;
  157. if (!iio_device_claim_direct(indio_dev))
  158. return -EBUSY;
  159. mutex_lock(&data->lock);
  160. ret = hdc2010_get_peak_measurement_byte(data, chan);
  161. mutex_unlock(&data->lock);
  162. iio_device_release_direct(indio_dev);
  163. if (ret < 0)
  164. return ret;
  165. /* Scaling up the value so we can use same offset as RAW */
  166. *val = ret * 256;
  167. return IIO_VAL_INT;
  168. }
  169. case IIO_CHAN_INFO_SCALE:
  170. *val2 = 65536;
  171. if (chan->type == IIO_TEMP)
  172. *val = 165000;
  173. else
  174. *val = 100000;
  175. return IIO_VAL_FRACTIONAL;
  176. case IIO_CHAN_INFO_OFFSET:
  177. *val = -15887;
  178. *val2 = 515151;
  179. return IIO_VAL_INT_PLUS_MICRO;
  180. default:
  181. return -EINVAL;
  182. }
  183. }
  184. static int hdc2010_write_raw(struct iio_dev *indio_dev,
  185. struct iio_chan_spec const *chan,
  186. int val, int val2, long mask)
  187. {
  188. struct hdc2010_data *data = iio_priv(indio_dev);
  189. int new, ret;
  190. switch (mask) {
  191. case IIO_CHAN_INFO_RAW:
  192. if (chan->type != IIO_CURRENT || val2 != 0)
  193. return -EINVAL;
  194. switch (val) {
  195. case 1:
  196. new = HDC2010_HEATER_EN;
  197. break;
  198. case 0:
  199. new = 0;
  200. break;
  201. default:
  202. return -EINVAL;
  203. }
  204. mutex_lock(&data->lock);
  205. ret = hdc2010_update_drdy_config(data, HDC2010_HEATER_EN, new);
  206. mutex_unlock(&data->lock);
  207. return ret;
  208. default:
  209. return -EINVAL;
  210. }
  211. }
  212. static const struct iio_info hdc2010_info = {
  213. .read_raw = hdc2010_read_raw,
  214. .write_raw = hdc2010_write_raw,
  215. .attrs = &hdc2010_attribute_group,
  216. };
  217. static int hdc2010_probe(struct i2c_client *client)
  218. {
  219. struct iio_dev *indio_dev;
  220. struct hdc2010_data *data;
  221. u8 tmp;
  222. int ret;
  223. if (!i2c_check_functionality(client->adapter,
  224. I2C_FUNC_SMBUS_WORD_DATA | I2C_FUNC_SMBUS_BYTE | I2C_FUNC_I2C))
  225. return -EOPNOTSUPP;
  226. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  227. if (!indio_dev)
  228. return -ENOMEM;
  229. data = iio_priv(indio_dev);
  230. i2c_set_clientdata(client, indio_dev);
  231. data->client = client;
  232. mutex_init(&data->lock);
  233. /*
  234. * As DEVICE ID register does not differentiate between
  235. * HDC2010 and HDC2080, we have the name hardcoded
  236. */
  237. indio_dev->name = "hdc2010";
  238. indio_dev->modes = INDIO_DIRECT_MODE;
  239. indio_dev->info = &hdc2010_info;
  240. indio_dev->channels = hdc2010_channels;
  241. indio_dev->num_channels = ARRAY_SIZE(hdc2010_channels);
  242. /* Enable Automatic Measurement Mode at 5Hz */
  243. ret = hdc2010_update_drdy_config(data, HDC2010_AMM, HDC2010_AMM);
  244. if (ret)
  245. return ret;
  246. /*
  247. * We enable both temp and humidity measurement.
  248. * However the measurement won't start even in AMM until triggered.
  249. */
  250. tmp = (data->measurement_config & ~HDC2010_MEAS_CONF) |
  251. HDC2010_MEAS_TRIG;
  252. ret = i2c_smbus_write_byte_data(client, HDC2010_REG_MEASUREMENT_CONF, tmp);
  253. if (ret) {
  254. dev_warn(&client->dev, "Unable to set up measurement\n");
  255. if (hdc2010_update_drdy_config(data, HDC2010_AMM, 0))
  256. dev_warn(&client->dev, "Unable to restore default AMM\n");
  257. return ret;
  258. }
  259. data->measurement_config = tmp;
  260. return iio_device_register(indio_dev);
  261. }
  262. static void hdc2010_remove(struct i2c_client *client)
  263. {
  264. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  265. struct hdc2010_data *data = iio_priv(indio_dev);
  266. iio_device_unregister(indio_dev);
  267. /* Disable Automatic Measurement Mode */
  268. if (hdc2010_update_drdy_config(data, HDC2010_AMM, 0))
  269. dev_warn(&client->dev, "Unable to restore default AMM\n");
  270. }
  271. static const struct i2c_device_id hdc2010_id[] = {
  272. { "hdc2010" },
  273. { "hdc2080" },
  274. { }
  275. };
  276. MODULE_DEVICE_TABLE(i2c, hdc2010_id);
  277. static const struct of_device_id hdc2010_dt_ids[] = {
  278. { .compatible = "ti,hdc2010" },
  279. { .compatible = "ti,hdc2080" },
  280. { }
  281. };
  282. MODULE_DEVICE_TABLE(of, hdc2010_dt_ids);
  283. static struct i2c_driver hdc2010_driver = {
  284. .driver = {
  285. .name = "hdc2010",
  286. .of_match_table = hdc2010_dt_ids,
  287. },
  288. .probe = hdc2010_probe,
  289. .remove = hdc2010_remove,
  290. .id_table = hdc2010_id,
  291. };
  292. module_i2c_driver(hdc2010_driver);
  293. MODULE_AUTHOR("Eugene Zaikonnikov <ez@norphonic.com>");
  294. MODULE_DESCRIPTION("TI HDC2010 humidity and temperature sensor driver");
  295. MODULE_LICENSE("GPL");