reset: uniphier-glue: Use reset_control_bulk API
[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         resource_size_t size;
37         int i, ret;
38
39         priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
40         if (!priv)
41                 return -ENOMEM;
42
43         priv->data = of_device_get_match_data(dev);
44         if (WARN_ON(!priv->data || priv->data->nclks > MAX_CLKS ||
45                     priv->data->nrsts > MAX_RSTS))
46                 return -EINVAL;
47
48         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
49         size = resource_size(res);
50         priv->rdata.membase = devm_ioremap_resource(dev, res);
51         if (IS_ERR(priv->rdata.membase))
52                 return PTR_ERR(priv->rdata.membase);
53
54         for (i = 0; i < priv->data->nclks; i++)
55                 priv->clk[i].id = priv->data->clock_names[i];
56         ret = devm_clk_bulk_get(dev, priv->data->nclks, priv->clk);
57         if (ret)
58                 return ret;
59
60         for (i = 0; i < priv->data->nrsts; i++)
61                 priv->rst[i].id = priv->data->reset_names[i];
62         ret = devm_reset_control_bulk_get_shared(dev, priv->data->nrsts,
63                                                  priv->rst);
64         if (ret)
65                 return ret;
66
67         ret = clk_bulk_prepare_enable(priv->data->nclks, priv->clk);
68         if (ret)
69                 return ret;
70
71         ret = reset_control_bulk_deassert(priv->data->nrsts, priv->rst);
72         if (ret)
73                 goto out_clk_disable;
74
75         spin_lock_init(&priv->rdata.lock);
76         priv->rdata.rcdev.owner = THIS_MODULE;
77         priv->rdata.rcdev.nr_resets = size * BITS_PER_BYTE;
78         priv->rdata.rcdev.ops = &reset_simple_ops;
79         priv->rdata.rcdev.of_node = dev->of_node;
80         priv->rdata.active_low = true;
81
82         platform_set_drvdata(pdev, priv);
83
84         ret = devm_reset_controller_register(dev, &priv->rdata.rcdev);
85         if (ret)
86                 goto out_rst_assert;
87
88         return 0;
89
90 out_rst_assert:
91         reset_control_bulk_assert(priv->data->nrsts, priv->rst);
92
93 out_clk_disable:
94         clk_bulk_disable_unprepare(priv->data->nclks, priv->clk);
95
96         return ret;
97 }
98
99 static int uniphier_glue_reset_remove(struct platform_device *pdev)
100 {
101         struct uniphier_glue_reset_priv *priv = platform_get_drvdata(pdev);
102
103         reset_control_bulk_assert(priv->data->nrsts, priv->rst);
104
105         clk_bulk_disable_unprepare(priv->data->nclks, priv->clk);
106
107         return 0;
108 }
109
110 static const char * const uniphier_pro4_clock_reset_names[] = {
111         "gio", "link",
112 };
113
114 static const struct uniphier_glue_reset_soc_data uniphier_pro4_data = {
115         .nclks = ARRAY_SIZE(uniphier_pro4_clock_reset_names),
116         .clock_names = uniphier_pro4_clock_reset_names,
117         .nrsts = ARRAY_SIZE(uniphier_pro4_clock_reset_names),
118         .reset_names = uniphier_pro4_clock_reset_names,
119 };
120
121 static const char * const uniphier_pxs2_clock_reset_names[] = {
122         "link",
123 };
124
125 static const struct uniphier_glue_reset_soc_data uniphier_pxs2_data = {
126         .nclks = ARRAY_SIZE(uniphier_pxs2_clock_reset_names),
127         .clock_names = uniphier_pxs2_clock_reset_names,
128         .nrsts = ARRAY_SIZE(uniphier_pxs2_clock_reset_names),
129         .reset_names = uniphier_pxs2_clock_reset_names,
130 };
131
132 static const struct of_device_id uniphier_glue_reset_match[] = {
133         {
134                 .compatible = "socionext,uniphier-pro4-usb3-reset",
135                 .data = &uniphier_pro4_data,
136         },
137         {
138                 .compatible = "socionext,uniphier-pro5-usb3-reset",
139                 .data = &uniphier_pro4_data,
140         },
141         {
142                 .compatible = "socionext,uniphier-pxs2-usb3-reset",
143                 .data = &uniphier_pxs2_data,
144         },
145         {
146                 .compatible = "socionext,uniphier-ld20-usb3-reset",
147                 .data = &uniphier_pxs2_data,
148         },
149         {
150                 .compatible = "socionext,uniphier-pxs3-usb3-reset",
151                 .data = &uniphier_pxs2_data,
152         },
153         {
154                 .compatible = "socionext,uniphier-pro4-ahci-reset",
155                 .data = &uniphier_pro4_data,
156         },
157         {
158                 .compatible = "socionext,uniphier-pxs2-ahci-reset",
159                 .data = &uniphier_pxs2_data,
160         },
161         {
162                 .compatible = "socionext,uniphier-pxs3-ahci-reset",
163                 .data = &uniphier_pxs2_data,
164         },
165         { /* Sentinel */ }
166 };
167 MODULE_DEVICE_TABLE(of, uniphier_glue_reset_match);
168
169 static struct platform_driver uniphier_glue_reset_driver = {
170         .probe = uniphier_glue_reset_probe,
171         .remove = uniphier_glue_reset_remove,
172         .driver = {
173                 .name = "uniphier-glue-reset",
174                 .of_match_table = uniphier_glue_reset_match,
175         },
176 };
177 module_platform_driver(uniphier_glue_reset_driver);
178
179 MODULE_AUTHOR("Kunihiko Hayashi <hayashi.kunihiko@socionext.com>");
180 MODULE_DESCRIPTION("UniPhier Glue layer reset driver");
181 MODULE_LICENSE("GPL");