xref: /linux/drivers/hwmon/sht21.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
1fd534e9bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2430400b8SUrs Fleisch /* Sensirion SHT21 humidity and temperature sensor driver
3430400b8SUrs Fleisch  *
4430400b8SUrs Fleisch  * Copyright (C) 2010 Urs Fleisch <urs.fleisch@sensirion.com>
5430400b8SUrs Fleisch  *
6ad736c1aSAlexander A. Klimov  * Data sheet available at https://www.sensirion.com/file/datasheet_sht21
7430400b8SUrs Fleisch  */
8430400b8SUrs Fleisch 
9430400b8SUrs Fleisch #include <linux/module.h>
10430400b8SUrs Fleisch #include <linux/init.h>
11430400b8SUrs Fleisch #include <linux/slab.h>
12430400b8SUrs Fleisch #include <linux/i2c.h>
13430400b8SUrs Fleisch #include <linux/hwmon.h>
14430400b8SUrs Fleisch #include <linux/hwmon-sysfs.h>
15430400b8SUrs Fleisch #include <linux/err.h>
16430400b8SUrs Fleisch #include <linux/mutex.h>
17430400b8SUrs Fleisch #include <linux/device.h>
18dcd8f392SJean Delvare #include <linux/jiffies.h>
19430400b8SUrs Fleisch 
20430400b8SUrs Fleisch /* I2C command bytes */
21430400b8SUrs Fleisch #define SHT21_TRIG_T_MEASUREMENT_HM  0xe3
22430400b8SUrs Fleisch #define SHT21_TRIG_RH_MEASUREMENT_HM 0xe5
2353e678d7SPeter A. Bigot #define SHT21_READ_SNB_CMD1 0xFA
2453e678d7SPeter A. Bigot #define SHT21_READ_SNB_CMD2 0x0F
2553e678d7SPeter A. Bigot #define SHT21_READ_SNAC_CMD1 0xFC
2653e678d7SPeter A. Bigot #define SHT21_READ_SNAC_CMD2 0xC9
27430400b8SUrs Fleisch 
28430400b8SUrs Fleisch /**
29430400b8SUrs Fleisch  * struct sht21 - SHT21 device specific data
30679f50b8SGuenter Roeck  * @client: I2C client device
31430400b8SUrs Fleisch  * @lock: mutex to protect measurement values
32430400b8SUrs Fleisch  * @last_update: time of last update (jiffies)
33430400b8SUrs Fleisch  * @temperature: cached temperature measurement value
34430400b8SUrs Fleisch  * @humidity: cached humidity measurement value
3553e678d7SPeter A. Bigot  * @valid: only 0 before first measurement is taken
3653e678d7SPeter A. Bigot  * @eic: cached electronic identification code text
37430400b8SUrs Fleisch  */
38430400b8SUrs Fleisch struct sht21 {
39b8d56898SAxel Lin 	struct i2c_client *client;
40430400b8SUrs Fleisch 	struct mutex lock;
41430400b8SUrs Fleisch 	unsigned long last_update;
42430400b8SUrs Fleisch 	int temperature;
43430400b8SUrs Fleisch 	int humidity;
44952a11caSPaul Fertser 	bool valid;
4553e678d7SPeter A. Bigot 	char eic[18];
46430400b8SUrs Fleisch };
47430400b8SUrs Fleisch 
48430400b8SUrs Fleisch /**
49430400b8SUrs Fleisch  * sht21_temp_ticks_to_millicelsius() - convert raw temperature ticks to
50430400b8SUrs Fleisch  * milli celsius
51430400b8SUrs Fleisch  * @ticks: temperature ticks value received from sensor
52430400b8SUrs Fleisch  */
sht21_temp_ticks_to_millicelsius(int ticks)53430400b8SUrs Fleisch static inline int sht21_temp_ticks_to_millicelsius(int ticks)
54430400b8SUrs Fleisch {
55430400b8SUrs Fleisch 	ticks &= ~0x0003; /* clear status bits */
56430400b8SUrs Fleisch 	/*
57430400b8SUrs Fleisch 	 * Formula T = -46.85 + 175.72 * ST / 2^16 from data sheet 6.2,
58430400b8SUrs Fleisch 	 * optimized for integer fixed point (3 digits) arithmetic
59430400b8SUrs Fleisch 	 */
60430400b8SUrs Fleisch 	return ((21965 * ticks) >> 13) - 46850;
61430400b8SUrs Fleisch }
62430400b8SUrs Fleisch 
63430400b8SUrs Fleisch /**
64430400b8SUrs Fleisch  * sht21_rh_ticks_to_per_cent_mille() - convert raw humidity ticks to
65430400b8SUrs Fleisch  * one-thousandths of a percent relative humidity
66430400b8SUrs Fleisch  * @ticks: humidity ticks value received from sensor
67430400b8SUrs Fleisch  */
sht21_rh_ticks_to_per_cent_mille(int ticks)68430400b8SUrs Fleisch static inline int sht21_rh_ticks_to_per_cent_mille(int ticks)
69430400b8SUrs Fleisch {
70430400b8SUrs Fleisch 	ticks &= ~0x0003; /* clear status bits */
71430400b8SUrs Fleisch 	/*
72430400b8SUrs Fleisch 	 * Formula RH = -6 + 125 * SRH / 2^16 from data sheet 6.1,
73430400b8SUrs Fleisch 	 * optimized for integer fixed point (3 digits) arithmetic
74430400b8SUrs Fleisch 	 */
75430400b8SUrs Fleisch 	return ((15625 * ticks) >> 13) - 6000;
76430400b8SUrs Fleisch }
77430400b8SUrs Fleisch 
78430400b8SUrs Fleisch /**
79430400b8SUrs Fleisch  * sht21_update_measurements() - get updated measurements from device
80b8d56898SAxel Lin  * @dev: device
81430400b8SUrs Fleisch  *
82430400b8SUrs Fleisch  * Returns 0 on success, else negative errno.
83430400b8SUrs Fleisch  */
sht21_update_measurements(struct device * dev)84b8d56898SAxel Lin static int sht21_update_measurements(struct device *dev)
85430400b8SUrs Fleisch {
86430400b8SUrs Fleisch 	int ret = 0;
87b8d56898SAxel Lin 	struct sht21 *sht21 = dev_get_drvdata(dev);
88b8d56898SAxel Lin 	struct i2c_client *client = sht21->client;
89430400b8SUrs Fleisch 
90430400b8SUrs Fleisch 	mutex_lock(&sht21->lock);
91430400b8SUrs Fleisch 	/*
92430400b8SUrs Fleisch 	 * Data sheet 2.4:
93430400b8SUrs Fleisch 	 * SHT2x should not be active for more than 10% of the time - e.g.
94430400b8SUrs Fleisch 	 * maximum two measurements per second at 12bit accuracy shall be made.
95430400b8SUrs Fleisch 	 */
96430400b8SUrs Fleisch 	if (time_after(jiffies, sht21->last_update + HZ / 2) || !sht21->valid) {
9790f4102cSJean Delvare 		ret = i2c_smbus_read_word_swapped(client,
9890f4102cSJean Delvare 						  SHT21_TRIG_T_MEASUREMENT_HM);
99430400b8SUrs Fleisch 		if (ret < 0)
100430400b8SUrs Fleisch 			goto out;
101430400b8SUrs Fleisch 		sht21->temperature = sht21_temp_ticks_to_millicelsius(ret);
10290f4102cSJean Delvare 		ret = i2c_smbus_read_word_swapped(client,
103430400b8SUrs Fleisch 						  SHT21_TRIG_RH_MEASUREMENT_HM);
104430400b8SUrs Fleisch 		if (ret < 0)
105430400b8SUrs Fleisch 			goto out;
106430400b8SUrs Fleisch 		sht21->humidity = sht21_rh_ticks_to_per_cent_mille(ret);
107430400b8SUrs Fleisch 		sht21->last_update = jiffies;
108952a11caSPaul Fertser 		sht21->valid = true;
109430400b8SUrs Fleisch 	}
110430400b8SUrs Fleisch out:
111430400b8SUrs Fleisch 	mutex_unlock(&sht21->lock);
112430400b8SUrs Fleisch 
113430400b8SUrs Fleisch 	return ret >= 0 ? 0 : ret;
114430400b8SUrs Fleisch }
115430400b8SUrs Fleisch 
116430400b8SUrs Fleisch /**
117ffdb3ba0SRandy Dunlap  * sht21_temperature_show() - show temperature measurement value in sysfs
118430400b8SUrs Fleisch  * @dev: device
119430400b8SUrs Fleisch  * @attr: device attribute
120430400b8SUrs Fleisch  * @buf: sysfs buffer (PAGE_SIZE) where measurement values are written to
121430400b8SUrs Fleisch  *
122430400b8SUrs Fleisch  * Will be called on read access to temp1_input sysfs attribute.
123430400b8SUrs Fleisch  * Returns number of bytes written into buffer, negative errno on error.
124430400b8SUrs Fleisch  */
sht21_temperature_show(struct device * dev,struct device_attribute * attr,char * buf)1255512eb06SGuenter Roeck static ssize_t sht21_temperature_show(struct device *dev,
126430400b8SUrs Fleisch 				      struct device_attribute *attr,
127430400b8SUrs Fleisch 				      char *buf)
128430400b8SUrs Fleisch {
129b8d56898SAxel Lin 	struct sht21 *sht21 = dev_get_drvdata(dev);
130b8d56898SAxel Lin 	int ret;
131b8d56898SAxel Lin 
132b8d56898SAxel Lin 	ret = sht21_update_measurements(dev);
133430400b8SUrs Fleisch 	if (ret < 0)
134430400b8SUrs Fleisch 		return ret;
135430400b8SUrs Fleisch 	return sprintf(buf, "%d\n", sht21->temperature);
136430400b8SUrs Fleisch }
137430400b8SUrs Fleisch 
138430400b8SUrs Fleisch /**
139ffdb3ba0SRandy Dunlap  * sht21_humidity_show() - show humidity measurement value in sysfs
140430400b8SUrs Fleisch  * @dev: device
141430400b8SUrs Fleisch  * @attr: device attribute
142430400b8SUrs Fleisch  * @buf: sysfs buffer (PAGE_SIZE) where measurement values are written to
143430400b8SUrs Fleisch  *
144430400b8SUrs Fleisch  * Will be called on read access to humidity1_input sysfs attribute.
145430400b8SUrs Fleisch  * Returns number of bytes written into buffer, negative errno on error.
146430400b8SUrs Fleisch  */
sht21_humidity_show(struct device * dev,struct device_attribute * attr,char * buf)1475512eb06SGuenter Roeck static ssize_t sht21_humidity_show(struct device *dev,
1485512eb06SGuenter Roeck 				   struct device_attribute *attr, char *buf)
149430400b8SUrs Fleisch {
150b8d56898SAxel Lin 	struct sht21 *sht21 = dev_get_drvdata(dev);
151b8d56898SAxel Lin 	int ret;
152b8d56898SAxel Lin 
153b8d56898SAxel Lin 	ret = sht21_update_measurements(dev);
154430400b8SUrs Fleisch 	if (ret < 0)
155430400b8SUrs Fleisch 		return ret;
156430400b8SUrs Fleisch 	return sprintf(buf, "%d\n", sht21->humidity);
157430400b8SUrs Fleisch }
158430400b8SUrs Fleisch 
eic_read(struct sht21 * sht21)15953e678d7SPeter A. Bigot static ssize_t eic_read(struct sht21 *sht21)
16053e678d7SPeter A. Bigot {
16153e678d7SPeter A. Bigot 	struct i2c_client *client = sht21->client;
16253e678d7SPeter A. Bigot 	u8 tx[2];
16353e678d7SPeter A. Bigot 	u8 rx[8];
16453e678d7SPeter A. Bigot 	u8 eic[8];
16553e678d7SPeter A. Bigot 	struct i2c_msg msgs[2] = {
16653e678d7SPeter A. Bigot 		{
16753e678d7SPeter A. Bigot 			.addr = client->addr,
16853e678d7SPeter A. Bigot 			.flags = 0,
16953e678d7SPeter A. Bigot 			.len = 2,
17053e678d7SPeter A. Bigot 			.buf = tx,
17153e678d7SPeter A. Bigot 		},
17253e678d7SPeter A. Bigot 		{
17353e678d7SPeter A. Bigot 			.addr = client->addr,
17453e678d7SPeter A. Bigot 			.flags = I2C_M_RD,
17553e678d7SPeter A. Bigot 			.len = 8,
17653e678d7SPeter A. Bigot 			.buf = rx,
17753e678d7SPeter A. Bigot 		},
17853e678d7SPeter A. Bigot 	};
17953e678d7SPeter A. Bigot 	int ret;
18053e678d7SPeter A. Bigot 
18153e678d7SPeter A. Bigot 	tx[0] = SHT21_READ_SNB_CMD1;
18253e678d7SPeter A. Bigot 	tx[1] = SHT21_READ_SNB_CMD2;
18353e678d7SPeter A. Bigot 	ret = i2c_transfer(client->adapter, msgs, 2);
18453e678d7SPeter A. Bigot 	if (ret < 0)
18553e678d7SPeter A. Bigot 		goto out;
18653e678d7SPeter A. Bigot 	eic[2] = rx[0];
18753e678d7SPeter A. Bigot 	eic[3] = rx[2];
18853e678d7SPeter A. Bigot 	eic[4] = rx[4];
18953e678d7SPeter A. Bigot 	eic[5] = rx[6];
19053e678d7SPeter A. Bigot 
19153e678d7SPeter A. Bigot 	tx[0] = SHT21_READ_SNAC_CMD1;
19253e678d7SPeter A. Bigot 	tx[1] = SHT21_READ_SNAC_CMD2;
19353e678d7SPeter A. Bigot 	msgs[1].len = 6;
19453e678d7SPeter A. Bigot 	ret = i2c_transfer(client->adapter, msgs, 2);
19553e678d7SPeter A. Bigot 	if (ret < 0)
19653e678d7SPeter A. Bigot 		goto out;
19753e678d7SPeter A. Bigot 	eic[0] = rx[3];
19853e678d7SPeter A. Bigot 	eic[1] = rx[4];
19953e678d7SPeter A. Bigot 	eic[6] = rx[0];
20053e678d7SPeter A. Bigot 	eic[7] = rx[1];
20153e678d7SPeter A. Bigot 
202*38f9fa39SAndy Shevchenko 	ret = snprintf(sht21->eic, sizeof(sht21->eic), "%8phN\n", eic);
20353e678d7SPeter A. Bigot out:
20453e678d7SPeter A. Bigot 	if (ret < 0)
20553e678d7SPeter A. Bigot 		sht21->eic[0] = 0;
20653e678d7SPeter A. Bigot 
20753e678d7SPeter A. Bigot 	return ret;
20853e678d7SPeter A. Bigot }
20953e678d7SPeter A. Bigot 
21053e678d7SPeter A. Bigot /**
21153e678d7SPeter A. Bigot  * eic_show() - show Electronic Identification Code in sysfs
21253e678d7SPeter A. Bigot  * @dev: device
21353e678d7SPeter A. Bigot  * @attr: device attribute
21453e678d7SPeter A. Bigot  * @buf: sysfs buffer (PAGE_SIZE) where EIC is written
21553e678d7SPeter A. Bigot  *
21653e678d7SPeter A. Bigot  * Will be called on read access to eic sysfs attribute.
21753e678d7SPeter A. Bigot  * Returns number of bytes written into buffer, negative errno on error.
21853e678d7SPeter A. Bigot  */
eic_show(struct device * dev,struct device_attribute * attr,char * buf)21953e678d7SPeter A. Bigot static ssize_t eic_show(struct device *dev,
22053e678d7SPeter A. Bigot 	struct device_attribute *attr,
22153e678d7SPeter A. Bigot 	char *buf)
22253e678d7SPeter A. Bigot {
22353e678d7SPeter A. Bigot 	struct sht21 *sht21 = dev_get_drvdata(dev);
22453e678d7SPeter A. Bigot 	int ret;
22553e678d7SPeter A. Bigot 
22653e678d7SPeter A. Bigot 	ret = sizeof(sht21->eic) - 1;
22753e678d7SPeter A. Bigot 	mutex_lock(&sht21->lock);
22853e678d7SPeter A. Bigot 	if (!sht21->eic[0])
22953e678d7SPeter A. Bigot 		ret = eic_read(sht21);
23053e678d7SPeter A. Bigot 	if (ret > 0)
23153e678d7SPeter A. Bigot 		memcpy(buf, sht21->eic, ret);
23253e678d7SPeter A. Bigot 	mutex_unlock(&sht21->lock);
23353e678d7SPeter A. Bigot 	return ret;
23453e678d7SPeter A. Bigot }
23553e678d7SPeter A. Bigot 
236430400b8SUrs Fleisch /* sysfs attributes */
2375512eb06SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp1_input, sht21_temperature, 0);
2385512eb06SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(humidity1_input, sht21_humidity, 0);
23953e678d7SPeter A. Bigot static DEVICE_ATTR_RO(eic);
240430400b8SUrs Fleisch 
241b8d56898SAxel Lin static struct attribute *sht21_attrs[] = {
242430400b8SUrs Fleisch 	&sensor_dev_attr_temp1_input.dev_attr.attr,
243430400b8SUrs Fleisch 	&sensor_dev_attr_humidity1_input.dev_attr.attr,
24453e678d7SPeter A. Bigot 	&dev_attr_eic.attr,
245430400b8SUrs Fleisch 	NULL
246430400b8SUrs Fleisch };
247430400b8SUrs Fleisch 
248b8d56898SAxel Lin ATTRIBUTE_GROUPS(sht21);
249430400b8SUrs Fleisch 
sht21_probe(struct i2c_client * client)25067487038SStephen Kitt static int sht21_probe(struct i2c_client *client)
251430400b8SUrs Fleisch {
252b8d56898SAxel Lin 	struct device *dev = &client->dev;
253b8d56898SAxel Lin 	struct device *hwmon_dev;
254430400b8SUrs Fleisch 	struct sht21 *sht21;
255430400b8SUrs Fleisch 
256430400b8SUrs Fleisch 	if (!i2c_check_functionality(client->adapter,
257430400b8SUrs Fleisch 				     I2C_FUNC_SMBUS_WORD_DATA)) {
258430400b8SUrs Fleisch 		dev_err(&client->dev,
259430400b8SUrs Fleisch 			"adapter does not support SMBus word transactions\n");
260430400b8SUrs Fleisch 		return -ENODEV;
261430400b8SUrs Fleisch 	}
262430400b8SUrs Fleisch 
263b8d56898SAxel Lin 	sht21 = devm_kzalloc(dev, sizeof(*sht21), GFP_KERNEL);
264a844af19SGuenter Roeck 	if (!sht21)
265430400b8SUrs Fleisch 		return -ENOMEM;
266a844af19SGuenter Roeck 
267b8d56898SAxel Lin 	sht21->client = client;
268430400b8SUrs Fleisch 
269430400b8SUrs Fleisch 	mutex_init(&sht21->lock);
270430400b8SUrs Fleisch 
271b8d56898SAxel Lin 	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
272b8d56898SAxel Lin 							   sht21, sht21_groups);
273b8d56898SAxel Lin 	return PTR_ERR_OR_ZERO(hwmon_dev);
274430400b8SUrs Fleisch }
275430400b8SUrs Fleisch 
276430400b8SUrs Fleisch /* Device ID table */
277430400b8SUrs Fleisch static const struct i2c_device_id sht21_id[] = {
278d8a66f36SUwe Kleine-König 	{ "sht21" },
279430400b8SUrs Fleisch 	{ }
280430400b8SUrs Fleisch };
281430400b8SUrs Fleisch MODULE_DEVICE_TABLE(i2c, sht21_id);
282430400b8SUrs Fleisch 
283430400b8SUrs Fleisch static struct i2c_driver sht21_driver = {
284430400b8SUrs Fleisch 	.driver.name = "sht21",
2851975d167SUwe Kleine-König 	.probe       = sht21_probe,
286430400b8SUrs Fleisch 	.id_table    = sht21_id,
287430400b8SUrs Fleisch };
288430400b8SUrs Fleisch 
289f0967eeaSAxel Lin module_i2c_driver(sht21_driver);
290430400b8SUrs Fleisch 
291430400b8SUrs Fleisch MODULE_AUTHOR("Urs Fleisch <urs.fleisch@sensirion.com>");
292430400b8SUrs Fleisch MODULE_DESCRIPTION("Sensirion SHT21 humidity and temperature sensor driver");
293430400b8SUrs Fleisch MODULE_LICENSE("GPL");
294