blob: 375f3ea3b80c45a71e3b73ae5698cd3e36a5f211 [file] [log] [blame]
Alexandre Bellonice8dc092018-01-06 01:09:26 +01001// SPDX-License-Identifier: (GPL-2.0 OR MIT)
2/*
3 * Microsemi SoCs pinctrl driver
4 *
5 * Author: <alexandre.belloni@free-electrons.com>
6 * License: Dual MIT/GPL
7 * Copyright (c) 2017 Microsemi Corporation
8 */
9
10#include <linux/gpio/driver.h>
11#include <linux/interrupt.h>
12#include <linux/io.h>
13#include <linux/of_device.h>
Quentin Schulzbe36abb2018-07-25 14:26:21 +020014#include <linux/of_irq.h>
Alexandre Bellonice8dc092018-01-06 01:09:26 +010015#include <linux/of_platform.h>
16#include <linux/pinctrl/pinctrl.h>
17#include <linux/pinctrl/pinmux.h>
18#include <linux/pinctrl/pinconf.h>
19#include <linux/pinctrl/pinconf-generic.h>
20#include <linux/platform_device.h>
21#include <linux/regmap.h>
22#include <linux/slab.h>
23
24#include "core.h"
25#include "pinconf.h"
26#include "pinmux.h"
27
28#define OCELOT_GPIO_OUT_SET 0x0
29#define OCELOT_GPIO_OUT_CLR 0x4
30#define OCELOT_GPIO_OUT 0x8
31#define OCELOT_GPIO_IN 0xc
32#define OCELOT_GPIO_OE 0x10
33#define OCELOT_GPIO_INTR 0x14
34#define OCELOT_GPIO_INTR_ENA 0x18
35#define OCELOT_GPIO_INTR_IDENT 0x1c
36#define OCELOT_GPIO_ALT0 0x20
37#define OCELOT_GPIO_ALT1 0x24
38#define OCELOT_GPIO_SD_MAP 0x28
39
Alexandre Bellonice8dc092018-01-06 01:09:26 +010040#define OCELOT_FUNC_PER_PIN 4
41
42enum {
43 FUNC_NONE,
44 FUNC_GPIO,
45 FUNC_IRQ0_IN,
46 FUNC_IRQ0_OUT,
47 FUNC_IRQ1_IN,
48 FUNC_IRQ1_OUT,
Lars Povlsenedc72542020-05-13 14:55:20 +020049 FUNC_MIIM,
Alexandre Bellonice8dc092018-01-06 01:09:26 +010050 FUNC_PCI_WAKE,
51 FUNC_PTP0,
52 FUNC_PTP1,
53 FUNC_PTP2,
54 FUNC_PTP3,
55 FUNC_PWM,
Lars Povlsenedc72542020-05-13 14:55:20 +020056 FUNC_RECO_CLK,
57 FUNC_SFP,
Alexandre Bellonice8dc092018-01-06 01:09:26 +010058 FUNC_SG0,
Alexandre Bellonida801ab2018-12-20 15:44:31 +010059 FUNC_SG1,
60 FUNC_SG2,
Alexandre Bellonice8dc092018-01-06 01:09:26 +010061 FUNC_SI,
62 FUNC_TACHO,
63 FUNC_TWI,
Alexandre Bellonida801ab2018-12-20 15:44:31 +010064 FUNC_TWI2,
Alexandre Bellonice8dc092018-01-06 01:09:26 +010065 FUNC_TWI_SCL_M,
66 FUNC_UART,
67 FUNC_UART2,
68 FUNC_MAX
69};
70
71static const char *const ocelot_function_names[] = {
72 [FUNC_NONE] = "none",
73 [FUNC_GPIO] = "gpio",
74 [FUNC_IRQ0_IN] = "irq0_in",
75 [FUNC_IRQ0_OUT] = "irq0_out",
76 [FUNC_IRQ1_IN] = "irq1_in",
77 [FUNC_IRQ1_OUT] = "irq1_out",
Lars Povlsenedc72542020-05-13 14:55:20 +020078 [FUNC_MIIM] = "miim",
Alexandre Bellonice8dc092018-01-06 01:09:26 +010079 [FUNC_PCI_WAKE] = "pci_wake",
80 [FUNC_PTP0] = "ptp0",
81 [FUNC_PTP1] = "ptp1",
82 [FUNC_PTP2] = "ptp2",
83 [FUNC_PTP3] = "ptp3",
84 [FUNC_PWM] = "pwm",
Lars Povlsenedc72542020-05-13 14:55:20 +020085 [FUNC_RECO_CLK] = "reco_clk",
86 [FUNC_SFP] = "sfp",
Alexandre Bellonice8dc092018-01-06 01:09:26 +010087 [FUNC_SG0] = "sg0",
Alexandre Bellonida801ab2018-12-20 15:44:31 +010088 [FUNC_SG1] = "sg1",
89 [FUNC_SG2] = "sg2",
Alexandre Bellonice8dc092018-01-06 01:09:26 +010090 [FUNC_SI] = "si",
91 [FUNC_TACHO] = "tacho",
92 [FUNC_TWI] = "twi",
Alexandre Bellonida801ab2018-12-20 15:44:31 +010093 [FUNC_TWI2] = "twi2",
Alexandre Bellonice8dc092018-01-06 01:09:26 +010094 [FUNC_TWI_SCL_M] = "twi_scl_m",
95 [FUNC_UART] = "uart",
96 [FUNC_UART2] = "uart2",
97};
98
99struct ocelot_pmx_func {
100 const char **groups;
101 unsigned int ngroups;
102};
103
104struct ocelot_pin_caps {
105 unsigned int pin;
106 unsigned char functions[OCELOT_FUNC_PER_PIN];
107};
108
109struct ocelot_pinctrl {
110 struct device *dev;
111 struct pinctrl_dev *pctl;
112 struct gpio_chip gpio_chip;
113 struct regmap *map;
Alexandre Bellonida801ab2018-12-20 15:44:31 +0100114 struct pinctrl_desc *desc;
Alexandre Bellonice8dc092018-01-06 01:09:26 +0100115 struct ocelot_pmx_func func[FUNC_MAX];
Alexandre Bellonida801ab2018-12-20 15:44:31 +0100116 u8 stride;
Alexandre Bellonice8dc092018-01-06 01:09:26 +0100117};
118
119#define OCELOT_P(p, f0, f1, f2) \
120static struct ocelot_pin_caps ocelot_pin_##p = { \
121 .pin = p, \
122 .functions = { \
123 FUNC_GPIO, FUNC_##f0, FUNC_##f1, FUNC_##f2, \
124 }, \
125}
126
127OCELOT_P(0, SG0, NONE, NONE);
128OCELOT_P(1, SG0, NONE, NONE);
129OCELOT_P(2, SG0, NONE, NONE);
130OCELOT_P(3, SG0, NONE, NONE);
Alexandre Belloni17f79082018-07-11 15:01:26 +0200131OCELOT_P(4, IRQ0_IN, IRQ0_OUT, TWI_SCL_M);
Alexandre Bellonice8dc092018-01-06 01:09:26 +0100132OCELOT_P(5, IRQ1_IN, IRQ1_OUT, PCI_WAKE);
133OCELOT_P(6, UART, TWI_SCL_M, NONE);
134OCELOT_P(7, UART, TWI_SCL_M, NONE);
135OCELOT_P(8, SI, TWI_SCL_M, IRQ0_OUT);
136OCELOT_P(9, SI, TWI_SCL_M, IRQ1_OUT);
Lars Povlsenedc72542020-05-13 14:55:20 +0200137OCELOT_P(10, PTP2, TWI_SCL_M, SFP);
138OCELOT_P(11, PTP3, TWI_SCL_M, SFP);
139OCELOT_P(12, UART2, TWI_SCL_M, SFP);
140OCELOT_P(13, UART2, TWI_SCL_M, SFP);
141OCELOT_P(14, MIIM, TWI_SCL_M, SFP);
142OCELOT_P(15, MIIM, TWI_SCL_M, SFP);
Alexandre Bellonice8dc092018-01-06 01:09:26 +0100143OCELOT_P(16, TWI, NONE, SI);
144OCELOT_P(17, TWI, TWI_SCL_M, SI);
145OCELOT_P(18, PTP0, TWI_SCL_M, NONE);
146OCELOT_P(19, PTP1, TWI_SCL_M, NONE);
Lars Povlsenedc72542020-05-13 14:55:20 +0200147OCELOT_P(20, RECO_CLK, TACHO, TWI_SCL_M);
148OCELOT_P(21, RECO_CLK, PWM, TWI_SCL_M);
Alexandre Bellonice8dc092018-01-06 01:09:26 +0100149
150#define OCELOT_PIN(n) { \
151 .number = n, \
152 .name = "GPIO_"#n, \
153 .drv_data = &ocelot_pin_##n \
154}
155
156static const struct pinctrl_pin_desc ocelot_pins[] = {
157 OCELOT_PIN(0),
158 OCELOT_PIN(1),
159 OCELOT_PIN(2),
160 OCELOT_PIN(3),
161 OCELOT_PIN(4),
162 OCELOT_PIN(5),
163 OCELOT_PIN(6),
164 OCELOT_PIN(7),
165 OCELOT_PIN(8),
166 OCELOT_PIN(9),
167 OCELOT_PIN(10),
168 OCELOT_PIN(11),
169 OCELOT_PIN(12),
170 OCELOT_PIN(13),
171 OCELOT_PIN(14),
172 OCELOT_PIN(15),
173 OCELOT_PIN(16),
174 OCELOT_PIN(17),
175 OCELOT_PIN(18),
176 OCELOT_PIN(19),
177 OCELOT_PIN(20),
178 OCELOT_PIN(21),
179};
180
Alexandre Bellonida801ab2018-12-20 15:44:31 +0100181#define JAGUAR2_P(p, f0, f1) \
182static struct ocelot_pin_caps jaguar2_pin_##p = { \
183 .pin = p, \
184 .functions = { \
185 FUNC_GPIO, FUNC_##f0, FUNC_##f1, FUNC_NONE \
186 }, \
187}
188
189JAGUAR2_P(0, SG0, NONE);
190JAGUAR2_P(1, SG0, NONE);
191JAGUAR2_P(2, SG0, NONE);
192JAGUAR2_P(3, SG0, NONE);
193JAGUAR2_P(4, SG1, NONE);
194JAGUAR2_P(5, SG1, NONE);
195JAGUAR2_P(6, IRQ0_IN, IRQ0_OUT);
196JAGUAR2_P(7, IRQ1_IN, IRQ1_OUT);
197JAGUAR2_P(8, PTP0, NONE);
198JAGUAR2_P(9, PTP1, NONE);
199JAGUAR2_P(10, UART, NONE);
200JAGUAR2_P(11, UART, NONE);
201JAGUAR2_P(12, SG1, NONE);
202JAGUAR2_P(13, SG1, NONE);
203JAGUAR2_P(14, TWI, TWI_SCL_M);
204JAGUAR2_P(15, TWI, NONE);
205JAGUAR2_P(16, SI, TWI_SCL_M);
206JAGUAR2_P(17, SI, TWI_SCL_M);
207JAGUAR2_P(18, SI, TWI_SCL_M);
208JAGUAR2_P(19, PCI_WAKE, NONE);
209JAGUAR2_P(20, IRQ0_OUT, TWI_SCL_M);
210JAGUAR2_P(21, IRQ1_OUT, TWI_SCL_M);
211JAGUAR2_P(22, TACHO, NONE);
212JAGUAR2_P(23, PWM, NONE);
213JAGUAR2_P(24, UART2, NONE);
214JAGUAR2_P(25, UART2, SI);
215JAGUAR2_P(26, PTP2, SI);
216JAGUAR2_P(27, PTP3, SI);
217JAGUAR2_P(28, TWI2, SI);
218JAGUAR2_P(29, TWI2, SI);
219JAGUAR2_P(30, SG2, SI);
220JAGUAR2_P(31, SG2, SI);
221JAGUAR2_P(32, SG2, SI);
222JAGUAR2_P(33, SG2, SI);
223JAGUAR2_P(34, NONE, TWI_SCL_M);
224JAGUAR2_P(35, NONE, TWI_SCL_M);
225JAGUAR2_P(36, NONE, TWI_SCL_M);
226JAGUAR2_P(37, NONE, TWI_SCL_M);
227JAGUAR2_P(38, NONE, TWI_SCL_M);
228JAGUAR2_P(39, NONE, TWI_SCL_M);
229JAGUAR2_P(40, NONE, TWI_SCL_M);
230JAGUAR2_P(41, NONE, TWI_SCL_M);
231JAGUAR2_P(42, NONE, TWI_SCL_M);
232JAGUAR2_P(43, NONE, TWI_SCL_M);
Lars Povlsenedc72542020-05-13 14:55:20 +0200233JAGUAR2_P(44, NONE, SFP);
234JAGUAR2_P(45, NONE, SFP);
235JAGUAR2_P(46, NONE, SFP);
236JAGUAR2_P(47, NONE, SFP);
237JAGUAR2_P(48, SFP, NONE);
238JAGUAR2_P(49, SFP, SI);
239JAGUAR2_P(50, SFP, SI);
240JAGUAR2_P(51, SFP, SI);
241JAGUAR2_P(52, SFP, NONE);
242JAGUAR2_P(53, SFP, NONE);
243JAGUAR2_P(54, SFP, NONE);
244JAGUAR2_P(55, SFP, NONE);
245JAGUAR2_P(56, MIIM, SFP);
246JAGUAR2_P(57, MIIM, SFP);
247JAGUAR2_P(58, MIIM, SFP);
248JAGUAR2_P(59, MIIM, SFP);
Alexandre Bellonida801ab2018-12-20 15:44:31 +0100249JAGUAR2_P(60, NONE, NONE);
250JAGUAR2_P(61, NONE, NONE);
251JAGUAR2_P(62, NONE, NONE);
252JAGUAR2_P(63, NONE, NONE);
253
254#define JAGUAR2_PIN(n) { \
255 .number = n, \
256 .name = "GPIO_"#n, \
257 .drv_data = &jaguar2_pin_##n \
258}
259
260static const struct pinctrl_pin_desc jaguar2_pins[] = {
261 JAGUAR2_PIN(0),
262 JAGUAR2_PIN(1),
263 JAGUAR2_PIN(2),
264 JAGUAR2_PIN(3),
265 JAGUAR2_PIN(4),
266 JAGUAR2_PIN(5),
267 JAGUAR2_PIN(6),
268 JAGUAR2_PIN(7),
269 JAGUAR2_PIN(8),
270 JAGUAR2_PIN(9),
271 JAGUAR2_PIN(10),
272 JAGUAR2_PIN(11),
273 JAGUAR2_PIN(12),
274 JAGUAR2_PIN(13),
275 JAGUAR2_PIN(14),
276 JAGUAR2_PIN(15),
277 JAGUAR2_PIN(16),
278 JAGUAR2_PIN(17),
279 JAGUAR2_PIN(18),
280 JAGUAR2_PIN(19),
281 JAGUAR2_PIN(20),
282 JAGUAR2_PIN(21),
283 JAGUAR2_PIN(22),
284 JAGUAR2_PIN(23),
285 JAGUAR2_PIN(24),
286 JAGUAR2_PIN(25),
287 JAGUAR2_PIN(26),
288 JAGUAR2_PIN(27),
289 JAGUAR2_PIN(28),
290 JAGUAR2_PIN(29),
291 JAGUAR2_PIN(30),
292 JAGUAR2_PIN(31),
293 JAGUAR2_PIN(32),
294 JAGUAR2_PIN(33),
295 JAGUAR2_PIN(34),
296 JAGUAR2_PIN(35),
297 JAGUAR2_PIN(36),
298 JAGUAR2_PIN(37),
299 JAGUAR2_PIN(38),
300 JAGUAR2_PIN(39),
301 JAGUAR2_PIN(40),
302 JAGUAR2_PIN(41),
303 JAGUAR2_PIN(42),
304 JAGUAR2_PIN(43),
305 JAGUAR2_PIN(44),
306 JAGUAR2_PIN(45),
307 JAGUAR2_PIN(46),
308 JAGUAR2_PIN(47),
309 JAGUAR2_PIN(48),
310 JAGUAR2_PIN(49),
311 JAGUAR2_PIN(50),
312 JAGUAR2_PIN(51),
313 JAGUAR2_PIN(52),
314 JAGUAR2_PIN(53),
315 JAGUAR2_PIN(54),
316 JAGUAR2_PIN(55),
317 JAGUAR2_PIN(56),
318 JAGUAR2_PIN(57),
319 JAGUAR2_PIN(58),
320 JAGUAR2_PIN(59),
321 JAGUAR2_PIN(60),
322 JAGUAR2_PIN(61),
323 JAGUAR2_PIN(62),
324 JAGUAR2_PIN(63),
325};
326
Alexandre Bellonice8dc092018-01-06 01:09:26 +0100327static int ocelot_get_functions_count(struct pinctrl_dev *pctldev)
328{
329 return ARRAY_SIZE(ocelot_function_names);
330}
331
332static const char *ocelot_get_function_name(struct pinctrl_dev *pctldev,
333 unsigned int function)
334{
335 return ocelot_function_names[function];
336}
337
338static int ocelot_get_function_groups(struct pinctrl_dev *pctldev,
339 unsigned int function,
340 const char *const **groups,
341 unsigned *const num_groups)
342{
343 struct ocelot_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
344
345 *groups = info->func[function].groups;
346 *num_groups = info->func[function].ngroups;
347
348 return 0;
349}
350
Alexandre Bellonida801ab2018-12-20 15:44:31 +0100351static int ocelot_pin_function_idx(struct ocelot_pinctrl *info,
352 unsigned int pin, unsigned int function)
Alexandre Bellonice8dc092018-01-06 01:09:26 +0100353{
Alexandre Bellonida801ab2018-12-20 15:44:31 +0100354 struct ocelot_pin_caps *p = info->desc->pins[pin].drv_data;
Alexandre Bellonice8dc092018-01-06 01:09:26 +0100355 int i;
356
357 for (i = 0; i < OCELOT_FUNC_PER_PIN; i++) {
358 if (function == p->functions[i])
359 return i;
360 }
361
362 return -1;
363}
364
Alexandre Belloni4b360822019-06-20 20:30:37 +0200365#define REG_ALT(msb, info, p) (OCELOT_GPIO_ALT0 * (info)->stride + 4 * ((msb) + ((info)->stride * ((p) / 32))))
Alexandre Bellonida801ab2018-12-20 15:44:31 +0100366
Alexandre Bellonice8dc092018-01-06 01:09:26 +0100367static int ocelot_pinmux_set_mux(struct pinctrl_dev *pctldev,
368 unsigned int selector, unsigned int group)
369{
370 struct ocelot_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
Alexandre Bellonida801ab2018-12-20 15:44:31 +0100371 struct ocelot_pin_caps *pin = info->desc->pins[group].drv_data;
372 unsigned int p = pin->pin % 32;
Alexandre Bellonice8dc092018-01-06 01:09:26 +0100373 int f;
374
Alexandre Bellonida801ab2018-12-20 15:44:31 +0100375 f = ocelot_pin_function_idx(info, group, selector);
Alexandre Bellonice8dc092018-01-06 01:09:26 +0100376 if (f < 0)
377 return -EINVAL;
378
379 /*
380 * f is encoded on two bits.
Alexandre Belloni4b360822019-06-20 20:30:37 +0200381 * bit 0 of f goes in BIT(pin) of ALT[0], bit 1 of f goes in BIT(pin) of
382 * ALT[1]
Alexandre Bellonice8dc092018-01-06 01:09:26 +0100383 * This is racy because both registers can't be updated at the same time
384 * but it doesn't matter much for now.
385 */
Alexandre Belloni4b360822019-06-20 20:30:37 +0200386 regmap_update_bits(info->map, REG_ALT(0, info, pin->pin),
Alexandre Bellonida801ab2018-12-20 15:44:31 +0100387 BIT(p), f << p);
Alexandre Belloni4b360822019-06-20 20:30:37 +0200388 regmap_update_bits(info->map, REG_ALT(1, info, pin->pin),
Alexandre Bellonida801ab2018-12-20 15:44:31 +0100389 BIT(p), f << (p - 1));
Alexandre Bellonice8dc092018-01-06 01:09:26 +0100390
391 return 0;
392}
393
Alexandre Belloni4b360822019-06-20 20:30:37 +0200394#define REG(r, info, p) ((r) * (info)->stride + (4 * ((p) / 32)))
395
Alexandre Bellonice8dc092018-01-06 01:09:26 +0100396static int ocelot_gpio_set_direction(struct pinctrl_dev *pctldev,
397 struct pinctrl_gpio_range *range,
398 unsigned int pin, bool input)
399{
400 struct ocelot_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
Alexandre Bellonida801ab2018-12-20 15:44:31 +0100401 unsigned int p = pin % 32;
Alexandre Bellonice8dc092018-01-06 01:09:26 +0100402
Alexandre Bellonif2818ba2019-06-20 20:30:36 +0200403 regmap_update_bits(info->map, REG(OCELOT_GPIO_OE, info, pin), BIT(p),
Alexandre Bellonida801ab2018-12-20 15:44:31 +0100404 input ? 0 : BIT(p));
Alexandre Bellonice8dc092018-01-06 01:09:26 +0100405
406 return 0;
407}
408
409static int ocelot_gpio_request_enable(struct pinctrl_dev *pctldev,
410 struct pinctrl_gpio_range *range,
411 unsigned int offset)
412{
413 struct ocelot_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
Alexandre Bellonida801ab2018-12-20 15:44:31 +0100414 unsigned int p = offset % 32;
Alexandre Bellonice8dc092018-01-06 01:09:26 +0100415
Alexandre Belloni4b360822019-06-20 20:30:37 +0200416 regmap_update_bits(info->map, REG_ALT(0, info, offset),
Alexandre Bellonida801ab2018-12-20 15:44:31 +0100417 BIT(p), 0);
Alexandre Belloni4b360822019-06-20 20:30:37 +0200418 regmap_update_bits(info->map, REG_ALT(1, info, offset),
Alexandre Bellonida801ab2018-12-20 15:44:31 +0100419 BIT(p), 0);
Alexandre Bellonice8dc092018-01-06 01:09:26 +0100420
421 return 0;
422}
423
424static const struct pinmux_ops ocelot_pmx_ops = {
425 .get_functions_count = ocelot_get_functions_count,
426 .get_function_name = ocelot_get_function_name,
427 .get_function_groups = ocelot_get_function_groups,
428 .set_mux = ocelot_pinmux_set_mux,
429 .gpio_set_direction = ocelot_gpio_set_direction,
430 .gpio_request_enable = ocelot_gpio_request_enable,
431};
432
433static int ocelot_pctl_get_groups_count(struct pinctrl_dev *pctldev)
434{
Alexandre Bellonida801ab2018-12-20 15:44:31 +0100435 struct ocelot_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
436
437 return info->desc->npins;
Alexandre Bellonice8dc092018-01-06 01:09:26 +0100438}
439
440static const char *ocelot_pctl_get_group_name(struct pinctrl_dev *pctldev,
441 unsigned int group)
442{
Alexandre Bellonida801ab2018-12-20 15:44:31 +0100443 struct ocelot_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
444
445 return info->desc->pins[group].name;
Alexandre Bellonice8dc092018-01-06 01:09:26 +0100446}
447
448static int ocelot_pctl_get_group_pins(struct pinctrl_dev *pctldev,
449 unsigned int group,
450 const unsigned int **pins,
451 unsigned int *num_pins)
452{
Alexandre Bellonida801ab2018-12-20 15:44:31 +0100453 struct ocelot_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
454
455 *pins = &info->desc->pins[group].number;
Alexandre Bellonice8dc092018-01-06 01:09:26 +0100456 *num_pins = 1;
457
458 return 0;
459}
460
461static const struct pinctrl_ops ocelot_pctl_ops = {
462 .get_groups_count = ocelot_pctl_get_groups_count,
463 .get_group_name = ocelot_pctl_get_group_name,
464 .get_group_pins = ocelot_pctl_get_group_pins,
465 .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
466 .dt_free_map = pinconf_generic_dt_free_map,
467};
468
469static struct pinctrl_desc ocelot_desc = {
470 .name = "ocelot-pinctrl",
471 .pins = ocelot_pins,
472 .npins = ARRAY_SIZE(ocelot_pins),
473 .pctlops = &ocelot_pctl_ops,
474 .pmxops = &ocelot_pmx_ops,
475 .owner = THIS_MODULE,
476};
477
Alexandre Bellonida801ab2018-12-20 15:44:31 +0100478static struct pinctrl_desc jaguar2_desc = {
479 .name = "jaguar2-pinctrl",
480 .pins = jaguar2_pins,
481 .npins = ARRAY_SIZE(jaguar2_pins),
482 .pctlops = &ocelot_pctl_ops,
483 .pmxops = &ocelot_pmx_ops,
484 .owner = THIS_MODULE,
485};
486
Alexandre Bellonice8dc092018-01-06 01:09:26 +0100487static int ocelot_create_group_func_map(struct device *dev,
488 struct ocelot_pinctrl *info)
489{
Alexandre Bellonice8dc092018-01-06 01:09:26 +0100490 int f, npins, i;
Alexandre Bellonida801ab2018-12-20 15:44:31 +0100491 u8 *pins = kcalloc(info->desc->npins, sizeof(u8), GFP_KERNEL);
492
493 if (!pins)
494 return -ENOMEM;
Alexandre Bellonice8dc092018-01-06 01:09:26 +0100495
496 for (f = 0; f < FUNC_MAX; f++) {
Alexandre Bellonida801ab2018-12-20 15:44:31 +0100497 for (npins = 0, i = 0; i < info->desc->npins; i++) {
498 if (ocelot_pin_function_idx(info, i, f) >= 0)
Alexandre Bellonice8dc092018-01-06 01:09:26 +0100499 pins[npins++] = i;
500 }
501
Alexandre Bellonida801ab2018-12-20 15:44:31 +0100502 if (!npins)
503 continue;
504
Alexandre Bellonice8dc092018-01-06 01:09:26 +0100505 info->func[f].ngroups = npins;
Alexandre Bellonida801ab2018-12-20 15:44:31 +0100506 info->func[f].groups = devm_kcalloc(dev, npins, sizeof(char *),
507 GFP_KERNEL);
508 if (!info->func[f].groups) {
509 kfree(pins);
Alexandre Bellonice8dc092018-01-06 01:09:26 +0100510 return -ENOMEM;
Alexandre Bellonida801ab2018-12-20 15:44:31 +0100511 }
Alexandre Bellonice8dc092018-01-06 01:09:26 +0100512
513 for (i = 0; i < npins; i++)
Alexandre Bellonida801ab2018-12-20 15:44:31 +0100514 info->func[f].groups[i] = info->desc->pins[pins[i]].name;
Alexandre Bellonice8dc092018-01-06 01:09:26 +0100515 }
516
Alexandre Bellonida801ab2018-12-20 15:44:31 +0100517 kfree(pins);
518
Alexandre Bellonice8dc092018-01-06 01:09:26 +0100519 return 0;
520}
521
522static int ocelot_pinctrl_register(struct platform_device *pdev,
523 struct ocelot_pinctrl *info)
524{
525 int ret;
526
527 ret = ocelot_create_group_func_map(&pdev->dev, info);
528 if (ret) {
529 dev_err(&pdev->dev, "Unable to create group func map.\n");
530 return ret;
531 }
532
Alexandre Bellonida801ab2018-12-20 15:44:31 +0100533 info->pctl = devm_pinctrl_register(&pdev->dev, info->desc, info);
Alexandre Bellonice8dc092018-01-06 01:09:26 +0100534 if (IS_ERR(info->pctl)) {
535 dev_err(&pdev->dev, "Failed to register pinctrl\n");
536 return PTR_ERR(info->pctl);
537 }
538
539 return 0;
540}
541
542static int ocelot_gpio_get(struct gpio_chip *chip, unsigned int offset)
543{
544 struct ocelot_pinctrl *info = gpiochip_get_data(chip);
545 unsigned int val;
546
Alexandre Bellonida801ab2018-12-20 15:44:31 +0100547 regmap_read(info->map, REG(OCELOT_GPIO_IN, info, offset), &val);
Alexandre Bellonice8dc092018-01-06 01:09:26 +0100548
Alexandre Bellonida801ab2018-12-20 15:44:31 +0100549 return !!(val & BIT(offset % 32));
Alexandre Bellonice8dc092018-01-06 01:09:26 +0100550}
551
552static void ocelot_gpio_set(struct gpio_chip *chip, unsigned int offset,
553 int value)
554{
555 struct ocelot_pinctrl *info = gpiochip_get_data(chip);
556
557 if (value)
Alexandre Bellonida801ab2018-12-20 15:44:31 +0100558 regmap_write(info->map, REG(OCELOT_GPIO_OUT_SET, info, offset),
559 BIT(offset % 32));
Alexandre Bellonice8dc092018-01-06 01:09:26 +0100560 else
Alexandre Bellonida801ab2018-12-20 15:44:31 +0100561 regmap_write(info->map, REG(OCELOT_GPIO_OUT_CLR, info, offset),
562 BIT(offset % 32));
Alexandre Bellonice8dc092018-01-06 01:09:26 +0100563}
564
565static int ocelot_gpio_get_direction(struct gpio_chip *chip,
566 unsigned int offset)
567{
568 struct ocelot_pinctrl *info = gpiochip_get_data(chip);
569 unsigned int val;
570
Alexandre Bellonida801ab2018-12-20 15:44:31 +0100571 regmap_read(info->map, REG(OCELOT_GPIO_OE, info, offset), &val);
Alexandre Bellonice8dc092018-01-06 01:09:26 +0100572
Matti Vaittinen3c827872020-02-14 15:57:12 +0200573 if (val & BIT(offset % 32))
574 return GPIO_LINE_DIRECTION_OUT;
575
576 return GPIO_LINE_DIRECTION_IN;
Alexandre Bellonice8dc092018-01-06 01:09:26 +0100577}
578
579static int ocelot_gpio_direction_input(struct gpio_chip *chip,
580 unsigned int offset)
581{
582 return pinctrl_gpio_direction_input(chip->base + offset);
583}
584
585static int ocelot_gpio_direction_output(struct gpio_chip *chip,
586 unsigned int offset, int value)
587{
588 struct ocelot_pinctrl *info = gpiochip_get_data(chip);
Alexandre Bellonida801ab2018-12-20 15:44:31 +0100589 unsigned int pin = BIT(offset % 32);
Alexandre Bellonice8dc092018-01-06 01:09:26 +0100590
591 if (value)
Alexandre Bellonida801ab2018-12-20 15:44:31 +0100592 regmap_write(info->map, REG(OCELOT_GPIO_OUT_SET, info, offset),
593 pin);
Alexandre Bellonice8dc092018-01-06 01:09:26 +0100594 else
Alexandre Bellonida801ab2018-12-20 15:44:31 +0100595 regmap_write(info->map, REG(OCELOT_GPIO_OUT_CLR, info, offset),
596 pin);
Alexandre Bellonice8dc092018-01-06 01:09:26 +0100597
598 return pinctrl_gpio_direction_output(chip->base + offset);
599}
600
601static const struct gpio_chip ocelot_gpiolib_chip = {
602 .request = gpiochip_generic_request,
603 .free = gpiochip_generic_free,
604 .set = ocelot_gpio_set,
605 .get = ocelot_gpio_get,
606 .get_direction = ocelot_gpio_get_direction,
607 .direction_input = ocelot_gpio_direction_input,
608 .direction_output = ocelot_gpio_direction_output,
609 .owner = THIS_MODULE,
610};
611
Quentin Schulzbe36abb2018-07-25 14:26:21 +0200612static void ocelot_irq_mask(struct irq_data *data)
613{
614 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
615 struct ocelot_pinctrl *info = gpiochip_get_data(chip);
616 unsigned int gpio = irqd_to_hwirq(data);
617
Alexandre Bellonida801ab2018-12-20 15:44:31 +0100618 regmap_update_bits(info->map, REG(OCELOT_GPIO_INTR_ENA, info, gpio),
619 BIT(gpio % 32), 0);
Quentin Schulzbe36abb2018-07-25 14:26:21 +0200620}
621
622static void ocelot_irq_unmask(struct irq_data *data)
623{
624 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
625 struct ocelot_pinctrl *info = gpiochip_get_data(chip);
626 unsigned int gpio = irqd_to_hwirq(data);
627
Alexandre Bellonida801ab2018-12-20 15:44:31 +0100628 regmap_update_bits(info->map, REG(OCELOT_GPIO_INTR_ENA, info, gpio),
629 BIT(gpio % 32), BIT(gpio % 32));
Quentin Schulzbe36abb2018-07-25 14:26:21 +0200630}
631
632static void ocelot_irq_ack(struct irq_data *data)
633{
634 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
635 struct ocelot_pinctrl *info = gpiochip_get_data(chip);
636 unsigned int gpio = irqd_to_hwirq(data);
637
Alexandre Bellonida801ab2018-12-20 15:44:31 +0100638 regmap_write_bits(info->map, REG(OCELOT_GPIO_INTR, info, gpio),
639 BIT(gpio % 32), BIT(gpio % 32));
Quentin Schulzbe36abb2018-07-25 14:26:21 +0200640}
641
642static int ocelot_irq_set_type(struct irq_data *data, unsigned int type);
643
644static struct irq_chip ocelot_eoi_irqchip = {
645 .name = "gpio",
646 .irq_mask = ocelot_irq_mask,
647 .irq_eoi = ocelot_irq_ack,
648 .irq_unmask = ocelot_irq_unmask,
649 .flags = IRQCHIP_EOI_THREADED | IRQCHIP_EOI_IF_HANDLED,
650 .irq_set_type = ocelot_irq_set_type,
651};
652
653static struct irq_chip ocelot_irqchip = {
654 .name = "gpio",
655 .irq_mask = ocelot_irq_mask,
656 .irq_ack = ocelot_irq_ack,
657 .irq_unmask = ocelot_irq_unmask,
658 .irq_set_type = ocelot_irq_set_type,
659};
660
661static int ocelot_irq_set_type(struct irq_data *data, unsigned int type)
662{
663 type &= IRQ_TYPE_SENSE_MASK;
664
665 if (!(type & (IRQ_TYPE_EDGE_BOTH | IRQ_TYPE_LEVEL_HIGH)))
666 return -EINVAL;
667
668 if (type & IRQ_TYPE_LEVEL_HIGH)
669 irq_set_chip_handler_name_locked(data, &ocelot_eoi_irqchip,
670 handle_fasteoi_irq, NULL);
671 if (type & IRQ_TYPE_EDGE_BOTH)
672 irq_set_chip_handler_name_locked(data, &ocelot_irqchip,
673 handle_edge_irq, NULL);
674
675 return 0;
676}
677
678static void ocelot_irq_handler(struct irq_desc *desc)
679{
680 struct irq_chip *parent_chip = irq_desc_get_chip(desc);
681 struct gpio_chip *chip = irq_desc_get_handler_data(desc);
682 struct ocelot_pinctrl *info = gpiochip_get_data(chip);
Alexandre Bellonida801ab2018-12-20 15:44:31 +0100683 unsigned int reg = 0, irq, i;
Quentin Schulzbe36abb2018-07-25 14:26:21 +0200684 unsigned long irqs;
685
Alexandre Bellonida801ab2018-12-20 15:44:31 +0100686 for (i = 0; i < info->stride; i++) {
687 regmap_read(info->map, OCELOT_GPIO_INTR_IDENT + 4 * i, &reg);
688 if (!reg)
689 continue;
Quentin Schulzbe36abb2018-07-25 14:26:21 +0200690
Alexandre Bellonida801ab2018-12-20 15:44:31 +0100691 chained_irq_enter(parent_chip, desc);
Quentin Schulzbe36abb2018-07-25 14:26:21 +0200692
Alexandre Bellonida801ab2018-12-20 15:44:31 +0100693 irqs = reg;
Quentin Schulzbe36abb2018-07-25 14:26:21 +0200694
Alexandre Bellonida801ab2018-12-20 15:44:31 +0100695 for_each_set_bit(irq, &irqs,
696 min(32U, info->desc->npins - 32 * i))
697 generic_handle_irq(irq_linear_revmap(chip->irq.domain,
698 irq + 32 * i));
699
700 chained_irq_exit(parent_chip, desc);
Quentin Schulzbe36abb2018-07-25 14:26:21 +0200701 }
Quentin Schulzbe36abb2018-07-25 14:26:21 +0200702}
703
Alexandre Bellonice8dc092018-01-06 01:09:26 +0100704static int ocelot_gpiochip_register(struct platform_device *pdev,
705 struct ocelot_pinctrl *info)
706{
707 struct gpio_chip *gc;
Linus Walleijd874bec2019-10-02 13:44:54 +0200708 struct gpio_irq_chip *girq;
Quentin Schulzbe36abb2018-07-25 14:26:21 +0200709 int ret, irq;
Alexandre Bellonice8dc092018-01-06 01:09:26 +0100710
711 info->gpio_chip = ocelot_gpiolib_chip;
712
713 gc = &info->gpio_chip;
Alexandre Bellonida801ab2018-12-20 15:44:31 +0100714 gc->ngpio = info->desc->npins;
Alexandre Bellonice8dc092018-01-06 01:09:26 +0100715 gc->parent = &pdev->dev;
716 gc->base = 0;
717 gc->of_node = info->dev->of_node;
718 gc->label = "ocelot-gpio";
719
Lars Povlsen550713e2020-05-13 14:55:19 +0200720 irq = irq_of_parse_and_map(gc->of_node, 0);
721 if (irq) {
722 girq = &gc->irq;
723 girq->chip = &ocelot_irqchip;
724 girq->parent_handler = ocelot_irq_handler;
725 girq->num_parents = 1;
726 girq->parents = devm_kcalloc(&pdev->dev, 1,
727 sizeof(*girq->parents),
728 GFP_KERNEL);
729 if (!girq->parents)
730 return -ENOMEM;
731 girq->parents[0] = irq;
732 girq->default_type = IRQ_TYPE_NONE;
733 girq->handler = handle_edge_irq;
734 }
Linus Walleijd874bec2019-10-02 13:44:54 +0200735
736 ret = devm_gpiochip_add_data(&pdev->dev, gc, info);
Quentin Schulzbe36abb2018-07-25 14:26:21 +0200737 if (ret)
738 return ret;
739
Alexandre Bellonice8dc092018-01-06 01:09:26 +0100740 return 0;
741}
742
Alexandre Bellonice8dc092018-01-06 01:09:26 +0100743static const struct of_device_id ocelot_pinctrl_of_match[] = {
Alexandre Bellonida801ab2018-12-20 15:44:31 +0100744 { .compatible = "mscc,ocelot-pinctrl", .data = &ocelot_desc },
745 { .compatible = "mscc,jaguar2-pinctrl", .data = &jaguar2_desc },
Alexandre Bellonice8dc092018-01-06 01:09:26 +0100746 {},
747};
748
Colin Ian Kingce3e7f02018-02-08 14:24:37 +0000749static int ocelot_pinctrl_probe(struct platform_device *pdev)
Alexandre Bellonice8dc092018-01-06 01:09:26 +0100750{
751 struct device *dev = &pdev->dev;
752 struct ocelot_pinctrl *info;
753 void __iomem *base;
754 int ret;
Alexandre Bellonida801ab2018-12-20 15:44:31 +0100755 struct regmap_config regmap_config = {
756 .reg_bits = 32,
757 .val_bits = 32,
758 .reg_stride = 4,
759 };
Alexandre Bellonice8dc092018-01-06 01:09:26 +0100760
761 info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
762 if (!info)
763 return -ENOMEM;
764
Alexandre Bellonida801ab2018-12-20 15:44:31 +0100765 info->desc = (struct pinctrl_desc *)device_get_match_data(dev);
766
Alexandre Bellonice8dc092018-01-06 01:09:26 +0100767 base = devm_ioremap_resource(dev,
768 platform_get_resource(pdev, IORESOURCE_MEM, 0));
769 if (IS_ERR(base)) {
770 dev_err(dev, "Failed to ioremap registers\n");
771 return PTR_ERR(base);
772 }
773
Alexandre Bellonida801ab2018-12-20 15:44:31 +0100774 info->stride = 1 + (info->desc->npins - 1) / 32;
775 regmap_config.max_register = OCELOT_GPIO_SD_MAP * info->stride + 15 * 4;
776
777 info->map = devm_regmap_init_mmio(dev, base, &regmap_config);
Alexandre Bellonice8dc092018-01-06 01:09:26 +0100778 if (IS_ERR(info->map)) {
779 dev_err(dev, "Failed to create regmap\n");
780 return PTR_ERR(info->map);
781 }
782 dev_set_drvdata(dev, info->map);
783 info->dev = dev;
784
785 ret = ocelot_pinctrl_register(pdev, info);
786 if (ret)
787 return ret;
788
789 ret = ocelot_gpiochip_register(pdev, info);
790 if (ret)
791 return ret;
792
793 return 0;
794}
795
796static struct platform_driver ocelot_pinctrl_driver = {
797 .driver = {
798 .name = "pinctrl-ocelot",
799 .of_match_table = of_match_ptr(ocelot_pinctrl_of_match),
800 .suppress_bind_attrs = true,
801 },
802 .probe = ocelot_pinctrl_probe,
803};
804builtin_platform_driver(ocelot_pinctrl_driver);