xref: /linux/drivers/nvmem/uniphier-efuse.c (revision 7340c6df49df1b261892d287444c255d0a378063)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * UniPhier eFuse driver
4  *
5  * Copyright (C) 2017 Socionext Inc.
6  */
7 
8 #include <linux/device.h>
9 #include <linux/io.h>
10 #include <linux/module.h>
11 #include <linux/mod_devicetable.h>
12 #include <linux/nvmem-provider.h>
13 #include <linux/platform_device.h>
14 
15 struct uniphier_efuse_priv {
16 	void __iomem *base;
17 };
18 
19 static int uniphier_reg_read(void *context,
20 			     unsigned int reg, void *_val, size_t bytes)
21 {
22 	struct uniphier_efuse_priv *priv = context;
23 	u8 *val = _val;
24 	int offs;
25 
26 	for (offs = 0; offs < bytes; offs += sizeof(u8))
27 		*val++ = readb(priv->base + reg + offs);
28 
29 	return 0;
30 }
31 
32 static int uniphier_efuse_probe(struct platform_device *pdev)
33 {
34 	struct device *dev = &pdev->dev;
35 	struct resource *res;
36 	struct nvmem_device *nvmem;
37 	struct nvmem_config econfig = {};
38 	struct uniphier_efuse_priv *priv;
39 
40 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
41 	if (!priv)
42 		return -ENOMEM;
43 
44 	priv->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
45 	if (IS_ERR(priv->base))
46 		return PTR_ERR(priv->base);
47 
48 	econfig.stride = 1;
49 	econfig.word_size = 1;
50 	econfig.read_only = true;
51 	econfig.reg_read = uniphier_reg_read;
52 	econfig.size = resource_size(res);
53 	econfig.priv = priv;
54 	econfig.dev = dev;
55 	nvmem = devm_nvmem_register(dev, &econfig);
56 
57 	return PTR_ERR_OR_ZERO(nvmem);
58 }
59 
60 static const struct of_device_id uniphier_efuse_of_match[] = {
61 	{ .compatible = "socionext,uniphier-efuse",},
62 	{/* sentinel */},
63 };
64 MODULE_DEVICE_TABLE(of, uniphier_efuse_of_match);
65 
66 static struct platform_driver uniphier_efuse_driver = {
67 	.probe = uniphier_efuse_probe,
68 	.driver = {
69 		.name = "uniphier-efuse",
70 		.of_match_table = uniphier_efuse_of_match,
71 	},
72 };
73 module_platform_driver(uniphier_efuse_driver);
74 
75 MODULE_AUTHOR("Keiji Hayashibara <hayashibara.keiji@socionext.com>");
76 MODULE_DESCRIPTION("UniPhier eFuse driver");
77 MODULE_LICENSE("GPL v2");
78