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