1 // SPDX-License-Identifier: GPL-2.0-only
3 * Support for AMS AS73211 JENCOLOR(R) Digital XYZ Sensor and AMS AS7331
4 * UVA, UVB and UVC (DUV) Ultraviolet Sensor
6 * Author: Christian Eggers <ceggers@arri.de>
8 * Copyright (c) 2020 ARRI Lighting
10 * Color light sensor with 16-bit channels for x, y, z and temperature);
11 * 7-bit I2C slave address 0x74 .. 0x77.
14 * AS73211: https://ams.com/documents/20143/36005/AS73211_DS000556_3-01.pdf
15 * AS7331: https://ams.com/documents/20143/9106314/AS7331_DS001047_4-00.pdf
18 #include <linux/bitfield.h>
19 #include <linux/completion.h>
20 #include <linux/delay.h>
21 #include <linux/i2c.h>
22 #include <linux/iio/buffer.h>
23 #include <linux/iio/iio.h>
24 #include <linux/iio/sysfs.h>
25 #include <linux/iio/trigger_consumer.h>
26 #include <linux/iio/triggered_buffer.h>
27 #include <linux/module.h>
28 #include <linux/mutex.h>
30 #include <linux/units.h>
32 #define AS73211_DRV_NAME "as73211"
34 /* AS73211 configuration registers */
35 #define AS73211_REG_OSR 0x0
36 #define AS73211_REG_AGEN 0x2
37 #define AS73211_REG_CREG1 0x6
38 #define AS73211_REG_CREG2 0x7
39 #define AS73211_REG_CREG3 0x8
41 /* AS73211 output register bank */
42 #define AS73211_OUT_OSR_STATUS 0
43 #define AS73211_OUT_TEMP 1
44 #define AS73211_OUT_MRES1 2
45 #define AS73211_OUT_MRES2 3
46 #define AS73211_OUT_MRES3 4
48 #define AS73211_OSR_SS BIT(7)
49 #define AS73211_OSR_PD BIT(6)
50 #define AS73211_OSR_SW_RES BIT(3)
51 #define AS73211_OSR_DOS_MASK GENMASK(2, 0)
52 #define AS73211_OSR_DOS_CONFIG FIELD_PREP(AS73211_OSR_DOS_MASK, 0x2)
53 #define AS73211_OSR_DOS_MEASURE FIELD_PREP(AS73211_OSR_DOS_MASK, 0x3)
55 #define AS73211_AGEN_DEVID_MASK GENMASK(7, 4)
56 #define AS73211_AGEN_DEVID(x) FIELD_PREP(AS73211_AGEN_DEVID_MASK, (x))
57 #define AS73211_AGEN_MUT_MASK GENMASK(3, 0)
58 #define AS73211_AGEN_MUT(x) FIELD_PREP(AS73211_AGEN_MUT_MASK, (x))
60 #define AS73211_CREG1_GAIN_MASK GENMASK(7, 4)
61 #define AS73211_CREG1_GAIN_1 11
62 #define AS73211_CREG1_TIME_MASK GENMASK(3, 0)
64 #define AS73211_CREG3_CCLK_MASK GENMASK(1, 0)
66 #define AS73211_OSR_STATUS_OUTCONVOF BIT(15)
67 #define AS73211_OSR_STATUS_MRESOF BIT(14)
68 #define AS73211_OSR_STATUS_ADCOF BIT(13)
69 #define AS73211_OSR_STATUS_LDATA BIT(12)
70 #define AS73211_OSR_STATUS_NDATA BIT(11)
71 #define AS73211_OSR_STATUS_NOTREADY BIT(10)
73 #define AS73211_SAMPLE_FREQ_BASE 1024000
75 #define AS73211_SAMPLE_TIME_NUM 15
76 #define AS73211_SAMPLE_TIME_MAX_MS BIT(AS73211_SAMPLE_TIME_NUM - 1)
78 /* Available sample frequencies are 1.024MHz multiplied by powers of two. */
79 static const int as73211_samp_freq_avail
[] = {
80 AS73211_SAMPLE_FREQ_BASE
* 1,
81 AS73211_SAMPLE_FREQ_BASE
* 2,
82 AS73211_SAMPLE_FREQ_BASE
* 4,
83 AS73211_SAMPLE_FREQ_BASE
* 8,
86 static const int as73211_hardwaregain_avail
[] = {
87 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048,
93 * struct as73211_spec_dev_data - device-specific data
94 * @intensity_scale: Function to retrieve intensity scale values.
95 * @channels: Device channels.
96 * @num_channels: Number of channels of the device.
98 struct as73211_spec_dev_data
{
99 int (*intensity_scale
)(struct as73211_data
*data
, int chan
, int *val
, int *val2
);
100 struct iio_chan_spec
const *channels
;
105 * struct as73211_data - Instance data for one AS73211
106 * @client: I2C client.
107 * @osr: Cached Operational State Register.
108 * @creg1: Cached Configuration Register 1.
109 * @creg2: Cached Configuration Register 2.
110 * @creg3: Cached Configuration Register 3.
111 * @mutex: Keeps cached registers in sync with the device.
112 * @completion: Completion to wait for interrupt.
113 * @int_time_avail: Available integration times (depend on sampling frequency).
114 * @spec_dev: device-specific configuration.
116 struct as73211_data
{
117 struct i2c_client
*client
;
123 struct completion completion
;
124 int int_time_avail
[AS73211_SAMPLE_TIME_NUM
* 2];
125 const struct as73211_spec_dev_data
*spec_dev
;
128 #define AS73211_COLOR_CHANNEL(_color, _si, _addr) { \
129 .type = IIO_INTENSITY, \
131 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), \
132 .info_mask_shared_by_type = \
133 BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
134 BIT(IIO_CHAN_INFO_HARDWAREGAIN) | \
135 BIT(IIO_CHAN_INFO_INT_TIME), \
136 .info_mask_shared_by_type_available = \
137 BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
138 BIT(IIO_CHAN_INFO_HARDWAREGAIN) | \
139 BIT(IIO_CHAN_INFO_INT_TIME), \
140 .channel2 = IIO_MOD_##_color, \
147 .endianness = IIO_LE, \
151 #define AS73211_OFFSET_TEMP_INT (-66)
152 #define AS73211_OFFSET_TEMP_MICRO 900000
153 #define AS73211_SCALE_TEMP_INT 0
154 #define AS73211_SCALE_TEMP_MICRO 50000
156 #define AS73211_SCALE_X 277071108 /* nW/m^2 */
157 #define AS73211_SCALE_Y 298384270 /* nW/m^2 */
158 #define AS73211_SCALE_Z 160241927 /* nW/m^2 */
160 #define AS7331_SCALE_UVA 340000 /* nW/cm^2 */
161 #define AS7331_SCALE_UVB 378000 /* nW/cm^2 */
162 #define AS7331_SCALE_UVC 166000 /* nW/cm^2 */
164 /* Channel order MUST match devices result register order */
165 #define AS73211_SCAN_INDEX_TEMP 0
166 #define AS73211_SCAN_INDEX_X 1
167 #define AS73211_SCAN_INDEX_Y 2
168 #define AS73211_SCAN_INDEX_Z 3
169 #define AS73211_SCAN_INDEX_TS 4
171 #define AS73211_SCAN_MASK_COLOR ( \
172 BIT(AS73211_SCAN_INDEX_X) | \
173 BIT(AS73211_SCAN_INDEX_Y) | \
174 BIT(AS73211_SCAN_INDEX_Z))
176 #define AS73211_SCAN_MASK_ALL ( \
177 BIT(AS73211_SCAN_INDEX_TEMP) | \
178 AS73211_SCAN_MASK_COLOR)
180 static const struct iio_chan_spec as73211_channels
[] = {
183 .info_mask_separate
=
184 BIT(IIO_CHAN_INFO_RAW
) |
185 BIT(IIO_CHAN_INFO_OFFSET
) |
186 BIT(IIO_CHAN_INFO_SCALE
),
187 .address
= AS73211_OUT_TEMP
,
188 .scan_index
= AS73211_SCAN_INDEX_TEMP
,
193 .endianness
= IIO_LE
,
196 AS73211_COLOR_CHANNEL(X
, AS73211_SCAN_INDEX_X
, AS73211_OUT_MRES1
),
197 AS73211_COLOR_CHANNEL(Y
, AS73211_SCAN_INDEX_Y
, AS73211_OUT_MRES2
),
198 AS73211_COLOR_CHANNEL(Z
, AS73211_SCAN_INDEX_Z
, AS73211_OUT_MRES3
),
199 IIO_CHAN_SOFT_TIMESTAMP(AS73211_SCAN_INDEX_TS
),
202 static const struct iio_chan_spec as7331_channels
[] = {
205 .info_mask_separate
=
206 BIT(IIO_CHAN_INFO_RAW
) |
207 BIT(IIO_CHAN_INFO_OFFSET
) |
208 BIT(IIO_CHAN_INFO_SCALE
),
209 .address
= AS73211_OUT_TEMP
,
210 .scan_index
= AS73211_SCAN_INDEX_TEMP
,
215 .endianness
= IIO_LE
,
218 AS73211_COLOR_CHANNEL(LIGHT_UVA
, AS73211_SCAN_INDEX_X
, AS73211_OUT_MRES1
),
219 AS73211_COLOR_CHANNEL(LIGHT_UVB
, AS73211_SCAN_INDEX_Y
, AS73211_OUT_MRES2
),
220 AS73211_COLOR_CHANNEL(LIGHT_DUV
, AS73211_SCAN_INDEX_Z
, AS73211_OUT_MRES3
),
221 IIO_CHAN_SOFT_TIMESTAMP(AS73211_SCAN_INDEX_TS
),
224 static unsigned int as73211_integration_time_1024cyc(struct as73211_data
*data
)
227 * Return integration time in units of 1024 clock cycles. Integration time
228 * in CREG1 is in powers of 2 (x 1024 cycles).
230 return BIT(FIELD_GET(AS73211_CREG1_TIME_MASK
, data
->creg1
));
233 static unsigned int as73211_integration_time_us(struct as73211_data
*data
,
234 unsigned int integration_time_1024cyc
)
237 * f_samp is configured in CREG3 in powers of 2 (x 1.024 MHz)
238 * t_cycl is configured in CREG1 in powers of 2 (x 1024 cycles)
239 * t_int_us = 1 / (f_samp) * t_cycl * US_PER_SEC
240 * = 1 / (2^CREG3_CCLK * 1,024,000) * 2^CREG1_CYCLES * 1,024 * US_PER_SEC
241 * = 2^(-CREG3_CCLK) * 2^CREG1_CYCLES * 1,000
242 * In order to get rid of negative exponents, we extend the "fraction"
243 * by 2^3 (CREG3_CCLK,max = 3)
244 * t_int_us = 2^(3-CREG3_CCLK) * 2^CREG1_CYCLES * 125
246 return BIT(3 - FIELD_GET(AS73211_CREG3_CCLK_MASK
, data
->creg3
)) *
247 integration_time_1024cyc
* 125;
250 static void as73211_integration_time_calc_avail(struct as73211_data
*data
)
254 for (i
= 0; i
< ARRAY_SIZE(data
->int_time_avail
) / 2; i
++) {
255 unsigned int time_us
= as73211_integration_time_us(data
, BIT(i
));
257 data
->int_time_avail
[i
* 2 + 0] = time_us
/ USEC_PER_SEC
;
258 data
->int_time_avail
[i
* 2 + 1] = time_us
% USEC_PER_SEC
;
262 static unsigned int as73211_gain(struct as73211_data
*data
)
264 /* gain can be calculated from CREG1 as 2^(11 - CREG1_GAIN) */
265 return BIT(AS73211_CREG1_GAIN_1
- FIELD_GET(AS73211_CREG1_GAIN_MASK
, data
->creg1
));
268 /* must be called with as73211_data::mutex held. */
269 static int as73211_req_data(struct as73211_data
*data
)
271 unsigned int time_us
= as73211_integration_time_us(data
,
272 as73211_integration_time_1024cyc(data
));
273 struct device
*dev
= &data
->client
->dev
;
274 union i2c_smbus_data smbus_data
;
278 if (data
->client
->irq
)
279 reinit_completion(&data
->completion
);
282 * During measurement, there should be no traffic on the i2c bus as the
283 * electrical noise would disturb the measurement process.
285 i2c_lock_bus(data
->client
->adapter
, I2C_LOCK_SEGMENT
);
287 data
->osr
&= ~AS73211_OSR_DOS_MASK
;
288 data
->osr
|= AS73211_OSR_DOS_MEASURE
| AS73211_OSR_SS
;
290 smbus_data
.byte
= data
->osr
;
291 ret
= __i2c_smbus_xfer(data
->client
->adapter
, data
->client
->addr
,
292 data
->client
->flags
, I2C_SMBUS_WRITE
,
293 AS73211_REG_OSR
, I2C_SMBUS_BYTE_DATA
, &smbus_data
);
295 i2c_unlock_bus(data
->client
->adapter
, I2C_LOCK_SEGMENT
);
300 * Reset AS73211_OSR_SS (is self clearing) in order to avoid unintentional
301 * triggering of further measurements later.
303 data
->osr
&= ~AS73211_OSR_SS
;
306 * Add 33% extra margin for the timeout. fclk,min = fclk,typ - 27%.
308 time_us
+= time_us
/ 3;
309 if (data
->client
->irq
) {
310 ret
= wait_for_completion_timeout(&data
->completion
, usecs_to_jiffies(time_us
));
312 dev_err(dev
, "timeout waiting for READY IRQ\n");
313 i2c_unlock_bus(data
->client
->adapter
, I2C_LOCK_SEGMENT
);
317 /* Wait integration time */
318 usleep_range(time_us
, 2 * time_us
);
321 i2c_unlock_bus(data
->client
->adapter
, I2C_LOCK_SEGMENT
);
323 ret
= i2c_smbus_read_word_data(data
->client
, AS73211_OUT_OSR_STATUS
);
328 if (osr_status
!= (AS73211_OSR_DOS_MEASURE
| AS73211_OSR_STATUS_NDATA
)) {
329 if (osr_status
& AS73211_OSR_SS
) {
330 dev_err(dev
, "%s() Measurement has not stopped\n", __func__
);
333 if (osr_status
& AS73211_OSR_STATUS_NOTREADY
) {
334 dev_err(dev
, "%s() Data is not ready\n", __func__
);
337 if (!(osr_status
& AS73211_OSR_STATUS_NDATA
)) {
338 dev_err(dev
, "%s() No new data available\n", __func__
);
341 if (osr_status
& AS73211_OSR_STATUS_LDATA
) {
342 dev_err(dev
, "%s() Result buffer overrun\n", __func__
);
345 if (osr_status
& AS73211_OSR_STATUS_ADCOF
) {
346 dev_err(dev
, "%s() ADC overflow\n", __func__
);
349 if (osr_status
& AS73211_OSR_STATUS_MRESOF
) {
350 dev_err(dev
, "%s() Measurement result overflow\n", __func__
);
353 if (osr_status
& AS73211_OSR_STATUS_OUTCONVOF
) {
354 dev_err(dev
, "%s() Timer overflow\n", __func__
);
357 dev_err(dev
, "%s() Unexpected status value\n", __func__
);
364 static int as73211_intensity_scale(struct as73211_data
*data
, int chan
,
369 *val
= AS73211_SCALE_X
;
372 *val
= AS73211_SCALE_Y
;
375 *val
= AS73211_SCALE_Z
;
380 *val2
= as73211_integration_time_1024cyc(data
) * as73211_gain(data
);
382 return IIO_VAL_FRACTIONAL
;
385 static int as7331_intensity_scale(struct as73211_data
*data
, int chan
,
389 case IIO_MOD_LIGHT_UVA
:
390 *val
= AS7331_SCALE_UVA
;
392 case IIO_MOD_LIGHT_UVB
:
393 *val
= AS7331_SCALE_UVB
;
395 case IIO_MOD_LIGHT_DUV
:
396 *val
= AS7331_SCALE_UVC
;
401 *val2
= as73211_integration_time_1024cyc(data
) * as73211_gain(data
);
403 return IIO_VAL_FRACTIONAL
;
406 static int as73211_read_raw(struct iio_dev
*indio_dev
, struct iio_chan_spec
const *chan
,
407 int *val
, int *val2
, long mask
)
409 struct as73211_data
*data
= iio_priv(indio_dev
);
412 case IIO_CHAN_INFO_RAW
: {
415 ret
= iio_device_claim_direct_mode(indio_dev
);
419 ret
= as73211_req_data(data
);
421 iio_device_release_direct_mode(indio_dev
);
425 ret
= i2c_smbus_read_word_data(data
->client
, chan
->address
);
426 iio_device_release_direct_mode(indio_dev
);
433 case IIO_CHAN_INFO_OFFSET
:
434 *val
= AS73211_OFFSET_TEMP_INT
;
435 *val2
= AS73211_OFFSET_TEMP_MICRO
;
436 return IIO_VAL_INT_PLUS_MICRO
;
438 case IIO_CHAN_INFO_SCALE
:
439 switch (chan
->type
) {
441 *val
= AS73211_SCALE_TEMP_INT
;
442 *val2
= AS73211_SCALE_TEMP_MICRO
;
443 return IIO_VAL_INT_PLUS_MICRO
;
446 return data
->spec_dev
->intensity_scale(data
, chan
->channel2
,
453 case IIO_CHAN_INFO_SAMP_FREQ
:
454 /* f_samp is configured in CREG3 in powers of 2 (x 1.024 MHz) */
455 *val
= BIT(FIELD_GET(AS73211_CREG3_CCLK_MASK
, data
->creg3
)) *
456 AS73211_SAMPLE_FREQ_BASE
;
459 case IIO_CHAN_INFO_HARDWAREGAIN
:
460 *val
= as73211_gain(data
);
463 case IIO_CHAN_INFO_INT_TIME
: {
464 unsigned int time_us
;
466 mutex_lock(&data
->mutex
);
467 time_us
= as73211_integration_time_us(data
, as73211_integration_time_1024cyc(data
));
468 mutex_unlock(&data
->mutex
);
469 *val
= time_us
/ USEC_PER_SEC
;
470 *val2
= time_us
% USEC_PER_SEC
;
471 return IIO_VAL_INT_PLUS_MICRO
;
478 static int as73211_read_avail(struct iio_dev
*indio_dev
, struct iio_chan_spec
const *chan
,
479 const int **vals
, int *type
, int *length
, long mask
)
481 struct as73211_data
*data
= iio_priv(indio_dev
);
484 case IIO_CHAN_INFO_SAMP_FREQ
:
485 *length
= ARRAY_SIZE(as73211_samp_freq_avail
);
486 *vals
= as73211_samp_freq_avail
;
488 return IIO_AVAIL_LIST
;
490 case IIO_CHAN_INFO_HARDWAREGAIN
:
491 *length
= ARRAY_SIZE(as73211_hardwaregain_avail
);
492 *vals
= as73211_hardwaregain_avail
;
494 return IIO_AVAIL_LIST
;
496 case IIO_CHAN_INFO_INT_TIME
:
497 *length
= ARRAY_SIZE(data
->int_time_avail
);
498 *vals
= data
->int_time_avail
;
499 *type
= IIO_VAL_INT_PLUS_MICRO
;
500 return IIO_AVAIL_LIST
;
507 static int _as73211_write_raw(struct iio_dev
*indio_dev
,
508 struct iio_chan_spec
const *chan __always_unused
,
509 int val
, int val2
, long mask
)
511 struct as73211_data
*data
= iio_priv(indio_dev
);
515 case IIO_CHAN_INFO_SAMP_FREQ
: {
516 int reg_bits
, freq_kHz
= val
/ HZ_PER_KHZ
; /* 1024, 2048, ... */
518 /* val must be 1024 * 2^x */
519 if (val
< 0 || (freq_kHz
* HZ_PER_KHZ
) != val
||
520 !is_power_of_2(freq_kHz
) || val2
)
523 /* f_samp is configured in CREG3 in powers of 2 (x 1.024 MHz (=2^10)) */
524 reg_bits
= ilog2(freq_kHz
) - 10;
525 if (!FIELD_FIT(AS73211_CREG3_CCLK_MASK
, reg_bits
))
528 data
->creg3
&= ~AS73211_CREG3_CCLK_MASK
;
529 data
->creg3
|= FIELD_PREP(AS73211_CREG3_CCLK_MASK
, reg_bits
);
530 as73211_integration_time_calc_avail(data
);
532 ret
= i2c_smbus_write_byte_data(data
->client
, AS73211_REG_CREG3
, data
->creg3
);
538 case IIO_CHAN_INFO_HARDWAREGAIN
: {
539 unsigned int reg_bits
;
541 if (val
< 0 || !is_power_of_2(val
) || val2
)
544 /* gain can be calculated from CREG1 as 2^(11 - CREG1_GAIN) */
545 reg_bits
= AS73211_CREG1_GAIN_1
- ilog2(val
);
546 if (!FIELD_FIT(AS73211_CREG1_GAIN_MASK
, reg_bits
))
549 data
->creg1
&= ~AS73211_CREG1_GAIN_MASK
;
550 data
->creg1
|= FIELD_PREP(AS73211_CREG1_GAIN_MASK
, reg_bits
);
552 ret
= i2c_smbus_write_byte_data(data
->client
, AS73211_REG_CREG1
, data
->creg1
);
558 case IIO_CHAN_INFO_INT_TIME
: {
559 int val_us
= val
* USEC_PER_SEC
+ val2
;
563 /* f_samp is configured in CREG3 in powers of 2 (x 1.024 MHz) */
564 int f_samp_1_024mhz
= BIT(FIELD_GET(AS73211_CREG3_CCLK_MASK
, data
->creg3
));
567 * time_ms = time_us * US_PER_MS * f_samp_1_024mhz / MHZ_PER_HZ
568 * = time_us * f_samp_1_024mhz / 1000
570 time_ms
= (val_us
* f_samp_1_024mhz
) / 1000; /* 1 ms, 2 ms, ... (power of two) */
571 if (time_ms
< 0 || !is_power_of_2(time_ms
) || time_ms
> AS73211_SAMPLE_TIME_MAX_MS
)
574 reg_bits
= ilog2(time_ms
);
575 if (!FIELD_FIT(AS73211_CREG1_TIME_MASK
, reg_bits
))
576 return -EINVAL
; /* not possible due to previous tests */
578 data
->creg1
&= ~AS73211_CREG1_TIME_MASK
;
579 data
->creg1
|= FIELD_PREP(AS73211_CREG1_TIME_MASK
, reg_bits
);
581 ret
= i2c_smbus_write_byte_data(data
->client
, AS73211_REG_CREG1
, data
->creg1
);
592 static int as73211_write_raw(struct iio_dev
*indio_dev
, struct iio_chan_spec
const *chan
,
593 int val
, int val2
, long mask
)
595 struct as73211_data
*data
= iio_priv(indio_dev
);
598 mutex_lock(&data
->mutex
);
600 ret
= iio_device_claim_direct_mode(indio_dev
);
604 /* Need to switch to config mode ... */
605 if ((data
->osr
& AS73211_OSR_DOS_MASK
) != AS73211_OSR_DOS_CONFIG
) {
606 data
->osr
&= ~AS73211_OSR_DOS_MASK
;
607 data
->osr
|= AS73211_OSR_DOS_CONFIG
;
609 ret
= i2c_smbus_write_byte_data(data
->client
, AS73211_REG_OSR
, data
->osr
);
614 ret
= _as73211_write_raw(indio_dev
, chan
, val
, val2
, mask
);
617 iio_device_release_direct_mode(indio_dev
);
619 mutex_unlock(&data
->mutex
);
623 static irqreturn_t
as73211_ready_handler(int irq __always_unused
, void *priv
)
625 struct as73211_data
*data
= iio_priv(priv
);
627 complete(&data
->completion
);
632 static irqreturn_t
as73211_trigger_handler(int irq __always_unused
, void *p
)
634 struct iio_poll_func
*pf
= p
;
635 struct iio_dev
*indio_dev
= pf
->indio_dev
;
636 struct as73211_data
*data
= iio_priv(indio_dev
);
641 int data_result
, ret
;
643 mutex_lock(&data
->mutex
);
645 data_result
= as73211_req_data(data
);
646 if (data_result
< 0 && data_result
!= -EOVERFLOW
)
647 goto done
; /* don't push any data for errors other than EOVERFLOW */
649 if (*indio_dev
->active_scan_mask
== AS73211_SCAN_MASK_ALL
) {
650 /* Optimization for reading all (color + temperature) channels */
651 u8 addr
= as73211_channels
[0].address
;
652 struct i2c_msg msgs
[] = {
654 .addr
= data
->client
->addr
,
660 .addr
= data
->client
->addr
,
662 .len
= sizeof(scan
.chan
),
663 .buf
= (u8
*)&scan
.chan
,
667 ret
= i2c_transfer(data
->client
->adapter
, msgs
, ARRAY_SIZE(msgs
));
671 /* Optimization for reading only color channels */
673 /* AS73211 starts reading at address 2 */
674 ret
= i2c_master_recv(data
->client
,
675 (char *)&scan
.chan
[1], 3 * sizeof(scan
.chan
[1]));
682 * Saturate all channels (in case of overflows). Temperature channel
683 * is not affected by overflows.
685 scan
.chan
[1] = cpu_to_le16(U16_MAX
);
686 scan
.chan
[2] = cpu_to_le16(U16_MAX
);
687 scan
.chan
[3] = cpu_to_le16(U16_MAX
);
690 iio_push_to_buffers_with_timestamp(indio_dev
, &scan
, iio_get_time_ns(indio_dev
));
693 mutex_unlock(&data
->mutex
);
694 iio_trigger_notify_done(indio_dev
->trig
);
699 static const struct iio_info as73211_info
= {
700 .read_raw
= as73211_read_raw
,
701 .read_avail
= as73211_read_avail
,
702 .write_raw
= as73211_write_raw
,
705 static int as73211_power(struct iio_dev
*indio_dev
, bool state
)
707 struct as73211_data
*data
= iio_priv(indio_dev
);
710 mutex_lock(&data
->mutex
);
713 data
->osr
&= ~AS73211_OSR_PD
;
715 data
->osr
|= AS73211_OSR_PD
;
717 ret
= i2c_smbus_write_byte_data(data
->client
, AS73211_REG_OSR
, data
->osr
);
719 mutex_unlock(&data
->mutex
);
727 static void as73211_power_disable(void *data
)
729 struct iio_dev
*indio_dev
= data
;
731 as73211_power(indio_dev
, false);
734 static int as73211_probe(struct i2c_client
*client
)
736 struct device
*dev
= &client
->dev
;
737 struct as73211_data
*data
;
738 struct iio_dev
*indio_dev
;
741 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*data
));
745 data
= iio_priv(indio_dev
);
746 i2c_set_clientdata(client
, indio_dev
);
747 data
->client
= client
;
749 data
->spec_dev
= i2c_get_match_data(client
);
753 mutex_init(&data
->mutex
);
754 init_completion(&data
->completion
);
756 indio_dev
->info
= &as73211_info
;
757 indio_dev
->name
= AS73211_DRV_NAME
;
758 indio_dev
->channels
= data
->spec_dev
->channels
;
759 indio_dev
->num_channels
= data
->spec_dev
->num_channels
;
760 indio_dev
->modes
= INDIO_DIRECT_MODE
;
762 ret
= i2c_smbus_read_byte_data(data
->client
, AS73211_REG_OSR
);
768 data
->osr
|= AS73211_OSR_SW_RES
;
769 ret
= i2c_smbus_write_byte_data(data
->client
, AS73211_REG_OSR
, data
->osr
);
773 ret
= i2c_smbus_read_byte_data(data
->client
, AS73211_REG_OSR
);
779 * Reading AGEN is only possible after reset (AGEN is not available if
780 * device is in measurement mode).
782 ret
= i2c_smbus_read_byte_data(data
->client
, AS73211_REG_AGEN
);
786 /* At the time of writing this driver, only DEVID 2 and MUT 1 are known. */
787 if ((ret
& AS73211_AGEN_DEVID_MASK
) != AS73211_AGEN_DEVID(2) ||
788 (ret
& AS73211_AGEN_MUT_MASK
) != AS73211_AGEN_MUT(1))
791 ret
= i2c_smbus_read_byte_data(data
->client
, AS73211_REG_CREG1
);
796 ret
= i2c_smbus_read_byte_data(data
->client
, AS73211_REG_CREG2
);
801 ret
= i2c_smbus_read_byte_data(data
->client
, AS73211_REG_CREG3
);
805 as73211_integration_time_calc_avail(data
);
807 ret
= as73211_power(indio_dev
, true);
811 ret
= devm_add_action_or_reset(dev
, as73211_power_disable
, indio_dev
);
815 ret
= devm_iio_triggered_buffer_setup(dev
, indio_dev
, NULL
, as73211_trigger_handler
, NULL
);
820 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
822 as73211_ready_handler
,
824 client
->name
, indio_dev
);
829 return devm_iio_device_register(dev
, indio_dev
);
832 static int as73211_suspend(struct device
*dev
)
834 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
836 return as73211_power(indio_dev
, false);
839 static int as73211_resume(struct device
*dev
)
841 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
843 return as73211_power(indio_dev
, true);
846 static DEFINE_SIMPLE_DEV_PM_OPS(as73211_pm_ops
, as73211_suspend
,
849 static const struct as73211_spec_dev_data as73211_spec
= {
850 .intensity_scale
= as73211_intensity_scale
,
851 .channels
= as73211_channels
,
852 .num_channels
= ARRAY_SIZE(as73211_channels
),
855 static const struct as73211_spec_dev_data as7331_spec
= {
856 .intensity_scale
= as7331_intensity_scale
,
857 .channels
= as7331_channels
,
858 .num_channels
= ARRAY_SIZE(as7331_channels
),
861 static const struct of_device_id as73211_of_match
[] = {
862 { .compatible
= "ams,as73211", &as73211_spec
},
863 { .compatible
= "ams,as7331", &as7331_spec
},
866 MODULE_DEVICE_TABLE(of
, as73211_of_match
);
868 static const struct i2c_device_id as73211_id
[] = {
869 { "as73211", (kernel_ulong_t
)&as73211_spec
},
870 { "as7331", (kernel_ulong_t
)&as7331_spec
},
873 MODULE_DEVICE_TABLE(i2c
, as73211_id
);
875 static struct i2c_driver as73211_driver
= {
877 .name
= AS73211_DRV_NAME
,
878 .of_match_table
= as73211_of_match
,
879 .pm
= pm_sleep_ptr(&as73211_pm_ops
),
881 .probe
= as73211_probe
,
882 .id_table
= as73211_id
,
884 module_i2c_driver(as73211_driver
);
886 MODULE_AUTHOR("Christian Eggers <ceggers@arri.de>");
887 MODULE_DESCRIPTION("AS73211 XYZ True Color Sensor driver");
888 MODULE_LICENSE("GPL");