14c3dd9cdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2064a7463SDavid Barksdale /*
3064a7463SDavid Barksdale * si7020.c - Silicon Labs Si7013/20/21 Relative Humidity and Temp Sensors
4064a7463SDavid Barksdale * Copyright (c) 2013,2014 Uplogix, Inc.
5064a7463SDavid Barksdale * David Barksdale <dbarksdale@uplogix.com>
6064a7463SDavid Barksdale */
7064a7463SDavid Barksdale
8064a7463SDavid Barksdale /*
9064a7463SDavid Barksdale * The Silicon Labs Si7013/20/21 Relative Humidity and Temperature Sensors
10064a7463SDavid Barksdale * are i2c devices which have an identical programming interface for
11064a7463SDavid Barksdale * measuring relative humidity and temperature. The Si7013 has an additional
12064a7463SDavid Barksdale * temperature input which this driver does not support.
13064a7463SDavid Barksdale *
14064a7463SDavid Barksdale * Data Sheets:
15064a7463SDavid Barksdale * Si7013: http://www.silabs.com/Support%20Documents/TechnicalDocs/Si7013.pdf
16064a7463SDavid Barksdale * Si7020: http://www.silabs.com/Support%20Documents/TechnicalDocs/Si7020.pdf
17064a7463SDavid Barksdale * Si7021: http://www.silabs.com/Support%20Documents/TechnicalDocs/Si7021.pdf
18064a7463SDavid Barksdale */
19064a7463SDavid Barksdale
20064a7463SDavid Barksdale #include <linux/delay.h>
21064a7463SDavid Barksdale #include <linux/i2c.h>
22064a7463SDavid Barksdale #include <linux/module.h>
234231f9d1SJonathan Cameron #include <linux/mod_devicetable.h>
24064a7463SDavid Barksdale #include <linux/slab.h>
25064a7463SDavid Barksdale #include <linux/sysfs.h>
26*2aac3f9aSAntoni Pokusinski #include <linux/stat.h>
27064a7463SDavid Barksdale
28064a7463SDavid Barksdale #include <linux/iio/iio.h>
29064a7463SDavid Barksdale #include <linux/iio/sysfs.h>
30064a7463SDavid Barksdale
31064a7463SDavid Barksdale /* Measure Relative Humidity, Hold Master Mode */
32064a7463SDavid Barksdale #define SI7020CMD_RH_HOLD 0xE5
33064a7463SDavid Barksdale /* Measure Temperature, Hold Master Mode */
34064a7463SDavid Barksdale #define SI7020CMD_TEMP_HOLD 0xE3
35064a7463SDavid Barksdale /* Software Reset */
36064a7463SDavid Barksdale #define SI7020CMD_RESET 0xFE
37*2aac3f9aSAntoni Pokusinski #define SI7020CMD_USR_WRITE 0xE6
38*2aac3f9aSAntoni Pokusinski /* "Heater Enabled" bit in the User Register */
39*2aac3f9aSAntoni Pokusinski #define SI7020_USR_HEATER_EN BIT(2)
40*2aac3f9aSAntoni Pokusinski #define SI7020CMD_HEATER_WRITE 0x51
41*2aac3f9aSAntoni Pokusinski /* Heater current configuration bits */
42*2aac3f9aSAntoni Pokusinski #define SI7020_HEATER_VAL GENMASK(3, 0)
43*2aac3f9aSAntoni Pokusinski
44*2aac3f9aSAntoni Pokusinski struct si7020_data {
45*2aac3f9aSAntoni Pokusinski struct i2c_client *client;
46*2aac3f9aSAntoni Pokusinski /* Lock for cached register values */
47*2aac3f9aSAntoni Pokusinski struct mutex lock;
48*2aac3f9aSAntoni Pokusinski u8 user_reg;
49*2aac3f9aSAntoni Pokusinski u8 heater_reg;
50*2aac3f9aSAntoni Pokusinski };
51*2aac3f9aSAntoni Pokusinski
52*2aac3f9aSAntoni Pokusinski static const int si7020_heater_vals[] = { 0, 1, 0xF };
53064a7463SDavid Barksdale
si7020_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)54064a7463SDavid Barksdale static int si7020_read_raw(struct iio_dev *indio_dev,
55064a7463SDavid Barksdale struct iio_chan_spec const *chan, int *val,
56064a7463SDavid Barksdale int *val2, long mask)
57064a7463SDavid Barksdale {
58*2aac3f9aSAntoni Pokusinski struct si7020_data *data = iio_priv(indio_dev);
59064a7463SDavid Barksdale int ret;
60064a7463SDavid Barksdale
61064a7463SDavid Barksdale switch (mask) {
62064a7463SDavid Barksdale case IIO_CHAN_INFO_RAW:
63*2aac3f9aSAntoni Pokusinski if (chan->type == IIO_CURRENT) {
64*2aac3f9aSAntoni Pokusinski *val = data->heater_reg;
65*2aac3f9aSAntoni Pokusinski return IIO_VAL_INT;
66*2aac3f9aSAntoni Pokusinski }
67*2aac3f9aSAntoni Pokusinski
68*2aac3f9aSAntoni Pokusinski ret = i2c_smbus_read_word_swapped(data->client,
69064a7463SDavid Barksdale chan->type == IIO_TEMP ?
70064a7463SDavid Barksdale SI7020CMD_TEMP_HOLD :
71064a7463SDavid Barksdale SI7020CMD_RH_HOLD);
72064a7463SDavid Barksdale if (ret < 0)
73064a7463SDavid Barksdale return ret;
74064a7463SDavid Barksdale *val = ret >> 2;
75ecf7e207SNicola Corna /*
76ecf7e207SNicola Corna * Humidity values can slightly exceed the 0-100%RH
77ecf7e207SNicola Corna * range and should be corrected by software
78ecf7e207SNicola Corna */
79345b4830SHartmut Knaack if (chan->type == IIO_HUMIDITYRELATIVE)
80ecf7e207SNicola Corna *val = clamp_val(*val, 786, 13893);
81064a7463SDavid Barksdale return IIO_VAL_INT;
82064a7463SDavid Barksdale case IIO_CHAN_INFO_SCALE:
83064a7463SDavid Barksdale if (chan->type == IIO_TEMP)
84064a7463SDavid Barksdale *val = 175720; /* = 175.72 * 1000 */
85064a7463SDavid Barksdale else
86064a7463SDavid Barksdale *val = 125 * 1000;
87064a7463SDavid Barksdale *val2 = 65536 >> 2;
88064a7463SDavid Barksdale return IIO_VAL_FRACTIONAL;
89064a7463SDavid Barksdale case IIO_CHAN_INFO_OFFSET:
90064a7463SDavid Barksdale /*
91064a7463SDavid Barksdale * Since iio_convert_raw_to_processed_unlocked assumes offset
92064a7463SDavid Barksdale * is an integer we have to round these values and lose
93064a7463SDavid Barksdale * accuracy.
94064a7463SDavid Barksdale * Relative humidity will be 0.0032959% too high and
95064a7463SDavid Barksdale * temperature will be 0.00277344 degrees too high.
96064a7463SDavid Barksdale * This is no big deal because it's within the accuracy of the
97064a7463SDavid Barksdale * sensor.
98064a7463SDavid Barksdale */
99064a7463SDavid Barksdale if (chan->type == IIO_TEMP)
100064a7463SDavid Barksdale *val = -4368; /* = -46.85 * (65536 >> 2) / 175.72 */
101064a7463SDavid Barksdale else
102064a7463SDavid Barksdale *val = -786; /* = -6 * (65536 >> 2) / 125 */
103064a7463SDavid Barksdale return IIO_VAL_INT;
104064a7463SDavid Barksdale default:
105064a7463SDavid Barksdale break;
106064a7463SDavid Barksdale }
107064a7463SDavid Barksdale
108064a7463SDavid Barksdale return -EINVAL;
109064a7463SDavid Barksdale }
110064a7463SDavid Barksdale
111064a7463SDavid Barksdale static const struct iio_chan_spec si7020_channels[] = {
112064a7463SDavid Barksdale {
113064a7463SDavid Barksdale .type = IIO_HUMIDITYRELATIVE,
114064a7463SDavid Barksdale .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
115064a7463SDavid Barksdale BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_OFFSET),
116064a7463SDavid Barksdale },
117064a7463SDavid Barksdale {
118064a7463SDavid Barksdale .type = IIO_TEMP,
119064a7463SDavid Barksdale .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
120064a7463SDavid Barksdale BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_OFFSET),
121*2aac3f9aSAntoni Pokusinski },
122*2aac3f9aSAntoni Pokusinski {
123*2aac3f9aSAntoni Pokusinski .type = IIO_CURRENT,
124*2aac3f9aSAntoni Pokusinski .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
125*2aac3f9aSAntoni Pokusinski .info_mask_separate_available = BIT(IIO_CHAN_INFO_RAW),
126*2aac3f9aSAntoni Pokusinski .extend_name = "heater",
127064a7463SDavid Barksdale }
128064a7463SDavid Barksdale };
129064a7463SDavid Barksdale
si7020_update_reg(struct si7020_data * data,u8 * reg,u8 cmd,u8 mask,u8 val)130*2aac3f9aSAntoni Pokusinski static int si7020_update_reg(struct si7020_data *data,
131*2aac3f9aSAntoni Pokusinski u8 *reg, u8 cmd, u8 mask, u8 val)
132*2aac3f9aSAntoni Pokusinski {
133*2aac3f9aSAntoni Pokusinski u8 new = (*reg & ~mask) | val;
134*2aac3f9aSAntoni Pokusinski int ret;
135*2aac3f9aSAntoni Pokusinski
136*2aac3f9aSAntoni Pokusinski ret = i2c_smbus_write_byte_data(data->client, cmd, new);
137*2aac3f9aSAntoni Pokusinski if (ret)
138*2aac3f9aSAntoni Pokusinski return ret;
139*2aac3f9aSAntoni Pokusinski
140*2aac3f9aSAntoni Pokusinski *reg = new;
141*2aac3f9aSAntoni Pokusinski
142*2aac3f9aSAntoni Pokusinski return 0;
143*2aac3f9aSAntoni Pokusinski }
144*2aac3f9aSAntoni Pokusinski
si7020_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)145*2aac3f9aSAntoni Pokusinski static int si7020_write_raw(struct iio_dev *indio_dev,
146*2aac3f9aSAntoni Pokusinski struct iio_chan_spec const *chan,
147*2aac3f9aSAntoni Pokusinski int val, int val2, long mask)
148*2aac3f9aSAntoni Pokusinski {
149*2aac3f9aSAntoni Pokusinski struct si7020_data *data = iio_priv(indio_dev);
150*2aac3f9aSAntoni Pokusinski int ret;
151*2aac3f9aSAntoni Pokusinski
152*2aac3f9aSAntoni Pokusinski switch (mask) {
153*2aac3f9aSAntoni Pokusinski case IIO_CHAN_INFO_RAW:
154*2aac3f9aSAntoni Pokusinski if (chan->type != IIO_CURRENT || val2 != 0 ||
155*2aac3f9aSAntoni Pokusinski val < si7020_heater_vals[0] || val > si7020_heater_vals[2])
156*2aac3f9aSAntoni Pokusinski return -EINVAL;
157*2aac3f9aSAntoni Pokusinski
158*2aac3f9aSAntoni Pokusinski scoped_guard(mutex, &data->lock)
159*2aac3f9aSAntoni Pokusinski ret = si7020_update_reg(data, &data->heater_reg,
160*2aac3f9aSAntoni Pokusinski SI7020CMD_HEATER_WRITE, SI7020_HEATER_VAL, val);
161*2aac3f9aSAntoni Pokusinski return ret;
162*2aac3f9aSAntoni Pokusinski default:
163*2aac3f9aSAntoni Pokusinski return -EINVAL;
164*2aac3f9aSAntoni Pokusinski }
165*2aac3f9aSAntoni Pokusinski }
166*2aac3f9aSAntoni Pokusinski
si7020_read_available(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,const int ** vals,int * type,int * length,long mask)167*2aac3f9aSAntoni Pokusinski static int si7020_read_available(struct iio_dev *indio_dev,
168*2aac3f9aSAntoni Pokusinski struct iio_chan_spec const *chan,
169*2aac3f9aSAntoni Pokusinski const int **vals,
170*2aac3f9aSAntoni Pokusinski int *type, int *length, long mask)
171*2aac3f9aSAntoni Pokusinski {
172*2aac3f9aSAntoni Pokusinski if (mask != IIO_CHAN_INFO_RAW || chan->type != IIO_CURRENT)
173*2aac3f9aSAntoni Pokusinski return -EINVAL;
174*2aac3f9aSAntoni Pokusinski
175*2aac3f9aSAntoni Pokusinski *vals = si7020_heater_vals;
176*2aac3f9aSAntoni Pokusinski *type = IIO_VAL_INT;
177*2aac3f9aSAntoni Pokusinski
178*2aac3f9aSAntoni Pokusinski return IIO_AVAIL_RANGE;
179*2aac3f9aSAntoni Pokusinski }
180*2aac3f9aSAntoni Pokusinski
si7020_show_heater_en(struct device * dev,struct device_attribute * attr,char * buf)181*2aac3f9aSAntoni Pokusinski static ssize_t si7020_show_heater_en(struct device *dev,
182*2aac3f9aSAntoni Pokusinski struct device_attribute *attr, char *buf)
183*2aac3f9aSAntoni Pokusinski {
184*2aac3f9aSAntoni Pokusinski struct iio_dev *indio_dev = dev_to_iio_dev(dev);
185*2aac3f9aSAntoni Pokusinski struct si7020_data *data = iio_priv(indio_dev);
186*2aac3f9aSAntoni Pokusinski
187*2aac3f9aSAntoni Pokusinski return sysfs_emit(buf, "%d\n", !!(data->user_reg & SI7020_USR_HEATER_EN));
188*2aac3f9aSAntoni Pokusinski }
189*2aac3f9aSAntoni Pokusinski
si7020_store_heater_en(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)190*2aac3f9aSAntoni Pokusinski static ssize_t si7020_store_heater_en(struct device *dev,
191*2aac3f9aSAntoni Pokusinski struct device_attribute *attr,
192*2aac3f9aSAntoni Pokusinski const char *buf, size_t len)
193*2aac3f9aSAntoni Pokusinski {
194*2aac3f9aSAntoni Pokusinski struct iio_dev *indio_dev = dev_to_iio_dev(dev);
195*2aac3f9aSAntoni Pokusinski struct si7020_data *data = iio_priv(indio_dev);
196*2aac3f9aSAntoni Pokusinski int ret;
197*2aac3f9aSAntoni Pokusinski bool val;
198*2aac3f9aSAntoni Pokusinski
199*2aac3f9aSAntoni Pokusinski ret = kstrtobool(buf, &val);
200*2aac3f9aSAntoni Pokusinski if (ret)
201*2aac3f9aSAntoni Pokusinski return ret;
202*2aac3f9aSAntoni Pokusinski
203*2aac3f9aSAntoni Pokusinski scoped_guard(mutex, &data->lock)
204*2aac3f9aSAntoni Pokusinski ret = si7020_update_reg(data, &data->user_reg, SI7020CMD_USR_WRITE,
205*2aac3f9aSAntoni Pokusinski SI7020_USR_HEATER_EN, val ? SI7020_USR_HEATER_EN : 0);
206*2aac3f9aSAntoni Pokusinski
207*2aac3f9aSAntoni Pokusinski return ret < 0 ? ret : len;
208*2aac3f9aSAntoni Pokusinski }
209*2aac3f9aSAntoni Pokusinski
210*2aac3f9aSAntoni Pokusinski static IIO_DEVICE_ATTR(heater_enable, 0644,
211*2aac3f9aSAntoni Pokusinski si7020_show_heater_en, si7020_store_heater_en, 0);
212*2aac3f9aSAntoni Pokusinski
213*2aac3f9aSAntoni Pokusinski static struct attribute *si7020_attributes[] = {
214*2aac3f9aSAntoni Pokusinski &iio_dev_attr_heater_enable.dev_attr.attr,
215*2aac3f9aSAntoni Pokusinski NULL
216*2aac3f9aSAntoni Pokusinski };
217*2aac3f9aSAntoni Pokusinski
218*2aac3f9aSAntoni Pokusinski static const struct attribute_group si7020_attribute_group = {
219*2aac3f9aSAntoni Pokusinski .attrs = si7020_attributes,
220*2aac3f9aSAntoni Pokusinski };
221*2aac3f9aSAntoni Pokusinski
222064a7463SDavid Barksdale static const struct iio_info si7020_info = {
223064a7463SDavid Barksdale .read_raw = si7020_read_raw,
224*2aac3f9aSAntoni Pokusinski .write_raw = si7020_write_raw,
225*2aac3f9aSAntoni Pokusinski .read_avail = si7020_read_available,
226*2aac3f9aSAntoni Pokusinski .attrs = &si7020_attribute_group,
227064a7463SDavid Barksdale };
228064a7463SDavid Barksdale
si7020_probe(struct i2c_client * client)229e6b61055SUwe Kleine-König static int si7020_probe(struct i2c_client *client)
230064a7463SDavid Barksdale {
231064a7463SDavid Barksdale struct iio_dev *indio_dev;
232*2aac3f9aSAntoni Pokusinski struct si7020_data *data;
233064a7463SDavid Barksdale int ret;
234064a7463SDavid Barksdale
235064a7463SDavid Barksdale if (!i2c_check_functionality(client->adapter,
236064a7463SDavid Barksdale I2C_FUNC_SMBUS_WRITE_BYTE |
237064a7463SDavid Barksdale I2C_FUNC_SMBUS_READ_WORD_DATA))
238f8d9d3b4SMatt Ranostay return -EOPNOTSUPP;
239064a7463SDavid Barksdale
240064a7463SDavid Barksdale /* Reset device, loads default settings. */
241064a7463SDavid Barksdale ret = i2c_smbus_write_byte(client, SI7020CMD_RESET);
242064a7463SDavid Barksdale if (ret < 0)
243064a7463SDavid Barksdale return ret;
244064a7463SDavid Barksdale /* Wait the maximum power-up time after software reset. */
245064a7463SDavid Barksdale msleep(15);
246064a7463SDavid Barksdale
247e01becbaSAndrey Smirnov indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
248064a7463SDavid Barksdale if (!indio_dev)
249064a7463SDavid Barksdale return -ENOMEM;
250064a7463SDavid Barksdale
251064a7463SDavid Barksdale data = iio_priv(indio_dev);
252*2aac3f9aSAntoni Pokusinski i2c_set_clientdata(client, indio_dev);
253*2aac3f9aSAntoni Pokusinski data->client = client;
254*2aac3f9aSAntoni Pokusinski mutex_init(&data->lock);
255064a7463SDavid Barksdale
256064a7463SDavid Barksdale indio_dev->name = dev_name(&client->dev);
257064a7463SDavid Barksdale indio_dev->modes = INDIO_DIRECT_MODE;
258064a7463SDavid Barksdale indio_dev->info = &si7020_info;
259064a7463SDavid Barksdale indio_dev->channels = si7020_channels;
260064a7463SDavid Barksdale indio_dev->num_channels = ARRAY_SIZE(si7020_channels);
261064a7463SDavid Barksdale
262*2aac3f9aSAntoni Pokusinski /* All the "reserved" bits in the User Register are 1s by default */
263*2aac3f9aSAntoni Pokusinski data->user_reg = 0x3A;
264*2aac3f9aSAntoni Pokusinski data->heater_reg = 0x0;
265*2aac3f9aSAntoni Pokusinski
266064a7463SDavid Barksdale return devm_iio_device_register(&client->dev, indio_dev);
267064a7463SDavid Barksdale }
268064a7463SDavid Barksdale
269064a7463SDavid Barksdale static const struct i2c_device_id si7020_id[] = {
2704391affaSUwe Kleine-König { "si7020" },
2714391affaSUwe Kleine-König { "th06" },
272064a7463SDavid Barksdale { }
273064a7463SDavid Barksdale };
274064a7463SDavid Barksdale MODULE_DEVICE_TABLE(i2c, si7020_id);
275064a7463SDavid Barksdale
276b0d80198SPaul Kocialkowski static const struct of_device_id si7020_dt_ids[] = {
277b0d80198SPaul Kocialkowski { .compatible = "silabs,si7020" },
278b0d80198SPaul Kocialkowski { }
279b0d80198SPaul Kocialkowski };
280b0d80198SPaul Kocialkowski MODULE_DEVICE_TABLE(of, si7020_dt_ids);
281b0d80198SPaul Kocialkowski
282064a7463SDavid Barksdale static struct i2c_driver si7020_driver = {
283b0d80198SPaul Kocialkowski .driver = {
284b0d80198SPaul Kocialkowski .name = "si7020",
2854231f9d1SJonathan Cameron .of_match_table = si7020_dt_ids,
286b0d80198SPaul Kocialkowski },
2877cf15f42SUwe Kleine-König .probe = si7020_probe,
288064a7463SDavid Barksdale .id_table = si7020_id,
289064a7463SDavid Barksdale };
290064a7463SDavid Barksdale
291064a7463SDavid Barksdale module_i2c_driver(si7020_driver);
292064a7463SDavid Barksdale MODULE_DESCRIPTION("Silicon Labs Si7013/20/21 Relative Humidity and Temperature Sensors");
293064a7463SDavid Barksdale MODULE_AUTHOR("David Barksdale <dbarksdale@uplogix.com>");
294064a7463SDavid Barksdale MODULE_LICENSE("GPL");
295