1 /*
2  * UniPhier eFuse driver
3  *
4  * Copyright (C) 2017 Socionext Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15 
16 #include <linux/device.h>
17 #include <linux/io.h>
18 #include <linux/module.h>
19 #include <linux/mod_devicetable.h>
20 #include <linux/nvmem-provider.h>
21 #include <linux/platform_device.h>
22 
23 struct uniphier_efuse_priv {
24 	void __iomem *base;
25 };
26 
27 static int uniphier_reg_read(void *context,
28 			     unsigned int reg, void *_val, size_t bytes)
29 {
30 	struct uniphier_efuse_priv *priv = context;
31 	u8 *val = _val;
32 	int offs;
33 
34 	for (offs = 0; offs < bytes; offs += sizeof(u8))
35 		*val++ = readb(priv->base + reg + offs);
36 
37 	return 0;
38 }
39 
40 static int uniphier_efuse_probe(struct platform_device *pdev)
41 {
42 	struct device *dev = &pdev->dev;
43 	struct resource *res;
44 	struct nvmem_device *nvmem;
45 	struct nvmem_config econfig = {};
46 	struct uniphier_efuse_priv *priv;
47 
48 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
49 	if (!priv)
50 		return -ENOMEM;
51 
52 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
53 	priv->base = devm_ioremap_resource(dev, res);
54 	if (IS_ERR(priv->base))
55 		return PTR_ERR(priv->base);
56 
57 	econfig.stride = 1;
58 	econfig.word_size = 1;
59 	econfig.read_only = true;
60 	econfig.reg_read = uniphier_reg_read;
61 	econfig.size = resource_size(res);
62 	econfig.priv = priv;
63 	econfig.dev = dev;
64 	nvmem = devm_nvmem_register(dev, &econfig);
65 
66 	return PTR_ERR_OR_ZERO(nvmem);
67 }
68 
69 static const struct of_device_id uniphier_efuse_of_match[] = {
70 	{ .compatible = "socionext,uniphier-efuse",},
71 	{/* sentinel */},
72 };
73 MODULE_DEVICE_TABLE(of, uniphier_efuse_of_match);
74 
75 static struct platform_driver uniphier_efuse_driver = {
76 	.probe = uniphier_efuse_probe,
77 	.driver = {
78 		.name = "uniphier-efuse",
79 		.of_match_table = uniphier_efuse_of_match,
80 	},
81 };
82 module_platform_driver(uniphier_efuse_driver);
83 
84 MODULE_AUTHOR("Keiji Hayashibara <hayashibara.keiji@socionext.com>");
85 MODULE_DESCRIPTION("UniPhier eFuse driver");
86 MODULE_LICENSE("GPL v2");
87