si7020.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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_swapped(*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. /*
  55. * Humidity values can slightly exceed the 0-100%RH
  56. * range and should be corrected by software
  57. */
  58. if (chan->type == IIO_HUMIDITYRELATIVE)
  59. *val = clamp_val(*val, 786, 13893);
  60. return IIO_VAL_INT;
  61. case IIO_CHAN_INFO_SCALE:
  62. if (chan->type == IIO_TEMP)
  63. *val = 175720; /* = 175.72 * 1000 */
  64. else
  65. *val = 125 * 1000;
  66. *val2 = 65536 >> 2;
  67. return IIO_VAL_FRACTIONAL;
  68. case IIO_CHAN_INFO_OFFSET:
  69. /*
  70. * Since iio_convert_raw_to_processed_unlocked assumes offset
  71. * is an integer we have to round these values and lose
  72. * accuracy.
  73. * Relative humidity will be 0.0032959% too high and
  74. * temperature will be 0.00277344 degrees too high.
  75. * This is no big deal because it's within the accuracy of the
  76. * sensor.
  77. */
  78. if (chan->type == IIO_TEMP)
  79. *val = -4368; /* = -46.85 * (65536 >> 2) / 175.72 */
  80. else
  81. *val = -786; /* = -6 * (65536 >> 2) / 125 */
  82. return IIO_VAL_INT;
  83. default:
  84. break;
  85. }
  86. return -EINVAL;
  87. }
  88. static const struct iio_chan_spec si7020_channels[] = {
  89. {
  90. .type = IIO_HUMIDITYRELATIVE,
  91. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  92. BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_OFFSET),
  93. },
  94. {
  95. .type = IIO_TEMP,
  96. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  97. BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_OFFSET),
  98. }
  99. };
  100. static const struct iio_info si7020_info = {
  101. .read_raw = si7020_read_raw,
  102. .driver_module = THIS_MODULE,
  103. };
  104. static int si7020_probe(struct i2c_client *client,
  105. const struct i2c_device_id *id)
  106. {
  107. struct iio_dev *indio_dev;
  108. struct i2c_client **data;
  109. int ret;
  110. if (!i2c_check_functionality(client->adapter,
  111. I2C_FUNC_SMBUS_WRITE_BYTE |
  112. I2C_FUNC_SMBUS_READ_WORD_DATA))
  113. return -EOPNOTSUPP;
  114. /* Reset device, loads default settings. */
  115. ret = i2c_smbus_write_byte(client, SI7020CMD_RESET);
  116. if (ret < 0)
  117. return ret;
  118. /* Wait the maximum power-up time after software reset. */
  119. msleep(15);
  120. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  121. if (!indio_dev)
  122. return -ENOMEM;
  123. data = iio_priv(indio_dev);
  124. *data = client;
  125. indio_dev->dev.parent = &client->dev;
  126. indio_dev->name = dev_name(&client->dev);
  127. indio_dev->modes = INDIO_DIRECT_MODE;
  128. indio_dev->info = &si7020_info;
  129. indio_dev->channels = si7020_channels;
  130. indio_dev->num_channels = ARRAY_SIZE(si7020_channels);
  131. return devm_iio_device_register(&client->dev, indio_dev);
  132. }
  133. static const struct i2c_device_id si7020_id[] = {
  134. { "si7020", 0 },
  135. { "th06", 0 },
  136. { }
  137. };
  138. MODULE_DEVICE_TABLE(i2c, si7020_id);
  139. static const struct of_device_id si7020_dt_ids[] = {
  140. { .compatible = "silabs,si7020" },
  141. { }
  142. };
  143. MODULE_DEVICE_TABLE(of, si7020_dt_ids);
  144. static struct i2c_driver si7020_driver = {
  145. .driver = {
  146. .name = "si7020",
  147. .of_match_table = of_match_ptr(si7020_dt_ids),
  148. },
  149. .probe = si7020_probe,
  150. .id_table = si7020_id,
  151. };
  152. module_i2c_driver(si7020_driver);
  153. MODULE_DESCRIPTION("Silicon Labs Si7013/20/21 Relative Humidity and Temperature Sensors");
  154. MODULE_AUTHOR("David Barksdale <dbarksdale@uplogix.com>");
  155. MODULE_LICENSE("GPL");