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/bitfield.h>
10 #include <linux/clk.h>
11 #include <linux/export.h>
12 #include <linux/gpio/driver.h>
13 #include <linux/hwspinlock.h>
14 #include <linux/io.h>
15 #include <linux/irq.h>
16 #include <linux/mfd/syscon.h>
17 #include <linux/module.h>
18 #include <linux/of.h>
19 #include <linux/of_address.h>
20 #include <linux/of_irq.h>
21 #include <linux/platform_device.h>
22 #include <linux/property.h>
23 #include <linux/regmap.h>
24 #include <linux/reset.h>
25 #include <linux/seq_file.h>
26 #include <linux/slab.h>
27 #include <linux/string_choices.h>
28
29 #include <linux/pinctrl/consumer.h>
30 #include <linux/pinctrl/machine.h>
31 #include <linux/pinctrl/pinconf-generic.h>
32 #include <linux/pinctrl/pinconf.h>
33 #include <linux/pinctrl/pinctrl.h>
34 #include <linux/pinctrl/pinmux.h>
35
36 #include "../core.h"
37 #include "../pinconf.h"
38 #include "../pinctrl-utils.h"
39 #include "pinctrl-stm32.h"
40
41 #define STM32_GPIO_CID1 1
42
43 #define STM32_GPIO_MODER 0x00
44 #define STM32_GPIO_TYPER 0x04
45 #define STM32_GPIO_SPEEDR 0x08
46 #define STM32_GPIO_PUPDR 0x0c
47 #define STM32_GPIO_IDR 0x10
48 #define STM32_GPIO_ODR 0x14
49 #define STM32_GPIO_BSRR 0x18
50 #define STM32_GPIO_LCKR 0x1c
51 #define STM32_GPIO_AFRL 0x20
52 #define STM32_GPIO_AFRH 0x24
53 #define STM32_GPIO_SECCFGR 0x30
54 #define STM32_GPIO_CIDCFGR(x) (0x50 + (0x8 * (x)))
55 #define STM32_GPIO_SEMCR(x) (0x54 + (0x8 * (x)))
56
57 /* custom bitfield to backup pin status */
58 #define STM32_GPIO_BKP_MODE_SHIFT 0
59 #define STM32_GPIO_BKP_MODE_MASK GENMASK(1, 0)
60 #define STM32_GPIO_BKP_ALT_SHIFT 2
61 #define STM32_GPIO_BKP_ALT_MASK GENMASK(5, 2)
62 #define STM32_GPIO_BKP_SPEED_SHIFT 6
63 #define STM32_GPIO_BKP_SPEED_MASK GENMASK(7, 6)
64 #define STM32_GPIO_BKP_PUPD_SHIFT 8
65 #define STM32_GPIO_BKP_PUPD_MASK GENMASK(9, 8)
66 #define STM32_GPIO_BKP_TYPE 10
67 #define STM32_GPIO_BKP_VAL 11
68
69 #define STM32_GPIO_CIDCFGR_CFEN BIT(0)
70 #define STM32_GPIO_CIDCFGR_SEMEN BIT(1)
71 #define STM32_GPIO_CIDCFGR_SCID_MASK GENMASK(5, 4)
72 #define STM32_GPIO_CIDCFGR_SEMWL_CID1 BIT(16 + STM32_GPIO_CID1)
73
74 #define STM32_GPIO_SEMCR_SEM_MUTEX BIT(0)
75 #define STM32_GPIO_SEMCR_SEMCID_MASK GENMASK(5, 4)
76
77 #define STM32_GPIO_PINS_PER_BANK 16
78 #define STM32_GPIO_IRQ_LINE 16
79
80 #define SYSCFG_IRQMUX_MASK GENMASK(3, 0)
81
82 #define gpio_range_to_bank(chip) \
83 container_of(chip, struct stm32_gpio_bank, range)
84
85 #define HWSPNLCK_TIMEOUT 1000 /* usec */
86
87 static const char * const stm32_gpio_functions[] = {
88 "gpio", "af0", "af1",
89 "af2", "af3", "af4",
90 "af5", "af6", "af7",
91 "af8", "af9", "af10",
92 "af11", "af12", "af13",
93 "af14", "af15", "analog",
94 "reserved",
95 };
96
97 struct stm32_pinctrl_group {
98 const char *name;
99 unsigned long config;
100 unsigned pin;
101 };
102
103 struct stm32_gpio_bank {
104 void __iomem *base;
105 struct reset_control *rstc;
106 spinlock_t lock;
107 struct gpio_chip gpio_chip;
108 struct pinctrl_gpio_range range;
109 struct fwnode_handle *fwnode;
110 struct irq_domain *domain;
111 u32 bank_nr;
112 u32 bank_ioport_nr;
113 u32 pin_backup[STM32_GPIO_PINS_PER_BANK];
114 u8 irq_type[STM32_GPIO_PINS_PER_BANK];
115 bool secure_control;
116 bool rif_control;
117 };
118
119 struct stm32_pinctrl {
120 struct device *dev;
121 struct pinctrl_dev *pctl_dev;
122 struct pinctrl_desc pctl_desc;
123 struct stm32_pinctrl_group *groups;
124 unsigned ngroups;
125 const char **grp_names;
126 struct stm32_gpio_bank *banks;
127 struct clk_bulk_data *clks;
128 unsigned nbanks;
129 const struct stm32_pinctrl_match_data *match_data;
130 struct irq_domain *domain;
131 struct regmap *regmap;
132 struct regmap_field *irqmux[STM32_GPIO_PINS_PER_BANK];
133 struct hwspinlock *hwlock;
134 struct stm32_desc_pin *pins;
135 u32 npins;
136 u32 pkg;
137 u16 irqmux_map;
138 spinlock_t irqmux_lock;
139 };
140
141 static void stm32_pmx_get_mode(struct stm32_gpio_bank *bank, int pin, u32 *mode, u32 *alt);
142
stm32_gpio_pin(int gpio)143 static inline int stm32_gpio_pin(int gpio)
144 {
145 return gpio % STM32_GPIO_PINS_PER_BANK;
146 }
147
stm32_gpio_get_mode(u32 function)148 static inline u32 stm32_gpio_get_mode(u32 function)
149 {
150 switch (function) {
151 case STM32_PIN_GPIO:
152 return 0;
153 case STM32_PIN_AF(0) ... STM32_PIN_AF(15):
154 return 2;
155 case STM32_PIN_ANALOG:
156 return 3;
157 }
158
159 return 0;
160 }
161
stm32_gpio_get_alt(u32 function)162 static inline u32 stm32_gpio_get_alt(u32 function)
163 {
164 switch (function) {
165 case STM32_PIN_GPIO:
166 return 0;
167 case STM32_PIN_AF(0) ... STM32_PIN_AF(15):
168 return function - 1;
169 case STM32_PIN_ANALOG:
170 return 0;
171 }
172
173 return 0;
174 }
175
stm32_gpio_backup_value(struct stm32_gpio_bank * bank,u32 offset,u32 value)176 static void stm32_gpio_backup_value(struct stm32_gpio_bank *bank,
177 u32 offset, u32 value)
178 {
179 bank->pin_backup[offset] &= ~BIT(STM32_GPIO_BKP_VAL);
180 bank->pin_backup[offset] |= value << STM32_GPIO_BKP_VAL;
181 }
182
stm32_gpio_backup_mode(struct stm32_gpio_bank * bank,u32 offset,u32 mode,u32 alt)183 static void stm32_gpio_backup_mode(struct stm32_gpio_bank *bank, u32 offset,
184 u32 mode, u32 alt)
185 {
186 bank->pin_backup[offset] &= ~(STM32_GPIO_BKP_MODE_MASK |
187 STM32_GPIO_BKP_ALT_MASK);
188 bank->pin_backup[offset] |= mode << STM32_GPIO_BKP_MODE_SHIFT;
189 bank->pin_backup[offset] |= alt << STM32_GPIO_BKP_ALT_SHIFT;
190 }
191
stm32_gpio_backup_driving(struct stm32_gpio_bank * bank,u32 offset,u32 drive)192 static void stm32_gpio_backup_driving(struct stm32_gpio_bank *bank, u32 offset,
193 u32 drive)
194 {
195 bank->pin_backup[offset] &= ~BIT(STM32_GPIO_BKP_TYPE);
196 bank->pin_backup[offset] |= drive << STM32_GPIO_BKP_TYPE;
197 }
198
stm32_gpio_backup_speed(struct stm32_gpio_bank * bank,u32 offset,u32 speed)199 static void stm32_gpio_backup_speed(struct stm32_gpio_bank *bank, u32 offset,
200 u32 speed)
201 {
202 bank->pin_backup[offset] &= ~STM32_GPIO_BKP_SPEED_MASK;
203 bank->pin_backup[offset] |= speed << STM32_GPIO_BKP_SPEED_SHIFT;
204 }
205
stm32_gpio_backup_bias(struct stm32_gpio_bank * bank,u32 offset,u32 bias)206 static void stm32_gpio_backup_bias(struct stm32_gpio_bank *bank, u32 offset,
207 u32 bias)
208 {
209 bank->pin_backup[offset] &= ~STM32_GPIO_BKP_PUPD_MASK;
210 bank->pin_backup[offset] |= bias << STM32_GPIO_BKP_PUPD_SHIFT;
211 }
212
213 /* RIF functions */
214
stm32_gpio_rif_valid(struct stm32_gpio_bank * bank,unsigned int gpio_nr)215 static bool stm32_gpio_rif_valid(struct stm32_gpio_bank *bank, unsigned int gpio_nr)
216 {
217 u32 cid;
218
219 cid = readl_relaxed(bank->base + STM32_GPIO_CIDCFGR(gpio_nr));
220
221 if (!(cid & STM32_GPIO_CIDCFGR_CFEN))
222 return true;
223
224 if (!(cid & STM32_GPIO_CIDCFGR_SEMEN)) {
225 if (FIELD_GET(STM32_GPIO_CIDCFGR_SCID_MASK, cid) == STM32_GPIO_CID1)
226 return true;
227
228 return false;
229 }
230
231 if (cid & STM32_GPIO_CIDCFGR_SEMWL_CID1)
232 return true;
233
234 return false;
235 }
236
stm32_gpio_rif_acquire_semaphore(struct stm32_gpio_bank * bank,unsigned int gpio_nr)237 static bool stm32_gpio_rif_acquire_semaphore(struct stm32_gpio_bank *bank, unsigned int gpio_nr)
238 {
239 u32 cid, sem;
240
241 cid = readl_relaxed(bank->base + STM32_GPIO_CIDCFGR(gpio_nr));
242
243 if (!(cid & STM32_GPIO_CIDCFGR_CFEN))
244 return true;
245
246 if (!(cid & STM32_GPIO_CIDCFGR_SEMEN)) {
247 if (FIELD_GET(STM32_GPIO_CIDCFGR_SCID_MASK, cid) == STM32_GPIO_CID1)
248 return true;
249
250 return false;
251 }
252
253 if (!(cid & STM32_GPIO_CIDCFGR_SEMWL_CID1))
254 return false;
255
256 sem = readl_relaxed(bank->base + STM32_GPIO_SEMCR(gpio_nr));
257 if (sem & STM32_GPIO_SEMCR_SEM_MUTEX) {
258 if (FIELD_GET(STM32_GPIO_SEMCR_SEMCID_MASK, sem) == STM32_GPIO_CID1)
259 return true;
260
261 return false;
262 }
263
264 writel_relaxed(STM32_GPIO_SEMCR_SEM_MUTEX, bank->base + STM32_GPIO_SEMCR(gpio_nr));
265
266 sem = readl_relaxed(bank->base + STM32_GPIO_SEMCR(gpio_nr));
267 if (sem & STM32_GPIO_SEMCR_SEM_MUTEX &&
268 FIELD_GET(STM32_GPIO_SEMCR_SEMCID_MASK, sem) == STM32_GPIO_CID1)
269 return true;
270
271 return false;
272 }
273
stm32_gpio_rif_release_semaphore(struct stm32_gpio_bank * bank,unsigned int gpio_nr)274 static void stm32_gpio_rif_release_semaphore(struct stm32_gpio_bank *bank, unsigned int gpio_nr)
275 {
276 u32 cid;
277
278 cid = readl_relaxed(bank->base + STM32_GPIO_CIDCFGR(gpio_nr));
279
280 if (!(cid & STM32_GPIO_CIDCFGR_CFEN))
281 return;
282
283 if (cid & STM32_GPIO_CIDCFGR_SEMEN)
284 writel_relaxed(0, bank->base + STM32_GPIO_SEMCR(gpio_nr));
285 }
286
287 /* GPIO functions */
288
__stm32_gpio_set(struct stm32_gpio_bank * bank,unsigned offset,int value)289 static inline void __stm32_gpio_set(struct stm32_gpio_bank *bank,
290 unsigned offset, int value)
291 {
292 stm32_gpio_backup_value(bank, offset, value);
293
294 if (!value)
295 offset += STM32_GPIO_PINS_PER_BANK;
296
297 writel_relaxed(BIT(offset), bank->base + STM32_GPIO_BSRR);
298 }
299
stm32_gpio_request(struct gpio_chip * chip,unsigned offset)300 static int stm32_gpio_request(struct gpio_chip *chip, unsigned offset)
301 {
302 struct stm32_gpio_bank *bank = gpiochip_get_data(chip);
303 struct stm32_pinctrl *pctl = dev_get_drvdata(bank->gpio_chip.parent);
304 struct pinctrl_gpio_range *range;
305 int pin = offset + (bank->bank_nr * STM32_GPIO_PINS_PER_BANK);
306
307 range = pinctrl_find_gpio_range_from_pin_nolock(pctl->pctl_dev, pin);
308 if (!range) {
309 dev_err(pctl->dev, "pin %d not in range.\n", pin);
310 return -EINVAL;
311 }
312
313 if (bank->rif_control) {
314 if (!stm32_gpio_rif_acquire_semaphore(bank, offset)) {
315 dev_err(pctl->dev, "pin %d not available.\n", pin);
316 return -EINVAL;
317 }
318 }
319
320 return pinctrl_gpio_request(chip, offset);
321 }
322
stm32_gpio_free(struct gpio_chip * chip,unsigned int offset)323 static void stm32_gpio_free(struct gpio_chip *chip, unsigned int offset)
324 {
325 struct stm32_gpio_bank *bank = gpiochip_get_data(chip);
326
327 pinctrl_gpio_free(chip, offset);
328
329 if (bank->rif_control)
330 stm32_gpio_rif_release_semaphore(bank, offset);
331 }
332
stm32_gpio_get(struct gpio_chip * chip,unsigned offset)333 static int stm32_gpio_get(struct gpio_chip *chip, unsigned offset)
334 {
335 struct stm32_gpio_bank *bank = gpiochip_get_data(chip);
336
337 return !!(readl_relaxed(bank->base + STM32_GPIO_IDR) & BIT(offset));
338 }
339
stm32_gpio_set(struct gpio_chip * chip,unsigned int offset,int value)340 static int stm32_gpio_set(struct gpio_chip *chip, unsigned int offset,
341 int value)
342 {
343 struct stm32_gpio_bank *bank = gpiochip_get_data(chip);
344
345 __stm32_gpio_set(bank, offset, value);
346
347 return 0;
348 }
349
stm32_gpio_direction_output(struct gpio_chip * chip,unsigned offset,int value)350 static int stm32_gpio_direction_output(struct gpio_chip *chip,
351 unsigned offset, int value)
352 {
353 struct stm32_gpio_bank *bank = gpiochip_get_data(chip);
354
355 __stm32_gpio_set(bank, offset, value);
356
357 return pinctrl_gpio_direction_output(chip, offset);
358 }
359
360
stm32_gpio_to_irq(struct gpio_chip * chip,unsigned int offset)361 static int stm32_gpio_to_irq(struct gpio_chip *chip, unsigned int offset)
362 {
363 struct stm32_gpio_bank *bank = gpiochip_get_data(chip);
364 struct irq_fwspec fwspec;
365
366 fwspec.fwnode = bank->fwnode;
367 fwspec.param_count = 2;
368 fwspec.param[0] = offset;
369 fwspec.param[1] = IRQ_TYPE_NONE;
370
371 return irq_create_fwspec_mapping(&fwspec);
372 }
373
stm32_gpio_get_direction(struct gpio_chip * chip,unsigned int offset)374 static int stm32_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
375 {
376 struct stm32_gpio_bank *bank = gpiochip_get_data(chip);
377 int pin = stm32_gpio_pin(offset);
378 int ret;
379 u32 mode, alt;
380
381 stm32_pmx_get_mode(bank, pin, &mode, &alt);
382 if ((alt == 0) && (mode == 0))
383 ret = GPIO_LINE_DIRECTION_IN;
384 else if ((alt == 0) && (mode == 1))
385 ret = GPIO_LINE_DIRECTION_OUT;
386 else
387 ret = -EINVAL;
388
389 return ret;
390 }
391
stm32_gpio_init_valid_mask(struct gpio_chip * chip,unsigned long * valid_mask,unsigned int ngpios)392 static int stm32_gpio_init_valid_mask(struct gpio_chip *chip,
393 unsigned long *valid_mask,
394 unsigned int ngpios)
395 {
396 struct stm32_gpio_bank *bank = gpiochip_get_data(chip);
397 struct stm32_pinctrl *pctl = dev_get_drvdata(bank->gpio_chip.parent);
398 unsigned int i;
399 u32 sec;
400
401 /* All gpio are valid per default */
402 bitmap_fill(valid_mask, ngpios);
403
404 if (bank->secure_control) {
405 /* Tag secured pins as invalid */
406 sec = readl_relaxed(bank->base + STM32_GPIO_SECCFGR);
407
408 for (i = 0; i < ngpios; i++) {
409 if (sec & BIT(i)) {
410 clear_bit(i, valid_mask);
411 dev_dbg(pctl->dev, "No access to gpio %d - %d\n", bank->bank_nr, i);
412 }
413 }
414 }
415
416 if (bank->rif_control) {
417 for (i = 0; i < ngpios; i++) {
418 if (!test_bit(i, valid_mask))
419 continue;
420
421 if (stm32_gpio_rif_valid(bank, i))
422 continue;
423
424 dev_dbg(pctl->dev, "RIF semaphore ownership conflict, GPIO %u", i);
425 clear_bit(i, valid_mask);
426 }
427 }
428
429 return 0;
430 }
431
432 static const struct gpio_chip stm32_gpio_template = {
433 .request = stm32_gpio_request,
434 .free = stm32_gpio_free,
435 .get = stm32_gpio_get,
436 .set_rv = stm32_gpio_set,
437 .direction_input = pinctrl_gpio_direction_input,
438 .direction_output = stm32_gpio_direction_output,
439 .to_irq = stm32_gpio_to_irq,
440 .get_direction = stm32_gpio_get_direction,
441 .set_config = gpiochip_generic_config,
442 .init_valid_mask = stm32_gpio_init_valid_mask,
443 };
444
stm32_gpio_irq_trigger(struct irq_data * d)445 static void stm32_gpio_irq_trigger(struct irq_data *d)
446 {
447 struct stm32_gpio_bank *bank = d->domain->host_data;
448 int level;
449
450 /* Do not access the GPIO if this is not LEVEL triggered IRQ. */
451 if (!(bank->irq_type[d->hwirq] & IRQ_TYPE_LEVEL_MASK))
452 return;
453
454 /* If level interrupt type then retrig */
455 level = stm32_gpio_get(&bank->gpio_chip, d->hwirq);
456 if ((level == 0 && bank->irq_type[d->hwirq] == IRQ_TYPE_LEVEL_LOW) ||
457 (level == 1 && bank->irq_type[d->hwirq] == IRQ_TYPE_LEVEL_HIGH))
458 irq_chip_retrigger_hierarchy(d);
459 }
460
stm32_gpio_irq_eoi(struct irq_data * d)461 static void stm32_gpio_irq_eoi(struct irq_data *d)
462 {
463 irq_chip_eoi_parent(d);
464 stm32_gpio_irq_trigger(d);
465 };
466
stm32_gpio_set_type(struct irq_data * d,unsigned int type)467 static int stm32_gpio_set_type(struct irq_data *d, unsigned int type)
468 {
469 struct stm32_gpio_bank *bank = d->domain->host_data;
470 u32 parent_type;
471
472 switch (type) {
473 case IRQ_TYPE_EDGE_RISING:
474 case IRQ_TYPE_EDGE_FALLING:
475 case IRQ_TYPE_EDGE_BOTH:
476 parent_type = type;
477 break;
478 case IRQ_TYPE_LEVEL_HIGH:
479 parent_type = IRQ_TYPE_EDGE_RISING;
480 break;
481 case IRQ_TYPE_LEVEL_LOW:
482 parent_type = IRQ_TYPE_EDGE_FALLING;
483 break;
484 default:
485 return -EINVAL;
486 }
487
488 bank->irq_type[d->hwirq] = type;
489
490 return irq_chip_set_type_parent(d, parent_type);
491 };
492
stm32_gpio_irq_request_resources(struct irq_data * irq_data)493 static int stm32_gpio_irq_request_resources(struct irq_data *irq_data)
494 {
495 struct stm32_gpio_bank *bank = irq_data->domain->host_data;
496 struct stm32_pinctrl *pctl = dev_get_drvdata(bank->gpio_chip.parent);
497 int ret;
498
499 ret = pinctrl_gpio_direction_input(&bank->gpio_chip, irq_data->hwirq);
500 if (ret)
501 return ret;
502
503 ret = gpiochip_lock_as_irq(&bank->gpio_chip, irq_data->hwirq);
504 if (ret) {
505 dev_err(pctl->dev, "unable to lock HW IRQ %lu for IRQ\n",
506 irq_data->hwirq);
507 return ret;
508 }
509
510 return 0;
511 }
512
stm32_gpio_irq_release_resources(struct irq_data * irq_data)513 static void stm32_gpio_irq_release_resources(struct irq_data *irq_data)
514 {
515 struct stm32_gpio_bank *bank = irq_data->domain->host_data;
516
517 gpiochip_unlock_as_irq(&bank->gpio_chip, irq_data->hwirq);
518 }
519
stm32_gpio_irq_unmask(struct irq_data * d)520 static void stm32_gpio_irq_unmask(struct irq_data *d)
521 {
522 irq_chip_unmask_parent(d);
523 stm32_gpio_irq_trigger(d);
524 }
525
526 static struct irq_chip stm32_gpio_irq_chip = {
527 .name = "stm32gpio",
528 .irq_eoi = stm32_gpio_irq_eoi,
529 .irq_ack = irq_chip_ack_parent,
530 .irq_mask = irq_chip_mask_parent,
531 .irq_unmask = stm32_gpio_irq_unmask,
532 .irq_set_type = stm32_gpio_set_type,
533 .irq_set_wake = irq_chip_set_wake_parent,
534 .irq_request_resources = stm32_gpio_irq_request_resources,
535 .irq_release_resources = stm32_gpio_irq_release_resources,
536 .irq_set_affinity = IS_ENABLED(CONFIG_SMP) ? irq_chip_set_affinity_parent : NULL,
537 };
538
stm32_gpio_domain_translate(struct irq_domain * d,struct irq_fwspec * fwspec,unsigned long * hwirq,unsigned int * type)539 static int stm32_gpio_domain_translate(struct irq_domain *d,
540 struct irq_fwspec *fwspec,
541 unsigned long *hwirq,
542 unsigned int *type)
543 {
544 if ((fwspec->param_count != 2) ||
545 (fwspec->param[0] >= STM32_GPIO_IRQ_LINE))
546 return -EINVAL;
547
548 *hwirq = fwspec->param[0];
549 *type = fwspec->param[1];
550 return 0;
551 }
552
stm32_gpio_domain_activate(struct irq_domain * d,struct irq_data * irq_data,bool reserve)553 static int stm32_gpio_domain_activate(struct irq_domain *d,
554 struct irq_data *irq_data, bool reserve)
555 {
556 struct stm32_gpio_bank *bank = d->host_data;
557 struct stm32_pinctrl *pctl = dev_get_drvdata(bank->gpio_chip.parent);
558 int ret = 0;
559
560 if (pctl->hwlock) {
561 ret = hwspin_lock_timeout_in_atomic(pctl->hwlock,
562 HWSPNLCK_TIMEOUT);
563 if (ret) {
564 dev_err(pctl->dev, "Can't get hwspinlock\n");
565 return ret;
566 }
567 }
568
569 regmap_field_write(pctl->irqmux[irq_data->hwirq], bank->bank_ioport_nr);
570
571 if (pctl->hwlock)
572 hwspin_unlock_in_atomic(pctl->hwlock);
573
574 return ret;
575 }
576
stm32_gpio_domain_alloc(struct irq_domain * d,unsigned int virq,unsigned int nr_irqs,void * data)577 static int stm32_gpio_domain_alloc(struct irq_domain *d,
578 unsigned int virq,
579 unsigned int nr_irqs, void *data)
580 {
581 struct stm32_gpio_bank *bank = d->host_data;
582 struct irq_fwspec *fwspec = data;
583 struct irq_fwspec parent_fwspec;
584 struct stm32_pinctrl *pctl = dev_get_drvdata(bank->gpio_chip.parent);
585 irq_hw_number_t hwirq = fwspec->param[0];
586 unsigned long flags;
587 int ret = 0;
588
589 /*
590 * Check first that the IRQ MUX of that line is free.
591 * gpio irq mux is shared between several banks, protect with a lock
592 */
593 spin_lock_irqsave(&pctl->irqmux_lock, flags);
594
595 if (pctl->irqmux_map & BIT(hwirq)) {
596 dev_err(pctl->dev, "irq line %ld already requested.\n", hwirq);
597 ret = -EBUSY;
598 } else {
599 pctl->irqmux_map |= BIT(hwirq);
600 }
601
602 spin_unlock_irqrestore(&pctl->irqmux_lock, flags);
603 if (ret)
604 return ret;
605
606 parent_fwspec.fwnode = d->parent->fwnode;
607 parent_fwspec.param_count = 2;
608 parent_fwspec.param[0] = fwspec->param[0];
609 parent_fwspec.param[1] = fwspec->param[1];
610
611 irq_domain_set_hwirq_and_chip(d, virq, hwirq, &stm32_gpio_irq_chip,
612 bank);
613
614 return irq_domain_alloc_irqs_parent(d, virq, nr_irqs, &parent_fwspec);
615 }
616
stm32_gpio_domain_free(struct irq_domain * d,unsigned int virq,unsigned int nr_irqs)617 static void stm32_gpio_domain_free(struct irq_domain *d, unsigned int virq,
618 unsigned int nr_irqs)
619 {
620 struct stm32_gpio_bank *bank = d->host_data;
621 struct stm32_pinctrl *pctl = dev_get_drvdata(bank->gpio_chip.parent);
622 struct irq_data *irq_data = irq_domain_get_irq_data(d, virq);
623 unsigned long flags, hwirq = irq_data->hwirq;
624
625 irq_domain_free_irqs_common(d, virq, nr_irqs);
626
627 spin_lock_irqsave(&pctl->irqmux_lock, flags);
628 pctl->irqmux_map &= ~BIT(hwirq);
629 spin_unlock_irqrestore(&pctl->irqmux_lock, flags);
630 }
631
632 static const struct irq_domain_ops stm32_gpio_domain_ops = {
633 .translate = stm32_gpio_domain_translate,
634 .alloc = stm32_gpio_domain_alloc,
635 .free = stm32_gpio_domain_free,
636 .activate = stm32_gpio_domain_activate,
637 };
638
639 /* Pinctrl functions */
640 static struct stm32_pinctrl_group *
stm32_pctrl_find_group_by_pin(struct stm32_pinctrl * pctl,u32 pin)641 stm32_pctrl_find_group_by_pin(struct stm32_pinctrl *pctl, u32 pin)
642 {
643 int i;
644
645 for (i = 0; i < pctl->ngroups; i++) {
646 struct stm32_pinctrl_group *grp = pctl->groups + i;
647
648 if (grp->pin == pin)
649 return grp;
650 }
651
652 return NULL;
653 }
654
stm32_pctrl_is_function_valid(struct stm32_pinctrl * pctl,u32 pin_num,u32 fnum)655 static bool stm32_pctrl_is_function_valid(struct stm32_pinctrl *pctl,
656 u32 pin_num, u32 fnum)
657 {
658 int i, k;
659
660 for (i = 0; i < pctl->npins; i++) {
661 const struct stm32_desc_pin *pin = pctl->pins + i;
662 const struct stm32_desc_function *func = pin->functions;
663
664 if (pin->pin.number != pin_num)
665 continue;
666
667 if (fnum == STM32_PIN_RSVD)
668 return true;
669
670 for (k = 0; k < STM32_CONFIG_NUM; k++) {
671 if (func->num == fnum)
672 return true;
673 func++;
674 }
675
676 break;
677 }
678
679 dev_err(pctl->dev, "invalid function %d on pin %d .\n", fnum, pin_num);
680
681 return false;
682 }
683
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)684 static int stm32_pctrl_dt_node_to_map_func(struct stm32_pinctrl *pctl,
685 u32 pin, u32 fnum, struct stm32_pinctrl_group *grp,
686 struct pinctrl_map **map, unsigned *reserved_maps,
687 unsigned *num_maps)
688 {
689 if (*num_maps == *reserved_maps)
690 return -ENOSPC;
691
692 (*map)[*num_maps].type = PIN_MAP_TYPE_MUX_GROUP;
693 (*map)[*num_maps].data.mux.group = grp->name;
694
695 if (!stm32_pctrl_is_function_valid(pctl, pin, fnum))
696 return -EINVAL;
697
698 (*map)[*num_maps].data.mux.function = stm32_gpio_functions[fnum];
699 (*num_maps)++;
700
701 return 0;
702 }
703
stm32_pctrl_dt_subnode_to_map(struct pinctrl_dev * pctldev,struct device_node * node,struct pinctrl_map ** map,unsigned * reserved_maps,unsigned * num_maps)704 static int stm32_pctrl_dt_subnode_to_map(struct pinctrl_dev *pctldev,
705 struct device_node *node,
706 struct pinctrl_map **map,
707 unsigned *reserved_maps,
708 unsigned *num_maps)
709 {
710 struct stm32_pinctrl *pctl;
711 struct stm32_pinctrl_group *grp;
712 struct property *pins;
713 u32 pinfunc, pin, func;
714 unsigned long *configs;
715 unsigned int num_configs;
716 bool has_config = 0;
717 unsigned reserve = 0;
718 int num_pins, num_funcs, maps_per_pin, i, err = 0;
719
720 pctl = pinctrl_dev_get_drvdata(pctldev);
721
722 pins = of_find_property(node, "pinmux", NULL);
723 if (!pins) {
724 dev_err(pctl->dev, "missing pins property in node %pOFn .\n",
725 node);
726 return -EINVAL;
727 }
728
729 err = pinconf_generic_parse_dt_config(node, pctldev, &configs,
730 &num_configs);
731 if (err)
732 return err;
733
734 if (num_configs)
735 has_config = 1;
736
737 num_pins = pins->length / sizeof(u32);
738 num_funcs = num_pins;
739 maps_per_pin = 0;
740 if (num_funcs)
741 maps_per_pin++;
742 if (has_config && num_pins >= 1)
743 maps_per_pin++;
744
745 if (!num_pins || !maps_per_pin) {
746 err = -EINVAL;
747 goto exit;
748 }
749
750 reserve = num_pins * maps_per_pin;
751
752 err = pinctrl_utils_reserve_map(pctldev, map,
753 reserved_maps, num_maps, reserve);
754 if (err)
755 goto exit;
756
757 for (i = 0; i < num_pins; i++) {
758 err = of_property_read_u32_index(node, "pinmux",
759 i, &pinfunc);
760 if (err)
761 goto exit;
762
763 pin = STM32_GET_PIN_NO(pinfunc);
764 func = STM32_GET_PIN_FUNC(pinfunc);
765
766 if (!stm32_pctrl_is_function_valid(pctl, pin, func)) {
767 err = -EINVAL;
768 goto exit;
769 }
770
771 grp = stm32_pctrl_find_group_by_pin(pctl, pin);
772 if (!grp) {
773 dev_err(pctl->dev, "unable to match pin %d to group\n",
774 pin);
775 err = -EINVAL;
776 goto exit;
777 }
778
779 err = stm32_pctrl_dt_node_to_map_func(pctl, pin, func, grp, map,
780 reserved_maps, num_maps);
781 if (err)
782 goto exit;
783
784 if (has_config) {
785 err = pinctrl_utils_add_map_configs(pctldev, map,
786 reserved_maps, num_maps, grp->name,
787 configs, num_configs,
788 PIN_MAP_TYPE_CONFIGS_GROUP);
789 if (err)
790 goto exit;
791 }
792 }
793
794 exit:
795 kfree(configs);
796 return err;
797 }
798
stm32_pctrl_dt_node_to_map(struct pinctrl_dev * pctldev,struct device_node * np_config,struct pinctrl_map ** map,unsigned * num_maps)799 static int stm32_pctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
800 struct device_node *np_config,
801 struct pinctrl_map **map, unsigned *num_maps)
802 {
803 unsigned reserved_maps;
804 int ret;
805
806 *map = NULL;
807 *num_maps = 0;
808 reserved_maps = 0;
809
810 for_each_child_of_node_scoped(np_config, np) {
811 ret = stm32_pctrl_dt_subnode_to_map(pctldev, np, map,
812 &reserved_maps, num_maps);
813 if (ret < 0) {
814 pinctrl_utils_free_map(pctldev, *map, *num_maps);
815 return ret;
816 }
817 }
818
819 return 0;
820 }
821
stm32_pctrl_get_groups_count(struct pinctrl_dev * pctldev)822 static int stm32_pctrl_get_groups_count(struct pinctrl_dev *pctldev)
823 {
824 struct stm32_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
825
826 return pctl->ngroups;
827 }
828
stm32_pctrl_get_group_name(struct pinctrl_dev * pctldev,unsigned group)829 static const char *stm32_pctrl_get_group_name(struct pinctrl_dev *pctldev,
830 unsigned group)
831 {
832 struct stm32_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
833
834 return pctl->groups[group].name;
835 }
836
stm32_pctrl_get_group_pins(struct pinctrl_dev * pctldev,unsigned group,const unsigned ** pins,unsigned * num_pins)837 static int stm32_pctrl_get_group_pins(struct pinctrl_dev *pctldev,
838 unsigned group,
839 const unsigned **pins,
840 unsigned *num_pins)
841 {
842 struct stm32_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
843
844 *pins = (unsigned *)&pctl->groups[group].pin;
845 *num_pins = 1;
846
847 return 0;
848 }
849
850 static const struct pinctrl_ops stm32_pctrl_ops = {
851 .dt_node_to_map = stm32_pctrl_dt_node_to_map,
852 .dt_free_map = pinctrl_utils_free_map,
853 .get_groups_count = stm32_pctrl_get_groups_count,
854 .get_group_name = stm32_pctrl_get_group_name,
855 .get_group_pins = stm32_pctrl_get_group_pins,
856 };
857
858
859 /* Pinmux functions */
860
stm32_pmx_get_funcs_cnt(struct pinctrl_dev * pctldev)861 static int stm32_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
862 {
863 return ARRAY_SIZE(stm32_gpio_functions);
864 }
865
stm32_pmx_get_func_name(struct pinctrl_dev * pctldev,unsigned selector)866 static const char *stm32_pmx_get_func_name(struct pinctrl_dev *pctldev,
867 unsigned selector)
868 {
869 return stm32_gpio_functions[selector];
870 }
871
stm32_pmx_get_func_groups(struct pinctrl_dev * pctldev,unsigned function,const char * const ** groups,unsigned * const num_groups)872 static int stm32_pmx_get_func_groups(struct pinctrl_dev *pctldev,
873 unsigned function,
874 const char * const **groups,
875 unsigned * const num_groups)
876 {
877 struct stm32_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
878
879 *groups = pctl->grp_names;
880 *num_groups = pctl->ngroups;
881
882 return 0;
883 }
884
stm32_pmx_set_mode(struct stm32_gpio_bank * bank,int pin,u32 mode,u32 alt)885 static int stm32_pmx_set_mode(struct stm32_gpio_bank *bank,
886 int pin, u32 mode, u32 alt)
887 {
888 struct stm32_pinctrl *pctl = dev_get_drvdata(bank->gpio_chip.parent);
889 u32 val;
890 int alt_shift = (pin % 8) * 4;
891 int alt_offset = STM32_GPIO_AFRL + (pin / 8) * 4;
892 unsigned long flags;
893 int err = 0;
894
895 spin_lock_irqsave(&bank->lock, flags);
896
897 if (pctl->hwlock) {
898 err = hwspin_lock_timeout_in_atomic(pctl->hwlock,
899 HWSPNLCK_TIMEOUT);
900 if (err) {
901 dev_err(pctl->dev, "Can't get hwspinlock\n");
902 goto unlock;
903 }
904 }
905
906 val = readl_relaxed(bank->base + alt_offset);
907 val &= ~GENMASK(alt_shift + 3, alt_shift);
908 val |= (alt << alt_shift);
909 writel_relaxed(val, bank->base + alt_offset);
910
911 val = readl_relaxed(bank->base + STM32_GPIO_MODER);
912 val &= ~GENMASK(pin * 2 + 1, pin * 2);
913 val |= mode << (pin * 2);
914 writel_relaxed(val, bank->base + STM32_GPIO_MODER);
915
916 if (pctl->hwlock)
917 hwspin_unlock_in_atomic(pctl->hwlock);
918
919 stm32_gpio_backup_mode(bank, pin, mode, alt);
920
921 unlock:
922 spin_unlock_irqrestore(&bank->lock, flags);
923
924 return err;
925 }
926
stm32_pmx_get_mode(struct stm32_gpio_bank * bank,int pin,u32 * mode,u32 * alt)927 static void stm32_pmx_get_mode(struct stm32_gpio_bank *bank, int pin, u32 *mode, u32 *alt)
928 {
929 u32 val;
930 int alt_shift = (pin % 8) * 4;
931 int alt_offset = STM32_GPIO_AFRL + (pin / 8) * 4;
932 unsigned long flags;
933
934 spin_lock_irqsave(&bank->lock, flags);
935
936 val = readl_relaxed(bank->base + alt_offset);
937 val &= GENMASK(alt_shift + 3, alt_shift);
938 *alt = val >> alt_shift;
939
940 val = readl_relaxed(bank->base + STM32_GPIO_MODER);
941 val &= GENMASK(pin * 2 + 1, pin * 2);
942 *mode = val >> (pin * 2);
943
944 spin_unlock_irqrestore(&bank->lock, flags);
945 }
946
stm32_pmx_set_mux(struct pinctrl_dev * pctldev,unsigned function,unsigned group)947 static int stm32_pmx_set_mux(struct pinctrl_dev *pctldev,
948 unsigned function,
949 unsigned group)
950 {
951 bool ret;
952 struct stm32_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
953 struct stm32_pinctrl_group *g = pctl->groups + group;
954 struct pinctrl_gpio_range *range;
955 struct stm32_gpio_bank *bank;
956 u32 mode, alt;
957 int pin;
958
959 ret = stm32_pctrl_is_function_valid(pctl, g->pin, function);
960 if (!ret)
961 return -EINVAL;
962
963 range = pinctrl_find_gpio_range_from_pin(pctldev, g->pin);
964 if (!range) {
965 dev_err(pctl->dev, "No gpio range defined.\n");
966 return -EINVAL;
967 }
968
969 if (function == STM32_PIN_RSVD) {
970 dev_dbg(pctl->dev, "Reserved pins, skipping HW update.\n");
971 return 0;
972 }
973
974 bank = gpiochip_get_data(range->gc);
975 pin = stm32_gpio_pin(g->pin);
976
977 mode = stm32_gpio_get_mode(function);
978 alt = stm32_gpio_get_alt(function);
979
980 return stm32_pmx_set_mode(bank, pin, mode, alt);
981 }
982
stm32_pmx_gpio_set_direction(struct pinctrl_dev * pctldev,struct pinctrl_gpio_range * range,unsigned gpio,bool input)983 static int stm32_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
984 struct pinctrl_gpio_range *range, unsigned gpio,
985 bool input)
986 {
987 struct stm32_gpio_bank *bank = gpiochip_get_data(range->gc);
988 int pin = stm32_gpio_pin(gpio);
989
990 return stm32_pmx_set_mode(bank, pin, !input, 0);
991 }
992
stm32_pmx_request(struct pinctrl_dev * pctldev,unsigned int gpio)993 static int stm32_pmx_request(struct pinctrl_dev *pctldev, unsigned int gpio)
994 {
995 struct stm32_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
996 struct pinctrl_gpio_range *range;
997
998 range = pinctrl_find_gpio_range_from_pin_nolock(pctldev, gpio);
999 if (!range) {
1000 dev_err(pctl->dev, "No gpio range defined.\n");
1001 return -EINVAL;
1002 }
1003
1004 if (!gpiochip_line_is_valid(range->gc, stm32_gpio_pin(gpio))) {
1005 dev_warn(pctl->dev, "Can't access gpio %d\n", gpio);
1006 return -EACCES;
1007 }
1008
1009 return 0;
1010 }
1011
1012 static const struct pinmux_ops stm32_pmx_ops = {
1013 .get_functions_count = stm32_pmx_get_funcs_cnt,
1014 .get_function_name = stm32_pmx_get_func_name,
1015 .get_function_groups = stm32_pmx_get_func_groups,
1016 .set_mux = stm32_pmx_set_mux,
1017 .gpio_set_direction = stm32_pmx_gpio_set_direction,
1018 .request = stm32_pmx_request,
1019 .strict = true,
1020 };
1021
1022 /* Pinconf functions */
1023
stm32_pconf_set_driving(struct stm32_gpio_bank * bank,unsigned offset,u32 drive)1024 static int stm32_pconf_set_driving(struct stm32_gpio_bank *bank,
1025 unsigned offset, u32 drive)
1026 {
1027 struct stm32_pinctrl *pctl = dev_get_drvdata(bank->gpio_chip.parent);
1028 unsigned long flags;
1029 u32 val;
1030 int err = 0;
1031
1032 spin_lock_irqsave(&bank->lock, flags);
1033
1034 if (pctl->hwlock) {
1035 err = hwspin_lock_timeout_in_atomic(pctl->hwlock,
1036 HWSPNLCK_TIMEOUT);
1037 if (err) {
1038 dev_err(pctl->dev, "Can't get hwspinlock\n");
1039 goto unlock;
1040 }
1041 }
1042
1043 val = readl_relaxed(bank->base + STM32_GPIO_TYPER);
1044 val &= ~BIT(offset);
1045 val |= drive << offset;
1046 writel_relaxed(val, bank->base + STM32_GPIO_TYPER);
1047
1048 if (pctl->hwlock)
1049 hwspin_unlock_in_atomic(pctl->hwlock);
1050
1051 stm32_gpio_backup_driving(bank, offset, drive);
1052
1053 unlock:
1054 spin_unlock_irqrestore(&bank->lock, flags);
1055
1056 return err;
1057 }
1058
stm32_pconf_get_driving(struct stm32_gpio_bank * bank,unsigned int offset)1059 static u32 stm32_pconf_get_driving(struct stm32_gpio_bank *bank,
1060 unsigned int offset)
1061 {
1062 unsigned long flags;
1063 u32 val;
1064
1065 spin_lock_irqsave(&bank->lock, flags);
1066
1067 val = readl_relaxed(bank->base + STM32_GPIO_TYPER);
1068 val &= BIT(offset);
1069
1070 spin_unlock_irqrestore(&bank->lock, flags);
1071
1072 return (val >> offset);
1073 }
1074
stm32_pconf_set_speed(struct stm32_gpio_bank * bank,unsigned offset,u32 speed)1075 static int stm32_pconf_set_speed(struct stm32_gpio_bank *bank,
1076 unsigned offset, u32 speed)
1077 {
1078 struct stm32_pinctrl *pctl = dev_get_drvdata(bank->gpio_chip.parent);
1079 unsigned long flags;
1080 u32 val;
1081 int err = 0;
1082
1083 spin_lock_irqsave(&bank->lock, flags);
1084
1085 if (pctl->hwlock) {
1086 err = hwspin_lock_timeout_in_atomic(pctl->hwlock,
1087 HWSPNLCK_TIMEOUT);
1088 if (err) {
1089 dev_err(pctl->dev, "Can't get hwspinlock\n");
1090 goto unlock;
1091 }
1092 }
1093
1094 val = readl_relaxed(bank->base + STM32_GPIO_SPEEDR);
1095 val &= ~GENMASK(offset * 2 + 1, offset * 2);
1096 val |= speed << (offset * 2);
1097 writel_relaxed(val, bank->base + STM32_GPIO_SPEEDR);
1098
1099 if (pctl->hwlock)
1100 hwspin_unlock_in_atomic(pctl->hwlock);
1101
1102 stm32_gpio_backup_speed(bank, offset, speed);
1103
1104 unlock:
1105 spin_unlock_irqrestore(&bank->lock, flags);
1106
1107 return err;
1108 }
1109
stm32_pconf_get_speed(struct stm32_gpio_bank * bank,unsigned int offset)1110 static u32 stm32_pconf_get_speed(struct stm32_gpio_bank *bank,
1111 unsigned int offset)
1112 {
1113 unsigned long flags;
1114 u32 val;
1115
1116 spin_lock_irqsave(&bank->lock, flags);
1117
1118 val = readl_relaxed(bank->base + STM32_GPIO_SPEEDR);
1119 val &= GENMASK(offset * 2 + 1, offset * 2);
1120
1121 spin_unlock_irqrestore(&bank->lock, flags);
1122
1123 return (val >> (offset * 2));
1124 }
1125
stm32_pconf_set_bias(struct stm32_gpio_bank * bank,unsigned offset,u32 bias)1126 static int stm32_pconf_set_bias(struct stm32_gpio_bank *bank,
1127 unsigned offset, u32 bias)
1128 {
1129 struct stm32_pinctrl *pctl = dev_get_drvdata(bank->gpio_chip.parent);
1130 unsigned long flags;
1131 u32 val;
1132 int err = 0;
1133
1134 spin_lock_irqsave(&bank->lock, flags);
1135
1136 if (pctl->hwlock) {
1137 err = hwspin_lock_timeout_in_atomic(pctl->hwlock,
1138 HWSPNLCK_TIMEOUT);
1139 if (err) {
1140 dev_err(pctl->dev, "Can't get hwspinlock\n");
1141 goto unlock;
1142 }
1143 }
1144
1145 val = readl_relaxed(bank->base + STM32_GPIO_PUPDR);
1146 val &= ~GENMASK(offset * 2 + 1, offset * 2);
1147 val |= bias << (offset * 2);
1148 writel_relaxed(val, bank->base + STM32_GPIO_PUPDR);
1149
1150 if (pctl->hwlock)
1151 hwspin_unlock_in_atomic(pctl->hwlock);
1152
1153 stm32_gpio_backup_bias(bank, offset, bias);
1154
1155 unlock:
1156 spin_unlock_irqrestore(&bank->lock, flags);
1157
1158 return err;
1159 }
1160
stm32_pconf_get_bias(struct stm32_gpio_bank * bank,unsigned int offset)1161 static u32 stm32_pconf_get_bias(struct stm32_gpio_bank *bank,
1162 unsigned int offset)
1163 {
1164 unsigned long flags;
1165 u32 val;
1166
1167 spin_lock_irqsave(&bank->lock, flags);
1168
1169 val = readl_relaxed(bank->base + STM32_GPIO_PUPDR);
1170 val &= GENMASK(offset * 2 + 1, offset * 2);
1171
1172 spin_unlock_irqrestore(&bank->lock, flags);
1173
1174 return (val >> (offset * 2));
1175 }
1176
stm32_pconf_get(struct stm32_gpio_bank * bank,unsigned int offset,bool dir)1177 static bool stm32_pconf_get(struct stm32_gpio_bank *bank,
1178 unsigned int offset, bool dir)
1179 {
1180 unsigned long flags;
1181 u32 val;
1182
1183 spin_lock_irqsave(&bank->lock, flags);
1184
1185 if (dir)
1186 val = !!(readl_relaxed(bank->base + STM32_GPIO_IDR) &
1187 BIT(offset));
1188 else
1189 val = !!(readl_relaxed(bank->base + STM32_GPIO_ODR) &
1190 BIT(offset));
1191
1192 spin_unlock_irqrestore(&bank->lock, flags);
1193
1194 return val;
1195 }
1196
stm32_pconf_parse_conf(struct pinctrl_dev * pctldev,unsigned int pin,enum pin_config_param param,enum pin_config_param arg)1197 static int stm32_pconf_parse_conf(struct pinctrl_dev *pctldev,
1198 unsigned int pin, enum pin_config_param param,
1199 enum pin_config_param arg)
1200 {
1201 struct stm32_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
1202 struct pinctrl_gpio_range *range;
1203 struct stm32_gpio_bank *bank;
1204 int offset, ret = 0;
1205
1206 range = pinctrl_find_gpio_range_from_pin_nolock(pctldev, pin);
1207 if (!range) {
1208 dev_err(pctl->dev, "No gpio range defined.\n");
1209 return -EINVAL;
1210 }
1211
1212 bank = gpiochip_get_data(range->gc);
1213 offset = stm32_gpio_pin(pin);
1214
1215 if (!gpiochip_line_is_valid(range->gc, offset)) {
1216 dev_warn(pctl->dev, "Can't access gpio %d\n", pin);
1217 return -EACCES;
1218 }
1219
1220 switch (param) {
1221 case PIN_CONFIG_DRIVE_PUSH_PULL:
1222 ret = stm32_pconf_set_driving(bank, offset, 0);
1223 break;
1224 case PIN_CONFIG_DRIVE_OPEN_DRAIN:
1225 ret = stm32_pconf_set_driving(bank, offset, 1);
1226 break;
1227 case PIN_CONFIG_SLEW_RATE:
1228 ret = stm32_pconf_set_speed(bank, offset, arg);
1229 break;
1230 case PIN_CONFIG_BIAS_DISABLE:
1231 ret = stm32_pconf_set_bias(bank, offset, 0);
1232 break;
1233 case PIN_CONFIG_BIAS_PULL_UP:
1234 ret = stm32_pconf_set_bias(bank, offset, 1);
1235 break;
1236 case PIN_CONFIG_BIAS_PULL_DOWN:
1237 ret = stm32_pconf_set_bias(bank, offset, 2);
1238 break;
1239 case PIN_CONFIG_OUTPUT:
1240 __stm32_gpio_set(bank, offset, arg);
1241 ret = stm32_pmx_gpio_set_direction(pctldev, range, pin, false);
1242 break;
1243 default:
1244 ret = -ENOTSUPP;
1245 }
1246
1247 return ret;
1248 }
1249
stm32_pconf_group_get(struct pinctrl_dev * pctldev,unsigned group,unsigned long * config)1250 static int stm32_pconf_group_get(struct pinctrl_dev *pctldev,
1251 unsigned group,
1252 unsigned long *config)
1253 {
1254 struct stm32_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
1255
1256 *config = pctl->groups[group].config;
1257
1258 return 0;
1259 }
1260
stm32_pconf_group_set(struct pinctrl_dev * pctldev,unsigned group,unsigned long * configs,unsigned num_configs)1261 static int stm32_pconf_group_set(struct pinctrl_dev *pctldev, unsigned group,
1262 unsigned long *configs, unsigned num_configs)
1263 {
1264 struct stm32_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
1265 struct stm32_pinctrl_group *g = &pctl->groups[group];
1266 int i, ret;
1267
1268 for (i = 0; i < num_configs; i++) {
1269 mutex_lock(&pctldev->mutex);
1270 ret = stm32_pconf_parse_conf(pctldev, g->pin,
1271 pinconf_to_config_param(configs[i]),
1272 pinconf_to_config_argument(configs[i]));
1273 mutex_unlock(&pctldev->mutex);
1274 if (ret < 0)
1275 return ret;
1276
1277 g->config = configs[i];
1278 }
1279
1280 return 0;
1281 }
1282
stm32_pconf_set(struct pinctrl_dev * pctldev,unsigned int pin,unsigned long * configs,unsigned int num_configs)1283 static int stm32_pconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
1284 unsigned long *configs, unsigned int num_configs)
1285 {
1286 int i, ret;
1287
1288 for (i = 0; i < num_configs; i++) {
1289 ret = stm32_pconf_parse_conf(pctldev, pin,
1290 pinconf_to_config_param(configs[i]),
1291 pinconf_to_config_argument(configs[i]));
1292 if (ret < 0)
1293 return ret;
1294 }
1295
1296 return 0;
1297 }
1298
1299 static struct stm32_desc_pin *
stm32_pconf_get_pin_desc_by_pin_number(struct stm32_pinctrl * pctl,unsigned int pin_number)1300 stm32_pconf_get_pin_desc_by_pin_number(struct stm32_pinctrl *pctl,
1301 unsigned int pin_number)
1302 {
1303 struct stm32_desc_pin *pins = pctl->pins;
1304 int i;
1305
1306 for (i = 0; i < pctl->npins; i++) {
1307 if (pins->pin.number == pin_number)
1308 return pins;
1309 pins++;
1310 }
1311 return NULL;
1312 }
1313
stm32_pconf_dbg_show(struct pinctrl_dev * pctldev,struct seq_file * s,unsigned int pin)1314 static void stm32_pconf_dbg_show(struct pinctrl_dev *pctldev,
1315 struct seq_file *s,
1316 unsigned int pin)
1317 {
1318 struct stm32_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
1319 const struct stm32_desc_pin *pin_desc;
1320 struct pinctrl_gpio_range *range;
1321 struct stm32_gpio_bank *bank;
1322 int offset;
1323 u32 mode, alt, drive, speed, bias;
1324 static const char * const modes[] = {
1325 "input", "output", "alternate", "analog" };
1326 static const char * const speeds[] = {
1327 "low", "medium", "high", "very high" };
1328 static const char * const biasing[] = {
1329 "floating", "pull up", "pull down", "" };
1330 bool val;
1331
1332 range = pinctrl_find_gpio_range_from_pin_nolock(pctldev, pin);
1333 if (!range)
1334 return;
1335
1336 bank = gpiochip_get_data(range->gc);
1337 offset = stm32_gpio_pin(pin);
1338
1339 if (!gpiochip_line_is_valid(range->gc, offset)) {
1340 seq_puts(s, "NO ACCESS");
1341 return;
1342 }
1343
1344 stm32_pmx_get_mode(bank, offset, &mode, &alt);
1345 bias = stm32_pconf_get_bias(bank, offset);
1346
1347 seq_printf(s, "%s ", modes[mode]);
1348
1349 switch (mode) {
1350 /* input */
1351 case 0:
1352 val = stm32_pconf_get(bank, offset, true);
1353 seq_printf(s, "- %s - %s",
1354 str_high_low(val),
1355 biasing[bias]);
1356 break;
1357
1358 /* output */
1359 case 1:
1360 drive = stm32_pconf_get_driving(bank, offset);
1361 speed = stm32_pconf_get_speed(bank, offset);
1362 val = stm32_pconf_get(bank, offset, false);
1363 seq_printf(s, "- %s - %s - %s - %s %s",
1364 str_high_low(val),
1365 drive ? "open drain" : "push pull",
1366 biasing[bias],
1367 speeds[speed], "speed");
1368 break;
1369
1370 /* alternate */
1371 case 2:
1372 drive = stm32_pconf_get_driving(bank, offset);
1373 speed = stm32_pconf_get_speed(bank, offset);
1374 pin_desc = stm32_pconf_get_pin_desc_by_pin_number(pctl, pin);
1375 if (!pin_desc)
1376 return;
1377
1378 seq_printf(s, "%d (%s) - %s - %s - %s %s", alt,
1379 pin_desc->functions[alt + 1].name,
1380 drive ? "open drain" : "push pull",
1381 biasing[bias],
1382 speeds[speed], "speed");
1383 break;
1384
1385 /* analog */
1386 case 3:
1387 break;
1388 }
1389 }
1390
1391 static const struct pinconf_ops stm32_pconf_ops = {
1392 .pin_config_group_get = stm32_pconf_group_get,
1393 .pin_config_group_set = stm32_pconf_group_set,
1394 .pin_config_set = stm32_pconf_set,
1395 .pin_config_dbg_show = stm32_pconf_dbg_show,
1396 };
1397
stm32_pctrl_get_desc_pin_from_gpio(struct stm32_pinctrl * pctl,struct stm32_gpio_bank * bank,unsigned int offset)1398 static struct stm32_desc_pin *stm32_pctrl_get_desc_pin_from_gpio(struct stm32_pinctrl *pctl,
1399 struct stm32_gpio_bank *bank,
1400 unsigned int offset)
1401 {
1402 unsigned int stm32_pin_nb = bank->bank_nr * STM32_GPIO_PINS_PER_BANK + offset;
1403 struct stm32_desc_pin *pin_desc;
1404 int i;
1405
1406 /* With few exceptions (e.g. bank 'Z'), pin number matches with pin index in array */
1407 if (stm32_pin_nb < pctl->npins) {
1408 pin_desc = pctl->pins + stm32_pin_nb;
1409 if (pin_desc->pin.number == stm32_pin_nb)
1410 return pin_desc;
1411 }
1412
1413 /* Otherwise, loop all array to find the pin with the right number */
1414 for (i = 0; i < pctl->npins; i++) {
1415 pin_desc = pctl->pins + i;
1416 if (pin_desc->pin.number == stm32_pin_nb)
1417 return pin_desc;
1418 }
1419 return NULL;
1420 }
1421
stm32_gpiolib_register_bank(struct stm32_pinctrl * pctl,struct fwnode_handle * fwnode)1422 static int stm32_gpiolib_register_bank(struct stm32_pinctrl *pctl, struct fwnode_handle *fwnode)
1423 {
1424 struct stm32_gpio_bank *bank = &pctl->banks[pctl->nbanks];
1425 int bank_ioport_nr;
1426 struct pinctrl_gpio_range *range = &bank->range;
1427 struct fwnode_reference_args args;
1428 struct device *dev = pctl->dev;
1429 struct resource res;
1430 int npins = STM32_GPIO_PINS_PER_BANK;
1431 int bank_nr, err, i = 0;
1432 struct stm32_desc_pin *stm32_pin;
1433 char **names;
1434
1435 if (!IS_ERR(bank->rstc))
1436 reset_control_deassert(bank->rstc);
1437
1438 if (of_address_to_resource(to_of_node(fwnode), 0, &res))
1439 return -ENODEV;
1440
1441 bank->base = devm_ioremap_resource(dev, &res);
1442 if (IS_ERR(bank->base))
1443 return PTR_ERR(bank->base);
1444
1445 bank->gpio_chip = stm32_gpio_template;
1446
1447 fwnode_property_read_string(fwnode, "st,bank-name", &bank->gpio_chip.label);
1448
1449 if (!fwnode_property_get_reference_args(fwnode, "gpio-ranges", NULL, 3, i, &args)) {
1450 bank_nr = args.args[1] / STM32_GPIO_PINS_PER_BANK;
1451 bank->gpio_chip.base = args.args[1];
1452
1453 /* get the last defined gpio line (offset + nb of pins) */
1454 npins = args.args[0] + args.args[2];
1455 while (!fwnode_property_get_reference_args(fwnode, "gpio-ranges", NULL, 3, ++i, &args))
1456 npins = max(npins, (int)(args.args[0] + args.args[2]));
1457 } else {
1458 bank_nr = pctl->nbanks;
1459 bank->gpio_chip.base = bank_nr * STM32_GPIO_PINS_PER_BANK;
1460 range->name = bank->gpio_chip.label;
1461 range->id = bank_nr;
1462 range->pin_base = range->id * STM32_GPIO_PINS_PER_BANK;
1463 range->base = range->id * STM32_GPIO_PINS_PER_BANK;
1464 range->npins = npins;
1465 range->gc = &bank->gpio_chip;
1466 pinctrl_add_gpio_range(pctl->pctl_dev,
1467 &pctl->banks[bank_nr].range);
1468 }
1469
1470 if (fwnode_property_read_u32(fwnode, "st,bank-ioport", &bank_ioport_nr))
1471 bank_ioport_nr = bank_nr;
1472
1473 bank->gpio_chip.base = -1;
1474
1475 bank->gpio_chip.ngpio = npins;
1476 bank->gpio_chip.fwnode = fwnode;
1477 bank->gpio_chip.parent = dev;
1478 bank->bank_nr = bank_nr;
1479 bank->bank_ioport_nr = bank_ioport_nr;
1480 bank->secure_control = pctl->match_data->secure_control;
1481 bank->rif_control = pctl->match_data->rif_control;
1482 spin_lock_init(&bank->lock);
1483
1484 if (pctl->domain) {
1485 /* create irq hierarchical domain */
1486 bank->fwnode = fwnode;
1487
1488 bank->domain = irq_domain_create_hierarchy(pctl->domain, 0, STM32_GPIO_IRQ_LINE,
1489 bank->fwnode, &stm32_gpio_domain_ops,
1490 bank);
1491
1492 if (!bank->domain)
1493 return -ENODEV;
1494 }
1495
1496 names = devm_kcalloc(dev, npins, sizeof(char *), GFP_KERNEL);
1497 if (!names)
1498 return -ENOMEM;
1499
1500 for (i = 0; i < npins; i++) {
1501 stm32_pin = stm32_pctrl_get_desc_pin_from_gpio(pctl, bank, i);
1502 if (stm32_pin && stm32_pin->pin.name) {
1503 names[i] = devm_kasprintf(dev, GFP_KERNEL, "%s", stm32_pin->pin.name);
1504 if (!names[i])
1505 return -ENOMEM;
1506 } else {
1507 names[i] = NULL;
1508 }
1509 }
1510
1511 bank->gpio_chip.names = (const char * const *)names;
1512
1513 err = gpiochip_add_data(&bank->gpio_chip, bank);
1514 if (err) {
1515 dev_err(dev, "Failed to add gpiochip(%d)!\n", bank_nr);
1516 return err;
1517 }
1518
1519 dev_info(dev, "%s bank added\n", bank->gpio_chip.label);
1520 return 0;
1521 }
1522
stm32_pctrl_get_irq_domain(struct platform_device * pdev)1523 static struct irq_domain *stm32_pctrl_get_irq_domain(struct platform_device *pdev)
1524 {
1525 struct device_node *np = pdev->dev.of_node;
1526 struct device_node *parent;
1527 struct irq_domain *domain;
1528
1529 if (!of_property_present(np, "interrupt-parent"))
1530 return NULL;
1531
1532 parent = of_irq_find_parent(np);
1533 if (!parent)
1534 return ERR_PTR(-ENXIO);
1535
1536 domain = irq_find_host(parent);
1537 of_node_put(parent);
1538 if (!domain)
1539 /* domain not registered yet */
1540 return ERR_PTR(-EPROBE_DEFER);
1541
1542 return domain;
1543 }
1544
stm32_pctrl_dt_setup_irq(struct platform_device * pdev,struct stm32_pinctrl * pctl)1545 static int stm32_pctrl_dt_setup_irq(struct platform_device *pdev,
1546 struct stm32_pinctrl *pctl)
1547 {
1548 struct device_node *np = pdev->dev.of_node;
1549 struct device *dev = &pdev->dev;
1550 struct regmap *rm;
1551 int offset, ret, i;
1552 int mask, mask_width;
1553
1554 pctl->regmap = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
1555 if (IS_ERR(pctl->regmap))
1556 return PTR_ERR(pctl->regmap);
1557
1558 rm = pctl->regmap;
1559
1560 ret = of_property_read_u32_index(np, "st,syscfg", 1, &offset);
1561 if (ret)
1562 return ret;
1563
1564 ret = of_property_read_u32_index(np, "st,syscfg", 2, &mask);
1565 if (ret)
1566 mask = SYSCFG_IRQMUX_MASK;
1567
1568 mask_width = fls(mask);
1569
1570 for (i = 0; i < STM32_GPIO_PINS_PER_BANK; i++) {
1571 struct reg_field mux;
1572
1573 mux.reg = offset + (i / 4) * 4;
1574 mux.lsb = (i % 4) * mask_width;
1575 mux.msb = mux.lsb + mask_width - 1;
1576
1577 dev_dbg(dev, "irqmux%d: reg:%#x, lsb:%d, msb:%d\n",
1578 i, mux.reg, mux.lsb, mux.msb);
1579
1580 pctl->irqmux[i] = devm_regmap_field_alloc(dev, rm, mux);
1581 if (IS_ERR(pctl->irqmux[i]))
1582 return PTR_ERR(pctl->irqmux[i]);
1583 }
1584
1585 return 0;
1586 }
1587
stm32_pctrl_build_state(struct platform_device * pdev)1588 static int stm32_pctrl_build_state(struct platform_device *pdev)
1589 {
1590 struct stm32_pinctrl *pctl = platform_get_drvdata(pdev);
1591 int i;
1592
1593 pctl->ngroups = pctl->npins;
1594
1595 /* Allocate groups */
1596 pctl->groups = devm_kcalloc(&pdev->dev, pctl->ngroups,
1597 sizeof(*pctl->groups), GFP_KERNEL);
1598 if (!pctl->groups)
1599 return -ENOMEM;
1600
1601 /* We assume that one pin is one group, use pin name as group name. */
1602 pctl->grp_names = devm_kcalloc(&pdev->dev, pctl->ngroups,
1603 sizeof(*pctl->grp_names), GFP_KERNEL);
1604 if (!pctl->grp_names)
1605 return -ENOMEM;
1606
1607 for (i = 0; i < pctl->npins; i++) {
1608 const struct stm32_desc_pin *pin = pctl->pins + i;
1609 struct stm32_pinctrl_group *group = pctl->groups + i;
1610
1611 group->name = pin->pin.name;
1612 group->pin = pin->pin.number;
1613 pctl->grp_names[i] = pin->pin.name;
1614 }
1615
1616 return 0;
1617 }
1618
stm32_pctrl_create_pins_tab(struct stm32_pinctrl * pctl,struct stm32_desc_pin * pins)1619 static int stm32_pctrl_create_pins_tab(struct stm32_pinctrl *pctl,
1620 struct stm32_desc_pin *pins)
1621 {
1622 const struct stm32_desc_pin *p;
1623 int i, nb_pins_available = 0;
1624
1625 for (i = 0; i < pctl->match_data->npins; i++) {
1626 p = pctl->match_data->pins + i;
1627 if (pctl->pkg && !(pctl->pkg & p->pkg))
1628 continue;
1629 pins->pin = p->pin;
1630 memcpy((struct stm32_desc_pin *)pins->functions, p->functions,
1631 STM32_CONFIG_NUM * sizeof(struct stm32_desc_function));
1632 pins++;
1633 nb_pins_available++;
1634 }
1635
1636 pctl->npins = nb_pins_available;
1637
1638 return 0;
1639 }
1640
stm32_pctl_probe(struct platform_device * pdev)1641 int stm32_pctl_probe(struct platform_device *pdev)
1642 {
1643 const struct stm32_pinctrl_match_data *match_data;
1644 struct fwnode_handle *child;
1645 struct device *dev = &pdev->dev;
1646 struct stm32_pinctrl *pctl;
1647 struct pinctrl_pin_desc *pins;
1648 int i, ret, hwlock_id;
1649 unsigned int banks;
1650
1651 match_data = device_get_match_data(dev);
1652 if (!match_data)
1653 return -EINVAL;
1654
1655 pctl = devm_kzalloc(dev, sizeof(*pctl), GFP_KERNEL);
1656 if (!pctl)
1657 return -ENOMEM;
1658
1659 platform_set_drvdata(pdev, pctl);
1660
1661 /* check for IRQ controller (may require deferred probe) */
1662 pctl->domain = stm32_pctrl_get_irq_domain(pdev);
1663 if (IS_ERR(pctl->domain))
1664 return PTR_ERR(pctl->domain);
1665 if (!pctl->domain)
1666 dev_warn(dev, "pinctrl without interrupt support\n");
1667
1668 /* hwspinlock is optional */
1669 hwlock_id = of_hwspin_lock_get_id(pdev->dev.of_node, 0);
1670 if (hwlock_id < 0) {
1671 if (hwlock_id == -EPROBE_DEFER)
1672 return hwlock_id;
1673 } else {
1674 pctl->hwlock = hwspin_lock_request_specific(hwlock_id);
1675 }
1676
1677 spin_lock_init(&pctl->irqmux_lock);
1678
1679 pctl->dev = dev;
1680 pctl->match_data = match_data;
1681
1682 /* get optional package information */
1683 if (!device_property_read_u32(dev, "st,package", &pctl->pkg))
1684 dev_dbg(pctl->dev, "package detected: %x\n", pctl->pkg);
1685
1686 pctl->pins = devm_kcalloc(pctl->dev, pctl->match_data->npins,
1687 sizeof(*pctl->pins), GFP_KERNEL);
1688 if (!pctl->pins)
1689 return -ENOMEM;
1690
1691 ret = stm32_pctrl_create_pins_tab(pctl, pctl->pins);
1692 if (ret)
1693 return ret;
1694
1695 ret = stm32_pctrl_build_state(pdev);
1696 if (ret) {
1697 dev_err(dev, "build state failed: %d\n", ret);
1698 return -EINVAL;
1699 }
1700
1701 if (pctl->domain) {
1702 ret = stm32_pctrl_dt_setup_irq(pdev, pctl);
1703 if (ret)
1704 return ret;
1705 }
1706
1707 pins = devm_kcalloc(&pdev->dev, pctl->npins, sizeof(*pins),
1708 GFP_KERNEL);
1709 if (!pins)
1710 return -ENOMEM;
1711
1712 for (i = 0; i < pctl->npins; i++)
1713 pins[i] = pctl->pins[i].pin;
1714
1715 pctl->pctl_desc.name = dev_name(&pdev->dev);
1716 pctl->pctl_desc.owner = THIS_MODULE;
1717 pctl->pctl_desc.pins = pins;
1718 pctl->pctl_desc.npins = pctl->npins;
1719 pctl->pctl_desc.link_consumers = true;
1720 pctl->pctl_desc.confops = &stm32_pconf_ops;
1721 pctl->pctl_desc.pctlops = &stm32_pctrl_ops;
1722 pctl->pctl_desc.pmxops = &stm32_pmx_ops;
1723 pctl->dev = &pdev->dev;
1724
1725 pctl->pctl_dev = devm_pinctrl_register(&pdev->dev, &pctl->pctl_desc,
1726 pctl);
1727
1728 if (IS_ERR(pctl->pctl_dev)) {
1729 dev_err(&pdev->dev, "Failed pinctrl registration\n");
1730 return PTR_ERR(pctl->pctl_dev);
1731 }
1732
1733 banks = gpiochip_node_count(dev);
1734 if (!banks) {
1735 dev_err(dev, "at least one GPIO bank is required\n");
1736 return -EINVAL;
1737 }
1738 pctl->banks = devm_kcalloc(dev, banks, sizeof(*pctl->banks),
1739 GFP_KERNEL);
1740 if (!pctl->banks)
1741 return -ENOMEM;
1742
1743 pctl->clks = devm_kcalloc(dev, banks, sizeof(*pctl->clks),
1744 GFP_KERNEL);
1745 if (!pctl->clks)
1746 return -ENOMEM;
1747
1748 i = 0;
1749 for_each_gpiochip_node(dev, child) {
1750 struct stm32_gpio_bank *bank = &pctl->banks[i];
1751 struct device_node *np = to_of_node(child);
1752
1753 bank->rstc = of_reset_control_get_exclusive(np, NULL);
1754 if (PTR_ERR(bank->rstc) == -EPROBE_DEFER) {
1755 fwnode_handle_put(child);
1756 return -EPROBE_DEFER;
1757 }
1758
1759 pctl->clks[i].clk = of_clk_get_by_name(np, NULL);
1760 if (IS_ERR(pctl->clks[i].clk)) {
1761 fwnode_handle_put(child);
1762 return dev_err_probe(dev, PTR_ERR(pctl->clks[i].clk),
1763 "failed to get clk\n");
1764 }
1765 pctl->clks[i].id = "pctl";
1766 i++;
1767 }
1768
1769 ret = clk_bulk_prepare_enable(banks, pctl->clks);
1770 if (ret) {
1771 dev_err(dev, "failed to prepare_enable clk (%d)\n", ret);
1772 return ret;
1773 }
1774
1775 for_each_gpiochip_node(dev, child) {
1776 ret = stm32_gpiolib_register_bank(pctl, child);
1777 if (ret) {
1778 fwnode_handle_put(child);
1779 goto err_register;
1780 }
1781
1782 pctl->nbanks++;
1783 }
1784
1785 dev_info(dev, "Pinctrl STM32 initialized\n");
1786
1787 return 0;
1788 err_register:
1789 for (i = 0; i < pctl->nbanks; i++) {
1790 struct stm32_gpio_bank *bank = &pctl->banks[i];
1791
1792 gpiochip_remove(&bank->gpio_chip);
1793 }
1794
1795 clk_bulk_disable_unprepare(banks, pctl->clks);
1796 return ret;
1797 }
1798 EXPORT_SYMBOL(stm32_pctl_probe);
1799
stm32_pinctrl_restore_gpio_regs(struct stm32_pinctrl * pctl,u32 pin)1800 static int __maybe_unused stm32_pinctrl_restore_gpio_regs(
1801 struct stm32_pinctrl *pctl, u32 pin)
1802 {
1803 const struct pin_desc *desc = pin_desc_get(pctl->pctl_dev, pin);
1804 u32 val, alt, mode, offset = stm32_gpio_pin(pin);
1805 struct pinctrl_gpio_range *range;
1806 struct stm32_gpio_bank *bank;
1807 bool pin_is_irq;
1808 int ret;
1809
1810 range = pinctrl_find_gpio_range_from_pin(pctl->pctl_dev, pin);
1811 if (!range)
1812 return 0;
1813
1814 if (!gpiochip_line_is_valid(range->gc, offset))
1815 return 0;
1816
1817 pin_is_irq = gpiochip_line_is_irq(range->gc, offset);
1818
1819 if (!desc || (!pin_is_irq && !desc->gpio_owner))
1820 return 0;
1821
1822 bank = gpiochip_get_data(range->gc);
1823
1824 alt = bank->pin_backup[offset] & STM32_GPIO_BKP_ALT_MASK;
1825 alt >>= STM32_GPIO_BKP_ALT_SHIFT;
1826 mode = bank->pin_backup[offset] & STM32_GPIO_BKP_MODE_MASK;
1827 mode >>= STM32_GPIO_BKP_MODE_SHIFT;
1828
1829 ret = stm32_pmx_set_mode(bank, offset, mode, alt);
1830 if (ret)
1831 return ret;
1832
1833 if (mode == 1) {
1834 val = bank->pin_backup[offset] & BIT(STM32_GPIO_BKP_VAL);
1835 val = val >> STM32_GPIO_BKP_VAL;
1836 __stm32_gpio_set(bank, offset, val);
1837 }
1838
1839 val = bank->pin_backup[offset] & BIT(STM32_GPIO_BKP_TYPE);
1840 val >>= STM32_GPIO_BKP_TYPE;
1841 ret = stm32_pconf_set_driving(bank, offset, val);
1842 if (ret)
1843 return ret;
1844
1845 val = bank->pin_backup[offset] & STM32_GPIO_BKP_SPEED_MASK;
1846 val >>= STM32_GPIO_BKP_SPEED_SHIFT;
1847 ret = stm32_pconf_set_speed(bank, offset, val);
1848 if (ret)
1849 return ret;
1850
1851 val = bank->pin_backup[offset] & STM32_GPIO_BKP_PUPD_MASK;
1852 val >>= STM32_GPIO_BKP_PUPD_SHIFT;
1853 ret = stm32_pconf_set_bias(bank, offset, val);
1854 if (ret)
1855 return ret;
1856
1857 if (pin_is_irq)
1858 regmap_field_write(pctl->irqmux[offset], bank->bank_ioport_nr);
1859
1860 return 0;
1861 }
1862
stm32_pinctrl_suspend(struct device * dev)1863 int __maybe_unused stm32_pinctrl_suspend(struct device *dev)
1864 {
1865 struct stm32_pinctrl *pctl = dev_get_drvdata(dev);
1866
1867 clk_bulk_disable(pctl->nbanks, pctl->clks);
1868
1869 return 0;
1870 }
1871 EXPORT_SYMBOL(stm32_pinctrl_suspend);
1872
stm32_pinctrl_resume(struct device * dev)1873 int __maybe_unused stm32_pinctrl_resume(struct device *dev)
1874 {
1875 struct stm32_pinctrl *pctl = dev_get_drvdata(dev);
1876 struct stm32_pinctrl_group *g = pctl->groups;
1877 int i, ret;
1878
1879 ret = clk_bulk_enable(pctl->nbanks, pctl->clks);
1880 if (ret)
1881 return ret;
1882
1883 for (i = 0; i < pctl->ngroups; i++, g++)
1884 stm32_pinctrl_restore_gpio_regs(pctl, g->pin);
1885
1886 return 0;
1887 }
1888 EXPORT_SYMBOL(stm32_pinctrl_resume);
1889
1890 MODULE_AUTHOR("Alexandre Torgue <alexandre.torgue@foss.st.com>");
1891 MODULE_DESCRIPTION("STM32 core pinctrl driver");
1892 MODULE_LICENSE("GPL");
1893