This source file includes following definitions.
- xway_stp_get
- xway_stp_set
- xway_stp_dir_out
- xway_stp_request
- xway_stp_hw_init
- xway_stp_probe
- xway_stp_init
1
2
3
4
5
6
7 #include <linux/slab.h>
8 #include <linux/init.h>
9 #include <linux/module.h>
10 #include <linux/types.h>
11 #include <linux/of_platform.h>
12 #include <linux/mutex.h>
13 #include <linux/gpio/driver.h>
14 #include <linux/io.h>
15 #include <linux/clk.h>
16 #include <linux/err.h>
17
18
19
20
21
22
23
24
25
26 #define XWAY_STP_CON0 0x00
27
28 #define XWAY_STP_CON1 0x04
29
30 #define XWAY_STP_CPU0 0x08
31
32 #define XWAY_STP_CPU1 0x0C
33
34 #define XWAY_STP_AR 0x10
35
36
37 #define XWAY_STP_CON_SWU BIT(31)
38
39
40 #define XWAY_STP_2HZ 0
41 #define XWAY_STP_4HZ BIT(23)
42 #define XWAY_STP_8HZ BIT(24)
43 #define XWAY_STP_10HZ (BIT(24) | BIT(23))
44 #define XWAY_STP_SPEED_MASK (0xf << 23)
45
46
47 #define XWAY_STP_UPD_FPI BIT(31)
48 #define XWAY_STP_UPD_MASK (BIT(31) | BIT(30))
49
50
51 #define XWAY_STP_ADSL_SHIFT 24
52 #define XWAY_STP_ADSL_MASK 0x3
53
54
55 #define XWAY_STP_PHY_MASK 0x7
56 #define XWAY_STP_PHY1_SHIFT 27
57 #define XWAY_STP_PHY2_SHIFT 15
58
59
60 #define XWAY_STP_GROUP0 BIT(0)
61 #define XWAY_STP_GROUP1 BIT(1)
62 #define XWAY_STP_GROUP2 BIT(2)
63 #define XWAY_STP_GROUP_MASK (0x7)
64
65
66 #define XWAY_STP_FALLING BIT(26)
67 #define XWAY_STP_EDGE_MASK BIT(26)
68
69 #define xway_stp_r32(m, reg) __raw_readl(m + reg)
70 #define xway_stp_w32(m, val, reg) __raw_writel(val, m + reg)
71 #define xway_stp_w32_mask(m, clear, set, reg) \
72 xway_stp_w32(m, (xway_stp_r32(m, reg) & ~(clear)) | (set), reg)
73
74 struct xway_stp {
75 struct gpio_chip gc;
76 void __iomem *virt;
77 u32 edge;
78 u32 shadow;
79 u8 groups;
80 u8 dsl;
81 u8 phy1;
82 u8 phy2;
83 u8 reserved;
84 };
85
86
87
88
89
90
91
92
93 static int xway_stp_get(struct gpio_chip *gc, unsigned int gpio)
94 {
95 struct xway_stp *chip = gpiochip_get_data(gc);
96
97 return (xway_stp_r32(chip->virt, XWAY_STP_CPU0) & BIT(gpio));
98 }
99
100
101
102
103
104
105
106
107
108 static void xway_stp_set(struct gpio_chip *gc, unsigned gpio, int val)
109 {
110 struct xway_stp *chip = gpiochip_get_data(gc);
111
112 if (val)
113 chip->shadow |= BIT(gpio);
114 else
115 chip->shadow &= ~BIT(gpio);
116 xway_stp_w32(chip->virt, chip->shadow, XWAY_STP_CPU0);
117 xway_stp_w32_mask(chip->virt, 0, XWAY_STP_CON_SWU, XWAY_STP_CON0);
118 }
119
120
121
122
123
124
125
126
127
128 static int xway_stp_dir_out(struct gpio_chip *gc, unsigned gpio, int val)
129 {
130 xway_stp_set(gc, gpio, val);
131
132 return 0;
133 }
134
135
136
137
138
139
140
141
142 static int xway_stp_request(struct gpio_chip *gc, unsigned gpio)
143 {
144 struct xway_stp *chip = gpiochip_get_data(gc);
145
146 if ((gpio < 8) && (chip->reserved & BIT(gpio))) {
147 dev_err(gc->parent, "GPIO %d is driven by hardware\n", gpio);
148 return -ENODEV;
149 }
150
151 return 0;
152 }
153
154
155
156
157
158 static void xway_stp_hw_init(struct xway_stp *chip)
159 {
160
161 xway_stp_w32(chip->virt, 0, XWAY_STP_AR);
162 xway_stp_w32(chip->virt, 0, XWAY_STP_CPU0);
163 xway_stp_w32(chip->virt, 0, XWAY_STP_CPU1);
164 xway_stp_w32(chip->virt, XWAY_STP_CON_SWU, XWAY_STP_CON0);
165 xway_stp_w32(chip->virt, 0, XWAY_STP_CON1);
166
167
168 xway_stp_w32_mask(chip->virt, XWAY_STP_EDGE_MASK,
169 chip->edge, XWAY_STP_CON0);
170
171
172 xway_stp_w32_mask(chip->virt, XWAY_STP_GROUP_MASK,
173 chip->groups, XWAY_STP_CON1);
174
175
176 xway_stp_w32_mask(chip->virt,
177 XWAY_STP_ADSL_MASK << XWAY_STP_ADSL_SHIFT,
178 chip->dsl << XWAY_STP_ADSL_SHIFT,
179 XWAY_STP_CON0);
180
181
182 xway_stp_w32_mask(chip->virt,
183 XWAY_STP_PHY_MASK << XWAY_STP_PHY1_SHIFT,
184 chip->phy1 << XWAY_STP_PHY1_SHIFT,
185 XWAY_STP_CON0);
186 xway_stp_w32_mask(chip->virt,
187 XWAY_STP_PHY_MASK << XWAY_STP_PHY2_SHIFT,
188 chip->phy2 << XWAY_STP_PHY2_SHIFT,
189 XWAY_STP_CON1);
190
191
192 chip->reserved = (chip->phy2 << 5) | (chip->phy1 << 2) | chip->dsl;
193
194
195
196
197
198 if (chip->reserved)
199 xway_stp_w32_mask(chip->virt, XWAY_STP_UPD_MASK,
200 XWAY_STP_UPD_FPI, XWAY_STP_CON1);
201 }
202
203 static int xway_stp_probe(struct platform_device *pdev)
204 {
205 u32 shadow, groups, dsl, phy;
206 struct xway_stp *chip;
207 struct clk *clk;
208 int ret = 0;
209
210 chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
211 if (!chip)
212 return -ENOMEM;
213
214 chip->virt = devm_platform_ioremap_resource(pdev, 0);
215 if (IS_ERR(chip->virt))
216 return PTR_ERR(chip->virt);
217
218 chip->gc.parent = &pdev->dev;
219 chip->gc.label = "stp-xway";
220 chip->gc.direction_output = xway_stp_dir_out;
221 chip->gc.get = xway_stp_get;
222 chip->gc.set = xway_stp_set;
223 chip->gc.request = xway_stp_request;
224 chip->gc.base = -1;
225 chip->gc.owner = THIS_MODULE;
226
227
228 if (!of_property_read_u32(pdev->dev.of_node, "lantiq,shadow", &shadow))
229 chip->shadow = shadow;
230
231
232 if (!of_property_read_u32(pdev->dev.of_node, "lantiq,groups", &groups))
233 chip->groups = groups & XWAY_STP_GROUP_MASK;
234 else
235 chip->groups = XWAY_STP_GROUP0;
236 chip->gc.ngpio = fls(chip->groups) * 8;
237
238
239 if (!of_property_read_u32(pdev->dev.of_node, "lantiq,dsl", &dsl))
240 chip->dsl = dsl & XWAY_STP_ADSL_MASK;
241
242
243 if (of_machine_is_compatible("lantiq,ar9") ||
244 of_machine_is_compatible("lantiq,gr9") ||
245 of_machine_is_compatible("lantiq,vr9")) {
246 if (!of_property_read_u32(pdev->dev.of_node, "lantiq,phy1", &phy))
247 chip->phy1 = phy & XWAY_STP_PHY_MASK;
248 if (!of_property_read_u32(pdev->dev.of_node, "lantiq,phy2", &phy))
249 chip->phy2 = phy & XWAY_STP_PHY_MASK;
250 }
251
252
253 if (!of_find_property(pdev->dev.of_node, "lantiq,rising", NULL))
254 chip->edge = XWAY_STP_FALLING;
255
256 clk = devm_clk_get(&pdev->dev, NULL);
257 if (IS_ERR(clk)) {
258 dev_err(&pdev->dev, "Failed to get clock\n");
259 return PTR_ERR(clk);
260 }
261
262 ret = clk_prepare_enable(clk);
263 if (ret)
264 return ret;
265
266 xway_stp_hw_init(chip);
267
268 ret = devm_gpiochip_add_data(&pdev->dev, &chip->gc, chip);
269 if (ret) {
270 clk_disable_unprepare(clk);
271 return ret;
272 }
273
274 dev_info(&pdev->dev, "Init done\n");
275
276 return 0;
277 }
278
279 static const struct of_device_id xway_stp_match[] = {
280 { .compatible = "lantiq,gpio-stp-xway" },
281 {},
282 };
283 MODULE_DEVICE_TABLE(of, xway_stp_match);
284
285 static struct platform_driver xway_stp_driver = {
286 .probe = xway_stp_probe,
287 .driver = {
288 .name = "gpio-stp-xway",
289 .of_match_table = xway_stp_match,
290 },
291 };
292
293 static int __init xway_stp_init(void)
294 {
295 return platform_driver_register(&xway_stp_driver);
296 }
297
298 subsys_initcall(xway_stp_init);