xref: /openbmc/linux/drivers/nvmem/meson-efuse.c (revision b8d312aa)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Amlogic Meson GX eFuse Driver
4  *
5  * Copyright (c) 2016 Endless Computers, Inc.
6  * Author: Carlo Caione <carlo@endlessm.com>
7  */
8 
9 #include <linux/clk.h>
10 #include <linux/module.h>
11 #include <linux/nvmem-provider.h>
12 #include <linux/of.h>
13 #include <linux/platform_device.h>
14 
15 #include <linux/firmware/meson/meson_sm.h>
16 
17 static int meson_efuse_read(void *context, unsigned int offset,
18 			    void *val, size_t bytes)
19 {
20 	return meson_sm_call_read((u8 *)val, bytes, SM_EFUSE_READ, offset,
21 				  bytes, 0, 0, 0);
22 }
23 
24 static int meson_efuse_write(void *context, unsigned int offset,
25 			     void *val, size_t bytes)
26 {
27 	return meson_sm_call_write((u8 *)val, bytes, SM_EFUSE_WRITE, offset,
28 				   bytes, 0, 0, 0);
29 }
30 
31 static const struct of_device_id meson_efuse_match[] = {
32 	{ .compatible = "amlogic,meson-gxbb-efuse", },
33 	{ /* sentinel */ },
34 };
35 MODULE_DEVICE_TABLE(of, meson_efuse_match);
36 
37 static int meson_efuse_probe(struct platform_device *pdev)
38 {
39 	struct device *dev = &pdev->dev;
40 	struct nvmem_device *nvmem;
41 	struct nvmem_config *econfig;
42 	struct clk *clk;
43 	unsigned int size;
44 	int ret;
45 
46 	clk = devm_clk_get(dev, NULL);
47 	if (IS_ERR(clk)) {
48 		ret = PTR_ERR(clk);
49 		if (ret != -EPROBE_DEFER)
50 			dev_err(dev, "failed to get efuse gate");
51 		return ret;
52 	}
53 
54 	ret = clk_prepare_enable(clk);
55 	if (ret) {
56 		dev_err(dev, "failed to enable gate");
57 		return ret;
58 	}
59 
60 	ret = devm_add_action_or_reset(dev,
61 				       (void(*)(void *))clk_disable_unprepare,
62 				       clk);
63 	if (ret) {
64 		dev_err(dev, "failed to add disable callback");
65 		return ret;
66 	}
67 
68 	if (meson_sm_call(SM_EFUSE_USER_MAX, &size, 0, 0, 0, 0, 0) < 0) {
69 		dev_err(dev, "failed to get max user");
70 		return -EINVAL;
71 	}
72 
73 	econfig = devm_kzalloc(dev, sizeof(*econfig), GFP_KERNEL);
74 	if (!econfig)
75 		return -ENOMEM;
76 
77 	econfig->dev = dev;
78 	econfig->name = dev_name(dev);
79 	econfig->stride = 1;
80 	econfig->word_size = 1;
81 	econfig->reg_read = meson_efuse_read;
82 	econfig->reg_write = meson_efuse_write;
83 	econfig->size = size;
84 
85 	nvmem = devm_nvmem_register(&pdev->dev, econfig);
86 
87 	return PTR_ERR_OR_ZERO(nvmem);
88 }
89 
90 static struct platform_driver meson_efuse_driver = {
91 	.probe = meson_efuse_probe,
92 	.driver = {
93 		.name = "meson-efuse",
94 		.of_match_table = meson_efuse_match,
95 	},
96 };
97 
98 module_platform_driver(meson_efuse_driver);
99 
100 MODULE_AUTHOR("Carlo Caione <carlo@endlessm.com>");
101 MODULE_DESCRIPTION("Amlogic Meson GX NVMEM driver");
102 MODULE_LICENSE("GPL v2");
103