Greg Kroah-Hartman | e3b3d0f | 2017-11-06 18:11:51 +0100 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Richard Genoud | 84130aa | 2014-05-13 20:20:43 +0200 | [diff] [blame] | 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 | |
Uwe Kleine-König | ce59e48 | 2015-09-30 10:19:41 +0200 | [diff] [blame] | 26 | struct uart_port; |
| 27 | |
Richard Genoud | 84130aa | 2014-05-13 20:20:43 +0200 | [diff] [blame] | 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, |
Richard Genoud | 84130aa | 2014-05-13 20:20:43 +0200 | [diff] [blame] | 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 | /* |
Yegor Yefremov | bf5cee6 | 2016-05-31 10:59:16 +0200 | [diff] [blame] | 52 | * Get state of the modem control input lines from GPIOs. |
Richard Genoud | 84130aa | 2014-05-13 20:20:43 +0200 | [diff] [blame] | 53 | * The mctrl flags are updated and returned. |
| 54 | */ |
| 55 | unsigned int mctrl_gpio_get(struct mctrl_gpios *gpios, unsigned int *mctrl); |
| 56 | |
| 57 | /* |
Yegor Yefremov | bf5cee6 | 2016-05-31 10:59:16 +0200 | [diff] [blame] | 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 | /* |
Richard Genoud | 84130aa | 2014-05-13 20:20:43 +0200 | [diff] [blame] | 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 | /* |
Geert Uytterhoeven | 03e970b | 2016-04-14 12:01:32 +0200 | [diff] [blame] | 71 | * Request and set direction of modem control line GPIOs and set up irq |
Uwe Kleine-König | ce59e48 | 2015-09-30 10:19:41 +0200 | [diff] [blame] | 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 | /* |
Geert Uytterhoeven | 03e970b | 2016-04-14 12:01:32 +0200 | [diff] [blame] | 80 | * Request and set direction of modem control line GPIOs. |
Richard Genoud | 84130aa | 2014-05-13 20:20:43 +0200 | [diff] [blame] | 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 | */ |
Uwe Kleine-König | 7d8c70d | 2015-09-30 10:19:40 +0200 | [diff] [blame] | 85 | struct mctrl_gpios *mctrl_gpio_init_noauto(struct device *dev, |
| 86 | unsigned int idx); |
Richard Genoud | 84130aa | 2014-05-13 20:20:43 +0200 | [diff] [blame] | 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 | |
Uwe Kleine-König | ce59e48 | 2015-09-30 10:19:41 +0200 | [diff] [blame] | 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 | |
Richard Genoud | 84130aa | 2014-05-13 20:20:43 +0200 | [diff] [blame] | 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 | |
Yegor Yefremov | bf5cee6 | 2016-05-31 10:59:16 +0200 | [diff] [blame] | 118 | static inline unsigned int |
| 119 | mctrl_gpio_get_outputs(struct mctrl_gpios *gpios, unsigned int *mctrl) |
| 120 | { |
| 121 | return *mctrl; |
| 122 | } |
| 123 | |
Richard Genoud | 84130aa | 2014-05-13 20:20:43 +0200 | [diff] [blame] | 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 |
Uwe Kleine-König | ce59e48 | 2015-09-30 10:19:41 +0200 | [diff] [blame] | 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 |
Uwe Kleine-König | 7d8c70d | 2015-09-30 10:19:40 +0200 | [diff] [blame] | 138 | struct mctrl_gpios *mctrl_gpio_init_noauto(struct device *dev, unsigned int idx) |
Richard Genoud | 84130aa | 2014-05-13 20:20:43 +0200 | [diff] [blame] | 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 | |
Arnd Bergmann | 1b306f9 | 2015-10-12 17:04:14 +0200 | [diff] [blame] | 148 | static inline void mctrl_gpio_enable_ms(struct mctrl_gpios *gpios) |
Uwe Kleine-König | ce59e48 | 2015-09-30 10:19:41 +0200 | [diff] [blame] | 149 | { |
| 150 | } |
| 151 | |
Arnd Bergmann | 1b306f9 | 2015-10-12 17:04:14 +0200 | [diff] [blame] | 152 | static inline void mctrl_gpio_disable_ms(struct mctrl_gpios *gpios) |
Uwe Kleine-König | ce59e48 | 2015-09-30 10:19:41 +0200 | [diff] [blame] | 153 | { |
| 154 | } |
| 155 | |
Richard Genoud | 84130aa | 2014-05-13 20:20:43 +0200 | [diff] [blame] | 156 | #endif /* GPIOLIB */ |
| 157 | |
| 158 | #endif |