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