xref: /linux/drivers/nvmem/meson-efuse.c (revision 2a267e7c41aa88215de2b542de797d03d16ecdfd)
1 /*
2  * Amlogic Meson GX eFuse Driver
3  *
4  * Copyright (c) 2016 Endless Computers, Inc.
5  * Author: Carlo Caione <carlo@endlessm.com>
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of version 2 of the GNU General Public License as
9  * published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14  * more details.
15  */
16 
17 #include <linux/clk.h>
18 #include <linux/module.h>
19 #include <linux/nvmem-provider.h>
20 #include <linux/of.h>
21 #include <linux/platform_device.h>
22 
23 #include <linux/firmware/meson/meson_sm.h>
24 
25 static int meson_efuse_read(void *context, unsigned int offset,
26 			    void *val, size_t bytes)
27 {
28 	return meson_sm_call_read((u8 *)val, bytes, SM_EFUSE_READ, offset,
29 				  bytes, 0, 0, 0);
30 }
31 
32 static int meson_efuse_write(void *context, unsigned int offset,
33 			     void *val, size_t bytes)
34 {
35 	return meson_sm_call_write((u8 *)val, bytes, SM_EFUSE_WRITE, offset,
36 				   bytes, 0, 0, 0);
37 }
38 
39 static const struct of_device_id meson_efuse_match[] = {
40 	{ .compatible = "amlogic,meson-gxbb-efuse", },
41 	{ /* sentinel */ },
42 };
43 MODULE_DEVICE_TABLE(of, meson_efuse_match);
44 
45 static int meson_efuse_probe(struct platform_device *pdev)
46 {
47 	struct device *dev = &pdev->dev;
48 	struct nvmem_device *nvmem;
49 	struct nvmem_config *econfig;
50 	struct clk *clk;
51 	unsigned int size;
52 	int ret;
53 
54 	clk = devm_clk_get(dev, NULL);
55 	if (IS_ERR(clk)) {
56 		ret = PTR_ERR(clk);
57 		if (ret != -EPROBE_DEFER)
58 			dev_err(dev, "failed to get efuse gate");
59 		return ret;
60 	}
61 
62 	ret = clk_prepare_enable(clk);
63 	if (ret) {
64 		dev_err(dev, "failed to enable gate");
65 		return ret;
66 	}
67 
68 	ret = devm_add_action_or_reset(dev,
69 				       (void(*)(void *))clk_disable_unprepare,
70 				       clk);
71 	if (ret) {
72 		dev_err(dev, "failed to add disable callback");
73 		return ret;
74 	}
75 
76 	if (meson_sm_call(SM_EFUSE_USER_MAX, &size, 0, 0, 0, 0, 0) < 0) {
77 		dev_err(dev, "failed to get max user");
78 		return -EINVAL;
79 	}
80 
81 	econfig = devm_kzalloc(dev, sizeof(*econfig), GFP_KERNEL);
82 	if (!econfig)
83 		return -ENOMEM;
84 
85 	econfig->dev = dev;
86 	econfig->name = dev_name(dev);
87 	econfig->stride = 1;
88 	econfig->word_size = 1;
89 	econfig->reg_read = meson_efuse_read;
90 	econfig->reg_write = meson_efuse_write;
91 	econfig->size = size;
92 
93 	nvmem = devm_nvmem_register(&pdev->dev, econfig);
94 
95 	return PTR_ERR_OR_ZERO(nvmem);
96 }
97 
98 static struct platform_driver meson_efuse_driver = {
99 	.probe = meson_efuse_probe,
100 	.driver = {
101 		.name = "meson-efuse",
102 		.of_match_table = meson_efuse_match,
103 	},
104 };
105 
106 module_platform_driver(meson_efuse_driver);
107 
108 MODULE_AUTHOR("Carlo Caione <carlo@endlessm.com>");
109 MODULE_DESCRIPTION("Amlogic Meson GX NVMEM driver");
110 MODULE_LICENSE("GPL v2");
111