Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / nvmem / meson-efuse.c
blobd6b533497ce1a5334acd6c647e1d87c15cc579af
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Amlogic Meson GX eFuse Driver
5 * Copyright (c) 2016 Endless Computers, Inc.
6 * Author: Carlo Caione <carlo@endlessm.com>
7 */
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>
15 #include <linux/firmware/meson/meson_sm.h>
17 static int meson_efuse_read(void *context, unsigned int offset,
18 void *val, size_t bytes)
20 struct meson_sm_firmware *fw = context;
22 return meson_sm_call_read(fw, (u8 *)val, bytes, SM_EFUSE_READ, offset,
23 bytes, 0, 0, 0);
26 static int meson_efuse_write(void *context, unsigned int offset,
27 void *val, size_t bytes)
29 struct meson_sm_firmware *fw = context;
31 return meson_sm_call_write(fw, (u8 *)val, bytes, SM_EFUSE_WRITE, offset,
32 bytes, 0, 0, 0);
35 static const struct of_device_id meson_efuse_match[] = {
36 { .compatible = "amlogic,meson-gxbb-efuse", },
37 { /* sentinel */ },
39 MODULE_DEVICE_TABLE(of, meson_efuse_match);
41 static int meson_efuse_probe(struct platform_device *pdev)
43 struct device *dev = &pdev->dev;
44 struct meson_sm_firmware *fw;
45 struct device_node *sm_np;
46 struct nvmem_device *nvmem;
47 struct nvmem_config *econfig;
48 struct clk *clk;
49 unsigned int size;
50 int ret;
52 sm_np = of_parse_phandle(pdev->dev.of_node, "secure-monitor", 0);
53 if (!sm_np) {
54 dev_err(&pdev->dev, "no secure-monitor node\n");
55 return -ENODEV;
58 fw = meson_sm_get(sm_np);
59 of_node_put(sm_np);
60 if (!fw)
61 return -EPROBE_DEFER;
63 clk = devm_clk_get(dev, NULL);
64 if (IS_ERR(clk)) {
65 ret = PTR_ERR(clk);
66 if (ret != -EPROBE_DEFER)
67 dev_err(dev, "failed to get efuse gate");
68 return ret;
71 ret = clk_prepare_enable(clk);
72 if (ret) {
73 dev_err(dev, "failed to enable gate");
74 return ret;
77 ret = devm_add_action_or_reset(dev,
78 (void(*)(void *))clk_disable_unprepare,
79 clk);
80 if (ret) {
81 dev_err(dev, "failed to add disable callback");
82 return ret;
85 if (meson_sm_call(fw, SM_EFUSE_USER_MAX, &size, 0, 0, 0, 0, 0) < 0) {
86 dev_err(dev, "failed to get max user");
87 return -EINVAL;
90 econfig = devm_kzalloc(dev, sizeof(*econfig), GFP_KERNEL);
91 if (!econfig)
92 return -ENOMEM;
94 econfig->dev = dev;
95 econfig->name = dev_name(dev);
96 econfig->stride = 1;
97 econfig->word_size = 1;
98 econfig->reg_read = meson_efuse_read;
99 econfig->reg_write = meson_efuse_write;
100 econfig->size = size;
101 econfig->priv = fw;
103 nvmem = devm_nvmem_register(&pdev->dev, econfig);
105 return PTR_ERR_OR_ZERO(nvmem);
108 static struct platform_driver meson_efuse_driver = {
109 .probe = meson_efuse_probe,
110 .driver = {
111 .name = "meson-efuse",
112 .of_match_table = meson_efuse_match,
116 module_platform_driver(meson_efuse_driver);
118 MODULE_AUTHOR("Carlo Caione <carlo@endlessm.com>");
119 MODULE_DESCRIPTION("Amlogic Meson GX NVMEM driver");
120 MODULE_LICENSE("GPL v2");