Linux Audio

Check our new training course

Loading...
v5.9
  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) */
 19static const unsigned char shtc1_cmd_measure_blocking_hpm[]    = { 0x7C, 0xA2 };
 20static const unsigned char shtc1_cmd_measure_nonblocking_hpm[] = { 0x78, 0x66 };
 21
 22/* commands (low precision mode) */
 23static const unsigned char shtc1_cmd_measure_blocking_lpm[]    = { 0x64, 0x58 };
 24static const unsigned char shtc1_cmd_measure_nonblocking_lpm[] = { 0x60, 0x9c };
 25
 26/* command for reading the ID register */
 27static const unsigned char shtc1_cmd_read_id_reg[]             = { 0xef, 0xc8 };
 28
 29/*
 30 * constants for reading the ID register
 31 * SHTC1: 0x0007 with mask 0x003f
 32 * SHTW1: 0x0007 with mask 0x003f
 33 * SHTC3: 0x0807 with mask 0x083f
 34 */
 35#define SHTC3_ID      0x0807
 36#define SHTC3_ID_MASK 0x083f
 37#define SHTC1_ID      0x0007
 38#define SHTC1_ID_MASK 0x003f
 39
 40/* delays for non-blocking i2c commands, both in us */
 41#define SHTC1_NONBLOCKING_WAIT_TIME_HPM  14400
 42#define SHTC1_NONBLOCKING_WAIT_TIME_LPM   1000
 43#define SHTC3_NONBLOCKING_WAIT_TIME_HPM  12100
 44#define SHTC3_NONBLOCKING_WAIT_TIME_LPM    800
 45
 46#define SHTC1_CMD_LENGTH      2
 47#define SHTC1_RESPONSE_LENGTH 6
 48
 49enum shtcx_chips {
 50	shtc1,
 51	shtc3,
 52};
 53
 54struct shtc1_data {
 55	struct i2c_client *client;
 56	struct mutex update_lock;
 57	bool valid;
 58	unsigned long last_updated; /* in jiffies */
 59
 60	const unsigned char *command;
 61	unsigned int nonblocking_wait_time; /* in us */
 62
 63	struct shtc1_platform_data setup;
 64	enum shtcx_chips chip;
 65
 66	int temperature; /* 1000 * temperature in dgr C */
 67	int humidity; /* 1000 * relative humidity in %RH */
 68};
 69
 70static int shtc1_update_values(struct i2c_client *client,
 71			       struct shtc1_data *data,
 72			       char *buf, int bufsize)
 73{
 74	int ret = i2c_master_send(client, data->command, SHTC1_CMD_LENGTH);
 75	if (ret != SHTC1_CMD_LENGTH) {
 76		dev_err(&client->dev, "failed to send command: %d\n", ret);
 77		return ret < 0 ? ret : -EIO;
 78	}
 79
 80	/*
 81	 * In blocking mode (clock stretching mode) the I2C bus
 82	 * is blocked for other traffic, thus the call to i2c_master_recv()
 83	 * will wait until the data is ready. For non blocking mode, we
 84	 * have to wait ourselves.
 85	 */
 86	if (!data->setup.blocking_io)
 87		usleep_range(data->nonblocking_wait_time,
 88			     data->nonblocking_wait_time + 1000);
 89
 90	ret = i2c_master_recv(client, buf, bufsize);
 91	if (ret != bufsize) {
 92		dev_err(&client->dev, "failed to read values: %d\n", ret);
 93		return ret < 0 ? ret : -EIO;
 94	}
 95
 96	return 0;
 97}
 98
 99/* sysfs attributes */
100static struct shtc1_data *shtc1_update_client(struct device *dev)
101{
102	struct shtc1_data *data = dev_get_drvdata(dev);
103	struct i2c_client *client = data->client;
104	unsigned char buf[SHTC1_RESPONSE_LENGTH];
105	int val;
106	int ret = 0;
107
108	mutex_lock(&data->update_lock);
109
110	if (time_after(jiffies, data->last_updated + HZ / 10) || !data->valid) {
111		ret = shtc1_update_values(client, data, buf, sizeof(buf));
112		if (ret)
113			goto out;
114
115		/*
116		 * From datasheet:
117		 * T = -45 + 175 * ST / 2^16
118		 * RH = 100 * SRH / 2^16
119		 *
120		 * Adapted for integer fixed point (3 digit) arithmetic.
121		 */
122		val = be16_to_cpup((__be16 *)buf);
123		data->temperature = ((21875 * val) >> 13) - 45000;
124		val = be16_to_cpup((__be16 *)(buf + 3));
125		data->humidity = ((12500 * val) >> 13);
126
127		data->last_updated = jiffies;
128		data->valid = true;
129	}
130
131out:
132	mutex_unlock(&data->update_lock);
133
134	return ret == 0 ? data : ERR_PTR(ret);
135}
136
137static ssize_t temp1_input_show(struct device *dev,
138				struct device_attribute *attr,
139				char *buf)
140{
141	struct shtc1_data *data = shtc1_update_client(dev);
142	if (IS_ERR(data))
143		return PTR_ERR(data);
144
145	return sprintf(buf, "%d\n", data->temperature);
146}
147
148static ssize_t humidity1_input_show(struct device *dev,
149				    struct device_attribute *attr, char *buf)
150{
151	struct shtc1_data *data = shtc1_update_client(dev);
152	if (IS_ERR(data))
153		return PTR_ERR(data);
154
155	return sprintf(buf, "%d\n", data->humidity);
156}
157
158static DEVICE_ATTR_RO(temp1_input);
159static DEVICE_ATTR_RO(humidity1_input);
160
161static struct attribute *shtc1_attrs[] = {
162	&dev_attr_temp1_input.attr,
163	&dev_attr_humidity1_input.attr,
164	NULL
165};
166
167ATTRIBUTE_GROUPS(shtc1);
168
169static void shtc1_select_command(struct shtc1_data *data)
170{
171	if (data->setup.high_precision) {
172		data->command = data->setup.blocking_io ?
173				shtc1_cmd_measure_blocking_hpm :
174				shtc1_cmd_measure_nonblocking_hpm;
175		data->nonblocking_wait_time = (data->chip == shtc1) ?
176				SHTC1_NONBLOCKING_WAIT_TIME_HPM :
177				SHTC3_NONBLOCKING_WAIT_TIME_HPM;
178	} else {
179		data->command = data->setup.blocking_io ?
180				shtc1_cmd_measure_blocking_lpm :
181				shtc1_cmd_measure_nonblocking_lpm;
182		data->nonblocking_wait_time = (data->chip == shtc1) ?
183				SHTC1_NONBLOCKING_WAIT_TIME_LPM :
184				SHTC3_NONBLOCKING_WAIT_TIME_LPM;
185	}
186}
187
188static int shtc1_probe(struct i2c_client *client,
189		       const struct i2c_device_id *id)
 
190{
191	int ret;
192	u16 id_reg;
193	char id_reg_buf[2];
194	struct shtc1_data *data;
195	struct device *hwmon_dev;
196	enum shtcx_chips chip = id->driver_data;
197	struct i2c_adapter *adap = client->adapter;
198	struct device *dev = &client->dev;
 
199
200	if (!i2c_check_functionality(adap, I2C_FUNC_I2C)) {
201		dev_err(dev, "plain i2c transactions not supported\n");
202		return -ENODEV;
203	}
204
205	ret = i2c_master_send(client, shtc1_cmd_read_id_reg, SHTC1_CMD_LENGTH);
206	if (ret != SHTC1_CMD_LENGTH) {
207		dev_err(dev, "could not send read_id_reg command: %d\n", ret);
208		return ret < 0 ? ret : -ENODEV;
209	}
210	ret = i2c_master_recv(client, id_reg_buf, sizeof(id_reg_buf));
211	if (ret != sizeof(id_reg_buf)) {
212		dev_err(dev, "could not read ID register: %d\n", ret);
213		return -ENODEV;
214	}
215
216	id_reg = be16_to_cpup((__be16 *)id_reg_buf);
217	if (chip == shtc3) {
218		if ((id_reg & SHTC3_ID_MASK) != SHTC3_ID) {
219			dev_err(dev, "SHTC3 ID register does not match\n");
220			return -ENODEV;
221		}
222	} else if ((id_reg & SHTC1_ID_MASK) != SHTC1_ID) {
223		dev_err(dev, "SHTC1 ID register does not match\n");
224		return -ENODEV;
225	}
226
227	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
228	if (!data)
229		return -ENOMEM;
230
231	data->setup.blocking_io = false;
232	data->setup.high_precision = true;
233	data->client = client;
234	data->chip = chip;
235
236	if (client->dev.platform_data)
237		data->setup = *(struct shtc1_platform_data *)dev->platform_data;
 
 
 
 
 
 
238	shtc1_select_command(data);
239	mutex_init(&data->update_lock);
240
241	hwmon_dev = devm_hwmon_device_register_with_groups(dev,
242							   client->name,
243							   data,
244							   shtc1_groups);
245	if (IS_ERR(hwmon_dev))
246		dev_dbg(dev, "unable to register hwmon device\n");
247
248	return PTR_ERR_OR_ZERO(hwmon_dev);
249}
250
251/* device ID table */
252static const struct i2c_device_id shtc1_id[] = {
253	{ "shtc1", shtc1 },
254	{ "shtw1", shtc1 },
255	{ "shtc3", shtc3 },
256	{ }
257};
258MODULE_DEVICE_TABLE(i2c, shtc1_id);
259
 
 
 
 
 
 
 
 
260static struct i2c_driver shtc1_i2c_driver = {
261	.driver.name  = "shtc1",
262	.probe        = shtc1_probe,
 
 
 
263	.id_table     = shtc1_id,
264};
265
266module_i2c_driver(shtc1_i2c_driver);
267
268MODULE_AUTHOR("Johannes Winkelmann <johannes.winkelmann@sensirion.com>");
269MODULE_DESCRIPTION("Sensirion SHTC1 humidity and temperature sensor driver");
270MODULE_LICENSE("GPL");
v6.2
  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#include <linux/of.h>
 18
 19/* commands (high precision mode) */
 20static const unsigned char shtc1_cmd_measure_blocking_hpm[]    = { 0x7C, 0xA2 };
 21static const unsigned char shtc1_cmd_measure_nonblocking_hpm[] = { 0x78, 0x66 };
 22
 23/* commands (low precision mode) */
 24static const unsigned char shtc1_cmd_measure_blocking_lpm[]    = { 0x64, 0x58 };
 25static const unsigned char shtc1_cmd_measure_nonblocking_lpm[] = { 0x60, 0x9c };
 26
 27/* command for reading the ID register */
 28static const unsigned char shtc1_cmd_read_id_reg[]             = { 0xef, 0xc8 };
 29
 30/*
 31 * constants for reading the ID register
 32 * SHTC1: 0x0007 with mask 0x003f
 33 * SHTW1: 0x0007 with mask 0x003f
 34 * SHTC3: 0x0807 with mask 0x083f
 35 */
 36#define SHTC3_ID      0x0807
 37#define SHTC3_ID_MASK 0x083f
 38#define SHTC1_ID      0x0007
 39#define SHTC1_ID_MASK 0x003f
 40
 41/* delays for non-blocking i2c commands, both in us */
 42#define SHTC1_NONBLOCKING_WAIT_TIME_HPM  14400
 43#define SHTC1_NONBLOCKING_WAIT_TIME_LPM   1000
 44#define SHTC3_NONBLOCKING_WAIT_TIME_HPM  12100
 45#define SHTC3_NONBLOCKING_WAIT_TIME_LPM    800
 46
 47#define SHTC1_CMD_LENGTH      2
 48#define SHTC1_RESPONSE_LENGTH 6
 49
 50enum shtcx_chips {
 51	shtc1,
 52	shtc3,
 53};
 54
 55struct shtc1_data {
 56	struct i2c_client *client;
 57	struct mutex update_lock;
 58	bool valid;
 59	unsigned long last_updated; /* in jiffies */
 60
 61	const unsigned char *command;
 62	unsigned int nonblocking_wait_time; /* in us */
 63
 64	struct shtc1_platform_data setup;
 65	enum shtcx_chips chip;
 66
 67	int temperature; /* 1000 * temperature in dgr C */
 68	int humidity; /* 1000 * relative humidity in %RH */
 69};
 70
 71static int shtc1_update_values(struct i2c_client *client,
 72			       struct shtc1_data *data,
 73			       char *buf, int bufsize)
 74{
 75	int ret = i2c_master_send(client, data->command, SHTC1_CMD_LENGTH);
 76	if (ret != SHTC1_CMD_LENGTH) {
 77		dev_err(&client->dev, "failed to send command: %d\n", ret);
 78		return ret < 0 ? ret : -EIO;
 79	}
 80
 81	/*
 82	 * In blocking mode (clock stretching mode) the I2C bus
 83	 * is blocked for other traffic, thus the call to i2c_master_recv()
 84	 * will wait until the data is ready. For non blocking mode, we
 85	 * have to wait ourselves.
 86	 */
 87	if (!data->setup.blocking_io)
 88		usleep_range(data->nonblocking_wait_time,
 89			     data->nonblocking_wait_time + 1000);
 90
 91	ret = i2c_master_recv(client, buf, bufsize);
 92	if (ret != bufsize) {
 93		dev_err(&client->dev, "failed to read values: %d\n", ret);
 94		return ret < 0 ? ret : -EIO;
 95	}
 96
 97	return 0;
 98}
 99
100/* sysfs attributes */
101static struct shtc1_data *shtc1_update_client(struct device *dev)
102{
103	struct shtc1_data *data = dev_get_drvdata(dev);
104	struct i2c_client *client = data->client;
105	unsigned char buf[SHTC1_RESPONSE_LENGTH];
106	int val;
107	int ret = 0;
108
109	mutex_lock(&data->update_lock);
110
111	if (time_after(jiffies, data->last_updated + HZ / 10) || !data->valid) {
112		ret = shtc1_update_values(client, data, buf, sizeof(buf));
113		if (ret)
114			goto out;
115
116		/*
117		 * From datasheet:
118		 * T = -45 + 175 * ST / 2^16
119		 * RH = 100 * SRH / 2^16
120		 *
121		 * Adapted for integer fixed point (3 digit) arithmetic.
122		 */
123		val = be16_to_cpup((__be16 *)buf);
124		data->temperature = ((21875 * val) >> 13) - 45000;
125		val = be16_to_cpup((__be16 *)(buf + 3));
126		data->humidity = ((12500 * val) >> 13);
127
128		data->last_updated = jiffies;
129		data->valid = true;
130	}
131
132out:
133	mutex_unlock(&data->update_lock);
134
135	return ret == 0 ? data : ERR_PTR(ret);
136}
137
138static ssize_t temp1_input_show(struct device *dev,
139				struct device_attribute *attr,
140				char *buf)
141{
142	struct shtc1_data *data = shtc1_update_client(dev);
143	if (IS_ERR(data))
144		return PTR_ERR(data);
145
146	return sprintf(buf, "%d\n", data->temperature);
147}
148
149static ssize_t humidity1_input_show(struct device *dev,
150				    struct device_attribute *attr, char *buf)
151{
152	struct shtc1_data *data = shtc1_update_client(dev);
153	if (IS_ERR(data))
154		return PTR_ERR(data);
155
156	return sprintf(buf, "%d\n", data->humidity);
157}
158
159static DEVICE_ATTR_RO(temp1_input);
160static DEVICE_ATTR_RO(humidity1_input);
161
162static struct attribute *shtc1_attrs[] = {
163	&dev_attr_temp1_input.attr,
164	&dev_attr_humidity1_input.attr,
165	NULL
166};
167
168ATTRIBUTE_GROUPS(shtc1);
169
170static void shtc1_select_command(struct shtc1_data *data)
171{
172	if (data->setup.high_precision) {
173		data->command = data->setup.blocking_io ?
174				shtc1_cmd_measure_blocking_hpm :
175				shtc1_cmd_measure_nonblocking_hpm;
176		data->nonblocking_wait_time = (data->chip == shtc1) ?
177				SHTC1_NONBLOCKING_WAIT_TIME_HPM :
178				SHTC3_NONBLOCKING_WAIT_TIME_HPM;
179	} else {
180		data->command = data->setup.blocking_io ?
181				shtc1_cmd_measure_blocking_lpm :
182				shtc1_cmd_measure_nonblocking_lpm;
183		data->nonblocking_wait_time = (data->chip == shtc1) ?
184				SHTC1_NONBLOCKING_WAIT_TIME_LPM :
185				SHTC3_NONBLOCKING_WAIT_TIME_LPM;
186	}
187}
188
189static const struct i2c_device_id shtc1_id[];
190
191static int shtc1_probe(struct i2c_client *client)
192{
193	int ret;
194	u16 id_reg;
195	char id_reg_buf[2];
196	struct shtc1_data *data;
197	struct device *hwmon_dev;
198	enum shtcx_chips chip = i2c_match_id(shtc1_id, client)->driver_data;
199	struct i2c_adapter *adap = client->adapter;
200	struct device *dev = &client->dev;
201	struct device_node *np = dev->of_node;
202
203	if (!i2c_check_functionality(adap, I2C_FUNC_I2C)) {
204		dev_err(dev, "plain i2c transactions not supported\n");
205		return -ENODEV;
206	}
207
208	ret = i2c_master_send(client, shtc1_cmd_read_id_reg, SHTC1_CMD_LENGTH);
209	if (ret != SHTC1_CMD_LENGTH) {
210		dev_err(dev, "could not send read_id_reg command: %d\n", ret);
211		return ret < 0 ? ret : -ENODEV;
212	}
213	ret = i2c_master_recv(client, id_reg_buf, sizeof(id_reg_buf));
214	if (ret != sizeof(id_reg_buf)) {
215		dev_err(dev, "could not read ID register: %d\n", ret);
216		return -ENODEV;
217	}
218
219	id_reg = be16_to_cpup((__be16 *)id_reg_buf);
220	if (chip == shtc3) {
221		if ((id_reg & SHTC3_ID_MASK) != SHTC3_ID) {
222			dev_err(dev, "SHTC3 ID register does not match\n");
223			return -ENODEV;
224		}
225	} else if ((id_reg & SHTC1_ID_MASK) != SHTC1_ID) {
226		dev_err(dev, "SHTC1 ID register does not match\n");
227		return -ENODEV;
228	}
229
230	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
231	if (!data)
232		return -ENOMEM;
233
234	data->setup.blocking_io = false;
235	data->setup.high_precision = true;
236	data->client = client;
237	data->chip = chip;
238
239	if (np) {
240		data->setup.blocking_io = of_property_read_bool(np, "sensirion,blocking-io");
241		data->setup.high_precision = !of_property_read_bool(np, "sensicon,low-precision");
242	} else {
243		if (client->dev.platform_data)
244			data->setup = *(struct shtc1_platform_data *)dev->platform_data;
245	}
246
247	shtc1_select_command(data);
248	mutex_init(&data->update_lock);
249
250	hwmon_dev = devm_hwmon_device_register_with_groups(dev,
251							   client->name,
252							   data,
253							   shtc1_groups);
254	if (IS_ERR(hwmon_dev))
255		dev_dbg(dev, "unable to register hwmon device\n");
256
257	return PTR_ERR_OR_ZERO(hwmon_dev);
258}
259
260/* device ID table */
261static const struct i2c_device_id shtc1_id[] = {
262	{ "shtc1", shtc1 },
263	{ "shtw1", shtc1 },
264	{ "shtc3", shtc3 },
265	{ }
266};
267MODULE_DEVICE_TABLE(i2c, shtc1_id);
268
269static const struct of_device_id shtc1_of_match[] = {
270	{ .compatible = "sensirion,shtc1" },
271	{ .compatible = "sensirion,shtw1" },
272	{ .compatible = "sensirion,shtc3" },
273	{ }
274};
275MODULE_DEVICE_TABLE(of, shtc1_of_match);
276
277static struct i2c_driver shtc1_i2c_driver = {
278	.driver = {
279		.name = "shtc1",
280		.of_match_table = shtc1_of_match,
281	},
282	.probe_new    = shtc1_probe,
283	.id_table     = shtc1_id,
284};
285
286module_i2c_driver(shtc1_i2c_driver);
287
288MODULE_AUTHOR("Johannes Winkelmann <johannes.winkelmann@sensirion.com>");
289MODULE_DESCRIPTION("Sensirion SHTC1 humidity and temperature sensor driver");
290MODULE_LICENSE("GPL");