Linux Audio

Check our new training course

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