1 // SPDX-License-Identifier: GPL-2.0-only
3 * Support for AMS AS73211 JENCOLOR(R) Digital XYZ Sensor
5 * Author: Christian Eggers <ceggers@arri.de>
7 * Copyright (c) 2020 ARRI Lighting
9 * Color light sensor with 16-bit channels for x, y, z and temperature);
10 * 7-bit I2C slave address 0x74 .. 0x77.
12 * Datasheet: https://ams.com/documents/20143/36005/AS73211_DS000556_3-01.pdf
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>
28 #define HZ_PER_KHZ 1000
30 #define AS73211_DRV_NAME "as73211"
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
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
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)
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))
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)
62 #define AS73211_CREG3_CCLK_MASK GENMASK(1, 0)
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)
71 #define AS73211_SAMPLE_FREQ_BASE 1024000
73 #define AS73211_SAMPLE_TIME_NUM 15
74 #define AS73211_SAMPLE_TIME_MAX_MS BIT(AS73211_SAMPLE_TIME_NUM - 1)
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,
84 static const int as73211_hardwaregain_avail
[] = {
85 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048,
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).
100 struct i2c_client
*client
;
106 struct completion completion
;
107 int int_time_avail
[AS73211_SAMPLE_TIME_NUM
* 2];
110 #define AS73211_COLOR_CHANNEL(_color, _si, _addr) { \
111 .type = IIO_INTENSITY, \
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, \
129 .endianness = IIO_LE, \
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
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 */
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
149 #define AS73211_SCAN_MASK_COLOR ( \
150 BIT(AS73211_SCAN_INDEX_X) | \
151 BIT(AS73211_SCAN_INDEX_Y) | \
152 BIT(AS73211_SCAN_INDEX_Z))
154 #define AS73211_SCAN_MASK_ALL ( \
155 BIT(AS73211_SCAN_INDEX_TEMP) | \
156 AS73211_SCAN_MASK_COLOR)
158 static const struct iio_chan_spec as73211_channels
[] = {
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
,
171 .endianness
= IIO_LE
,
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
),
180 static unsigned int as73211_integration_time_1024cyc(struct as73211_data
*data
)
183 * Return integration time in units of 1024 clock cycles. Integration time
184 * in CREG1 is in powers of 2 (x 1024 cycles).
186 return BIT(FIELD_GET(AS73211_CREG1_TIME_MASK
, data
->creg1
));
189 static unsigned int as73211_integration_time_us(struct as73211_data
*data
,
190 unsigned int integration_time_1024cyc
)
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
202 return BIT(3 - FIELD_GET(AS73211_CREG3_CCLK_MASK
, data
->creg3
)) *
203 integration_time_1024cyc
* 125;
206 static void as73211_integration_time_calc_avail(struct as73211_data
*data
)
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
));
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
;
218 static unsigned int as73211_gain(struct as73211_data
*data
)
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
));
224 /* must be called with as73211_data::mutex held. */
225 static int as73211_req_data(struct as73211_data
*data
)
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
;
234 if (data
->client
->irq
)
235 reinit_completion(&data
->completion
);
238 * During measurement, there should be no traffic on the i2c bus as the
239 * electrical noise would disturb the measurement process.
241 i2c_lock_bus(data
->client
->adapter
, I2C_LOCK_SEGMENT
);
243 data
->osr
&= ~AS73211_OSR_DOS_MASK
;
244 data
->osr
|= AS73211_OSR_DOS_MEASURE
| AS73211_OSR_SS
;
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
);
251 i2c_unlock_bus(data
->client
->adapter
, I2C_LOCK_SEGMENT
);
256 * Reset AS73211_OSR_SS (is self clearing) in order to avoid unintentional
257 * triggering of further measurements later.
259 data
->osr
&= ~AS73211_OSR_SS
;
262 * Add 33% extra margin for the timeout. fclk,min = fclk,typ - 27%.
264 time_us
+= time_us
/ 3;
265 if (data
->client
->irq
) {
266 ret
= wait_for_completion_timeout(&data
->completion
, usecs_to_jiffies(time_us
));
268 dev_err(dev
, "timeout waiting for READY IRQ\n");
269 i2c_unlock_bus(data
->client
->adapter
, I2C_LOCK_SEGMENT
);
273 /* Wait integration time */
274 usleep_range(time_us
, 2 * time_us
);
277 i2c_unlock_bus(data
->client
->adapter
, I2C_LOCK_SEGMENT
);
279 ret
= i2c_smbus_read_word_data(data
->client
, AS73211_OUT_OSR_STATUS
);
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__
);
289 if (osr_status
& AS73211_OSR_STATUS_NOTREADY
) {
290 dev_err(dev
, "%s() Data is not ready\n", __func__
);
293 if (!(osr_status
& AS73211_OSR_STATUS_NDATA
)) {
294 dev_err(dev
, "%s() No new data available\n", __func__
);
297 if (osr_status
& AS73211_OSR_STATUS_LDATA
) {
298 dev_err(dev
, "%s() Result buffer overrun\n", __func__
);
301 if (osr_status
& AS73211_OSR_STATUS_ADCOF
) {
302 dev_err(dev
, "%s() ADC overflow\n", __func__
);
305 if (osr_status
& AS73211_OSR_STATUS_MRESOF
) {
306 dev_err(dev
, "%s() Measurement result overflow\n", __func__
);
309 if (osr_status
& AS73211_OSR_STATUS_OUTCONVOF
) {
310 dev_err(dev
, "%s() Timer overflow\n", __func__
);
313 dev_err(dev
, "%s() Unexpected status value\n", __func__
);
320 static int as73211_read_raw(struct iio_dev
*indio_dev
, struct iio_chan_spec
const *chan
,
321 int *val
, int *val2
, long mask
)
323 struct as73211_data
*data
= iio_priv(indio_dev
);
326 case IIO_CHAN_INFO_RAW
: {
329 ret
= iio_device_claim_direct_mode(indio_dev
);
333 ret
= as73211_req_data(data
);
335 iio_device_release_direct_mode(indio_dev
);
339 ret
= i2c_smbus_read_word_data(data
->client
, chan
->address
);
340 iio_device_release_direct_mode(indio_dev
);
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
;
352 case IIO_CHAN_INFO_SCALE
:
353 switch (chan
->type
) {
355 *val
= AS73211_SCALE_TEMP_INT
;
356 *val2
= AS73211_SCALE_TEMP_MICRO
;
357 return IIO_VAL_INT_PLUS_MICRO
;
359 case IIO_INTENSITY
: {
362 switch (chan
->channel2
) {
364 scale
= AS73211_SCALE_X
;
367 scale
= AS73211_SCALE_Y
;
370 scale
= AS73211_SCALE_Z
;
375 scale
/= as73211_gain(data
);
376 scale
/= as73211_integration_time_1024cyc(data
);
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
;
390 case IIO_CHAN_INFO_HARDWAREGAIN
:
391 *val
= as73211_gain(data
);
394 case IIO_CHAN_INFO_INT_TIME
: {
395 unsigned int time_us
;
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
;
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
)
412 struct as73211_data
*data
= iio_priv(indio_dev
);
415 case IIO_CHAN_INFO_SAMP_FREQ
:
416 *length
= ARRAY_SIZE(as73211_samp_freq_avail
);
417 *vals
= as73211_samp_freq_avail
;
419 return IIO_AVAIL_LIST
;
421 case IIO_CHAN_INFO_HARDWAREGAIN
:
422 *length
= ARRAY_SIZE(as73211_hardwaregain_avail
);
423 *vals
= as73211_hardwaregain_avail
;
425 return IIO_AVAIL_LIST
;
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
;
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
)
442 struct as73211_data
*data
= iio_priv(indio_dev
);
446 case IIO_CHAN_INFO_SAMP_FREQ
: {
447 int reg_bits
, freq_kHz
= val
/ HZ_PER_KHZ
; /* 1024, 2048, ... */
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
)
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
))
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
);
463 ret
= i2c_smbus_write_byte_data(data
->client
, AS73211_REG_CREG3
, data
->creg3
);
469 case IIO_CHAN_INFO_HARDWAREGAIN
: {
470 unsigned int reg_bits
;
472 if (val
< 0 || !is_power_of_2(val
) || val2
)
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
))
480 data
->creg1
&= ~AS73211_CREG1_GAIN_MASK
;
481 data
->creg1
|= FIELD_PREP(AS73211_CREG1_GAIN_MASK
, reg_bits
);
483 ret
= i2c_smbus_write_byte_data(data
->client
, AS73211_REG_CREG1
, data
->creg1
);
489 case IIO_CHAN_INFO_INT_TIME
: {
490 int val_us
= val
* USEC_PER_SEC
+ val2
;
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
));
498 * time_ms = time_us * US_PER_MS * f_samp_1_024mhz / MHZ_PER_HZ
499 * = time_us * f_samp_1_024mhz / 1000
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
)
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 */
509 data
->creg1
&= ~AS73211_CREG1_TIME_MASK
;
510 data
->creg1
|= FIELD_PREP(AS73211_CREG1_TIME_MASK
, reg_bits
);
512 ret
= i2c_smbus_write_byte_data(data
->client
, AS73211_REG_CREG1
, data
->creg1
);
523 static int as73211_write_raw(struct iio_dev
*indio_dev
, struct iio_chan_spec
const *chan
,
524 int val
, int val2
, long mask
)
526 struct as73211_data
*data
= iio_priv(indio_dev
);
529 mutex_lock(&data
->mutex
);
531 ret
= iio_device_claim_direct_mode(indio_dev
);
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
;
540 ret
= i2c_smbus_write_byte_data(data
->client
, AS73211_REG_OSR
, data
->osr
);
545 ret
= _as73211_write_raw(indio_dev
, chan
, val
, val2
, mask
);
548 iio_device_release_direct_mode(indio_dev
);
550 mutex_unlock(&data
->mutex
);
554 static irqreturn_t
as73211_ready_handler(int irq __always_unused
, void *priv
)
556 struct as73211_data
*data
= iio_priv(priv
);
558 complete(&data
->completion
);
563 static irqreturn_t
as73211_trigger_handler(int irq __always_unused
, void *p
)
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
);
572 int data_result
, ret
;
574 mutex_lock(&data
->mutex
);
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 */
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
[] = {
585 .addr
= data
->client
->addr
,
591 .addr
= data
->client
->addr
,
593 .len
= sizeof(scan
.chan
),
594 .buf
= (u8
*)&scan
.chan
,
598 ret
= i2c_transfer(data
->client
->adapter
, msgs
, ARRAY_SIZE(msgs
));
602 /* Optimization for reading only color channels */
604 /* AS73211 starts reading at address 2 */
605 ret
= i2c_master_recv(data
->client
,
606 (char *)&scan
.chan
[1], 3 * sizeof(scan
.chan
[1]));
613 * Saturate all channels (in case of overflows). Temperature channel
614 * is not affected by overflows.
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
);
621 iio_push_to_buffers_with_timestamp(indio_dev
, &scan
, iio_get_time_ns(indio_dev
));
624 mutex_unlock(&data
->mutex
);
625 iio_trigger_notify_done(indio_dev
->trig
);
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
,
636 static int as73211_power(struct iio_dev
*indio_dev
, bool state
)
638 struct as73211_data
*data
= iio_priv(indio_dev
);
641 mutex_lock(&data
->mutex
);
644 data
->osr
&= ~AS73211_OSR_PD
;
646 data
->osr
|= AS73211_OSR_PD
;
648 ret
= i2c_smbus_write_byte_data(data
->client
, AS73211_REG_OSR
, data
->osr
);
650 mutex_unlock(&data
->mutex
);
658 static void as73211_power_disable(void *data
)
660 struct iio_dev
*indio_dev
= data
;
662 as73211_power(indio_dev
, false);
665 static int as73211_probe(struct i2c_client
*client
)
667 struct device
*dev
= &client
->dev
;
668 struct as73211_data
*data
;
669 struct iio_dev
*indio_dev
;
672 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*data
));
676 data
= iio_priv(indio_dev
);
677 i2c_set_clientdata(client
, indio_dev
);
678 data
->client
= client
;
680 mutex_init(&data
->mutex
);
681 init_completion(&data
->completion
);
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
;
689 ret
= i2c_smbus_read_byte_data(data
->client
, AS73211_REG_OSR
);
695 data
->osr
|= AS73211_OSR_SW_RES
;
696 ret
= i2c_smbus_write_byte_data(data
->client
, AS73211_REG_OSR
, data
->osr
);
700 ret
= i2c_smbus_read_byte_data(data
->client
, AS73211_REG_OSR
);
706 * Reading AGEN is only possible after reset (AGEN is not available if
707 * device is in measurement mode).
709 ret
= i2c_smbus_read_byte_data(data
->client
, AS73211_REG_AGEN
);
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))
718 ret
= i2c_smbus_read_byte_data(data
->client
, AS73211_REG_CREG1
);
723 ret
= i2c_smbus_read_byte_data(data
->client
, AS73211_REG_CREG2
);
728 ret
= i2c_smbus_read_byte_data(data
->client
, AS73211_REG_CREG3
);
732 as73211_integration_time_calc_avail(data
);
734 ret
= as73211_power(indio_dev
, true);
738 ret
= devm_add_action_or_reset(dev
, as73211_power_disable
, indio_dev
);
742 ret
= devm_iio_triggered_buffer_setup(dev
, indio_dev
, NULL
, as73211_trigger_handler
, NULL
);
747 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
749 as73211_ready_handler
,
751 client
->name
, indio_dev
);
756 return devm_iio_device_register(dev
, indio_dev
);
759 static int __maybe_unused
as73211_suspend(struct device
*dev
)
761 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
763 return as73211_power(indio_dev
, false);
766 static int __maybe_unused
as73211_resume(struct device
*dev
)
768 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
770 return as73211_power(indio_dev
, true);
773 static SIMPLE_DEV_PM_OPS(as73211_pm_ops
, as73211_suspend
, as73211_resume
);
775 static const struct of_device_id as73211_of_match
[] = {
776 { .compatible
= "ams,as73211" },
779 MODULE_DEVICE_TABLE(of
, as73211_of_match
);
781 static const struct i2c_device_id as73211_id
[] = {
785 MODULE_DEVICE_TABLE(i2c
, as73211_id
);
787 static struct i2c_driver as73211_driver
= {
789 .name
= AS73211_DRV_NAME
,
790 .of_match_table
= as73211_of_match
,
791 .pm
= &as73211_pm_ops
,
793 .probe_new
= as73211_probe
,
794 .id_table
= as73211_id
,
796 module_i2c_driver(as73211_driver
);
798 MODULE_AUTHOR("Christian Eggers <ceggers@arri.de>");
799 MODULE_DESCRIPTION("AS73211 XYZ True Color Sensor driver");
800 MODULE_LICENSE("GPL");