Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

tty/serial: at91: use mctrl_gpio helpers

On sam9x5, dedicated CTS (and RTS) pins are unusable together with the
LCDC, the EMAC, or the MMC because they share the same line.

Moreover, the USART controller doesn't handle DTR/DSR/DCD/RI signals,
so we have to control them via GPIO.

This patch permits to use GPIOs to control the CTS/RTS/DTR/DSR/DCD/RI
signals.

Signed-off-by: Richard Genoud <richard.genoud@gmail.com>
Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Acked-by: Nicolas Ferre <nicolas.ferre@atmel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Richard Genoud and committed by
Greg Kroah-Hartman
e0b0baad 84130aac

+88 -68
+8 -2
Documentation/devicetree/bindings/serial/atmel-usart.txt
··· 13 13 Optional properties: 14 14 - atmel,use-dma-rx: use of PDC or DMA for receiving data 15 15 - atmel,use-dma-tx: use of PDC or DMA for transmitting data 16 - - rts-gpios: specify a GPIO for RTS line. It will use specified PIO instead of the peripheral 17 - function pin for the USART RTS feature. If unsure, don't specify this property. 16 + - {rts,cts,dtr,dsr,rng,dcd}-gpios: specify a GPIO for RTS/CTS/DTR/DSR/RI/DCD line respectively. 17 + It will use specified PIO instead of the peripheral function pin for the USART feature. 18 + If unsure, don't specify this property. 18 19 - add dma bindings for dma transfer: 19 20 - dmas: DMA specifier, consisting of a phandle to DMA controller node, 20 21 memory peripheral interface and USART DMA channel ID, FIFO configuration. ··· 37 36 atmel,use-dma-rx; 38 37 atmel,use-dma-tx; 39 38 rts-gpios = <&pioD 15 GPIO_ACTIVE_LOW>; 39 + cts-gpios = <&pioD 16 GPIO_ACTIVE_LOW>; 40 + dtr-gpios = <&pioD 17 GPIO_ACTIVE_LOW>; 41 + dsr-gpios = <&pioD 18 GPIO_ACTIVE_LOW>; 42 + dcd-gpios = <&pioD 20 GPIO_ACTIVE_LOW>; 43 + rng-gpios = <&pioD 19 GPIO_ACTIVE_LOW>; 40 44 }; 41 45 42 46 - use DMA:
+10 -6
arch/arm/mach-at91/at91rm9200_devices.c
··· 15 15 16 16 #include <linux/dma-mapping.h> 17 17 #include <linux/gpio.h> 18 + #include <linux/gpio/driver.h> 18 19 #include <linux/platform_device.h> 19 20 #include <linux/i2c-gpio.h> 20 21 ··· 924 923 static struct atmel_uart_data dbgu_data = { 925 924 .use_dma_tx = 0, 926 925 .use_dma_rx = 0, /* DBGU not capable of receive DMA */ 927 - .rts_gpio = -EINVAL, 928 926 }; 929 927 930 928 static u64 dbgu_dmamask = DMA_BIT_MASK(32); ··· 962 962 static struct atmel_uart_data uart0_data = { 963 963 .use_dma_tx = 1, 964 964 .use_dma_rx = 1, 965 - .rts_gpio = -EINVAL, 965 + }; 966 + 967 + static struct gpiod_lookup_table uart0_gpios_table = { 968 + .dev_id = "atmel_usart", 969 + .table = { 970 + GPIO_LOOKUP("pioA", 21, "rts", GPIO_ACTIVE_LOW), 971 + { }, 972 + }, 966 973 }; 967 974 968 975 static u64 uart0_dmamask = DMA_BIT_MASK(32); ··· 1000 993 * We need to drive the pin manually. The serial driver will driver 1001 994 * this to high when initializing. 1002 995 */ 1003 - uart0_data.rts_gpio = AT91_PIN_PA21; 996 + gpiod_add_lookup_table(&uart0_gpios_table); 1004 997 } 1005 998 } 1006 999 ··· 1020 1013 static struct atmel_uart_data uart1_data = { 1021 1014 .use_dma_tx = 1, 1022 1015 .use_dma_rx = 1, 1023 - .rts_gpio = -EINVAL, 1024 1016 }; 1025 1017 1026 1018 static u64 uart1_dmamask = DMA_BIT_MASK(32); ··· 1071 1065 static struct atmel_uart_data uart2_data = { 1072 1066 .use_dma_tx = 1, 1073 1067 .use_dma_rx = 1, 1074 - .rts_gpio = -EINVAL, 1075 1068 }; 1076 1069 1077 1070 static u64 uart2_dmamask = DMA_BIT_MASK(32); ··· 1114 1109 static struct atmel_uart_data uart3_data = { 1115 1110 .use_dma_tx = 1, 1116 1111 .use_dma_rx = 1, 1117 - .rts_gpio = -EINVAL, 1118 1112 }; 1119 1113 1120 1114 static u64 uart3_dmamask = DMA_BIT_MASK(32);
-7
arch/arm/mach-at91/at91sam9260_devices.c
··· 820 820 static struct atmel_uart_data dbgu_data = { 821 821 .use_dma_tx = 0, 822 822 .use_dma_rx = 0, /* DBGU not capable of receive DMA */ 823 - .rts_gpio = -EINVAL, 824 823 }; 825 824 826 825 static u64 dbgu_dmamask = DMA_BIT_MASK(32); ··· 858 859 static struct atmel_uart_data uart0_data = { 859 860 .use_dma_tx = 1, 860 861 .use_dma_rx = 1, 861 - .rts_gpio = -EINVAL, 862 862 }; 863 863 864 864 static u64 uart0_dmamask = DMA_BIT_MASK(32); ··· 909 911 static struct atmel_uart_data uart1_data = { 910 912 .use_dma_tx = 1, 911 913 .use_dma_rx = 1, 912 - .rts_gpio = -EINVAL, 913 914 }; 914 915 915 916 static u64 uart1_dmamask = DMA_BIT_MASK(32); ··· 952 955 static struct atmel_uart_data uart2_data = { 953 956 .use_dma_tx = 1, 954 957 .use_dma_rx = 1, 955 - .rts_gpio = -EINVAL, 956 958 }; 957 959 958 960 static u64 uart2_dmamask = DMA_BIT_MASK(32); ··· 995 999 static struct atmel_uart_data uart3_data = { 996 1000 .use_dma_tx = 1, 997 1001 .use_dma_rx = 1, 998 - .rts_gpio = -EINVAL, 999 1002 }; 1000 1003 1001 1004 static u64 uart3_dmamask = DMA_BIT_MASK(32); ··· 1038 1043 static struct atmel_uart_data uart4_data = { 1039 1044 .use_dma_tx = 1, 1040 1045 .use_dma_rx = 1, 1041 - .rts_gpio = -EINVAL, 1042 1046 }; 1043 1047 1044 1048 static u64 uart4_dmamask = DMA_BIT_MASK(32); ··· 1076 1082 static struct atmel_uart_data uart5_data = { 1077 1083 .use_dma_tx = 1, 1078 1084 .use_dma_rx = 1, 1079 - .rts_gpio = -EINVAL, 1080 1085 }; 1081 1086 1082 1087 static u64 uart5_dmamask = DMA_BIT_MASK(32);
-4
arch/arm/mach-at91/at91sam9261_devices.c
··· 881 881 static struct atmel_uart_data dbgu_data = { 882 882 .use_dma_tx = 0, 883 883 .use_dma_rx = 0, /* DBGU not capable of receive DMA */ 884 - .rts_gpio = -EINVAL, 885 884 }; 886 885 887 886 static u64 dbgu_dmamask = DMA_BIT_MASK(32); ··· 919 920 static struct atmel_uart_data uart0_data = { 920 921 .use_dma_tx = 1, 921 922 .use_dma_rx = 1, 922 - .rts_gpio = -EINVAL, 923 923 }; 924 924 925 925 static u64 uart0_dmamask = DMA_BIT_MASK(32); ··· 962 964 static struct atmel_uart_data uart1_data = { 963 965 .use_dma_tx = 1, 964 966 .use_dma_rx = 1, 965 - .rts_gpio = -EINVAL, 966 967 }; 967 968 968 969 static u64 uart1_dmamask = DMA_BIT_MASK(32); ··· 1005 1008 static struct atmel_uart_data uart2_data = { 1006 1009 .use_dma_tx = 1, 1007 1010 .use_dma_rx = 1, 1008 - .rts_gpio = -EINVAL, 1009 1011 }; 1010 1012 1011 1013 static u64 uart2_dmamask = DMA_BIT_MASK(32);
-4
arch/arm/mach-at91/at91sam9263_devices.c
··· 1325 1325 static struct atmel_uart_data dbgu_data = { 1326 1326 .use_dma_tx = 0, 1327 1327 .use_dma_rx = 0, /* DBGU not capable of receive DMA */ 1328 - .rts_gpio = -EINVAL, 1329 1328 }; 1330 1329 1331 1330 static u64 dbgu_dmamask = DMA_BIT_MASK(32); ··· 1363 1364 static struct atmel_uart_data uart0_data = { 1364 1365 .use_dma_tx = 1, 1365 1366 .use_dma_rx = 1, 1366 - .rts_gpio = -EINVAL, 1367 1367 }; 1368 1368 1369 1369 static u64 uart0_dmamask = DMA_BIT_MASK(32); ··· 1406 1408 static struct atmel_uart_data uart1_data = { 1407 1409 .use_dma_tx = 1, 1408 1410 .use_dma_rx = 1, 1409 - .rts_gpio = -EINVAL, 1410 1411 }; 1411 1412 1412 1413 static u64 uart1_dmamask = DMA_BIT_MASK(32); ··· 1449 1452 static struct atmel_uart_data uart2_data = { 1450 1453 .use_dma_tx = 1, 1451 1454 .use_dma_rx = 1, 1452 - .rts_gpio = -EINVAL, 1453 1455 }; 1454 1456 1455 1457 static u64 uart2_dmamask = DMA_BIT_MASK(32);
-5
arch/arm/mach-at91/at91sam9g45_devices.c
··· 1588 1588 static struct atmel_uart_data dbgu_data = { 1589 1589 .use_dma_tx = 0, 1590 1590 .use_dma_rx = 0, 1591 - .rts_gpio = -EINVAL, 1592 1591 }; 1593 1592 1594 1593 static u64 dbgu_dmamask = DMA_BIT_MASK(32); ··· 1626 1627 static struct atmel_uart_data uart0_data = { 1627 1628 .use_dma_tx = 1, 1628 1629 .use_dma_rx = 1, 1629 - .rts_gpio = -EINVAL, 1630 1630 }; 1631 1631 1632 1632 static u64 uart0_dmamask = DMA_BIT_MASK(32); ··· 1669 1671 static struct atmel_uart_data uart1_data = { 1670 1672 .use_dma_tx = 1, 1671 1673 .use_dma_rx = 1, 1672 - .rts_gpio = -EINVAL, 1673 1674 }; 1674 1675 1675 1676 static u64 uart1_dmamask = DMA_BIT_MASK(32); ··· 1712 1715 static struct atmel_uart_data uart2_data = { 1713 1716 .use_dma_tx = 1, 1714 1717 .use_dma_rx = 1, 1715 - .rts_gpio = -EINVAL, 1716 1718 }; 1717 1719 1718 1720 static u64 uart2_dmamask = DMA_BIT_MASK(32); ··· 1755 1759 static struct atmel_uart_data uart3_data = { 1756 1760 .use_dma_tx = 1, 1757 1761 .use_dma_rx = 1, 1758 - .rts_gpio = -EINVAL, 1759 1762 }; 1760 1763 1761 1764 static u64 uart3_dmamask = DMA_BIT_MASK(32);
-5
arch/arm/mach-at91/at91sam9rl_devices.c
··· 957 957 static struct atmel_uart_data dbgu_data = { 958 958 .use_dma_tx = 0, 959 959 .use_dma_rx = 0, /* DBGU not capable of receive DMA */ 960 - .rts_gpio = -EINVAL, 961 960 }; 962 961 963 962 static u64 dbgu_dmamask = DMA_BIT_MASK(32); ··· 995 996 static struct atmel_uart_data uart0_data = { 996 997 .use_dma_tx = 1, 997 998 .use_dma_rx = 1, 998 - .rts_gpio = -EINVAL, 999 999 }; 1000 1000 1001 1001 static u64 uart0_dmamask = DMA_BIT_MASK(32); ··· 1046 1048 static struct atmel_uart_data uart1_data = { 1047 1049 .use_dma_tx = 1, 1048 1050 .use_dma_rx = 1, 1049 - .rts_gpio = -EINVAL, 1050 1051 }; 1051 1052 1052 1053 static u64 uart1_dmamask = DMA_BIT_MASK(32); ··· 1089 1092 static struct atmel_uart_data uart2_data = { 1090 1093 .use_dma_tx = 1, 1091 1094 .use_dma_rx = 1, 1092 - .rts_gpio = -EINVAL, 1093 1095 }; 1094 1096 1095 1097 static u64 uart2_dmamask = DMA_BIT_MASK(32); ··· 1132 1136 static struct atmel_uart_data uart3_data = { 1133 1137 .use_dma_tx = 1, 1134 1138 .use_dma_rx = 1, 1135 - .rts_gpio = -EINVAL, 1136 1139 }; 1137 1140 1138 1141 static u64 uart3_dmamask = DMA_BIT_MASK(32);
+1
drivers/tty/serial/Kconfig
··· 117 117 bool "AT91 / AT32 on-chip serial port support" 118 118 depends on ARCH_AT91 || AVR32 119 119 select SERIAL_CORE 120 + select SERIAL_MCTRL_GPIO 120 121 help 121 122 This enables the driver for the on-chip UARTs of the Atmel 122 123 AT91 and AT32 processors.
+69 -34
drivers/tty/serial/atmel_serial.c
··· 43 43 #include <linux/platform_data/atmel.h> 44 44 #include <linux/timer.h> 45 45 #include <linux/gpio.h> 46 + #include <linux/gpio/consumer.h> 47 + #include <linux/err.h> 46 48 47 49 #include <asm/io.h> 48 50 #include <asm/ioctls.h> ··· 58 56 #endif 59 57 60 58 #include <linux/serial_core.h> 59 + 60 + #include "serial_mctrl_gpio.h" 61 61 62 62 static void atmel_start_rx(struct uart_port *port); 63 63 static void atmel_stop_rx(struct uart_port *port); ··· 166 162 struct circ_buf rx_ring; 167 163 168 164 struct serial_rs485 rs485; /* rs485 settings */ 169 - int rts_gpio; /* optional RTS GPIO */ 165 + struct mctrl_gpios *gpios; 170 166 unsigned int tx_done_mask; 171 167 bool is_usart; /* usart or uart */ 172 168 struct timer_list uart_timer; /* uart timer */ ··· 241 237 return atmel_port->use_dma_rx; 242 238 } 243 239 240 + static unsigned int atmel_get_lines_status(struct uart_port *port) 241 + { 242 + struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 243 + unsigned int status, ret = 0; 244 + 245 + status = UART_GET_CSR(port); 246 + 247 + mctrl_gpio_get(atmel_port->gpios, &ret); 248 + 249 + if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios, 250 + UART_GPIO_CTS))) { 251 + if (ret & TIOCM_CTS) 252 + status &= ~ATMEL_US_CTS; 253 + else 254 + status |= ATMEL_US_CTS; 255 + } 256 + 257 + if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios, 258 + UART_GPIO_DSR))) { 259 + if (ret & TIOCM_DSR) 260 + status &= ~ATMEL_US_DSR; 261 + else 262 + status |= ATMEL_US_DSR; 263 + } 264 + 265 + if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios, 266 + UART_GPIO_RI))) { 267 + if (ret & TIOCM_RI) 268 + status &= ~ATMEL_US_RI; 269 + else 270 + status |= ATMEL_US_RI; 271 + } 272 + 273 + if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios, 274 + UART_GPIO_DCD))) { 275 + if (ret & TIOCM_CD) 276 + status &= ~ATMEL_US_DCD; 277 + else 278 + status |= ATMEL_US_DCD; 279 + } 280 + 281 + return status; 282 + } 283 + 244 284 /* Enable or disable the rs485 support */ 245 285 void atmel_config_rs485(struct uart_port *port, struct serial_rs485 *rs485conf) 246 286 { ··· 344 296 unsigned int mode; 345 297 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 346 298 347 - /* 348 - * AT91RM9200 Errata #39: RTS0 is not internally connected 349 - * to PA21. We need to drive the pin as a GPIO. 350 - */ 351 - if (gpio_is_valid(atmel_port->rts_gpio)) { 352 - if (mctrl & TIOCM_RTS) 353 - gpio_set_value(atmel_port->rts_gpio, 0); 354 - else 355 - gpio_set_value(atmel_port->rts_gpio, 1); 356 - } 357 - 358 299 if (mctrl & TIOCM_RTS) 359 300 control |= ATMEL_US_RTSEN; 360 301 else ··· 355 318 control |= ATMEL_US_DTRDIS; 356 319 357 320 UART_PUT_CR(port, control); 321 + 322 + mctrl_gpio_set(atmel_port->gpios, mctrl); 358 323 359 324 /* Local loopback mode? */ 360 325 mode = UART_GET_MR(port) & ~ATMEL_US_CHMODE; ··· 385 346 */ 386 347 static u_int atmel_get_mctrl(struct uart_port *port) 387 348 { 388 - unsigned int status, ret = 0; 349 + struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 350 + unsigned int ret = 0, status; 389 351 390 352 status = UART_GET_CSR(port); 391 353 ··· 402 362 if (!(status & ATMEL_US_RI)) 403 363 ret |= TIOCM_RI; 404 364 405 - return ret; 365 + return mctrl_gpio_get(atmel_port->gpios, &ret); 406 366 } 407 367 408 368 /* ··· 1082 1042 unsigned int status, pending, pass_counter = 0; 1083 1043 1084 1044 do { 1085 - status = UART_GET_CSR(port); 1045 + status = atmel_get_lines_status(port); 1086 1046 pending = status & UART_GET_IMR(port); 1087 1047 if (!pending) 1088 1048 break; ··· 1608 1568 } 1609 1569 1610 1570 /* Save current CSR for comparison in atmel_tasklet_func() */ 1611 - atmel_port->irq_status_prev = UART_GET_CSR(port); 1571 + atmel_port->irq_status_prev = atmel_get_lines_status(port); 1612 1572 atmel_port->irq_status = atmel_port->irq_status_prev; 1613 1573 1614 1574 /* ··· 2364 2324 #define atmel_serial_resume NULL 2365 2325 #endif 2366 2326 2327 + static int atmel_init_gpios(struct atmel_uart_port *p, struct device *dev) 2328 + { 2329 + p->gpios = mctrl_gpio_init(dev, 0); 2330 + if (IS_ERR_OR_NULL(p->gpios)) 2331 + return -1; 2332 + 2333 + return 0; 2334 + } 2335 + 2367 2336 static int atmel_serial_probe(struct platform_device *pdev) 2368 2337 { 2369 2338 struct atmel_uart_port *port; ··· 2408 2359 port = &atmel_ports[ret]; 2409 2360 port->backup_imr = 0; 2410 2361 port->uart.line = ret; 2411 - port->rts_gpio = -EINVAL; /* Invalid, zero could be valid */ 2412 - if (pdata) 2413 - port->rts_gpio = pdata->rts_gpio; 2414 - else if (np) 2415 - port->rts_gpio = of_get_named_gpio(np, "rts-gpios", 0); 2416 2362 2417 - if (gpio_is_valid(port->rts_gpio)) { 2418 - ret = devm_gpio_request(&pdev->dev, port->rts_gpio, "RTS"); 2419 - if (ret) { 2420 - dev_err(&pdev->dev, "error requesting RTS GPIO\n"); 2421 - goto err; 2422 - } 2423 - /* Default to 1 as RTS is active low */ 2424 - ret = gpio_direction_output(port->rts_gpio, 1); 2425 - if (ret) { 2426 - dev_err(&pdev->dev, "error setting up RTS GPIO\n"); 2427 - goto err; 2428 - } 2429 - } 2363 + ret = atmel_init_gpios(port, &pdev->dev); 2364 + if (ret < 0) 2365 + dev_err(&pdev->dev, "%s", 2366 + "Failed to initialize GPIOs. The serial port may not work as expected"); 2430 2367 2431 2368 ret = atmel_init_port(port, pdev); 2432 2369 if (ret)
-1
include/linux/platform_data/atmel.h
··· 84 84 short use_dma_rx; /* use receive DMA? */ 85 85 void __iomem *regs; /* virt. base address, if any */ 86 86 struct serial_rs485 rs485; /* rs485 settings */ 87 - int rts_gpio; /* optional RTS GPIO */ 88 87 }; 89 88 90 89 /* Touchscreen Controller */