From: Heiner Kallweit <hkallweit1@gmail.com>
To: Bartosz Golaszewski <brgl@bgdev.pl>
Cc: "linux-i2c@vger.kernel.org" <linux-i2c@vger.kernel.org>
Subject: [PATCH v6 5/7] eeprom: at24: add regmap-based read function
Date: Tue, 28 Nov 2017 21:51:50 +0100 [thread overview]
Message-ID: <edcbceee-e5ee-1345-0e4c-b0827b67c1dd@gmail.com> (raw)
In-Reply-To: <328f770d-e79d-cbb5-1cd7-238862aa8e1c@gmail.com>
Add regmap-based read function and instead of using three different
read functions (standard, mac, serial) use just one and factor out the
read offset adjustment for mac and serial to at24_adjust_read_offset.
Signed-off-by: Heiner Kallweit <hkallweit1@gmail.com>
---
v2:
- rebased
v3:
- improve readability
- re-introduce debug message
- introduce at24_adjust_read_offset
v4:
- move offset adjustment calculation to probe function
v5:
- small style improvements to at24_get_offset_adj
v6:
- rebased
- add byte_len parameter to at24_get_offset_adj
---
drivers/misc/eeprom/at24.c | 57 +++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 56 insertions(+), 1 deletion(-)
diff --git a/drivers/misc/eeprom/at24.c b/drivers/misc/eeprom/at24.c
index fa28d87f7..8172ca627 100644
--- a/drivers/misc/eeprom/at24.c
+++ b/drivers/misc/eeprom/at24.c
@@ -75,6 +75,7 @@ struct at24_data {
unsigned write_max;
unsigned num_addresses;
+ unsigned int offset_adj;
struct nvmem_config nvmem_config;
struct nvmem_device *nvmem;
@@ -312,6 +313,36 @@ static ssize_t at24_eeprom_read_smbus(struct at24_data *at24, char *buf,
return -ETIMEDOUT;
}
+static ssize_t at24_regmap_read(struct at24_data *at24, char *buf,
+ unsigned int offset, size_t count)
+{
+ unsigned long timeout, read_time;
+ struct at24_client *at24_client;
+ struct i2c_client *client;
+ struct regmap *regmap;
+ int ret;
+
+ at24_client = at24_translate_offset(at24, &offset);
+ regmap = at24_client->regmap;
+ client = at24_client->client;
+
+ if (count > io_limit)
+ count = io_limit;
+
+ /* adjust offset for mac and serial read ops */
+ offset += at24->offset_adj;
+
+ loop_until_timeout(timeout, read_time) {
+ ret = regmap_bulk_read(regmap, offset, buf, count);
+ dev_dbg(&client->dev, "read %zu@%d --> %d (%ld)\n",
+ count, offset, ret, jiffies);
+ if (!ret)
+ return count;
+ }
+
+ return -ETIMEDOUT;
+}
+
static ssize_t at24_eeprom_read_i2c(struct at24_data *at24, char *buf,
unsigned int offset, size_t count)
{
@@ -535,7 +566,7 @@ static int at24_read(void *priv, unsigned int off, void *val, size_t count)
while (count) {
int status;
- status = at24->read_func(at24, buf, off, count);
+ status = at24_regmap_read(at24, buf, off, count);
if (status < 0) {
mutex_unlock(&at24->lock);
pm_runtime_put(&client->dev);
@@ -628,6 +659,29 @@ static void at24_get_pdata(struct device *dev, struct at24_platform_data *chip)
}
}
+static unsigned int at24_get_offset_adj(u8 flags, unsigned int byte_len)
+{
+ if (flags & AT24_FLAG_MAC) {
+ /* EUI-48 starts from 0x9a, EUI-64 from 0x98 */
+ return 0xa0 - byte_len;
+ } else if (flags & AT24_FLAG_SERIAL && flags & AT24_FLAG_ADDR16) {
+ /*
+ * For 16 bit address pointers, the word address must contain
+ * a '10' sequence in bits 11 and 10 regardless of the
+ * intended position of the address pointer.
+ */
+ return 0x0800;
+ } else if (flags & AT24_FLAG_SERIAL) {
+ /*
+ * Otherwise the word address must begin with a '10' sequence,
+ * regardless of the intended address.
+ */
+ return 0x0080;
+ } else {
+ return 0;
+ }
+}
+
static const struct regmap_config regmap_config_8 = {
.reg_bits = 8,
.val_bits = 8,
@@ -754,6 +808,7 @@ static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id)
at24->use_smbus = use_smbus;
at24->chip = chip;
at24->num_addresses = num_addresses;
+ at24->offset_adj = at24_get_offset_adj(chip.flags, chip.byte_len);
at24->client[0].client = client;
at24->client[0].regmap = devm_regmap_init_i2c(client, config);
--
2.15.0
next prev parent reply other threads:[~2017-11-28 20:52 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-11-28 20:40 [PATCH v6 0/7] eeprom: at24: switch driver to regmap_i2c Heiner Kallweit
2017-11-28 20:51 ` [PATCH v6 1/7] eeprom: at24: add basic regmap_i2c support Heiner Kallweit
2017-11-28 20:51 ` [PATCH v6 2/7] eeprom: at24: change at24_translate_offset return type Heiner Kallweit
2017-11-28 20:51 ` [PATCH v6 3/7] eeprom: at24: add regmap-based write function Heiner Kallweit
2017-11-28 20:51 ` [PATCH v6 4/7] eeprom: at24: remove old write functions Heiner Kallweit
2017-11-28 20:51 ` Heiner Kallweit [this message]
2017-11-28 20:51 ` [PATCH v6 6/7] eeprom: at24: remove old read functions Heiner Kallweit
2017-11-28 20:51 ` [PATCH v6 7/7] eeprom: at24: remove now unneeded smbus-related code Heiner Kallweit
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=edcbceee-e5ee-1345-0e4c-b0827b67c1dd@gmail.com \
--to=hkallweit1@gmail.com \
--cc=brgl@bgdev.pl \
--cc=linux-i2c@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).