hdc100x.c 11 KB

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