From: Guenter Roeck <linux@roeck-us.net>
To: alexandru.tachici@analog.com, linux-hwmon@vger.kernel.org,
linux-kernel@vger.kernel.org, devicetree@vger.kernel.org
Cc: robh+dt@kernel.org
Subject: Re: [PATCH v3 2/6] hwmon: (pmbus/core) Add Block WR
Date: Fri, 29 May 2020 10:55:34 -0700 [thread overview]
Message-ID: <fbe5afc6-56d7-1802-6fa4-8aac4ea58bfa@roeck-us.net> (raw)
In-Reply-To: <20200529130506.73511-3-alexandru.tachici@analog.com>
On 5/29/20 6:05 AM, alexandru.tachici@analog.com wrote:
> From: Alexandru Tachici <alexandru.tachici@analog.com>
>
> PmBus devices support Block Write-Block Read Process
> Call described in SMBus specification v 2.0 with the
> exception that Block writes and reads are permitted to
> have up 255 data bytes instead of max 32 bytes (SMBus).
>
> This patch adds Block WR process call support for PMBus.
>
I don't think I want to have this code in the PMBus core.
We can move it there if needed at some point in the future,
but for the time being I'd rather have it in the driver
needing it.
> Signed-off-by: Alexandru Tachici <alexandru.tachici@analog.com>
> ---
> drivers/hwmon/pmbus/Kconfig | 2 +-
> drivers/hwmon/pmbus/pmbus.h | 4 ++
> drivers/hwmon/pmbus/pmbus_core.c | 88 ++++++++++++++++++++++++++++++++
> 3 files changed, 93 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/hwmon/pmbus/Kconfig b/drivers/hwmon/pmbus/Kconfig
> index 6949483aa732..f11712fdcea8 100644
> --- a/drivers/hwmon/pmbus/Kconfig
> +++ b/drivers/hwmon/pmbus/Kconfig
> @@ -5,7 +5,7 @@
>
> menuconfig PMBUS
> tristate "PMBus support"
> - depends on I2C
> + depends on I2C && CRC8
This should be select CRC8, not depends.
> help
> Say yes here if you want to enable PMBus support.
>
> diff --git a/drivers/hwmon/pmbus/pmbus.h b/drivers/hwmon/pmbus/pmbus.h
> index 18e06fc6c53f..ae4e15c5dff2 100644
> --- a/drivers/hwmon/pmbus/pmbus.h
> +++ b/drivers/hwmon/pmbus/pmbus.h
> @@ -392,6 +392,8 @@ enum pmbus_sensor_classes {
> #define PMBUS_PHASE_VIRTUAL BIT(30) /* Phases on this page are virtual */
> #define PMBUS_PAGE_VIRTUAL BIT(31) /* Page is virtual */
>
> +#define PMBUS_BLOCK_MAX 255
> +
> enum pmbus_data_format { linear = 0, direct, vid };
> enum vrm_version { vr11 = 0, vr12, vr13, imvp9, amd625mv };
>
> @@ -467,6 +469,8 @@ int pmbus_read_word_data(struct i2c_client *client, int page, int phase,
> u8 reg);
> int pmbus_write_word_data(struct i2c_client *client, int page, u8 reg,
> u16 word);
> +int pmbus_block_wr(struct i2c_client *client, u8 cmd, u8 w_len, u8 *data_w,
> + u8 *data_r);
> int pmbus_read_byte_data(struct i2c_client *client, int page, u8 reg);
> int pmbus_write_byte(struct i2c_client *client, int page, u8 value);
> int pmbus_write_byte_data(struct i2c_client *client, int page, u8 reg,
> diff --git a/drivers/hwmon/pmbus/pmbus_core.c b/drivers/hwmon/pmbus/pmbus_core.c
> index 8d321bf7d15b..ef63468da3b5 100644
> --- a/drivers/hwmon/pmbus/pmbus_core.c
> +++ b/drivers/hwmon/pmbus/pmbus_core.c
> @@ -6,6 +6,7 @@
> * Copyright (c) 2012 Guenter Roeck
> */
>
> +#include <linux/crc8.h>
> #include <linux/debugfs.h>
> #include <linux/kernel.h>
> #include <linux/math64.h>
> @@ -44,6 +45,8 @@
>
> #define PMBUS_NAME_SIZE 24
>
> +DECLARE_CRC8_TABLE(pmbus_crc_table);
> +
> struct pmbus_sensor {
> struct pmbus_sensor *next;
> char name[PMBUS_NAME_SIZE]; /* sysfs sensor name */
> @@ -184,6 +187,89 @@ int pmbus_set_page(struct i2c_client *client, int page, int phase)
> }
> EXPORT_SYMBOL_GPL(pmbus_set_page);
>
> +/* Block Write/Read command.
I won't accept network-style multi-line comments.
> + * @client: Handle to slave device
> + * @cmd: Byte interpreted by slave
> + * @w_len: Size of write data block; PMBus allows at most 255 bytes
> + * @data_w: byte array which will be written.
> + * @data_r: Byte array into which data will be read; big enough to hold
> + * the data returned by the slave. PMBus allows at most 255 bytes.
> + *
> + * Different from Block Read as it sends data and waits for the slave to
> + * return a value dependent on that data. The protocol is simply a Write Block
> + * followed by a Read Block without the Read-Block command field and the
> + * Write-Block STOP bit.
> + *
> + * Returns number of bytes read or negative errno.
> + */
> +int pmbus_block_wr(struct i2c_client *client, u8 cmd, u8 w_len,
_wr is misleading, since it suggests an abbreviated _write.
Better use something like _transfer or _xfer.
> + u8 *data_w, u8 *data_r)
> +{
> + u8 write_buf[PMBUS_BLOCK_MAX + 1];
> + struct i2c_msg msgs[2] = {
> + {
> + .addr = client->addr,
> + .flags = 0,
> + .buf = write_buf,
> + .len = w_len + 2,
> + },
> + {
> + .addr = client->addr,
> + .flags = I2C_M_RD,
> + .len = PMBUS_BLOCK_MAX,
> + }
> + };
> + u8 addr = 0;
> + u8 crc = 0;
> + int ret;
> +
> + msgs[0].buf[0] = cmd;
> + msgs[0].buf[1] = w_len;
> + memcpy(&msgs[0].buf[2], data_w, w_len);
> +
w_len can be up to 255, meaning up to 255 + 2 bytes can be written
into write_buf. Yet, write_buf is only 256 bytes long.
> + msgs[0].buf = i2c_get_dma_safe_msg_buf(&msgs[0], 1);
> + if (!msgs[0].buf)
> + return -ENOMEM;
> +
> + msgs[1].buf = i2c_get_dma_safe_msg_buf(&msgs[1], 1);
> + if (!msgs[1].buf) {
> + i2c_put_dma_safe_msg_buf(msgs[0].buf, &msgs[0], false);
> + return -ENOMEM;
> + }
> +
> + ret = i2c_transfer(client->adapter, msgs, 2);
> + if (ret != 2) {
> + dev_err(&client->dev, "I2C transfer error.");
ret may be 1, which would suggest to the caller that one byte
of data was returned. Also, I am not in favor of logging noise.
> + goto cleanup;
> + }
> +
> + if (client->flags & I2C_CLIENT_PEC) {
> + addr = i2c_8bit_addr_from_msg(&msgs[0]);
> + crc = crc8(pmbus_crc_table, &addr, 1, crc);
> + crc = crc8(pmbus_crc_table, msgs[0].buf, msgs[0].len, crc);
> +
> + addr = i2c_8bit_addr_from_msg(&msgs[1]);
> + crc = crc8(pmbus_crc_table, &addr, 1, crc);
> + crc = crc8(pmbus_crc_table, msgs[1].buf, msgs[1].buf[0] + 1,
> + crc);
> +
> + if (crc != msgs[1].buf[msgs[1].buf[0] + 1]) {
> + ret = -EBADMSG;
> + goto cleanup;
> + }
> + }
> +
> + memcpy(data_r, &msgs[1].buf[1], msgs[1].buf[0]);
The length of the read buffer is 255 bytes, yet the code suggests
that up to 256 bytes can actually be received.
> + ret = msgs[1].buf[0];
> +
> +cleanup:
> + i2c_put_dma_safe_msg_buf(msgs[0].buf, &msgs[0], true);
> + i2c_put_dma_safe_msg_buf(msgs[1].buf, &msgs[1], true);
> +
> + return ret;
> +}
> +EXPORT_SYMBOL_GPL(pmbus_block_wr);
> +
> int pmbus_write_byte(struct i2c_client *client, int page, u8 value)
> {
> int rv;
> @@ -2522,6 +2608,8 @@ int pmbus_do_probe(struct i2c_client *client, const struct i2c_device_id *id,
> if (!data->groups)
> return -ENOMEM;
>
> + crc8_populate_msb(pmbus_crc_table, 0x7);
> +
> i2c_set_clientdata(client, data);
> mutex_init(&data->update_lock);
> data->dev = dev;
>
next prev parent reply other threads:[~2020-05-29 17:55 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-05-29 13:05 [PATCH v3 0/6] hwmon: pmbus: adm1266: add support alexandru.tachici
2020-05-29 13:05 ` [PATCH v3 1/6] " alexandru.tachici
2020-05-29 13:05 ` [PATCH v3 2/6] hwmon: (pmbus/core) Add Block WR alexandru.tachici
2020-05-29 17:55 ` Guenter Roeck [this message]
2020-05-29 13:05 ` [PATCH v3 3/6] hwmon: pmbus: adm1266: Add support for GPIOs alexandru.tachici
2020-05-29 18:36 ` Guenter Roeck
2020-05-29 13:05 ` [PATCH v3 4/6] hwmon: pmbus: adm1266: add debugfs attr for states alexandru.tachici
2020-05-29 18:43 ` Guenter Roeck
2020-05-29 13:05 ` [PATCH v3 5/6] hwmon: pmbus: adm1266: read blackbox alexandru.tachici
2020-05-29 18:45 ` Guenter Roeck
2020-05-29 13:05 ` [PATCH v3 6/6] dt-bindings: hwmon: Add bindings for ADM1266 alexandru.tachici
2020-05-29 21:17 ` Rob Herring
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=fbe5afc6-56d7-1802-6fa4-8aac4ea58bfa@roeck-us.net \
--to=linux@roeck-us.net \
--cc=alexandru.tachici@analog.com \
--cc=devicetree@vger.kernel.org \
--cc=linux-hwmon@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=robh+dt@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
all inboxes | Powered by JetHome®