Linux 4.16.11
[linux/fpc-iii.git] / drivers / tty / serial / serial_mctrl_gpio.c
blob1c06325beacaeb3a6e19011385fcc379ef16ce8e
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 int value_array[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 value_array[count] = !!(mctrl & mctrl_gpios_desc[i].mctrl);
53 count++;
55 gpiod_set_array_value(count, desc_array, value_array);
57 EXPORT_SYMBOL_GPL(mctrl_gpio_set);
59 struct gpio_desc *mctrl_gpio_to_gpiod(struct mctrl_gpios *gpios,
60 enum mctrl_gpio_idx gidx)
62 return gpios->gpio[gidx];
64 EXPORT_SYMBOL_GPL(mctrl_gpio_to_gpiod);
66 unsigned int mctrl_gpio_get(struct mctrl_gpios *gpios, unsigned int *mctrl)
68 enum mctrl_gpio_idx i;
70 if (gpios == NULL)
71 return *mctrl;
73 for (i = 0; i < UART_GPIO_MAX; i++) {
74 if (gpios->gpio[i] && !mctrl_gpios_desc[i].dir_out) {
75 if (gpiod_get_value(gpios->gpio[i]))
76 *mctrl |= mctrl_gpios_desc[i].mctrl;
77 else
78 *mctrl &= ~mctrl_gpios_desc[i].mctrl;
82 return *mctrl;
84 EXPORT_SYMBOL_GPL(mctrl_gpio_get);
86 unsigned int
87 mctrl_gpio_get_outputs(struct mctrl_gpios *gpios, unsigned int *mctrl)
89 enum mctrl_gpio_idx i;
91 if (gpios == NULL)
92 return *mctrl;
94 for (i = 0; i < UART_GPIO_MAX; i++) {
95 if (gpios->gpio[i] && mctrl_gpios_desc[i].dir_out) {
96 if (gpiod_get_value(gpios->gpio[i]))
97 *mctrl |= mctrl_gpios_desc[i].mctrl;
98 else
99 *mctrl &= ~mctrl_gpios_desc[i].mctrl;
103 return *mctrl;
105 EXPORT_SYMBOL_GPL(mctrl_gpio_get_outputs);
107 struct mctrl_gpios *mctrl_gpio_init_noauto(struct device *dev, unsigned int idx)
109 struct mctrl_gpios *gpios;
110 enum mctrl_gpio_idx i;
112 gpios = devm_kzalloc(dev, sizeof(*gpios), GFP_KERNEL);
113 if (!gpios)
114 return ERR_PTR(-ENOMEM);
116 for (i = 0; i < UART_GPIO_MAX; i++) {
117 enum gpiod_flags flags;
119 if (mctrl_gpios_desc[i].dir_out)
120 flags = GPIOD_OUT_LOW;
121 else
122 flags = GPIOD_IN;
124 gpios->gpio[i] =
125 devm_gpiod_get_index_optional(dev,
126 mctrl_gpios_desc[i].name,
127 idx, flags);
129 if (IS_ERR(gpios->gpio[i]))
130 return ERR_CAST(gpios->gpio[i]);
133 return gpios;
135 EXPORT_SYMBOL_GPL(mctrl_gpio_init_noauto);
137 #define MCTRL_ANY_DELTA (TIOCM_RI | TIOCM_DSR | TIOCM_CD | TIOCM_CTS)
138 static irqreturn_t mctrl_gpio_irq_handle(int irq, void *context)
140 struct mctrl_gpios *gpios = context;
141 struct uart_port *port = gpios->port;
142 u32 mctrl = gpios->mctrl_prev;
143 u32 mctrl_diff;
144 unsigned long flags;
146 mctrl_gpio_get(gpios, &mctrl);
148 spin_lock_irqsave(&port->lock, flags);
150 mctrl_diff = mctrl ^ gpios->mctrl_prev;
151 gpios->mctrl_prev = mctrl;
153 if (mctrl_diff & MCTRL_ANY_DELTA && port->state != NULL) {
154 if ((mctrl_diff & mctrl) & TIOCM_RI)
155 port->icount.rng++;
157 if ((mctrl_diff & mctrl) & TIOCM_DSR)
158 port->icount.dsr++;
160 if (mctrl_diff & TIOCM_CD)
161 uart_handle_dcd_change(port, mctrl & TIOCM_CD);
163 if (mctrl_diff & TIOCM_CTS)
164 uart_handle_cts_change(port, mctrl & TIOCM_CTS);
166 wake_up_interruptible(&port->state->port.delta_msr_wait);
169 spin_unlock_irqrestore(&port->lock, flags);
171 return IRQ_HANDLED;
174 struct mctrl_gpios *mctrl_gpio_init(struct uart_port *port, unsigned int idx)
176 struct mctrl_gpios *gpios;
177 enum mctrl_gpio_idx i;
179 gpios = mctrl_gpio_init_noauto(port->dev, idx);
180 if (IS_ERR(gpios))
181 return gpios;
183 gpios->port = port;
185 for (i = 0; i < UART_GPIO_MAX; ++i) {
186 int ret;
188 if (!gpios->gpio[i] || mctrl_gpios_desc[i].dir_out)
189 continue;
191 ret = gpiod_to_irq(gpios->gpio[i]);
192 if (ret <= 0) {
193 dev_err(port->dev,
194 "failed to find corresponding irq for %s (idx=%d, err=%d)\n",
195 mctrl_gpios_desc[i].name, idx, ret);
196 return ERR_PTR(ret);
198 gpios->irq[i] = ret;
200 /* irqs should only be enabled in .enable_ms */
201 irq_set_status_flags(gpios->irq[i], IRQ_NOAUTOEN);
203 ret = devm_request_irq(port->dev, gpios->irq[i],
204 mctrl_gpio_irq_handle,
205 IRQ_TYPE_EDGE_BOTH, dev_name(port->dev),
206 gpios);
207 if (ret) {
208 /* alternatively implement polling */
209 dev_err(port->dev,
210 "failed to request irq for %s (idx=%d, err=%d)\n",
211 mctrl_gpios_desc[i].name, idx, ret);
212 return ERR_PTR(ret);
216 return gpios;
218 EXPORT_SYMBOL_GPL(mctrl_gpio_init);
220 void mctrl_gpio_free(struct device *dev, struct mctrl_gpios *gpios)
222 enum mctrl_gpio_idx i;
224 if (gpios == NULL)
225 return;
227 for (i = 0; i < UART_GPIO_MAX; i++) {
228 if (gpios->irq[i])
229 devm_free_irq(gpios->port->dev, gpios->irq[i], gpios);
231 if (gpios->gpio[i])
232 devm_gpiod_put(dev, gpios->gpio[i]);
234 devm_kfree(dev, gpios);
236 EXPORT_SYMBOL_GPL(mctrl_gpio_free);
238 void mctrl_gpio_enable_ms(struct mctrl_gpios *gpios)
240 enum mctrl_gpio_idx i;
242 if (gpios == NULL)
243 return;
245 /* .enable_ms may be called multiple times */
246 if (gpios->mctrl_on)
247 return;
249 gpios->mctrl_on = true;
251 /* get initial status of modem lines GPIOs */
252 mctrl_gpio_get(gpios, &gpios->mctrl_prev);
254 for (i = 0; i < UART_GPIO_MAX; ++i) {
255 if (!gpios->irq[i])
256 continue;
258 enable_irq(gpios->irq[i]);
261 EXPORT_SYMBOL_GPL(mctrl_gpio_enable_ms);
263 void mctrl_gpio_disable_ms(struct mctrl_gpios *gpios)
265 enum mctrl_gpio_idx i;
267 if (gpios == NULL)
268 return;
270 if (!gpios->mctrl_on)
271 return;
273 gpios->mctrl_on = false;
275 for (i = 0; i < UART_GPIO_MAX; ++i) {
276 if (!gpios->irq[i])
277 continue;
279 disable_irq(gpios->irq[i]);
282 EXPORT_SYMBOL_GPL(mctrl_gpio_disable_ms);
284 MODULE_LICENSE("GPL");