1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3 * Helpers for controlling modem lines via GPIO
4 *
5 * Copyright (C) 2014 Paratronic S.A.
6 */
7
8 #ifndef __SERIAL_MCTRL_GPIO__
9 #define __SERIAL_MCTRL_GPIO__
10
11 #include <linux/err.h>
12 #include <linux/device.h>
13 #include <linux/gpio/consumer.h>
14
15 struct uart_port;
16
17 enum mctrl_gpio_idx {
18 UART_GPIO_CTS,
19 UART_GPIO_DSR,
20 UART_GPIO_DCD,
21 UART_GPIO_RNG,
22 UART_GPIO_RI = UART_GPIO_RNG,
23 UART_GPIO_RTS,
24 UART_GPIO_DTR,
25 UART_GPIO_MAX,
26 };
27
28 /*
29 * Opaque descriptor for modem lines controlled by GPIOs
30 */
31 struct mctrl_gpios;
32
33 #ifdef CONFIG_GPIOLIB
34
35 /*
36 * Set state of the modem control output lines via GPIOs.
37 */
38 void mctrl_gpio_set(struct mctrl_gpios *gpios, unsigned int mctrl);
39
40 /*
41 * Get state of the modem control input lines from GPIOs.
42 * The mctrl flags are updated and returned.
43 */
44 unsigned int mctrl_gpio_get(struct mctrl_gpios *gpios, unsigned int *mctrl);
45
46 /*
47 * Get state of the modem control output lines from GPIOs.
48 * The mctrl flags are updated and returned.
49 */
50 unsigned int
51 mctrl_gpio_get_outputs(struct mctrl_gpios *gpios, unsigned int *mctrl);
52
53 /*
54 * Returns the associated struct gpio_desc to the modem line gidx
55 */
56 struct gpio_desc *mctrl_gpio_to_gpiod(struct mctrl_gpios *gpios,
57 enum mctrl_gpio_idx gidx);
58
59 /*
60 * Request and set direction of modem control line GPIOs and set up irq
61 * handling.
62 * devm_* functions are used, so there's no need to call mctrl_gpio_free().
63 * Returns a pointer to the allocated mctrl structure if ok, -ENOMEM on
64 * allocation error.
65 */
66 struct mctrl_gpios *mctrl_gpio_init(struct uart_port *port, unsigned int idx);
67
68 /*
69 * Request and set direction of modem control line GPIOs.
70 * devm_* functions are used, so there's no need to call mctrl_gpio_free().
71 * Returns a pointer to the allocated mctrl structure if ok, -ENOMEM on
72 * allocation error.
73 */
74 struct mctrl_gpios *mctrl_gpio_init_noauto(struct device *dev,
75 unsigned int idx);
76
77 /*
78 * Free the mctrl_gpios structure.
79 * Normally, this function will not be called, as the GPIOs will
80 * be disposed of by the resource management code.
81 */
82 void mctrl_gpio_free(struct device *dev, struct mctrl_gpios *gpios);
83
84 /*
85 * Enable gpio interrupts to report status line changes.
86 */
87 void mctrl_gpio_enable_ms(struct mctrl_gpios *gpios);
88
89 /*
90 * Disable gpio interrupts to report status line changes, and block until
91 * any corresponding IRQ is processed
92 */
93 void mctrl_gpio_disable_ms_sync(struct mctrl_gpios *gpios);
94
95 /*
96 * Disable gpio interrupts to report status line changes, and return
97 * immediately
98 */
99 void mctrl_gpio_disable_ms_no_sync(struct mctrl_gpios *gpios);
100
101 /*
102 * Enable gpio wakeup interrupts to enable wake up source.
103 */
104 void mctrl_gpio_enable_irq_wake(struct mctrl_gpios *gpios);
105
106 /*
107 * Disable gpio wakeup interrupts to enable wake up source.
108 */
109 void mctrl_gpio_disable_irq_wake(struct mctrl_gpios *gpios);
110
111 #else /* GPIOLIB */
112
113 static inline
mctrl_gpio_set(struct mctrl_gpios * gpios,unsigned int mctrl)114 void mctrl_gpio_set(struct mctrl_gpios *gpios, unsigned int mctrl)
115 {
116 }
117
118 static inline
mctrl_gpio_get(struct mctrl_gpios * gpios,unsigned int * mctrl)119 unsigned int mctrl_gpio_get(struct mctrl_gpios *gpios, unsigned int *mctrl)
120 {
121 return *mctrl;
122 }
123
124 static inline unsigned int
mctrl_gpio_get_outputs(struct mctrl_gpios * gpios,unsigned int * mctrl)125 mctrl_gpio_get_outputs(struct mctrl_gpios *gpios, unsigned int *mctrl)
126 {
127 return *mctrl;
128 }
129
130 static inline
mctrl_gpio_to_gpiod(struct mctrl_gpios * gpios,enum mctrl_gpio_idx gidx)131 struct gpio_desc *mctrl_gpio_to_gpiod(struct mctrl_gpios *gpios,
132 enum mctrl_gpio_idx gidx)
133 {
134 return NULL;
135 }
136
137 static inline
mctrl_gpio_init(struct uart_port * port,unsigned int idx)138 struct mctrl_gpios *mctrl_gpio_init(struct uart_port *port, unsigned int idx)
139 {
140 return NULL;
141 }
142
143 static inline
mctrl_gpio_init_noauto(struct device * dev,unsigned int idx)144 struct mctrl_gpios *mctrl_gpio_init_noauto(struct device *dev, unsigned int idx)
145 {
146 return NULL;
147 }
148
149 static inline
mctrl_gpio_free(struct device * dev,struct mctrl_gpios * gpios)150 void mctrl_gpio_free(struct device *dev, struct mctrl_gpios *gpios)
151 {
152 }
153
mctrl_gpio_enable_ms(struct mctrl_gpios * gpios)154 static inline void mctrl_gpio_enable_ms(struct mctrl_gpios *gpios)
155 {
156 }
157
mctrl_gpio_disable_ms_sync(struct mctrl_gpios * gpios)158 static inline void mctrl_gpio_disable_ms_sync(struct mctrl_gpios *gpios)
159 {
160 }
161
mctrl_gpio_disable_ms_no_sync(struct mctrl_gpios * gpios)162 static inline void mctrl_gpio_disable_ms_no_sync(struct mctrl_gpios *gpios)
163 {
164 }
165
mctrl_gpio_enable_irq_wake(struct mctrl_gpios * gpios)166 static inline void mctrl_gpio_enable_irq_wake(struct mctrl_gpios *gpios)
167 {
168 }
169
mctrl_gpio_disable_irq_wake(struct mctrl_gpios * gpios)170 static inline void mctrl_gpio_disable_irq_wake(struct mctrl_gpios *gpios)
171 {
172 }
173
174 #endif /* GPIOLIB */
175
176 #endif
177