1 // SPDX-License-Identifier: GPL-2.0+
3 * Helpers for controlling modem lines via GPIO
5 * Copyright (C) 2014 Paratronic S.A.
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"
19 struct uart_port
*port
;
20 struct gpio_desc
*gpio
[UART_GPIO_MAX
];
21 int irq
[UART_GPIO_MAX
];
22 unsigned int mctrl_prev
;
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;
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
);
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
;
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
;
78 *mctrl
&= ~mctrl_gpios_desc
[i
].mctrl
;
84 EXPORT_SYMBOL_GPL(mctrl_gpio_get
);
87 mctrl_gpio_get_outputs(struct mctrl_gpios
*gpios
, unsigned int *mctrl
)
89 enum mctrl_gpio_idx i
;
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
;
99 *mctrl
&= ~mctrl_gpios_desc
[i
].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
);
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
;
125 devm_gpiod_get_index_optional(dev
,
126 mctrl_gpios_desc
[i
].name
,
129 if (IS_ERR(gpios
->gpio
[i
]))
130 return ERR_CAST(gpios
->gpio
[i
]);
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
;
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
)
157 if ((mctrl_diff
& mctrl
) & TIOCM_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
);
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
);
185 for (i
= 0; i
< UART_GPIO_MAX
; ++i
) {
188 if (!gpios
->gpio
[i
] || mctrl_gpios_desc
[i
].dir_out
)
191 ret
= gpiod_to_irq(gpios
->gpio
[i
]);
194 "failed to find corresponding irq for %s (idx=%d, err=%d)\n",
195 mctrl_gpios_desc
[i
].name
, idx
, 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
),
208 /* alternatively implement polling */
210 "failed to request irq for %s (idx=%d, err=%d)\n",
211 mctrl_gpios_desc
[i
].name
, idx
, ret
);
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
;
227 for (i
= 0; i
< UART_GPIO_MAX
; i
++) {
229 devm_free_irq(gpios
->port
->dev
, gpios
->irq
[i
], gpios
);
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
;
245 /* .enable_ms may be called multiple times */
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
) {
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
;
270 if (!gpios
->mctrl_on
)
273 gpios
->mctrl_on
= false;
275 for (i
= 0; i
< UART_GPIO_MAX
; ++i
) {
279 disable_irq(gpios
->irq
[i
]);
282 EXPORT_SYMBOL_GPL(mctrl_gpio_disable_ms
);
284 MODULE_LICENSE("GPL");