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 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> 15 16 #include "serial_mctrl_gpio.h" 17 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; 24 }; 25 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, }, 37 }; 38 39 void mctrl_gpio_set(struct mctrl_gpios *gpios, unsigned int mctrl) 40 { 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; 45 46 if (gpios == NULL) 47 return; 48 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++; 54 } 55 gpiod_set_array_value(count, desc_array, value_array); 56 } 57 EXPORT_SYMBOL_GPL(mctrl_gpio_set); 58 59 struct gpio_desc *mctrl_gpio_to_gpiod(struct mctrl_gpios *gpios, 60 enum mctrl_gpio_idx gidx) 61 { 62 return gpios->gpio[gidx]; 63 } 64 EXPORT_SYMBOL_GPL(mctrl_gpio_to_gpiod); 65 66 unsigned int mctrl_gpio_get(struct mctrl_gpios *gpios, unsigned int *mctrl) 67 { 68 enum mctrl_gpio_idx i; 69 70 if (gpios == NULL) 71 return *mctrl; 72 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; 79 } 80 } 81 82 return *mctrl; 83 } 84 EXPORT_SYMBOL_GPL(mctrl_gpio_get); 85 86 unsigned int 87 mctrl_gpio_get_outputs(struct mctrl_gpios *gpios, unsigned int *mctrl) 88 { 89 enum mctrl_gpio_idx i; 90 91 if (gpios == NULL) 92 return *mctrl; 93 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; 100 } 101 } 102 103 return *mctrl; 104 } 105 EXPORT_SYMBOL_GPL(mctrl_gpio_get_outputs); 106 107 struct mctrl_gpios *mctrl_gpio_init_noauto(struct device *dev, unsigned int idx) 108 { 109 struct mctrl_gpios *gpios; 110 enum mctrl_gpio_idx i; 111 112 gpios = devm_kzalloc(dev, sizeof(*gpios), GFP_KERNEL); 113 if (!gpios) 114 return ERR_PTR(-ENOMEM); 115 116 for (i = 0; i < UART_GPIO_MAX; i++) { 117 enum gpiod_flags flags; 118 119 if (mctrl_gpios_desc[i].dir_out) 120 flags = GPIOD_OUT_LOW; 121 else 122 flags = GPIOD_IN; 123 124 gpios->gpio[i] = 125 devm_gpiod_get_index_optional(dev, 126 mctrl_gpios_desc[i].name, 127 idx, flags); 128 129 if (IS_ERR(gpios->gpio[i])) 130 return ERR_CAST(gpios->gpio[i]); 131 } 132 133 return gpios; 134 } 135 EXPORT_SYMBOL_GPL(mctrl_gpio_init_noauto); 136 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) 139 { 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; 145 146 mctrl_gpio_get(gpios, &mctrl); 147 148 spin_lock_irqsave(&port->lock, flags); 149 150 mctrl_diff = mctrl ^ gpios->mctrl_prev; 151 gpios->mctrl_prev = mctrl; 152 153 if (mctrl_diff & MCTRL_ANY_DELTA && port->state != NULL) { 154 if ((mctrl_diff & mctrl) & TIOCM_RI) 155 port->icount.rng++; 156 157 if ((mctrl_diff & mctrl) & TIOCM_DSR) 158 port->icount.dsr++; 159 160 if (mctrl_diff & TIOCM_CD) 161 uart_handle_dcd_change(port, mctrl & TIOCM_CD); 162 163 if (mctrl_diff & TIOCM_CTS) 164 uart_handle_cts_change(port, mctrl & TIOCM_CTS); 165 166 wake_up_interruptible(&port->state->port.delta_msr_wait); 167 } 168 169 spin_unlock_irqrestore(&port->lock, flags); 170 171 return IRQ_HANDLED; 172 } 173 174 struct mctrl_gpios *mctrl_gpio_init(struct uart_port *port, unsigned int idx) 175 { 176 struct mctrl_gpios *gpios; 177 enum mctrl_gpio_idx i; 178 179 gpios = mctrl_gpio_init_noauto(port->dev, idx); 180 if (IS_ERR(gpios)) 181 return gpios; 182 183 gpios->port = port; 184 185 for (i = 0; i < UART_GPIO_MAX; ++i) { 186 int ret; 187 188 if (!gpios->gpio[i] || mctrl_gpios_desc[i].dir_out) 189 continue; 190 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); 197 } 198 gpios->irq[i] = ret; 199 200 /* irqs should only be enabled in .enable_ms */ 201 irq_set_status_flags(gpios->irq[i], IRQ_NOAUTOEN); 202 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); 213 } 214 } 215 216 return gpios; 217 } 218 EXPORT_SYMBOL_GPL(mctrl_gpio_init); 219 220 void mctrl_gpio_free(struct device *dev, struct mctrl_gpios *gpios) 221 { 222 enum mctrl_gpio_idx i; 223 224 if (gpios == NULL) 225 return; 226 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); 230 231 if (gpios->gpio[i]) 232 devm_gpiod_put(dev, gpios->gpio[i]); 233 } 234 devm_kfree(dev, gpios); 235 } 236 EXPORT_SYMBOL_GPL(mctrl_gpio_free); 237 238 void mctrl_gpio_enable_ms(struct mctrl_gpios *gpios) 239 { 240 enum mctrl_gpio_idx i; 241 242 if (gpios == NULL) 243 return; 244 245 /* .enable_ms may be called multiple times */ 246 if (gpios->mctrl_on) 247 return; 248 249 gpios->mctrl_on = true; 250 251 /* get initial status of modem lines GPIOs */ 252 mctrl_gpio_get(gpios, &gpios->mctrl_prev); 253 254 for (i = 0; i < UART_GPIO_MAX; ++i) { 255 if (!gpios->irq[i]) 256 continue; 257 258 enable_irq(gpios->irq[i]); 259 } 260 } 261 EXPORT_SYMBOL_GPL(mctrl_gpio_enable_ms); 262 263 void mctrl_gpio_disable_ms(struct mctrl_gpios *gpios) 264 { 265 enum mctrl_gpio_idx i; 266 267 if (gpios == NULL) 268 return; 269 270 if (!gpios->mctrl_on) 271 return; 272 273 gpios->mctrl_on = false; 274 275 for (i = 0; i < UART_GPIO_MAX; ++i) { 276 if (!gpios->irq[i]) 277 continue; 278 279 disable_irq(gpios->irq[i]); 280 } 281 } 282 EXPORT_SYMBOL_GPL(mctrl_gpio_disable_ms); 283 284 MODULE_LICENSE("GPL"); 285