drm/nouveau: consume the return of large GSP message
[drm/drm-misc.git] / drivers / iio / light / as73211.c
blobbe0068081ebbbb37fdfb252b67a77b302ff725f6
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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.
13 * Datasheets:
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>
29 #include <linux/pm.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,
90 struct as73211_data;
92 /**
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;
101 int num_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;
118 u8 osr;
119 u8 creg1;
120 u8 creg2;
121 u8 creg3;
122 struct mutex mutex;
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, \
130 .modified = 1, \
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, \
141 .address = _addr, \
142 .scan_index = _si, \
143 .scan_type = { \
144 .sign = 'u', \
145 .realbits = 16, \
146 .storagebits = 16, \
147 .endianness = IIO_LE, \
148 }, \
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[] = {
182 .type = IIO_TEMP,
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,
189 .scan_type = {
190 .sign = 'u',
191 .realbits = 16,
192 .storagebits = 16,
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[] = {
204 .type = IIO_TEMP,
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,
211 .scan_type = {
212 .sign = 'u',
213 .realbits = 16,
214 .storagebits = 16,
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)
252 int i;
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;
275 u16 osr_status;
276 int ret;
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);
294 if (ret < 0) {
295 i2c_unlock_bus(data->client->adapter, I2C_LOCK_SEGMENT);
296 return ret;
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));
311 if (!ret) {
312 dev_err(dev, "timeout waiting for READY IRQ\n");
313 i2c_unlock_bus(data->client->adapter, I2C_LOCK_SEGMENT);
314 return -ETIMEDOUT;
316 } else {
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);
324 if (ret < 0)
325 return ret;
327 osr_status = ret;
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__);
331 return -ETIME;
333 if (osr_status & AS73211_OSR_STATUS_NOTREADY) {
334 dev_err(dev, "%s() Data is not ready\n", __func__);
335 return -ENODATA;
337 if (!(osr_status & AS73211_OSR_STATUS_NDATA)) {
338 dev_err(dev, "%s() No new data available\n", __func__);
339 return -ENODATA;
341 if (osr_status & AS73211_OSR_STATUS_LDATA) {
342 dev_err(dev, "%s() Result buffer overrun\n", __func__);
343 return -ENOBUFS;
345 if (osr_status & AS73211_OSR_STATUS_ADCOF) {
346 dev_err(dev, "%s() ADC overflow\n", __func__);
347 return -EOVERFLOW;
349 if (osr_status & AS73211_OSR_STATUS_MRESOF) {
350 dev_err(dev, "%s() Measurement result overflow\n", __func__);
351 return -EOVERFLOW;
353 if (osr_status & AS73211_OSR_STATUS_OUTCONVOF) {
354 dev_err(dev, "%s() Timer overflow\n", __func__);
355 return -EOVERFLOW;
357 dev_err(dev, "%s() Unexpected status value\n", __func__);
358 return -EIO;
361 return 0;
364 static int as73211_intensity_scale(struct as73211_data *data, int chan,
365 int *val, int *val2)
367 switch (chan) {
368 case IIO_MOD_X:
369 *val = AS73211_SCALE_X;
370 break;
371 case IIO_MOD_Y:
372 *val = AS73211_SCALE_Y;
373 break;
374 case IIO_MOD_Z:
375 *val = AS73211_SCALE_Z;
376 break;
377 default:
378 return -EINVAL;
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,
386 int *val, int *val2)
388 switch (chan) {
389 case IIO_MOD_LIGHT_UVA:
390 *val = AS7331_SCALE_UVA;
391 break;
392 case IIO_MOD_LIGHT_UVB:
393 *val = AS7331_SCALE_UVB;
394 break;
395 case IIO_MOD_LIGHT_DUV:
396 *val = AS7331_SCALE_UVC;
397 break;
398 default:
399 return -EINVAL;
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);
411 switch (mask) {
412 case IIO_CHAN_INFO_RAW: {
413 int ret;
415 ret = iio_device_claim_direct_mode(indio_dev);
416 if (ret < 0)
417 return ret;
419 ret = as73211_req_data(data);
420 if (ret < 0) {
421 iio_device_release_direct_mode(indio_dev);
422 return ret;
425 ret = i2c_smbus_read_word_data(data->client, chan->address);
426 iio_device_release_direct_mode(indio_dev);
427 if (ret < 0)
428 return ret;
430 *val = ret;
431 return IIO_VAL_INT;
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) {
440 case IIO_TEMP:
441 *val = AS73211_SCALE_TEMP_INT;
442 *val2 = AS73211_SCALE_TEMP_MICRO;
443 return IIO_VAL_INT_PLUS_MICRO;
445 case IIO_INTENSITY:
446 return data->spec_dev->intensity_scale(data, chan->channel2,
447 val, val2);
449 default:
450 return -EINVAL;
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;
457 return IIO_VAL_INT;
459 case IIO_CHAN_INFO_HARDWAREGAIN:
460 *val = as73211_gain(data);
461 return IIO_VAL_INT;
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;
473 default:
474 return -EINVAL;
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);
483 switch (mask) {
484 case IIO_CHAN_INFO_SAMP_FREQ:
485 *length = ARRAY_SIZE(as73211_samp_freq_avail);
486 *vals = as73211_samp_freq_avail;
487 *type = IIO_VAL_INT;
488 return IIO_AVAIL_LIST;
490 case IIO_CHAN_INFO_HARDWAREGAIN:
491 *length = ARRAY_SIZE(as73211_hardwaregain_avail);
492 *vals = as73211_hardwaregain_avail;
493 *type = IIO_VAL_INT;
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;
502 default:
503 return -EINVAL;
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);
512 int ret;
514 switch (mask) {
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)
521 return -EINVAL;
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))
526 return -EINVAL;
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);
533 if (ret < 0)
534 return ret;
536 return 0;
538 case IIO_CHAN_INFO_HARDWAREGAIN: {
539 unsigned int reg_bits;
541 if (val < 0 || !is_power_of_2(val) || val2)
542 return -EINVAL;
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))
547 return -EINVAL;
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);
553 if (ret < 0)
554 return ret;
556 return 0;
558 case IIO_CHAN_INFO_INT_TIME: {
559 int val_us = val * USEC_PER_SEC + val2;
560 int time_ms;
561 int reg_bits;
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)
572 return -EINVAL;
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);
582 if (ret < 0)
583 return ret;
585 return 0;
587 default:
588 return -EINVAL;
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);
596 int ret;
598 mutex_lock(&data->mutex);
600 ret = iio_device_claim_direct_mode(indio_dev);
601 if (ret < 0)
602 goto error_unlock;
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);
610 if (ret < 0)
611 goto error_release;
614 ret = _as73211_write_raw(indio_dev, chan, val, val2, mask);
616 error_release:
617 iio_device_release_direct_mode(indio_dev);
618 error_unlock:
619 mutex_unlock(&data->mutex);
620 return ret;
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);
629 return IRQ_HANDLED;
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);
637 struct {
638 __le16 chan[4];
639 s64 ts __aligned(8);
640 } scan;
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,
655 .flags = 0,
656 .len = 1,
657 .buf = &addr,
660 .addr = data->client->addr,
661 .flags = I2C_M_RD,
662 .len = sizeof(scan.chan),
663 .buf = (u8 *)&scan.chan,
667 ret = i2c_transfer(data->client->adapter, msgs, ARRAY_SIZE(msgs));
668 if (ret < 0)
669 goto done;
670 } else {
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]));
676 if (ret < 0)
677 goto done;
680 if (data_result) {
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));
692 done:
693 mutex_unlock(&data->mutex);
694 iio_trigger_notify_done(indio_dev->trig);
696 return IRQ_HANDLED;
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);
708 int ret;
710 mutex_lock(&data->mutex);
712 if (state)
713 data->osr &= ~AS73211_OSR_PD;
714 else
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);
721 if (ret < 0)
722 return ret;
724 return 0;
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;
739 int ret;
741 indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
742 if (!indio_dev)
743 return -ENOMEM;
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);
750 if (!data->spec_dev)
751 return -EINVAL;
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);
763 if (ret < 0)
764 return ret;
765 data->osr = ret;
767 /* reset device */
768 data->osr |= AS73211_OSR_SW_RES;
769 ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_OSR, data->osr);
770 if (ret < 0)
771 return ret;
773 ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_OSR);
774 if (ret < 0)
775 return ret;
776 data->osr = ret;
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);
783 if (ret < 0)
784 return ret;
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))
789 return -ENODEV;
791 ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_CREG1);
792 if (ret < 0)
793 return ret;
794 data->creg1 = ret;
796 ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_CREG2);
797 if (ret < 0)
798 return ret;
799 data->creg2 = ret;
801 ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_CREG3);
802 if (ret < 0)
803 return ret;
804 data->creg3 = ret;
805 as73211_integration_time_calc_avail(data);
807 ret = as73211_power(indio_dev, true);
808 if (ret < 0)
809 return ret;
811 ret = devm_add_action_or_reset(dev, as73211_power_disable, indio_dev);
812 if (ret)
813 return ret;
815 ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL, as73211_trigger_handler, NULL);
816 if (ret)
817 return ret;
819 if (client->irq) {
820 ret = devm_request_threaded_irq(&client->dev, client->irq,
821 NULL,
822 as73211_ready_handler,
823 IRQF_ONESHOT,
824 client->name, indio_dev);
825 if (ret)
826 return ret;
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,
847 as73211_resume);
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 = {
876 .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");