mbox series

[v10,0/4] Add Intel LJCA device driver

Message ID 1693091643-20867-1-git-send-email-wentong.wu@intel.com
Headers show
Series Add Intel LJCA device driver | expand

Message

Wentong Wu Aug. 26, 2023, 11:13 p.m. UTC
Add driver for Intel La Jolla Cove Adapter (LJCA) device. This is a
USB-GPIO, USB-I2C and USB-SPI device. We add 4 drivers to support this
device: a USB driver, a GPIO chip driver, a I2C controller driver and
a SPI controller driver.

---
v10:
 - remove ljca_i2c_format_slave_addr
 - remove memset before write write w_packet
 - make ljca_i2c_stop void and print err message in case failure
 - use dev_err_probe in ljca_i2c_probe function

v9:
 - overhaul usb-ljca driver to make it more structured and easy understand
 - fix memory leak issue for usb-ljca driver
 - add spinlock to protect tx_buf and ex_buf
 - change exported APIs for usb-ljca driver
 - unify prefix for structures and functions for i2c-ljca driver
 - unify prefix for structures and functions for spi-ljca driver
 - unify prefix for structures and functions for gpio-ljca driver
 - update gpio-ljca, i2c-ljca and spi-ljca drivers according to usb-ljca's changes

Wentong Wu (4):
  usb: Add support for Intel LJCA device
  i2c: Add support for Intel LJCA USB I2C driver
  spi: Add support for Intel LJCA USB SPI driver
  gpio: update Intel LJCA USB GPIO driver

 drivers/gpio/Kconfig          |   4 +-
 drivers/gpio/gpio-ljca.c      | 242 +++++++------
 drivers/i2c/busses/Kconfig    |  11 +
 drivers/i2c/busses/Makefile   |   1 +
 drivers/i2c/busses/i2c-ljca.c | 332 +++++++++++++++++
 drivers/spi/Kconfig           |  11 +
 drivers/spi/Makefile          |   1 +
 drivers/spi/spi-ljca.c        | 297 +++++++++++++++
 drivers/usb/misc/Kconfig      |  14 +
 drivers/usb/misc/Makefile     |   1 +
 drivers/usb/misc/usb-ljca.c   | 817 ++++++++++++++++++++++++++++++++++++++++++
 include/linux/usb/ljca.h      | 113 ++++++
 12 files changed, 1741 insertions(+), 103 deletions(-)
 create mode 100644 drivers/i2c/busses/i2c-ljca.c
 create mode 100644 drivers/spi/spi-ljca.c
 create mode 100644 drivers/usb/misc/usb-ljca.c
 create mode 100644 include/linux/usb/ljca.h

Comments

Wolfram Sang Aug. 28, 2023, 7:24 p.m. UTC | #1
Hi,

thank you for your patches!

...

> +static int ljca_i2c_start(struct ljca_i2c_dev *ljca_i2c, u8 slave_addr,
> +			  enum ljca_xfer_type type)
> +{
> +	struct ljca_i2c_rw_packet *w_packet =
> +			(struct ljca_i2c_rw_packet *)ljca_i2c->obuf;
> +	struct ljca_i2c_rw_packet *r_packet =
> +			(struct ljca_i2c_rw_packet *)ljca_i2c->ibuf;
> +	s16 rp_len;
> +	int ret;
> +
> +	w_packet->id = ljca_i2c->i2c_info->id;
> +	w_packet->len = cpu_to_le16(sizeof(*w_packet->data));
> +	w_packet->data[0] = (slave_addr << 1) | type;
> +
> +	ret = ljca_transfer(ljca_i2c->ljca, LJCA_I2C_START, w_packet,
> +			    struct_size(w_packet, data, 1), r_packet,
> +			    LJCA_I2C_BUF_SIZE);
> +	if (ret < 0 || ret < sizeof(*r_packet))
> +		return ret < 0 ? ret : -EIO;
> +
> +	rp_len = le16_to_cpu(r_packet->len);
> +	if (rp_len < 0 || r_packet->id != w_packet->id) {
> +		dev_err(&ljca_i2c->adap.dev,
> +			"i2c start failed len: %d id: %d %d\n",
> +			rp_len, r_packet->id, w_packet->id);

All dev_err look more like dev_dbg to me. They are not helpful for the
regular user, I'd think.

> +		return -EIO;
> +	}
> +
> +	return 0;
> +}
> +

...

> +static int ljca_i2c_pure_read(struct ljca_i2c_dev *ljca_i2c, u8 *data, u8 len)
> +{
> +	struct ljca_i2c_rw_packet *w_packet =
> +			(struct ljca_i2c_rw_packet *)ljca_i2c->obuf;
> +	struct ljca_i2c_rw_packet *r_packet =
> +			(struct ljca_i2c_rw_packet *)ljca_i2c->ibuf;
> +	s16 rp_len;
> +	int ret;
> +
> +	if (len > LJCA_I2C_MAX_XFER_SIZE)
> +		return -EINVAL;

You can remove this check. You already have a quirk structure, so the
core will do it for you.

...

> +static int ljca_i2c_pure_write(struct ljca_i2c_dev *ljca_i2c, u8 *data, u8 len)
> +{
> +	struct ljca_i2c_rw_packet *w_packet =
> +			(struct ljca_i2c_rw_packet *)ljca_i2c->obuf;
> +	struct ljca_i2c_rw_packet *r_packet =
> +			(struct ljca_i2c_rw_packet *)ljca_i2c->ibuf;
> +	s16 rplen;
> +	int ret;
> +
> +	if (len > LJCA_I2C_MAX_XFER_SIZE)
> +		return -EINVAL;

You can remove this check. You already have a quirk structure, so the
core will do it for you.

...

> +static u32 ljca_i2c_func(struct i2c_adapter *adap)
> +{
> +	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
> +}

Have you successfully tried SMBUS_QUICK (e.g. with scanning a bus with
'i2cdetect')?

...
 
> +static int ljca_i2c_probe(struct auxiliary_device *auxdev,
> +			  const struct auxiliary_device_id *aux_dev_id)
> +{
> +	struct ljca_client *ljca = auxiliary_dev_to_ljca_client(auxdev);
> +	struct ljca_i2c_dev *ljca_i2c;
> +	int ret;
> +
> +	ljca_i2c = devm_kzalloc(&auxdev->dev, sizeof(*ljca_i2c), GFP_KERNEL);
> +	if (!ljca_i2c)
> +		return -ENOMEM;
> +
> +	ljca_i2c->ljca = ljca;
> +	ljca_i2c->i2c_info = dev_get_platdata(&auxdev->dev);
> +
> +	ljca_i2c->adap.owner = THIS_MODULE;
> +	ljca_i2c->adap.class = I2C_CLASS_HWMON;

Just to make sure: you want class based instantiation here because you
have no other way of describing clients? I guess it makes sense for USB,
just wanted to ask.

Other than that, it looks good!

All the best,

   Wolfram
Wentong Wu Aug. 29, 2023, 3:29 a.m. UTC | #2
Hi Wolfram,

Thanks for your review.

> From: Wolfram Sang <wsa@kernel.org>
> 
> Hi,
> 
> thank you for your patches!
> 
> ...
> 
> > +static int ljca_i2c_start(struct ljca_i2c_dev *ljca_i2c, u8 slave_addr,
> > +			  enum ljca_xfer_type type)
> > +{
> > +	struct ljca_i2c_rw_packet *w_packet =
> > +			(struct ljca_i2c_rw_packet *)ljca_i2c->obuf;
> > +	struct ljca_i2c_rw_packet *r_packet =
> > +			(struct ljca_i2c_rw_packet *)ljca_i2c->ibuf;
> > +	s16 rp_len;
> > +	int ret;
> > +
> > +	w_packet->id = ljca_i2c->i2c_info->id;
> > +	w_packet->len = cpu_to_le16(sizeof(*w_packet->data));
> > +	w_packet->data[0] = (slave_addr << 1) | type;
> > +
> > +	ret = ljca_transfer(ljca_i2c->ljca, LJCA_I2C_START, w_packet,
> > +			    struct_size(w_packet, data, 1), r_packet,
> > +			    LJCA_I2C_BUF_SIZE);
> > +	if (ret < 0 || ret < sizeof(*r_packet))
> > +		return ret < 0 ? ret : -EIO;
> > +
> > +	rp_len = le16_to_cpu(r_packet->len);
> > +	if (rp_len < 0 || r_packet->id != w_packet->id) {
> > +		dev_err(&ljca_i2c->adap.dev,
> > +			"i2c start failed len: %d id: %d %d\n",
> > +			rp_len, r_packet->id, w_packet->id);
> 
> All dev_err look more like dev_dbg to me. They are not helpful for the regular
> user, I'd think.

Ack, thanks

> 
> > +		return -EIO;
> > +	}
> > +
> > +	return 0;
> > +}
> > +
> 
> ...
> 
> > +static int ljca_i2c_pure_read(struct ljca_i2c_dev *ljca_i2c, u8
> > +*data, u8 len) {
> > +	struct ljca_i2c_rw_packet *w_packet =
> > +			(struct ljca_i2c_rw_packet *)ljca_i2c->obuf;
> > +	struct ljca_i2c_rw_packet *r_packet =
> > +			(struct ljca_i2c_rw_packet *)ljca_i2c->ibuf;
> > +	s16 rp_len;
> > +	int ret;
> > +
> > +	if (len > LJCA_I2C_MAX_XFER_SIZE)
> > +		return -EINVAL;
> 
> You can remove this check. You already have a quirk structure, so the core will
> do it for you.

Ack, thanks

> 
> ...
> 
> > +static int ljca_i2c_pure_write(struct ljca_i2c_dev *ljca_i2c, u8
> > +*data, u8 len) {
> > +	struct ljca_i2c_rw_packet *w_packet =
> > +			(struct ljca_i2c_rw_packet *)ljca_i2c->obuf;
> > +	struct ljca_i2c_rw_packet *r_packet =
> > +			(struct ljca_i2c_rw_packet *)ljca_i2c->ibuf;
> > +	s16 rplen;
> > +	int ret;
> > +
> > +	if (len > LJCA_I2C_MAX_XFER_SIZE)
> > +		return -EINVAL;
> 
> You can remove this check. You already have a quirk structure, so the core will
> do it for you.

Ack, thanks

> 
> ...
> 
> > +static u32 ljca_i2c_func(struct i2c_adapter *adap) {
> > +	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; }
> 
> Have you successfully tried SMBUS_QUICK (e.g. with scanning a bus with
> 'i2cdetect')?

Thanks, we just suppose it support SMBUS function after reviewed the
core code, but never try the SMBUS functionality, so I decide to remove
the I2C_FUNC_SMBUS_EMUL support currently and after we verify it working
correctly, we will add it in another patch.

Thanks

> 
> ...
> 
> > +static int ljca_i2c_probe(struct auxiliary_device *auxdev,
> > +			  const struct auxiliary_device_id *aux_dev_id) {
> > +	struct ljca_client *ljca = auxiliary_dev_to_ljca_client(auxdev);
> > +	struct ljca_i2c_dev *ljca_i2c;
> > +	int ret;
> > +
> > +	ljca_i2c = devm_kzalloc(&auxdev->dev, sizeof(*ljca_i2c), GFP_KERNEL);
> > +	if (!ljca_i2c)
> > +		return -ENOMEM;
> > +
> > +	ljca_i2c->ljca = ljca;
> > +	ljca_i2c->i2c_info = dev_get_platdata(&auxdev->dev);
> > +
> > +	ljca_i2c->adap.owner = THIS_MODULE;
> > +	ljca_i2c->adap.class = I2C_CLASS_HWMON;
> 
> Just to make sure: you want class based instantiation here because you have no
> other way of describing clients? 

Actually it's about the method 3 of instantiate I2C devices described in
https://www.kernel.org/doc/Documentation/i2c/instantiating-devices
and it's not preferred wherever possible. And below are the definitions
about i2c adapter class, we only have I2C_CLASS_HWMON class tested,
so just put it here.

/* i2c adapter classes (bitmask) */
#define I2C_CLASS_HWMON         (1<<0)  /* lm_sensors, ... */
#define I2C_CLASS_DDC           (1<<3)  /* DDC bus on graphics adapters */
#define I2C_CLASS_SPD           (1<<7)  /* Memory modules */
/* Warn users that the adapter doesn't support classes anymore */
#define I2C_CLASS_DEPRECATED    (1<<8)

BR,
Wentong
> I guess it makes sense for USB, just wanted to
> ask.
> 
> Other than that, it looks good!
> 
> All the best,
> 
>    Wolfram
Wolfram Sang Aug. 29, 2023, 7:43 a.m. UTC | #3
> Actually it's about the method 3 of instantiate I2C devices described in
> https://www.kernel.org/doc/Documentation/i2c/instantiating-devices
> and it's not preferred wherever possible. And below are the definitions
> about i2c adapter class, we only have I2C_CLASS_HWMON class tested,
> so just put it here.

Yeah, as I mentioned before, it may make sense for a USB based device. I
just wanted to make sure you did it intentionally. Keep it.
Wentong Wu Aug. 29, 2023, 7:46 a.m. UTC | #4
> From: Wolfram Sang <wsa@kernel.org>
> 
> 
> > Actually it's about the method 3 of instantiate I2C devices described
> > in https://www.kernel.org/doc/Documentation/i2c/instantiating-devices
> > and it's not preferred wherever possible. And below are the
> > definitions about i2c adapter class, we only have I2C_CLASS_HWMON
> > class tested, so just put it here.
> 
> Yeah, as I mentioned before, it may make sense for a USB based device. I just
> wanted to make sure you did it intentionally. Keep it.

Thanks, please help review v11 patch set as well which is to address your comments.

BR,
Wentong