root/drivers/iio/humidity/am2315.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. am2315_crc
  2. am2315_ping
  3. am2315_read_data
  4. am2315_trigger_handler
  5. am2315_read_raw
  6. am2315_probe

   1 // SPDX-License-Identifier: GPL-2.0-only
   2 /**
   3  * Aosong AM2315 relative humidity and temperature
   4  *
   5  * Copyright (c) 2016, Intel Corporation.
   6  *
   7  * 7-bit I2C address: 0x5C.
   8  */
   9 
  10 #include <linux/acpi.h>
  11 #include <linux/delay.h>
  12 #include <linux/i2c.h>
  13 #include <linux/kernel.h>
  14 #include <linux/module.h>
  15 #include <linux/iio/buffer.h>
  16 #include <linux/iio/iio.h>
  17 #include <linux/iio/sysfs.h>
  18 #include <linux/iio/trigger_consumer.h>
  19 #include <linux/iio/triggered_buffer.h>
  20 
  21 #define AM2315_REG_HUM_MSB                      0x00
  22 #define AM2315_REG_HUM_LSB                      0x01
  23 #define AM2315_REG_TEMP_MSB                     0x02
  24 #define AM2315_REG_TEMP_LSB                     0x03
  25 
  26 #define AM2315_FUNCTION_READ                    0x03
  27 #define AM2315_HUM_OFFSET                       2
  28 #define AM2315_TEMP_OFFSET                      4
  29 #define AM2315_ALL_CHANNEL_MASK                 GENMASK(1, 0)
  30 
  31 #define AM2315_DRIVER_NAME                      "am2315"
  32 
  33 struct am2315_data {
  34         struct i2c_client *client;
  35         struct mutex lock;
  36         s16 buffer[8]; /* 2x16-bit channels + 2x16 padding + 4x16 timestamp */
  37 };
  38 
  39 struct am2315_sensor_data {
  40         s16 hum_data;
  41         s16 temp_data;
  42 };
  43 
  44 static const struct iio_chan_spec am2315_channels[] = {
  45         {
  46                 .type = IIO_HUMIDITYRELATIVE,
  47                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  48                                       BIT(IIO_CHAN_INFO_SCALE),
  49                 .scan_index = 0,
  50                 .scan_type = {
  51                         .sign = 's',
  52                         .realbits = 16,
  53                         .storagebits = 16,
  54                         .endianness = IIO_CPU,
  55                 },
  56         },
  57         {
  58                 .type = IIO_TEMP,
  59                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  60                                       BIT(IIO_CHAN_INFO_SCALE),
  61                 .scan_index = 1,
  62                 .scan_type = {
  63                         .sign = 's',
  64                         .realbits = 16,
  65                         .storagebits = 16,
  66                         .endianness = IIO_CPU,
  67                 },
  68         },
  69         IIO_CHAN_SOFT_TIMESTAMP(2),
  70 };
  71 
  72 /* CRC calculation algorithm, as specified in the datasheet (page 13). */
  73 static u16 am2315_crc(u8 *data, u8 nr_bytes)
  74 {
  75         int i;
  76         u16 crc = 0xffff;
  77 
  78         while (nr_bytes--) {
  79                 crc ^= *data++;
  80                 for (i = 0; i < 8; i++) {
  81                         if (crc & 0x01) {
  82                                 crc >>= 1;
  83                                 crc ^= 0xA001;
  84                         } else {
  85                                 crc >>= 1;
  86                         }
  87                 }
  88         }
  89 
  90         return crc;
  91 }
  92 
  93 /* Simple function that sends a few bytes to the device to wake it up. */
  94 static void am2315_ping(struct i2c_client *client)
  95 {
  96         i2c_smbus_read_byte_data(client, AM2315_REG_HUM_MSB);
  97 }
  98 
  99 static int am2315_read_data(struct am2315_data *data,
 100                             struct am2315_sensor_data *sensor_data)
 101 {
 102         int ret;
 103         /* tx_buf format: <function code> <start addr> <nr of regs to read> */
 104         u8 tx_buf[3] = { AM2315_FUNCTION_READ, AM2315_REG_HUM_MSB, 4 };
 105         /*
 106          * rx_buf format:
 107          * <function code> <number of registers read>
 108          * <humidity MSB> <humidity LSB> <temp MSB> <temp LSB>
 109          * <CRC LSB> <CRC MSB>
 110          */
 111         u8 rx_buf[8];
 112         u16 crc;
 113 
 114         /* First wake up the device. */
 115         am2315_ping(data->client);
 116 
 117         mutex_lock(&data->lock);
 118         ret = i2c_master_send(data->client, tx_buf, sizeof(tx_buf));
 119         if (ret < 0) {
 120                 dev_err(&data->client->dev, "failed to send read request\n");
 121                 goto exit_unlock;
 122         }
 123         /* Wait 2-3 ms, then read back the data sent by the device. */
 124         usleep_range(2000, 3000);
 125         /* Do a bulk data read, then pick out what we need. */
 126         ret = i2c_master_recv(data->client, rx_buf, sizeof(rx_buf));
 127         if (ret < 0) {
 128                 dev_err(&data->client->dev, "failed to read sensor data\n");
 129                 goto exit_unlock;
 130         }
 131         mutex_unlock(&data->lock);
 132         /*
 133          * Do a CRC check on the data and compare it to the value
 134          * calculated by the device.
 135          */
 136         crc = am2315_crc(rx_buf, sizeof(rx_buf) - 2);
 137         if ((crc & 0xff) != rx_buf[6] || (crc >> 8) != rx_buf[7]) {
 138                 dev_err(&data->client->dev, "failed to verify sensor data\n");
 139                 return -EIO;
 140         }
 141 
 142         sensor_data->hum_data = (rx_buf[AM2315_HUM_OFFSET] << 8) |
 143                                  rx_buf[AM2315_HUM_OFFSET + 1];
 144         sensor_data->temp_data = (rx_buf[AM2315_TEMP_OFFSET] << 8) |
 145                                   rx_buf[AM2315_TEMP_OFFSET + 1];
 146 
 147         return ret;
 148 
 149 exit_unlock:
 150         mutex_unlock(&data->lock);
 151         return ret;
 152 }
 153 
 154 static irqreturn_t am2315_trigger_handler(int irq, void *p)
 155 {
 156         int i;
 157         int ret;
 158         int bit;
 159         struct iio_poll_func *pf = p;
 160         struct iio_dev *indio_dev = pf->indio_dev;
 161         struct am2315_data *data = iio_priv(indio_dev);
 162         struct am2315_sensor_data sensor_data;
 163 
 164         ret = am2315_read_data(data, &sensor_data);
 165         if (ret < 0)
 166                 goto err;
 167 
 168         mutex_lock(&data->lock);
 169         if (*(indio_dev->active_scan_mask) == AM2315_ALL_CHANNEL_MASK) {
 170                 data->buffer[0] = sensor_data.hum_data;
 171                 data->buffer[1] = sensor_data.temp_data;
 172         } else {
 173                 i = 0;
 174                 for_each_set_bit(bit, indio_dev->active_scan_mask,
 175                                  indio_dev->masklength) {
 176                         data->buffer[i] = (bit ? sensor_data.temp_data :
 177                                                  sensor_data.hum_data);
 178                         i++;
 179                 }
 180         }
 181         mutex_unlock(&data->lock);
 182 
 183         iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
 184                                            pf->timestamp);
 185 err:
 186         iio_trigger_notify_done(indio_dev->trig);
 187         return IRQ_HANDLED;
 188 }
 189 
 190 static int am2315_read_raw(struct iio_dev *indio_dev,
 191                            struct iio_chan_spec const *chan,
 192                            int *val, int *val2, long mask)
 193 {
 194         int ret;
 195         struct am2315_sensor_data sensor_data;
 196         struct am2315_data *data = iio_priv(indio_dev);
 197 
 198         switch (mask) {
 199         case IIO_CHAN_INFO_RAW:
 200                 ret = am2315_read_data(data, &sensor_data);
 201                 if (ret < 0)
 202                         return ret;
 203                 *val = (chan->type == IIO_HUMIDITYRELATIVE) ?
 204                                 sensor_data.hum_data : sensor_data.temp_data;
 205                 return IIO_VAL_INT;
 206         case IIO_CHAN_INFO_SCALE:
 207                 *val = 100;
 208                 return IIO_VAL_INT;
 209         }
 210 
 211         return -EINVAL;
 212 }
 213 
 214 static const struct iio_info am2315_info = {
 215         .read_raw               = am2315_read_raw,
 216 };
 217 
 218 static int am2315_probe(struct i2c_client *client,
 219                         const struct i2c_device_id *id)
 220 {
 221         int ret;
 222         struct iio_dev *indio_dev;
 223         struct am2315_data *data;
 224 
 225         indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
 226         if (!indio_dev) {
 227                 dev_err(&client->dev, "iio allocation failed!\n");
 228                 return -ENOMEM;
 229         }
 230 
 231         data = iio_priv(indio_dev);
 232         data->client = client;
 233         i2c_set_clientdata(client, indio_dev);
 234         mutex_init(&data->lock);
 235 
 236         indio_dev->dev.parent = &client->dev;
 237         indio_dev->info = &am2315_info;
 238         indio_dev->name = AM2315_DRIVER_NAME;
 239         indio_dev->modes = INDIO_DIRECT_MODE;
 240         indio_dev->channels = am2315_channels;
 241         indio_dev->num_channels = ARRAY_SIZE(am2315_channels);
 242 
 243         ret = devm_iio_triggered_buffer_setup(&client->dev,
 244                                         indio_dev, iio_pollfunc_store_time,
 245                                          am2315_trigger_handler, NULL);
 246         if (ret < 0) {
 247                 dev_err(&client->dev, "iio triggered buffer setup failed\n");
 248                 return ret;
 249         }
 250 
 251         return devm_iio_device_register(&client->dev, indio_dev);
 252 }
 253 
 254 static const struct i2c_device_id am2315_i2c_id[] = {
 255         {"am2315", 0},
 256         {}
 257 };
 258 MODULE_DEVICE_TABLE(i2c, am2315_i2c_id);
 259 
 260 static const struct acpi_device_id am2315_acpi_id[] = {
 261         {"AOS2315", 0},
 262         {}
 263 };
 264 
 265 MODULE_DEVICE_TABLE(acpi, am2315_acpi_id);
 266 
 267 static struct i2c_driver am2315_driver = {
 268         .driver = {
 269                 .name = "am2315",
 270                 .acpi_match_table = ACPI_PTR(am2315_acpi_id),
 271         },
 272         .probe =            am2315_probe,
 273         .id_table =         am2315_i2c_id,
 274 };
 275 
 276 module_i2c_driver(am2315_driver);
 277 
 278 MODULE_AUTHOR("Tiberiu Breana <tiberiu.a.breana@intel.com>");
 279 MODULE_DESCRIPTION("Aosong AM2315 relative humidity and temperature");
 280 MODULE_LICENSE("GPL v2");

/* [<][>][^][v][top][bottom][index][help] */