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