scd30_i2c.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Sensirion SCD30 carbon dioxide sensor i2c driver
  4. *
  5. * Copyright (c) 2020 Tomasz Duszynski <tomasz.duszynski@octakon.com>
  6. *
  7. * I2C slave address: 0x61
  8. */
  9. #include <linux/crc8.h>
  10. #include <linux/device.h>
  11. #include <linux/errno.h>
  12. #include <linux/i2c.h>
  13. #include <linux/mod_devicetable.h>
  14. #include <linux/module.h>
  15. #include <linux/types.h>
  16. #include <asm/unaligned.h>
  17. #include "scd30.h"
  18. #define SCD30_I2C_MAX_BUF_SIZE 18
  19. #define SCD30_I2C_CRC8_POLYNOMIAL 0x31
  20. static u16 scd30_i2c_cmd_lookup_tbl[] = {
  21. [CMD_START_MEAS] = 0x0010,
  22. [CMD_STOP_MEAS] = 0x0104,
  23. [CMD_MEAS_INTERVAL] = 0x4600,
  24. [CMD_MEAS_READY] = 0x0202,
  25. [CMD_READ_MEAS] = 0x0300,
  26. [CMD_ASC] = 0x5306,
  27. [CMD_FRC] = 0x5204,
  28. [CMD_TEMP_OFFSET] = 0x5403,
  29. [CMD_FW_VERSION] = 0xd100,
  30. [CMD_RESET] = 0xd304,
  31. };
  32. DECLARE_CRC8_TABLE(scd30_i2c_crc8_tbl);
  33. static int scd30_i2c_xfer(struct scd30_state *state, char *txbuf, int txsize,
  34. char *rxbuf, int rxsize)
  35. {
  36. struct i2c_client *client = to_i2c_client(state->dev);
  37. int ret;
  38. /*
  39. * repeated start is not supported hence instead of sending two i2c
  40. * messages in a row we send one by one
  41. */
  42. ret = i2c_master_send(client, txbuf, txsize);
  43. if (ret < 0)
  44. return ret;
  45. if (ret != txsize)
  46. return -EIO;
  47. if (!rxbuf)
  48. return 0;
  49. ret = i2c_master_recv(client, rxbuf, rxsize);
  50. if (ret < 0)
  51. return ret;
  52. if (ret != rxsize)
  53. return -EIO;
  54. return 0;
  55. }
  56. static int scd30_i2c_command(struct scd30_state *state, enum scd30_cmd cmd, u16 arg,
  57. void *response, int size)
  58. {
  59. char buf[SCD30_I2C_MAX_BUF_SIZE];
  60. char *rsp = response;
  61. int i, ret;
  62. char crc;
  63. put_unaligned_be16(scd30_i2c_cmd_lookup_tbl[cmd], buf);
  64. i = 2;
  65. if (rsp) {
  66. /* each two bytes are followed by a crc8 */
  67. size += size / 2;
  68. } else {
  69. put_unaligned_be16(arg, buf + i);
  70. crc = crc8(scd30_i2c_crc8_tbl, buf + i, 2, CRC8_INIT_VALUE);
  71. i += 2;
  72. buf[i] = crc;
  73. i += 1;
  74. /* commands below don't take an argument */
  75. if ((cmd == CMD_STOP_MEAS) || (cmd == CMD_RESET))
  76. i -= 3;
  77. }
  78. ret = scd30_i2c_xfer(state, buf, i, buf, size);
  79. if (ret)
  80. return ret;
  81. /* validate received data and strip off crc bytes */
  82. for (i = 0; i < size; i += 3) {
  83. crc = crc8(scd30_i2c_crc8_tbl, buf + i, 2, CRC8_INIT_VALUE);
  84. if (crc != buf[i + 2]) {
  85. dev_err(state->dev, "data integrity check failed\n");
  86. return -EIO;
  87. }
  88. *rsp++ = buf[i];
  89. *rsp++ = buf[i + 1];
  90. }
  91. return 0;
  92. }
  93. static int scd30_i2c_probe(struct i2c_client *client)
  94. {
  95. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  96. return -EOPNOTSUPP;
  97. crc8_populate_msb(scd30_i2c_crc8_tbl, SCD30_I2C_CRC8_POLYNOMIAL);
  98. return scd30_probe(&client->dev, client->irq, client->name, NULL, scd30_i2c_command);
  99. }
  100. static const struct of_device_id scd30_i2c_of_match[] = {
  101. { .compatible = "sensirion,scd30" },
  102. { }
  103. };
  104. MODULE_DEVICE_TABLE(of, scd30_i2c_of_match);
  105. static struct i2c_driver scd30_i2c_driver = {
  106. .driver = {
  107. .name = KBUILD_MODNAME,
  108. .of_match_table = scd30_i2c_of_match,
  109. .pm = &scd30_pm_ops,
  110. },
  111. .probe_new = scd30_i2c_probe,
  112. };
  113. module_i2c_driver(scd30_i2c_driver);
  114. MODULE_AUTHOR("Tomasz Duszynski <tomasz.duszynski@octakon.com>");
  115. MODULE_DESCRIPTION("Sensirion SCD30 carbon dioxide sensor i2c driver");
  116. MODULE_LICENSE("GPL v2");