am2315.c 7.2 KB

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