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