linux-i2c.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH i2c-next v3 0/6] i2c: mux: mlxcpld: Extend driver functionality
@ 2021-01-30 17:34 Vadim Pasternak
  2021-01-30 17:34 ` [PATCH i2c-next v3 1/6] i2c: mux: mlxcpld: Convert driver to platform driver Vadim Pasternak
                   ` (5 more replies)
  0 siblings, 6 replies; 9+ messages in thread
From: Vadim Pasternak @ 2021-01-30 17:34 UTC (permalink / raw)
  To: peda, wsa; +Cc: linux-i2c, Vadim Pasternak

The patchset adds new features for the existing Mellanox systems.

Patch #1 converts driver to platform driver.
Patches #2-#3 prepare driver for word addressing support.
Patch #4 adds support for word address space devices.
Patch #5 extends mux number supported by driver.
Patch #6 adds callback notification about mux creation.

Vadim Pasternak (6):
  i2c: mux: mlxcpld: Convert driver to platform driver
  i2c: mux: mlxcpld: Prepare mux selection infrastructure for two-byte
    support
  i2c: mux: mlxcpld: Rename mux ids array
  i2c: mux: mlxcpld: Extend driver to support word address space devices
  i2c: mux: mlxcpld: Extend supported mux number
  i2c: mux: mlxcpld: Add callback to notify mux creation completion

 drivers/i2c/muxes/i2c-mux-mlxcpld.c   | 128 ++++++++++++++++++++--------------
 include/linux/platform_data/mlxcpld.h |  11 ++-
 2 files changed, 83 insertions(+), 56 deletions(-)

-- 
2.11.0


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH i2c-next v3 1/6] i2c: mux: mlxcpld: Convert driver to platform driver
  2021-01-30 17:34 [PATCH i2c-next v3 0/6] i2c: mux: mlxcpld: Extend driver functionality Vadim Pasternak
@ 2021-01-30 17:34 ` Vadim Pasternak
  2021-01-30 17:34 ` [PATCH i2c-next v3 2/6] i2c: mux: mlxcpld: Prepare mux selection infrastructure for two-byte support Vadim Pasternak
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 9+ messages in thread
From: Vadim Pasternak @ 2021-01-30 17:34 UTC (permalink / raw)
  To: peda, wsa; +Cc: linux-i2c, Vadim Pasternak

Convert driver from 'i2c' to 'platform'.
The motivation is to avoid I2C addressing conflict between
‘i2c-mux-cpld’ driver, providing mux selection and deselection through
CPLD ‘mux control’ register, and CPLD host driver. The CPLD is I2C
device and is multi-functional device performing logic for different
components, like LED, ‘hwmon’, interrupt control, watchdog etcetera.
For such configuration CPLD should be host I2C device, connected to the
relevant I2C bus with the relevant I2C address and all others component
drivers are supposed to be its children.
The hierarchy in such case will be like in the below example:
ls /sys/bus/i2c/devices/44-0032
i2c-mux-mlxcpld.44  leds-mlxreg.44  mlxreg-io.44
ls /sys/bus/i2c/devices/44-0032/i2c-mux-mlxcpld.44
channel-0, …,  channel-X

Currently this driver is not activated by any kernel driver,
so this conversion doesn’t affect any user.

Signed-off-by: Vadim Pasternak <vadimp@nvidia.com>
Reviewed-by: Michael Shych <michaelsh@nvidia.com>
---
v1->v2:
 Comments pointed out by Peter:
 - Drop change for last channel setting in mlxcpld_mux_deselect().
---
 drivers/i2c/muxes/i2c-mux-mlxcpld.c | 62 +++++++++++++++++--------------------
 1 file changed, 28 insertions(+), 34 deletions(-)

diff --git a/drivers/i2c/muxes/i2c-mux-mlxcpld.c b/drivers/i2c/muxes/i2c-mux-mlxcpld.c
index 3d894cfb19df..b53f1479272d 100644
--- a/drivers/i2c/muxes/i2c-mux-mlxcpld.c
+++ b/drivers/i2c/muxes/i2c-mux-mlxcpld.c
@@ -20,10 +20,12 @@
 /* mlxcpld_mux - mux control structure:
  * @last_chan - last register value
  * @client - I2C device client
+ * @pdata: platform data
  */
 struct mlxcpld_mux {
 	u8 last_chan;
 	struct i2c_client *client;
+	struct mlxcpld_mux_plat_data pdata;
 };
 
 /* MUX logic description.
@@ -54,37 +56,30 @@ struct mlxcpld_mux {
  *
  */
 
-static const struct i2c_device_id mlxcpld_mux_id[] = {
-	{ "mlxcpld_mux_module", 0 },
-	{ }
-};
-MODULE_DEVICE_TABLE(i2c, mlxcpld_mux_id);
-
 /* Write to mux register. Don't use i2c_transfer() and i2c_smbus_xfer()
  * for this as they will try to lock adapter a second time.
  */
 static int mlxcpld_mux_reg_write(struct i2c_adapter *adap,
-				 struct i2c_client *client, u8 val)
+				 struct mlxcpld_mux *mux, u8 val)
 {
-	struct mlxcpld_mux_plat_data *pdata = dev_get_platdata(&client->dev);
+	struct i2c_client *client = mux->client;
 	union i2c_smbus_data data = { .byte = val };
 
 	return __i2c_smbus_xfer(adap, client->addr, client->flags,
-				I2C_SMBUS_WRITE, pdata->sel_reg_addr,
+				I2C_SMBUS_WRITE, mux->pdata.sel_reg_addr,
 				I2C_SMBUS_BYTE_DATA, &data);
 }
 
 static int mlxcpld_mux_select_chan(struct i2c_mux_core *muxc, u32 chan)
 {
-	struct mlxcpld_mux *data = i2c_mux_priv(muxc);
-	struct i2c_client *client = data->client;
+	struct mlxcpld_mux *mux = i2c_mux_priv(muxc);
 	u8 regval = chan + 1;
 	int err = 0;
 
 	/* Only select the channel if its different from the last channel */
-	if (data->last_chan != regval) {
-		err = mlxcpld_mux_reg_write(muxc->parent, client, regval);
-		data->last_chan = err < 0 ? 0 : regval;
+	if (mux->last_chan != regval) {
+		err = mlxcpld_mux_reg_write(muxc->parent, mux, regval);
+		mux->last_chan = err < 0 ? 0 : regval;
 	}
 
 	return err;
@@ -92,21 +87,19 @@ static int mlxcpld_mux_select_chan(struct i2c_mux_core *muxc, u32 chan)
 
 static int mlxcpld_mux_deselect(struct i2c_mux_core *muxc, u32 chan)
 {
-	struct mlxcpld_mux *data = i2c_mux_priv(muxc);
-	struct i2c_client *client = data->client;
+	struct mlxcpld_mux *mux = i2c_mux_priv(muxc);
 
 	/* Deselect active channel */
-	data->last_chan = 0;
+	mux->last_chan = 0;
 
-	return mlxcpld_mux_reg_write(muxc->parent, client, data->last_chan);
+	return mlxcpld_mux_reg_write(muxc->parent, mux, mux->last_chan);
 }
 
 /* Probe/reomove functions */
-static int mlxcpld_mux_probe(struct i2c_client *client,
-			     const struct i2c_device_id *id)
+static int mlxcpld_mux_probe(struct platform_device *pdev)
 {
-	struct i2c_adapter *adap = client->adapter;
-	struct mlxcpld_mux_plat_data *pdata = dev_get_platdata(&client->dev);
+	struct mlxcpld_mux_plat_data *pdata = dev_get_platdata(&pdev->dev);
+	struct i2c_client *client = to_i2c_client(pdev->dev.parent);
 	struct i2c_mux_core *muxc;
 	int num, force;
 	struct mlxcpld_mux *data;
@@ -115,18 +108,20 @@ static int mlxcpld_mux_probe(struct i2c_client *client,
 	if (!pdata)
 		return -EINVAL;
 
-	if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
+	if (!i2c_check_functionality(client->adapter,
+				     I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
 		return -ENODEV;
 
-	muxc = i2c_mux_alloc(adap, &client->dev, CPLD_MUX_MAX_NCHANS,
+	muxc = i2c_mux_alloc(client->adapter, &pdev->dev, CPLD_MUX_MAX_NCHANS,
 			     sizeof(*data), 0, mlxcpld_mux_select_chan,
 			     mlxcpld_mux_deselect);
 	if (!muxc)
 		return -ENOMEM;
 
+	platform_set_drvdata(pdev, muxc);
 	data = i2c_mux_priv(muxc);
-	i2c_set_clientdata(client, muxc);
 	data->client = client;
+	memcpy(&data->pdata, pdata, sizeof(*pdata));
 	data->last_chan = 0; /* force the first selection */
 
 	/* Create an adapter for each channel. */
@@ -149,24 +144,23 @@ static int mlxcpld_mux_probe(struct i2c_client *client,
 	return err;
 }
 
-static int mlxcpld_mux_remove(struct i2c_client *client)
+static int mlxcpld_mux_remove(struct platform_device *pdev)
 {
-	struct i2c_mux_core *muxc = i2c_get_clientdata(client);
+	struct i2c_mux_core *muxc = platform_get_drvdata(pdev);
 
 	i2c_mux_del_adapters(muxc);
 	return 0;
 }
 
-static struct i2c_driver mlxcpld_mux_driver = {
-	.driver		= {
-		.name	= "mlxcpld-mux",
+static struct platform_driver mlxcpld_mux_driver = {
+	.driver = {
+		.name = "i2c-mux-mlxcpld",
 	},
-	.probe		= mlxcpld_mux_probe,
-	.remove		= mlxcpld_mux_remove,
-	.id_table	= mlxcpld_mux_id,
+	.probe = mlxcpld_mux_probe,
+	.remove = mlxcpld_mux_remove,
 };
 
-module_i2c_driver(mlxcpld_mux_driver);
+module_platform_driver(mlxcpld_mux_driver);
 
 MODULE_AUTHOR("Michael Shych (michaels@mellanox.com)");
 MODULE_DESCRIPTION("Mellanox I2C-CPLD-MUX driver");
-- 
2.11.0


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH i2c-next v3 2/6] i2c: mux: mlxcpld: Prepare mux selection infrastructure for two-byte support
  2021-01-30 17:34 [PATCH i2c-next v3 0/6] i2c: mux: mlxcpld: Extend driver functionality Vadim Pasternak
  2021-01-30 17:34 ` [PATCH i2c-next v3 1/6] i2c: mux: mlxcpld: Convert driver to platform driver Vadim Pasternak
@ 2021-01-30 17:34 ` Vadim Pasternak
  2021-01-30 17:34 ` [PATCH i2c-next v3 3/6] i2c: mux: mlxcpld: Rename mux ids array Vadim Pasternak
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 9+ messages in thread
From: Vadim Pasternak @ 2021-01-30 17:34 UTC (permalink / raw)
  To: peda, wsa; +Cc: linux-i2c, Vadim Pasternak

Allow to program register value zero to the mux register, which is
required for word address mux register space support.
Change key selector type from 'unsigned short' to 'integer' in order to
allow to set it to -1 on deselection.
Rename key selector field from 'last_chan' to 'last_val', since this
fields keeps actually selector value and not channel number.

Signed-off-by: Vadim Pasternak <vadimp@nvidia.com>
---
 drivers/i2c/muxes/i2c-mux-mlxcpld.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/drivers/i2c/muxes/i2c-mux-mlxcpld.c b/drivers/i2c/muxes/i2c-mux-mlxcpld.c
index b53f1479272d..113ad84cdd94 100644
--- a/drivers/i2c/muxes/i2c-mux-mlxcpld.c
+++ b/drivers/i2c/muxes/i2c-mux-mlxcpld.c
@@ -18,12 +18,12 @@
 #define CPLD_MUX_MAX_NCHANS	8
 
 /* mlxcpld_mux - mux control structure:
- * @last_chan - last register value
+ * @last_val - last selected register value or -1 if mux deselected
  * @client - I2C device client
  * @pdata: platform data
  */
 struct mlxcpld_mux {
-	u8 last_chan;
+	int last_val;
 	struct i2c_client *client;
 	struct mlxcpld_mux_plat_data pdata;
 };
@@ -60,7 +60,7 @@ struct mlxcpld_mux {
  * for this as they will try to lock adapter a second time.
  */
 static int mlxcpld_mux_reg_write(struct i2c_adapter *adap,
-				 struct mlxcpld_mux *mux, u8 val)
+				 struct mlxcpld_mux *mux, u32 val)
 {
 	struct i2c_client *client = mux->client;
 	union i2c_smbus_data data = { .byte = val };
@@ -73,13 +73,13 @@ static int mlxcpld_mux_reg_write(struct i2c_adapter *adap,
 static int mlxcpld_mux_select_chan(struct i2c_mux_core *muxc, u32 chan)
 {
 	struct mlxcpld_mux *mux = i2c_mux_priv(muxc);
-	u8 regval = chan + 1;
+	u32 regval = chan + 1;
 	int err = 0;
 
 	/* Only select the channel if its different from the last channel */
-	if (mux->last_chan != regval) {
+	if (mux->last_val != regval) {
 		err = mlxcpld_mux_reg_write(muxc->parent, mux, regval);
-		mux->last_chan = err < 0 ? 0 : regval;
+		mux->last_val = err < 0 ? -1 : regval;
 	}
 
 	return err;
@@ -90,9 +90,9 @@ static int mlxcpld_mux_deselect(struct i2c_mux_core *muxc, u32 chan)
 	struct mlxcpld_mux *mux = i2c_mux_priv(muxc);
 
 	/* Deselect active channel */
-	mux->last_chan = 0;
+	mux->last_val = -1;
 
-	return mlxcpld_mux_reg_write(muxc->parent, mux, mux->last_chan);
+	return mlxcpld_mux_reg_write(muxc->parent, mux, 0);
 }
 
 /* Probe/reomove functions */
@@ -122,7 +122,7 @@ static int mlxcpld_mux_probe(struct platform_device *pdev)
 	data = i2c_mux_priv(muxc);
 	data->client = client;
 	memcpy(&data->pdata, pdata, sizeof(*pdata));
-	data->last_chan = 0; /* force the first selection */
+	data->last_val = -1; /* force the first selection */
 
 	/* Create an adapter for each channel. */
 	for (num = 0; num < CPLD_MUX_MAX_NCHANS; num++) {
-- 
2.11.0


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH i2c-next v3 3/6] i2c: mux: mlxcpld: Rename mux ids array
  2021-01-30 17:34 [PATCH i2c-next v3 0/6] i2c: mux: mlxcpld: Extend driver functionality Vadim Pasternak
  2021-01-30 17:34 ` [PATCH i2c-next v3 1/6] i2c: mux: mlxcpld: Convert driver to platform driver Vadim Pasternak
  2021-01-30 17:34 ` [PATCH i2c-next v3 2/6] i2c: mux: mlxcpld: Prepare mux selection infrastructure for two-byte support Vadim Pasternak
@ 2021-01-30 17:34 ` Vadim Pasternak
  2021-02-01  9:09   ` Peter Rosin
  2021-01-30 17:34 ` [PATCH i2c-next v3 4/6] i2c: mux: mlxcpld: Extend driver to support word address space devices Vadim Pasternak
                   ` (2 subsequent siblings)
  5 siblings, 1 reply; 9+ messages in thread
From: Vadim Pasternak @ 2021-01-30 17:34 UTC (permalink / raw)
  To: peda, wsa; +Cc: linux-i2c, Vadim Pasternak

Rename mux ids array from 'adap_ids' to 'chan_ids'.
The motivation is to prepare infrastructure to be able to:
- Create only the child adapters which are actually needed - for which
  channel ids are specified.
- To assign 'nrs' to these child adapters dynamically, with no 'nr'
  enforcement.

Signed-off-by: Vadim Pasternak <vadimp@nvidia.com>
---
 drivers/i2c/muxes/i2c-mux-mlxcpld.c   | 2 +-
 include/linux/platform_data/mlxcpld.h | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/i2c/muxes/i2c-mux-mlxcpld.c b/drivers/i2c/muxes/i2c-mux-mlxcpld.c
index 113ad84cdd94..9e9d74bd1059 100644
--- a/drivers/i2c/muxes/i2c-mux-mlxcpld.c
+++ b/drivers/i2c/muxes/i2c-mux-mlxcpld.c
@@ -130,7 +130,7 @@ static int mlxcpld_mux_probe(struct platform_device *pdev)
 			/* discard unconfigured channels */
 			break;
 
-		force = pdata->adap_ids[num];
+		force = pdata->chan_ids[num];
 
 		err = i2c_mux_add_adapter(muxc, force, num, 0);
 		if (err)
diff --git a/include/linux/platform_data/mlxcpld.h b/include/linux/platform_data/mlxcpld.h
index e6c18bf017dd..04d93c563c04 100644
--- a/include/linux/platform_data/mlxcpld.h
+++ b/include/linux/platform_data/mlxcpld.h
@@ -11,12 +11,12 @@
 /* Platform data for the CPLD I2C multiplexers */
 
 /* mlxcpld_mux_plat_data - per mux data, used with i2c_register_board_info
- * @adap_ids - adapter array
+ * @chan_ids - channels array
  * @num_adaps - number of adapters
  * @sel_reg_addr - mux select register offset in CPLD space
  */
 struct mlxcpld_mux_plat_data {
-	int *adap_ids;
+	int *chan_ids;
 	int num_adaps;
 	int sel_reg_addr;
 };
-- 
2.11.0


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH i2c-next v3 4/6] i2c: mux: mlxcpld: Extend driver to support word address space devices
  2021-01-30 17:34 [PATCH i2c-next v3 0/6] i2c: mux: mlxcpld: Extend driver functionality Vadim Pasternak
                   ` (2 preceding siblings ...)
  2021-01-30 17:34 ` [PATCH i2c-next v3 3/6] i2c: mux: mlxcpld: Rename mux ids array Vadim Pasternak
@ 2021-01-30 17:34 ` Vadim Pasternak
  2021-02-01  9:09   ` Peter Rosin
  2021-01-30 17:34 ` [PATCH i2c-next v3 5/6] i2c: mux: mlxcpld: Extend supported mux number Vadim Pasternak
  2021-01-30 17:34 ` [PATCH i2c-next v3 6/6] i2c: mux: mlxcpld: Add callback to notify mux creation completion Vadim Pasternak
  5 siblings, 1 reply; 9+ messages in thread
From: Vadim Pasternak @ 2021-01-30 17:34 UTC (permalink / raw)
  To: peda, wsa; +Cc: linux-i2c, Vadim Pasternak

Extend driver to allow I2C routing control through CPLD devices with
word address space. Till now only CPLD devices with byte address space
have been supported.

Signed-off-by: Vadim Pasternak <vadimp@nvidia.com>
Reviewed-by: Michael Shych <michaelsh@nvidia.com>
---
v2->v3:
 Comments pointed out by Peter:
 - Do not change mlxcpld_mux_reg_write() argumnet "val" to "chan",
   since it is misleading for the one-byte case.
 - Drop cpu_to_be16() conversion from mlxcpld_mux_reg_write().
v1->v2:
 Comments pointed out by Peter:
 - Remove data buffer allocation from 'mlxcpld_mux' structure, do it on
   stack instead.
 - Do not use array pdata.adap_ids[] in mlxcpld_mux_reg_write() for
   channel assignment.
 - Return back 'regval' variable, used for channel assignment in
   mlxcpld_mux_select_chan().
 - Fix functionality validation in mlxcpld_mux_probe().
 - Fix comment for 'reg_size' field in mlxcpld_mux_plat_data' structure.
  Added by Vadim:
  - Change type of register select address to '__be16' to align with
    type in assignment in cpu_to_be16().
---
 drivers/i2c/muxes/i2c-mux-mlxcpld.c   | 47 +++++++++++++++++++++++++++++------
 include/linux/platform_data/mlxcpld.h |  2 ++
 2 files changed, 41 insertions(+), 8 deletions(-)

diff --git a/drivers/i2c/muxes/i2c-mux-mlxcpld.c b/drivers/i2c/muxes/i2c-mux-mlxcpld.c
index 9e9d74bd1059..71d4b8813704 100644
--- a/drivers/i2c/muxes/i2c-mux-mlxcpld.c
+++ b/drivers/i2c/muxes/i2c-mux-mlxcpld.c
@@ -63,19 +63,39 @@ static int mlxcpld_mux_reg_write(struct i2c_adapter *adap,
 				 struct mlxcpld_mux *mux, u32 val)
 {
 	struct i2c_client *client = mux->client;
-	union i2c_smbus_data data = { .byte = val };
-
-	return __i2c_smbus_xfer(adap, client->addr, client->flags,
-				I2C_SMBUS_WRITE, mux->pdata.sel_reg_addr,
-				I2C_SMBUS_BYTE_DATA, &data);
+	union i2c_smbus_data data;
+	struct i2c_msg msg;
+	u8 buf[3];
+
+	switch (mux->pdata.reg_size) {
+	case 1:
+		data.byte = val;
+		return __i2c_smbus_xfer(adap, client->addr, client->flags,
+					I2C_SMBUS_WRITE, mux->pdata.sel_reg_addr,
+					I2C_SMBUS_BYTE_DATA, &data);
+	case 2:
+		buf[0] = mux->pdata.sel_reg_addr >> 8;
+		buf[1] = mux->pdata.sel_reg_addr;
+		buf[2] = val;
+		msg.addr = client->addr;
+		msg.buf = buf;
+		msg.len = mux->pdata.reg_size + 1;
+		msg.flags = 0;
+		return __i2c_transfer(adap, &msg, 1);
+	default:
+		return -EINVAL;
+	}
 }
 
 static int mlxcpld_mux_select_chan(struct i2c_mux_core *muxc, u32 chan)
 {
 	struct mlxcpld_mux *mux = i2c_mux_priv(muxc);
-	u32 regval = chan + 1;
+	u32 regval = chan;
 	int err = 0;
 
+	if (mux->pdata.reg_size == 1)
+		regval += 1;
+
 	/* Only select the channel if its different from the last channel */
 	if (mux->last_val != regval) {
 		err = mlxcpld_mux_reg_write(muxc->parent, mux, regval);
@@ -103,13 +123,24 @@ static int mlxcpld_mux_probe(struct platform_device *pdev)
 	struct i2c_mux_core *muxc;
 	int num, force;
 	struct mlxcpld_mux *data;
+	u32 func;
 	int err;
 
 	if (!pdata)
 		return -EINVAL;
 
-	if (!i2c_check_functionality(client->adapter,
-				     I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
+	switch (pdata->reg_size) {
+	case 1:
+		func = I2C_FUNC_SMBUS_WRITE_BYTE_DATA;
+		break;
+	case 2:
+		func = I2C_FUNC_I2C;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	if (!i2c_check_functionality(client->adapter, func))
 		return -ENODEV;
 
 	muxc = i2c_mux_alloc(client->adapter, &pdev->dev, CPLD_MUX_MAX_NCHANS,
diff --git a/include/linux/platform_data/mlxcpld.h b/include/linux/platform_data/mlxcpld.h
index 04d93c563c04..a7bee798d991 100644
--- a/include/linux/platform_data/mlxcpld.h
+++ b/include/linux/platform_data/mlxcpld.h
@@ -14,11 +14,13 @@
  * @chan_ids - channels array
  * @num_adaps - number of adapters
  * @sel_reg_addr - mux select register offset in CPLD space
+ * @reg_size: register size in bytes
  */
 struct mlxcpld_mux_plat_data {
 	int *chan_ids;
 	int num_adaps;
 	int sel_reg_addr;
+	u8 reg_size;
 };
 
 #endif /* _LINUX_I2C_MLXCPLD_H */
-- 
2.11.0


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH i2c-next v3 5/6] i2c: mux: mlxcpld: Extend supported mux number
  2021-01-30 17:34 [PATCH i2c-next v3 0/6] i2c: mux: mlxcpld: Extend driver functionality Vadim Pasternak
                   ` (3 preceding siblings ...)
  2021-01-30 17:34 ` [PATCH i2c-next v3 4/6] i2c: mux: mlxcpld: Extend driver to support word address space devices Vadim Pasternak
@ 2021-01-30 17:34 ` Vadim Pasternak
  2021-01-30 17:34 ` [PATCH i2c-next v3 6/6] i2c: mux: mlxcpld: Add callback to notify mux creation completion Vadim Pasternak
  5 siblings, 0 replies; 9+ messages in thread
From: Vadim Pasternak @ 2021-01-30 17:34 UTC (permalink / raw)
  To: peda, wsa; +Cc: linux-i2c, Vadim Pasternak

Allow to extend mux number supported by driver.
Currently it is limited by eight, which is not enough for new coming
Mellanox modular system with line cards, which require up to 64 mux
support.

Signed-off-by: Vadim Pasternak <vadimp@nvidia.com>
Reviewed-by: Michael Shych <michaelsh@nvidia.com>
---
v1->v2:
 Comments pointed out by Peter:
 - Remove introducing of 'base_nr' field.
 - Drop chan increment in mlxcpld_mux_select_chan().
 Added by Vadim:
 - Rename 'adaps_ids' array to 'chan_ids' array.
 - Drop forcing of adapter 'nr'.
---
 drivers/i2c/muxes/i2c-mux-mlxcpld.c | 17 ++++-------------
 1 file changed, 4 insertions(+), 13 deletions(-)

diff --git a/drivers/i2c/muxes/i2c-mux-mlxcpld.c b/drivers/i2c/muxes/i2c-mux-mlxcpld.c
index 71d4b8813704..5e0672f9979b 100644
--- a/drivers/i2c/muxes/i2c-mux-mlxcpld.c
+++ b/drivers/i2c/muxes/i2c-mux-mlxcpld.c
@@ -15,8 +15,6 @@
 #include <linux/platform_device.h>
 #include <linux/slab.h>
 
-#define CPLD_MUX_MAX_NCHANS	8
-
 /* mlxcpld_mux - mux control structure:
  * @last_val - last selected register value or -1 if mux deselected
  * @client - I2C device client
@@ -121,10 +119,9 @@ static int mlxcpld_mux_probe(struct platform_device *pdev)
 	struct mlxcpld_mux_plat_data *pdata = dev_get_platdata(&pdev->dev);
 	struct i2c_client *client = to_i2c_client(pdev->dev.parent);
 	struct i2c_mux_core *muxc;
-	int num, force;
 	struct mlxcpld_mux *data;
+	int num, err;
 	u32 func;
-	int err;
 
 	if (!pdata)
 		return -EINVAL;
@@ -143,7 +140,7 @@ static int mlxcpld_mux_probe(struct platform_device *pdev)
 	if (!i2c_check_functionality(client->adapter, func))
 		return -ENODEV;
 
-	muxc = i2c_mux_alloc(client->adapter, &pdev->dev, CPLD_MUX_MAX_NCHANS,
+	muxc = i2c_mux_alloc(client->adapter, &pdev->dev, pdata->num_adaps,
 			     sizeof(*data), 0, mlxcpld_mux_select_chan,
 			     mlxcpld_mux_deselect);
 	if (!muxc)
@@ -156,14 +153,8 @@ static int mlxcpld_mux_probe(struct platform_device *pdev)
 	data->last_val = -1; /* force the first selection */
 
 	/* Create an adapter for each channel. */
-	for (num = 0; num < CPLD_MUX_MAX_NCHANS; num++) {
-		if (num >= pdata->num_adaps)
-			/* discard unconfigured channels */
-			break;
-
-		force = pdata->chan_ids[num];
-
-		err = i2c_mux_add_adapter(muxc, force, num, 0);
+	for (num = 0; num < pdata->num_adaps; num++) {
+		err = i2c_mux_add_adapter(muxc, 0, pdata->chan_ids[num], 0);
 		if (err)
 			goto virt_reg_failed;
 	}
-- 
2.11.0


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH i2c-next v3 6/6] i2c: mux: mlxcpld: Add callback to notify mux creation completion
  2021-01-30 17:34 [PATCH i2c-next v3 0/6] i2c: mux: mlxcpld: Extend driver functionality Vadim Pasternak
                   ` (4 preceding siblings ...)
  2021-01-30 17:34 ` [PATCH i2c-next v3 5/6] i2c: mux: mlxcpld: Extend supported mux number Vadim Pasternak
@ 2021-01-30 17:34 ` Vadim Pasternak
  5 siblings, 0 replies; 9+ messages in thread
From: Vadim Pasternak @ 2021-01-30 17:34 UTC (permalink / raw)
  To: peda, wsa; +Cc: linux-i2c, Vadim Pasternak

Add notification to inform caller that mux objects array has been
created. It allows to user, invoked platform device registration for
"i2c-mux-mlxcpld" driver, to be notified that mux infrastructure is
available, and thus some devices could be connected to this
infrastructure.

Signed-off-by: Vadim Pasternak <vadimp@nvidia.com>
---
 drivers/i2c/muxes/i2c-mux-mlxcpld.c   | 4 ++++
 include/linux/platform_data/mlxcpld.h | 5 +++++
 2 files changed, 9 insertions(+)

diff --git a/drivers/i2c/muxes/i2c-mux-mlxcpld.c b/drivers/i2c/muxes/i2c-mux-mlxcpld.c
index 5e0672f9979b..1a879f6a31ef 100644
--- a/drivers/i2c/muxes/i2c-mux-mlxcpld.c
+++ b/drivers/i2c/muxes/i2c-mux-mlxcpld.c
@@ -159,6 +159,10 @@ static int mlxcpld_mux_probe(struct platform_device *pdev)
 			goto virt_reg_failed;
 	}
 
+	/* Notify caller when all channels' adapters are created. */
+	if (pdata->completion_notify)
+		pdata->completion_notify(pdata->handle, muxc->parent, muxc->adapter);
+
 	return 0;
 
 virt_reg_failed:
diff --git a/include/linux/platform_data/mlxcpld.h b/include/linux/platform_data/mlxcpld.h
index a7bee798d991..d7610b528856 100644
--- a/include/linux/platform_data/mlxcpld.h
+++ b/include/linux/platform_data/mlxcpld.h
@@ -15,12 +15,17 @@
  * @num_adaps - number of adapters
  * @sel_reg_addr - mux select register offset in CPLD space
  * @reg_size: register size in bytes
+ * @handle: handle to be passed by callback
+ * @completion_notify: callback to notify when all the adapters are created
  */
 struct mlxcpld_mux_plat_data {
 	int *chan_ids;
 	int num_adaps;
 	int sel_reg_addr;
 	u8 reg_size;
+	void *handle;
+	int (*completion_notify)(void *handle, struct i2c_adapter *parent,
+				 struct i2c_adapter *adapters[]);
 };
 
 #endif /* _LINUX_I2C_MLXCPLD_H */
-- 
2.11.0


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH i2c-next v3 3/6] i2c: mux: mlxcpld: Rename mux ids array
  2021-01-30 17:34 ` [PATCH i2c-next v3 3/6] i2c: mux: mlxcpld: Rename mux ids array Vadim Pasternak
@ 2021-02-01  9:09   ` Peter Rosin
  0 siblings, 0 replies; 9+ messages in thread
From: Peter Rosin @ 2021-02-01  9:09 UTC (permalink / raw)
  To: Vadim Pasternak, wsa; +Cc: linux-i2c

Hi!

On 2021-01-30 18:34, Vadim Pasternak wrote:
> Rename mux ids array from 'adap_ids' to 'chan_ids'.
> The motivation is to prepare infrastructure to be able to:
> - Create only the child adapters which are actually needed - for which
>   channel ids are specified.
> - To assign 'nrs' to these child adapters dynamically, with no 'nr'
>   enforcement.

The series is starting to take shape!

However, it's still bad to only change the name of adap_id to chan_id
here. I.e., the division between 3/6 and 5/6 makes no sense at all.
For as long as the variable is used to fill in the force_nr argument of
i2c_mux_add_adapter, the name adap_id makes sense. It's only when the
variable is used to fill in the chan_id argument of that function that
the name chan_id makes more sense.

See, it is not just a rename, in the end you have changed the meaning.
You should change the name at the same time you change the semantics.

I.e., this patch needs to include the parts of 5/6 that deals with
argument two and three of the i2c_mux_add_adapter call (but not the
parts that deal with the CPLD_MUX_MAX_NCHANS to pdata->num_adaps
transition, I feel that should remain a separate patch).

Cheers,
Peter

> Signed-off-by: Vadim Pasternak <vadimp@nvidia.com>
> ---
>  drivers/i2c/muxes/i2c-mux-mlxcpld.c   | 2 +-
>  include/linux/platform_data/mlxcpld.h | 4 ++--
>  2 files changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/i2c/muxes/i2c-mux-mlxcpld.c b/drivers/i2c/muxes/i2c-mux-mlxcpld.c
> index 113ad84cdd94..9e9d74bd1059 100644
> --- a/drivers/i2c/muxes/i2c-mux-mlxcpld.c
> +++ b/drivers/i2c/muxes/i2c-mux-mlxcpld.c
> @@ -130,7 +130,7 @@ static int mlxcpld_mux_probe(struct platform_device *pdev)
>  			/* discard unconfigured channels */
>  			break;
>  
> -		force = pdata->adap_ids[num];
> +		force = pdata->chan_ids[num];
>  
>  		err = i2c_mux_add_adapter(muxc, force, num, 0);
>  		if (err)
> diff --git a/include/linux/platform_data/mlxcpld.h b/include/linux/platform_data/mlxcpld.h
> index e6c18bf017dd..04d93c563c04 100644
> --- a/include/linux/platform_data/mlxcpld.h
> +++ b/include/linux/platform_data/mlxcpld.h
> @@ -11,12 +11,12 @@
>  /* Platform data for the CPLD I2C multiplexers */
>  
>  /* mlxcpld_mux_plat_data - per mux data, used with i2c_register_board_info
> - * @adap_ids - adapter array
> + * @chan_ids - channels array
>   * @num_adaps - number of adapters
>   * @sel_reg_addr - mux select register offset in CPLD space
>   */
>  struct mlxcpld_mux_plat_data {
> -	int *adap_ids;
> +	int *chan_ids;
>  	int num_adaps;
>  	int sel_reg_addr;
>  };
> 

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH i2c-next v3 4/6] i2c: mux: mlxcpld: Extend driver to support word address space devices
  2021-01-30 17:34 ` [PATCH i2c-next v3 4/6] i2c: mux: mlxcpld: Extend driver to support word address space devices Vadim Pasternak
@ 2021-02-01  9:09   ` Peter Rosin
  0 siblings, 0 replies; 9+ messages in thread
From: Peter Rosin @ 2021-02-01  9:09 UTC (permalink / raw)
  To: Vadim Pasternak, wsa; +Cc: linux-i2c

Hi!

On 2021-01-30 18:34, Vadim Pasternak wrote:
> Extend driver to allow I2C routing control through CPLD devices with
> word address space. Till now only CPLD devices with byte address space
> have been supported.
> 
> Signed-off-by: Vadim Pasternak <vadimp@nvidia.com>
> Reviewed-by: Michael Shych <michaelsh@nvidia.com>
> ---
> v2->v3:
>  Comments pointed out by Peter:
>  - Do not change mlxcpld_mux_reg_write() argumnet "val" to "chan",
>    since it is misleading for the one-byte case.
>  - Drop cpu_to_be16() conversion from mlxcpld_mux_reg_write().
> v1->v2:
>  Comments pointed out by Peter:
>  - Remove data buffer allocation from 'mlxcpld_mux' structure, do it on
>    stack instead.
>  - Do not use array pdata.adap_ids[] in mlxcpld_mux_reg_write() for
>    channel assignment.
>  - Return back 'regval' variable, used for channel assignment in
>    mlxcpld_mux_select_chan().
>  - Fix functionality validation in mlxcpld_mux_probe().
>  - Fix comment for 'reg_size' field in mlxcpld_mux_plat_data' structure.
>   Added by Vadim:
>   - Change type of register select address to '__be16' to align with
>     type in assignment in cpu_to_be16().
> ---
>  drivers/i2c/muxes/i2c-mux-mlxcpld.c   | 47 +++++++++++++++++++++++++++++------
>  include/linux/platform_data/mlxcpld.h |  2 ++
>  2 files changed, 41 insertions(+), 8 deletions(-)
> 
> diff --git a/drivers/i2c/muxes/i2c-mux-mlxcpld.c b/drivers/i2c/muxes/i2c-mux-mlxcpld.c
> index 9e9d74bd1059..71d4b8813704 100644
> --- a/drivers/i2c/muxes/i2c-mux-mlxcpld.c
> +++ b/drivers/i2c/muxes/i2c-mux-mlxcpld.c
> @@ -63,19 +63,39 @@ static int mlxcpld_mux_reg_write(struct i2c_adapter *adap,
>  				 struct mlxcpld_mux *mux, u32 val)
>  {
>  	struct i2c_client *client = mux->client;
> -	union i2c_smbus_data data = { .byte = val };
> -
> -	return __i2c_smbus_xfer(adap, client->addr, client->flags,
> -				I2C_SMBUS_WRITE, mux->pdata.sel_reg_addr,
> -				I2C_SMBUS_BYTE_DATA, &data);
> +	union i2c_smbus_data data;
> +	struct i2c_msg msg;
> +	u8 buf[3];
> +
> +	switch (mux->pdata.reg_size) {
> +	case 1:
> +		data.byte = val;
> +		return __i2c_smbus_xfer(adap, client->addr, client->flags,
> +					I2C_SMBUS_WRITE, mux->pdata.sel_reg_addr,
> +					I2C_SMBUS_BYTE_DATA, &data);
> +	case 2:
> +		buf[0] = mux->pdata.sel_reg_addr >> 8;
> +		buf[1] = mux->pdata.sel_reg_addr;
> +		buf[2] = val;
> +		msg.addr = client->addr;
> +		msg.buf = buf;
> +		msg.len = mux->pdata.reg_size + 1;
> +		msg.flags = 0;
> +		return __i2c_transfer(adap, &msg, 1);
> +	default:
> +		return -EINVAL;
> +	}
>  }
>  
>  static int mlxcpld_mux_select_chan(struct i2c_mux_core *muxc, u32 chan)
>  {
>  	struct mlxcpld_mux *mux = i2c_mux_priv(muxc);
> -	u32 regval = chan + 1;
> +	u32 regval = chan;
>  	int err = 0;
>  
> +	if (mux->pdata.reg_size == 1)
> +		regval += 1;
> +

I can only think of one reason to keep this offset between chan and regval,
and that is if you need to be compatible with some old one-byte case that
absolutely must have channels 0-7, that can't be adapted to channels 1-8
instead. If you are able to change all cases expecting channels 0-7 to
expect 1-8 at the same time you update the kernel, then I would simply
drop this conditional offset.

If you can't change that, then I think you should add a comment for why
this offset between the channel and the regval exists. It just looks odd
and convuluted, and it begs for an explanation when the offset is only
there for the one-byte case and not for the two-byte case. When I see it,
my brain fires up and I start wondering what the ?"#%& is going on :-)

Cheers,
Peter

>  	/* Only select the channel if its different from the last channel */
>  	if (mux->last_val != regval) {
>  		err = mlxcpld_mux_reg_write(muxc->parent, mux, regval);
> @@ -103,13 +123,24 @@ static int mlxcpld_mux_probe(struct platform_device *pdev)
>  	struct i2c_mux_core *muxc;
>  	int num, force;
>  	struct mlxcpld_mux *data;
> +	u32 func;
>  	int err;
>  
>  	if (!pdata)
>  		return -EINVAL;
>  
> -	if (!i2c_check_functionality(client->adapter,
> -				     I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
> +	switch (pdata->reg_size) {
> +	case 1:
> +		func = I2C_FUNC_SMBUS_WRITE_BYTE_DATA;
> +		break;
> +	case 2:
> +		func = I2C_FUNC_I2C;
> +		break;
> +	default:
> +		return -EINVAL;
> +	}
> +
> +	if (!i2c_check_functionality(client->adapter, func))
>  		return -ENODEV;
>  
>  	muxc = i2c_mux_alloc(client->adapter, &pdev->dev, CPLD_MUX_MAX_NCHANS,
> diff --git a/include/linux/platform_data/mlxcpld.h b/include/linux/platform_data/mlxcpld.h
> index 04d93c563c04..a7bee798d991 100644
> --- a/include/linux/platform_data/mlxcpld.h
> +++ b/include/linux/platform_data/mlxcpld.h
> @@ -14,11 +14,13 @@
>   * @chan_ids - channels array
>   * @num_adaps - number of adapters
>   * @sel_reg_addr - mux select register offset in CPLD space
> + * @reg_size: register size in bytes
>   */
>  struct mlxcpld_mux_plat_data {
>  	int *chan_ids;
>  	int num_adaps;
>  	int sel_reg_addr;
> +	u8 reg_size;
>  };
>  
>  #endif /* _LINUX_I2C_MLXCPLD_H */
> 

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2021-02-01  9:12 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-30 17:34 [PATCH i2c-next v3 0/6] i2c: mux: mlxcpld: Extend driver functionality Vadim Pasternak
2021-01-30 17:34 ` [PATCH i2c-next v3 1/6] i2c: mux: mlxcpld: Convert driver to platform driver Vadim Pasternak
2021-01-30 17:34 ` [PATCH i2c-next v3 2/6] i2c: mux: mlxcpld: Prepare mux selection infrastructure for two-byte support Vadim Pasternak
2021-01-30 17:34 ` [PATCH i2c-next v3 3/6] i2c: mux: mlxcpld: Rename mux ids array Vadim Pasternak
2021-02-01  9:09   ` Peter Rosin
2021-01-30 17:34 ` [PATCH i2c-next v3 4/6] i2c: mux: mlxcpld: Extend driver to support word address space devices Vadim Pasternak
2021-02-01  9:09   ` Peter Rosin
2021-01-30 17:34 ` [PATCH i2c-next v3 5/6] i2c: mux: mlxcpld: Extend supported mux number Vadim Pasternak
2021-01-30 17:34 ` [PATCH i2c-next v3 6/6] i2c: mux: mlxcpld: Add callback to notify mux creation completion Vadim Pasternak

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).