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