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 DECLARE_BITMAP(values
, 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 __assign_bit(count
, values
,
53 mctrl
& mctrl_gpios_desc
[i
].mctrl
);
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
;
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
;
79 *mctrl
&= ~mctrl_gpios_desc
[i
].mctrl
;
85 EXPORT_SYMBOL_GPL(mctrl_gpio_get
);
88 mctrl_gpio_get_outputs(struct mctrl_gpios
*gpios
, unsigned int *mctrl
)
90 enum mctrl_gpio_idx i
;
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
;
100 *mctrl
&= ~mctrl_gpios_desc
[i
].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
);
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
;
126 devm_gpiod_get_index_optional(dev
,
127 mctrl_gpios_desc
[i
].name
,
130 if (IS_ERR(gpios
->gpio
[i
]))
131 return ERR_CAST(gpios
->gpio
[i
]);
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
;
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
)
158 if ((mctrl_diff
& mctrl
) & TIOCM_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
);
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
);
186 for (i
= 0; i
< UART_GPIO_MAX
; ++i
) {
189 if (!gpios
->gpio
[i
] || mctrl_gpios_desc
[i
].dir_out
)
192 ret
= gpiod_to_irq(gpios
->gpio
[i
]);
195 "failed to find corresponding irq for %s (idx=%d, err=%d)\n",
196 mctrl_gpios_desc
[i
].name
, idx
, 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
),
209 /* alternatively implement polling */
211 "failed to request irq for %s (idx=%d, err=%d)\n",
212 mctrl_gpios_desc
[i
].name
, idx
, ret
);
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
;
228 for (i
= 0; i
< UART_GPIO_MAX
; i
++) {
230 devm_free_irq(gpios
->port
->dev
, gpios
->irq
[i
], gpios
);
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
;
246 /* .enable_ms may be called multiple times */
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
) {
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
;
271 if (!gpios
->mctrl_on
)
274 gpios
->mctrl_on
= false;
276 for (i
= 0; i
< UART_GPIO_MAX
; ++i
) {
280 disable_irq(gpios
->irq
[i
]);
283 EXPORT_SYMBOL_GPL(mctrl_gpio_disable_ms
);
285 MODULE_LICENSE("GPL");