This patch adds probe and remove helper functions for nvmems which are
mmio based, With these helper function new nvmem consumer drivers need
very little code add its driver.

This code is currently used for qfprom and sunxi-sid nvmem consumer
drivers.

Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@xxxxxxxxxx>
---
 drivers/nvmem/Makefile     |  1 +
 drivers/nvmem/nvmem-mmio.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++
 drivers/nvmem/nvmem-mmio.h | 41 +++++++++++++++++++++++++++
 3 files changed, 111 insertions(+)
 create mode 100644 drivers/nvmem/nvmem-mmio.c
 create mode 100644 drivers/nvmem/nvmem-mmio.h

Index: linux-4.4.39-rt50/drivers/nvmem/Makefile
===================================================================
--- linux-4.4.39-rt50.orig/drivers/nvmem/Makefile       2016-01-11 00:01:32.000000000 +0100
+++ linux-4.4.39-rt50/drivers/nvmem/Makefile    2017-01-27 00:01:26.681006531 +0100
@@ -4,6 +4,7 @@
 
 obj-$(CONFIG_NVMEM)            += nvmem_core.o
 nvmem_core-y                   := core.o
+nvmem_core-y                   += nvmem-mmio.o
 
 # Devices
 obj-$(CONFIG_NVMEM_IMX_OCOTP)  += nvmem-imx-ocotp.o
Index: linux-4.4.39-rt50/drivers/nvmem/nvmem-mmio.c
===================================================================
--- /dev/null   1970-01-01 00:00:00.000000000 +0000
+++ linux-4.4.39-rt50/drivers/nvmem/nvmem-mmio.c        2017-01-27 00:00:49.430148965 +0100
@@ -0,0 +1,69 @@
+/*
+ * Copyright (C) 2015 Srinivas Kandagatla <srinivas.kandagatla@xxxxxxxxxx>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 and
+ * only version 2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include <linux/device.h>
+#include <linux/module.h>
+#include <linux/err.h>
+#include <linux/of.h>
+#include <linux/of_device.h>
+#include "nvmem-mmio.h"
+
+int nvmem_mmio_remove(struct platform_device *pdev)
+{
+       struct nvmem_device *nvmem = platform_get_drvdata(pdev);
+
+       return nvmem_unregister(nvmem);
+}
+EXPORT_SYMBOL_GPL(nvmem_mmio_remove);
+
+int nvmem_mmio_probe(struct platform_device *pdev)
+{
+       struct device *dev = &pdev->dev;
+       struct resource *res;
+       const struct nvmem_mmio_data *data;
+       struct nvmem_device *nvmem;
+       struct regmap *regmap;
+       const struct of_device_id *match;
+       void __iomem *base;
+
+       if (!dev || !dev->driver)
+               return -ENODEV;
+
+       match = of_match_device(dev->driver->of_match_table, dev);
+       if (!match || !match->data)
+               return -EINVAL;
+
+       data = match->data;
+
+       res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+       base = devm_ioremap_resource(dev, res);
+       if (IS_ERR(base))
+               return PTR_ERR(base);
+
+       data->regmap_config->max_register = resource_size(res) - 1;
+
+       regmap = devm_regmap_init_mmio(dev, base, data->regmap_config);
+       if (IS_ERR(regmap)) {
+               dev_err(dev, "regmap init failed\n");
+               return PTR_ERR(regmap);
+       }
+       data->nvmem_config->dev = dev;
+       nvmem = nvmem_register(data->nvmem_config);
+       if (IS_ERR(nvmem))
+               return PTR_ERR(nvmem);
+
+       platform_set_drvdata(pdev, nvmem);
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(nvmem_mmio_probe);
Index: linux-4.4.39-rt50/drivers/nvmem/nvmem-mmio.h
===================================================================
--- /dev/null   1970-01-01 00:00:00.000000000 +0000
+++ linux-4.4.39-rt50/drivers/nvmem/nvmem-mmio.h        2017-01-27 00:00:49.453148877 +0100
@@ -0,0 +1,41 @@
+/*
+ * MMIO based nvmem providers.
+ *
+ * Copyright (C) 2015 Srinivas Kandagatla <srinivas.kandagatla@xxxxxxxxxx>
+ *
+ * This file is licensed under the terms of the GNU General Public
+ * License version 2.  This program is licensed "as is" without any
+ * warranty of any kind, whether express or implied.
+ */
+
+#ifndef _LINUX_NVMEM_MMIO_H
+#define _LINUX_NVMEM_MMIO_H
+
+#include <linux/platform_device.h>
+#include <linux/nvmem-provider.h>
+#include <linux/regmap.h>
+
+struct nvmem_mmio_data {
+       struct regmap_config *regmap_config;
+       struct nvmem_config *nvmem_config;
+};
+
+#if IS_ENABLED(CONFIG_NVMEM)
+
+int nvmem_mmio_probe(struct platform_device *pdev);
+int nvmem_mmio_remove(struct platform_device *pdev);
+
+#else
+
+static inline int nvmem_mmio_probe(struct platform_device *pdev)
+{
+       return -ENOSYS;
+}
+
+static inline int nvmem_mmio_remove(struct platform_device *pdev)
+{
+       return -ENOSYS;
+}
+#endif
+
+#endif  /* ifndef _LINUX_NVMEM_MMIO_H */