hdc100x.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * hdc100x.c - Support for the TI HDC100x temperature + humidity sensors
  4. *
  5. * Copyright (C) 2015, 2018
  6. * Author: Matt Ranostay <matt.ranostay@konsulko.com>
  7. *
  8. * Datasheets:
  9. * http://www.ti.com/product/HDC1000/datasheet
  10. * http://www.ti.com/product/HDC1008/datasheet
  11. * http://www.ti.com/product/HDC1010/datasheet
  12. * http://www.ti.com/product/HDC1050/datasheet
  13. * http://www.ti.com/product/HDC1080/datasheet
  14. */
  15. #include <linux/delay.h>
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/i2c.h>
  19. #include <linux/iio/iio.h>
  20. #include <linux/iio/sysfs.h>
  21. #include <linux/iio/buffer.h>
  22. #include <linux/iio/trigger_consumer.h>
  23. #include <linux/iio/triggered_buffer.h>
  24. #define HDC100X_REG_TEMP 0x00
  25. #define HDC100X_REG_HUMIDITY 0x01
  26. #define HDC100X_REG_CONFIG 0x02
  27. #define HDC100X_REG_CONFIG_ACQ_MODE BIT(12)
  28. #define HDC100X_REG_CONFIG_HEATER_EN BIT(13)
  29. struct hdc100x_data {
  30. struct i2c_client *client;
  31. struct mutex lock;
  32. u16 config;
  33. /* integration time of the sensor */
  34. int adc_int_us[2];
  35. };
  36. /* integration time in us */
  37. static const int hdc100x_int_time[][3] = {
  38. { 6350, 3650, 0 }, /* IIO_TEMP channel*/
  39. { 6500, 3850, 2500 }, /* IIO_HUMIDITYRELATIVE channel */
  40. };
  41. /* HDC100X_REG_CONFIG shift and mask values */
  42. static const struct {
  43. int shift;
  44. int mask;
  45. } hdc100x_resolution_shift[2] = {
  46. { /* IIO_TEMP channel */
  47. .shift = 10,
  48. .mask = 1
  49. },
  50. { /* IIO_HUMIDITYRELATIVE channel */
  51. .shift = 8,
  52. .mask = 3,
  53. },
  54. };
  55. static IIO_CONST_ATTR(temp_integration_time_available,
  56. "0.00365 0.00635");
  57. static IIO_CONST_ATTR(humidityrelative_integration_time_available,
  58. "0.0025 0.00385 0.0065");
  59. static IIO_CONST_ATTR(out_current_heater_raw_available,
  60. "0 1");
  61. static struct attribute *hdc100x_attributes[] = {
  62. &iio_const_attr_temp_integration_time_available.dev_attr.attr,
  63. &iio_const_attr_humidityrelative_integration_time_available.dev_attr.attr,
  64. &iio_const_attr_out_current_heater_raw_available.dev_attr.attr,
  65. NULL
  66. };
  67. static const struct attribute_group hdc100x_attribute_group = {
  68. .attrs = hdc100x_attributes,
  69. };
  70. static const struct iio_chan_spec hdc100x_channels[] = {
  71. {
  72. .type = IIO_TEMP,
  73. .address = HDC100X_REG_TEMP,
  74. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  75. BIT(IIO_CHAN_INFO_SCALE) |
  76. BIT(IIO_CHAN_INFO_INT_TIME) |
  77. BIT(IIO_CHAN_INFO_OFFSET),
  78. .scan_index = 0,
  79. .scan_type = {
  80. .sign = 's',
  81. .realbits = 16,
  82. .storagebits = 16,
  83. .endianness = IIO_BE,
  84. },
  85. },
  86. {
  87. .type = IIO_HUMIDITYRELATIVE,
  88. .address = HDC100X_REG_HUMIDITY,
  89. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  90. BIT(IIO_CHAN_INFO_SCALE) |
  91. BIT(IIO_CHAN_INFO_INT_TIME),
  92. .scan_index = 1,
  93. .scan_type = {
  94. .sign = 'u',
  95. .realbits = 16,
  96. .storagebits = 16,
  97. .endianness = IIO_BE,
  98. },
  99. },
  100. {
  101. .type = IIO_CURRENT,
  102. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  103. .extend_name = "heater",
  104. .output = 1,
  105. .scan_index = -1,
  106. },
  107. IIO_CHAN_SOFT_TIMESTAMP(2),
  108. };
  109. static const unsigned long hdc100x_scan_masks[] = {0x3, 0};
  110. static int hdc100x_update_config(struct hdc100x_data *data, int mask, int val)
  111. {
  112. int tmp = (~mask & data->config) | val;
  113. int ret;
  114. ret = i2c_smbus_write_word_swapped(data->client,
  115. HDC100X_REG_CONFIG, tmp);
  116. if (!ret)
  117. data->config = tmp;
  118. return ret;
  119. }
  120. static int hdc100x_set_it_time(struct hdc100x_data *data, int chan, int val2)
  121. {
  122. int shift = hdc100x_resolution_shift[chan].shift;
  123. int ret = -EINVAL;
  124. int i;
  125. for (i = 0; i < ARRAY_SIZE(hdc100x_int_time[chan]); i++) {
  126. if (val2 && val2 == hdc100x_int_time[chan][i]) {
  127. ret = hdc100x_update_config(data,
  128. hdc100x_resolution_shift[chan].mask << shift,
  129. i << shift);
  130. if (!ret)
  131. data->adc_int_us[chan] = val2;
  132. break;
  133. }
  134. }
  135. return ret;
  136. }
  137. static int hdc100x_get_measurement(struct hdc100x_data *data,
  138. struct iio_chan_spec const *chan)
  139. {
  140. struct i2c_client *client = data->client;
  141. int delay = data->adc_int_us[chan->address];
  142. int ret;
  143. __be16 val;
  144. /* start measurement */
  145. ret = i2c_smbus_write_byte(client, chan->address);
  146. if (ret < 0) {
  147. dev_err(&client->dev, "cannot start measurement");
  148. return ret;
  149. }
  150. /* wait for integration time to pass */
  151. usleep_range(delay, delay + 1000);
  152. /* read measurement */
  153. ret = i2c_master_recv(data->client, (char *)&val, sizeof(val));
  154. if (ret < 0) {
  155. dev_err(&client->dev, "cannot read sensor data\n");
  156. return ret;
  157. }
  158. return be16_to_cpu(val);
  159. }
  160. static int hdc100x_get_heater_status(struct hdc100x_data *data)
  161. {
  162. return !!(data->config & HDC100X_REG_CONFIG_HEATER_EN);
  163. }
  164. static int hdc100x_read_raw(struct iio_dev *indio_dev,
  165. struct iio_chan_spec const *chan, int *val,
  166. int *val2, long mask)
  167. {
  168. struct hdc100x_data *data = iio_priv(indio_dev);
  169. switch (mask) {
  170. case IIO_CHAN_INFO_RAW: {
  171. int ret;
  172. mutex_lock(&data->lock);
  173. if (chan->type == IIO_CURRENT) {
  174. *val = hdc100x_get_heater_status(data);
  175. ret = IIO_VAL_INT;
  176. } else {
  177. ret = iio_device_claim_direct_mode(indio_dev);
  178. if (ret) {
  179. mutex_unlock(&data->lock);
  180. return ret;
  181. }
  182. ret = hdc100x_get_measurement(data, chan);
  183. iio_device_release_direct_mode(indio_dev);
  184. if (ret >= 0) {
  185. *val = ret;
  186. ret = IIO_VAL_INT;
  187. }
  188. }
  189. mutex_unlock(&data->lock);
  190. return ret;
  191. }
  192. case IIO_CHAN_INFO_INT_TIME:
  193. *val = 0;
  194. *val2 = data->adc_int_us[chan->address];
  195. return IIO_VAL_INT_PLUS_MICRO;
  196. case IIO_CHAN_INFO_SCALE:
  197. if (chan->type == IIO_TEMP) {
  198. *val = 165000;
  199. *val2 = 65536;
  200. return IIO_VAL_FRACTIONAL;
  201. } else {
  202. *val = 100;
  203. *val2 = 65536;
  204. return IIO_VAL_FRACTIONAL;
  205. }
  206. break;
  207. case IIO_CHAN_INFO_OFFSET:
  208. *val = -15887;
  209. *val2 = 515151;
  210. return IIO_VAL_INT_PLUS_MICRO;
  211. default:
  212. return -EINVAL;
  213. }
  214. }
  215. static int hdc100x_write_raw(struct iio_dev *indio_dev,
  216. struct iio_chan_spec const *chan,
  217. int val, int val2, long mask)
  218. {
  219. struct hdc100x_data *data = iio_priv(indio_dev);
  220. int ret = -EINVAL;
  221. switch (mask) {
  222. case IIO_CHAN_INFO_INT_TIME:
  223. if (val != 0)
  224. return -EINVAL;
  225. mutex_lock(&data->lock);
  226. ret = hdc100x_set_it_time(data, chan->address, val2);
  227. mutex_unlock(&data->lock);
  228. return ret;
  229. case IIO_CHAN_INFO_RAW:
  230. if (chan->type != IIO_CURRENT || val2 != 0)
  231. return -EINVAL;
  232. mutex_lock(&data->lock);
  233. ret = hdc100x_update_config(data, HDC100X_REG_CONFIG_HEATER_EN,
  234. val ? HDC100X_REG_CONFIG_HEATER_EN : 0);
  235. mutex_unlock(&data->lock);
  236. return ret;
  237. default:
  238. return -EINVAL;
  239. }
  240. }
  241. static int hdc100x_buffer_postenable(struct iio_dev *indio_dev)
  242. {
  243. struct hdc100x_data *data = iio_priv(indio_dev);
  244. int ret;
  245. /* Buffer is enabled. First set ACQ Mode, then attach poll func */
  246. mutex_lock(&data->lock);
  247. ret = hdc100x_update_config(data, HDC100X_REG_CONFIG_ACQ_MODE,
  248. HDC100X_REG_CONFIG_ACQ_MODE);
  249. mutex_unlock(&data->lock);
  250. if (ret)
  251. return ret;
  252. return iio_triggered_buffer_postenable(indio_dev);
  253. }
  254. static int hdc100x_buffer_predisable(struct iio_dev *indio_dev)
  255. {
  256. struct hdc100x_data *data = iio_priv(indio_dev);
  257. int ret;
  258. /* First detach poll func, then reset ACQ mode. OK to disable buffer */
  259. ret = iio_triggered_buffer_predisable(indio_dev);
  260. if (ret)
  261. return ret;
  262. mutex_lock(&data->lock);
  263. ret = hdc100x_update_config(data, HDC100X_REG_CONFIG_ACQ_MODE, 0);
  264. mutex_unlock(&data->lock);
  265. return ret;
  266. }
  267. static const struct iio_buffer_setup_ops hdc_buffer_setup_ops = {
  268. .postenable = hdc100x_buffer_postenable,
  269. .predisable = hdc100x_buffer_predisable,
  270. };
  271. static irqreturn_t hdc100x_trigger_handler(int irq, void *p)
  272. {
  273. struct iio_poll_func *pf = p;
  274. struct iio_dev *indio_dev = pf->indio_dev;
  275. struct hdc100x_data *data = iio_priv(indio_dev);
  276. struct i2c_client *client = data->client;
  277. int delay = data->adc_int_us[0] + data->adc_int_us[1];
  278. int ret;
  279. s16 buf[8]; /* 2x s16 + padding + 8 byte timestamp */
  280. /* dual read starts at temp register */
  281. mutex_lock(&data->lock);
  282. ret = i2c_smbus_write_byte(client, HDC100X_REG_TEMP);
  283. if (ret < 0) {
  284. dev_err(&client->dev, "cannot start measurement\n");
  285. goto err;
  286. }
  287. usleep_range(delay, delay + 1000);
  288. ret = i2c_master_recv(client, (u8 *)buf, 4);
  289. if (ret < 0) {
  290. dev_err(&client->dev, "cannot read sensor data\n");
  291. goto err;
  292. }
  293. iio_push_to_buffers_with_timestamp(indio_dev, buf,
  294. iio_get_time_ns(indio_dev));
  295. err:
  296. mutex_unlock(&data->lock);
  297. iio_trigger_notify_done(indio_dev->trig);
  298. return IRQ_HANDLED;
  299. }
  300. static const struct iio_info hdc100x_info = {
  301. .read_raw = hdc100x_read_raw,
  302. .write_raw = hdc100x_write_raw,
  303. .attrs = &hdc100x_attribute_group,
  304. };
  305. static int hdc100x_probe(struct i2c_client *client,
  306. const struct i2c_device_id *id)
  307. {
  308. struct iio_dev *indio_dev;
  309. struct hdc100x_data *data;
  310. int ret;
  311. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA |
  312. I2C_FUNC_SMBUS_BYTE | I2C_FUNC_I2C))
  313. return -EOPNOTSUPP;
  314. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  315. if (!indio_dev)
  316. return -ENOMEM;
  317. data = iio_priv(indio_dev);
  318. i2c_set_clientdata(client, indio_dev);
  319. data->client = client;
  320. mutex_init(&data->lock);
  321. indio_dev->dev.parent = &client->dev;
  322. indio_dev->name = dev_name(&client->dev);
  323. indio_dev->modes = INDIO_DIRECT_MODE;
  324. indio_dev->info = &hdc100x_info;
  325. indio_dev->channels = hdc100x_channels;
  326. indio_dev->num_channels = ARRAY_SIZE(hdc100x_channels);
  327. indio_dev->available_scan_masks = hdc100x_scan_masks;
  328. /* be sure we are in a known state */
  329. hdc100x_set_it_time(data, 0, hdc100x_int_time[0][0]);
  330. hdc100x_set_it_time(data, 1, hdc100x_int_time[1][0]);
  331. hdc100x_update_config(data, HDC100X_REG_CONFIG_ACQ_MODE, 0);
  332. ret = iio_triggered_buffer_setup(indio_dev, NULL,
  333. hdc100x_trigger_handler,
  334. &hdc_buffer_setup_ops);
  335. if (ret < 0) {
  336. dev_err(&client->dev, "iio triggered buffer setup failed\n");
  337. return ret;
  338. }
  339. ret = iio_device_register(indio_dev);
  340. if (ret < 0)
  341. iio_triggered_buffer_cleanup(indio_dev);
  342. return ret;
  343. }
  344. static int hdc100x_remove(struct i2c_client *client)
  345. {
  346. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  347. iio_device_unregister(indio_dev);
  348. iio_triggered_buffer_cleanup(indio_dev);
  349. return 0;
  350. }
  351. static const struct i2c_device_id hdc100x_id[] = {
  352. { "hdc100x", 0 },
  353. { "hdc1000", 0 },
  354. { "hdc1008", 0 },
  355. { "hdc1010", 0 },
  356. { "hdc1050", 0 },
  357. { "hdc1080", 0 },
  358. { }
  359. };
  360. MODULE_DEVICE_TABLE(i2c, hdc100x_id);
  361. static const struct of_device_id hdc100x_dt_ids[] = {
  362. { .compatible = "ti,hdc1000" },
  363. { .compatible = "ti,hdc1008" },
  364. { .compatible = "ti,hdc1010" },
  365. { .compatible = "ti,hdc1050" },
  366. { .compatible = "ti,hdc1080" },
  367. { }
  368. };
  369. MODULE_DEVICE_TABLE(of, hdc100x_dt_ids);
  370. static struct i2c_driver hdc100x_driver = {
  371. .driver = {
  372. .name = "hdc100x",
  373. .of_match_table = of_match_ptr(hdc100x_dt_ids),
  374. },
  375. .probe = hdc100x_probe,
  376. .remove = hdc100x_remove,
  377. .id_table = hdc100x_id,
  378. };
  379. module_i2c_driver(hdc100x_driver);
  380. MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>");
  381. MODULE_DESCRIPTION("TI HDC100x humidity and temperature sensor driver");
  382. MODULE_LICENSE("GPL");