i2c: core: add devm_i2c_new_ancillary_device()
Subsystems (2)
cover i2c: core: add devm_i2c_new_ancillary_device()
Add a device-managed version of i2c_new_ancillary_device().
I have an own use case, but was surprised to find it on an old wish list.
https://elinux.org/images/b/b3/I2c_21st-ELCE-2019-Sang.pdf
Heiner Kallweit (2):
i2c: core: add devm_i2c_new_ancillary_device()
i2c: core: extend use case for dflt addr in i2c_new_ancillary_device
drivers/i2c/i2c-core-base.c | 50 ++++++++++++++++++++++++++++--------- include/linux/i2c.h | 5 ++++ 2 files changed, 43 insertions(+), 12 deletions(-)
Signature · 1 line
2.39.1
PATCH 1/2 i2c: core: add devm_i2c_new_ancillary_device()
This adds a device-managed version of i2c_new_ancillary_device(). Create a helper i2c_prepare_ancillary_device() to avoid code duplication.
--- drivers/i2c/i2c-core-base.c | 43 ++++++++++++++++++++++++++----------- include/linux/i2c.h | 5 +++++ 2 files changed, 36 insertions(+), 12 deletions(-)
drivers/i2c/i2c-core-base.c +31 -12
@@ -1123,6 +1123,24 @@ struct i2c_client *devm_i2c_new_dummy_device(struct device *dev, }EXPORT_SYMBOL_GPL (devm_i2c_new_dummy_device); +static u32 i2c_prepare_ancillary_device (struct i2c_client *client, +const char *name,u16 default_addr) +{ +struct device_node *np = client->dev.of_node; +u32 addr = default_addr; +int i; + +if (np) { + i =of_property_match_string (np,"reg-names" , name); +if (i >=0 ) +of_property_read_u32_index (np,"reg" , i, &addr); + } + +dev_dbg (&client->adapter->dev,"Address for %s : %#x\n" , name, addr); + +return addr; +} +/** * i2c_new_ancillary_device - Helper to get the instantiated secondary address * and create the associated device
@@ -1146,24 +1164,25 @@ EXPORT_SYMBOL_GPL(devm_i2c_new_dummy_device);* i2c_unregister_device(); or an ERR_PTR to describe the error. */ struct i2c_client *i2c_new_ancillary_device (struct i2c_client *client, -const char *name, -u16 default_addr) +const char *name, +u16 default_addr) { -struct device_node *np = client->dev.of_node; -u32 addr = default_addr; -int i; - -if (np) { - i =of_property_match_string (np,"reg-names" , name); -if (i >=0 ) -of_property_read_u32_index (np,"reg" , i, &addr); - } +u32 addr =i2c_prepare_ancillary_device (client, name, default_addr); -dev_dbg (&client->adapter->dev,"Address for %s : 0x%x\n" , name, addr);return i2c_new_dummy_device (client->adapter, addr); }EXPORT_SYMBOL_GPL (i2c_new_ancillary_device); +struct i2c_client *devm_i2c_new_ancillary_device (struct i2c_client *client, +const char *name, +u16 default_addr) +{ +u32 addr =i2c_prepare_ancillary_device (client, name, default_addr); + +return devm_i2c_new_dummy_device (&client->dev, client->adapter, addr); +} +EXPORT_SYMBOL_GPL (devm_i2c_new_ancillary_device); +/* ------------------------------------------------------------------------- */ /* I2C bus adapters -- one roots each I2C or SMBUS segment */
include/linux/i2c.h +5 -0
@@ -487,6 +487,11 @@ i2c_new_ancillary_device(struct i2c_client *client,const char *name, u16 default_addr); +struct i2c_client * +devm_i2c_new_ancillary_device (struct i2c_client *client, +const char *name, +u16 default_addr); +void i2c_unregister_device (struct i2c_client *client);struct i2c_client *i2c_verify_client (struct device *dev);
Signature · 1 line
2.39.1
PATCH 2/2 i2c: core: extend use case for dflt addr in i2c_new_ancillary_device
In reply to Heiner KallweitInterpret default_addr 0 in a way that the caller doesn't want a default.
Note: i2c_new_dummy_device() would fail anyway, but it would leave an ugly error message complaining about invalid address 0.
--- drivers/i2c/i2c-core-base.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
drivers/i2c/i2c-core-base.c +8 -1
@@ -1146,7 +1146,8 @@ static u32 i2c_prepare_ancillary_device(struct i2c_client *client,* and create the associated device * @client: Handle to the primary client * @name: Handle to specify which secondary address to get -* @default_addr: Used as a fallback if no secondary address was specified +* @default_addr: Used as a fallback if no secondary address was specified. +* Don't try to fall back if default_addr is zero. * Context: can sleep * * I2C clients can be composed of multiple I2C slaves bound together in a single
@@ -1169,6 +1170,9 @@ struct i2c_client *i2c_new_ancillary_device(struct i2c_client *client, {u32 addr =i2c_prepare_ancillary_device (client, name, default_addr); +if (!addr && !default_addr) +return ERR_PTR (-ENOENT ); +return i2c_new_dummy_device (client->adapter, addr); }EXPORT_SYMBOL_GPL (i2c_new_ancillary_device);
@@ -1179,6 +1183,9 @@ struct i2c_client *devm_i2c_new_ancillary_device(struct i2c_client *client, {u32 addr =i2c_prepare_ancillary_device (client, name, default_addr); +if (!addr && !default_addr) +return ERR_PTR (-ENOENT ); +return devm_i2c_new_dummy_device (&client->dev, client->adapter, addr); }EXPORT_SYMBOL_GPL (devm_i2c_new_ancillary_device);
Signature · 1 line
2.39.1