1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Support for AMS AS73211 JENCOLOR(R) Digital XYZ Sensor 4 * 5 * Author: Christian Eggers <ceggers@arri.de> 6 * 7 * Copyright (c) 2020 ARRI Lighting 8 * 9 * Color light sensor with 16-bit channels for x, y, z and temperature); 10 * 7-bit I2C slave address 0x74 .. 0x77. 11 * 12 * Datasheet: https://ams.com/documents/20143/36005/AS73211_DS000556_3-01.pdf 13 */ 14 15 #include <linux/bitfield.h> 16 #include <linux/completion.h> 17 #include <linux/delay.h> 18 #include <linux/i2c.h> 19 #include <linux/iio/buffer.h> 20 #include <linux/iio/iio.h> 21 #include <linux/iio/sysfs.h> 22 #include <linux/iio/trigger_consumer.h> 23 #include <linux/iio/triggered_buffer.h> 24 #include <linux/module.h> 25 #include <linux/mutex.h> 26 #include <linux/pm.h> 27 28 #define HZ_PER_KHZ 1000 29 30 #define AS73211_DRV_NAME "as73211" 31 32 /* AS73211 configuration registers */ 33 #define AS73211_REG_OSR 0x0 34 #define AS73211_REG_AGEN 0x2 35 #define AS73211_REG_CREG1 0x6 36 #define AS73211_REG_CREG2 0x7 37 #define AS73211_REG_CREG3 0x8 38 39 /* AS73211 output register bank */ 40 #define AS73211_OUT_OSR_STATUS 0 41 #define AS73211_OUT_TEMP 1 42 #define AS73211_OUT_MRES1 2 43 #define AS73211_OUT_MRES2 3 44 #define AS73211_OUT_MRES3 4 45 46 #define AS73211_OSR_SS BIT(7) 47 #define AS73211_OSR_PD BIT(6) 48 #define AS73211_OSR_SW_RES BIT(3) 49 #define AS73211_OSR_DOS_MASK GENMASK(2, 0) 50 #define AS73211_OSR_DOS_CONFIG FIELD_PREP(AS73211_OSR_DOS_MASK, 0x2) 51 #define AS73211_OSR_DOS_MEASURE FIELD_PREP(AS73211_OSR_DOS_MASK, 0x3) 52 53 #define AS73211_AGEN_DEVID_MASK GENMASK(7, 4) 54 #define AS73211_AGEN_DEVID(x) FIELD_PREP(AS73211_AGEN_DEVID_MASK, (x)) 55 #define AS73211_AGEN_MUT_MASK GENMASK(3, 0) 56 #define AS73211_AGEN_MUT(x) FIELD_PREP(AS73211_AGEN_MUT_MASK, (x)) 57 58 #define AS73211_CREG1_GAIN_MASK GENMASK(7, 4) 59 #define AS73211_CREG1_GAIN_1 11 60 #define AS73211_CREG1_TIME_MASK GENMASK(3, 0) 61 62 #define AS73211_CREG3_CCLK_MASK GENMASK(1, 0) 63 64 #define AS73211_OSR_STATUS_OUTCONVOF BIT(15) 65 #define AS73211_OSR_STATUS_MRESOF BIT(14) 66 #define AS73211_OSR_STATUS_ADCOF BIT(13) 67 #define AS73211_OSR_STATUS_LDATA BIT(12) 68 #define AS73211_OSR_STATUS_NDATA BIT(11) 69 #define AS73211_OSR_STATUS_NOTREADY BIT(10) 70 71 #define AS73211_SAMPLE_FREQ_BASE 1024000 72 73 #define AS73211_SAMPLE_TIME_NUM 15 74 #define AS73211_SAMPLE_TIME_MAX_MS BIT(AS73211_SAMPLE_TIME_NUM - 1) 75 76 /* Available sample frequencies are 1.024MHz multiplied by powers of two. */ 77 static const int as73211_samp_freq_avail[] = { 78 AS73211_SAMPLE_FREQ_BASE * 1, 79 AS73211_SAMPLE_FREQ_BASE * 2, 80 AS73211_SAMPLE_FREQ_BASE * 4, 81 AS73211_SAMPLE_FREQ_BASE * 8, 82 }; 83 84 static const int as73211_hardwaregain_avail[] = { 85 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 86 }; 87 88 /** 89 * struct as73211_data - Instance data for one AS73211 90 * @client: I2C client. 91 * @osr: Cached Operational State Register. 92 * @creg1: Cached Configuration Register 1. 93 * @creg2: Cached Configuration Register 2. 94 * @creg3: Cached Configuration Register 3. 95 * @mutex: Keeps cached registers in sync with the device. 96 * @completion: Completion to wait for interrupt. 97 * @int_time_avail: Available integration times (depend on sampling frequency). 98 */ 99 struct as73211_data { 100 struct i2c_client *client; 101 u8 osr; 102 u8 creg1; 103 u8 creg2; 104 u8 creg3; 105 struct mutex mutex; 106 struct completion completion; 107 int int_time_avail[AS73211_SAMPLE_TIME_NUM * 2]; 108 }; 109 110 #define AS73211_COLOR_CHANNEL(_color, _si, _addr) { \ 111 .type = IIO_INTENSITY, \ 112 .modified = 1, \ 113 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), \ 114 .info_mask_shared_by_type = \ 115 BIT(IIO_CHAN_INFO_SAMP_FREQ) | \ 116 BIT(IIO_CHAN_INFO_HARDWAREGAIN) | \ 117 BIT(IIO_CHAN_INFO_INT_TIME), \ 118 .info_mask_shared_by_type_available = \ 119 BIT(IIO_CHAN_INFO_SAMP_FREQ) | \ 120 BIT(IIO_CHAN_INFO_HARDWAREGAIN) | \ 121 BIT(IIO_CHAN_INFO_INT_TIME), \ 122 .channel2 = IIO_MOD_##_color, \ 123 .address = _addr, \ 124 .scan_index = _si, \ 125 .scan_type = { \ 126 .sign = 'u', \ 127 .realbits = 16, \ 128 .storagebits = 16, \ 129 .endianness = IIO_LE, \ 130 }, \ 131 } 132 133 #define AS73211_OFFSET_TEMP_INT (-66) 134 #define AS73211_OFFSET_TEMP_MICRO 900000 135 #define AS73211_SCALE_TEMP_INT 0 136 #define AS73211_SCALE_TEMP_MICRO 50000 137 138 #define AS73211_SCALE_X 277071108 /* nW/m^2 */ 139 #define AS73211_SCALE_Y 298384270 /* nW/m^2 */ 140 #define AS73211_SCALE_Z 160241927 /* nW/m^2 */ 141 142 /* Channel order MUST match devices result register order */ 143 #define AS73211_SCAN_INDEX_TEMP 0 144 #define AS73211_SCAN_INDEX_X 1 145 #define AS73211_SCAN_INDEX_Y 2 146 #define AS73211_SCAN_INDEX_Z 3 147 #define AS73211_SCAN_INDEX_TS 4 148 149 #define AS73211_SCAN_MASK_COLOR ( \ 150 BIT(AS73211_SCAN_INDEX_X) | \ 151 BIT(AS73211_SCAN_INDEX_Y) | \ 152 BIT(AS73211_SCAN_INDEX_Z)) 153 154 #define AS73211_SCAN_MASK_ALL ( \ 155 BIT(AS73211_SCAN_INDEX_TEMP) | \ 156 AS73211_SCAN_MASK_COLOR) 157 158 static const struct iio_chan_spec as73211_channels[] = { 159 { 160 .type = IIO_TEMP, 161 .info_mask_separate = 162 BIT(IIO_CHAN_INFO_RAW) | 163 BIT(IIO_CHAN_INFO_OFFSET) | 164 BIT(IIO_CHAN_INFO_SCALE), 165 .address = AS73211_OUT_TEMP, 166 .scan_index = AS73211_SCAN_INDEX_TEMP, 167 .scan_type = { 168 .sign = 'u', 169 .realbits = 16, 170 .storagebits = 16, 171 .endianness = IIO_LE, 172 } 173 }, 174 AS73211_COLOR_CHANNEL(X, AS73211_SCAN_INDEX_X, AS73211_OUT_MRES1), 175 AS73211_COLOR_CHANNEL(Y, AS73211_SCAN_INDEX_Y, AS73211_OUT_MRES2), 176 AS73211_COLOR_CHANNEL(Z, AS73211_SCAN_INDEX_Z, AS73211_OUT_MRES3), 177 IIO_CHAN_SOFT_TIMESTAMP(AS73211_SCAN_INDEX_TS), 178 }; 179 180 static unsigned int as73211_integration_time_1024cyc(struct as73211_data *data) 181 { 182 /* 183 * Return integration time in units of 1024 clock cycles. Integration time 184 * in CREG1 is in powers of 2 (x 1024 cycles). 185 */ 186 return BIT(FIELD_GET(AS73211_CREG1_TIME_MASK, data->creg1)); 187 } 188 189 static unsigned int as73211_integration_time_us(struct as73211_data *data, 190 unsigned int integration_time_1024cyc) 191 { 192 /* 193 * f_samp is configured in CREG3 in powers of 2 (x 1.024 MHz) 194 * t_cycl is configured in CREG1 in powers of 2 (x 1024 cycles) 195 * t_int_us = 1 / (f_samp) * t_cycl * US_PER_SEC 196 * = 1 / (2^CREG3_CCLK * 1,024,000) * 2^CREG1_CYCLES * 1,024 * US_PER_SEC 197 * = 2^(-CREG3_CCLK) * 2^CREG1_CYCLES * 1,000 198 * In order to get rid of negative exponents, we extend the "fraction" 199 * by 2^3 (CREG3_CCLK,max = 3) 200 * t_int_us = 2^(3-CREG3_CCLK) * 2^CREG1_CYCLES * 125 201 */ 202 return BIT(3 - FIELD_GET(AS73211_CREG3_CCLK_MASK, data->creg3)) * 203 integration_time_1024cyc * 125; 204 } 205 206 static void as73211_integration_time_calc_avail(struct as73211_data *data) 207 { 208 int i; 209 210 for (i = 0; i < ARRAY_SIZE(data->int_time_avail) / 2; i++) { 211 unsigned int time_us = as73211_integration_time_us(data, BIT(i)); 212 213 data->int_time_avail[i * 2 + 0] = time_us / USEC_PER_SEC; 214 data->int_time_avail[i * 2 + 1] = time_us % USEC_PER_SEC; 215 } 216 } 217 218 static unsigned int as73211_gain(struct as73211_data *data) 219 { 220 /* gain can be calculated from CREG1 as 2^(11 - CREG1_GAIN) */ 221 return BIT(AS73211_CREG1_GAIN_1 - FIELD_GET(AS73211_CREG1_GAIN_MASK, data->creg1)); 222 } 223 224 /* must be called with as73211_data::mutex held. */ 225 static int as73211_req_data(struct as73211_data *data) 226 { 227 unsigned int time_us = as73211_integration_time_us(data, 228 as73211_integration_time_1024cyc(data)); 229 struct device *dev = &data->client->dev; 230 union i2c_smbus_data smbus_data; 231 u16 osr_status; 232 int ret; 233 234 if (data->client->irq) 235 reinit_completion(&data->completion); 236 237 /* 238 * During measurement, there should be no traffic on the i2c bus as the 239 * electrical noise would disturb the measurement process. 240 */ 241 i2c_lock_bus(data->client->adapter, I2C_LOCK_SEGMENT); 242 243 data->osr &= ~AS73211_OSR_DOS_MASK; 244 data->osr |= AS73211_OSR_DOS_MEASURE | AS73211_OSR_SS; 245 246 smbus_data.byte = data->osr; 247 ret = __i2c_smbus_xfer(data->client->adapter, data->client->addr, 248 data->client->flags, I2C_SMBUS_WRITE, 249 AS73211_REG_OSR, I2C_SMBUS_BYTE_DATA, &smbus_data); 250 if (ret < 0) { 251 i2c_unlock_bus(data->client->adapter, I2C_LOCK_SEGMENT); 252 return ret; 253 } 254 255 /* 256 * Reset AS73211_OSR_SS (is self clearing) in order to avoid unintentional 257 * triggering of further measurements later. 258 */ 259 data->osr &= ~AS73211_OSR_SS; 260 261 /* 262 * Add 33% extra margin for the timeout. fclk,min = fclk,typ - 27%. 263 */ 264 time_us += time_us / 3; 265 if (data->client->irq) { 266 ret = wait_for_completion_timeout(&data->completion, usecs_to_jiffies(time_us)); 267 if (!ret) { 268 dev_err(dev, "timeout waiting for READY IRQ\n"); 269 i2c_unlock_bus(data->client->adapter, I2C_LOCK_SEGMENT); 270 return -ETIMEDOUT; 271 } 272 } else { 273 /* Wait integration time */ 274 usleep_range(time_us, 2 * time_us); 275 } 276 277 i2c_unlock_bus(data->client->adapter, I2C_LOCK_SEGMENT); 278 279 ret = i2c_smbus_read_word_data(data->client, AS73211_OUT_OSR_STATUS); 280 if (ret < 0) 281 return ret; 282 283 osr_status = ret; 284 if (osr_status != (AS73211_OSR_DOS_MEASURE | AS73211_OSR_STATUS_NDATA)) { 285 if (osr_status & AS73211_OSR_SS) { 286 dev_err(dev, "%s() Measurement has not stopped\n", __func__); 287 return -ETIME; 288 } 289 if (osr_status & AS73211_OSR_STATUS_NOTREADY) { 290 dev_err(dev, "%s() Data is not ready\n", __func__); 291 return -ENODATA; 292 } 293 if (!(osr_status & AS73211_OSR_STATUS_NDATA)) { 294 dev_err(dev, "%s() No new data available\n", __func__); 295 return -ENODATA; 296 } 297 if (osr_status & AS73211_OSR_STATUS_LDATA) { 298 dev_err(dev, "%s() Result buffer overrun\n", __func__); 299 return -ENOBUFS; 300 } 301 if (osr_status & AS73211_OSR_STATUS_ADCOF) { 302 dev_err(dev, "%s() ADC overflow\n", __func__); 303 return -EOVERFLOW; 304 } 305 if (osr_status & AS73211_OSR_STATUS_MRESOF) { 306 dev_err(dev, "%s() Measurement result overflow\n", __func__); 307 return -EOVERFLOW; 308 } 309 if (osr_status & AS73211_OSR_STATUS_OUTCONVOF) { 310 dev_err(dev, "%s() Timer overflow\n", __func__); 311 return -EOVERFLOW; 312 } 313 dev_err(dev, "%s() Unexpected status value\n", __func__); 314 return -EIO; 315 } 316 317 return 0; 318 } 319 320 static int as73211_read_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan, 321 int *val, int *val2, long mask) 322 { 323 struct as73211_data *data = iio_priv(indio_dev); 324 325 switch (mask) { 326 case IIO_CHAN_INFO_RAW: { 327 int ret; 328 329 ret = iio_device_claim_direct_mode(indio_dev); 330 if (ret < 0) 331 return ret; 332 333 ret = as73211_req_data(data); 334 if (ret < 0) { 335 iio_device_release_direct_mode(indio_dev); 336 return ret; 337 } 338 339 ret = i2c_smbus_read_word_data(data->client, chan->address); 340 iio_device_release_direct_mode(indio_dev); 341 if (ret < 0) 342 return ret; 343 344 *val = ret; 345 return IIO_VAL_INT; 346 } 347 case IIO_CHAN_INFO_OFFSET: 348 *val = AS73211_OFFSET_TEMP_INT; 349 *val2 = AS73211_OFFSET_TEMP_MICRO; 350 return IIO_VAL_INT_PLUS_MICRO; 351 352 case IIO_CHAN_INFO_SCALE: 353 switch (chan->type) { 354 case IIO_TEMP: 355 *val = AS73211_SCALE_TEMP_INT; 356 *val2 = AS73211_SCALE_TEMP_MICRO; 357 return IIO_VAL_INT_PLUS_MICRO; 358 359 case IIO_INTENSITY: { 360 unsigned int scale; 361 362 switch (chan->channel2) { 363 case IIO_MOD_X: 364 scale = AS73211_SCALE_X; 365 break; 366 case IIO_MOD_Y: 367 scale = AS73211_SCALE_Y; 368 break; 369 case IIO_MOD_Z: 370 scale = AS73211_SCALE_Z; 371 break; 372 default: 373 return -EINVAL; 374 } 375 scale /= as73211_gain(data); 376 scale /= as73211_integration_time_1024cyc(data); 377 *val = scale; 378 return IIO_VAL_INT; 379 380 default: 381 return -EINVAL; 382 }} 383 384 case IIO_CHAN_INFO_SAMP_FREQ: 385 /* f_samp is configured in CREG3 in powers of 2 (x 1.024 MHz) */ 386 *val = BIT(FIELD_GET(AS73211_CREG3_CCLK_MASK, data->creg3)) * 387 AS73211_SAMPLE_FREQ_BASE; 388 return IIO_VAL_INT; 389 390 case IIO_CHAN_INFO_HARDWAREGAIN: 391 *val = as73211_gain(data); 392 return IIO_VAL_INT; 393 394 case IIO_CHAN_INFO_INT_TIME: { 395 unsigned int time_us; 396 397 mutex_lock(&data->mutex); 398 time_us = as73211_integration_time_us(data, as73211_integration_time_1024cyc(data)); 399 mutex_unlock(&data->mutex); 400 *val = time_us / USEC_PER_SEC; 401 *val2 = time_us % USEC_PER_SEC; 402 return IIO_VAL_INT_PLUS_MICRO; 403 404 default: 405 return -EINVAL; 406 }} 407 } 408 409 static int as73211_read_avail(struct iio_dev *indio_dev, struct iio_chan_spec const *chan, 410 const int **vals, int *type, int *length, long mask) 411 { 412 struct as73211_data *data = iio_priv(indio_dev); 413 414 switch (mask) { 415 case IIO_CHAN_INFO_SAMP_FREQ: 416 *length = ARRAY_SIZE(as73211_samp_freq_avail); 417 *vals = as73211_samp_freq_avail; 418 *type = IIO_VAL_INT; 419 return IIO_AVAIL_LIST; 420 421 case IIO_CHAN_INFO_HARDWAREGAIN: 422 *length = ARRAY_SIZE(as73211_hardwaregain_avail); 423 *vals = as73211_hardwaregain_avail; 424 *type = IIO_VAL_INT; 425 return IIO_AVAIL_LIST; 426 427 case IIO_CHAN_INFO_INT_TIME: 428 *length = ARRAY_SIZE(data->int_time_avail); 429 *vals = data->int_time_avail; 430 *type = IIO_VAL_INT_PLUS_MICRO; 431 return IIO_AVAIL_LIST; 432 433 default: 434 return -EINVAL; 435 } 436 } 437 438 static int _as73211_write_raw(struct iio_dev *indio_dev, 439 struct iio_chan_spec const *chan __always_unused, 440 int val, int val2, long mask) 441 { 442 struct as73211_data *data = iio_priv(indio_dev); 443 int ret; 444 445 switch (mask) { 446 case IIO_CHAN_INFO_SAMP_FREQ: { 447 int reg_bits, freq_kHz = val / HZ_PER_KHZ; /* 1024, 2048, ... */ 448 449 /* val must be 1024 * 2^x */ 450 if (val < 0 || (freq_kHz * HZ_PER_KHZ) != val || 451 !is_power_of_2(freq_kHz) || val2) 452 return -EINVAL; 453 454 /* f_samp is configured in CREG3 in powers of 2 (x 1.024 MHz (=2^10)) */ 455 reg_bits = ilog2(freq_kHz) - 10; 456 if (!FIELD_FIT(AS73211_CREG3_CCLK_MASK, reg_bits)) 457 return -EINVAL; 458 459 data->creg3 &= ~AS73211_CREG3_CCLK_MASK; 460 data->creg3 |= FIELD_PREP(AS73211_CREG3_CCLK_MASK, reg_bits); 461 as73211_integration_time_calc_avail(data); 462 463 ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_CREG3, data->creg3); 464 if (ret < 0) 465 return ret; 466 467 return 0; 468 } 469 case IIO_CHAN_INFO_HARDWAREGAIN: { 470 unsigned int reg_bits; 471 472 if (val < 0 || !is_power_of_2(val) || val2) 473 return -EINVAL; 474 475 /* gain can be calculated from CREG1 as 2^(11 - CREG1_GAIN) */ 476 reg_bits = AS73211_CREG1_GAIN_1 - ilog2(val); 477 if (!FIELD_FIT(AS73211_CREG1_GAIN_MASK, reg_bits)) 478 return -EINVAL; 479 480 data->creg1 &= ~AS73211_CREG1_GAIN_MASK; 481 data->creg1 |= FIELD_PREP(AS73211_CREG1_GAIN_MASK, reg_bits); 482 483 ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_CREG1, data->creg1); 484 if (ret < 0) 485 return ret; 486 487 return 0; 488 } 489 case IIO_CHAN_INFO_INT_TIME: { 490 int val_us = val * USEC_PER_SEC + val2; 491 int time_ms; 492 int reg_bits; 493 494 /* f_samp is configured in CREG3 in powers of 2 (x 1.024 MHz) */ 495 int f_samp_1_024mhz = BIT(FIELD_GET(AS73211_CREG3_CCLK_MASK, data->creg3)); 496 497 /* 498 * time_ms = time_us * US_PER_MS * f_samp_1_024mhz / MHZ_PER_HZ 499 * = time_us * f_samp_1_024mhz / 1000 500 */ 501 time_ms = (val_us * f_samp_1_024mhz) / 1000; /* 1 ms, 2 ms, ... (power of two) */ 502 if (time_ms < 0 || !is_power_of_2(time_ms) || time_ms > AS73211_SAMPLE_TIME_MAX_MS) 503 return -EINVAL; 504 505 reg_bits = ilog2(time_ms); 506 if (!FIELD_FIT(AS73211_CREG1_TIME_MASK, reg_bits)) 507 return -EINVAL; /* not possible due to previous tests */ 508 509 data->creg1 &= ~AS73211_CREG1_TIME_MASK; 510 data->creg1 |= FIELD_PREP(AS73211_CREG1_TIME_MASK, reg_bits); 511 512 ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_CREG1, data->creg1); 513 if (ret < 0) 514 return ret; 515 516 return 0; 517 518 default: 519 return -EINVAL; 520 }} 521 } 522 523 static int as73211_write_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan, 524 int val, int val2, long mask) 525 { 526 struct as73211_data *data = iio_priv(indio_dev); 527 int ret; 528 529 mutex_lock(&data->mutex); 530 531 ret = iio_device_claim_direct_mode(indio_dev); 532 if (ret < 0) 533 goto error_unlock; 534 535 /* Need to switch to config mode ... */ 536 if ((data->osr & AS73211_OSR_DOS_MASK) != AS73211_OSR_DOS_CONFIG) { 537 data->osr &= ~AS73211_OSR_DOS_MASK; 538 data->osr |= AS73211_OSR_DOS_CONFIG; 539 540 ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_OSR, data->osr); 541 if (ret < 0) 542 goto error_release; 543 } 544 545 ret = _as73211_write_raw(indio_dev, chan, val, val2, mask); 546 547 error_release: 548 iio_device_release_direct_mode(indio_dev); 549 error_unlock: 550 mutex_unlock(&data->mutex); 551 return ret; 552 } 553 554 static irqreturn_t as73211_ready_handler(int irq __always_unused, void *priv) 555 { 556 struct as73211_data *data = iio_priv(priv); 557 558 complete(&data->completion); 559 560 return IRQ_HANDLED; 561 } 562 563 static irqreturn_t as73211_trigger_handler(int irq __always_unused, void *p) 564 { 565 struct iio_poll_func *pf = p; 566 struct iio_dev *indio_dev = pf->indio_dev; 567 struct as73211_data *data = iio_priv(indio_dev); 568 struct { 569 __le16 chan[4]; 570 s64 ts __aligned(8); 571 } scan; 572 int data_result, ret; 573 574 mutex_lock(&data->mutex); 575 576 data_result = as73211_req_data(data); 577 if (data_result < 0 && data_result != -EOVERFLOW) 578 goto done; /* don't push any data for errors other than EOVERFLOW */ 579 580 if (*indio_dev->active_scan_mask == AS73211_SCAN_MASK_ALL) { 581 /* Optimization for reading all (color + temperature) channels */ 582 u8 addr = as73211_channels[0].address; 583 struct i2c_msg msgs[] = { 584 { 585 .addr = data->client->addr, 586 .flags = 0, 587 .len = 1, 588 .buf = &addr, 589 }, 590 { 591 .addr = data->client->addr, 592 .flags = I2C_M_RD, 593 .len = sizeof(scan.chan), 594 .buf = (u8 *)&scan.chan, 595 }, 596 }; 597 598 ret = i2c_transfer(data->client->adapter, msgs, ARRAY_SIZE(msgs)); 599 if (ret < 0) 600 goto done; 601 } else { 602 /* Optimization for reading only color channels */ 603 604 /* AS73211 starts reading at address 2 */ 605 ret = i2c_master_recv(data->client, 606 (char *)&scan.chan[1], 3 * sizeof(scan.chan[1])); 607 if (ret < 0) 608 goto done; 609 } 610 611 if (data_result) { 612 /* 613 * Saturate all channels (in case of overflows). Temperature channel 614 * is not affected by overflows. 615 */ 616 scan.chan[1] = cpu_to_le16(U16_MAX); 617 scan.chan[2] = cpu_to_le16(U16_MAX); 618 scan.chan[3] = cpu_to_le16(U16_MAX); 619 } 620 621 iio_push_to_buffers_with_timestamp(indio_dev, &scan, iio_get_time_ns(indio_dev)); 622 623 done: 624 mutex_unlock(&data->mutex); 625 iio_trigger_notify_done(indio_dev->trig); 626 627 return IRQ_HANDLED; 628 } 629 630 static const struct iio_info as73211_info = { 631 .read_raw = as73211_read_raw, 632 .read_avail = as73211_read_avail, 633 .write_raw = as73211_write_raw, 634 }; 635 636 static int as73211_power(struct iio_dev *indio_dev, bool state) 637 { 638 struct as73211_data *data = iio_priv(indio_dev); 639 int ret; 640 641 mutex_lock(&data->mutex); 642 643 if (state) 644 data->osr &= ~AS73211_OSR_PD; 645 else 646 data->osr |= AS73211_OSR_PD; 647 648 ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_OSR, data->osr); 649 650 mutex_unlock(&data->mutex); 651 652 if (ret < 0) 653 return ret; 654 655 return 0; 656 } 657 658 static void as73211_power_disable(void *data) 659 { 660 struct iio_dev *indio_dev = data; 661 662 as73211_power(indio_dev, false); 663 } 664 665 static int as73211_probe(struct i2c_client *client) 666 { 667 struct device *dev = &client->dev; 668 struct as73211_data *data; 669 struct iio_dev *indio_dev; 670 int ret; 671 672 indio_dev = devm_iio_device_alloc(dev, sizeof(*data)); 673 if (!indio_dev) 674 return -ENOMEM; 675 676 data = iio_priv(indio_dev); 677 i2c_set_clientdata(client, indio_dev); 678 data->client = client; 679 680 mutex_init(&data->mutex); 681 init_completion(&data->completion); 682 683 indio_dev->info = &as73211_info; 684 indio_dev->name = AS73211_DRV_NAME; 685 indio_dev->channels = as73211_channels; 686 indio_dev->num_channels = ARRAY_SIZE(as73211_channels); 687 indio_dev->modes = INDIO_DIRECT_MODE; 688 689 ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_OSR); 690 if (ret < 0) 691 return ret; 692 data->osr = ret; 693 694 /* reset device */ 695 data->osr |= AS73211_OSR_SW_RES; 696 ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_OSR, data->osr); 697 if (ret < 0) 698 return ret; 699 700 ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_OSR); 701 if (ret < 0) 702 return ret; 703 data->osr = ret; 704 705 /* 706 * Reading AGEN is only possible after reset (AGEN is not available if 707 * device is in measurement mode). 708 */ 709 ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_AGEN); 710 if (ret < 0) 711 return ret; 712 713 /* At the time of writing this driver, only DEVID 2 and MUT 1 are known. */ 714 if ((ret & AS73211_AGEN_DEVID_MASK) != AS73211_AGEN_DEVID(2) || 715 (ret & AS73211_AGEN_MUT_MASK) != AS73211_AGEN_MUT(1)) 716 return -ENODEV; 717 718 ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_CREG1); 719 if (ret < 0) 720 return ret; 721 data->creg1 = ret; 722 723 ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_CREG2); 724 if (ret < 0) 725 return ret; 726 data->creg2 = ret; 727 728 ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_CREG3); 729 if (ret < 0) 730 return ret; 731 data->creg3 = ret; 732 as73211_integration_time_calc_avail(data); 733 734 ret = as73211_power(indio_dev, true); 735 if (ret < 0) 736 return ret; 737 738 ret = devm_add_action_or_reset(dev, as73211_power_disable, indio_dev); 739 if (ret) 740 return ret; 741 742 ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL, as73211_trigger_handler, NULL); 743 if (ret) 744 return ret; 745 746 if (client->irq) { 747 ret = devm_request_threaded_irq(&client->dev, client->irq, 748 NULL, 749 as73211_ready_handler, 750 IRQF_ONESHOT, 751 client->name, indio_dev); 752 if (ret) 753 return ret; 754 } 755 756 return devm_iio_device_register(dev, indio_dev); 757 } 758 759 static int __maybe_unused as73211_suspend(struct device *dev) 760 { 761 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 762 763 return as73211_power(indio_dev, false); 764 } 765 766 static int __maybe_unused as73211_resume(struct device *dev) 767 { 768 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 769 770 return as73211_power(indio_dev, true); 771 } 772 773 static SIMPLE_DEV_PM_OPS(as73211_pm_ops, as73211_suspend, as73211_resume); 774 775 static const struct of_device_id as73211_of_match[] = { 776 { .compatible = "ams,as73211" }, 777 { } 778 }; 779 MODULE_DEVICE_TABLE(of, as73211_of_match); 780 781 static const struct i2c_device_id as73211_id[] = { 782 { "as73211", 0 }, 783 { } 784 }; 785 MODULE_DEVICE_TABLE(i2c, as73211_id); 786 787 static struct i2c_driver as73211_driver = { 788 .driver = { 789 .name = AS73211_DRV_NAME, 790 .of_match_table = as73211_of_match, 791 .pm = &as73211_pm_ops, 792 }, 793 .probe_new = as73211_probe, 794 .id_table = as73211_id, 795 }; 796 module_i2c_driver(as73211_driver); 797 798 MODULE_AUTHOR("Christian Eggers <ceggers@arri.de>"); 799 MODULE_DESCRIPTION("AS73211 XYZ True Color Sensor driver"); 800 MODULE_LICENSE("GPL"); 801