Thomas Gleixner | 1802d0b | 2019-05-27 08:55:21 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Andrew-CT Chen | 4c7e4fe | 2015-12-07 10:58:11 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2015 MediaTek Inc. |
| 4 | * Author: Andrew-CT Chen <andrew-ct.chen@mediatek.com> |
Andrew-CT Chen | 4c7e4fe | 2015-12-07 10:58:11 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <linux/device.h> |
| 8 | #include <linux/module.h> |
Randy Dunlap | ac31672 | 2018-06-19 22:47:28 -0700 | [diff] [blame] | 9 | #include <linux/mod_devicetable.h> |
Srinivas Kandagatla | ba360fd | 2016-05-02 19:36:13 +0100 | [diff] [blame] | 10 | #include <linux/io.h> |
Andrew-CT Chen | 4c7e4fe | 2015-12-07 10:58:11 +0000 | [diff] [blame] | 11 | #include <linux/nvmem-provider.h> |
| 12 | #include <linux/platform_device.h> |
Andrew-CT Chen | 4c7e4fe | 2015-12-07 10:58:11 +0000 | [diff] [blame] | 13 | |
Masahiro Yamada | a48f1ff | 2017-10-21 01:57:40 +0900 | [diff] [blame] | 14 | struct mtk_efuse_priv { |
| 15 | void __iomem *base; |
| 16 | }; |
| 17 | |
Srinivas Kandagatla | ba360fd | 2016-05-02 19:36:13 +0100 | [diff] [blame] | 18 | static int mtk_reg_read(void *context, |
| 19 | unsigned int reg, void *_val, size_t bytes) |
| 20 | { |
Masahiro Yamada | a48f1ff | 2017-10-21 01:57:40 +0900 | [diff] [blame] | 21 | struct mtk_efuse_priv *priv = context; |
Chunfeng Yun | 98e2c4e | 2021-12-09 17:42:34 +0000 | [diff] [blame] | 22 | void __iomem *addr = priv->base + reg; |
| 23 | u8 *val = _val; |
| 24 | int i; |
Srinivas Kandagatla | ba360fd | 2016-05-02 19:36:13 +0100 | [diff] [blame] | 25 | |
Chunfeng Yun | 98e2c4e | 2021-12-09 17:42:34 +0000 | [diff] [blame] | 26 | for (i = 0; i < bytes; i++, val++) |
| 27 | *val = readb(addr + i); |
Srinivas Kandagatla | ba360fd | 2016-05-02 19:36:13 +0100 | [diff] [blame] | 28 | |
| 29 | return 0; |
| 30 | } |
| 31 | |
Andrew-CT Chen | 4c7e4fe | 2015-12-07 10:58:11 +0000 | [diff] [blame] | 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; |
Masahiro Yamada | 4dd5f60 | 2017-10-21 01:57:39 +0900 | [diff] [blame] | 37 | struct nvmem_config econfig = {}; |
Masahiro Yamada | a48f1ff | 2017-10-21 01:57:40 +0900 | [diff] [blame] | 38 | struct mtk_efuse_priv *priv; |
| 39 | |
| 40 | priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); |
| 41 | if (!priv) |
| 42 | return -ENOMEM; |
Andrew-CT Chen | 4c7e4fe | 2015-12-07 10:58:11 +0000 | [diff] [blame] | 43 | |
| 44 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Masahiro Yamada | a48f1ff | 2017-10-21 01:57:40 +0900 | [diff] [blame] | 45 | priv->base = devm_ioremap_resource(dev, res); |
| 46 | if (IS_ERR(priv->base)) |
| 47 | return PTR_ERR(priv->base); |
Andrew-CT Chen | 4c7e4fe | 2015-12-07 10:58:11 +0000 | [diff] [blame] | 48 | |
Chunfeng Yun | 98e2c4e | 2021-12-09 17:42:34 +0000 | [diff] [blame] | 49 | econfig.stride = 1; |
| 50 | econfig.word_size = 1; |
Masahiro Yamada | 4dd5f60 | 2017-10-21 01:57:39 +0900 | [diff] [blame] | 51 | econfig.reg_read = mtk_reg_read; |
Masahiro Yamada | 4dd5f60 | 2017-10-21 01:57:39 +0900 | [diff] [blame] | 52 | econfig.size = resource_size(res); |
Masahiro Yamada | a48f1ff | 2017-10-21 01:57:40 +0900 | [diff] [blame] | 53 | econfig.priv = priv; |
Masahiro Yamada | 4dd5f60 | 2017-10-21 01:57:39 +0900 | [diff] [blame] | 54 | econfig.dev = dev; |
Andrey Smirnov | 7e68a64 | 2018-03-09 14:47:03 +0000 | [diff] [blame] | 55 | nvmem = devm_nvmem_register(dev, &econfig); |
Andrew-CT Chen | 4c7e4fe | 2015-12-07 10:58:11 +0000 | [diff] [blame] | 56 | |
Andrey Smirnov | 7e68a64 | 2018-03-09 14:47:03 +0000 | [diff] [blame] | 57 | return PTR_ERR_OR_ZERO(nvmem); |
Andrew-CT Chen | 4c7e4fe | 2015-12-07 10:58:11 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | static const struct of_device_id mtk_efuse_of_match[] = { |
| 61 | { .compatible = "mediatek,mt8173-efuse",}, |
| 62 | { .compatible = "mediatek,efuse",}, |
| 63 | {/* sentinel */}, |
| 64 | }; |
| 65 | MODULE_DEVICE_TABLE(of, mtk_efuse_of_match); |
| 66 | |
| 67 | static struct platform_driver mtk_efuse_driver = { |
| 68 | .probe = mtk_efuse_probe, |
Andrew-CT Chen | 4c7e4fe | 2015-12-07 10:58:11 +0000 | [diff] [blame] | 69 | .driver = { |
| 70 | .name = "mediatek,efuse", |
| 71 | .of_match_table = mtk_efuse_of_match, |
| 72 | }, |
| 73 | }; |
Andrew-CT Chen | 564e7f8 | 2016-02-22 11:24:05 +0000 | [diff] [blame] | 74 | |
| 75 | static 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 | |
| 88 | static void __exit mtk_efuse_exit(void) |
| 89 | { |
| 90 | return platform_driver_unregister(&mtk_efuse_driver); |
| 91 | } |
| 92 | |
| 93 | subsys_initcall(mtk_efuse_init); |
| 94 | module_exit(mtk_efuse_exit); |
| 95 | |
Andrew-CT Chen | 4c7e4fe | 2015-12-07 10:58:11 +0000 | [diff] [blame] | 96 | MODULE_AUTHOR("Andrew-CT Chen <andrew-ct.chen@mediatek.com>"); |
| 97 | MODULE_DESCRIPTION("Mediatek EFUSE driver"); |
| 98 | MODULE_LICENSE("GPL v2"); |