Skip to contentSkip to conversation

Add Semtech SX9360 SAR Sensor support

PATCH In review Gwendal Grignou 3 participants 6 messages Jan 1, 2022 – Jan 16, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v6 cover Add Semtech SX9360 SAR Sensor support

Add a new Semtech SAR sensor SX9360.
It is a simpler version of SX9324, with only one phase instead of 4. Leverage sx_common.h interface defined when SX9324 was added.

Major changes since v4:
- Use sysfs_emit instead of printf for real.
- Use SEMTECH_PROX export namespace.

Major changes since v4:
- Use simple pm macro
- Unify use of returning on default
- Add space before '}'
- Use sysfs_emit instead of printf.

Major changes since v3:
- Remove "reference" as a new modifier: it is not needed, indexed channels and labels are used instead.
- Use dev_get_drvdata to access iio device structure.

Major changes since v2:
- Register "modifier" as a new modifier.
- Fix device tree binding syntax
- Fix issues reported during sx9324 driver review:
- fix include with iwyu

Major changes since v1:
- Integrate errors found in sx9324 driver.
- Simplify whomai function.
- Fix cut and paste in bindings.
- Handle negative sysfs parameters.

Gwendal Grignou (3):
iio: proximity: Add sx9360 support
dt-bindings: iio: Add sx9360 binding
iio: sx9360: Add dt-binding support

 .../iio/proximity/semtech,sx9360.yaml         |  89 ++
 drivers/iio/proximity/Kconfig                 |  14 +
 drivers/iio/proximity/Makefile                |   1 +
 drivers/iio/proximity/sx9360.c                | 893 ++++++++++++++++++
 4 files changed, 997 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/proximity/semtech,sx9360.yaml
 create mode 100644 drivers/iio/proximity/sx9360.c
Signature · 1 line
2.34.1.448.ga2b2bfdf31-goog
#1 lore raw

PATCH v6 1/3 iio: proximity: Add sx9360 support

A simplified version of SX9324, it only have one pin and 2 phases (aka channels).
Only one event is presented.

---
Changes since v5:
- Use export namespace SEMTECH_PROX
- Use sysfs_emit instead of printf for real.

Changes since v4:
- Use simple pm macro
- Unify use of returning on default
- Add space before '}'
- Use sysfs_emit instead of printf.

Changes since v3:
- Remove "reference" as a new modifier: it is not needed, indexed channels and labels are used instead.
- Use dev_get_drvdata to access iio device structure.
- Use already calculated local variable
- Use default: clause to return when possible.

Changes since v2:
- Fix issues reported during sx9324 driver review:
- fix include with iwyu
- Remove call to ACPI_PTR to prevent unused variable warning.
- Fix panic when setting frequency to 0.
- Add offset to decipher interrupt register
- Fix default register value.

Changes since v1:
- Remove SX9360_DRIVER_NAME
- Simplify whoami function
- Define WHOAMI register value internally.
- Handle negative values when setting sysfs parameters.

 drivers/iio/proximity/Kconfig  |  14 +
 drivers/iio/proximity/Makefile |   1 +
 drivers/iio/proximity/sx9360.c | 809 +++++++++++++++++++++++++++++++++
 3 files changed, 824 insertions(+)
 create mode 100644 drivers/iio/proximity/sx9360.c
drivers/iio/proximity/Kconfig +14 -0
index dc66f339e5b12a..0e5c17530b8bb4 100644
@@ -145,6 +145,20 @@ config SX9324
 	  To compile this driver as a module, choose M here: the
 	  module will be called sx9324.
 
+config SX9360
+	tristate "SX9360 Semtech proximity sensor"
+	select IIO_BUFFER
+	select IIO_TRIGGERED_BUFFER
+	select REGMAP_I2C
+	select SX_COMMON
+	depends on I2C
+	help
+	  Say Y here to build a driver for Semtech's SX9360
+	  proximity/button sensor, a simplified SX9324.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called sx9360.
+
 config SX9500
 	tristate "SX9500 Semtech proximity sensor"
 	select IIO_BUFFER
drivers/iio/proximity/Makefile +1 -0
index cffe962b352718..cc838bb5408a89 100644
@@ -15,6 +15,7 @@ obj-$(CONFIG_SRF04)		+= srf04.o
 obj-$(CONFIG_SRF08)		+= srf08.o
 obj-$(CONFIG_SX9310)		+= sx9310.o
 obj-$(CONFIG_SX9324)		+= sx9324.o
+obj-$(CONFIG_SX9360)		+= sx9360.o
 obj-$(CONFIG_SX_COMMON) 	+= sx_common.o
 obj-$(CONFIG_SX9500)		+= sx9500.o
 obj-$(CONFIG_VCNL3020)		+= vcnl3020.o
drivers/iio/proximity/sx9360.c new file +809 -0

809 lines not on this page · raw message

Signature · 1 line
2.34.1.448.ga2b2bfdf31-goog
#2 lore raw

Re: [PATCH v6 1/3] iio: proximity: Add sx9360 support

Quoted 7 lines · On Sat, 1 Jan 2022 12:37:58 -0800 Gwendal Grignou <[email protected]> wrote:

A simplified version of SX9324, it only have one pin and 2 phases (aka channels).
Only one event is presented.

---

Series applied but I got a build warning which lead me to take another look at he below code.

...

drivers/iio/proximity/sx9360.c line 390

Quoted 8 lines
drivers/iio/proximity/sx9360.c new file +8 -0
+static int sx9360_set_samp_freq(struct sx_common_data *data,
+				int val, int val2)
+{
+	int ret, reg;
+	__be16 buf;
+
+	reg = val * 8192 / SX9360_FOSC_HZ + val2 * 8192 / (SX9360_FOSC_MHZ);
+	buf = cpu_to_be16(val);

I've changed this to cpu_to_be16(reg) which I'm assuming was the intent.

Quoted 9 lines
drivers/iio/proximity/sx9360.c new file +9 -0
+	mutex_lock(&data->mutex);
+
+	ret = regmap_bulk_write(data->regmap, SX9360_REG_GNRL_CTRL1, &buf,
+				sizeof(buf));
+
+	mutex_unlock(&data->mutex);
+
+	return ret;
+}
maintainer
#3 lore raw

Re: [PATCH v6 1/3] iio: proximity: Add sx9360 support

Quoted 7 lines · On Sun, 9 Jan 2022 11:00:00 +0000 Jonathan Cameron…
Quoted 4 lines · On Sat, 1 Jan 2022 12:37:58 -0800 Gwendal Grignou <[email protected]> wrote:

A simplified version of SX9324, it only have one pin and 2 phases (aka channels).
Only one event is presented.

Quoted 5 lines
Quoted 3 lines

---

Series applied but I got a build warning which lead me to take another look at he below code.

Follow up tweak like the 9324 patch this one had some {A, B } spacing so I've changed that to { A, B }

The wonders of editing history ;)

Thanks,

Jonathan

Quoted 22 lines

...

drivers/iio/proximity/sx9360.c line 390

Quoted 8 lines
drivers/iio/proximity/sx9360.c new file +8 -0
+static int sx9360_set_samp_freq(struct sx_common_data *data,
+				int val, int val2)
+{
+	int ret, reg;
+	__be16 buf;
+
+	reg = val * 8192 / SX9360_FOSC_HZ + val2 * 8192 / (SX9360_FOSC_MHZ);
+	buf = cpu_to_be16(val);  

I've changed this to cpu_to_be16(reg) which I'm assuming was the intent.

Quoted 9 lines
drivers/iio/proximity/sx9360.c new file +9 -0
+	mutex_lock(&data->mutex);
+
+	ret = regmap_bulk_write(data->regmap, SX9360_REG_GNRL_CTRL1, &buf,
+				sizeof(buf));
+
+	mutex_unlock(&data->mutex);
+
+	return ret;
+}  
#4 lore raw

PATCH v6 2/3 dt-bindings: iio: Add sx9360 binding

In reply to Gwendal Grignou

Add binding to configure Semtech sx9360 sensor.
It is a simpler version of sx9324.

---
No Changes since v5.
No Changes since v4.
No Changes since v3.
Changes since v2:
- Use const instead of single enum.
- Use proper syntax for maximum/minimum
- Fix spelling errors.

Changes since v1:
- Fix cut and paste error.
- Add . at end of sentence.

 .../iio/proximity/semtech,sx9360.yaml         | 89 +++++++++++++++++++
 1 file changed, 89 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/proximity/semtech,sx9360.yaml
Documentation/devicetree/bindings/iio/proximity/semtech,sx9360.yaml new file +89 -0
new file mode 100644
index 00000000000000..63e1a1fd00d4ca
@@ -0,0 +1,89 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/proximity/semtech,sx9360.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Semtech's SX9360 capacitive proximity sensor
+
+maintainers:
+  - Gwendal Grignou <[email protected]>
+  - Daniel Campello <[email protected]>
+
+description: |
+  Semtech's SX9360 proximity sensor.
+
+properties:
+  compatible:
+    const: semtech,sx9360
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    description:
+      Generated by device to announce preceding read request has finished
+      and data is available or that a close/far proximity event has happened.
+    maxItems: 1
+
+  vdd-supply:
+    description: Main power supply
+
+  svdd-supply:
+    description: Host interface power supply
+
+  "#io-channel-cells":
+    const: 1
+
+  semtech,resolution:
+    $ref: /schemas/types.yaml#/definitions/uint32-array
+    enum: [8, 16, 32, 64, 128, 256, 512, 1024]
+    description:
+      Capacitance measurement resolution. For both phases, "reference" and
+      "measurement". Higher the number, higher the resolution.
+    default: 128
+
+  semtech,proxraw-strength:
+    $ref: /schemas/types.yaml#/definitions/uint32
+    minimum: 0
+    maximum: 7
+    default: 1
+    description:
+      PROXRAW filter strength for both phases. A value of 0 represents off,
+      and other values represent 1-1/2^N.
+
+  semtech,avg-pos-strength:
+    $ref: /schemas/types.yaml#/definitions/uint32
+    enum: [0, 16, 64, 128, 256, 512, 1024, 4294967295]
+    default: 16
+    description: |
+      Average positive filter strength. A value of 0 represents off and
+      UINT_MAX (4294967295) represents infinite. Other values
+      represent 1-1/N.
+
+required:
+  - compatible
+  - reg
+  - "#io-channel-cells"
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/irq.h>
+    i2c {
+      #address-cells = <1>;
+      #size-cells = <0>;
+      proximity@28 {
+        compatible = "semtech,sx9360";
+        reg = <0x28>;
+        interrupt-parent = <&pio>;
+        interrupts = <5 IRQ_TYPE_LEVEL_LOW 5>;
+        vdd-supply = <&pp3300_a>;
+        svdd-supply = <&pp1800_prox>;
+        #io-channel-cells = <1>;
+        semtech,resolution = <256>;
+        semtech,proxraw-strength = <2>;
+        semtech,avg-pos-strength = <64>;
+      };
+    };
Signature · 1 line
2.34.1.448.ga2b2bfdf31-goog
#5 lore raw

PATCH v6 3/3 iio: sx9360: Add dt-binding support

In reply to Gwendal Grignou

Add support to configure sx9360 from dt-binding, to match device hardware setup.

---
No changes in v6.
No changes in v5.
Changes since v3:
- Concatenate 2 lines.

Changes since v2:
- Add include when needed.
- Move default register constant to main patch.

No changes in v2.

 drivers/iio/proximity/sx9360.c | 84 ++++++++++++++++++++++++++++++++++
 1 file changed, 84 insertions(+)
drivers/iio/proximity/sx9360.c +84 -0
index f07d6bba07735d..ec0c4ff51bb874 100644
@@ -18,6 +18,7 @@
 #include <linux/mod_devicetable.h>
 #include <linux/module.h>
 #include <linux/pm.h>
+#include <linux/property.h>
 #include <linux/regmap.h>
 
 #include <linux/iio/iio.h>
@@ -64,6 +65,7 @@
 #define SX9360_REG_PROX_CTRL0_PHM	0x41
 #define SX9360_REG_PROX_CTRL0_GAIN_MASK	GENMASK(5, 3)
 #define SX9360_REG_PROX_CTRL0_GAIN_1		0x80
+#define SX9360_REG_PROX_CTRL0_RAWFILT_MASK	GENMASK(2, 0)
 #define SX9360_REG_PROX_CTRL0_RAWFILT_1P50	0x01
 #define SX9360_REG_PROX_CTRL1		0x42
 #define SX9360_REG_PROX_CTRL1_AVGNEG_THRESH_MASK	GENMASK(5, 3)
@@ -657,6 +659,41 @@ static int sx9360_write_raw(struct iio_dev *indio_dev,
 	}
 }
 
+static const struct sx_common_reg_default sx9360_default_regs[] = {
+	{ SX9360_REG_IRQ_MSK, 0x00 },
+	{ SX9360_REG_IRQ_CFG, 0x00 },
+	/*
+	 * The lower 2 bits should not be set as it enable sensors measurements.
+	 * Turning the detection on before the configuration values are set to
+	 * good values can cause the device to return erroneous readings.
+	 */
+	{ SX9360_REG_GNRL_CTRL0, 0x00 },
+	{ SX9360_REG_GNRL_CTRL1, 0x00 },
+	{ SX9360_REG_GNRL_CTRL2, SX9360_REG_GNRL_CTRL2_PERIOD_102MS },
+
+	{ SX9360_REG_AFE_CTRL1, 0x00 },
+	{ SX9360_REG_AFE_PARAM0_PHR, SX9360_REG_AFE_PARAM0_RSVD |
+		SX9360_REG_AFE_PARAM0_RESOLUTION_128 },
+	{ SX9360_REG_AFE_PARAM1_PHR, SX9360_REG_AFE_PARAM1_AGAIN_PHM_6PF |
+		SX9360_REG_AFE_PARAM1_FREQ_83_33HZ },
+	{ SX9360_REG_AFE_PARAM0_PHM, SX9360_REG_AFE_PARAM0_RSVD |
+		SX9360_REG_AFE_PARAM0_RESOLUTION_128 },
+	{ SX9360_REG_AFE_PARAM1_PHM, SX9360_REG_AFE_PARAM1_AGAIN_PHM_6PF |
+		SX9360_REG_AFE_PARAM1_FREQ_83_33HZ },
+
+	{ SX9360_REG_PROX_CTRL0_PHR, SX9360_REG_PROX_CTRL0_GAIN_1 |
+		SX9360_REG_PROX_CTRL0_RAWFILT_1P50 },
+	{ SX9360_REG_PROX_CTRL0_PHM, SX9360_REG_PROX_CTRL0_GAIN_1 |
+		SX9360_REG_PROX_CTRL0_RAWFILT_1P50 },
+	{ SX9360_REG_PROX_CTRL1, SX9360_REG_PROX_CTRL1_AVGNEG_THRESH_16K },
+	{ SX9360_REG_PROX_CTRL2, SX9360_REG_PROX_CTRL2_AVGDEB_2SAMPLES |
+		SX9360_REG_PROX_CTRL2_AVGPOS_THRESH_16K },
+	{ SX9360_REG_PROX_CTRL3, SX9360_REG_PROX_CTRL3_AVGNEG_FILT_2 |
+		SX9360_REG_PROX_CTRL3_AVGPOS_FILT_256 },
+	{ SX9360_REG_PROX_CTRL4, 0x00 },
+	{ SX9360_REG_PROX_CTRL5, SX9360_REG_PROX_CTRL5_PROXTHRESH_32 },
+};
+
 /* Activate all channels and perform an initial compensation. */
 static int sx9360_init_compensation(struct iio_dev *indio_dev)
 {
@@ -676,6 +713,51 @@ static int sx9360_init_compensation(struct iio_dev *indio_dev)
 				       20000, 2000000);
 }
 
+static const struct sx_common_reg_default *
+sx9360_get_default_reg(struct device *dev, int idx,
+		       struct sx_common_reg_default *reg_def)
+{
+	u32 raw = 0, pos = 0;
+	int ret;
+
+	memcpy(reg_def, &sx9360_default_regs[idx], sizeof(*reg_def));
+	switch (reg_def->reg) {
+	case SX9360_REG_AFE_PARAM0_PHR:
+	case SX9360_REG_AFE_PARAM0_PHM:
+		ret = device_property_read_u32(dev, "semtech,resolution", &raw);
+		if (ret)
+			break;
+
+		raw = ilog2(raw) - 3;
+
+		reg_def->def &= ~SX9360_REG_AFE_PARAM0_RESOLUTION_MASK;
+		reg_def->def |= FIELD_PREP(SX9360_REG_AFE_PARAM0_RESOLUTION_MASK, raw);
+		break;
+	case SX9360_REG_PROX_CTRL0_PHR:
+	case SX9360_REG_PROX_CTRL0_PHM:
+		ret = device_property_read_u32(dev, "semtech,proxraw-strength", &raw);
+		if (ret)
+			break;
+
+		reg_def->def &= ~SX9360_REG_PROX_CTRL0_RAWFILT_MASK;
+		reg_def->def |= FIELD_PREP(SX9360_REG_PROX_CTRL0_RAWFILT_MASK, raw);
+		break;
+	case SX9360_REG_PROX_CTRL3:
+		ret = device_property_read_u32(dev, "semtech,avg-pos-strength",
+					       &pos);
+		if (ret)
+			break;
+
+		/* Powers of 2, except for a gap between 16 and 64 */
+		raw = clamp(ilog2(pos), 3, 11) - (pos >= 32 ? 4 : 3);
+		reg_def->def &= ~SX9360_REG_PROX_CTRL3_AVGPOS_FILT_MASK;
+		reg_def->def |= FIELD_PREP(SX9360_REG_PROX_CTRL3_AVGPOS_FILT_MASK, raw);
+		break;
+	}
+
+	return reg_def;
+}
+
 static int sx9360_check_whoami(struct device *dev, struct iio_dev *indio_dev)
 {
 	/*
@@ -695,12 +777,14 @@ static const struct sx_common_chip_info sx9360_chip_info = {
 	.mask_enable_chan = SX9360_REG_GNRL_CTRL0_PHEN_MASK,
 	.stat_offset = 3,
 	.num_channels = SX9360_NUM_CHANNELS,
+	.num_default_regs = ARRAY_SIZE(sx9360_default_regs),
 
 	.ops = {
 		.read_prox_data = sx9360_read_prox_data,
 		.check_whoami = sx9360_check_whoami,
 		.init_compensation = sx9360_init_compensation,
 		.wait_for_sample = sx9360_wait_for_sample,
+		.get_default_reg = sx9360_get_default_reg,
 	},
 
 	.iio_channels = sx9360_channels,
Signature · 1 line
2.34.1.448.ga2b2bfdf31-goog