blob: e9a375dd84af81ae9a921e2313b1885edd3fbcc2 [file] [log] [blame]
Thomas Gleixner1802d0b2019-05-27 08:55:21 +02001// SPDX-License-Identifier: GPL-2.0-only
Andrew-CT Chen4c7e4fe2015-12-07 10:58:11 +00002/*
3 * Copyright (c) 2015 MediaTek Inc.
4 * Author: Andrew-CT Chen <andrew-ct.chen@mediatek.com>
Andrew-CT Chen4c7e4fe2015-12-07 10:58:11 +00005 */
6
7#include <linux/device.h>
8#include <linux/module.h>
Randy Dunlapac316722018-06-19 22:47:28 -07009#include <linux/mod_devicetable.h>
Srinivas Kandagatlaba360fd2016-05-02 19:36:13 +010010#include <linux/io.h>
Andrew-CT Chen4c7e4fe2015-12-07 10:58:11 +000011#include <linux/nvmem-provider.h>
12#include <linux/platform_device.h>
Andrew-CT Chen4c7e4fe2015-12-07 10:58:11 +000013
Masahiro Yamadaa48f1ff2017-10-21 01:57:40 +090014struct mtk_efuse_priv {
15 void __iomem *base;
16};
17
Srinivas Kandagatlaba360fd2016-05-02 19:36:13 +010018static int mtk_reg_read(void *context,
19 unsigned int reg, void *_val, size_t bytes)
20{
Masahiro Yamadaa48f1ff2017-10-21 01:57:40 +090021 struct mtk_efuse_priv *priv = context;
Chunfeng Yun98e2c4e2021-12-09 17:42:34 +000022 void __iomem *addr = priv->base + reg;
23 u8 *val = _val;
24 int i;
Srinivas Kandagatlaba360fd2016-05-02 19:36:13 +010025
Chunfeng Yun98e2c4e2021-12-09 17:42:34 +000026 for (i = 0; i < bytes; i++, val++)
27 *val = readb(addr + i);
Srinivas Kandagatlaba360fd2016-05-02 19:36:13 +010028
29 return 0;
30}
31
Andrew-CT Chen4c7e4fe2015-12-07 10:58:11 +000032static int mtk_efuse_probe(struct platform_device *pdev)
33{
34 struct device *dev = &pdev->dev;
35 struct resource *res;
36 struct nvmem_device *nvmem;
Masahiro Yamada4dd5f602017-10-21 01:57:39 +090037 struct nvmem_config econfig = {};
Masahiro Yamadaa48f1ff2017-10-21 01:57:40 +090038 struct mtk_efuse_priv *priv;
39
40 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
41 if (!priv)
42 return -ENOMEM;
Andrew-CT Chen4c7e4fe2015-12-07 10:58:11 +000043
44 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Masahiro Yamadaa48f1ff2017-10-21 01:57:40 +090045 priv->base = devm_ioremap_resource(dev, res);
46 if (IS_ERR(priv->base))
47 return PTR_ERR(priv->base);
Andrew-CT Chen4c7e4fe2015-12-07 10:58:11 +000048
Chunfeng Yun98e2c4e2021-12-09 17:42:34 +000049 econfig.stride = 1;
50 econfig.word_size = 1;
Masahiro Yamada4dd5f602017-10-21 01:57:39 +090051 econfig.reg_read = mtk_reg_read;
Masahiro Yamada4dd5f602017-10-21 01:57:39 +090052 econfig.size = resource_size(res);
Masahiro Yamadaa48f1ff2017-10-21 01:57:40 +090053 econfig.priv = priv;
Masahiro Yamada4dd5f602017-10-21 01:57:39 +090054 econfig.dev = dev;
Andrey Smirnov7e68a642018-03-09 14:47:03 +000055 nvmem = devm_nvmem_register(dev, &econfig);
Andrew-CT Chen4c7e4fe2015-12-07 10:58:11 +000056
Andrey Smirnov7e68a642018-03-09 14:47:03 +000057 return PTR_ERR_OR_ZERO(nvmem);
Andrew-CT Chen4c7e4fe2015-12-07 10:58:11 +000058}
59
60static const struct of_device_id mtk_efuse_of_match[] = {
61 { .compatible = "mediatek,mt8173-efuse",},
62 { .compatible = "mediatek,efuse",},
63 {/* sentinel */},
64};
65MODULE_DEVICE_TABLE(of, mtk_efuse_of_match);
66
67static struct platform_driver mtk_efuse_driver = {
68 .probe = mtk_efuse_probe,
Andrew-CT Chen4c7e4fe2015-12-07 10:58:11 +000069 .driver = {
70 .name = "mediatek,efuse",
71 .of_match_table = mtk_efuse_of_match,
72 },
73};
Andrew-CT Chen564e7f82016-02-22 11:24:05 +000074
75static int __init mtk_efuse_init(void)
76{
77 int ret;
78
79 ret = platform_driver_register(&mtk_efuse_driver);
80 if (ret) {
81 pr_err("Failed to register efuse driver\n");
82 return ret;
83 }
84
85 return 0;
86}
87
88static void __exit mtk_efuse_exit(void)
89{
90 return platform_driver_unregister(&mtk_efuse_driver);
91}
92
93subsys_initcall(mtk_efuse_init);
94module_exit(mtk_efuse_exit);
95
Andrew-CT Chen4c7e4fe2015-12-07 10:58:11 +000096MODULE_AUTHOR("Andrew-CT Chen <andrew-ct.chen@mediatek.com>");
97MODULE_DESCRIPTION("Mediatek EFUSE driver");
98MODULE_LICENSE("GPL v2");