1 /* 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. 15 */ 16 17 #include <linux/err.h> 18 #include <linux/device.h> 19 #include <linux/irq.h> 20 #include <linux/gpio/consumer.h> 21 #include <linux/termios.h> 22 #include <linux/serial_core.h> 23 #include <linux/module.h> 24 25 #include "serial_mctrl_gpio.h" 26 27 struct mctrl_gpios { 28 struct uart_port *port; 29 struct gpio_desc *gpio[UART_GPIO_MAX]; 30 int irq[UART_GPIO_MAX]; 31 unsigned int mctrl_prev; 32 bool mctrl_on; 33 }; 34 35 static 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 50 void mctrl_gpio_set(struct mctrl_gpios *gpios, unsigned int mctrl) 51 { 52 enum mctrl_gpio_idx i; 53 struct gpio_desc *desc_array[UART_GPIO_MAX]; 54 int value_array[UART_GPIO_MAX]; 55 unsigned int count = 0; 56 57 for (i = 0; i < UART_GPIO_MAX; i++) 58 if (gpios->gpio[i] && mctrl_gpios_desc[i].dir_out) { 59 desc_array[count] = gpios->gpio[i]; 60 value_array[count] = !!(mctrl & mctrl_gpios_desc[i].mctrl); 61 count++; 62 } 63 gpiod_set_array_value(count, desc_array, value_array); 64 } 65 EXPORT_SYMBOL_GPL(mctrl_gpio_set); 66 67 struct gpio_desc *mctrl_gpio_to_gpiod(struct mctrl_gpios *gpios, 68 enum mctrl_gpio_idx gidx) 69 { 70 return gpios->gpio[gidx]; 71 } 72 EXPORT_SYMBOL_GPL(mctrl_gpio_to_gpiod); 73 74 unsigned int mctrl_gpio_get(struct mctrl_gpios *gpios, unsigned int *mctrl) 75 { 76 enum mctrl_gpio_idx i; 77 78 for (i = 0; i < UART_GPIO_MAX; i++) { 79 if (gpios->gpio[i] && !mctrl_gpios_desc[i].dir_out) { 80 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 } 89 EXPORT_SYMBOL_GPL(mctrl_gpio_get); 90 91 struct mctrl_gpios *mctrl_gpio_init_noauto(struct device *dev, unsigned int idx) 92 { 93 struct mctrl_gpios *gpios; 94 enum mctrl_gpio_idx i; 95 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++) { 101 enum gpiod_flags flags; 102 103 if (mctrl_gpios_desc[i].dir_out) 104 flags = GPIOD_OUT_LOW; 105 else 106 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])) 114 return ERR_CAST(gpios->gpio[i]); 115 } 116 117 return gpios; 118 } 119 EXPORT_SYMBOL_GPL(mctrl_gpio_init_noauto); 120 121 #define MCTRL_ANY_DELTA (TIOCM_RI | TIOCM_DSR | TIOCM_CD | TIOCM_CTS) 122 static 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; 128 129 mctrl_gpio_get(gpios, &mctrl); 130 131 mctrl_diff = mctrl ^ gpios->mctrl_prev; 132 gpios->mctrl_prev = mctrl; 133 134 if (mctrl_diff & MCTRL_ANY_DELTA && port->state != NULL) { 135 if ((mctrl_diff & mctrl) & TIOCM_RI) 136 port->icount.rng++; 137 138 if ((mctrl_diff & mctrl) & TIOCM_DSR) 139 port->icount.dsr++; 140 141 if (mctrl_diff & TIOCM_CD) 142 uart_handle_dcd_change(port, mctrl & TIOCM_CD); 143 144 if (mctrl_diff & TIOCM_CTS) 145 uart_handle_cts_change(port, mctrl & TIOCM_CTS); 146 147 wake_up_interruptible(&port->state->port.delta_msr_wait); 148 } 149 150 return IRQ_HANDLED; 151 } 152 153 struct mctrl_gpios *mctrl_gpio_init(struct uart_port *port, unsigned int idx) 154 { 155 struct mctrl_gpios *gpios; 156 enum mctrl_gpio_idx i; 157 158 gpios = mctrl_gpio_init_noauto(port->dev, idx); 159 if (IS_ERR(gpios)) 160 return gpios; 161 162 gpios->port = port; 163 164 for (i = 0; i < UART_GPIO_MAX; ++i) { 165 int ret; 166 167 if (!gpios->gpio[i] || mctrl_gpios_desc[i].dir_out) 168 continue; 169 170 ret = gpiod_to_irq(gpios->gpio[i]); 171 if (ret <= 0) { 172 dev_err(port->dev, 173 "failed to find corresponding irq for %s (idx=%d, err=%d)\n", 174 mctrl_gpios_desc[i].name, idx, ret); 175 return ERR_PTR(ret); 176 } 177 gpios->irq[i] = ret; 178 179 /* irqs should only be enabled in .enable_ms */ 180 irq_set_status_flags(gpios->irq[i], IRQ_NOAUTOEN); 181 182 ret = devm_request_irq(port->dev, gpios->irq[i], 183 mctrl_gpio_irq_handle, 184 IRQ_TYPE_EDGE_BOTH, dev_name(port->dev), 185 gpios); 186 if (ret) { 187 /* alternatively implement polling */ 188 dev_err(port->dev, 189 "failed to request irq for %s (idx=%d, err=%d)\n", 190 mctrl_gpios_desc[i].name, idx, ret); 191 return ERR_PTR(ret); 192 } 193 } 194 195 return gpios; 196 } 197 EXPORT_SYMBOL_GPL(mctrl_gpio_init); 198 199 void mctrl_gpio_free(struct device *dev, struct mctrl_gpios *gpios) 200 { 201 enum mctrl_gpio_idx i; 202 203 for (i = 0; i < UART_GPIO_MAX; i++) { 204 if (gpios->irq[i]) 205 devm_free_irq(gpios->port->dev, gpios->irq[i], gpios); 206 207 if (gpios->gpio[i]) 208 devm_gpiod_put(dev, gpios->gpio[i]); 209 } 210 devm_kfree(dev, gpios); 211 } 212 EXPORT_SYMBOL_GPL(mctrl_gpio_free); 213 214 void mctrl_gpio_enable_ms(struct mctrl_gpios *gpios) 215 { 216 enum mctrl_gpio_idx i; 217 218 /* .enable_ms may be called multiple times */ 219 if (gpios->mctrl_on) 220 return; 221 222 gpios->mctrl_on = true; 223 224 /* get initial status of modem lines GPIOs */ 225 mctrl_gpio_get(gpios, &gpios->mctrl_prev); 226 227 for (i = 0; i < UART_GPIO_MAX; ++i) { 228 if (!gpios->irq[i]) 229 continue; 230 231 enable_irq(gpios->irq[i]); 232 } 233 } 234 EXPORT_SYMBOL_GPL(mctrl_gpio_enable_ms); 235 236 void mctrl_gpio_disable_ms(struct mctrl_gpios *gpios) 237 { 238 enum mctrl_gpio_idx i; 239 240 if (!gpios->mctrl_on) 241 return; 242 243 gpios->mctrl_on = false; 244 245 for (i = 0; i < UART_GPIO_MAX; ++i) { 246 if (!gpios->irq[i]) 247 continue; 248 249 disable_irq(gpios->irq[i]); 250 } 251 } 252 EXPORT_SYMBOL_GPL(mctrl_gpio_disable_ms); 253 254 MODULE_LICENSE("GPL"); 255