Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0-only
  2
  3/*
  4 * aht10.c - Linux hwmon driver for AHT10/AHT20 Temperature and Humidity sensors
  5 * Copyright (C) 2020 Johannes Cornelis Draaijer
  6 */
  7
  8#include <linux/delay.h>
  9#include <linux/hwmon.h>
 10#include <linux/i2c.h>
 11#include <linux/ktime.h>
 12#include <linux/module.h>
 13#include <linux/crc8.h>
 14
 15#define AHT10_MEAS_SIZE		6
 16
 17#define AHT20_MEAS_SIZE		7
 18#define AHT20_CRC8_POLY		0x31
 19
 20/*
 21 * Poll intervals (in milliseconds)
 22 */
 23#define AHT10_DEFAULT_MIN_POLL_INTERVAL	2000
 24#define AHT10_MIN_POLL_INTERVAL		2000
 25
 26/*
 27 * I2C command delays (in microseconds)
 28 */
 29#define AHT10_MEAS_DELAY	80000
 30#define AHT10_CMD_DELAY		350000
 31#define AHT10_DELAY_EXTRA	100000
 32
 33/*
 34 * Command bytes
 35 */
 36#define AHT10_CMD_INIT	0b11100001
 37#define AHT10_CMD_MEAS	0b10101100
 38#define AHT10_CMD_RST	0b10111010
 39
 40/*
 41 * Flags in the answer byte/command
 42 */
 43#define AHT10_CAL_ENABLED	BIT(3)
 44#define AHT10_BUSY		BIT(7)
 45#define AHT10_MODE_NOR		(BIT(5) | BIT(6))
 46#define AHT10_MODE_CYC		BIT(5)
 47#define AHT10_MODE_CMD		BIT(6)
 48
 49#define AHT10_MAX_POLL_INTERVAL_LEN	30
 50
 51enum aht10_variant { aht10, aht20 };
 52
 53static const struct i2c_device_id aht10_id[] = {
 54	{ "aht10", aht10 },
 55	{ "aht20", aht20 },
 56	{ },
 57};
 58MODULE_DEVICE_TABLE(i2c, aht10_id);
 59
 60/**
 61 *   struct aht10_data - All the data required to operate an AHT10/AHT20 chip
 62 *   @client: the i2c client associated with the AHT10/AHT20
 63 *   @lock: a mutex that is used to prevent parallel access to the
 64 *          i2c client
 65 *   @min_poll_interval: the minimum poll interval
 66 *                   While the poll rate limit is not 100% necessary,
 67 *                   the datasheet recommends that a measurement
 68 *                   is not performed too often to prevent
 69 *                   the chip from warming up due to the heat it generates.
 70 *                   If it's unwanted, it can be ignored setting it to
 71 *                   it to 0. Default value is 2000 ms
 72 *   @previous_poll_time: the previous time that the AHT10/AHT20
 73 *                        was polled
 74 *   @temperature: the latest temperature value received from
 75 *                 the AHT10/AHT20
 76 *   @humidity: the latest humidity value received from the
 77 *              AHT10/AHT20
 78 *   @crc8: crc8 support flag
 79 *   @meas_size: measurements data size
 80 */
 81
 82struct aht10_data {
 83	struct i2c_client *client;
 84	/*
 85	 * Prevent simultaneous access to the i2c
 86	 * client and previous_poll_time
 87	 */
 88	struct mutex lock;
 89	ktime_t min_poll_interval;
 90	ktime_t previous_poll_time;
 91	int temperature;
 92	int humidity;
 93	bool crc8;
 94	unsigned int meas_size;
 95};
 96
 97/**
 98 * aht10_init() - Initialize an AHT10/AHT20 chip
 99 * @data: the data associated with this AHT10/AHT20 chip
100 * Return: 0 if successful, 1 if not
 
101 */
102static int aht10_init(struct aht10_data *data)
103{
104	const u8 cmd_init[] = {AHT10_CMD_INIT, AHT10_CAL_ENABLED | AHT10_MODE_CYC,
105			       0x00};
106	int res;
107	u8 status;
108	struct i2c_client *client = data->client;
109
110	res = i2c_master_send(client, cmd_init, 3);
111	if (res < 0)
112		return res;
113
114	usleep_range(AHT10_CMD_DELAY, AHT10_CMD_DELAY +
115		     AHT10_DELAY_EXTRA);
116
117	res = i2c_master_recv(client, &status, 1);
118	if (res != 1)
119		return -ENODATA;
120
121	if (status & AHT10_BUSY)
122		return -EBUSY;
123
124	return 0;
125}
126
127/**
128 * aht10_polltime_expired() - check if the minimum poll interval has
129 *                                  expired
130 * @data: the data containing the time to compare
131 * Return: 1 if the minimum poll interval has expired, 0 if not
132 */
133static int aht10_polltime_expired(struct aht10_data *data)
134{
135	ktime_t current_time = ktime_get_boottime();
136	ktime_t difference = ktime_sub(current_time, data->previous_poll_time);
137
138	return ktime_after(difference, data->min_poll_interval);
139}
140
141DECLARE_CRC8_TABLE(crc8_table);
142
143/**
144 * crc8_check() - check crc of the sensor's measurements
145 * @raw_data: data frame received from sensor(including crc as the last byte)
146 * @count: size of the data frame
147 * Return: 0 if successful, 1 if not
148 */
149static int crc8_check(u8 *raw_data, int count)
150{
151	/*
152	 * crc calculated on the whole frame(including crc byte) should yield
153	 * zero in case of correctly received bytes
154	 */
155	return crc8(crc8_table, raw_data, count, CRC8_INIT_VALUE);
156}
157
158/**
159 * aht10_read_values() - read and parse the raw data from the AHT10/AHT20
160 * @data: the struct aht10_data to use for the lock
161 * Return: 0 if successful, 1 if not
162 */
163static int aht10_read_values(struct aht10_data *data)
164{
165	const u8 cmd_meas[] = {AHT10_CMD_MEAS, 0x33, 0x00};
166	u32 temp, hum;
167	int res;
168	u8 raw_data[AHT20_MEAS_SIZE];
169	struct i2c_client *client = data->client;
170
171	mutex_lock(&data->lock);
172	if (!aht10_polltime_expired(data)) {
173		mutex_unlock(&data->lock);
174		return 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
175	}
176
177	res = i2c_master_send(client, cmd_meas, sizeof(cmd_meas));
178	if (res < 0) {
179		mutex_unlock(&data->lock);
180		return res;
181	}
182
183	usleep_range(AHT10_MEAS_DELAY, AHT10_MEAS_DELAY + AHT10_DELAY_EXTRA);
184
185	res = i2c_master_recv(client, raw_data, data->meas_size);
186	if (res != data->meas_size) {
187		mutex_unlock(&data->lock);
188		if (res >= 0)
189			return -ENODATA;
190		return res;
191	}
192
193	if (data->crc8 && crc8_check(raw_data, data->meas_size)) {
194		mutex_unlock(&data->lock);
195		return -EIO;
196	}
197
198	hum =   ((u32)raw_data[1] << 12u) |
199		((u32)raw_data[2] << 4u) |
200		((raw_data[3] & 0xF0u) >> 4u);
201
202	temp =  ((u32)(raw_data[3] & 0x0Fu) << 16u) |
203		((u32)raw_data[4] << 8u) |
204		raw_data[5];
205
206	temp = ((temp * 625) >> 15u) * 10;
207	hum = ((hum * 625) >> 16u) * 10;
208
209	data->temperature = (int)temp - 50000;
210	data->humidity = hum;
211	data->previous_poll_time = ktime_get_boottime();
212
213	mutex_unlock(&data->lock);
214	return 0;
215}
216
217/**
218 * aht10_interval_write() - store the given minimum poll interval.
219 * Return: 0 on success, -EINVAL if a value lower than the
220 *         AHT10_MIN_POLL_INTERVAL is given
221 */
222static ssize_t aht10_interval_write(struct aht10_data *data,
223				    long val)
224{
225	data->min_poll_interval = ms_to_ktime(clamp_val(val, 2000, LONG_MAX));
226	return 0;
227}
228
229/**
230 * aht10_interval_read() - read the minimum poll interval
231 *                            in milliseconds
232 */
233static ssize_t aht10_interval_read(struct aht10_data *data,
234				   long *val)
235{
236	*val = ktime_to_ms(data->min_poll_interval);
237	return 0;
238}
239
240/**
241 * aht10_temperature1_read() - read the temperature in millidegrees
242 */
243static int aht10_temperature1_read(struct aht10_data *data, long *val)
244{
245	int res;
246
247	res = aht10_read_values(data);
248	if (res < 0)
249		return res;
250
251	*val = data->temperature;
252	return 0;
253}
254
255/**
256 * aht10_humidity1_read() - read the relative humidity in millipercent
257 */
258static int aht10_humidity1_read(struct aht10_data *data, long *val)
259{
260	int res;
261
262	res = aht10_read_values(data);
263	if (res < 0)
264		return res;
265
266	*val = data->humidity;
267	return 0;
268}
269
270static umode_t aht10_hwmon_visible(const void *data, enum hwmon_sensor_types type,
271				   u32 attr, int channel)
272{
273	switch (type) {
274	case hwmon_temp:
275	case hwmon_humidity:
276		return 0444;
277	case hwmon_chip:
278		return 0644;
279	default:
280		return 0;
281	}
282}
283
284static int aht10_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
285			    u32 attr, int channel, long *val)
286{
287	struct aht10_data *data = dev_get_drvdata(dev);
288
289	switch (type) {
290	case hwmon_temp:
291		return aht10_temperature1_read(data, val);
292	case hwmon_humidity:
293		return aht10_humidity1_read(data, val);
294	case hwmon_chip:
295		return aht10_interval_read(data, val);
296	default:
297		return -EOPNOTSUPP;
298	}
299}
300
301static int aht10_hwmon_write(struct device *dev, enum hwmon_sensor_types type,
302			     u32 attr, int channel, long val)
303{
304	struct aht10_data *data = dev_get_drvdata(dev);
305
306	switch (type) {
307	case hwmon_chip:
308		return aht10_interval_write(data, val);
309	default:
310		return -EOPNOTSUPP;
311	}
312}
313
314static const struct hwmon_channel_info * const aht10_info[] = {
315	HWMON_CHANNEL_INFO(chip, HWMON_C_UPDATE_INTERVAL),
316	HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT),
317	HWMON_CHANNEL_INFO(humidity, HWMON_H_INPUT),
318	NULL,
319};
320
321static const struct hwmon_ops aht10_hwmon_ops = {
322	.is_visible = aht10_hwmon_visible,
323	.read = aht10_hwmon_read,
324	.write = aht10_hwmon_write,
325};
326
327static const struct hwmon_chip_info aht10_chip_info = {
328	.ops = &aht10_hwmon_ops,
329	.info = aht10_info,
330};
331
332static int aht10_probe(struct i2c_client *client)
333{
334	const struct i2c_device_id *id = i2c_match_id(aht10_id, client);
335	enum aht10_variant variant = id->driver_data;
336	struct device *device = &client->dev;
337	struct device *hwmon_dev;
338	struct aht10_data *data;
339	int res;
340
341	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
342		return -ENOENT;
343
344	data = devm_kzalloc(device, sizeof(*data), GFP_KERNEL);
345	if (!data)
346		return -ENOMEM;
347
348	data->min_poll_interval = ms_to_ktime(AHT10_DEFAULT_MIN_POLL_INTERVAL);
349	data->client = client;
350
351	switch (variant) {
352	case aht20:
353		data->meas_size = AHT20_MEAS_SIZE;
354		data->crc8 = true;
355		crc8_populate_msb(crc8_table, AHT20_CRC8_POLY);
356		break;
357	default:
358		data->meas_size = AHT10_MEAS_SIZE;
359		break;
360	}
361
362	mutex_init(&data->lock);
363
364	res = aht10_init(data);
365	if (res < 0)
366		return res;
367
368	res = aht10_read_values(data);
369	if (res < 0)
370		return res;
371
372	hwmon_dev = devm_hwmon_device_register_with_info(device,
373							 client->name,
374							 data,
375							 &aht10_chip_info,
376							 NULL);
377
378	return PTR_ERR_OR_ZERO(hwmon_dev);
379}
380
 
 
 
 
 
 
381static struct i2c_driver aht10_driver = {
382	.driver = {
383		.name = "aht10",
384	},
385	.probe      = aht10_probe,
386	.id_table   = aht10_id,
387};
388
389module_i2c_driver(aht10_driver);
390
391MODULE_AUTHOR("Johannes Cornelis Draaijer <jcdra1@gmail.com>");
392MODULE_DESCRIPTION("AHT10/AHT20 Temperature and Humidity sensor driver");
393MODULE_VERSION("1.0");
394MODULE_LICENSE("GPL v2");
v6.2
  1// SPDX-License-Identifier: GPL-2.0-only
  2
  3/*
  4 * aht10.c - Linux hwmon driver for AHT10 Temperature and Humidity sensor
  5 * Copyright (C) 2020 Johannes Cornelis Draaijer
  6 */
  7
  8#include <linux/delay.h>
  9#include <linux/hwmon.h>
 10#include <linux/i2c.h>
 11#include <linux/ktime.h>
 12#include <linux/module.h>
 
 13
 14#define AHT10_MEAS_SIZE		6
 15
 
 
 
 16/*
 17 * Poll intervals (in milliseconds)
 18 */
 19#define AHT10_DEFAULT_MIN_POLL_INTERVAL	2000
 20#define AHT10_MIN_POLL_INTERVAL		2000
 21
 22/*
 23 * I2C command delays (in microseconds)
 24 */
 25#define AHT10_MEAS_DELAY	80000
 26#define AHT10_CMD_DELAY		350000
 27#define AHT10_DELAY_EXTRA	100000
 28
 29/*
 30 * Command bytes
 31 */
 32#define AHT10_CMD_INIT	0b11100001
 33#define AHT10_CMD_MEAS	0b10101100
 34#define AHT10_CMD_RST	0b10111010
 35
 36/*
 37 * Flags in the answer byte/command
 38 */
 39#define AHT10_CAL_ENABLED	BIT(3)
 40#define AHT10_BUSY		BIT(7)
 41#define AHT10_MODE_NOR		(BIT(5) | BIT(6))
 42#define AHT10_MODE_CYC		BIT(5)
 43#define AHT10_MODE_CMD		BIT(6)
 44
 45#define AHT10_MAX_POLL_INTERVAL_LEN	30
 46
 
 
 
 
 
 
 
 
 
 47/**
 48 *   struct aht10_data - All the data required to operate an AHT10 chip
 49 *   @client: the i2c client associated with the AHT10
 50 *   @lock: a mutex that is used to prevent parallel access to the
 51 *          i2c client
 52 *   @min_poll_interval: the minimum poll interval
 53 *                   While the poll rate limit is not 100% necessary,
 54 *                   the datasheet recommends that a measurement
 55 *                   is not performed too often to prevent
 56 *                   the chip from warming up due to the heat it generates.
 57 *                   If it's unwanted, it can be ignored setting it to
 58 *                   it to 0. Default value is 2000 ms
 59 *   @previous_poll_time: the previous time that the AHT10
 60 *                        was polled
 61 *   @temperature: the latest temperature value received from
 62 *                 the AHT10
 63 *   @humidity: the latest humidity value received from the
 64 *              AHT10
 
 
 65 */
 66
 67struct aht10_data {
 68	struct i2c_client *client;
 69	/*
 70	 * Prevent simultaneous access to the i2c
 71	 * client and previous_poll_time
 72	 */
 73	struct mutex lock;
 74	ktime_t min_poll_interval;
 75	ktime_t previous_poll_time;
 76	int temperature;
 77	int humidity;
 
 
 78};
 79
 80/**
 81 * aht10_init() - Initialize an AHT10 chip
 82 * @client: the i2c client associated with the AHT10
 83 * @data: the data associated with this AHT10 chip
 84 * Return: 0 if succesfull, 1 if not
 85 */
 86static int aht10_init(struct aht10_data *data)
 87{
 88	const u8 cmd_init[] = {AHT10_CMD_INIT, AHT10_CAL_ENABLED | AHT10_MODE_CYC,
 89			       0x00};
 90	int res;
 91	u8 status;
 92	struct i2c_client *client = data->client;
 93
 94	res = i2c_master_send(client, cmd_init, 3);
 95	if (res < 0)
 96		return res;
 97
 98	usleep_range(AHT10_CMD_DELAY, AHT10_CMD_DELAY +
 99		     AHT10_DELAY_EXTRA);
100
101	res = i2c_master_recv(client, &status, 1);
102	if (res != 1)
103		return -ENODATA;
104
105	if (status & AHT10_BUSY)
106		return -EBUSY;
107
108	return 0;
109}
110
111/**
112 * aht10_polltime_expired() - check if the minimum poll interval has
113 *                                  expired
114 * @data: the data containing the time to compare
115 * Return: 1 if the minimum poll interval has expired, 0 if not
116 */
117static int aht10_polltime_expired(struct aht10_data *data)
118{
119	ktime_t current_time = ktime_get_boottime();
120	ktime_t difference = ktime_sub(current_time, data->previous_poll_time);
121
122	return ktime_after(difference, data->min_poll_interval);
123}
124
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125/**
126 * aht10_read_values() - read and parse the raw data from the AHT10
127 * @aht10_data: the struct aht10_data to use for the lock
128 * Return: 0 if succesfull, 1 if not
129 */
130static int aht10_read_values(struct aht10_data *data)
131{
132	const u8 cmd_meas[] = {AHT10_CMD_MEAS, 0x33, 0x00};
133	u32 temp, hum;
134	int res;
135	u8 raw_data[AHT10_MEAS_SIZE];
136	struct i2c_client *client = data->client;
137
138	mutex_lock(&data->lock);
139	if (aht10_polltime_expired(data)) {
140		res = i2c_master_send(client, cmd_meas, sizeof(cmd_meas));
141		if (res < 0) {
142			mutex_unlock(&data->lock);
143			return res;
144		}
145
146		usleep_range(AHT10_MEAS_DELAY,
147			     AHT10_MEAS_DELAY + AHT10_DELAY_EXTRA);
148
149		res = i2c_master_recv(client, raw_data, AHT10_MEAS_SIZE);
150		if (res != AHT10_MEAS_SIZE) {
151			mutex_unlock(&data->lock);
152			if (res >= 0)
153				return -ENODATA;
154			else
155				return res;
156		}
157
158		hum =   ((u32)raw_data[1] << 12u) |
159			((u32)raw_data[2] << 4u) |
160			((raw_data[3] & 0xF0u) >> 4u);
161
162		temp =  ((u32)(raw_data[3] & 0x0Fu) << 16u) |
163			((u32)raw_data[4] << 8u) |
164			raw_data[5];
165
166		temp = ((temp * 625) >> 15u) * 10;
167		hum = ((hum * 625) >> 16u) * 10;
168
169		data->temperature = (int)temp - 50000;
170		data->humidity = hum;
171		data->previous_poll_time = ktime_get_boottime();
172	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
173	mutex_unlock(&data->lock);
174	return 0;
175}
176
177/**
178 * aht10_interval_write() - store the given minimum poll interval.
179 * Return: 0 on success, -EINVAL if a value lower than the
180 *         AHT10_MIN_POLL_INTERVAL is given
181 */
182static ssize_t aht10_interval_write(struct aht10_data *data,
183				    long val)
184{
185	data->min_poll_interval = ms_to_ktime(clamp_val(val, 2000, LONG_MAX));
186	return 0;
187}
188
189/**
190 * aht10_interval_read() - read the minimum poll interval
191 *                            in milliseconds
192 */
193static ssize_t aht10_interval_read(struct aht10_data *data,
194				   long *val)
195{
196	*val = ktime_to_ms(data->min_poll_interval);
197	return 0;
198}
199
200/**
201 * aht10_temperature1_read() - read the temperature in millidegrees
202 */
203static int aht10_temperature1_read(struct aht10_data *data, long *val)
204{
205	int res;
206
207	res = aht10_read_values(data);
208	if (res < 0)
209		return res;
210
211	*val = data->temperature;
212	return 0;
213}
214
215/**
216 * aht10_humidity1_read() - read the relative humidity in millipercent
217 */
218static int aht10_humidity1_read(struct aht10_data *data, long *val)
219{
220	int res;
221
222	res = aht10_read_values(data);
223	if (res < 0)
224		return res;
225
226	*val = data->humidity;
227	return 0;
228}
229
230static umode_t aht10_hwmon_visible(const void *data, enum hwmon_sensor_types type,
231				   u32 attr, int channel)
232{
233	switch (type) {
234	case hwmon_temp:
235	case hwmon_humidity:
236		return 0444;
237	case hwmon_chip:
238		return 0644;
239	default:
240		return 0;
241	}
242}
243
244static int aht10_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
245			    u32 attr, int channel, long *val)
246{
247	struct aht10_data *data = dev_get_drvdata(dev);
248
249	switch (type) {
250	case hwmon_temp:
251		return aht10_temperature1_read(data, val);
252	case hwmon_humidity:
253		return aht10_humidity1_read(data, val);
254	case hwmon_chip:
255		return aht10_interval_read(data, val);
256	default:
257		return -EOPNOTSUPP;
258	}
259}
260
261static int aht10_hwmon_write(struct device *dev, enum hwmon_sensor_types type,
262			     u32 attr, int channel, long val)
263{
264	struct aht10_data *data = dev_get_drvdata(dev);
265
266	switch (type) {
267	case hwmon_chip:
268		return aht10_interval_write(data, val);
269	default:
270		return -EOPNOTSUPP;
271	}
272}
273
274static const struct hwmon_channel_info *aht10_info[] = {
275	HWMON_CHANNEL_INFO(chip, HWMON_C_UPDATE_INTERVAL),
276	HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT),
277	HWMON_CHANNEL_INFO(humidity, HWMON_H_INPUT),
278	NULL,
279};
280
281static const struct hwmon_ops aht10_hwmon_ops = {
282	.is_visible = aht10_hwmon_visible,
283	.read = aht10_hwmon_read,
284	.write = aht10_hwmon_write,
285};
286
287static const struct hwmon_chip_info aht10_chip_info = {
288	.ops = &aht10_hwmon_ops,
289	.info = aht10_info,
290};
291
292static int aht10_probe(struct i2c_client *client)
293{
 
 
294	struct device *device = &client->dev;
295	struct device *hwmon_dev;
296	struct aht10_data *data;
297	int res;
298
299	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
300		return -ENOENT;
301
302	data = devm_kzalloc(device, sizeof(*data), GFP_KERNEL);
303	if (!data)
304		return -ENOMEM;
305
306	data->min_poll_interval = ms_to_ktime(AHT10_DEFAULT_MIN_POLL_INTERVAL);
307	data->client = client;
308
 
 
 
 
 
 
 
 
 
 
 
309	mutex_init(&data->lock);
310
311	res = aht10_init(data);
312	if (res < 0)
313		return res;
314
315	res = aht10_read_values(data);
316	if (res < 0)
317		return res;
318
319	hwmon_dev = devm_hwmon_device_register_with_info(device,
320							 client->name,
321							 data,
322							 &aht10_chip_info,
323							 NULL);
324
325	return PTR_ERR_OR_ZERO(hwmon_dev);
326}
327
328static const struct i2c_device_id aht10_id[] = {
329	{ "aht10", 0 },
330	{ },
331};
332MODULE_DEVICE_TABLE(i2c, aht10_id);
333
334static struct i2c_driver aht10_driver = {
335	.driver = {
336		.name = "aht10",
337	},
338	.probe_new  = aht10_probe,
339	.id_table   = aht10_id,
340};
341
342module_i2c_driver(aht10_driver);
343
344MODULE_AUTHOR("Johannes Cornelis Draaijer <jcdra1@gmail.com>");
345MODULE_DESCRIPTION("AHT10 Temperature and Humidity sensor driver");
346MODULE_VERSION("1.0");
347MODULE_LICENSE("GPL v2");