Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 
0003 /*
0004  * aht10.c - Linux hwmon driver for AHT10 Temperature and Humidity sensor
0005  * Copyright (C) 2020 Johannes Cornelis Draaijer
0006  */
0007 
0008 #include <linux/delay.h>
0009 #include <linux/hwmon.h>
0010 #include <linux/i2c.h>
0011 #include <linux/ktime.h>
0012 #include <linux/module.h>
0013 
0014 #define AHT10_MEAS_SIZE     6
0015 
0016 /*
0017  * Poll intervals (in milliseconds)
0018  */
0019 #define AHT10_DEFAULT_MIN_POLL_INTERVAL 2000
0020 #define AHT10_MIN_POLL_INTERVAL     2000
0021 
0022 /*
0023  * I2C command delays (in microseconds)
0024  */
0025 #define AHT10_MEAS_DELAY    80000
0026 #define AHT10_CMD_DELAY     350000
0027 #define AHT10_DELAY_EXTRA   100000
0028 
0029 /*
0030  * Command bytes
0031  */
0032 #define AHT10_CMD_INIT  0b11100001
0033 #define AHT10_CMD_MEAS  0b10101100
0034 #define AHT10_CMD_RST   0b10111010
0035 
0036 /*
0037  * Flags in the answer byte/command
0038  */
0039 #define AHT10_CAL_ENABLED   BIT(3)
0040 #define AHT10_BUSY      BIT(7)
0041 #define AHT10_MODE_NOR      (BIT(5) | BIT(6))
0042 #define AHT10_MODE_CYC      BIT(5)
0043 #define AHT10_MODE_CMD      BIT(6)
0044 
0045 #define AHT10_MAX_POLL_INTERVAL_LEN 30
0046 
0047 /**
0048  *   struct aht10_data - All the data required to operate an AHT10 chip
0049  *   @client: the i2c client associated with the AHT10
0050  *   @lock: a mutex that is used to prevent parallel access to the
0051  *          i2c client
0052  *   @min_poll_interval: the minimum poll interval
0053  *                   While the poll rate limit is not 100% necessary,
0054  *                   the datasheet recommends that a measurement
0055  *                   is not performed too often to prevent
0056  *                   the chip from warming up due to the heat it generates.
0057  *                   If it's unwanted, it can be ignored setting it to
0058  *                   it to 0. Default value is 2000 ms
0059  *   @previous_poll_time: the previous time that the AHT10
0060  *                        was polled
0061  *   @temperature: the latest temperature value received from
0062  *                 the AHT10
0063  *   @humidity: the latest humidity value received from the
0064  *              AHT10
0065  */
0066 
0067 struct aht10_data {
0068     struct i2c_client *client;
0069     /*
0070      * Prevent simultaneous access to the i2c
0071      * client and previous_poll_time
0072      */
0073     struct mutex lock;
0074     ktime_t min_poll_interval;
0075     ktime_t previous_poll_time;
0076     int temperature;
0077     int humidity;
0078 };
0079 
0080 /**
0081  * aht10_init() - Initialize an AHT10 chip
0082  * @client: the i2c client associated with the AHT10
0083  * @data: the data associated with this AHT10 chip
0084  * Return: 0 if succesfull, 1 if not
0085  */
0086 static int aht10_init(struct aht10_data *data)
0087 {
0088     const u8 cmd_init[] = {AHT10_CMD_INIT, AHT10_CAL_ENABLED | AHT10_MODE_CYC,
0089                    0x00};
0090     int res;
0091     u8 status;
0092     struct i2c_client *client = data->client;
0093 
0094     res = i2c_master_send(client, cmd_init, 3);
0095     if (res < 0)
0096         return res;
0097 
0098     usleep_range(AHT10_CMD_DELAY, AHT10_CMD_DELAY +
0099              AHT10_DELAY_EXTRA);
0100 
0101     res = i2c_master_recv(client, &status, 1);
0102     if (res != 1)
0103         return -ENODATA;
0104 
0105     if (status & AHT10_BUSY)
0106         return -EBUSY;
0107 
0108     return 0;
0109 }
0110 
0111 /**
0112  * aht10_polltime_expired() - check if the minimum poll interval has
0113  *                                  expired
0114  * @data: the data containing the time to compare
0115  * Return: 1 if the minimum poll interval has expired, 0 if not
0116  */
0117 static int aht10_polltime_expired(struct aht10_data *data)
0118 {
0119     ktime_t current_time = ktime_get_boottime();
0120     ktime_t difference = ktime_sub(current_time, data->previous_poll_time);
0121 
0122     return ktime_after(difference, data->min_poll_interval);
0123 }
0124 
0125 /**
0126  * aht10_read_values() - read and parse the raw data from the AHT10
0127  * @aht10_data: the struct aht10_data to use for the lock
0128  * Return: 0 if succesfull, 1 if not
0129  */
0130 static int aht10_read_values(struct aht10_data *data)
0131 {
0132     const u8 cmd_meas[] = {AHT10_CMD_MEAS, 0x33, 0x00};
0133     u32 temp, hum;
0134     int res;
0135     u8 raw_data[AHT10_MEAS_SIZE];
0136     struct i2c_client *client = data->client;
0137 
0138     mutex_lock(&data->lock);
0139     if (aht10_polltime_expired(data)) {
0140         res = i2c_master_send(client, cmd_meas, sizeof(cmd_meas));
0141         if (res < 0) {
0142             mutex_unlock(&data->lock);
0143             return res;
0144         }
0145 
0146         usleep_range(AHT10_MEAS_DELAY,
0147                  AHT10_MEAS_DELAY + AHT10_DELAY_EXTRA);
0148 
0149         res = i2c_master_recv(client, raw_data, AHT10_MEAS_SIZE);
0150         if (res != AHT10_MEAS_SIZE) {
0151             mutex_unlock(&data->lock);
0152             if (res >= 0)
0153                 return -ENODATA;
0154             else
0155                 return res;
0156         }
0157 
0158         hum =   ((u32)raw_data[1] << 12u) |
0159             ((u32)raw_data[2] << 4u) |
0160             ((raw_data[3] & 0xF0u) >> 4u);
0161 
0162         temp =  ((u32)(raw_data[3] & 0x0Fu) << 16u) |
0163             ((u32)raw_data[4] << 8u) |
0164             raw_data[5];
0165 
0166         temp = ((temp * 625) >> 15u) * 10;
0167         hum = ((hum * 625) >> 16u) * 10;
0168 
0169         data->temperature = (int)temp - 50000;
0170         data->humidity = hum;
0171         data->previous_poll_time = ktime_get_boottime();
0172     }
0173     mutex_unlock(&data->lock);
0174     return 0;
0175 }
0176 
0177 /**
0178  * aht10_interval_write() - store the given minimum poll interval.
0179  * Return: 0 on success, -EINVAL if a value lower than the
0180  *         AHT10_MIN_POLL_INTERVAL is given
0181  */
0182 static ssize_t aht10_interval_write(struct aht10_data *data,
0183                     long val)
0184 {
0185     data->min_poll_interval = ms_to_ktime(clamp_val(val, 2000, LONG_MAX));
0186     return 0;
0187 }
0188 
0189 /**
0190  * aht10_interval_read() - read the minimum poll interval
0191  *                            in milliseconds
0192  */
0193 static ssize_t aht10_interval_read(struct aht10_data *data,
0194                    long *val)
0195 {
0196     *val = ktime_to_ms(data->min_poll_interval);
0197     return 0;
0198 }
0199 
0200 /**
0201  * aht10_temperature1_read() - read the temperature in millidegrees
0202  */
0203 static int aht10_temperature1_read(struct aht10_data *data, long *val)
0204 {
0205     int res;
0206 
0207     res = aht10_read_values(data);
0208     if (res < 0)
0209         return res;
0210 
0211     *val = data->temperature;
0212     return 0;
0213 }
0214 
0215 /**
0216  * aht10_humidity1_read() - read the relative humidity in millipercent
0217  */
0218 static int aht10_humidity1_read(struct aht10_data *data, long *val)
0219 {
0220     int res;
0221 
0222     res = aht10_read_values(data);
0223     if (res < 0)
0224         return res;
0225 
0226     *val = data->humidity;
0227     return 0;
0228 }
0229 
0230 static umode_t aht10_hwmon_visible(const void *data, enum hwmon_sensor_types type,
0231                    u32 attr, int channel)
0232 {
0233     switch (type) {
0234     case hwmon_temp:
0235     case hwmon_humidity:
0236         return 0444;
0237     case hwmon_chip:
0238         return 0644;
0239     default:
0240         return 0;
0241     }
0242 }
0243 
0244 static int aht10_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
0245                 u32 attr, int channel, long *val)
0246 {
0247     struct aht10_data *data = dev_get_drvdata(dev);
0248 
0249     switch (type) {
0250     case hwmon_temp:
0251         return aht10_temperature1_read(data, val);
0252     case hwmon_humidity:
0253         return aht10_humidity1_read(data, val);
0254     case hwmon_chip:
0255         return aht10_interval_read(data, val);
0256     default:
0257         return -EOPNOTSUPP;
0258     }
0259 }
0260 
0261 static int aht10_hwmon_write(struct device *dev, enum hwmon_sensor_types type,
0262                  u32 attr, int channel, long val)
0263 {
0264     struct aht10_data *data = dev_get_drvdata(dev);
0265 
0266     switch (type) {
0267     case hwmon_chip:
0268         return aht10_interval_write(data, val);
0269     default:
0270         return -EOPNOTSUPP;
0271     }
0272 }
0273 
0274 static const struct hwmon_channel_info *aht10_info[] = {
0275     HWMON_CHANNEL_INFO(chip, HWMON_C_UPDATE_INTERVAL),
0276     HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT),
0277     HWMON_CHANNEL_INFO(humidity, HWMON_H_INPUT),
0278     NULL,
0279 };
0280 
0281 static const struct hwmon_ops aht10_hwmon_ops = {
0282     .is_visible = aht10_hwmon_visible,
0283     .read = aht10_hwmon_read,
0284     .write = aht10_hwmon_write,
0285 };
0286 
0287 static const struct hwmon_chip_info aht10_chip_info = {
0288     .ops = &aht10_hwmon_ops,
0289     .info = aht10_info,
0290 };
0291 
0292 static int aht10_probe(struct i2c_client *client,
0293                const struct i2c_device_id *aht10_id)
0294 {
0295     struct device *device = &client->dev;
0296     struct device *hwmon_dev;
0297     struct aht10_data *data;
0298     int res;
0299 
0300     if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
0301         return -ENOENT;
0302 
0303     data = devm_kzalloc(device, sizeof(*data), GFP_KERNEL);
0304     if (!data)
0305         return -ENOMEM;
0306 
0307     data->min_poll_interval = ms_to_ktime(AHT10_DEFAULT_MIN_POLL_INTERVAL);
0308     data->client = client;
0309 
0310     mutex_init(&data->lock);
0311 
0312     res = aht10_init(data);
0313     if (res < 0)
0314         return res;
0315 
0316     res = aht10_read_values(data);
0317     if (res < 0)
0318         return res;
0319 
0320     hwmon_dev = devm_hwmon_device_register_with_info(device,
0321                              client->name,
0322                              data,
0323                              &aht10_chip_info,
0324                              NULL);
0325 
0326     return PTR_ERR_OR_ZERO(hwmon_dev);
0327 }
0328 
0329 static const struct i2c_device_id aht10_id[] = {
0330     { "aht10", 0 },
0331     { },
0332 };
0333 MODULE_DEVICE_TABLE(i2c, aht10_id);
0334 
0335 static struct i2c_driver aht10_driver = {
0336     .driver = {
0337         .name = "aht10",
0338     },
0339     .probe      = aht10_probe,
0340     .id_table   = aht10_id,
0341 };
0342 
0343 module_i2c_driver(aht10_driver);
0344 
0345 MODULE_AUTHOR("Johannes Cornelis Draaijer <jcdra1@gmail.com>");
0346 MODULE_DESCRIPTION("AHT10 Temperature and Humidity sensor driver");
0347 MODULE_VERSION("1.0");
0348 MODULE_LICENSE("GPL v2");