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