From: Guenter Roeck <linux@roeck-us.net>
To: Heikki Krogerus <heikki.krogerus@linux.intel.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
linux-usb@vger.kernel.org, stable@vger.kernel.org
Subject: [1/2] usb: typec: tps6598x: handle block reads separately with plain-I2C adapters
Date: Fri, 20 Apr 2018 10:26:08 -0700 [thread overview]
Message-ID: <20180420172608.GB8997@roeck-us.net> (raw)
On Wed, Apr 18, 2018 at 03:34:09PM +0300, Heikki Krogerus wrote:
> If the I2C adapter that the PD controller is attached to
> does not support SMBus protocol, the driver needs to handle
> block reads separately. The first byte returned in block
> read protocol will show the total number of bytes. It needs
> to be stripped away.
>
> This is handled separately in the driver only because right
> now we have no way of requesting the used protocol with
> regmap-i2c. This is in practice a workaround for what is
> really a problem in regmap-i2c. The other option would have
> been to register custom regmap, or not use regmap at all,
> however, since the solution is very simple, I choose to use
> it in this case for convenience. It is easy to remove once
> we figure out how to handle this kind of cases in
> regmap-i2c.
>
> Fixes: 0a4c005bd171 ("usb: typec: driver for TI TPS6598x USB Power Delivery controllers")
> Signed-off-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
> ---
> drivers/usb/typec/tps6598x.c | 42 ++++++++++++++++++++++++++++++------
> 1 file changed, 35 insertions(+), 7 deletions(-)
>
> diff --git a/drivers/usb/typec/tps6598x.c b/drivers/usb/typec/tps6598x.c
> index 8b8406867c02..82f09cd9792d 100644
> --- a/drivers/usb/typec/tps6598x.c
> +++ b/drivers/usb/typec/tps6598x.c
> @@ -73,6 +73,7 @@ struct tps6598x {
> struct device *dev;
> struct regmap *regmap;
> struct mutex lock; /* device lock */
> + u8 i2c_protocol:1;
>
> struct typec_port *port;
> struct typec_partner *partner;
> @@ -80,6 +81,23 @@ struct tps6598x {
> struct typec_capability typec_cap;
> };
>
> +static int
> +tps6598x_block_read(struct tps6598x *tps, u8 reg, void *val, ssize_t len)
> +{
> + u8 data[len + 1];
> + int ret;
> +
> + if (!tps->i2c_protocol)
> + return regmap_raw_read(tps->regmap, reg, val, len);
> +
> + ret = regmap_raw_read(tps->regmap, reg, data, sizeof(data));
> + if (ret)
> + return ret;
> +
Sanity check ?
if (data[0] != len)
return -Esomething;
Other than that,
Reviewed-by: Guenter Roeck <linux@roeck-us.net>
> + memcpy(val, &data[1], len);
> + return 0;
> +}
> +
> static inline int tps6598x_read16(struct tps6598x *tps, u8 reg, u16 *val)
> {
> return regmap_raw_read(tps->regmap, reg, val, sizeof(u16));
> @@ -87,12 +105,12 @@ static inline int tps6598x_read16(struct tps6598x *tps, u8 reg, u16 *val)
>
> static inline int tps6598x_read32(struct tps6598x *tps, u8 reg, u32 *val)
> {
> - return regmap_raw_read(tps->regmap, reg, val, sizeof(u32));
> + return tps6598x_block_read(tps, reg, val, sizeof(u32));
> }
>
> static inline int tps6598x_read64(struct tps6598x *tps, u8 reg, u64 *val)
> {
> - return regmap_raw_read(tps->regmap, reg, val, sizeof(u64));
> + return tps6598x_block_read(tps, reg, val, sizeof(u64));
> }
>
> static inline int tps6598x_write16(struct tps6598x *tps, u8 reg, u16 val)
> @@ -121,8 +139,8 @@ static int tps6598x_read_partner_identity(struct tps6598x *tps)
> struct tps6598x_rx_identity_reg id;
> int ret;
>
> - ret = regmap_raw_read(tps->regmap, TPS_REG_RX_IDENTITY_SOP,
> - &id, sizeof(id));
> + ret = tps6598x_block_read(tps, TPS_REG_RX_IDENTITY_SOP,
> + &id, sizeof(id));
> if (ret)
> return ret;
>
> @@ -224,13 +242,13 @@ static int tps6598x_exec_cmd(struct tps6598x *tps, const char *cmd,
> } while (val);
>
> if (out_len) {
> - ret = regmap_raw_read(tps->regmap, TPS_REG_DATA1,
> - out_data, out_len);
> + ret = tps6598x_block_read(tps, TPS_REG_DATA1,
> + out_data, out_len);
> if (ret)
> return ret;
> val = out_data[0];
> } else {
> - ret = regmap_read(tps->regmap, TPS_REG_DATA1, &val);
> + ret = tps6598x_block_read(tps, TPS_REG_DATA1, &val, sizeof(u8));
> if (ret)
> return ret;
> }
> @@ -385,6 +403,16 @@ static int tps6598x_probe(struct i2c_client *client)
> if (!vid)
> return -ENODEV;
>
> + /*
> + * Checking can the adapter handle SMBus protocol. If if can not, the
> + * driver needs to take care of block reads separately.
> + *
> + * FIXME: Testing with I2C_FUNC_I2C. regmap-i2c uses I2C protocol
> + * unconditionally if the adapter has I2C_FUNC_I2C set.
> + */
> + if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
> + tps->i2c_protocol = true;
> +
> ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
> if (ret < 0)
> return ret;
> --
> 2.17.0
>
---
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
next reply other threads:[~2018-04-20 17:26 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-04-20 17:26 Guenter Roeck [this message]
-- strict thread matches above, loose matches on Subject: below --
2018-04-24 13:45 [1/2] usb: typec: tps6598x: handle block reads separately with plain-I2C adapters Heikki Krogerus
2018-04-24 13:02 Guenter Roeck
2018-04-24 11:46 Heikki Krogerus
2018-04-23 16:43 Guenter Roeck
2018-04-23 8:03 Heikki Krogerus
2018-04-22 12:54 Greg Kroah-Hartman
2018-04-18 12:34 Heikki Krogerus
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20180420172608.GB8997@roeck-us.net \
--to=linux@roeck-us.net \
--cc=gregkh@linuxfoundation.org \
--cc=heikki.krogerus@linux.intel.com \
--cc=linux-usb@vger.kernel.org \
--cc=stable@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).