* [PATCH v1] i2c: Use named initializers for arrays of i2c_device_data
@ 2026-05-18 16:45 Uwe Kleine-König (The Capable Hub)
2026-05-18 16:50 ` Uwe Kleine-König (The Capable Hub)
2026-05-19 8:55 ` Wolfram Sang
0 siblings, 2 replies; 3+ messages in thread
From: Uwe Kleine-König (The Capable Hub) @ 2026-05-18 16:45 UTC (permalink / raw)
To: Wolfram Sang, Peter Rosin; +Cc: Michael Hennerich, linux-i2c, linux-kernel
While being less compact, using named initializers allows to more easily
see which members of the structs are assigned which value without having
to lookup the declaration of the struct. And it's also more robust
against changes to the struct definition.
The mentioned robustness is relevant for a planned change to struct
i2c_device_id that replaces .driver_data by an anonymous union.
While touching all these arrays, unify usage of whitespace in the list
terminator.
This patch doesn't modify the compiled arrays, only their representation
in source form benefits. The former was confirmed with x86 and arm64
builds.
Signed-off-by: Uwe Kleine-König (The Capable Hub) <u.kleine-koenig@baylibre.com>
Forwarded: id:20260518134035.644762-2-u.kleine-koenig@baylibre.com
---
Hello,
the mentioned change to i2c_device_id is the following:
diff --git a/include/linux/mod_devicetable.h b/include/linux/mod_devicetable.h
index 23ff24080dfd..aebd3a5e90af 100644
--- a/include/linux/mod_devicetable.h
+++ b/include/linux/mod_devicetable.h
@@ -477,7 +477,11 @@ struct rpmsg_device_id {
struct i2c_device_id {
char name[I2C_NAME_SIZE];
- kernel_ulong_t driver_data; /* Data private to the driver */
+ union {
+ /* Data private to the driver */
+ kernel_ulong_t driver_data;
+ const void *driver_data_ptr;
+ };
};
/* pci_epf */
and this requires that .driver_data is assigned via a named initializer
for static data. This requirement isn't a bad one because named
initializers are also much better readable than list initializers.
The union added to struct i2c_device_id enables further cleanups like:
diff --git a/drivers/regulator/ad5398.c b/drivers/regulator/ad5398.c
index 0123ca8157a8..dfb0b07500a7 100644
--- a/drivers/regulator/ad5398.c
+++ b/drivers/regulator/ad5398.c
@@ -207,8 +207,8 @@ struct ad5398_current_data_format {
static const struct ad5398_current_data_format df_10_4_120 = {10, 4, 0, 120000};
static const struct i2c_device_id ad5398_id[] = {
- { .name = "ad5398", .driver_data = (kernel_ulong_t)&df_10_4_120 },
- { .name = "ad5821", .driver_data = (kernel_ulong_t)&df_10_4_120 },
+ { .name = "ad5398", .driver_data_ptr = &df_10_4_120 },
+ { .name = "ad5821", .driver_data_ptr = &df_10_4_120 },
{ }
};
MODULE_DEVICE_TABLE(i2c, ad5398_id);
@@ -219,8 +219,7 @@ static int ad5398_probe(struct i2c_client *client)
struct regulator_init_data *init_data = dev_get_platdata(&client->dev);
struct regulator_config config = { };
struct ad5398_chip_info *chip;
- const struct ad5398_current_data_format *df =
- (struct ad5398_current_data_format *)id->driver_data;
+ const struct ad5398_current_data_format *df = id->driver_data;
chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
if (!chip)
that are an improvement for readability (again!) and it keeps some
properties of the pointers (here: being const) without having to pay
attention for that. (I didn't find an i2c driver that benefits, so this
is "only" a regulator driver example.)
My additional motivation for this effort is CHERI[1]. This is a hardware
extension that uses 128 bit pointers but unsigned long is still 64 bit.
So with CHERI you cannot store pointers in unsigned long variables.
Best regards
Uwe
[1] https://cheri-alliance.org/discover-cheri/
https://lwn.net/Articles/1037974/
drivers/i2c/i2c-core-base.c | 4 ++--
drivers/i2c/i2c-slave-eeprom.c | 16 ++++++-------
drivers/i2c/i2c-slave-testunit.c | 2 +-
drivers/i2c/i2c-smbus.c | 2 +-
drivers/i2c/muxes/i2c-mux-ltc4306.c | 4 ++--
drivers/i2c/muxes/i2c-mux-pca9541.c | 4 ++--
drivers/i2c/muxes/i2c-mux-pca954x.c | 36 ++++++++++++++---------------
7 files changed, 34 insertions(+), 34 deletions(-)
diff --git a/drivers/i2c/i2c-core-base.c b/drivers/i2c/i2c-core-base.c
index 9c46147e3506..7e4b7adffd6e 100644
--- a/drivers/i2c/i2c-core-base.c
+++ b/drivers/i2c/i2c-core-base.c
@@ -1107,8 +1107,8 @@ EXPORT_SYMBOL(i2c_find_device_by_fwnode);
static const struct i2c_device_id dummy_id[] = {
- { "dummy", },
- { "smbus_host_notify", },
+ { .name = "dummy" },
+ { .name = "smbus_host_notify" },
{ }
};
diff --git a/drivers/i2c/i2c-slave-eeprom.c b/drivers/i2c/i2c-slave-eeprom.c
index 6bc2ef650a74..226d722af662 100644
--- a/drivers/i2c/i2c-slave-eeprom.c
+++ b/drivers/i2c/i2c-slave-eeprom.c
@@ -191,14 +191,14 @@ static void i2c_slave_eeprom_remove(struct i2c_client *client)
}
static const struct i2c_device_id i2c_slave_eeprom_id[] = {
- { "slave-24c02", I2C_SLAVE_DEVICE_MAGIC(2048 / 8, 0) },
- { "slave-24c02ro", I2C_SLAVE_DEVICE_MAGIC(2048 / 8, I2C_SLAVE_FLAG_RO) },
- { "slave-24c32", I2C_SLAVE_DEVICE_MAGIC(32768 / 8, I2C_SLAVE_FLAG_ADDR16) },
- { "slave-24c32ro", I2C_SLAVE_DEVICE_MAGIC(32768 / 8, I2C_SLAVE_FLAG_ADDR16 | I2C_SLAVE_FLAG_RO) },
- { "slave-24c64", I2C_SLAVE_DEVICE_MAGIC(65536 / 8, I2C_SLAVE_FLAG_ADDR16) },
- { "slave-24c64ro", I2C_SLAVE_DEVICE_MAGIC(65536 / 8, I2C_SLAVE_FLAG_ADDR16 | I2C_SLAVE_FLAG_RO) },
- { "slave-24c512", I2C_SLAVE_DEVICE_MAGIC(524288 / 8, I2C_SLAVE_FLAG_ADDR16) },
- { "slave-24c512ro", I2C_SLAVE_DEVICE_MAGIC(524288 / 8, I2C_SLAVE_FLAG_ADDR16 | I2C_SLAVE_FLAG_RO) },
+ { .name = "slave-24c02", .driver_data = I2C_SLAVE_DEVICE_MAGIC(2048 / 8, 0) },
+ { .name = "slave-24c02ro", .driver_data = I2C_SLAVE_DEVICE_MAGIC(2048 / 8, I2C_SLAVE_FLAG_RO) },
+ { .name = "slave-24c32", .driver_data = I2C_SLAVE_DEVICE_MAGIC(32768 / 8, I2C_SLAVE_FLAG_ADDR16) },
+ { .name = "slave-24c32ro", .driver_data = I2C_SLAVE_DEVICE_MAGIC(32768 / 8, I2C_SLAVE_FLAG_ADDR16 | I2C_SLAVE_FLAG_RO) },
+ { .name = "slave-24c64", .driver_data = I2C_SLAVE_DEVICE_MAGIC(65536 / 8, I2C_SLAVE_FLAG_ADDR16) },
+ { .name = "slave-24c64ro", .driver_data = I2C_SLAVE_DEVICE_MAGIC(65536 / 8, I2C_SLAVE_FLAG_ADDR16 | I2C_SLAVE_FLAG_RO) },
+ { .name = "slave-24c512", .driver_data = I2C_SLAVE_DEVICE_MAGIC(524288 / 8, I2C_SLAVE_FLAG_ADDR16) },
+ { .name = "slave-24c512ro", .driver_data = I2C_SLAVE_DEVICE_MAGIC(524288 / 8, I2C_SLAVE_FLAG_ADDR16 | I2C_SLAVE_FLAG_RO) },
{ }
};
MODULE_DEVICE_TABLE(i2c, i2c_slave_eeprom_id);
diff --git a/drivers/i2c/i2c-slave-testunit.c b/drivers/i2c/i2c-slave-testunit.c
index 6de4307050dd..68a07d957113 100644
--- a/drivers/i2c/i2c-slave-testunit.c
+++ b/drivers/i2c/i2c-slave-testunit.c
@@ -270,7 +270,7 @@ static void i2c_slave_testunit_remove(struct i2c_client *client)
}
static const struct i2c_device_id i2c_slave_testunit_id[] = {
- { "slave-testunit" },
+ { .name = "slave-testunit" },
{ }
};
MODULE_DEVICE_TABLE(i2c, i2c_slave_testunit_id);
diff --git a/drivers/i2c/i2c-smbus.c b/drivers/i2c/i2c-smbus.c
index bc7bd55e6370..069f08f68bc1 100644
--- a/drivers/i2c/i2c-smbus.c
+++ b/drivers/i2c/i2c-smbus.c
@@ -220,7 +220,7 @@ static void smbalert_remove(struct i2c_client *ara)
}
static const struct i2c_device_id smbalert_ids[] = {
- { "smbus_alert" },
+ { .name = "smbus_alert" },
{ /* LIST END */ }
};
MODULE_DEVICE_TABLE(i2c, smbalert_ids);
diff --git a/drivers/i2c/muxes/i2c-mux-ltc4306.c b/drivers/i2c/muxes/i2c-mux-ltc4306.c
index 50fbc0d06e62..18228f5b80e8 100644
--- a/drivers/i2c/muxes/i2c-mux-ltc4306.c
+++ b/drivers/i2c/muxes/i2c-mux-ltc4306.c
@@ -191,8 +191,8 @@ static int ltc4306_deselect_mux(struct i2c_mux_core *muxc, u32 chan)
}
static const struct i2c_device_id ltc4306_id[] = {
- { "ltc4305", ltc_4305 },
- { "ltc4306", ltc_4306 },
+ { .name = "ltc4305", .driver_data = ltc_4305 },
+ { .name = "ltc4306", .driver_data = ltc_4306 },
{ }
};
MODULE_DEVICE_TABLE(i2c, ltc4306_id);
diff --git a/drivers/i2c/muxes/i2c-mux-pca9541.c b/drivers/i2c/muxes/i2c-mux-pca9541.c
index 3d8002caf703..9a59129bc50f 100644
--- a/drivers/i2c/muxes/i2c-mux-pca9541.c
+++ b/drivers/i2c/muxes/i2c-mux-pca9541.c
@@ -74,8 +74,8 @@ struct pca9541 {
};
static const struct i2c_device_id pca9541_id[] = {
- { "pca9541" },
- {}
+ { .name = "pca9541" },
+ { }
};
MODULE_DEVICE_TABLE(i2c, pca9541_id);
diff --git a/drivers/i2c/muxes/i2c-mux-pca954x.c b/drivers/i2c/muxes/i2c-mux-pca954x.c
index b9f370c9f018..8fca709ed279 100644
--- a/drivers/i2c/muxes/i2c-mux-pca954x.c
+++ b/drivers/i2c/muxes/i2c-mux-pca954x.c
@@ -250,24 +250,24 @@ static const struct chip_desc chips[] = {
};
static const struct i2c_device_id pca954x_id[] = {
- { "max7356", max_7356 },
- { "max7357", max_7357 },
- { "max7358", max_7358 },
- { "max7367", max_7367 },
- { "max7368", max_7368 },
- { "max7369", max_7369 },
- { "pca9540", pca_9540 },
- { "pca9542", pca_9542 },
- { "pca9543", pca_9543 },
- { "pca9544", pca_9544 },
- { "pca9545", pca_9545 },
- { "pca9546", pca_9546 },
- { "pca9547", pca_9547 },
- { "pca9548", pca_9548 },
- { "pca9846", pca_9846 },
- { "pca9847", pca_9847 },
- { "pca9848", pca_9848 },
- { "pca9849", pca_9849 },
+ { .name = "max7356", .driver_data = max_7356 },
+ { .name = "max7357", .driver_data = max_7357 },
+ { .name = "max7358", .driver_data = max_7358 },
+ { .name = "max7367", .driver_data = max_7367 },
+ { .name = "max7368", .driver_data = max_7368 },
+ { .name = "max7369", .driver_data = max_7369 },
+ { .name = "pca9540", .driver_data = pca_9540 },
+ { .name = "pca9542", .driver_data = pca_9542 },
+ { .name = "pca9543", .driver_data = pca_9543 },
+ { .name = "pca9544", .driver_data = pca_9544 },
+ { .name = "pca9545", .driver_data = pca_9545 },
+ { .name = "pca9546", .driver_data = pca_9546 },
+ { .name = "pca9547", .driver_data = pca_9547 },
+ { .name = "pca9548", .driver_data = pca_9548 },
+ { .name = "pca9846", .driver_data = pca_9846 },
+ { .name = "pca9847", .driver_data = pca_9847 },
+ { .name = "pca9848", .driver_data = pca_9848 },
+ { .name = "pca9849", .driver_data = pca_9849 },
{ }
};
MODULE_DEVICE_TABLE(i2c, pca954x_id);
base-commit: 254f49634ee16a731174d2ae34bc50bd5f45e731
--
2.47.3
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH v1] i2c: Use named initializers for arrays of i2c_device_data
2026-05-18 16:45 [PATCH v1] i2c: Use named initializers for arrays of i2c_device_data Uwe Kleine-König (The Capable Hub)
@ 2026-05-18 16:50 ` Uwe Kleine-König (The Capable Hub)
2026-05-19 8:55 ` Wolfram Sang
1 sibling, 0 replies; 3+ messages in thread
From: Uwe Kleine-König (The Capable Hub) @ 2026-05-18 16:50 UTC (permalink / raw)
To: Wolfram Sang, Peter Rosin; +Cc: Michael Hennerich, linux-i2c, linux-kernel
[-- Attachment #1: Type: text/plain, Size: 420 bytes --]
On Mon, May 18, 2026 at 06:45:09PM +0200, Uwe Kleine-König (The Capable Hub) wrote:
> Forwarded: id:20260518134035.644762-2-u.kleine-koenig@baylibre.com
That line wasn't supposed to be part of the commit log. (I use these to
keep track of submitted patches, but this is from the similar tpm patch
that I used as a template for this one 🤦.)
So if you apply this patch, please drop this line.
Thanks
Uwe
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH v1] i2c: Use named initializers for arrays of i2c_device_data
2026-05-18 16:45 [PATCH v1] i2c: Use named initializers for arrays of i2c_device_data Uwe Kleine-König (The Capable Hub)
2026-05-18 16:50 ` Uwe Kleine-König (The Capable Hub)
@ 2026-05-19 8:55 ` Wolfram Sang
1 sibling, 0 replies; 3+ messages in thread
From: Wolfram Sang @ 2026-05-19 8:55 UTC (permalink / raw)
To: Uwe Kleine-König (The Capable Hub), Andi Shyti
Cc: Peter Rosin, Michael Hennerich, linux-i2c, linux-kernel
[-- Attachment #1: Type: text/plain, Size: 1047 bytes --]
On Mon, May 18, 2026 at 06:45:09PM +0200, Uwe Kleine-König (The Capable Hub) wrote:
> While being less compact, using named initializers allows to more easily
> see which members of the structs are assigned which value without having
> to lookup the declaration of the struct. And it's also more robust
> against changes to the struct definition.
>
> The mentioned robustness is relevant for a planned change to struct
> i2c_device_id that replaces .driver_data by an anonymous union.
>
> While touching all these arrays, unify usage of whitespace in the list
> terminator.
>
> This patch doesn't modify the compiled arrays, only their representation
> in source form benefits. The former was confirmed with x86 and arm64
> builds.
>
> Signed-off-by: Uwe Kleine-König (The Capable Hub) <u.kleine-koenig@baylibre.com>
> Forwarded: id:20260518134035.644762-2-u.kleine-koenig@baylibre.com
Reviewed-by: Wolfram Sang <wsa+renesas@sang-engineering.com>
Andi, you might want to apply it early to simplify merge conflicts.
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-05-19 8:55 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-05-18 16:45 [PATCH v1] i2c: Use named initializers for arrays of i2c_device_data Uwe Kleine-König (The Capable Hub)
2026-05-18 16:50 ` Uwe Kleine-König (The Capable Hub)
2026-05-19 8:55 ` Wolfram Sang
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox