xref: /openbmc/linux/drivers/nvmem/mtk-efuse.c (revision b732539e)
1 /*
2  * Copyright (c) 2015 MediaTek Inc.
3  * Author: Andrew-CT Chen <andrew-ct.chen@mediatek.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14 
15 #include <linux/device.h>
16 #include <linux/module.h>
17 #include <linux/io.h>
18 #include <linux/nvmem-provider.h>
19 #include <linux/platform_device.h>
20 
21 struct mtk_efuse_priv {
22 	void __iomem *base;
23 };
24 
25 static int mtk_reg_read(void *context,
26 			unsigned int reg, void *_val, size_t bytes)
27 {
28 	struct mtk_efuse_priv *priv = context;
29 	u32 *val = _val;
30 	int i = 0, words = bytes / 4;
31 
32 	while (words--)
33 		*val++ = readl(priv->base + reg + (i++ * 4));
34 
35 	return 0;
36 }
37 
38 static int mtk_reg_write(void *context,
39 			 unsigned int reg, void *_val, size_t bytes)
40 {
41 	struct mtk_efuse_priv *priv = context;
42 	u32 *val = _val;
43 	int i = 0, words = bytes / 4;
44 
45 	while (words--)
46 		writel(*val++, priv->base + reg + (i++ * 4));
47 
48 	return 0;
49 }
50 
51 static int mtk_efuse_probe(struct platform_device *pdev)
52 {
53 	struct device *dev = &pdev->dev;
54 	struct resource *res;
55 	struct nvmem_device *nvmem;
56 	struct nvmem_config econfig = {};
57 	struct mtk_efuse_priv *priv;
58 
59 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
60 	if (!priv)
61 		return -ENOMEM;
62 
63 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
64 	priv->base = devm_ioremap_resource(dev, res);
65 	if (IS_ERR(priv->base))
66 		return PTR_ERR(priv->base);
67 
68 	econfig.stride = 4;
69 	econfig.word_size = 4;
70 	econfig.reg_read = mtk_reg_read;
71 	econfig.reg_write = mtk_reg_write;
72 	econfig.size = resource_size(res);
73 	econfig.priv = priv;
74 	econfig.dev = dev;
75 	nvmem = devm_nvmem_register(dev, &econfig);
76 
77 	return PTR_ERR_OR_ZERO(nvmem);
78 }
79 
80 static const struct of_device_id mtk_efuse_of_match[] = {
81 	{ .compatible = "mediatek,mt8173-efuse",},
82 	{ .compatible = "mediatek,efuse",},
83 	{/* sentinel */},
84 };
85 MODULE_DEVICE_TABLE(of, mtk_efuse_of_match);
86 
87 static struct platform_driver mtk_efuse_driver = {
88 	.probe = mtk_efuse_probe,
89 	.driver = {
90 		.name = "mediatek,efuse",
91 		.of_match_table = mtk_efuse_of_match,
92 	},
93 };
94 
95 static int __init mtk_efuse_init(void)
96 {
97 	int ret;
98 
99 	ret = platform_driver_register(&mtk_efuse_driver);
100 	if (ret) {
101 		pr_err("Failed to register efuse driver\n");
102 		return ret;
103 	}
104 
105 	return 0;
106 }
107 
108 static void __exit mtk_efuse_exit(void)
109 {
110 	return platform_driver_unregister(&mtk_efuse_driver);
111 }
112 
113 subsys_initcall(mtk_efuse_init);
114 module_exit(mtk_efuse_exit);
115 
116 MODULE_AUTHOR("Andrew-CT Chen <andrew-ct.chen@mediatek.com>");
117 MODULE_DESCRIPTION("Mediatek EFUSE driver");
118 MODULE_LICENSE("GPL v2");
119