sunxi: backport sunxi-mmc controller driver from 4.13
[openwrt/openwrt.git] / target / linux / sunxi / patches-4.4 / 105-phy-use_of_match_node.patch
1 From 5c627d8e7660c170c591ef281184fd11d0493440 Mon Sep 17 00:00:00 2001
2 From: Hans de Goede <hdegoede@redhat.com>
3 Date: Fri, 11 Dec 2015 16:32:17 +0100
4 Subject: [PATCH] phy-sun4i-usb: Use of_match_node to get model specific config
5 data
6
7 Use of_match_node instead of calling of_device_is_compatible a ton of
8 times to get model specific config data.
9
10 Signed-off-by: Hans de Goede <hdegoede@redhat.com>
11 Signed-off-by: Kishon Vijay Abraham I <kishon@ti.com>
12 ---
13 drivers/phy/phy-sun4i-usb.c | 121 +++++++++++++++++++++++++++++---------------
14 1 file changed, 79 insertions(+), 42 deletions(-)
15
16 --- a/drivers/phy/phy-sun4i-usb.c
17 +++ b/drivers/phy/phy-sun4i-usb.c
18 @@ -32,6 +32,7 @@
19 #include <linux/mutex.h>
20 #include <linux/of.h>
21 #include <linux/of_address.h>
22 +#include <linux/of_device.h>
23 #include <linux/of_gpio.h>
24 #include <linux/phy/phy.h>
25 #include <linux/phy/phy-sun4i-usb.h>
26 @@ -88,12 +89,23 @@
27 #define DEBOUNCE_TIME msecs_to_jiffies(50)
28 #define POLL_TIME msecs_to_jiffies(250)
29
30 +enum sun4i_usb_phy_type {
31 + sun4i_a10_phy,
32 + sun8i_a33_phy,
33 +};
34 +
35 +struct sun4i_usb_phy_cfg {
36 + int num_phys;
37 + enum sun4i_usb_phy_type type;
38 + u32 disc_thresh;
39 + u8 phyctl_offset;
40 + bool dedicated_clocks;
41 +};
42 +
43 struct sun4i_usb_phy_data {
44 void __iomem *base;
45 + const struct sun4i_usb_phy_cfg *cfg;
46 struct mutex mutex;
47 - int num_phys;
48 - u32 disc_thresh;
49 - bool has_a33_phyctl;
50 struct sun4i_usb_phy {
51 struct phy *phy;
52 void __iomem *pmu;
53 @@ -159,17 +171,14 @@ static void sun4i_usb_phy_write(struct s
54 {
55 struct sun4i_usb_phy_data *phy_data = to_sun4i_usb_phy_data(phy);
56 u32 temp, usbc_bit = BIT(phy->index * 2);
57 - void *phyctl;
58 + void *phyctl = phy_data->base + phy_data->cfg->phyctl_offset;
59 int i;
60
61 mutex_lock(&phy_data->mutex);
62
63 - if (phy_data->has_a33_phyctl) {
64 - phyctl = phy_data->base + REG_PHYCTL_A33;
65 + if (phy_data->cfg->type == sun8i_a33_phy) {
66 /* A33 needs us to set phyctl to 0 explicitly */
67 writel(0, phyctl);
68 - } else {
69 - phyctl = phy_data->base + REG_PHYCTL_A10;
70 }
71
72 for (i = 0; i < len; i++) {
73 @@ -249,7 +258,8 @@ static int sun4i_usb_phy_init(struct phy
74 sun4i_usb_phy_write(phy, PHY_TX_AMPLITUDE_TUNE, 0x14, 5);
75
76 /* Disconnect threshold adjustment */
77 - sun4i_usb_phy_write(phy, PHY_DISCON_TH_SEL, data->disc_thresh, 2);
78 + sun4i_usb_phy_write(phy, PHY_DISCON_TH_SEL,
79 + data->cfg->disc_thresh, 2);
80
81 sun4i_usb_phy_passby(phy, 1);
82
83 @@ -476,7 +486,7 @@ static struct phy *sun4i_usb_phy_xlate(s
84 {
85 struct sun4i_usb_phy_data *data = dev_get_drvdata(dev);
86
87 - if (args->args[0] >= data->num_phys)
88 + if (args->args[0] >= data->cfg->num_phys)
89 return ERR_PTR(-ENODEV);
90
91 return data->phys[args->args[0]].phy;
92 @@ -511,7 +521,6 @@ static int sun4i_usb_phy_probe(struct pl
93 struct device *dev = &pdev->dev;
94 struct device_node *np = dev->of_node;
95 struct phy_provider *phy_provider;
96 - bool dedicated_clocks;
97 struct resource *res;
98 int i, ret;
99
100 @@ -522,29 +531,9 @@ static int sun4i_usb_phy_probe(struct pl
101 mutex_init(&data->mutex);
102 INIT_DELAYED_WORK(&data->detect, sun4i_usb_phy0_id_vbus_det_scan);
103 dev_set_drvdata(dev, data);
104 -
105 - if (of_device_is_compatible(np, "allwinner,sun5i-a13-usb-phy") ||
106 - of_device_is_compatible(np, "allwinner,sun8i-a23-usb-phy") ||
107 - of_device_is_compatible(np, "allwinner,sun8i-a33-usb-phy"))
108 - data->num_phys = 2;
109 - else
110 - data->num_phys = 3;
111 -
112 - if (of_device_is_compatible(np, "allwinner,sun5i-a13-usb-phy") ||
113 - of_device_is_compatible(np, "allwinner,sun7i-a20-usb-phy"))
114 - data->disc_thresh = 2;
115 - else
116 - data->disc_thresh = 3;
117 -
118 - if (of_device_is_compatible(np, "allwinner,sun6i-a31-usb-phy") ||
119 - of_device_is_compatible(np, "allwinner,sun8i-a23-usb-phy") ||
120 - of_device_is_compatible(np, "allwinner,sun8i-a33-usb-phy"))
121 - dedicated_clocks = true;
122 - else
123 - dedicated_clocks = false;
124 -
125 - if (of_device_is_compatible(np, "allwinner,sun8i-a33-usb-phy"))
126 - data->has_a33_phyctl = true;
127 + data->cfg = of_device_get_match_data(dev);
128 + if (!data->cfg)
129 + return -EINVAL;
130
131 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy_ctrl");
132 data->base = devm_ioremap_resource(dev, res);
133 @@ -590,7 +579,7 @@ static int sun4i_usb_phy_probe(struct pl
134 }
135 }
136
137 - for (i = 0; i < data->num_phys; i++) {
138 + for (i = 0; i < data->cfg->num_phys; i++) {
139 struct sun4i_usb_phy *phy = data->phys + i;
140 char name[16];
141
142 @@ -602,7 +591,7 @@ static int sun4i_usb_phy_probe(struct pl
143 phy->vbus = NULL;
144 }
145
146 - if (dedicated_clocks)
147 + if (data->cfg->dedicated_clocks)
148 snprintf(name, sizeof(name), "usb%d_phy", i);
149 else
150 strlcpy(name, "usb_phy", sizeof(name));
151 @@ -689,13 +678,61 @@ static int sun4i_usb_phy_probe(struct pl
152 return 0;
153 }
154
155 +static const struct sun4i_usb_phy_cfg sun4i_a10_cfg = {
156 + .num_phys = 3,
157 + .type = sun4i_a10_phy,
158 + .disc_thresh = 3,
159 + .phyctl_offset = REG_PHYCTL_A10,
160 + .dedicated_clocks = false,
161 +};
162 +
163 +static const struct sun4i_usb_phy_cfg sun5i_a13_cfg = {
164 + .num_phys = 2,
165 + .type = sun4i_a10_phy,
166 + .disc_thresh = 2,
167 + .phyctl_offset = REG_PHYCTL_A10,
168 + .dedicated_clocks = false,
169 +};
170 +
171 +static const struct sun4i_usb_phy_cfg sun6i_a31_cfg = {
172 + .num_phys = 3,
173 + .type = sun4i_a10_phy,
174 + .disc_thresh = 3,
175 + .phyctl_offset = REG_PHYCTL_A10,
176 + .dedicated_clocks = true,
177 +};
178 +
179 +static const struct sun4i_usb_phy_cfg sun7i_a20_cfg = {
180 + .num_phys = 3,
181 + .type = sun4i_a10_phy,
182 + .disc_thresh = 2,
183 + .phyctl_offset = REG_PHYCTL_A10,
184 + .dedicated_clocks = false,
185 +};
186 +
187 +static const struct sun4i_usb_phy_cfg sun8i_a23_cfg = {
188 + .num_phys = 2,
189 + .type = sun4i_a10_phy,
190 + .disc_thresh = 3,
191 + .phyctl_offset = REG_PHYCTL_A10,
192 + .dedicated_clocks = true,
193 +};
194 +
195 +static const struct sun4i_usb_phy_cfg sun8i_a33_cfg = {
196 + .num_phys = 2,
197 + .type = sun8i_a33_phy,
198 + .disc_thresh = 3,
199 + .phyctl_offset = REG_PHYCTL_A33,
200 + .dedicated_clocks = true,
201 +};
202 +
203 static const struct of_device_id sun4i_usb_phy_of_match[] = {
204 - { .compatible = "allwinner,sun4i-a10-usb-phy" },
205 - { .compatible = "allwinner,sun5i-a13-usb-phy" },
206 - { .compatible = "allwinner,sun6i-a31-usb-phy" },
207 - { .compatible = "allwinner,sun7i-a20-usb-phy" },
208 - { .compatible = "allwinner,sun8i-a23-usb-phy" },
209 - { .compatible = "allwinner,sun8i-a33-usb-phy" },
210 + { .compatible = "allwinner,sun4i-a10-usb-phy", .data = &sun4i_a10_cfg },
211 + { .compatible = "allwinner,sun5i-a13-usb-phy", .data = &sun5i_a13_cfg },
212 + { .compatible = "allwinner,sun6i-a31-usb-phy", .data = &sun6i_a31_cfg },
213 + { .compatible = "allwinner,sun7i-a20-usb-phy", .data = &sun7i_a20_cfg },
214 + { .compatible = "allwinner,sun8i-a23-usb-phy", .data = &sun8i_a23_cfg },
215 + { .compatible = "allwinner,sun8i-a33-usb-phy", .data = &sun8i_a33_cfg },
216 { },
217 };
218 MODULE_DEVICE_TABLE(of, sun4i_usb_phy_of_match);