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