2014-10-22 15:31:07

by Jean-Michel Hautbois

[permalink] [raw]
Subject: [PATCH 1/2] i2c: Add generic support passing secondary devices addresses

Some I2C devices have multiple addresses assigned, for example each address
corresponding to a different internal register map page of the device.
So far drivers which need support for this have handled this with a driver
specific and non-generic implementation, e.g. passing the additional address
via platform data.

This patch provides a new helper function called i2c_new_secondary_device()
which is intended to provide a generic way to get the secondary address
as well as instantiate a struct i2c_client for the secondary address.

The function expects a pointer to the primary i2c_client, a name
for the secondary address and an optional default address. The name is used
as a handle to specify which secondary address to get.

The default address is used as a fallback in case no secondary address
was explicitly specified. In case no secondary address and no default
address were specified the function returns NULL.

For now the function only supports look-up of the secondary address
from devicetree, but it can be extended in the future
to for example support board files and/or ACPI.

Signed-off-by: Jean-Michel Hautbois <[email protected]>
---
drivers/i2c/i2c-core.c | 40 ++++++++++++++++++++++++++++++++++++++++
include/linux/i2c.h | 8 ++++++++
2 files changed, 48 insertions(+)

diff --git a/drivers/i2c/i2c-core.c b/drivers/i2c/i2c-core.c
index 2f90ac6..fd3b07c 100644
--- a/drivers/i2c/i2c-core.c
+++ b/drivers/i2c/i2c-core.c
@@ -1166,6 +1166,46 @@ struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
}
EXPORT_SYMBOL_GPL(i2c_new_dummy);

+/**
+ * i2c_new_secondary_device - Helper to get the instantiated secondary address
+ * @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
+ * Context: can sleep
+ *
+ * This returns an I2C client bound to the "dummy" driver based on DT parsing.
+ *
+ * This returns the new i2c client, which should be saved for later use with
+ * i2c_unregister_device(); or NULL to indicate an error.
+ */
+struct i2c_client *i2c_new_secondary_device(struct i2c_client *client,
+ const char *name,
+ u16 default_addr)
+{
+ int i;
+ u32 addr;
+ struct device_node *np;
+
+ np = client->dev.of_node;
+
+ if (np) {
+ i = of_property_match_string(np, "reg-names", name);
+ if (i >= 0)
+ of_property_read_u32_index(np, "reg", i, &addr);
+ else if (default_addr != 0)
+ addr = default_addr;
+ else
+ addr = NULL;
+ } else {
+ addr = default_addr;
+ }
+
+ dev_dbg(&client->adapter->dev, "Address for %s : 0x%x\n", name, addr);
+ return i2c_new_dummy(client->adapter, addr);
+}
+EXPORT_SYMBOL_GPL(i2c_new_secondary_device);
+
+
/* ------------------------------------------------------------------------- */

/* I2C bus adapters -- one roots each I2C or SMBUS segment */
diff --git a/include/linux/i2c.h b/include/linux/i2c.h
index b556e0a..8629287 100644
--- a/include/linux/i2c.h
+++ b/include/linux/i2c.h
@@ -322,6 +322,14 @@ extern int i2c_probe_func_quick_read(struct i2c_adapter *, unsigned short addr);
extern struct i2c_client *
i2c_new_dummy(struct i2c_adapter *adap, u16 address);

+/* Helper function providing a generic way to get the secondary address
+ * as well as a client handle to this extra address.
+ */
+extern struct i2c_client *
+i2c_new_secondary_device(struct i2c_client *client,
+ const char *name,
+ u16 default_addr);
+
extern void i2c_unregister_device(struct i2c_client *);
#endif /* I2C */

--
2.1.2


2014-10-22 15:31:13

by Jean-Michel Hautbois

[permalink] [raw]
Subject: [PATCH 2/2] adv7604: Add support for i2c_new_secondary_device

The ADV7604 has thirteen 256-byte maps that can be accessed via the main
I²C ports. Each map has it own I²C address and acts
as a standard slave device on the I²C bus.

If nothing is defined, it uses default addresses.
The main purpose is using two adv76xx on the same i2c bus.

Signed-off-by: Jean-Michel Hautbois <[email protected]>
---
.../devicetree/bindings/media/i2c/adv7604.txt | 16 +++++-
drivers/media/i2c/adv7604.c | 59 ++++++++++++++--------
2 files changed, 53 insertions(+), 22 deletions(-)

diff --git a/Documentation/devicetree/bindings/media/i2c/adv7604.txt b/Documentation/devicetree/bindings/media/i2c/adv7604.txt
index 5c8b3e6..8486b5c 100644
--- a/Documentation/devicetree/bindings/media/i2c/adv7604.txt
+++ b/Documentation/devicetree/bindings/media/i2c/adv7604.txt
@@ -12,7 +12,10 @@ Required Properties:
- "adi,adv7611" for the ADV7611
- "adi,adv7604" for the ADV7604

- - reg: I2C slave address
+ - reg: I2C slave addresses
+ The ADV7604 has thirteen 256-byte maps that can be accessed via the main
+ I²C ports. Each map has it own I²C address and acts
+ as a standard slave device on the I²C bus.

- hpd-gpios: References to the GPIOs that control the HDMI hot-plug
detection pins, one per HDMI input. The active flag indicates the GPIO
@@ -33,6 +36,12 @@ The digital output port node must contain at least one endpoint.
Optional Properties:

- reset-gpios: Reference to the GPIO connected to the device's reset pin.
+ - reg-names : Names of maps with programmable addresses.
+ It can contain any map needing another address than default one.
+ Possible maps names are :
+ADV7604 : "main", "avlink", "cec", "infoframe", "esdp", "dpp", "afe", "rep",
+ "edid", "hdmi", "test", "cp", "vdp"
+ADV7611 : "main", "cec", "infoframe", "afe", "rep", "edid", "hdmi", "cp"

Optional Endpoint Properties:

@@ -51,7 +60,10 @@ Example:

hdmi_receiver@4c {
compatible = "adi,adv7611";
- reg = <0x4c>;
+ /* edid page will be accessible @ 0x66 on i2c bus */
+ /* other maps keep their default addresses */
+ reg = <0x4c 0x66>;
+ reg-names = "main", "edid";

reset-gpios = <&ioexp 0 GPIO_ACTIVE_LOW>;
hpd-gpios = <&ioexp 2 GPIO_ACTIVE_HIGH>;
diff --git a/drivers/media/i2c/adv7604.c b/drivers/media/i2c/adv7604.c
index 421035f..e4e30a2 100644
--- a/drivers/media/i2c/adv7604.c
+++ b/drivers/media/i2c/adv7604.c
@@ -326,6 +326,27 @@ static const struct adv7604_video_standards adv7604_prim_mode_hdmi_gr[] = {
{ },
};

+struct adv7604_register {
+ const char *name;
+ u8 default_addr;
+};
+
+static const struct adv7604_register adv7604_secondary_names[] = {
+ [ADV7604_PAGE_IO] = { "main", 0x4c },
+ [ADV7604_PAGE_AVLINK] = { "avlink", 0x42 },
+ [ADV7604_PAGE_CEC] = { "cec", 0x40 },
+ [ADV7604_PAGE_INFOFRAME] = { "infoframe", 0x3e },
+ [ADV7604_PAGE_ESDP] = { "esdp", 0x38 },
+ [ADV7604_PAGE_DPP] = { "dpp", 0x3c },
+ [ADV7604_PAGE_AFE] = { "afe", 0x26 },
+ [ADV7604_PAGE_REP] = { "rep", 0x32 },
+ [ADV7604_PAGE_EDID] = { "edid", 0x36 },
+ [ADV7604_PAGE_HDMI] = { "hdmi", 0x34 },
+ [ADV7604_PAGE_TEST] = { "test", 0x30 },
+ [ADV7604_PAGE_CP] = { "cp", 0x22 },
+ [ADV7604_PAGE_VDP] = { "vdp", 0x24 },
+};
+
/* ----------------------------------------------------------------------- */

static inline struct adv7604_state *to_state(struct v4l2_subdev *sd)
@@ -2528,13 +2549,26 @@ static void adv7604_unregister_clients(struct adv7604_state *state)
}

static struct i2c_client *adv7604_dummy_client(struct v4l2_subdev *sd,
- u8 addr, u8 io_reg)
+ unsigned int i)
{
struct i2c_client *client = v4l2_get_subdevdata(sd);
+ struct adv7604_platform_data *pdata = client->dev.platform_data;
+ unsigned int io_reg = 0xf2 + i;
+ unsigned int default_addr = io_read(sd, io_reg) >> 1;
+ struct i2c_client *new_client;
+
+ if (pdata && pdata->i2c_addresses[i])
+ new_client = i2c_new_dummy(client->adapter,
+ pdata->i2c_addresses[i]);
+ else
+ new_client = i2c_new_secondary_device(client,
+ adv7604_secondary_names[i].name,
+ adv7604_secondary_names[i].default_addr);
+
+ if (new_client)
+ io_write(sd, io_reg, new_client->addr << 1);

- if (addr)
- io_write(sd, io_reg, addr << 1);
- return i2c_new_dummy(client->adapter, io_read(sd, io_reg) >> 1);
+ return new_client;
}

static const struct adv7604_reg_seq adv7604_recommended_settings_afe[] = {
@@ -2718,20 +2752,6 @@ static int adv7604_parse_dt(struct adv7604_state *state)
/* Disable the interrupt for now as no DT-based board uses it. */
state->pdata.int1_config = ADV7604_INT1_CONFIG_DISABLED;

- /* Use the default I2C addresses. */
- state->pdata.i2c_addresses[ADV7604_PAGE_AVLINK] = 0x42;
- state->pdata.i2c_addresses[ADV7604_PAGE_CEC] = 0x40;
- state->pdata.i2c_addresses[ADV7604_PAGE_INFOFRAME] = 0x3e;
- state->pdata.i2c_addresses[ADV7604_PAGE_ESDP] = 0x38;
- state->pdata.i2c_addresses[ADV7604_PAGE_DPP] = 0x3c;
- state->pdata.i2c_addresses[ADV7604_PAGE_AFE] = 0x26;
- state->pdata.i2c_addresses[ADV7604_PAGE_REP] = 0x32;
- state->pdata.i2c_addresses[ADV7604_PAGE_EDID] = 0x36;
- state->pdata.i2c_addresses[ADV7604_PAGE_HDMI] = 0x34;
- state->pdata.i2c_addresses[ADV7604_PAGE_TEST] = 0x30;
- state->pdata.i2c_addresses[ADV7604_PAGE_CP] = 0x22;
- state->pdata.i2c_addresses[ADV7604_PAGE_VDP] = 0x24;
-
/* Hardcode the remaining platform data fields. */
state->pdata.disable_pwrdnb = 0;
state->pdata.disable_cable_det_rst = 0;
@@ -2892,8 +2912,7 @@ static int adv7604_probe(struct i2c_client *client,
continue;

state->i2c_clients[i] =
- adv7604_dummy_client(sd, state->pdata.i2c_addresses[i],
- 0xf2 + i);
+ adv7604_dummy_client(sd, i);
if (state->i2c_clients[i] == NULL) {
err = -ENOMEM;
v4l2_err(sd, "failed to create i2c client %u\n", i);
--
2.1.2

2014-10-22 23:37:48

by Laurent Pinchart

[permalink] [raw]
Subject: Re: [PATCH 1/2] i2c: Add generic support passing secondary devices addresses

Hi Jean-Michel,

Thank you for the patch.

On Wednesday 22 October 2014 17:30:47 Jean-Michel Hautbois wrote:
> Some I2C devices have multiple addresses assigned, for example each address
> corresponding to a different internal register map page of the device.
> So far drivers which need support for this have handled this with a driver
> specific and non-generic implementation, e.g. passing the additional address
> via platform data.
>
> This patch provides a new helper function called i2c_new_secondary_device()
> which is intended to provide a generic way to get the secondary address
> as well as instantiate a struct i2c_client for the secondary address.
>
> The function expects a pointer to the primary i2c_client, a name
> for the secondary address and an optional default address. The name is used
> as a handle to specify which secondary address to get.
>
> The default address is used as a fallback in case no secondary address
> was explicitly specified. In case no secondary address and no default
> address were specified the function returns NULL.
>
> For now the function only supports look-up of the secondary address
> from devicetree, but it can be extended in the future
> to for example support board files and/or ACPI.

As this is core code I believe the DT bindings should be documented somewhere
in Documentation/devicetree/bindings/i2c/.

> Signed-off-by: Jean-Michel Hautbois <[email protected]>
> ---
> drivers/i2c/i2c-core.c | 40 ++++++++++++++++++++++++++++++++++++++++
> include/linux/i2c.h | 8 ++++++++
> 2 files changed, 48 insertions(+)
>
> diff --git a/drivers/i2c/i2c-core.c b/drivers/i2c/i2c-core.c
> index 2f90ac6..fd3b07c 100644
> --- a/drivers/i2c/i2c-core.c
> +++ b/drivers/i2c/i2c-core.c
> @@ -1166,6 +1166,46 @@ struct i2c_client *i2c_new_dummy(struct i2c_adapter
> *adapter, u16 address) }
> EXPORT_SYMBOL_GPL(i2c_new_dummy);
>
> +/**
> + * i2c_new_secondary_device - Helper to get the instantiated secondary
> address

It does more than that, it also creates the 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
> + * Context: can sleep
> + *
> + * This returns an I2C client bound to the "dummy" driver based on DT
> parsing.

Could you elaborate on that ? I would explain that the address is retrieved
from the firmware based on the name, and that default_addr is used in case the
firmware doesn't provide any information.

> + *
> + * This returns the new i2c client, which should be saved for later use
> with
> + * i2c_unregister_device(); or NULL to indicate an error.
> + */
> +struct i2c_client *i2c_new_secondary_device(struct i2c_client *client,
> + const char *name,
> + u16 default_addr)
> +{
> + int i;
> + u32 addr;
> + struct device_node *np;
> +
> + np = client->dev.of_node;
> +
> + if (np) {
> + i = of_property_match_string(np, "reg-names", name);
> + if (i >= 0)
> + of_property_read_u32_index(np, "reg", i, &addr);

This call could fail in which case addr will be uninitialized.

> + else if (default_addr != 0)
> + addr = default_addr;
> + else
> + addr = NULL;

addr isn't a pointer. I'm surprised the compiler hasn't warned you.

> + } else {
> + addr = default_addr;
> + }

The whole logic can be simplified to

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 : 0x%x\n", name, addr);
> + return i2c_new_dummy(client->adapter, addr);
> +}
> +EXPORT_SYMBOL_GPL(i2c_new_secondary_device);
> +
> +
> /*
> -------------------------------------------------------------------------
> */
>
> /* I2C bus adapters -- one roots each I2C or SMBUS segment */
> diff --git a/include/linux/i2c.h b/include/linux/i2c.h
> index b556e0a..8629287 100644
> --- a/include/linux/i2c.h
> +++ b/include/linux/i2c.h
> @@ -322,6 +322,14 @@ extern int i2c_probe_func_quick_read(struct i2c_adapter
> *, unsigned short addr); extern struct i2c_client *
> i2c_new_dummy(struct i2c_adapter *adap, u16 address);
>
> +/* Helper function providing a generic way to get the secondary address
> + * as well as a client handle to this extra address.
> + */

The function is already documented in i2c-core.c, I would ditch this comment.

> +extern struct i2c_client *
> +i2c_new_secondary_device(struct i2c_client *client,
> + const char *name,
> + u16 default_addr);
> +
> extern void i2c_unregister_device(struct i2c_client *);
> #endif /* I2C */

--
Regards,

Laurent Pinchart

2014-10-22 23:44:19

by Laurent Pinchart

[permalink] [raw]
Subject: Re: [PATCH 2/2] adv7604: Add support for i2c_new_secondary_device

Hi Jean-Michel,

Thank you for the patch.

On Wednesday 22 October 2014 17:30:48 Jean-Michel Hautbois wrote:
> The ADV7604 has thirteen 256-byte maps that can be accessed via the main
> I?C ports. Each map has it own I?C address and acts
> as a standard slave device on the I?C bus.
>
> If nothing is defined, it uses default addresses.
> The main purpose is using two adv76xx on the same i2c bus.
>
> Signed-off-by: Jean-Michel Hautbois <[email protected]>
> ---
> .../devicetree/bindings/media/i2c/adv7604.txt | 16 +++++-
> drivers/media/i2c/adv7604.c | 59 ++++++++++++-------
> 2 files changed, 53 insertions(+), 22 deletions(-)
>
> diff --git a/Documentation/devicetree/bindings/media/i2c/adv7604.txt
> b/Documentation/devicetree/bindings/media/i2c/adv7604.txt index
> 5c8b3e6..8486b5c 100644
> --- a/Documentation/devicetree/bindings/media/i2c/adv7604.txt
> +++ b/Documentation/devicetree/bindings/media/i2c/adv7604.txt
> @@ -12,7 +12,10 @@ Required Properties:
> - "adi,adv7611" for the ADV7611
> - "adi,adv7604" for the ADV7604

Given that I'll have comment on the independent patch that adds support for
the adv7604, you should rebase this series to remote that dependency if you
want to get it merged now, otherwise it will get delayed.

>
> - - reg: I2C slave address
> + - reg: I2C slave addresses
> + The ADV7604 has thirteen 256-byte maps that can be accessed via the
> main
> + I?C ports. Each map has it own I?C address and acts
> + as a standard slave device on the I?C bus.
>
> - hpd-gpios: References to the GPIOs that control the HDMI hot-plug
> detection pins, one per HDMI input. The active flag indicates the GPIO
> @@ -33,6 +36,12 @@ The digital output port node must contain at least one
> endpoint. Optional Properties:
>
> - reset-gpios: Reference to the GPIO connected to the device's reset pin.
> + - reg-names : Names of maps with programmable addresses.
> + It can contain any map needing another address than default one.
> + Possible maps names are :
> +ADV7604 : "main", "avlink", "cec", "infoframe", "esdp", "dpp", "afe",
> "rep",
> + "edid", "hdmi", "test", "cp", "vdp"

If you rebase the series in order to avoid depending on the adv7604 DT support
patch this line should be moved to "adv7604: Add DT parsing support".

> +ADV7611 : "main", "cec", "infoframe", "afe", "rep", "edid", "hdmi", "cp"
>
> Optional Endpoint Properties:
>
> @@ -51,7 +60,10 @@ Example:
>
> hdmi_receiver@4c {
> compatible = "adi,adv7611";
> - reg = <0x4c>;
> + /* edid page will be accessible @ 0x66 on i2c bus */
> + /* other maps keep their default addresses */
> + reg = <0x4c 0x66>;
> + reg-names = "main", "edid";
>
> reset-gpios = <&ioexp 0 GPIO_ACTIVE_LOW>;
> hpd-gpios = <&ioexp 2 GPIO_ACTIVE_HIGH>;
> diff --git a/drivers/media/i2c/adv7604.c b/drivers/media/i2c/adv7604.c
> index 421035f..e4e30a2 100644
> --- a/drivers/media/i2c/adv7604.c
> +++ b/drivers/media/i2c/adv7604.c
> @@ -326,6 +326,27 @@ static const struct adv7604_video_standards
> adv7604_prim_mode_hdmi_gr[] = { { },
> };
>
> +struct adv7604_register {
> + const char *name;
> + u8 default_addr;
> +};
> +
> +static const struct adv7604_register adv7604_secondary_names[] = {
> + [ADV7604_PAGE_IO] = { "main", 0x4c },
> + [ADV7604_PAGE_AVLINK] = { "avlink", 0x42 },
> + [ADV7604_PAGE_CEC] = { "cec", 0x40 },
> + [ADV7604_PAGE_INFOFRAME] = { "infoframe", 0x3e },
> + [ADV7604_PAGE_ESDP] = { "esdp", 0x38 },
> + [ADV7604_PAGE_DPP] = { "dpp", 0x3c },
> + [ADV7604_PAGE_AFE] = { "afe", 0x26 },
> + [ADV7604_PAGE_REP] = { "rep", 0x32 },
> + [ADV7604_PAGE_EDID] = { "edid", 0x36 },
> + [ADV7604_PAGE_HDMI] = { "hdmi", 0x34 },
> + [ADV7604_PAGE_TEST] = { "test", 0x30 },
> + [ADV7604_PAGE_CP] = { "cp", 0x22 },
> + [ADV7604_PAGE_VDP] = { "vdp", 0x24 },
> +};
> +
> /* -----------------------------------------------------------------------
> */
>
> static inline struct adv7604_state *to_state(struct v4l2_subdev *sd)
> @@ -2528,13 +2549,26 @@ static void adv7604_unregister_clients(struct
> adv7604_state *state) }
>
> static struct i2c_client *adv7604_dummy_client(struct v4l2_subdev *sd,
> - u8 addr, u8 io_reg)
> + unsigned int i)
> {
> struct i2c_client *client = v4l2_get_subdevdata(sd);
> + struct adv7604_platform_data *pdata = client->dev.platform_data;
> + unsigned int io_reg = 0xf2 + i;
> + unsigned int default_addr = io_read(sd, io_reg) >> 1;

The variable isn't used.

> + struct i2c_client *new_client;
> +
> + if (pdata && pdata->i2c_addresses[i])
> + new_client = i2c_new_dummy(client->adapter,
> + pdata->i2c_addresses[i]);
> + else
> + new_client = i2c_new_secondary_device(client,
> + adv7604_secondary_names[i].name,
> + adv7604_secondary_names[i].default_addr);
> +
> + if (new_client)
> + io_write(sd, io_reg, new_client->addr << 1);
>
> - if (addr)
> - io_write(sd, io_reg, addr << 1);
> - return i2c_new_dummy(client->adapter, io_read(sd, io_reg) >> 1);
> + return new_client;
> }
>
> static const struct adv7604_reg_seq adv7604_recommended_settings_afe[] = {
> @@ -2718,20 +2752,6 @@ static int adv7604_parse_dt(struct adv7604_state
> *state) /* Disable the interrupt for now as no DT-based board uses it. */
> state->pdata.int1_config = ADV7604_INT1_CONFIG_DISABLED;
>
> - /* Use the default I2C addresses. */
> - state->pdata.i2c_addresses[ADV7604_PAGE_AVLINK] = 0x42;
> - state->pdata.i2c_addresses[ADV7604_PAGE_CEC] = 0x40;
> - state->pdata.i2c_addresses[ADV7604_PAGE_INFOFRAME] = 0x3e;
> - state->pdata.i2c_addresses[ADV7604_PAGE_ESDP] = 0x38;
> - state->pdata.i2c_addresses[ADV7604_PAGE_DPP] = 0x3c;
> - state->pdata.i2c_addresses[ADV7604_PAGE_AFE] = 0x26;
> - state->pdata.i2c_addresses[ADV7604_PAGE_REP] = 0x32;
> - state->pdata.i2c_addresses[ADV7604_PAGE_EDID] = 0x36;
> - state->pdata.i2c_addresses[ADV7604_PAGE_HDMI] = 0x34;
> - state->pdata.i2c_addresses[ADV7604_PAGE_TEST] = 0x30;
> - state->pdata.i2c_addresses[ADV7604_PAGE_CP] = 0x22;
> - state->pdata.i2c_addresses[ADV7604_PAGE_VDP] = 0x24;
> -
> /* Hardcode the remaining platform data fields. */
> state->pdata.disable_pwrdnb = 0;
> state->pdata.disable_cable_det_rst = 0;
> @@ -2892,8 +2912,7 @@ static int adv7604_probe(struct i2c_client *client,
> continue;
>
> state->i2c_clients[i] =
> - adv7604_dummy_client(sd, state->pdata.i2c_addresses[i],
> - 0xf2 + i);
> + adv7604_dummy_client(sd, i);
> if (state->i2c_clients[i] == NULL) {
> err = -ENOMEM;
> v4l2_err(sd, "failed to create i2c client %u\n", i);

--
Regards,

Laurent Pinchart

2014-10-23 06:00:13

by Jean-Michel Hautbois

[permalink] [raw]
Subject: Re: [PATCH 1/2] i2c: Add generic support passing secondary devices addresses

Hi Laurent,

Thank you for your review,

2014-10-23 1:37 GMT+02:00 Laurent Pinchart <[email protected]>:
> Hi Jean-Michel,
>
> Thank you for the patch.
>
> On Wednesday 22 October 2014 17:30:47 Jean-Michel Hautbois wrote:
>> Some I2C devices have multiple addresses assigned, for example each address
>> corresponding to a different internal register map page of the device.
>> So far drivers which need support for this have handled this with a driver
>> specific and non-generic implementation, e.g. passing the additional address
>> via platform data.
>>
>> This patch provides a new helper function called i2c_new_secondary_device()
>> which is intended to provide a generic way to get the secondary address
>> as well as instantiate a struct i2c_client for the secondary address.
>>
>> The function expects a pointer to the primary i2c_client, a name
>> for the secondary address and an optional default address. The name is used
>> as a handle to specify which secondary address to get.
>>
>> The default address is used as a fallback in case no secondary address
>> was explicitly specified. In case no secondary address and no default
>> address were specified the function returns NULL.
>>
>> For now the function only supports look-up of the secondary address
>> from devicetree, but it can be extended in the future
>> to for example support board files and/or ACPI.
>
> As this is core code I believe the DT bindings should be documented somewhere
> in Documentation/devicetree/bindings/i2c/.

Mmh, probably yes, but I don't know where precisely, as all the
bindings are devices specific here...

>> Signed-off-by: Jean-Michel Hautbois <[email protected]>
>> ---
>> drivers/i2c/i2c-core.c | 40 ++++++++++++++++++++++++++++++++++++++++
>> include/linux/i2c.h | 8 ++++++++
>> 2 files changed, 48 insertions(+)
>>
>> diff --git a/drivers/i2c/i2c-core.c b/drivers/i2c/i2c-core.c
>> index 2f90ac6..fd3b07c 100644
>> --- a/drivers/i2c/i2c-core.c
>> +++ b/drivers/i2c/i2c-core.c
>> @@ -1166,6 +1166,46 @@ struct i2c_client *i2c_new_dummy(struct i2c_adapter
>> *adapter, u16 address) }
>> EXPORT_SYMBOL_GPL(i2c_new_dummy);
>>
>> +/**
>> + * i2c_new_secondary_device - Helper to get the instantiated secondary
>> address
>
> It does more than that, it also creates the device.

Right, how about :
+ * i2c_new_secondary_device - Helper to get the instantiated secondary address
+ * 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
>> + * Context: can sleep
>> + *
>> + * This returns an I2C client bound to the "dummy" driver based on DT
>> parsing.
>
> Could you elaborate on that ? I would explain that the address is retrieved
> from the firmware based on the name, and that default_addr is used in case the
> firmware doesn't provide any information.

Something like that ?
+ * This returns an I2C client bound to the "dummy" driver based on DT parsing.
+ * It retrieves the address based on the name.
+ * It uses default_addr if no information is provided by firmware.


>> + *
>> + * This returns the new i2c client, which should be saved for later use
>> with
>> + * i2c_unregister_device(); or NULL to indicate an error.
>> + */
>> +struct i2c_client *i2c_new_secondary_device(struct i2c_client *client,
>> + const char *name,
>> + u16 default_addr)
>> +{
>> + int i;
>> + u32 addr;
>> + struct device_node *np;
>> +
>> + np = client->dev.of_node;
>> +
>> + if (np) {
>> + i = of_property_match_string(np, "reg-names", name);
>> + if (i >= 0)
>> + of_property_read_u32_index(np, "reg", i, &addr);
>
> This call could fail in which case addr will be uninitialized.
>
>> + else if (default_addr != 0)
>> + addr = default_addr;
>> + else
>> + addr = NULL;
>
> addr isn't a pointer. I'm surprised the compiler hasn't warned you.
It has, just didn't notice it, sorry fir the noise.

>> + } else {
>> + addr = default_addr;
>> + }
>
> The whole logic can be simplified to
>
> 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);
> }
>

OK, applied on my side.

Thanks,
JM

2014-10-23 06:02:35

by Jean-Michel Hautbois

[permalink] [raw]
Subject: Re: [PATCH 2/2] adv7604: Add support for i2c_new_secondary_device

Hi Laurent,

Thank you for reviewing,

2014-10-23 1:44 GMT+02:00 Laurent Pinchart <[email protected]>:
> Hi Jean-Michel,
>
> Thank you for the patch.
>
> On Wednesday 22 October 2014 17:30:48 Jean-Michel Hautbois wrote:
>> The ADV7604 has thirteen 256-byte maps that can be accessed via the main
>> I²C ports. Each map has it own I²C address and acts
>> as a standard slave device on the I²C bus.
>>
>> If nothing is defined, it uses default addresses.
>> The main purpose is using two adv76xx on the same i2c bus.
>>
>> Signed-off-by: Jean-Michel Hautbois <[email protected]>
>> ---
>> .../devicetree/bindings/media/i2c/adv7604.txt | 16 +++++-
>> drivers/media/i2c/adv7604.c | 59 ++++++++++++-------
>> 2 files changed, 53 insertions(+), 22 deletions(-)
>>
>> diff --git a/Documentation/devicetree/bindings/media/i2c/adv7604.txt
>> b/Documentation/devicetree/bindings/media/i2c/adv7604.txt index
>> 5c8b3e6..8486b5c 100644
>> --- a/Documentation/devicetree/bindings/media/i2c/adv7604.txt
>> +++ b/Documentation/devicetree/bindings/media/i2c/adv7604.txt
>> @@ -12,7 +12,10 @@ Required Properties:
>> - "adi,adv7611" for the ADV7611
>> - "adi,adv7604" for the ADV7604
>
> Given that I'll have comment on the independent patch that adds support for
> the adv7604, you should rebase this series to remote that dependency if you
> want to get it merged now, otherwise it will get delayed.

Oh, you are right, of course, I forgot rebasing (as well as numbering
the patch as v2)...

>>
>> - - reg: I2C slave address
>> + - reg: I2C slave addresses
>> + The ADV7604 has thirteen 256-byte maps that can be accessed via the
>> main
>> + I²C ports. Each map has it own I²C address and acts
>> + as a standard slave device on the I²C bus.
>>
>> - hpd-gpios: References to the GPIOs that control the HDMI hot-plug
>> detection pins, one per HDMI input. The active flag indicates the GPIO
>> @@ -33,6 +36,12 @@ The digital output port node must contain at least one
>> endpoint. Optional Properties:
>>
>> - reset-gpios: Reference to the GPIO connected to the device's reset pin.
>> + - reg-names : Names of maps with programmable addresses.
>> + It can contain any map needing another address than default one.
>> + Possible maps names are :
>> +ADV7604 : "main", "avlink", "cec", "infoframe", "esdp", "dpp", "afe",
>> "rep",
>> + "edid", "hdmi", "test", "cp", "vdp"
>
> If you rebase the series in order to avoid depending on the adv7604 DT support
> patch this line should be moved to "adv7604: Add DT parsing support".
>
>> +ADV7611 : "main", "cec", "infoframe", "afe", "rep", "edid", "hdmi", "cp"
>>
>> Optional Endpoint Properties:
>>
>> @@ -51,7 +60,10 @@ Example:
>>
>> hdmi_receiver@4c {
>> compatible = "adi,adv7611";
>> - reg = <0x4c>;
>> + /* edid page will be accessible @ 0x66 on i2c bus */
>> + /* other maps keep their default addresses */
>> + reg = <0x4c 0x66>;
>> + reg-names = "main", "edid";
>>
>> reset-gpios = <&ioexp 0 GPIO_ACTIVE_LOW>;
>> hpd-gpios = <&ioexp 2 GPIO_ACTIVE_HIGH>;
>> diff --git a/drivers/media/i2c/adv7604.c b/drivers/media/i2c/adv7604.c
>> index 421035f..e4e30a2 100644
>> --- a/drivers/media/i2c/adv7604.c
>> +++ b/drivers/media/i2c/adv7604.c
>> @@ -326,6 +326,27 @@ static const struct adv7604_video_standards
>> adv7604_prim_mode_hdmi_gr[] = { { },
>> };
>>
>> +struct adv7604_register {
>> + const char *name;
>> + u8 default_addr;
>> +};
>> +
>> +static const struct adv7604_register adv7604_secondary_names[] = {
>> + [ADV7604_PAGE_IO] = { "main", 0x4c },
>> + [ADV7604_PAGE_AVLINK] = { "avlink", 0x42 },
>> + [ADV7604_PAGE_CEC] = { "cec", 0x40 },
>> + [ADV7604_PAGE_INFOFRAME] = { "infoframe", 0x3e },
>> + [ADV7604_PAGE_ESDP] = { "esdp", 0x38 },
>> + [ADV7604_PAGE_DPP] = { "dpp", 0x3c },
>> + [ADV7604_PAGE_AFE] = { "afe", 0x26 },
>> + [ADV7604_PAGE_REP] = { "rep", 0x32 },
>> + [ADV7604_PAGE_EDID] = { "edid", 0x36 },
>> + [ADV7604_PAGE_HDMI] = { "hdmi", 0x34 },
>> + [ADV7604_PAGE_TEST] = { "test", 0x30 },
>> + [ADV7604_PAGE_CP] = { "cp", 0x22 },
>> + [ADV7604_PAGE_VDP] = { "vdp", 0x24 },
>> +};
>> +
>> /* -----------------------------------------------------------------------
>> */
>>
>> static inline struct adv7604_state *to_state(struct v4l2_subdev *sd)
>> @@ -2528,13 +2549,26 @@ static void adv7604_unregister_clients(struct
>> adv7604_state *state) }
>>
>> static struct i2c_client *adv7604_dummy_client(struct v4l2_subdev *sd,
>> - u8 addr, u8 io_reg)
>> + unsigned int i)
>> {
>> struct i2c_client *client = v4l2_get_subdevdata(sd);
>> + struct adv7604_platform_data *pdata = client->dev.platform_data;
>> + unsigned int io_reg = 0xf2 + i;
>> + unsigned int default_addr = io_read(sd, io_reg) >> 1;
>
> The variable isn't used.

Removed. Again, didn't check sufficiently my compiler warnings...

>> + struct i2c_client *new_client;
>> +
>> + if (pdata && pdata->i2c_addresses[i])
>> + new_client = i2c_new_dummy(client->adapter,
>> + pdata->i2c_addresses[i]);
>> + else
>> + new_client = i2c_new_secondary_device(client,
>> + adv7604_secondary_names[i].name,
>> + adv7604_secondary_names[i].default_addr);
>> +
>> + if (new_client)
>> + io_write(sd, io_reg, new_client->addr << 1);
>>
>> - if (addr)
>> - io_write(sd, io_reg, addr << 1);
>> - return i2c_new_dummy(client->adapter, io_read(sd, io_reg) >> 1);
>> + return new_client;
>> }
>>
>> static const struct adv7604_reg_seq adv7604_recommended_settings_afe[] = {
>> @@ -2718,20 +2752,6 @@ static int adv7604_parse_dt(struct adv7604_state
>> *state) /* Disable the interrupt for now as no DT-based board uses it. */
>> state->pdata.int1_config = ADV7604_INT1_CONFIG_DISABLED;
>>
>> - /* Use the default I2C addresses. */
>> - state->pdata.i2c_addresses[ADV7604_PAGE_AVLINK] = 0x42;
>> - state->pdata.i2c_addresses[ADV7604_PAGE_CEC] = 0x40;
>> - state->pdata.i2c_addresses[ADV7604_PAGE_INFOFRAME] = 0x3e;
>> - state->pdata.i2c_addresses[ADV7604_PAGE_ESDP] = 0x38;
>> - state->pdata.i2c_addresses[ADV7604_PAGE_DPP] = 0x3c;
>> - state->pdata.i2c_addresses[ADV7604_PAGE_AFE] = 0x26;
>> - state->pdata.i2c_addresses[ADV7604_PAGE_REP] = 0x32;
>> - state->pdata.i2c_addresses[ADV7604_PAGE_EDID] = 0x36;
>> - state->pdata.i2c_addresses[ADV7604_PAGE_HDMI] = 0x34;
>> - state->pdata.i2c_addresses[ADV7604_PAGE_TEST] = 0x30;
>> - state->pdata.i2c_addresses[ADV7604_PAGE_CP] = 0x22;
>> - state->pdata.i2c_addresses[ADV7604_PAGE_VDP] = 0x24;
>> -
>> /* Hardcode the remaining platform data fields. */
>> state->pdata.disable_pwrdnb = 0;
>> state->pdata.disable_cable_det_rst = 0;
>> @@ -2892,8 +2912,7 @@ static int adv7604_probe(struct i2c_client *client,
>> continue;
>>
>> state->i2c_clients[i] =
>> - adv7604_dummy_client(sd, state->pdata.i2c_addresses[i],
>> - 0xf2 + i);
>> + adv7604_dummy_client(sd, i);
>> if (state->i2c_clients[i] == NULL) {
>> err = -ENOMEM;
>> v4l2_err(sd, "failed to create i2c client %u\n", i);
>
> --
> Regards,
>
> Laurent Pinchart
>

Thanks,
JM

2014-10-26 23:25:13

by Laurent Pinchart

[permalink] [raw]
Subject: Re: [PATCH 1/2] i2c: Add generic support passing secondary devices addresses

Hi Jean-Michel,

On Thursday 23 October 2014 07:59:53 Jean-Michel Hautbois wrote:
> 2014-10-23 1:37 GMT+02:00 Laurent Pinchart:
> > On Wednesday 22 October 2014 17:30:47 Jean-Michel Hautbois wrote:
> >> Some I2C devices have multiple addresses assigned, for example each
> >> address corresponding to a different internal register map page of the
> >> device. So far drivers which need support for this have handled this with
> >> a driver specific and non-generic implementation, e.g. passing the
> >> additional address via platform data.
> >>
> >> This patch provides a new helper function called
> >> i2c_new_secondary_device()
> >> which is intended to provide a generic way to get the secondary address
> >> as well as instantiate a struct i2c_client for the secondary address.
> >>
> >> The function expects a pointer to the primary i2c_client, a name
> >> for the secondary address and an optional default address. The name is
> >> used as a handle to specify which secondary address to get.
> >>
> >> The default address is used as a fallback in case no secondary address
> >> was explicitly specified. In case no secondary address and no default
> >> address were specified the function returns NULL.
> >>
> >> For now the function only supports look-up of the secondary address
> >> from devicetree, but it can be extended in the future
> >> to for example support board files and/or ACPI.
> >
> > As this is core code I believe the DT bindings should be documented
> > somewhere in Documentation/devicetree/bindings/i2c/.
>
> Mmh, probably yes, but I don't know where precisely, as all the
> bindings are devices specific here...

Lucky you, you can create the I2C core DT bindings documentation :-)
Documentation/devicetree/bindings/i2c/i2c.txt sounds like a good candidate.

> >> Signed-off-by: Jean-Michel Hautbois <[email protected]>
> >> ---
> >>
> >> drivers/i2c/i2c-core.c | 40 ++++++++++++++++++++++++++++++++++++++++
> >> include/linux/i2c.h | 8 ++++++++
> >> 2 files changed, 48 insertions(+)
> >>
> >> diff --git a/drivers/i2c/i2c-core.c b/drivers/i2c/i2c-core.c
> >> index 2f90ac6..fd3b07c 100644
> >> --- a/drivers/i2c/i2c-core.c
> >> +++ b/drivers/i2c/i2c-core.c
> >> @@ -1166,6 +1166,46 @@ struct i2c_client *i2c_new_dummy(struct
> >> i2c_adapter
> >> *adapter, u16 address) }
> >>
> >> EXPORT_SYMBOL_GPL(i2c_new_dummy);
> >>
> >> +/**
> >> + * i2c_new_secondary_device - Helper to get the instantiated secondary
> >> address
> >
> > It does more than that, it also creates the device.
>
> Right, how about :
> + * i2c_new_secondary_device - Helper to get the instantiated secondary
> address
> + * 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
> >> + * Context: can sleep
> >> + *
> >> + * This returns an I2C client bound to the "dummy" driver based on DT
> >> parsing.
> >
> > Could you elaborate on that ? I would explain that the address is
> > retrieved from the firmware based on the name, and that default_addr is
> > used in case the firmware doesn't provide any information.
>
> Something like that ?
> + * This returns an I2C client bound to the "dummy" driver based on DT
> parsing.
> + * It retrieves the address based on the name.
> + * It uses default_addr if no information is provided by firmware.

"I2C clients can be composed of multiple I2C slaves bound together in a single
component. The I2C client driver then binds to the master I2C slave and needs
to create I2C dummy clients to communicate with all the other slaves.

This function creates an returns an I2C dummy client whose I2C address is
retrieved from the platform firmware based on the given slave name. If no
address is specified by the firmware default_addr is used.

On DT-based platforms the address is retrieved from the "reg" property entry
cell whose "reg-names" value matches the slave name."

> >> + *
> >> + * This returns the new i2c client, which should be saved for later use
> >> with
> >> + * i2c_unregister_device(); or NULL to indicate an error.
> >> + */
> >> +struct i2c_client *i2c_new_secondary_device(struct i2c_client *client,
> >> + const char *name,
> >> + u16 default_addr)
> >> +{
> >> + int i;
> >> + u32 addr;
> >> + struct device_node *np;
> >> +
> >> + np = client->dev.of_node;
> >> +
> >> + if (np) {
> >> + i = of_property_match_string(np, "reg-names", name);
> >> + if (i >= 0)
> >> + of_property_read_u32_index(np, "reg", i, &addr);
> >
> > This call could fail in which case addr will be uninitialized.
> >
> >> + else if (default_addr != 0)
> >> + addr = default_addr;
> >> + else
> >> + addr = NULL;
> >
> > addr isn't a pointer. I'm surprised the compiler hasn't warned you.
>
> It has, just didn't notice it, sorry fir the noise.
>
> >> + } else {
> >> + addr = default_addr;
> >> + }
> >
> > The whole logic can be simplified to
> >
> > 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);
> > }
>
> OK, applied on my side.

--
Regards,

Laurent Pinchart