ams-iaq-core.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * ams-iaq-core.c - Support for AMS iAQ-Core VOC 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/module.h>
  18. #include <linux/mutex.h>
  19. #include <linux/init.h>
  20. #include <linux/i2c.h>
  21. #include <linux/iio/iio.h>
  22. #define AMS_IAQCORE_DATA_SIZE 9
  23. #define AMS_IAQCORE_VOC_CO2_IDX 0
  24. #define AMS_IAQCORE_VOC_RESISTANCE_IDX 1
  25. #define AMS_IAQCORE_VOC_TVOC_IDX 2
  26. struct ams_iaqcore_reading {
  27. __be16 co2_ppm;
  28. u8 status;
  29. __be32 resistance;
  30. __be16 voc_ppb;
  31. } __attribute__((__packed__));
  32. struct ams_iaqcore_data {
  33. struct i2c_client *client;
  34. struct mutex lock;
  35. unsigned long last_update;
  36. struct ams_iaqcore_reading buffer;
  37. };
  38. static const struct iio_chan_spec ams_iaqcore_channels[] = {
  39. {
  40. .type = IIO_CONCENTRATION,
  41. .channel2 = IIO_MOD_CO2,
  42. .modified = 1,
  43. .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
  44. .address = AMS_IAQCORE_VOC_CO2_IDX,
  45. },
  46. {
  47. .type = IIO_RESISTANCE,
  48. .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
  49. .address = AMS_IAQCORE_VOC_RESISTANCE_IDX,
  50. },
  51. {
  52. .type = IIO_CONCENTRATION,
  53. .channel2 = IIO_MOD_VOC,
  54. .modified = 1,
  55. .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
  56. .address = AMS_IAQCORE_VOC_TVOC_IDX,
  57. },
  58. };
  59. static int ams_iaqcore_read_measurement(struct ams_iaqcore_data *data)
  60. {
  61. struct i2c_client *client = data->client;
  62. int ret;
  63. struct i2c_msg msg = {
  64. .addr = client->addr,
  65. .flags = client->flags | I2C_M_RD,
  66. .len = AMS_IAQCORE_DATA_SIZE,
  67. .buf = (char *) &data->buffer,
  68. };
  69. ret = i2c_transfer(client->adapter, &msg, 1);
  70. return (ret == AMS_IAQCORE_DATA_SIZE) ? 0 : ret;
  71. }
  72. static int ams_iaqcore_get_measurement(struct ams_iaqcore_data *data)
  73. {
  74. int ret;
  75. /* sensor can only be polled once a second max per datasheet */
  76. if (!time_after(jiffies, data->last_update + HZ))
  77. return 0;
  78. ret = ams_iaqcore_read_measurement(data);
  79. if (ret < 0)
  80. return ret;
  81. data->last_update = jiffies;
  82. return 0;
  83. }
  84. static int ams_iaqcore_read_raw(struct iio_dev *indio_dev,
  85. struct iio_chan_spec const *chan, int *val,
  86. int *val2, long mask)
  87. {
  88. struct ams_iaqcore_data *data = iio_priv(indio_dev);
  89. int ret;
  90. if (mask != IIO_CHAN_INFO_PROCESSED)
  91. return -EINVAL;
  92. mutex_lock(&data->lock);
  93. ret = ams_iaqcore_get_measurement(data);
  94. if (ret)
  95. goto err_out;
  96. switch (chan->address) {
  97. case AMS_IAQCORE_VOC_CO2_IDX:
  98. *val = 0;
  99. *val2 = be16_to_cpu(data->buffer.co2_ppm);
  100. ret = IIO_VAL_INT_PLUS_MICRO;
  101. break;
  102. case AMS_IAQCORE_VOC_RESISTANCE_IDX:
  103. *val = be32_to_cpu(data->buffer.resistance);
  104. ret = IIO_VAL_INT;
  105. break;
  106. case AMS_IAQCORE_VOC_TVOC_IDX:
  107. *val = 0;
  108. *val2 = be16_to_cpu(data->buffer.voc_ppb);
  109. ret = IIO_VAL_INT_PLUS_NANO;
  110. break;
  111. default:
  112. ret = -EINVAL;
  113. }
  114. err_out:
  115. mutex_unlock(&data->lock);
  116. return ret;
  117. }
  118. static const struct iio_info ams_iaqcore_info = {
  119. .read_raw = ams_iaqcore_read_raw,
  120. .driver_module = THIS_MODULE,
  121. };
  122. static int ams_iaqcore_probe(struct i2c_client *client,
  123. const struct i2c_device_id *id)
  124. {
  125. struct iio_dev *indio_dev;
  126. struct ams_iaqcore_data *data;
  127. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  128. if (!indio_dev)
  129. return -ENOMEM;
  130. data = iio_priv(indio_dev);
  131. i2c_set_clientdata(client, indio_dev);
  132. data->client = client;
  133. /* so initial reading will complete */
  134. data->last_update = jiffies - HZ;
  135. mutex_init(&data->lock);
  136. indio_dev->dev.parent = &client->dev;
  137. indio_dev->info = &ams_iaqcore_info,
  138. indio_dev->name = dev_name(&client->dev);
  139. indio_dev->modes = INDIO_DIRECT_MODE;
  140. indio_dev->channels = ams_iaqcore_channels;
  141. indio_dev->num_channels = ARRAY_SIZE(ams_iaqcore_channels);
  142. return devm_iio_device_register(&client->dev, indio_dev);
  143. }
  144. static const struct i2c_device_id ams_iaqcore_id[] = {
  145. { "ams-iaq-core", 0 },
  146. { }
  147. };
  148. MODULE_DEVICE_TABLE(i2c, ams_iaqcore_id);
  149. static const struct of_device_id ams_iaqcore_dt_ids[] = {
  150. { .compatible = "ams,iaq-core" },
  151. { }
  152. };
  153. MODULE_DEVICE_TABLE(of, ams_iaqcore_dt_ids);
  154. static struct i2c_driver ams_iaqcore_driver = {
  155. .driver = {
  156. .name = "ams-iaq-core",
  157. .of_match_table = of_match_ptr(ams_iaqcore_dt_ids),
  158. },
  159. .probe = ams_iaqcore_probe,
  160. .id_table = ams_iaqcore_id,
  161. };
  162. module_i2c_driver(ams_iaqcore_driver);
  163. MODULE_AUTHOR("Matt Ranostay <mranostay@gmail.com>");
  164. MODULE_DESCRIPTION("AMS iAQ-Core VOC sensors");
  165. MODULE_LICENSE("GPL v2");