Skip to contentSkip to conversation

i2c: add sc18is600 driver

PATCH In review Sebastian Reichel 2 participants 2 messages Jul 1, 2017 – Jul 29, 2017

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

PATCH v2 1/1 i2c: add sc18is600 driver

This adds an I²C master driver for SPI -> I²C bus bridge chips. It currently supports NXP's SC18IS600 and SC18IS601, as well as Silicon Labs' CP2120. The driver was only tested on SC18IS600.

---
Changes since PATCHv1:
* Reported by Julia Lawall / buildbot
- fix double initializations of buffer_size and clock_base
* Reported by Andy Shevchenko
- Use clamp_val() instead of open coding
- Simplify some statements
- Use of_device_get_match_data() instead of of_match_device()
- Check for clk_prepare_enable() failure
- Introduce SC18IS600_CLK_DIVIDER define instead of magic number
- Rename struct sc18is600dev *dev to *s600dev.
* Reported by Wolfram Sang
- Move config to "External I2C/SMBus adapter drivers"
- Drop debug messages
- Use -ENXIO on address NAK
* Add support for I2C_M_IGNORE_NAK

---
 .../devicetree/bindings/i2c/i2c-cp2120.txt         |   1 +
 .../devicetree/bindings/i2c/i2c-sc18is600.txt      |  62 +++
 drivers/i2c/busses/Kconfig                         |  10 +
 drivers/i2c/busses/Makefile                        |   1 +
 drivers/i2c/busses/i2c-sc18is600.c                 | 575 +++++++++++++++++++++
 5 files changed, 649 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/i2c/i2c-cp2120.txt
 create mode 100644 Documentation/devicetree/bindings/i2c/i2c-sc18is600.txt
 create mode 100644 drivers/i2c/busses/i2c-sc18is600.c
Documentation/devicetree/bindings/i2c/i2c-cp2120.txt new file +1 -0
new file mode 100644
index 000000000000..95e06e74f288
@@ -0,0 +1 @@
+Please see binding for i2c-sc18is600
Documentation/devicetree/bindings/i2c/i2c-sc18is600.txt new file +62 -0

62 lines not on this page · raw message

drivers/i2c/busses/Kconfig +10 -0
index 144cbadc7c72..572ac87fd37a 100644
@@ -1123,6 +1123,16 @@ config I2C_ROBOTFUZZ_OSIF
 	  This driver can also be built as a module.  If so, the module
 	  will be called i2c-osif.
 
+config I2C_SC18IS600
+	tristate "NXP SC18IS600"
+	depends on SPI && REGMAP
+	help
+	  If you say yes to this option, support will be included for the
+	  NXP SC18IS600 SPI to I2C-bus interface.
+
+	  This driver can also be built as a module. If so, the module
+	  will be called i2c-sc18is600.
+
 config I2C_TAOS_EVM
 	tristate "TAOS evaluation module"
 	depends on TTY
drivers/i2c/busses/Makefile +1 -0
index 30b60855fbcd..f3552dd3f91c 100644
@@ -109,6 +109,7 @@ obj-$(CONFIG_I2C_DLN2)		+= i2c-dln2.o
 obj-$(CONFIG_I2C_PARPORT)	+= i2c-parport.o
 obj-$(CONFIG_I2C_PARPORT_LIGHT)	+= i2c-parport-light.o
 obj-$(CONFIG_I2C_ROBOTFUZZ_OSIF)	+= i2c-robotfuzz-osif.o
+obj-$(CONFIG_I2C_SC18IS600)	+= i2c-sc18is600.o
 obj-$(CONFIG_I2C_TAOS_EVM)	+= i2c-taos-evm.o
 obj-$(CONFIG_I2C_TINY_USB)	+= i2c-tiny-usb.o
 obj-$(CONFIG_I2C_VIPERBOARD)	+= i2c-viperboard.o
drivers/i2c/busses/i2c-sc18is600.c new file +575 -0

575 lines not on this page · raw message

Signature · 1 line
2.11.0
#1 lore raw

Hi,

thanks for V2.

Quoted 3 lines · Sebastian Reichel

This adds an I²C master driver for SPI -> I²C bus bridge chips. It currently supports NXP's SC18IS600 and SC18IS601, as well as Silicon Labs' CP2120. The driver was only tested on SC18IS600.

I prefer 'I2C' to "I²C' to keep things simple, but I'll leave it to you.

Documentation/devicetree/bindings/i2c/i2c-cp2120.txt line 1

Quoted 3 lines

+++ b/Documentation/devicetree/bindings/i2c/i2c-cp2120.txt

Documentation/devicetree/bindings/i2c/i2c-cp2120.txt new file +1 -0
@@ -0,0 +1 @@
+Please see binding for i2c-sc18is600

I still prefer this to be dropped. There is no i2c-cp2120 driver.

Quoted 3 lines

+ * Datasheets:
+ * - http://www.nxp.com/documents/data_sheet/SC18IS600.pdf
+ * - https://www.silabs.com/documents/public/data-sheets/CP2120.pdf

Nice pointers.

Quoted 1 line

+static int sc18is600_read_write(struct sc18is600dev *s600dev,

This function used to be named '*_read_after_write' which is correct. '*_read_write' sounds like it does the opposite!

Quoted 5 lines
drivers/i2c/busses/i2c-sc18is600.c new file +5 -0
+static int sc18is600_xfer(struct i2c_adapter *adapter,
+			  struct i2c_msg *msgs, int num)
+{
+	struct sc18is600dev *s600dev = adapter->algo_data;
+	int read_operations = 0;

drivers/i2c/busses/i2c-sc18is600.c line 345

Quoted 10 lines
drivers/i2c/busses/i2c-sc18is600.c new file +10 -0
+	bool ignore_nak = false;
+	int i, err;
+
+	for (i = 0; i < num; i++) {
+		if (msgs[i].len > s600dev->chip->buffer_size)
+			return -EOPNOTSUPP;
+
+		/* chip only support standard read & write */
+		if (msgs[i].flags & ~I2C_M_RD)
+			return -EOPNOTSUPP;

This will bail out on I2C_M_IGNORE_NAK which you added support for. Didn't you test that?

drivers/i2c/busses/i2c-sc18is600.c line 349

Quoted 4 lines
drivers/i2c/busses/i2c-sc18is600.c new file +4 -0
+
+		if (msgs[i].flags & I2C_M_RD)
+			read_operations++;
+	}

I'd think the for loop can completely go by populating an i2c_quirks structure. From what I see, you'd need I2C_AQ_COMB | I2C_AQ_COMB_WRITE_FIRST with max_comb_1st_msg_len and max_comb_2nd_msg_len properly set. But please double check.

drivers/i2c/busses/i2c-sc18is600.c line 355

Quoted 6 lines
drivers/i2c/busses/i2c-sc18is600.c new file +6 -0
+
+	reinit_completion(&s600dev->completion);
+
+	if (num == 1) {
+		if (msgs[0].flags & I2C_M_IGNORE_NAK)
+			ignore_nak = true;

Why do you ignore NAK only for 1 message?

drivers/i2c/busses/i2c-sc18is600.c line 368

Quoted 13 lines
drivers/i2c/busses/i2c-sc18is600.c new file +13 -0
+
+		if (read_operations == 1)
+			err = sc18is600_read(s600dev, &msgs[0]);
+		else
+			err = sc18is600_write(s600dev, &msgs[0]);
+	} else if (num == 2) {
+		if (read_operations == 1)
+			err = sc18is600_read_write(s600dev, &msgs[0], &msgs[1]);
+		else
+			err = sc18is600_write_write(s600dev, &msgs[0], &msgs[1]);
+	} else {
+		return -EOPNOTSUPP;
+	}

With i2c_quirks, the last else-block could also go.

drivers/i2c/busses/i2c-sc18is600.c line 379

Quoted 3 lines
drivers/i2c/busses/i2c-sc18is600.c new file +3 -0
+	if (!err) {
+		dev_warn(&s600dev->spi->dev,
+			 "timeout waiting for irq, poll status register");

Don't log on timeouts, it is not unusual to happen on the bus.

Quoted 4 lines
drivers/i2c/busses/i2c-sc18is600.c new file +4 -0
+		s600dev->state = SC18IS600_STAT_BUSY;
+		regmap_read(s600dev->regmap, SC18IS600_REG_I2C_STAT,
+			    &s600dev->state);
+	}

...

drivers/i2c/busses/i2c-sc18is600.c line 441

Quoted 3 lines
drivers/i2c/busses/i2c-sc18is600.c new file +3 -0
+	s600dev->spi = spi;
+	s600dev->adapter.owner = THIS_MODULE;
+	s600dev->adapter.class = I2C_CLASS_DEPRECATED;

Huh? This is a new driver, why do you need it? Hmm, maybe I could rephrase the docs a little.

...

drivers/i2c/busses/i2c-sc18is600.c line 511

Quoted 1 line
drivers/i2c/busses/i2c-sc18is600.c new file +1 -0
+		err = clk_prepare_enable(s600dev->clk);

You never disable the clock anywhere.

Regards,

Wolfram