i2c: add sc18is600 driver
Subsystems (2)
PATCH RESEND 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.
---
Hi,
This is identical to the patch [0], that I send 3 months ago and got zero feedback from I2C maintainers. I rebased to v4.12-rc5, which did not require any changes.
[0] https://lwn.net/Articles/718347/
-- Sebastian
--- .../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 | 572 +++++++++++++++++++++ 5 files changed, 646 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
@@ -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
@@ -210,6 +210,16 @@ config I2C_NFORCE2_S4985 This driver can also be built as a module. If so, the module will be called i2c-nforce2-s4985. +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_SIS5595 tristate "SiS 5595" depends on PCI
drivers/i2c/busses/Makefile +1 -0
@@ -18,6 +18,7 @@ obj-$(CONFIG_I2C_ISMT) += i2c-ismt.o obj-$(CONFIG_I2C_NFORCE2) += i2c-nforce2.o obj-$(CONFIG_I2C_NFORCE2_S4985) += i2c-nforce2-s4985.o obj-$(CONFIG_I2C_PIIX4) += i2c-piix4.o +obj-$(CONFIG_I2C_SC18IS600) += i2c-sc18is600.o obj-$(CONFIG_I2C_SIS5595) += i2c-sis5595.o obj-$(CONFIG_I2C_SIS630) += i2c-sis630.o obj-$(CONFIG_I2C_SIS96X) += i2c-sis96x.o
drivers/i2c/busses/i2c-sc18is600.c new file +572 -0
572 lines not on this page · raw message
Signature · 1 line
2.11.0
drivers/i2c/busses/i2c-sc18is600.c line 220
Quoted 13 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.
drivers/i2c/busses/i2c-sc18is600.c new file +8
-0+static void sc18is600_setup_clock_frequency (struct sc18is600dev *dev) +{ +int reg =DIV_ROUND_UP (dev->clock_base, dev->i2c_clock_frequency); + +if (reg <5 ) + reg =5 ; +if (reg >255 ) + reg =255 ;
clamp_t()
Quoted 6 lines
drivers/i2c/busses/i2c-sc18is600.c new file +5
-0+ +dev_dbg (&dev->spi->dev,"i2c clock frequency: %08x" , reg); +regmap_write (dev->regmap,SC18IS600_REG_I2C_CLOCK , reg); +} +static void sc18is600_setup_timeout(struct sc18is600dev *dev,
drivers/i2c/busses/i2c-sc18is600.c line 235
Quoted 10 lines
drivers/i2c/busses/i2c-sc18is600.c new file +9
-0+ bool enable, int timeout_ms) +{ + int timeout = DIV_ROUND_UP(timeout_ms * dev->chip->timeout_base, 10000); +u8 reg; + +if (timeout <=0 ) + timeout =1 ; +if (timeout >255 ) + timeout =255 ;
Ditto.
drivers/i2c/busses/i2c-sc18is600.c line 238
Quoted 3 lines
drivers/i2c/busses/i2c-sc18is600.c new file +3
-0+ + reg = (timeout &0x7F ) <<1 ; + reg |= (!!enable);
Redundant parens.
Might be one line.
Quoted 10 lines
drivers/i2c/busses/i2c-sc18is600.c new file +7
-0+ +dev_dbg (&dev->spi->dev,"i2c timeout: %08x" , reg); +regmap_write (dev->regmap,SC18IS600_REG_I2C_TIMEOUT , reg); +} +static int sc18is600_xfer (struct i2c_adapter *adapter, +struct i2c_msg *msgs,int num) +{
drivers/i2c/busses/i2c-sc18is600.c line 371
Quoted 10 lines
drivers/i2c/busses/i2c-sc18is600.c new file +10
-0+if (num ==1 && read_operations ==1 ) + err =sc18is600_read (dev, &msgs[0 ]); +else if (num ==1 ) + err =sc18is600_write (dev, &msgs[0 ]); +else if (num ==2 && read_operations ==1 ) + err =sc18is600_read_after_write (dev, &msgs[0 ], &msgs[1 ]); +else if (num ==2 ) + err =sc18is600_write_after_write (dev, &msgs[0 ], &msgs[1 ]); +else +return -EOPNOTSUPP ;
It will look better
if (x == 1) {
if (y)
else
} else if (x == 2) {
if (y)
else
}
Quoted 6 lines
drivers/i2c/busses/i2c-sc18is600.c new file +6
-0+switch (dev->state) { +case SC18IS600_STAT_OK : +break ; +case SC18IS600_STAT_NAK_ADDR : +return -EIO ; +case SC18IS600_STAT_NAK_DATA :
drivers/i2c/busses/i2c-sc18is600.c line 400
Quoted 10 lines
drivers/i2c/busses/i2c-sc18is600.c new file +9
-0+return -EREMOTEIO ; +case SC18IS600_STAT_SIZE : +return -EINVAL ; +case SC18IS600_STAT_TIMEOUT : +return -ETIMEDOUT ; +case SC18IS600_STAT_TIMEOUT2 : +return -ETIMEDOUT ; +case SC18IS600_STAT_BLOCKED : +return -ETIMEDOUT ;
You may use
case X:
case Y:
return Z;
Quoted 7 lines
drivers/i2c/busses/i2c-sc18is600.c new file +6
-0+default : +case SC18IS600_STAT_BUSY : +dev_err (&dev->spi->dev,"device hangup detected, reset!" ); +sc18is600_reset (dev); +return -EAGAIN ; + }
drivers/i2c/busses/i2c-sc18is600.c line 446
Quoted 10 lines
drivers/i2c/busses/i2c-sc18is600.c new file +9
-0+static int sc18is600_probe (struct spi_device *spi) +{ +const struct of_device_id *of_id; +struct sc18is600dev *dev; +int err; + + of_id =of_match_device (sc18is600_of_match, &spi->dev); +if (!of_id) +return -ENODEV ;
of_device_get_match_data() ?
drivers/i2c/busses/i2c-sc18is600.c line 449
Quoted 2 lines
drivers/i2c/busses/i2c-sc18is600.c new file +2
-0+ dev =devm_kzalloc (&spi->dev,sizeof (*dev),GFP_KERNEL ); +if (dev ==NULL )
if (!dev)
...and better to use s600dev or alike to avoid confusion.
drivers/i2c/busses/i2c-sc18is600.c line 464
Quoted 5 lines
drivers/i2c/busses/i2c-sc18is600.c new file +3
-0+return -ENOMEM ; + snprintf(dev->adapter.name, sizeof(dev->adapter.name), + "SC18IS600 at SPI %02d device %02d",
Isn't too much for adapter name?
I don't remember if it's part of ABI, in that case it's even worse.
drivers/i2c/busses/i2c-sc18is600.c line 519
Quoted 7 lines
drivers/i2c/busses/i2c-sc18is600.c new file +6
-0+ spi->master->bus_num, spi->chip_select); +if (!dev->chip->clock_base) { + dev->clk =devm_clk_get (&spi->dev,"clkin" ); +if (IS_ERR (dev->clk)) { + err =PTR_ERR (dev->clk); +dev_err (&spi->dev,"could not acquire vdd: %d\n" , err);
vdd-> Vdd ?
drivers/i2c/busses/i2c-sc18is600.c line 523
Quoted 5 lines
drivers/i2c/busses/i2c-sc18is600.c new file +4
-0+return err; + } + +clk_prepare_enable (dev->clk);
This might fail.
Quoted 1 line
+
dev->clock_base=clk_get_rate(dev->clk)/ 4;
Magic.
Quoted 3 lines
drivers/i2c/busses/i2c-sc18is600.c new file +3
-0+ }else { + dev->clock_base = dev->chip->clock_base; + }
Signature · 6 lines
With Best Regards, Andy Shevchenko -- To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Hi Andy,
Thanks for your feedback.
Quoted 10 lines · Andy Shevchenko
Quoted 8 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.
drivers/i2c/busses/i2c-sc18is600.c new file +4
-0+static void sc18is600_setup_clock_frequency (struct sc18is600dev *dev) +{ +int reg =DIV_ROUND_UP (dev->clock_base, dev->i2c_clock_frequency); +
Quoted 6 lines
drivers/i2c/busses/i2c-sc18is600.c line 220
Quoted 4 lines
drivers/i2c/busses/i2c-sc18is600.c new file +4
-0+if (reg <5 ) + reg =5 ; +if (reg >255 ) + reg =255 ;
clamp_t()
I will use clamp_val()
Quoted 3 lines
[...]
lot's of suggestions for improvements.
[...]
Ack.
Quoted 10 lines
...and better to use s600dev or alike to avoid confusion.
drivers/i2c/busses/i2c-sc18is600.c line 464
Quoted 5 lines
drivers/i2c/busses/i2c-sc18is600.c new file +3
-0+return -ENOMEM ; + snprintf(dev->adapter.name, sizeof(dev->adapter.name), + "SC18IS600 at SPI %02d device %02d",Isn't too much for adapter name?
I don't remember if it's part of ABI, in that case it's even worse.
Well, it's displayed by "i2cdetect -l". Here are the names from some other drivers, which I used as reference:
i2c-tiny-usb at bus %03d device %03d i2c-diolan-u2c at bus %03d device %03d OSIF at bus %03d device %03d SMBus Via Pro adapter at %04x SMBus SIS5595 adapter at %04x
Quoted 3 lines
[...]
lot's of suggestions for improvements.
[...]
Ack.
-- Sebastian
Quoted 1 line · Sebastian Reichel
Thanks for your feedback.
You're welcome!
Quoted 10 lines · Andy Shevchenko
Quoted 8 lines · Sebastian Reichel
drivers/i2c/busses/i2c-sc18is600.c line 464
Quoted 4 lines
drivers/i2c/busses/i2c-sc18is600.c new file +2
-0+ snprintf(dev->adapter.name, sizeof(dev->adapter.name), + "SC18IS600 at SPI %02d device %02d",Isn't too much for adapter name?
I don't remember if it's part of ABI, in that case it's even worse.
Quoted 8 lines
Well, it's displayed by "i2cdetect -l". Here are the names from some other drivers, which I used as reference:
i2c-tiny-usb at bus %03d device %03d i2c-diolan-u2c at bus %03d device %03d OSIF at bus %03d device %03d SMBus Via Pro adapter at %04x SMBus SIS5595 adapter at %04x
Ah, okay.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 12 lines
drivers/i2c/busses/i2c-sc18is600.c new file +11
-0+switch (dev->state) { +case SC18IS600_STAT_OK : +break ; +case SC18IS600_STAT_NAK_ADDR : +return -EIO ; +case SC18IS600_STAT_NAK_DATA : +return -EREMOTEIO ; +case SC18IS600_STAT_SIZE : +return -EINVAL ; +case SC18IS600_STAT_TIMEOUT : +return -ETIMEDOUT ;
drivers/i2c/busses/i2c-sc18is600.c line 406
Quoted 10 lines
drivers/i2c/busses/i2c-sc18is600.c new file +10
-0+case SC18IS600_STAT_TIMEOUT2 : +return -ETIMEDOUT ; +case SC18IS600_STAT_BLOCKED : +return -ETIMEDOUT ; +default : +case SC18IS600_STAT_BUSY : +dev_err (&dev->spi->dev,"device hangup detected, reset!" ); +sc18is600_reset (dev); +return -EAGAIN ; + }
Please have a look at Documentation/i2c/fault-codes. Most look good, but the NAK cases probably need fixing.
Quoted 3 lines
This is identical to the patch [0], that I send 3 months ago and got zero feedback from I2C maintainers.
I2C has not enough reviewers, this is a known issue. Pointing it out alone does not really help. You could have reviewed your own patch; after 3 months, one sometimes sees issues not noticed before. Or look at other driver reviews to find out about common issues in new drivers (like the suggested error codes on transmission failures)...
Documentation/devicetree/bindings/i2c/i2c-cp2120.txt line 1
Quoted 7 lines
Documentation/devicetree/bindings/i2c/i2c-cp2120.txt new file +1
-0@@ -0,0 +1 @@ +Please see binding for i2c-sc18is600
I'll leave it to Rob, but since there is no driver 'i2c-cp2120', I'd rather drop it.
Documentation/devicetree/bindings/i2c/i2c-sc18is600.txt
Quoted 5 lines
Documentation/devicetree/bindings/i2c/i2c-sc18is600.txt new file +0
-0
And Rob will ask you to send bindings as seperate patches, so I'll do already, too.
Quoted 8 lines
drivers/i2c/busses/Kconfig +0
-0@@ -210,6 +210,16 @@ config I2C_NFORCE2_S4985 This driver can also be built as a module. If so, the module will be called i2c-nforce2-s4985.
drivers/i2c/busses/Kconfig config I2C_NFORCE2_S4985 · line 222
Quoted 10 lines
drivers/i2c/busses/Kconfig +10
-0+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. +
It should rather go to the "External I2C/SMBus adapter drivers" section.
drivers/i2c/busses/Makefile obj-$(CONFIG_I2C_ISMT) += i2c-ismt.o · line 21
Quoted 12 lines
drivers/i2c/busses/Kconfig +0
-0config I2C_SIS5595 tristate "SiS 5595" depends on PCIdrivers/i2c/busses/Makefile +1
-0@@ -18,6 +18,7 @@ obj-$(CONFIG_I2C_ISMT) += i2c-ismt.o obj-$(CONFIG_I2C_NFORCE2) += i2c-nforce2.o obj-$(CONFIG_I2C_NFORCE2_S4985) += i2c-nforce2-s4985.o obj-$(CONFIG_I2C_PIIX4) += i2c-piix4.o +obj-$(CONFIG_I2C_SC18IS600) += i2c-sc18is600.o
Ditto.
drivers/i2c/busses/Makefile obj-$(CONFIG_I2C_ISMT) += i2c-ismt.o
Quoted 12 lines
drivers/i2c/busses/Makefile +9
-0obj-$(CONFIG_I2C_SIS5595) += i2c-sis5595.o obj-$(CONFIG_I2C_SIS630) += i2c-sis630.o obj-$(CONFIG_I2C_SIS96X) += i2c-sis96x.o +static int sc18is600_read(struct sc18is600dev *dev, struct i2c_msg *msg) +{ + u8 header[] = { SC18IS600_CMD_RD, msg->len, msg->addr << 1 }; + struct spi_transfer xfer[1] = { 0 }; + + xfer[0].tx_buf = header; + xfer[0].len = sizeof(header); + + dev_dbg(&dev->spi->dev, "r(addr=%x, len=%d)", msg->addr, msg->len);
Maybe you could drop these? We have tracing messages and core debug messages for printing message destination addresses and lengths. Just saying...
And what Andy said (thanks!). But looks quite good, in general.
Regards,
Wolfram