xref: /openbmc/linux/drivers/hwmon/shtc1.c (revision 4e95bc26)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Sensirion SHTC1 humidity and temperature sensor driver
3  *
4  * Copyright (C) 2014 Sensirion AG, Switzerland
5  * Author: Johannes Winkelmann <johannes.winkelmann@sensirion.com>
6  */
7 
8 #include <linux/module.h>
9 #include <linux/init.h>
10 #include <linux/slab.h>
11 #include <linux/i2c.h>
12 #include <linux/hwmon.h>
13 #include <linux/hwmon-sysfs.h>
14 #include <linux/err.h>
15 #include <linux/delay.h>
16 #include <linux/platform_data/shtc1.h>
17 
18 /* commands (high precision mode) */
19 static const unsigned char shtc1_cmd_measure_blocking_hpm[]    = { 0x7C, 0xA2 };
20 static const unsigned char shtc1_cmd_measure_nonblocking_hpm[] = { 0x78, 0x66 };
21 
22 /* commands (low precision mode) */
23 static const unsigned char shtc1_cmd_measure_blocking_lpm[]    = { 0x64, 0x58 };
24 static const unsigned char shtc1_cmd_measure_nonblocking_lpm[] = { 0x60, 0x9c };
25 
26 /* command for reading the ID register */
27 static const unsigned char shtc1_cmd_read_id_reg[]	       = { 0xef, 0xc8 };
28 
29 /* constants for reading the ID register */
30 #define SHTC1_ID	  0x07
31 #define SHTC1_ID_REG_MASK 0x1f
32 
33 /* delays for non-blocking i2c commands, both in us */
34 #define SHTC1_NONBLOCKING_WAIT_TIME_HPM  14400
35 #define SHTC1_NONBLOCKING_WAIT_TIME_LPM   1000
36 
37 #define SHTC1_CMD_LENGTH      2
38 #define SHTC1_RESPONSE_LENGTH 6
39 
40 struct shtc1_data {
41 	struct i2c_client *client;
42 	struct mutex update_lock;
43 	bool valid;
44 	unsigned long last_updated; /* in jiffies */
45 
46 	const unsigned char *command;
47 	unsigned int nonblocking_wait_time; /* in us */
48 
49 	struct shtc1_platform_data setup;
50 
51 	int temperature; /* 1000 * temperature in dgr C */
52 	int humidity; /* 1000 * relative humidity in %RH */
53 };
54 
55 static int shtc1_update_values(struct i2c_client *client,
56 			       struct shtc1_data *data,
57 			       char *buf, int bufsize)
58 {
59 	int ret = i2c_master_send(client, data->command, SHTC1_CMD_LENGTH);
60 	if (ret != SHTC1_CMD_LENGTH) {
61 		dev_err(&client->dev, "failed to send command: %d\n", ret);
62 		return ret < 0 ? ret : -EIO;
63 	}
64 
65 	/*
66 	 * In blocking mode (clock stretching mode) the I2C bus
67 	 * is blocked for other traffic, thus the call to i2c_master_recv()
68 	 * will wait until the data is ready. For non blocking mode, we
69 	 * have to wait ourselves.
70 	 */
71 	if (!data->setup.blocking_io)
72 		usleep_range(data->nonblocking_wait_time,
73 			     data->nonblocking_wait_time + 1000);
74 
75 	ret = i2c_master_recv(client, buf, bufsize);
76 	if (ret != bufsize) {
77 		dev_err(&client->dev, "failed to read values: %d\n", ret);
78 		return ret < 0 ? ret : -EIO;
79 	}
80 
81 	return 0;
82 }
83 
84 /* sysfs attributes */
85 static struct shtc1_data *shtc1_update_client(struct device *dev)
86 {
87 	struct shtc1_data *data = dev_get_drvdata(dev);
88 	struct i2c_client *client = data->client;
89 	unsigned char buf[SHTC1_RESPONSE_LENGTH];
90 	int val;
91 	int ret = 0;
92 
93 	mutex_lock(&data->update_lock);
94 
95 	if (time_after(jiffies, data->last_updated + HZ / 10) || !data->valid) {
96 		ret = shtc1_update_values(client, data, buf, sizeof(buf));
97 		if (ret)
98 			goto out;
99 
100 		/*
101 		 * From datasheet:
102 		 * T = -45 + 175 * ST / 2^16
103 		 * RH = 100 * SRH / 2^16
104 		 *
105 		 * Adapted for integer fixed point (3 digit) arithmetic.
106 		 */
107 		val = be16_to_cpup((__be16 *)buf);
108 		data->temperature = ((21875 * val) >> 13) - 45000;
109 		val = be16_to_cpup((__be16 *)(buf + 3));
110 		data->humidity = ((12500 * val) >> 13);
111 
112 		data->last_updated = jiffies;
113 		data->valid = true;
114 	}
115 
116 out:
117 	mutex_unlock(&data->update_lock);
118 
119 	return ret == 0 ? data : ERR_PTR(ret);
120 }
121 
122 static ssize_t temp1_input_show(struct device *dev,
123 				struct device_attribute *attr,
124 				char *buf)
125 {
126 	struct shtc1_data *data = shtc1_update_client(dev);
127 	if (IS_ERR(data))
128 		return PTR_ERR(data);
129 
130 	return sprintf(buf, "%d\n", data->temperature);
131 }
132 
133 static ssize_t humidity1_input_show(struct device *dev,
134 				    struct device_attribute *attr, char *buf)
135 {
136 	struct shtc1_data *data = shtc1_update_client(dev);
137 	if (IS_ERR(data))
138 		return PTR_ERR(data);
139 
140 	return sprintf(buf, "%d\n", data->humidity);
141 }
142 
143 static DEVICE_ATTR_RO(temp1_input);
144 static DEVICE_ATTR_RO(humidity1_input);
145 
146 static struct attribute *shtc1_attrs[] = {
147 	&dev_attr_temp1_input.attr,
148 	&dev_attr_humidity1_input.attr,
149 	NULL
150 };
151 
152 ATTRIBUTE_GROUPS(shtc1);
153 
154 static void shtc1_select_command(struct shtc1_data *data)
155 {
156 	if (data->setup.high_precision) {
157 		data->command = data->setup.blocking_io ?
158 				shtc1_cmd_measure_blocking_hpm :
159 				shtc1_cmd_measure_nonblocking_hpm;
160 		data->nonblocking_wait_time = SHTC1_NONBLOCKING_WAIT_TIME_HPM;
161 
162 	} else {
163 		data->command = data->setup.blocking_io ?
164 				shtc1_cmd_measure_blocking_lpm :
165 				shtc1_cmd_measure_nonblocking_lpm;
166 		data->nonblocking_wait_time = SHTC1_NONBLOCKING_WAIT_TIME_LPM;
167 	}
168 }
169 
170 static int shtc1_probe(struct i2c_client *client,
171 		       const struct i2c_device_id *id)
172 {
173 	int ret;
174 	char id_reg[2];
175 	struct shtc1_data *data;
176 	struct device *hwmon_dev;
177 	struct i2c_adapter *adap = client->adapter;
178 	struct device *dev = &client->dev;
179 
180 	if (!i2c_check_functionality(adap, I2C_FUNC_I2C)) {
181 		dev_err(dev, "plain i2c transactions not supported\n");
182 		return -ENODEV;
183 	}
184 
185 	ret = i2c_master_send(client, shtc1_cmd_read_id_reg, SHTC1_CMD_LENGTH);
186 	if (ret != SHTC1_CMD_LENGTH) {
187 		dev_err(dev, "could not send read_id_reg command: %d\n", ret);
188 		return ret < 0 ? ret : -ENODEV;
189 	}
190 	ret = i2c_master_recv(client, id_reg, sizeof(id_reg));
191 	if (ret != sizeof(id_reg)) {
192 		dev_err(dev, "could not read ID register: %d\n", ret);
193 		return -ENODEV;
194 	}
195 	if ((id_reg[1] & SHTC1_ID_REG_MASK) != SHTC1_ID) {
196 		dev_err(dev, "ID register doesn't match\n");
197 		return -ENODEV;
198 	}
199 
200 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
201 	if (!data)
202 		return -ENOMEM;
203 
204 	data->setup.blocking_io = false;
205 	data->setup.high_precision = true;
206 	data->client = client;
207 
208 	if (client->dev.platform_data)
209 		data->setup = *(struct shtc1_platform_data *)dev->platform_data;
210 	shtc1_select_command(data);
211 	mutex_init(&data->update_lock);
212 
213 	hwmon_dev = devm_hwmon_device_register_with_groups(dev,
214 							   client->name,
215 							   data,
216 							   shtc1_groups);
217 	if (IS_ERR(hwmon_dev))
218 		dev_dbg(dev, "unable to register hwmon device\n");
219 
220 	return PTR_ERR_OR_ZERO(hwmon_dev);
221 }
222 
223 /* device ID table */
224 static const struct i2c_device_id shtc1_id[] = {
225 	{ "shtc1", 0 },
226 	{ "shtw1", 0 },
227 	{ }
228 };
229 MODULE_DEVICE_TABLE(i2c, shtc1_id);
230 
231 static struct i2c_driver shtc1_i2c_driver = {
232 	.driver.name  = "shtc1",
233 	.probe        = shtc1_probe,
234 	.id_table     = shtc1_id,
235 };
236 
237 module_i2c_driver(shtc1_i2c_driver);
238 
239 MODULE_AUTHOR("Johannes Winkelmann <johannes.winkelmann@sensirion.com>");
240 MODULE_DESCRIPTION("Sensirion SHTC1 humidity and temperature sensor driver");
241 MODULE_LICENSE("GPL");
242