The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
To: Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	linux-kernel@vger.kernel.org
Cc: Arnd Bergmann <arnd@arndb.de>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Jiri Prchal <jiri.prchal@aksignal.cz>
Subject: [PATCH v1 07/10] misc: at25: Factor out at_fram_to_chip()
Date: Thu, 25 Nov 2021 23:32:00 +0200	[thread overview]
Message-ID: <20211125213203.86693-8-andriy.shevchenko@linux.intel.com> (raw)
In-Reply-To: <20211125213203.86693-1-andriy.shevchenko@linux.intel.com>

In the similar way as it's done for EEPROM, factor out
a new helper function for FRAM.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 drivers/misc/eeprom/at25.c | 85 ++++++++++++++++++++------------------
 1 file changed, 44 insertions(+), 41 deletions(-)

diff --git a/drivers/misc/eeprom/at25.c b/drivers/misc/eeprom/at25.c
index c9660a4625ce..b9d26c9ee768 100644
--- a/drivers/misc/eeprom/at25.c
+++ b/drivers/misc/eeprom/at25.c
@@ -31,9 +31,9 @@
 
 #define	FM25_SN_LEN	8		/* serial number length */
 struct at25_data {
+	struct spi_eeprom	chip;
 	struct spi_device	*spi;
 	struct mutex		lock;
-	struct spi_eeprom	chip;
 	unsigned		addrlen;
 	struct nvmem_config	nvmem_config;
 	struct nvmem_device	*nvmem;
@@ -360,6 +360,44 @@ static int at25_fw_to_chip(struct device *dev, struct spi_eeprom *chip)
 	return 0;
 }
 
+static int at25_fram_to_chip(struct device *dev, struct spi_eeprom *chip)
+{
+	struct at25_data *at25 = container_of(chip, struct at25_data, chip);
+	u8 sernum[FM25_SN_LEN];
+	u8 id[FM25_ID_LEN];
+	int i;
+
+	strncpy(chip->name, "fm25", sizeof(chip->name));
+
+	/* Get ID of chip */
+	fm25_aux_read(at25, id, FM25_RDID, FM25_ID_LEN);
+	if (id[6] != 0xc2) {
+		dev_err(dev, "Error: no Cypress FRAM (id %02x)\n", id[6]);
+		return -ENODEV;
+	}
+	/* Set size found in ID */
+	if (id[7] < 0x21 || id[7] > 0x26) {
+		dev_err(dev, "Error: unsupported size (id %02x)\n", id[7]);
+		return -ENODEV;
+	}
+
+	chip->byte_len = BIT(id[7] - 0x21 + 4) * 1024;
+	if (chip->byte_len > 64 * 1024)
+		chip->flags |= EE_ADDR3;
+	else
+		chip->flags |= EE_ADDR2;
+
+	if (id[8]) {
+		fm25_aux_read(at25, sernum, FM25_RDSN, FM25_SN_LEN);
+		/* Swap byte order */
+		for (i = 0; i < FM25_SN_LEN; i++)
+			at25->sernum[i] = sernum[FM25_SN_LEN - 1 - i];
+	}
+
+	chip->page_size = PAGE_SIZE;
+	return 0;
+}
+
 static const struct of_device_id at25_of_match[] = {
 	{ .compatible = "atmel,at25",},
 	{ .compatible = "cypress,fm25",},
@@ -380,10 +418,7 @@ static int at25_probe(struct spi_device *spi)
 	int			err;
 	int			sr;
 	struct spi_eeprom *pdata;
-	u8 id[FM25_ID_LEN];
-	u8 sernum[FM25_SN_LEN];
 	bool is_fram;
-	int i;
 
 	err = device_property_match_string(&spi->dev, "compatible", "cypress,fm25");
 	if (err >= 0)
@@ -414,44 +449,12 @@ static int at25_probe(struct spi_device *spi)
 	if (pdata) {
 		at25->chip = *pdata;
 	} else {
-		if (is_fram) {
-			/* We file fields for FRAM case later on */
-		} else {
-			err = at25_fw_to_chip(&spi->dev, &at25->chip);
-			if (err)
-				return err;
-		}
-	}
-
-	if (is_fram) {
-		/* Get ID of chip */
-		fm25_aux_read(at25, id, FM25_RDID, FM25_ID_LEN);
-		if (id[6] != 0xc2) {
-			dev_err(&spi->dev,
-				"Error: no Cypress FRAM (id %02x)\n", id[6]);
-			return -ENODEV;
-		}
-		/* set size found in ID */
-		if (id[7] < 0x21 || id[7] > 0x26) {
-			dev_err(&spi->dev, "Error: unsupported size (id %02x)\n", id[7]);
-			return -ENODEV;
-		}
-
-		at25->chip.byte_len = BIT(id[7] - 0x21 + 4) * 1024;
-		if (at25->chip.byte_len > 64 * 1024)
-			at25->chip.flags |= EE_ADDR3;
+		if (is_fram)
+			err = at25_fram_to_chip(&spi->dev, &at25->chip);
 		else
-			at25->chip.flags |= EE_ADDR2;
-
-		if (id[8]) {
-			fm25_aux_read(at25, sernum, FM25_RDSN, FM25_SN_LEN);
-			/* swap byte order */
-			for (i = 0; i < FM25_SN_LEN; i++)
-				at25->sernum[i] = sernum[FM25_SN_LEN - 1 - i];
-		}
-
-		at25->chip.page_size = PAGE_SIZE;
-		strncpy(at25->chip.name, "fm25", sizeof(at25->chip.name));
+			err = at25_fw_to_chip(&spi->dev, &at25->chip);
+		if (err)
+			return err;
 	}
 
 	/* For now we only support 8/16/24 bit addressing */
-- 
2.33.0


  parent reply	other threads:[~2021-11-25 21:37 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-25 21:31 [PATCH v1 00/10] misc: at25: Code cleanups and improvements Andy Shevchenko
2021-11-25 21:31 ` [PATCH v1 01/10] misc: at25: Use at25->chip instead of local chip everywhere in ->probe() Andy Shevchenko
2021-11-25 21:31 ` [PATCH v1 02/10] misc: at25: Unshadow error codes in at25_fw_to_chip() Andy Shevchenko
2021-11-25 21:31 ` [PATCH v1 03/10] misc: at25: Check new property ("address-width") first Andy Shevchenko
2021-11-25 21:31 ` [PATCH v1 04/10] misc: at25: Get platform data via dev_get_platdata() Andy Shevchenko
2021-11-25 21:31 ` [PATCH v1 05/10] misc: at25: Get rid of intermediate storage for AT25 chip data Andy Shevchenko
2021-11-25 21:31 ` [PATCH v1 06/10] misc: at25: Switch to use BIT() instead of custom approaches Andy Shevchenko
2021-11-25 21:32 ` Andy Shevchenko [this message]
2021-11-25 21:32 ` [PATCH v1 08/10] misc: at25: Reorganize headers for better maintenance Andy Shevchenko
2021-11-25 21:32 ` [PATCH v1 09/10] misc: at25: Replace commas by spaces in the ID tables Andy Shevchenko
2021-11-25 21:32 ` [PATCH v1 10/10] misc: at25: Align comment style Andy Shevchenko
2021-11-25 22:03 ` [PATCH v1 00/10] misc: at25: Code cleanups and improvements Arnd Bergmann
2021-11-26  9:52   ` Andy Shevchenko
2021-11-26  9:59     ` Andy Shevchenko
2021-12-01 14:19 ` Andy Shevchenko

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=20211125213203.86693-8-andriy.shevchenko@linux.intel.com \
    --to=andriy.shevchenko@linux.intel.com \
    --cc=arnd@arndb.de \
    --cc=gregkh@linuxfoundation.org \
    --cc=jiri.prchal@aksignal.cz \
    --cc=linux-kernel@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