xref: /linux/drivers/nvmem/mtk-efuse.c (revision 4359a011e259a4608afc7fb3635370c9d4ba5943)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2015 MediaTek Inc.
4  * Author: Andrew-CT Chen <andrew-ct.chen@mediatek.com>
5  */
6 
7 #include <linux/device.h>
8 #include <linux/module.h>
9 #include <linux/mod_devicetable.h>
10 #include <linux/io.h>
11 #include <linux/nvmem-provider.h>
12 #include <linux/platform_device.h>
13 
14 struct mtk_efuse_priv {
15 	void __iomem *base;
16 };
17 
18 static int mtk_reg_read(void *context,
19 			unsigned int reg, void *_val, size_t bytes)
20 {
21 	struct mtk_efuse_priv *priv = context;
22 	void __iomem *addr = priv->base + reg;
23 	u8 *val = _val;
24 	int i;
25 
26 	for (i = 0; i < bytes; i++, val++)
27 		*val = readb(addr + i);
28 
29 	return 0;
30 }
31 
32 static int mtk_efuse_probe(struct platform_device *pdev)
33 {
34 	struct device *dev = &pdev->dev;
35 	struct resource *res;
36 	struct nvmem_device *nvmem;
37 	struct nvmem_config econfig = {};
38 	struct mtk_efuse_priv *priv;
39 
40 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
41 	if (!priv)
42 		return -ENOMEM;
43 
44 	priv->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
45 	if (IS_ERR(priv->base))
46 		return PTR_ERR(priv->base);
47 
48 	econfig.stride = 1;
49 	econfig.word_size = 1;
50 	econfig.reg_read = mtk_reg_read;
51 	econfig.size = resource_size(res);
52 	econfig.priv = priv;
53 	econfig.dev = dev;
54 	nvmem = devm_nvmem_register(dev, &econfig);
55 
56 	return PTR_ERR_OR_ZERO(nvmem);
57 }
58 
59 static const struct of_device_id mtk_efuse_of_match[] = {
60 	{ .compatible = "mediatek,mt8173-efuse",},
61 	{ .compatible = "mediatek,efuse",},
62 	{/* sentinel */},
63 };
64 MODULE_DEVICE_TABLE(of, mtk_efuse_of_match);
65 
66 static struct platform_driver mtk_efuse_driver = {
67 	.probe = mtk_efuse_probe,
68 	.driver = {
69 		.name = "mediatek,efuse",
70 		.of_match_table = mtk_efuse_of_match,
71 	},
72 };
73 
74 static int __init mtk_efuse_init(void)
75 {
76 	int ret;
77 
78 	ret = platform_driver_register(&mtk_efuse_driver);
79 	if (ret) {
80 		pr_err("Failed to register efuse driver\n");
81 		return ret;
82 	}
83 
84 	return 0;
85 }
86 
87 static void __exit mtk_efuse_exit(void)
88 {
89 	return platform_driver_unregister(&mtk_efuse_driver);
90 }
91 
92 subsys_initcall(mtk_efuse_init);
93 module_exit(mtk_efuse_exit);
94 
95 MODULE_AUTHOR("Andrew-CT Chen <andrew-ct.chen@mediatek.com>");
96 MODULE_DESCRIPTION("Mediatek EFUSE driver");
97 MODULE_LICENSE("GPL v2");
98