xref: /openbmc/linux/drivers/iio/humidity/si7020.c (revision a8abb5634f185b5842baedfd775d22afa2235c5a)
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>
26064a7463SDavid Barksdale 
27064a7463SDavid Barksdale #include <linux/iio/iio.h>
28064a7463SDavid Barksdale #include <linux/iio/sysfs.h>
29064a7463SDavid Barksdale 
30064a7463SDavid Barksdale /* Measure Relative Humidity, Hold Master Mode */
31064a7463SDavid Barksdale #define SI7020CMD_RH_HOLD	0xE5
32064a7463SDavid Barksdale /* Measure Temperature, Hold Master Mode */
33064a7463SDavid Barksdale #define SI7020CMD_TEMP_HOLD	0xE3
34064a7463SDavid Barksdale /* Software Reset */
35064a7463SDavid Barksdale #define SI7020CMD_RESET		0xFE
36064a7463SDavid Barksdale 
si7020_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)37064a7463SDavid Barksdale static int si7020_read_raw(struct iio_dev *indio_dev,
38064a7463SDavid Barksdale 			   struct iio_chan_spec const *chan, int *val,
39064a7463SDavid Barksdale 			   int *val2, long mask)
40064a7463SDavid Barksdale {
41e765537aSJonathan Cameron 	struct i2c_client **client = iio_priv(indio_dev);
42064a7463SDavid Barksdale 	int ret;
43064a7463SDavid Barksdale 
44064a7463SDavid Barksdale 	switch (mask) {
45064a7463SDavid Barksdale 	case IIO_CHAN_INFO_RAW:
460d2f6fd3SChris Lesiak 		ret = i2c_smbus_read_word_swapped(*client,
47064a7463SDavid Barksdale 						  chan->type == IIO_TEMP ?
48064a7463SDavid Barksdale 						  SI7020CMD_TEMP_HOLD :
49064a7463SDavid Barksdale 						  SI7020CMD_RH_HOLD);
50064a7463SDavid Barksdale 		if (ret < 0)
51064a7463SDavid Barksdale 			return ret;
52064a7463SDavid Barksdale 		*val = ret >> 2;
53ecf7e207SNicola Corna 		/*
54ecf7e207SNicola Corna 		 * Humidity values can slightly exceed the 0-100%RH
55ecf7e207SNicola Corna 		 * range and should be corrected by software
56ecf7e207SNicola Corna 		 */
57345b4830SHartmut Knaack 		if (chan->type == IIO_HUMIDITYRELATIVE)
58ecf7e207SNicola Corna 			*val = clamp_val(*val, 786, 13893);
59064a7463SDavid Barksdale 		return IIO_VAL_INT;
60064a7463SDavid Barksdale 	case IIO_CHAN_INFO_SCALE:
61064a7463SDavid Barksdale 		if (chan->type == IIO_TEMP)
62064a7463SDavid Barksdale 			*val = 175720; /* = 175.72 * 1000 */
63064a7463SDavid Barksdale 		else
64064a7463SDavid Barksdale 			*val = 125 * 1000;
65064a7463SDavid Barksdale 		*val2 = 65536 >> 2;
66064a7463SDavid Barksdale 		return IIO_VAL_FRACTIONAL;
67064a7463SDavid Barksdale 	case IIO_CHAN_INFO_OFFSET:
68064a7463SDavid Barksdale 		/*
69064a7463SDavid Barksdale 		 * Since iio_convert_raw_to_processed_unlocked assumes offset
70064a7463SDavid Barksdale 		 * is an integer we have to round these values and lose
71064a7463SDavid Barksdale 		 * accuracy.
72064a7463SDavid Barksdale 		 * Relative humidity will be 0.0032959% too high and
73064a7463SDavid Barksdale 		 * temperature will be 0.00277344 degrees too high.
74064a7463SDavid Barksdale 		 * This is no big deal because it's within the accuracy of the
75064a7463SDavid Barksdale 		 * sensor.
76064a7463SDavid Barksdale 		 */
77064a7463SDavid Barksdale 		if (chan->type == IIO_TEMP)
78064a7463SDavid Barksdale 			*val = -4368; /* = -46.85 * (65536 >> 2) / 175.72 */
79064a7463SDavid Barksdale 		else
80064a7463SDavid Barksdale 			*val = -786; /* = -6 * (65536 >> 2) / 125 */
81064a7463SDavid Barksdale 		return IIO_VAL_INT;
82064a7463SDavid Barksdale 	default:
83064a7463SDavid Barksdale 		break;
84064a7463SDavid Barksdale 	}
85064a7463SDavid Barksdale 
86064a7463SDavid Barksdale 	return -EINVAL;
87064a7463SDavid Barksdale }
88064a7463SDavid Barksdale 
89064a7463SDavid Barksdale static const struct iio_chan_spec si7020_channels[] = {
90064a7463SDavid Barksdale 	{
91064a7463SDavid Barksdale 		.type = IIO_HUMIDITYRELATIVE,
92064a7463SDavid Barksdale 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
93064a7463SDavid Barksdale 			BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_OFFSET),
94064a7463SDavid Barksdale 	},
95064a7463SDavid Barksdale 	{
96064a7463SDavid Barksdale 		.type = IIO_TEMP,
97064a7463SDavid Barksdale 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
98064a7463SDavid Barksdale 			BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_OFFSET),
99064a7463SDavid Barksdale 	}
100064a7463SDavid Barksdale };
101064a7463SDavid Barksdale 
102064a7463SDavid Barksdale static const struct iio_info si7020_info = {
103064a7463SDavid Barksdale 	.read_raw = si7020_read_raw,
104064a7463SDavid Barksdale };
105064a7463SDavid Barksdale 
si7020_probe(struct i2c_client * client)106e6b61055SUwe Kleine-König static int si7020_probe(struct i2c_client *client)
107064a7463SDavid Barksdale {
108*a8abb563SEddie James 	struct i2c_adapter *root;
109064a7463SDavid Barksdale 	struct iio_dev *indio_dev;
110064a7463SDavid Barksdale 	struct i2c_client **data;
111064a7463SDavid Barksdale 	int ret;
112064a7463SDavid Barksdale 
113064a7463SDavid Barksdale 	if (!i2c_check_functionality(client->adapter,
114064a7463SDavid Barksdale 				     I2C_FUNC_SMBUS_WRITE_BYTE |
115064a7463SDavid Barksdale 				     I2C_FUNC_SMBUS_READ_WORD_DATA))
116f8d9d3b4SMatt Ranostay 		return -EOPNOTSUPP;
117064a7463SDavid Barksdale 
118*a8abb563SEddie James 	root = i2c_lock_select_bus(client->adapter);
119*a8abb563SEddie James 	if (IS_ERR(root))
120*a8abb563SEddie James 		return PTR_ERR(root);
121*a8abb563SEddie James 
122064a7463SDavid Barksdale 	/* Reset device, loads default settings. */
123*a8abb563SEddie James 	ret = __i2c_smbus_xfer(root, client->addr, client->flags,
124*a8abb563SEddie James 			       I2C_SMBUS_WRITE, SI7020CMD_RESET,
125*a8abb563SEddie James 			       I2C_SMBUS_BYTE, NULL);
126*a8abb563SEddie James 	if (ret < 0) {
127*a8abb563SEddie James 		i2c_unlock_deselect_bus(client->adapter);
128064a7463SDavid Barksdale 		return ret;
129*a8abb563SEddie James 	}
130*a8abb563SEddie James 
131064a7463SDavid Barksdale 	/* Wait the maximum power-up time after software reset. */
132064a7463SDavid Barksdale 	msleep(15);
133064a7463SDavid Barksdale 
134*a8abb563SEddie James 	i2c_unlock_deselect_bus(client->adapter);
135*a8abb563SEddie James 
136e01becbaSAndrey Smirnov 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
137064a7463SDavid Barksdale 	if (!indio_dev)
138064a7463SDavid Barksdale 		return -ENOMEM;
139064a7463SDavid Barksdale 
140064a7463SDavid Barksdale 	data = iio_priv(indio_dev);
141064a7463SDavid Barksdale 	*data = client;
142064a7463SDavid Barksdale 
143064a7463SDavid Barksdale 	indio_dev->name = dev_name(&client->dev);
144064a7463SDavid Barksdale 	indio_dev->modes = INDIO_DIRECT_MODE;
145064a7463SDavid Barksdale 	indio_dev->info = &si7020_info;
146064a7463SDavid Barksdale 	indio_dev->channels = si7020_channels;
147064a7463SDavid Barksdale 	indio_dev->num_channels = ARRAY_SIZE(si7020_channels);
148064a7463SDavid Barksdale 
149064a7463SDavid Barksdale 	return devm_iio_device_register(&client->dev, indio_dev);
150064a7463SDavid Barksdale }
151064a7463SDavid Barksdale 
152064a7463SDavid Barksdale static const struct i2c_device_id si7020_id[] = {
153064a7463SDavid Barksdale 	{ "si7020", 0 },
154920dad0cSCristina Moraru 	{ "th06", 0 },
155064a7463SDavid Barksdale 	{ }
156064a7463SDavid Barksdale };
157064a7463SDavid Barksdale MODULE_DEVICE_TABLE(i2c, si7020_id);
158064a7463SDavid Barksdale 
159b0d80198SPaul Kocialkowski static const struct of_device_id si7020_dt_ids[] = {
160b0d80198SPaul Kocialkowski 	{ .compatible = "silabs,si7020" },
161b0d80198SPaul Kocialkowski 	{ }
162b0d80198SPaul Kocialkowski };
163b0d80198SPaul Kocialkowski MODULE_DEVICE_TABLE(of, si7020_dt_ids);
164b0d80198SPaul Kocialkowski 
165064a7463SDavid Barksdale static struct i2c_driver si7020_driver = {
166b0d80198SPaul Kocialkowski 	.driver = {
167b0d80198SPaul Kocialkowski 		.name = "si7020",
1684231f9d1SJonathan Cameron 		.of_match_table = si7020_dt_ids,
169b0d80198SPaul Kocialkowski 	},
1707cf15f42SUwe Kleine-König 	.probe		= si7020_probe,
171064a7463SDavid Barksdale 	.id_table	= si7020_id,
172064a7463SDavid Barksdale };
173064a7463SDavid Barksdale 
174064a7463SDavid Barksdale module_i2c_driver(si7020_driver);
175064a7463SDavid Barksdale MODULE_DESCRIPTION("Silicon Labs Si7013/20/21 Relative Humidity and Temperature Sensors");
176064a7463SDavid Barksdale MODULE_AUTHOR("David Barksdale <dbarksdale@uplogix.com>");
177064a7463SDavid Barksdale MODULE_LICENSE("GPL");
178