hdc100x.c 11 KB

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