1 // SPDX-License-Identifier: GPL-2.0
3 * Driver for STMicroelectronics Multi-Function eXpander (STMFX) GPIO expander
5 * Copyright (C) 2019 STMicroelectronics
6 * Author(s): Amelie Delaunay <amelie.delaunay@st.com>.
8 #include <linux/gpio/driver.h>
9 #include <linux/interrupt.h>
10 #include <linux/mfd/stmfx.h>
11 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/seq_file.h>
14 #include <linux/string_choices.h>
16 #include <linux/pinctrl/pinconf.h>
17 #include <linux/pinctrl/pinmux.h>
20 #include "pinctrl-utils.h"
23 /* GPIO_STATE1 0x10, GPIO_STATE2 0x11, GPIO_STATE3 0x12 */
24 #define STMFX_REG_GPIO_STATE STMFX_REG_GPIO_STATE1 /* R */
25 /* GPIO_DIR1 0x60, GPIO_DIR2 0x61, GPIO_DIR3 0x63 */
26 #define STMFX_REG_GPIO_DIR STMFX_REG_GPIO_DIR1 /* RW */
27 /* GPIO_TYPE1 0x64, GPIO_TYPE2 0x65, GPIO_TYPE3 0x66 */
28 #define STMFX_REG_GPIO_TYPE STMFX_REG_GPIO_TYPE1 /* RW */
29 /* GPIO_PUPD1 0x68, GPIO_PUPD2 0x69, GPIO_PUPD3 0x6A */
30 #define STMFX_REG_GPIO_PUPD STMFX_REG_GPIO_PUPD1 /* RW */
31 /* GPO_SET1 0x6C, GPO_SET2 0x6D, GPO_SET3 0x6E */
32 #define STMFX_REG_GPO_SET STMFX_REG_GPO_SET1 /* RW */
33 /* GPO_CLR1 0x70, GPO_CLR2 0x71, GPO_CLR3 0x72 */
34 #define STMFX_REG_GPO_CLR STMFX_REG_GPO_CLR1 /* RW */
35 /* IRQ_GPI_SRC1 0x48, IRQ_GPI_SRC2 0x49, IRQ_GPI_SRC3 0x4A */
36 #define STMFX_REG_IRQ_GPI_SRC STMFX_REG_IRQ_GPI_SRC1 /* RW */
37 /* IRQ_GPI_EVT1 0x4C, IRQ_GPI_EVT2 0x4D, IRQ_GPI_EVT3 0x4E */
38 #define STMFX_REG_IRQ_GPI_EVT STMFX_REG_IRQ_GPI_EVT1 /* RW */
39 /* IRQ_GPI_TYPE1 0x50, IRQ_GPI_TYPE2 0x51, IRQ_GPI_TYPE3 0x52 */
40 #define STMFX_REG_IRQ_GPI_TYPE STMFX_REG_IRQ_GPI_TYPE1 /* RW */
41 /* IRQ_GPI_PENDING1 0x0C, IRQ_GPI_PENDING2 0x0D, IRQ_GPI_PENDING3 0x0E*/
42 #define STMFX_REG_IRQ_GPI_PENDING STMFX_REG_IRQ_GPI_PENDING1 /* R */
43 /* IRQ_GPI_ACK1 0x54, IRQ_GPI_ACK2 0x55, IRQ_GPI_ACK3 0x56 */
44 #define STMFX_REG_IRQ_GPI_ACK STMFX_REG_IRQ_GPI_ACK1 /* RW */
46 #define NR_GPIO_REGS 3
47 #define NR_GPIOS_PER_REG 8
48 #define get_reg(offset) ((offset) / NR_GPIOS_PER_REG)
49 #define get_shift(offset) ((offset) % NR_GPIOS_PER_REG)
50 #define get_mask(offset) (BIT(get_shift(offset)))
53 * STMFX pinctrl can have up to 24 pins if STMFX other functions are not used.
54 * Pins availability is managed thanks to gpio-ranges property.
56 static const struct pinctrl_pin_desc stmfx_pins
[] = {
57 PINCTRL_PIN(0, "gpio0"),
58 PINCTRL_PIN(1, "gpio1"),
59 PINCTRL_PIN(2, "gpio2"),
60 PINCTRL_PIN(3, "gpio3"),
61 PINCTRL_PIN(4, "gpio4"),
62 PINCTRL_PIN(5, "gpio5"),
63 PINCTRL_PIN(6, "gpio6"),
64 PINCTRL_PIN(7, "gpio7"),
65 PINCTRL_PIN(8, "gpio8"),
66 PINCTRL_PIN(9, "gpio9"),
67 PINCTRL_PIN(10, "gpio10"),
68 PINCTRL_PIN(11, "gpio11"),
69 PINCTRL_PIN(12, "gpio12"),
70 PINCTRL_PIN(13, "gpio13"),
71 PINCTRL_PIN(14, "gpio14"),
72 PINCTRL_PIN(15, "gpio15"),
73 PINCTRL_PIN(16, "agpio0"),
74 PINCTRL_PIN(17, "agpio1"),
75 PINCTRL_PIN(18, "agpio2"),
76 PINCTRL_PIN(19, "agpio3"),
77 PINCTRL_PIN(20, "agpio4"),
78 PINCTRL_PIN(21, "agpio5"),
79 PINCTRL_PIN(22, "agpio6"),
80 PINCTRL_PIN(23, "agpio7"),
83 struct stmfx_pinctrl
{
86 struct pinctrl_dev
*pctl_dev
;
87 struct pinctrl_desc pctl_desc
;
88 struct gpio_chip gpio_chip
;
89 struct mutex lock
; /* IRQ bus lock */
90 unsigned long gpio_valid_mask
;
91 /* Cache of IRQ_GPI_* registers for bus_lock */
92 u8 irq_gpi_src
[NR_GPIO_REGS
];
93 u8 irq_gpi_type
[NR_GPIO_REGS
];
94 u8 irq_gpi_evt
[NR_GPIO_REGS
];
95 u8 irq_toggle_edge
[NR_GPIO_REGS
];
97 /* Backup of GPIO_* registers for suspend/resume */
98 u8 bkp_gpio_state
[NR_GPIO_REGS
];
99 u8 bkp_gpio_dir
[NR_GPIO_REGS
];
100 u8 bkp_gpio_type
[NR_GPIO_REGS
];
101 u8 bkp_gpio_pupd
[NR_GPIO_REGS
];
105 static int stmfx_gpio_get(struct gpio_chip
*gc
, unsigned int offset
)
107 struct stmfx_pinctrl
*pctl
= gpiochip_get_data(gc
);
108 u32 reg
= STMFX_REG_GPIO_STATE
+ get_reg(offset
);
109 u32 mask
= get_mask(offset
);
113 ret
= regmap_read(pctl
->stmfx
->map
, reg
, &value
);
115 return ret
? ret
: !!(value
& mask
);
118 static void stmfx_gpio_set(struct gpio_chip
*gc
, unsigned int offset
, int value
)
120 struct stmfx_pinctrl
*pctl
= gpiochip_get_data(gc
);
121 u32 reg
= value
? STMFX_REG_GPO_SET
: STMFX_REG_GPO_CLR
;
122 u32 mask
= get_mask(offset
);
124 regmap_write_bits(pctl
->stmfx
->map
, reg
+ get_reg(offset
),
128 static int stmfx_gpio_get_direction(struct gpio_chip
*gc
, unsigned int offset
)
130 struct stmfx_pinctrl
*pctl
= gpiochip_get_data(gc
);
131 u32 reg
= STMFX_REG_GPIO_DIR
+ get_reg(offset
);
132 u32 mask
= get_mask(offset
);
136 ret
= regmap_read(pctl
->stmfx
->map
, reg
, &val
);
138 * On stmfx, gpio pins direction is (0)input, (1)output.
144 return GPIO_LINE_DIRECTION_OUT
;
146 return GPIO_LINE_DIRECTION_IN
;
149 static int stmfx_gpio_direction_input(struct gpio_chip
*gc
, unsigned int offset
)
151 struct stmfx_pinctrl
*pctl
= gpiochip_get_data(gc
);
152 u32 reg
= STMFX_REG_GPIO_DIR
+ get_reg(offset
);
153 u32 mask
= get_mask(offset
);
155 return regmap_write_bits(pctl
->stmfx
->map
, reg
, mask
, 0);
158 static int stmfx_gpio_direction_output(struct gpio_chip
*gc
,
159 unsigned int offset
, int value
)
161 struct stmfx_pinctrl
*pctl
= gpiochip_get_data(gc
);
162 u32 reg
= STMFX_REG_GPIO_DIR
+ get_reg(offset
);
163 u32 mask
= get_mask(offset
);
165 stmfx_gpio_set(gc
, offset
, value
);
167 return regmap_write_bits(pctl
->stmfx
->map
, reg
, mask
, mask
);
170 static int stmfx_pinconf_get_pupd(struct stmfx_pinctrl
*pctl
,
173 u32 reg
= STMFX_REG_GPIO_PUPD
+ get_reg(offset
);
174 u32 pupd
, mask
= get_mask(offset
);
177 ret
= regmap_read(pctl
->stmfx
->map
, reg
, &pupd
);
181 return !!(pupd
& mask
);
184 static int stmfx_pinconf_set_pupd(struct stmfx_pinctrl
*pctl
,
185 unsigned int offset
, u32 pupd
)
187 u32 reg
= STMFX_REG_GPIO_PUPD
+ get_reg(offset
);
188 u32 mask
= get_mask(offset
);
190 return regmap_write_bits(pctl
->stmfx
->map
, reg
, mask
, pupd
? mask
: 0);
193 static int stmfx_pinconf_get_type(struct stmfx_pinctrl
*pctl
,
196 u32 reg
= STMFX_REG_GPIO_TYPE
+ get_reg(offset
);
197 u32 type
, mask
= get_mask(offset
);
200 ret
= regmap_read(pctl
->stmfx
->map
, reg
, &type
);
204 return !!(type
& mask
);
207 static int stmfx_pinconf_set_type(struct stmfx_pinctrl
*pctl
,
208 unsigned int offset
, u32 type
)
210 u32 reg
= STMFX_REG_GPIO_TYPE
+ get_reg(offset
);
211 u32 mask
= get_mask(offset
);
213 return regmap_write_bits(pctl
->stmfx
->map
, reg
, mask
, type
? mask
: 0);
216 static int stmfx_pinconf_get(struct pinctrl_dev
*pctldev
,
217 unsigned int pin
, unsigned long *config
)
219 struct stmfx_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
220 u32 param
= pinconf_to_config_param(*config
);
221 struct pinctrl_gpio_range
*range
;
223 int ret
, dir
, type
, pupd
;
225 range
= pinctrl_find_gpio_range_from_pin_nolock(pctldev
, pin
);
229 dir
= stmfx_gpio_get_direction(&pctl
->gpio_chip
, pin
);
234 * Currently the gpiolib IN is 1 and OUT is 0 but let's not count
235 * on it just to be on the safe side also in the future :)
237 dir
= (dir
== GPIO_LINE_DIRECTION_IN
) ? 1 : 0;
239 type
= stmfx_pinconf_get_type(pctl
, pin
);
242 pupd
= stmfx_pinconf_get_pupd(pctl
, pin
);
247 case PIN_CONFIG_BIAS_DISABLE
:
248 if ((!dir
&& (!type
|| !pupd
)) || (dir
&& !type
))
251 case PIN_CONFIG_BIAS_PULL_DOWN
:
252 if (dir
&& type
&& !pupd
)
255 case PIN_CONFIG_BIAS_PULL_UP
:
259 case PIN_CONFIG_DRIVE_OPEN_DRAIN
:
260 if ((!dir
&& type
) || (dir
&& !type
))
263 case PIN_CONFIG_DRIVE_PUSH_PULL
:
264 if ((!dir
&& !type
) || (dir
&& type
))
267 case PIN_CONFIG_OUTPUT
:
271 ret
= stmfx_gpio_get(&pctl
->gpio_chip
, pin
);
281 *config
= pinconf_to_config_packed(param
, arg
);
286 static int stmfx_pinconf_set(struct pinctrl_dev
*pctldev
, unsigned int pin
,
287 unsigned long *configs
, unsigned int num_configs
)
289 struct stmfx_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
290 struct pinctrl_gpio_range
*range
;
291 enum pin_config_param param
;
295 range
= pinctrl_find_gpio_range_from_pin_nolock(pctldev
, pin
);
297 dev_err(pctldev
->dev
, "pin %d is not available\n", pin
);
301 for (i
= 0; i
< num_configs
; i
++) {
302 param
= pinconf_to_config_param(configs
[i
]);
303 arg
= pinconf_to_config_argument(configs
[i
]);
306 case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT
:
307 case PIN_CONFIG_BIAS_DISABLE
:
308 case PIN_CONFIG_DRIVE_PUSH_PULL
:
309 ret
= stmfx_pinconf_set_type(pctl
, pin
, 0);
313 case PIN_CONFIG_BIAS_PULL_DOWN
:
314 ret
= stmfx_pinconf_set_type(pctl
, pin
, 1);
317 ret
= stmfx_pinconf_set_pupd(pctl
, pin
, 0);
321 case PIN_CONFIG_BIAS_PULL_UP
:
322 ret
= stmfx_pinconf_set_type(pctl
, pin
, 1);
325 ret
= stmfx_pinconf_set_pupd(pctl
, pin
, 1);
329 case PIN_CONFIG_DRIVE_OPEN_DRAIN
:
330 ret
= stmfx_pinconf_set_type(pctl
, pin
, 1);
334 case PIN_CONFIG_OUTPUT
:
335 ret
= stmfx_gpio_direction_output(&pctl
->gpio_chip
,
348 static void stmfx_pinconf_dbg_show(struct pinctrl_dev
*pctldev
,
349 struct seq_file
*s
, unsigned int offset
)
351 struct stmfx_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
352 struct pinctrl_gpio_range
*range
;
353 int dir
, type
, pupd
, val
;
355 range
= pinctrl_find_gpio_range_from_pin_nolock(pctldev
, offset
);
359 dir
= stmfx_gpio_get_direction(&pctl
->gpio_chip
, offset
);
362 type
= stmfx_pinconf_get_type(pctl
, offset
);
365 pupd
= stmfx_pinconf_get_pupd(pctl
, offset
);
368 val
= stmfx_gpio_get(&pctl
->gpio_chip
, offset
);
372 if (dir
== GPIO_LINE_DIRECTION_OUT
) {
373 seq_printf(s
, "output %s ", str_high_low(val
));
375 seq_printf(s
, "open drain %s internal pull-up ",
376 pupd
? "with" : "without");
378 seq_puts(s
, "push pull no pull ");
380 seq_printf(s
, "input %s ", str_high_low(val
));
382 seq_printf(s
, "with internal pull-%s ",
383 pupd
? "up" : "down");
385 seq_printf(s
, "%s ", pupd
? "floating" : "analog");
389 static const struct pinconf_ops stmfx_pinconf_ops
= {
390 .pin_config_get
= stmfx_pinconf_get
,
391 .pin_config_set
= stmfx_pinconf_set
,
392 .pin_config_dbg_show
= stmfx_pinconf_dbg_show
,
395 static int stmfx_pinctrl_get_groups_count(struct pinctrl_dev
*pctldev
)
400 static const char *stmfx_pinctrl_get_group_name(struct pinctrl_dev
*pctldev
,
401 unsigned int selector
)
406 static int stmfx_pinctrl_get_group_pins(struct pinctrl_dev
*pctldev
,
407 unsigned int selector
,
408 const unsigned int **pins
,
409 unsigned int *num_pins
)
414 static const struct pinctrl_ops stmfx_pinctrl_ops
= {
415 .get_groups_count
= stmfx_pinctrl_get_groups_count
,
416 .get_group_name
= stmfx_pinctrl_get_group_name
,
417 .get_group_pins
= stmfx_pinctrl_get_group_pins
,
418 .dt_node_to_map
= pinconf_generic_dt_node_to_map_pin
,
419 .dt_free_map
= pinctrl_utils_free_map
,
422 static void stmfx_pinctrl_irq_mask(struct irq_data
*data
)
424 struct gpio_chip
*gpio_chip
= irq_data_get_irq_chip_data(data
);
425 struct stmfx_pinctrl
*pctl
= gpiochip_get_data(gpio_chip
);
426 u32 reg
= get_reg(data
->hwirq
);
427 u32 mask
= get_mask(data
->hwirq
);
429 pctl
->irq_gpi_src
[reg
] &= ~mask
;
430 gpiochip_disable_irq(gpio_chip
, irqd_to_hwirq(data
));
433 static void stmfx_pinctrl_irq_unmask(struct irq_data
*data
)
435 struct gpio_chip
*gpio_chip
= irq_data_get_irq_chip_data(data
);
436 struct stmfx_pinctrl
*pctl
= gpiochip_get_data(gpio_chip
);
437 u32 reg
= get_reg(data
->hwirq
);
438 u32 mask
= get_mask(data
->hwirq
);
440 gpiochip_enable_irq(gpio_chip
, irqd_to_hwirq(data
));
441 pctl
->irq_gpi_src
[reg
] |= mask
;
444 static int stmfx_pinctrl_irq_set_type(struct irq_data
*data
, unsigned int type
)
446 struct gpio_chip
*gpio_chip
= irq_data_get_irq_chip_data(data
);
447 struct stmfx_pinctrl
*pctl
= gpiochip_get_data(gpio_chip
);
448 u32 reg
= get_reg(data
->hwirq
);
449 u32 mask
= get_mask(data
->hwirq
);
451 if (type
== IRQ_TYPE_NONE
)
454 if (type
& IRQ_TYPE_EDGE_BOTH
) {
455 pctl
->irq_gpi_evt
[reg
] |= mask
;
456 irq_set_handler_locked(data
, handle_edge_irq
);
458 pctl
->irq_gpi_evt
[reg
] &= ~mask
;
459 irq_set_handler_locked(data
, handle_level_irq
);
462 if ((type
& IRQ_TYPE_EDGE_RISING
) || (type
& IRQ_TYPE_LEVEL_HIGH
))
463 pctl
->irq_gpi_type
[reg
] |= mask
;
465 pctl
->irq_gpi_type
[reg
] &= ~mask
;
468 * In case of (type & IRQ_TYPE_EDGE_BOTH), we need to know current
469 * GPIO value to set the right edge trigger. But in atomic context
470 * here we can't access registers over I2C. That's why (type &
471 * IRQ_TYPE_EDGE_BOTH) will be managed in .irq_sync_unlock.
474 if ((type
& IRQ_TYPE_EDGE_BOTH
) == IRQ_TYPE_EDGE_BOTH
)
475 pctl
->irq_toggle_edge
[reg
] |= mask
;
477 pctl
->irq_toggle_edge
[reg
] &= mask
;
482 static void stmfx_pinctrl_irq_bus_lock(struct irq_data
*data
)
484 struct gpio_chip
*gpio_chip
= irq_data_get_irq_chip_data(data
);
485 struct stmfx_pinctrl
*pctl
= gpiochip_get_data(gpio_chip
);
487 mutex_lock(&pctl
->lock
);
490 static void stmfx_pinctrl_irq_bus_sync_unlock(struct irq_data
*data
)
492 struct gpio_chip
*gpio_chip
= irq_data_get_irq_chip_data(data
);
493 struct stmfx_pinctrl
*pctl
= gpiochip_get_data(gpio_chip
);
494 u32 reg
= get_reg(data
->hwirq
);
495 u32 mask
= get_mask(data
->hwirq
);
498 * In case of IRQ_TYPE_EDGE_BOTH), read the current GPIO value
499 * (this couldn't be done in .irq_set_type because of atomic context)
500 * to set the right irq trigger type.
502 if (pctl
->irq_toggle_edge
[reg
] & mask
) {
503 if (stmfx_gpio_get(gpio_chip
, data
->hwirq
))
504 pctl
->irq_gpi_type
[reg
] &= ~mask
;
506 pctl
->irq_gpi_type
[reg
] |= mask
;
509 regmap_bulk_write(pctl
->stmfx
->map
, STMFX_REG_IRQ_GPI_EVT
,
510 pctl
->irq_gpi_evt
, NR_GPIO_REGS
);
511 regmap_bulk_write(pctl
->stmfx
->map
, STMFX_REG_IRQ_GPI_TYPE
,
512 pctl
->irq_gpi_type
, NR_GPIO_REGS
);
513 regmap_bulk_write(pctl
->stmfx
->map
, STMFX_REG_IRQ_GPI_SRC
,
514 pctl
->irq_gpi_src
, NR_GPIO_REGS
);
516 mutex_unlock(&pctl
->lock
);
519 static int stmfx_gpio_irq_request_resources(struct irq_data
*data
)
521 struct gpio_chip
*gpio_chip
= irq_data_get_irq_chip_data(data
);
524 ret
= stmfx_gpio_direction_input(gpio_chip
, data
->hwirq
);
528 return gpiochip_reqres_irq(gpio_chip
, data
->hwirq
);
531 static void stmfx_gpio_irq_release_resources(struct irq_data
*data
)
533 struct gpio_chip
*gpio_chip
= irq_data_get_irq_chip_data(data
);
535 return gpiochip_relres_irq(gpio_chip
, data
->hwirq
);
538 static void stmfx_pinctrl_irq_toggle_trigger(struct stmfx_pinctrl
*pctl
,
541 u32 reg
= get_reg(offset
);
542 u32 mask
= get_mask(offset
);
545 if (!(pctl
->irq_toggle_edge
[reg
] & mask
))
548 val
= stmfx_gpio_get(&pctl
->gpio_chip
, offset
);
553 pctl
->irq_gpi_type
[reg
] &= mask
;
554 regmap_write_bits(pctl
->stmfx
->map
,
555 STMFX_REG_IRQ_GPI_TYPE
+ reg
,
559 pctl
->irq_gpi_type
[reg
] |= mask
;
560 regmap_write_bits(pctl
->stmfx
->map
,
561 STMFX_REG_IRQ_GPI_TYPE
+ reg
,
566 static irqreturn_t
stmfx_pinctrl_irq_thread_fn(int irq
, void *dev_id
)
568 struct stmfx_pinctrl
*pctl
= (struct stmfx_pinctrl
*)dev_id
;
569 struct gpio_chip
*gc
= &pctl
->gpio_chip
;
570 u8 pending
[NR_GPIO_REGS
];
571 u8 src
[NR_GPIO_REGS
] = {0, 0, 0};
572 unsigned long n
, status
;
575 ret
= regmap_bulk_read(pctl
->stmfx
->map
, STMFX_REG_IRQ_GPI_PENDING
,
576 &pending
, NR_GPIO_REGS
);
580 regmap_bulk_write(pctl
->stmfx
->map
, STMFX_REG_IRQ_GPI_SRC
,
583 BUILD_BUG_ON(NR_GPIO_REGS
> sizeof(status
));
584 for (i
= 0, status
= 0; i
< NR_GPIO_REGS
; i
++)
585 status
|= (unsigned long)pending
[i
] << (i
* 8);
586 for_each_set_bit(n
, &status
, gc
->ngpio
) {
587 handle_nested_irq(irq_find_mapping(gc
->irq
.domain
, n
));
588 stmfx_pinctrl_irq_toggle_trigger(pctl
, n
);
591 regmap_bulk_write(pctl
->stmfx
->map
, STMFX_REG_IRQ_GPI_SRC
,
592 pctl
->irq_gpi_src
, NR_GPIO_REGS
);
597 static void stmfx_pinctrl_irq_print_chip(struct irq_data
*d
, struct seq_file
*p
)
599 struct gpio_chip
*gpio_chip
= irq_data_get_irq_chip_data(d
);
600 struct stmfx_pinctrl
*pctl
= gpiochip_get_data(gpio_chip
);
602 seq_puts(p
, dev_name(pctl
->dev
));
605 static const struct irq_chip stmfx_pinctrl_irq_chip
= {
606 .irq_mask
= stmfx_pinctrl_irq_mask
,
607 .irq_unmask
= stmfx_pinctrl_irq_unmask
,
608 .irq_set_type
= stmfx_pinctrl_irq_set_type
,
609 .irq_bus_lock
= stmfx_pinctrl_irq_bus_lock
,
610 .irq_bus_sync_unlock
= stmfx_pinctrl_irq_bus_sync_unlock
,
611 .irq_request_resources
= stmfx_gpio_irq_request_resources
,
612 .irq_release_resources
= stmfx_gpio_irq_release_resources
,
613 .irq_print_chip
= stmfx_pinctrl_irq_print_chip
,
614 .flags
= IRQCHIP_IMMUTABLE
,
617 static int stmfx_pinctrl_gpio_function_enable(struct stmfx_pinctrl
*pctl
)
619 struct pinctrl_gpio_range
*gpio_range
;
620 struct pinctrl_dev
*pctl_dev
= pctl
->pctl_dev
;
621 u32 func
= STMFX_FUNC_GPIO
;
623 pctl
->gpio_valid_mask
= GENMASK(15, 0);
625 gpio_range
= pinctrl_find_gpio_range_from_pin(pctl_dev
, 16);
627 func
|= STMFX_FUNC_ALTGPIO_LOW
;
628 pctl
->gpio_valid_mask
|= GENMASK(19, 16);
631 gpio_range
= pinctrl_find_gpio_range_from_pin(pctl_dev
, 20);
633 func
|= STMFX_FUNC_ALTGPIO_HIGH
;
634 pctl
->gpio_valid_mask
|= GENMASK(23, 20);
637 return stmfx_function_enable(pctl
->stmfx
, func
);
640 static int stmfx_pinctrl_probe(struct platform_device
*pdev
)
642 struct stmfx
*stmfx
= dev_get_drvdata(pdev
->dev
.parent
);
643 struct device_node
*np
= pdev
->dev
.of_node
;
644 struct stmfx_pinctrl
*pctl
;
645 struct gpio_irq_chip
*girq
;
648 pctl
= devm_kzalloc(stmfx
->dev
, sizeof(*pctl
), GFP_KERNEL
);
652 platform_set_drvdata(pdev
, pctl
);
654 pctl
->dev
= &pdev
->dev
;
657 if (!of_property_present(np
, "gpio-ranges")) {
658 dev_err(pctl
->dev
, "missing required gpio-ranges property\n");
662 irq
= platform_get_irq(pdev
, 0);
666 mutex_init(&pctl
->lock
);
668 /* Register pin controller */
669 pctl
->pctl_desc
.name
= "stmfx-pinctrl";
670 pctl
->pctl_desc
.pctlops
= &stmfx_pinctrl_ops
;
671 pctl
->pctl_desc
.confops
= &stmfx_pinconf_ops
;
672 pctl
->pctl_desc
.pins
= stmfx_pins
;
673 pctl
->pctl_desc
.npins
= ARRAY_SIZE(stmfx_pins
);
674 pctl
->pctl_desc
.owner
= THIS_MODULE
;
675 pctl
->pctl_desc
.link_consumers
= true;
677 ret
= devm_pinctrl_register_and_init(pctl
->dev
, &pctl
->pctl_desc
,
678 pctl
, &pctl
->pctl_dev
);
680 dev_err(pctl
->dev
, "pinctrl registration failed\n");
684 ret
= pinctrl_enable(pctl
->pctl_dev
);
686 dev_err(pctl
->dev
, "pinctrl enable failed\n");
690 /* Register gpio controller */
691 pctl
->gpio_chip
.label
= "stmfx-gpio";
692 pctl
->gpio_chip
.parent
= pctl
->dev
;
693 pctl
->gpio_chip
.get_direction
= stmfx_gpio_get_direction
;
694 pctl
->gpio_chip
.direction_input
= stmfx_gpio_direction_input
;
695 pctl
->gpio_chip
.direction_output
= stmfx_gpio_direction_output
;
696 pctl
->gpio_chip
.get
= stmfx_gpio_get
;
697 pctl
->gpio_chip
.set
= stmfx_gpio_set
;
698 pctl
->gpio_chip
.set_config
= gpiochip_generic_config
;
699 pctl
->gpio_chip
.base
= -1;
700 pctl
->gpio_chip
.ngpio
= pctl
->pctl_desc
.npins
;
701 pctl
->gpio_chip
.can_sleep
= true;
703 girq
= &pctl
->gpio_chip
.irq
;
704 gpio_irq_chip_set_chip(girq
, &stmfx_pinctrl_irq_chip
);
705 /* This will let us handle the parent IRQ in the driver */
706 girq
->parent_handler
= NULL
;
707 girq
->num_parents
= 0;
708 girq
->parents
= NULL
;
709 girq
->default_type
= IRQ_TYPE_NONE
;
710 girq
->handler
= handle_bad_irq
;
711 girq
->threaded
= true;
713 ret
= devm_gpiochip_add_data(pctl
->dev
, &pctl
->gpio_chip
, pctl
);
715 dev_err(pctl
->dev
, "gpio_chip registration failed\n");
719 ret
= stmfx_pinctrl_gpio_function_enable(pctl
);
723 ret
= devm_request_threaded_irq(pctl
->dev
, irq
, NULL
,
724 stmfx_pinctrl_irq_thread_fn
,
726 dev_name(pctl
->dev
), pctl
);
728 dev_err(pctl
->dev
, "cannot request irq%d\n", irq
);
733 "%ld GPIOs available\n", hweight_long(pctl
->gpio_valid_mask
));
738 static void stmfx_pinctrl_remove(struct platform_device
*pdev
)
740 struct stmfx
*stmfx
= dev_get_drvdata(pdev
->dev
.parent
);
743 ret
= stmfx_function_disable(stmfx
,
745 STMFX_FUNC_ALTGPIO_LOW
|
746 STMFX_FUNC_ALTGPIO_HIGH
);
748 dev_err(&pdev
->dev
, "Failed to disable pins (%pe)\n",
752 #ifdef CONFIG_PM_SLEEP
753 static int stmfx_pinctrl_backup_regs(struct stmfx_pinctrl
*pctl
)
757 ret
= regmap_bulk_read(pctl
->stmfx
->map
, STMFX_REG_GPIO_STATE
,
758 &pctl
->bkp_gpio_state
, NR_GPIO_REGS
);
761 ret
= regmap_bulk_read(pctl
->stmfx
->map
, STMFX_REG_GPIO_DIR
,
762 &pctl
->bkp_gpio_dir
, NR_GPIO_REGS
);
765 ret
= regmap_bulk_read(pctl
->stmfx
->map
, STMFX_REG_GPIO_TYPE
,
766 &pctl
->bkp_gpio_type
, NR_GPIO_REGS
);
769 ret
= regmap_bulk_read(pctl
->stmfx
->map
, STMFX_REG_GPIO_PUPD
,
770 &pctl
->bkp_gpio_pupd
, NR_GPIO_REGS
);
777 static int stmfx_pinctrl_restore_regs(struct stmfx_pinctrl
*pctl
)
781 ret
= regmap_bulk_write(pctl
->stmfx
->map
, STMFX_REG_GPIO_DIR
,
782 pctl
->bkp_gpio_dir
, NR_GPIO_REGS
);
785 ret
= regmap_bulk_write(pctl
->stmfx
->map
, STMFX_REG_GPIO_TYPE
,
786 pctl
->bkp_gpio_type
, NR_GPIO_REGS
);
789 ret
= regmap_bulk_write(pctl
->stmfx
->map
, STMFX_REG_GPIO_PUPD
,
790 pctl
->bkp_gpio_pupd
, NR_GPIO_REGS
);
793 ret
= regmap_bulk_write(pctl
->stmfx
->map
, STMFX_REG_GPO_SET
,
794 pctl
->bkp_gpio_state
, NR_GPIO_REGS
);
797 ret
= regmap_bulk_write(pctl
->stmfx
->map
, STMFX_REG_IRQ_GPI_EVT
,
798 pctl
->irq_gpi_evt
, NR_GPIO_REGS
);
801 ret
= regmap_bulk_write(pctl
->stmfx
->map
, STMFX_REG_IRQ_GPI_TYPE
,
802 pctl
->irq_gpi_type
, NR_GPIO_REGS
);
805 ret
= regmap_bulk_write(pctl
->stmfx
->map
, STMFX_REG_IRQ_GPI_SRC
,
806 pctl
->irq_gpi_src
, NR_GPIO_REGS
);
813 static int stmfx_pinctrl_suspend(struct device
*dev
)
815 struct stmfx_pinctrl
*pctl
= dev_get_drvdata(dev
);
818 ret
= stmfx_pinctrl_backup_regs(pctl
);
820 dev_err(pctl
->dev
, "registers backup failure\n");
827 static int stmfx_pinctrl_resume(struct device
*dev
)
829 struct stmfx_pinctrl
*pctl
= dev_get_drvdata(dev
);
832 ret
= stmfx_pinctrl_restore_regs(pctl
);
834 dev_err(pctl
->dev
, "registers restoration failure\n");
842 static SIMPLE_DEV_PM_OPS(stmfx_pinctrl_dev_pm_ops
,
843 stmfx_pinctrl_suspend
, stmfx_pinctrl_resume
);
845 static const struct of_device_id stmfx_pinctrl_of_match
[] = {
846 { .compatible
= "st,stmfx-0300-pinctrl", },
849 MODULE_DEVICE_TABLE(of
, stmfx_pinctrl_of_match
);
851 static struct platform_driver stmfx_pinctrl_driver
= {
853 .name
= "stmfx-pinctrl",
854 .of_match_table
= stmfx_pinctrl_of_match
,
855 .pm
= &stmfx_pinctrl_dev_pm_ops
,
857 .probe
= stmfx_pinctrl_probe
,
858 .remove
= stmfx_pinctrl_remove
,
860 module_platform_driver(stmfx_pinctrl_driver
);
862 MODULE_DESCRIPTION("STMFX pinctrl/GPIO driver");
863 MODULE_AUTHOR("Amelie Delaunay <amelie.delaunay@st.com>");
864 MODULE_LICENSE("GPL v2");