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

USB: serial: ftdi_sio: implement GPIO support for FT-X devices

This patch allows using the CBUS pins of FT-X devices as GPIO in CBUS
bitbanging mode. There is no conflict between the GPIO and VCP
functionality in this mode. Tested on FT230X and FT231X.

As there is no way to request the current CBUS register configuration
from the device, all CBUS pins are set to a known state when the first
GPIO is requested. This allows using libftdi to set the GPIO pins
before loading this module for UART functionality, a behavior that
existing applications might be relying upon (though no specific case
is known to the authors of this patch).

Signed-off-by: Karoly Pados <pados@pados.hu>
[ johan: minor style changes ]
Signed-off-by: Johan Hovold <johan@kernel.org>

authored by

Karoly Pados and committed by
Johan Hovold
ba93cc7d bbc1f57a

+385 -2
+359 -1
drivers/usb/serial/ftdi_sio.c
··· 39 39 #include <linux/uaccess.h> 40 40 #include <linux/usb.h> 41 41 #include <linux/serial.h> 42 + #include <linux/gpio/driver.h> 42 43 #include <linux/usb/serial.h> 43 44 #include "ftdi_sio.h" 44 45 #include "ftdi_sio_ids.h" ··· 73 72 unsigned int latency; /* latency setting in use */ 74 73 unsigned short max_packet_size; 75 74 struct mutex cfg_lock; /* Avoid mess by parallel calls of config ioctl() and change_speed() */ 75 + #ifdef CONFIG_GPIOLIB 76 + struct gpio_chip gc; 77 + struct mutex gpio_lock; /* protects GPIO state */ 78 + bool gpio_registered; /* is the gpiochip in kernel registered */ 79 + bool gpio_used; /* true if the user requested a gpio */ 80 + u8 gpio_altfunc; /* which pins are in gpio mode */ 81 + u8 gpio_output; /* pin directions cache */ 82 + u8 gpio_value; /* pin value for outputs */ 83 + #endif 76 84 }; 77 85 78 86 /* struct ftdi_sio_quirk is used by devices requiring special attention. */ ··· 1776 1766 1777 1767 } 1778 1768 1769 + #ifdef CONFIG_GPIOLIB 1770 + 1771 + static const char * const ftdi_ftx_gpio_names[] = { 1772 + "CBUS0", "CBUS1", "CBUS2", "CBUS3" 1773 + }; 1774 + 1775 + static int ftdi_set_bitmode(struct usb_serial_port *port, u8 mode) 1776 + { 1777 + struct ftdi_private *priv = usb_get_serial_port_data(port); 1778 + struct usb_serial *serial = port->serial; 1779 + int result; 1780 + u16 val; 1781 + 1782 + val = (mode << 8) | (priv->gpio_output << 4) | priv->gpio_value; 1783 + result = usb_control_msg(serial->dev, 1784 + usb_sndctrlpipe(serial->dev, 0), 1785 + FTDI_SIO_SET_BITMODE_REQUEST, 1786 + FTDI_SIO_SET_BITMODE_REQUEST_TYPE, val, 1787 + priv->interface, NULL, 0, WDR_TIMEOUT); 1788 + if (result < 0) { 1789 + dev_err(&serial->interface->dev, 1790 + "bitmode request failed for value 0x%04x: %d\n", 1791 + val, result); 1792 + } 1793 + 1794 + return result; 1795 + } 1796 + 1797 + static int ftdi_set_cbus_pins(struct usb_serial_port *port) 1798 + { 1799 + return ftdi_set_bitmode(port, FTDI_SIO_BITMODE_CBUS); 1800 + } 1801 + 1802 + static int ftdi_exit_cbus_mode(struct usb_serial_port *port) 1803 + { 1804 + struct ftdi_private *priv = usb_get_serial_port_data(port); 1805 + 1806 + priv->gpio_output = 0; 1807 + priv->gpio_value = 0; 1808 + return ftdi_set_bitmode(port, FTDI_SIO_BITMODE_RESET); 1809 + } 1810 + 1811 + static int ftdi_gpio_request(struct gpio_chip *gc, unsigned int offset) 1812 + { 1813 + struct usb_serial_port *port = gpiochip_get_data(gc); 1814 + struct ftdi_private *priv = usb_get_serial_port_data(port); 1815 + int result; 1816 + 1817 + if (priv->gpio_altfunc & BIT(offset)) 1818 + return -ENODEV; 1819 + 1820 + mutex_lock(&priv->gpio_lock); 1821 + if (!priv->gpio_used) { 1822 + /* Set default pin states, as we cannot get them from device */ 1823 + priv->gpio_output = 0x00; 1824 + priv->gpio_value = 0x00; 1825 + result = ftdi_set_cbus_pins(port); 1826 + if (result) { 1827 + mutex_unlock(&priv->gpio_lock); 1828 + return result; 1829 + } 1830 + 1831 + priv->gpio_used = true; 1832 + } 1833 + mutex_unlock(&priv->gpio_lock); 1834 + 1835 + return 0; 1836 + } 1837 + 1838 + static int ftdi_read_cbus_pins(struct usb_serial_port *port) 1839 + { 1840 + struct ftdi_private *priv = usb_get_serial_port_data(port); 1841 + struct usb_serial *serial = port->serial; 1842 + unsigned char *buf; 1843 + int result; 1844 + 1845 + buf = kmalloc(1, GFP_KERNEL); 1846 + if (!buf) 1847 + return -ENOMEM; 1848 + 1849 + result = usb_control_msg(serial->dev, 1850 + usb_rcvctrlpipe(serial->dev, 0), 1851 + FTDI_SIO_READ_PINS_REQUEST, 1852 + FTDI_SIO_READ_PINS_REQUEST_TYPE, 0, 1853 + priv->interface, buf, 1, WDR_TIMEOUT); 1854 + if (result < 1) { 1855 + if (result >= 0) 1856 + result = -EIO; 1857 + } else { 1858 + result = buf[0]; 1859 + } 1860 + 1861 + kfree(buf); 1862 + 1863 + return result; 1864 + } 1865 + 1866 + static int ftdi_gpio_get(struct gpio_chip *gc, unsigned int gpio) 1867 + { 1868 + struct usb_serial_port *port = gpiochip_get_data(gc); 1869 + int result; 1870 + 1871 + result = ftdi_read_cbus_pins(port); 1872 + if (result < 0) 1873 + return result; 1874 + 1875 + return !!(result & BIT(gpio)); 1876 + } 1877 + 1878 + static void ftdi_gpio_set(struct gpio_chip *gc, unsigned int gpio, int value) 1879 + { 1880 + struct usb_serial_port *port = gpiochip_get_data(gc); 1881 + struct ftdi_private *priv = usb_get_serial_port_data(port); 1882 + 1883 + mutex_lock(&priv->gpio_lock); 1884 + 1885 + if (value) 1886 + priv->gpio_value |= BIT(gpio); 1887 + else 1888 + priv->gpio_value &= ~BIT(gpio); 1889 + 1890 + ftdi_set_cbus_pins(port); 1891 + 1892 + mutex_unlock(&priv->gpio_lock); 1893 + } 1894 + 1895 + static int ftdi_gpio_get_multiple(struct gpio_chip *gc, unsigned long *mask, 1896 + unsigned long *bits) 1897 + { 1898 + struct usb_serial_port *port = gpiochip_get_data(gc); 1899 + int result; 1900 + 1901 + result = ftdi_read_cbus_pins(port); 1902 + if (result < 0) 1903 + return result; 1904 + 1905 + *bits = result & *mask; 1906 + 1907 + return 0; 1908 + } 1909 + 1910 + static void ftdi_gpio_set_multiple(struct gpio_chip *gc, unsigned long *mask, 1911 + unsigned long *bits) 1912 + { 1913 + struct usb_serial_port *port = gpiochip_get_data(gc); 1914 + struct ftdi_private *priv = usb_get_serial_port_data(port); 1915 + 1916 + mutex_lock(&priv->gpio_lock); 1917 + 1918 + priv->gpio_value &= ~(*mask); 1919 + priv->gpio_value |= *bits & *mask; 1920 + ftdi_set_cbus_pins(port); 1921 + 1922 + mutex_unlock(&priv->gpio_lock); 1923 + } 1924 + 1925 + static int ftdi_gpio_direction_get(struct gpio_chip *gc, unsigned int gpio) 1926 + { 1927 + struct usb_serial_port *port = gpiochip_get_data(gc); 1928 + struct ftdi_private *priv = usb_get_serial_port_data(port); 1929 + 1930 + return !(priv->gpio_output & BIT(gpio)); 1931 + } 1932 + 1933 + static int ftdi_gpio_direction_input(struct gpio_chip *gc, unsigned int gpio) 1934 + { 1935 + struct usb_serial_port *port = gpiochip_get_data(gc); 1936 + struct ftdi_private *priv = usb_get_serial_port_data(port); 1937 + int result; 1938 + 1939 + mutex_lock(&priv->gpio_lock); 1940 + 1941 + priv->gpio_output &= ~BIT(gpio); 1942 + result = ftdi_set_cbus_pins(port); 1943 + 1944 + mutex_unlock(&priv->gpio_lock); 1945 + 1946 + return result; 1947 + } 1948 + 1949 + static int ftdi_gpio_direction_output(struct gpio_chip *gc, unsigned int gpio, 1950 + int value) 1951 + { 1952 + struct usb_serial_port *port = gpiochip_get_data(gc); 1953 + struct ftdi_private *priv = usb_get_serial_port_data(port); 1954 + int result; 1955 + 1956 + mutex_lock(&priv->gpio_lock); 1957 + 1958 + priv->gpio_output |= BIT(gpio); 1959 + if (value) 1960 + priv->gpio_value |= BIT(gpio); 1961 + else 1962 + priv->gpio_value &= ~BIT(gpio); 1963 + 1964 + result = ftdi_set_cbus_pins(port); 1965 + 1966 + mutex_unlock(&priv->gpio_lock); 1967 + 1968 + return result; 1969 + } 1970 + 1971 + static int ftdi_read_eeprom(struct usb_serial *serial, void *dst, u16 addr, 1972 + u16 nbytes) 1973 + { 1974 + int read = 0; 1975 + 1976 + if (addr % 2 != 0) 1977 + return -EINVAL; 1978 + if (nbytes % 2 != 0) 1979 + return -EINVAL; 1980 + 1981 + /* Read EEPROM two bytes at a time */ 1982 + while (read < nbytes) { 1983 + int rv; 1984 + 1985 + rv = usb_control_msg(serial->dev, 1986 + usb_rcvctrlpipe(serial->dev, 0), 1987 + FTDI_SIO_READ_EEPROM_REQUEST, 1988 + FTDI_SIO_READ_EEPROM_REQUEST_TYPE, 1989 + 0, (addr + read) / 2, dst + read, 2, 1990 + WDR_TIMEOUT); 1991 + if (rv < 2) { 1992 + if (rv >= 0) 1993 + return -EIO; 1994 + else 1995 + return rv; 1996 + } 1997 + 1998 + read += rv; 1999 + } 2000 + 2001 + return 0; 2002 + } 2003 + 2004 + static int ftx_gpioconf_init(struct usb_serial_port *port) 2005 + { 2006 + struct ftdi_private *priv = usb_get_serial_port_data(port); 2007 + struct usb_serial *serial = port->serial; 2008 + const u16 cbus_cfg_addr = 0x1a; 2009 + const u16 cbus_cfg_size = 4; 2010 + u8 *cbus_cfg_buf; 2011 + int result; 2012 + u8 i; 2013 + 2014 + cbus_cfg_buf = kmalloc(cbus_cfg_size, GFP_KERNEL); 2015 + if (!cbus_cfg_buf) 2016 + return -ENOMEM; 2017 + 2018 + result = ftdi_read_eeprom(serial, cbus_cfg_buf, 2019 + cbus_cfg_addr, cbus_cfg_size); 2020 + if (result < 0) 2021 + goto out_free; 2022 + 2023 + /* FIXME: FT234XD alone has 1 GPIO, but how to recognize this IC? */ 2024 + priv->gc.ngpio = 4; 2025 + priv->gc.names = ftdi_ftx_gpio_names; 2026 + 2027 + /* Determine which pins are configured for CBUS bitbanging */ 2028 + priv->gpio_altfunc = 0xff; 2029 + for (i = 0; i < priv->gc.ngpio; ++i) { 2030 + if (cbus_cfg_buf[i] == FTDI_FTX_CBUS_MUX_GPIO) 2031 + priv->gpio_altfunc &= ~BIT(i); 2032 + } 2033 + 2034 + out_free: 2035 + kfree(cbus_cfg_buf); 2036 + 2037 + return result; 2038 + } 2039 + 2040 + static int ftdi_gpio_init(struct usb_serial_port *port) 2041 + { 2042 + struct ftdi_private *priv = usb_get_serial_port_data(port); 2043 + struct usb_serial *serial = port->serial; 2044 + int result; 2045 + 2046 + switch (priv->chip_type) { 2047 + case FTX: 2048 + result = ftx_gpioconf_init(port); 2049 + break; 2050 + default: 2051 + return 0; 2052 + } 2053 + 2054 + if (result < 0) 2055 + return result; 2056 + 2057 + mutex_init(&priv->gpio_lock); 2058 + 2059 + priv->gc.label = "ftdi-cbus"; 2060 + priv->gc.request = ftdi_gpio_request; 2061 + priv->gc.get_direction = ftdi_gpio_direction_get; 2062 + priv->gc.direction_input = ftdi_gpio_direction_input; 2063 + priv->gc.direction_output = ftdi_gpio_direction_output; 2064 + priv->gc.get = ftdi_gpio_get; 2065 + priv->gc.set = ftdi_gpio_set; 2066 + priv->gc.get_multiple = ftdi_gpio_get_multiple; 2067 + priv->gc.set_multiple = ftdi_gpio_set_multiple; 2068 + priv->gc.owner = THIS_MODULE; 2069 + priv->gc.parent = &serial->interface->dev; 2070 + priv->gc.base = -1; 2071 + priv->gc.can_sleep = true; 2072 + 2073 + result = gpiochip_add_data(&priv->gc, port); 2074 + if (!result) 2075 + priv->gpio_registered = true; 2076 + 2077 + return result; 2078 + } 2079 + 2080 + static void ftdi_gpio_remove(struct usb_serial_port *port) 2081 + { 2082 + struct ftdi_private *priv = usb_get_serial_port_data(port); 2083 + 2084 + if (priv->gpio_registered) { 2085 + gpiochip_remove(&priv->gc); 2086 + priv->gpio_registered = false; 2087 + } 2088 + 2089 + if (priv->gpio_used) { 2090 + /* Exiting CBUS-mode does not reset pin states. */ 2091 + ftdi_exit_cbus_mode(port); 2092 + priv->gpio_used = false; 2093 + } 2094 + } 2095 + 2096 + #else 2097 + 2098 + static int ftdi_gpio_init(struct usb_serial_port *port) 2099 + { 2100 + return 0; 2101 + } 2102 + 2103 + static void ftdi_gpio_remove(struct usb_serial_port *port) { } 2104 + 2105 + #endif /* CONFIG_GPIOLIB */ 2106 + 1779 2107 /* 1780 2108 * *************************************************************************** 1781 2109 * FTDI driver specific functions ··· 2142 1794 { 2143 1795 struct ftdi_private *priv; 2144 1796 const struct ftdi_sio_quirk *quirk = usb_get_serial_data(port->serial); 2145 - 1797 + int result; 2146 1798 2147 1799 priv = kzalloc(sizeof(struct ftdi_private), GFP_KERNEL); 2148 1800 if (!priv) ··· 2161 1813 priv->latency = 16; 2162 1814 write_latency_timer(port); 2163 1815 create_sysfs_attrs(port); 1816 + 1817 + result = ftdi_gpio_init(port); 1818 + if (result < 0) { 1819 + dev_err(&port->serial->interface->dev, 1820 + "GPIO initialisation failed: %d\n", 1821 + result); 1822 + } 1823 + 2164 1824 return 0; 2165 1825 } 2166 1826 ··· 2285 1929 static int ftdi_sio_port_remove(struct usb_serial_port *port) 2286 1930 { 2287 1931 struct ftdi_private *priv = usb_get_serial_port_data(port); 1932 + 1933 + ftdi_gpio_remove(port); 2288 1934 2289 1935 remove_sysfs_attrs(port); 2290 1936
+26 -1
drivers/usb/serial/ftdi_sio.h
··· 35 35 #define FTDI_SIO_SET_EVENT_CHAR 6 /* Set the event character */ 36 36 #define FTDI_SIO_SET_ERROR_CHAR 7 /* Set the error character */ 37 37 #define FTDI_SIO_SET_LATENCY_TIMER 9 /* Set the latency timer */ 38 - #define FTDI_SIO_GET_LATENCY_TIMER 10 /* Get the latency timer */ 38 + #define FTDI_SIO_GET_LATENCY_TIMER 0x0a /* Get the latency timer */ 39 + #define FTDI_SIO_SET_BITMODE 0x0b /* Set bitbang mode */ 40 + #define FTDI_SIO_READ_PINS 0x0c /* Read immediate value of pins */ 41 + #define FTDI_SIO_READ_EEPROM 0x90 /* Read EEPROM */ 39 42 40 43 /* Interface indices for FT2232, FT2232H and FT4232H devices */ 41 44 #define INTERFACE_A 1 ··· 436 433 * 1 = active 437 434 */ 438 435 436 + /* FTDI_SIO_SET_BITMODE */ 437 + #define FTDI_SIO_SET_BITMODE_REQUEST_TYPE 0x40 438 + #define FTDI_SIO_SET_BITMODE_REQUEST FTDI_SIO_SET_BITMODE 439 + 440 + /* Possible bitmodes for FTDI_SIO_SET_BITMODE_REQUEST */ 441 + #define FTDI_SIO_BITMODE_RESET 0x00 442 + #define FTDI_SIO_BITMODE_CBUS 0x20 443 + 444 + /* FTDI_SIO_READ_PINS */ 445 + #define FTDI_SIO_READ_PINS_REQUEST_TYPE 0xc0 446 + #define FTDI_SIO_READ_PINS_REQUEST FTDI_SIO_READ_PINS 447 + 448 + /* 449 + * FTDI_SIO_READ_EEPROM 450 + * 451 + * EEPROM format found in FTDI AN_201, "FT-X MTP memory Configuration", 452 + * http://www.ftdichip.com/Support/Documents/AppNotes/AN_201_FT-X%20MTP%20Memory%20Configuration.pdf 453 + */ 454 + #define FTDI_SIO_READ_EEPROM_REQUEST_TYPE 0xc0 455 + #define FTDI_SIO_READ_EEPROM_REQUEST FTDI_SIO_READ_EEPROM 456 + 457 + #define FTDI_FTX_CBUS_MUX_GPIO 8 439 458 440 459 441 460 /* Descriptors returned by the device