blob: cb3b48b47d649ac986f4c9597fc2d1a633f3db5a [file] [log] [blame]
Srinivas Kandagatla4ab11992015-07-27 12:15:00 +01001/*
2 * Copyright (C) 2015 Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14#include <linux/device.h>
15#include <linux/module.h>
Srinivas Kandagatla382c62f2016-04-24 20:28:08 +010016#include <linux/io.h>
Srinivas Kandagatla4ab11992015-07-27 12:15:00 +010017#include <linux/nvmem-provider.h>
18#include <linux/platform_device.h>
Srinivas Kandagatla4ab11992015-07-27 12:15:00 +010019
Masahiro Yamadaec3672b2017-10-21 01:57:41 +090020struct qfprom_priv {
21 void __iomem *base;
22};
23
Srinivas Kandagatla382c62f2016-04-24 20:28:08 +010024static int qfprom_reg_read(void *context,
25 unsigned int reg, void *_val, size_t bytes)
26{
Masahiro Yamadaec3672b2017-10-21 01:57:41 +090027 struct qfprom_priv *priv = context;
Vivek Gautam01d0d2c2017-01-04 16:18:09 +000028 u8 *val = _val;
29 int i = 0, words = bytes;
Srinivas Kandagatla4ab11992015-07-27 12:15:00 +010030
Srinivas Kandagatla382c62f2016-04-24 20:28:08 +010031 while (words--)
Masahiro Yamadaec3672b2017-10-21 01:57:41 +090032 *val++ = readb(priv->base + reg + i++);
Srinivas Kandagatla382c62f2016-04-24 20:28:08 +010033
34 return 0;
35}
36
37static int qfprom_reg_write(void *context,
38 unsigned int reg, void *_val, size_t bytes)
39{
Masahiro Yamadaec3672b2017-10-21 01:57:41 +090040 struct qfprom_priv *priv = context;
Vivek Gautam01d0d2c2017-01-04 16:18:09 +000041 u8 *val = _val;
42 int i = 0, words = bytes;
Srinivas Kandagatla382c62f2016-04-24 20:28:08 +010043
44 while (words--)
Masahiro Yamadaec3672b2017-10-21 01:57:41 +090045 writeb(*val++, priv->base + reg + i++);
Srinivas Kandagatla382c62f2016-04-24 20:28:08 +010046
47 return 0;
48}
Srinivas Kandagatla4ab11992015-07-27 12:15:00 +010049
50static int qfprom_remove(struct platform_device *pdev)
51{
52 struct nvmem_device *nvmem = platform_get_drvdata(pdev);
53
54 return nvmem_unregister(nvmem);
55}
56
Srinivas Kandagatla382c62f2016-04-24 20:28:08 +010057static struct nvmem_config econfig = {
58 .name = "qfprom",
Vivek Gautam01d0d2c2017-01-04 16:18:09 +000059 .stride = 1,
Srinivas Kandagatla382c62f2016-04-24 20:28:08 +010060 .word_size = 1,
61 .reg_read = qfprom_reg_read,
62 .reg_write = qfprom_reg_write,
63};
64
Srinivas Kandagatla4ab11992015-07-27 12:15:00 +010065static int qfprom_probe(struct platform_device *pdev)
66{
67 struct device *dev = &pdev->dev;
68 struct resource *res;
69 struct nvmem_device *nvmem;
Masahiro Yamadaec3672b2017-10-21 01:57:41 +090070 struct qfprom_priv *priv;
71
72 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
73 if (!priv)
74 return -ENOMEM;
Srinivas Kandagatla4ab11992015-07-27 12:15:00 +010075
76 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Masahiro Yamadaec3672b2017-10-21 01:57:41 +090077 priv->base = devm_ioremap_resource(dev, res);
78 if (IS_ERR(priv->base))
79 return PTR_ERR(priv->base);
Srinivas Kandagatla4ab11992015-07-27 12:15:00 +010080
Srinivas Kandagatla382c62f2016-04-24 20:28:08 +010081 econfig.size = resource_size(res);
Srinivas Kandagatla4ab11992015-07-27 12:15:00 +010082 econfig.dev = dev;
Masahiro Yamadaec3672b2017-10-21 01:57:41 +090083 econfig.priv = priv;
Srinivas Kandagatla382c62f2016-04-24 20:28:08 +010084
Srinivas Kandagatla4ab11992015-07-27 12:15:00 +010085 nvmem = nvmem_register(&econfig);
86 if (IS_ERR(nvmem))
87 return PTR_ERR(nvmem);
88
89 platform_set_drvdata(pdev, nvmem);
90
91 return 0;
92}
93
94static const struct of_device_id qfprom_of_match[] = {
95 { .compatible = "qcom,qfprom",},
96 {/* sentinel */},
97};
98MODULE_DEVICE_TABLE(of, qfprom_of_match);
99
100static struct platform_driver qfprom_driver = {
101 .probe = qfprom_probe,
102 .remove = qfprom_remove,
103 .driver = {
104 .name = "qcom,qfprom",
105 .of_match_table = qfprom_of_match,
106 },
107};
108module_platform_driver(qfprom_driver);
109MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org>");
110MODULE_DESCRIPTION("Qualcomm QFPROM driver");
111MODULE_LICENSE("GPL v2");