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