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>
24 #include <linux/property.h>
26 #include "serial_mctrl_gpio.h"
29 struct uart_port *port;
30 struct gpio_desc *gpio[UART_GPIO_MAX];
31 int irq[UART_GPIO_MAX];
32 unsigned int mctrl_prev;
40 } mctrl_gpios_desc[UART_GPIO_MAX] = {
41 { "cts", TIOCM_CTS, false, },
42 { "dsr", TIOCM_DSR, false, },
43 { "dcd", TIOCM_CD, false, },
44 { "rng", TIOCM_RNG, false, },
45 { "rts", TIOCM_RTS, true, },
46 { "dtr", TIOCM_DTR, true, },
49 void mctrl_gpio_set(struct mctrl_gpios *gpios, unsigned int mctrl)
51 enum mctrl_gpio_idx i;
52 struct gpio_desc *desc_array[UART_GPIO_MAX];
53 int value_array[UART_GPIO_MAX];
54 unsigned int count = 0;
59 for (i = 0; i < UART_GPIO_MAX; i++)
60 if (gpios->gpio[i] && mctrl_gpios_desc[i].dir_out) {
61 desc_array[count] = gpios->gpio[i];
62 value_array[count] = !!(mctrl & mctrl_gpios_desc[i].mctrl);
65 gpiod_set_array_value(count, desc_array, value_array);
67 EXPORT_SYMBOL_GPL(mctrl_gpio_set);
69 struct gpio_desc *mctrl_gpio_to_gpiod(struct mctrl_gpios *gpios,
70 enum mctrl_gpio_idx gidx)
75 return gpios->gpio[gidx];
77 EXPORT_SYMBOL_GPL(mctrl_gpio_to_gpiod);
79 unsigned int mctrl_gpio_get(struct mctrl_gpios *gpios, unsigned int *mctrl)
81 enum mctrl_gpio_idx i;
86 for (i = 0; i < UART_GPIO_MAX; i++) {
87 if (gpios->gpio[i] && !mctrl_gpios_desc[i].dir_out) {
88 if (gpiod_get_value(gpios->gpio[i]))
89 *mctrl |= mctrl_gpios_desc[i].mctrl;
91 *mctrl &= ~mctrl_gpios_desc[i].mctrl;
97 EXPORT_SYMBOL_GPL(mctrl_gpio_get);
100 mctrl_gpio_get_outputs(struct mctrl_gpios *gpios, unsigned int *mctrl)
102 enum mctrl_gpio_idx i;
107 for (i = 0; i < UART_GPIO_MAX; i++) {
108 if (gpios->gpio[i] && mctrl_gpios_desc[i].dir_out) {
109 if (gpiod_get_value(gpios->gpio[i]))
110 *mctrl |= mctrl_gpios_desc[i].mctrl;
112 *mctrl &= ~mctrl_gpios_desc[i].mctrl;
118 EXPORT_SYMBOL_GPL(mctrl_gpio_get_outputs);
120 struct mctrl_gpios *mctrl_gpio_init_noauto(struct device *dev, unsigned int idx)
122 struct mctrl_gpios *gpios;
123 enum mctrl_gpio_idx i;
125 gpios = devm_kzalloc(dev, sizeof(*gpios), GFP_KERNEL);
127 return ERR_PTR(-ENOMEM);
129 for (i = 0; i < UART_GPIO_MAX; i++) {
130 enum gpiod_flags flags;
134 /* Check if GPIO property exists and continue if not */
135 gpio_str = kasprintf(GFP_KERNEL, "%s-gpios",
136 mctrl_gpios_desc[i].name);
140 present = device_property_present(dev, gpio_str);
145 if (mctrl_gpios_desc[i].dir_out)
146 flags = GPIOD_OUT_LOW;
151 devm_gpiod_get_index_optional(dev,
152 mctrl_gpios_desc[i].name,
155 if (IS_ERR(gpios->gpio[i]))
156 return ERR_CAST(gpios->gpio[i]);
161 EXPORT_SYMBOL_GPL(mctrl_gpio_init_noauto);
163 #define MCTRL_ANY_DELTA (TIOCM_RI | TIOCM_DSR | TIOCM_CD | TIOCM_CTS)
164 static irqreturn_t mctrl_gpio_irq_handle(int irq, void *context)
166 struct mctrl_gpios *gpios = context;
167 struct uart_port *port = gpios->port;
168 u32 mctrl = gpios->mctrl_prev;
172 mctrl_gpio_get(gpios, &mctrl);
174 spin_lock_irqsave(&port->lock, flags);
176 mctrl_diff = mctrl ^ gpios->mctrl_prev;
177 gpios->mctrl_prev = mctrl;
179 if (mctrl_diff & MCTRL_ANY_DELTA && port->state != NULL) {
180 if ((mctrl_diff & mctrl) & TIOCM_RI)
183 if ((mctrl_diff & mctrl) & TIOCM_DSR)
186 if (mctrl_diff & TIOCM_CD)
187 uart_handle_dcd_change(port, mctrl & TIOCM_CD);
189 if (mctrl_diff & TIOCM_CTS)
190 uart_handle_cts_change(port, mctrl & TIOCM_CTS);
192 wake_up_interruptible(&port->state->port.delta_msr_wait);
195 spin_unlock_irqrestore(&port->lock, flags);
200 struct mctrl_gpios *mctrl_gpio_init(struct uart_port *port, unsigned int idx)
202 struct mctrl_gpios *gpios;
203 enum mctrl_gpio_idx i;
205 gpios = mctrl_gpio_init_noauto(port->dev, idx);
211 for (i = 0; i < UART_GPIO_MAX; ++i) {
214 if (!gpios->gpio[i] || mctrl_gpios_desc[i].dir_out)
217 ret = gpiod_to_irq(gpios->gpio[i]);
220 "failed to find corresponding irq for %s (idx=%d, err=%d)\n",
221 mctrl_gpios_desc[i].name, idx, ret);
226 /* irqs should only be enabled in .enable_ms */
227 irq_set_status_flags(gpios->irq[i], IRQ_NOAUTOEN);
229 ret = devm_request_irq(port->dev, gpios->irq[i],
230 mctrl_gpio_irq_handle,
231 IRQ_TYPE_EDGE_BOTH, dev_name(port->dev),
234 /* alternatively implement polling */
236 "failed to request irq for %s (idx=%d, err=%d)\n",
237 mctrl_gpios_desc[i].name, idx, ret);
244 EXPORT_SYMBOL_GPL(mctrl_gpio_init);
246 void mctrl_gpio_free(struct device *dev, struct mctrl_gpios *gpios)
248 enum mctrl_gpio_idx i;
253 for (i = 0; i < UART_GPIO_MAX; i++) {
255 devm_free_irq(gpios->port->dev, gpios->irq[i], gpios);
258 devm_gpiod_put(dev, gpios->gpio[i]);
260 devm_kfree(dev, gpios);
262 EXPORT_SYMBOL_GPL(mctrl_gpio_free);
264 void mctrl_gpio_enable_ms(struct mctrl_gpios *gpios)
266 enum mctrl_gpio_idx i;
271 /* .enable_ms may be called multiple times */
275 gpios->mctrl_on = true;
277 /* get initial status of modem lines GPIOs */
278 mctrl_gpio_get(gpios, &gpios->mctrl_prev);
280 for (i = 0; i < UART_GPIO_MAX; ++i) {
284 enable_irq(gpios->irq[i]);
287 EXPORT_SYMBOL_GPL(mctrl_gpio_enable_ms);
289 void mctrl_gpio_disable_ms(struct mctrl_gpios *gpios)
291 enum mctrl_gpio_idx i;
296 if (!gpios->mctrl_on)
299 gpios->mctrl_on = false;
301 for (i = 0; i < UART_GPIO_MAX; ++i) {
305 disable_irq(gpios->irq[i]);
308 EXPORT_SYMBOL_GPL(mctrl_gpio_disable_ms);
310 MODULE_LICENSE("GPL");