Skip to contentSkip to conversation

iio: add IIO_DISTANCE type

PATCH Superseded Matt Ranostay 2 participants 5 messages Jan 31, 2014 – Feb 1, 2014 1 message not in the archive

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 iio: add IIO_DISTANCE type

---
 drivers/iio/industrialio-core.c | 1 +
 include/linux/iio/types.h       | 1 +
 2 files changed, 2 insertions(+)
drivers/iio/industrialio-core.c +1 -0
index acc911a..ac999ab 100644
@@ -70,6 +70,7 @@ static const char * const iio_chan_type_name_spec[] = {
 	[IIO_CCT] = "cct",
 	[IIO_PRESSURE] = "pressure",
 	[IIO_HUMIDITYRELATIVE] = "humidityrelative",
+	[IIO_DISTANCE] = "distance",
 };
 
 static const char * const iio_modifier_names[] = {
include/linux/iio/types.h +1 -0
index 084d882..675c2d8 100644
@@ -30,6 +30,7 @@ enum iio_chan_type {
 	IIO_CCT,
 	IIO_PRESSURE,
 	IIO_HUMIDITYRELATIVE,
+	IIO_DISTANCE,
 };
 
 enum iio_modifier {
Signature · 1 line
1.8.3.2
#1 lore raw

PATCH 2/2 iio: Add AS3935 lightning sensor support

AS3935 chipset can detect lightning strikes and reports those back as events and the estimated distance to the storm.

---
 .../ABI/testing/sysfs-bus-iio-distance-as3935      |  19 +
 .../devicetree/bindings/iio/distance/as3935.txt    |  25 ++
 drivers/iio/Kconfig                                |   1 +
 drivers/iio/Makefile                               |   1 +
 drivers/iio/distance/Kconfig                       |  19 +
 drivers/iio/distance/Makefile                      |   6 +
 drivers/iio/distance/as3935.c                      | 458 +++++++++++++++++++++
 7 files changed, 529 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-bus-iio-distance-as3935
 create mode 100644 Documentation/devicetree/bindings/iio/distance/as3935.txt
 create mode 100644 drivers/iio/distance/Kconfig
 create mode 100644 drivers/iio/distance/Makefile
 create mode 100644 drivers/iio/distance/as3935.c
Documentation/ABI/testing/sysfs-bus-iio-distance-as3935 new file +19 -0
new file mode 100644
index 0000000..cf9a67c
@@ -0,0 +1,19 @@
+What		/sys/bus/iio/devices/iio:deviceX/in_distance_raw
+Date:		January 2014
+KernelVersion:	3.15
+Contact:	Matt Ranostay <[email protected]>
+Description:
+		Get the current distance in kilometers of storm
+		0    = no storm activity
+		1    = storm overhead
+		1-40 = distance in kilometers
+		63   = out of range
+
+What		/sys/bus/iio/devices/iio:deviceX/gain_boost
+Date:		January 2014
+KernelVersion:	3.15
+Contact:	Matt Ranostay <[email protected]>
+Description:
+		Show or set the gain boost of the amp, from 0-31 range.
+		18 = indoors (default)
+		14 = outdoors
Documentation/devicetree/bindings/iio/distance/as3935.txt new file +25 -0
new file mode 100644
index 0000000..af35827
@@ -0,0 +1,25 @@
+Austrian Microsystems AS3935 Franklin lightning sensor device driver
+
+Required properties:
+	- compatible: must be "ams,as3935"
+	- reg: SPI chip select number for the device
+	- spi-max-frequency: Max SPI frequency to use
+	- spi-cpha: SPI Mode 1
+	- gpios: GPIO input of interrupt line from IRQ pin of AS3935 IC
+
+Optional properties:
+	- ams,tune-cap: Calibration tuning capacitor stepping value 0 - 15.
+	  Range of 0 to 120 pF, 8pF steps. This will require using the calibration
+	  data from the manufacturer.
+
+
+Example:
+
+		as3935@0 {
+			compatible = "ams,as3935";
+			reg = <0>;
+			spi-max-frequency = <100000>;
+			spi-cpha;
+			ams,tune-cap = /bits/ 8 <10>;
+			gpios = <&gpio1 16 10>;
+		};
drivers/iio/Kconfig +1 -0
index 5dd0e12..5164a68 100644
@@ -63,6 +63,7 @@ source "drivers/iio/adc/Kconfig"
 source "drivers/iio/amplifiers/Kconfig"
 source "drivers/iio/common/Kconfig"
 source "drivers/iio/dac/Kconfig"
+source "drivers/iio/distance/Kconfig"
 source "drivers/iio/frequency/Kconfig"
 source "drivers/iio/gyro/Kconfig"
 source "drivers/iio/humidity/Kconfig"
drivers/iio/Makefile +1 -0
index 887d390..1574731 100644
@@ -16,6 +16,7 @@ obj-y += adc/
 obj-y += amplifiers/
 obj-y += common/
 obj-y += dac/
+obj-y += distance/
 obj-y += gyro/
 obj-y += frequency/
 obj-y += humidity/
drivers/iio/distance/Kconfig new file +19 -0
new file mode 100644
index 0000000..753352c
@@ -0,0 +1,19 @@
+#
+# Distance sensors
+#
+
+menu "Lightning sensors"
+
+config AS3935
+	tristate "AS3935 Franklin lightning sensor"
+	select IIO_BUFFER
+	select IIO_TRIGGERED_BUFFER
+	depends on SPI
+	help
+	 If you say yes here you get support for the Austrian Microsystems
+	 AS3935 lightning detection sensor.
+
+	 This driver can also be built as a module. If so, the module
+	 will be called as3935
+
+endmenu
drivers/iio/distance/Makefile new file +6 -0
new file mode 100644
index 0000000..f63b70d
@@ -0,0 +1,6 @@
+#
+# Makefile for IIO distance sensors
+#
+
+# When adding new entries keep the list in alphabetical order
+obj-$(CONFIG_AS3935)		+= as3935.o
drivers/iio/distance/as3935.c new file +458 -0

458 lines not on this page · raw message

Signature · 1 line
1.8.3.2
#2 lore raw

Re: [PATCH 2/2] iio: Add AS3935 lightning sensor support

On Friday, January 31, 2014 at 04:38:23 PM, Matt Ranostay wrote:

[...]

Quoted 22 lines
(quoted diff) +0 -0

b/Documentation/devicetree/bindings/iio/distance/as3935.txt new file mode 100644
index 0000000..af35827

Documentation/devicetree/bindings/iio/distance/as3935.txt new file +12 -0
@@ -0,0 +1,25 @@
+Austrian Microsystems AS3935 Franklin lightning sensor device driver
+
+Required properties:
+	- compatible: must be "ams,as3935"
+	- reg: SPI chip select number for the device
+	- spi-max-frequency: Max SPI frequency to use
+	- spi-cpha: SPI Mode 1
+	- gpios: GPIO input of interrupt line from IRQ pin of AS3935 IC
+
+Optional properties:
+	- ams,tune-cap: Calibration tuning capacitor stepping value 0 - 15.
+	  Range of 0 to 120 pF, 8pF steps. This will require using the

calibration + data from the manufacturer.

Documentation/devicetree/bindings/iio/distance/as3935.txt new file +2 -0
+
+

Documentation/devicetree/bindings/iio/distance/as3935.txt line 25

Quoted 10 lines
Documentation/devicetree/bindings/iio/distance/as3935.txt new file +10 -0
+Example:
+
+		as3935@0 {
+			compatible = "ams,as3935";
+			reg = <0>;
+			spi-max-frequency = <100000>;
+			spi-cpha;
+			ams,tune-cap = /bits/ 8 <10>;
+			gpios = <&gpio1 16 10>;
+		};

You should CC devicetree-discuss with new bindings! Such a grave flub ;-)

[...]

Documentation/devicetree/bindings/iio/distance/as3935.txt

Quoted 11 lines
Documentation/devicetree/bindings/iio/distance/as3935.txt new file +11 -0
+config AS3935
+	tristate "AS3935 Franklin lightning sensor"
+	select IIO_BUFFER
+	select IIO_TRIGGERED_BUFFER
+	depends on SPI
+	help
+	 If you say yes here you get support for the Austrian Microsystems
+	 AS3935 lightning detection sensor.
+
+	 This driver can also be built as a module. If so, the module
+	 will be called as3935

Fullstop's missing at the end of the above sentence .

[...]

Quoted 5 lines
Documentation/devicetree/bindings/iio/distance/as3935.txt new file +5 -0
+#define AS3935_AFE_GAIN		0x00
+#define AS3935_AFE_MASK		0x3F
+#define AS3935_AFE_GAIN_MAX	0x1F
+
+#define AS3935_INT		0x03

Documentation/devicetree/bindings/iio/distance/as3935.txt

Quoted 10 lines
Documentation/devicetree/bindings/iio/distance/as3935.txt new file +10 -0
+#define AS3935_INT_MASK		0x07
+#define AS3935_DATA		0x07
+#define AS3935_DATA_MASK	0x1F
+
+#define AS3935_TUNE_CAP		0x08
+#define AS3935_CALIBRATE	0x3D
+
+#define AS3935_WRITE_DATA	(0x1 << 15)
+#define AS3935_READ_DATA	(0x1 << 14)
+#define AS3935_ADDRESS(x)	(x << 8)

((x) << 8)

[...]

Documentation/devicetree/bindings/iio/distance/as3935.txt

Quoted 8 lines
Documentation/devicetree/bindings/iio/distance/as3935.txt new file +8 -0
+static int as3935_write(struct as3935_state *st,
+				unsigned int reg,
+				unsigned int val)
+{
+	u8 buf[2];
+	int ret;
+
+	mutex_lock(&st->lock);

You don't need to protect the writes to buf[] with a mutex, since the buf[] is on stack here. Each thread will have it's own local copy of that. DTTO for $reg variable.

Actually, I think you don't even need mutex around all of the the SPI sync stuff. The SPI framework already has a mutex in it.

Quoted 9 lines
Documentation/devicetree/bindings/iio/distance/as3935.txt new file +9 -0
+	buf[0] = AS3935_WRITE_DATA | AS3935_ADDRESS(reg) >> 8;
+	buf[1] = val;
+
+	ret = spi_write(st->spi, (u8 *) &buf, 2);
+	mutex_unlock(&st->lock);
+
+	return ret;
+};
+

[...]

Documentation/devicetree/bindings/iio/distance/as3935.txt

Quoted 9 lines
Documentation/devicetree/bindings/iio/distance/as3935.txt new file +9 -0
+static int as3935_read_raw(struct iio_dev *indio_dev,
+			   struct iio_chan_spec const *chan,
+			   int *val,
+			   int *val2,
+			   long m)
+{
+	struct as3935_state *st = iio_priv(indio_dev);
+
+	if (m == IIO_CHAN_INFO_RAW) {

if (m != IIO...)
return -EINVAL;

... rest of the code ...

This will cut down on the depth of indent.

Quoted 10 lines
Documentation/devicetree/bindings/iio/distance/as3935.txt new file +10 -0
+		int ret;
+		*val2 = 0;
+		ret = as3935_read(st, AS3935_DATA, val);
+		if (ret)
+			return ret;
+		return IIO_VAL_INT;
+	}
+
+	return -EINVAL;
+}

[...]

Documentation/devicetree/bindings/iio/distance/as3935.txt

Quoted 11 lines
Documentation/devicetree/bindings/iio/distance/as3935.txt new file +11 -0
+#ifdef CONFIG_PM_SLEEP
+static int as3935_suspend(struct spi_device *spi, pm_message_t msg)
+{
+	struct iio_dev *indio_dev = spi_get_drvdata(spi);
+	struct as3935_state *st = iio_priv(indio_dev);
+	int val, ret;
+
+	ret = as3935_read(st, AS3935_AFE_GAIN, &val);
+	if (ret)
+		return ret;
+	val |= 0x01;

What's this hexadecimal magic constant here?

Quoted 4 lines
Documentation/devicetree/bindings/iio/distance/as3935.txt new file +4 -0
+
+	return as3935_write(st, AS3935_AFE_GAIN, val);
+}
+

Documentation/devicetree/bindings/iio/distance/as3935.txt

Quoted 10 lines
Documentation/devicetree/bindings/iio/distance/as3935.txt new file +10 -0
+static int as3935_resume(struct spi_device *spi)
+{
+	struct iio_dev *indio_dev = spi_get_drvdata(spi);
+	struct as3935_state *st = iio_priv(indio_dev);
+	int val, ret;
+
+	ret = as3935_read(st, AS3935_AFE_GAIN, &val);
+	if (ret)
+		return ret;
+	val &= ~1;

What's this decimal magic constant here?

Quoted 14 lines
Documentation/devicetree/bindings/iio/distance/as3935.txt new file +14 -0
+	return as3935_write(st, AS3935_AFE_GAIN, val);
+}
+#else
+#define as3935_suspend	NULL
+#define as3935_resume	NULL
+#endif
+
+static int as3935_probe(struct spi_device *spi)
+{
+	struct iio_dev *indio_dev;
+	struct iio_trigger *trig;
+	struct as3935_state *st;
+	struct device_node *np = spi->dev.of_node;
+	int gpio;

Documentation/devicetree/bindings/iio/distance/as3935.txt

Quoted 10 lines
Documentation/devicetree/bindings/iio/distance/as3935.txt new file +10 -0
+	int irq;
+	int ret;
+
+	/* Grab the GPIO to use for lightning event interrupt */
+	if (np)
+		gpio = of_get_gpio(spi->dev.of_node, 0);
+	else {
+		dev_err(&spi->dev, "unable to get interrupt gpio\n");
+		return -EINVAL;
+	}

The logic is a bit weird here. Why don't you check this like so:

if (!np) {
... bail ...
}

gpio = ...

Quoted 22 lines
Documentation/devicetree/bindings/iio/distance/as3935.txt new file +22 -0
+	/* GPIO event setup */
+	ret = devm_gpio_request(&spi->dev, gpio, "as3935");
+	if (ret) {
+		dev_err(&spi->dev, "failed to request GPIO %u\n", gpio);
+		return ret;
+	}
+
+	ret = gpio_direction_input(gpio);
+	if (ret) {
+		dev_err(&spi->dev, "failed to set pin direction\n");
+		return -EINVAL;
+	}
+
+	/* IRQ setup */
+	irq = gpio_to_irq(gpio);
+	if (irq < 0) {
+		dev_err(&spi->dev, "failed to map GPIO to IRQ: %d\n", irq);
+		return -EINVAL;
+	}
+
+	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(st));
+	if (!indio_dev)

Documentation/devicetree/bindings/iio/distance/as3935.txt

Quoted 10 lines
Documentation/devicetree/bindings/iio/distance/as3935.txt new file +10 -0
+		return -ENOMEM;
+
+	st = iio_priv(indio_dev);
+	st->spi = spi;
+	st->tune_cap = 0;
+	spi_set_drvdata(spi, indio_dev);
+	mutex_init(&st->lock);
+	INIT_DELAYED_WORK(&st->work, as3935_event_work);
+
+	of_property_read_u8(np, "ams,tune-cap", &st->tune_cap);

This can fail, check the retval please.

Quoted 5 lines
Documentation/devicetree/bindings/iio/distance/as3935.txt new file +5 -0
+	if (st->tune_cap > 15) {
+		dev_err(&spi->dev,
+			"wrong tune_cap setting of %d\n", st->tune_cap);
+		return -EINVAL;
+	}

[...]

Documentation/devicetree/bindings/iio/distance/as3935.txt

Quoted 4 lines
Documentation/devicetree/bindings/iio/distance/as3935.txt new file +4 -0
+
+MODULE_AUTHOR("Matt Ranostay <[email protected]>");
+MODULE_DESCRIPTION("AS3935 lightning sensor");
+MODULE_LICENSE("GPL");

MODULE_ALIAS() is missing , no?

A message that is not in the archive (sent off-list or not yet received): CAKzfze8b6m__TprPoNthRZ=2h9D11MfK8Q-mR9=KWySP4aDnMA@mail.gmail.com

#4 lore raw

Re: [PATCH 2/2] iio: Add AS3935 lightning sensor support

Quoted 4 lines · Matt Ranostay

Duly noted. Will be fixed in the next rev... mutexs are pointless as you say.. only corner case would be if you were changing gain_boost and a event came in at the same time. But that wouldn't harm anything except slightly delay the gain_boost being updated by some x microseconds.

Please stop top-posting ;-)

btw I think you might need a mutex around the entire:

+static void calibrate_as3935(struct as3935_state *st) +{
+ /* mask disturber interrupt bit */
+ as3935_write(st, AS3935_INT, 1 << 5);
+
+ as3935_write(st, AS3935_CALIBRATE, 0x96);
+ as3935_write(st, AS3935_TUNE_CAP, 1 << 5 | st->tune_cap);
+
+ mdelay(2);
+ as3935_write(st, AS3935_TUNE_CAP, st->tune_cap);
+}

and similar functions where you do a bunch of register accesses. This is because you probably want to protect them against concurent execution so the chip won't be confused if a user were to poke something via SYSFS twice.

Best regards,
Marek Vasut

#5 lore raw
In reply to Matt Ranostay

On Friday, January 31, 2014 at 04:38:22 PM, Matt Ranostay wrote:

The commit message is completely missing, it's entirely unclear what the intention of this patch is. Please write a commit message !

Quoted 23 lines
---
 drivers/iio/industrialio-core.c | 1 +
 include/linux/iio/types.h       | 1 +
 2 files changed, 2 insertions(+)
(quoted diff) +0 -0

b/drivers/iio/industrialio-core.c index acc911a..ac999ab 100644

drivers/iio/industrialio-core.c +1 -0
@@ -70,6 +70,7 @@ static const char * const iio_chan_type_name_spec[] = {
 	[IIO_CCT] = "cct",
 	[IIO_PRESSURE] = "pressure",
 	[IIO_HUMIDITYRELATIVE] = "humidityrelative",
+	[IIO_DISTANCE] = "distance",
 };
 
 static const char * const iio_modifier_names[] = {
include/linux/iio/types.h +0 -0
index 084d882..675c2d8 100644

include/linux/iio/types.h enum iio_chan_type { · line 36

Quoted 7 lines
include/linux/iio/types.h +1 -0
@@ -30,6 +30,7 @@ enum iio_chan_type {
 	IIO_CCT,
 	IIO_PRESSURE,
 	IIO_HUMIDITYRELATIVE,
+	IIO_DISTANCE,
 };
 
 enum iio_modifier {

Best regards,
Marek Vasut