Blame view

drivers/hwmon/sht21.c 8.14 KB
430400b86   Urs Fleisch   hwmon: driver for...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  /* Sensirion SHT21 humidity and temperature sensor driver
   *
   * Copyright (C) 2010 Urs Fleisch <urs.fleisch@sensirion.com>
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation; either version 2 of the License, or
   * (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, write to the Free Software
   * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA
   *
   * Data sheet available (5/2010) at
   * http://www.sensirion.com/en/pdf/product_information/Datasheet-humidity-sensor-SHT21.pdf
   */
  
  #include <linux/module.h>
  #include <linux/init.h>
  #include <linux/slab.h>
  #include <linux/i2c.h>
  #include <linux/hwmon.h>
  #include <linux/hwmon-sysfs.h>
  #include <linux/err.h>
  #include <linux/mutex.h>
  #include <linux/device.h>
dcd8f3923   Jean Delvare   hwmon: Add missin...
32
  #include <linux/jiffies.h>
430400b86   Urs Fleisch   hwmon: driver for...
33
34
35
36
  
  /* I2C command bytes */
  #define SHT21_TRIG_T_MEASUREMENT_HM  0xe3
  #define SHT21_TRIG_RH_MEASUREMENT_HM 0xe5
53e678d75   Peter A. Bigot   hwmon: (sht21) Ad...
37
38
39
40
  #define SHT21_READ_SNB_CMD1 0xFA
  #define SHT21_READ_SNB_CMD2 0x0F
  #define SHT21_READ_SNAC_CMD1 0xFC
  #define SHT21_READ_SNAC_CMD2 0xC9
430400b86   Urs Fleisch   hwmon: driver for...
41
42
43
44
45
  
  /**
   * struct sht21 - SHT21 device specific data
   * @hwmon_dev: device registered with hwmon
   * @lock: mutex to protect measurement values
430400b86   Urs Fleisch   hwmon: driver for...
46
47
48
   * @last_update: time of last update (jiffies)
   * @temperature: cached temperature measurement value
   * @humidity: cached humidity measurement value
53e678d75   Peter A. Bigot   hwmon: (sht21) Ad...
49
50
   * @valid: only 0 before first measurement is taken
   * @eic: cached electronic identification code text
430400b86   Urs Fleisch   hwmon: driver for...
51
52
   */
  struct sht21 {
b8d568983   Axel Lin   hwmon: (sht21) Co...
53
  	struct i2c_client *client;
430400b86   Urs Fleisch   hwmon: driver for...
54
  	struct mutex lock;
430400b86   Urs Fleisch   hwmon: driver for...
55
56
57
  	unsigned long last_update;
  	int temperature;
  	int humidity;
53e678d75   Peter A. Bigot   hwmon: (sht21) Ad...
58
59
  	char valid;
  	char eic[18];
430400b86   Urs Fleisch   hwmon: driver for...
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
  };
  
  /**
   * sht21_temp_ticks_to_millicelsius() - convert raw temperature ticks to
   * milli celsius
   * @ticks: temperature ticks value received from sensor
   */
  static inline int sht21_temp_ticks_to_millicelsius(int ticks)
  {
  	ticks &= ~0x0003; /* clear status bits */
  	/*
  	 * Formula T = -46.85 + 175.72 * ST / 2^16 from data sheet 6.2,
  	 * optimized for integer fixed point (3 digits) arithmetic
  	 */
  	return ((21965 * ticks) >> 13) - 46850;
  }
  
  /**
   * sht21_rh_ticks_to_per_cent_mille() - convert raw humidity ticks to
   * one-thousandths of a percent relative humidity
   * @ticks: humidity ticks value received from sensor
   */
  static inline int sht21_rh_ticks_to_per_cent_mille(int ticks)
  {
  	ticks &= ~0x0003; /* clear status bits */
  	/*
  	 * Formula RH = -6 + 125 * SRH / 2^16 from data sheet 6.1,
  	 * optimized for integer fixed point (3 digits) arithmetic
  	 */
  	return ((15625 * ticks) >> 13) - 6000;
  }
  
  /**
430400b86   Urs Fleisch   hwmon: driver for...
93
   * sht21_update_measurements() - get updated measurements from device
b8d568983   Axel Lin   hwmon: (sht21) Co...
94
   * @dev: device
430400b86   Urs Fleisch   hwmon: driver for...
95
96
97
   *
   * Returns 0 on success, else negative errno.
   */
b8d568983   Axel Lin   hwmon: (sht21) Co...
98
  static int sht21_update_measurements(struct device *dev)
430400b86   Urs Fleisch   hwmon: driver for...
99
100
  {
  	int ret = 0;
b8d568983   Axel Lin   hwmon: (sht21) Co...
101
102
  	struct sht21 *sht21 = dev_get_drvdata(dev);
  	struct i2c_client *client = sht21->client;
430400b86   Urs Fleisch   hwmon: driver for...
103
104
105
106
107
108
109
110
  
  	mutex_lock(&sht21->lock);
  	/*
  	 * Data sheet 2.4:
  	 * SHT2x should not be active for more than 10% of the time - e.g.
  	 * maximum two measurements per second at 12bit accuracy shall be made.
  	 */
  	if (time_after(jiffies, sht21->last_update + HZ / 2) || !sht21->valid) {
90f4102ce   Jean Delvare   hwmon: Use i2c_sm...
111
112
  		ret = i2c_smbus_read_word_swapped(client,
  						  SHT21_TRIG_T_MEASUREMENT_HM);
430400b86   Urs Fleisch   hwmon: driver for...
113
114
115
  		if (ret < 0)
  			goto out;
  		sht21->temperature = sht21_temp_ticks_to_millicelsius(ret);
90f4102ce   Jean Delvare   hwmon: Use i2c_sm...
116
117
  		ret = i2c_smbus_read_word_swapped(client,
  						  SHT21_TRIG_RH_MEASUREMENT_HM);
430400b86   Urs Fleisch   hwmon: driver for...
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
  		if (ret < 0)
  			goto out;
  		sht21->humidity = sht21_rh_ticks_to_per_cent_mille(ret);
  		sht21->last_update = jiffies;
  		sht21->valid = 1;
  	}
  out:
  	mutex_unlock(&sht21->lock);
  
  	return ret >= 0 ? 0 : ret;
  }
  
  /**
   * sht21_show_temperature() - show temperature measurement value in sysfs
   * @dev: device
   * @attr: device attribute
   * @buf: sysfs buffer (PAGE_SIZE) where measurement values are written to
   *
   * Will be called on read access to temp1_input sysfs attribute.
   * Returns number of bytes written into buffer, negative errno on error.
   */
  static ssize_t sht21_show_temperature(struct device *dev,
  	struct device_attribute *attr,
  	char *buf)
  {
b8d568983   Axel Lin   hwmon: (sht21) Co...
143
144
145
146
  	struct sht21 *sht21 = dev_get_drvdata(dev);
  	int ret;
  
  	ret = sht21_update_measurements(dev);
430400b86   Urs Fleisch   hwmon: driver for...
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
  	if (ret < 0)
  		return ret;
  	return sprintf(buf, "%d
  ", sht21->temperature);
  }
  
  /**
   * sht21_show_humidity() - show humidity measurement value in sysfs
   * @dev: device
   * @attr: device attribute
   * @buf: sysfs buffer (PAGE_SIZE) where measurement values are written to
   *
   * Will be called on read access to humidity1_input sysfs attribute.
   * Returns number of bytes written into buffer, negative errno on error.
   */
  static ssize_t sht21_show_humidity(struct device *dev,
  	struct device_attribute *attr,
  	char *buf)
  {
b8d568983   Axel Lin   hwmon: (sht21) Co...
166
167
168
169
  	struct sht21 *sht21 = dev_get_drvdata(dev);
  	int ret;
  
  	ret = sht21_update_measurements(dev);
430400b86   Urs Fleisch   hwmon: driver for...
170
171
172
173
174
  	if (ret < 0)
  		return ret;
  	return sprintf(buf, "%d
  ", sht21->humidity);
  }
53e678d75   Peter A. Bigot   hwmon: (sht21) Ad...
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
  static ssize_t eic_read(struct sht21 *sht21)
  {
  	struct i2c_client *client = sht21->client;
  	u8 tx[2];
  	u8 rx[8];
  	u8 eic[8];
  	struct i2c_msg msgs[2] = {
  		{
  			.addr = client->addr,
  			.flags = 0,
  			.len = 2,
  			.buf = tx,
  		},
  		{
  			.addr = client->addr,
  			.flags = I2C_M_RD,
  			.len = 8,
  			.buf = rx,
  		},
  	};
  	int ret;
  
  	tx[0] = SHT21_READ_SNB_CMD1;
  	tx[1] = SHT21_READ_SNB_CMD2;
  	ret = i2c_transfer(client->adapter, msgs, 2);
  	if (ret < 0)
  		goto out;
  	eic[2] = rx[0];
  	eic[3] = rx[2];
  	eic[4] = rx[4];
  	eic[5] = rx[6];
  
  	tx[0] = SHT21_READ_SNAC_CMD1;
  	tx[1] = SHT21_READ_SNAC_CMD2;
  	msgs[1].len = 6;
  	ret = i2c_transfer(client->adapter, msgs, 2);
  	if (ret < 0)
  		goto out;
  	eic[0] = rx[3];
  	eic[1] = rx[4];
  	eic[6] = rx[0];
  	eic[7] = rx[1];
  
  	ret = snprintf(sht21->eic, sizeof(sht21->eic),
  		       "%02x%02x%02x%02x%02x%02x%02x%02x
  ",
  		       eic[0], eic[1], eic[2], eic[3],
  		       eic[4], eic[5], eic[6], eic[7]);
  out:
  	if (ret < 0)
  		sht21->eic[0] = 0;
  
  	return ret;
  }
  
  /**
   * eic_show() - show Electronic Identification Code in sysfs
   * @dev: device
   * @attr: device attribute
   * @buf: sysfs buffer (PAGE_SIZE) where EIC is written
   *
   * Will be called on read access to eic sysfs attribute.
   * Returns number of bytes written into buffer, negative errno on error.
   */
  static ssize_t eic_show(struct device *dev,
  	struct device_attribute *attr,
  	char *buf)
  {
  	struct sht21 *sht21 = dev_get_drvdata(dev);
  	int ret;
  
  	ret = sizeof(sht21->eic) - 1;
  	mutex_lock(&sht21->lock);
  	if (!sht21->eic[0])
  		ret = eic_read(sht21);
  	if (ret > 0)
  		memcpy(buf, sht21->eic, ret);
  	mutex_unlock(&sht21->lock);
  	return ret;
  }
430400b86   Urs Fleisch   hwmon: driver for...
255
256
257
258
259
  /* sysfs attributes */
  static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, sht21_show_temperature,
  	NULL, 0);
  static SENSOR_DEVICE_ATTR(humidity1_input, S_IRUGO, sht21_show_humidity,
  	NULL, 0);
53e678d75   Peter A. Bigot   hwmon: (sht21) Ad...
260
  static DEVICE_ATTR_RO(eic);
430400b86   Urs Fleisch   hwmon: driver for...
261

b8d568983   Axel Lin   hwmon: (sht21) Co...
262
  static struct attribute *sht21_attrs[] = {
430400b86   Urs Fleisch   hwmon: driver for...
263
264
  	&sensor_dev_attr_temp1_input.dev_attr.attr,
  	&sensor_dev_attr_humidity1_input.dev_attr.attr,
53e678d75   Peter A. Bigot   hwmon: (sht21) Ad...
265
  	&dev_attr_eic.attr,
430400b86   Urs Fleisch   hwmon: driver for...
266
267
  	NULL
  };
b8d568983   Axel Lin   hwmon: (sht21) Co...
268
  ATTRIBUTE_GROUPS(sht21);
430400b86   Urs Fleisch   hwmon: driver for...
269

6c931ae1c   Bill Pemberton   hwmon: remove use...
270
  static int sht21_probe(struct i2c_client *client,
430400b86   Urs Fleisch   hwmon: driver for...
271
272
  	const struct i2c_device_id *id)
  {
b8d568983   Axel Lin   hwmon: (sht21) Co...
273
274
  	struct device *dev = &client->dev;
  	struct device *hwmon_dev;
430400b86   Urs Fleisch   hwmon: driver for...
275
  	struct sht21 *sht21;
430400b86   Urs Fleisch   hwmon: driver for...
276
277
278
279
280
281
282
283
  
  	if (!i2c_check_functionality(client->adapter,
  				     I2C_FUNC_SMBUS_WORD_DATA)) {
  		dev_err(&client->dev,
  			"adapter does not support SMBus word transactions
  ");
  		return -ENODEV;
  	}
b8d568983   Axel Lin   hwmon: (sht21) Co...
284
  	sht21 = devm_kzalloc(dev, sizeof(*sht21), GFP_KERNEL);
a844af19b   Guenter Roeck   hwmon: (sht21) Co...
285
  	if (!sht21)
430400b86   Urs Fleisch   hwmon: driver for...
286
  		return -ENOMEM;
a844af19b   Guenter Roeck   hwmon: (sht21) Co...
287

b8d568983   Axel Lin   hwmon: (sht21) Co...
288
  	sht21->client = client;
430400b86   Urs Fleisch   hwmon: driver for...
289
290
  
  	mutex_init(&sht21->lock);
b8d568983   Axel Lin   hwmon: (sht21) Co...
291
292
293
  	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
  							   sht21, sht21_groups);
  	return PTR_ERR_OR_ZERO(hwmon_dev);
430400b86   Urs Fleisch   hwmon: driver for...
294
295
296
297
298
299
300
301
302
303
304
305
  }
  
  /* Device ID table */
  static const struct i2c_device_id sht21_id[] = {
  	{ "sht21", 0 },
  	{ }
  };
  MODULE_DEVICE_TABLE(i2c, sht21_id);
  
  static struct i2c_driver sht21_driver = {
  	.driver.name = "sht21",
  	.probe       = sht21_probe,
430400b86   Urs Fleisch   hwmon: driver for...
306
307
  	.id_table    = sht21_id,
  };
f0967eea8   Axel Lin   hwmon: convert dr...
308
  module_i2c_driver(sht21_driver);
430400b86   Urs Fleisch   hwmon: driver for...
309
310
311
312
  
  MODULE_AUTHOR("Urs Fleisch <urs.fleisch@sensirion.com>");
  MODULE_DESCRIPTION("Sensirion SHT21 humidity and temperature sensor driver");
  MODULE_LICENSE("GPL");