1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) Maxime Coquelin 2015
4 * Copyright (C) STMicroelectronics 2017
5 * Author: Maxime Coquelin <mcoquelin.stm32@gmail.com>
6 *
7 * Heavily based on Mediatek's pinctrl driver
8 */
9 #include <linux/clk.h>
10 #include <linux/gpio/driver.h>
11 #include <linux/io.h>
12 #include <linux/irq.h>
13 #include <linux/mfd/syscon.h>
14 #include <linux/module.h>
15 #include <linux/of.h>
16 #include <linux/of_address.h>
17 #include <linux/of_device.h>
18 #include <linux/of_irq.h>
19 #include <linux/pinctrl/consumer.h>
20 #include <linux/pinctrl/machine.h>
21 #include <linux/pinctrl/pinconf.h>
22 #include <linux/pinctrl/pinconf-generic.h>
23 #include <linux/pinctrl/pinctrl.h>
24 #include <linux/pinctrl/pinmux.h>
25 #include <linux/platform_device.h>
26 #include <linux/regmap.h>
27 #include <linux/reset.h>
28 #include <linux/slab.h>
29
30 #include "../core.h"
31 #include "../pinconf.h"
32 #include "../pinctrl-utils.h"
33 #include "pinctrl-stm32.h"
34
35 #define STM32_GPIO_MODER 0x00
36 #define STM32_GPIO_TYPER 0x04
37 #define STM32_GPIO_SPEEDR 0x08
38 #define STM32_GPIO_PUPDR 0x0c
39 #define STM32_GPIO_IDR 0x10
40 #define STM32_GPIO_ODR 0x14
41 #define STM32_GPIO_BSRR 0x18
42 #define STM32_GPIO_LCKR 0x1c
43 #define STM32_GPIO_AFRL 0x20
44 #define STM32_GPIO_AFRH 0x24
45
46 #define STM32_GPIO_PINS_PER_BANK 16
47 #define STM32_GPIO_IRQ_LINE 16
48
49 #define SYSCFG_IRQMUX_MASK GENMASK(3, 0)
50
51 #define gpio_range_to_bank(chip) \
52 container_of(chip, struct stm32_gpio_bank, range)
53
54 static const char * const stm32_gpio_functions[] = {
55 "gpio", "af0", "af1",
56 "af2", "af3", "af4",
57 "af5", "af6", "af7",
58 "af8", "af9", "af10",
59 "af11", "af12", "af13",
60 "af14", "af15", "analog",
61 };
62
63 struct stm32_pinctrl_group {
64 const char *name;
65 unsigned long config;
66 unsigned pin;
67 };
68
69 struct stm32_gpio_bank {
70 void __iomem *base;
71 struct clk *clk;
72 spinlock_t lock;
73 struct gpio_chip gpio_chip;
74 struct pinctrl_gpio_range range;
75 struct fwnode_handle *fwnode;
76 struct irq_domain *domain;
77 u32 bank_nr;
78 u32 bank_ioport_nr;
79 };
80
81 struct stm32_pinctrl {
82 struct device *dev;
83 struct pinctrl_dev *pctl_dev;
84 struct pinctrl_desc pctl_desc;
85 struct stm32_pinctrl_group *groups;
86 unsigned ngroups;
87 const char **grp_names;
88 struct stm32_gpio_bank *banks;
89 unsigned nbanks;
90 const struct stm32_pinctrl_match_data *match_data;
91 struct irq_domain *domain;
92 struct regmap *regmap;
93 struct regmap_field *irqmux[STM32_GPIO_PINS_PER_BANK];
94 };
95
stm32_gpio_pin(int gpio)96 static inline int stm32_gpio_pin(int gpio)
97 {
98 return gpio % STM32_GPIO_PINS_PER_BANK;
99 }
100
stm32_gpio_get_mode(u32 function)101 static inline u32 stm32_gpio_get_mode(u32 function)
102 {
103 switch (function) {
104 case STM32_PIN_GPIO:
105 return 0;
106 case STM32_PIN_AF(0) ... STM32_PIN_AF(15):
107 return 2;
108 case STM32_PIN_ANALOG:
109 return 3;
110 }
111
112 return 0;
113 }
114
stm32_gpio_get_alt(u32 function)115 static inline u32 stm32_gpio_get_alt(u32 function)
116 {
117 switch (function) {
118 case STM32_PIN_GPIO:
119 return 0;
120 case STM32_PIN_AF(0) ... STM32_PIN_AF(15):
121 return function - 1;
122 case STM32_PIN_ANALOG:
123 return 0;
124 }
125
126 return 0;
127 }
128
129 /* GPIO functions */
130
__stm32_gpio_set(struct stm32_gpio_bank * bank,unsigned offset,int value)131 static inline void __stm32_gpio_set(struct stm32_gpio_bank *bank,
132 unsigned offset, int value)
133 {
134 if (!value)
135 offset += STM32_GPIO_PINS_PER_BANK;
136
137 clk_enable(bank->clk);
138
139 writel_relaxed(BIT(offset), bank->base + STM32_GPIO_BSRR);
140
141 clk_disable(bank->clk);
142 }
143
stm32_gpio_request(struct gpio_chip * chip,unsigned offset)144 static int stm32_gpio_request(struct gpio_chip *chip, unsigned offset)
145 {
146 struct stm32_gpio_bank *bank = gpiochip_get_data(chip);
147 struct stm32_pinctrl *pctl = dev_get_drvdata(bank->gpio_chip.parent);
148 struct pinctrl_gpio_range *range;
149 int pin = offset + (bank->bank_nr * STM32_GPIO_PINS_PER_BANK);
150
151 range = pinctrl_find_gpio_range_from_pin_nolock(pctl->pctl_dev, pin);
152 if (!range) {
153 dev_err(pctl->dev, "pin %d not in range.\n", pin);
154 return -EINVAL;
155 }
156
157 return pinctrl_gpio_request(chip->base + offset);
158 }
159
stm32_gpio_free(struct gpio_chip * chip,unsigned offset)160 static void stm32_gpio_free(struct gpio_chip *chip, unsigned offset)
161 {
162 pinctrl_gpio_free(chip->base + offset);
163 }
164
stm32_gpio_get(struct gpio_chip * chip,unsigned offset)165 static int stm32_gpio_get(struct gpio_chip *chip, unsigned offset)
166 {
167 struct stm32_gpio_bank *bank = gpiochip_get_data(chip);
168 int ret;
169
170 clk_enable(bank->clk);
171
172 ret = !!(readl_relaxed(bank->base + STM32_GPIO_IDR) & BIT(offset));
173
174 clk_disable(bank->clk);
175
176 return ret;
177 }
178
stm32_gpio_set(struct gpio_chip * chip,unsigned offset,int value)179 static void stm32_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
180 {
181 struct stm32_gpio_bank *bank = gpiochip_get_data(chip);
182
183 __stm32_gpio_set(bank, offset, value);
184 }
185
stm32_gpio_direction_input(struct gpio_chip * chip,unsigned offset)186 static int stm32_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
187 {
188 return pinctrl_gpio_direction_input(chip->base + offset);
189 }
190
stm32_gpio_direction_output(struct gpio_chip * chip,unsigned offset,int value)191 static int stm32_gpio_direction_output(struct gpio_chip *chip,
192 unsigned offset, int value)
193 {
194 struct stm32_gpio_bank *bank = gpiochip_get_data(chip);
195
196 __stm32_gpio_set(bank, offset, value);
197 pinctrl_gpio_direction_output(chip->base + offset);
198
199 return 0;
200 }
201
202
stm32_gpio_to_irq(struct gpio_chip * chip,unsigned int offset)203 static int stm32_gpio_to_irq(struct gpio_chip *chip, unsigned int offset)
204 {
205 struct stm32_gpio_bank *bank = gpiochip_get_data(chip);
206 struct irq_fwspec fwspec;
207
208 fwspec.fwnode = bank->fwnode;
209 fwspec.param_count = 2;
210 fwspec.param[0] = offset;
211 fwspec.param[1] = IRQ_TYPE_NONE;
212
213 return irq_create_fwspec_mapping(&fwspec);
214 }
215
stm32_gpio_get_direction(struct gpio_chip * chip,unsigned int offset)216 static int stm32_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
217 {
218 struct stm32_gpio_bank *bank = gpiochip_get_data(chip);
219 int pin = stm32_gpio_pin(offset);
220 int ret;
221 u32 mode, alt;
222
223 stm32_pmx_get_mode(bank, pin, &mode, &alt);
224 if ((alt == 0) && (mode == 0))
225 ret = 1;
226 else if ((alt == 0) && (mode == 1))
227 ret = 0;
228 else
229 ret = -EINVAL;
230
231 return ret;
232 }
233
234 static const struct gpio_chip stm32_gpio_template = {
235 .request = stm32_gpio_request,
236 .free = stm32_gpio_free,
237 .get = stm32_gpio_get,
238 .set = stm32_gpio_set,
239 .direction_input = stm32_gpio_direction_input,
240 .direction_output = stm32_gpio_direction_output,
241 .to_irq = stm32_gpio_to_irq,
242 .get_direction = stm32_gpio_get_direction,
243 };
244
stm32_gpio_irq_request_resources(struct irq_data * irq_data)245 static int stm32_gpio_irq_request_resources(struct irq_data *irq_data)
246 {
247 struct stm32_gpio_bank *bank = irq_data->domain->host_data;
248 struct stm32_pinctrl *pctl = dev_get_drvdata(bank->gpio_chip.parent);
249 int ret;
250
251 ret = stm32_gpio_direction_input(&bank->gpio_chip, irq_data->hwirq);
252 if (ret)
253 return ret;
254
255 ret = gpiochip_lock_as_irq(&bank->gpio_chip, irq_data->hwirq);
256 if (ret) {
257 dev_err(pctl->dev, "unable to lock HW IRQ %lu for IRQ\n",
258 irq_data->hwirq);
259 return ret;
260 }
261
262 return 0;
263 }
264
stm32_gpio_irq_release_resources(struct irq_data * irq_data)265 static void stm32_gpio_irq_release_resources(struct irq_data *irq_data)
266 {
267 struct stm32_gpio_bank *bank = irq_data->domain->host_data;
268
269 gpiochip_unlock_as_irq(&bank->gpio_chip, irq_data->hwirq);
270 }
271
272 static struct irq_chip stm32_gpio_irq_chip = {
273 .name = "stm32gpio",
274 .irq_eoi = irq_chip_eoi_parent,
275 .irq_ack = irq_chip_ack_parent,
276 .irq_mask = irq_chip_mask_parent,
277 .irq_unmask = irq_chip_unmask_parent,
278 .irq_set_type = irq_chip_set_type_parent,
279 .irq_set_wake = irq_chip_set_wake_parent,
280 .irq_request_resources = stm32_gpio_irq_request_resources,
281 .irq_release_resources = stm32_gpio_irq_release_resources,
282 };
283
stm32_gpio_domain_translate(struct irq_domain * d,struct irq_fwspec * fwspec,unsigned long * hwirq,unsigned int * type)284 static int stm32_gpio_domain_translate(struct irq_domain *d,
285 struct irq_fwspec *fwspec,
286 unsigned long *hwirq,
287 unsigned int *type)
288 {
289 if ((fwspec->param_count != 2) ||
290 (fwspec->param[0] >= STM32_GPIO_IRQ_LINE))
291 return -EINVAL;
292
293 *hwirq = fwspec->param[0];
294 *type = fwspec->param[1];
295 return 0;
296 }
297
stm32_gpio_domain_activate(struct irq_domain * d,struct irq_data * irq_data,bool reserve)298 static int stm32_gpio_domain_activate(struct irq_domain *d,
299 struct irq_data *irq_data, bool reserve)
300 {
301 struct stm32_gpio_bank *bank = d->host_data;
302 struct stm32_pinctrl *pctl = dev_get_drvdata(bank->gpio_chip.parent);
303
304 regmap_field_write(pctl->irqmux[irq_data->hwirq], bank->bank_ioport_nr);
305 return 0;
306 }
307
stm32_gpio_domain_alloc(struct irq_domain * d,unsigned int virq,unsigned int nr_irqs,void * data)308 static int stm32_gpio_domain_alloc(struct irq_domain *d,
309 unsigned int virq,
310 unsigned int nr_irqs, void *data)
311 {
312 struct stm32_gpio_bank *bank = d->host_data;
313 struct irq_fwspec *fwspec = data;
314 struct irq_fwspec parent_fwspec;
315 irq_hw_number_t hwirq;
316
317 hwirq = fwspec->param[0];
318 parent_fwspec.fwnode = d->parent->fwnode;
319 parent_fwspec.param_count = 2;
320 parent_fwspec.param[0] = fwspec->param[0];
321 parent_fwspec.param[1] = fwspec->param[1];
322
323 irq_domain_set_hwirq_and_chip(d, virq, hwirq, &stm32_gpio_irq_chip,
324 bank);
325
326 return irq_domain_alloc_irqs_parent(d, virq, nr_irqs, &parent_fwspec);
327 }
328
329 static const struct irq_domain_ops stm32_gpio_domain_ops = {
330 .translate = stm32_gpio_domain_translate,
331 .alloc = stm32_gpio_domain_alloc,
332 .free = irq_domain_free_irqs_common,
333 .activate = stm32_gpio_domain_activate,
334 };
335
336 /* Pinctrl functions */
337 static struct stm32_pinctrl_group *
stm32_pctrl_find_group_by_pin(struct stm32_pinctrl * pctl,u32 pin)338 stm32_pctrl_find_group_by_pin(struct stm32_pinctrl *pctl, u32 pin)
339 {
340 int i;
341
342 for (i = 0; i < pctl->ngroups; i++) {
343 struct stm32_pinctrl_group *grp = pctl->groups + i;
344
345 if (grp->pin == pin)
346 return grp;
347 }
348
349 return NULL;
350 }
351
stm32_pctrl_is_function_valid(struct stm32_pinctrl * pctl,u32 pin_num,u32 fnum)352 static bool stm32_pctrl_is_function_valid(struct stm32_pinctrl *pctl,
353 u32 pin_num, u32 fnum)
354 {
355 int i;
356
357 for (i = 0; i < pctl->match_data->npins; i++) {
358 const struct stm32_desc_pin *pin = pctl->match_data->pins + i;
359 const struct stm32_desc_function *func = pin->functions;
360
361 if (pin->pin.number != pin_num)
362 continue;
363
364 while (func && func->name) {
365 if (func->num == fnum)
366 return true;
367 func++;
368 }
369
370 break;
371 }
372
373 return false;
374 }
375
stm32_pctrl_dt_node_to_map_func(struct stm32_pinctrl * pctl,u32 pin,u32 fnum,struct stm32_pinctrl_group * grp,struct pinctrl_map ** map,unsigned * reserved_maps,unsigned * num_maps)376 static int stm32_pctrl_dt_node_to_map_func(struct stm32_pinctrl *pctl,
377 u32 pin, u32 fnum, struct stm32_pinctrl_group *grp,
378 struct pinctrl_map **map, unsigned *reserved_maps,
379 unsigned *num_maps)
380 {
381 if (*num_maps == *reserved_maps)
382 return -ENOSPC;
383
384 (*map)[*num_maps].type = PIN_MAP_TYPE_MUX_GROUP;
385 (*map)[*num_maps].data.mux.group = grp->name;
386
387 if (!stm32_pctrl_is_function_valid(pctl, pin, fnum)) {
388 dev_err(pctl->dev, "invalid function %d on pin %d .\n",
389 fnum, pin);
390 return -EINVAL;
391 }
392
393 (*map)[*num_maps].data.mux.function = stm32_gpio_functions[fnum];
394 (*num_maps)++;
395
396 return 0;
397 }
398
stm32_pctrl_dt_subnode_to_map(struct pinctrl_dev * pctldev,struct device_node * node,struct pinctrl_map ** map,unsigned * reserved_maps,unsigned * num_maps)399 static int stm32_pctrl_dt_subnode_to_map(struct pinctrl_dev *pctldev,
400 struct device_node *node,
401 struct pinctrl_map **map,
402 unsigned *reserved_maps,
403 unsigned *num_maps)
404 {
405 struct stm32_pinctrl *pctl;
406 struct stm32_pinctrl_group *grp;
407 struct property *pins;
408 u32 pinfunc, pin, func;
409 unsigned long *configs;
410 unsigned int num_configs;
411 bool has_config = 0;
412 unsigned reserve = 0;
413 int num_pins, num_funcs, maps_per_pin, i, err = 0;
414
415 pctl = pinctrl_dev_get_drvdata(pctldev);
416
417 pins = of_find_property(node, "pinmux", NULL);
418 if (!pins) {
419 dev_err(pctl->dev, "missing pins property in node %s .\n",
420 node->name);
421 return -EINVAL;
422 }
423
424 err = pinconf_generic_parse_dt_config(node, pctldev, &configs,
425 &num_configs);
426 if (err)
427 return err;
428
429 if (num_configs)
430 has_config = 1;
431
432 num_pins = pins->length / sizeof(u32);
433 num_funcs = num_pins;
434 maps_per_pin = 0;
435 if (num_funcs)
436 maps_per_pin++;
437 if (has_config && num_pins >= 1)
438 maps_per_pin++;
439
440 if (!num_pins || !maps_per_pin) {
441 err = -EINVAL;
442 goto exit;
443 }
444
445 reserve = num_pins * maps_per_pin;
446
447 err = pinctrl_utils_reserve_map(pctldev, map,
448 reserved_maps, num_maps, reserve);
449 if (err)
450 goto exit;
451
452 for (i = 0; i < num_pins; i++) {
453 err = of_property_read_u32_index(node, "pinmux",
454 i, &pinfunc);
455 if (err)
456 goto exit;
457
458 pin = STM32_GET_PIN_NO(pinfunc);
459 func = STM32_GET_PIN_FUNC(pinfunc);
460
461 if (!stm32_pctrl_is_function_valid(pctl, pin, func)) {
462 dev_err(pctl->dev, "invalid function.\n");
463 err = -EINVAL;
464 goto exit;
465 }
466
467 grp = stm32_pctrl_find_group_by_pin(pctl, pin);
468 if (!grp) {
469 dev_err(pctl->dev, "unable to match pin %d to group\n",
470 pin);
471 err = -EINVAL;
472 goto exit;
473 }
474
475 err = stm32_pctrl_dt_node_to_map_func(pctl, pin, func, grp, map,
476 reserved_maps, num_maps);
477 if (err)
478 goto exit;
479
480 if (has_config) {
481 err = pinctrl_utils_add_map_configs(pctldev, map,
482 reserved_maps, num_maps, grp->name,
483 configs, num_configs,
484 PIN_MAP_TYPE_CONFIGS_GROUP);
485 if (err)
486 goto exit;
487 }
488 }
489
490 exit:
491 kfree(configs);
492 return err;
493 }
494
stm32_pctrl_dt_node_to_map(struct pinctrl_dev * pctldev,struct device_node * np_config,struct pinctrl_map ** map,unsigned * num_maps)495 static int stm32_pctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
496 struct device_node *np_config,
497 struct pinctrl_map **map, unsigned *num_maps)
498 {
499 struct device_node *np;
500 unsigned reserved_maps;
501 int ret;
502
503 *map = NULL;
504 *num_maps = 0;
505 reserved_maps = 0;
506
507 for_each_child_of_node(np_config, np) {
508 ret = stm32_pctrl_dt_subnode_to_map(pctldev, np, map,
509 &reserved_maps, num_maps);
510 if (ret < 0) {
511 pinctrl_utils_free_map(pctldev, *map, *num_maps);
512 return ret;
513 }
514 }
515
516 return 0;
517 }
518
stm32_pctrl_get_groups_count(struct pinctrl_dev * pctldev)519 static int stm32_pctrl_get_groups_count(struct pinctrl_dev *pctldev)
520 {
521 struct stm32_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
522
523 return pctl->ngroups;
524 }
525
stm32_pctrl_get_group_name(struct pinctrl_dev * pctldev,unsigned group)526 static const char *stm32_pctrl_get_group_name(struct pinctrl_dev *pctldev,
527 unsigned group)
528 {
529 struct stm32_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
530
531 return pctl->groups[group].name;
532 }
533
stm32_pctrl_get_group_pins(struct pinctrl_dev * pctldev,unsigned group,const unsigned ** pins,unsigned * num_pins)534 static int stm32_pctrl_get_group_pins(struct pinctrl_dev *pctldev,
535 unsigned group,
536 const unsigned **pins,
537 unsigned *num_pins)
538 {
539 struct stm32_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
540
541 *pins = (unsigned *)&pctl->groups[group].pin;
542 *num_pins = 1;
543
544 return 0;
545 }
546
547 static const struct pinctrl_ops stm32_pctrl_ops = {
548 .dt_node_to_map = stm32_pctrl_dt_node_to_map,
549 .dt_free_map = pinctrl_utils_free_map,
550 .get_groups_count = stm32_pctrl_get_groups_count,
551 .get_group_name = stm32_pctrl_get_group_name,
552 .get_group_pins = stm32_pctrl_get_group_pins,
553 };
554
555
556 /* Pinmux functions */
557
stm32_pmx_get_funcs_cnt(struct pinctrl_dev * pctldev)558 static int stm32_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
559 {
560 return ARRAY_SIZE(stm32_gpio_functions);
561 }
562
stm32_pmx_get_func_name(struct pinctrl_dev * pctldev,unsigned selector)563 static const char *stm32_pmx_get_func_name(struct pinctrl_dev *pctldev,
564 unsigned selector)
565 {
566 return stm32_gpio_functions[selector];
567 }
568
stm32_pmx_get_func_groups(struct pinctrl_dev * pctldev,unsigned function,const char * const ** groups,unsigned * const num_groups)569 static int stm32_pmx_get_func_groups(struct pinctrl_dev *pctldev,
570 unsigned function,
571 const char * const **groups,
572 unsigned * const num_groups)
573 {
574 struct stm32_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
575
576 *groups = pctl->grp_names;
577 *num_groups = pctl->ngroups;
578
579 return 0;
580 }
581
stm32_pmx_set_mode(struct stm32_gpio_bank * bank,int pin,u32 mode,u32 alt)582 static void stm32_pmx_set_mode(struct stm32_gpio_bank *bank,
583 int pin, u32 mode, u32 alt)
584 {
585 u32 val;
586 int alt_shift = (pin % 8) * 4;
587 int alt_offset = STM32_GPIO_AFRL + (pin / 8) * 4;
588 unsigned long flags;
589
590 clk_enable(bank->clk);
591 spin_lock_irqsave(&bank->lock, flags);
592
593 val = readl_relaxed(bank->base + alt_offset);
594 val &= ~GENMASK(alt_shift + 3, alt_shift);
595 val |= (alt << alt_shift);
596 writel_relaxed(val, bank->base + alt_offset);
597
598 val = readl_relaxed(bank->base + STM32_GPIO_MODER);
599 val &= ~GENMASK(pin * 2 + 1, pin * 2);
600 val |= mode << (pin * 2);
601 writel_relaxed(val, bank->base + STM32_GPIO_MODER);
602
603 spin_unlock_irqrestore(&bank->lock, flags);
604 clk_disable(bank->clk);
605 }
606
stm32_pmx_get_mode(struct stm32_gpio_bank * bank,int pin,u32 * mode,u32 * alt)607 void stm32_pmx_get_mode(struct stm32_gpio_bank *bank, int pin, u32 *mode,
608 u32 *alt)
609 {
610 u32 val;
611 int alt_shift = (pin % 8) * 4;
612 int alt_offset = STM32_GPIO_AFRL + (pin / 8) * 4;
613 unsigned long flags;
614
615 clk_enable(bank->clk);
616 spin_lock_irqsave(&bank->lock, flags);
617
618 val = readl_relaxed(bank->base + alt_offset);
619 val &= GENMASK(alt_shift + 3, alt_shift);
620 *alt = val >> alt_shift;
621
622 val = readl_relaxed(bank->base + STM32_GPIO_MODER);
623 val &= GENMASK(pin * 2 + 1, pin * 2);
624 *mode = val >> (pin * 2);
625
626 spin_unlock_irqrestore(&bank->lock, flags);
627 clk_disable(bank->clk);
628 }
629
stm32_pmx_set_mux(struct pinctrl_dev * pctldev,unsigned function,unsigned group)630 static int stm32_pmx_set_mux(struct pinctrl_dev *pctldev,
631 unsigned function,
632 unsigned group)
633 {
634 bool ret;
635 struct stm32_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
636 struct stm32_pinctrl_group *g = pctl->groups + group;
637 struct pinctrl_gpio_range *range;
638 struct stm32_gpio_bank *bank;
639 u32 mode, alt;
640 int pin;
641
642 ret = stm32_pctrl_is_function_valid(pctl, g->pin, function);
643 if (!ret) {
644 dev_err(pctl->dev, "invalid function %d on group %d .\n",
645 function, group);
646 return -EINVAL;
647 }
648
649 range = pinctrl_find_gpio_range_from_pin(pctldev, g->pin);
650 if (!range) {
651 dev_err(pctl->dev, "No gpio range defined.\n");
652 return -EINVAL;
653 }
654
655 bank = gpiochip_get_data(range->gc);
656 pin = stm32_gpio_pin(g->pin);
657
658 mode = stm32_gpio_get_mode(function);
659 alt = stm32_gpio_get_alt(function);
660
661 stm32_pmx_set_mode(bank, pin, mode, alt);
662
663 return 0;
664 }
665
stm32_pmx_gpio_set_direction(struct pinctrl_dev * pctldev,struct pinctrl_gpio_range * range,unsigned gpio,bool input)666 static int stm32_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
667 struct pinctrl_gpio_range *range, unsigned gpio,
668 bool input)
669 {
670 struct stm32_gpio_bank *bank = gpiochip_get_data(range->gc);
671 int pin = stm32_gpio_pin(gpio);
672
673 stm32_pmx_set_mode(bank, pin, !input, 0);
674
675 return 0;
676 }
677
678 static const struct pinmux_ops stm32_pmx_ops = {
679 .get_functions_count = stm32_pmx_get_funcs_cnt,
680 .get_function_name = stm32_pmx_get_func_name,
681 .get_function_groups = stm32_pmx_get_func_groups,
682 .set_mux = stm32_pmx_set_mux,
683 .gpio_set_direction = stm32_pmx_gpio_set_direction,
684 .strict = true,
685 };
686
687 /* Pinconf functions */
688
stm32_pconf_set_driving(struct stm32_gpio_bank * bank,unsigned offset,u32 drive)689 static void stm32_pconf_set_driving(struct stm32_gpio_bank *bank,
690 unsigned offset, u32 drive)
691 {
692 unsigned long flags;
693 u32 val;
694
695 clk_enable(bank->clk);
696 spin_lock_irqsave(&bank->lock, flags);
697
698 val = readl_relaxed(bank->base + STM32_GPIO_TYPER);
699 val &= ~BIT(offset);
700 val |= drive << offset;
701 writel_relaxed(val, bank->base + STM32_GPIO_TYPER);
702
703 spin_unlock_irqrestore(&bank->lock, flags);
704 clk_disable(bank->clk);
705 }
706
stm32_pconf_get_driving(struct stm32_gpio_bank * bank,unsigned int offset)707 static u32 stm32_pconf_get_driving(struct stm32_gpio_bank *bank,
708 unsigned int offset)
709 {
710 unsigned long flags;
711 u32 val;
712
713 clk_enable(bank->clk);
714 spin_lock_irqsave(&bank->lock, flags);
715
716 val = readl_relaxed(bank->base + STM32_GPIO_TYPER);
717 val &= BIT(offset);
718
719 spin_unlock_irqrestore(&bank->lock, flags);
720 clk_disable(bank->clk);
721
722 return (val >> offset);
723 }
724
stm32_pconf_set_speed(struct stm32_gpio_bank * bank,unsigned offset,u32 speed)725 static void stm32_pconf_set_speed(struct stm32_gpio_bank *bank,
726 unsigned offset, u32 speed)
727 {
728 unsigned long flags;
729 u32 val;
730
731 clk_enable(bank->clk);
732 spin_lock_irqsave(&bank->lock, flags);
733
734 val = readl_relaxed(bank->base + STM32_GPIO_SPEEDR);
735 val &= ~GENMASK(offset * 2 + 1, offset * 2);
736 val |= speed << (offset * 2);
737 writel_relaxed(val, bank->base + STM32_GPIO_SPEEDR);
738
739 spin_unlock_irqrestore(&bank->lock, flags);
740 clk_disable(bank->clk);
741 }
742
stm32_pconf_get_speed(struct stm32_gpio_bank * bank,unsigned int offset)743 static u32 stm32_pconf_get_speed(struct stm32_gpio_bank *bank,
744 unsigned int offset)
745 {
746 unsigned long flags;
747 u32 val;
748
749 clk_enable(bank->clk);
750 spin_lock_irqsave(&bank->lock, flags);
751
752 val = readl_relaxed(bank->base + STM32_GPIO_SPEEDR);
753 val &= GENMASK(offset * 2 + 1, offset * 2);
754
755 spin_unlock_irqrestore(&bank->lock, flags);
756 clk_disable(bank->clk);
757
758 return (val >> (offset * 2));
759 }
760
stm32_pconf_set_bias(struct stm32_gpio_bank * bank,unsigned offset,u32 bias)761 static void stm32_pconf_set_bias(struct stm32_gpio_bank *bank,
762 unsigned offset, u32 bias)
763 {
764 unsigned long flags;
765 u32 val;
766
767 clk_enable(bank->clk);
768 spin_lock_irqsave(&bank->lock, flags);
769
770 val = readl_relaxed(bank->base + STM32_GPIO_PUPDR);
771 val &= ~GENMASK(offset * 2 + 1, offset * 2);
772 val |= bias << (offset * 2);
773 writel_relaxed(val, bank->base + STM32_GPIO_PUPDR);
774
775 spin_unlock_irqrestore(&bank->lock, flags);
776 clk_disable(bank->clk);
777 }
778
stm32_pconf_get_bias(struct stm32_gpio_bank * bank,unsigned int offset)779 static u32 stm32_pconf_get_bias(struct stm32_gpio_bank *bank,
780 unsigned int offset)
781 {
782 unsigned long flags;
783 u32 val;
784
785 clk_enable(bank->clk);
786 spin_lock_irqsave(&bank->lock, flags);
787
788 val = readl_relaxed(bank->base + STM32_GPIO_PUPDR);
789 val &= GENMASK(offset * 2 + 1, offset * 2);
790
791 spin_unlock_irqrestore(&bank->lock, flags);
792 clk_disable(bank->clk);
793
794 return (val >> (offset * 2));
795 }
796
stm32_pconf_get(struct stm32_gpio_bank * bank,unsigned int offset,bool dir)797 static bool stm32_pconf_get(struct stm32_gpio_bank *bank,
798 unsigned int offset, bool dir)
799 {
800 unsigned long flags;
801 u32 val;
802
803 clk_enable(bank->clk);
804 spin_lock_irqsave(&bank->lock, flags);
805
806 if (dir)
807 val = !!(readl_relaxed(bank->base + STM32_GPIO_IDR) &
808 BIT(offset));
809 else
810 val = !!(readl_relaxed(bank->base + STM32_GPIO_ODR) &
811 BIT(offset));
812
813 spin_unlock_irqrestore(&bank->lock, flags);
814 clk_disable(bank->clk);
815
816 return val;
817 }
818
stm32_pconf_parse_conf(struct pinctrl_dev * pctldev,unsigned int pin,enum pin_config_param param,enum pin_config_param arg)819 static int stm32_pconf_parse_conf(struct pinctrl_dev *pctldev,
820 unsigned int pin, enum pin_config_param param,
821 enum pin_config_param arg)
822 {
823 struct stm32_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
824 struct pinctrl_gpio_range *range;
825 struct stm32_gpio_bank *bank;
826 int offset, ret = 0;
827
828 range = pinctrl_find_gpio_range_from_pin(pctldev, pin);
829 if (!range) {
830 dev_err(pctl->dev, "No gpio range defined.\n");
831 return -EINVAL;
832 }
833
834 bank = gpiochip_get_data(range->gc);
835 offset = stm32_gpio_pin(pin);
836
837 switch (param) {
838 case PIN_CONFIG_DRIVE_PUSH_PULL:
839 stm32_pconf_set_driving(bank, offset, 0);
840 break;
841 case PIN_CONFIG_DRIVE_OPEN_DRAIN:
842 stm32_pconf_set_driving(bank, offset, 1);
843 break;
844 case PIN_CONFIG_SLEW_RATE:
845 stm32_pconf_set_speed(bank, offset, arg);
846 break;
847 case PIN_CONFIG_BIAS_DISABLE:
848 stm32_pconf_set_bias(bank, offset, 0);
849 break;
850 case PIN_CONFIG_BIAS_PULL_UP:
851 stm32_pconf_set_bias(bank, offset, 1);
852 break;
853 case PIN_CONFIG_BIAS_PULL_DOWN:
854 stm32_pconf_set_bias(bank, offset, 2);
855 break;
856 case PIN_CONFIG_OUTPUT:
857 __stm32_gpio_set(bank, offset, arg);
858 ret = stm32_pmx_gpio_set_direction(pctldev, range, pin, false);
859 break;
860 default:
861 ret = -EINVAL;
862 }
863
864 return ret;
865 }
866
stm32_pconf_group_get(struct pinctrl_dev * pctldev,unsigned group,unsigned long * config)867 static int stm32_pconf_group_get(struct pinctrl_dev *pctldev,
868 unsigned group,
869 unsigned long *config)
870 {
871 struct stm32_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
872
873 *config = pctl->groups[group].config;
874
875 return 0;
876 }
877
stm32_pconf_group_set(struct pinctrl_dev * pctldev,unsigned group,unsigned long * configs,unsigned num_configs)878 static int stm32_pconf_group_set(struct pinctrl_dev *pctldev, unsigned group,
879 unsigned long *configs, unsigned num_configs)
880 {
881 struct stm32_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
882 struct stm32_pinctrl_group *g = &pctl->groups[group];
883 int i, ret;
884
885 for (i = 0; i < num_configs; i++) {
886 ret = stm32_pconf_parse_conf(pctldev, g->pin,
887 pinconf_to_config_param(configs[i]),
888 pinconf_to_config_argument(configs[i]));
889 if (ret < 0)
890 return ret;
891
892 g->config = configs[i];
893 }
894
895 return 0;
896 }
897
stm32_pconf_dbg_show(struct pinctrl_dev * pctldev,struct seq_file * s,unsigned int pin)898 static void stm32_pconf_dbg_show(struct pinctrl_dev *pctldev,
899 struct seq_file *s,
900 unsigned int pin)
901 {
902 struct pinctrl_gpio_range *range;
903 struct stm32_gpio_bank *bank;
904 int offset;
905 u32 mode, alt, drive, speed, bias;
906 static const char * const modes[] = {
907 "input", "output", "alternate", "analog" };
908 static const char * const speeds[] = {
909 "low", "medium", "high", "very high" };
910 static const char * const biasing[] = {
911 "floating", "pull up", "pull down", "" };
912 bool val;
913
914 range = pinctrl_find_gpio_range_from_pin_nolock(pctldev, pin);
915 if (!range)
916 return;
917
918 bank = gpiochip_get_data(range->gc);
919 offset = stm32_gpio_pin(pin);
920
921 stm32_pmx_get_mode(bank, offset, &mode, &alt);
922 bias = stm32_pconf_get_bias(bank, offset);
923
924 seq_printf(s, "%s ", modes[mode]);
925
926 switch (mode) {
927 /* input */
928 case 0:
929 val = stm32_pconf_get(bank, offset, true);
930 seq_printf(s, "- %s - %s",
931 val ? "high" : "low",
932 biasing[bias]);
933 break;
934
935 /* output */
936 case 1:
937 drive = stm32_pconf_get_driving(bank, offset);
938 speed = stm32_pconf_get_speed(bank, offset);
939 val = stm32_pconf_get(bank, offset, false);
940 seq_printf(s, "- %s - %s - %s - %s %s",
941 val ? "high" : "low",
942 drive ? "open drain" : "push pull",
943 biasing[bias],
944 speeds[speed], "speed");
945 break;
946
947 /* alternate */
948 case 2:
949 drive = stm32_pconf_get_driving(bank, offset);
950 speed = stm32_pconf_get_speed(bank, offset);
951 seq_printf(s, "%d - %s - %s - %s %s", alt,
952 drive ? "open drain" : "push pull",
953 biasing[bias],
954 speeds[speed], "speed");
955 break;
956
957 /* analog */
958 case 3:
959 break;
960 }
961 }
962
963
964 static const struct pinconf_ops stm32_pconf_ops = {
965 .pin_config_group_get = stm32_pconf_group_get,
966 .pin_config_group_set = stm32_pconf_group_set,
967 .pin_config_dbg_show = stm32_pconf_dbg_show,
968 };
969
stm32_gpiolib_register_bank(struct stm32_pinctrl * pctl,struct device_node * np)970 static int stm32_gpiolib_register_bank(struct stm32_pinctrl *pctl,
971 struct device_node *np)
972 {
973 struct stm32_gpio_bank *bank = &pctl->banks[pctl->nbanks];
974 int bank_ioport_nr;
975 struct pinctrl_gpio_range *range = &bank->range;
976 struct of_phandle_args args;
977 struct device *dev = pctl->dev;
978 struct resource res;
979 struct reset_control *rstc;
980 int npins = STM32_GPIO_PINS_PER_BANK;
981 int bank_nr, err;
982
983 rstc = of_reset_control_get_exclusive(np, NULL);
984 if (!IS_ERR(rstc))
985 reset_control_deassert(rstc);
986
987 if (of_address_to_resource(np, 0, &res))
988 return -ENODEV;
989
990 bank->base = devm_ioremap_resource(dev, &res);
991 if (IS_ERR(bank->base))
992 return PTR_ERR(bank->base);
993
994 bank->clk = of_clk_get_by_name(np, NULL);
995 if (IS_ERR(bank->clk)) {
996 dev_err(dev, "failed to get clk (%ld)\n", PTR_ERR(bank->clk));
997 return PTR_ERR(bank->clk);
998 }
999
1000 err = clk_prepare(bank->clk);
1001 if (err) {
1002 dev_err(dev, "failed to prepare clk (%d)\n", err);
1003 return err;
1004 }
1005
1006 bank->gpio_chip = stm32_gpio_template;
1007
1008 of_property_read_string(np, "st,bank-name", &bank->gpio_chip.label);
1009
1010 if (!of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3, 0, &args)) {
1011 bank_nr = args.args[1] / STM32_GPIO_PINS_PER_BANK;
1012 bank->gpio_chip.base = args.args[1];
1013 } else {
1014 bank_nr = pctl->nbanks;
1015 bank->gpio_chip.base = bank_nr * STM32_GPIO_PINS_PER_BANK;
1016 range->name = bank->gpio_chip.label;
1017 range->id = bank_nr;
1018 range->pin_base = range->id * STM32_GPIO_PINS_PER_BANK;
1019 range->base = range->id * STM32_GPIO_PINS_PER_BANK;
1020 range->npins = npins;
1021 range->gc = &bank->gpio_chip;
1022 pinctrl_add_gpio_range(pctl->pctl_dev,
1023 &pctl->banks[bank_nr].range);
1024 }
1025
1026 if (of_property_read_u32(np, "st,bank-ioport", &bank_ioport_nr))
1027 bank_ioport_nr = bank_nr;
1028
1029 bank->gpio_chip.base = bank_nr * STM32_GPIO_PINS_PER_BANK;
1030
1031 bank->gpio_chip.ngpio = npins;
1032 bank->gpio_chip.of_node = np;
1033 bank->gpio_chip.parent = dev;
1034 bank->bank_nr = bank_nr;
1035 bank->bank_ioport_nr = bank_ioport_nr;
1036 spin_lock_init(&bank->lock);
1037
1038 /* create irq hierarchical domain */
1039 bank->fwnode = of_node_to_fwnode(np);
1040
1041 bank->domain = irq_domain_create_hierarchy(pctl->domain, 0,
1042 STM32_GPIO_IRQ_LINE, bank->fwnode,
1043 &stm32_gpio_domain_ops, bank);
1044
1045 if (!bank->domain)
1046 return -ENODEV;
1047
1048 err = gpiochip_add_data(&bank->gpio_chip, bank);
1049 if (err) {
1050 dev_err(dev, "Failed to add gpiochip(%d)!\n", bank_nr);
1051 return err;
1052 }
1053
1054 dev_info(dev, "%s bank added\n", bank->gpio_chip.label);
1055 return 0;
1056 }
1057
stm32_pctrl_dt_setup_irq(struct platform_device * pdev,struct stm32_pinctrl * pctl)1058 static int stm32_pctrl_dt_setup_irq(struct platform_device *pdev,
1059 struct stm32_pinctrl *pctl)
1060 {
1061 struct device_node *np = pdev->dev.of_node, *parent;
1062 struct device *dev = &pdev->dev;
1063 struct regmap *rm;
1064 int offset, ret, i;
1065 int mask, mask_width;
1066
1067 parent = of_irq_find_parent(np);
1068 if (!parent)
1069 return -ENXIO;
1070
1071 pctl->domain = irq_find_host(parent);
1072 if (!pctl->domain)
1073 return -ENXIO;
1074
1075 pctl->regmap = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
1076 if (IS_ERR(pctl->regmap))
1077 return PTR_ERR(pctl->regmap);
1078
1079 rm = pctl->regmap;
1080
1081 ret = of_property_read_u32_index(np, "st,syscfg", 1, &offset);
1082 if (ret)
1083 return ret;
1084
1085 ret = of_property_read_u32_index(np, "st,syscfg", 2, &mask);
1086 if (ret)
1087 mask = SYSCFG_IRQMUX_MASK;
1088
1089 mask_width = fls(mask);
1090
1091 for (i = 0; i < STM32_GPIO_PINS_PER_BANK; i++) {
1092 struct reg_field mux;
1093
1094 mux.reg = offset + (i / 4) * 4;
1095 mux.lsb = (i % 4) * mask_width;
1096 mux.msb = mux.lsb + mask_width - 1;
1097
1098 dev_dbg(dev, "irqmux%d: reg:%#x, lsb:%d, msb:%d\n",
1099 i, mux.reg, mux.lsb, mux.msb);
1100
1101 pctl->irqmux[i] = devm_regmap_field_alloc(dev, rm, mux);
1102 if (IS_ERR(pctl->irqmux[i]))
1103 return PTR_ERR(pctl->irqmux[i]);
1104 }
1105
1106 return 0;
1107 }
1108
stm32_pctrl_build_state(struct platform_device * pdev)1109 static int stm32_pctrl_build_state(struct platform_device *pdev)
1110 {
1111 struct stm32_pinctrl *pctl = platform_get_drvdata(pdev);
1112 int i;
1113
1114 pctl->ngroups = pctl->match_data->npins;
1115
1116 /* Allocate groups */
1117 pctl->groups = devm_kcalloc(&pdev->dev, pctl->ngroups,
1118 sizeof(*pctl->groups), GFP_KERNEL);
1119 if (!pctl->groups)
1120 return -ENOMEM;
1121
1122 /* We assume that one pin is one group, use pin name as group name. */
1123 pctl->grp_names = devm_kcalloc(&pdev->dev, pctl->ngroups,
1124 sizeof(*pctl->grp_names), GFP_KERNEL);
1125 if (!pctl->grp_names)
1126 return -ENOMEM;
1127
1128 for (i = 0; i < pctl->match_data->npins; i++) {
1129 const struct stm32_desc_pin *pin = pctl->match_data->pins + i;
1130 struct stm32_pinctrl_group *group = pctl->groups + i;
1131
1132 group->name = pin->pin.name;
1133 group->pin = pin->pin.number;
1134
1135 pctl->grp_names[i] = pin->pin.name;
1136 }
1137
1138 return 0;
1139 }
1140
stm32_pctl_probe(struct platform_device * pdev)1141 int stm32_pctl_probe(struct platform_device *pdev)
1142 {
1143 struct device_node *np = pdev->dev.of_node;
1144 struct device_node *child;
1145 const struct of_device_id *match;
1146 struct device *dev = &pdev->dev;
1147 struct stm32_pinctrl *pctl;
1148 struct pinctrl_pin_desc *pins;
1149 int i, ret, banks = 0;
1150
1151 if (!np)
1152 return -EINVAL;
1153
1154 match = of_match_device(dev->driver->of_match_table, dev);
1155 if (!match || !match->data)
1156 return -EINVAL;
1157
1158 if (!of_find_property(np, "pins-are-numbered", NULL)) {
1159 dev_err(dev, "only support pins-are-numbered format\n");
1160 return -EINVAL;
1161 }
1162
1163 pctl = devm_kzalloc(dev, sizeof(*pctl), GFP_KERNEL);
1164 if (!pctl)
1165 return -ENOMEM;
1166
1167 platform_set_drvdata(pdev, pctl);
1168
1169 pctl->dev = dev;
1170 pctl->match_data = match->data;
1171 ret = stm32_pctrl_build_state(pdev);
1172 if (ret) {
1173 dev_err(dev, "build state failed: %d\n", ret);
1174 return -EINVAL;
1175 }
1176
1177 if (of_find_property(np, "interrupt-parent", NULL)) {
1178 ret = stm32_pctrl_dt_setup_irq(pdev, pctl);
1179 if (ret)
1180 return ret;
1181 }
1182
1183 pins = devm_kcalloc(&pdev->dev, pctl->match_data->npins, sizeof(*pins),
1184 GFP_KERNEL);
1185 if (!pins)
1186 return -ENOMEM;
1187
1188 for (i = 0; i < pctl->match_data->npins; i++)
1189 pins[i] = pctl->match_data->pins[i].pin;
1190
1191 pctl->pctl_desc.name = dev_name(&pdev->dev);
1192 pctl->pctl_desc.owner = THIS_MODULE;
1193 pctl->pctl_desc.pins = pins;
1194 pctl->pctl_desc.npins = pctl->match_data->npins;
1195 pctl->pctl_desc.confops = &stm32_pconf_ops;
1196 pctl->pctl_desc.pctlops = &stm32_pctrl_ops;
1197 pctl->pctl_desc.pmxops = &stm32_pmx_ops;
1198 pctl->dev = &pdev->dev;
1199
1200 pctl->pctl_dev = devm_pinctrl_register(&pdev->dev, &pctl->pctl_desc,
1201 pctl);
1202
1203 if (IS_ERR(pctl->pctl_dev)) {
1204 dev_err(&pdev->dev, "Failed pinctrl registration\n");
1205 return PTR_ERR(pctl->pctl_dev);
1206 }
1207
1208 for_each_available_child_of_node(np, child)
1209 if (of_property_read_bool(child, "gpio-controller"))
1210 banks++;
1211
1212 if (!banks) {
1213 dev_err(dev, "at least one GPIO bank is required\n");
1214 return -EINVAL;
1215 }
1216 pctl->banks = devm_kcalloc(dev, banks, sizeof(*pctl->banks),
1217 GFP_KERNEL);
1218 if (!pctl->banks)
1219 return -ENOMEM;
1220
1221 for_each_available_child_of_node(np, child) {
1222 if (of_property_read_bool(child, "gpio-controller")) {
1223 ret = stm32_gpiolib_register_bank(pctl, child);
1224 if (ret)
1225 return ret;
1226
1227 pctl->nbanks++;
1228 }
1229 }
1230
1231 dev_info(dev, "Pinctrl STM32 initialized\n");
1232
1233 return 0;
1234 }
1235
1236