xref: /openbmc/linux/drivers/hwmon/aht10.c (revision 014714b8a62107d22bd2a089801bbfb4bb6412ae)
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 
67 struct 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  * @data: the data associated with this AHT10 chip
83  * Return: 0 if successful, 1 if not
84  */
85 static int aht10_init(struct aht10_data *data)
86 {
87 	const u8 cmd_init[] = {AHT10_CMD_INIT, AHT10_CAL_ENABLED | AHT10_MODE_CYC,
88 			       0x00};
89 	int res;
90 	u8 status;
91 	struct i2c_client *client = data->client;
92 
93 	res = i2c_master_send(client, cmd_init, 3);
94 	if (res < 0)
95 		return res;
96 
97 	usleep_range(AHT10_CMD_DELAY, AHT10_CMD_DELAY +
98 		     AHT10_DELAY_EXTRA);
99 
100 	res = i2c_master_recv(client, &status, 1);
101 	if (res != 1)
102 		return -ENODATA;
103 
104 	if (status & AHT10_BUSY)
105 		return -EBUSY;
106 
107 	return 0;
108 }
109 
110 /**
111  * aht10_polltime_expired() - check if the minimum poll interval has
112  *                                  expired
113  * @data: the data containing the time to compare
114  * Return: 1 if the minimum poll interval has expired, 0 if not
115  */
116 static int aht10_polltime_expired(struct aht10_data *data)
117 {
118 	ktime_t current_time = ktime_get_boottime();
119 	ktime_t difference = ktime_sub(current_time, data->previous_poll_time);
120 
121 	return ktime_after(difference, data->min_poll_interval);
122 }
123 
124 /**
125  * aht10_read_values() - read and parse the raw data from the AHT10
126  * @data: the struct aht10_data to use for the lock
127  * Return: 0 if successful, 1 if not
128  */
129 static int aht10_read_values(struct aht10_data *data)
130 {
131 	const u8 cmd_meas[] = {AHT10_CMD_MEAS, 0x33, 0x00};
132 	u32 temp, hum;
133 	int res;
134 	u8 raw_data[AHT10_MEAS_SIZE];
135 	struct i2c_client *client = data->client;
136 
137 	mutex_lock(&data->lock);
138 	if (!aht10_polltime_expired(data)) {
139 		mutex_unlock(&data->lock);
140 		return 0;
141 	}
142 
143 	res = i2c_master_send(client, cmd_meas, sizeof(cmd_meas));
144 	if (res < 0) {
145 		mutex_unlock(&data->lock);
146 		return res;
147 	}
148 
149 	usleep_range(AHT10_MEAS_DELAY, AHT10_MEAS_DELAY + AHT10_DELAY_EXTRA);
150 
151 	res = i2c_master_recv(client, raw_data, AHT10_MEAS_SIZE);
152 	if (res != AHT10_MEAS_SIZE) {
153 		mutex_unlock(&data->lock);
154 		if (res >= 0)
155 			return -ENODATA;
156 		return res;
157 	}
158 
159 	hum =   ((u32)raw_data[1] << 12u) |
160 		((u32)raw_data[2] << 4u) |
161 		((raw_data[3] & 0xF0u) >> 4u);
162 
163 	temp =  ((u32)(raw_data[3] & 0x0Fu) << 16u) |
164 		((u32)raw_data[4] << 8u) |
165 		raw_data[5];
166 
167 	temp = ((temp * 625) >> 15u) * 10;
168 	hum = ((hum * 625) >> 16u) * 10;
169 
170 	data->temperature = (int)temp - 50000;
171 	data->humidity = hum;
172 	data->previous_poll_time = ktime_get_boottime();
173 
174 	mutex_unlock(&data->lock);
175 	return 0;
176 }
177 
178 /**
179  * aht10_interval_write() - store the given minimum poll interval.
180  * Return: 0 on success, -EINVAL if a value lower than the
181  *         AHT10_MIN_POLL_INTERVAL is given
182  */
183 static ssize_t aht10_interval_write(struct aht10_data *data,
184 				    long val)
185 {
186 	data->min_poll_interval = ms_to_ktime(clamp_val(val, 2000, LONG_MAX));
187 	return 0;
188 }
189 
190 /**
191  * aht10_interval_read() - read the minimum poll interval
192  *                            in milliseconds
193  */
194 static ssize_t aht10_interval_read(struct aht10_data *data,
195 				   long *val)
196 {
197 	*val = ktime_to_ms(data->min_poll_interval);
198 	return 0;
199 }
200 
201 /**
202  * aht10_temperature1_read() - read the temperature in millidegrees
203  */
204 static int aht10_temperature1_read(struct aht10_data *data, long *val)
205 {
206 	int res;
207 
208 	res = aht10_read_values(data);
209 	if (res < 0)
210 		return res;
211 
212 	*val = data->temperature;
213 	return 0;
214 }
215 
216 /**
217  * aht10_humidity1_read() - read the relative humidity in millipercent
218  */
219 static int aht10_humidity1_read(struct aht10_data *data, long *val)
220 {
221 	int res;
222 
223 	res = aht10_read_values(data);
224 	if (res < 0)
225 		return res;
226 
227 	*val = data->humidity;
228 	return 0;
229 }
230 
231 static umode_t aht10_hwmon_visible(const void *data, enum hwmon_sensor_types type,
232 				   u32 attr, int channel)
233 {
234 	switch (type) {
235 	case hwmon_temp:
236 	case hwmon_humidity:
237 		return 0444;
238 	case hwmon_chip:
239 		return 0644;
240 	default:
241 		return 0;
242 	}
243 }
244 
245 static int aht10_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
246 			    u32 attr, int channel, long *val)
247 {
248 	struct aht10_data *data = dev_get_drvdata(dev);
249 
250 	switch (type) {
251 	case hwmon_temp:
252 		return aht10_temperature1_read(data, val);
253 	case hwmon_humidity:
254 		return aht10_humidity1_read(data, val);
255 	case hwmon_chip:
256 		return aht10_interval_read(data, val);
257 	default:
258 		return -EOPNOTSUPP;
259 	}
260 }
261 
262 static int aht10_hwmon_write(struct device *dev, enum hwmon_sensor_types type,
263 			     u32 attr, int channel, long val)
264 {
265 	struct aht10_data *data = dev_get_drvdata(dev);
266 
267 	switch (type) {
268 	case hwmon_chip:
269 		return aht10_interval_write(data, val);
270 	default:
271 		return -EOPNOTSUPP;
272 	}
273 }
274 
275 static const struct hwmon_channel_info * const aht10_info[] = {
276 	HWMON_CHANNEL_INFO(chip, HWMON_C_UPDATE_INTERVAL),
277 	HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT),
278 	HWMON_CHANNEL_INFO(humidity, HWMON_H_INPUT),
279 	NULL,
280 };
281 
282 static const struct hwmon_ops aht10_hwmon_ops = {
283 	.is_visible = aht10_hwmon_visible,
284 	.read = aht10_hwmon_read,
285 	.write = aht10_hwmon_write,
286 };
287 
288 static const struct hwmon_chip_info aht10_chip_info = {
289 	.ops = &aht10_hwmon_ops,
290 	.info = aht10_info,
291 };
292 
293 static int aht10_probe(struct i2c_client *client)
294 {
295 	struct device *device = &client->dev;
296 	struct device *hwmon_dev;
297 	struct aht10_data *data;
298 	int res;
299 
300 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
301 		return -ENOENT;
302 
303 	data = devm_kzalloc(device, sizeof(*data), GFP_KERNEL);
304 	if (!data)
305 		return -ENOMEM;
306 
307 	data->min_poll_interval = ms_to_ktime(AHT10_DEFAULT_MIN_POLL_INTERVAL);
308 	data->client = client;
309 
310 	mutex_init(&data->lock);
311 
312 	res = aht10_init(data);
313 	if (res < 0)
314 		return res;
315 
316 	res = aht10_read_values(data);
317 	if (res < 0)
318 		return res;
319 
320 	hwmon_dev = devm_hwmon_device_register_with_info(device,
321 							 client->name,
322 							 data,
323 							 &aht10_chip_info,
324 							 NULL);
325 
326 	return PTR_ERR_OR_ZERO(hwmon_dev);
327 }
328 
329 static const struct i2c_device_id aht10_id[] = {
330 	{ "aht10", 0 },
331 	{ },
332 };
333 MODULE_DEVICE_TABLE(i2c, aht10_id);
334 
335 static struct i2c_driver aht10_driver = {
336 	.driver = {
337 		.name = "aht10",
338 	},
339 	.probe      = aht10_probe,
340 	.id_table   = aht10_id,
341 };
342 
343 module_i2c_driver(aht10_driver);
344 
345 MODULE_AUTHOR("Johannes Cornelis Draaijer <jcdra1@gmail.com>");
346 MODULE_DESCRIPTION("AHT10 Temperature and Humidity sensor driver");
347 MODULE_VERSION("1.0");
348 MODULE_LICENSE("GPL v2");
349