1 /*
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>
16 #include <linux/mod_devicetable.h>
17 #include <linux/io.h>
18 #include <linux/nvmem-provider.h>
19 #include <linux/platform_device.h>
20 
21 struct qfprom_priv {
22 	void __iomem *base;
23 };
24 
qfprom_reg_read(void * context,unsigned int reg,void * _val,size_t bytes)25 static int qfprom_reg_read(void *context,
26 			unsigned int reg, void *_val, size_t bytes)
27 {
28 	struct qfprom_priv *priv = context;
29 	u8 *val = _val;
30 	int i = 0, words = bytes;
31 
32 	while (words--)
33 		*val++ = readb(priv->base + reg + i++);
34 
35 	return 0;
36 }
37 
38 static struct nvmem_config econfig = {
39 	.name = "qfprom",
40 	.stride = 1,
41 	.word_size = 1,
42 	.reg_read = qfprom_reg_read,
43 };
44 
qfprom_probe(struct platform_device * pdev)45 static int qfprom_probe(struct platform_device *pdev)
46 {
47 	struct device *dev = &pdev->dev;
48 	struct resource *res;
49 	struct nvmem_device *nvmem;
50 	struct qfprom_priv *priv;
51 
52 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
53 	if (!priv)
54 		return -ENOMEM;
55 
56 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
57 	priv->base = devm_ioremap_resource(dev, res);
58 	if (IS_ERR(priv->base))
59 		return PTR_ERR(priv->base);
60 
61 	econfig.size = resource_size(res);
62 	econfig.dev = dev;
63 	econfig.priv = priv;
64 
65 	nvmem = devm_nvmem_register(dev, &econfig);
66 
67 	return PTR_ERR_OR_ZERO(nvmem);
68 }
69 
70 static const struct of_device_id qfprom_of_match[] = {
71 	{ .compatible = "qcom,qfprom",},
72 	{/* sentinel */},
73 };
74 MODULE_DEVICE_TABLE(of, qfprom_of_match);
75 
76 static struct platform_driver qfprom_driver = {
77 	.probe = qfprom_probe,
78 	.driver = {
79 		.name = "qcom,qfprom",
80 		.of_match_table = qfprom_of_match,
81 	},
82 };
83 module_platform_driver(qfprom_driver);
84 MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org>");
85 MODULE_DESCRIPTION("Qualcomm QFPROM driver");
86 MODULE_LICENSE("GPL v2");
87