hdc100x.c 10 KB

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