2 * Helpers for controlling modem lines via GPIO
4 * Copyright (C) 2014 Paratronic S.A.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #include <linux/err.h>
18 #include <linux/device.h>
19 #include <linux/irq.h>
20 #include <linux/gpio/consumer.h>
21 #include <linux/termios.h>
22 #include <linux/serial_core.h>
23 #include <linux/module.h>
25 #include "serial_mctrl_gpio.h"
28 struct uart_port *port;
29 struct gpio_desc *gpio[UART_GPIO_MAX];
30 int irq[UART_GPIO_MAX];
31 unsigned int mctrl_prev;
39 } mctrl_gpios_desc[UART_GPIO_MAX] = {
40 { "cts", TIOCM_CTS, false, },
41 { "dsr", TIOCM_DSR, false, },
42 { "dcd", TIOCM_CD, false, },
43 { "rng", TIOCM_RNG, false, },
44 { "rts", TIOCM_RTS, true, },
45 { "dtr", TIOCM_DTR, true, },
48 void mctrl_gpio_set(struct mctrl_gpios *gpios, unsigned int mctrl)
50 enum mctrl_gpio_idx i;
51 struct gpio_desc *desc_array[UART_GPIO_MAX];
52 int value_array[UART_GPIO_MAX];
53 unsigned int count = 0;
58 for (i = 0; i < UART_GPIO_MAX; i++)
59 if (gpios->gpio[i] && mctrl_gpios_desc[i].dir_out) {
60 desc_array[count] = gpios->gpio[i];
61 value_array[count] = !!(mctrl & mctrl_gpios_desc[i].mctrl);
64 gpiod_set_array_value(count, desc_array, value_array);
66 EXPORT_SYMBOL_GPL(mctrl_gpio_set);
68 struct gpio_desc *mctrl_gpio_to_gpiod(struct mctrl_gpios *gpios,
69 enum mctrl_gpio_idx gidx)
74 return gpios->gpio[gidx];
76 EXPORT_SYMBOL_GPL(mctrl_gpio_to_gpiod);
78 unsigned int mctrl_gpio_get(struct mctrl_gpios *gpios, unsigned int *mctrl)
80 enum mctrl_gpio_idx i;
85 for (i = 0; i < UART_GPIO_MAX; i++) {
86 if (gpios->gpio[i] && !mctrl_gpios_desc[i].dir_out) {
87 if (gpiod_get_value(gpios->gpio[i]))
88 *mctrl |= mctrl_gpios_desc[i].mctrl;
90 *mctrl &= ~mctrl_gpios_desc[i].mctrl;
96 EXPORT_SYMBOL_GPL(mctrl_gpio_get);
99 mctrl_gpio_get_outputs(struct mctrl_gpios *gpios, unsigned int *mctrl)
101 enum mctrl_gpio_idx i;
106 for (i = 0; i < UART_GPIO_MAX; i++) {
107 if (gpios->gpio[i] && mctrl_gpios_desc[i].dir_out) {
108 if (gpiod_get_value(gpios->gpio[i]))
109 *mctrl |= mctrl_gpios_desc[i].mctrl;
111 *mctrl &= ~mctrl_gpios_desc[i].mctrl;
117 EXPORT_SYMBOL_GPL(mctrl_gpio_get_outputs);
119 struct mctrl_gpios *mctrl_gpio_init_noauto(struct device *dev, unsigned int idx)
121 struct mctrl_gpios *gpios;
122 enum mctrl_gpio_idx i;
124 gpios = devm_kzalloc(dev, sizeof(*gpios), GFP_KERNEL);
126 return ERR_PTR(-ENOMEM);
128 for (i = 0; i < UART_GPIO_MAX; i++) {
129 enum gpiod_flags flags;
131 if (mctrl_gpios_desc[i].dir_out)
132 flags = GPIOD_OUT_LOW;
137 devm_gpiod_get_index_optional(dev,
138 mctrl_gpios_desc[i].name,
141 if (IS_ERR(gpios->gpio[i]))
142 return ERR_CAST(gpios->gpio[i]);
147 EXPORT_SYMBOL_GPL(mctrl_gpio_init_noauto);
149 #define MCTRL_ANY_DELTA (TIOCM_RI | TIOCM_DSR | TIOCM_CD | TIOCM_CTS)
150 static irqreturn_t mctrl_gpio_irq_handle(int irq, void *context)
152 struct mctrl_gpios *gpios = context;
153 struct uart_port *port = gpios->port;
154 u32 mctrl = gpios->mctrl_prev;
158 mctrl_gpio_get(gpios, &mctrl);
160 spin_lock_irqsave(&port->lock, flags);
162 mctrl_diff = mctrl ^ gpios->mctrl_prev;
163 gpios->mctrl_prev = mctrl;
165 if (mctrl_diff & MCTRL_ANY_DELTA && port->state != NULL) {
166 if ((mctrl_diff & mctrl) & TIOCM_RI)
169 if ((mctrl_diff & mctrl) & TIOCM_DSR)
172 if (mctrl_diff & TIOCM_CD)
173 uart_handle_dcd_change(port, mctrl & TIOCM_CD);
175 if (mctrl_diff & TIOCM_CTS)
176 uart_handle_cts_change(port, mctrl & TIOCM_CTS);
178 wake_up_interruptible(&port->state->port.delta_msr_wait);
181 spin_unlock_irqrestore(&port->lock, flags);
186 struct mctrl_gpios *mctrl_gpio_init(struct uart_port *port, unsigned int idx)
188 struct mctrl_gpios *gpios;
189 enum mctrl_gpio_idx i;
191 gpios = mctrl_gpio_init_noauto(port->dev, idx);
197 for (i = 0; i < UART_GPIO_MAX; ++i) {
200 if (!gpios->gpio[i] || mctrl_gpios_desc[i].dir_out)
203 ret = gpiod_to_irq(gpios->gpio[i]);
206 "failed to find corresponding irq for %s (idx=%d, err=%d)\n",
207 mctrl_gpios_desc[i].name, idx, ret);
212 /* irqs should only be enabled in .enable_ms */
213 irq_set_status_flags(gpios->irq[i], IRQ_NOAUTOEN);
215 ret = devm_request_irq(port->dev, gpios->irq[i],
216 mctrl_gpio_irq_handle,
217 IRQ_TYPE_EDGE_BOTH, dev_name(port->dev),
220 /* alternatively implement polling */
222 "failed to request irq for %s (idx=%d, err=%d)\n",
223 mctrl_gpios_desc[i].name, idx, ret);
230 EXPORT_SYMBOL_GPL(mctrl_gpio_init);
232 void mctrl_gpio_free(struct device *dev, struct mctrl_gpios *gpios)
234 enum mctrl_gpio_idx i;
239 for (i = 0; i < UART_GPIO_MAX; i++) {
241 devm_free_irq(gpios->port->dev, gpios->irq[i], gpios);
244 devm_gpiod_put(dev, gpios->gpio[i]);
246 devm_kfree(dev, gpios);
248 EXPORT_SYMBOL_GPL(mctrl_gpio_free);
250 void mctrl_gpio_enable_ms(struct mctrl_gpios *gpios)
252 enum mctrl_gpio_idx i;
257 /* .enable_ms may be called multiple times */
261 gpios->mctrl_on = true;
263 /* get initial status of modem lines GPIOs */
264 mctrl_gpio_get(gpios, &gpios->mctrl_prev);
266 for (i = 0; i < UART_GPIO_MAX; ++i) {
270 enable_irq(gpios->irq[i]);
273 EXPORT_SYMBOL_GPL(mctrl_gpio_enable_ms);
275 void mctrl_gpio_disable_ms(struct mctrl_gpios *gpios)
277 enum mctrl_gpio_idx i;
282 if (!gpios->mctrl_on)
285 gpios->mctrl_on = false;
287 for (i = 0; i < UART_GPIO_MAX; ++i) {
291 disable_irq(gpios->irq[i]);
294 EXPORT_SYMBOL_GPL(mctrl_gpio_disable_ms);
296 MODULE_LICENSE("GPL");