Skip to contentSkip to conversation

iio: light: gp2ap020a00f: Add VCC supply and GP2AP030A00F support

PATCH In review Linus Walleij 5 participants 14 messages Nov 8 – Nov 14

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

v2 cover iio: light: gp2ap020a00f: Add VCC supply and GP2AP030A00F support

Describe and enable the optional VCC supply for the GP2AP020A00F and GP2AP030A00F. Preserve compatibility with existing device trees that only describe the LED supply.

Extend the existing binding and driver to support the GP2AP030A00F. It shares the register layout, but has different proximity resolution encodings and no automatic lux calculation. Select model-specific initialization, keep reserved bits clear and expose fixed-range raw clear and infrared counts. Keep generic supply handling in separate patches from the model-specific binding and driver additions.

Assisted by LLM.

---
Changes in v2:
- Replace vdd/vio with one optional vcc supply, matching the VCC pin.
- Use devm_regulator_get_enable() for VCC, allowing dummy regulators.
- Move the VLED description into the generic supply binding patch.
- Split variant infrastructure from GP2AP030A00F support.
- Use "GP2AP020A00F and similar" in family descriptions.
- Use reverse Christmas tree ordering for local declarations.
- Explicitly select MANUAL_CALC when automatic ALS calculation is absent.
- Reject missing chip match data before accessing the device.
- Use fsleep() for the GP2AP030A00F startup delay.
- Select static channel arrays and counts through chip_info.
- Link to v1: https://patch.msgid.link/[email protected]

---
Linus Walleij (5):
dt-bindings: iio: light: sharp,gp2ap020a00f: Add VCC supply
dt-bindings: iio: light: sharp,gp2ap020a00f: Add GP2AP030A00F
iio: light: gp2ap020a00f: Enable the VCC supply
iio: light: gp2ap020a00f: Prepare for chip variants
iio: light: gp2ap020a00f: Add GP2AP030A00F support

 .../bindings/iio/light/sharp,gp2ap020a00f.yaml     |  17 ++-
 drivers/iio/light/Kconfig                          |   6 +-
 drivers/iio/light/gp2ap020a00f.c                   | 136 +++++++++++++++++++--
 3 files changed, 143 insertions(+), 16 deletions(-)

---
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
change-id: 20261107-gp2ap030-99504c2dc038

Best regards,

Signature · 1 line
Linus Walleij <[email protected]>
#1 lore raw

PATCH v2 1/5 dt-bindings: iio: light: sharp,gp2ap020a00f: Add VCC supply

Describe the sensor core supply as vcc-supply, following the VCC pin name in the GP2AP030A00F datasheet. VI2C denotes the I2C and interrupt pull-up voltage, not a separate sensor supply pin.

Keep the property optional to preserve compatibility with existing device trees that only describe vled-supply.

Document the existing LED supply as the infrared LED anode supply (LEDA).

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

---
 Documentation/devicetree/bindings/iio/light/sharp,gp2ap020a00f.yaml | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
Documentation/devicetree/bindings/iio/light/sharp,gp2ap020a00f.yaml +5 -1
index 3fabf1f576cf..afba0b005b34 100644
@@ -22,7 +22,11 @@ properties:
   interrupts:
     maxItems: 1
 
-  vled-supply: true
+  vcc-supply:
+    description: Sensor core power supply (VCC).
+
+  vled-supply:
+    description: Infrared LED anode power supply (LEDA).
 
 additionalProperties: false
 
Signature · 1 line
2.56.0
Acked-by on v2 1/5
#2 lore raw

Re: [PATCH v2 1/5] dt-bindings: iio: light: sharp,gp2ap020a00f: Add VCC supply

Quoted 10 lines · Linus Walleij

Describe the sensor core supply as vcc-supply, following the VCC pin name in the GP2AP030A00F datasheet. VI2C denotes the I2C and interrupt pull-up voltage, not a separate sensor supply pin.

Keep the property optional to preserve compatibility with existing device trees that only describe vled-supply.

Document the existing LED supply as the infrared LED anode supply (LEDA).

Quoted 6 lines

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

---
 Documentation/devicetree/bindings/iio/light/sharp,gp2ap020a00f.yaml | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

Best regards,
Krzysztof

#3 lore raw

PATCH v2 2/5 dt-bindings: iio: light: sharp,gp2ap020a00f: Add GP2AP030A00F

In reply to Linus Walleij

The GP2AP030A00F uses the same register address layout and interrupt interface as the GP2AP020A00F, but returns raw clear and infrared counts instead of providing automatic illuminance calculation. Describe it in the existing binding with its own compatible.

Datasheet: https://global.sharp/products/device/lineup/data/pdf/datasheet/gp2ap030a00f_e.pdf
Datasheet: https://jp.sharp/products/device/doc/opto/gp2ap030a00f_appl_e.pdf

---
 .../devicetree/bindings/iio/light/sharp,gp2ap020a00f.yaml     | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
Documentation/devicetree/bindings/iio/light/sharp,gp2ap020a00f.yaml +8 -3
index afba0b005b34..f4a262dbc956 100644
@@ -4,17 +4,22 @@
 $id: http://devicetree.org/schemas/iio/light/sharp,gp2ap020a00f.yaml#
 $schema: http://devicetree.org/meta-schemas/core.yaml#
 
-title: Sharp GP2AP020A00F I2C Proximity/ALS sensor
+title: Sharp GP2AP020A00F and similar proximity and ambient light sensors
 
 maintainers:
   - Kyungmin Park <[email protected]>
 
 description: |
-  The proximity detector sensor requires power supply for its built-in led.
+  The sensors provide digital clear, infrared and proximity measurements.
+  The GP2AP020A00F can also calculate illuminance internally, while the
+  GP2AP030A00F exposes raw clear and infrared photodiode counts.
+  The proximity detector requires a power supply for its built-in LED.
 
 properties:
   compatible:
-    const: sharp,gp2ap020a00f
+    enum:
+      - sharp,gp2ap020a00f
+      - sharp,gp2ap030a00f
 
   reg:
     maxItems: 1
Signature · 1 line
2.56.0
Reviewed-by on v2 2/5
#4 lore raw

Re: [PATCH v2 2/5] dt-bindings: iio: light: sharp,gp2ap020a00f: Add GP2AP030A00F

Quoted 11 lines · Linus Walleij

The GP2AP030A00F uses the same register address layout and interrupt interface as the GP2AP020A00F, but returns raw clear and infrared counts instead of providing automatic illuminance calculation. Describe it in the existing binding with its own compatible.

Datasheet: https://global.sharp/products/device/lineup/data/pdf/datasheet/gp2ap030a00f_e.pdf
Datasheet: https://jp.sharp/products/device/doc/opto/gp2ap030a00f_appl_e.pdf

---
 .../devicetree/bindings/iio/light/sharp,gp2ap020a00f.yaml     | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

Best regards,
Krzysztof

#5 lore raw

PATCH v2 3/5 iio: light: gp2ap020a00f: Enable the VCC supply

In reply to Linus Walleij

Enable the VCC supply before accessing the device. Use the managed helper so it is disabled on probe failure and removal. Let the regulator framework provide a dummy regulator for an unspecified supply, and propagate errors, including deferred probe.

---
 drivers/iio/light/gp2ap020a00f.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/iio/light/gp2ap020a00f.c +4 -0
index b435dc5e8706..dc07d137f084 100644
@@ -1414,6 +1414,10 @@ static int gp2ap020a00f_probe(struct i2c_client *client)
 
 	data = iio_priv(indio_dev);
 
+	err = devm_regulator_get_enable(dev, "vcc");
+	if (err)
+		return dev_err_probe(dev, err, "Failed to enable VCC supply\n");
+
 	data->vled_reg = devm_regulator_get(dev, "vled");
 	if (IS_ERR(data->vled_reg))
 		return PTR_ERR(data->vled_reg);
Signature · 1 line
2.56.0
Reviewed-by on v2 3/5
#6 lore raw

Re: [PATCH v2 3/5] iio: light: gp2ap020a00f: Enable the VCC supply

Quoted 7 lines · On Sun, 08 Nov 2026 22:44:01 +0100 Linus Walleij <[email protected]> wrote:

Enable the VCC supply before accessing the device. Use the managed helper so it is disabled on probe failure and removal. Let the regulator framework provide a dummy regulator for an unspecified supply, and propagate errors, including deferred probe.

---

Signature · 2 lines
Kind regards,
Joshua Crofts
#7 lore raw

PATCH v2 4/5 iio: light: gp2ap020a00f: Prepare for chip variants

In reply to Linus Walleij

Introduce chip data for the device name, initialization table and channel layout, including the channel count. Use I2C/OF match data to select it during probe, preparing for sensors with shared register layouts and different initialization settings.

Populate only the GP2AP020A00F data, keeping its register values, channels and operating behavior unchanged.

---
 drivers/iio/light/gp2ap020a00f.c | 32 +++++++++++++++++++++++++-------
 1 file changed, 25 insertions(+), 7 deletions(-)
drivers/iio/light/gp2ap020a00f.c +25 -7
index dc07d137f084..5464311515c7 100644
@@ -234,7 +234,15 @@ enum gp2ap020a00f_thresh_val_id {
 	GP2AP020A00F_THRESH_PH,
 };
 
+struct gp2ap020a00f_chip_info {
+	const char *name;
+	const u8 *reg_init_tab;
+	const struct iio_chan_spec *channels;
+	unsigned int num_channels;
+};
+
 struct gp2ap020a00f_data {
+	const struct gp2ap020a00f_chip_info *chip_info;
 	struct i2c_client *client;
 	struct mutex lock;
 	char *buffer;
@@ -1309,6 +1317,13 @@ static const struct iio_chan_spec gp2ap020a00f_channels[] = {
 	IIO_CHAN_SOFT_TIMESTAMP(GP2AP020A00F_CHAN_TIMESTAMP),
 };
 
+static const struct gp2ap020a00f_chip_info gp2ap020a00f_chip_info = {
+	.name = "gp2ap020a00f",
+	.reg_init_tab = gp2ap020a00f_reg_init_tab,
+	.channels = gp2ap020a00f_channels,
+	.num_channels = ARRAY_SIZE(gp2ap020a00f_channels),
+};
+
 static const struct iio_info gp2ap020a00f_info = {
 	.read_raw = &gp2ap020a00f_read_raw,
 	.read_event_value = &gp2ap020a00f_read_event_val,
@@ -1401,7 +1416,6 @@ static const struct iio_buffer_setup_ops gp2ap020a00f_buffer_setup_ops = {
 
 static int gp2ap020a00f_probe(struct i2c_client *client)
 {
-	const struct i2c_device_id *id = i2c_client_get_device_id(client);
 	struct device *dev = &client->dev;
 	struct gp2ap020a00f_data *data;
 	struct iio_dev *indio_dev;
@@ -1413,6 +1427,9 @@ static int gp2ap020a00f_probe(struct i2c_client *client)
 		return -ENOMEM;
 
 	data = iio_priv(indio_dev);
+	data->chip_info = i2c_get_match_data(client);
+	if (!data->chip_info)
+		return -EINVAL;
 
 	err = devm_regulator_get_enable(dev, "vcc");
 	if (err)
@@ -1435,7 +1452,7 @@ static int gp2ap020a00f_probe(struct i2c_client *client)
 
 	/* Initialize device registers */
 	err = regmap_bulk_write(regmap, GP2AP020A00F_OP_REG,
-			gp2ap020a00f_reg_init_tab,
+			data->chip_info->reg_init_tab,
 			ARRAY_SIZE(gp2ap020a00f_reg_init_tab));
 
 	if (err < 0) {
@@ -1451,10 +1468,10 @@ static int gp2ap020a00f_probe(struct i2c_client *client)
 	init_waitqueue_head(&data->data_ready_queue);
 
 	mutex_init(&data->lock);
-	indio_dev->channels = gp2ap020a00f_channels;
-	indio_dev->num_channels = ARRAY_SIZE(gp2ap020a00f_channels);
+	indio_dev->channels = data->chip_info->channels;
+	indio_dev->num_channels = data->chip_info->num_channels;
 	indio_dev->info = &gp2ap020a00f_info;
-	indio_dev->name = id->name;
+	indio_dev->name = data->chip_info->name;
 	indio_dev->modes = INDIO_DIRECT_MODE;
 
 	/* Allocate buffer */
@@ -1530,13 +1547,14 @@ static void gp2ap020a00f_remove(struct i2c_client *client)
 }
 
 static const struct i2c_device_id gp2ap020a00f_id[] = {
-	{ .name = "gp2ap020a00f" },
+	{ .name = "gp2ap020a00f",
+	  .driver_data = (kernel_ulong_t)&gp2ap020a00f_chip_info },
 	{ }
 };
 MODULE_DEVICE_TABLE(i2c, gp2ap020a00f_id);
 
 static const struct of_device_id gp2ap020a00f_of_match[] = {
-	{ .compatible = "sharp,gp2ap020a00f" },
+	{ .compatible = "sharp,gp2ap020a00f", .data = &gp2ap020a00f_chip_info },
 	{ }
 };
 MODULE_DEVICE_TABLE(of, gp2ap020a00f_of_match);
Signature · 1 line
2.56.0
Reviewed-by on v2 4/5
#8 lore raw

Re: [PATCH v2 4/5] iio: light: gp2ap020a00f: Prepare for chip variants

On Sun, 08 Nov 2026 22:44:02 +0100
Linus Walleij <[email protected]> wrote:

...

drivers/iio/light/gp2ap020a00f.c static int gp2ap020a00f_probe(struct i2c_client *client) · line 1432

Quoted 13 lines

static int gp2ap020a00f_probe(struct i2c_client *client) {

drivers/iio/light/gp2ap020a00f.c +3 -1
-	const struct i2c_device_id *id = i2c_client_get_device_id(client);
 	struct device *dev = &client->dev;
 	struct gp2ap020a00f_data *data;
 	struct iio_dev *indio_dev;
@@ -1413,6 +1427,9 @@ static int gp2ap020a00f_probe(struct i2c_client *client)
 		return -ENOMEM;
 
 	data = iio_priv(indio_dev);
+	data->chip_info = i2c_get_match_data(client);
+	if (!data->chip_info)
+		return -EINVAL;

-ENODATA is better.

Otherwise LGTM.

Signature · 2 lines
Kind regards,
Joshua Crofts
#9 lore raw

Re: [PATCH v2 4/5] iio: light: gp2ap020a00f: Prepare for chip variants

In reply to Linus Walleij
Quoted 7 lines · Linus Walleij

Introduce chip data for the device name, initialization table and channel layout, including the channel count. Use I2C/OF match data to select it during probe, preparing for sensors with shared register layouts and different initialization settings.

Populate only the GP2AP020A00F data, keeping its register values, channels and operating behavior unchanged.

...

drivers/iio/light/gp2ap020a00f.c static int gp2ap020a00f_probe(struct i2c_client *client) · line 1432

Quoted 4 lines
drivers/iio/light/gp2ap020a00f.c +3 -0
 	data = iio_priv(indio_dev);
+	data->chip_info = i2c_get_match_data(client);
+	if (!data->chip_info)
+		return -EINVAL;

-ENODATA

...

drivers/iio/light/gp2ap020a00f.c static void gp2ap020a00f_remove(struct i2c_client *client) · line 1551

Quoted 4 lines

static const struct i2c_device_id gp2ap020a00f_id[] = {

drivers/iio/light/gp2ap020a00f.c +2 -1
-	{ .name = "gp2ap020a00f" },
+	{ .name = "gp2ap020a00f",
+	  .driver_data = (kernel_ulong_t)&gp2ap020a00f_chip_info },

It's unusual style, either make it 4 lines or 1 line per entry.

Quoted 2 lines

{ }
};

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#10 lore raw

PATCH v2 5/5 iio: light: gp2ap020a00f: Add GP2AP030A00F support

In reply to Linus Walleij

The GP2AP030A00F has the same register addresses and interrupt modes as the GP2AP020A00F, but does not implement its automatic illuminance calculation or selectable ambient-light cancellation.

Datasheet: https://jp.sharp/products/device/doc/opto/gp2ap030a00f_appl_e.pdf

---
 drivers/iio/light/Kconfig        |   6 +--
 drivers/iio/light/gp2ap020a00f.c | 100 ++++++++++++++++++++++++++++++++++++++-
 2 files changed, 101 insertions(+), 5 deletions(-)
drivers/iio/light/Kconfig +3 -3
index 462fff1ddef2..89fe003371b8 100644
@@ -259,15 +259,15 @@ config GP2AP002
 	  module will be called gp2ap002.
 
 config GP2AP020A00F
-	tristate "Sharp GP2AP020A00F Proximity/ALS sensor"
+	tristate "Sharp GP2AP020A00F and similar Proximity/ALS sensors"
 	depends on I2C
 	select REGMAP_I2C
 	select IIO_BUFFER
 	select IIO_TRIGGERED_BUFFER
 	select IRQ_WORK
 	help
-	  Say Y here if you have a Sharp GP2AP020A00F proximity/ALS combo-chip
-	  hooked to an I2C bus.
+	  Say Y here to enable support for the Sharp GP2AP020A00F and similar
+	  proximity/ALS combo-chips hooked to an I2C bus.
 
 	  To compile this driver as a module, choose M here: the
 	  module will be called gp2ap020a00f.
drivers/iio/light/gp2ap020a00f.c +98 -2

192 lines not on this page · raw message

Signature · 1 line
2.56.0
Reviewed-by on v2 5/5
#11 lore raw

Re: [PATCH v2 5/5] iio: light: gp2ap020a00f: Add GP2AP030A00F support

On Sun, 08 Nov 2026 22:44:03 +0100
Linus Walleij <[email protected]> wrote:

...

drivers/iio/light/gp2ap020a00f.c static int gp2ap020a00f_probe(struct i2c_client *client) · line 1544

Quoted 7 lines

static const struct iio_info gp2ap020a00f_info = {

drivers/iio/light/gp2ap020a00f.c +2 -0
@@ -1450,6 +1540,9 @@ static int gp2ap020a00f_probe(struct i2c_client *client)
 		goto error_regulator_disable;
 	}
 
+	if (!data->chip_info->has_als_auto)
+		fsleep(1000);

Maybe a comment on why 1000 specifically (datasheet reference etc.).

Signature · 2 lines
Kind regards,
Joshua Crofts
#12 lore raw

Re: [PATCH v2 5/5] iio: light: gp2ap020a00f: Add GP2AP030A00F support

In reply to Linus Walleij
Quoted 3 lines · Linus Walleij

The GP2AP030A00F has the same register addresses and interrupt modes as the GP2AP020A00F, but does not implement its automatic illuminance calculation or selectable ambient-light cancellation.

...

drivers/iio/light/Kconfig config GP2AP002 · line 262

Quoted 3 lines

config GP2AP020A00F

drivers/iio/light/Kconfig +1 -1
-	tristate "Sharp GP2AP020A00F Proximity/ALS sensor"
+	tristate "Sharp GP2AP020A00F and similar Proximity/ALS sensors"

Also make sure the text in MODULE_DESCRIPTION() and perhaps top comment in the code matches the above (ideally be the same).

drivers/iio/light/Kconfig config GP2AP002 · line 270

Quoted 10 lines
drivers/iio/light/Kconfig +2 -2
 	depends on I2C
 	select REGMAP_I2C
 	select IIO_BUFFER
 	select IIO_TRIGGERED_BUFFER
 	select IRQ_WORK
 	help
-	  Say Y here if you have a Sharp GP2AP020A00F proximity/ALS combo-chip
-	  hooked to an I2C bus.
+	  Say Y here to enable support for the Sharp GP2AP020A00F and similar
+	  proximity/ALS combo-chips hooked to an I2C bus.

Here we actually want more precise list of supported part numbers.

...

/* ...a comment to explain the delay... */

drivers/iio/light/gp2ap020a00f.c static int gp2ap020a00f_probe(struct i2c_client *client) · line 1544

Quoted 2 lines
drivers/iio/light/gp2ap020a00f.c +2 -0
+	if (!data->chip_info->has_als_auto)
+		fsleep(1000);

1 * USEC_PER_MSEC

...

drivers/iio/light/gp2ap020a00f.c static void gp2ap020a00f_remove(struct i2c_client *client) · line 1646

Quoted 2 lines
drivers/iio/light/gp2ap020a00f.c +2 -0
+	{ .name = "gp2ap030a00f",
+	  .driver_data = (kernel_ulong_t)&gp2ap030a00f_chip_info },

Same comment about the chosen style.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#13 lore raw

Re: [PATCH v2 5/5] iio: light: gp2ap020a00f: Add GP2AP030A00F support

In reply to Linus Walleij

drivers/iio/light/gp2ap020a00f.c static const struct iio_chan_spec gp2ap020a00f_channels[] = { · line 1359

Quoted 12 lines
drivers/iio/light/gp2ap020a00f.c +10 -0
+static const struct iio_chan_spec gp2ap030a00f_channels[] = {
+	{
+		.type = IIO_LIGHT,
+		.channel2 = IIO_MOD_LIGHT_CLEAR,
+		.modified = 1,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
+		.scan_type = {
+			.sign = 'u',
+			.realbits = 16,
+			.shift = 0,

Shift = 0 is fine to consider as an 'obvious' default.

Though does make me wonder, why are these 16 bit values being stored in 32 bit space with no shifts?

Upshot of taking a look. I'm not sure they are are 16 bit unless the value prior to the potential * 16 has top few bits empty.

If it does then the dance of putting this in a 32 bit store makes limited sense.

Please take a look the data with and without
GP2AP020A00F_FLAG_LUX_MODE_HI being set.

Quoted 8 lines
drivers/iio/light/gp2ap020a00f.c +8 -0
+			.storagebits = 32,
+			.endianness = IIO_LE,
+		},
+		.scan_index = GP2AP020A00F_SCAN_MODE_LIGHT_CLEAR,
+		.address = GP2AP020A00F_D0_L_REG,
+		.event_spec = gp2ap020a00f_event_spec_light,
+		.num_event_specs = ARRAY_SIZE(gp2ap020a00f_event_spec_light),
+	},