File Index
Files and directories
Sign in
FILE

as73211.c

File preview with syntax highlighting.

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