reset: uniphier-glue: Fix possible null-ptr-deref
[platform/kernel/linux-rpi.git] / drivers / reset / reset-uniphier-glue.c
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // reset-uniphier-glue.c - Glue layer reset driver for UniPhier
4 // Copyright 2018 Socionext Inc.
5 // Author: Kunihiko Hayashi <hayashi.kunihiko@socionext.com>
6
7 #include <linux/clk.h>
8 #include <linux/module.h>
9 #include <linux/of_device.h>
10 #include <linux/platform_device.h>
11 #include <linux/reset.h>
12 #include <linux/reset/reset-simple.h>
13
14 #define MAX_CLKS        2
15 #define MAX_RSTS        2
16
17 struct uniphier_glue_reset_soc_data {
18         int nclks;
19         const char * const *clock_names;
20         int nrsts;
21         const char * const *reset_names;
22 };
23
24 struct uniphier_glue_reset_priv {
25         struct clk_bulk_data clk[MAX_CLKS];
26         struct reset_control_bulk_data rst[MAX_RSTS];
27         struct reset_simple_data rdata;
28         const struct uniphier_glue_reset_soc_data *data;
29 };
30
31 static int uniphier_glue_reset_probe(struct platform_device *pdev)
32 {
33         struct device *dev = &pdev->dev;
34         struct uniphier_glue_reset_priv *priv;
35         struct resource *res;
36         int i, ret;
37
38         priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
39         if (!priv)
40                 return -ENOMEM;
41
42         priv->data = of_device_get_match_data(dev);
43         if (WARN_ON(!priv->data || priv->data->nclks > MAX_CLKS ||
44                     priv->data->nrsts > MAX_RSTS))
45                 return -EINVAL;
46
47         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
48         priv->rdata.membase = devm_ioremap_resource(dev, res);
49         if (IS_ERR(priv->rdata.membase))
50                 return PTR_ERR(priv->rdata.membase);
51
52         for (i = 0; i < priv->data->nclks; i++)
53                 priv->clk[i].id = priv->data->clock_names[i];
54         ret = devm_clk_bulk_get(dev, priv->data->nclks, priv->clk);
55         if (ret)
56                 return ret;
57
58         for (i = 0; i < priv->data->nrsts; i++)
59                 priv->rst[i].id = priv->data->reset_names[i];
60         ret = devm_reset_control_bulk_get_shared(dev, priv->data->nrsts,
61                                                  priv->rst);
62         if (ret)
63                 return ret;
64
65         ret = clk_bulk_prepare_enable(priv->data->nclks, priv->clk);
66         if (ret)
67                 return ret;
68
69         ret = reset_control_bulk_deassert(priv->data->nrsts, priv->rst);
70         if (ret)
71                 goto out_clk_disable;
72
73         spin_lock_init(&priv->rdata.lock);
74         priv->rdata.rcdev.owner = THIS_MODULE;
75         priv->rdata.rcdev.nr_resets = resource_size(res) * BITS_PER_BYTE;
76         priv->rdata.rcdev.ops = &reset_simple_ops;
77         priv->rdata.rcdev.of_node = dev->of_node;
78         priv->rdata.active_low = true;
79
80         platform_set_drvdata(pdev, priv);
81
82         ret = devm_reset_controller_register(dev, &priv->rdata.rcdev);
83         if (ret)
84                 goto out_rst_assert;
85
86         return 0;
87
88 out_rst_assert:
89         reset_control_bulk_assert(priv->data->nrsts, priv->rst);
90
91 out_clk_disable:
92         clk_bulk_disable_unprepare(priv->data->nclks, priv->clk);
93
94         return ret;
95 }
96
97 static int uniphier_glue_reset_remove(struct platform_device *pdev)
98 {
99         struct uniphier_glue_reset_priv *priv = platform_get_drvdata(pdev);
100
101         reset_control_bulk_assert(priv->data->nrsts, priv->rst);
102
103         clk_bulk_disable_unprepare(priv->data->nclks, priv->clk);
104
105         return 0;
106 }
107
108 static const char * const uniphier_pro4_clock_reset_names[] = {
109         "gio", "link",
110 };
111
112 static const struct uniphier_glue_reset_soc_data uniphier_pro4_data = {
113         .nclks = ARRAY_SIZE(uniphier_pro4_clock_reset_names),
114         .clock_names = uniphier_pro4_clock_reset_names,
115         .nrsts = ARRAY_SIZE(uniphier_pro4_clock_reset_names),
116         .reset_names = uniphier_pro4_clock_reset_names,
117 };
118
119 static const char * const uniphier_pxs2_clock_reset_names[] = {
120         "link",
121 };
122
123 static const struct uniphier_glue_reset_soc_data uniphier_pxs2_data = {
124         .nclks = ARRAY_SIZE(uniphier_pxs2_clock_reset_names),
125         .clock_names = uniphier_pxs2_clock_reset_names,
126         .nrsts = ARRAY_SIZE(uniphier_pxs2_clock_reset_names),
127         .reset_names = uniphier_pxs2_clock_reset_names,
128 };
129
130 static const struct of_device_id uniphier_glue_reset_match[] = {
131         {
132                 .compatible = "socionext,uniphier-pro4-usb3-reset",
133                 .data = &uniphier_pro4_data,
134         },
135         {
136                 .compatible = "socionext,uniphier-pro5-usb3-reset",
137                 .data = &uniphier_pro4_data,
138         },
139         {
140                 .compatible = "socionext,uniphier-pxs2-usb3-reset",
141                 .data = &uniphier_pxs2_data,
142         },
143         {
144                 .compatible = "socionext,uniphier-ld20-usb3-reset",
145                 .data = &uniphier_pxs2_data,
146         },
147         {
148                 .compatible = "socionext,uniphier-pxs3-usb3-reset",
149                 .data = &uniphier_pxs2_data,
150         },
151         {
152                 .compatible = "socionext,uniphier-pro4-ahci-reset",
153                 .data = &uniphier_pro4_data,
154         },
155         {
156                 .compatible = "socionext,uniphier-pxs2-ahci-reset",
157                 .data = &uniphier_pxs2_data,
158         },
159         {
160                 .compatible = "socionext,uniphier-pxs3-ahci-reset",
161                 .data = &uniphier_pxs2_data,
162         },
163         { /* Sentinel */ }
164 };
165 MODULE_DEVICE_TABLE(of, uniphier_glue_reset_match);
166
167 static struct platform_driver uniphier_glue_reset_driver = {
168         .probe = uniphier_glue_reset_probe,
169         .remove = uniphier_glue_reset_remove,
170         .driver = {
171                 .name = "uniphier-glue-reset",
172                 .of_match_table = uniphier_glue_reset_match,
173         },
174 };
175 module_platform_driver(uniphier_glue_reset_driver);
176
177 MODULE_AUTHOR("Kunihiko Hayashi <hayashi.kunihiko@socionext.com>");
178 MODULE_DESCRIPTION("UniPhier Glue layer reset driver");
179 MODULE_LICENSE("GPL");