blob: 9e1086f7f22a9c45f708f1126d0ff0298b9b2c8b [file] [log] [blame]
Richard Genoud84130aa2014-05-13 20:20:43 +02001/*
2 * Helpers for controlling modem lines via GPIO
3 *
4 * Copyright (C) 2014 Paratronic S.A.
5 *
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.
10 *
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.
Richard Genoud84130aa2014-05-13 20:20:43 +020015 */
16
17#include <linux/err.h>
18#include <linux/device.h>
Uwe Kleine-Königce59e482015-09-30 10:19:41 +020019#include <linux/irq.h>
Richard Genoud84130aa2014-05-13 20:20:43 +020020#include <linux/gpio/consumer.h>
Alexander Shiyan93b88772014-09-20 09:34:45 +040021#include <linux/termios.h>
Uwe Kleine-Königce59e482015-09-30 10:19:41 +020022#include <linux/serial_core.h>
Romain Izard82a3f872016-02-23 15:54:54 +010023#include <linux/module.h>
Richard Genoud84130aa2014-05-13 20:20:43 +020024
25#include "serial_mctrl_gpio.h"
26
27struct mctrl_gpios {
Uwe Kleine-Königce59e482015-09-30 10:19:41 +020028 struct uart_port *port;
Richard Genoud84130aa2014-05-13 20:20:43 +020029 struct gpio_desc *gpio[UART_GPIO_MAX];
Uwe Kleine-Königce59e482015-09-30 10:19:41 +020030 int irq[UART_GPIO_MAX];
31 unsigned int mctrl_prev;
32 bool mctrl_on;
Richard Genoud84130aa2014-05-13 20:20:43 +020033};
34
35static const struct {
36 const char *name;
37 unsigned int mctrl;
38 bool dir_out;
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, },
46 { "out1", TIOCM_OUT1, true, },
47 { "out2", TIOCM_OUT2, true, },
48};
49
50void mctrl_gpio_set(struct mctrl_gpios *gpios, unsigned int mctrl)
51{
52 enum mctrl_gpio_idx i;
Rojhalat Ibrahim834296a2014-11-17 18:31:30 +010053 struct gpio_desc *desc_array[UART_GPIO_MAX];
54 int value_array[UART_GPIO_MAX];
55 unsigned int count = 0;
Richard Genoud84130aa2014-05-13 20:20:43 +020056
Richard Genoud84130aa2014-05-13 20:20:43 +020057 for (i = 0; i < UART_GPIO_MAX; i++)
Uwe Kleine-König445df7f2015-05-19 21:56:29 +020058 if (gpios->gpio[i] && mctrl_gpios_desc[i].dir_out) {
Rojhalat Ibrahim834296a2014-11-17 18:31:30 +010059 desc_array[count] = gpios->gpio[i];
60 value_array[count] = !!(mctrl & mctrl_gpios_desc[i].mctrl);
61 count++;
62 }
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +020063 gpiod_set_array_value(count, desc_array, value_array);
Richard Genoud84130aa2014-05-13 20:20:43 +020064}
65EXPORT_SYMBOL_GPL(mctrl_gpio_set);
66
67struct gpio_desc *mctrl_gpio_to_gpiod(struct mctrl_gpios *gpios,
68 enum mctrl_gpio_idx gidx)
69{
Uwe Kleine-König9e9f0792015-02-12 15:24:41 +010070 return gpios->gpio[gidx];
Richard Genoud84130aa2014-05-13 20:20:43 +020071}
72EXPORT_SYMBOL_GPL(mctrl_gpio_to_gpiod);
73
74unsigned int mctrl_gpio_get(struct mctrl_gpios *gpios, unsigned int *mctrl)
75{
76 enum mctrl_gpio_idx i;
77
Richard Genoud84130aa2014-05-13 20:20:43 +020078 for (i = 0; i < UART_GPIO_MAX; i++) {
Uwe Kleine-König9e9f0792015-02-12 15:24:41 +010079 if (gpios->gpio[i] && !mctrl_gpios_desc[i].dir_out) {
Richard Genoud84130aa2014-05-13 20:20:43 +020080 if (gpiod_get_value(gpios->gpio[i]))
81 *mctrl |= mctrl_gpios_desc[i].mctrl;
82 else
83 *mctrl &= ~mctrl_gpios_desc[i].mctrl;
84 }
85 }
86
87 return *mctrl;
88}
89EXPORT_SYMBOL_GPL(mctrl_gpio_get);
90
Uwe Kleine-König7d8c70d2015-09-30 10:19:40 +020091struct mctrl_gpios *mctrl_gpio_init_noauto(struct device *dev, unsigned int idx)
Richard Genoud84130aa2014-05-13 20:20:43 +020092{
93 struct mctrl_gpios *gpios;
94 enum mctrl_gpio_idx i;
Richard Genoud84130aa2014-05-13 20:20:43 +020095
96 gpios = devm_kzalloc(dev, sizeof(*gpios), GFP_KERNEL);
97 if (!gpios)
98 return ERR_PTR(-ENOMEM);
99
100 for (i = 0; i < UART_GPIO_MAX; i++) {
Uwe Kleine-König1d267ea2015-02-12 15:24:42 +0100101 enum gpiod_flags flags;
Richard Genoud84130aa2014-05-13 20:20:43 +0200102
103 if (mctrl_gpios_desc[i].dir_out)
Uwe Kleine-König1d267ea2015-02-12 15:24:42 +0100104 flags = GPIOD_OUT_LOW;
Richard Genoud84130aa2014-05-13 20:20:43 +0200105 else
Uwe Kleine-König1d267ea2015-02-12 15:24:42 +0100106 flags = GPIOD_IN;
107
108 gpios->gpio[i] =
109 devm_gpiod_get_index_optional(dev,
110 mctrl_gpios_desc[i].name,
111 idx, flags);
112
113 if (IS_ERR(gpios->gpio[i]))
Fabio Estevam13bc2bb2015-03-10 12:23:18 -0300114 return ERR_CAST(gpios->gpio[i]);
Richard Genoud84130aa2014-05-13 20:20:43 +0200115 }
116
117 return gpios;
118}
Uwe Kleine-König7d8c70d2015-09-30 10:19:40 +0200119EXPORT_SYMBOL_GPL(mctrl_gpio_init_noauto);
Richard Genoud84130aa2014-05-13 20:20:43 +0200120
Uwe Kleine-Königce59e482015-09-30 10:19:41 +0200121#define MCTRL_ANY_DELTA (TIOCM_RI | TIOCM_DSR | TIOCM_CD | TIOCM_CTS)
122static irqreturn_t mctrl_gpio_irq_handle(int irq, void *context)
123{
124 struct mctrl_gpios *gpios = context;
125 struct uart_port *port = gpios->port;
126 u32 mctrl = gpios->mctrl_prev;
127 u32 mctrl_diff;
Yegor Yefremovd11df6182016-04-29 10:45:07 +0200128 unsigned long flags;
Uwe Kleine-Königce59e482015-09-30 10:19:41 +0200129
130 mctrl_gpio_get(gpios, &mctrl);
131
Yegor Yefremovd11df6182016-04-29 10:45:07 +0200132 spin_lock_irqsave(&port->lock, flags);
133
Uwe Kleine-Königce59e482015-09-30 10:19:41 +0200134 mctrl_diff = mctrl ^ gpios->mctrl_prev;
135 gpios->mctrl_prev = mctrl;
136
137 if (mctrl_diff & MCTRL_ANY_DELTA && port->state != NULL) {
138 if ((mctrl_diff & mctrl) & TIOCM_RI)
139 port->icount.rng++;
140
141 if ((mctrl_diff & mctrl) & TIOCM_DSR)
142 port->icount.dsr++;
143
144 if (mctrl_diff & TIOCM_CD)
145 uart_handle_dcd_change(port, mctrl & TIOCM_CD);
146
147 if (mctrl_diff & TIOCM_CTS)
148 uart_handle_cts_change(port, mctrl & TIOCM_CTS);
149
150 wake_up_interruptible(&port->state->port.delta_msr_wait);
151 }
152
Yegor Yefremovd11df6182016-04-29 10:45:07 +0200153 spin_unlock_irqrestore(&port->lock, flags);
154
Uwe Kleine-Königce59e482015-09-30 10:19:41 +0200155 return IRQ_HANDLED;
156}
157
158struct mctrl_gpios *mctrl_gpio_init(struct uart_port *port, unsigned int idx)
159{
160 struct mctrl_gpios *gpios;
161 enum mctrl_gpio_idx i;
162
163 gpios = mctrl_gpio_init_noauto(port->dev, idx);
164 if (IS_ERR(gpios))
165 return gpios;
166
167 gpios->port = port;
168
169 for (i = 0; i < UART_GPIO_MAX; ++i) {
170 int ret;
171
172 if (!gpios->gpio[i] || mctrl_gpios_desc[i].dir_out)
173 continue;
174
175 ret = gpiod_to_irq(gpios->gpio[i]);
176 if (ret <= 0) {
177 dev_err(port->dev,
178 "failed to find corresponding irq for %s (idx=%d, err=%d)\n",
179 mctrl_gpios_desc[i].name, idx, ret);
180 return ERR_PTR(ret);
181 }
182 gpios->irq[i] = ret;
183
184 /* irqs should only be enabled in .enable_ms */
185 irq_set_status_flags(gpios->irq[i], IRQ_NOAUTOEN);
186
187 ret = devm_request_irq(port->dev, gpios->irq[i],
188 mctrl_gpio_irq_handle,
189 IRQ_TYPE_EDGE_BOTH, dev_name(port->dev),
190 gpios);
191 if (ret) {
192 /* alternatively implement polling */
193 dev_err(port->dev,
194 "failed to request irq for %s (idx=%d, err=%d)\n",
195 mctrl_gpios_desc[i].name, idx, ret);
196 return ERR_PTR(ret);
197 }
198 }
199
200 return gpios;
201}
Uwe Kleine-König4f71a2e2015-12-13 11:30:02 +0100202EXPORT_SYMBOL_GPL(mctrl_gpio_init);
Uwe Kleine-Königce59e482015-09-30 10:19:41 +0200203
Richard Genoud84130aa2014-05-13 20:20:43 +0200204void mctrl_gpio_free(struct device *dev, struct mctrl_gpios *gpios)
205{
206 enum mctrl_gpio_idx i;
207
Uwe Kleine-Königce59e482015-09-30 10:19:41 +0200208 for (i = 0; i < UART_GPIO_MAX; i++) {
209 if (gpios->irq[i])
210 devm_free_irq(gpios->port->dev, gpios->irq[i], gpios);
211
Uwe Kleine-König445df7f2015-05-19 21:56:29 +0200212 if (gpios->gpio[i])
Richard Genoud84130aa2014-05-13 20:20:43 +0200213 devm_gpiod_put(dev, gpios->gpio[i]);
Uwe Kleine-Königce59e482015-09-30 10:19:41 +0200214 }
Richard Genoud84130aa2014-05-13 20:20:43 +0200215 devm_kfree(dev, gpios);
216}
217EXPORT_SYMBOL_GPL(mctrl_gpio_free);
Uwe Kleine-Königce59e482015-09-30 10:19:41 +0200218
219void mctrl_gpio_enable_ms(struct mctrl_gpios *gpios)
220{
221 enum mctrl_gpio_idx i;
222
223 /* .enable_ms may be called multiple times */
224 if (gpios->mctrl_on)
225 return;
226
227 gpios->mctrl_on = true;
228
229 /* get initial status of modem lines GPIOs */
230 mctrl_gpio_get(gpios, &gpios->mctrl_prev);
231
232 for (i = 0; i < UART_GPIO_MAX; ++i) {
233 if (!gpios->irq[i])
234 continue;
235
236 enable_irq(gpios->irq[i]);
237 }
238}
239EXPORT_SYMBOL_GPL(mctrl_gpio_enable_ms);
240
241void mctrl_gpio_disable_ms(struct mctrl_gpios *gpios)
242{
243 enum mctrl_gpio_idx i;
244
245 if (!gpios->mctrl_on)
246 return;
247
248 gpios->mctrl_on = false;
249
250 for (i = 0; i < UART_GPIO_MAX; ++i) {
251 if (!gpios->irq[i])
252 continue;
253
254 disable_irq(gpios->irq[i]);
255 }
256}
Uwe Kleine-König4f71a2e2015-12-13 11:30:02 +0100257EXPORT_SYMBOL_GPL(mctrl_gpio_disable_ms);
Romain Izard82a3f872016-02-23 15:54:54 +0100258
259MODULE_LICENSE("GPL");