hdc100x.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * hdc100x.c - Support for the TI HDC100x temperature + humidity sensors
  3. *
  4. * Copyright (C) 2015 Matt Ranostay <mranostay@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. */
  17. #include <linux/delay.h>
  18. #include <linux/module.h>
  19. #include <linux/init.h>
  20. #include <linux/i2c.h>
  21. #include <linux/iio/iio.h>
  22. #include <linux/iio/sysfs.h>
  23. #define HDC100X_REG_TEMP 0x00
  24. #define HDC100X_REG_HUMIDITY 0x01
  25. #define HDC100X_REG_CONFIG 0x02
  26. #define HDC100X_REG_CONFIG_HEATER_EN BIT(13)
  27. struct hdc100x_data {
  28. struct i2c_client *client;
  29. struct mutex lock;
  30. u16 config;
  31. /* integration time of the sensor */
  32. int adc_int_us[2];
  33. };
  34. /* integration time in us */
  35. static const int hdc100x_int_time[][3] = {
  36. { 6350, 3650, 0 }, /* IIO_TEMP channel*/
  37. { 6500, 3850, 2500 }, /* IIO_HUMIDITYRELATIVE channel */
  38. };
  39. /* HDC100X_REG_CONFIG shift and mask values */
  40. static const struct {
  41. int shift;
  42. int mask;
  43. } hdc100x_resolution_shift[2] = {
  44. { /* IIO_TEMP channel */
  45. .shift = 10,
  46. .mask = 1
  47. },
  48. { /* IIO_HUMIDITYRELATIVE channel */
  49. .shift = 8,
  50. .mask = 3,
  51. },
  52. };
  53. static IIO_CONST_ATTR(temp_integration_time_available,
  54. "0.00365 0.00635");
  55. static IIO_CONST_ATTR(humidityrelative_integration_time_available,
  56. "0.0025 0.00385 0.0065");
  57. static IIO_CONST_ATTR(out_current_heater_raw_available,
  58. "0 1");
  59. static struct attribute *hdc100x_attributes[] = {
  60. &iio_const_attr_temp_integration_time_available.dev_attr.attr,
  61. &iio_const_attr_humidityrelative_integration_time_available.dev_attr.attr,
  62. &iio_const_attr_out_current_heater_raw_available.dev_attr.attr,
  63. NULL
  64. };
  65. static struct attribute_group hdc100x_attribute_group = {
  66. .attrs = hdc100x_attributes,
  67. };
  68. static const struct iio_chan_spec hdc100x_channels[] = {
  69. {
  70. .type = IIO_TEMP,
  71. .address = HDC100X_REG_TEMP,
  72. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  73. BIT(IIO_CHAN_INFO_SCALE) |
  74. BIT(IIO_CHAN_INFO_INT_TIME) |
  75. BIT(IIO_CHAN_INFO_OFFSET),
  76. },
  77. {
  78. .type = IIO_HUMIDITYRELATIVE,
  79. .address = HDC100X_REG_HUMIDITY,
  80. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  81. BIT(IIO_CHAN_INFO_SCALE) |
  82. BIT(IIO_CHAN_INFO_INT_TIME)
  83. },
  84. {
  85. .type = IIO_CURRENT,
  86. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  87. .extend_name = "heater",
  88. .output = 1,
  89. },
  90. };
  91. static int hdc100x_update_config(struct hdc100x_data *data, int mask, int val)
  92. {
  93. int tmp = (~mask & data->config) | val;
  94. int ret;
  95. ret = i2c_smbus_write_word_swapped(data->client,
  96. HDC100X_REG_CONFIG, tmp);
  97. if (!ret)
  98. data->config = tmp;
  99. return ret;
  100. }
  101. static int hdc100x_set_it_time(struct hdc100x_data *data, int chan, int val2)
  102. {
  103. int shift = hdc100x_resolution_shift[chan].shift;
  104. int ret = -EINVAL;
  105. int i;
  106. for (i = 0; i < ARRAY_SIZE(hdc100x_int_time[chan]); i++) {
  107. if (val2 && val2 == hdc100x_int_time[chan][i]) {
  108. ret = hdc100x_update_config(data,
  109. hdc100x_resolution_shift[chan].mask << shift,
  110. i << shift);
  111. if (!ret)
  112. data->adc_int_us[chan] = val2;
  113. break;
  114. }
  115. }
  116. return ret;
  117. }
  118. static int hdc100x_get_measurement(struct hdc100x_data *data,
  119. struct iio_chan_spec const *chan)
  120. {
  121. struct i2c_client *client = data->client;
  122. int delay = data->adc_int_us[chan->address];
  123. int ret;
  124. __be16 val;
  125. /* start measurement */
  126. ret = i2c_smbus_write_byte(client, chan->address);
  127. if (ret < 0) {
  128. dev_err(&client->dev, "cannot start measurement");
  129. return ret;
  130. }
  131. /* wait for integration time to pass */
  132. usleep_range(delay, delay + 1000);
  133. /* read measurement */
  134. ret = i2c_master_recv(data->client, (char *)&val, sizeof(val));
  135. if (ret < 0) {
  136. dev_err(&client->dev, "cannot read sensor data\n");
  137. return ret;
  138. }
  139. return be16_to_cpu(val);
  140. }
  141. static int hdc100x_get_heater_status(struct hdc100x_data *data)
  142. {
  143. return !!(data->config & HDC100X_REG_CONFIG_HEATER_EN);
  144. }
  145. static int hdc100x_read_raw(struct iio_dev *indio_dev,
  146. struct iio_chan_spec const *chan, int *val,
  147. int *val2, long mask)
  148. {
  149. struct hdc100x_data *data = iio_priv(indio_dev);
  150. switch (mask) {
  151. case IIO_CHAN_INFO_RAW: {
  152. int ret;
  153. mutex_lock(&data->lock);
  154. if (chan->type == IIO_CURRENT) {
  155. *val = hdc100x_get_heater_status(data);
  156. ret = IIO_VAL_INT;
  157. } else {
  158. ret = hdc100x_get_measurement(data, chan);
  159. if (ret >= 0) {
  160. *val = ret;
  161. ret = IIO_VAL_INT;
  162. }
  163. }
  164. mutex_unlock(&data->lock);
  165. return ret;
  166. }
  167. case IIO_CHAN_INFO_INT_TIME:
  168. *val = 0;
  169. *val2 = data->adc_int_us[chan->address];
  170. return IIO_VAL_INT_PLUS_MICRO;
  171. case IIO_CHAN_INFO_SCALE:
  172. if (chan->type == IIO_TEMP) {
  173. *val = 165000;
  174. *val2 = 65536;
  175. return IIO_VAL_FRACTIONAL;
  176. } else {
  177. *val = 100;
  178. *val2 = 65536;
  179. return IIO_VAL_FRACTIONAL;
  180. }
  181. break;
  182. case IIO_CHAN_INFO_OFFSET:
  183. *val = -15887;
  184. *val2 = 515151;
  185. return IIO_VAL_INT_PLUS_MICRO;
  186. default:
  187. return -EINVAL;
  188. }
  189. }
  190. static int hdc100x_write_raw(struct iio_dev *indio_dev,
  191. struct iio_chan_spec const *chan,
  192. int val, int val2, long mask)
  193. {
  194. struct hdc100x_data *data = iio_priv(indio_dev);
  195. int ret = -EINVAL;
  196. switch (mask) {
  197. case IIO_CHAN_INFO_INT_TIME:
  198. if (val != 0)
  199. return -EINVAL;
  200. mutex_lock(&data->lock);
  201. ret = hdc100x_set_it_time(data, chan->address, val2);
  202. mutex_unlock(&data->lock);
  203. return ret;
  204. case IIO_CHAN_INFO_RAW:
  205. if (chan->type != IIO_CURRENT || val2 != 0)
  206. return -EINVAL;
  207. mutex_lock(&data->lock);
  208. ret = hdc100x_update_config(data, HDC100X_REG_CONFIG_HEATER_EN,
  209. val ? HDC100X_REG_CONFIG_HEATER_EN : 0);
  210. mutex_unlock(&data->lock);
  211. return ret;
  212. default:
  213. return -EINVAL;
  214. }
  215. }
  216. static const struct iio_info hdc100x_info = {
  217. .read_raw = hdc100x_read_raw,
  218. .write_raw = hdc100x_write_raw,
  219. .attrs = &hdc100x_attribute_group,
  220. .driver_module = THIS_MODULE,
  221. };
  222. static int hdc100x_probe(struct i2c_client *client,
  223. const struct i2c_device_id *id)
  224. {
  225. struct iio_dev *indio_dev;
  226. struct hdc100x_data *data;
  227. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA |
  228. I2C_FUNC_SMBUS_BYTE | I2C_FUNC_I2C))
  229. return -EOPNOTSUPP;
  230. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  231. if (!indio_dev)
  232. return -ENOMEM;
  233. data = iio_priv(indio_dev);
  234. i2c_set_clientdata(client, indio_dev);
  235. data->client = client;
  236. mutex_init(&data->lock);
  237. indio_dev->dev.parent = &client->dev;
  238. indio_dev->name = dev_name(&client->dev);
  239. indio_dev->modes = INDIO_DIRECT_MODE;
  240. indio_dev->info = &hdc100x_info;
  241. indio_dev->channels = hdc100x_channels;
  242. indio_dev->num_channels = ARRAY_SIZE(hdc100x_channels);
  243. /* be sure we are in a known state */
  244. hdc100x_set_it_time(data, 0, hdc100x_int_time[0][0]);
  245. hdc100x_set_it_time(data, 1, hdc100x_int_time[1][0]);
  246. return devm_iio_device_register(&client->dev, indio_dev);
  247. }
  248. static const struct i2c_device_id hdc100x_id[] = {
  249. { "hdc100x", 0 },
  250. { }
  251. };
  252. MODULE_DEVICE_TABLE(i2c, hdc100x_id);
  253. static struct i2c_driver hdc100x_driver = {
  254. .driver = {
  255. .name = "hdc100x",
  256. },
  257. .probe = hdc100x_probe,
  258. .id_table = hdc100x_id,
  259. };
  260. module_i2c_driver(hdc100x_driver);
  261. MODULE_AUTHOR("Matt Ranostay <mranostay@gmail.com>");
  262. MODULE_DESCRIPTION("TI HDC100x humidity and temperature sensor driver");
  263. MODULE_LICENSE("GPL");