am2315.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /**
  2. * Aosong AM2315 relative humidity and temperature
  3. *
  4. * Copyright (c) 2016, Intel Corporation.
  5. *
  6. * This file is subject to the terms and conditions of version 2 of
  7. * the GNU General Public License. See the file COPYING in the main
  8. * directory of this archive for more details.
  9. *
  10. * 7-bit I2C address: 0x5C.
  11. */
  12. #include <linux/acpi.h>
  13. #include <linux/delay.h>
  14. #include <linux/i2c.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/iio/buffer.h>
  18. #include <linux/iio/iio.h>
  19. #include <linux/iio/sysfs.h>
  20. #include <linux/iio/trigger_consumer.h>
  21. #include <linux/iio/triggered_buffer.h>
  22. #define AM2315_REG_HUM_MSB 0x00
  23. #define AM2315_REG_HUM_LSB 0x01
  24. #define AM2315_REG_TEMP_MSB 0x02
  25. #define AM2315_REG_TEMP_LSB 0x03
  26. #define AM2315_FUNCTION_READ 0x03
  27. #define AM2315_HUM_OFFSET 2
  28. #define AM2315_TEMP_OFFSET 4
  29. #define AM2315_ALL_CHANNEL_MASK GENMASK(1, 0)
  30. #define AM2315_DRIVER_NAME "am2315"
  31. struct am2315_data {
  32. struct i2c_client *client;
  33. struct mutex lock;
  34. s16 buffer[8]; /* 2x16-bit channels + 2x16 padding + 4x16 timestamp */
  35. };
  36. struct am2315_sensor_data {
  37. s16 hum_data;
  38. s16 temp_data;
  39. };
  40. static const struct iio_chan_spec am2315_channels[] = {
  41. {
  42. .type = IIO_HUMIDITYRELATIVE,
  43. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  44. BIT(IIO_CHAN_INFO_SCALE),
  45. .scan_index = 0,
  46. .scan_type = {
  47. .sign = 's',
  48. .realbits = 16,
  49. .storagebits = 16,
  50. .endianness = IIO_CPU,
  51. },
  52. },
  53. {
  54. .type = IIO_TEMP,
  55. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  56. BIT(IIO_CHAN_INFO_SCALE),
  57. .scan_index = 1,
  58. .scan_type = {
  59. .sign = 's',
  60. .realbits = 16,
  61. .storagebits = 16,
  62. .endianness = IIO_CPU,
  63. },
  64. },
  65. IIO_CHAN_SOFT_TIMESTAMP(2),
  66. };
  67. /* CRC calculation algorithm, as specified in the datasheet (page 13). */
  68. static u16 am2315_crc(u8 *data, u8 nr_bytes)
  69. {
  70. int i;
  71. u16 crc = 0xffff;
  72. while (nr_bytes--) {
  73. crc ^= *data++;
  74. for (i = 0; i < 8; i++) {
  75. if (crc & 0x01) {
  76. crc >>= 1;
  77. crc ^= 0xA001;
  78. } else {
  79. crc >>= 1;
  80. }
  81. }
  82. }
  83. return crc;
  84. }
  85. /* Simple function that sends a few bytes to the device to wake it up. */
  86. static void am2315_ping(struct i2c_client *client)
  87. {
  88. i2c_smbus_read_byte_data(client, AM2315_REG_HUM_MSB);
  89. }
  90. static int am2315_read_data(struct am2315_data *data,
  91. struct am2315_sensor_data *sensor_data)
  92. {
  93. int ret;
  94. /* tx_buf format: <function code> <start addr> <nr of regs to read> */
  95. u8 tx_buf[3] = { AM2315_FUNCTION_READ, AM2315_REG_HUM_MSB, 4 };
  96. /*
  97. * rx_buf format:
  98. * <function code> <number of registers read>
  99. * <humidity MSB> <humidity LSB> <temp MSB> <temp LSB>
  100. * <CRC LSB> <CRC MSB>
  101. */
  102. u8 rx_buf[8];
  103. u16 crc;
  104. /* First wake up the device. */
  105. am2315_ping(data->client);
  106. mutex_lock(&data->lock);
  107. ret = i2c_master_send(data->client, tx_buf, sizeof(tx_buf));
  108. if (ret < 0) {
  109. dev_err(&data->client->dev, "failed to send read request\n");
  110. goto exit_unlock;
  111. }
  112. /* Wait 2-3 ms, then read back the data sent by the device. */
  113. usleep_range(2000, 3000);
  114. /* Do a bulk data read, then pick out what we need. */
  115. ret = i2c_master_recv(data->client, rx_buf, sizeof(rx_buf));
  116. if (ret < 0) {
  117. dev_err(&data->client->dev, "failed to read sensor data\n");
  118. goto exit_unlock;
  119. }
  120. mutex_unlock(&data->lock);
  121. /*
  122. * Do a CRC check on the data and compare it to the value
  123. * calculated by the device.
  124. */
  125. crc = am2315_crc(rx_buf, sizeof(rx_buf) - 2);
  126. if ((crc & 0xff) != rx_buf[6] || (crc >> 8) != rx_buf[7]) {
  127. dev_err(&data->client->dev, "failed to verify sensor data\n");
  128. return -EIO;
  129. }
  130. sensor_data->hum_data = (rx_buf[AM2315_HUM_OFFSET] << 8) |
  131. rx_buf[AM2315_HUM_OFFSET + 1];
  132. sensor_data->temp_data = (rx_buf[AM2315_TEMP_OFFSET] << 8) |
  133. rx_buf[AM2315_TEMP_OFFSET + 1];
  134. return ret;
  135. exit_unlock:
  136. mutex_unlock(&data->lock);
  137. return ret;
  138. }
  139. static irqreturn_t am2315_trigger_handler(int irq, void *p)
  140. {
  141. int i;
  142. int ret;
  143. int bit;
  144. struct iio_poll_func *pf = p;
  145. struct iio_dev *indio_dev = pf->indio_dev;
  146. struct am2315_data *data = iio_priv(indio_dev);
  147. struct am2315_sensor_data sensor_data;
  148. ret = am2315_read_data(data, &sensor_data);
  149. if (ret < 0)
  150. goto err;
  151. mutex_lock(&data->lock);
  152. if (*(indio_dev->active_scan_mask) == AM2315_ALL_CHANNEL_MASK) {
  153. data->buffer[0] = sensor_data.hum_data;
  154. data->buffer[1] = sensor_data.temp_data;
  155. } else {
  156. i = 0;
  157. for_each_set_bit(bit, indio_dev->active_scan_mask,
  158. indio_dev->masklength) {
  159. data->buffer[i] = (bit ? sensor_data.temp_data :
  160. sensor_data.hum_data);
  161. i++;
  162. }
  163. }
  164. mutex_unlock(&data->lock);
  165. iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
  166. pf->timestamp);
  167. err:
  168. iio_trigger_notify_done(indio_dev->trig);
  169. return IRQ_HANDLED;
  170. }
  171. static int am2315_read_raw(struct iio_dev *indio_dev,
  172. struct iio_chan_spec const *chan,
  173. int *val, int *val2, long mask)
  174. {
  175. int ret;
  176. struct am2315_sensor_data sensor_data;
  177. struct am2315_data *data = iio_priv(indio_dev);
  178. switch (mask) {
  179. case IIO_CHAN_INFO_RAW:
  180. ret = am2315_read_data(data, &sensor_data);
  181. if (ret < 0)
  182. return ret;
  183. *val = (chan->type == IIO_HUMIDITYRELATIVE) ?
  184. sensor_data.hum_data : sensor_data.temp_data;
  185. return IIO_VAL_INT;
  186. case IIO_CHAN_INFO_SCALE:
  187. *val = 100;
  188. return IIO_VAL_INT;
  189. }
  190. return -EINVAL;
  191. }
  192. static const struct iio_info am2315_info = {
  193. .read_raw = am2315_read_raw,
  194. };
  195. static int am2315_probe(struct i2c_client *client,
  196. const struct i2c_device_id *id)
  197. {
  198. int ret;
  199. struct iio_dev *indio_dev;
  200. struct am2315_data *data;
  201. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  202. if (!indio_dev) {
  203. dev_err(&client->dev, "iio allocation failed!\n");
  204. return -ENOMEM;
  205. }
  206. data = iio_priv(indio_dev);
  207. data->client = client;
  208. i2c_set_clientdata(client, indio_dev);
  209. mutex_init(&data->lock);
  210. indio_dev->dev.parent = &client->dev;
  211. indio_dev->info = &am2315_info;
  212. indio_dev->name = AM2315_DRIVER_NAME;
  213. indio_dev->modes = INDIO_DIRECT_MODE;
  214. indio_dev->channels = am2315_channels;
  215. indio_dev->num_channels = ARRAY_SIZE(am2315_channels);
  216. ret = iio_triggered_buffer_setup(indio_dev, iio_pollfunc_store_time,
  217. am2315_trigger_handler, NULL);
  218. if (ret < 0) {
  219. dev_err(&client->dev, "iio triggered buffer setup failed\n");
  220. return ret;
  221. }
  222. ret = iio_device_register(indio_dev);
  223. if (ret < 0)
  224. goto err_buffer_cleanup;
  225. return 0;
  226. err_buffer_cleanup:
  227. iio_triggered_buffer_cleanup(indio_dev);
  228. return ret;
  229. }
  230. static int am2315_remove(struct i2c_client *client)
  231. {
  232. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  233. iio_device_unregister(indio_dev);
  234. iio_triggered_buffer_cleanup(indio_dev);
  235. return 0;
  236. }
  237. static const struct i2c_device_id am2315_i2c_id[] = {
  238. {"am2315", 0},
  239. {}
  240. };
  241. MODULE_DEVICE_TABLE(i2c, am2315_i2c_id);
  242. static const struct acpi_device_id am2315_acpi_id[] = {
  243. {"AOS2315", 0},
  244. {}
  245. };
  246. MODULE_DEVICE_TABLE(acpi, am2315_acpi_id);
  247. static struct i2c_driver am2315_driver = {
  248. .driver = {
  249. .name = "am2315",
  250. .acpi_match_table = ACPI_PTR(am2315_acpi_id),
  251. },
  252. .probe = am2315_probe,
  253. .remove = am2315_remove,
  254. .id_table = am2315_i2c_id,
  255. };
  256. module_i2c_driver(am2315_driver);
  257. MODULE_AUTHOR("Tiberiu Breana <tiberiu.a.breana@intel.com>");
  258. MODULE_DESCRIPTION("Aosong AM2315 relative humidity and temperature");
  259. MODULE_LICENSE("GPL v2");