si7020.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * si7020.c - Silicon Labs Si7013/20/21 Relative Humidity and Temp Sensors
  3. * Copyright (c) 2013,2014 Uplogix, Inc.
  4. * David Barksdale <dbarksdale@uplogix.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. /*
  16. * The Silicon Labs Si7013/20/21 Relative Humidity and Temperature Sensors
  17. * are i2c devices which have an identical programming interface for
  18. * measuring relative humidity and temperature. The Si7013 has an additional
  19. * temperature input which this driver does not support.
  20. *
  21. * Data Sheets:
  22. * Si7013: http://www.silabs.com/Support%20Documents/TechnicalDocs/Si7013.pdf
  23. * Si7020: http://www.silabs.com/Support%20Documents/TechnicalDocs/Si7020.pdf
  24. * Si7021: http://www.silabs.com/Support%20Documents/TechnicalDocs/Si7021.pdf
  25. */
  26. #include <linux/delay.h>
  27. #include <linux/i2c.h>
  28. #include <linux/module.h>
  29. #include <linux/slab.h>
  30. #include <linux/sysfs.h>
  31. #include <linux/iio/iio.h>
  32. #include <linux/iio/sysfs.h>
  33. /* Measure Relative Humidity, Hold Master Mode */
  34. #define SI7020CMD_RH_HOLD 0xE5
  35. /* Measure Temperature, Hold Master Mode */
  36. #define SI7020CMD_TEMP_HOLD 0xE3
  37. /* Software Reset */
  38. #define SI7020CMD_RESET 0xFE
  39. static int si7020_read_raw(struct iio_dev *indio_dev,
  40. struct iio_chan_spec const *chan, int *val,
  41. int *val2, long mask)
  42. {
  43. struct i2c_client **client = iio_priv(indio_dev);
  44. int ret;
  45. switch (mask) {
  46. case IIO_CHAN_INFO_RAW:
  47. ret = i2c_smbus_read_word_data(*client,
  48. chan->type == IIO_TEMP ?
  49. SI7020CMD_TEMP_HOLD :
  50. SI7020CMD_RH_HOLD);
  51. if (ret < 0)
  52. return ret;
  53. *val = ret >> 2;
  54. if (chan->type == IIO_HUMIDITYRELATIVE)
  55. *val &= GENMASK(11, 0);
  56. return IIO_VAL_INT;
  57. case IIO_CHAN_INFO_SCALE:
  58. if (chan->type == IIO_TEMP)
  59. *val = 175720; /* = 175.72 * 1000 */
  60. else
  61. *val = 125 * 1000;
  62. *val2 = 65536 >> 2;
  63. return IIO_VAL_FRACTIONAL;
  64. case IIO_CHAN_INFO_OFFSET:
  65. /*
  66. * Since iio_convert_raw_to_processed_unlocked assumes offset
  67. * is an integer we have to round these values and lose
  68. * accuracy.
  69. * Relative humidity will be 0.0032959% too high and
  70. * temperature will be 0.00277344 degrees too high.
  71. * This is no big deal because it's within the accuracy of the
  72. * sensor.
  73. */
  74. if (chan->type == IIO_TEMP)
  75. *val = -4368; /* = -46.85 * (65536 >> 2) / 175.72 */
  76. else
  77. *val = -786; /* = -6 * (65536 >> 2) / 125 */
  78. return IIO_VAL_INT;
  79. default:
  80. break;
  81. }
  82. return -EINVAL;
  83. }
  84. static const struct iio_chan_spec si7020_channels[] = {
  85. {
  86. .type = IIO_HUMIDITYRELATIVE,
  87. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  88. BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_OFFSET),
  89. },
  90. {
  91. .type = IIO_TEMP,
  92. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  93. BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_OFFSET),
  94. }
  95. };
  96. static const struct iio_info si7020_info = {
  97. .read_raw = si7020_read_raw,
  98. .driver_module = THIS_MODULE,
  99. };
  100. static int si7020_probe(struct i2c_client *client,
  101. const struct i2c_device_id *id)
  102. {
  103. struct iio_dev *indio_dev;
  104. struct i2c_client **data;
  105. int ret;
  106. if (!i2c_check_functionality(client->adapter,
  107. I2C_FUNC_SMBUS_WRITE_BYTE |
  108. I2C_FUNC_SMBUS_READ_WORD_DATA))
  109. return -ENODEV;
  110. /* Reset device, loads default settings. */
  111. ret = i2c_smbus_write_byte(client, SI7020CMD_RESET);
  112. if (ret < 0)
  113. return ret;
  114. /* Wait the maximum power-up time after software reset. */
  115. msleep(15);
  116. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  117. if (!indio_dev)
  118. return -ENOMEM;
  119. data = iio_priv(indio_dev);
  120. *data = client;
  121. indio_dev->dev.parent = &client->dev;
  122. indio_dev->name = dev_name(&client->dev);
  123. indio_dev->modes = INDIO_DIRECT_MODE;
  124. indio_dev->info = &si7020_info;
  125. indio_dev->channels = si7020_channels;
  126. indio_dev->num_channels = ARRAY_SIZE(si7020_channels);
  127. return devm_iio_device_register(&client->dev, indio_dev);
  128. }
  129. static const struct i2c_device_id si7020_id[] = {
  130. { "si7020", 0 },
  131. { }
  132. };
  133. MODULE_DEVICE_TABLE(i2c, si7020_id);
  134. static struct i2c_driver si7020_driver = {
  135. .driver.name = "si7020",
  136. .probe = si7020_probe,
  137. .id_table = si7020_id,
  138. };
  139. module_i2c_driver(si7020_driver);
  140. MODULE_DESCRIPTION("Silicon Labs Si7013/20/21 Relative Humidity and Temperature Sensors");
  141. MODULE_AUTHOR("David Barksdale <dbarksdale@uplogix.com>");
  142. MODULE_LICENSE("GPL");