Andrew-CT Chen | 4c7e4fe | 2015-12-07 10:58:11 +0000 | [diff] [blame] | 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> |
Randy Dunlap | ac31672 | 2018-06-19 22:47:28 -0700 | [diff] [blame^] | 17 | #include <linux/mod_devicetable.h> |
Srinivas Kandagatla | ba360fd | 2016-05-02 19:36:13 +0100 | [diff] [blame] | 18 | #include <linux/io.h> |
Andrew-CT Chen | 4c7e4fe | 2015-12-07 10:58:11 +0000 | [diff] [blame] | 19 | #include <linux/nvmem-provider.h> |
| 20 | #include <linux/platform_device.h> |
Andrew-CT Chen | 4c7e4fe | 2015-12-07 10:58:11 +0000 | [diff] [blame] | 21 | |
Masahiro Yamada | a48f1ff | 2017-10-21 01:57:40 +0900 | [diff] [blame] | 22 | struct mtk_efuse_priv { |
| 23 | void __iomem *base; |
| 24 | }; |
| 25 | |
Srinivas Kandagatla | ba360fd | 2016-05-02 19:36:13 +0100 | [diff] [blame] | 26 | static int mtk_reg_read(void *context, |
| 27 | unsigned int reg, void *_val, size_t bytes) |
| 28 | { |
Masahiro Yamada | a48f1ff | 2017-10-21 01:57:40 +0900 | [diff] [blame] | 29 | struct mtk_efuse_priv *priv = context; |
Srinivas Kandagatla | ba360fd | 2016-05-02 19:36:13 +0100 | [diff] [blame] | 30 | u32 *val = _val; |
| 31 | int i = 0, words = bytes / 4; |
| 32 | |
| 33 | while (words--) |
Masahiro Yamada | a48f1ff | 2017-10-21 01:57:40 +0900 | [diff] [blame] | 34 | *val++ = readl(priv->base + reg + (i++ * 4)); |
Srinivas Kandagatla | ba360fd | 2016-05-02 19:36:13 +0100 | [diff] [blame] | 35 | |
| 36 | return 0; |
| 37 | } |
| 38 | |
| 39 | static int mtk_reg_write(void *context, |
| 40 | unsigned int reg, void *_val, size_t bytes) |
| 41 | { |
Masahiro Yamada | a48f1ff | 2017-10-21 01:57:40 +0900 | [diff] [blame] | 42 | struct mtk_efuse_priv *priv = context; |
Srinivas Kandagatla | ba360fd | 2016-05-02 19:36:13 +0100 | [diff] [blame] | 43 | u32 *val = _val; |
| 44 | int i = 0, words = bytes / 4; |
| 45 | |
| 46 | while (words--) |
Masahiro Yamada | a48f1ff | 2017-10-21 01:57:40 +0900 | [diff] [blame] | 47 | writel(*val++, priv->base + reg + (i++ * 4)); |
Srinivas Kandagatla | ba360fd | 2016-05-02 19:36:13 +0100 | [diff] [blame] | 48 | |
| 49 | return 0; |
| 50 | } |
Andrew-CT Chen | 4c7e4fe | 2015-12-07 10:58:11 +0000 | [diff] [blame] | 51 | |
| 52 | static int mtk_efuse_probe(struct platform_device *pdev) |
| 53 | { |
| 54 | struct device *dev = &pdev->dev; |
| 55 | struct resource *res; |
| 56 | struct nvmem_device *nvmem; |
Masahiro Yamada | 4dd5f60 | 2017-10-21 01:57:39 +0900 | [diff] [blame] | 57 | struct nvmem_config econfig = {}; |
Masahiro Yamada | a48f1ff | 2017-10-21 01:57:40 +0900 | [diff] [blame] | 58 | struct mtk_efuse_priv *priv; |
| 59 | |
| 60 | priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); |
| 61 | if (!priv) |
| 62 | return -ENOMEM; |
Andrew-CT Chen | 4c7e4fe | 2015-12-07 10:58:11 +0000 | [diff] [blame] | 63 | |
| 64 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Masahiro Yamada | a48f1ff | 2017-10-21 01:57:40 +0900 | [diff] [blame] | 65 | priv->base = devm_ioremap_resource(dev, res); |
| 66 | if (IS_ERR(priv->base)) |
| 67 | return PTR_ERR(priv->base); |
Andrew-CT Chen | 4c7e4fe | 2015-12-07 10:58:11 +0000 | [diff] [blame] | 68 | |
Masahiro Yamada | 4dd5f60 | 2017-10-21 01:57:39 +0900 | [diff] [blame] | 69 | econfig.stride = 4; |
| 70 | econfig.word_size = 4; |
| 71 | econfig.reg_read = mtk_reg_read; |
| 72 | econfig.reg_write = mtk_reg_write; |
| 73 | econfig.size = resource_size(res); |
Masahiro Yamada | a48f1ff | 2017-10-21 01:57:40 +0900 | [diff] [blame] | 74 | econfig.priv = priv; |
Masahiro Yamada | 4dd5f60 | 2017-10-21 01:57:39 +0900 | [diff] [blame] | 75 | econfig.dev = dev; |
Andrey Smirnov | 7e68a64 | 2018-03-09 14:47:03 +0000 | [diff] [blame] | 76 | nvmem = devm_nvmem_register(dev, &econfig); |
Andrew-CT Chen | 4c7e4fe | 2015-12-07 10:58:11 +0000 | [diff] [blame] | 77 | |
Andrey Smirnov | 7e68a64 | 2018-03-09 14:47:03 +0000 | [diff] [blame] | 78 | return PTR_ERR_OR_ZERO(nvmem); |
Andrew-CT Chen | 4c7e4fe | 2015-12-07 10:58:11 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | static const struct of_device_id mtk_efuse_of_match[] = { |
| 82 | { .compatible = "mediatek,mt8173-efuse",}, |
| 83 | { .compatible = "mediatek,efuse",}, |
| 84 | {/* sentinel */}, |
| 85 | }; |
| 86 | MODULE_DEVICE_TABLE(of, mtk_efuse_of_match); |
| 87 | |
| 88 | static struct platform_driver mtk_efuse_driver = { |
| 89 | .probe = mtk_efuse_probe, |
Andrew-CT Chen | 4c7e4fe | 2015-12-07 10:58:11 +0000 | [diff] [blame] | 90 | .driver = { |
| 91 | .name = "mediatek,efuse", |
| 92 | .of_match_table = mtk_efuse_of_match, |
| 93 | }, |
| 94 | }; |
Andrew-CT Chen | 564e7f8 | 2016-02-22 11:24:05 +0000 | [diff] [blame] | 95 | |
| 96 | static int __init mtk_efuse_init(void) |
| 97 | { |
| 98 | int ret; |
| 99 | |
| 100 | ret = platform_driver_register(&mtk_efuse_driver); |
| 101 | if (ret) { |
| 102 | pr_err("Failed to register efuse driver\n"); |
| 103 | return ret; |
| 104 | } |
| 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | static void __exit mtk_efuse_exit(void) |
| 110 | { |
| 111 | return platform_driver_unregister(&mtk_efuse_driver); |
| 112 | } |
| 113 | |
| 114 | subsys_initcall(mtk_efuse_init); |
| 115 | module_exit(mtk_efuse_exit); |
| 116 | |
Andrew-CT Chen | 4c7e4fe | 2015-12-07 10:58:11 +0000 | [diff] [blame] | 117 | MODULE_AUTHOR("Andrew-CT Chen <andrew-ct.chen@mediatek.com>"); |
| 118 | MODULE_DESCRIPTION("Mediatek EFUSE driver"); |
| 119 | MODULE_LICENSE("GPL v2"); |