Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Support for AMS AS73211 JENCOLOR(R) Digital XYZ Sensor
0004  *
0005  * Author: Christian Eggers <ceggers@arri.de>
0006  *
0007  * Copyright (c) 2020 ARRI Lighting
0008  *
0009  * Color light sensor with 16-bit channels for x, y, z and temperature);
0010  * 7-bit I2C slave address 0x74 .. 0x77.
0011  *
0012  * Datasheet: https://ams.com/documents/20143/36005/AS73211_DS000556_3-01.pdf
0013  */
0014 
0015 #include <linux/bitfield.h>
0016 #include <linux/completion.h>
0017 #include <linux/delay.h>
0018 #include <linux/i2c.h>
0019 #include <linux/iio/buffer.h>
0020 #include <linux/iio/iio.h>
0021 #include <linux/iio/sysfs.h>
0022 #include <linux/iio/trigger_consumer.h>
0023 #include <linux/iio/triggered_buffer.h>
0024 #include <linux/module.h>
0025 #include <linux/mutex.h>
0026 #include <linux/pm.h>
0027 #include <linux/units.h>
0028 
0029 #define AS73211_DRV_NAME "as73211"
0030 
0031 /* AS73211 configuration registers */
0032 #define AS73211_REG_OSR    0x0
0033 #define AS73211_REG_AGEN   0x2
0034 #define AS73211_REG_CREG1  0x6
0035 #define AS73211_REG_CREG2  0x7
0036 #define AS73211_REG_CREG3  0x8
0037 
0038 /* AS73211 output register bank */
0039 #define AS73211_OUT_OSR_STATUS    0
0040 #define AS73211_OUT_TEMP          1
0041 #define AS73211_OUT_MRES1         2
0042 #define AS73211_OUT_MRES2         3
0043 #define AS73211_OUT_MRES3         4
0044 
0045 #define AS73211_OSR_SS            BIT(7)
0046 #define AS73211_OSR_PD            BIT(6)
0047 #define AS73211_OSR_SW_RES        BIT(3)
0048 #define AS73211_OSR_DOS_MASK      GENMASK(2, 0)
0049 #define AS73211_OSR_DOS_CONFIG    FIELD_PREP(AS73211_OSR_DOS_MASK, 0x2)
0050 #define AS73211_OSR_DOS_MEASURE   FIELD_PREP(AS73211_OSR_DOS_MASK, 0x3)
0051 
0052 #define AS73211_AGEN_DEVID_MASK   GENMASK(7, 4)
0053 #define AS73211_AGEN_DEVID(x)     FIELD_PREP(AS73211_AGEN_DEVID_MASK, (x))
0054 #define AS73211_AGEN_MUT_MASK     GENMASK(3, 0)
0055 #define AS73211_AGEN_MUT(x)       FIELD_PREP(AS73211_AGEN_MUT_MASK, (x))
0056 
0057 #define AS73211_CREG1_GAIN_MASK   GENMASK(7, 4)
0058 #define AS73211_CREG1_GAIN_1      11
0059 #define AS73211_CREG1_TIME_MASK   GENMASK(3, 0)
0060 
0061 #define AS73211_CREG3_CCLK_MASK   GENMASK(1, 0)
0062 
0063 #define AS73211_OSR_STATUS_OUTCONVOF  BIT(15)
0064 #define AS73211_OSR_STATUS_MRESOF     BIT(14)
0065 #define AS73211_OSR_STATUS_ADCOF      BIT(13)
0066 #define AS73211_OSR_STATUS_LDATA      BIT(12)
0067 #define AS73211_OSR_STATUS_NDATA      BIT(11)
0068 #define AS73211_OSR_STATUS_NOTREADY   BIT(10)
0069 
0070 #define AS73211_SAMPLE_FREQ_BASE      1024000
0071 
0072 #define AS73211_SAMPLE_TIME_NUM       15
0073 #define AS73211_SAMPLE_TIME_MAX_MS    BIT(AS73211_SAMPLE_TIME_NUM - 1)
0074 
0075 /* Available sample frequencies are 1.024MHz multiplied by powers of two. */
0076 static const int as73211_samp_freq_avail[] = {
0077     AS73211_SAMPLE_FREQ_BASE * 1,
0078     AS73211_SAMPLE_FREQ_BASE * 2,
0079     AS73211_SAMPLE_FREQ_BASE * 4,
0080     AS73211_SAMPLE_FREQ_BASE * 8,
0081 };
0082 
0083 static const int as73211_hardwaregain_avail[] = {
0084     1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048,
0085 };
0086 
0087 /**
0088  * struct as73211_data - Instance data for one AS73211
0089  * @client: I2C client.
0090  * @osr:    Cached Operational State Register.
0091  * @creg1:  Cached Configuration Register 1.
0092  * @creg2:  Cached Configuration Register 2.
0093  * @creg3:  Cached Configuration Register 3.
0094  * @mutex:  Keeps cached registers in sync with the device.
0095  * @completion: Completion to wait for interrupt.
0096  * @int_time_avail: Available integration times (depend on sampling frequency).
0097  */
0098 struct as73211_data {
0099     struct i2c_client *client;
0100     u8 osr;
0101     u8 creg1;
0102     u8 creg2;
0103     u8 creg3;
0104     struct mutex mutex;
0105     struct completion completion;
0106     int int_time_avail[AS73211_SAMPLE_TIME_NUM * 2];
0107 };
0108 
0109 #define AS73211_COLOR_CHANNEL(_color, _si, _addr) { \
0110     .type = IIO_INTENSITY, \
0111     .modified = 1, \
0112     .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), \
0113     .info_mask_shared_by_type = \
0114         BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
0115         BIT(IIO_CHAN_INFO_HARDWAREGAIN) | \
0116         BIT(IIO_CHAN_INFO_INT_TIME), \
0117     .info_mask_shared_by_type_available = \
0118         BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
0119         BIT(IIO_CHAN_INFO_HARDWAREGAIN) | \
0120         BIT(IIO_CHAN_INFO_INT_TIME), \
0121     .channel2 = IIO_MOD_##_color, \
0122     .address = _addr, \
0123     .scan_index = _si, \
0124     .scan_type = { \
0125         .sign = 'u', \
0126         .realbits = 16, \
0127         .storagebits = 16, \
0128         .endianness = IIO_LE, \
0129     }, \
0130 }
0131 
0132 #define AS73211_OFFSET_TEMP_INT    (-66)
0133 #define AS73211_OFFSET_TEMP_MICRO  900000
0134 #define AS73211_SCALE_TEMP_INT     0
0135 #define AS73211_SCALE_TEMP_MICRO   50000
0136 
0137 #define AS73211_SCALE_X 277071108  /* nW/m^2 */
0138 #define AS73211_SCALE_Y 298384270  /* nW/m^2 */
0139 #define AS73211_SCALE_Z 160241927  /* nW/m^2 */
0140 
0141 /* Channel order MUST match devices result register order */
0142 #define AS73211_SCAN_INDEX_TEMP 0
0143 #define AS73211_SCAN_INDEX_X    1
0144 #define AS73211_SCAN_INDEX_Y    2
0145 #define AS73211_SCAN_INDEX_Z    3
0146 #define AS73211_SCAN_INDEX_TS   4
0147 
0148 #define AS73211_SCAN_MASK_COLOR ( \
0149     BIT(AS73211_SCAN_INDEX_X) |   \
0150     BIT(AS73211_SCAN_INDEX_Y) |   \
0151     BIT(AS73211_SCAN_INDEX_Z))
0152 
0153 #define AS73211_SCAN_MASK_ALL (    \
0154     BIT(AS73211_SCAN_INDEX_TEMP) | \
0155     AS73211_SCAN_MASK_COLOR)
0156 
0157 static const struct iio_chan_spec as73211_channels[] = {
0158     {
0159         .type = IIO_TEMP,
0160         .info_mask_separate =
0161             BIT(IIO_CHAN_INFO_RAW) |
0162             BIT(IIO_CHAN_INFO_OFFSET) |
0163             BIT(IIO_CHAN_INFO_SCALE),
0164         .address = AS73211_OUT_TEMP,
0165         .scan_index = AS73211_SCAN_INDEX_TEMP,
0166         .scan_type = {
0167             .sign = 'u',
0168             .realbits = 16,
0169             .storagebits = 16,
0170             .endianness = IIO_LE,
0171         }
0172     },
0173     AS73211_COLOR_CHANNEL(X, AS73211_SCAN_INDEX_X, AS73211_OUT_MRES1),
0174     AS73211_COLOR_CHANNEL(Y, AS73211_SCAN_INDEX_Y, AS73211_OUT_MRES2),
0175     AS73211_COLOR_CHANNEL(Z, AS73211_SCAN_INDEX_Z, AS73211_OUT_MRES3),
0176     IIO_CHAN_SOFT_TIMESTAMP(AS73211_SCAN_INDEX_TS),
0177 };
0178 
0179 static unsigned int as73211_integration_time_1024cyc(struct as73211_data *data)
0180 {
0181     /*
0182      * Return integration time in units of 1024 clock cycles. Integration time
0183      * in CREG1 is in powers of 2 (x 1024 cycles).
0184      */
0185     return BIT(FIELD_GET(AS73211_CREG1_TIME_MASK, data->creg1));
0186 }
0187 
0188 static unsigned int as73211_integration_time_us(struct as73211_data *data,
0189                          unsigned int integration_time_1024cyc)
0190 {
0191     /*
0192      * f_samp is configured in CREG3 in powers of 2 (x 1.024 MHz)
0193      * t_cycl is configured in CREG1 in powers of 2 (x 1024 cycles)
0194      * t_int_us = 1 / (f_samp) * t_cycl * US_PER_SEC
0195      *          = 1 / (2^CREG3_CCLK * 1,024,000) * 2^CREG1_CYCLES * 1,024 * US_PER_SEC
0196      *          = 2^(-CREG3_CCLK) * 2^CREG1_CYCLES * 1,000
0197      * In order to get rid of negative exponents, we extend the "fraction"
0198      * by 2^3 (CREG3_CCLK,max = 3)
0199      * t_int_us = 2^(3-CREG3_CCLK) * 2^CREG1_CYCLES * 125
0200      */
0201     return BIT(3 - FIELD_GET(AS73211_CREG3_CCLK_MASK, data->creg3)) *
0202         integration_time_1024cyc * 125;
0203 }
0204 
0205 static void as73211_integration_time_calc_avail(struct as73211_data *data)
0206 {
0207     int i;
0208 
0209     for (i = 0; i < ARRAY_SIZE(data->int_time_avail) / 2; i++) {
0210         unsigned int time_us = as73211_integration_time_us(data, BIT(i));
0211 
0212         data->int_time_avail[i * 2 + 0] = time_us / USEC_PER_SEC;
0213         data->int_time_avail[i * 2 + 1] = time_us % USEC_PER_SEC;
0214     }
0215 }
0216 
0217 static unsigned int as73211_gain(struct as73211_data *data)
0218 {
0219     /* gain can be calculated from CREG1 as 2^(11 - CREG1_GAIN) */
0220     return BIT(AS73211_CREG1_GAIN_1 - FIELD_GET(AS73211_CREG1_GAIN_MASK, data->creg1));
0221 }
0222 
0223 /* must be called with as73211_data::mutex held. */
0224 static int as73211_req_data(struct as73211_data *data)
0225 {
0226     unsigned int time_us = as73211_integration_time_us(data,
0227                                 as73211_integration_time_1024cyc(data));
0228     struct device *dev = &data->client->dev;
0229     union i2c_smbus_data smbus_data;
0230     u16 osr_status;
0231     int ret;
0232 
0233     if (data->client->irq)
0234         reinit_completion(&data->completion);
0235 
0236     /*
0237      * During measurement, there should be no traffic on the i2c bus as the
0238      * electrical noise would disturb the measurement process.
0239      */
0240     i2c_lock_bus(data->client->adapter, I2C_LOCK_SEGMENT);
0241 
0242     data->osr &= ~AS73211_OSR_DOS_MASK;
0243     data->osr |= AS73211_OSR_DOS_MEASURE | AS73211_OSR_SS;
0244 
0245     smbus_data.byte = data->osr;
0246     ret = __i2c_smbus_xfer(data->client->adapter, data->client->addr,
0247             data->client->flags, I2C_SMBUS_WRITE,
0248             AS73211_REG_OSR, I2C_SMBUS_BYTE_DATA, &smbus_data);
0249     if (ret < 0) {
0250         i2c_unlock_bus(data->client->adapter, I2C_LOCK_SEGMENT);
0251         return ret;
0252     }
0253 
0254     /*
0255      * Reset AS73211_OSR_SS (is self clearing) in order to avoid unintentional
0256      * triggering of further measurements later.
0257      */
0258     data->osr &= ~AS73211_OSR_SS;
0259 
0260     /*
0261      * Add 33% extra margin for the timeout. fclk,min = fclk,typ - 27%.
0262      */
0263     time_us += time_us / 3;
0264     if (data->client->irq) {
0265         ret = wait_for_completion_timeout(&data->completion, usecs_to_jiffies(time_us));
0266         if (!ret) {
0267             dev_err(dev, "timeout waiting for READY IRQ\n");
0268             i2c_unlock_bus(data->client->adapter, I2C_LOCK_SEGMENT);
0269             return -ETIMEDOUT;
0270         }
0271     } else {
0272         /* Wait integration time */
0273         usleep_range(time_us, 2 * time_us);
0274     }
0275 
0276     i2c_unlock_bus(data->client->adapter, I2C_LOCK_SEGMENT);
0277 
0278     ret = i2c_smbus_read_word_data(data->client, AS73211_OUT_OSR_STATUS);
0279     if (ret < 0)
0280         return ret;
0281 
0282     osr_status = ret;
0283     if (osr_status != (AS73211_OSR_DOS_MEASURE | AS73211_OSR_STATUS_NDATA)) {
0284         if (osr_status & AS73211_OSR_SS) {
0285             dev_err(dev, "%s() Measurement has not stopped\n", __func__);
0286             return -ETIME;
0287         }
0288         if (osr_status & AS73211_OSR_STATUS_NOTREADY) {
0289             dev_err(dev, "%s() Data is not ready\n", __func__);
0290             return -ENODATA;
0291         }
0292         if (!(osr_status & AS73211_OSR_STATUS_NDATA)) {
0293             dev_err(dev, "%s() No new data available\n", __func__);
0294             return -ENODATA;
0295         }
0296         if (osr_status & AS73211_OSR_STATUS_LDATA) {
0297             dev_err(dev, "%s() Result buffer overrun\n", __func__);
0298             return -ENOBUFS;
0299         }
0300         if (osr_status & AS73211_OSR_STATUS_ADCOF) {
0301             dev_err(dev, "%s() ADC overflow\n", __func__);
0302             return -EOVERFLOW;
0303         }
0304         if (osr_status & AS73211_OSR_STATUS_MRESOF) {
0305             dev_err(dev, "%s() Measurement result overflow\n", __func__);
0306             return -EOVERFLOW;
0307         }
0308         if (osr_status & AS73211_OSR_STATUS_OUTCONVOF) {
0309             dev_err(dev, "%s() Timer overflow\n", __func__);
0310             return -EOVERFLOW;
0311         }
0312         dev_err(dev, "%s() Unexpected status value\n", __func__);
0313         return -EIO;
0314     }
0315 
0316     return 0;
0317 }
0318 
0319 static int as73211_read_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan,
0320                  int *val, int *val2, long mask)
0321 {
0322     struct as73211_data *data = iio_priv(indio_dev);
0323 
0324     switch (mask) {
0325     case IIO_CHAN_INFO_RAW: {
0326         int ret;
0327 
0328         ret = iio_device_claim_direct_mode(indio_dev);
0329         if (ret < 0)
0330             return ret;
0331 
0332         ret = as73211_req_data(data);
0333         if (ret < 0) {
0334             iio_device_release_direct_mode(indio_dev);
0335             return ret;
0336         }
0337 
0338         ret = i2c_smbus_read_word_data(data->client, chan->address);
0339         iio_device_release_direct_mode(indio_dev);
0340         if (ret < 0)
0341             return ret;
0342 
0343         *val = ret;
0344         return IIO_VAL_INT;
0345     }
0346     case IIO_CHAN_INFO_OFFSET:
0347         *val = AS73211_OFFSET_TEMP_INT;
0348         *val2 = AS73211_OFFSET_TEMP_MICRO;
0349         return IIO_VAL_INT_PLUS_MICRO;
0350 
0351     case IIO_CHAN_INFO_SCALE:
0352         switch (chan->type) {
0353         case IIO_TEMP:
0354             *val = AS73211_SCALE_TEMP_INT;
0355             *val2 = AS73211_SCALE_TEMP_MICRO;
0356             return IIO_VAL_INT_PLUS_MICRO;
0357 
0358         case IIO_INTENSITY: {
0359             unsigned int scale;
0360 
0361             switch (chan->channel2) {
0362             case IIO_MOD_X:
0363                 scale = AS73211_SCALE_X;
0364                 break;
0365             case IIO_MOD_Y:
0366                 scale = AS73211_SCALE_Y;
0367                 break;
0368             case IIO_MOD_Z:
0369                 scale = AS73211_SCALE_Z;
0370                 break;
0371             default:
0372                 return -EINVAL;
0373             }
0374             scale /= as73211_gain(data);
0375             scale /= as73211_integration_time_1024cyc(data);
0376             *val = scale;
0377             return IIO_VAL_INT;
0378 
0379         default:
0380             return -EINVAL;
0381         }}
0382 
0383     case IIO_CHAN_INFO_SAMP_FREQ:
0384         /* f_samp is configured in CREG3 in powers of 2 (x 1.024 MHz) */
0385         *val = BIT(FIELD_GET(AS73211_CREG3_CCLK_MASK, data->creg3)) *
0386             AS73211_SAMPLE_FREQ_BASE;
0387         return IIO_VAL_INT;
0388 
0389     case IIO_CHAN_INFO_HARDWAREGAIN:
0390         *val = as73211_gain(data);
0391         return IIO_VAL_INT;
0392 
0393     case IIO_CHAN_INFO_INT_TIME: {
0394         unsigned int time_us;
0395 
0396         mutex_lock(&data->mutex);
0397         time_us = as73211_integration_time_us(data, as73211_integration_time_1024cyc(data));
0398         mutex_unlock(&data->mutex);
0399         *val = time_us / USEC_PER_SEC;
0400         *val2 = time_us % USEC_PER_SEC;
0401         return IIO_VAL_INT_PLUS_MICRO;
0402 
0403     default:
0404         return -EINVAL;
0405     }}
0406 }
0407 
0408 static int as73211_read_avail(struct iio_dev *indio_dev, struct iio_chan_spec const *chan,
0409                    const int **vals, int *type, int *length, long mask)
0410 {
0411     struct as73211_data *data = iio_priv(indio_dev);
0412 
0413     switch (mask) {
0414     case IIO_CHAN_INFO_SAMP_FREQ:
0415         *length = ARRAY_SIZE(as73211_samp_freq_avail);
0416         *vals = as73211_samp_freq_avail;
0417         *type = IIO_VAL_INT;
0418         return IIO_AVAIL_LIST;
0419 
0420     case IIO_CHAN_INFO_HARDWAREGAIN:
0421         *length = ARRAY_SIZE(as73211_hardwaregain_avail);
0422         *vals = as73211_hardwaregain_avail;
0423         *type = IIO_VAL_INT;
0424         return IIO_AVAIL_LIST;
0425 
0426     case IIO_CHAN_INFO_INT_TIME:
0427         *length = ARRAY_SIZE(data->int_time_avail);
0428         *vals = data->int_time_avail;
0429         *type = IIO_VAL_INT_PLUS_MICRO;
0430         return IIO_AVAIL_LIST;
0431 
0432     default:
0433         return -EINVAL;
0434     }
0435 }
0436 
0437 static int _as73211_write_raw(struct iio_dev *indio_dev,
0438                    struct iio_chan_spec const *chan __always_unused,
0439                    int val, int val2, long mask)
0440 {
0441     struct as73211_data *data = iio_priv(indio_dev);
0442     int ret;
0443 
0444     switch (mask) {
0445     case IIO_CHAN_INFO_SAMP_FREQ: {
0446         int reg_bits, freq_kHz = val / HZ_PER_KHZ;  /* 1024, 2048, ... */
0447 
0448         /* val must be 1024 * 2^x */
0449         if (val < 0 || (freq_kHz * HZ_PER_KHZ) != val ||
0450                 !is_power_of_2(freq_kHz) || val2)
0451             return -EINVAL;
0452 
0453         /* f_samp is configured in CREG3 in powers of 2 (x 1.024 MHz (=2^10)) */
0454         reg_bits = ilog2(freq_kHz) - 10;
0455         if (!FIELD_FIT(AS73211_CREG3_CCLK_MASK, reg_bits))
0456             return -EINVAL;
0457 
0458         data->creg3 &= ~AS73211_CREG3_CCLK_MASK;
0459         data->creg3 |= FIELD_PREP(AS73211_CREG3_CCLK_MASK, reg_bits);
0460         as73211_integration_time_calc_avail(data);
0461 
0462         ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_CREG3, data->creg3);
0463         if (ret < 0)
0464             return ret;
0465 
0466         return 0;
0467     }
0468     case IIO_CHAN_INFO_HARDWAREGAIN: {
0469         unsigned int reg_bits;
0470 
0471         if (val < 0 || !is_power_of_2(val) || val2)
0472             return -EINVAL;
0473 
0474         /* gain can be calculated from CREG1 as 2^(11 - CREG1_GAIN) */
0475         reg_bits = AS73211_CREG1_GAIN_1 - ilog2(val);
0476         if (!FIELD_FIT(AS73211_CREG1_GAIN_MASK, reg_bits))
0477             return -EINVAL;
0478 
0479         data->creg1 &= ~AS73211_CREG1_GAIN_MASK;
0480         data->creg1 |= FIELD_PREP(AS73211_CREG1_GAIN_MASK, reg_bits);
0481 
0482         ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_CREG1, data->creg1);
0483         if (ret < 0)
0484             return ret;
0485 
0486         return 0;
0487     }
0488     case IIO_CHAN_INFO_INT_TIME: {
0489         int val_us = val * USEC_PER_SEC + val2;
0490         int time_ms;
0491         int reg_bits;
0492 
0493         /* f_samp is configured in CREG3 in powers of 2 (x 1.024 MHz) */
0494         int f_samp_1_024mhz = BIT(FIELD_GET(AS73211_CREG3_CCLK_MASK, data->creg3));
0495 
0496         /*
0497          * time_ms = time_us * US_PER_MS * f_samp_1_024mhz / MHZ_PER_HZ
0498          *         = time_us * f_samp_1_024mhz / 1000
0499          */
0500         time_ms = (val_us * f_samp_1_024mhz) / 1000;  /* 1 ms, 2 ms, ... (power of two) */
0501         if (time_ms < 0 || !is_power_of_2(time_ms) || time_ms > AS73211_SAMPLE_TIME_MAX_MS)
0502             return -EINVAL;
0503 
0504         reg_bits = ilog2(time_ms);
0505         if (!FIELD_FIT(AS73211_CREG1_TIME_MASK, reg_bits))
0506             return -EINVAL;  /* not possible due to previous tests */
0507 
0508         data->creg1 &= ~AS73211_CREG1_TIME_MASK;
0509         data->creg1 |= FIELD_PREP(AS73211_CREG1_TIME_MASK, reg_bits);
0510 
0511         ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_CREG1, data->creg1);
0512         if (ret < 0)
0513             return ret;
0514 
0515         return 0;
0516 
0517     default:
0518         return -EINVAL;
0519     }}
0520 }
0521 
0522 static int as73211_write_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan,
0523                   int val, int val2, long mask)
0524 {
0525     struct as73211_data *data = iio_priv(indio_dev);
0526     int ret;
0527 
0528     mutex_lock(&data->mutex);
0529 
0530     ret = iio_device_claim_direct_mode(indio_dev);
0531     if (ret < 0)
0532         goto error_unlock;
0533 
0534     /* Need to switch to config mode ... */
0535     if ((data->osr & AS73211_OSR_DOS_MASK) != AS73211_OSR_DOS_CONFIG) {
0536         data->osr &= ~AS73211_OSR_DOS_MASK;
0537         data->osr |= AS73211_OSR_DOS_CONFIG;
0538 
0539         ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_OSR, data->osr);
0540         if (ret < 0)
0541             goto error_release;
0542     }
0543 
0544     ret = _as73211_write_raw(indio_dev, chan, val, val2, mask);
0545 
0546 error_release:
0547     iio_device_release_direct_mode(indio_dev);
0548 error_unlock:
0549     mutex_unlock(&data->mutex);
0550     return ret;
0551 }
0552 
0553 static irqreturn_t as73211_ready_handler(int irq __always_unused, void *priv)
0554 {
0555     struct as73211_data *data = iio_priv(priv);
0556 
0557     complete(&data->completion);
0558 
0559     return IRQ_HANDLED;
0560 }
0561 
0562 static irqreturn_t as73211_trigger_handler(int irq __always_unused, void *p)
0563 {
0564     struct iio_poll_func *pf = p;
0565     struct iio_dev *indio_dev = pf->indio_dev;
0566     struct as73211_data *data = iio_priv(indio_dev);
0567     struct {
0568         __le16 chan[4];
0569         s64 ts __aligned(8);
0570     } scan;
0571     int data_result, ret;
0572 
0573     mutex_lock(&data->mutex);
0574 
0575     data_result = as73211_req_data(data);
0576     if (data_result < 0 && data_result != -EOVERFLOW)
0577         goto done;  /* don't push any data for errors other than EOVERFLOW */
0578 
0579     if (*indio_dev->active_scan_mask == AS73211_SCAN_MASK_ALL) {
0580         /* Optimization for reading all (color + temperature) channels */
0581         u8 addr = as73211_channels[0].address;
0582         struct i2c_msg msgs[] = {
0583             {
0584                 .addr = data->client->addr,
0585                 .flags = 0,
0586                 .len = 1,
0587                 .buf = &addr,
0588             },
0589             {
0590                 .addr = data->client->addr,
0591                 .flags = I2C_M_RD,
0592                 .len = sizeof(scan.chan),
0593                 .buf = (u8 *)&scan.chan,
0594             },
0595         };
0596 
0597         ret = i2c_transfer(data->client->adapter, msgs, ARRAY_SIZE(msgs));
0598         if (ret < 0)
0599             goto done;
0600     } else {
0601         /* Optimization for reading only color channels */
0602 
0603         /* AS73211 starts reading at address 2 */
0604         ret = i2c_master_recv(data->client,
0605                 (char *)&scan.chan[1], 3 * sizeof(scan.chan[1]));
0606         if (ret < 0)
0607             goto done;
0608     }
0609 
0610     if (data_result) {
0611         /*
0612          * Saturate all channels (in case of overflows). Temperature channel
0613          * is not affected by overflows.
0614          */
0615         scan.chan[1] = cpu_to_le16(U16_MAX);
0616         scan.chan[2] = cpu_to_le16(U16_MAX);
0617         scan.chan[3] = cpu_to_le16(U16_MAX);
0618     }
0619 
0620     iio_push_to_buffers_with_timestamp(indio_dev, &scan, iio_get_time_ns(indio_dev));
0621 
0622 done:
0623     mutex_unlock(&data->mutex);
0624     iio_trigger_notify_done(indio_dev->trig);
0625 
0626     return IRQ_HANDLED;
0627 }
0628 
0629 static const struct iio_info as73211_info = {
0630     .read_raw = as73211_read_raw,
0631     .read_avail = as73211_read_avail,
0632     .write_raw = as73211_write_raw,
0633 };
0634 
0635 static int as73211_power(struct iio_dev *indio_dev, bool state)
0636 {
0637     struct as73211_data *data = iio_priv(indio_dev);
0638     int ret;
0639 
0640     mutex_lock(&data->mutex);
0641 
0642     if (state)
0643         data->osr &= ~AS73211_OSR_PD;
0644     else
0645         data->osr |= AS73211_OSR_PD;
0646 
0647     ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_OSR, data->osr);
0648 
0649     mutex_unlock(&data->mutex);
0650 
0651     if (ret < 0)
0652         return ret;
0653 
0654     return 0;
0655 }
0656 
0657 static void as73211_power_disable(void *data)
0658 {
0659     struct iio_dev *indio_dev = data;
0660 
0661     as73211_power(indio_dev, false);
0662 }
0663 
0664 static int as73211_probe(struct i2c_client *client)
0665 {
0666     struct device *dev = &client->dev;
0667     struct as73211_data *data;
0668     struct iio_dev *indio_dev;
0669     int ret;
0670 
0671     indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
0672     if (!indio_dev)
0673         return -ENOMEM;
0674 
0675     data = iio_priv(indio_dev);
0676     i2c_set_clientdata(client, indio_dev);
0677     data->client = client;
0678 
0679     mutex_init(&data->mutex);
0680     init_completion(&data->completion);
0681 
0682     indio_dev->info = &as73211_info;
0683     indio_dev->name = AS73211_DRV_NAME;
0684     indio_dev->channels = as73211_channels;
0685     indio_dev->num_channels = ARRAY_SIZE(as73211_channels);
0686     indio_dev->modes = INDIO_DIRECT_MODE;
0687 
0688     ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_OSR);
0689     if (ret < 0)
0690         return ret;
0691     data->osr = ret;
0692 
0693     /* reset device */
0694     data->osr |= AS73211_OSR_SW_RES;
0695     ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_OSR, data->osr);
0696     if (ret < 0)
0697         return ret;
0698 
0699     ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_OSR);
0700     if (ret < 0)
0701         return ret;
0702     data->osr = ret;
0703 
0704     /*
0705      * Reading AGEN is only possible after reset (AGEN is not available if
0706      * device is in measurement mode).
0707      */
0708     ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_AGEN);
0709     if (ret < 0)
0710         return ret;
0711 
0712     /* At the time of writing this driver, only DEVID 2 and MUT 1 are known. */
0713     if ((ret & AS73211_AGEN_DEVID_MASK) != AS73211_AGEN_DEVID(2) ||
0714         (ret & AS73211_AGEN_MUT_MASK) != AS73211_AGEN_MUT(1))
0715         return -ENODEV;
0716 
0717     ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_CREG1);
0718     if (ret < 0)
0719         return ret;
0720     data->creg1 = ret;
0721 
0722     ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_CREG2);
0723     if (ret < 0)
0724         return ret;
0725     data->creg2 = ret;
0726 
0727     ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_CREG3);
0728     if (ret < 0)
0729         return ret;
0730     data->creg3 = ret;
0731     as73211_integration_time_calc_avail(data);
0732 
0733     ret = as73211_power(indio_dev, true);
0734     if (ret < 0)
0735         return ret;
0736 
0737     ret = devm_add_action_or_reset(dev, as73211_power_disable, indio_dev);
0738     if (ret)
0739         return ret;
0740 
0741     ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL, as73211_trigger_handler, NULL);
0742     if (ret)
0743         return ret;
0744 
0745     if (client->irq) {
0746         ret = devm_request_threaded_irq(&client->dev, client->irq,
0747                 NULL,
0748                 as73211_ready_handler,
0749                 IRQF_ONESHOT,
0750                 client->name, indio_dev);
0751         if (ret)
0752             return ret;
0753     }
0754 
0755     return devm_iio_device_register(dev, indio_dev);
0756 }
0757 
0758 static int as73211_suspend(struct device *dev)
0759 {
0760     struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
0761 
0762     return as73211_power(indio_dev, false);
0763 }
0764 
0765 static int as73211_resume(struct device *dev)
0766 {
0767     struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
0768 
0769     return as73211_power(indio_dev, true);
0770 }
0771 
0772 static DEFINE_SIMPLE_DEV_PM_OPS(as73211_pm_ops, as73211_suspend,
0773                 as73211_resume);
0774 
0775 static const struct of_device_id as73211_of_match[] = {
0776     { .compatible = "ams,as73211" },
0777     { }
0778 };
0779 MODULE_DEVICE_TABLE(of, as73211_of_match);
0780 
0781 static const struct i2c_device_id as73211_id[] = {
0782     { "as73211", 0 },
0783     { }
0784 };
0785 MODULE_DEVICE_TABLE(i2c, as73211_id);
0786 
0787 static struct i2c_driver as73211_driver = {
0788     .driver = {
0789         .name           = AS73211_DRV_NAME,
0790         .of_match_table = as73211_of_match,
0791         .pm             = pm_sleep_ptr(&as73211_pm_ops),
0792     },
0793     .probe_new  = as73211_probe,
0794     .id_table   = as73211_id,
0795 };
0796 module_i2c_driver(as73211_driver);
0797 
0798 MODULE_AUTHOR("Christian Eggers <ceggers@arri.de>");
0799 MODULE_DESCRIPTION("AS73211 XYZ True Color Sensor driver");
0800 MODULE_LICENSE("GPL");