1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Helpers for controlling modem lines via GPIO 4 * 5 * Copyright (C) 2014 Paratronic S.A. 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; either version 2 of the License, or 10 * (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 */ 17 18 #include <linux/err.h> 19 #include <linux/device.h> 20 #include <linux/irq.h> 21 #include <linux/gpio/consumer.h> 22 #include <linux/termios.h> 23 #include <linux/serial_core.h> 24 #include <linux/module.h> 25 26 #include "serial_mctrl_gpio.h" 27 28 struct mctrl_gpios { 29 struct uart_port *port; 30 struct gpio_desc *gpio[UART_GPIO_MAX]; 31 int irq[UART_GPIO_MAX]; 32 unsigned int mctrl_prev; 33 bool mctrl_on; 34 }; 35 36 static const struct { 37 const char *name; 38 unsigned int mctrl; 39 bool dir_out; 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, }, 47 }; 48 49 void mctrl_gpio_set(struct mctrl_gpios *gpios, unsigned int mctrl) 50 { 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; 55 56 if (gpios == NULL) 57 return; 58 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); 63 count++; 64 } 65 gpiod_set_array_value(count, desc_array, value_array); 66 } 67 EXPORT_SYMBOL_GPL(mctrl_gpio_set); 68 69 struct gpio_desc *mctrl_gpio_to_gpiod(struct mctrl_gpios *gpios, 70 enum mctrl_gpio_idx gidx) 71 { 72 return gpios->gpio[gidx]; 73 } 74 EXPORT_SYMBOL_GPL(mctrl_gpio_to_gpiod); 75 76 unsigned int mctrl_gpio_get(struct mctrl_gpios *gpios, unsigned int *mctrl) 77 { 78 enum mctrl_gpio_idx i; 79 80 if (gpios == NULL) 81 return *mctrl; 82 83 for (i = 0; i < UART_GPIO_MAX; i++) { 84 if (gpios->gpio[i] && !mctrl_gpios_desc[i].dir_out) { 85 if (gpiod_get_value(gpios->gpio[i])) 86 *mctrl |= mctrl_gpios_desc[i].mctrl; 87 else 88 *mctrl &= ~mctrl_gpios_desc[i].mctrl; 89 } 90 } 91 92 return *mctrl; 93 } 94 EXPORT_SYMBOL_GPL(mctrl_gpio_get); 95 96 unsigned int 97 mctrl_gpio_get_outputs(struct mctrl_gpios *gpios, unsigned int *mctrl) 98 { 99 enum mctrl_gpio_idx i; 100 101 if (gpios == NULL) 102 return *mctrl; 103 104 for (i = 0; i < UART_GPIO_MAX; i++) { 105 if (gpios->gpio[i] && mctrl_gpios_desc[i].dir_out) { 106 if (gpiod_get_value(gpios->gpio[i])) 107 *mctrl |= mctrl_gpios_desc[i].mctrl; 108 else 109 *mctrl &= ~mctrl_gpios_desc[i].mctrl; 110 } 111 } 112 113 return *mctrl; 114 } 115 EXPORT_SYMBOL_GPL(mctrl_gpio_get_outputs); 116 117 struct mctrl_gpios *mctrl_gpio_init_noauto(struct device *dev, unsigned int idx) 118 { 119 struct mctrl_gpios *gpios; 120 enum mctrl_gpio_idx i; 121 122 gpios = devm_kzalloc(dev, sizeof(*gpios), GFP_KERNEL); 123 if (!gpios) 124 return ERR_PTR(-ENOMEM); 125 126 for (i = 0; i < UART_GPIO_MAX; i++) { 127 enum gpiod_flags flags; 128 129 if (mctrl_gpios_desc[i].dir_out) 130 flags = GPIOD_OUT_LOW; 131 else 132 flags = GPIOD_IN; 133 134 gpios->gpio[i] = 135 devm_gpiod_get_index_optional(dev, 136 mctrl_gpios_desc[i].name, 137 idx, flags); 138 139 if (IS_ERR(gpios->gpio[i])) 140 return ERR_CAST(gpios->gpio[i]); 141 } 142 143 return gpios; 144 } 145 EXPORT_SYMBOL_GPL(mctrl_gpio_init_noauto); 146 147 #define MCTRL_ANY_DELTA (TIOCM_RI | TIOCM_DSR | TIOCM_CD | TIOCM_CTS) 148 static irqreturn_t mctrl_gpio_irq_handle(int irq, void *context) 149 { 150 struct mctrl_gpios *gpios = context; 151 struct uart_port *port = gpios->port; 152 u32 mctrl = gpios->mctrl_prev; 153 u32 mctrl_diff; 154 unsigned long flags; 155 156 mctrl_gpio_get(gpios, &mctrl); 157 158 spin_lock_irqsave(&port->lock, flags); 159 160 mctrl_diff = mctrl ^ gpios->mctrl_prev; 161 gpios->mctrl_prev = mctrl; 162 163 if (mctrl_diff & MCTRL_ANY_DELTA && port->state != NULL) { 164 if ((mctrl_diff & mctrl) & TIOCM_RI) 165 port->icount.rng++; 166 167 if ((mctrl_diff & mctrl) & TIOCM_DSR) 168 port->icount.dsr++; 169 170 if (mctrl_diff & TIOCM_CD) 171 uart_handle_dcd_change(port, mctrl & TIOCM_CD); 172 173 if (mctrl_diff & TIOCM_CTS) 174 uart_handle_cts_change(port, mctrl & TIOCM_CTS); 175 176 wake_up_interruptible(&port->state->port.delta_msr_wait); 177 } 178 179 spin_unlock_irqrestore(&port->lock, flags); 180 181 return IRQ_HANDLED; 182 } 183 184 struct mctrl_gpios *mctrl_gpio_init(struct uart_port *port, unsigned int idx) 185 { 186 struct mctrl_gpios *gpios; 187 enum mctrl_gpio_idx i; 188 189 gpios = mctrl_gpio_init_noauto(port->dev, idx); 190 if (IS_ERR(gpios)) 191 return gpios; 192 193 gpios->port = port; 194 195 for (i = 0; i < UART_GPIO_MAX; ++i) { 196 int ret; 197 198 if (!gpios->gpio[i] || mctrl_gpios_desc[i].dir_out) 199 continue; 200 201 ret = gpiod_to_irq(gpios->gpio[i]); 202 if (ret <= 0) { 203 dev_err(port->dev, 204 "failed to find corresponding irq for %s (idx=%d, err=%d)\n", 205 mctrl_gpios_desc[i].name, idx, ret); 206 return ERR_PTR(ret); 207 } 208 gpios->irq[i] = ret; 209 210 /* irqs should only be enabled in .enable_ms */ 211 irq_set_status_flags(gpios->irq[i], IRQ_NOAUTOEN); 212 213 ret = devm_request_irq(port->dev, gpios->irq[i], 214 mctrl_gpio_irq_handle, 215 IRQ_TYPE_EDGE_BOTH, dev_name(port->dev), 216 gpios); 217 if (ret) { 218 /* alternatively implement polling */ 219 dev_err(port->dev, 220 "failed to request irq for %s (idx=%d, err=%d)\n", 221 mctrl_gpios_desc[i].name, idx, ret); 222 return ERR_PTR(ret); 223 } 224 } 225 226 return gpios; 227 } 228 EXPORT_SYMBOL_GPL(mctrl_gpio_init); 229 230 void mctrl_gpio_free(struct device *dev, struct mctrl_gpios *gpios) 231 { 232 enum mctrl_gpio_idx i; 233 234 if (gpios == NULL) 235 return; 236 237 for (i = 0; i < UART_GPIO_MAX; i++) { 238 if (gpios->irq[i]) 239 devm_free_irq(gpios->port->dev, gpios->irq[i], gpios); 240 241 if (gpios->gpio[i]) 242 devm_gpiod_put(dev, gpios->gpio[i]); 243 } 244 devm_kfree(dev, gpios); 245 } 246 EXPORT_SYMBOL_GPL(mctrl_gpio_free); 247 248 void mctrl_gpio_enable_ms(struct mctrl_gpios *gpios) 249 { 250 enum mctrl_gpio_idx i; 251 252 if (gpios == NULL) 253 return; 254 255 /* .enable_ms may be called multiple times */ 256 if (gpios->mctrl_on) 257 return; 258 259 gpios->mctrl_on = true; 260 261 /* get initial status of modem lines GPIOs */ 262 mctrl_gpio_get(gpios, &gpios->mctrl_prev); 263 264 for (i = 0; i < UART_GPIO_MAX; ++i) { 265 if (!gpios->irq[i]) 266 continue; 267 268 enable_irq(gpios->irq[i]); 269 } 270 } 271 EXPORT_SYMBOL_GPL(mctrl_gpio_enable_ms); 272 273 void mctrl_gpio_disable_ms(struct mctrl_gpios *gpios) 274 { 275 enum mctrl_gpio_idx i; 276 277 if (gpios == NULL) 278 return; 279 280 if (!gpios->mctrl_on) 281 return; 282 283 gpios->mctrl_on = false; 284 285 for (i = 0; i < UART_GPIO_MAX; ++i) { 286 if (!gpios->irq[i]) 287 continue; 288 289 disable_irq(gpios->irq[i]); 290 } 291 } 292 EXPORT_SYMBOL_GPL(mctrl_gpio_disable_ms); 293 294 MODULE_LICENSE("GPL"); 295