From: Crt Mori <cmo-fc6wVz46lShBDgjK7y7TUQ@public.gmane.org>
To: "Simon G. Vogl"
<simon-nD9nYVNVf00W+b/DJNNodF6hYfS7NtTn@public.gmane.org>,
Wolfram Sang <wsa-z923LK4zBo2bacvFa/9K2g@public.gmane.org>
Cc: linux-i2c-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
Crt Mori <cmo-fc6wVz46lShBDgjK7y7TUQ@public.gmane.org>
Subject: [PATCH] i2c: core: add support for read commands longer than byte
Date: Fri, 24 Jul 2015 15:20:42 +0200 [thread overview]
Message-ID: <1437744042-24471-1-git-send-email-cmo@melexis.com> (raw)
If you want to send more than 1 byte as command to slave you need to go away
from the smbus (which has a 8 bit command defined in specification). i2c has
no such limitation in specification and since we want to have a longer than
1 byte command, we need additional function to handle it. With outside
buffers we have also avoided endianness problems (if we would just define a
u16 command instead of u8). Also there are now no limitations (as per i2c
specification) of command length.
Addressed read function is when we have a slave device which accepts commands
longer than byte or some subcommands in same write string before issuing a
repeat start before read cycle to read more than byte (or byte) from slave
device.
Signed-off-by: Crt Mori <cmo-fc6wVz46lShBDgjK7y7TUQ@public.gmane.org>
---
drivers/i2c/i2c-core.c | 40 ++++++++++++++++++++++++++++++++++++++++
include/linux/i2c.h | 4 ++++
2 files changed, 44 insertions(+)
diff --git a/drivers/i2c/i2c-core.c b/drivers/i2c/i2c-core.c
index 069a41f..bccf83f 100644
--- a/drivers/i2c/i2c-core.c
+++ b/drivers/i2c/i2c-core.c
@@ -2980,6 +2980,46 @@ int i2c_slave_unregister(struct i2c_client *client)
EXPORT_SYMBOL_GPL(i2c_slave_unregister);
#endif
+/**
+ * i2c_addressed_read - writes read commands with desired length and read
+ * desired length of data.
+ * @client: Handle to slave device
+ * @command: Pointer to command location
+ * @cmd_len: Number of command bytes on command location
+ * @data: Pointer to read data location
+ * @data_len: Expected number of read bytes
+ *
+ * Addressed read on slave device when command is longer than byte. Returns
+ * negative errno on error and 0 on success.
+ */
+s32 i2c_addressed_read(const struct i2c_client *client, u8 *command,
+ u16 cmd_len, u8 *data, u16 data_len)
+{
+ s32 status;
+ struct i2c_msg msg[2] = {
+ {
+ .addr = client->addr,
+ .flags = client->flags,
+ .len = cmd_len,
+ .buf = command,
+ },
+ {
+ .addr = client->addr,
+ .flags = client->flags | I2C_M_RD,
+ .len = data_len,
+ .buf = data,
+ },
+ };
+
+ status = i2c_transfer(client->adapter, msg, 2);
+
+ if (status < 0)
+ return status;
+
+ return 0;
+}
+EXPORT_SYMBOL(i2c_addressed_read);
+
MODULE_AUTHOR("Simon G. Vogl <simon-nD9nYVNVf00W+b/DJNNodF6hYfS7NtTn@public.gmane.org>");
MODULE_DESCRIPTION("I2C-Bus main module");
MODULE_LICENSE("GPL");
diff --git a/include/linux/i2c.h b/include/linux/i2c.h
index e83a738..d3cc1af 100644
--- a/include/linux/i2c.h
+++ b/include/linux/i2c.h
@@ -121,6 +121,10 @@ extern s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client,
extern s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client,
u8 command, u8 length,
const u8 *values);
+
+s32 i2c_addressed_read(const struct i2c_client *client, u8 *command,
+ u16 cmd_len, u8 *data, u16 data_len);
+
#endif /* I2C */
/**
--
2.1.4
next reply other threads:[~2015-07-24 13:20 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-07-24 13:20 Crt Mori [this message]
[not found] ` <1437744042-24471-1-git-send-email-cmo-fc6wVz46lShBDgjK7y7TUQ@public.gmane.org>
2015-08-04 11:16 ` [PATCH] i2c: core: add support for read commands longer than byte Crt Mori
2015-08-09 7:40 ` Wolfram Sang
2015-08-10 7:26 ` Crt Mori
[not found] ` <CAKv63usiWb4QgV=pnwuqFJtTogRca7UjtmiSKV69OTM2h7LtHw-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2015-08-10 7:55 ` Crt Mori
2015-08-10 9:13 ` Wolfram Sang
2015-08-10 10:06 ` Crt Mori
[not found] ` <CAKv63usQZBqjcAWFEpFvkRuGLd7JXcJh=5aKmd8B94A68dsZ7w-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2015-08-10 10:34 ` Wolfram Sang
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=1437744042-24471-1-git-send-email-cmo@melexis.com \
--to=cmo-fc6wvz46lshbdgjk7y7tuq@public.gmane.org \
--cc=linux-i2c-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
--cc=simon-nD9nYVNVf00W+b/DJNNodF6hYfS7NtTn@public.gmane.org \
--cc=wsa-z923LK4zBo2bacvFa/9K2g@public.gmane.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).