hdc100x.c 11 KB

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