* [PATCH v2 0/3] iio: chemical: Add Sharp GP2Y1010AU0F dust sensor support
@ 2025-06-12 10:07 surajsonawane0215
2025-06-12 10:07 ` [PATCH v2 1/3] iio: Add IIO_DENSITY channel type surajsonawane0215
` (2 more replies)
0 siblings, 3 replies; 11+ messages in thread
From: surajsonawane0215 @ 2025-06-12 10:07 UTC (permalink / raw)
To: Jonathan Cameron
Cc: David Lechner, Nuno Sá, Andy Shevchenko, Rob Herring,
Krzysztof Kozlowski, Conor Dooley, linux-iio, devicetree,
linux-kernel, surajsonawane0215
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain; charset=yes, Size: 2570 bytes --]
From: Suraj Sonawane <surajsonawane0215@gmail.com>
This patch series adds support for the Sharp GP2Y1010AU0F optical
dust sensor, which measures airborne particulate matter concentration
in grams per cubic meter (g/m³). The implementation includes:
1. A new IIO_DENSITY channel type for particulate sensors (Patch 1)
2. Device tree bindings documentation (Patch 2)
3. The driver implementation (Patch 3)
The sensor requires:
- One GPIO for device enable control
- One ADC channel for analog output measurement
- Optional LED trigger for synchronized sampling
Tested on BeagleBone Black with:
- P8_12 (GPIO_44) for LED control
- P9_39 (AIN0) for analog output
Changes since v1:
- Added new IIO_DENSITY channel type (Patch 1)
- Documented ABI for density measurements (g/m³) in sysfs-bus-iio
- Updated timing properties to match datasheet:
* sharp,led-on-delay-us: 280μs (from 40μs)
* sharp,measurement-window-us: 280μs (from 200μs)
- Added reg property for multi-sensor support
- Moved datasheet reference to tags block
- Hardware Interface:
* Replaced manual power management with devm_regulator_get_enabled()
* Consolidated LED timing into single delay operation
* Improved error handling in read sequence
- Removed unnecessary IIO_BUFFER dependencies
- Alphabetized Makefile entry and header includes
- Replaced extend_name with proper label callback
- Standardized on pdev->dev for all device logging
- Applied IIO formatting conventions ({ } spacing)
- Changed IIO_VOLTAGE → IIO_DENSITY channel type
- Updated all timing values to datasheet specifications
V1: [PATCH 1/2] https://lore.kernel.org/lkml/20250425215149.49068-1-surajsonawane0215@gmail.com/
[PATCH 2/2] https://lore.kernel.org/lkml/20250425221214.50255-1-surajsonawane0215@gmail.com/
Thanks!
Suraj Sonawane
Suraj Sonawane (3):
iio: Add IIO_DENSITY channel type
dt-bindings: iio: chemical: Add sharp,gp2y1010au0f
iio: chemical: Add driver for Sharp GP2Y1010AU0F
Documentation/ABI/testing/sysfs-bus-iio | 8 ++
.../iio/chemical/sharp,gp2y1010au0f.yaml | 78 +++++++++++
MAINTAINERS | 7 +
drivers/iio/chemical/Kconfig | 10 ++
drivers/iio/chemical/Makefile | 1 +
drivers/iio/chemical/gp2y1010.c | 126 ++++++++++++++++++
include/uapi/linux/iio/types.h | 1 +
7 files changed, 231 insertions(+)
create mode 100644 Documentation/devicetree/bindings/iio/chemical/sharp,gp2y1010au0f.yaml
create mode 100644 drivers/iio/chemical/gp2y1010.c
--
2.43.0
^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH v2 1/3] iio: Add IIO_DENSITY channel type
2025-06-12 10:07 [PATCH v2 0/3] iio: chemical: Add Sharp GP2Y1010AU0F dust sensor support surajsonawane0215
@ 2025-06-12 10:07 ` surajsonawane0215
2025-06-12 12:58 ` Andy Shevchenko
2025-06-12 16:23 ` David Lechner
2025-06-12 10:07 ` [PATCH v2 2/3] dt-bindings: iio: chemical: Add sharp,gp2y1010au0f surajsonawane0215
2025-06-12 10:07 ` [PATCH v2 3/3] iio: chemical: Add driver for Sharp GP2Y1010AU0F surajsonawane0215
2 siblings, 2 replies; 11+ messages in thread
From: surajsonawane0215 @ 2025-06-12 10:07 UTC (permalink / raw)
To: Jonathan Cameron
Cc: David Lechner, Nuno Sá, Andy Shevchenko, Rob Herring,
Krzysztof Kozlowski, Conor Dooley, linux-iio, devicetree,
linux-kernel, surajsonawane0215
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain; charset=yes, Size: 1583 bytes --]
From: Suraj Sonawane <surajsonawane0215@gmail.com>
Add IIO_DENSITY channel type for particulate matter sensors,
with base units of grams per cubic meter (g/m³). This is needed
for optical dust sensors like the Sharp GP2Y1010AU0F that measure
airborne particle concentration rather than raw voltage.
Signed-off-by: Suraj Sonawane <surajsonawane0215@gmail.com>
---
V2: Initial version introducing IIO_DENSITY channel type
Documentation/ABI/testing/sysfs-bus-iio | 8 ++++++++
include/uapi/linux/iio/types.h | 1 +
2 files changed, 9 insertions(+)
diff --git a/Documentation/ABI/testing/sysfs-bus-iio b/Documentation/ABI/testing/sysfs-bus-iio
index 190bfcc1e..9b1b538ce 100644
--- a/Documentation/ABI/testing/sysfs-bus-iio
+++ b/Documentation/ABI/testing/sysfs-bus-iio
@@ -2383,3 +2383,11 @@ Description:
Value representing the user's attention to the system expressed
in units as percentage. This usually means if the user is
looking at the screen or not.
+
+What: /sys/bus/iio/devices/iio:deviceX/in_density_raw
+KernelVersion: 6.15
+Contact: linux-iio@vger.kernel.org
+Description:
+ Raw reading from an optical dust sensor.
+ This value is proportional to dust density in air,
+ with base units of g/m^3.
+
diff --git a/include/uapi/linux/iio/types.h b/include/uapi/linux/iio/types.h
index 3eb0821af..e7d09ec0b 100644
--- a/include/uapi/linux/iio/types.h
+++ b/include/uapi/linux/iio/types.h
@@ -37,6 +37,7 @@ enum iio_chan_type {
IIO_DISTANCE,
IIO_VELOCITY,
IIO_CONCENTRATION,
+ IIO_DENSITY,
IIO_RESISTANCE,
IIO_PH,
IIO_UVINDEX,
--
2.43.0
^ permalink raw reply related [flat|nested] 11+ messages in thread
* [PATCH v2 2/3] dt-bindings: iio: chemical: Add sharp,gp2y1010au0f
2025-06-12 10:07 [PATCH v2 0/3] iio: chemical: Add Sharp GP2Y1010AU0F dust sensor support surajsonawane0215
2025-06-12 10:07 ` [PATCH v2 1/3] iio: Add IIO_DENSITY channel type surajsonawane0215
@ 2025-06-12 10:07 ` surajsonawane0215
2025-06-12 10:23 ` Krzysztof Kozlowski
2025-06-12 16:37 ` David Lechner
2025-06-12 10:07 ` [PATCH v2 3/3] iio: chemical: Add driver for Sharp GP2Y1010AU0F surajsonawane0215
2 siblings, 2 replies; 11+ messages in thread
From: surajsonawane0215 @ 2025-06-12 10:07 UTC (permalink / raw)
To: Jonathan Cameron
Cc: David Lechner, Nuno Sá, Andy Shevchenko, Rob Herring,
Krzysztof Kozlowski, Conor Dooley, linux-iio, devicetree,
linux-kernel, surajsonawane0215
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain; charset=yes, Size: 3182 bytes --]
From: Suraj Sonawane <surajsonawane0215@gmail.com>
Add device tree bindings for Sharp GP2Y1010AU0F optical dust sensor.
The sensor measures particulate matter concentration via infrared
scattering with pulsed LED illumination and analog output.
Datasheet: https://global.sharp/products/device/lineup/data/pdf/datasheet/gp2y1010au_appl_e.pdf
Signed-off-by: Suraj Sonawane <surajsonawane0215@gmail.com>
---
V1: https://lore.kernel.org/lkml/20250425215149.49068-1-surajsonawane0215@gmail.com/
V2:
- Updated timing properties to match datasheet exactly
- Added reg property for multi-sensor support
.../iio/chemical/sharp,gp2y1010au0f.yaml | 78 +++++++++++++++++++
1 file changed, 78 insertions(+)
create mode 100644 Documentation/devicetree/bindings/iio/chemical/sharp,gp2y1010au0f.yaml
diff --git a/Documentation/devicetree/bindings/iio/chemical/sharp,gp2y1010au0f.yaml b/Documentation/devicetree/bindings/iio/chemical/sharp,gp2y1010au0f.yaml
new file mode 100644
index 000000000..b4e5110d0
--- /dev/null
+++ b/Documentation/devicetree/bindings/iio/chemical/sharp,gp2y1010au0f.yaml
@@ -0,0 +1,78 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/chemical/sharp,gp2y1010au0f.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Sharp GP2Y1010AU0F Optical Dust Sensor
+
+maintainers:
+ - Suraj Sonawane <surajsonawane0215@gmail.com>
+
+description: |
+ Optical dust sensor measuring particulate matter concentration via infrared scattering.
+ Requires ADC for analog output and GPIO for pulsed LED control with strict timing.
+ Datasheet: https://global.sharp/products/device/lineup/data/pdf/datasheet/gp2y1010au_appl_e.pdf
+
+properties:
+ compatible:
+ const: sharp,gp2y1010au0f
+
+ reg:
+ maxItems: 1
+
+ vdd-supply: true
+
+ led-gpios:
+ description: GPIO connected to the sensor's LED control pin (V-LED)
+ maxItems: 1
+
+ io-channels:
+ description: ADC channel connected to the sensor's analog output (Vo)
+ maxItems: 1
+
+ io-channel-names:
+ const: dust
+
+ sharp,led-on-delay-us:
+ description: |
+ Delay in microseconds after turning the LED ON before reading ADC.
+ The datasheet recommends 280µs after LED ON for accurate measurement.
+ (See Section 6-1 of the datasheet: Sampling timing = 0.28ms)
+
+ default: 280
+ minimum: 0
+ maximum: 320
+
+ sharp,measurement-window-us:
+ description: |
+ Duration in microseconds the LED remains ON for measurement.
+ The recommended pulse width is 320µs ±20µs. (See Section 6-1)
+
+ default: 320
+ minimum: 300
+ maximum: 340
+
+required:
+ - compatible
+ - reg
+ - led-gpios
+ - io-channels
+ - io-channel-names
+
+additionalProperties: false
+
+examples:
+ - |
+ #include <dt-bindings/gpio/gpio.h>
+
+ dust_sensor {
+ compatible = "sharp,gp2y1010au0f";
+ reg = <0>;
+ vdd-supply = <&vcc>;
+ led-gpios = <&gpio 44 GPIO_ACTIVE_HIGH>;
+ io-channels = <&adc 0>;
+ io-channel-names = "dust";
+ sharp,led-on-delay-us = <280>;
+ sharp,measurement-window-us = <320>;
+ };
--
2.43.0
^ permalink raw reply related [flat|nested] 11+ messages in thread
* [PATCH v2 3/3] iio: chemical: Add driver for Sharp GP2Y1010AU0F
2025-06-12 10:07 [PATCH v2 0/3] iio: chemical: Add Sharp GP2Y1010AU0F dust sensor support surajsonawane0215
2025-06-12 10:07 ` [PATCH v2 1/3] iio: Add IIO_DENSITY channel type surajsonawane0215
2025-06-12 10:07 ` [PATCH v2 2/3] dt-bindings: iio: chemical: Add sharp,gp2y1010au0f surajsonawane0215
@ 2025-06-12 10:07 ` surajsonawane0215
2025-06-12 13:15 ` Andy Shevchenko
` (2 more replies)
2 siblings, 3 replies; 11+ messages in thread
From: surajsonawane0215 @ 2025-06-12 10:07 UTC (permalink / raw)
To: Jonathan Cameron
Cc: David Lechner, Nuno Sá, Andy Shevchenko, Rob Herring,
Krzysztof Kozlowski, Conor Dooley, linux-iio, devicetree,
linux-kernel, surajsonawane0215
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain; charset=yes, Size: 7384 bytes --]
From: Suraj Sonawane <surajsonawane0215@gmail.com>
Implement support for the Sharp GP2Y1010AU0F optical dust sensor which
measures particulate matter concentration using infrared scattering.
The sensor requires precise 320μs LED pulses with ADC sampling at 280μs
after LED activation (as specified in datasheet section 6-1).
The driver provides:
- Raw density readings via IIO_DENSITY channel type
- Hardware-agnostic operation via GPIO and IIO ADC interfaces
- Power management through regulator framework
- Device Tree binding support
Datasheet: https://global.sharp/products/device/lineup/data/pdf/datasheet/gp2y1010au_appl_e.pdf
Signed-off-by: Suraj Sonawane <surajsonawane0215@gmail.com>
---
V1: https://lore.kernel.org/lkml/20250425221214.50255-1-surajsonawane0215@gmail.com/
V2:
- Added `reg` property for multi-sensor support
- Moved datasheet reference to tags block
- Hardware interface improvements:
* Used devm_regulator_get_enable() for power management
* Consolidated LED ON timing into a single delay block
* Improved error handling in the ADC read sequence
- Dropped IIO_BUFFER dependencies
- Alphabetized Makefile entry and #includes
- Replaced `extend_name` with proper label callback
- Standardized device logs using `pdev->dev`
- Adopted IIO formatting conventions (brace spacing)
- Switched channel type from IIO_VOLTAGE to IIO_DENSITY
- Adjusted timing values to match datasheet specs
MAINTAINERS | 7 ++
drivers/iio/chemical/Kconfig | 10 +++
drivers/iio/chemical/Makefile | 1 +
drivers/iio/chemical/gp2y1010.c | 126 ++++++++++++++++++++++++++++++++
4 files changed, 144 insertions(+)
create mode 100644 drivers/iio/chemical/gp2y1010.c
diff --git a/MAINTAINERS b/MAINTAINERS
index a92290fff..ed239b60d 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -22568,6 +22568,13 @@ T: git git://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio.git
F: Documentation/devicetree/bindings/iio/light/sharp,gp2ap002.yaml
F: drivers/iio/light/gp2ap002.c
+SHARP GP2Y1010AU0F DUST SENSOR DRIVER
+M: Suraj Sonawane <surajsonawane0215@gmail.com>
+L: linux-iio@vger.kernel.org
+S: Maintained
+F: Documentation/devicetree/bindings/iio/chemical/sharp,gp2y1010au0f.yaml
+F: drivers/iio/chemical/gp2y1010.c
+
SHARP RJ54N1CB0C SENSOR DRIVER
M: Jacopo Mondi <jacopo@jmondi.org>
L: linux-media@vger.kernel.org
diff --git a/drivers/iio/chemical/Kconfig b/drivers/iio/chemical/Kconfig
index b22afa1f6..35c126836 100644
--- a/drivers/iio/chemical/Kconfig
+++ b/drivers/iio/chemical/Kconfig
@@ -129,6 +129,16 @@ config PMS7003
To compile this driver as a module, choose M here: the module will
be called pms7003.
+
+config GP2Y1010AU0F
+ tristate "Sharp GP2Y1010AU0F optical dust sensor"
+ depends on IIO
+ 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 SCD30_CORE
tristate "SCD30 carbon dioxide sensor driver"
diff --git a/drivers/iio/chemical/Makefile b/drivers/iio/chemical/Makefile
index 2287a00a6..f1d932ab0 100644
--- a/drivers/iio/chemical/Makefile
+++ b/drivers/iio/chemical/Makefile
@@ -14,6 +14,7 @@ obj-$(CONFIG_CCS811) += ccs811.o
obj-$(CONFIG_ENS160) += ens160_core.o
obj-$(CONFIG_ENS160_I2C) += ens160_i2c.o
obj-$(CONFIG_ENS160_SPI) += ens160_spi.o
+obj-$(CONFIG_GP2Y1010AU0F) += gp2y1010.o
obj-$(CONFIG_IAQCORE) += ams-iaq-core.o
obj-$(CONFIG_MHZ19B) += mhz19b.o
obj-$(CONFIG_PMS7003) += pms7003.o
diff --git a/drivers/iio/chemical/gp2y1010.c b/drivers/iio/chemical/gp2y1010.c
new file mode 100644
index 000000000..3a8657035
--- /dev/null
+++ b/drivers/iio/chemical/gp2y1010.c
@@ -0,0 +1,126 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2025 Suraj Sonawane <surajsonawane0215@gmail.com>
+ * Sharp GP2Y1010AU0F Dust Sensor Driver
+ * Datasheet: https://global.sharp/products/device/lineup/data/pdf/datasheet/gp2y1010au_appl_e.pdf
+ */
+
+#include <linux/delay.h>
+#include <linux/err.h>
+#include <linux/gpio/consumer.h>
+#include <linux/iio/consumer.h>
+#include <linux/iio/iio.h>
+#include <linux/init.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+#include <linux/regulator/consumer.h>
+
+/* Timings based on GP2Y1010AU0F datasheet Section 6-1 */
+#define GP2Y1010_LED_PULSE_US 320 /* Total LED ON time (0.32 ms) */
+#define GP2Y1010_SAMPLE_DELAY_US 280 /* ADC sampling after LED ON (0.28 ms) */
+
+struct gp2y1010_data {
+ struct gpio_desc *led_gpio;
+ struct iio_channel *adc_chan;
+ int v_clean; /* Calibration: voltage in clean air (mV) */
+};
+
+static int gp2y1010_read_raw(struct iio_dev *indio_dev,
+ 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;
+
+ gpiod_set_value(data->led_gpio, 1);
+ udelay(GP2Y1010_SAMPLE_DELAY_US);
+
+ ret = iio_read_channel_processed(data->adc_chan, &voltage_mv);
+
+ /* Wait remaining time to complete 320 µs total LED pulse width */
+ udelay(GP2Y1010_LED_PULSE_US - GP2Y1010_SAMPLE_DELAY_US);
+ gpiod_set_value(data->led_gpio, 0);
+
+ if (ret < 0)
+ return ret;
+
+ *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_DENSITY,
+ .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
+ },
+};
+
+static int gp2y1010_probe(struct platform_device *pdev)
+{
+ struct device *dev = &pdev->dev;
+ struct iio_dev *indio_dev;
+ struct gp2y1010_data *data;
+ enum iio_chan_type ch_type;
+ int ret;
+
+ indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
+ if (!indio_dev)
+ return -ENOMEM;
+
+ data = iio_priv(indio_dev);
+ data->v_clean = 900;
+
+ data->led_gpio = devm_gpiod_get(dev, "led", GPIOD_OUT_LOW);
+ if (IS_ERR(data->led_gpio))
+ return dev_err_probe(dev, PTR_ERR(data->led_gpio), "Failed to get LED GPIO\n");
+
+ ret = devm_regulator_get_enable(dev, "vdd");
+ if (ret)
+ return ret;
+ udelay(100);
+
+ data->adc_chan = devm_iio_channel_get(dev, "dust");
+ if (IS_ERR(data->adc_chan))
+ return dev_err_probe(dev, PTR_ERR(data->adc_chan), "Failed to get ADC channel\n");
+
+ ret = iio_get_channel_type(data->adc_chan, &ch_type);
+ if (ret < 0)
+ return ret;
+ if (ch_type != IIO_DENSITY)
+ return dev_err_probe(dev, -EINVAL, "ADC channel is not density type\n");
+
+ indio_dev->name = dev_name(dev);
+ indio_dev->info = &gp2y1010_info;
+ indio_dev->channels = gp2y1010_channels;
+ indio_dev->num_channels = ARRAY_SIZE(gp2y1010_channels);
+ indio_dev->modes = INDIO_DIRECT_MODE;
+
+ return devm_iio_device_register(dev, indio_dev);
+}
+
+static const struct of_device_id gp2y1010_of_match[] = {
+ { .compatible = "sharp,gp2y1010au0f" },
+ { }
+};
+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,
+};
+module_platform_driver(gp2y1010_driver);
+
+MODULE_AUTHOR("Suraj Sonawane <surajsonawane0215@gmail.com>");
+MODULE_DESCRIPTION("Sharp GP2Y1010AU0F Dust Sensor Driver");
+MODULE_LICENSE("GPL");
--
2.43.0
^ permalink raw reply related [flat|nested] 11+ messages in thread
* Re: [PATCH v2 2/3] dt-bindings: iio: chemical: Add sharp,gp2y1010au0f
2025-06-12 10:07 ` [PATCH v2 2/3] dt-bindings: iio: chemical: Add sharp,gp2y1010au0f surajsonawane0215
@ 2025-06-12 10:23 ` Krzysztof Kozlowski
2025-06-12 16:37 ` David Lechner
1 sibling, 0 replies; 11+ messages in thread
From: Krzysztof Kozlowski @ 2025-06-12 10:23 UTC (permalink / raw)
To: surajsonawane0215, Jonathan Cameron
Cc: David Lechner, Nuno Sá, Andy Shevchenko, Rob Herring,
Krzysztof Kozlowski, Conor Dooley, linux-iio, devicetree,
linux-kernel
On 12/06/2025 12:07, surajsonawane0215@gmail.com wrote:
> +description: |
> + Optical dust sensor measuring particulate matter concentration via infrared scattering.
> + Requires ADC for analog output and GPIO for pulsed LED control with strict timing.
Wrap according to Linux coding style (so 80).
> + Datasheet: https://global.sharp/products/device/lineup/data/pdf/datasheet/gp2y1010au_appl_e.pdf
> +
> +properties:
> + compatible:
> + const: sharp,gp2y1010au0f
> +
> + reg:
> + maxItems: 1
> +
> + vdd-supply: true
> +
> + led-gpios:
> + description: GPIO connected to the sensor's LED control pin (V-LED)
> + maxItems: 1
> +
> + io-channels:
> + description: ADC channel connected to the sensor's analog output (Vo)
> + maxItems: 1
> +
> + io-channel-names:
> + const: dust
> +
> + sharp,led-on-delay-us:
> + description: |
> + Delay in microseconds after turning the LED ON before reading ADC.
> + The datasheet recommends 280µs after LED ON for accurate measurement.
> + (See Section 6-1 of the datasheet: Sampling timing = 0.28ms)
> +
Drop blank line
> + default: 280
> + minimum: 0
> + maximum: 320
> +
> + sharp,measurement-window-us:
> + description: |
> + Duration in microseconds the LED remains ON for measurement.
> + The recommended pulse width is 320µs ±20µs. (See Section 6-1)
What is the point of calling something recommended if there is no other
choice? Your schema here:
> +
> + default: 320
> + minimum: 300
> + maximum: 340
Says 360 is not allowed, so what is the meaning of recommended in such case?
> +
> +required:
> + - compatible
> + - reg
> + - led-gpios
> + - io-channels
> + - io-channel-names
> +
> +additionalProperties: false
> +
> +examples:
> + - |
> + #include <dt-bindings/gpio/gpio.h>
> +
> + dust_sensor {
No underscores, see DTS coding style. Not tested either, missing unit
address and testing would point it out most likely.
Best regards,
Krzysztof
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v2 1/3] iio: Add IIO_DENSITY channel type
2025-06-12 10:07 ` [PATCH v2 1/3] iio: Add IIO_DENSITY channel type surajsonawane0215
@ 2025-06-12 12:58 ` Andy Shevchenko
2025-06-12 16:23 ` David Lechner
1 sibling, 0 replies; 11+ messages in thread
From: Andy Shevchenko @ 2025-06-12 12:58 UTC (permalink / raw)
To: surajsonawane0215
Cc: Jonathan Cameron, David Lechner, Nuno Sá, Andy Shevchenko,
Rob Herring, Krzysztof Kozlowski, Conor Dooley, linux-iio,
devicetree, linux-kernel
On Thu, Jun 12, 2025 at 03:37:44PM +0530, surajsonawane0215@gmail.com wrote:
> From: Suraj Sonawane <surajsonawane0215@gmail.com>
>
> Add IIO_DENSITY channel type for particulate matter sensors,
> with base units of grams per cubic meter (g/m³). This is needed
> for optical dust sensors like the Sharp GP2Y1010AU0F that measure
> airborne particle concentration rather than raw voltage.
...
> +What: /sys/bus/iio/devices/iio:deviceX/in_density_raw
> +KernelVersion: 6.15
If it was missed in the past, add Fixes tag, otherwise this boat already
sailed, the v6.17 is the nearest one.
> +Contact: linux-iio@vger.kernel.org
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v2 3/3] iio: chemical: Add driver for Sharp GP2Y1010AU0F
2025-06-12 10:07 ` [PATCH v2 3/3] iio: chemical: Add driver for Sharp GP2Y1010AU0F surajsonawane0215
@ 2025-06-12 13:15 ` Andy Shevchenko
2025-06-12 18:24 ` David Lechner
2025-06-14 12:33 ` Jonathan Cameron
2 siblings, 0 replies; 11+ messages in thread
From: Andy Shevchenko @ 2025-06-12 13:15 UTC (permalink / raw)
To: surajsonawane0215
Cc: Jonathan Cameron, David Lechner, Nuno Sá, Andy Shevchenko,
Rob Herring, Krzysztof Kozlowski, Conor Dooley, linux-iio,
devicetree, linux-kernel
On Thu, Jun 12, 2025 at 03:37:46PM +0530, surajsonawane0215@gmail.com wrote:
>
> Implement support for the Sharp GP2Y1010AU0F optical dust sensor which
> measures particulate matter concentration using infrared scattering.
> The sensor requires precise 320μs LED pulses with ADC sampling at 280μs
> after LED activation (as specified in datasheet section 6-1).
>
> The driver provides:
> - Raw density readings via IIO_DENSITY channel type
> - Hardware-agnostic operation via GPIO and IIO ADC interfaces
> - Power management through regulator framework
> - Device Tree binding support
> Datasheet: https://global.sharp/products/device/lineup/data/pdf/datasheet/gp2y1010au_appl_e.pdf
>
> Signed-off-by: Suraj Sonawane <surajsonawane0215@gmail.com>
No blank line(s) in the tag block.
> ---
...
> config PMS7003
> To compile this driver as a module, choose M here: the module will
> be called pms7003.
> +
> +config GP2Y1010AU0F
Shouldn't this be alphabetically ordered?
> + tristate "Sharp GP2Y1010AU0F optical dust sensor"
> + depends on IIO
Is it needed? Nothing is missed?
...
> +#include <linux/delay.h>
> +#include <linux/err.h>
> +#include <linux/gpio/consumer.h>
> +#include <linux/iio/consumer.h>
> +#include <linux/iio/iio.h>
> +#include <linux/init.h>
> +#include <linux/module.h>
> +#include <linux/of.h>
No of.h in a new code, please. Also follow IWYU principle, there are missed inclusionfs.
> +#include <linux/platform_device.h>
> +#include <linux/regulator/consumer.h>
...
> +struct gp2y1010_data {
> + struct gpio_desc *led_gpio;
> + struct iio_channel *adc_chan;
> + int v_clean; /* Calibration: voltage in clean air (mV) */
clean_air_uV is much better naming, most of the comment won't be needed with it.
> +};
...
> +static int gp2y1010_read_raw(struct iio_dev *indio_dev,
> + struct iio_chan_spec const *chan,
> + int *val, int *val2, long mask)
> +{
> + struct gp2y1010_data *data = iio_priv(indio_dev);
> + int ret, voltage_mv;
_mV
Also I recommend to split them as they are not semantically the same.
> +
> + if (mask != IIO_CHAN_INFO_RAW)
> + return -EINVAL;
> +
> + gpiod_set_value(data->led_gpio, 1);
> + udelay(GP2Y1010_SAMPLE_DELAY_US);
flseep() and explain the need of it.
> +
> + ret = iio_read_channel_processed(data->adc_chan, &voltage_mv);
> +
> + /* Wait remaining time to complete 320 µs total LED pulse width */
> + udelay(GP2Y1010_LED_PULSE_US - GP2Y1010_SAMPLE_DELAY_US);
fsleep()
> + gpiod_set_value(data->led_gpio, 0);
> +
> + if (ret < 0)
> + return ret;
> +
> + *val = voltage_mv;
> + return IIO_VAL_INT;
> +}
...
> +static int gp2y1010_probe(struct platform_device *pdev)
> +{
> + struct device *dev = &pdev->dev;
> + struct iio_dev *indio_dev;
> + struct gp2y1010_data *data;
> + enum iio_chan_type ch_type;
> + int ret;
> +
> + indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
> + if (!indio_dev)
> + return -ENOMEM;
> +
> + data = iio_priv(indio_dev);
> + data->v_clean = 900;
The default must be defined with a comment as constant.
> + data->led_gpio = devm_gpiod_get(dev, "led", GPIOD_OUT_LOW);
> + if (IS_ERR(data->led_gpio))
> + return dev_err_probe(dev, PTR_ERR(data->led_gpio), "Failed to get LED GPIO\n");
> +
> + ret = devm_regulator_get_enable(dev, "vdd");
> + if (ret)
> + return ret;
> + udelay(100);
> +
> + data->adc_chan = devm_iio_channel_get(dev, "dust");
> + if (IS_ERR(data->adc_chan))
> + return dev_err_probe(dev, PTR_ERR(data->adc_chan), "Failed to get ADC channel\n");
> +
> + ret = iio_get_channel_type(data->adc_chan, &ch_type);
> + if (ret < 0)
> + return ret;
> + if (ch_type != IIO_DENSITY)
> + return dev_err_probe(dev, -EINVAL, "ADC channel is not density type\n");
> +
> + indio_dev->name = dev_name(dev);
> + indio_dev->info = &gp2y1010_info;
> + indio_dev->channels = gp2y1010_channels;
> + indio_dev->num_channels = ARRAY_SIZE(gp2y1010_channels);
> + indio_dev->modes = INDIO_DIRECT_MODE;
> +
> + return devm_iio_device_register(dev, indio_dev);
> +}
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v2 1/3] iio: Add IIO_DENSITY channel type
2025-06-12 10:07 ` [PATCH v2 1/3] iio: Add IIO_DENSITY channel type surajsonawane0215
2025-06-12 12:58 ` Andy Shevchenko
@ 2025-06-12 16:23 ` David Lechner
1 sibling, 0 replies; 11+ messages in thread
From: David Lechner @ 2025-06-12 16:23 UTC (permalink / raw)
To: surajsonawane0215, Jonathan Cameron
Cc: Nuno Sá, Andy Shevchenko, Rob Herring, Krzysztof Kozlowski,
Conor Dooley, linux-iio, devicetree, linux-kernel
On 6/12/25 5:07 AM, surajsonawane0215@gmail.com wrote:
> From: Suraj Sonawane <surajsonawane0215@gmail.com>
>
> Add IIO_DENSITY channel type for particulate matter sensors,
> with base units of grams per cubic meter (g/m³). This is needed
> for optical dust sensors like the Sharp GP2Y1010AU0F that measure
> airborne particle concentration rather than raw voltage.
>
> Signed-off-by: Suraj Sonawane <surajsonawane0215@gmail.com>
> ---
> V2: Initial version introducing IIO_DENSITY channel type
>
> Documentation/ABI/testing/sysfs-bus-iio | 8 ++++++++
> include/uapi/linux/iio/types.h | 1 +
> 2 files changed, 9 insertions(+)
>
> diff --git a/Documentation/ABI/testing/sysfs-bus-iio b/Documentation/ABI/testing/sysfs-bus-iio
> index 190bfcc1e..9b1b538ce 100644
> --- a/Documentation/ABI/testing/sysfs-bus-iio
> +++ b/Documentation/ABI/testing/sysfs-bus-iio
> @@ -2383,3 +2383,11 @@ Description:
> Value representing the user's attention to the system expressed
> in units as percentage. This usually means if the user is
> looking at the screen or not.
> +
> +What: /sys/bus/iio/devices/iio:deviceX/in_density_raw
> +KernelVersion: 6.15
> +Contact: linux-iio@vger.kernel.org
> +Description:
> + Raw reading from an optical dust sensor.
> + This value is proportional to dust density in air,
I don't see why density would be specific to dust. We could make
this description more generic.
> + with base units of g/m^3.
> +
> diff --git a/include/uapi/linux/iio/types.h b/include/uapi/linux/iio/types.h
> index 3eb0821af..e7d09ec0b 100644
> --- a/include/uapi/linux/iio/types.h
> +++ b/include/uapi/linux/iio/types.h
> @@ -37,6 +37,7 @@ enum iio_chan_type {
> IIO_DISTANCE,
> IIO_VELOCITY,
> IIO_CONCENTRATION,
> + IIO_DENSITY,
This is part of the userspace ABI, new items have to be added
to the end. We can't change the meaning of the numeric values.
> IIO_RESISTANCE,
> IIO_PH,
> IIO_UVINDEX,
There are also a couple more places where this new channel needs
be added as well. See [1] for an example (although density probably
doesn't need to be added to known events).
[1]: https://lore.kernel.org/all/20241101-hpd-v3-3-e9c80b7c7164@chromium.org/
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v2 2/3] dt-bindings: iio: chemical: Add sharp,gp2y1010au0f
2025-06-12 10:07 ` [PATCH v2 2/3] dt-bindings: iio: chemical: Add sharp,gp2y1010au0f surajsonawane0215
2025-06-12 10:23 ` Krzysztof Kozlowski
@ 2025-06-12 16:37 ` David Lechner
1 sibling, 0 replies; 11+ messages in thread
From: David Lechner @ 2025-06-12 16:37 UTC (permalink / raw)
To: surajsonawane0215, Jonathan Cameron
Cc: Nuno Sá, Andy Shevchenko, Rob Herring, Krzysztof Kozlowski,
Conor Dooley, linux-iio, devicetree, linux-kernel
On 6/12/25 5:07 AM, surajsonawane0215@gmail.com wrote:
> From: Suraj Sonawane <surajsonawane0215@gmail.com>
>
> Add device tree bindings for Sharp GP2Y1010AU0F optical dust sensor.
> The sensor measures particulate matter concentration via infrared
> scattering with pulsed LED illumination and analog output.
>
> Datasheet: https://global.sharp/products/device/lineup/data/pdf/datasheet/gp2y1010au_appl_e.pdf
>
> Signed-off-by: Suraj Sonawane <surajsonawane0215@gmail.com>
> ---
> V1: https://lore.kernel.org/lkml/20250425215149.49068-1-surajsonawane0215@gmail.com/
> V2:
> - Updated timing properties to match datasheet exactly
> - Added reg property for multi-sensor support
>
> .../iio/chemical/sharp,gp2y1010au0f.yaml | 78 +++++++++++++++++++
> 1 file changed, 78 insertions(+)
> create mode 100644 Documentation/devicetree/bindings/iio/chemical/sharp,gp2y1010au0f.yaml
>
> diff --git a/Documentation/devicetree/bindings/iio/chemical/sharp,gp2y1010au0f.yaml b/Documentation/devicetree/bindings/iio/chemical/sharp,gp2y1010au0f.yaml
> new file mode 100644
> index 000000000..b4e5110d0
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/iio/chemical/sharp,gp2y1010au0f.yaml
> @@ -0,0 +1,78 @@
> +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
> +%YAML 1.2
> +---
> +$id: http://devicetree.org/schemas/iio/chemical/sharp,gp2y1010au0f.yaml#
> +$schema: http://devicetree.org/meta-schemas/core.yaml#
> +
> +title: Sharp GP2Y1010AU0F Optical Dust Sensor
> +
> +maintainers:
> + - Suraj Sonawane <surajsonawane0215@gmail.com>
> +
> +description: |
> + Optical dust sensor measuring particulate matter concentration via infrared scattering.
> + Requires ADC for analog output and GPIO for pulsed LED control with strict timing.
> + Datasheet: https://global.sharp/products/device/lineup/data/pdf/datasheet/gp2y1010au_appl_e.pdf
> +
> +properties:
> + compatible:
> + const: sharp,gp2y1010au0f
> +
> + reg:
> + maxItems: 1
There is no SPI or I2C bus, so reg doesn't make sense.
> +
> + vdd-supply: true
> +
> + led-gpios:
> + description: GPIO connected to the sensor's LED control pin (V-LED)
> + maxItems: 1
From the looks of the datasheet it seems like the light requires
a PWM rather than a GPIO to control it. I don't see how a GPIO
could be accurate enough in the timing.
> +
> + io-channels:
> + description: ADC channel connected to the sensor's analog output (Vo)
> + maxItems: 1
> +
> + io-channel-names:
> + const: dust
> +
> + sharp,led-on-delay-us:
> + description: |
> + Delay in microseconds after turning the LED ON before reading ADC.
> + The datasheet recommends 280µs after LED ON for accurate measurement.
> + (See Section 6-1 of the datasheet: Sampling timing = 0.28ms)
> +
> + default: 280
> + minimum: 0
> + maximum: 320
For this, I would expect the same or a synchronized PWM output to trigger
the ADC in order to be able to get accurate enough timing.
> +
> + sharp,measurement-window-us:
> + description: |
> + Duration in microseconds the LED remains ON for measurement.
> + The recommended pulse width is 320µs ±20µs. (See Section 6-1)
> +
> + default: 320
> + minimum: 300
> + maximum: 340
And this would be the duty cycle of the light PWM.
> +
> +required:
> + - compatible
> + - reg
> + - led-gpios
> + - io-channels
> + - io-channel-names
> +
> +additionalProperties: false
> +
> +examples:
> + - |
> + #include <dt-bindings/gpio/gpio.h>
> +
> + dust_sensor {
> + compatible = "sharp,gp2y1010au0f";
> + reg = <0>;
> + vdd-supply = <&vcc>;
> + led-gpios = <&gpio 44 GPIO_ACTIVE_HIGH>;
> + io-channels = <&adc 0>;
> + io-channel-names = "dust";
> + sharp,led-on-delay-us = <280>;
> + sharp,measurement-window-us = <320>;
> + };
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v2 3/3] iio: chemical: Add driver for Sharp GP2Y1010AU0F
2025-06-12 10:07 ` [PATCH v2 3/3] iio: chemical: Add driver for Sharp GP2Y1010AU0F surajsonawane0215
2025-06-12 13:15 ` Andy Shevchenko
@ 2025-06-12 18:24 ` David Lechner
2025-06-14 12:33 ` Jonathan Cameron
2 siblings, 0 replies; 11+ messages in thread
From: David Lechner @ 2025-06-12 18:24 UTC (permalink / raw)
To: surajsonawane0215, Jonathan Cameron
Cc: Nuno Sá, Andy Shevchenko, Rob Herring, Krzysztof Kozlowski,
Conor Dooley, linux-iio, devicetree, linux-kernel
On 6/12/25 5:07 AM, surajsonawane0215@gmail.com wrote:
> From: Suraj Sonawane <surajsonawane0215@gmail.com>
>
> Implement support for the Sharp GP2Y1010AU0F optical dust sensor which
> measures particulate matter concentration using infrared scattering.
> The sensor requires precise 320μs LED pulses with ADC sampling at 280μs
> after LED activation (as specified in datasheet section 6-1).
>
> The driver provides:
> - Raw density readings via IIO_DENSITY channel type
> - Hardware-agnostic operation via GPIO and IIO ADC interfaces
> - Power management through regulator framework
> - Device Tree binding support
>
> Datasheet: https://global.sharp/products/device/lineup/data/pdf/datasheet/gp2y1010au_appl_e.pdf
>
> Signed-off-by: Suraj Sonawane <surajsonawane0215@gmail.com>
> ---
...
> diff --git a/drivers/iio/chemical/Kconfig b/drivers/iio/chemical/Kconfig
> index b22afa1f6..35c126836 100644
> --- a/drivers/iio/chemical/Kconfig
> +++ b/drivers/iio/chemical/Kconfig
> @@ -129,6 +129,16 @@ config PMS7003
>
> To compile this driver as a module, choose M here: the module will
> be called pms7003.
> +
> +config GP2Y1010AU0F
> + tristate "Sharp GP2Y1010AU0F optical dust sensor"
> + depends on IIO
> + 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.
The object file name is just gp2y1010.
>
> config SCD30_CORE
> tristate "SCD30 carbon dioxide sensor driver"
> diff --git a/drivers/iio/chemical/Makefile b/drivers/iio/chemical/Makefile
> index 2287a00a6..f1d932ab0 100644
> --- a/drivers/iio/chemical/Makefile
> +++ b/drivers/iio/chemical/Makefile
> @@ -14,6 +14,7 @@ obj-$(CONFIG_CCS811) += ccs811.o
> obj-$(CONFIG_ENS160) += ens160_core.o
> obj-$(CONFIG_ENS160_I2C) += ens160_i2c.o
> obj-$(CONFIG_ENS160_SPI) += ens160_spi.o
> +obj-$(CONFIG_GP2Y1010AU0F) += gp2y1010.o
> obj-$(CONFIG_IAQCORE) += ams-iaq-core.o
> obj-$(CONFIG_MHZ19B) += mhz19b.o
> obj-$(CONFIG_PMS7003) += pms7003.o
> diff --git a/drivers/iio/chemical/gp2y1010.c b/drivers/iio/chemical/gp2y1010.c
> new file mode 100644
> index 000000000..3a8657035
> --- /dev/null
> +++ b/drivers/iio/chemical/gp2y1010.c
> @@ -0,0 +1,126 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * Copyright (c) 2025 Suraj Sonawane <surajsonawane0215@gmail.com>
> + * Sharp GP2Y1010AU0F Dust Sensor Driver
> + * Datasheet: https://global.sharp/products/device/lineup/data/pdf/datasheet/gp2y1010au_appl_e.pdf
> + */
> +
> +#include <linux/delay.h>
> +#include <linux/err.h>
> +#include <linux/gpio/consumer.h>
> +#include <linux/iio/consumer.h>
> +#include <linux/iio/iio.h>
> +#include <linux/init.h>
> +#include <linux/module.h>
> +#include <linux/of.h>
> +#include <linux/platform_device.h>
> +#include <linux/regulator/consumer.h>
> +
> +/* Timings based on GP2Y1010AU0F datasheet Section 6-1 */
> +#define GP2Y1010_LED_PULSE_US 320 /* Total LED ON time (0.32 ms) */
> +#define GP2Y1010_SAMPLE_DELAY_US 280 /* ADC sampling after LED ON (0.28 ms) */
If we aren't using the values from the devicetree for this
I would leave those out of the devicetree bindings until we
are really sure we need them.
> +
> +struct gp2y1010_data {
> + struct gpio_desc *led_gpio;
> + struct iio_channel *adc_chan;
> + int v_clean; /* Calibration: voltage in clean air (mV) */
> +};
> +
> +static int gp2y1010_read_raw(struct iio_dev *indio_dev,
> + 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;
> +
> + gpiod_set_value(data->led_gpio, 1);
> + udelay(GP2Y1010_SAMPLE_DELAY_US);
> +
> + ret = iio_read_channel_processed(data->adc_chan, &voltage_mv);
As I mentioned in the reply to the devicetree bindings. I have some
doubts that we could get accurate enough timing to have the ADC read
exactly at the peak output +/-20 microseconds in Linux like this.
Are you using an oscilloscope to verify that the timing is correct?
> +
> + /* Wait remaining time to complete 320 µs total LED pulse width */
> + udelay(GP2Y1010_LED_PULSE_US - GP2Y1010_SAMPLE_DELAY_US);
> + gpiod_set_value(data->led_gpio, 0);
> +
> + if (ret < 0)
> + return ret;
> +
> + *val = voltage_mv;
Since we can't read a raw value directly from the sensor,
might as well convert this to the processes value. This
would also allow to handle the non-linear parts at the
extremes of the graph shown in the datasheet.
> + 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_DENSITY,
> + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
> + },
> +};
There is only one channel, so we don't need an array.
> +
> +static int gp2y1010_probe(struct platform_device *pdev)
> +{
> + struct device *dev = &pdev->dev;
> + struct iio_dev *indio_dev;
> + struct gp2y1010_data *data;
> + enum iio_chan_type ch_type;
> + int ret;
> +
> + indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
> + if (!indio_dev)
> + return -ENOMEM;
> +
> + data = iio_priv(indio_dev);
> + data->v_clean = 900;
This isn't used anywhere.
> +
> + data->led_gpio = devm_gpiod_get(dev, "led", GPIOD_OUT_LOW);
> + if (IS_ERR(data->led_gpio))
> + return dev_err_probe(dev, PTR_ERR(data->led_gpio), "Failed to get LED GPIO\n");
> +
> + ret = devm_regulator_get_enable(dev, "vdd");
> + if (ret)
> + return ret;
> + udelay(100);
Datasheet says this delay is "less than 1 sec". So this seems rather short.
> +
> + data->adc_chan = devm_iio_channel_get(dev, "dust");
> + if (IS_ERR(data->adc_chan))
> + return dev_err_probe(dev, PTR_ERR(data->adc_chan), "Failed to get ADC channel\n");
> +
> + ret = iio_get_channel_type(data->adc_chan, &ch_type);
> + if (ret < 0)
> + return ret;
> + if (ch_type != IIO_DENSITY)
> + return dev_err_probe(dev, -EINVAL, "ADC channel is not density type\n");
This check looks wrong. Aren't we expecting a voltage channel?
> +
> + indio_dev->name = dev_name(dev);
> + indio_dev->info = &gp2y1010_info;
> + indio_dev->channels = gp2y1010_channels;
> + indio_dev->num_channels = ARRAY_SIZE(gp2y1010_channels);
> + indio_dev->modes = INDIO_DIRECT_MODE;
> +
> + return devm_iio_device_register(dev, indio_dev);
> +}
> +
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v2 3/3] iio: chemical: Add driver for Sharp GP2Y1010AU0F
2025-06-12 10:07 ` [PATCH v2 3/3] iio: chemical: Add driver for Sharp GP2Y1010AU0F surajsonawane0215
2025-06-12 13:15 ` Andy Shevchenko
2025-06-12 18:24 ` David Lechner
@ 2025-06-14 12:33 ` Jonathan Cameron
2 siblings, 0 replies; 11+ messages in thread
From: Jonathan Cameron @ 2025-06-14 12:33 UTC (permalink / raw)
To: surajsonawane0215
Cc: David Lechner, Nuno Sá, Andy Shevchenko, Rob Herring,
Krzysztof Kozlowski, Conor Dooley, linux-iio, devicetree,
linux-kernel
On Thu, 12 Jun 2025 15:37:46 +0530
surajsonawane0215@gmail.com wrote:
> From: Suraj Sonawane <surajsonawane0215@gmail.com>
>
> Implement support for the Sharp GP2Y1010AU0F optical dust sensor which
> measures particulate matter concentration using infrared scattering.
> The sensor requires precise 320μs LED pulses with ADC sampling at 280μs
> after LED activation (as specified in datasheet section 6-1).
>
> The driver provides:
> - Raw density readings via IIO_DENSITY channel type
> - Hardware-agnostic operation via GPIO and IIO ADC interfaces
> - Power management through regulator framework
> - Device Tree binding support
>
> Datasheet: https://global.sharp/products/device/lineup/data/pdf/datasheet/gp2y1010au_appl_e.pdf
>
> Signed-off-by: Suraj Sonawane <surajsonawane0215@gmail.com>
A few additional comments from me.
Jonathan
> obj-$(CONFIG_PMS7003) += pms7003.o
> diff --git a/drivers/iio/chemical/gp2y1010.c b/drivers/iio/chemical/gp2y1010.c
> new file mode 100644
> index 000000000..3a8657035
> --- /dev/null
> +++ b/drivers/iio/chemical/gp2y1010.c
> @@ -0,0 +1,126 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * Copyright (c) 2025 Suraj Sonawane <surajsonawane0215@gmail.com>
> + * Sharp GP2Y1010AU0F Dust Sensor Driver
> + * Datasheet: https://global.sharp/products/device/lineup/data/pdf/datasheet/gp2y1010au_appl_e.pdf
> + */
> +
> +#include <linux/delay.h>
> +#include <linux/err.h>
> +#include <linux/gpio/consumer.h>
> +#include <linux/iio/consumer.h>
> +#include <linux/iio/iio.h>
> +#include <linux/init.h>
> +#include <linux/module.h>
> +#include <linux/of.h>
> +#include <linux/platform_device.h>
> +#include <linux/regulator/consumer.h>
> +
> +/* Timings based on GP2Y1010AU0F datasheet Section 6-1 */
> +#define GP2Y1010_LED_PULSE_US 320 /* Total LED ON time (0.32 ms) */
> +#define GP2Y1010_SAMPLE_DELAY_US 280 /* ADC sampling after LED ON (0.28 ms) */
> +
> +struct gp2y1010_data {
> + struct gpio_desc *led_gpio;
> + struct iio_channel *adc_chan;
> + int v_clean; /* Calibration: voltage in clean air (mV) */
> +};
> +
> +static int gp2y1010_read_raw(struct iio_dev *indio_dev,
> + struct iio_chan_spec const *chan,
> + int *val, int *val2, long mask)
Odd alignment. Aim for under the s of struct. That is immediately after the bracket.
> +{
> + struct gp2y1010_data *data = iio_priv(indio_dev);
> + int ret, voltage_mv;
> +
> + if (mask != IIO_CHAN_INFO_RAW)
> + return -EINVAL;
> +
> + gpiod_set_value(data->led_gpio, 1);
> + udelay(GP2Y1010_SAMPLE_DELAY_US);
> +
> + ret = iio_read_channel_processed(data->adc_chan, &voltage_mv);
> +
> + /* Wait remaining time to complete 320 µs total LED pulse width */
> + udelay(GP2Y1010_LED_PULSE_US - GP2Y1010_SAMPLE_DELAY_US);
> + gpiod_set_value(data->led_gpio, 0);
> +
> + if (ret < 0)
> + return ret;
> +
> + *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_DENSITY,
> + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
> + },
Where there is only one channel, no need to bother with an array.
There are lots of drivers that do this, but I'm trying to discourage it
in new drivers.
indio_dev->channels = &gp271010_channel;
indio_dev->num_channels = 1;
is obvious enough.
> +};
> +
> +static int gp2y1010_probe(struct platform_device *pdev)
> +{
> + struct device *dev = &pdev->dev;
> + struct iio_dev *indio_dev;
> + struct gp2y1010_data *data;
> + enum iio_chan_type ch_type;
> + int ret;
> +
> + indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
> + if (!indio_dev)
> + return -ENOMEM;
> +
> + data = iio_priv(indio_dev);
> + data->v_clean = 900;
> +
> + data->led_gpio = devm_gpiod_get(dev, "led", GPIOD_OUT_LOW);
> + if (IS_ERR(data->led_gpio))
> + return dev_err_probe(dev, PTR_ERR(data->led_gpio), "Failed to get LED GPIO\n");
> +
> + ret = devm_regulator_get_enable(dev, "vdd");
> + if (ret)
> + return ret;
> + udelay(100);
> +
> + data->adc_chan = devm_iio_channel_get(dev, "dust");
> + if (IS_ERR(data->adc_chan))
> + return dev_err_probe(dev, PTR_ERR(data->adc_chan), "Failed to get ADC channel\n");
> +
> + ret = iio_get_channel_type(data->adc_chan, &ch_type);
> + if (ret < 0)
> + return ret;
> + if (ch_type != IIO_DENSITY)
> + return dev_err_probe(dev, -EINVAL, "ADC channel is not density type\n");
I'm confused. The ADC channel type providing us a measurement service (as here we
are the consumer) is going to be voltage (or maybe current) because ADCs don't measure dust.
This driver has to do the conversion from voltage to the value needed to report a density channel.
> +
> + indio_dev->name = dev_name(dev);
Hard code the part number here rather than getting it from elsewhere. That makes
it much easier to see that this is what we expect.
> + indio_dev->info = &gp2y1010_info;
> + indio_dev->channels = gp2y1010_channels;
> + indio_dev->num_channels = ARRAY_SIZE(gp2y1010_channels);
> + indio_dev->modes = INDIO_DIRECT_MODE;
> +
> + return devm_iio_device_register(dev, indio_dev);
> +}
^ permalink raw reply [flat|nested] 11+ messages in thread
end of thread, other threads:[~2025-06-14 12:34 UTC | newest]
Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-06-12 10:07 [PATCH v2 0/3] iio: chemical: Add Sharp GP2Y1010AU0F dust sensor support surajsonawane0215
2025-06-12 10:07 ` [PATCH v2 1/3] iio: Add IIO_DENSITY channel type surajsonawane0215
2025-06-12 12:58 ` Andy Shevchenko
2025-06-12 16:23 ` David Lechner
2025-06-12 10:07 ` [PATCH v2 2/3] dt-bindings: iio: chemical: Add sharp,gp2y1010au0f surajsonawane0215
2025-06-12 10:23 ` Krzysztof Kozlowski
2025-06-12 16:37 ` David Lechner
2025-06-12 10:07 ` [PATCH v2 3/3] iio: chemical: Add driver for Sharp GP2Y1010AU0F surajsonawane0215
2025-06-12 13:15 ` Andy Shevchenko
2025-06-12 18:24 ` David Lechner
2025-06-14 12:33 ` Jonathan Cameron
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).