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