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 19 #ifndef __SERIAL_MCTRL_GPIO__ 20 #define __SERIAL_MCTRL_GPIO__ 21 22 #include <linux/err.h> 23 #include <linux/device.h> 24 #include <linux/gpio/consumer.h> 25 26 struct uart_port; 27 28 enum mctrl_gpio_idx { 29 UART_GPIO_CTS, 30 UART_GPIO_DSR, 31 UART_GPIO_DCD, 32 UART_GPIO_RNG, 33 UART_GPIO_RI = UART_GPIO_RNG, 34 UART_GPIO_RTS, 35 UART_GPIO_DTR, 36 UART_GPIO_MAX, 37 }; 38 39 /* 40 * Opaque descriptor for modem lines controlled by GPIOs 41 */ 42 struct mctrl_gpios; 43 44 #ifdef CONFIG_GPIOLIB 45 46 /* 47 * Set state of the modem control output lines via GPIOs. 48 */ 49 void mctrl_gpio_set(struct mctrl_gpios *gpios, unsigned int mctrl); 50 51 /* 52 * Get state of the modem control input lines from GPIOs. 53 * The mctrl flags are updated and returned. 54 */ 55 unsigned int mctrl_gpio_get(struct mctrl_gpios *gpios, unsigned int *mctrl); 56 57 /* 58 * Get state of the modem control output lines from GPIOs. 59 * The mctrl flags are updated and returned. 60 */ 61 unsigned int 62 mctrl_gpio_get_outputs(struct mctrl_gpios *gpios, unsigned int *mctrl); 63 64 /* 65 * Returns the associated struct gpio_desc to the modem line gidx 66 */ 67 struct gpio_desc *mctrl_gpio_to_gpiod(struct mctrl_gpios *gpios, 68 enum mctrl_gpio_idx gidx); 69 70 /* 71 * Request and set direction of modem control line GPIOs and set up irq 72 * handling. 73 * devm_* functions are used, so there's no need to call mctrl_gpio_free(). 74 * Returns a pointer to the allocated mctrl structure if ok, -ENOMEM on 75 * allocation error. 76 */ 77 struct mctrl_gpios *mctrl_gpio_init(struct uart_port *port, unsigned int idx); 78 79 /* 80 * Request and set direction of modem control line GPIOs. 81 * devm_* functions are used, so there's no need to call mctrl_gpio_free(). 82 * Returns a pointer to the allocated mctrl structure if ok, -ENOMEM on 83 * allocation error. 84 */ 85 struct mctrl_gpios *mctrl_gpio_init_noauto(struct device *dev, 86 unsigned int idx); 87 88 /* 89 * Free the mctrl_gpios structure. 90 * Normally, this function will not be called, as the GPIOs will 91 * be disposed of by the resource management code. 92 */ 93 void mctrl_gpio_free(struct device *dev, struct mctrl_gpios *gpios); 94 95 /* 96 * Enable gpio interrupts to report status line changes. 97 */ 98 void mctrl_gpio_enable_ms(struct mctrl_gpios *gpios); 99 100 /* 101 * Disable gpio interrupts to report status line changes. 102 */ 103 void mctrl_gpio_disable_ms(struct mctrl_gpios *gpios); 104 105 #else /* GPIOLIB */ 106 107 static inline 108 void mctrl_gpio_set(struct mctrl_gpios *gpios, unsigned int mctrl) 109 { 110 } 111 112 static inline 113 unsigned int mctrl_gpio_get(struct mctrl_gpios *gpios, unsigned int *mctrl) 114 { 115 return *mctrl; 116 } 117 118 static inline unsigned int 119 mctrl_gpio_get_outputs(struct mctrl_gpios *gpios, unsigned int *mctrl) 120 { 121 return *mctrl; 122 } 123 124 static inline 125 struct gpio_desc *mctrl_gpio_to_gpiod(struct mctrl_gpios *gpios, 126 enum mctrl_gpio_idx gidx) 127 { 128 return ERR_PTR(-ENOSYS); 129 } 130 131 static inline 132 struct mctrl_gpios *mctrl_gpio_init(struct uart_port *port, unsigned int idx) 133 { 134 return ERR_PTR(-ENOSYS); 135 } 136 137 static inline 138 struct mctrl_gpios *mctrl_gpio_init_noauto(struct device *dev, unsigned int idx) 139 { 140 return ERR_PTR(-ENOSYS); 141 } 142 143 static inline 144 void mctrl_gpio_free(struct device *dev, struct mctrl_gpios *gpios) 145 { 146 } 147 148 static inline void mctrl_gpio_enable_ms(struct mctrl_gpios *gpios) 149 { 150 } 151 152 static inline void mctrl_gpio_disable_ms(struct mctrl_gpios *gpios) 153 { 154 } 155 156 #endif /* GPIOLIB */ 157 158 #endif 159