1 // SPDX-License-Identifier: GPL-2.0-only
3 * si7020.c - Silicon Labs Si7013/20/21 Relative Humidity and Temp Sensors
4 * Copyright (c) 2013,2014 Uplogix, Inc.
9 * The Silicon Labs Si7013/20/21 Relative Humidity and Temperature Sensors
10 * are i2c devices which have an identical programming interface for
11 * measuring relative humidity and temperature. The Si7013 has an additional
12 * temperature input which this driver does not support.
15 * Si7013: http://www.silabs.com/Support%20Documents/TechnicalDocs/Si7013.pdf
16 * Si7020: http://www.silabs.com/Support%20Documents/TechnicalDocs/Si7020.pdf
17 * Si7021: http://www.silabs.com/Support%20Documents/TechnicalDocs/Si7021.pdf
20 #include <linux/delay.h>
21 #include <linux/i2c.h>
22 #include <linux/module.h>
23 #include <linux/mod_devicetable.h>
24 #include <linux/slab.h>
25 #include <linux/sysfs.h>
27 #include <linux/iio/iio.h>
28 #include <linux/iio/sysfs.h>
30 /* Measure Relative Humidity, Hold Master Mode */
31 #define SI7020CMD_RH_HOLD 0xE5
32 /* Measure Temperature, Hold Master Mode */
33 #define SI7020CMD_TEMP_HOLD 0xE3
35 #define SI7020CMD_RESET 0xFE
37 static int si7020_read_raw(struct iio_dev *indio_dev,
38 struct iio_chan_spec const *chan, int *val,
41 struct i2c_client **client = iio_priv(indio_dev);
45 case IIO_CHAN_INFO_RAW:
46 ret = i2c_smbus_read_word_swapped(*client,
47 chan->type == IIO_TEMP ?
54 * Humidity values can slightly exceed the 0-100%RH
55 * range and should be corrected by software
57 if (chan->type == IIO_HUMIDITYRELATIVE)
58 *val = clamp_val(*val, 786, 13893);
60 case IIO_CHAN_INFO_SCALE:
61 if (chan->type == IIO_TEMP)
62 *val = 175720; /* = 175.72 * 1000 */
66 return IIO_VAL_FRACTIONAL;
67 case IIO_CHAN_INFO_OFFSET:
69 * Since iio_convert_raw_to_processed_unlocked assumes offset
70 * is an integer we have to round these values and lose
72 * Relative humidity will be 0.0032959% too high and
73 * temperature will be 0.00277344 degrees too high.
74 * This is no big deal because it's within the accuracy of the
77 if (chan->type == IIO_TEMP)
78 *val = -4368; /* = -46.85 * (65536 >> 2) / 175.72 */
80 *val = -786; /* = -6 * (65536 >> 2) / 125 */
89 static const struct iio_chan_spec si7020_channels[] = {
91 .type = IIO_HUMIDITYRELATIVE,
92 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
93 BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_OFFSET),
97 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
98 BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_OFFSET),
102 static const struct iio_info si7020_info = {
103 .read_raw = si7020_read_raw,
106 static int si7020_probe(struct i2c_client *client)
108 struct iio_dev *indio_dev;
109 struct i2c_client **data;
112 if (!i2c_check_functionality(client->adapter,
113 I2C_FUNC_SMBUS_WRITE_BYTE |
114 I2C_FUNC_SMBUS_READ_WORD_DATA))
117 /* Reset device, loads default settings. */
118 ret = i2c_smbus_write_byte(client, SI7020CMD_RESET);
121 /* Wait the maximum power-up time after software reset. */
124 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
128 data = iio_priv(indio_dev);
131 indio_dev->name = dev_name(&client->dev);
132 indio_dev->modes = INDIO_DIRECT_MODE;
133 indio_dev->info = &si7020_info;
134 indio_dev->channels = si7020_channels;
135 indio_dev->num_channels = ARRAY_SIZE(si7020_channels);
137 return devm_iio_device_register(&client->dev, indio_dev);
140 static const struct i2c_device_id si7020_id[] = {
145 MODULE_DEVICE_TABLE(i2c, si7020_id);
147 static const struct of_device_id si7020_dt_ids[] = {
148 { .compatible = "silabs,si7020" },
151 MODULE_DEVICE_TABLE(of, si7020_dt_ids);
153 static struct i2c_driver si7020_driver = {
156 .of_match_table = si7020_dt_ids,
158 .probe_new = si7020_probe,
159 .id_table = si7020_id,
162 module_i2c_driver(si7020_driver);
163 MODULE_DESCRIPTION("Silicon Labs Si7013/20/21 Relative Humidity and Temperature Sensors");
165 MODULE_LICENSE("GPL");