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