atlas-ezo-sensor.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * atlas-ezo-sensor.c - Support for Atlas Scientific EZO sensors
  4. *
  5. * Copyright (C) 2020 Konsulko Group
  6. * Author: Matt Ranostay <[email protected]>
  7. */
  8. #include <linux/init.h>
  9. #include <linux/delay.h>
  10. #include <linux/mod_devicetable.h>
  11. #include <linux/module.h>
  12. #include <linux/mutex.h>
  13. #include <linux/property.h>
  14. #include <linux/err.h>
  15. #include <linux/i2c.h>
  16. #include <linux/iio/iio.h>
  17. #define ATLAS_EZO_DRV_NAME "atlas-ezo-sensor"
  18. #define ATLAS_INT_TIME_IN_MS 950
  19. #define ATLAS_INT_HUM_TIME_IN_MS 350
  20. enum {
  21. ATLAS_CO2_EZO,
  22. ATLAS_O2_EZO,
  23. ATLAS_HUM_EZO,
  24. };
  25. struct atlas_ezo_device {
  26. const struct iio_chan_spec *channels;
  27. int num_channels;
  28. int delay;
  29. };
  30. struct atlas_ezo_data {
  31. struct i2c_client *client;
  32. const struct atlas_ezo_device *chip;
  33. /* lock to avoid multiple concurrent read calls */
  34. struct mutex lock;
  35. u8 buffer[8];
  36. };
  37. #define ATLAS_CONCENTRATION_CHANNEL(_modifier) \
  38. { \
  39. .type = IIO_CONCENTRATION, \
  40. .modified = 1,\
  41. .channel2 = _modifier, \
  42. .info_mask_separate = \
  43. BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), \
  44. .scan_index = 0, \
  45. .scan_type = { \
  46. .sign = 'u', \
  47. .realbits = 32, \
  48. .storagebits = 32, \
  49. .endianness = IIO_CPU, \
  50. }, \
  51. }
  52. static const struct iio_chan_spec atlas_co2_ezo_channels[] = {
  53. ATLAS_CONCENTRATION_CHANNEL(IIO_MOD_CO2),
  54. };
  55. static const struct iio_chan_spec atlas_o2_ezo_channels[] = {
  56. ATLAS_CONCENTRATION_CHANNEL(IIO_MOD_O2),
  57. };
  58. static const struct iio_chan_spec atlas_hum_ezo_channels[] = {
  59. {
  60. .type = IIO_HUMIDITYRELATIVE,
  61. .info_mask_separate =
  62. BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
  63. .scan_index = 0,
  64. .scan_type = {
  65. .sign = 'u',
  66. .realbits = 32,
  67. .storagebits = 32,
  68. .endianness = IIO_CPU,
  69. },
  70. },
  71. };
  72. static struct atlas_ezo_device atlas_ezo_devices[] = {
  73. [ATLAS_CO2_EZO] = {
  74. .channels = atlas_co2_ezo_channels,
  75. .num_channels = 1,
  76. .delay = ATLAS_INT_TIME_IN_MS,
  77. },
  78. [ATLAS_O2_EZO] = {
  79. .channels = atlas_o2_ezo_channels,
  80. .num_channels = 1,
  81. .delay = ATLAS_INT_TIME_IN_MS,
  82. },
  83. [ATLAS_HUM_EZO] = {
  84. .channels = atlas_hum_ezo_channels,
  85. .num_channels = 1,
  86. .delay = ATLAS_INT_HUM_TIME_IN_MS,
  87. },
  88. };
  89. static void atlas_ezo_sanitize(char *buf)
  90. {
  91. char *ptr = strchr(buf, '.');
  92. if (!ptr)
  93. return;
  94. memmove(ptr, ptr + 1, strlen(ptr));
  95. }
  96. static int atlas_ezo_read_raw(struct iio_dev *indio_dev,
  97. struct iio_chan_spec const *chan,
  98. int *val, int *val2, long mask)
  99. {
  100. struct atlas_ezo_data *data = iio_priv(indio_dev);
  101. struct i2c_client *client = data->client;
  102. if (chan->type != IIO_CONCENTRATION)
  103. return -EINVAL;
  104. switch (mask) {
  105. case IIO_CHAN_INFO_RAW: {
  106. int ret;
  107. long tmp;
  108. mutex_lock(&data->lock);
  109. tmp = i2c_smbus_write_byte(client, 'R');
  110. if (tmp < 0) {
  111. mutex_unlock(&data->lock);
  112. return tmp;
  113. }
  114. msleep(data->chip->delay);
  115. tmp = i2c_master_recv(client, data->buffer, sizeof(data->buffer));
  116. if (tmp < 0 || data->buffer[0] != 1) {
  117. mutex_unlock(&data->lock);
  118. return -EBUSY;
  119. }
  120. /* removing floating point for fixed number representation */
  121. atlas_ezo_sanitize(data->buffer + 2);
  122. ret = kstrtol(data->buffer + 1, 10, &tmp);
  123. *val = tmp;
  124. mutex_unlock(&data->lock);
  125. return ret ? ret : IIO_VAL_INT;
  126. }
  127. case IIO_CHAN_INFO_SCALE:
  128. switch (chan->type) {
  129. case IIO_HUMIDITYRELATIVE:
  130. *val = 10;
  131. return IIO_VAL_INT;
  132. case IIO_CONCENTRATION:
  133. break;
  134. default:
  135. return -EINVAL;
  136. }
  137. /* IIO_CONCENTRATION modifiers */
  138. switch (chan->channel2) {
  139. case IIO_MOD_CO2:
  140. *val = 0;
  141. *val2 = 100; /* 0.0001 */
  142. return IIO_VAL_INT_PLUS_MICRO;
  143. case IIO_MOD_O2:
  144. *val = 100;
  145. return IIO_VAL_INT;
  146. }
  147. return -EINVAL;
  148. }
  149. return 0;
  150. }
  151. static const struct iio_info atlas_info = {
  152. .read_raw = atlas_ezo_read_raw,
  153. };
  154. static const struct i2c_device_id atlas_ezo_id[] = {
  155. { "atlas-co2-ezo", (kernel_ulong_t)&atlas_ezo_devices[ATLAS_CO2_EZO] },
  156. { "atlas-o2-ezo", (kernel_ulong_t)&atlas_ezo_devices[ATLAS_O2_EZO] },
  157. { "atlas-hum-ezo", (kernel_ulong_t)&atlas_ezo_devices[ATLAS_HUM_EZO] },
  158. {}
  159. };
  160. MODULE_DEVICE_TABLE(i2c, atlas_ezo_id);
  161. static const struct of_device_id atlas_ezo_dt_ids[] = {
  162. { .compatible = "atlas,co2-ezo", .data = &atlas_ezo_devices[ATLAS_CO2_EZO], },
  163. { .compatible = "atlas,o2-ezo", .data = &atlas_ezo_devices[ATLAS_O2_EZO], },
  164. { .compatible = "atlas,hum-ezo", .data = &atlas_ezo_devices[ATLAS_HUM_EZO], },
  165. {}
  166. };
  167. MODULE_DEVICE_TABLE(of, atlas_ezo_dt_ids);
  168. static int atlas_ezo_probe(struct i2c_client *client,
  169. const struct i2c_device_id *id)
  170. {
  171. const struct atlas_ezo_device *chip;
  172. struct atlas_ezo_data *data;
  173. struct iio_dev *indio_dev;
  174. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  175. if (!indio_dev)
  176. return -ENOMEM;
  177. if (dev_fwnode(&client->dev))
  178. chip = device_get_match_data(&client->dev);
  179. else
  180. chip = (const struct atlas_ezo_device *)id->driver_data;
  181. if (!chip)
  182. return -EINVAL;
  183. indio_dev->info = &atlas_info;
  184. indio_dev->name = ATLAS_EZO_DRV_NAME;
  185. indio_dev->channels = chip->channels;
  186. indio_dev->num_channels = chip->num_channels;
  187. indio_dev->modes = INDIO_DIRECT_MODE;
  188. data = iio_priv(indio_dev);
  189. data->client = client;
  190. data->chip = chip;
  191. mutex_init(&data->lock);
  192. return devm_iio_device_register(&client->dev, indio_dev);
  193. };
  194. static struct i2c_driver atlas_ezo_driver = {
  195. .driver = {
  196. .name = ATLAS_EZO_DRV_NAME,
  197. .of_match_table = atlas_ezo_dt_ids,
  198. },
  199. .probe = atlas_ezo_probe,
  200. .id_table = atlas_ezo_id,
  201. };
  202. module_i2c_driver(atlas_ezo_driver);
  203. MODULE_AUTHOR("Matt Ranostay <[email protected]>");
  204. MODULE_DESCRIPTION("Atlas Scientific EZO sensors");
  205. MODULE_LICENSE("GPL");