Re: [PATCH 3/8] nvmem: Add driver for the eeprom in qnap-mcu controllers

From: Andy Shevchenko

Date: Wed Nov 26 2025 - 08:42:05 EST


On Fri, Nov 14, 2025 at 11:06:31AM +0000, srini@xxxxxxxxxx wrote:
>
> The qnap-mcu also has an eeprom connected to it, that contains some
> specific product-information like the mac addresses for the network
> interfaces.
>
> Add a nvmem driver for it.

I have no problem with current code, but below some hints in case you want to
improve it.

...

+ device.h
+ err.h

> +#include <linux/mfd/qnap-mcu.h>
> +#include <linux/module.h>
> +#include <linux/nvmem-provider.h>
> +#include <linux/platform_device.h>
> +#include <linux/slab.h>

+ string.h
+ types.h

...

> +static int qnap_mcu_eeprom_read_block(struct qnap_mcu *mcu, unsigned int offset,
> + void *val, size_t bytes)
> +{
> + const u8 cmd[] = { 0xf7, 0xa1, offset, bytes };
> + u8 *reply;
> + int ret = 0;
> +
> + reply = kzalloc(bytes + sizeof(cmd), GFP_KERNEL);
> + if (!reply)
> + return -ENOMEM;

Using __free() from cleanup.h actually fits here and makes code easier to read.

> + ret = qnap_mcu_exec(mcu, cmd, sizeof(cmd), reply, bytes + sizeof(cmd));
> + if (ret)
> + goto out;
> +
> + /* First bytes must mirror the sent command */
> + if (memcmp(cmd, reply, sizeof(cmd))) {
> + ret = -EIO;
> + goto out;
> + }
> +
> + memcpy(val, reply + sizeof(cmd), bytes);
> +
> +out:
> + kfree(reply);
> + return ret;
> +}

...

> +static int qnap_mcu_eeprom_probe(struct platform_device *pdev)
> +{

struct device *parent = pdev->dev.parent;

> + struct qnap_mcu *mcu = dev_get_drvdata(pdev->dev.parent);

struct qnap_mcu *mcu = dev_get_drvdata(parent);

> + struct nvmem_config nvcfg = {};
> + struct nvmem_device *ndev;
> +
> + nvcfg.dev = &pdev->dev;

> + nvcfg.of_node = pdev->dev.parent->of_node;

nvcfg.of_node = dev_of_node(parent);

(will need to include of.h, but ideally nvmem can be switched to fwnode)

> + nvcfg.name = dev_name(&pdev->dev);
> + nvcfg.id = NVMEM_DEVID_NONE;
> + nvcfg.owner = THIS_MODULE;
> + nvcfg.type = NVMEM_TYPE_EEPROM;
> + nvcfg.read_only = true;
> + nvcfg.root_only = false;
> + nvcfg.reg_read = qnap_mcu_eeprom_read;
> + nvcfg.size = QNAP_MCU_EEPROM_SIZE,
> + nvcfg.word_size = 1,
> + nvcfg.stride = 1,
> + nvcfg.priv = mcu,
> +
> + ndev = devm_nvmem_register(&pdev->dev, &nvcfg);

> + if (IS_ERR(ndev))
> + return PTR_ERR(ndev);
> +
> + return 0;

retrun PTR_ERR_OR_ZERO(...);

> +}

--
With Best Regards,
Andy Shevchenko