staging: erofs: integrate decompression inplace
[linux/fpc-iii.git] / drivers / tty / serial / serial_mctrl_gpio.c
blob39ed56214cd32c32447ef30baf1997dc27d2a003
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Helpers for controlling modem lines via GPIO
5 * Copyright (C) 2014 Paratronic S.A.
6 */
8 #include <linux/err.h>
9 #include <linux/device.h>
10 #include <linux/irq.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/termios.h>
13 #include <linux/serial_core.h>
14 #include <linux/module.h>
16 #include "serial_mctrl_gpio.h"
18 struct mctrl_gpios {
19 struct uart_port *port;
20 struct gpio_desc *gpio[UART_GPIO_MAX];
21 int irq[UART_GPIO_MAX];
22 unsigned int mctrl_prev;
23 bool mctrl_on;
26 static const struct {
27 const char *name;
28 unsigned int mctrl;
29 bool dir_out;
30 } mctrl_gpios_desc[UART_GPIO_MAX] = {
31 { "cts", TIOCM_CTS, false, },
32 { "dsr", TIOCM_DSR, false, },
33 { "dcd", TIOCM_CD, false, },
34 { "rng", TIOCM_RNG, false, },
35 { "rts", TIOCM_RTS, true, },
36 { "dtr", TIOCM_DTR, true, },
39 void mctrl_gpio_set(struct mctrl_gpios *gpios, unsigned int mctrl)
41 enum mctrl_gpio_idx i;
42 struct gpio_desc *desc_array[UART_GPIO_MAX];
43 DECLARE_BITMAP(values, UART_GPIO_MAX);
44 unsigned int count = 0;
46 if (gpios == NULL)
47 return;
49 for (i = 0; i < UART_GPIO_MAX; i++)
50 if (gpios->gpio[i] && mctrl_gpios_desc[i].dir_out) {
51 desc_array[count] = gpios->gpio[i];
52 __assign_bit(count, values,
53 mctrl & mctrl_gpios_desc[i].mctrl);
54 count++;
56 gpiod_set_array_value(count, desc_array, NULL, values);
58 EXPORT_SYMBOL_GPL(mctrl_gpio_set);
60 struct gpio_desc *mctrl_gpio_to_gpiod(struct mctrl_gpios *gpios,
61 enum mctrl_gpio_idx gidx)
63 return gpios->gpio[gidx];
65 EXPORT_SYMBOL_GPL(mctrl_gpio_to_gpiod);
67 unsigned int mctrl_gpio_get(struct mctrl_gpios *gpios, unsigned int *mctrl)
69 enum mctrl_gpio_idx i;
71 if (gpios == NULL)
72 return *mctrl;
74 for (i = 0; i < UART_GPIO_MAX; i++) {
75 if (gpios->gpio[i] && !mctrl_gpios_desc[i].dir_out) {
76 if (gpiod_get_value(gpios->gpio[i]))
77 *mctrl |= mctrl_gpios_desc[i].mctrl;
78 else
79 *mctrl &= ~mctrl_gpios_desc[i].mctrl;
83 return *mctrl;
85 EXPORT_SYMBOL_GPL(mctrl_gpio_get);
87 unsigned int
88 mctrl_gpio_get_outputs(struct mctrl_gpios *gpios, unsigned int *mctrl)
90 enum mctrl_gpio_idx i;
92 if (gpios == NULL)
93 return *mctrl;
95 for (i = 0; i < UART_GPIO_MAX; i++) {
96 if (gpios->gpio[i] && mctrl_gpios_desc[i].dir_out) {
97 if (gpiod_get_value(gpios->gpio[i]))
98 *mctrl |= mctrl_gpios_desc[i].mctrl;
99 else
100 *mctrl &= ~mctrl_gpios_desc[i].mctrl;
104 return *mctrl;
106 EXPORT_SYMBOL_GPL(mctrl_gpio_get_outputs);
108 struct mctrl_gpios *mctrl_gpio_init_noauto(struct device *dev, unsigned int idx)
110 struct mctrl_gpios *gpios;
111 enum mctrl_gpio_idx i;
113 gpios = devm_kzalloc(dev, sizeof(*gpios), GFP_KERNEL);
114 if (!gpios)
115 return ERR_PTR(-ENOMEM);
117 for (i = 0; i < UART_GPIO_MAX; i++) {
118 enum gpiod_flags flags;
120 if (mctrl_gpios_desc[i].dir_out)
121 flags = GPIOD_OUT_LOW;
122 else
123 flags = GPIOD_IN;
125 gpios->gpio[i] =
126 devm_gpiod_get_index_optional(dev,
127 mctrl_gpios_desc[i].name,
128 idx, flags);
130 if (IS_ERR(gpios->gpio[i]))
131 return ERR_CAST(gpios->gpio[i]);
134 return gpios;
136 EXPORT_SYMBOL_GPL(mctrl_gpio_init_noauto);
138 #define MCTRL_ANY_DELTA (TIOCM_RI | TIOCM_DSR | TIOCM_CD | TIOCM_CTS)
139 static irqreturn_t mctrl_gpio_irq_handle(int irq, void *context)
141 struct mctrl_gpios *gpios = context;
142 struct uart_port *port = gpios->port;
143 u32 mctrl = gpios->mctrl_prev;
144 u32 mctrl_diff;
145 unsigned long flags;
147 mctrl_gpio_get(gpios, &mctrl);
149 spin_lock_irqsave(&port->lock, flags);
151 mctrl_diff = mctrl ^ gpios->mctrl_prev;
152 gpios->mctrl_prev = mctrl;
154 if (mctrl_diff & MCTRL_ANY_DELTA && port->state != NULL) {
155 if ((mctrl_diff & mctrl) & TIOCM_RI)
156 port->icount.rng++;
158 if ((mctrl_diff & mctrl) & TIOCM_DSR)
159 port->icount.dsr++;
161 if (mctrl_diff & TIOCM_CD)
162 uart_handle_dcd_change(port, mctrl & TIOCM_CD);
164 if (mctrl_diff & TIOCM_CTS)
165 uart_handle_cts_change(port, mctrl & TIOCM_CTS);
167 wake_up_interruptible(&port->state->port.delta_msr_wait);
170 spin_unlock_irqrestore(&port->lock, flags);
172 return IRQ_HANDLED;
175 struct mctrl_gpios *mctrl_gpio_init(struct uart_port *port, unsigned int idx)
177 struct mctrl_gpios *gpios;
178 enum mctrl_gpio_idx i;
180 gpios = mctrl_gpio_init_noauto(port->dev, idx);
181 if (IS_ERR(gpios))
182 return gpios;
184 gpios->port = port;
186 for (i = 0; i < UART_GPIO_MAX; ++i) {
187 int ret;
189 if (!gpios->gpio[i] || mctrl_gpios_desc[i].dir_out)
190 continue;
192 ret = gpiod_to_irq(gpios->gpio[i]);
193 if (ret <= 0) {
194 dev_err(port->dev,
195 "failed to find corresponding irq for %s (idx=%d, err=%d)\n",
196 mctrl_gpios_desc[i].name, idx, ret);
197 return ERR_PTR(ret);
199 gpios->irq[i] = ret;
201 /* irqs should only be enabled in .enable_ms */
202 irq_set_status_flags(gpios->irq[i], IRQ_NOAUTOEN);
204 ret = devm_request_irq(port->dev, gpios->irq[i],
205 mctrl_gpio_irq_handle,
206 IRQ_TYPE_EDGE_BOTH, dev_name(port->dev),
207 gpios);
208 if (ret) {
209 /* alternatively implement polling */
210 dev_err(port->dev,
211 "failed to request irq for %s (idx=%d, err=%d)\n",
212 mctrl_gpios_desc[i].name, idx, ret);
213 return ERR_PTR(ret);
217 return gpios;
219 EXPORT_SYMBOL_GPL(mctrl_gpio_init);
221 void mctrl_gpio_free(struct device *dev, struct mctrl_gpios *gpios)
223 enum mctrl_gpio_idx i;
225 if (gpios == NULL)
226 return;
228 for (i = 0; i < UART_GPIO_MAX; i++) {
229 if (gpios->irq[i])
230 devm_free_irq(gpios->port->dev, gpios->irq[i], gpios);
232 if (gpios->gpio[i])
233 devm_gpiod_put(dev, gpios->gpio[i]);
235 devm_kfree(dev, gpios);
237 EXPORT_SYMBOL_GPL(mctrl_gpio_free);
239 void mctrl_gpio_enable_ms(struct mctrl_gpios *gpios)
241 enum mctrl_gpio_idx i;
243 if (gpios == NULL)
244 return;
246 /* .enable_ms may be called multiple times */
247 if (gpios->mctrl_on)
248 return;
250 gpios->mctrl_on = true;
252 /* get initial status of modem lines GPIOs */
253 mctrl_gpio_get(gpios, &gpios->mctrl_prev);
255 for (i = 0; i < UART_GPIO_MAX; ++i) {
256 if (!gpios->irq[i])
257 continue;
259 enable_irq(gpios->irq[i]);
262 EXPORT_SYMBOL_GPL(mctrl_gpio_enable_ms);
264 void mctrl_gpio_disable_ms(struct mctrl_gpios *gpios)
266 enum mctrl_gpio_idx i;
268 if (gpios == NULL)
269 return;
271 if (!gpios->mctrl_on)
272 return;
274 gpios->mctrl_on = false;
276 for (i = 0; i < UART_GPIO_MAX; ++i) {
277 if (!gpios->irq[i])
278 continue;
280 disable_irq(gpios->irq[i]);
283 EXPORT_SYMBOL_GPL(mctrl_gpio_disable_ms);
285 MODULE_LICENSE("GPL");