1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * si7020.c - Silicon Labs Si7013/20/21 Relative Humidity and Temp Sensors
4 * Copyright (c) 2013,2014 Uplogix, Inc.
5 * David Barksdale <dbarksdale@uplogix.com>
6 */
7
8 /*
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.
13 *
14 * Data Sheets:
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
18 */
19
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>
26
27 #include <linux/iio/iio.h>
28 #include <linux/iio/sysfs.h>
29
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
34 /* Software Reset */
35 #define SI7020CMD_RESET 0xFE
36
si7020_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)37 static int si7020_read_raw(struct iio_dev *indio_dev,
38 struct iio_chan_spec const *chan, int *val,
39 int *val2, long mask)
40 {
41 struct i2c_client **client = iio_priv(indio_dev);
42 int ret;
43
44 switch (mask) {
45 case IIO_CHAN_INFO_RAW:
46 ret = i2c_smbus_read_word_swapped(*client,
47 chan->type == IIO_TEMP ?
48 SI7020CMD_TEMP_HOLD :
49 SI7020CMD_RH_HOLD);
50 if (ret < 0)
51 return ret;
52 *val = ret >> 2;
53 /*
54 * Humidity values can slightly exceed the 0-100%RH
55 * range and should be corrected by software
56 */
57 if (chan->type == IIO_HUMIDITYRELATIVE)
58 *val = clamp_val(*val, 786, 13893);
59 return IIO_VAL_INT;
60 case IIO_CHAN_INFO_SCALE:
61 if (chan->type == IIO_TEMP)
62 *val = 175720; /* = 175.72 * 1000 */
63 else
64 *val = 125 * 1000;
65 *val2 = 65536 >> 2;
66 return IIO_VAL_FRACTIONAL;
67 case IIO_CHAN_INFO_OFFSET:
68 /*
69 * Since iio_convert_raw_to_processed_unlocked assumes offset
70 * is an integer we have to round these values and lose
71 * accuracy.
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
75 * sensor.
76 */
77 if (chan->type == IIO_TEMP)
78 *val = -4368; /* = -46.85 * (65536 >> 2) / 175.72 */
79 else
80 *val = -786; /* = -6 * (65536 >> 2) / 125 */
81 return IIO_VAL_INT;
82 default:
83 break;
84 }
85
86 return -EINVAL;
87 }
88
89 static const struct iio_chan_spec si7020_channels[] = {
90 {
91 .type = IIO_HUMIDITYRELATIVE,
92 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
93 BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_OFFSET),
94 },
95 {
96 .type = IIO_TEMP,
97 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
98 BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_OFFSET),
99 }
100 };
101
102 static const struct iio_info si7020_info = {
103 .read_raw = si7020_read_raw,
104 };
105
si7020_probe(struct i2c_client * client)106 static int si7020_probe(struct i2c_client *client)
107 {
108 struct iio_dev *indio_dev;
109 struct i2c_client **data;
110 int ret;
111
112 if (!i2c_check_functionality(client->adapter,
113 I2C_FUNC_SMBUS_WRITE_BYTE |
114 I2C_FUNC_SMBUS_READ_WORD_DATA))
115 return -EOPNOTSUPP;
116
117 /* Reset device, loads default settings. */
118 ret = i2c_smbus_write_byte(client, SI7020CMD_RESET);
119 if (ret < 0)
120 return ret;
121 /* Wait the maximum power-up time after software reset. */
122 msleep(15);
123
124 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
125 if (!indio_dev)
126 return -ENOMEM;
127
128 data = iio_priv(indio_dev);
129 *data = client;
130
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);
136
137 return devm_iio_device_register(&client->dev, indio_dev);
138 }
139
140 static const struct i2c_device_id si7020_id[] = {
141 { "si7020", 0 },
142 { "th06", 0 },
143 { }
144 };
145 MODULE_DEVICE_TABLE(i2c, si7020_id);
146
147 static const struct of_device_id si7020_dt_ids[] = {
148 { .compatible = "silabs,si7020" },
149 { }
150 };
151 MODULE_DEVICE_TABLE(of, si7020_dt_ids);
152
153 static struct i2c_driver si7020_driver = {
154 .driver = {
155 .name = "si7020",
156 .of_match_table = si7020_dt_ids,
157 },
158 .probe = si7020_probe,
159 .id_table = si7020_id,
160 };
161
162 module_i2c_driver(si7020_driver);
163 MODULE_DESCRIPTION("Silicon Labs Si7013/20/21 Relative Humidity and Temperature Sensors");
164 MODULE_AUTHOR("David Barksdale <dbarksdale@uplogix.com>");
165 MODULE_LICENSE("GPL");
166