linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 00/13] nvmem: patches for 5.1
@ 2019-01-28 15:54 Srinivas Kandagatla
  2019-01-28 15:54 ` [PATCH 01/13] nvmem: sc27xx: Convert nvmem offset to block index Srinivas Kandagatla
                   ` (12 more replies)
  0 siblings, 13 replies; 14+ messages in thread
From: Srinivas Kandagatla @ 2019-01-28 15:54 UTC (permalink / raw)
  To: gregkh; +Cc: linux-kernel

Hi Greg,

Here are few nvmem patches which includes
	- some improvements to nvmem core done by Alban
	- add support for imx MX7ULP and MX6ULL.
	- fix block index calculation on sc27xx provider
	- add ACPI support to BCM OCOTP provider

Can you please pick them up for 5.1.

thanks,
srini


Alban Bedel (7):
  nvmem: core: Set the provider read-only when no write callback is
    given
  nvmem: core: Fix of_nvmem_cell_get() for optional cells
  nvmem: core: Fix cell lookup when no cell is found
  nvmem: core: Properly handle connection ID in of_nvmem_device_get()
  nvmem: core: Always reference the device returned by
    nvmem_device_get()
  nvmem: core: Fix device reference leak
  nvmem: core: Avoid useless iterations in nvmem_cell_get_from_lookup()

Anson Huang (2):
  dt-bindings: nvmem: imx-ocotp: add compatible string for i.MX7ULP
  nvmem: imx-ocotp: add i.MX7ULP support

Freeman Liu (1):
  nvmem: sc27xx: Convert nvmem offset to block index

Srinath Mannam (1):
  nvmem: bcm-ocotp: Add ACPI support to BCM OCOTP

Stefan Wahren (2):
  dt-bindings: imx-ocotp: Add i.MX6ULL/ULZ support
  nvmem: imx-ocotp: Implement i.MX6ULL/ULZ support

 .../devicetree/bindings/nvmem/imx-ocotp.txt   |  4 +-
 drivers/nvmem/bcm-ocotp.c                     | 37 +++++++++---------
 drivers/nvmem/core.c                          | 38 +++++++++++--------
 drivers/nvmem/imx-ocotp.c                     | 13 +++++++
 drivers/nvmem/sc27xx-efuse.c                  | 12 ++++--
 5 files changed, 66 insertions(+), 38 deletions(-)

-- 
2.20.1


^ permalink raw reply	[flat|nested] 14+ messages in thread

* [PATCH 01/13] nvmem: sc27xx: Convert nvmem offset to block index
  2019-01-28 15:54 [PATCH 00/13] nvmem: patches for 5.1 Srinivas Kandagatla
@ 2019-01-28 15:54 ` Srinivas Kandagatla
  2019-01-28 15:54 ` [PATCH 02/13] nvmem: bcm-ocotp: Add ACPI support to BCM OCOTP Srinivas Kandagatla
                   ` (11 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: Srinivas Kandagatla @ 2019-01-28 15:54 UTC (permalink / raw)
  To: gregkh; +Cc: linux-kernel

From: Freeman Liu <freeman.liu@unisoc.com>

The Spreadtrum SC27XX efuse data are organized by blocks and each block
contains 2 bytes data. Moreover the nvmem core always pass the offset
in byte to the controller, so we should change the offset in byte to
the correct block index and block offset to read the data.

Signed-off-by: Freeman Liu <freeman.liu@unisoc.com>
Signed-off-by: Baolin Wang <baolin.wang@linaro.org>
Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
---
 drivers/nvmem/sc27xx-efuse.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/drivers/nvmem/sc27xx-efuse.c b/drivers/nvmem/sc27xx-efuse.c
index 33185d8d82cf..c6ee21018d80 100644
--- a/drivers/nvmem/sc27xx-efuse.c
+++ b/drivers/nvmem/sc27xx-efuse.c
@@ -106,10 +106,12 @@ static int sc27xx_efuse_poll_status(struct sc27xx_efuse *efuse, u32 bits)
 static int sc27xx_efuse_read(void *context, u32 offset, void *val, size_t bytes)
 {
 	struct sc27xx_efuse *efuse = context;
-	u32 buf;
+	u32 buf, blk_index = offset / SC27XX_EFUSE_BLOCK_WIDTH;
+	u32 blk_offset = (offset % SC27XX_EFUSE_BLOCK_WIDTH) * BITS_PER_BYTE;
 	int ret;
 
-	if (offset > SC27XX_EFUSE_BLOCK_MAX || bytes > SC27XX_EFUSE_BLOCK_WIDTH)
+	if (blk_index > SC27XX_EFUSE_BLOCK_MAX ||
+	    bytes > SC27XX_EFUSE_BLOCK_WIDTH)
 		return -EINVAL;
 
 	ret = sc27xx_efuse_lock(efuse);
@@ -133,7 +135,7 @@ static int sc27xx_efuse_read(void *context, u32 offset, void *val, size_t bytes)
 	/* Set the block address to be read. */
 	ret = regmap_write(efuse->regmap,
 			   efuse->base + SC27XX_EFUSE_BLOCK_INDEX,
-			   offset & SC27XX_EFUSE_BLOCK_MASK);
+			   blk_index & SC27XX_EFUSE_BLOCK_MASK);
 	if (ret)
 		goto disable_efuse;
 
@@ -171,8 +173,10 @@ static int sc27xx_efuse_read(void *context, u32 offset, void *val, size_t bytes)
 unlock_efuse:
 	sc27xx_efuse_unlock(efuse);
 
-	if (!ret)
+	if (!ret) {
+		buf >>= blk_offset;
 		memcpy(val, &buf, bytes);
+	}
 
 	return ret;
 }
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH 02/13] nvmem: bcm-ocotp: Add ACPI support to BCM OCOTP
  2019-01-28 15:54 [PATCH 00/13] nvmem: patches for 5.1 Srinivas Kandagatla
  2019-01-28 15:54 ` [PATCH 01/13] nvmem: sc27xx: Convert nvmem offset to block index Srinivas Kandagatla
@ 2019-01-28 15:54 ` Srinivas Kandagatla
  2019-01-28 15:54 ` [PATCH 03/13] dt-bindings: nvmem: imx-ocotp: add compatible string for i.MX7ULP Srinivas Kandagatla
                   ` (10 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: Srinivas Kandagatla @ 2019-01-28 15:54 UTC (permalink / raw)
  To: gregkh; +Cc: linux-kernel

From: Srinath Mannam <srinath.mannam@broadcom.com>

Add ACPI support to bcm ocotp driver

This patch is based on Linux-4.20-rc7.

Signed-off-by: Srinath Mannam <srinath.mannam@broadcom.com>
Acked-by: Scott Branden <scott.branden@broadcom.com>
Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
---
 drivers/nvmem/bcm-ocotp.c | 37 ++++++++++++++++++++-----------------
 1 file changed, 20 insertions(+), 17 deletions(-)

diff --git a/drivers/nvmem/bcm-ocotp.c b/drivers/nvmem/bcm-ocotp.c
index 4159b3f41d79..a8097511582a 100644
--- a/drivers/nvmem/bcm-ocotp.c
+++ b/drivers/nvmem/bcm-ocotp.c
@@ -11,13 +11,14 @@
  * GNU General Public License for more details.
  */
 
+#include <linux/acpi.h>
 #include <linux/delay.h>
 #include <linux/device.h>
 #include <linux/io.h>
 #include <linux/module.h>
 #include <linux/nvmem-provider.h>
 #include <linux/of.h>
-#include <linux/of_address.h>
+#include <linux/of_device.h>
 #include <linux/platform_device.h>
 
 /*
@@ -78,9 +79,9 @@ static struct otpc_map otp_map_v2 = {
 };
 
 struct otpc_priv {
-	struct device       *dev;
-	void __iomem        *base;
-	struct otpc_map     *map;
+	struct device *dev;
+	void __iomem *base;
+	const struct otpc_map *map;
 	struct nvmem_config *config;
 };
 
@@ -237,16 +238,22 @@ static struct nvmem_config bcm_otpc_nvmem_config = {
 };
 
 static const struct of_device_id bcm_otpc_dt_ids[] = {
-	{ .compatible = "brcm,ocotp" },
-	{ .compatible = "brcm,ocotp-v2" },
+	{ .compatible = "brcm,ocotp", .data = &otp_map },
+	{ .compatible = "brcm,ocotp-v2", .data = &otp_map_v2 },
 	{ },
 };
 MODULE_DEVICE_TABLE(of, bcm_otpc_dt_ids);
 
+static const struct acpi_device_id bcm_otpc_acpi_ids[] = {
+	{ .id = "BRCM0700", .driver_data = (kernel_ulong_t)&otp_map },
+	{ .id = "BRCM0701", .driver_data = (kernel_ulong_t)&otp_map_v2 },
+	{ /* sentinel */ }
+};
+MODULE_DEVICE_TABLE(acpi, bcm_otpc_acpi_ids);
+
 static int bcm_otpc_probe(struct platform_device *pdev)
 {
 	struct device *dev = &pdev->dev;
-	struct device_node *dn = dev->of_node;
 	struct resource *res;
 	struct otpc_priv *priv;
 	struct nvmem_device *nvmem;
@@ -257,14 +264,9 @@ static int bcm_otpc_probe(struct platform_device *pdev)
 	if (!priv)
 		return -ENOMEM;
 
-	if (of_device_is_compatible(dev->of_node, "brcm,ocotp"))
-		priv->map = &otp_map;
-	else if (of_device_is_compatible(dev->of_node, "brcm,ocotp-v2"))
-		priv->map = &otp_map_v2;
-	else {
-		dev_err(dev, "%s otpc config map not defined\n", __func__);
-		return -EINVAL;
-	}
+	priv->map = device_get_match_data(dev);
+	if (!priv->map)
+		return -ENODEV;
 
 	/* Get OTP base address register. */
 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
@@ -281,7 +283,7 @@ static int bcm_otpc_probe(struct platform_device *pdev)
 	reset_start_bit(priv->base);
 
 	/* Read size of memory in words. */
-	err = of_property_read_u32(dn, "brcm,ocotp-size", &num_words);
+	err = device_property_read_u32(dev, "brcm,ocotp-size", &num_words);
 	if (err) {
 		dev_err(dev, "size parameter not specified\n");
 		return -EINVAL;
@@ -294,7 +296,7 @@ static int bcm_otpc_probe(struct platform_device *pdev)
 	bcm_otpc_nvmem_config.dev = dev;
 	bcm_otpc_nvmem_config.priv = priv;
 
-	if (of_device_is_compatible(dev->of_node, "brcm,ocotp-v2")) {
+	if (priv->map == &otp_map_v2) {
 		bcm_otpc_nvmem_config.word_size = 8;
 		bcm_otpc_nvmem_config.stride = 8;
 	}
@@ -315,6 +317,7 @@ static struct platform_driver bcm_otpc_driver = {
 	.driver = {
 		.name	= "brcm-otpc",
 		.of_match_table = bcm_otpc_dt_ids,
+		.acpi_match_table = ACPI_PTR(bcm_otpc_acpi_ids),
 	},
 };
 module_platform_driver(bcm_otpc_driver);
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH 03/13] dt-bindings: nvmem: imx-ocotp: add compatible string for i.MX7ULP
  2019-01-28 15:54 [PATCH 00/13] nvmem: patches for 5.1 Srinivas Kandagatla
  2019-01-28 15:54 ` [PATCH 01/13] nvmem: sc27xx: Convert nvmem offset to block index Srinivas Kandagatla
  2019-01-28 15:54 ` [PATCH 02/13] nvmem: bcm-ocotp: Add ACPI support to BCM OCOTP Srinivas Kandagatla
@ 2019-01-28 15:54 ` Srinivas Kandagatla
  2019-01-28 15:54 ` [PATCH 04/13] nvmem: imx-ocotp: add i.MX7ULP support Srinivas Kandagatla
                   ` (9 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: Srinivas Kandagatla @ 2019-01-28 15:54 UTC (permalink / raw)
  To: gregkh; +Cc: linux-kernel

From: Anson Huang <anson.huang@nxp.com>

Add new compatible string for i.MX7ULP SOC.

Signed-off-by: Anson Huang <Anson.Huang@nxp.com>
Reviewed-by: Rob Herring <robh@kernel.org>
Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
---
 Documentation/devicetree/bindings/nvmem/imx-ocotp.txt | 1 +
 1 file changed, 1 insertion(+)

diff --git a/Documentation/devicetree/bindings/nvmem/imx-ocotp.txt b/Documentation/devicetree/bindings/nvmem/imx-ocotp.txt
index 792bc5fafeb9..ff389cf1606e 100644
--- a/Documentation/devicetree/bindings/nvmem/imx-ocotp.txt
+++ b/Documentation/devicetree/bindings/nvmem/imx-ocotp.txt
@@ -11,6 +11,7 @@ Required properties:
 	"fsl,imx6ul-ocotp" (i.MX6UL),
 	"fsl,imx7d-ocotp" (i.MX7D/S),
 	"fsl,imx6sll-ocotp" (i.MX6SLL),
+	"fsl,imx7ulp-ocotp" (i.MX7ULP),
 	followed by "syscon".
 - #address-cells : Should be 1
 - #size-cells : Should be 1
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH 04/13] nvmem: imx-ocotp: add i.MX7ULP support
  2019-01-28 15:54 [PATCH 00/13] nvmem: patches for 5.1 Srinivas Kandagatla
                   ` (2 preceding siblings ...)
  2019-01-28 15:54 ` [PATCH 03/13] dt-bindings: nvmem: imx-ocotp: add compatible string for i.MX7ULP Srinivas Kandagatla
@ 2019-01-28 15:54 ` Srinivas Kandagatla
  2019-01-28 15:54 ` [PATCH 05/13] dt-bindings: imx-ocotp: Add i.MX6ULL/ULZ support Srinivas Kandagatla
                   ` (8 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: Srinivas Kandagatla @ 2019-01-28 15:54 UTC (permalink / raw)
  To: gregkh; +Cc: linux-kernel

From: Anson Huang <anson.huang@nxp.com>

i.MX7ULP is a new SoC of i.MX family which has 8 kbit eFuse OTP,
enable ocotp driver support for this SoC.

Signed-off-by: Anson Huang <Anson.Huang@nxp.com>
Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
---
 drivers/nvmem/imx-ocotp.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/drivers/nvmem/imx-ocotp.c b/drivers/nvmem/imx-ocotp.c
index afb429a417fe..90a398252770 100644
--- a/drivers/nvmem/imx-ocotp.c
+++ b/drivers/nvmem/imx-ocotp.c
@@ -433,6 +433,11 @@ static const struct ocotp_params imx7d_params = {
 	.set_timing = imx_ocotp_set_imx7_timing,
 };
 
+static const struct ocotp_params imx7ulp_params = {
+	.nregs = 256,
+	.bank_address_words = 0,
+};
+
 static const struct of_device_id imx_ocotp_dt_ids[] = {
 	{ .compatible = "fsl,imx6q-ocotp",  .data = &imx6q_params },
 	{ .compatible = "fsl,imx6sl-ocotp", .data = &imx6sl_params },
@@ -440,6 +445,7 @@ static const struct of_device_id imx_ocotp_dt_ids[] = {
 	{ .compatible = "fsl,imx6ul-ocotp", .data = &imx6ul_params },
 	{ .compatible = "fsl,imx7d-ocotp",  .data = &imx7d_params },
 	{ .compatible = "fsl,imx6sll-ocotp", .data = &imx6sll_params },
+	{ .compatible = "fsl,imx7ulp-ocotp", .data = &imx7ulp_params },
 	{ },
 };
 MODULE_DEVICE_TABLE(of, imx_ocotp_dt_ids);
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH 05/13] dt-bindings: imx-ocotp: Add i.MX6ULL/ULZ support
  2019-01-28 15:54 [PATCH 00/13] nvmem: patches for 5.1 Srinivas Kandagatla
                   ` (3 preceding siblings ...)
  2019-01-28 15:54 ` [PATCH 04/13] nvmem: imx-ocotp: add i.MX7ULP support Srinivas Kandagatla
@ 2019-01-28 15:54 ` Srinivas Kandagatla
  2019-01-28 15:54 ` [PATCH 06/13] nvmem: imx-ocotp: Implement " Srinivas Kandagatla
                   ` (7 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: Srinivas Kandagatla @ 2019-01-28 15:54 UTC (permalink / raw)
  To: gregkh; +Cc: linux-kernel

From: Stefan Wahren <stefan.wahren@i2se.com>

Since the i.MX6ULL/ULZ only supports 8 OTP banks we need to introduce a new
compatible.

Signed-off-by: Stefan Wahren <stefan.wahren@i2se.com>
Reviewed-by: Rob Herring <robh@kernel.org>
Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
---
 Documentation/devicetree/bindings/nvmem/imx-ocotp.txt | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/Documentation/devicetree/bindings/nvmem/imx-ocotp.txt b/Documentation/devicetree/bindings/nvmem/imx-ocotp.txt
index ff389cf1606e..7a999a135e56 100644
--- a/Documentation/devicetree/bindings/nvmem/imx-ocotp.txt
+++ b/Documentation/devicetree/bindings/nvmem/imx-ocotp.txt
@@ -1,7 +1,7 @@
 Freescale i.MX6 On-Chip OTP Controller (OCOTP) device tree bindings
 
 This binding represents the on-chip eFuse OTP controller found on
-i.MX6Q/D, i.MX6DL/S, i.MX6SL, i.MX6SX, i.MX6UL and i.MX6SLL SoCs.
+i.MX6Q/D, i.MX6DL/S, i.MX6SL, i.MX6SX, i.MX6UL, i.MX6ULL/ULZ and i.MX6SLL SoCs.
 
 Required properties:
 - compatible: should be one of
@@ -9,6 +9,7 @@ Required properties:
 	"fsl,imx6sl-ocotp" (i.MX6SL), or
 	"fsl,imx6sx-ocotp" (i.MX6SX),
 	"fsl,imx6ul-ocotp" (i.MX6UL),
+	"fsl,imx6ull-ocotp" (i.MX6ULL/ULZ),
 	"fsl,imx7d-ocotp" (i.MX7D/S),
 	"fsl,imx6sll-ocotp" (i.MX6SLL),
 	"fsl,imx7ulp-ocotp" (i.MX7ULP),
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH 06/13] nvmem: imx-ocotp: Implement i.MX6ULL/ULZ support
  2019-01-28 15:54 [PATCH 00/13] nvmem: patches for 5.1 Srinivas Kandagatla
                   ` (4 preceding siblings ...)
  2019-01-28 15:54 ` [PATCH 05/13] dt-bindings: imx-ocotp: Add i.MX6ULL/ULZ support Srinivas Kandagatla
@ 2019-01-28 15:54 ` Srinivas Kandagatla
  2019-01-28 15:55 ` [PATCH 07/13] nvmem: core: Set the provider read-only when no write callback is given Srinivas Kandagatla
                   ` (6 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: Srinivas Kandagatla @ 2019-01-28 15:54 UTC (permalink / raw)
  To: gregkh; +Cc: linux-kernel

From: Stefan Wahren <stefan.wahren@i2se.com>

In comparision to the i.MX6UL the lower cost variants i.MX6ULL/ULZ only
supports 8 OTP banks a 8 words.

Signed-off-by: Stefan Wahren <stefan.wahren@i2se.com>
Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
---
 drivers/nvmem/imx-ocotp.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/drivers/nvmem/imx-ocotp.c b/drivers/nvmem/imx-ocotp.c
index 90a398252770..08a9b1ef8ae4 100644
--- a/drivers/nvmem/imx-ocotp.c
+++ b/drivers/nvmem/imx-ocotp.c
@@ -427,6 +427,12 @@ static const struct ocotp_params imx6ul_params = {
 	.set_timing = imx_ocotp_set_imx6_timing,
 };
 
+static const struct ocotp_params imx6ull_params = {
+	.nregs = 64,
+	.bank_address_words = 0,
+	.set_timing = imx_ocotp_set_imx6_timing,
+};
+
 static const struct ocotp_params imx7d_params = {
 	.nregs = 64,
 	.bank_address_words = 4,
@@ -443,6 +449,7 @@ static const struct of_device_id imx_ocotp_dt_ids[] = {
 	{ .compatible = "fsl,imx6sl-ocotp", .data = &imx6sl_params },
 	{ .compatible = "fsl,imx6sx-ocotp", .data = &imx6sx_params },
 	{ .compatible = "fsl,imx6ul-ocotp", .data = &imx6ul_params },
+	{ .compatible = "fsl,imx6ull-ocotp", .data = &imx6ull_params },
 	{ .compatible = "fsl,imx7d-ocotp",  .data = &imx7d_params },
 	{ .compatible = "fsl,imx6sll-ocotp", .data = &imx6sll_params },
 	{ .compatible = "fsl,imx7ulp-ocotp", .data = &imx7ulp_params },
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH 07/13] nvmem: core: Set the provider read-only when no write callback is given
  2019-01-28 15:54 [PATCH 00/13] nvmem: patches for 5.1 Srinivas Kandagatla
                   ` (5 preceding siblings ...)
  2019-01-28 15:54 ` [PATCH 06/13] nvmem: imx-ocotp: Implement " Srinivas Kandagatla
@ 2019-01-28 15:55 ` Srinivas Kandagatla
  2019-01-28 15:55 ` [PATCH 08/13] nvmem: core: Fix of_nvmem_cell_get() for optional cells Srinivas Kandagatla
                   ` (5 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: Srinivas Kandagatla @ 2019-01-28 15:55 UTC (permalink / raw)
  To: gregkh; +Cc: linux-kernel

From: Alban Bedel <albeu@free.fr>

If no write callback is given the device should be marked as read-only.
While at it also move from a bit or to a logical or as that is a logical
expression.

Signed-off-by: Alban Bedel <albeu@free.fr>
Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
---
 drivers/nvmem/core.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index f7301bb4ef3b..cf2e1091fe89 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -646,8 +646,8 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
 			     config->name ? config->id : nvmem->id);
 	}
 
-	nvmem->read_only = device_property_present(config->dev, "read-only") |
-			   config->read_only;
+	nvmem->read_only = device_property_present(config->dev, "read-only") ||
+			   config->read_only || !nvmem->reg_write;
 
 	if (config->root_only)
 		nvmem->dev.groups = nvmem->read_only ?
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH 08/13] nvmem: core: Fix of_nvmem_cell_get() for optional cells
  2019-01-28 15:54 [PATCH 00/13] nvmem: patches for 5.1 Srinivas Kandagatla
                   ` (6 preceding siblings ...)
  2019-01-28 15:55 ` [PATCH 07/13] nvmem: core: Set the provider read-only when no write callback is given Srinivas Kandagatla
@ 2019-01-28 15:55 ` Srinivas Kandagatla
  2019-01-28 15:55 ` [PATCH 09/13] nvmem: core: Fix cell lookup when no cell is found Srinivas Kandagatla
                   ` (4 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: Srinivas Kandagatla @ 2019-01-28 15:55 UTC (permalink / raw)
  To: gregkh; +Cc: linux-kernel

From: Alban Bedel <albeu@free.fr>

of_nvmem_cell_get() should return -ENOENT when a cell isn't defined,
otherwise callers can't distinguish between a missing cell and other
errors.

Signed-off-by: Alban Bedel <albeu@free.fr>
Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
---
 drivers/nvmem/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index cf2e1091fe89..f8c43da6f2ca 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -1031,7 +1031,7 @@ struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, const char *id)
 
 	cell_np = of_parse_phandle(np, "nvmem-cells", index);
 	if (!cell_np)
-		return ERR_PTR(-EINVAL);
+		return ERR_PTR(-ENOENT);
 
 	nvmem_np = of_get_next_parent(cell_np);
 	if (!nvmem_np)
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH 09/13] nvmem: core: Fix cell lookup when no cell is found
  2019-01-28 15:54 [PATCH 00/13] nvmem: patches for 5.1 Srinivas Kandagatla
                   ` (7 preceding siblings ...)
  2019-01-28 15:55 ` [PATCH 08/13] nvmem: core: Fix of_nvmem_cell_get() for optional cells Srinivas Kandagatla
@ 2019-01-28 15:55 ` Srinivas Kandagatla
  2019-01-28 15:55 ` [PATCH 10/13] nvmem: core: Properly handle connection ID in of_nvmem_device_get() Srinivas Kandagatla
                   ` (3 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: Srinivas Kandagatla @ 2019-01-28 15:55 UTC (permalink / raw)
  To: gregkh; +Cc: linux-kernel

From: Alban Bedel <albeu@free.fr>

If the cell list is not empty and nvmem_find_cell_by_node/name() is
called for a cell that is not present in the list they will return an
invalid pointer instead of NULL. This happen because
list_for_each_entry() stop once it reach the list head again, but as
the list head is not contained in a struct nvmem_cell the iteration
variable then contains an invalid value.

This is easily solved by using a variable to iterate over the list and
one to return the cell found.

Signed-off-by: Alban Bedel <albeu@free.fr>
Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
---
 drivers/nvmem/core.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index f8c43da6f2ca..a2ad44104aa2 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -525,12 +525,14 @@ static int nvmem_add_cells_from_table(struct nvmem_device *nvmem)
 static struct nvmem_cell *
 nvmem_find_cell_by_name(struct nvmem_device *nvmem, const char *cell_id)
 {
-	struct nvmem_cell *cell = NULL;
+	struct nvmem_cell *iter, *cell = NULL;
 
 	mutex_lock(&nvmem_mutex);
-	list_for_each_entry(cell, &nvmem->cells, node) {
-		if (strcmp(cell_id, cell->name) == 0)
+	list_for_each_entry(iter, &nvmem->cells, node) {
+		if (strcmp(cell_id, iter->name) == 0) {
+			cell = iter;
 			break;
+		}
 	}
 	mutex_unlock(&nvmem_mutex);
 
@@ -994,12 +996,14 @@ nvmem_cell_get_from_lookup(struct device *dev, const char *con_id)
 static struct nvmem_cell *
 nvmem_find_cell_by_node(struct nvmem_device *nvmem, struct device_node *np)
 {
-	struct nvmem_cell *cell = NULL;
+	struct nvmem_cell *iter, *cell = NULL;
 
 	mutex_lock(&nvmem_mutex);
-	list_for_each_entry(cell, &nvmem->cells, node) {
-		if (np == cell->np)
+	list_for_each_entry(iter, &nvmem->cells, node) {
+		if (np == iter->np) {
+			cell = iter;
 			break;
+		}
 	}
 	mutex_unlock(&nvmem_mutex);
 
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH 10/13] nvmem: core: Properly handle connection ID in of_nvmem_device_get()
  2019-01-28 15:54 [PATCH 00/13] nvmem: patches for 5.1 Srinivas Kandagatla
                   ` (8 preceding siblings ...)
  2019-01-28 15:55 ` [PATCH 09/13] nvmem: core: Fix cell lookup when no cell is found Srinivas Kandagatla
@ 2019-01-28 15:55 ` Srinivas Kandagatla
  2019-01-28 15:55 ` [PATCH 11/13] nvmem: core: Always reference the device returned by nvmem_device_get() Srinivas Kandagatla
                   ` (2 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: Srinivas Kandagatla @ 2019-01-28 15:55 UTC (permalink / raw)
  To: gregkh; +Cc: linux-kernel

From: Alban Bedel <albeu@free.fr>

of_nvmem_device_get() would crash if NULL was passed as a connection
ID. Rework this to use the usual sementic of assuming the first
connection when no connection ID is given.

Furthermore of_nvmem_device_get() would return -EINVAL when it failed
to resolve the connection, making it impossible to properly implement
an optional connection. Return -ENOENT instead to let the caller know
that the connection doesn't exists.

Signed-off-by: Alban Bedel <albeu@free.fr>
Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
---
 drivers/nvmem/core.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index a2ad44104aa2..a749a5cb0a4e 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -839,13 +839,14 @@ struct nvmem_device *of_nvmem_device_get(struct device_node *np, const char *id)
 {
 
 	struct device_node *nvmem_np;
-	int index;
+	int index = 0;
 
-	index = of_property_match_string(np, "nvmem-names", id);
+	if (id)
+		index = of_property_match_string(np, "nvmem-names", id);
 
 	nvmem_np = of_parse_phandle(np, "nvmem", index);
 	if (!nvmem_np)
-		return ERR_PTR(-EINVAL);
+		return ERR_PTR(-ENOENT);
 
 	return __nvmem_device_get(nvmem_np, NULL);
 }
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH 11/13] nvmem: core: Always reference the device returned by nvmem_device_get()
  2019-01-28 15:54 [PATCH 00/13] nvmem: patches for 5.1 Srinivas Kandagatla
                   ` (9 preceding siblings ...)
  2019-01-28 15:55 ` [PATCH 10/13] nvmem: core: Properly handle connection ID in of_nvmem_device_get() Srinivas Kandagatla
@ 2019-01-28 15:55 ` Srinivas Kandagatla
  2019-01-28 15:55 ` [PATCH 12/13] nvmem: core: Fix device reference leak Srinivas Kandagatla
  2019-01-28 15:55 ` [PATCH 13/13] nvmem: core: Avoid useless iterations in nvmem_cell_get_from_lookup() Srinivas Kandagatla
  12 siblings, 0 replies; 14+ messages in thread
From: Srinivas Kandagatla @ 2019-01-28 15:55 UTC (permalink / raw)
  To: gregkh; +Cc: linux-kernel

From: Alban Bedel <albeu@free.fr>

In nvmem_device_get(), when the device lookup fails with DT it
currently fallback on nvmem_find() which is wrong for two reasons.
First nvmem_find() return NULL when nothing is found instead of an
ERR_PTR. But nvmem_find() also just lookup the device, it doesn't
reference the module and increment the reference count like it is done
in the DT path.

To fix this we replace the call to nvmem_find() with a call to
__nvmem_device_get() which does all the referencing and return a
proper ERR_PTR in case of error.

Signed-off-by: Alban Bedel <albeu@free.fr>
Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
---
 drivers/nvmem/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index a749a5cb0a4e..1752768dd2d2 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -874,7 +874,7 @@ struct nvmem_device *nvmem_device_get(struct device *dev, const char *dev_name)
 
 	}
 
-	return nvmem_find(dev_name);
+	return __nvmem_device_get(NULL, dev_name);
 }
 EXPORT_SYMBOL_GPL(nvmem_device_get);
 
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH 12/13] nvmem: core: Fix device reference leak
  2019-01-28 15:54 [PATCH 00/13] nvmem: patches for 5.1 Srinivas Kandagatla
                   ` (10 preceding siblings ...)
  2019-01-28 15:55 ` [PATCH 11/13] nvmem: core: Always reference the device returned by nvmem_device_get() Srinivas Kandagatla
@ 2019-01-28 15:55 ` Srinivas Kandagatla
  2019-01-28 15:55 ` [PATCH 13/13] nvmem: core: Avoid useless iterations in nvmem_cell_get_from_lookup() Srinivas Kandagatla
  12 siblings, 0 replies; 14+ messages in thread
From: Srinivas Kandagatla @ 2019-01-28 15:55 UTC (permalink / raw)
  To: gregkh; +Cc: linux-kernel

From: Alban Bedel <albeu@free.fr>

__nvmem_device_get() make use of bus_find_device() to get the relevant
device and this function increase the reference count of the device
found, however this is not accounted for anywhere. Fix
__nvmem_device_get() and __nvmem_device_put() to properly release this
reference count.

Signed-off-by: Alban Bedel <albeu@free.fr>
Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
---
 drivers/nvmem/core.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index 1752768dd2d2..5400017ef616 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -811,6 +811,7 @@ static struct nvmem_device *__nvmem_device_get(struct device_node *np,
 			"could not increase module refcount for cell %s\n",
 			nvmem_dev_name(nvmem));
 
+		put_device(&nvmem->dev);
 		return ERR_PTR(-EINVAL);
 	}
 
@@ -821,6 +822,7 @@ static struct nvmem_device *__nvmem_device_get(struct device_node *np,
 
 static void __nvmem_device_put(struct nvmem_device *nvmem)
 {
+	put_device(&nvmem->dev);
 	module_put(nvmem->owner);
 	kref_put(&nvmem->refcnt, nvmem_device_release);
 }
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH 13/13] nvmem: core: Avoid useless iterations in nvmem_cell_get_from_lookup()
  2019-01-28 15:54 [PATCH 00/13] nvmem: patches for 5.1 Srinivas Kandagatla
                   ` (11 preceding siblings ...)
  2019-01-28 15:55 ` [PATCH 12/13] nvmem: core: Fix device reference leak Srinivas Kandagatla
@ 2019-01-28 15:55 ` Srinivas Kandagatla
  12 siblings, 0 replies; 14+ messages in thread
From: Srinivas Kandagatla @ 2019-01-28 15:55 UTC (permalink / raw)
  To: gregkh; +Cc: linux-kernel

From: Alban Bedel <albeu@free.fr>

Once the correct cell has been found there is no need to continue
iterating, just stop there. While at it replace the goto used to leave
the loop with simple break statements.

Signed-off-by: Alban Bedel <albeu@free.fr>
Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
---
 drivers/nvmem/core.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index 5400017ef616..9dd07eae1f3e 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -977,7 +977,7 @@ nvmem_cell_get_from_lookup(struct device *dev, const char *con_id)
 			if (IS_ERR(nvmem)) {
 				/* Provider may not be registered yet. */
 				cell = ERR_CAST(nvmem);
-				goto out;
+				break;
 			}
 
 			cell = nvmem_find_cell_by_name(nvmem,
@@ -985,12 +985,11 @@ nvmem_cell_get_from_lookup(struct device *dev, const char *con_id)
 			if (!cell) {
 				__nvmem_device_put(nvmem);
 				cell = ERR_PTR(-ENOENT);
-				goto out;
 			}
+			break;
 		}
 	}
 
-out:
 	mutex_unlock(&nvmem_lookup_mutex);
 	return cell;
 }
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2019-01-28 17:36 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-01-28 15:54 [PATCH 00/13] nvmem: patches for 5.1 Srinivas Kandagatla
2019-01-28 15:54 ` [PATCH 01/13] nvmem: sc27xx: Convert nvmem offset to block index Srinivas Kandagatla
2019-01-28 15:54 ` [PATCH 02/13] nvmem: bcm-ocotp: Add ACPI support to BCM OCOTP Srinivas Kandagatla
2019-01-28 15:54 ` [PATCH 03/13] dt-bindings: nvmem: imx-ocotp: add compatible string for i.MX7ULP Srinivas Kandagatla
2019-01-28 15:54 ` [PATCH 04/13] nvmem: imx-ocotp: add i.MX7ULP support Srinivas Kandagatla
2019-01-28 15:54 ` [PATCH 05/13] dt-bindings: imx-ocotp: Add i.MX6ULL/ULZ support Srinivas Kandagatla
2019-01-28 15:54 ` [PATCH 06/13] nvmem: imx-ocotp: Implement " Srinivas Kandagatla
2019-01-28 15:55 ` [PATCH 07/13] nvmem: core: Set the provider read-only when no write callback is given Srinivas Kandagatla
2019-01-28 15:55 ` [PATCH 08/13] nvmem: core: Fix of_nvmem_cell_get() for optional cells Srinivas Kandagatla
2019-01-28 15:55 ` [PATCH 09/13] nvmem: core: Fix cell lookup when no cell is found Srinivas Kandagatla
2019-01-28 15:55 ` [PATCH 10/13] nvmem: core: Properly handle connection ID in of_nvmem_device_get() Srinivas Kandagatla
2019-01-28 15:55 ` [PATCH 11/13] nvmem: core: Always reference the device returned by nvmem_device_get() Srinivas Kandagatla
2019-01-28 15:55 ` [PATCH 12/13] nvmem: core: Fix device reference leak Srinivas Kandagatla
2019-01-28 15:55 ` [PATCH 13/13] nvmem: core: Avoid useless iterations in nvmem_cell_get_from_lookup() Srinivas Kandagatla

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).