blob: 6a537d959f14b9d5d6df952ef1554fbf63cd7220 [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;
Srinivas Kandagatlaba360fd2016-05-02 19:36:13 +010022 u32 *val = _val;
23 int i = 0, words = bytes / 4;
24
25 while (words--)
Masahiro Yamadaa48f1ff2017-10-21 01:57:40 +090026 *val++ = readl(priv->base + reg + (i++ * 4));
Srinivas Kandagatlaba360fd2016-05-02 19:36:13 +010027
28 return 0;
29}
30
Andrew-CT Chen4c7e4fe2015-12-07 10:58:11 +000031static int mtk_efuse_probe(struct platform_device *pdev)
32{
33 struct device *dev = &pdev->dev;
34 struct resource *res;
35 struct nvmem_device *nvmem;
Masahiro Yamada4dd5f602017-10-21 01:57:39 +090036 struct nvmem_config econfig = {};
Masahiro Yamadaa48f1ff2017-10-21 01:57:40 +090037 struct mtk_efuse_priv *priv;
38
39 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
40 if (!priv)
41 return -ENOMEM;
Andrew-CT Chen4c7e4fe2015-12-07 10:58:11 +000042
43 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Masahiro Yamadaa48f1ff2017-10-21 01:57:40 +090044 priv->base = devm_ioremap_resource(dev, res);
45 if (IS_ERR(priv->base))
46 return PTR_ERR(priv->base);
Andrew-CT Chen4c7e4fe2015-12-07 10:58:11 +000047
Masahiro Yamada4dd5f602017-10-21 01:57:39 +090048 econfig.stride = 4;
49 econfig.word_size = 4;
50 econfig.reg_read = mtk_reg_read;
Masahiro Yamada4dd5f602017-10-21 01:57:39 +090051 econfig.size = resource_size(res);
Masahiro Yamadaa48f1ff2017-10-21 01:57:40 +090052 econfig.priv = priv;
Masahiro Yamada4dd5f602017-10-21 01:57:39 +090053 econfig.dev = dev;
Andrey Smirnov7e68a642018-03-09 14:47:03 +000054 nvmem = devm_nvmem_register(dev, &econfig);
Andrew-CT Chen4c7e4fe2015-12-07 10:58:11 +000055
Andrey Smirnov7e68a642018-03-09 14:47:03 +000056 return PTR_ERR_OR_ZERO(nvmem);
Andrew-CT Chen4c7e4fe2015-12-07 10:58:11 +000057}
58
59static const struct of_device_id mtk_efuse_of_match[] = {
60 { .compatible = "mediatek,mt8173-efuse",},
61 { .compatible = "mediatek,efuse",},
62 {/* sentinel */},
63};
64MODULE_DEVICE_TABLE(of, mtk_efuse_of_match);
65
66static struct platform_driver mtk_efuse_driver = {
67 .probe = mtk_efuse_probe,
Andrew-CT Chen4c7e4fe2015-12-07 10:58:11 +000068 .driver = {
69 .name = "mediatek,efuse",
70 .of_match_table = mtk_efuse_of_match,
71 },
72};
Andrew-CT Chen564e7f82016-02-22 11:24:05 +000073
74static 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
87static void __exit mtk_efuse_exit(void)
88{
89 return platform_driver_unregister(&mtk_efuse_driver);
90}
91
92subsys_initcall(mtk_efuse_init);
93module_exit(mtk_efuse_exit);
94
Andrew-CT Chen4c7e4fe2015-12-07 10:58:11 +000095MODULE_AUTHOR("Andrew-CT Chen <andrew-ct.chen@mediatek.com>");
96MODULE_DESCRIPTION("Mediatek EFUSE driver");
97MODULE_LICENSE("GPL v2");