Skip to contentSkip to conversation

iio: chemical: Add driver for Sharp GP2Y1010AU0F dust sensor

PATCH In review [email protected] 2 participants 2 messages Apr 25, 2025 – Apr 26, 2025 IIO

lore
Share & tools
lore
#0 lore raw

PATCH 2/2 iio: chemical: Add driver for Sharp GP2Y1010AU0F dust sensor

From: Suraj Sonawane <[email protected]>

Implement support for the Sharp GP2Y1010AU0F optical dust sensor which measures particulate matter concentration using infrared scattering. The sensor requires precise 280us LED pulses with ADC sampling at 200us after LED activation.

The driver provides:
- Raw voltage readings through IIO interface
- Hardware-agnostic operation via GPIO and IIO ADC interfaces
- Power management through regulator framework
- Device Tree binding support

Device operation:
1. Activate IR LED for 280us
2. Wait 40us after LED activation
3. Sample analog output at 200us (peak sensitivity)
4. Convert voltage to dust density via calibration parameters

Tested on BeagleBone Black with:
- P8_12 (GPIO_44) for LED control
- P9_39 (AIN0) for analog output

Datasheet:
https://global.sharp/products/device/lineup/data/pdf/datasheet/gp2y1010au_appl_e.pdf

---
 MAINTAINERS                     |   7 ++
 drivers/iio/chemical/Kconfig    |  12 +++
 drivers/iio/chemical/Makefile   |   1 +
 drivers/iio/chemical/gp2y1010.c | 183 ++++++++++++++++++++++++++++++++
 4 files changed, 203 insertions(+)
 create mode 100644 drivers/iio/chemical/gp2y1010.c
MAINTAINERS +7 -0
index f31aeb6b4..54e0f67e0 100644
@@ -21974,6 +21974,13 @@ F:	drivers/iio/chemical/sps30.c
 F:	drivers/iio/chemical/sps30_i2c.c
 F:	drivers/iio/chemical/sps30_serial.c
 
+SHARP GP2Y1010AU0F DUST SENSOR DRIVER
+M:	Suraj Sonawane <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	Documentation/devicetree/bindings/iio/chemical/sharp,gp2y1010au0f.yaml
+F:	drivers/iio/chemical/gp2y1010.c
+
 SERIAL DEVICE BUS
 M:	Rob Herring <[email protected]>
 L:	[email protected]
drivers/iio/chemical/Kconfig +12 -0
index 330fe0af9..119c6e6d8 100644
@@ -100,6 +100,18 @@ config ENS160_SPI
 	tristate
 	select REGMAP_SPI
 
+config GP2Y1010AU0F
+	tristate "Sharp GP2Y1010AU0F optical dust sensor"
+	depends on IIO
+	select IIO_BUFFER
+	select IIO_TRIGGERED_BUFFER
+	help
+	  Say Y here to build support for Sharp GP2Y1010AU0F optical dust sensor
+	  that measures particulate matter concentration in air.
+
+	  To compile this driver as a module, choose M here: the module will be
+	  called gp2y1010au0f.
+
 config IAQCORE
 	tristate "AMS iAQ-Core VOC sensors"
 	depends on I2C
drivers/iio/chemical/Makefile +1 -0
index 4866db06b..296bbc0d0 100644
@@ -15,6 +15,7 @@ obj-$(CONFIG_ENS160) += ens160_core.o
 obj-$(CONFIG_ENS160_I2C) += ens160_i2c.o
 obj-$(CONFIG_ENS160_SPI) += ens160_spi.o
 obj-$(CONFIG_IAQCORE)		+= ams-iaq-core.o
+obj-$(CONFIG_GP2Y1010AU0F) += gp2y1010.o
 obj-$(CONFIG_PMS7003) += pms7003.o
 obj-$(CONFIG_SCD30_CORE) += scd30_core.o
 obj-$(CONFIG_SCD30_I2C) += scd30_i2c.o
drivers/iio/chemical/gp2y1010.c new file +183 -0

183 lines not on this page · raw message

Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 19 lines · On Sat, 26 Apr 2025 03:42:14 +0530 [email protected] wrote:

From: Suraj Sonawane <[email protected]>

Implement support for the Sharp GP2Y1010AU0F optical dust sensor which measures particulate matter concentration using infrared scattering. The sensor requires precise 280us LED pulses with ADC sampling at 200us after LED activation.

The driver provides:
- Raw voltage readings through IIO interface
- Hardware-agnostic operation via GPIO and IIO ADC interfaces
- Power management through regulator framework
- Device Tree binding support

Device operation:
1. Activate IR LED for 280us
2. Wait 40us after LED activation
3. Sample analog output at 200us (peak sensitivity)
4. Convert voltage to dust density via calibration parameters

Quoted 6 lines

Tested on BeagleBone Black with:
- P8_12 (GPIO_44) for LED control
- P9_39 (AIN0) for analog output

Datasheet:
https://global.sharp/products/device/lineup/data/pdf/datasheet/gp2y1010au_appl_e.pdf

As in patch 1, make this part of the tags block.

Quoted 2 lines

Various comments inline. Interesting new type of sensor, so I think we need a new channel type. Trying to make that general is always the challenge, but currently density looks appropriate to me.

Jonathan

Quoted 31 lines
---
 MAINTAINERS                     |   7 ++
 drivers/iio/chemical/Kconfig    |  12 +++
 drivers/iio/chemical/Makefile   |   1 +
 drivers/iio/chemical/gp2y1010.c | 183 ++++++++++++++++++++++++++++++++
 4 files changed, 203 insertions(+)
 create mode 100644 drivers/iio/chemical/gp2y1010.c
MAINTAINERS +7 -0
index f31aeb6b4..54e0f67e0 100644
@@ -21974,6 +21974,13 @@ F:	drivers/iio/chemical/sps30.c
 F:	drivers/iio/chemical/sps30_i2c.c
 F:	drivers/iio/chemical/sps30_serial.c
 
+SHARP GP2Y1010AU0F DUST SENSOR DRIVER
+M:	Suraj Sonawane <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	Documentation/devicetree/bindings/iio/chemical/sharp,gp2y1010au0f.yaml
+F:	drivers/iio/chemical/gp2y1010.c
+
 SERIAL DEVICE BUS
 M:	Rob Herring <[email protected]>
 L:	[email protected]
drivers/iio/chemical/Kconfig +0 -0
index 330fe0af9..119c6e6d8 100644

drivers/iio/chemical/Kconfig config ENS160_SPI · line 107

Quoted 8 lines
drivers/iio/chemical/Kconfig +5 -0
@@ -100,6 +100,18 @@ config ENS160_SPI
 	tristate
 	select REGMAP_SPI
 
+config GP2Y1010AU0F
+	tristate "Sharp GP2Y1010AU0F optical dust sensor"
+	depends on IIO
+	select IIO_BUFFER
+	select IIO_TRIGGERED_BUFFER

Not seeing any buffered support in here so these seem unnecessary. For a device like this I'd be very surprised if there was any reason to add it!

Quoted 15 lines
drivers/iio/chemical/Kconfig +7 -0
+	help
+	  Say Y here to build support for Sharp GP2Y1010AU0F optical dust sensor
+	  that measures particulate matter concentration in air.
+
+	  To compile this driver as a module, choose M here: the module will be
+	  called gp2y1010au0f.
+
 config IAQCORE
 	tristate "AMS iAQ-Core VOC sensors"
 	depends on I2C
drivers/iio/chemical/Makefile +0 -0
index 4866db06b..296bbc0d0 100644

drivers/iio/chemical/Makefile obj-$(CONFIG_ENS160) += ens160_core.o · line 18

Quoted 4 lines
drivers/iio/chemical/Makefile +1 -0
@@ -15,6 +15,7 @@ obj-$(CONFIG_ENS160) += ens160_core.o
 obj-$(CONFIG_ENS160_I2C) += ens160_i2c.o
 obj-$(CONFIG_ENS160_SPI) += ens160_spi.o
 obj-$(CONFIG_IAQCORE)		+= ams-iaq-core.o
+obj-$(CONFIG_GP2Y1010AU0F) += gp2y1010.o

Alphabetical order by config name.

Quoted 9 lines
drivers/iio/chemical/Makefile +0 -0
 obj-$(CONFIG_PMS7003) += pms7003.o
 obj-$(CONFIG_SCD30_CORE) += scd30_core.o
 obj-$(CONFIG_SCD30_I2C) += scd30_i2c.o
drivers/iio/chemical/gp2y1010.c new file +0 -0
new file mode 100644
index 000000000..19c09e0e3

drivers/iio/chemical/gp2y1010.c line 9

Quoted 9 lines
drivers/iio/chemical/gp2y1010.c new file +9 -0
@@ -0,0 +1,183 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2025 Suraj Sonawane <[email protected]>
+ *
+ * Sharp GP2Y1010AU0F Dust Sensor Driver
+ *
+ * Datasheet:
+ * https://global.sharp/products/device/lineup/data/pdf/datasheet/gp2y1010au_appl_e.pdf
+ *

This blank line doesn't add anything so drop it.

drivers/iio/chemical/gp2y1010.c line 12

Quoted 3 lines
drivers/iio/chemical/gp2y1010.c new file +3 -0
+ */
+
+#include <linux/module.h>

Alphabetical order preferred in IIO.

drivers/iio/chemical/gp2y1010.c line 15

Quoted 3 lines
drivers/iio/chemical/gp2y1010.c new file +3 -0
+#include <linux/init.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>

This only applies if there is custom userspace ABI.

drivers/iio/chemical/gp2y1010.c line 19

Quoted 4 lines
drivers/iio/chemical/gp2y1010.c new file +4 -0
+#include <linux/iio/consumer.h>
+#include <linux/of_device.h>
+#include <linux/mod_devicetable.h>
+#include <linux/of.h>

Why? I'm not (correctly) seeing an of specific alls in here.

drivers/iio/chemical/gp2y1010.c line 32

Quoted 13 lines
drivers/iio/chemical/gp2y1010.c new file +13 -0
+#include <linux/platform_device.h>
+#include <linux/err.h>
+#include <linux/regulator/consumer.h>
+#include <linux/gpio/consumer.h>
+#include <linux/delay.h>
+
+#define GP2Y1010_LED_PULSE_US    280  /* LED on-time (280us) */
+#define GP2Y1010_SAMPLE_DELAY_US  40  /* Wait 40us after LED on */
+#define GP2Y1010_MEASUREMENT_US  200  /* Measure 200us after LED on */
+
+struct gp2y1010_data {
+	struct gpio_desc *led_gpio;
+	struct iio_dev *indio_dev;

As below. We shouldn't need to get to the indio_dev from here. Everything should go the other way (iio_priv(indio_dev) to get to this).

drivers/iio/chemical/gp2y1010.c line 44

Quoted 12 lines
drivers/iio/chemical/gp2y1010.c new file +12 -0
+	struct iio_channel *adc_chan;
+	struct regulator *vdd;
+	int v_clean;  /* Calibration: voltage in clean air (mV) */
+};
+
+static int gp2y1010_power_on(struct gp2y1010_data *data)
+{
+	int ret;
+
+	ret = regulator_enable(data->vdd);
+	if (ret) {
+		dev_err(&data->indio_dev->dev, "Failed to enable vdd regulator\n");

Generally we'd use the pdev->dev for error messages not the IIO one because the resulting name print is normally easier to use.

drivers/iio/chemical/gp2y1010.c line 46

Quoted 2 lines
drivers/iio/chemical/gp2y1010.c new file +2 -0
+		return ret;
+	}

See below. I don't currently see the usefulness for this manual enabling /disabling code.

Quoted 11 lines
drivers/iio/chemical/gp2y1010.c new file +11 -0
+
+	udelay(100); /* Short delay for regulator stability */
+	return 0;
+}
+
+static void gp2y1010_power_off(struct gp2y1010_data *data)
+{
+	regulator_disable(data->vdd);
+}
+
+static int gp2y1010_read_raw(struct iio_dev *indio_dev,

drivers/iio/chemical/gp2y1010.c line 67

Quoted 10 lines
drivers/iio/chemical/gp2y1010.c new file +10 -0
+							 struct iio_chan_spec const *chan,
+							 int *val, int *val2, long mask)
+{
+	struct gp2y1010_data *data = iio_priv(indio_dev);
+	int ret, voltage_mv;
+
+	if (mask != IIO_CHAN_INFO_RAW)
+		return -EINVAL;
+
+    /* Turn on LED */

Kind of obvious so not sure the comment helps.

drivers/iio/chemical/gp2y1010.c line 74

Quoted 7 lines
drivers/iio/chemical/gp2y1010.c new file +7 -0
+	gpiod_set_value(data->led_gpio, 1);
+
+    /* Wait 40us (datasheet: delay after LED on) */
+	udelay(GP2Y1010_SAMPLE_DELAY_US);
+
+    /* Read ADC at 200us (peak sensitivity) */
+	udelay(GP2Y1010_MEASUREMENT_US - GP2Y1010_SAMPLE_DELAY_US);

Why not a single delay of the total time?

drivers/iio/chemical/gp2y1010.c line 75

Quoted 1 line
drivers/iio/chemical/gp2y1010.c new file +1 -0
+	ret = iio_read_channel_processed(data->adc_chan, &voltage_mv);
	gpiod_set_value(data->led_gpio, 0);
	if (ret < 0 ) {

...

as then you can avoid turning the led off in both good and bad paths via separate calls.

drivers/iio/chemical/gp2y1010.c line 81

Quoted 6 lines
drivers/iio/chemical/gp2y1010.c new file +6 -0
+	if (ret < 0) {
+		gpiod_set_value(data->led_gpio, 0);
+		return ret;
+	}
+
+    /* Turn off LED (total pulse width = 280us) */

This comment is useful because of the provided time information so keep this one.

drivers/iio/chemical/gp2y1010.c line 84

Quoted 3 lines
drivers/iio/chemical/gp2y1010.c new file +3 -0
+	gpiod_set_value(data->led_gpio, 0);
+
+    /* Store raw voltage (for debugging) */

Why is it for debugging? This is ultimately what we pass to userspace.

drivers/iio/chemical/gp2y1010.c line 96

Quoted 12 lines
drivers/iio/chemical/gp2y1010.c new file +12 -0
+	*val = voltage_mv;
+
+	return IIO_VAL_INT;
+}
+
+static const struct iio_info gp2y1010_info = {
+	.read_raw = gp2y1010_read_raw,
+};
+
+static const struct iio_chan_spec gp2y1010_channels[] = {
+	{
+		.type = IIO_VOLTAGE,

Whilst you are measuring a voltage the real thing being measured is about dust. There is a graph in the datasheet of voltage vs dust density in mg/m^3 So I think this needs a new channel type. Probably IIO_DENSITY with base units of g / m^3

in this case without per sensor calibration I doubt we can get to a processed value but we can still have a raw channel of IIO_DENSITY type.

Remember to add Docs to Documentation/ABI/testing/sysfs-bus-iio for the new density type.

drivers/iio/chemical/gp2y1010.c line 98

Quoted 2 lines
drivers/iio/chemical/gp2y1010.c new file +2 -0
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
+		.extend_name = "dust",

extend_name is a terrible old design decision that we more or less never use in new drivers because it makes for a hard ABI for userspace code to parse.

Instead provide the get_label() callback if this is useful.

drivers/iio/chemical/gp2y1010.c line 108

Quoted 10 lines
drivers/iio/chemical/gp2y1010.c new file +10 -0
+	},
+};
+
+static int gp2y1010_probe(struct platform_device *pdev)
+{
+	struct gp2y1010_data *data;
+	struct iio_dev *indio_dev;
+	int ret;
+
+	indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*data));

Quite a lot of references to &pdev->dev so I would have a local variable

	struct device *dev = &pdev->dev;

that will shorten a lot of lines a little bit.

drivers/iio/chemical/gp2y1010.c line 113

Quoted 5 lines
drivers/iio/chemical/gp2y1010.c new file +5 -0
+	if (!indio_dev)
+		return -ENOMEM;
+
+	data = iio_priv(indio_dev);
+	data->indio_dev = indio_dev;

This is usually not a great sign. Generally it means that you have passed
data to some callback where you should have passed the iio_dev and used iio_priv() to get to data. We should not be seeing the linkages in both directions.

drivers/iio/chemical/gp2y1010.c line 114

Quoted 1 line
drivers/iio/chemical/gp2y1010.c new file +1 -0
+	data->v_clean = 900;  /* Default calibration (adjust per sensor) */

If you need to adjust this per sensor, how do we do it?

drivers/iio/chemical/gp2y1010.c line 116

Quoted 2 lines
drivers/iio/chemical/gp2y1010.c new file +2 -0
+
+	/* Get LED GPIO */

This code is self explanatory. Comments that don't add a lot of value tend to just pad out code and provide places for future disagreements between docs and code as a driver evolves. So generally we just don't have this sort of comment.

drivers/iio/chemical/gp2y1010.c line 122

Quoted 6 lines
drivers/iio/chemical/gp2y1010.c new file +6 -0
+	data->led_gpio = devm_gpiod_get(&pdev->dev, "led", GPIOD_OUT_LOW);
+	if (IS_ERR(data->led_gpio))
+		return dev_err_probe(&pdev->dev, PTR_ERR(data->led_gpio),
+							"Failed to get LED GPIO\n");
+
+    /* Get regulator */

Likewise. Self explanatory.

drivers/iio/chemical/gp2y1010.c line 123

Quoted 1 line
drivers/iio/chemical/gp2y1010.c new file +1 -0
+	data->vdd = devm_regulator_get(&pdev->dev, "vdd");

No need to keep hold of vdd for now (you may need it later if you do runtime pm or similar) so can use

	ret = devm_regulator_get_enabled(dev, "vdd);
	if (ret)
		return ret;

	udelay(100); /* Short delay for regulator stability */

and drop the need to power it down explicitly.

drivers/iio/chemical/gp2y1010.c line 131

Quoted 8 lines
drivers/iio/chemical/gp2y1010.c new file +8 -0
+	if (IS_ERR(data->vdd))
+		return dev_err_probe(&pdev->dev, PTR_ERR(data->vdd),
+							"Failed to get regulator\n");
+
+    /* Power on sensor */
+	ret = gp2y1010_power_on(data);
+	if (ret)
+		return ret;

As below

	ret = devm_add_action_or_reset(&pdev->dev, new_power_off_callback, data);
	if (ret)
		return ret;

however becomes irrelevant if you make changes suggested above.

Quoted 20 lines
drivers/iio/chemical/gp2y1010.c new file +20 -0
+
+    /* Get ADC channel */
+	data->adc_chan = devm_iio_channel_get(&pdev->dev, "dust");
+	if (IS_ERR(data->adc_chan)) {
+		gp2y1010_power_off(data);
+		return dev_err_probe(&pdev->dev, PTR_ERR(data->adc_chan),
+							"Failed to get ADC channel\n");
+	}
+
+	/* Setup IIO device */
+	indio_dev->name = "gp2y1010";
+	indio_dev->info = &gp2y1010_info;
+	indio_dev->channels = gp2y1010_channels;
+	indio_dev->num_channels = ARRAY_SIZE(gp2y1010_channels);
+	indio_dev->modes = INDIO_DIRECT_MODE;
+
+	ret = devm_iio_device_register(&pdev->dev, indio_dev);
+	if (ret) {
+		gp2y1010_power_off(data);
+		return ret;

drivers/iio/chemical/gp2y1010.c line 161

Quoted 10 lines
drivers/iio/chemical/gp2y1010.c new file +10 -0
+	}
+
+	return 0;
+}
+
+static void gp2y1010_remove(struct platform_device *pdev)
+{
+	struct gp2y1010_data *data = platform_get_drvdata(pdev);
+
+	gp2y1010_power_off(data);

Use a devm_add_action_or_reset() with a custom callback to power this down.

As things currently stand, you are mixing devm cleanup and manual cleanup which means that we remove the userspace interfaces after turning the power off. That's generally a path to weird bug reports!

drivers/iio/chemical/gp2y1010.c line 166

Quoted 5 lines
drivers/iio/chemical/gp2y1010.c new file +5 -0
+}
+
+static const struct of_device_id gp2y1010_of_match[] = {
+	{ .compatible = "sharp,gp2y1010au0f", },
+	{}

We have standardized in IIO on a space between brackets on terminators like this

	{ }

No particular reason for that choice beyond wanting to pick one or the other style!

Quoted 17 lines
drivers/iio/chemical/gp2y1010.c new file +17 -0
+};
+MODULE_DEVICE_TABLE(of, gp2y1010_of_match);
+
+static struct platform_driver gp2y1010_driver = {
+	.driver = {
+		.name = "gp2y1010",
+		.of_match_table = gp2y1010_of_match,
+	},
+	.probe = gp2y1010_probe,
+	.remove = gp2y1010_remove,
+};
+
+module_platform_driver(gp2y1010_driver);
+
+MODULE_AUTHOR("Suraj Sonawane <[email protected]>");
+MODULE_DESCRIPTION("Sharp GP2Y1010AU0F Dust Sensor Driver");
+MODULE_LICENSE("GPL");