blob: d057f1bfb2e985e03decc8e744643590a6386847 [file] [log] [blame]
Thomas Gleixner97fb5e82019-05-29 07:17:58 -07001// SPDX-License-Identifier: GPL-2.0-only
Srinivas Kandagatla4ab11992015-07-27 12:15:00 +01002/*
3 * Copyright (C) 2015 Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
Srinivas Kandagatla4ab11992015-07-27 12:15:00 +01004 */
5
6#include <linux/device.h>
7#include <linux/module.h>
Randy Dunlapac316722018-06-19 22:47:28 -07008#include <linux/mod_devicetable.h>
Srinivas Kandagatla382c62f2016-04-24 20:28:08 +01009#include <linux/io.h>
Srinivas Kandagatla4ab11992015-07-27 12:15:00 +010010#include <linux/nvmem-provider.h>
11#include <linux/platform_device.h>
Srinivas Kandagatla4ab11992015-07-27 12:15:00 +010012
Masahiro Yamadaec3672b2017-10-21 01:57:41 +090013struct qfprom_priv {
14 void __iomem *base;
15};
16
Srinivas Kandagatla382c62f2016-04-24 20:28:08 +010017static int qfprom_reg_read(void *context,
18 unsigned int reg, void *_val, size_t bytes)
19{
Masahiro Yamadaec3672b2017-10-21 01:57:41 +090020 struct qfprom_priv *priv = context;
Vivek Gautam01d0d2c2017-01-04 16:18:09 +000021 u8 *val = _val;
22 int i = 0, words = bytes;
Srinivas Kandagatla4ab11992015-07-27 12:15:00 +010023
Srinivas Kandagatla382c62f2016-04-24 20:28:08 +010024 while (words--)
Masahiro Yamadaec3672b2017-10-21 01:57:41 +090025 *val++ = readb(priv->base + reg + i++);
Srinivas Kandagatla382c62f2016-04-24 20:28:08 +010026
27 return 0;
28}
29
30static int qfprom_reg_write(void *context,
31 unsigned int reg, void *_val, size_t bytes)
32{
Masahiro Yamadaec3672b2017-10-21 01:57:41 +090033 struct qfprom_priv *priv = context;
Vivek Gautam01d0d2c2017-01-04 16:18:09 +000034 u8 *val = _val;
35 int i = 0, words = bytes;
Srinivas Kandagatla382c62f2016-04-24 20:28:08 +010036
37 while (words--)
Masahiro Yamadaec3672b2017-10-21 01:57:41 +090038 writeb(*val++, priv->base + reg + i++);
Srinivas Kandagatla382c62f2016-04-24 20:28:08 +010039
40 return 0;
41}
Srinivas Kandagatla4ab11992015-07-27 12:15:00 +010042
Srinivas Kandagatla382c62f2016-04-24 20:28:08 +010043static struct nvmem_config econfig = {
44 .name = "qfprom",
Vivek Gautam01d0d2c2017-01-04 16:18:09 +000045 .stride = 1,
Srinivas Kandagatla382c62f2016-04-24 20:28:08 +010046 .word_size = 1,
47 .reg_read = qfprom_reg_read,
48 .reg_write = qfprom_reg_write,
49};
50
Srinivas Kandagatla4ab11992015-07-27 12:15:00 +010051static int qfprom_probe(struct platform_device *pdev)
52{
53 struct device *dev = &pdev->dev;
54 struct resource *res;
55 struct nvmem_device *nvmem;
Masahiro Yamadaec3672b2017-10-21 01:57:41 +090056 struct qfprom_priv *priv;
57
58 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
59 if (!priv)
60 return -ENOMEM;
Srinivas Kandagatla4ab11992015-07-27 12:15:00 +010061
62 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Masahiro Yamadaec3672b2017-10-21 01:57:41 +090063 priv->base = devm_ioremap_resource(dev, res);
64 if (IS_ERR(priv->base))
65 return PTR_ERR(priv->base);
Srinivas Kandagatla4ab11992015-07-27 12:15:00 +010066
Srinivas Kandagatla382c62f2016-04-24 20:28:08 +010067 econfig.size = resource_size(res);
Srinivas Kandagatla4ab11992015-07-27 12:15:00 +010068 econfig.dev = dev;
Masahiro Yamadaec3672b2017-10-21 01:57:41 +090069 econfig.priv = priv;
Srinivas Kandagatla382c62f2016-04-24 20:28:08 +010070
Andrey Smirnove5692ef2018-03-09 14:47:09 +000071 nvmem = devm_nvmem_register(dev, &econfig);
Srinivas Kandagatla4ab11992015-07-27 12:15:00 +010072
Andrey Smirnove5692ef2018-03-09 14:47:09 +000073 return PTR_ERR_OR_ZERO(nvmem);
Srinivas Kandagatla4ab11992015-07-27 12:15:00 +010074}
75
76static const struct of_device_id qfprom_of_match[] = {
77 { .compatible = "qcom,qfprom",},
78 {/* sentinel */},
79};
80MODULE_DEVICE_TABLE(of, qfprom_of_match);
81
82static struct platform_driver qfprom_driver = {
83 .probe = qfprom_probe,
Srinivas Kandagatla4ab11992015-07-27 12:15:00 +010084 .driver = {
85 .name = "qcom,qfprom",
86 .of_match_table = qfprom_of_match,
87 },
88};
89module_platform_driver(qfprom_driver);
90MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org>");
91MODULE_DESCRIPTION("Qualcomm QFPROM driver");
92MODULE_LICENSE("GPL v2");