linux-iio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v6 0/2] iio: light: Support AMS AS73211 digital XYZ sensor
@ 2020-08-05  5:57 Christian Eggers
  2020-08-05  5:57 ` [PATCH v6 1/2] dt-bindings: iio: light: add AMS AS73211 support Christian Eggers
  2020-08-05  5:57 ` [PATCH v6 2/2] iio: light: as73211: New driver Christian Eggers
  0 siblings, 2 replies; 20+ messages in thread
From: Christian Eggers @ 2020-08-05  5:57 UTC (permalink / raw)
  To: Rob Herring, Andy Shevchenko, Jonathan Cameron, Jonathan Cameron
  Cc: Hartmut Knaack, Lars-Peter Clausen, Peter Meerwald-Stadler,
	linux-iio, devicetree, linux-kernel, Christian Eggers

This series adds support for the AMS AS73211 digital XYZ sensor.

Probably everything is fine now...


Changes in v6:
---------------
- [2/2] Fixed remaining open points reported by Andy Shevchenko.

Changes in v5:
---------------
- [1/2] Reviewed by Rob Herring
- [2/2] Added KHZ_PER_HR define
- [2/2] Added AS73211_SAMPLE_FREQ_BASE define
- [2/2] Slight changes in comments
- [2/2] Claim direct mode in write_raw()
- [2/2] Saturate only in case of overflow
- [2/2] Don't set indio_dev->dev.parent
- [2/2] Fix error path (order) in probe()

Changes in v4:
---------------
- Integrated 2nd review from Andy Shevchenko
- Use more devm_ functions in as73211_probe()

Changes in v3:
---------------
- Integrated comments from Andy Shevchenko
- Integrated comments from Jonathan Cameron

Changes in v2:
---------------
- Fix $id in dt binding
- Document full I2C address range in "reg" property
- Move "buffer" member out of "struct as73211_data"
- Fix sparse warnings by using correct data types



^ permalink raw reply	[flat|nested] 20+ messages in thread

* [PATCH v6 1/2] dt-bindings: iio: light: add AMS AS73211 support
  2020-08-05  5:57 [PATCH v6 0/2] iio: light: Support AMS AS73211 digital XYZ sensor Christian Eggers
@ 2020-08-05  5:57 ` Christian Eggers
  2020-08-06 17:44   ` Jonathan Cameron
  2020-08-05  5:57 ` [PATCH v6 2/2] iio: light: as73211: New driver Christian Eggers
  1 sibling, 1 reply; 20+ messages in thread
From: Christian Eggers @ 2020-08-05  5:57 UTC (permalink / raw)
  To: Rob Herring, Andy Shevchenko, Jonathan Cameron, Jonathan Cameron
  Cc: Hartmut Knaack, Lars-Peter Clausen, Peter Meerwald-Stadler,
	linux-iio, devicetree, linux-kernel, Christian Eggers,
	Rob Herring

Add DT bindings for AMS AS73211 XYZ True Color Sensor.

Signed-off-by: Christian Eggers <ceggers@arri.de>
Reviewed-by: Rob Herring <robh@kernel.org>
---
 .../bindings/iio/light/ams,as73211.yaml       | 54 +++++++++++++++++++
 1 file changed, 54 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/light/ams,as73211.yaml

diff --git a/Documentation/devicetree/bindings/iio/light/ams,as73211.yaml b/Documentation/devicetree/bindings/iio/light/ams,as73211.yaml
new file mode 100644
index 000000000000..0e8cd02759b3
--- /dev/null
+++ b/Documentation/devicetree/bindings/iio/light/ams,as73211.yaml
@@ -0,0 +1,54 @@
+# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/light/ams,as73211.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: AMS AS73211 JENCOLOR(R) Digital XYZ Sensor
+
+maintainers:
+  - Christian Eggers <ceggers@arri.de>
+
+description: |
+  XYZ True Color Sensor with I2C Interface
+  https://ams.com/documents/20143/36005/AS73211_DS000556_3-01.pdf/a65474c0-b302-c2fd-e30a-c98df87616df
+
+properties:
+  compatible:
+    enum:
+      - ams,as73211
+
+  reg:
+    description:
+      I2C address of the device (0x74...0x77).
+    maxItems: 1
+
+  interrupts:
+    description:
+      Interrupt specifier for the READY interrupt generated by the device.
+    maxItems: 1
+
+required:
+  - compatible
+  - reg
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/irq.h>
+
+    i2c {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        as73211@74 {
+            compatible = "ams,as73211";
+            reg = <0x74>;
+            pinctrl-names = "default";
+            pinctrl-0 = <&pinctrl_color_sensor>;
+            interrupt-parent = <&gpio2>;
+            interrupts = <19 IRQ_TYPE_EDGE_RISING>; /* READY */
+        };
+    };
+...
-- 
Christian Eggers
Embedded software developer

Arnold & Richter Cine Technik GmbH & Co. Betriebs KG
Sitz: Muenchen - Registergericht: Amtsgericht Muenchen - Handelsregisternummer: HRA 57918
Persoenlich haftender Gesellschafter: Arnold & Richter Cine Technik GmbH
Sitz: Muenchen - Registergericht: Amtsgericht Muenchen - Handelsregisternummer: HRB 54477
Geschaeftsfuehrer: Dr. Michael Neuhaeuser; Stephan Schenk; Walter Trauninger; Markus Zeiler


^ permalink raw reply	[flat|nested] 20+ messages in thread

* [PATCH v6 2/2] iio: light: as73211: New driver
  2020-08-05  5:57 [PATCH v6 0/2] iio: light: Support AMS AS73211 digital XYZ sensor Christian Eggers
  2020-08-05  5:57 ` [PATCH v6 1/2] dt-bindings: iio: light: add AMS AS73211 support Christian Eggers
@ 2020-08-05  5:57 ` Christian Eggers
  2020-08-05  7:39   ` Andy Shevchenko
  2020-08-06 17:44   ` Jonathan Cameron
  1 sibling, 2 replies; 20+ messages in thread
From: Christian Eggers @ 2020-08-05  5:57 UTC (permalink / raw)
  To: Rob Herring, Andy Shevchenko, Jonathan Cameron, Jonathan Cameron
  Cc: Hartmut Knaack, Lars-Peter Clausen, Peter Meerwald-Stadler,
	linux-iio, devicetree, linux-kernel, Christian Eggers

Support for AMS AS73211 JENCOLOR(R) Digital XYZ Sensor.

This driver has no built-in trigger. In order for making triggered
measurements, an external (software) trigger driver like
iio-trig-hrtimer or iio-trig-sysfs is required.

The sensor supports single and continuous measurement modes. The latter
is not used by design as this would require tight timing synchronization
between hardware and driver without much benefit.

Datasheet: https://ams.com/documents/20143/36005/AS73211_DS000556_3-01.pdf
Signed-off-by: Christian Eggers <ceggers@arri.de>
---
 MAINTAINERS                 |   7 +
 drivers/iio/light/Kconfig   |  15 +
 drivers/iio/light/Makefile  |   1 +
 drivers/iio/light/as73211.c | 801 ++++++++++++++++++++++++++++++++++++
 4 files changed, 824 insertions(+)
 create mode 100644 drivers/iio/light/as73211.c

diff --git a/MAINTAINERS b/MAINTAINERS
index 068d6e94122b..673570414147 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -934,6 +934,13 @@ S:	Supported
 F:	arch/arm64/boot/dts/amd/amd-seattle-xgbe*.dtsi
 F:	drivers/net/ethernet/amd/xgbe/
 
+AMS AS73211 DRIVER
+M:	Christian Eggers <ceggers@arri.de>
+L:	linux-iio@vger.kernel.org
+S:	Maintained
+F:	Documentation/devicetree/bindings/iio/light/ams,as73211.yaml
+F:	drivers/iio/light/as73211.c
+
 ANALOG DEVICES INC AD5686 DRIVER
 M:	Michael Hennerich <Michael.Hennerich@analog.com>
 L:	linux-pm@vger.kernel.org
diff --git a/drivers/iio/light/Kconfig b/drivers/iio/light/Kconfig
index 182bd18c4bb2..cade6dc0305b 100644
--- a/drivers/iio/light/Kconfig
+++ b/drivers/iio/light/Kconfig
@@ -86,6 +86,21 @@ config APDS9960
 	  To compile this driver as a module, choose M here: the
 	  module will be called apds9960
 
+config AS73211
+	tristate "AMS AS73211 XYZ color sensor"
+	depends on I2C
+	select IIO_BUFFER
+	select IIO_TRIGGERED_BUFFER
+	help
+	 If you say yes here you get support for the AMS AS73211
+	 JENCOLOR(R) Digital XYZ Sensor.
+
+	 For triggered measurements, you will need an additional trigger driver
+	 like IIO_HRTIMER_TRIGGER or IIO_SYSFS_TRIGGER.
+
+	 This driver can also be built as a module.  If so, the module
+	 will be called as73211.
+
 config BH1750
 	tristate "ROHM BH1750 ambient light sensor"
 	depends on I2C
diff --git a/drivers/iio/light/Makefile b/drivers/iio/light/Makefile
index d1c8aa30b9a8..ea376deaca54 100644
--- a/drivers/iio/light/Makefile
+++ b/drivers/iio/light/Makefile
@@ -11,6 +11,7 @@ obj-$(CONFIG_AL3010)		+= al3010.o
 obj-$(CONFIG_AL3320A)		+= al3320a.o
 obj-$(CONFIG_APDS9300)		+= apds9300.o
 obj-$(CONFIG_APDS9960)		+= apds9960.o
+obj-$(CONFIG_AS73211)		+= as73211.o
 obj-$(CONFIG_BH1750)		+= bh1750.o
 obj-$(CONFIG_BH1780)		+= bh1780.o
 obj-$(CONFIG_CM32181)		+= cm32181.o
diff --git a/drivers/iio/light/as73211.c b/drivers/iio/light/as73211.c
new file mode 100644
index 000000000000..3383aaacbf52
--- /dev/null
+++ b/drivers/iio/light/as73211.c
@@ -0,0 +1,801 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Support for AMS AS73211 JENCOLOR(R) Digital XYZ Sensor
+ *
+ * Author: Christian Eggers <ceggers@arri.de>
+ *
+ * Copyright (c) 2020 ARRI Lighting
+ *
+ * Color light sensor with 16-bit channels for x, y, z and temperature);
+ * 7-bit I2C slave address 0x74 .. 0x77.
+ *
+ * Datasheet: https://ams.com/documents/20143/36005/AS73211_DS000556_3-01.pdf
+ */
+
+#include <linux/bitfield.h>
+#include <linux/completion.h>
+#include <linux/delay.h>
+#include <linux/i2c.h>
+#include <linux/iio/buffer.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
+#include <linux/iio/trigger_consumer.h>
+#include <linux/iio/triggered_buffer.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/pm.h>
+
+#define HZ_PER_KHZ 1000
+
+#define AS73211_DRV_NAME "as73211"
+
+/* AS73211 configuration registers */
+#define AS73211_REG_OSR    0x0
+#define AS73211_REG_AGEN   0x2
+#define AS73211_REG_CREG1  0x6
+#define AS73211_REG_CREG2  0x7
+#define AS73211_REG_CREG3  0x8
+
+/* AS73211 output register bank */
+#define AS73211_OUT_OSR_STATUS    0
+#define AS73211_OUT_TEMP          1
+#define AS73211_OUT_MRES1         2
+#define AS73211_OUT_MRES2         3
+#define AS73211_OUT_MRES3         4
+
+#define AS73211_OSR_SS            BIT(7)
+#define AS73211_OSR_PD            BIT(6)
+#define AS73211_OSR_SW_RES        BIT(3)
+#define AS73211_OSR_DOS_MASK      GENMASK(2, 0)
+#define AS73211_OSR_DOS_CONFIG    FIELD_PREP(AS73211_OSR_DOS_MASK, 0x2)
+#define AS73211_OSR_DOS_MEASURE   FIELD_PREP(AS73211_OSR_DOS_MASK, 0x3)
+
+#define AS73211_AGEN_DEVID_MASK   GENMASK(7, 4)
+#define AS73211_AGEN_DEVID(x)     FIELD_PREP(AS73211_AGEN_DEVID_MASK, (x))
+#define AS73211_AGEN_MUT_MASK     GENMASK(3, 0)
+#define AS73211_AGEN_MUT(x)       FIELD_PREP(AS73211_AGEN_MUT_MASK, (x))
+
+#define AS73211_CREG1_GAIN_MASK   GENMASK(7, 4)
+#define AS73211_CREG1_GAIN_1      13
+#define AS73211_CREG1_TIME_MASK   GENMASK(3, 0)
+
+#define AS73211_CREG3_CCLK_MASK   GENMASK(1, 0)
+
+#define AS73211_OSR_STATUS_OUTCONVOF  BIT(15)
+#define AS73211_OSR_STATUS_MRESOF     BIT(14)
+#define AS73211_OSR_STATUS_ADCOF      BIT(13)
+#define AS73211_OSR_STATUS_LDATA      BIT(12)
+#define AS73211_OSR_STATUS_NDATA      BIT(11)
+#define AS73211_OSR_STATUS_NOTREADY   BIT(10)
+
+#define AS73211_SAMPLE_FREQ_BASE      1024000
+
+#define AS73211_SAMPLE_TIME_NUM       15
+#define AS73211_SAMPLE_TIME_MAX_MS    BIT(AS73211_SAMPLE_TIME_NUM - 1)
+
+/* Available sample frequencies are 1.024MHz multiplied by powers of two. */
+static const int as73211_samp_freq_avail[] = {
+	AS73211_SAMPLE_FREQ_BASE * 1,
+	AS73211_SAMPLE_FREQ_BASE * 2,
+	AS73211_SAMPLE_FREQ_BASE * 4,
+	AS73211_SAMPLE_FREQ_BASE * 8,
+};
+
+static const int as73211_hardwaregain_avail[] = {
+	1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048,
+};
+
+/**
+ * struct as73211_data - Instance data for one AS73211
+ * @client: I2C client.
+ * @osr:    Cached Operational State Register.
+ * @creg1:  Cached Configuration Register 1.
+ * @creg2:  Cached Configuration Register 2.
+ * @creg3:  Cached Configuration Register 3.
+ * @mutex:  Keeps cached registers in sync with the device.
+ * @completion: Completion to wait for interrupt.
+ * @int_time_avail: Available integration times (depend on sampling frequency).
+ */
+struct as73211_data {
+	struct i2c_client *client;
+	u8 osr;
+	u8 creg1;
+	u8 creg2;
+	u8 creg3;
+	struct mutex mutex;
+	struct completion completion;
+	int int_time_avail[AS73211_SAMPLE_TIME_NUM * 2];
+};
+
+#define AS73211_COLOR_CHANNEL(_color, _si, _addr) { \
+	.type = IIO_INTENSITY, \
+	.modified = 1, \
+	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), \
+	.info_mask_shared_by_type = \
+		BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
+		BIT(IIO_CHAN_INFO_HARDWAREGAIN) | \
+		BIT(IIO_CHAN_INFO_INT_TIME), \
+	.info_mask_shared_by_type_available = \
+		BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
+		BIT(IIO_CHAN_INFO_HARDWAREGAIN) | \
+		BIT(IIO_CHAN_INFO_INT_TIME), \
+	.channel2 = IIO_MOD_##_color, \
+	.address = _addr, \
+	.scan_index = _si, \
+	.scan_type = { \
+		.sign = 'u', \
+		.realbits = 16, \
+		.storagebits = 16, \
+		.endianness = IIO_LE, \
+	}, \
+}
+
+#define AS73211_OFFSET_TEMP_INT    (-66)
+#define AS73211_OFFSET_TEMP_MICRO  900000
+#define AS73211_SCALE_TEMP_INT     0
+#define AS73211_SCALE_TEMP_MICRO   50000
+
+#define AS73211_SCALE_X 277071108  /* nW/m^2 */
+#define AS73211_SCALE_Y 298384270  /* nW/m^2 */
+#define AS73211_SCALE_Z 160241927  /* nW/m^2 */
+
+/* Channel order MUST match devices result register order */
+#define AS73211_SCAN_INDEX_TEMP 0
+#define AS73211_SCAN_INDEX_X    1
+#define AS73211_SCAN_INDEX_Y    2
+#define AS73211_SCAN_INDEX_Z    3
+#define AS73211_SCAN_INDEX_TS   4
+
+#define AS73211_SCAN_MASK_COLOR ( \
+	BIT(AS73211_SCAN_INDEX_X) |   \
+	BIT(AS73211_SCAN_INDEX_Y) |   \
+	BIT(AS73211_SCAN_INDEX_Z))
+
+#define AS73211_SCAN_MASK_ALL (    \
+	BIT(AS73211_SCAN_INDEX_TEMP) | \
+	AS73211_SCAN_MASK_COLOR)
+
+static const struct iio_chan_spec as73211_channels[] = {
+	{
+		.type = IIO_TEMP,
+		.info_mask_separate =
+			BIT(IIO_CHAN_INFO_RAW) |
+			BIT(IIO_CHAN_INFO_OFFSET) |
+			BIT(IIO_CHAN_INFO_SCALE),
+		.address = AS73211_OUT_TEMP,
+		.scan_index = AS73211_SCAN_INDEX_TEMP,
+		.scan_type = {
+			.sign = 'u',
+			.realbits = 16,
+			.storagebits = 16,
+			.endianness = IIO_LE,
+		}
+	},
+	AS73211_COLOR_CHANNEL(X, AS73211_SCAN_INDEX_X, AS73211_OUT_MRES1),
+	AS73211_COLOR_CHANNEL(Y, AS73211_SCAN_INDEX_Y, AS73211_OUT_MRES2),
+	AS73211_COLOR_CHANNEL(Z, AS73211_SCAN_INDEX_Z, AS73211_OUT_MRES3),
+	IIO_CHAN_SOFT_TIMESTAMP(AS73211_SCAN_INDEX_TS),
+};
+
+static unsigned int as73211_integration_time_1024cyc(struct as73211_data *data)
+{
+	/*
+	 * Return integration time in units of 1024 clock cycles. Integration time
+	 * in CREG1 is in powers of 2 (x 1024 cycles).
+	 */
+	return BIT(FIELD_GET(AS73211_CREG1_TIME_MASK, data->creg1));
+}
+
+static unsigned int as73211_integration_time_us(struct as73211_data *data,
+						 unsigned int integration_time_1024cyc)
+{
+	/*
+	 * f_samp is configured in CREG3 in powers of 2 (x 1.024 MHz)
+	 * t_cycl is configured in CREG1 in powers of 2 (x 1024 cycles)
+	 * t_int_us = 1 / (f_samp) * t_cycl * US_PER_SEC
+	 *          = 1 / (2^CREG3_CCLK * 1,024,000) * 2^CREG1_CYCLES * 1,024 * US_PER_SEC
+	 *          = 2^(-CREG3_CCLK) * 2^CREG1_CYCLES * 1,000
+	 * In order to get rid of negative exponents, we extend the "fraction"
+	 * by 2^3 (CREG3_CCLK,max = 3)
+	 * t_int_us = 2^(3-CREG3_CCLK) * 2^CREG1_CYCLES * 125
+	 */
+	return BIT(3 - FIELD_GET(AS73211_CREG3_CCLK_MASK, data->creg3)) *
+		integration_time_1024cyc * 125;
+}
+
+static void as73211_integration_time_calc_avail(struct as73211_data *data)
+{
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(data->int_time_avail); i++) {
+		unsigned int time_us = as73211_integration_time_us(data, BIT(i));
+
+		data->int_time_avail[i * 2 + 0] = time_us / USEC_PER_SEC;
+		data->int_time_avail[i * 2 + 1] = time_us % USEC_PER_SEC;
+	}
+}
+
+static unsigned int as73211_gain(struct as73211_data *data)
+{
+	/* gain can be calculated from CREG1 as 2^(13 - CREG1_GAIN) */
+	return BIT(AS73211_CREG1_GAIN_1 - FIELD_GET(AS73211_CREG1_GAIN_MASK, data->creg1));
+}
+
+/* must be called with as73211_data::mutex held. */
+static int as73211_req_data(struct as73211_data *data)
+{
+	unsigned int time_us = as73211_integration_time_us(data,
+							    as73211_integration_time_1024cyc(data));
+	struct device *dev = &data->client->dev;
+	union i2c_smbus_data smbus_data;
+	u16 osr_status;
+	int ret;
+
+	if (data->client->irq)
+		reinit_completion(&data->completion);
+
+	/*
+	 * During measurement, there should be no traffic on the i2c bus as the
+	 * electrical noise would disturb the measurement process.
+	 */
+	i2c_lock_bus(data->client->adapter, I2C_LOCK_SEGMENT);
+
+	data->osr &= ~AS73211_OSR_DOS_MASK;
+	data->osr |= AS73211_OSR_DOS_MEASURE | AS73211_OSR_SS;
+
+	smbus_data.byte = data->osr;
+	ret = __i2c_smbus_xfer(data->client->adapter, data->client->addr,
+			data->client->flags, I2C_SMBUS_WRITE,
+			AS73211_REG_OSR, I2C_SMBUS_BYTE_DATA, &smbus_data);
+	if (ret < 0) {
+		i2c_unlock_bus(data->client->adapter, I2C_LOCK_SEGMENT);
+		return ret;
+	}
+
+	/*
+	 * Reset AS73211_OSR_SS (is self clearing) in order to avoid unintentional
+	 * triggering of further measurements later.
+	 */
+	data->osr &= ~AS73211_OSR_SS;
+
+	/*
+	 * Add some extra margin for the timeout. sensor timing is not as precise
+	 * as our one ...
+	 */
+	time_us += time_us / 8;
+	if (data->client->irq) {
+		ret = wait_for_completion_timeout(&data->completion, usecs_to_jiffies(time_us));
+		if (!ret) {
+			dev_err(dev, "timeout waiting for READY IRQ\n");
+			i2c_unlock_bus(data->client->adapter, I2C_LOCK_SEGMENT);
+			return -ETIMEDOUT;
+		}
+	} else {
+		/* Wait integration time */
+		usleep_range(time_us, 2 * time_us);
+	}
+
+	i2c_unlock_bus(data->client->adapter, I2C_LOCK_SEGMENT);
+
+	ret = i2c_smbus_read_word_data(data->client, AS73211_OUT_OSR_STATUS);
+	if (ret < 0)
+		return ret;
+
+	osr_status = ret;
+	if (osr_status != (AS73211_OSR_DOS_MEASURE | AS73211_OSR_STATUS_NDATA)) {
+		if (osr_status & AS73211_OSR_SS) {
+			dev_err(dev, "%s() Measurement has not stopped\n", __func__);
+			return -ETIME;
+		}
+		if (osr_status & AS73211_OSR_STATUS_NOTREADY) {
+			dev_err(dev, "%s() Data is not ready\n", __func__);
+			return -ENODATA;
+		}
+		if (!(osr_status & AS73211_OSR_STATUS_NDATA)) {
+			dev_err(dev, "%s() No new data available\n", __func__);
+			return -ENODATA;
+		}
+		if (osr_status & AS73211_OSR_STATUS_LDATA) {
+			dev_err(dev, "%s() Result buffer overrun\n", __func__);
+			return -ENOBUFS;
+		}
+		if (osr_status & AS73211_OSR_STATUS_ADCOF) {
+			dev_err(dev, "%s() ADC overflow\n", __func__);
+			return -EOVERFLOW;
+		}
+		if (osr_status & AS73211_OSR_STATUS_MRESOF) {
+			dev_err(dev, "%s() Measurement result overflow\n", __func__);
+			return -EOVERFLOW;
+		}
+		if (osr_status & AS73211_OSR_STATUS_OUTCONVOF) {
+			dev_err(dev, "%s() Timer overflow\n", __func__);
+			return -EOVERFLOW;
+		}
+		dev_err(dev, "%s() Unexpected status value\n", __func__);
+		return -EIO;
+	}
+
+	return 0;
+}
+
+static int as73211_read_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan,
+			     int *val, int *val2, long mask)
+{
+	struct as73211_data *data = iio_priv(indio_dev);
+
+	switch (mask) {
+	case IIO_CHAN_INFO_RAW: {
+		int ret;
+
+		ret = iio_device_claim_direct_mode(indio_dev);
+		if (ret < 0)
+			return ret;
+
+		ret = as73211_req_data(data);
+		if (ret < 0) {
+			iio_device_release_direct_mode(indio_dev);
+			return ret;
+		}
+
+		ret = i2c_smbus_read_word_data(data->client, chan->address);
+		iio_device_release_direct_mode(indio_dev);
+		if (ret < 0)
+			return ret;
+
+		*val = ret;
+		return IIO_VAL_INT;
+	}
+	case IIO_CHAN_INFO_OFFSET:
+		*val = AS73211_OFFSET_TEMP_INT;
+		*val2 = AS73211_OFFSET_TEMP_MICRO;
+		return IIO_VAL_INT_PLUS_MICRO;
+
+	case IIO_CHAN_INFO_SCALE:
+		switch (chan->type) {
+		case IIO_TEMP:
+			*val = AS73211_SCALE_TEMP_INT;
+			*val2 = AS73211_SCALE_TEMP_MICRO;
+			return IIO_VAL_INT_PLUS_MICRO;
+
+		case IIO_INTENSITY: {
+			unsigned int scale;
+
+			switch (chan->channel2) {
+			case IIO_MOD_X:
+				scale = AS73211_SCALE_X;
+				break;
+			case IIO_MOD_Y:
+				scale = AS73211_SCALE_Y;
+				break;
+			case IIO_MOD_Z:
+				scale = AS73211_SCALE_Z;
+				break;
+			default:
+				return -EINVAL;
+			}
+			scale /= as73211_gain(data);
+			scale /= as73211_integration_time_1024cyc(data);
+			*val = scale;
+			return IIO_VAL_INT;
+
+		default:
+			return -EINVAL;
+		}}
+
+	case IIO_CHAN_INFO_SAMP_FREQ:
+		/* f_samp is configured in CREG3 in powers of 2 (x 1.024 MHz) */
+		*val = BIT(FIELD_GET(AS73211_CREG3_CCLK_MASK, data->creg3)) *
+			AS73211_SAMPLE_FREQ_BASE;
+		return IIO_VAL_INT;
+
+	case IIO_CHAN_INFO_HARDWAREGAIN:
+		*val = as73211_gain(data);
+		return IIO_VAL_INT;
+
+	case IIO_CHAN_INFO_INT_TIME: {
+		unsigned int time_us;
+
+		mutex_lock(&data->mutex);
+		time_us = as73211_integration_time_us(data, as73211_integration_time_1024cyc(data));
+		mutex_unlock(&data->mutex);
+		*val = time_us / USEC_PER_SEC;
+		*val2 = time_us % USEC_PER_SEC;
+		return IIO_VAL_INT_PLUS_MICRO;
+
+	default:
+		return -EINVAL;
+	}}
+}
+
+static int as73211_read_avail(struct iio_dev *indio_dev, struct iio_chan_spec const *chan,
+			       const int **vals, int *type, int *length, long mask)
+{
+	struct as73211_data *data = iio_priv(indio_dev);
+
+	switch (mask) {
+	case IIO_CHAN_INFO_SAMP_FREQ:
+		*length = ARRAY_SIZE(as73211_samp_freq_avail);
+		*vals = as73211_samp_freq_avail;
+		*type = IIO_VAL_INT;
+		return IIO_AVAIL_LIST;
+
+	case IIO_CHAN_INFO_HARDWAREGAIN:
+		*length = ARRAY_SIZE(as73211_hardwaregain_avail);
+		*vals = as73211_hardwaregain_avail;
+		*type = IIO_VAL_INT;
+		return IIO_AVAIL_LIST;
+
+	case IIO_CHAN_INFO_INT_TIME:
+		*length = ARRAY_SIZE(data->int_time_avail);
+		*vals = data->int_time_avail;
+		*type = IIO_VAL_INT_PLUS_MICRO;
+		return IIO_AVAIL_LIST;
+
+	default:
+		return -EINVAL;
+	}
+}
+
+static int _as73211_write_raw(struct iio_dev *indio_dev,
+			       struct iio_chan_spec const *chan __always_unused,
+			       int val, int val2, long mask)
+{
+	struct as73211_data *data = iio_priv(indio_dev);
+	int ret;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_SAMP_FREQ: {
+		int reg_bits, freq_kHz = val / HZ_PER_KHZ;  /* 1024, 2048, ... */
+
+		/* val must be 1024 * 2^x */
+		if (val < 0 || (freq_kHz * HZ_PER_KHZ) != val ||
+				!is_power_of_2(freq_kHz) || val2)
+			return -EINVAL;
+
+		/* f_samp is configured in CREG3 in powers of 2 (x 1.024 MHz (=2^10)) */
+		reg_bits = ilog2(freq_kHz) - 10;
+		if (!FIELD_FIT(AS73211_CREG3_CCLK_MASK, reg_bits))
+			return -EINVAL;
+
+		data->creg3 &= ~AS73211_CREG3_CCLK_MASK;
+		data->creg3 |= FIELD_PREP(AS73211_CREG3_CCLK_MASK, reg_bits);
+		as73211_integration_time_calc_avail(data);
+
+		ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_CREG3, data->creg3);
+		if (ret < 0)
+			return ret;
+
+		return 0;
+	}
+	case IIO_CHAN_INFO_HARDWAREGAIN: {
+		unsigned int reg_bits;
+
+		if (val < 0 || !is_power_of_2(val) || val2)
+			return -EINVAL;
+
+		/* gain can be calculated from CREG1 as 2^(13 - CREG1_GAIN) */
+		reg_bits = AS73211_CREG1_GAIN_1 - ilog2(val);
+		if (!FIELD_FIT(AS73211_CREG1_GAIN_MASK, reg_bits))
+			return -EINVAL;
+
+		data->creg1 &= ~AS73211_CREG1_GAIN_MASK;
+		data->creg1 |= FIELD_PREP(AS73211_CREG1_GAIN_MASK, reg_bits);
+
+		ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_CREG1, data->creg1);
+		if (ret < 0)
+			return ret;
+
+		return 0;
+	}
+	case IIO_CHAN_INFO_INT_TIME: {
+		int val_us = val * USEC_PER_SEC + val2;
+		int time_ms;
+		int reg_bits;
+
+		/* f_samp is configured in CREG3 in powers of 2 (x 1.024 MHz) */
+		int f_samp_1_024mhz = BIT(FIELD_GET(AS73211_CREG3_CCLK_MASK, data->creg3));
+
+		/*
+		 * time_ms = time_us * US_PER_MS * f_samp_1_024mhz / MHZ_PER_HZ
+		 *         = time_us * f_samp_1_024mhz / 1000
+		 */
+		time_ms = (val_us * f_samp_1_024mhz) / 1000;  /* 1 ms, 2 ms, ... (power of two) */
+		if (time_ms < 0 || !is_power_of_2(time_ms) || time_ms > AS73211_SAMPLE_TIME_MAX_MS)
+			return -EINVAL;
+
+		reg_bits = ilog2(time_ms);
+		if (!FIELD_FIT(AS73211_CREG1_TIME_MASK, reg_bits))
+			return -EINVAL;  /* not possible due to previous tests */
+
+		data->creg1 &= ~AS73211_CREG1_TIME_MASK;
+		data->creg1 |= FIELD_PREP(AS73211_CREG1_TIME_MASK, reg_bits);
+
+		ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_CREG1, data->creg1);
+		if (ret < 0)
+			return ret;
+
+		return 0;
+
+	default:
+		return -EINVAL;
+	}}
+}
+
+static int as73211_write_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan,
+			      int val, int val2, long mask)
+{
+	struct as73211_data *data = iio_priv(indio_dev);
+	int ret;
+
+	mutex_lock(&data->mutex);
+
+	ret = iio_device_claim_direct_mode(indio_dev);
+	if (ret < 0)
+		goto error_unlock;
+
+	/* Need to switch to config mode ... */
+	if ((data->osr & AS73211_OSR_DOS_MASK) != AS73211_OSR_DOS_CONFIG) {
+		data->osr &= ~AS73211_OSR_DOS_MASK;
+		data->osr |= AS73211_OSR_DOS_CONFIG;
+
+		ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_OSR, data->osr);
+		if (ret < 0)
+			goto error_release;
+	}
+
+	ret = _as73211_write_raw(indio_dev, chan, val, val2, mask);
+
+error_release:
+	iio_device_release_direct_mode(indio_dev);
+error_unlock:
+	mutex_unlock(&data->mutex);
+	return ret;
+}
+
+static irqreturn_t as73211_ready_handler(int irq __always_unused, void *priv)
+{
+	struct as73211_data *data = iio_priv(priv);
+
+	complete(&data->completion);
+
+	return IRQ_HANDLED;
+}
+
+static irqreturn_t as73211_trigger_handler(int irq __always_unused, void *p)
+{
+	struct iio_poll_func *pf = p;
+	struct iio_dev *indio_dev = pf->indio_dev;
+	struct as73211_data *data = iio_priv(indio_dev);
+	struct {
+		__le16 chan[4];
+		s64 ts __aligned(8);
+	} scan;
+	int data_result, ret;
+
+	mutex_lock(&data->mutex);
+
+	data_result = as73211_req_data(data);
+	if (data_result < 0 && data_result != -EOVERFLOW)
+		goto done;  /* don't push any data for errors other than EOVERFLOW */
+
+	if (*indio_dev->active_scan_mask == AS73211_SCAN_MASK_ALL) {
+		/* Optimization for reading all (color + temperature) channels */
+		u8 addr = as73211_channels[0].address;
+		struct i2c_msg msgs[] = {
+			{
+				.addr = data->client->addr,
+				.flags = 0,
+				.len = 1,
+				.buf = &addr,
+			},
+			{
+				.addr = data->client->addr,
+				.flags = I2C_M_RD,
+				.len = sizeof(scan.chan),
+				.buf = (u8 *)&scan.chan,
+			},
+		};
+
+		ret = i2c_transfer(data->client->adapter, msgs, ARRAY_SIZE(msgs));
+		if (ret < 0)
+			goto done;
+	} else {
+		/* Optimization for reading only color channels */
+
+		/* AS73211 starts reading at address 2 */
+		ret = i2c_master_recv(data->client,
+				(char *)&scan.chan[1], 3 * sizeof(scan.chan[1]));
+		if (ret < 0)
+			goto done;
+	}
+
+	if (data_result) {
+		/*
+		 * Saturate all channels (in case of overflows). Temperature channel
+		 * is not affected by overflows.
+		 */
+		scan.chan[1] = cpu_to_le16(U16_MAX);
+		scan.chan[2] = cpu_to_le16(U16_MAX);
+		scan.chan[3] = cpu_to_le16(U16_MAX);
+	}
+
+	iio_push_to_buffers_with_timestamp(indio_dev, &scan, iio_get_time_ns(indio_dev));
+
+done:
+	mutex_unlock(&data->mutex);
+	iio_trigger_notify_done(indio_dev->trig);
+
+	return IRQ_HANDLED;
+}
+
+static const struct iio_info as73211_info = {
+	.read_raw = as73211_read_raw,
+	.read_avail = as73211_read_avail,
+	.write_raw = as73211_write_raw,
+};
+
+static int as73211_power(struct iio_dev *indio_dev, bool state)
+{
+	struct as73211_data *data = iio_priv(indio_dev);
+	int ret;
+
+	mutex_lock(&data->mutex);
+
+	if (state)
+		data->osr &= ~AS73211_OSR_PD;
+	else
+		data->osr |= AS73211_OSR_PD;
+
+	ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_OSR, data->osr);
+
+	mutex_unlock(&data->mutex);
+
+	if (ret < 0)
+		return ret;
+
+	return 0;
+}
+
+static void as73211_power_disable(void *data)
+{
+	struct iio_dev *indio_dev = data;
+
+	as73211_power(indio_dev, false);
+}
+
+static int as73211_probe(struct i2c_client *client)
+{
+	struct device *dev = &client->dev;
+	struct as73211_data *data;
+	struct iio_dev *indio_dev;
+	int ret;
+
+	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
+	if (!indio_dev)
+		return -ENOMEM;
+
+	data = iio_priv(indio_dev);
+	i2c_set_clientdata(client, indio_dev);
+	data->client = client;
+
+	mutex_init(&data->mutex);
+	init_completion(&data->completion);
+
+	indio_dev->info = &as73211_info;
+	indio_dev->name = AS73211_DRV_NAME;
+	indio_dev->channels = as73211_channels;
+	indio_dev->num_channels = ARRAY_SIZE(as73211_channels);
+	indio_dev->modes = INDIO_DIRECT_MODE;
+
+	ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_OSR);
+	if (ret < 0)
+		return ret;
+	data->osr = ret;
+
+	/* reset device */
+	data->osr |= AS73211_OSR_SW_RES;
+	ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_OSR, data->osr);
+	if (ret < 0)
+		return ret;
+
+	ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_OSR);
+	if (ret < 0)
+		return ret;
+	data->osr = ret;
+
+	/*
+	 * Reading AGEN is only possible after reset (AGEN is not available if
+	 * device is in measurement mode).
+	 */
+	ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_AGEN);
+	if (ret < 0)
+		return ret;
+
+	/* At the time of writing this driver, only DEVID 2 and MUT 1 are known. */
+	if ((ret & AS73211_AGEN_DEVID_MASK) != AS73211_AGEN_DEVID(2) ||
+	    (ret & AS73211_AGEN_MUT_MASK) != AS73211_AGEN_MUT(1))
+		return -ENODEV;
+
+	ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_CREG1);
+	if (ret < 0)
+		return ret;
+	data->creg1 = ret;
+
+	ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_CREG2);
+	if (ret < 0)
+		return ret;
+	data->creg2 = ret;
+
+	ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_CREG3);
+	if (ret < 0)
+		return ret;
+	data->creg3 = ret;
+	as73211_integration_time_calc_avail(data);
+
+	ret = as73211_power(indio_dev, true);
+	if (ret < 0)
+		return ret;
+
+	ret = devm_add_action_or_reset(dev, as73211_power_disable, indio_dev);
+	if (ret)
+		return ret;
+
+	ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL, as73211_trigger_handler, NULL);
+	if (ret)
+		return ret;
+
+	if (client->irq) {
+		ret = devm_request_threaded_irq(&client->dev, client->irq,
+				NULL,
+				as73211_ready_handler,
+				IRQF_ONESHOT,
+				client->name, indio_dev);
+		if (ret)
+			return ret;
+	}
+
+	return devm_iio_device_register(dev, indio_dev);
+}
+
+static int __maybe_unused as73211_suspend(struct device *dev)
+{
+	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
+
+	return as73211_power(indio_dev, false);
+}
+
+static int __maybe_unused as73211_resume(struct device *dev)
+{
+	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
+
+	return as73211_power(indio_dev, true);
+}
+
+static SIMPLE_DEV_PM_OPS(as73211_pm_ops, as73211_suspend, as73211_resume);
+
+static const struct of_device_id as73211_of_match[] = {
+	{ .compatible = "ams,as73211" },
+	{ }
+};
+MODULE_DEVICE_TABLE(of, as73211_of_match);
+
+static const struct i2c_device_id as73211_id[] = {
+	{ "as73211", 0 },
+	{ }
+};
+MODULE_DEVICE_TABLE(i2c, as73211_id);
+
+static struct i2c_driver as73211_driver = {
+	.driver = {
+		.name           = AS73211_DRV_NAME,
+		.of_match_table = as73211_of_match,
+		.pm             = &as73211_pm_ops,
+	},
+	.probe_new  = as73211_probe,
+	.id_table   = as73211_id,
+};
+module_i2c_driver(as73211_driver);
+
+MODULE_AUTHOR("Christian Eggers <ceggers@arri.de>");
+MODULE_DESCRIPTION("AS73211 XYZ True Color Sensor driver");
+MODULE_LICENSE("GPL");
-- 
Christian Eggers
Embedded software developer

Arnold & Richter Cine Technik GmbH & Co. Betriebs KG
Sitz: Muenchen - Registergericht: Amtsgericht Muenchen - Handelsregisternummer: HRA 57918
Persoenlich haftender Gesellschafter: Arnold & Richter Cine Technik GmbH
Sitz: Muenchen - Registergericht: Amtsgericht Muenchen - Handelsregisternummer: HRB 54477
Geschaeftsfuehrer: Dr. Michael Neuhaeuser; Stephan Schenk; Walter Trauninger; Markus Zeiler


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH v6 2/2] iio: light: as73211: New driver
  2020-08-05  5:57 ` [PATCH v6 2/2] iio: light: as73211: New driver Christian Eggers
@ 2020-08-05  7:39   ` Andy Shevchenko
  2020-08-06 17:44   ` Jonathan Cameron
  1 sibling, 0 replies; 20+ messages in thread
From: Andy Shevchenko @ 2020-08-05  7:39 UTC (permalink / raw)
  To: Christian Eggers
  Cc: Rob Herring, Jonathan Cameron, Jonathan Cameron, Hartmut Knaack,
	Lars-Peter Clausen, Peter Meerwald-Stadler, linux-iio,
	devicetree, Linux Kernel Mailing List

On Wed, Aug 5, 2020 at 9:00 AM Christian Eggers <ceggers@arri.de> wrote:
>
> Support for AMS AS73211 JENCOLOR(R) Digital XYZ Sensor.
>
> This driver has no built-in trigger. In order for making triggered
> measurements, an external (software) trigger driver like
> iio-trig-hrtimer or iio-trig-sysfs is required.
>
> The sensor supports single and continuous measurement modes. The latter
> is not used by design as this would require tight timing synchronization
> between hardware and driver without much benefit.
>

FWIW,
Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>

> Datasheet: https://ams.com/documents/20143/36005/AS73211_DS000556_3-01.pdf
> Signed-off-by: Christian Eggers <ceggers@arri.de>
> ---
>  MAINTAINERS                 |   7 +
>  drivers/iio/light/Kconfig   |  15 +
>  drivers/iio/light/Makefile  |   1 +
>  drivers/iio/light/as73211.c | 801 ++++++++++++++++++++++++++++++++++++
>  4 files changed, 824 insertions(+)
>  create mode 100644 drivers/iio/light/as73211.c
>
> diff --git a/MAINTAINERS b/MAINTAINERS
> index 068d6e94122b..673570414147 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -934,6 +934,13 @@ S: Supported
>  F:     arch/arm64/boot/dts/amd/amd-seattle-xgbe*.dtsi
>  F:     drivers/net/ethernet/amd/xgbe/
>
> +AMS AS73211 DRIVER
> +M:     Christian Eggers <ceggers@arri.de>
> +L:     linux-iio@vger.kernel.org
> +S:     Maintained
> +F:     Documentation/devicetree/bindings/iio/light/ams,as73211.yaml
> +F:     drivers/iio/light/as73211.c
> +
>  ANALOG DEVICES INC AD5686 DRIVER
>  M:     Michael Hennerich <Michael.Hennerich@analog.com>
>  L:     linux-pm@vger.kernel.org
> diff --git a/drivers/iio/light/Kconfig b/drivers/iio/light/Kconfig
> index 182bd18c4bb2..cade6dc0305b 100644
> --- a/drivers/iio/light/Kconfig
> +++ b/drivers/iio/light/Kconfig
> @@ -86,6 +86,21 @@ config APDS9960
>           To compile this driver as a module, choose M here: the
>           module will be called apds9960
>
> +config AS73211
> +       tristate "AMS AS73211 XYZ color sensor"
> +       depends on I2C
> +       select IIO_BUFFER
> +       select IIO_TRIGGERED_BUFFER
> +       help
> +        If you say yes here you get support for the AMS AS73211
> +        JENCOLOR(R) Digital XYZ Sensor.
> +
> +        For triggered measurements, you will need an additional trigger driver
> +        like IIO_HRTIMER_TRIGGER or IIO_SYSFS_TRIGGER.
> +
> +        This driver can also be built as a module.  If so, the module
> +        will be called as73211.
> +
>  config BH1750
>         tristate "ROHM BH1750 ambient light sensor"
>         depends on I2C
> diff --git a/drivers/iio/light/Makefile b/drivers/iio/light/Makefile
> index d1c8aa30b9a8..ea376deaca54 100644
> --- a/drivers/iio/light/Makefile
> +++ b/drivers/iio/light/Makefile
> @@ -11,6 +11,7 @@ obj-$(CONFIG_AL3010)          += al3010.o
>  obj-$(CONFIG_AL3320A)          += al3320a.o
>  obj-$(CONFIG_APDS9300)         += apds9300.o
>  obj-$(CONFIG_APDS9960)         += apds9960.o
> +obj-$(CONFIG_AS73211)          += as73211.o
>  obj-$(CONFIG_BH1750)           += bh1750.o
>  obj-$(CONFIG_BH1780)           += bh1780.o
>  obj-$(CONFIG_CM32181)          += cm32181.o
> diff --git a/drivers/iio/light/as73211.c b/drivers/iio/light/as73211.c
> new file mode 100644
> index 000000000000..3383aaacbf52
> --- /dev/null
> +++ b/drivers/iio/light/as73211.c
> @@ -0,0 +1,801 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +/*
> + * Support for AMS AS73211 JENCOLOR(R) Digital XYZ Sensor
> + *
> + * Author: Christian Eggers <ceggers@arri.de>
> + *
> + * Copyright (c) 2020 ARRI Lighting
> + *
> + * Color light sensor with 16-bit channels for x, y, z and temperature);
> + * 7-bit I2C slave address 0x74 .. 0x77.
> + *
> + * Datasheet: https://ams.com/documents/20143/36005/AS73211_DS000556_3-01.pdf
> + */
> +
> +#include <linux/bitfield.h>
> +#include <linux/completion.h>
> +#include <linux/delay.h>
> +#include <linux/i2c.h>
> +#include <linux/iio/buffer.h>
> +#include <linux/iio/iio.h>
> +#include <linux/iio/sysfs.h>
> +#include <linux/iio/trigger_consumer.h>
> +#include <linux/iio/triggered_buffer.h>
> +#include <linux/module.h>
> +#include <linux/mutex.h>
> +#include <linux/pm.h>
> +
> +#define HZ_PER_KHZ 1000
> +
> +#define AS73211_DRV_NAME "as73211"
> +
> +/* AS73211 configuration registers */
> +#define AS73211_REG_OSR    0x0
> +#define AS73211_REG_AGEN   0x2
> +#define AS73211_REG_CREG1  0x6
> +#define AS73211_REG_CREG2  0x7
> +#define AS73211_REG_CREG3  0x8
> +
> +/* AS73211 output register bank */
> +#define AS73211_OUT_OSR_STATUS    0
> +#define AS73211_OUT_TEMP          1
> +#define AS73211_OUT_MRES1         2
> +#define AS73211_OUT_MRES2         3
> +#define AS73211_OUT_MRES3         4
> +
> +#define AS73211_OSR_SS            BIT(7)
> +#define AS73211_OSR_PD            BIT(6)
> +#define AS73211_OSR_SW_RES        BIT(3)
> +#define AS73211_OSR_DOS_MASK      GENMASK(2, 0)
> +#define AS73211_OSR_DOS_CONFIG    FIELD_PREP(AS73211_OSR_DOS_MASK, 0x2)
> +#define AS73211_OSR_DOS_MEASURE   FIELD_PREP(AS73211_OSR_DOS_MASK, 0x3)
> +
> +#define AS73211_AGEN_DEVID_MASK   GENMASK(7, 4)
> +#define AS73211_AGEN_DEVID(x)     FIELD_PREP(AS73211_AGEN_DEVID_MASK, (x))
> +#define AS73211_AGEN_MUT_MASK     GENMASK(3, 0)
> +#define AS73211_AGEN_MUT(x)       FIELD_PREP(AS73211_AGEN_MUT_MASK, (x))
> +
> +#define AS73211_CREG1_GAIN_MASK   GENMASK(7, 4)
> +#define AS73211_CREG1_GAIN_1      13
> +#define AS73211_CREG1_TIME_MASK   GENMASK(3, 0)
> +
> +#define AS73211_CREG3_CCLK_MASK   GENMASK(1, 0)
> +
> +#define AS73211_OSR_STATUS_OUTCONVOF  BIT(15)
> +#define AS73211_OSR_STATUS_MRESOF     BIT(14)
> +#define AS73211_OSR_STATUS_ADCOF      BIT(13)
> +#define AS73211_OSR_STATUS_LDATA      BIT(12)
> +#define AS73211_OSR_STATUS_NDATA      BIT(11)
> +#define AS73211_OSR_STATUS_NOTREADY   BIT(10)
> +
> +#define AS73211_SAMPLE_FREQ_BASE      1024000
> +
> +#define AS73211_SAMPLE_TIME_NUM       15
> +#define AS73211_SAMPLE_TIME_MAX_MS    BIT(AS73211_SAMPLE_TIME_NUM - 1)
> +
> +/* Available sample frequencies are 1.024MHz multiplied by powers of two. */
> +static const int as73211_samp_freq_avail[] = {
> +       AS73211_SAMPLE_FREQ_BASE * 1,
> +       AS73211_SAMPLE_FREQ_BASE * 2,
> +       AS73211_SAMPLE_FREQ_BASE * 4,
> +       AS73211_SAMPLE_FREQ_BASE * 8,
> +};
> +
> +static const int as73211_hardwaregain_avail[] = {
> +       1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048,
> +};
> +
> +/**
> + * struct as73211_data - Instance data for one AS73211
> + * @client: I2C client.
> + * @osr:    Cached Operational State Register.
> + * @creg1:  Cached Configuration Register 1.
> + * @creg2:  Cached Configuration Register 2.
> + * @creg3:  Cached Configuration Register 3.
> + * @mutex:  Keeps cached registers in sync with the device.
> + * @completion: Completion to wait for interrupt.
> + * @int_time_avail: Available integration times (depend on sampling frequency).
> + */
> +struct as73211_data {
> +       struct i2c_client *client;
> +       u8 osr;
> +       u8 creg1;
> +       u8 creg2;
> +       u8 creg3;
> +       struct mutex mutex;
> +       struct completion completion;
> +       int int_time_avail[AS73211_SAMPLE_TIME_NUM * 2];
> +};
> +
> +#define AS73211_COLOR_CHANNEL(_color, _si, _addr) { \
> +       .type = IIO_INTENSITY, \
> +       .modified = 1, \
> +       .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), \
> +       .info_mask_shared_by_type = \
> +               BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
> +               BIT(IIO_CHAN_INFO_HARDWAREGAIN) | \
> +               BIT(IIO_CHAN_INFO_INT_TIME), \
> +       .info_mask_shared_by_type_available = \
> +               BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
> +               BIT(IIO_CHAN_INFO_HARDWAREGAIN) | \
> +               BIT(IIO_CHAN_INFO_INT_TIME), \
> +       .channel2 = IIO_MOD_##_color, \
> +       .address = _addr, \
> +       .scan_index = _si, \
> +       .scan_type = { \
> +               .sign = 'u', \
> +               .realbits = 16, \
> +               .storagebits = 16, \
> +               .endianness = IIO_LE, \
> +       }, \
> +}
> +
> +#define AS73211_OFFSET_TEMP_INT    (-66)
> +#define AS73211_OFFSET_TEMP_MICRO  900000
> +#define AS73211_SCALE_TEMP_INT     0
> +#define AS73211_SCALE_TEMP_MICRO   50000
> +
> +#define AS73211_SCALE_X 277071108  /* nW/m^2 */
> +#define AS73211_SCALE_Y 298384270  /* nW/m^2 */
> +#define AS73211_SCALE_Z 160241927  /* nW/m^2 */
> +
> +/* Channel order MUST match devices result register order */
> +#define AS73211_SCAN_INDEX_TEMP 0
> +#define AS73211_SCAN_INDEX_X    1
> +#define AS73211_SCAN_INDEX_Y    2
> +#define AS73211_SCAN_INDEX_Z    3
> +#define AS73211_SCAN_INDEX_TS   4
> +
> +#define AS73211_SCAN_MASK_COLOR ( \
> +       BIT(AS73211_SCAN_INDEX_X) |   \
> +       BIT(AS73211_SCAN_INDEX_Y) |   \
> +       BIT(AS73211_SCAN_INDEX_Z))
> +
> +#define AS73211_SCAN_MASK_ALL (    \
> +       BIT(AS73211_SCAN_INDEX_TEMP) | \
> +       AS73211_SCAN_MASK_COLOR)
> +
> +static const struct iio_chan_spec as73211_channels[] = {
> +       {
> +               .type = IIO_TEMP,
> +               .info_mask_separate =
> +                       BIT(IIO_CHAN_INFO_RAW) |
> +                       BIT(IIO_CHAN_INFO_OFFSET) |
> +                       BIT(IIO_CHAN_INFO_SCALE),
> +               .address = AS73211_OUT_TEMP,
> +               .scan_index = AS73211_SCAN_INDEX_TEMP,
> +               .scan_type = {
> +                       .sign = 'u',
> +                       .realbits = 16,
> +                       .storagebits = 16,
> +                       .endianness = IIO_LE,
> +               }
> +       },
> +       AS73211_COLOR_CHANNEL(X, AS73211_SCAN_INDEX_X, AS73211_OUT_MRES1),
> +       AS73211_COLOR_CHANNEL(Y, AS73211_SCAN_INDEX_Y, AS73211_OUT_MRES2),
> +       AS73211_COLOR_CHANNEL(Z, AS73211_SCAN_INDEX_Z, AS73211_OUT_MRES3),
> +       IIO_CHAN_SOFT_TIMESTAMP(AS73211_SCAN_INDEX_TS),
> +};
> +
> +static unsigned int as73211_integration_time_1024cyc(struct as73211_data *data)
> +{
> +       /*
> +        * Return integration time in units of 1024 clock cycles. Integration time
> +        * in CREG1 is in powers of 2 (x 1024 cycles).
> +        */
> +       return BIT(FIELD_GET(AS73211_CREG1_TIME_MASK, data->creg1));
> +}
> +
> +static unsigned int as73211_integration_time_us(struct as73211_data *data,
> +                                                unsigned int integration_time_1024cyc)
> +{
> +       /*
> +        * f_samp is configured in CREG3 in powers of 2 (x 1.024 MHz)
> +        * t_cycl is configured in CREG1 in powers of 2 (x 1024 cycles)
> +        * t_int_us = 1 / (f_samp) * t_cycl * US_PER_SEC
> +        *          = 1 / (2^CREG3_CCLK * 1,024,000) * 2^CREG1_CYCLES * 1,024 * US_PER_SEC
> +        *          = 2^(-CREG3_CCLK) * 2^CREG1_CYCLES * 1,000
> +        * In order to get rid of negative exponents, we extend the "fraction"
> +        * by 2^3 (CREG3_CCLK,max = 3)
> +        * t_int_us = 2^(3-CREG3_CCLK) * 2^CREG1_CYCLES * 125
> +        */
> +       return BIT(3 - FIELD_GET(AS73211_CREG3_CCLK_MASK, data->creg3)) *
> +               integration_time_1024cyc * 125;
> +}
> +
> +static void as73211_integration_time_calc_avail(struct as73211_data *data)
> +{
> +       int i;
> +
> +       for (i = 0; i < ARRAY_SIZE(data->int_time_avail); i++) {
> +               unsigned int time_us = as73211_integration_time_us(data, BIT(i));
> +
> +               data->int_time_avail[i * 2 + 0] = time_us / USEC_PER_SEC;
> +               data->int_time_avail[i * 2 + 1] = time_us % USEC_PER_SEC;
> +       }
> +}
> +
> +static unsigned int as73211_gain(struct as73211_data *data)
> +{
> +       /* gain can be calculated from CREG1 as 2^(13 - CREG1_GAIN) */
> +       return BIT(AS73211_CREG1_GAIN_1 - FIELD_GET(AS73211_CREG1_GAIN_MASK, data->creg1));
> +}
> +
> +/* must be called with as73211_data::mutex held. */
> +static int as73211_req_data(struct as73211_data *data)
> +{
> +       unsigned int time_us = as73211_integration_time_us(data,
> +                                                           as73211_integration_time_1024cyc(data));
> +       struct device *dev = &data->client->dev;
> +       union i2c_smbus_data smbus_data;
> +       u16 osr_status;
> +       int ret;
> +
> +       if (data->client->irq)
> +               reinit_completion(&data->completion);
> +
> +       /*
> +        * During measurement, there should be no traffic on the i2c bus as the
> +        * electrical noise would disturb the measurement process.
> +        */
> +       i2c_lock_bus(data->client->adapter, I2C_LOCK_SEGMENT);
> +
> +       data->osr &= ~AS73211_OSR_DOS_MASK;
> +       data->osr |= AS73211_OSR_DOS_MEASURE | AS73211_OSR_SS;
> +
> +       smbus_data.byte = data->osr;
> +       ret = __i2c_smbus_xfer(data->client->adapter, data->client->addr,
> +                       data->client->flags, I2C_SMBUS_WRITE,
> +                       AS73211_REG_OSR, I2C_SMBUS_BYTE_DATA, &smbus_data);
> +       if (ret < 0) {
> +               i2c_unlock_bus(data->client->adapter, I2C_LOCK_SEGMENT);
> +               return ret;
> +       }
> +
> +       /*
> +        * Reset AS73211_OSR_SS (is self clearing) in order to avoid unintentional
> +        * triggering of further measurements later.
> +        */
> +       data->osr &= ~AS73211_OSR_SS;
> +
> +       /*
> +        * Add some extra margin for the timeout. sensor timing is not as precise
> +        * as our one ...
> +        */
> +       time_us += time_us / 8;
> +       if (data->client->irq) {
> +               ret = wait_for_completion_timeout(&data->completion, usecs_to_jiffies(time_us));
> +               if (!ret) {
> +                       dev_err(dev, "timeout waiting for READY IRQ\n");
> +                       i2c_unlock_bus(data->client->adapter, I2C_LOCK_SEGMENT);
> +                       return -ETIMEDOUT;
> +               }
> +       } else {
> +               /* Wait integration time */
> +               usleep_range(time_us, 2 * time_us);
> +       }
> +
> +       i2c_unlock_bus(data->client->adapter, I2C_LOCK_SEGMENT);
> +
> +       ret = i2c_smbus_read_word_data(data->client, AS73211_OUT_OSR_STATUS);
> +       if (ret < 0)
> +               return ret;
> +
> +       osr_status = ret;
> +       if (osr_status != (AS73211_OSR_DOS_MEASURE | AS73211_OSR_STATUS_NDATA)) {
> +               if (osr_status & AS73211_OSR_SS) {
> +                       dev_err(dev, "%s() Measurement has not stopped\n", __func__);
> +                       return -ETIME;
> +               }
> +               if (osr_status & AS73211_OSR_STATUS_NOTREADY) {
> +                       dev_err(dev, "%s() Data is not ready\n", __func__);
> +                       return -ENODATA;
> +               }
> +               if (!(osr_status & AS73211_OSR_STATUS_NDATA)) {
> +                       dev_err(dev, "%s() No new data available\n", __func__);
> +                       return -ENODATA;
> +               }
> +               if (osr_status & AS73211_OSR_STATUS_LDATA) {
> +                       dev_err(dev, "%s() Result buffer overrun\n", __func__);
> +                       return -ENOBUFS;
> +               }
> +               if (osr_status & AS73211_OSR_STATUS_ADCOF) {
> +                       dev_err(dev, "%s() ADC overflow\n", __func__);
> +                       return -EOVERFLOW;
> +               }
> +               if (osr_status & AS73211_OSR_STATUS_MRESOF) {
> +                       dev_err(dev, "%s() Measurement result overflow\n", __func__);
> +                       return -EOVERFLOW;
> +               }
> +               if (osr_status & AS73211_OSR_STATUS_OUTCONVOF) {
> +                       dev_err(dev, "%s() Timer overflow\n", __func__);
> +                       return -EOVERFLOW;
> +               }
> +               dev_err(dev, "%s() Unexpected status value\n", __func__);
> +               return -EIO;
> +       }
> +
> +       return 0;
> +}
> +
> +static int as73211_read_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan,
> +                            int *val, int *val2, long mask)
> +{
> +       struct as73211_data *data = iio_priv(indio_dev);
> +
> +       switch (mask) {
> +       case IIO_CHAN_INFO_RAW: {
> +               int ret;
> +
> +               ret = iio_device_claim_direct_mode(indio_dev);
> +               if (ret < 0)
> +                       return ret;
> +
> +               ret = as73211_req_data(data);
> +               if (ret < 0) {
> +                       iio_device_release_direct_mode(indio_dev);
> +                       return ret;
> +               }
> +
> +               ret = i2c_smbus_read_word_data(data->client, chan->address);
> +               iio_device_release_direct_mode(indio_dev);
> +               if (ret < 0)
> +                       return ret;
> +
> +               *val = ret;
> +               return IIO_VAL_INT;
> +       }
> +       case IIO_CHAN_INFO_OFFSET:
> +               *val = AS73211_OFFSET_TEMP_INT;
> +               *val2 = AS73211_OFFSET_TEMP_MICRO;
> +               return IIO_VAL_INT_PLUS_MICRO;
> +
> +       case IIO_CHAN_INFO_SCALE:
> +               switch (chan->type) {
> +               case IIO_TEMP:
> +                       *val = AS73211_SCALE_TEMP_INT;
> +                       *val2 = AS73211_SCALE_TEMP_MICRO;
> +                       return IIO_VAL_INT_PLUS_MICRO;
> +
> +               case IIO_INTENSITY: {
> +                       unsigned int scale;
> +
> +                       switch (chan->channel2) {
> +                       case IIO_MOD_X:
> +                               scale = AS73211_SCALE_X;
> +                               break;
> +                       case IIO_MOD_Y:
> +                               scale = AS73211_SCALE_Y;
> +                               break;
> +                       case IIO_MOD_Z:
> +                               scale = AS73211_SCALE_Z;
> +                               break;
> +                       default:
> +                               return -EINVAL;
> +                       }
> +                       scale /= as73211_gain(data);
> +                       scale /= as73211_integration_time_1024cyc(data);
> +                       *val = scale;
> +                       return IIO_VAL_INT;
> +
> +               default:
> +                       return -EINVAL;
> +               }}
> +
> +       case IIO_CHAN_INFO_SAMP_FREQ:
> +               /* f_samp is configured in CREG3 in powers of 2 (x 1.024 MHz) */
> +               *val = BIT(FIELD_GET(AS73211_CREG3_CCLK_MASK, data->creg3)) *
> +                       AS73211_SAMPLE_FREQ_BASE;
> +               return IIO_VAL_INT;
> +
> +       case IIO_CHAN_INFO_HARDWAREGAIN:
> +               *val = as73211_gain(data);
> +               return IIO_VAL_INT;
> +
> +       case IIO_CHAN_INFO_INT_TIME: {
> +               unsigned int time_us;
> +
> +               mutex_lock(&data->mutex);
> +               time_us = as73211_integration_time_us(data, as73211_integration_time_1024cyc(data));
> +               mutex_unlock(&data->mutex);
> +               *val = time_us / USEC_PER_SEC;
> +               *val2 = time_us % USEC_PER_SEC;
> +               return IIO_VAL_INT_PLUS_MICRO;
> +
> +       default:
> +               return -EINVAL;
> +       }}
> +}
> +
> +static int as73211_read_avail(struct iio_dev *indio_dev, struct iio_chan_spec const *chan,
> +                              const int **vals, int *type, int *length, long mask)
> +{
> +       struct as73211_data *data = iio_priv(indio_dev);
> +
> +       switch (mask) {
> +       case IIO_CHAN_INFO_SAMP_FREQ:
> +               *length = ARRAY_SIZE(as73211_samp_freq_avail);
> +               *vals = as73211_samp_freq_avail;
> +               *type = IIO_VAL_INT;
> +               return IIO_AVAIL_LIST;
> +
> +       case IIO_CHAN_INFO_HARDWAREGAIN:
> +               *length = ARRAY_SIZE(as73211_hardwaregain_avail);
> +               *vals = as73211_hardwaregain_avail;
> +               *type = IIO_VAL_INT;
> +               return IIO_AVAIL_LIST;
> +
> +       case IIO_CHAN_INFO_INT_TIME:
> +               *length = ARRAY_SIZE(data->int_time_avail);
> +               *vals = data->int_time_avail;
> +               *type = IIO_VAL_INT_PLUS_MICRO;
> +               return IIO_AVAIL_LIST;
> +
> +       default:
> +               return -EINVAL;
> +       }
> +}
> +
> +static int _as73211_write_raw(struct iio_dev *indio_dev,
> +                              struct iio_chan_spec const *chan __always_unused,
> +                              int val, int val2, long mask)
> +{
> +       struct as73211_data *data = iio_priv(indio_dev);
> +       int ret;
> +
> +       switch (mask) {
> +       case IIO_CHAN_INFO_SAMP_FREQ: {
> +               int reg_bits, freq_kHz = val / HZ_PER_KHZ;  /* 1024, 2048, ... */
> +
> +               /* val must be 1024 * 2^x */
> +               if (val < 0 || (freq_kHz * HZ_PER_KHZ) != val ||
> +                               !is_power_of_2(freq_kHz) || val2)
> +                       return -EINVAL;
> +
> +               /* f_samp is configured in CREG3 in powers of 2 (x 1.024 MHz (=2^10)) */
> +               reg_bits = ilog2(freq_kHz) - 10;
> +               if (!FIELD_FIT(AS73211_CREG3_CCLK_MASK, reg_bits))
> +                       return -EINVAL;
> +
> +               data->creg3 &= ~AS73211_CREG3_CCLK_MASK;
> +               data->creg3 |= FIELD_PREP(AS73211_CREG3_CCLK_MASK, reg_bits);
> +               as73211_integration_time_calc_avail(data);
> +
> +               ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_CREG3, data->creg3);
> +               if (ret < 0)
> +                       return ret;
> +
> +               return 0;
> +       }
> +       case IIO_CHAN_INFO_HARDWAREGAIN: {
> +               unsigned int reg_bits;
> +
> +               if (val < 0 || !is_power_of_2(val) || val2)
> +                       return -EINVAL;
> +
> +               /* gain can be calculated from CREG1 as 2^(13 - CREG1_GAIN) */
> +               reg_bits = AS73211_CREG1_GAIN_1 - ilog2(val);
> +               if (!FIELD_FIT(AS73211_CREG1_GAIN_MASK, reg_bits))
> +                       return -EINVAL;
> +
> +               data->creg1 &= ~AS73211_CREG1_GAIN_MASK;
> +               data->creg1 |= FIELD_PREP(AS73211_CREG1_GAIN_MASK, reg_bits);
> +
> +               ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_CREG1, data->creg1);
> +               if (ret < 0)
> +                       return ret;
> +
> +               return 0;
> +       }
> +       case IIO_CHAN_INFO_INT_TIME: {
> +               int val_us = val * USEC_PER_SEC + val2;
> +               int time_ms;
> +               int reg_bits;
> +
> +               /* f_samp is configured in CREG3 in powers of 2 (x 1.024 MHz) */
> +               int f_samp_1_024mhz = BIT(FIELD_GET(AS73211_CREG3_CCLK_MASK, data->creg3));
> +
> +               /*
> +                * time_ms = time_us * US_PER_MS * f_samp_1_024mhz / MHZ_PER_HZ
> +                *         = time_us * f_samp_1_024mhz / 1000
> +                */
> +               time_ms = (val_us * f_samp_1_024mhz) / 1000;  /* 1 ms, 2 ms, ... (power of two) */
> +               if (time_ms < 0 || !is_power_of_2(time_ms) || time_ms > AS73211_SAMPLE_TIME_MAX_MS)
> +                       return -EINVAL;
> +
> +               reg_bits = ilog2(time_ms);
> +               if (!FIELD_FIT(AS73211_CREG1_TIME_MASK, reg_bits))
> +                       return -EINVAL;  /* not possible due to previous tests */
> +
> +               data->creg1 &= ~AS73211_CREG1_TIME_MASK;
> +               data->creg1 |= FIELD_PREP(AS73211_CREG1_TIME_MASK, reg_bits);
> +
> +               ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_CREG1, data->creg1);
> +               if (ret < 0)
> +                       return ret;
> +
> +               return 0;
> +
> +       default:
> +               return -EINVAL;
> +       }}
> +}
> +
> +static int as73211_write_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan,
> +                             int val, int val2, long mask)
> +{
> +       struct as73211_data *data = iio_priv(indio_dev);
> +       int ret;
> +
> +       mutex_lock(&data->mutex);
> +
> +       ret = iio_device_claim_direct_mode(indio_dev);
> +       if (ret < 0)
> +               goto error_unlock;
> +
> +       /* Need to switch to config mode ... */
> +       if ((data->osr & AS73211_OSR_DOS_MASK) != AS73211_OSR_DOS_CONFIG) {
> +               data->osr &= ~AS73211_OSR_DOS_MASK;
> +               data->osr |= AS73211_OSR_DOS_CONFIG;
> +
> +               ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_OSR, data->osr);
> +               if (ret < 0)
> +                       goto error_release;
> +       }
> +
> +       ret = _as73211_write_raw(indio_dev, chan, val, val2, mask);
> +
> +error_release:
> +       iio_device_release_direct_mode(indio_dev);
> +error_unlock:
> +       mutex_unlock(&data->mutex);
> +       return ret;
> +}
> +
> +static irqreturn_t as73211_ready_handler(int irq __always_unused, void *priv)
> +{
> +       struct as73211_data *data = iio_priv(priv);
> +
> +       complete(&data->completion);
> +
> +       return IRQ_HANDLED;
> +}
> +
> +static irqreturn_t as73211_trigger_handler(int irq __always_unused, void *p)
> +{
> +       struct iio_poll_func *pf = p;
> +       struct iio_dev *indio_dev = pf->indio_dev;
> +       struct as73211_data *data = iio_priv(indio_dev);
> +       struct {
> +               __le16 chan[4];
> +               s64 ts __aligned(8);
> +       } scan;
> +       int data_result, ret;
> +
> +       mutex_lock(&data->mutex);
> +
> +       data_result = as73211_req_data(data);
> +       if (data_result < 0 && data_result != -EOVERFLOW)
> +               goto done;  /* don't push any data for errors other than EOVERFLOW */
> +
> +       if (*indio_dev->active_scan_mask == AS73211_SCAN_MASK_ALL) {
> +               /* Optimization for reading all (color + temperature) channels */
> +               u8 addr = as73211_channels[0].address;
> +               struct i2c_msg msgs[] = {
> +                       {
> +                               .addr = data->client->addr,
> +                               .flags = 0,
> +                               .len = 1,
> +                               .buf = &addr,
> +                       },
> +                       {
> +                               .addr = data->client->addr,
> +                               .flags = I2C_M_RD,
> +                               .len = sizeof(scan.chan),
> +                               .buf = (u8 *)&scan.chan,
> +                       },
> +               };
> +
> +               ret = i2c_transfer(data->client->adapter, msgs, ARRAY_SIZE(msgs));
> +               if (ret < 0)
> +                       goto done;
> +       } else {
> +               /* Optimization for reading only color channels */
> +
> +               /* AS73211 starts reading at address 2 */
> +               ret = i2c_master_recv(data->client,
> +                               (char *)&scan.chan[1], 3 * sizeof(scan.chan[1]));
> +               if (ret < 0)
> +                       goto done;
> +       }
> +
> +       if (data_result) {
> +               /*
> +                * Saturate all channels (in case of overflows). Temperature channel
> +                * is not affected by overflows.
> +                */
> +               scan.chan[1] = cpu_to_le16(U16_MAX);
> +               scan.chan[2] = cpu_to_le16(U16_MAX);
> +               scan.chan[3] = cpu_to_le16(U16_MAX);
> +       }
> +
> +       iio_push_to_buffers_with_timestamp(indio_dev, &scan, iio_get_time_ns(indio_dev));
> +
> +done:
> +       mutex_unlock(&data->mutex);
> +       iio_trigger_notify_done(indio_dev->trig);
> +
> +       return IRQ_HANDLED;
> +}
> +
> +static const struct iio_info as73211_info = {
> +       .read_raw = as73211_read_raw,
> +       .read_avail = as73211_read_avail,
> +       .write_raw = as73211_write_raw,
> +};
> +
> +static int as73211_power(struct iio_dev *indio_dev, bool state)
> +{
> +       struct as73211_data *data = iio_priv(indio_dev);
> +       int ret;
> +
> +       mutex_lock(&data->mutex);
> +
> +       if (state)
> +               data->osr &= ~AS73211_OSR_PD;
> +       else
> +               data->osr |= AS73211_OSR_PD;
> +
> +       ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_OSR, data->osr);
> +
> +       mutex_unlock(&data->mutex);
> +
> +       if (ret < 0)
> +               return ret;
> +
> +       return 0;
> +}
> +
> +static void as73211_power_disable(void *data)
> +{
> +       struct iio_dev *indio_dev = data;
> +
> +       as73211_power(indio_dev, false);
> +}
> +
> +static int as73211_probe(struct i2c_client *client)
> +{
> +       struct device *dev = &client->dev;
> +       struct as73211_data *data;
> +       struct iio_dev *indio_dev;
> +       int ret;
> +
> +       indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
> +       if (!indio_dev)
> +               return -ENOMEM;
> +
> +       data = iio_priv(indio_dev);
> +       i2c_set_clientdata(client, indio_dev);
> +       data->client = client;
> +
> +       mutex_init(&data->mutex);
> +       init_completion(&data->completion);
> +
> +       indio_dev->info = &as73211_info;
> +       indio_dev->name = AS73211_DRV_NAME;
> +       indio_dev->channels = as73211_channels;
> +       indio_dev->num_channels = ARRAY_SIZE(as73211_channels);
> +       indio_dev->modes = INDIO_DIRECT_MODE;
> +
> +       ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_OSR);
> +       if (ret < 0)
> +               return ret;
> +       data->osr = ret;
> +
> +       /* reset device */
> +       data->osr |= AS73211_OSR_SW_RES;
> +       ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_OSR, data->osr);
> +       if (ret < 0)
> +               return ret;
> +
> +       ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_OSR);
> +       if (ret < 0)
> +               return ret;
> +       data->osr = ret;
> +
> +       /*
> +        * Reading AGEN is only possible after reset (AGEN is not available if
> +        * device is in measurement mode).
> +        */
> +       ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_AGEN);
> +       if (ret < 0)
> +               return ret;
> +
> +       /* At the time of writing this driver, only DEVID 2 and MUT 1 are known. */
> +       if ((ret & AS73211_AGEN_DEVID_MASK) != AS73211_AGEN_DEVID(2) ||
> +           (ret & AS73211_AGEN_MUT_MASK) != AS73211_AGEN_MUT(1))
> +               return -ENODEV;
> +
> +       ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_CREG1);
> +       if (ret < 0)
> +               return ret;
> +       data->creg1 = ret;
> +
> +       ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_CREG2);
> +       if (ret < 0)
> +               return ret;
> +       data->creg2 = ret;
> +
> +       ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_CREG3);
> +       if (ret < 0)
> +               return ret;
> +       data->creg3 = ret;
> +       as73211_integration_time_calc_avail(data);
> +
> +       ret = as73211_power(indio_dev, true);
> +       if (ret < 0)
> +               return ret;
> +
> +       ret = devm_add_action_or_reset(dev, as73211_power_disable, indio_dev);
> +       if (ret)
> +               return ret;
> +
> +       ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL, as73211_trigger_handler, NULL);
> +       if (ret)
> +               return ret;
> +
> +       if (client->irq) {
> +               ret = devm_request_threaded_irq(&client->dev, client->irq,
> +                               NULL,
> +                               as73211_ready_handler,
> +                               IRQF_ONESHOT,
> +                               client->name, indio_dev);
> +               if (ret)
> +                       return ret;
> +       }
> +
> +       return devm_iio_device_register(dev, indio_dev);
> +}
> +
> +static int __maybe_unused as73211_suspend(struct device *dev)
> +{
> +       struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
> +
> +       return as73211_power(indio_dev, false);
> +}
> +
> +static int __maybe_unused as73211_resume(struct device *dev)
> +{
> +       struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
> +
> +       return as73211_power(indio_dev, true);
> +}
> +
> +static SIMPLE_DEV_PM_OPS(as73211_pm_ops, as73211_suspend, as73211_resume);
> +
> +static const struct of_device_id as73211_of_match[] = {
> +       { .compatible = "ams,as73211" },
> +       { }
> +};
> +MODULE_DEVICE_TABLE(of, as73211_of_match);
> +
> +static const struct i2c_device_id as73211_id[] = {
> +       { "as73211", 0 },
> +       { }
> +};
> +MODULE_DEVICE_TABLE(i2c, as73211_id);
> +
> +static struct i2c_driver as73211_driver = {
> +       .driver = {
> +               .name           = AS73211_DRV_NAME,
> +               .of_match_table = as73211_of_match,
> +               .pm             = &as73211_pm_ops,
> +       },
> +       .probe_new  = as73211_probe,
> +       .id_table   = as73211_id,
> +};
> +module_i2c_driver(as73211_driver);
> +
> +MODULE_AUTHOR("Christian Eggers <ceggers@arri.de>");
> +MODULE_DESCRIPTION("AS73211 XYZ True Color Sensor driver");
> +MODULE_LICENSE("GPL");
> --
> Christian Eggers
> Embedded software developer
>
> Arnold & Richter Cine Technik GmbH & Co. Betriebs KG
> Sitz: Muenchen - Registergericht: Amtsgericht Muenchen - Handelsregisternummer: HRA 57918
> Persoenlich haftender Gesellschafter: Arnold & Richter Cine Technik GmbH
> Sitz: Muenchen - Registergericht: Amtsgericht Muenchen - Handelsregisternummer: HRB 54477
> Geschaeftsfuehrer: Dr. Michael Neuhaeuser; Stephan Schenk; Walter Trauninger; Markus Zeiler
>


-- 
With Best Regards,
Andy Shevchenko

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH v6 1/2] dt-bindings: iio: light: add AMS AS73211 support
  2020-08-05  5:57 ` [PATCH v6 1/2] dt-bindings: iio: light: add AMS AS73211 support Christian Eggers
@ 2020-08-06 17:44   ` Jonathan Cameron
  0 siblings, 0 replies; 20+ messages in thread
From: Jonathan Cameron @ 2020-08-06 17:44 UTC (permalink / raw)
  To: Christian Eggers
  Cc: Rob Herring, Andy Shevchenko, Jonathan Cameron, Hartmut Knaack,
	Lars-Peter Clausen, Peter Meerwald-Stadler, linux-iio,
	devicetree, linux-kernel, Rob Herring

On Wed, 5 Aug 2020 07:57:43 +0200
Christian Eggers <ceggers@arri.de> wrote:

> Add DT bindings for AMS AS73211 XYZ True Color Sensor.
> 
> Signed-off-by: Christian Eggers <ceggers@arri.de>
> Reviewed-by: Rob Herring <robh@kernel.org>
Applied to the togreg branch of iio.git and pushed out as testing for the
autobuilders to poke at it.

Thanks,

Jonathan

> ---
>  .../bindings/iio/light/ams,as73211.yaml       | 54 +++++++++++++++++++
>  1 file changed, 54 insertions(+)
>  create mode 100644 Documentation/devicetree/bindings/iio/light/ams,as73211.yaml
> 
> diff --git a/Documentation/devicetree/bindings/iio/light/ams,as73211.yaml b/Documentation/devicetree/bindings/iio/light/ams,as73211.yaml
> new file mode 100644
> index 000000000000..0e8cd02759b3
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/iio/light/ams,as73211.yaml
> @@ -0,0 +1,54 @@
> +# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause
> +%YAML 1.2
> +---
> +$id: http://devicetree.org/schemas/iio/light/ams,as73211.yaml#
> +$schema: http://devicetree.org/meta-schemas/core.yaml#
> +
> +title: AMS AS73211 JENCOLOR(R) Digital XYZ Sensor
> +
> +maintainers:
> +  - Christian Eggers <ceggers@arri.de>
> +
> +description: |
> +  XYZ True Color Sensor with I2C Interface
> +  https://ams.com/documents/20143/36005/AS73211_DS000556_3-01.pdf/a65474c0-b302-c2fd-e30a-c98df87616df
> +
> +properties:
> +  compatible:
> +    enum:
> +      - ams,as73211
> +
> +  reg:
> +    description:
> +      I2C address of the device (0x74...0x77).
> +    maxItems: 1
> +
> +  interrupts:
> +    description:
> +      Interrupt specifier for the READY interrupt generated by the device.
> +    maxItems: 1
> +
> +required:
> +  - compatible
> +  - reg
> +
> +additionalProperties: false
> +
> +examples:
> +  - |
> +    #include <dt-bindings/interrupt-controller/irq.h>
> +
> +    i2c {
> +        #address-cells = <1>;
> +        #size-cells = <0>;
> +
> +        as73211@74 {
> +            compatible = "ams,as73211";
> +            reg = <0x74>;
> +            pinctrl-names = "default";
> +            pinctrl-0 = <&pinctrl_color_sensor>;
> +            interrupt-parent = <&gpio2>;
> +            interrupts = <19 IRQ_TYPE_EDGE_RISING>; /* READY */
> +        };
> +    };
> +...


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH v6 2/2] iio: light: as73211: New driver
  2020-08-05  5:57 ` [PATCH v6 2/2] iio: light: as73211: New driver Christian Eggers
  2020-08-05  7:39   ` Andy Shevchenko
@ 2020-08-06 17:44   ` Jonathan Cameron
  2020-08-07 15:46     ` Christian Eggers
  2020-09-01 18:46     ` [PATCH v6 2/2] iio: light: as73211: New driver Jonathan Cameron
  1 sibling, 2 replies; 20+ messages in thread
From: Jonathan Cameron @ 2020-08-06 17:44 UTC (permalink / raw)
  To: Christian Eggers
  Cc: Rob Herring, Andy Shevchenko, Jonathan Cameron, Hartmut Knaack,
	Lars-Peter Clausen, Peter Meerwald-Stadler, linux-iio,
	devicetree, linux-kernel

On Wed, 5 Aug 2020 07:57:44 +0200
Christian Eggers <ceggers@arri.de> wrote:

> Support for AMS AS73211 JENCOLOR(R) Digital XYZ Sensor.
> 
> This driver has no built-in trigger. In order for making triggered
> measurements, an external (software) trigger driver like
> iio-trig-hrtimer or iio-trig-sysfs is required.
> 
> The sensor supports single and continuous measurement modes. The latter
> is not used by design as this would require tight timing synchronization
> between hardware and driver without much benefit.
> 
> Datasheet: https://ams.com/documents/20143/36005/AS73211_DS000556_3-01.pdf
> Signed-off-by: Christian Eggers <ceggers@arri.de>

Hi Christian,

I'll take this, but please send a follow up patch to add documentation
for in_intensity_x_raw and all the other new ABI this adds in
Documentation/ABI/testing/sysfs-bus-iio
I should have mentioned that earlier, but kind of assumed we already
had these documented for some reason!

Applied to the togreg branch of iio.git and pushed out as testing for
the autobuilders to play with it.

Thanks,

Jonathan


> ---
>  MAINTAINERS                 |   7 +
>  drivers/iio/light/Kconfig   |  15 +
>  drivers/iio/light/Makefile  |   1 +
>  drivers/iio/light/as73211.c | 801 ++++++++++++++++++++++++++++++++++++
>  4 files changed, 824 insertions(+)
>  create mode 100644 drivers/iio/light/as73211.c
> 
> diff --git a/MAINTAINERS b/MAINTAINERS
> index 068d6e94122b..673570414147 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -934,6 +934,13 @@ S:	Supported
>  F:	arch/arm64/boot/dts/amd/amd-seattle-xgbe*.dtsi
>  F:	drivers/net/ethernet/amd/xgbe/
>  
> +AMS AS73211 DRIVER
> +M:	Christian Eggers <ceggers@arri.de>
> +L:	linux-iio@vger.kernel.org
> +S:	Maintained
> +F:	Documentation/devicetree/bindings/iio/light/ams,as73211.yaml
> +F:	drivers/iio/light/as73211.c
> +
>  ANALOG DEVICES INC AD5686 DRIVER
>  M:	Michael Hennerich <Michael.Hennerich@analog.com>
>  L:	linux-pm@vger.kernel.org
> diff --git a/drivers/iio/light/Kconfig b/drivers/iio/light/Kconfig
> index 182bd18c4bb2..cade6dc0305b 100644
> --- a/drivers/iio/light/Kconfig
> +++ b/drivers/iio/light/Kconfig
> @@ -86,6 +86,21 @@ config APDS9960
>  	  To compile this driver as a module, choose M here: the
>  	  module will be called apds9960
>  
> +config AS73211
> +	tristate "AMS AS73211 XYZ color sensor"
> +	depends on I2C
> +	select IIO_BUFFER
> +	select IIO_TRIGGERED_BUFFER
> +	help
> +	 If you say yes here you get support for the AMS AS73211
> +	 JENCOLOR(R) Digital XYZ Sensor.
> +
> +	 For triggered measurements, you will need an additional trigger driver
> +	 like IIO_HRTIMER_TRIGGER or IIO_SYSFS_TRIGGER.
> +
> +	 This driver can also be built as a module.  If so, the module
> +	 will be called as73211.
> +
>  config BH1750
>  	tristate "ROHM BH1750 ambient light sensor"
>  	depends on I2C
> diff --git a/drivers/iio/light/Makefile b/drivers/iio/light/Makefile
> index d1c8aa30b9a8..ea376deaca54 100644
> --- a/drivers/iio/light/Makefile
> +++ b/drivers/iio/light/Makefile
> @@ -11,6 +11,7 @@ obj-$(CONFIG_AL3010)		+= al3010.o
>  obj-$(CONFIG_AL3320A)		+= al3320a.o
>  obj-$(CONFIG_APDS9300)		+= apds9300.o
>  obj-$(CONFIG_APDS9960)		+= apds9960.o
> +obj-$(CONFIG_AS73211)		+= as73211.o
>  obj-$(CONFIG_BH1750)		+= bh1750.o
>  obj-$(CONFIG_BH1780)		+= bh1780.o
>  obj-$(CONFIG_CM32181)		+= cm32181.o
> diff --git a/drivers/iio/light/as73211.c b/drivers/iio/light/as73211.c
> new file mode 100644
> index 000000000000..3383aaacbf52
> --- /dev/null
> +++ b/drivers/iio/light/as73211.c
> @@ -0,0 +1,801 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +/*
> + * Support for AMS AS73211 JENCOLOR(R) Digital XYZ Sensor
> + *
> + * Author: Christian Eggers <ceggers@arri.de>
> + *
> + * Copyright (c) 2020 ARRI Lighting
> + *
> + * Color light sensor with 16-bit channels for x, y, z and temperature);
> + * 7-bit I2C slave address 0x74 .. 0x77.
> + *
> + * Datasheet: https://ams.com/documents/20143/36005/AS73211_DS000556_3-01.pdf
> + */
> +
> +#include <linux/bitfield.h>
> +#include <linux/completion.h>
> +#include <linux/delay.h>
> +#include <linux/i2c.h>
> +#include <linux/iio/buffer.h>
> +#include <linux/iio/iio.h>
> +#include <linux/iio/sysfs.h>
> +#include <linux/iio/trigger_consumer.h>
> +#include <linux/iio/triggered_buffer.h>
> +#include <linux/module.h>
> +#include <linux/mutex.h>
> +#include <linux/pm.h>
> +
> +#define HZ_PER_KHZ 1000
> +
> +#define AS73211_DRV_NAME "as73211"
> +
> +/* AS73211 configuration registers */
> +#define AS73211_REG_OSR    0x0
> +#define AS73211_REG_AGEN   0x2
> +#define AS73211_REG_CREG1  0x6
> +#define AS73211_REG_CREG2  0x7
> +#define AS73211_REG_CREG3  0x8
> +
> +/* AS73211 output register bank */
> +#define AS73211_OUT_OSR_STATUS    0
> +#define AS73211_OUT_TEMP          1
> +#define AS73211_OUT_MRES1         2
> +#define AS73211_OUT_MRES2         3
> +#define AS73211_OUT_MRES3         4
> +
> +#define AS73211_OSR_SS            BIT(7)
> +#define AS73211_OSR_PD            BIT(6)
> +#define AS73211_OSR_SW_RES        BIT(3)
> +#define AS73211_OSR_DOS_MASK      GENMASK(2, 0)
> +#define AS73211_OSR_DOS_CONFIG    FIELD_PREP(AS73211_OSR_DOS_MASK, 0x2)
> +#define AS73211_OSR_DOS_MEASURE   FIELD_PREP(AS73211_OSR_DOS_MASK, 0x3)
> +
> +#define AS73211_AGEN_DEVID_MASK   GENMASK(7, 4)
> +#define AS73211_AGEN_DEVID(x)     FIELD_PREP(AS73211_AGEN_DEVID_MASK, (x))
> +#define AS73211_AGEN_MUT_MASK     GENMASK(3, 0)
> +#define AS73211_AGEN_MUT(x)       FIELD_PREP(AS73211_AGEN_MUT_MASK, (x))
> +
> +#define AS73211_CREG1_GAIN_MASK   GENMASK(7, 4)
> +#define AS73211_CREG1_GAIN_1      13
> +#define AS73211_CREG1_TIME_MASK   GENMASK(3, 0)
> +
> +#define AS73211_CREG3_CCLK_MASK   GENMASK(1, 0)
> +
> +#define AS73211_OSR_STATUS_OUTCONVOF  BIT(15)
> +#define AS73211_OSR_STATUS_MRESOF     BIT(14)
> +#define AS73211_OSR_STATUS_ADCOF      BIT(13)
> +#define AS73211_OSR_STATUS_LDATA      BIT(12)
> +#define AS73211_OSR_STATUS_NDATA      BIT(11)
> +#define AS73211_OSR_STATUS_NOTREADY   BIT(10)
> +
> +#define AS73211_SAMPLE_FREQ_BASE      1024000
> +
> +#define AS73211_SAMPLE_TIME_NUM       15
> +#define AS73211_SAMPLE_TIME_MAX_MS    BIT(AS73211_SAMPLE_TIME_NUM - 1)
> +
> +/* Available sample frequencies are 1.024MHz multiplied by powers of two. */
> +static const int as73211_samp_freq_avail[] = {
> +	AS73211_SAMPLE_FREQ_BASE * 1,
> +	AS73211_SAMPLE_FREQ_BASE * 2,
> +	AS73211_SAMPLE_FREQ_BASE * 4,
> +	AS73211_SAMPLE_FREQ_BASE * 8,
> +};
> +
> +static const int as73211_hardwaregain_avail[] = {
> +	1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048,
> +};
> +
> +/**
> + * struct as73211_data - Instance data for one AS73211
> + * @client: I2C client.
> + * @osr:    Cached Operational State Register.
> + * @creg1:  Cached Configuration Register 1.
> + * @creg2:  Cached Configuration Register 2.
> + * @creg3:  Cached Configuration Register 3.
> + * @mutex:  Keeps cached registers in sync with the device.
> + * @completion: Completion to wait for interrupt.
> + * @int_time_avail: Available integration times (depend on sampling frequency).
> + */
> +struct as73211_data {
> +	struct i2c_client *client;
> +	u8 osr;
> +	u8 creg1;
> +	u8 creg2;
> +	u8 creg3;
> +	struct mutex mutex;
> +	struct completion completion;
> +	int int_time_avail[AS73211_SAMPLE_TIME_NUM * 2];
> +};
> +
> +#define AS73211_COLOR_CHANNEL(_color, _si, _addr) { \
> +	.type = IIO_INTENSITY, \
> +	.modified = 1, \
> +	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), \
> +	.info_mask_shared_by_type = \
> +		BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
> +		BIT(IIO_CHAN_INFO_HARDWAREGAIN) | \
> +		BIT(IIO_CHAN_INFO_INT_TIME), \
> +	.info_mask_shared_by_type_available = \
> +		BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
> +		BIT(IIO_CHAN_INFO_HARDWAREGAIN) | \
> +		BIT(IIO_CHAN_INFO_INT_TIME), \
> +	.channel2 = IIO_MOD_##_color, \
> +	.address = _addr, \
> +	.scan_index = _si, \
> +	.scan_type = { \
> +		.sign = 'u', \
> +		.realbits = 16, \
> +		.storagebits = 16, \
> +		.endianness = IIO_LE, \
> +	}, \
> +}
> +
> +#define AS73211_OFFSET_TEMP_INT    (-66)
> +#define AS73211_OFFSET_TEMP_MICRO  900000
> +#define AS73211_SCALE_TEMP_INT     0
> +#define AS73211_SCALE_TEMP_MICRO   50000
> +
> +#define AS73211_SCALE_X 277071108  /* nW/m^2 */
> +#define AS73211_SCALE_Y 298384270  /* nW/m^2 */
> +#define AS73211_SCALE_Z 160241927  /* nW/m^2 */
> +
> +/* Channel order MUST match devices result register order */
> +#define AS73211_SCAN_INDEX_TEMP 0
> +#define AS73211_SCAN_INDEX_X    1
> +#define AS73211_SCAN_INDEX_Y    2
> +#define AS73211_SCAN_INDEX_Z    3
> +#define AS73211_SCAN_INDEX_TS   4
> +
> +#define AS73211_SCAN_MASK_COLOR ( \
> +	BIT(AS73211_SCAN_INDEX_X) |   \
> +	BIT(AS73211_SCAN_INDEX_Y) |   \
> +	BIT(AS73211_SCAN_INDEX_Z))
> +
> +#define AS73211_SCAN_MASK_ALL (    \
> +	BIT(AS73211_SCAN_INDEX_TEMP) | \
> +	AS73211_SCAN_MASK_COLOR)
> +
> +static const struct iio_chan_spec as73211_channels[] = {
> +	{
> +		.type = IIO_TEMP,
> +		.info_mask_separate =
> +			BIT(IIO_CHAN_INFO_RAW) |
> +			BIT(IIO_CHAN_INFO_OFFSET) |
> +			BIT(IIO_CHAN_INFO_SCALE),
> +		.address = AS73211_OUT_TEMP,
> +		.scan_index = AS73211_SCAN_INDEX_TEMP,
> +		.scan_type = {
> +			.sign = 'u',
> +			.realbits = 16,
> +			.storagebits = 16,
> +			.endianness = IIO_LE,
> +		}
> +	},
> +	AS73211_COLOR_CHANNEL(X, AS73211_SCAN_INDEX_X, AS73211_OUT_MRES1),
> +	AS73211_COLOR_CHANNEL(Y, AS73211_SCAN_INDEX_Y, AS73211_OUT_MRES2),
> +	AS73211_COLOR_CHANNEL(Z, AS73211_SCAN_INDEX_Z, AS73211_OUT_MRES3),
> +	IIO_CHAN_SOFT_TIMESTAMP(AS73211_SCAN_INDEX_TS),
> +};
> +
> +static unsigned int as73211_integration_time_1024cyc(struct as73211_data *data)
> +{
> +	/*
> +	 * Return integration time in units of 1024 clock cycles. Integration time
> +	 * in CREG1 is in powers of 2 (x 1024 cycles).
> +	 */
> +	return BIT(FIELD_GET(AS73211_CREG1_TIME_MASK, data->creg1));
> +}
> +
> +static unsigned int as73211_integration_time_us(struct as73211_data *data,
> +						 unsigned int integration_time_1024cyc)
> +{
> +	/*
> +	 * f_samp is configured in CREG3 in powers of 2 (x 1.024 MHz)
> +	 * t_cycl is configured in CREG1 in powers of 2 (x 1024 cycles)
> +	 * t_int_us = 1 / (f_samp) * t_cycl * US_PER_SEC
> +	 *          = 1 / (2^CREG3_CCLK * 1,024,000) * 2^CREG1_CYCLES * 1,024 * US_PER_SEC
> +	 *          = 2^(-CREG3_CCLK) * 2^CREG1_CYCLES * 1,000
> +	 * In order to get rid of negative exponents, we extend the "fraction"
> +	 * by 2^3 (CREG3_CCLK,max = 3)
> +	 * t_int_us = 2^(3-CREG3_CCLK) * 2^CREG1_CYCLES * 125
> +	 */
> +	return BIT(3 - FIELD_GET(AS73211_CREG3_CCLK_MASK, data->creg3)) *
> +		integration_time_1024cyc * 125;
> +}
> +
> +static void as73211_integration_time_calc_avail(struct as73211_data *data)
> +{
> +	int i;
> +
> +	for (i = 0; i < ARRAY_SIZE(data->int_time_avail); i++) {
> +		unsigned int time_us = as73211_integration_time_us(data, BIT(i));
> +
> +		data->int_time_avail[i * 2 + 0] = time_us / USEC_PER_SEC;
> +		data->int_time_avail[i * 2 + 1] = time_us % USEC_PER_SEC;
> +	}
> +}
> +
> +static unsigned int as73211_gain(struct as73211_data *data)
> +{
> +	/* gain can be calculated from CREG1 as 2^(13 - CREG1_GAIN) */
> +	return BIT(AS73211_CREG1_GAIN_1 - FIELD_GET(AS73211_CREG1_GAIN_MASK, data->creg1));
> +}
> +
> +/* must be called with as73211_data::mutex held. */
> +static int as73211_req_data(struct as73211_data *data)
> +{
> +	unsigned int time_us = as73211_integration_time_us(data,
> +							    as73211_integration_time_1024cyc(data));
> +	struct device *dev = &data->client->dev;
> +	union i2c_smbus_data smbus_data;
> +	u16 osr_status;
> +	int ret;
> +
> +	if (data->client->irq)
> +		reinit_completion(&data->completion);
> +
> +	/*
> +	 * During measurement, there should be no traffic on the i2c bus as the
> +	 * electrical noise would disturb the measurement process.
> +	 */
> +	i2c_lock_bus(data->client->adapter, I2C_LOCK_SEGMENT);
> +
> +	data->osr &= ~AS73211_OSR_DOS_MASK;
> +	data->osr |= AS73211_OSR_DOS_MEASURE | AS73211_OSR_SS;
> +
> +	smbus_data.byte = data->osr;
> +	ret = __i2c_smbus_xfer(data->client->adapter, data->client->addr,
> +			data->client->flags, I2C_SMBUS_WRITE,
> +			AS73211_REG_OSR, I2C_SMBUS_BYTE_DATA, &smbus_data);
> +	if (ret < 0) {
> +		i2c_unlock_bus(data->client->adapter, I2C_LOCK_SEGMENT);
> +		return ret;
> +	}
> +
> +	/*
> +	 * Reset AS73211_OSR_SS (is self clearing) in order to avoid unintentional
> +	 * triggering of further measurements later.
> +	 */
> +	data->osr &= ~AS73211_OSR_SS;
> +
> +	/*
> +	 * Add some extra margin for the timeout. sensor timing is not as precise
> +	 * as our one ...
> +	 */
> +	time_us += time_us / 8;
> +	if (data->client->irq) {
> +		ret = wait_for_completion_timeout(&data->completion, usecs_to_jiffies(time_us));
> +		if (!ret) {
> +			dev_err(dev, "timeout waiting for READY IRQ\n");
> +			i2c_unlock_bus(data->client->adapter, I2C_LOCK_SEGMENT);
> +			return -ETIMEDOUT;
> +		}
> +	} else {
> +		/* Wait integration time */
> +		usleep_range(time_us, 2 * time_us);
> +	}
> +
> +	i2c_unlock_bus(data->client->adapter, I2C_LOCK_SEGMENT);
> +
> +	ret = i2c_smbus_read_word_data(data->client, AS73211_OUT_OSR_STATUS);
> +	if (ret < 0)
> +		return ret;
> +
> +	osr_status = ret;
> +	if (osr_status != (AS73211_OSR_DOS_MEASURE | AS73211_OSR_STATUS_NDATA)) {
> +		if (osr_status & AS73211_OSR_SS) {
> +			dev_err(dev, "%s() Measurement has not stopped\n", __func__);
> +			return -ETIME;
> +		}
> +		if (osr_status & AS73211_OSR_STATUS_NOTREADY) {
> +			dev_err(dev, "%s() Data is not ready\n", __func__);
> +			return -ENODATA;
> +		}
> +		if (!(osr_status & AS73211_OSR_STATUS_NDATA)) {
> +			dev_err(dev, "%s() No new data available\n", __func__);
> +			return -ENODATA;
> +		}
> +		if (osr_status & AS73211_OSR_STATUS_LDATA) {
> +			dev_err(dev, "%s() Result buffer overrun\n", __func__);
> +			return -ENOBUFS;
> +		}
> +		if (osr_status & AS73211_OSR_STATUS_ADCOF) {
> +			dev_err(dev, "%s() ADC overflow\n", __func__);
> +			return -EOVERFLOW;
> +		}
> +		if (osr_status & AS73211_OSR_STATUS_MRESOF) {
> +			dev_err(dev, "%s() Measurement result overflow\n", __func__);
> +			return -EOVERFLOW;
> +		}
> +		if (osr_status & AS73211_OSR_STATUS_OUTCONVOF) {
> +			dev_err(dev, "%s() Timer overflow\n", __func__);
> +			return -EOVERFLOW;
> +		}
> +		dev_err(dev, "%s() Unexpected status value\n", __func__);
> +		return -EIO;
> +	}
> +
> +	return 0;
> +}
> +
> +static int as73211_read_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan,
> +			     int *val, int *val2, long mask)
> +{
> +	struct as73211_data *data = iio_priv(indio_dev);
> +
> +	switch (mask) {
> +	case IIO_CHAN_INFO_RAW: {
> +		int ret;
> +
> +		ret = iio_device_claim_direct_mode(indio_dev);
> +		if (ret < 0)
> +			return ret;
> +
> +		ret = as73211_req_data(data);
> +		if (ret < 0) {
> +			iio_device_release_direct_mode(indio_dev);
> +			return ret;
> +		}
> +
> +		ret = i2c_smbus_read_word_data(data->client, chan->address);
> +		iio_device_release_direct_mode(indio_dev);
> +		if (ret < 0)
> +			return ret;
> +
> +		*val = ret;
> +		return IIO_VAL_INT;
> +	}
> +	case IIO_CHAN_INFO_OFFSET:
> +		*val = AS73211_OFFSET_TEMP_INT;
> +		*val2 = AS73211_OFFSET_TEMP_MICRO;
> +		return IIO_VAL_INT_PLUS_MICRO;
> +
> +	case IIO_CHAN_INFO_SCALE:
> +		switch (chan->type) {
> +		case IIO_TEMP:
> +			*val = AS73211_SCALE_TEMP_INT;
> +			*val2 = AS73211_SCALE_TEMP_MICRO;
> +			return IIO_VAL_INT_PLUS_MICRO;
> +
> +		case IIO_INTENSITY: {
> +			unsigned int scale;
> +
> +			switch (chan->channel2) {
> +			case IIO_MOD_X:
> +				scale = AS73211_SCALE_X;
> +				break;
> +			case IIO_MOD_Y:
> +				scale = AS73211_SCALE_Y;
> +				break;
> +			case IIO_MOD_Z:
> +				scale = AS73211_SCALE_Z;
> +				break;
> +			default:
> +				return -EINVAL;
> +			}
> +			scale /= as73211_gain(data);
> +			scale /= as73211_integration_time_1024cyc(data);
> +			*val = scale;
> +			return IIO_VAL_INT;
> +
> +		default:
> +			return -EINVAL;
> +		}}
> +
> +	case IIO_CHAN_INFO_SAMP_FREQ:
> +		/* f_samp is configured in CREG3 in powers of 2 (x 1.024 MHz) */
> +		*val = BIT(FIELD_GET(AS73211_CREG3_CCLK_MASK, data->creg3)) *
> +			AS73211_SAMPLE_FREQ_BASE;
> +		return IIO_VAL_INT;
> +
> +	case IIO_CHAN_INFO_HARDWAREGAIN:
> +		*val = as73211_gain(data);
> +		return IIO_VAL_INT;
> +
> +	case IIO_CHAN_INFO_INT_TIME: {
> +		unsigned int time_us;
> +
> +		mutex_lock(&data->mutex);
> +		time_us = as73211_integration_time_us(data, as73211_integration_time_1024cyc(data));
> +		mutex_unlock(&data->mutex);
> +		*val = time_us / USEC_PER_SEC;
> +		*val2 = time_us % USEC_PER_SEC;
> +		return IIO_VAL_INT_PLUS_MICRO;
> +
> +	default:
> +		return -EINVAL;
> +	}}
> +}
> +
> +static int as73211_read_avail(struct iio_dev *indio_dev, struct iio_chan_spec const *chan,
> +			       const int **vals, int *type, int *length, long mask)
> +{
> +	struct as73211_data *data = iio_priv(indio_dev);
> +
> +	switch (mask) {
> +	case IIO_CHAN_INFO_SAMP_FREQ:
> +		*length = ARRAY_SIZE(as73211_samp_freq_avail);
> +		*vals = as73211_samp_freq_avail;
> +		*type = IIO_VAL_INT;
> +		return IIO_AVAIL_LIST;
> +
> +	case IIO_CHAN_INFO_HARDWAREGAIN:
> +		*length = ARRAY_SIZE(as73211_hardwaregain_avail);
> +		*vals = as73211_hardwaregain_avail;
> +		*type = IIO_VAL_INT;
> +		return IIO_AVAIL_LIST;
> +
> +	case IIO_CHAN_INFO_INT_TIME:
> +		*length = ARRAY_SIZE(data->int_time_avail);
> +		*vals = data->int_time_avail;
> +		*type = IIO_VAL_INT_PLUS_MICRO;
> +		return IIO_AVAIL_LIST;
> +
> +	default:
> +		return -EINVAL;
> +	}
> +}
> +
> +static int _as73211_write_raw(struct iio_dev *indio_dev,
> +			       struct iio_chan_spec const *chan __always_unused,
> +			       int val, int val2, long mask)
> +{
> +	struct as73211_data *data = iio_priv(indio_dev);
> +	int ret;
> +
> +	switch (mask) {
> +	case IIO_CHAN_INFO_SAMP_FREQ: {
> +		int reg_bits, freq_kHz = val / HZ_PER_KHZ;  /* 1024, 2048, ... */
> +
> +		/* val must be 1024 * 2^x */
> +		if (val < 0 || (freq_kHz * HZ_PER_KHZ) != val ||
> +				!is_power_of_2(freq_kHz) || val2)
> +			return -EINVAL;
> +
> +		/* f_samp is configured in CREG3 in powers of 2 (x 1.024 MHz (=2^10)) */
> +		reg_bits = ilog2(freq_kHz) - 10;
> +		if (!FIELD_FIT(AS73211_CREG3_CCLK_MASK, reg_bits))
> +			return -EINVAL;
> +
> +		data->creg3 &= ~AS73211_CREG3_CCLK_MASK;
> +		data->creg3 |= FIELD_PREP(AS73211_CREG3_CCLK_MASK, reg_bits);
> +		as73211_integration_time_calc_avail(data);
> +
> +		ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_CREG3, data->creg3);
> +		if (ret < 0)
> +			return ret;
> +
> +		return 0;
> +	}
> +	case IIO_CHAN_INFO_HARDWAREGAIN: {
> +		unsigned int reg_bits;
> +
> +		if (val < 0 || !is_power_of_2(val) || val2)
> +			return -EINVAL;
> +
> +		/* gain can be calculated from CREG1 as 2^(13 - CREG1_GAIN) */
> +		reg_bits = AS73211_CREG1_GAIN_1 - ilog2(val);
> +		if (!FIELD_FIT(AS73211_CREG1_GAIN_MASK, reg_bits))
> +			return -EINVAL;
> +
> +		data->creg1 &= ~AS73211_CREG1_GAIN_MASK;
> +		data->creg1 |= FIELD_PREP(AS73211_CREG1_GAIN_MASK, reg_bits);
> +
> +		ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_CREG1, data->creg1);
> +		if (ret < 0)
> +			return ret;
> +
> +		return 0;
> +	}
> +	case IIO_CHAN_INFO_INT_TIME: {
> +		int val_us = val * USEC_PER_SEC + val2;
> +		int time_ms;
> +		int reg_bits;
> +
> +		/* f_samp is configured in CREG3 in powers of 2 (x 1.024 MHz) */
> +		int f_samp_1_024mhz = BIT(FIELD_GET(AS73211_CREG3_CCLK_MASK, data->creg3));
> +
> +		/*
> +		 * time_ms = time_us * US_PER_MS * f_samp_1_024mhz / MHZ_PER_HZ
> +		 *         = time_us * f_samp_1_024mhz / 1000
> +		 */
> +		time_ms = (val_us * f_samp_1_024mhz) / 1000;  /* 1 ms, 2 ms, ... (power of two) */
> +		if (time_ms < 0 || !is_power_of_2(time_ms) || time_ms > AS73211_SAMPLE_TIME_MAX_MS)
> +			return -EINVAL;
> +
> +		reg_bits = ilog2(time_ms);
> +		if (!FIELD_FIT(AS73211_CREG1_TIME_MASK, reg_bits))
> +			return -EINVAL;  /* not possible due to previous tests */
> +
> +		data->creg1 &= ~AS73211_CREG1_TIME_MASK;
> +		data->creg1 |= FIELD_PREP(AS73211_CREG1_TIME_MASK, reg_bits);
> +
> +		ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_CREG1, data->creg1);
> +		if (ret < 0)
> +			return ret;
> +
> +		return 0;
> +
> +	default:
> +		return -EINVAL;
> +	}}
> +}
> +
> +static int as73211_write_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan,
> +			      int val, int val2, long mask)
> +{
> +	struct as73211_data *data = iio_priv(indio_dev);
> +	int ret;
> +
> +	mutex_lock(&data->mutex);
> +
> +	ret = iio_device_claim_direct_mode(indio_dev);
> +	if (ret < 0)
> +		goto error_unlock;
> +
> +	/* Need to switch to config mode ... */
> +	if ((data->osr & AS73211_OSR_DOS_MASK) != AS73211_OSR_DOS_CONFIG) {
> +		data->osr &= ~AS73211_OSR_DOS_MASK;
> +		data->osr |= AS73211_OSR_DOS_CONFIG;
> +
> +		ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_OSR, data->osr);
> +		if (ret < 0)
> +			goto error_release;
> +	}
> +
> +	ret = _as73211_write_raw(indio_dev, chan, val, val2, mask);
> +
> +error_release:
> +	iio_device_release_direct_mode(indio_dev);
> +error_unlock:
> +	mutex_unlock(&data->mutex);
> +	return ret;
> +}
> +
> +static irqreturn_t as73211_ready_handler(int irq __always_unused, void *priv)
> +{
> +	struct as73211_data *data = iio_priv(priv);
> +
> +	complete(&data->completion);
> +
> +	return IRQ_HANDLED;
> +}
> +
> +static irqreturn_t as73211_trigger_handler(int irq __always_unused, void *p)
> +{
> +	struct iio_poll_func *pf = p;
> +	struct iio_dev *indio_dev = pf->indio_dev;
> +	struct as73211_data *data = iio_priv(indio_dev);
> +	struct {
> +		__le16 chan[4];
> +		s64 ts __aligned(8);
> +	} scan;
> +	int data_result, ret;
> +
> +	mutex_lock(&data->mutex);
> +
> +	data_result = as73211_req_data(data);
> +	if (data_result < 0 && data_result != -EOVERFLOW)
> +		goto done;  /* don't push any data for errors other than EOVERFLOW */
> +
> +	if (*indio_dev->active_scan_mask == AS73211_SCAN_MASK_ALL) {
> +		/* Optimization for reading all (color + temperature) channels */
> +		u8 addr = as73211_channels[0].address;
> +		struct i2c_msg msgs[] = {
> +			{
> +				.addr = data->client->addr,
> +				.flags = 0,
> +				.len = 1,
> +				.buf = &addr,
> +			},
> +			{
> +				.addr = data->client->addr,
> +				.flags = I2C_M_RD,
> +				.len = sizeof(scan.chan),
> +				.buf = (u8 *)&scan.chan,
> +			},
> +		};
> +
> +		ret = i2c_transfer(data->client->adapter, msgs, ARRAY_SIZE(msgs));
> +		if (ret < 0)
> +			goto done;
> +	} else {
> +		/* Optimization for reading only color channels */
> +
> +		/* AS73211 starts reading at address 2 */
> +		ret = i2c_master_recv(data->client,
> +				(char *)&scan.chan[1], 3 * sizeof(scan.chan[1]));
> +		if (ret < 0)
> +			goto done;
> +	}
> +
> +	if (data_result) {
> +		/*
> +		 * Saturate all channels (in case of overflows). Temperature channel
> +		 * is not affected by overflows.
> +		 */
> +		scan.chan[1] = cpu_to_le16(U16_MAX);
> +		scan.chan[2] = cpu_to_le16(U16_MAX);
> +		scan.chan[3] = cpu_to_le16(U16_MAX);
> +	}
> +
> +	iio_push_to_buffers_with_timestamp(indio_dev, &scan, iio_get_time_ns(indio_dev));
> +
> +done:
> +	mutex_unlock(&data->mutex);
> +	iio_trigger_notify_done(indio_dev->trig);
> +
> +	return IRQ_HANDLED;
> +}
> +
> +static const struct iio_info as73211_info = {
> +	.read_raw = as73211_read_raw,
> +	.read_avail = as73211_read_avail,
> +	.write_raw = as73211_write_raw,
> +};
> +
> +static int as73211_power(struct iio_dev *indio_dev, bool state)
> +{
> +	struct as73211_data *data = iio_priv(indio_dev);
> +	int ret;
> +
> +	mutex_lock(&data->mutex);
> +
> +	if (state)
> +		data->osr &= ~AS73211_OSR_PD;
> +	else
> +		data->osr |= AS73211_OSR_PD;
> +
> +	ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_OSR, data->osr);
> +
> +	mutex_unlock(&data->mutex);
> +
> +	if (ret < 0)
> +		return ret;
> +
> +	return 0;
> +}
> +
> +static void as73211_power_disable(void *data)
> +{
> +	struct iio_dev *indio_dev = data;
> +
> +	as73211_power(indio_dev, false);
> +}
> +
> +static int as73211_probe(struct i2c_client *client)
> +{
> +	struct device *dev = &client->dev;
> +	struct as73211_data *data;
> +	struct iio_dev *indio_dev;
> +	int ret;
> +
> +	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
> +	if (!indio_dev)
> +		return -ENOMEM;
> +
> +	data = iio_priv(indio_dev);
> +	i2c_set_clientdata(client, indio_dev);
> +	data->client = client;
> +
> +	mutex_init(&data->mutex);
> +	init_completion(&data->completion);
> +
> +	indio_dev->info = &as73211_info;
> +	indio_dev->name = AS73211_DRV_NAME;
> +	indio_dev->channels = as73211_channels;
> +	indio_dev->num_channels = ARRAY_SIZE(as73211_channels);
> +	indio_dev->modes = INDIO_DIRECT_MODE;
> +
> +	ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_OSR);
> +	if (ret < 0)
> +		return ret;
> +	data->osr = ret;
> +
> +	/* reset device */
> +	data->osr |= AS73211_OSR_SW_RES;
> +	ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_OSR, data->osr);
> +	if (ret < 0)
> +		return ret;
> +
> +	ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_OSR);
> +	if (ret < 0)
> +		return ret;
> +	data->osr = ret;
> +
> +	/*
> +	 * Reading AGEN is only possible after reset (AGEN is not available if
> +	 * device is in measurement mode).
> +	 */
> +	ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_AGEN);
> +	if (ret < 0)
> +		return ret;
> +
> +	/* At the time of writing this driver, only DEVID 2 and MUT 1 are known. */
> +	if ((ret & AS73211_AGEN_DEVID_MASK) != AS73211_AGEN_DEVID(2) ||
> +	    (ret & AS73211_AGEN_MUT_MASK) != AS73211_AGEN_MUT(1))
> +		return -ENODEV;
> +
> +	ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_CREG1);
> +	if (ret < 0)
> +		return ret;
> +	data->creg1 = ret;
> +
> +	ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_CREG2);
> +	if (ret < 0)
> +		return ret;
> +	data->creg2 = ret;
> +
> +	ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_CREG3);
> +	if (ret < 0)
> +		return ret;
> +	data->creg3 = ret;
> +	as73211_integration_time_calc_avail(data);
> +
> +	ret = as73211_power(indio_dev, true);
> +	if (ret < 0)
> +		return ret;
> +
> +	ret = devm_add_action_or_reset(dev, as73211_power_disable, indio_dev);
> +	if (ret)
> +		return ret;
> +
> +	ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL, as73211_trigger_handler, NULL);
> +	if (ret)
> +		return ret;
> +
> +	if (client->irq) {
> +		ret = devm_request_threaded_irq(&client->dev, client->irq,
> +				NULL,
> +				as73211_ready_handler,
> +				IRQF_ONESHOT,
> +				client->name, indio_dev);
> +		if (ret)
> +			return ret;
> +	}
> +
> +	return devm_iio_device_register(dev, indio_dev);
> +}
> +
> +static int __maybe_unused as73211_suspend(struct device *dev)
> +{
> +	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
> +
> +	return as73211_power(indio_dev, false);
> +}
> +
> +static int __maybe_unused as73211_resume(struct device *dev)
> +{
> +	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
> +
> +	return as73211_power(indio_dev, true);
> +}
> +
> +static SIMPLE_DEV_PM_OPS(as73211_pm_ops, as73211_suspend, as73211_resume);
> +
> +static const struct of_device_id as73211_of_match[] = {
> +	{ .compatible = "ams,as73211" },
> +	{ }
> +};
> +MODULE_DEVICE_TABLE(of, as73211_of_match);
> +
> +static const struct i2c_device_id as73211_id[] = {
> +	{ "as73211", 0 },
> +	{ }
> +};
> +MODULE_DEVICE_TABLE(i2c, as73211_id);
> +
> +static struct i2c_driver as73211_driver = {
> +	.driver = {
> +		.name           = AS73211_DRV_NAME,
> +		.of_match_table = as73211_of_match,
> +		.pm             = &as73211_pm_ops,
> +	},
> +	.probe_new  = as73211_probe,
> +	.id_table   = as73211_id,
> +};
> +module_i2c_driver(as73211_driver);
> +
> +MODULE_AUTHOR("Christian Eggers <ceggers@arri.de>");
> +MODULE_DESCRIPTION("AS73211 XYZ True Color Sensor driver");
> +MODULE_LICENSE("GPL");


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH v6 2/2] iio: light: as73211: New driver
  2020-08-06 17:44   ` Jonathan Cameron
@ 2020-08-07 15:46     ` Christian Eggers
  2020-08-08 16:14       ` Jonathan Cameron
  2020-09-01 18:46     ` [PATCH v6 2/2] iio: light: as73211: New driver Jonathan Cameron
  1 sibling, 1 reply; 20+ messages in thread
From: Christian Eggers @ 2020-08-07 15:46 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: Rob Herring, Andy Shevchenko, Jonathan Cameron, Hartmut Knaack,
	Lars-Peter Clausen, Peter Meerwald-Stadler, linux-iio,
	devicetree, linux-kernel

Hi Jonathan,

On Thursday, 6 August 2020, 19:44:51 CEST, Jonathan Cameron wrote:
> Hi Christian,
> 
> I'll take this, but please send a follow up patch to add documentation
> for in_intensity_x_raw and all the other new ABI this adds in
> Documentation/ABI/testing/sysfs-bus-iio
> I should have mentioned that earlier, but kind of assumed we already
> had these documented for some reason!
where to add these entries in Documentation/ABI/testing/sysfs-bus-iio?
Shall all missing entries be added together at the bottom (as AS73211
is the first device using it) , or inserted in different existing paragraphs
(when the enums where introduced)?

Which kernel version shall I use (5.10)?

regards
Christian





^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH v6 2/2] iio: light: as73211: New driver
  2020-08-07 15:46     ` Christian Eggers
@ 2020-08-08 16:14       ` Jonathan Cameron
  2020-08-09  7:43         ` [PATCH] iio: documentation: light: Add as73211 sysfs documentation Christian Eggers
  0 siblings, 1 reply; 20+ messages in thread
From: Jonathan Cameron @ 2020-08-08 16:14 UTC (permalink / raw)
  To: Christian Eggers
  Cc: Rob Herring, Andy Shevchenko, Jonathan Cameron, Hartmut Knaack,
	Lars-Peter Clausen, Peter Meerwald-Stadler, linux-iio,
	devicetree, linux-kernel

On Fri, 7 Aug 2020 17:46:36 +0200
Christian Eggers <ceggers@arri.de> wrote:

> Hi Jonathan,
> 
> On Thursday, 6 August 2020, 19:44:51 CEST, Jonathan Cameron wrote:
> > Hi Christian,
> > 
> > I'll take this, but please send a follow up patch to add documentation
> > for in_intensity_x_raw and all the other new ABI this adds in
> > Documentation/ABI/testing/sysfs-bus-iio
> > I should have mentioned that earlier, but kind of assumed we already
> > had these documented for some reason!  
> where to add these entries in Documentation/ABI/testing/sysfs-bus-iio?
> Shall all missing entries be added together at the bottom (as AS73211
> is the first device using it) , or inserted in different existing paragraphs
> (when the enums where introduced)?

Insert them into the relevant groups that already exist.  In some cases
it will just be adding an entry with no specific explanation.
For the _raw attribute add a bit more info about what x, y and z are
(basically just say they are from cie1931 (I think?)

> 
> Which kernel version shall I use (5.10)?
This file doesn't change that quickly so that should be fine. I'll deal
with any merge conflicts that do occur.

Thanks,

Jonathan

> 
> regards
> Christian
> 
> 
> 
> 


^ permalink raw reply	[flat|nested] 20+ messages in thread

* [PATCH] iio: documentation: light: Add as73211 sysfs documentation
  2020-08-08 16:14       ` Jonathan Cameron
@ 2020-08-09  7:43         ` Christian Eggers
  2020-08-09 11:30           ` Jonathan Cameron
  2020-08-10  9:00           ` [PATCH] " Pavel Machek
  0 siblings, 2 replies; 20+ messages in thread
From: Christian Eggers @ 2020-08-09  7:43 UTC (permalink / raw)
  To: Jonathan Cameron, Jonathan Cameron
  Cc: Andy Shevchenko, Hartmut Knaack, Lars-Peter Clausen,
	Peter Meerwald-Stadler, linux-iio, linux-kernel,
	Christian Eggers

The driver for the as73211 light sensor provides the following not yet
documented sysfs entries:
- in_intensity_(x|y|z)_raw
- in_intensity_(x|y|z)_scale
- in_intensity_sampling_frequency(_available)
- in_intensity_hardwaregain(_available)
- in_intensity_integration_time

Signed-off-by: Christian Eggers <ceggers@arri.de>
---

On Thursday, 6 August 2020, 19:44:51 CEST, Jonathan Cameron wrote:
Hi Jonathan,

> Hi Christian,
> 
> I'll take this, but please send a follow up patch to add documentation
> for in_intensity_x_raw and all the other new ABI this adds in
> Documentation/ABI/testing/sysfs-bus-iio
> I should have mentioned that earlier, but kind of assumed we already
> had these documented for some reason!  
> [...]
> Insert them into the relevant groups that already exist.  In some cases
> it will just be adding an entry with no specific explanation.
> For the _raw attribute add a bit more info about what x, y and z are
> (basically just say they are from cie1931 (I think?)

I added all sysfs entries which were not present in 5.8-rc6.

Best regards
Christian

 Documentation/ABI/testing/sysfs-bus-iio | 26 ++++++++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)

diff --git a/Documentation/ABI/testing/sysfs-bus-iio b/Documentation/ABI/testing/sysfs-bus-iio
index d3e53a6d8331..14ae4bf053c5 100644
--- a/Documentation/ABI/testing/sysfs-bus-iio
+++ b/Documentation/ABI/testing/sysfs-bus-iio
@@ -40,6 +40,7 @@ Description:
 		buffered samples and events for device X.
 
 What:		/sys/bus/iio/devices/iio:deviceX/sampling_frequency
+What:		/sys/bus/iio/devices/iio:deviceX/in_intensity_sampling_frequency
 What:		/sys/bus/iio/devices/iio:deviceX/buffer/sampling_frequency
 What:		/sys/bus/iio/devices/triggerX/sampling_frequency
 KernelVersion:	2.6.35
@@ -55,6 +56,7 @@ Description:
 		then it is to be found in the base device directory.
 
 What:		/sys/bus/iio/devices/iio:deviceX/sampling_frequency_available
+What:		/sys/bus/iio/devices/iio:deviceX/in_intensity_sampling_frequency_available
 What:		/sys/bus/iio/devices/iio:deviceX/in_proximity_sampling_frequency_available
 What:		/sys/.../iio:deviceX/buffer/sampling_frequency_available
 What:		/sys/bus/iio/devices/triggerX/sampling_frequency_available
@@ -374,6 +376,9 @@ What:		/sys/bus/iio/devices/iio:deviceX/in_velocity_sqrt(x^2+y^2+z^2)_scale
 What:		/sys/bus/iio/devices/iio:deviceX/in_illuminance_scale
 What:		/sys/bus/iio/devices/iio:deviceX/in_countY_scale
 What:		/sys/bus/iio/devices/iio:deviceX/in_angl_scale
+What:		/sys/bus/iio/devices/iio:deviceX/in_intensity_x_scale
+What:		/sys/bus/iio/devices/iio:deviceX/in_intensity_y_scale
+What:		/sys/bus/iio/devices/iio:deviceX/in_intensity_z_scale
 KernelVersion:	2.6.35
 Contact:	linux-iio@vger.kernel.org
 Description:
@@ -484,6 +489,7 @@ Description:
 		are listed in this attribute.
 
 What		/sys/bus/iio/devices/iio:deviceX/out_voltageY_hardwaregain
+What:		/sys/bus/iio/devices/iio:deviceX/in_intensity_hardwaregain
 What:		/sys/bus/iio/devices/iio:deviceX/in_intensity_red_hardwaregain
 What:		/sys/bus/iio/devices/iio:deviceX/in_intensity_green_hardwaregain
 What:		/sys/bus/iio/devices/iio:deviceX/in_intensity_blue_hardwaregain
@@ -494,6 +500,13 @@ Description:
 		Hardware applied gain factor. If shared across all channels,
 		<type>_hardwaregain is used.
 
+What:		/sys/bus/iio/devices/iio:deviceX/in_intensity_hardwaregain_available
+KernelVersion:	5.10
+Contact:	linux-iio@vger.kernel.org
+Description:
+		Lists all available hardware applied gain factors. Shared across all
+		channels.
+
 What:		/sys/.../in_accel_filter_low_pass_3db_frequency
 What:		/sys/.../in_magn_filter_low_pass_3db_frequency
 What:		/sys/.../in_anglvel_filter_low_pass_3db_frequency
@@ -1333,6 +1346,7 @@ Description:
 		standardised CIE Erythemal Action Spectrum. UV index values range
 		from 0 (low) to >=11 (extreme).
 
+What:		/sys/.../iio:deviceX/in_intensity_integration_time
 What:		/sys/.../iio:deviceX/in_intensity_red_integration_time
 What:		/sys/.../iio:deviceX/in_intensity_green_integration_time
 What:		/sys/.../iio:deviceX/in_intensity_blue_integration_time
@@ -1342,7 +1356,8 @@ KernelVersion:	3.12
 Contact:	linux-iio@vger.kernel.org
 Description:
 		This attribute is used to get/set the integration time in
-		seconds.
+		seconds. If shared across all channels, <type>_integration_time
+		is used.
 
 What:		/sys/.../iio:deviceX/in_velocity_sqrt(x^2+y^2+z^2)_integration_time
 KernelVersion:	4.0
@@ -1739,3 +1754,12 @@ KernelVersion:	5.5
 Contact:	linux-iio@vger.kernel.org
 Description:
 		One of the following thermocouple types: B, E, J, K, N, R, S, T.
+
+What:		/sys/bus/iio/devices/iio:deviceX/in_intensity_x_raw
+What:		/sys/bus/iio/devices/iio:deviceX/in_intensity_y_raw
+What:		/sys/bus/iio/devices/iio:deviceX/in_intensity_z_raw
+KernelVersion:	5.10
+Contact:	linux-iio@vger.kernel.org
+Description:
+		Unscaled light intensity according to CIE 1931/DIN 5033 color space.
+		Units after application of scale are nano nanowatts per square meter.
-- 
Christian Eggers
Embedded software developer

Arnold & Richter Cine Technik GmbH & Co. Betriebs KG
Sitz: Muenchen - Registergericht: Amtsgericht Muenchen - Handelsregisternummer: HRA 57918
Persoenlich haftender Gesellschafter: Arnold & Richter Cine Technik GmbH
Sitz: Muenchen - Registergericht: Amtsgericht Muenchen - Handelsregisternummer: HRB 54477
Geschaeftsfuehrer: Dr. Michael Neuhaeuser; Stephan Schenk; Walter Trauninger; Markus Zeiler


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH] iio: documentation: light: Add as73211 sysfs documentation
  2020-08-09  7:43         ` [PATCH] iio: documentation: light: Add as73211 sysfs documentation Christian Eggers
@ 2020-08-09 11:30           ` Jonathan Cameron
  2020-08-10  5:24             ` [PATCH v2] " Christian Eggers
  2020-08-10  9:00           ` [PATCH] " Pavel Machek
  1 sibling, 1 reply; 20+ messages in thread
From: Jonathan Cameron @ 2020-08-09 11:30 UTC (permalink / raw)
  To: Christian Eggers
  Cc: Jonathan Cameron, Andy Shevchenko, Hartmut Knaack,
	Lars-Peter Clausen, Peter Meerwald-Stadler, linux-iio,
	linux-kernel

On Sun, 9 Aug 2020 09:43:35 +0200
Christian Eggers <ceggers@arri.de> wrote:

> The driver for the as73211 light sensor provides the following not yet
> documented sysfs entries:
> - in_intensity_(x|y|z)_raw
> - in_intensity_(x|y|z)_scale
> - in_intensity_sampling_frequency(_available)
> - in_intensity_hardwaregain(_available)
> - in_intensity_integration_time
> 
> Signed-off-by: Christian Eggers <ceggers@arri.de>

Hi Christian,

Just one tiny nitpick.  Otherwise looks good to me!

Thanks for doing this,

Jonathan

> ---
> 
> On Thursday, 6 August 2020, 19:44:51 CEST, Jonathan Cameron wrote:
> Hi Jonathan,
> 
> > Hi Christian,
> > 
> > I'll take this, but please send a follow up patch to add documentation
> > for in_intensity_x_raw and all the other new ABI this adds in
> > Documentation/ABI/testing/sysfs-bus-iio
> > I should have mentioned that earlier, but kind of assumed we already
> > had these documented for some reason!  
> > [...]
> > Insert them into the relevant groups that already exist.  In some cases
> > it will just be adding an entry with no specific explanation.
> > For the _raw attribute add a bit more info about what x, y and z are
> > (basically just say they are from cie1931 (I think?)  
> 
> I added all sysfs entries which were not present in 5.8-rc6.
> 
> Best regards
> Christian
> 
>  Documentation/ABI/testing/sysfs-bus-iio | 26 ++++++++++++++++++++++++-
>  1 file changed, 25 insertions(+), 1 deletion(-)
> 
> diff --git a/Documentation/ABI/testing/sysfs-bus-iio b/Documentation/ABI/testing/sysfs-bus-iio
> index d3e53a6d8331..14ae4bf053c5 100644
> --- a/Documentation/ABI/testing/sysfs-bus-iio
> +++ b/Documentation/ABI/testing/sysfs-bus-iio
> @@ -40,6 +40,7 @@ Description:
>  		buffered samples and events for device X.
>  
>  What:		/sys/bus/iio/devices/iio:deviceX/sampling_frequency
> +What:		/sys/bus/iio/devices/iio:deviceX/in_intensity_sampling_frequency
>  What:		/sys/bus/iio/devices/iio:deviceX/buffer/sampling_frequency
>  What:		/sys/bus/iio/devices/triggerX/sampling_frequency
>  KernelVersion:	2.6.35
> @@ -55,6 +56,7 @@ Description:
>  		then it is to be found in the base device directory.
>  
>  What:		/sys/bus/iio/devices/iio:deviceX/sampling_frequency_available
> +What:		/sys/bus/iio/devices/iio:deviceX/in_intensity_sampling_frequency_available
>  What:		/sys/bus/iio/devices/iio:deviceX/in_proximity_sampling_frequency_available
>  What:		/sys/.../iio:deviceX/buffer/sampling_frequency_available
>  What:		/sys/bus/iio/devices/triggerX/sampling_frequency_available
> @@ -374,6 +376,9 @@ What:		/sys/bus/iio/devices/iio:deviceX/in_velocity_sqrt(x^2+y^2+z^2)_scale
>  What:		/sys/bus/iio/devices/iio:deviceX/in_illuminance_scale
>  What:		/sys/bus/iio/devices/iio:deviceX/in_countY_scale
>  What:		/sys/bus/iio/devices/iio:deviceX/in_angl_scale
> +What:		/sys/bus/iio/devices/iio:deviceX/in_intensity_x_scale
> +What:		/sys/bus/iio/devices/iio:deviceX/in_intensity_y_scale
> +What:		/sys/bus/iio/devices/iio:deviceX/in_intensity_z_scale
>  KernelVersion:	2.6.35
>  Contact:	linux-iio@vger.kernel.org
>  Description:
> @@ -484,6 +489,7 @@ Description:
>  		are listed in this attribute.
>  
>  What		/sys/bus/iio/devices/iio:deviceX/out_voltageY_hardwaregain
> +What:		/sys/bus/iio/devices/iio:deviceX/in_intensity_hardwaregain
>  What:		/sys/bus/iio/devices/iio:deviceX/in_intensity_red_hardwaregain
>  What:		/sys/bus/iio/devices/iio:deviceX/in_intensity_green_hardwaregain
>  What:		/sys/bus/iio/devices/iio:deviceX/in_intensity_blue_hardwaregain
> @@ -494,6 +500,13 @@ Description:
>  		Hardware applied gain factor. If shared across all channels,
>  		<type>_hardwaregain is used.
>  
> +What:		/sys/bus/iio/devices/iio:deviceX/in_intensity_hardwaregain_available
> +KernelVersion:	5.10
> +Contact:	linux-iio@vger.kernel.org
> +Description:
> +		Lists all available hardware applied gain factors. Shared across all
> +		channels.
> +
>  What:		/sys/.../in_accel_filter_low_pass_3db_frequency
>  What:		/sys/.../in_magn_filter_low_pass_3db_frequency
>  What:		/sys/.../in_anglvel_filter_low_pass_3db_frequency
> @@ -1333,6 +1346,7 @@ Description:
>  		standardised CIE Erythemal Action Spectrum. UV index values range
>  		from 0 (low) to >=11 (extreme).
>  
> +What:		/sys/.../iio:deviceX/in_intensity_integration_time
>  What:		/sys/.../iio:deviceX/in_intensity_red_integration_time
>  What:		/sys/.../iio:deviceX/in_intensity_green_integration_time
>  What:		/sys/.../iio:deviceX/in_intensity_blue_integration_time
> @@ -1342,7 +1356,8 @@ KernelVersion:	3.12
>  Contact:	linux-iio@vger.kernel.org
>  Description:
>  		This attribute is used to get/set the integration time in
> -		seconds.
> +		seconds. If shared across all channels, <type>_integration_time
> +		is used.

If it is shared across 'ALL' channels then it will be just integration_time
If it is shared across all channels of a given type, it will be <type>_integration_time.

>  
>  What:		/sys/.../iio:deviceX/in_velocity_sqrt(x^2+y^2+z^2)_integration_time
>  KernelVersion:	4.0
> @@ -1739,3 +1754,12 @@ KernelVersion:	5.5
>  Contact:	linux-iio@vger.kernel.org
>  Description:
>  		One of the following thermocouple types: B, E, J, K, N, R, S, T.
> +
> +What:		/sys/bus/iio/devices/iio:deviceX/in_intensity_x_raw
> +What:		/sys/bus/iio/devices/iio:deviceX/in_intensity_y_raw
> +What:		/sys/bus/iio/devices/iio:deviceX/in_intensity_z_raw
> +KernelVersion:	5.10
> +Contact:	linux-iio@vger.kernel.org
> +Description:
> +		Unscaled light intensity according to CIE 1931/DIN 5033 color space.
> +		Units after application of scale are nano nanowatts per square meter.


^ permalink raw reply	[flat|nested] 20+ messages in thread

* [PATCH v2] iio: documentation: light: Add as73211 sysfs documentation
  2020-08-09 11:30           ` Jonathan Cameron
@ 2020-08-10  5:24             ` Christian Eggers
  2020-08-16  9:19               ` Jonathan Cameron
  0 siblings, 1 reply; 20+ messages in thread
From: Christian Eggers @ 2020-08-10  5:24 UTC (permalink / raw)
  To: Jonathan Cameron, Jonathan Cameron
  Cc: Andy Shevchenko, Hartmut Knaack, Lars-Peter Clausen,
	Peter Meerwald-Stadler, linux-iio, linux-kernel,
	Christian Eggers

The driver for the as73211 light sensor provides the following not yet
documented sysfs entries:
- in_intensity_(x|y|z)_raw
- in_intensity_(x|y|z)_scale
- in_intensity_sampling_frequency(_available)
- in_intensity_hardwaregain(_available)
- in_intensity_integration_time

Signed-off-by: Christian Eggers <ceggers@arri.de>
---
Jonathan Cameron:
> If it is shared across 'ALL' channels then it will be just integration_time
> If it is shared across all channels of a given type, it will be <type>_integration_time.
changed.

Best regards
Christian Eggers

 Documentation/ABI/testing/sysfs-bus-iio | 26 ++++++++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)

diff --git a/Documentation/ABI/testing/sysfs-bus-iio b/Documentation/ABI/testing/sysfs-bus-iio
index d3e53a6d8331..388560149875 100644
--- a/Documentation/ABI/testing/sysfs-bus-iio
+++ b/Documentation/ABI/testing/sysfs-bus-iio
@@ -40,6 +40,7 @@ Description:
 		buffered samples and events for device X.
 
 What:		/sys/bus/iio/devices/iio:deviceX/sampling_frequency
+What:		/sys/bus/iio/devices/iio:deviceX/in_intensity_sampling_frequency
 What:		/sys/bus/iio/devices/iio:deviceX/buffer/sampling_frequency
 What:		/sys/bus/iio/devices/triggerX/sampling_frequency
 KernelVersion:	2.6.35
@@ -55,6 +56,7 @@ Description:
 		then it is to be found in the base device directory.
 
 What:		/sys/bus/iio/devices/iio:deviceX/sampling_frequency_available
+What:		/sys/bus/iio/devices/iio:deviceX/in_intensity_sampling_frequency_available
 What:		/sys/bus/iio/devices/iio:deviceX/in_proximity_sampling_frequency_available
 What:		/sys/.../iio:deviceX/buffer/sampling_frequency_available
 What:		/sys/bus/iio/devices/triggerX/sampling_frequency_available
@@ -374,6 +376,9 @@ What:		/sys/bus/iio/devices/iio:deviceX/in_velocity_sqrt(x^2+y^2+z^2)_scale
 What:		/sys/bus/iio/devices/iio:deviceX/in_illuminance_scale
 What:		/sys/bus/iio/devices/iio:deviceX/in_countY_scale
 What:		/sys/bus/iio/devices/iio:deviceX/in_angl_scale
+What:		/sys/bus/iio/devices/iio:deviceX/in_intensity_x_scale
+What:		/sys/bus/iio/devices/iio:deviceX/in_intensity_y_scale
+What:		/sys/bus/iio/devices/iio:deviceX/in_intensity_z_scale
 KernelVersion:	2.6.35
 Contact:	linux-iio@vger.kernel.org
 Description:
@@ -484,6 +489,7 @@ Description:
 		are listed in this attribute.
 
 What		/sys/bus/iio/devices/iio:deviceX/out_voltageY_hardwaregain
+What:		/sys/bus/iio/devices/iio:deviceX/in_intensity_hardwaregain
 What:		/sys/bus/iio/devices/iio:deviceX/in_intensity_red_hardwaregain
 What:		/sys/bus/iio/devices/iio:deviceX/in_intensity_green_hardwaregain
 What:		/sys/bus/iio/devices/iio:deviceX/in_intensity_blue_hardwaregain
@@ -494,6 +500,13 @@ Description:
 		Hardware applied gain factor. If shared across all channels,
 		<type>_hardwaregain is used.
 
+What:		/sys/bus/iio/devices/iio:deviceX/in_intensity_hardwaregain_available
+KernelVersion:	5.10
+Contact:	linux-iio@vger.kernel.org
+Description:
+		Lists all available hardware applied gain factors. Shared across all
+		channels.
+
 What:		/sys/.../in_accel_filter_low_pass_3db_frequency
 What:		/sys/.../in_magn_filter_low_pass_3db_frequency
 What:		/sys/.../in_anglvel_filter_low_pass_3db_frequency
@@ -1333,6 +1346,7 @@ Description:
 		standardised CIE Erythemal Action Spectrum. UV index values range
 		from 0 (low) to >=11 (extreme).
 
+What:		/sys/.../iio:deviceX/in_intensity_integration_time
 What:		/sys/.../iio:deviceX/in_intensity_red_integration_time
 What:		/sys/.../iio:deviceX/in_intensity_green_integration_time
 What:		/sys/.../iio:deviceX/in_intensity_blue_integration_time
@@ -1342,7 +1356,8 @@ KernelVersion:	3.12
 Contact:	linux-iio@vger.kernel.org
 Description:
 		This attribute is used to get/set the integration time in
-		seconds.
+		seconds. If shared across all channels of a given type,
+		<type>_integration_time is used.
 
 What:		/sys/.../iio:deviceX/in_velocity_sqrt(x^2+y^2+z^2)_integration_time
 KernelVersion:	4.0
@@ -1739,3 +1754,12 @@ KernelVersion:	5.5
 Contact:	linux-iio@vger.kernel.org
 Description:
 		One of the following thermocouple types: B, E, J, K, N, R, S, T.
+
+What:		/sys/bus/iio/devices/iio:deviceX/in_intensity_x_raw
+What:		/sys/bus/iio/devices/iio:deviceX/in_intensity_y_raw
+What:		/sys/bus/iio/devices/iio:deviceX/in_intensity_z_raw
+KernelVersion:	5.10
+Contact:	linux-iio@vger.kernel.org
+Description:
+		Unscaled light intensity according to CIE 1931/DIN 5033 color space.
+		Units after application of scale are nano nanowatts per square meter.
-- 
Christian Eggers
Embedded software developer

Arnold & Richter Cine Technik GmbH & Co. Betriebs KG
Sitz: Muenchen - Registergericht: Amtsgericht Muenchen - Handelsregisternummer: HRA 57918
Persoenlich haftender Gesellschafter: Arnold & Richter Cine Technik GmbH
Sitz: Muenchen - Registergericht: Amtsgericht Muenchen - Handelsregisternummer: HRB 54477
Geschaeftsfuehrer: Dr. Michael Neuhaeuser; Stephan Schenk; Walter Trauninger; Markus Zeiler


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH] iio: documentation: light: Add as73211 sysfs documentation
  2020-08-09  7:43         ` [PATCH] iio: documentation: light: Add as73211 sysfs documentation Christian Eggers
  2020-08-09 11:30           ` Jonathan Cameron
@ 2020-08-10  9:00           ` Pavel Machek
  2020-08-10  9:57             ` Christian Eggers
  1 sibling, 1 reply; 20+ messages in thread
From: Pavel Machek @ 2020-08-10  9:00 UTC (permalink / raw)
  To: Christian Eggers
  Cc: Jonathan Cameron, Jonathan Cameron, Andy Shevchenko,
	Hartmut Knaack, Lars-Peter Clausen, Peter Meerwald-Stadler,
	linux-iio, linux-kernel

Hi!

> The driver for the as73211 light sensor provides the following not yet
> documented sysfs entries:
> - in_intensity_(x|y|z)_raw
> - in_intensity_(x|y|z)_scale
> - in_intensity_sampling_frequency(_available)
> - in_intensity_hardwaregain(_available)

Should that be hardware_gain ?
							Pavel



-- 

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH] iio: documentation: light: Add as73211 sysfs documentation
  2020-08-10  9:00           ` [PATCH] " Pavel Machek
@ 2020-08-10  9:57             ` Christian Eggers
  2020-08-10 12:35               ` Pavel Machek
  0 siblings, 1 reply; 20+ messages in thread
From: Christian Eggers @ 2020-08-10  9:57 UTC (permalink / raw)
  To: Pavel Machek
  Cc: Jonathan Cameron, Jonathan Cameron, Andy Shevchenko,
	Hartmut Knaack, Lars-Peter Clausen, Peter Meerwald-Stadler,
	linux-iio, linux-kernel

On Monday, 10 August 2020, 11:00:54 CEST, Pavel Machek wrote:
> Hi!
> 
> > The driver for the as73211 light sensor provides the following not yet
> > documented sysfs entries:
> > - in_intensity_(x|y|z)_raw
> > - in_intensity_(x|y|z)_scale
> > - in_intensity_sampling_frequency(_available)
> > - in_intensity_hardwaregain(_available)
> 
> Should that be hardware_gain ?
drivers/iio/industrialio-core.c:
[IIO_CHAN_INFO_HARDWAREGAIN] = "hardwaregain",

> 							
Pavel
regards
Christian






^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH] iio: documentation: light: Add as73211 sysfs documentation
  2020-08-10  9:57             ` Christian Eggers
@ 2020-08-10 12:35               ` Pavel Machek
  2020-08-10 13:24                 ` Christian Eggers
  0 siblings, 1 reply; 20+ messages in thread
From: Pavel Machek @ 2020-08-10 12:35 UTC (permalink / raw)
  To: Christian Eggers
  Cc: Jonathan Cameron, Jonathan Cameron, Andy Shevchenko,
	Hartmut Knaack, Lars-Peter Clausen, Peter Meerwald-Stadler,
	linux-iio, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 750 bytes --]

On Mon 2020-08-10 11:57:46, Christian Eggers wrote:
> On Monday, 10 August 2020, 11:00:54 CEST, Pavel Machek wrote:
> > Hi!
> >
> > > The driver for the as73211 light sensor provides the following not yet
> > > documented sysfs entries:
> > > - in_intensity_(x|y|z)_raw
> > > - in_intensity_(x|y|z)_scale
> > > - in_intensity_sampling_frequency(_available)
> > > - in_intensity_hardwaregain(_available)
> >
> > Should that be hardware_gain ?
> drivers/iio/industrialio-core.c:
> [IIO_CHAN_INFO_HARDWAREGAIN] = "hardwaregain",

Yep. Fix that before people start to really use it.
									Pavel
-- 
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 195 bytes --]

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH] iio: documentation: light: Add as73211 sysfs documentation
  2020-08-10 12:35               ` Pavel Machek
@ 2020-08-10 13:24                 ` Christian Eggers
  2020-08-16  9:18                   ` Jonathan Cameron
  0 siblings, 1 reply; 20+ messages in thread
From: Christian Eggers @ 2020-08-10 13:24 UTC (permalink / raw)
  To: Pavel Machek
  Cc: Jonathan Cameron, Jonathan Cameron, Andy Shevchenko,
	Hartmut Knaack, Lars-Peter Clausen, Peter Meerwald-Stadler,
	linux-iio, linux-kernel

Hi Pavel,

On Monday, 10 August 2020, 14:35:40 CEST, Pavel Machek wrote:
> On Mon 2020-08-10 11:57:46, Christian Eggers wrote:
> > On Monday, 10 August 2020, 11:00:54 CEST, Pavel Machek wrote:
> > > Hi!
> > > 
> > > > The driver for the as73211 light sensor provides the following not yet
> > > > documented sysfs entries:
> > > > - in_intensity_(x|y|z)_raw
> > > > - in_intensity_(x|y|z)_scale
> > > > - in_intensity_sampling_frequency(_available)
> > > > - in_intensity_hardwaregain(_available)
> > > 
> > > Should that be hardware_gain ?
> > 
> > drivers/iio/industrialio-core.c:
> > [IIO_CHAN_INFO_HARDWAREGAIN] = "hardwaregain",
> 
> Yep. Fix that before people start to really use it.
it's too late! IIO_CHAN_INFO_HARDWAREGAIN was introduced back in 2012. My 
patch only adds "hardwaregain" in a new context (of "intensity").

> 								
	Pavel

regards
Christian





^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH] iio: documentation: light: Add as73211 sysfs documentation
  2020-08-10 13:24                 ` Christian Eggers
@ 2020-08-16  9:18                   ` Jonathan Cameron
  0 siblings, 0 replies; 20+ messages in thread
From: Jonathan Cameron @ 2020-08-16  9:18 UTC (permalink / raw)
  To: Christian Eggers
  Cc: Pavel Machek, Jonathan Cameron, Andy Shevchenko, Hartmut Knaack,
	Lars-Peter Clausen, Peter Meerwald-Stadler, linux-iio,
	linux-kernel

On Mon, 10 Aug 2020 15:24:39 +0200
Christian Eggers <ceggers@arri.de> wrote:

> Hi Pavel,
> 
> On Monday, 10 August 2020, 14:35:40 CEST, Pavel Machek wrote:
> > On Mon 2020-08-10 11:57:46, Christian Eggers wrote:  
> > > On Monday, 10 August 2020, 11:00:54 CEST, Pavel Machek wrote:  
> > > > Hi!
> > > >  
> > > > > The driver for the as73211 light sensor provides the following not yet
> > > > > documented sysfs entries:
> > > > > - in_intensity_(x|y|z)_raw
> > > > > - in_intensity_(x|y|z)_scale
> > > > > - in_intensity_sampling_frequency(_available)
> > > > > - in_intensity_hardwaregain(_available)  
> > > >
> > > > Should that be hardware_gain ?  
> > >
> > > drivers/iio/industrialio-core.c:
> > > [IIO_CHAN_INFO_HARDWAREGAIN] = "hardwaregain",  
> >
> > Yep. Fix that before people start to really use it.  
> it's too late! IIO_CHAN_INFO_HARDWAREGAIN was introduced back in 2012. My
> patch only adds "hardwaregain" in a new context (of "intensity").

Why do you think that is broken?

It was a deliberate decision to try and stop putting _'s into new
elements because it was making userspace parsing harder.  hardwaregain
is a single 'token' in that file name.

We are stuck with some older forms that predate that decision
and a few that are closely related to existing elements and hence
had to maintain the 'spacing'.  Those have to be special cased
in tokenizing code. We don't want to introduced any more though.

One or two may have snuck through when we weren't awake of course.

Jonathan

> 
> >  
>         Pavel
> 
> regards
> Christian
> 
> 
> 
> 
> ________________________________
>  [http://assets.arri.com/media/sign/2020-04-03-E-mail-signature-Stellar2_V1.jpg] <https://microsites.arri.com/stellar/>
> 
> Get all the latest information from www.arri.com<https://www.arri.com/>, Facebook<https://www.facebook.com/TeamARRI>, Twitter<https://twitter.com/ARRIChannel>, Instagram<https://instagram.com/arri> and YouTube<https://www.youtube.com/user/ARRIChannel>.
> 
> Arnold & Richter Cine Technik GmbH & Co. Betriebs KG
> Sitz: München - Registergericht: Amtsgericht München - Handelsregisternummer: HRA 57918
> Persönlich haftender Gesellschafter: Arnold & Richter Cine Technik GmbH
> Sitz: München - Registergericht: Amtsgericht München - Handelsregisternummer: HRB 54477
> Geschäftsführer: Dr. Michael Neuhäuser; Stephan Schenk; Walter Trauninger; Markus Zeiler


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH v2] iio: documentation: light: Add as73211 sysfs documentation
  2020-08-10  5:24             ` [PATCH v2] " Christian Eggers
@ 2020-08-16  9:19               ` Jonathan Cameron
  0 siblings, 0 replies; 20+ messages in thread
From: Jonathan Cameron @ 2020-08-16  9:19 UTC (permalink / raw)
  To: Christian Eggers
  Cc: Jonathan Cameron, Andy Shevchenko, Hartmut Knaack,
	Lars-Peter Clausen, Peter Meerwald-Stadler, linux-iio,
	linux-kernel

On Mon, 10 Aug 2020 07:24:47 +0200
Christian Eggers <ceggers@arri.de> wrote:

> The driver for the as73211 light sensor provides the following not yet
> documented sysfs entries:
> - in_intensity_(x|y|z)_raw
> - in_intensity_(x|y|z)_scale
> - in_intensity_sampling_frequency(_available)
> - in_intensity_hardwaregain(_available)
> - in_intensity_integration_time
> 
> Signed-off-by: Christian Eggers <ceggers@arri.de>
Applied.

Thanks,

Jonathan

> ---
> Jonathan Cameron:
> > If it is shared across 'ALL' channels then it will be just integration_time
> > If it is shared across all channels of a given type, it will be <type>_integration_time.  
> changed.
> 
> Best regards
> Christian Eggers
> 
>  Documentation/ABI/testing/sysfs-bus-iio | 26 ++++++++++++++++++++++++-
>  1 file changed, 25 insertions(+), 1 deletion(-)
> 
> diff --git a/Documentation/ABI/testing/sysfs-bus-iio b/Documentation/ABI/testing/sysfs-bus-iio
> index d3e53a6d8331..388560149875 100644
> --- a/Documentation/ABI/testing/sysfs-bus-iio
> +++ b/Documentation/ABI/testing/sysfs-bus-iio
> @@ -40,6 +40,7 @@ Description:
>  		buffered samples and events for device X.
>  
>  What:		/sys/bus/iio/devices/iio:deviceX/sampling_frequency
> +What:		/sys/bus/iio/devices/iio:deviceX/in_intensity_sampling_frequency
>  What:		/sys/bus/iio/devices/iio:deviceX/buffer/sampling_frequency
>  What:		/sys/bus/iio/devices/triggerX/sampling_frequency
>  KernelVersion:	2.6.35
> @@ -55,6 +56,7 @@ Description:
>  		then it is to be found in the base device directory.
>  
>  What:		/sys/bus/iio/devices/iio:deviceX/sampling_frequency_available
> +What:		/sys/bus/iio/devices/iio:deviceX/in_intensity_sampling_frequency_available
>  What:		/sys/bus/iio/devices/iio:deviceX/in_proximity_sampling_frequency_available
>  What:		/sys/.../iio:deviceX/buffer/sampling_frequency_available
>  What:		/sys/bus/iio/devices/triggerX/sampling_frequency_available
> @@ -374,6 +376,9 @@ What:		/sys/bus/iio/devices/iio:deviceX/in_velocity_sqrt(x^2+y^2+z^2)_scale
>  What:		/sys/bus/iio/devices/iio:deviceX/in_illuminance_scale
>  What:		/sys/bus/iio/devices/iio:deviceX/in_countY_scale
>  What:		/sys/bus/iio/devices/iio:deviceX/in_angl_scale
> +What:		/sys/bus/iio/devices/iio:deviceX/in_intensity_x_scale
> +What:		/sys/bus/iio/devices/iio:deviceX/in_intensity_y_scale
> +What:		/sys/bus/iio/devices/iio:deviceX/in_intensity_z_scale
>  KernelVersion:	2.6.35
>  Contact:	linux-iio@vger.kernel.org
>  Description:
> @@ -484,6 +489,7 @@ Description:
>  		are listed in this attribute.
>  
>  What		/sys/bus/iio/devices/iio:deviceX/out_voltageY_hardwaregain
> +What:		/sys/bus/iio/devices/iio:deviceX/in_intensity_hardwaregain
>  What:		/sys/bus/iio/devices/iio:deviceX/in_intensity_red_hardwaregain
>  What:		/sys/bus/iio/devices/iio:deviceX/in_intensity_green_hardwaregain
>  What:		/sys/bus/iio/devices/iio:deviceX/in_intensity_blue_hardwaregain
> @@ -494,6 +500,13 @@ Description:
>  		Hardware applied gain factor. If shared across all channels,
>  		<type>_hardwaregain is used.
>  
> +What:		/sys/bus/iio/devices/iio:deviceX/in_intensity_hardwaregain_available
> +KernelVersion:	5.10
> +Contact:	linux-iio@vger.kernel.org
> +Description:
> +		Lists all available hardware applied gain factors. Shared across all
> +		channels.
> +
>  What:		/sys/.../in_accel_filter_low_pass_3db_frequency
>  What:		/sys/.../in_magn_filter_low_pass_3db_frequency
>  What:		/sys/.../in_anglvel_filter_low_pass_3db_frequency
> @@ -1333,6 +1346,7 @@ Description:
>  		standardised CIE Erythemal Action Spectrum. UV index values range
>  		from 0 (low) to >=11 (extreme).
>  
> +What:		/sys/.../iio:deviceX/in_intensity_integration_time
>  What:		/sys/.../iio:deviceX/in_intensity_red_integration_time
>  What:		/sys/.../iio:deviceX/in_intensity_green_integration_time
>  What:		/sys/.../iio:deviceX/in_intensity_blue_integration_time
> @@ -1342,7 +1356,8 @@ KernelVersion:	3.12
>  Contact:	linux-iio@vger.kernel.org
>  Description:
>  		This attribute is used to get/set the integration time in
> -		seconds.
> +		seconds. If shared across all channels of a given type,
> +		<type>_integration_time is used.
>  
>  What:		/sys/.../iio:deviceX/in_velocity_sqrt(x^2+y^2+z^2)_integration_time
>  KernelVersion:	4.0
> @@ -1739,3 +1754,12 @@ KernelVersion:	5.5
>  Contact:	linux-iio@vger.kernel.org
>  Description:
>  		One of the following thermocouple types: B, E, J, K, N, R, S, T.
> +
> +What:		/sys/bus/iio/devices/iio:deviceX/in_intensity_x_raw
> +What:		/sys/bus/iio/devices/iio:deviceX/in_intensity_y_raw
> +What:		/sys/bus/iio/devices/iio:deviceX/in_intensity_z_raw
> +KernelVersion:	5.10
> +Contact:	linux-iio@vger.kernel.org
> +Description:
> +		Unscaled light intensity according to CIE 1931/DIN 5033 color space.
> +		Units after application of scale are nano nanowatts per square meter.


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH v6 2/2] iio: light: as73211: New driver
  2020-08-06 17:44   ` Jonathan Cameron
  2020-08-07 15:46     ` Christian Eggers
@ 2020-09-01 18:46     ` Jonathan Cameron
  2020-09-03 18:43       ` Jonathan Cameron
  1 sibling, 1 reply; 20+ messages in thread
From: Jonathan Cameron @ 2020-09-01 18:46 UTC (permalink / raw)
  To: Christian Eggers
  Cc: Rob Herring, Andy Shevchenko, Jonathan Cameron, Hartmut Knaack,
	Lars-Peter Clausen, Peter Meerwald-Stadler, linux-iio,
	devicetree, linux-kernel

On Thu, 6 Aug 2020 18:44:51 +0100
Jonathan Cameron <jic23@kernel.org> wrote:

> On Wed, 5 Aug 2020 07:57:44 +0200
> Christian Eggers <ceggers@arri.de> wrote:
> 
> > Support for AMS AS73211 JENCOLOR(R) Digital XYZ Sensor.
> > 
> > This driver has no built-in trigger. In order for making triggered
> > measurements, an external (software) trigger driver like
> > iio-trig-hrtimer or iio-trig-sysfs is required.
> > 
> > The sensor supports single and continuous measurement modes. The latter
> > is not used by design as this would require tight timing synchronization
> > between hardware and driver without much benefit.
> > 
> > Datasheet: https://ams.com/documents/20143/36005/AS73211_DS000556_3-01.pdf
> > Signed-off-by: Christian Eggers <ceggers@arri.de>  
> 
> Hi Christian,
> 
> I'll take this, but please send a follow up patch to add documentation
> for in_intensity_x_raw and all the other new ABI this adds in
> Documentation/ABI/testing/sysfs-bus-iio
> I should have mentioned that earlier, but kind of assumed we already
> had these documented for some reason!
> 
> Applied to the togreg branch of iio.git and pushed out as testing for
> the autobuilders to play with it.
> 
> Thanks,
> 
> Jonathan
Unfortunatey I don't have the thread on this account / machine so I'll
reply here.

0-day highlighted an issue in here and Andy sent a suggested fix.

What Andy suggested almost works but there are some subtleties. So before
I make the change directly on the patch I wanted to confirm I haven't
gotten it horribly wrong!

I'm proposing just halving the range of the loop. See below.

Thanks,

Jonathan


> 
> 
> > ---
> >  MAINTAINERS                 |   7 +
> >  drivers/iio/light/Kconfig   |  15 +
> >  drivers/iio/light/Makefile  |   1 +
> >  drivers/iio/light/as73211.c | 801 ++++++++++++++++++++++++++++++++++++
> >  4 files changed, 824 insertions(+)
> >  create mode 100644 drivers/iio/light/as73211.c
> > 
> > diff --git a/MAINTAINERS b/MAINTAINERS
> > index 068d6e94122b..673570414147 100644
> > --- a/MAINTAINERS
> > +++ b/MAINTAINERS
> > @@ -934,6 +934,13 @@ S:	Supported
> >  F:	arch/arm64/boot/dts/amd/amd-seattle-xgbe*.dtsi
> >  F:	drivers/net/ethernet/amd/xgbe/
> >  
> > +AMS AS73211 DRIVER
> > +M:	Christian Eggers <ceggers@arri.de>
> > +L:	linux-iio@vger.kernel.org
> > +S:	Maintained
> > +F:	Documentation/devicetree/bindings/iio/light/ams,as73211.yaml
> > +F:	drivers/iio/light/as73211.c
> > +
> >  ANALOG DEVICES INC AD5686 DRIVER
> >  M:	Michael Hennerich <Michael.Hennerich@analog.com>
> >  L:	linux-pm@vger.kernel.org
> > diff --git a/drivers/iio/light/Kconfig b/drivers/iio/light/Kconfig
> > index 182bd18c4bb2..cade6dc0305b 100644
> > --- a/drivers/iio/light/Kconfig
> > +++ b/drivers/iio/light/Kconfig
> > @@ -86,6 +86,21 @@ config APDS9960
> >  	  To compile this driver as a module, choose M here: the
> >  	  module will be called apds9960
> >  
> > +config AS73211
> > +	tristate "AMS AS73211 XYZ color sensor"
> > +	depends on I2C
> > +	select IIO_BUFFER
> > +	select IIO_TRIGGERED_BUFFER
> > +	help
> > +	 If you say yes here you get support for the AMS AS73211
> > +	 JENCOLOR(R) Digital XYZ Sensor.
> > +
> > +	 For triggered measurements, you will need an additional trigger driver
> > +	 like IIO_HRTIMER_TRIGGER or IIO_SYSFS_TRIGGER.
> > +
> > +	 This driver can also be built as a module.  If so, the module
> > +	 will be called as73211.
> > +
> >  config BH1750
> >  	tristate "ROHM BH1750 ambient light sensor"
> >  	depends on I2C
> > diff --git a/drivers/iio/light/Makefile b/drivers/iio/light/Makefile
> > index d1c8aa30b9a8..ea376deaca54 100644
> > --- a/drivers/iio/light/Makefile
> > +++ b/drivers/iio/light/Makefile
> > @@ -11,6 +11,7 @@ obj-$(CONFIG_AL3010)		+= al3010.o
> >  obj-$(CONFIG_AL3320A)		+= al3320a.o
> >  obj-$(CONFIG_APDS9300)		+= apds9300.o
> >  obj-$(CONFIG_APDS9960)		+= apds9960.o
> > +obj-$(CONFIG_AS73211)		+= as73211.o
> >  obj-$(CONFIG_BH1750)		+= bh1750.o
> >  obj-$(CONFIG_BH1780)		+= bh1780.o
> >  obj-$(CONFIG_CM32181)		+= cm32181.o
> > diff --git a/drivers/iio/light/as73211.c b/drivers/iio/light/as73211.c
> > new file mode 100644
> > index 000000000000..3383aaacbf52
> > --- /dev/null
> > +++ b/drivers/iio/light/as73211.c
> > @@ -0,0 +1,801 @@
> > +// SPDX-License-Identifier: GPL-2.0-only
> > +/*
> > + * Support for AMS AS73211 JENCOLOR(R) Digital XYZ Sensor
> > + *
> > + * Author: Christian Eggers <ceggers@arri.de>
> > + *
> > + * Copyright (c) 2020 ARRI Lighting
> > + *
> > + * Color light sensor with 16-bit channels for x, y, z and temperature);
> > + * 7-bit I2C slave address 0x74 .. 0x77.
> > + *
> > + * Datasheet: https://ams.com/documents/20143/36005/AS73211_DS000556_3-01.pdf
> > + */
> > +
> > +#include <linux/bitfield.h>
> > +#include <linux/completion.h>
> > +#include <linux/delay.h>
> > +#include <linux/i2c.h>
> > +#include <linux/iio/buffer.h>
> > +#include <linux/iio/iio.h>
> > +#include <linux/iio/sysfs.h>
> > +#include <linux/iio/trigger_consumer.h>
> > +#include <linux/iio/triggered_buffer.h>
> > +#include <linux/module.h>
> > +#include <linux/mutex.h>
> > +#include <linux/pm.h>
> > +
> > +#define HZ_PER_KHZ 1000
> > +
> > +#define AS73211_DRV_NAME "as73211"
> > +
> > +/* AS73211 configuration registers */
> > +#define AS73211_REG_OSR    0x0
> > +#define AS73211_REG_AGEN   0x2
> > +#define AS73211_REG_CREG1  0x6
> > +#define AS73211_REG_CREG2  0x7
> > +#define AS73211_REG_CREG3  0x8
> > +
> > +/* AS73211 output register bank */
> > +#define AS73211_OUT_OSR_STATUS    0
> > +#define AS73211_OUT_TEMP          1
> > +#define AS73211_OUT_MRES1         2
> > +#define AS73211_OUT_MRES2         3
> > +#define AS73211_OUT_MRES3         4
> > +
> > +#define AS73211_OSR_SS            BIT(7)
> > +#define AS73211_OSR_PD            BIT(6)
> > +#define AS73211_OSR_SW_RES        BIT(3)
> > +#define AS73211_OSR_DOS_MASK      GENMASK(2, 0)
> > +#define AS73211_OSR_DOS_CONFIG    FIELD_PREP(AS73211_OSR_DOS_MASK, 0x2)
> > +#define AS73211_OSR_DOS_MEASURE   FIELD_PREP(AS73211_OSR_DOS_MASK, 0x3)
> > +
> > +#define AS73211_AGEN_DEVID_MASK   GENMASK(7, 4)
> > +#define AS73211_AGEN_DEVID(x)     FIELD_PREP(AS73211_AGEN_DEVID_MASK, (x))
> > +#define AS73211_AGEN_MUT_MASK     GENMASK(3, 0)
> > +#define AS73211_AGEN_MUT(x)       FIELD_PREP(AS73211_AGEN_MUT_MASK, (x))
> > +
> > +#define AS73211_CREG1_GAIN_MASK   GENMASK(7, 4)
> > +#define AS73211_CREG1_GAIN_1      13
> > +#define AS73211_CREG1_TIME_MASK   GENMASK(3, 0)
> > +
> > +#define AS73211_CREG3_CCLK_MASK   GENMASK(1, 0)
> > +
> > +#define AS73211_OSR_STATUS_OUTCONVOF  BIT(15)
> > +#define AS73211_OSR_STATUS_MRESOF     BIT(14)
> > +#define AS73211_OSR_STATUS_ADCOF      BIT(13)
> > +#define AS73211_OSR_STATUS_LDATA      BIT(12)
> > +#define AS73211_OSR_STATUS_NDATA      BIT(11)
> > +#define AS73211_OSR_STATUS_NOTREADY   BIT(10)
> > +
> > +#define AS73211_SAMPLE_FREQ_BASE      1024000
> > +
> > +#define AS73211_SAMPLE_TIME_NUM       15
> > +#define AS73211_SAMPLE_TIME_MAX_MS    BIT(AS73211_SAMPLE_TIME_NUM - 1)
> > +
> > +/* Available sample frequencies are 1.024MHz multiplied by powers of two. */
> > +static const int as73211_samp_freq_avail[] = {
> > +	AS73211_SAMPLE_FREQ_BASE * 1,
> > +	AS73211_SAMPLE_FREQ_BASE * 2,
> > +	AS73211_SAMPLE_FREQ_BASE * 4,
> > +	AS73211_SAMPLE_FREQ_BASE * 8,
> > +};
> > +
> > +static const int as73211_hardwaregain_avail[] = {
> > +	1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048,
> > +};
> > +
> > +/**
> > + * struct as73211_data - Instance data for one AS73211
> > + * @client: I2C client.
> > + * @osr:    Cached Operational State Register.
> > + * @creg1:  Cached Configuration Register 1.
> > + * @creg2:  Cached Configuration Register 2.
> > + * @creg3:  Cached Configuration Register 3.
> > + * @mutex:  Keeps cached registers in sync with the device.
> > + * @completion: Completion to wait for interrupt.
> > + * @int_time_avail: Available integration times (depend on sampling frequency).
> > + */
> > +struct as73211_data {
> > +	struct i2c_client *client;
> > +	u8 osr;
> > +	u8 creg1;
> > +	u8 creg2;
> > +	u8 creg3;
> > +	struct mutex mutex;
> > +	struct completion completion;
> > +	int int_time_avail[AS73211_SAMPLE_TIME_NUM * 2];

This is a flat array, as get_avail assumes.

> > +};
> > +
> > +#define AS73211_COLOR_CHANNEL(_color, _si, _addr) { \
> > +	.type = IIO_INTENSITY, \
> > +	.modified = 1, \
> > +	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), \
> > +	.info_mask_shared_by_type = \
> > +		BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
> > +		BIT(IIO_CHAN_INFO_HARDWAREGAIN) | \
> > +		BIT(IIO_CHAN_INFO_INT_TIME), \
> > +	.info_mask_shared_by_type_available = \
> > +		BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
> > +		BIT(IIO_CHAN_INFO_HARDWAREGAIN) | \
> > +		BIT(IIO_CHAN_INFO_INT_TIME), \
> > +	.channel2 = IIO_MOD_##_color, \
> > +	.address = _addr, \
> > +	.scan_index = _si, \
> > +	.scan_type = { \
> > +		.sign = 'u', \
> > +		.realbits = 16, \
> > +		.storagebits = 16, \
> > +		.endianness = IIO_LE, \
> > +	}, \
> > +}
> > +
> > +#define AS73211_OFFSET_TEMP_INT    (-66)
> > +#define AS73211_OFFSET_TEMP_MICRO  900000
> > +#define AS73211_SCALE_TEMP_INT     0
> > +#define AS73211_SCALE_TEMP_MICRO   50000
> > +
> > +#define AS73211_SCALE_X 277071108  /* nW/m^2 */
> > +#define AS73211_SCALE_Y 298384270  /* nW/m^2 */
> > +#define AS73211_SCALE_Z 160241927  /* nW/m^2 */
> > +
> > +/* Channel order MUST match devices result register order */
> > +#define AS73211_SCAN_INDEX_TEMP 0
> > +#define AS73211_SCAN_INDEX_X    1
> > +#define AS73211_SCAN_INDEX_Y    2
> > +#define AS73211_SCAN_INDEX_Z    3
> > +#define AS73211_SCAN_INDEX_TS   4
> > +
> > +#define AS73211_SCAN_MASK_COLOR ( \
> > +	BIT(AS73211_SCAN_INDEX_X) |   \
> > +	BIT(AS73211_SCAN_INDEX_Y) |   \
> > +	BIT(AS73211_SCAN_INDEX_Z))
> > +
> > +#define AS73211_SCAN_MASK_ALL (    \
> > +	BIT(AS73211_SCAN_INDEX_TEMP) | \
> > +	AS73211_SCAN_MASK_COLOR)
> > +
> > +static const struct iio_chan_spec as73211_channels[] = {
> > +	{
> > +		.type = IIO_TEMP,
> > +		.info_mask_separate =
> > +			BIT(IIO_CHAN_INFO_RAW) |
> > +			BIT(IIO_CHAN_INFO_OFFSET) |
> > +			BIT(IIO_CHAN_INFO_SCALE),
> > +		.address = AS73211_OUT_TEMP,
> > +		.scan_index = AS73211_SCAN_INDEX_TEMP,
> > +		.scan_type = {
> > +			.sign = 'u',
> > +			.realbits = 16,
> > +			.storagebits = 16,
> > +			.endianness = IIO_LE,
> > +		}
> > +	},
> > +	AS73211_COLOR_CHANNEL(X, AS73211_SCAN_INDEX_X, AS73211_OUT_MRES1),
> > +	AS73211_COLOR_CHANNEL(Y, AS73211_SCAN_INDEX_Y, AS73211_OUT_MRES2),
> > +	AS73211_COLOR_CHANNEL(Z, AS73211_SCAN_INDEX_Z, AS73211_OUT_MRES3),
> > +	IIO_CHAN_SOFT_TIMESTAMP(AS73211_SCAN_INDEX_TS),
> > +};
> > +
> > +static unsigned int as73211_integration_time_1024cyc(struct as73211_data *data)
> > +{
> > +	/*
> > +	 * Return integration time in units of 1024 clock cycles. Integration time
> > +	 * in CREG1 is in powers of 2 (x 1024 cycles).
> > +	 */
> > +	return BIT(FIELD_GET(AS73211_CREG1_TIME_MASK, data->creg1));
> > +}
> > +
> > +static unsigned int as73211_integration_time_us(struct as73211_data *data,
> > +						 unsigned int integration_time_1024cyc)
> > +{
> > +	/*
> > +	 * f_samp is configured in CREG3 in powers of 2 (x 1.024 MHz)
> > +	 * t_cycl is configured in CREG1 in powers of 2 (x 1024 cycles)
> > +	 * t_int_us = 1 / (f_samp) * t_cycl * US_PER_SEC
> > +	 *          = 1 / (2^CREG3_CCLK * 1,024,000) * 2^CREG1_CYCLES * 1,024 * US_PER_SEC
> > +	 *          = 2^(-CREG3_CCLK) * 2^CREG1_CYCLES * 1,000
> > +	 * In order to get rid of negative exponents, we extend the "fraction"
> > +	 * by 2^3 (CREG3_CCLK,max = 3)
> > +	 * t_int_us = 2^(3-CREG3_CCLK) * 2^CREG1_CYCLES * 125
> > +	 */
> > +	return BIT(3 - FIELD_GET(AS73211_CREG3_CCLK_MASK, data->creg3)) *
> > +		integration_time_1024cyc * 125;
> > +}
> > +
> > +static void as73211_integration_time_calc_avail(struct as73211_data *data)
> > +{
> > +	int i;
> > +
> > +	for (i = 0; i < ARRAY_SIZE(data->int_time_avail); i++) {
This loops goes to double the size of the array.

I'd propose to just do ARRAY_SIZE(data->int_time_avail) / 2 here

> > +		unsigned int time_us = as73211_integration_time_us(data, BIT(i));
> > +
> > +		data->int_time_avail[i * 2 + 0] = time_us / USEC_PER_SEC;
> > +		data->int_time_avail[i * 2 + 1] = time_us % USEC_PER_SEC;
> > +	}
> > +}
> > +
> > +static unsigned int as73211_gain(struct as73211_data *data)
> > +{
> > +	/* gain can be calculated from CREG1 as 2^(13 - CREG1_GAIN) */
> > +	return BIT(AS73211_CREG1_GAIN_1 - FIELD_GET(AS73211_CREG1_GAIN_MASK, data->creg1));
> > +}
> > +
> > +/* must be called with as73211_data::mutex held. */
> > +static int as73211_req_data(struct as73211_data *data)
> > +{
> > +	unsigned int time_us = as73211_integration_time_us(data,
> > +							    as73211_integration_time_1024cyc(data));
> > +	struct device *dev = &data->client->dev;
> > +	union i2c_smbus_data smbus_data;
> > +	u16 osr_status;
> > +	int ret;
> > +
> > +	if (data->client->irq)
> > +		reinit_completion(&data->completion);
> > +
> > +	/*
> > +	 * During measurement, there should be no traffic on the i2c bus as the
> > +	 * electrical noise would disturb the measurement process.
> > +	 */
> > +	i2c_lock_bus(data->client->adapter, I2C_LOCK_SEGMENT);
> > +
> > +	data->osr &= ~AS73211_OSR_DOS_MASK;
> > +	data->osr |= AS73211_OSR_DOS_MEASURE | AS73211_OSR_SS;
> > +
> > +	smbus_data.byte = data->osr;
> > +	ret = __i2c_smbus_xfer(data->client->adapter, data->client->addr,
> > +			data->client->flags, I2C_SMBUS_WRITE,
> > +			AS73211_REG_OSR, I2C_SMBUS_BYTE_DATA, &smbus_data);
> > +	if (ret < 0) {
> > +		i2c_unlock_bus(data->client->adapter, I2C_LOCK_SEGMENT);
> > +		return ret;
> > +	}
> > +
> > +	/*
> > +	 * Reset AS73211_OSR_SS (is self clearing) in order to avoid unintentional
> > +	 * triggering of further measurements later.
> > +	 */
> > +	data->osr &= ~AS73211_OSR_SS;
> > +
> > +	/*
> > +	 * Add some extra margin for the timeout. sensor timing is not as precise
> > +	 * as our one ...
> > +	 */
> > +	time_us += time_us / 8;
> > +	if (data->client->irq) {
> > +		ret = wait_for_completion_timeout(&data->completion, usecs_to_jiffies(time_us));
> > +		if (!ret) {
> > +			dev_err(dev, "timeout waiting for READY IRQ\n");
> > +			i2c_unlock_bus(data->client->adapter, I2C_LOCK_SEGMENT);
> > +			return -ETIMEDOUT;
> > +		}
> > +	} else {
> > +		/* Wait integration time */
> > +		usleep_range(time_us, 2 * time_us);
> > +	}
> > +
> > +	i2c_unlock_bus(data->client->adapter, I2C_LOCK_SEGMENT);
> > +
> > +	ret = i2c_smbus_read_word_data(data->client, AS73211_OUT_OSR_STATUS);
> > +	if (ret < 0)
> > +		return ret;
> > +
> > +	osr_status = ret;
> > +	if (osr_status != (AS73211_OSR_DOS_MEASURE | AS73211_OSR_STATUS_NDATA)) {
> > +		if (osr_status & AS73211_OSR_SS) {
> > +			dev_err(dev, "%s() Measurement has not stopped\n", __func__);
> > +			return -ETIME;
> > +		}
> > +		if (osr_status & AS73211_OSR_STATUS_NOTREADY) {
> > +			dev_err(dev, "%s() Data is not ready\n", __func__);
> > +			return -ENODATA;
> > +		}
> > +		if (!(osr_status & AS73211_OSR_STATUS_NDATA)) {
> > +			dev_err(dev, "%s() No new data available\n", __func__);
> > +			return -ENODATA;
> > +		}
> > +		if (osr_status & AS73211_OSR_STATUS_LDATA) {
> > +			dev_err(dev, "%s() Result buffer overrun\n", __func__);
> > +			return -ENOBUFS;
> > +		}
> > +		if (osr_status & AS73211_OSR_STATUS_ADCOF) {
> > +			dev_err(dev, "%s() ADC overflow\n", __func__);
> > +			return -EOVERFLOW;
> > +		}
> > +		if (osr_status & AS73211_OSR_STATUS_MRESOF) {
> > +			dev_err(dev, "%s() Measurement result overflow\n", __func__);
> > +			return -EOVERFLOW;
> > +		}
> > +		if (osr_status & AS73211_OSR_STATUS_OUTCONVOF) {
> > +			dev_err(dev, "%s() Timer overflow\n", __func__);
> > +			return -EOVERFLOW;
> > +		}
> > +		dev_err(dev, "%s() Unexpected status value\n", __func__);
> > +		return -EIO;
> > +	}
> > +
> > +	return 0;
> > +}
> > +
> > +static int as73211_read_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan,
> > +			     int *val, int *val2, long mask)
> > +{
> > +	struct as73211_data *data = iio_priv(indio_dev);
> > +
> > +	switch (mask) {
> > +	case IIO_CHAN_INFO_RAW: {
> > +		int ret;
> > +
> > +		ret = iio_device_claim_direct_mode(indio_dev);
> > +		if (ret < 0)
> > +			return ret;
> > +
> > +		ret = as73211_req_data(data);
> > +		if (ret < 0) {
> > +			iio_device_release_direct_mode(indio_dev);
> > +			return ret;
> > +		}
> > +
> > +		ret = i2c_smbus_read_word_data(data->client, chan->address);
> > +		iio_device_release_direct_mode(indio_dev);
> > +		if (ret < 0)
> > +			return ret;
> > +
> > +		*val = ret;
> > +		return IIO_VAL_INT;
> > +	}
> > +	case IIO_CHAN_INFO_OFFSET:
> > +		*val = AS73211_OFFSET_TEMP_INT;
> > +		*val2 = AS73211_OFFSET_TEMP_MICRO;
> > +		return IIO_VAL_INT_PLUS_MICRO;
> > +
> > +	case IIO_CHAN_INFO_SCALE:
> > +		switch (chan->type) {
> > +		case IIO_TEMP:
> > +			*val = AS73211_SCALE_TEMP_INT;
> > +			*val2 = AS73211_SCALE_TEMP_MICRO;
> > +			return IIO_VAL_INT_PLUS_MICRO;
> > +
> > +		case IIO_INTENSITY: {
> > +			unsigned int scale;
> > +
> > +			switch (chan->channel2) {
> > +			case IIO_MOD_X:
> > +				scale = AS73211_SCALE_X;
> > +				break;
> > +			case IIO_MOD_Y:
> > +				scale = AS73211_SCALE_Y;
> > +				break;
> > +			case IIO_MOD_Z:
> > +				scale = AS73211_SCALE_Z;
> > +				break;
> > +			default:
> > +				return -EINVAL;
> > +			}
> > +			scale /= as73211_gain(data);
> > +			scale /= as73211_integration_time_1024cyc(data);
> > +			*val = scale;
> > +			return IIO_VAL_INT;
> > +
> > +		default:
> > +			return -EINVAL;
> > +		}}
> > +
> > +	case IIO_CHAN_INFO_SAMP_FREQ:
> > +		/* f_samp is configured in CREG3 in powers of 2 (x 1.024 MHz) */
> > +		*val = BIT(FIELD_GET(AS73211_CREG3_CCLK_MASK, data->creg3)) *
> > +			AS73211_SAMPLE_FREQ_BASE;
> > +		return IIO_VAL_INT;
> > +
> > +	case IIO_CHAN_INFO_HARDWAREGAIN:
> > +		*val = as73211_gain(data);
> > +		return IIO_VAL_INT;
> > +
> > +	case IIO_CHAN_INFO_INT_TIME: {
> > +		unsigned int time_us;
> > +
> > +		mutex_lock(&data->mutex);
> > +		time_us = as73211_integration_time_us(data, as73211_integration_time_1024cyc(data));
> > +		mutex_unlock(&data->mutex);
> > +		*val = time_us / USEC_PER_SEC;
> > +		*val2 = time_us % USEC_PER_SEC;
> > +		return IIO_VAL_INT_PLUS_MICRO;
> > +
> > +	default:
> > +		return -EINVAL;
> > +	}}
> > +}
> > +
> > +static int as73211_read_avail(struct iio_dev *indio_dev, struct iio_chan_spec const *chan,
> > +			       const int **vals, int *type, int *length, long mask)
> > +{
> > +	struct as73211_data *data = iio_priv(indio_dev);
> > +
> > +	switch (mask) {
> > +	case IIO_CHAN_INFO_SAMP_FREQ:
> > +		*length = ARRAY_SIZE(as73211_samp_freq_avail);
> > +		*vals = as73211_samp_freq_avail;
> > +		*type = IIO_VAL_INT;
> > +		return IIO_AVAIL_LIST;
> > +
> > +	case IIO_CHAN_INFO_HARDWAREGAIN:
> > +		*length = ARRAY_SIZE(as73211_hardwaregain_avail);
> > +		*vals = as73211_hardwaregain_avail;
> > +		*type = IIO_VAL_INT;
> > +		return IIO_AVAIL_LIST;
> > +
> > +	case IIO_CHAN_INFO_INT_TIME:
> > +		*length = ARRAY_SIZE(data->int_time_avail);

This length is correct as read_avail expects it to be double
the number of entries, if using IIO_VAL_INT_PLUS_MICRO.

> > +		*vals = data->int_time_avail;
> > +		*type = IIO_VAL_INT_PLUS_MICRO;
> > +		return IIO_AVAIL_LIST;
> > +
> > +	default:
> > +		return -EINVAL;
> > +	}
> > +}
> > +
> > +static int _as73211_write_raw(struct iio_dev *indio_dev,
> > +			       struct iio_chan_spec const *chan __always_unused,
> > +			       int val, int val2, long mask)
> > +{
> > +	struct as73211_data *data = iio_priv(indio_dev);
> > +	int ret;
> > +
> > +	switch (mask) {
> > +	case IIO_CHAN_INFO_SAMP_FREQ: {
> > +		int reg_bits, freq_kHz = val / HZ_PER_KHZ;  /* 1024, 2048, ... */
> > +
> > +		/* val must be 1024 * 2^x */
> > +		if (val < 0 || (freq_kHz * HZ_PER_KHZ) != val ||
> > +				!is_power_of_2(freq_kHz) || val2)
> > +			return -EINVAL;
> > +
> > +		/* f_samp is configured in CREG3 in powers of 2 (x 1.024 MHz (=2^10)) */
> > +		reg_bits = ilog2(freq_kHz) - 10;
> > +		if (!FIELD_FIT(AS73211_CREG3_CCLK_MASK, reg_bits))
> > +			return -EINVAL;
> > +
> > +		data->creg3 &= ~AS73211_CREG3_CCLK_MASK;
> > +		data->creg3 |= FIELD_PREP(AS73211_CREG3_CCLK_MASK, reg_bits);
> > +		as73211_integration_time_calc_avail(data);
> > +
> > +		ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_CREG3, data->creg3);
> > +		if (ret < 0)
> > +			return ret;
> > +
> > +		return 0;
> > +	}
> > +	case IIO_CHAN_INFO_HARDWAREGAIN: {
> > +		unsigned int reg_bits;
> > +
> > +		if (val < 0 || !is_power_of_2(val) || val2)
> > +			return -EINVAL;
> > +
> > +		/* gain can be calculated from CREG1 as 2^(13 - CREG1_GAIN) */
> > +		reg_bits = AS73211_CREG1_GAIN_1 - ilog2(val);
> > +		if (!FIELD_FIT(AS73211_CREG1_GAIN_MASK, reg_bits))
> > +			return -EINVAL;
> > +
> > +		data->creg1 &= ~AS73211_CREG1_GAIN_MASK;
> > +		data->creg1 |= FIELD_PREP(AS73211_CREG1_GAIN_MASK, reg_bits);
> > +
> > +		ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_CREG1, data->creg1);
> > +		if (ret < 0)
> > +			return ret;
> > +
> > +		return 0;
> > +	}
> > +	case IIO_CHAN_INFO_INT_TIME: {
> > +		int val_us = val * USEC_PER_SEC + val2;
> > +		int time_ms;
> > +		int reg_bits;
> > +
> > +		/* f_samp is configured in CREG3 in powers of 2 (x 1.024 MHz) */
> > +		int f_samp_1_024mhz = BIT(FIELD_GET(AS73211_CREG3_CCLK_MASK, data->creg3));
> > +
> > +		/*
> > +		 * time_ms = time_us * US_PER_MS * f_samp_1_024mhz / MHZ_PER_HZ
> > +		 *         = time_us * f_samp_1_024mhz / 1000
> > +		 */
> > +		time_ms = (val_us * f_samp_1_024mhz) / 1000;  /* 1 ms, 2 ms, ... (power of two) */
> > +		if (time_ms < 0 || !is_power_of_2(time_ms) || time_ms > AS73211_SAMPLE_TIME_MAX_MS)
> > +			return -EINVAL;
> > +
> > +		reg_bits = ilog2(time_ms);
> > +		if (!FIELD_FIT(AS73211_CREG1_TIME_MASK, reg_bits))
> > +			return -EINVAL;  /* not possible due to previous tests */
> > +
> > +		data->creg1 &= ~AS73211_CREG1_TIME_MASK;
> > +		data->creg1 |= FIELD_PREP(AS73211_CREG1_TIME_MASK, reg_bits);
> > +
> > +		ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_CREG1, data->creg1);
> > +		if (ret < 0)
> > +			return ret;
> > +
> > +		return 0;
> > +
> > +	default:
> > +		return -EINVAL;
> > +	}}
> > +}
> > +
> > +static int as73211_write_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan,
> > +			      int val, int val2, long mask)
> > +{
> > +	struct as73211_data *data = iio_priv(indio_dev);
> > +	int ret;
> > +
> > +	mutex_lock(&data->mutex);
> > +
> > +	ret = iio_device_claim_direct_mode(indio_dev);
> > +	if (ret < 0)
> > +		goto error_unlock;
> > +
> > +	/* Need to switch to config mode ... */
> > +	if ((data->osr & AS73211_OSR_DOS_MASK) != AS73211_OSR_DOS_CONFIG) {
> > +		data->osr &= ~AS73211_OSR_DOS_MASK;
> > +		data->osr |= AS73211_OSR_DOS_CONFIG;
> > +
> > +		ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_OSR, data->osr);
> > +		if (ret < 0)
> > +			goto error_release;
> > +	}
> > +
> > +	ret = _as73211_write_raw(indio_dev, chan, val, val2, mask);
> > +
> > +error_release:
> > +	iio_device_release_direct_mode(indio_dev);
> > +error_unlock:
> > +	mutex_unlock(&data->mutex);
> > +	return ret;
> > +}
> > +
> > +static irqreturn_t as73211_ready_handler(int irq __always_unused, void *priv)
> > +{
> > +	struct as73211_data *data = iio_priv(priv);
> > +
> > +	complete(&data->completion);
> > +
> > +	return IRQ_HANDLED;
> > +}
> > +
> > +static irqreturn_t as73211_trigger_handler(int irq __always_unused, void *p)
> > +{
> > +	struct iio_poll_func *pf = p;
> > +	struct iio_dev *indio_dev = pf->indio_dev;
> > +	struct as73211_data *data = iio_priv(indio_dev);
> > +	struct {
> > +		__le16 chan[4];
> > +		s64 ts __aligned(8);
> > +	} scan;
> > +	int data_result, ret;
> > +
> > +	mutex_lock(&data->mutex);
> > +
> > +	data_result = as73211_req_data(data);
> > +	if (data_result < 0 && data_result != -EOVERFLOW)
> > +		goto done;  /* don't push any data for errors other than EOVERFLOW */
> > +
> > +	if (*indio_dev->active_scan_mask == AS73211_SCAN_MASK_ALL) {
> > +		/* Optimization for reading all (color + temperature) channels */
> > +		u8 addr = as73211_channels[0].address;
> > +		struct i2c_msg msgs[] = {
> > +			{
> > +				.addr = data->client->addr,
> > +				.flags = 0,
> > +				.len = 1,
> > +				.buf = &addr,
> > +			},
> > +			{
> > +				.addr = data->client->addr,
> > +				.flags = I2C_M_RD,
> > +				.len = sizeof(scan.chan),
> > +				.buf = (u8 *)&scan.chan,
> > +			},
> > +		};
> > +
> > +		ret = i2c_transfer(data->client->adapter, msgs, ARRAY_SIZE(msgs));
> > +		if (ret < 0)
> > +			goto done;
> > +	} else {
> > +		/* Optimization for reading only color channels */
> > +
> > +		/* AS73211 starts reading at address 2 */
> > +		ret = i2c_master_recv(data->client,
> > +				(char *)&scan.chan[1], 3 * sizeof(scan.chan[1]));
> > +		if (ret < 0)
> > +			goto done;
> > +	}
> > +
> > +	if (data_result) {
> > +		/*
> > +		 * Saturate all channels (in case of overflows). Temperature channel
> > +		 * is not affected by overflows.
> > +		 */
> > +		scan.chan[1] = cpu_to_le16(U16_MAX);
> > +		scan.chan[2] = cpu_to_le16(U16_MAX);
> > +		scan.chan[3] = cpu_to_le16(U16_MAX);
> > +	}
> > +
> > +	iio_push_to_buffers_with_timestamp(indio_dev, &scan, iio_get_time_ns(indio_dev));
> > +
> > +done:
> > +	mutex_unlock(&data->mutex);
> > +	iio_trigger_notify_done(indio_dev->trig);
> > +
> > +	return IRQ_HANDLED;
> > +}
> > +
> > +static const struct iio_info as73211_info = {
> > +	.read_raw = as73211_read_raw,
> > +	.read_avail = as73211_read_avail,
> > +	.write_raw = as73211_write_raw,
> > +};
> > +
> > +static int as73211_power(struct iio_dev *indio_dev, bool state)
> > +{
> > +	struct as73211_data *data = iio_priv(indio_dev);
> > +	int ret;
> > +
> > +	mutex_lock(&data->mutex);
> > +
> > +	if (state)
> > +		data->osr &= ~AS73211_OSR_PD;
> > +	else
> > +		data->osr |= AS73211_OSR_PD;
> > +
> > +	ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_OSR, data->osr);
> > +
> > +	mutex_unlock(&data->mutex);
> > +
> > +	if (ret < 0)
> > +		return ret;
> > +
> > +	return 0;
> > +}
> > +
> > +static void as73211_power_disable(void *data)
> > +{
> > +	struct iio_dev *indio_dev = data;
> > +
> > +	as73211_power(indio_dev, false);
> > +}
> > +
> > +static int as73211_probe(struct i2c_client *client)
> > +{
> > +	struct device *dev = &client->dev;
> > +	struct as73211_data *data;
> > +	struct iio_dev *indio_dev;
> > +	int ret;
> > +
> > +	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
> > +	if (!indio_dev)
> > +		return -ENOMEM;
> > +
> > +	data = iio_priv(indio_dev);
> > +	i2c_set_clientdata(client, indio_dev);
> > +	data->client = client;
> > +
> > +	mutex_init(&data->mutex);
> > +	init_completion(&data->completion);
> > +
> > +	indio_dev->info = &as73211_info;
> > +	indio_dev->name = AS73211_DRV_NAME;
> > +	indio_dev->channels = as73211_channels;
> > +	indio_dev->num_channels = ARRAY_SIZE(as73211_channels);
> > +	indio_dev->modes = INDIO_DIRECT_MODE;
> > +
> > +	ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_OSR);
> > +	if (ret < 0)
> > +		return ret;
> > +	data->osr = ret;
> > +
> > +	/* reset device */
> > +	data->osr |= AS73211_OSR_SW_RES;
> > +	ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_OSR, data->osr);
> > +	if (ret < 0)
> > +		return ret;
> > +
> > +	ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_OSR);
> > +	if (ret < 0)
> > +		return ret;
> > +	data->osr = ret;
> > +
> > +	/*
> > +	 * Reading AGEN is only possible after reset (AGEN is not available if
> > +	 * device is in measurement mode).
> > +	 */
> > +	ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_AGEN);
> > +	if (ret < 0)
> > +		return ret;
> > +
> > +	/* At the time of writing this driver, only DEVID 2 and MUT 1 are known. */
> > +	if ((ret & AS73211_AGEN_DEVID_MASK) != AS73211_AGEN_DEVID(2) ||
> > +	    (ret & AS73211_AGEN_MUT_MASK) != AS73211_AGEN_MUT(1))
> > +		return -ENODEV;
> > +
> > +	ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_CREG1);
> > +	if (ret < 0)
> > +		return ret;
> > +	data->creg1 = ret;
> > +
> > +	ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_CREG2);
> > +	if (ret < 0)
> > +		return ret;
> > +	data->creg2 = ret;
> > +
> > +	ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_CREG3);
> > +	if (ret < 0)
> > +		return ret;
> > +	data->creg3 = ret;
> > +	as73211_integration_time_calc_avail(data);
> > +
> > +	ret = as73211_power(indio_dev, true);
> > +	if (ret < 0)
> > +		return ret;
> > +
> > +	ret = devm_add_action_or_reset(dev, as73211_power_disable, indio_dev);
> > +	if (ret)
> > +		return ret;
> > +
> > +	ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL, as73211_trigger_handler, NULL);
> > +	if (ret)
> > +		return ret;
> > +
> > +	if (client->irq) {
> > +		ret = devm_request_threaded_irq(&client->dev, client->irq,
> > +				NULL,
> > +				as73211_ready_handler,
> > +				IRQF_ONESHOT,
> > +				client->name, indio_dev);
> > +		if (ret)
> > +			return ret;
> > +	}
> > +
> > +	return devm_iio_device_register(dev, indio_dev);
> > +}
> > +
> > +static int __maybe_unused as73211_suspend(struct device *dev)
> > +{
> > +	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
> > +
> > +	return as73211_power(indio_dev, false);
> > +}
> > +
> > +static int __maybe_unused as73211_resume(struct device *dev)
> > +{
> > +	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
> > +
> > +	return as73211_power(indio_dev, true);
> > +}
> > +
> > +static SIMPLE_DEV_PM_OPS(as73211_pm_ops, as73211_suspend, as73211_resume);
> > +
> > +static const struct of_device_id as73211_of_match[] = {
> > +	{ .compatible = "ams,as73211" },
> > +	{ }
> > +};
> > +MODULE_DEVICE_TABLE(of, as73211_of_match);
> > +
> > +static const struct i2c_device_id as73211_id[] = {
> > +	{ "as73211", 0 },
> > +	{ }
> > +};
> > +MODULE_DEVICE_TABLE(i2c, as73211_id);
> > +
> > +static struct i2c_driver as73211_driver = {
> > +	.driver = {
> > +		.name           = AS73211_DRV_NAME,
> > +		.of_match_table = as73211_of_match,
> > +		.pm             = &as73211_pm_ops,
> > +	},
> > +	.probe_new  = as73211_probe,
> > +	.id_table   = as73211_id,
> > +};
> > +module_i2c_driver(as73211_driver);
> > +
> > +MODULE_AUTHOR("Christian Eggers <ceggers@arri.de>");
> > +MODULE_DESCRIPTION("AS73211 XYZ True Color Sensor driver");
> > +MODULE_LICENSE("GPL");  
> 


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH v6 2/2] iio: light: as73211: New driver
  2020-09-01 18:46     ` [PATCH v6 2/2] iio: light: as73211: New driver Jonathan Cameron
@ 2020-09-03 18:43       ` Jonathan Cameron
  2020-09-09 18:05         ` Christian Eggers
  0 siblings, 1 reply; 20+ messages in thread
From: Jonathan Cameron @ 2020-09-03 18:43 UTC (permalink / raw)
  To: Christian Eggers
  Cc: Rob Herring, Andy Shevchenko, Jonathan Cameron, Hartmut Knaack,
	Lars-Peter Clausen, Peter Meerwald-Stadler, linux-iio,
	devicetree, linux-kernel

On Tue, 1 Sep 2020 19:46:00 +0100
Jonathan Cameron <jic23@kernel.org> wrote:

> On Thu, 6 Aug 2020 18:44:51 +0100
> Jonathan Cameron <jic23@kernel.org> wrote:
> 
> > On Wed, 5 Aug 2020 07:57:44 +0200
> > Christian Eggers <ceggers@arri.de> wrote:
> >   
> > > Support for AMS AS73211 JENCOLOR(R) Digital XYZ Sensor.
> > > 
> > > This driver has no built-in trigger. In order for making triggered
> > > measurements, an external (software) trigger driver like
> > > iio-trig-hrtimer or iio-trig-sysfs is required.
> > > 
> > > The sensor supports single and continuous measurement modes. The latter
> > > is not used by design as this would require tight timing synchronization
> > > between hardware and driver without much benefit.
> > > 
> > > Datasheet: https://ams.com/documents/20143/36005/AS73211_DS000556_3-01.pdf
> > > Signed-off-by: Christian Eggers <ceggers@arri.de>    
> > 
> > Hi Christian,
> > 
> > I'll take this, but please send a follow up patch to add documentation
> > for in_intensity_x_raw and all the other new ABI this adds in
> > Documentation/ABI/testing/sysfs-bus-iio
> > I should have mentioned that earlier, but kind of assumed we already
> > had these documented for some reason!
> > 
> > Applied to the togreg branch of iio.git and pushed out as testing for
> > the autobuilders to play with it.
> > 
> > Thanks,
> > 
> > Jonathan  
> Unfortunatey I don't have the thread on this account / machine so I'll
> reply here.
> 
> 0-day highlighted an issue in here and Andy sent a suggested fix.
> 
> What Andy suggested almost works but there are some subtleties. So before
> I make the change directly on the patch I wanted to confirm I haven't
> gotten it horribly wrong!
> 
> I'm proposing just halving the range of the loop. See below.

I've done this halving change because I'm fairly sure it's right and want
to get a clean report from 0-day before I do a pull request at the
weekend.  Still time to change if anyone shouts quickly. If not
there is still plenty of time for a fix.

Thanks,

Jonathan


> 
> Thanks,
> 
> Jonathan
> 
> 
> > 
> >   
> > > ---
> > >  MAINTAINERS                 |   7 +
> > >  drivers/iio/light/Kconfig   |  15 +
> > >  drivers/iio/light/Makefile  |   1 +
> > >  drivers/iio/light/as73211.c | 801 ++++++++++++++++++++++++++++++++++++
> > >  4 files changed, 824 insertions(+)
> > >  create mode 100644 drivers/iio/light/as73211.c
> > > 
> > > diff --git a/MAINTAINERS b/MAINTAINERS
> > > index 068d6e94122b..673570414147 100644
> > > --- a/MAINTAINERS
> > > +++ b/MAINTAINERS
> > > @@ -934,6 +934,13 @@ S:	Supported
> > >  F:	arch/arm64/boot/dts/amd/amd-seattle-xgbe*.dtsi
> > >  F:	drivers/net/ethernet/amd/xgbe/
> > >  
> > > +AMS AS73211 DRIVER
> > > +M:	Christian Eggers <ceggers@arri.de>
> > > +L:	linux-iio@vger.kernel.org
> > > +S:	Maintained
> > > +F:	Documentation/devicetree/bindings/iio/light/ams,as73211.yaml
> > > +F:	drivers/iio/light/as73211.c
> > > +
> > >  ANALOG DEVICES INC AD5686 DRIVER
> > >  M:	Michael Hennerich <Michael.Hennerich@analog.com>
> > >  L:	linux-pm@vger.kernel.org
> > > diff --git a/drivers/iio/light/Kconfig b/drivers/iio/light/Kconfig
> > > index 182bd18c4bb2..cade6dc0305b 100644
> > > --- a/drivers/iio/light/Kconfig
> > > +++ b/drivers/iio/light/Kconfig
> > > @@ -86,6 +86,21 @@ config APDS9960
> > >  	  To compile this driver as a module, choose M here: the
> > >  	  module will be called apds9960
> > >  
> > > +config AS73211
> > > +	tristate "AMS AS73211 XYZ color sensor"
> > > +	depends on I2C
> > > +	select IIO_BUFFER
> > > +	select IIO_TRIGGERED_BUFFER
> > > +	help
> > > +	 If you say yes here you get support for the AMS AS73211
> > > +	 JENCOLOR(R) Digital XYZ Sensor.
> > > +
> > > +	 For triggered measurements, you will need an additional trigger driver
> > > +	 like IIO_HRTIMER_TRIGGER or IIO_SYSFS_TRIGGER.
> > > +
> > > +	 This driver can also be built as a module.  If so, the module
> > > +	 will be called as73211.
> > > +
> > >  config BH1750
> > >  	tristate "ROHM BH1750 ambient light sensor"
> > >  	depends on I2C
> > > diff --git a/drivers/iio/light/Makefile b/drivers/iio/light/Makefile
> > > index d1c8aa30b9a8..ea376deaca54 100644
> > > --- a/drivers/iio/light/Makefile
> > > +++ b/drivers/iio/light/Makefile
> > > @@ -11,6 +11,7 @@ obj-$(CONFIG_AL3010)		+= al3010.o
> > >  obj-$(CONFIG_AL3320A)		+= al3320a.o
> > >  obj-$(CONFIG_APDS9300)		+= apds9300.o
> > >  obj-$(CONFIG_APDS9960)		+= apds9960.o
> > > +obj-$(CONFIG_AS73211)		+= as73211.o
> > >  obj-$(CONFIG_BH1750)		+= bh1750.o
> > >  obj-$(CONFIG_BH1780)		+= bh1780.o
> > >  obj-$(CONFIG_CM32181)		+= cm32181.o
> > > diff --git a/drivers/iio/light/as73211.c b/drivers/iio/light/as73211.c
> > > new file mode 100644
> > > index 000000000000..3383aaacbf52
> > > --- /dev/null
> > > +++ b/drivers/iio/light/as73211.c
> > > @@ -0,0 +1,801 @@
> > > +// SPDX-License-Identifier: GPL-2.0-only
> > > +/*
> > > + * Support for AMS AS73211 JENCOLOR(R) Digital XYZ Sensor
> > > + *
> > > + * Author: Christian Eggers <ceggers@arri.de>
> > > + *
> > > + * Copyright (c) 2020 ARRI Lighting
> > > + *
> > > + * Color light sensor with 16-bit channels for x, y, z and temperature);
> > > + * 7-bit I2C slave address 0x74 .. 0x77.
> > > + *
> > > + * Datasheet: https://ams.com/documents/20143/36005/AS73211_DS000556_3-01.pdf
> > > + */
> > > +
> > > +#include <linux/bitfield.h>
> > > +#include <linux/completion.h>
> > > +#include <linux/delay.h>
> > > +#include <linux/i2c.h>
> > > +#include <linux/iio/buffer.h>
> > > +#include <linux/iio/iio.h>
> > > +#include <linux/iio/sysfs.h>
> > > +#include <linux/iio/trigger_consumer.h>
> > > +#include <linux/iio/triggered_buffer.h>
> > > +#include <linux/module.h>
> > > +#include <linux/mutex.h>
> > > +#include <linux/pm.h>
> > > +
> > > +#define HZ_PER_KHZ 1000
> > > +
> > > +#define AS73211_DRV_NAME "as73211"
> > > +
> > > +/* AS73211 configuration registers */
> > > +#define AS73211_REG_OSR    0x0
> > > +#define AS73211_REG_AGEN   0x2
> > > +#define AS73211_REG_CREG1  0x6
> > > +#define AS73211_REG_CREG2  0x7
> > > +#define AS73211_REG_CREG3  0x8
> > > +
> > > +/* AS73211 output register bank */
> > > +#define AS73211_OUT_OSR_STATUS    0
> > > +#define AS73211_OUT_TEMP          1
> > > +#define AS73211_OUT_MRES1         2
> > > +#define AS73211_OUT_MRES2         3
> > > +#define AS73211_OUT_MRES3         4
> > > +
> > > +#define AS73211_OSR_SS            BIT(7)
> > > +#define AS73211_OSR_PD            BIT(6)
> > > +#define AS73211_OSR_SW_RES        BIT(3)
> > > +#define AS73211_OSR_DOS_MASK      GENMASK(2, 0)
> > > +#define AS73211_OSR_DOS_CONFIG    FIELD_PREP(AS73211_OSR_DOS_MASK, 0x2)
> > > +#define AS73211_OSR_DOS_MEASURE   FIELD_PREP(AS73211_OSR_DOS_MASK, 0x3)
> > > +
> > > +#define AS73211_AGEN_DEVID_MASK   GENMASK(7, 4)
> > > +#define AS73211_AGEN_DEVID(x)     FIELD_PREP(AS73211_AGEN_DEVID_MASK, (x))
> > > +#define AS73211_AGEN_MUT_MASK     GENMASK(3, 0)
> > > +#define AS73211_AGEN_MUT(x)       FIELD_PREP(AS73211_AGEN_MUT_MASK, (x))
> > > +
> > > +#define AS73211_CREG1_GAIN_MASK   GENMASK(7, 4)
> > > +#define AS73211_CREG1_GAIN_1      13
> > > +#define AS73211_CREG1_TIME_MASK   GENMASK(3, 0)
> > > +
> > > +#define AS73211_CREG3_CCLK_MASK   GENMASK(1, 0)
> > > +
> > > +#define AS73211_OSR_STATUS_OUTCONVOF  BIT(15)
> > > +#define AS73211_OSR_STATUS_MRESOF     BIT(14)
> > > +#define AS73211_OSR_STATUS_ADCOF      BIT(13)
> > > +#define AS73211_OSR_STATUS_LDATA      BIT(12)
> > > +#define AS73211_OSR_STATUS_NDATA      BIT(11)
> > > +#define AS73211_OSR_STATUS_NOTREADY   BIT(10)
> > > +
> > > +#define AS73211_SAMPLE_FREQ_BASE      1024000
> > > +
> > > +#define AS73211_SAMPLE_TIME_NUM       15
> > > +#define AS73211_SAMPLE_TIME_MAX_MS    BIT(AS73211_SAMPLE_TIME_NUM - 1)
> > > +
> > > +/* Available sample frequencies are 1.024MHz multiplied by powers of two. */
> > > +static const int as73211_samp_freq_avail[] = {
> > > +	AS73211_SAMPLE_FREQ_BASE * 1,
> > > +	AS73211_SAMPLE_FREQ_BASE * 2,
> > > +	AS73211_SAMPLE_FREQ_BASE * 4,
> > > +	AS73211_SAMPLE_FREQ_BASE * 8,
> > > +};
> > > +
> > > +static const int as73211_hardwaregain_avail[] = {
> > > +	1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048,
> > > +};
> > > +
> > > +/**
> > > + * struct as73211_data - Instance data for one AS73211
> > > + * @client: I2C client.
> > > + * @osr:    Cached Operational State Register.
> > > + * @creg1:  Cached Configuration Register 1.
> > > + * @creg2:  Cached Configuration Register 2.
> > > + * @creg3:  Cached Configuration Register 3.
> > > + * @mutex:  Keeps cached registers in sync with the device.
> > > + * @completion: Completion to wait for interrupt.
> > > + * @int_time_avail: Available integration times (depend on sampling frequency).
> > > + */
> > > +struct as73211_data {
> > > +	struct i2c_client *client;
> > > +	u8 osr;
> > > +	u8 creg1;
> > > +	u8 creg2;
> > > +	u8 creg3;
> > > +	struct mutex mutex;
> > > +	struct completion completion;
> > > +	int int_time_avail[AS73211_SAMPLE_TIME_NUM * 2];  
> 
> This is a flat array, as get_avail assumes.
> 
> > > +};
> > > +
> > > +#define AS73211_COLOR_CHANNEL(_color, _si, _addr) { \
> > > +	.type = IIO_INTENSITY, \
> > > +	.modified = 1, \
> > > +	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), \
> > > +	.info_mask_shared_by_type = \
> > > +		BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
> > > +		BIT(IIO_CHAN_INFO_HARDWAREGAIN) | \
> > > +		BIT(IIO_CHAN_INFO_INT_TIME), \
> > > +	.info_mask_shared_by_type_available = \
> > > +		BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
> > > +		BIT(IIO_CHAN_INFO_HARDWAREGAIN) | \
> > > +		BIT(IIO_CHAN_INFO_INT_TIME), \
> > > +	.channel2 = IIO_MOD_##_color, \
> > > +	.address = _addr, \
> > > +	.scan_index = _si, \
> > > +	.scan_type = { \
> > > +		.sign = 'u', \
> > > +		.realbits = 16, \
> > > +		.storagebits = 16, \
> > > +		.endianness = IIO_LE, \
> > > +	}, \
> > > +}
> > > +
> > > +#define AS73211_OFFSET_TEMP_INT    (-66)
> > > +#define AS73211_OFFSET_TEMP_MICRO  900000
> > > +#define AS73211_SCALE_TEMP_INT     0
> > > +#define AS73211_SCALE_TEMP_MICRO   50000
> > > +
> > > +#define AS73211_SCALE_X 277071108  /* nW/m^2 */
> > > +#define AS73211_SCALE_Y 298384270  /* nW/m^2 */
> > > +#define AS73211_SCALE_Z 160241927  /* nW/m^2 */
> > > +
> > > +/* Channel order MUST match devices result register order */
> > > +#define AS73211_SCAN_INDEX_TEMP 0
> > > +#define AS73211_SCAN_INDEX_X    1
> > > +#define AS73211_SCAN_INDEX_Y    2
> > > +#define AS73211_SCAN_INDEX_Z    3
> > > +#define AS73211_SCAN_INDEX_TS   4
> > > +
> > > +#define AS73211_SCAN_MASK_COLOR ( \
> > > +	BIT(AS73211_SCAN_INDEX_X) |   \
> > > +	BIT(AS73211_SCAN_INDEX_Y) |   \
> > > +	BIT(AS73211_SCAN_INDEX_Z))
> > > +
> > > +#define AS73211_SCAN_MASK_ALL (    \
> > > +	BIT(AS73211_SCAN_INDEX_TEMP) | \
> > > +	AS73211_SCAN_MASK_COLOR)
> > > +
> > > +static const struct iio_chan_spec as73211_channels[] = {
> > > +	{
> > > +		.type = IIO_TEMP,
> > > +		.info_mask_separate =
> > > +			BIT(IIO_CHAN_INFO_RAW) |
> > > +			BIT(IIO_CHAN_INFO_OFFSET) |
> > > +			BIT(IIO_CHAN_INFO_SCALE),
> > > +		.address = AS73211_OUT_TEMP,
> > > +		.scan_index = AS73211_SCAN_INDEX_TEMP,
> > > +		.scan_type = {
> > > +			.sign = 'u',
> > > +			.realbits = 16,
> > > +			.storagebits = 16,
> > > +			.endianness = IIO_LE,
> > > +		}
> > > +	},
> > > +	AS73211_COLOR_CHANNEL(X, AS73211_SCAN_INDEX_X, AS73211_OUT_MRES1),
> > > +	AS73211_COLOR_CHANNEL(Y, AS73211_SCAN_INDEX_Y, AS73211_OUT_MRES2),
> > > +	AS73211_COLOR_CHANNEL(Z, AS73211_SCAN_INDEX_Z, AS73211_OUT_MRES3),
> > > +	IIO_CHAN_SOFT_TIMESTAMP(AS73211_SCAN_INDEX_TS),
> > > +};
> > > +
> > > +static unsigned int as73211_integration_time_1024cyc(struct as73211_data *data)
> > > +{
> > > +	/*
> > > +	 * Return integration time in units of 1024 clock cycles. Integration time
> > > +	 * in CREG1 is in powers of 2 (x 1024 cycles).
> > > +	 */
> > > +	return BIT(FIELD_GET(AS73211_CREG1_TIME_MASK, data->creg1));
> > > +}
> > > +
> > > +static unsigned int as73211_integration_time_us(struct as73211_data *data,
> > > +						 unsigned int integration_time_1024cyc)
> > > +{
> > > +	/*
> > > +	 * f_samp is configured in CREG3 in powers of 2 (x 1.024 MHz)
> > > +	 * t_cycl is configured in CREG1 in powers of 2 (x 1024 cycles)
> > > +	 * t_int_us = 1 / (f_samp) * t_cycl * US_PER_SEC
> > > +	 *          = 1 / (2^CREG3_CCLK * 1,024,000) * 2^CREG1_CYCLES * 1,024 * US_PER_SEC
> > > +	 *          = 2^(-CREG3_CCLK) * 2^CREG1_CYCLES * 1,000
> > > +	 * In order to get rid of negative exponents, we extend the "fraction"
> > > +	 * by 2^3 (CREG3_CCLK,max = 3)
> > > +	 * t_int_us = 2^(3-CREG3_CCLK) * 2^CREG1_CYCLES * 125
> > > +	 */
> > > +	return BIT(3 - FIELD_GET(AS73211_CREG3_CCLK_MASK, data->creg3)) *
> > > +		integration_time_1024cyc * 125;
> > > +}
> > > +
> > > +static void as73211_integration_time_calc_avail(struct as73211_data *data)
> > > +{
> > > +	int i;
> > > +
> > > +	for (i = 0; i < ARRAY_SIZE(data->int_time_avail); i++) {  
> This loops goes to double the size of the array.
> 
> I'd propose to just do ARRAY_SIZE(data->int_time_avail) / 2 here
> 
> > > +		unsigned int time_us = as73211_integration_time_us(data, BIT(i));
> > > +
> > > +		data->int_time_avail[i * 2 + 0] = time_us / USEC_PER_SEC;
> > > +		data->int_time_avail[i * 2 + 1] = time_us % USEC_PER_SEC;
> > > +	}
> > > +}
> > > +
> > > +static unsigned int as73211_gain(struct as73211_data *data)
> > > +{
> > > +	/* gain can be calculated from CREG1 as 2^(13 - CREG1_GAIN) */
> > > +	return BIT(AS73211_CREG1_GAIN_1 - FIELD_GET(AS73211_CREG1_GAIN_MASK, data->creg1));
> > > +}
> > > +
> > > +/* must be called with as73211_data::mutex held. */
> > > +static int as73211_req_data(struct as73211_data *data)
> > > +{
> > > +	unsigned int time_us = as73211_integration_time_us(data,
> > > +							    as73211_integration_time_1024cyc(data));
> > > +	struct device *dev = &data->client->dev;
> > > +	union i2c_smbus_data smbus_data;
> > > +	u16 osr_status;
> > > +	int ret;
> > > +
> > > +	if (data->client->irq)
> > > +		reinit_completion(&data->completion);
> > > +
> > > +	/*
> > > +	 * During measurement, there should be no traffic on the i2c bus as the
> > > +	 * electrical noise would disturb the measurement process.
> > > +	 */
> > > +	i2c_lock_bus(data->client->adapter, I2C_LOCK_SEGMENT);
> > > +
> > > +	data->osr &= ~AS73211_OSR_DOS_MASK;
> > > +	data->osr |= AS73211_OSR_DOS_MEASURE | AS73211_OSR_SS;
> > > +
> > > +	smbus_data.byte = data->osr;
> > > +	ret = __i2c_smbus_xfer(data->client->adapter, data->client->addr,
> > > +			data->client->flags, I2C_SMBUS_WRITE,
> > > +			AS73211_REG_OSR, I2C_SMBUS_BYTE_DATA, &smbus_data);
> > > +	if (ret < 0) {
> > > +		i2c_unlock_bus(data->client->adapter, I2C_LOCK_SEGMENT);
> > > +		return ret;
> > > +	}
> > > +
> > > +	/*
> > > +	 * Reset AS73211_OSR_SS (is self clearing) in order to avoid unintentional
> > > +	 * triggering of further measurements later.
> > > +	 */
> > > +	data->osr &= ~AS73211_OSR_SS;
> > > +
> > > +	/*
> > > +	 * Add some extra margin for the timeout. sensor timing is not as precise
> > > +	 * as our one ...
> > > +	 */
> > > +	time_us += time_us / 8;
> > > +	if (data->client->irq) {
> > > +		ret = wait_for_completion_timeout(&data->completion, usecs_to_jiffies(time_us));
> > > +		if (!ret) {
> > > +			dev_err(dev, "timeout waiting for READY IRQ\n");
> > > +			i2c_unlock_bus(data->client->adapter, I2C_LOCK_SEGMENT);
> > > +			return -ETIMEDOUT;
> > > +		}
> > > +	} else {
> > > +		/* Wait integration time */
> > > +		usleep_range(time_us, 2 * time_us);
> > > +	}
> > > +
> > > +	i2c_unlock_bus(data->client->adapter, I2C_LOCK_SEGMENT);
> > > +
> > > +	ret = i2c_smbus_read_word_data(data->client, AS73211_OUT_OSR_STATUS);
> > > +	if (ret < 0)
> > > +		return ret;
> > > +
> > > +	osr_status = ret;
> > > +	if (osr_status != (AS73211_OSR_DOS_MEASURE | AS73211_OSR_STATUS_NDATA)) {
> > > +		if (osr_status & AS73211_OSR_SS) {
> > > +			dev_err(dev, "%s() Measurement has not stopped\n", __func__);
> > > +			return -ETIME;
> > > +		}
> > > +		if (osr_status & AS73211_OSR_STATUS_NOTREADY) {
> > > +			dev_err(dev, "%s() Data is not ready\n", __func__);
> > > +			return -ENODATA;
> > > +		}
> > > +		if (!(osr_status & AS73211_OSR_STATUS_NDATA)) {
> > > +			dev_err(dev, "%s() No new data available\n", __func__);
> > > +			return -ENODATA;
> > > +		}
> > > +		if (osr_status & AS73211_OSR_STATUS_LDATA) {
> > > +			dev_err(dev, "%s() Result buffer overrun\n", __func__);
> > > +			return -ENOBUFS;
> > > +		}
> > > +		if (osr_status & AS73211_OSR_STATUS_ADCOF) {
> > > +			dev_err(dev, "%s() ADC overflow\n", __func__);
> > > +			return -EOVERFLOW;
> > > +		}
> > > +		if (osr_status & AS73211_OSR_STATUS_MRESOF) {
> > > +			dev_err(dev, "%s() Measurement result overflow\n", __func__);
> > > +			return -EOVERFLOW;
> > > +		}
> > > +		if (osr_status & AS73211_OSR_STATUS_OUTCONVOF) {
> > > +			dev_err(dev, "%s() Timer overflow\n", __func__);
> > > +			return -EOVERFLOW;
> > > +		}
> > > +		dev_err(dev, "%s() Unexpected status value\n", __func__);
> > > +		return -EIO;
> > > +	}
> > > +
> > > +	return 0;
> > > +}
> > > +
> > > +static int as73211_read_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan,
> > > +			     int *val, int *val2, long mask)
> > > +{
> > > +	struct as73211_data *data = iio_priv(indio_dev);
> > > +
> > > +	switch (mask) {
> > > +	case IIO_CHAN_INFO_RAW: {
> > > +		int ret;
> > > +
> > > +		ret = iio_device_claim_direct_mode(indio_dev);
> > > +		if (ret < 0)
> > > +			return ret;
> > > +
> > > +		ret = as73211_req_data(data);
> > > +		if (ret < 0) {
> > > +			iio_device_release_direct_mode(indio_dev);
> > > +			return ret;
> > > +		}
> > > +
> > > +		ret = i2c_smbus_read_word_data(data->client, chan->address);
> > > +		iio_device_release_direct_mode(indio_dev);
> > > +		if (ret < 0)
> > > +			return ret;
> > > +
> > > +		*val = ret;
> > > +		return IIO_VAL_INT;
> > > +	}
> > > +	case IIO_CHAN_INFO_OFFSET:
> > > +		*val = AS73211_OFFSET_TEMP_INT;
> > > +		*val2 = AS73211_OFFSET_TEMP_MICRO;
> > > +		return IIO_VAL_INT_PLUS_MICRO;
> > > +
> > > +	case IIO_CHAN_INFO_SCALE:
> > > +		switch (chan->type) {
> > > +		case IIO_TEMP:
> > > +			*val = AS73211_SCALE_TEMP_INT;
> > > +			*val2 = AS73211_SCALE_TEMP_MICRO;
> > > +			return IIO_VAL_INT_PLUS_MICRO;
> > > +
> > > +		case IIO_INTENSITY: {
> > > +			unsigned int scale;
> > > +
> > > +			switch (chan->channel2) {
> > > +			case IIO_MOD_X:
> > > +				scale = AS73211_SCALE_X;
> > > +				break;
> > > +			case IIO_MOD_Y:
> > > +				scale = AS73211_SCALE_Y;
> > > +				break;
> > > +			case IIO_MOD_Z:
> > > +				scale = AS73211_SCALE_Z;
> > > +				break;
> > > +			default:
> > > +				return -EINVAL;
> > > +			}
> > > +			scale /= as73211_gain(data);
> > > +			scale /= as73211_integration_time_1024cyc(data);
> > > +			*val = scale;
> > > +			return IIO_VAL_INT;
> > > +
> > > +		default:
> > > +			return -EINVAL;
> > > +		}}
> > > +
> > > +	case IIO_CHAN_INFO_SAMP_FREQ:
> > > +		/* f_samp is configured in CREG3 in powers of 2 (x 1.024 MHz) */
> > > +		*val = BIT(FIELD_GET(AS73211_CREG3_CCLK_MASK, data->creg3)) *
> > > +			AS73211_SAMPLE_FREQ_BASE;
> > > +		return IIO_VAL_INT;
> > > +
> > > +	case IIO_CHAN_INFO_HARDWAREGAIN:
> > > +		*val = as73211_gain(data);
> > > +		return IIO_VAL_INT;
> > > +
> > > +	case IIO_CHAN_INFO_INT_TIME: {
> > > +		unsigned int time_us;
> > > +
> > > +		mutex_lock(&data->mutex);
> > > +		time_us = as73211_integration_time_us(data, as73211_integration_time_1024cyc(data));
> > > +		mutex_unlock(&data->mutex);
> > > +		*val = time_us / USEC_PER_SEC;
> > > +		*val2 = time_us % USEC_PER_SEC;
> > > +		return IIO_VAL_INT_PLUS_MICRO;
> > > +
> > > +	default:
> > > +		return -EINVAL;
> > > +	}}
> > > +}
> > > +
> > > +static int as73211_read_avail(struct iio_dev *indio_dev, struct iio_chan_spec const *chan,
> > > +			       const int **vals, int *type, int *length, long mask)
> > > +{
> > > +	struct as73211_data *data = iio_priv(indio_dev);
> > > +
> > > +	switch (mask) {
> > > +	case IIO_CHAN_INFO_SAMP_FREQ:
> > > +		*length = ARRAY_SIZE(as73211_samp_freq_avail);
> > > +		*vals = as73211_samp_freq_avail;
> > > +		*type = IIO_VAL_INT;
> > > +		return IIO_AVAIL_LIST;
> > > +
> > > +	case IIO_CHAN_INFO_HARDWAREGAIN:
> > > +		*length = ARRAY_SIZE(as73211_hardwaregain_avail);
> > > +		*vals = as73211_hardwaregain_avail;
> > > +		*type = IIO_VAL_INT;
> > > +		return IIO_AVAIL_LIST;
> > > +
> > > +	case IIO_CHAN_INFO_INT_TIME:
> > > +		*length = ARRAY_SIZE(data->int_time_avail);  
> 
> This length is correct as read_avail expects it to be double
> the number of entries, if using IIO_VAL_INT_PLUS_MICRO.
> 
> > > +		*vals = data->int_time_avail;
> > > +		*type = IIO_VAL_INT_PLUS_MICRO;
> > > +		return IIO_AVAIL_LIST;
> > > +
> > > +	default:
> > > +		return -EINVAL;
> > > +	}
> > > +}
> > > +
> > > +static int _as73211_write_raw(struct iio_dev *indio_dev,
> > > +			       struct iio_chan_spec const *chan __always_unused,
> > > +			       int val, int val2, long mask)
> > > +{
> > > +	struct as73211_data *data = iio_priv(indio_dev);
> > > +	int ret;
> > > +
> > > +	switch (mask) {
> > > +	case IIO_CHAN_INFO_SAMP_FREQ: {
> > > +		int reg_bits, freq_kHz = val / HZ_PER_KHZ;  /* 1024, 2048, ... */
> > > +
> > > +		/* val must be 1024 * 2^x */
> > > +		if (val < 0 || (freq_kHz * HZ_PER_KHZ) != val ||
> > > +				!is_power_of_2(freq_kHz) || val2)
> > > +			return -EINVAL;
> > > +
> > > +		/* f_samp is configured in CREG3 in powers of 2 (x 1.024 MHz (=2^10)) */
> > > +		reg_bits = ilog2(freq_kHz) - 10;
> > > +		if (!FIELD_FIT(AS73211_CREG3_CCLK_MASK, reg_bits))
> > > +			return -EINVAL;
> > > +
> > > +		data->creg3 &= ~AS73211_CREG3_CCLK_MASK;
> > > +		data->creg3 |= FIELD_PREP(AS73211_CREG3_CCLK_MASK, reg_bits);
> > > +		as73211_integration_time_calc_avail(data);
> > > +
> > > +		ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_CREG3, data->creg3);
> > > +		if (ret < 0)
> > > +			return ret;
> > > +
> > > +		return 0;
> > > +	}
> > > +	case IIO_CHAN_INFO_HARDWAREGAIN: {
> > > +		unsigned int reg_bits;
> > > +
> > > +		if (val < 0 || !is_power_of_2(val) || val2)
> > > +			return -EINVAL;
> > > +
> > > +		/* gain can be calculated from CREG1 as 2^(13 - CREG1_GAIN) */
> > > +		reg_bits = AS73211_CREG1_GAIN_1 - ilog2(val);
> > > +		if (!FIELD_FIT(AS73211_CREG1_GAIN_MASK, reg_bits))
> > > +			return -EINVAL;
> > > +
> > > +		data->creg1 &= ~AS73211_CREG1_GAIN_MASK;
> > > +		data->creg1 |= FIELD_PREP(AS73211_CREG1_GAIN_MASK, reg_bits);
> > > +
> > > +		ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_CREG1, data->creg1);
> > > +		if (ret < 0)
> > > +			return ret;
> > > +
> > > +		return 0;
> > > +	}
> > > +	case IIO_CHAN_INFO_INT_TIME: {
> > > +		int val_us = val * USEC_PER_SEC + val2;
> > > +		int time_ms;
> > > +		int reg_bits;
> > > +
> > > +		/* f_samp is configured in CREG3 in powers of 2 (x 1.024 MHz) */
> > > +		int f_samp_1_024mhz = BIT(FIELD_GET(AS73211_CREG3_CCLK_MASK, data->creg3));
> > > +
> > > +		/*
> > > +		 * time_ms = time_us * US_PER_MS * f_samp_1_024mhz / MHZ_PER_HZ
> > > +		 *         = time_us * f_samp_1_024mhz / 1000
> > > +		 */
> > > +		time_ms = (val_us * f_samp_1_024mhz) / 1000;  /* 1 ms, 2 ms, ... (power of two) */
> > > +		if (time_ms < 0 || !is_power_of_2(time_ms) || time_ms > AS73211_SAMPLE_TIME_MAX_MS)
> > > +			return -EINVAL;
> > > +
> > > +		reg_bits = ilog2(time_ms);
> > > +		if (!FIELD_FIT(AS73211_CREG1_TIME_MASK, reg_bits))
> > > +			return -EINVAL;  /* not possible due to previous tests */
> > > +
> > > +		data->creg1 &= ~AS73211_CREG1_TIME_MASK;
> > > +		data->creg1 |= FIELD_PREP(AS73211_CREG1_TIME_MASK, reg_bits);
> > > +
> > > +		ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_CREG1, data->creg1);
> > > +		if (ret < 0)
> > > +			return ret;
> > > +
> > > +		return 0;
> > > +
> > > +	default:
> > > +		return -EINVAL;
> > > +	}}
> > > +}
> > > +
> > > +static int as73211_write_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan,
> > > +			      int val, int val2, long mask)
> > > +{
> > > +	struct as73211_data *data = iio_priv(indio_dev);
> > > +	int ret;
> > > +
> > > +	mutex_lock(&data->mutex);
> > > +
> > > +	ret = iio_device_claim_direct_mode(indio_dev);
> > > +	if (ret < 0)
> > > +		goto error_unlock;
> > > +
> > > +	/* Need to switch to config mode ... */
> > > +	if ((data->osr & AS73211_OSR_DOS_MASK) != AS73211_OSR_DOS_CONFIG) {
> > > +		data->osr &= ~AS73211_OSR_DOS_MASK;
> > > +		data->osr |= AS73211_OSR_DOS_CONFIG;
> > > +
> > > +		ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_OSR, data->osr);
> > > +		if (ret < 0)
> > > +			goto error_release;
> > > +	}
> > > +
> > > +	ret = _as73211_write_raw(indio_dev, chan, val, val2, mask);
> > > +
> > > +error_release:
> > > +	iio_device_release_direct_mode(indio_dev);
> > > +error_unlock:
> > > +	mutex_unlock(&data->mutex);
> > > +	return ret;
> > > +}
> > > +
> > > +static irqreturn_t as73211_ready_handler(int irq __always_unused, void *priv)
> > > +{
> > > +	struct as73211_data *data = iio_priv(priv);
> > > +
> > > +	complete(&data->completion);
> > > +
> > > +	return IRQ_HANDLED;
> > > +}
> > > +
> > > +static irqreturn_t as73211_trigger_handler(int irq __always_unused, void *p)
> > > +{
> > > +	struct iio_poll_func *pf = p;
> > > +	struct iio_dev *indio_dev = pf->indio_dev;
> > > +	struct as73211_data *data = iio_priv(indio_dev);
> > > +	struct {
> > > +		__le16 chan[4];
> > > +		s64 ts __aligned(8);
> > > +	} scan;
> > > +	int data_result, ret;
> > > +
> > > +	mutex_lock(&data->mutex);
> > > +
> > > +	data_result = as73211_req_data(data);
> > > +	if (data_result < 0 && data_result != -EOVERFLOW)
> > > +		goto done;  /* don't push any data for errors other than EOVERFLOW */
> > > +
> > > +	if (*indio_dev->active_scan_mask == AS73211_SCAN_MASK_ALL) {
> > > +		/* Optimization for reading all (color + temperature) channels */
> > > +		u8 addr = as73211_channels[0].address;
> > > +		struct i2c_msg msgs[] = {
> > > +			{
> > > +				.addr = data->client->addr,
> > > +				.flags = 0,
> > > +				.len = 1,
> > > +				.buf = &addr,
> > > +			},
> > > +			{
> > > +				.addr = data->client->addr,
> > > +				.flags = I2C_M_RD,
> > > +				.len = sizeof(scan.chan),
> > > +				.buf = (u8 *)&scan.chan,
> > > +			},
> > > +		};
> > > +
> > > +		ret = i2c_transfer(data->client->adapter, msgs, ARRAY_SIZE(msgs));
> > > +		if (ret < 0)
> > > +			goto done;
> > > +	} else {
> > > +		/* Optimization for reading only color channels */
> > > +
> > > +		/* AS73211 starts reading at address 2 */
> > > +		ret = i2c_master_recv(data->client,
> > > +				(char *)&scan.chan[1], 3 * sizeof(scan.chan[1]));
> > > +		if (ret < 0)
> > > +			goto done;
> > > +	}
> > > +
> > > +	if (data_result) {
> > > +		/*
> > > +		 * Saturate all channels (in case of overflows). Temperature channel
> > > +		 * is not affected by overflows.
> > > +		 */
> > > +		scan.chan[1] = cpu_to_le16(U16_MAX);
> > > +		scan.chan[2] = cpu_to_le16(U16_MAX);
> > > +		scan.chan[3] = cpu_to_le16(U16_MAX);
> > > +	}
> > > +
> > > +	iio_push_to_buffers_with_timestamp(indio_dev, &scan, iio_get_time_ns(indio_dev));
> > > +
> > > +done:
> > > +	mutex_unlock(&data->mutex);
> > > +	iio_trigger_notify_done(indio_dev->trig);
> > > +
> > > +	return IRQ_HANDLED;
> > > +}
> > > +
> > > +static const struct iio_info as73211_info = {
> > > +	.read_raw = as73211_read_raw,
> > > +	.read_avail = as73211_read_avail,
> > > +	.write_raw = as73211_write_raw,
> > > +};
> > > +
> > > +static int as73211_power(struct iio_dev *indio_dev, bool state)
> > > +{
> > > +	struct as73211_data *data = iio_priv(indio_dev);
> > > +	int ret;
> > > +
> > > +	mutex_lock(&data->mutex);
> > > +
> > > +	if (state)
> > > +		data->osr &= ~AS73211_OSR_PD;
> > > +	else
> > > +		data->osr |= AS73211_OSR_PD;
> > > +
> > > +	ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_OSR, data->osr);
> > > +
> > > +	mutex_unlock(&data->mutex);
> > > +
> > > +	if (ret < 0)
> > > +		return ret;
> > > +
> > > +	return 0;
> > > +}
> > > +
> > > +static void as73211_power_disable(void *data)
> > > +{
> > > +	struct iio_dev *indio_dev = data;
> > > +
> > > +	as73211_power(indio_dev, false);
> > > +}
> > > +
> > > +static int as73211_probe(struct i2c_client *client)
> > > +{
> > > +	struct device *dev = &client->dev;
> > > +	struct as73211_data *data;
> > > +	struct iio_dev *indio_dev;
> > > +	int ret;
> > > +
> > > +	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
> > > +	if (!indio_dev)
> > > +		return -ENOMEM;
> > > +
> > > +	data = iio_priv(indio_dev);
> > > +	i2c_set_clientdata(client, indio_dev);
> > > +	data->client = client;
> > > +
> > > +	mutex_init(&data->mutex);
> > > +	init_completion(&data->completion);
> > > +
> > > +	indio_dev->info = &as73211_info;
> > > +	indio_dev->name = AS73211_DRV_NAME;
> > > +	indio_dev->channels = as73211_channels;
> > > +	indio_dev->num_channels = ARRAY_SIZE(as73211_channels);
> > > +	indio_dev->modes = INDIO_DIRECT_MODE;
> > > +
> > > +	ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_OSR);
> > > +	if (ret < 0)
> > > +		return ret;
> > > +	data->osr = ret;
> > > +
> > > +	/* reset device */
> > > +	data->osr |= AS73211_OSR_SW_RES;
> > > +	ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_OSR, data->osr);
> > > +	if (ret < 0)
> > > +		return ret;
> > > +
> > > +	ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_OSR);
> > > +	if (ret < 0)
> > > +		return ret;
> > > +	data->osr = ret;
> > > +
> > > +	/*
> > > +	 * Reading AGEN is only possible after reset (AGEN is not available if
> > > +	 * device is in measurement mode).
> > > +	 */
> > > +	ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_AGEN);
> > > +	if (ret < 0)
> > > +		return ret;
> > > +
> > > +	/* At the time of writing this driver, only DEVID 2 and MUT 1 are known. */
> > > +	if ((ret & AS73211_AGEN_DEVID_MASK) != AS73211_AGEN_DEVID(2) ||
> > > +	    (ret & AS73211_AGEN_MUT_MASK) != AS73211_AGEN_MUT(1))
> > > +		return -ENODEV;
> > > +
> > > +	ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_CREG1);
> > > +	if (ret < 0)
> > > +		return ret;
> > > +	data->creg1 = ret;
> > > +
> > > +	ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_CREG2);
> > > +	if (ret < 0)
> > > +		return ret;
> > > +	data->creg2 = ret;
> > > +
> > > +	ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_CREG3);
> > > +	if (ret < 0)
> > > +		return ret;
> > > +	data->creg3 = ret;
> > > +	as73211_integration_time_calc_avail(data);
> > > +
> > > +	ret = as73211_power(indio_dev, true);
> > > +	if (ret < 0)
> > > +		return ret;
> > > +
> > > +	ret = devm_add_action_or_reset(dev, as73211_power_disable, indio_dev);
> > > +	if (ret)
> > > +		return ret;
> > > +
> > > +	ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL, as73211_trigger_handler, NULL);
> > > +	if (ret)
> > > +		return ret;
> > > +
> > > +	if (client->irq) {
> > > +		ret = devm_request_threaded_irq(&client->dev, client->irq,
> > > +				NULL,
> > > +				as73211_ready_handler,
> > > +				IRQF_ONESHOT,
> > > +				client->name, indio_dev);
> > > +		if (ret)
> > > +			return ret;
> > > +	}
> > > +
> > > +	return devm_iio_device_register(dev, indio_dev);
> > > +}
> > > +
> > > +static int __maybe_unused as73211_suspend(struct device *dev)
> > > +{
> > > +	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
> > > +
> > > +	return as73211_power(indio_dev, false);
> > > +}
> > > +
> > > +static int __maybe_unused as73211_resume(struct device *dev)
> > > +{
> > > +	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
> > > +
> > > +	return as73211_power(indio_dev, true);
> > > +}
> > > +
> > > +static SIMPLE_DEV_PM_OPS(as73211_pm_ops, as73211_suspend, as73211_resume);
> > > +
> > > +static const struct of_device_id as73211_of_match[] = {
> > > +	{ .compatible = "ams,as73211" },
> > > +	{ }
> > > +};
> > > +MODULE_DEVICE_TABLE(of, as73211_of_match);
> > > +
> > > +static const struct i2c_device_id as73211_id[] = {
> > > +	{ "as73211", 0 },
> > > +	{ }
> > > +};
> > > +MODULE_DEVICE_TABLE(i2c, as73211_id);
> > > +
> > > +static struct i2c_driver as73211_driver = {
> > > +	.driver = {
> > > +		.name           = AS73211_DRV_NAME,
> > > +		.of_match_table = as73211_of_match,
> > > +		.pm             = &as73211_pm_ops,
> > > +	},
> > > +	.probe_new  = as73211_probe,
> > > +	.id_table   = as73211_id,
> > > +};
> > > +module_i2c_driver(as73211_driver);
> > > +
> > > +MODULE_AUTHOR("Christian Eggers <ceggers@arri.de>");
> > > +MODULE_DESCRIPTION("AS73211 XYZ True Color Sensor driver");
> > > +MODULE_LICENSE("GPL");    
> >   
> 


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH v6 2/2] iio: light: as73211: New driver
  2020-09-03 18:43       ` Jonathan Cameron
@ 2020-09-09 18:05         ` Christian Eggers
  0 siblings, 0 replies; 20+ messages in thread
From: Christian Eggers @ 2020-09-09 18:05 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: Rob Herring, Andy Shevchenko, Jonathan Cameron, Hartmut Knaack,
	Lars-Peter Clausen, Peter Meerwald-Stadler, linux-iio,
	devicetree, linux-kernel

Hi Jonathan,

sorry for the delay, I was on holiday last week.

On Thursday, 3 September 2020, 20:43:52 CEST, Jonathan Cameron wrote:
> 
> I've done this halving change because I'm fairly sure it's right and want
> to get a clean report from 0-day before I do a pull request at the
> weekend.  Still time to change if anyone shouts quickly. If not
> there is still plenty of time for a fix.

looks correct for me.

Thanks
Christian





^ permalink raw reply	[flat|nested] 20+ messages in thread

end of thread, other threads:[~2020-09-09 18:06 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-05  5:57 [PATCH v6 0/2] iio: light: Support AMS AS73211 digital XYZ sensor Christian Eggers
2020-08-05  5:57 ` [PATCH v6 1/2] dt-bindings: iio: light: add AMS AS73211 support Christian Eggers
2020-08-06 17:44   ` Jonathan Cameron
2020-08-05  5:57 ` [PATCH v6 2/2] iio: light: as73211: New driver Christian Eggers
2020-08-05  7:39   ` Andy Shevchenko
2020-08-06 17:44   ` Jonathan Cameron
2020-08-07 15:46     ` Christian Eggers
2020-08-08 16:14       ` Jonathan Cameron
2020-08-09  7:43         ` [PATCH] iio: documentation: light: Add as73211 sysfs documentation Christian Eggers
2020-08-09 11:30           ` Jonathan Cameron
2020-08-10  5:24             ` [PATCH v2] " Christian Eggers
2020-08-16  9:19               ` Jonathan Cameron
2020-08-10  9:00           ` [PATCH] " Pavel Machek
2020-08-10  9:57             ` Christian Eggers
2020-08-10 12:35               ` Pavel Machek
2020-08-10 13:24                 ` Christian Eggers
2020-08-16  9:18                   ` Jonathan Cameron
2020-09-01 18:46     ` [PATCH v6 2/2] iio: light: as73211: New driver Jonathan Cameron
2020-09-03 18:43       ` Jonathan Cameron
2020-09-09 18:05         ` Christian Eggers

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).