hdc100x.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. * Datasheets:
  17. * http://www.ti.com/product/HDC1000/datasheet
  18. * http://www.ti.com/product/HDC1008/datasheet
  19. * http://www.ti.com/product/HDC1010/datasheet
  20. * http://www.ti.com/product/HDC1050/datasheet
  21. * http://www.ti.com/product/HDC1080/datasheet
  22. */
  23. #include <linux/delay.h>
  24. #include <linux/module.h>
  25. #include <linux/init.h>
  26. #include <linux/i2c.h>
  27. #include <linux/iio/iio.h>
  28. #include <linux/iio/sysfs.h>
  29. #include <linux/iio/buffer.h>
  30. #include <linux/iio/trigger_consumer.h>
  31. #include <linux/iio/triggered_buffer.h>
  32. #define HDC100X_REG_TEMP 0x00
  33. #define HDC100X_REG_HUMIDITY 0x01
  34. #define HDC100X_REG_CONFIG 0x02
  35. #define HDC100X_REG_CONFIG_ACQ_MODE BIT(12)
  36. #define HDC100X_REG_CONFIG_HEATER_EN BIT(13)
  37. struct hdc100x_data {
  38. struct i2c_client *client;
  39. struct mutex lock;
  40. u16 config;
  41. /* integration time of the sensor */
  42. int adc_int_us[2];
  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];
  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. mutex_lock(&data->lock);
  181. if (chan->type == IIO_CURRENT) {
  182. *val = hdc100x_get_heater_status(data);
  183. ret = IIO_VAL_INT;
  184. } else {
  185. ret = iio_device_claim_direct_mode(indio_dev);
  186. if (ret) {
  187. mutex_unlock(&data->lock);
  188. return ret;
  189. }
  190. ret = hdc100x_get_measurement(data, chan);
  191. iio_device_release_direct_mode(indio_dev);
  192. if (ret >= 0) {
  193. *val = ret;
  194. ret = IIO_VAL_INT;
  195. }
  196. }
  197. mutex_unlock(&data->lock);
  198. return ret;
  199. }
  200. case IIO_CHAN_INFO_INT_TIME:
  201. *val = 0;
  202. *val2 = data->adc_int_us[chan->address];
  203. return IIO_VAL_INT_PLUS_MICRO;
  204. case IIO_CHAN_INFO_SCALE:
  205. if (chan->type == IIO_TEMP) {
  206. *val = 165000;
  207. *val2 = 65536;
  208. return IIO_VAL_FRACTIONAL;
  209. } else {
  210. *val = 100;
  211. *val2 = 65536;
  212. return IIO_VAL_FRACTIONAL;
  213. }
  214. break;
  215. case IIO_CHAN_INFO_OFFSET:
  216. *val = -15887;
  217. *val2 = 515151;
  218. return IIO_VAL_INT_PLUS_MICRO;
  219. default:
  220. return -EINVAL;
  221. }
  222. }
  223. static int hdc100x_write_raw(struct iio_dev *indio_dev,
  224. struct iio_chan_spec const *chan,
  225. int val, int val2, long mask)
  226. {
  227. struct hdc100x_data *data = iio_priv(indio_dev);
  228. int ret = -EINVAL;
  229. switch (mask) {
  230. case IIO_CHAN_INFO_INT_TIME:
  231. if (val != 0)
  232. return -EINVAL;
  233. mutex_lock(&data->lock);
  234. ret = hdc100x_set_it_time(data, chan->address, val2);
  235. mutex_unlock(&data->lock);
  236. return ret;
  237. case IIO_CHAN_INFO_RAW:
  238. if (chan->type != IIO_CURRENT || val2 != 0)
  239. return -EINVAL;
  240. mutex_lock(&data->lock);
  241. ret = hdc100x_update_config(data, HDC100X_REG_CONFIG_HEATER_EN,
  242. val ? HDC100X_REG_CONFIG_HEATER_EN : 0);
  243. mutex_unlock(&data->lock);
  244. return ret;
  245. default:
  246. return -EINVAL;
  247. }
  248. }
  249. static int hdc100x_buffer_postenable(struct iio_dev *indio_dev)
  250. {
  251. struct hdc100x_data *data = iio_priv(indio_dev);
  252. int ret;
  253. /* Buffer is enabled. First set ACQ Mode, then attach poll func */
  254. mutex_lock(&data->lock);
  255. ret = hdc100x_update_config(data, HDC100X_REG_CONFIG_ACQ_MODE,
  256. HDC100X_REG_CONFIG_ACQ_MODE);
  257. mutex_unlock(&data->lock);
  258. if (ret)
  259. return ret;
  260. return iio_triggered_buffer_postenable(indio_dev);
  261. }
  262. static int hdc100x_buffer_predisable(struct iio_dev *indio_dev)
  263. {
  264. struct hdc100x_data *data = iio_priv(indio_dev);
  265. int ret;
  266. /* First detach poll func, then reset ACQ mode. OK to disable buffer */
  267. ret = iio_triggered_buffer_predisable(indio_dev);
  268. if (ret)
  269. return ret;
  270. mutex_lock(&data->lock);
  271. ret = hdc100x_update_config(data, HDC100X_REG_CONFIG_ACQ_MODE, 0);
  272. mutex_unlock(&data->lock);
  273. return ret;
  274. }
  275. static const struct iio_buffer_setup_ops hdc_buffer_setup_ops = {
  276. .postenable = hdc100x_buffer_postenable,
  277. .predisable = hdc100x_buffer_predisable,
  278. };
  279. static irqreturn_t hdc100x_trigger_handler(int irq, void *p)
  280. {
  281. struct iio_poll_func *pf = p;
  282. struct iio_dev *indio_dev = pf->indio_dev;
  283. struct hdc100x_data *data = iio_priv(indio_dev);
  284. struct i2c_client *client = data->client;
  285. int delay = data->adc_int_us[0] + data->adc_int_us[1];
  286. int ret;
  287. s16 buf[8]; /* 2x s16 + padding + 8 byte timestamp */
  288. /* dual read starts at temp register */
  289. mutex_lock(&data->lock);
  290. ret = i2c_smbus_write_byte(client, HDC100X_REG_TEMP);
  291. if (ret < 0) {
  292. dev_err(&client->dev, "cannot start measurement\n");
  293. goto err;
  294. }
  295. usleep_range(delay, delay + 1000);
  296. ret = i2c_master_recv(client, (u8 *)buf, 4);
  297. if (ret < 0) {
  298. dev_err(&client->dev, "cannot read sensor data\n");
  299. goto err;
  300. }
  301. iio_push_to_buffers_with_timestamp(indio_dev, buf,
  302. iio_get_time_ns(indio_dev));
  303. err:
  304. mutex_unlock(&data->lock);
  305. iio_trigger_notify_done(indio_dev->trig);
  306. return IRQ_HANDLED;
  307. }
  308. static const struct iio_info hdc100x_info = {
  309. .read_raw = hdc100x_read_raw,
  310. .write_raw = hdc100x_write_raw,
  311. .attrs = &hdc100x_attribute_group,
  312. .driver_module = THIS_MODULE,
  313. };
  314. static int hdc100x_probe(struct i2c_client *client,
  315. const struct i2c_device_id *id)
  316. {
  317. struct iio_dev *indio_dev;
  318. struct hdc100x_data *data;
  319. int ret;
  320. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA |
  321. I2C_FUNC_SMBUS_BYTE | I2C_FUNC_I2C))
  322. return -EOPNOTSUPP;
  323. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  324. if (!indio_dev)
  325. return -ENOMEM;
  326. data = iio_priv(indio_dev);
  327. i2c_set_clientdata(client, indio_dev);
  328. data->client = client;
  329. mutex_init(&data->lock);
  330. indio_dev->dev.parent = &client->dev;
  331. indio_dev->name = dev_name(&client->dev);
  332. indio_dev->modes = INDIO_DIRECT_MODE;
  333. indio_dev->info = &hdc100x_info;
  334. indio_dev->channels = hdc100x_channels;
  335. indio_dev->num_channels = ARRAY_SIZE(hdc100x_channels);
  336. indio_dev->available_scan_masks = hdc100x_scan_masks;
  337. /* be sure we are in a known state */
  338. hdc100x_set_it_time(data, 0, hdc100x_int_time[0][0]);
  339. hdc100x_set_it_time(data, 1, hdc100x_int_time[1][0]);
  340. hdc100x_update_config(data, HDC100X_REG_CONFIG_ACQ_MODE, 0);
  341. ret = iio_triggered_buffer_setup(indio_dev, NULL,
  342. hdc100x_trigger_handler,
  343. &hdc_buffer_setup_ops);
  344. if (ret < 0) {
  345. dev_err(&client->dev, "iio triggered buffer setup failed\n");
  346. return ret;
  347. }
  348. ret = iio_device_register(indio_dev);
  349. if (ret < 0)
  350. iio_triggered_buffer_cleanup(indio_dev);
  351. return ret;
  352. }
  353. static int hdc100x_remove(struct i2c_client *client)
  354. {
  355. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  356. iio_device_unregister(indio_dev);
  357. iio_triggered_buffer_cleanup(indio_dev);
  358. return 0;
  359. }
  360. static const struct i2c_device_id hdc100x_id[] = {
  361. { "hdc100x", 0 },
  362. { "hdc1000", 0 },
  363. { "hdc1008", 0 },
  364. { "hdc1010", 0 },
  365. { "hdc1050", 0 },
  366. { "hdc1080", 0 },
  367. { }
  368. };
  369. MODULE_DEVICE_TABLE(i2c, hdc100x_id);
  370. static const struct of_device_id hdc100x_dt_ids[] = {
  371. { .compatible = "ti,hdc1000" },
  372. { .compatible = "ti,hdc1008" },
  373. { .compatible = "ti,hdc1010" },
  374. { .compatible = "ti,hdc1050" },
  375. { .compatible = "ti,hdc1080" },
  376. { }
  377. };
  378. MODULE_DEVICE_TABLE(of, hdc100x_dt_ids);
  379. static struct i2c_driver hdc100x_driver = {
  380. .driver = {
  381. .name = "hdc100x",
  382. .of_match_table = of_match_ptr(hdc100x_dt_ids),
  383. },
  384. .probe = hdc100x_probe,
  385. .remove = hdc100x_remove,
  386. .id_table = hdc100x_id,
  387. };
  388. module_i2c_driver(hdc100x_driver);
  389. MODULE_AUTHOR("Matt Ranostay <mranostay@gmail.com>");
  390. MODULE_DESCRIPTION("TI HDC100x humidity and temperature sensor driver");
  391. MODULE_LICENSE("GPL");