Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 1 | /* |
Alexander Shiyan | 003236d | 2013-06-29 10:44:19 +0400 | [diff] [blame] | 2 | * Maxim (Dallas) MAX3107/8/9, MAX14830 serial driver |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 3 | * |
Alexander Shiyan | e97e155 | 2014-02-07 18:16:04 +0400 | [diff] [blame] | 4 | * Copyright (C) 2012-2014 Alexander Shiyan <shc_work@mail.ru> |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 5 | * |
| 6 | * Based on max3100.c, by Christian Pellegrin <chripell@evolware.org> |
| 7 | * Based on max3110.c, by Feng Tang <feng.tang@intel.com> |
| 8 | * Based on max3107.c, by Aavamobile |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | */ |
| 15 | |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 16 | #include <linux/bitops.h> |
Alexander Shiyan | d3a8a25 | 2014-02-10 22:18:31 +0400 | [diff] [blame] | 17 | #include <linux/clk.h> |
Alexander Shiyan | 5f52904 | 2014-02-10 22:18:35 +0400 | [diff] [blame] | 18 | #include <linux/delay.h> |
| 19 | #include <linux/device.h> |
| 20 | #include <linux/gpio.h> |
| 21 | #include <linux/module.h> |
Alexander Shiyan | 58afc90 | 2014-02-10 22:18:36 +0400 | [diff] [blame] | 22 | #include <linux/of.h> |
| 23 | #include <linux/of_device.h> |
Alexander Shiyan | 5f52904 | 2014-02-10 22:18:35 +0400 | [diff] [blame] | 24 | #include <linux/regmap.h> |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 25 | #include <linux/serial_core.h> |
| 26 | #include <linux/serial.h> |
| 27 | #include <linux/tty.h> |
| 28 | #include <linux/tty_flip.h> |
Greg Kroah-Hartman | 1456dad | 2014-02-13 15:18:57 -0800 | [diff] [blame] | 29 | #include <linux/spi/spi.h> |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 30 | |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 31 | #define MAX310X_NAME "max310x" |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 32 | #define MAX310X_MAJOR 204 |
| 33 | #define MAX310X_MINOR 209 |
| 34 | |
| 35 | /* MAX310X register definitions */ |
| 36 | #define MAX310X_RHR_REG (0x00) /* RX FIFO */ |
| 37 | #define MAX310X_THR_REG (0x00) /* TX FIFO */ |
| 38 | #define MAX310X_IRQEN_REG (0x01) /* IRQ enable */ |
| 39 | #define MAX310X_IRQSTS_REG (0x02) /* IRQ status */ |
| 40 | #define MAX310X_LSR_IRQEN_REG (0x03) /* LSR IRQ enable */ |
| 41 | #define MAX310X_LSR_IRQSTS_REG (0x04) /* LSR IRQ status */ |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 42 | #define MAX310X_REG_05 (0x05) |
| 43 | #define MAX310X_SPCHR_IRQEN_REG MAX310X_REG_05 /* Special char IRQ en */ |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 44 | #define MAX310X_SPCHR_IRQSTS_REG (0x06) /* Special char IRQ status */ |
| 45 | #define MAX310X_STS_IRQEN_REG (0x07) /* Status IRQ enable */ |
| 46 | #define MAX310X_STS_IRQSTS_REG (0x08) /* Status IRQ status */ |
| 47 | #define MAX310X_MODE1_REG (0x09) /* MODE1 */ |
| 48 | #define MAX310X_MODE2_REG (0x0a) /* MODE2 */ |
| 49 | #define MAX310X_LCR_REG (0x0b) /* LCR */ |
| 50 | #define MAX310X_RXTO_REG (0x0c) /* RX timeout */ |
| 51 | #define MAX310X_HDPIXDELAY_REG (0x0d) /* Auto transceiver delays */ |
| 52 | #define MAX310X_IRDA_REG (0x0e) /* IRDA settings */ |
| 53 | #define MAX310X_FLOWLVL_REG (0x0f) /* Flow control levels */ |
| 54 | #define MAX310X_FIFOTRIGLVL_REG (0x10) /* FIFO IRQ trigger levels */ |
| 55 | #define MAX310X_TXFIFOLVL_REG (0x11) /* TX FIFO level */ |
| 56 | #define MAX310X_RXFIFOLVL_REG (0x12) /* RX FIFO level */ |
| 57 | #define MAX310X_FLOWCTRL_REG (0x13) /* Flow control */ |
| 58 | #define MAX310X_XON1_REG (0x14) /* XON1 character */ |
| 59 | #define MAX310X_XON2_REG (0x15) /* XON2 character */ |
| 60 | #define MAX310X_XOFF1_REG (0x16) /* XOFF1 character */ |
| 61 | #define MAX310X_XOFF2_REG (0x17) /* XOFF2 character */ |
| 62 | #define MAX310X_GPIOCFG_REG (0x18) /* GPIO config */ |
| 63 | #define MAX310X_GPIODATA_REG (0x19) /* GPIO data */ |
| 64 | #define MAX310X_PLLCFG_REG (0x1a) /* PLL config */ |
| 65 | #define MAX310X_BRGCFG_REG (0x1b) /* Baud rate generator conf */ |
| 66 | #define MAX310X_BRGDIVLSB_REG (0x1c) /* Baud rate divisor LSB */ |
| 67 | #define MAX310X_BRGDIVMSB_REG (0x1d) /* Baud rate divisor MSB */ |
| 68 | #define MAX310X_CLKSRC_REG (0x1e) /* Clock source */ |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 69 | #define MAX310X_REG_1F (0x1f) |
| 70 | |
| 71 | #define MAX310X_REVID_REG MAX310X_REG_1F /* Revision ID */ |
| 72 | |
| 73 | #define MAX310X_GLOBALIRQ_REG MAX310X_REG_1F /* Global IRQ (RO) */ |
| 74 | #define MAX310X_GLOBALCMD_REG MAX310X_REG_1F /* Global Command (WO) */ |
| 75 | |
| 76 | /* Extended registers */ |
| 77 | #define MAX310X_REVID_EXTREG MAX310X_REG_05 /* Revision ID */ |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 78 | |
| 79 | /* IRQ register bits */ |
| 80 | #define MAX310X_IRQ_LSR_BIT (1 << 0) /* LSR interrupt */ |
| 81 | #define MAX310X_IRQ_SPCHR_BIT (1 << 1) /* Special char interrupt */ |
| 82 | #define MAX310X_IRQ_STS_BIT (1 << 2) /* Status interrupt */ |
| 83 | #define MAX310X_IRQ_RXFIFO_BIT (1 << 3) /* RX FIFO interrupt */ |
| 84 | #define MAX310X_IRQ_TXFIFO_BIT (1 << 4) /* TX FIFO interrupt */ |
| 85 | #define MAX310X_IRQ_TXEMPTY_BIT (1 << 5) /* TX FIFO empty interrupt */ |
| 86 | #define MAX310X_IRQ_RXEMPTY_BIT (1 << 6) /* RX FIFO empty interrupt */ |
| 87 | #define MAX310X_IRQ_CTS_BIT (1 << 7) /* CTS interrupt */ |
| 88 | |
| 89 | /* LSR register bits */ |
| 90 | #define MAX310X_LSR_RXTO_BIT (1 << 0) /* RX timeout */ |
| 91 | #define MAX310X_LSR_RXOVR_BIT (1 << 1) /* RX overrun */ |
| 92 | #define MAX310X_LSR_RXPAR_BIT (1 << 2) /* RX parity error */ |
| 93 | #define MAX310X_LSR_FRERR_BIT (1 << 3) /* Frame error */ |
| 94 | #define MAX310X_LSR_RXBRK_BIT (1 << 4) /* RX break */ |
| 95 | #define MAX310X_LSR_RXNOISE_BIT (1 << 5) /* RX noise */ |
| 96 | #define MAX310X_LSR_CTS_BIT (1 << 7) /* CTS pin state */ |
| 97 | |
| 98 | /* Special character register bits */ |
| 99 | #define MAX310X_SPCHR_XON1_BIT (1 << 0) /* XON1 character */ |
| 100 | #define MAX310X_SPCHR_XON2_BIT (1 << 1) /* XON2 character */ |
| 101 | #define MAX310X_SPCHR_XOFF1_BIT (1 << 2) /* XOFF1 character */ |
| 102 | #define MAX310X_SPCHR_XOFF2_BIT (1 << 3) /* XOFF2 character */ |
| 103 | #define MAX310X_SPCHR_BREAK_BIT (1 << 4) /* RX break */ |
| 104 | #define MAX310X_SPCHR_MULTIDROP_BIT (1 << 5) /* 9-bit multidrop addr char */ |
| 105 | |
| 106 | /* Status register bits */ |
| 107 | #define MAX310X_STS_GPIO0_BIT (1 << 0) /* GPIO 0 interrupt */ |
| 108 | #define MAX310X_STS_GPIO1_BIT (1 << 1) /* GPIO 1 interrupt */ |
| 109 | #define MAX310X_STS_GPIO2_BIT (1 << 2) /* GPIO 2 interrupt */ |
| 110 | #define MAX310X_STS_GPIO3_BIT (1 << 3) /* GPIO 3 interrupt */ |
| 111 | #define MAX310X_STS_CLKREADY_BIT (1 << 5) /* Clock ready */ |
| 112 | #define MAX310X_STS_SLEEP_BIT (1 << 6) /* Sleep interrupt */ |
| 113 | |
| 114 | /* MODE1 register bits */ |
| 115 | #define MAX310X_MODE1_RXDIS_BIT (1 << 0) /* RX disable */ |
| 116 | #define MAX310X_MODE1_TXDIS_BIT (1 << 1) /* TX disable */ |
| 117 | #define MAX310X_MODE1_TXHIZ_BIT (1 << 2) /* TX pin three-state */ |
| 118 | #define MAX310X_MODE1_RTSHIZ_BIT (1 << 3) /* RTS pin three-state */ |
| 119 | #define MAX310X_MODE1_TRNSCVCTRL_BIT (1 << 4) /* Transceiver ctrl enable */ |
| 120 | #define MAX310X_MODE1_FORCESLEEP_BIT (1 << 5) /* Force sleep mode */ |
| 121 | #define MAX310X_MODE1_AUTOSLEEP_BIT (1 << 6) /* Auto sleep enable */ |
| 122 | #define MAX310X_MODE1_IRQSEL_BIT (1 << 7) /* IRQ pin enable */ |
| 123 | |
| 124 | /* MODE2 register bits */ |
| 125 | #define MAX310X_MODE2_RST_BIT (1 << 0) /* Chip reset */ |
| 126 | #define MAX310X_MODE2_FIFORST_BIT (1 << 1) /* FIFO reset */ |
| 127 | #define MAX310X_MODE2_RXTRIGINV_BIT (1 << 2) /* RX FIFO INT invert */ |
| 128 | #define MAX310X_MODE2_RXEMPTINV_BIT (1 << 3) /* RX FIFO empty INT invert */ |
| 129 | #define MAX310X_MODE2_SPCHR_BIT (1 << 4) /* Special chr detect enable */ |
| 130 | #define MAX310X_MODE2_LOOPBACK_BIT (1 << 5) /* Internal loopback enable */ |
| 131 | #define MAX310X_MODE2_MULTIDROP_BIT (1 << 6) /* 9-bit multidrop enable */ |
| 132 | #define MAX310X_MODE2_ECHOSUPR_BIT (1 << 7) /* ECHO suppression enable */ |
| 133 | |
| 134 | /* LCR register bits */ |
| 135 | #define MAX310X_LCR_LENGTH0_BIT (1 << 0) /* Word length bit 0 */ |
| 136 | #define MAX310X_LCR_LENGTH1_BIT (1 << 1) /* Word length bit 1 |
| 137 | * |
| 138 | * Word length bits table: |
| 139 | * 00 -> 5 bit words |
| 140 | * 01 -> 6 bit words |
| 141 | * 10 -> 7 bit words |
| 142 | * 11 -> 8 bit words |
| 143 | */ |
| 144 | #define MAX310X_LCR_STOPLEN_BIT (1 << 2) /* STOP length bit |
| 145 | * |
| 146 | * STOP length bit table: |
| 147 | * 0 -> 1 stop bit |
| 148 | * 1 -> 1-1.5 stop bits if |
| 149 | * word length is 5, |
| 150 | * 2 stop bits otherwise |
| 151 | */ |
| 152 | #define MAX310X_LCR_PARITY_BIT (1 << 3) /* Parity bit enable */ |
| 153 | #define MAX310X_LCR_EVENPARITY_BIT (1 << 4) /* Even parity bit enable */ |
| 154 | #define MAX310X_LCR_FORCEPARITY_BIT (1 << 5) /* 9-bit multidrop parity */ |
| 155 | #define MAX310X_LCR_TXBREAK_BIT (1 << 6) /* TX break enable */ |
| 156 | #define MAX310X_LCR_RTS_BIT (1 << 7) /* RTS pin control */ |
| 157 | #define MAX310X_LCR_WORD_LEN_5 (0x00) |
| 158 | #define MAX310X_LCR_WORD_LEN_6 (0x01) |
| 159 | #define MAX310X_LCR_WORD_LEN_7 (0x02) |
| 160 | #define MAX310X_LCR_WORD_LEN_8 (0x03) |
| 161 | |
| 162 | /* IRDA register bits */ |
| 163 | #define MAX310X_IRDA_IRDAEN_BIT (1 << 0) /* IRDA mode enable */ |
| 164 | #define MAX310X_IRDA_SIR_BIT (1 << 1) /* SIR mode enable */ |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 165 | |
| 166 | /* Flow control trigger level register masks */ |
| 167 | #define MAX310X_FLOWLVL_HALT_MASK (0x000f) /* Flow control halt level */ |
| 168 | #define MAX310X_FLOWLVL_RES_MASK (0x00f0) /* Flow control resume level */ |
| 169 | #define MAX310X_FLOWLVL_HALT(words) ((words / 8) & 0x0f) |
| 170 | #define MAX310X_FLOWLVL_RES(words) (((words / 8) & 0x0f) << 4) |
| 171 | |
| 172 | /* FIFO interrupt trigger level register masks */ |
| 173 | #define MAX310X_FIFOTRIGLVL_TX_MASK (0x0f) /* TX FIFO trigger level */ |
| 174 | #define MAX310X_FIFOTRIGLVL_RX_MASK (0xf0) /* RX FIFO trigger level */ |
| 175 | #define MAX310X_FIFOTRIGLVL_TX(words) ((words / 8) & 0x0f) |
| 176 | #define MAX310X_FIFOTRIGLVL_RX(words) (((words / 8) & 0x0f) << 4) |
| 177 | |
| 178 | /* Flow control register bits */ |
| 179 | #define MAX310X_FLOWCTRL_AUTORTS_BIT (1 << 0) /* Auto RTS flow ctrl enable */ |
| 180 | #define MAX310X_FLOWCTRL_AUTOCTS_BIT (1 << 1) /* Auto CTS flow ctrl enable */ |
| 181 | #define MAX310X_FLOWCTRL_GPIADDR_BIT (1 << 2) /* Enables that GPIO inputs |
| 182 | * are used in conjunction with |
| 183 | * XOFF2 for definition of |
| 184 | * special character */ |
| 185 | #define MAX310X_FLOWCTRL_SWFLOWEN_BIT (1 << 3) /* Auto SW flow ctrl enable */ |
| 186 | #define MAX310X_FLOWCTRL_SWFLOW0_BIT (1 << 4) /* SWFLOW bit 0 */ |
| 187 | #define MAX310X_FLOWCTRL_SWFLOW1_BIT (1 << 5) /* SWFLOW bit 1 |
| 188 | * |
| 189 | * SWFLOW bits 1 & 0 table: |
| 190 | * 00 -> no transmitter flow |
| 191 | * control |
| 192 | * 01 -> receiver compares |
| 193 | * XON2 and XOFF2 |
| 194 | * and controls |
| 195 | * transmitter |
| 196 | * 10 -> receiver compares |
| 197 | * XON1 and XOFF1 |
| 198 | * and controls |
| 199 | * transmitter |
| 200 | * 11 -> receiver compares |
| 201 | * XON1, XON2, XOFF1 and |
| 202 | * XOFF2 and controls |
| 203 | * transmitter |
| 204 | */ |
| 205 | #define MAX310X_FLOWCTRL_SWFLOW2_BIT (1 << 6) /* SWFLOW bit 2 */ |
| 206 | #define MAX310X_FLOWCTRL_SWFLOW3_BIT (1 << 7) /* SWFLOW bit 3 |
| 207 | * |
| 208 | * SWFLOW bits 3 & 2 table: |
| 209 | * 00 -> no received flow |
| 210 | * control |
| 211 | * 01 -> transmitter generates |
| 212 | * XON2 and XOFF2 |
| 213 | * 10 -> transmitter generates |
| 214 | * XON1 and XOFF1 |
| 215 | * 11 -> transmitter generates |
| 216 | * XON1, XON2, XOFF1 and |
| 217 | * XOFF2 |
| 218 | */ |
| 219 | |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 220 | /* PLL configuration register masks */ |
| 221 | #define MAX310X_PLLCFG_PREDIV_MASK (0x3f) /* PLL predivision value */ |
| 222 | #define MAX310X_PLLCFG_PLLFACTOR_MASK (0xc0) /* PLL multiplication factor */ |
| 223 | |
| 224 | /* Baud rate generator configuration register bits */ |
| 225 | #define MAX310X_BRGCFG_2XMODE_BIT (1 << 4) /* Double baud rate */ |
| 226 | #define MAX310X_BRGCFG_4XMODE_BIT (1 << 5) /* Quadruple baud rate */ |
| 227 | |
| 228 | /* Clock source register bits */ |
| 229 | #define MAX310X_CLKSRC_CRYST_BIT (1 << 1) /* Crystal osc enable */ |
| 230 | #define MAX310X_CLKSRC_PLL_BIT (1 << 2) /* PLL enable */ |
| 231 | #define MAX310X_CLKSRC_PLLBYP_BIT (1 << 3) /* PLL bypass */ |
| 232 | #define MAX310X_CLKSRC_EXTCLK_BIT (1 << 4) /* External clock enable */ |
| 233 | #define MAX310X_CLKSRC_CLK2RTS_BIT (1 << 7) /* Baud clk to RTS pin */ |
| 234 | |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 235 | /* Global commands */ |
| 236 | #define MAX310X_EXTREG_ENBL (0xce) |
| 237 | #define MAX310X_EXTREG_DSBL (0xcd) |
| 238 | |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 239 | /* Misc definitions */ |
| 240 | #define MAX310X_FIFO_SIZE (128) |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 241 | #define MAX310x_REV_MASK (0xfc) |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 242 | |
| 243 | /* MAX3107 specific */ |
| 244 | #define MAX3107_REV_ID (0xa0) |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 245 | |
Alexander Shiyan | 21fc509 | 2013-06-29 10:44:18 +0400 | [diff] [blame] | 246 | /* MAX3109 specific */ |
| 247 | #define MAX3109_REV_ID (0xc0) |
| 248 | |
Alexander Shiyan | 003236d | 2013-06-29 10:44:19 +0400 | [diff] [blame] | 249 | /* MAX14830 specific */ |
| 250 | #define MAX14830_BRGCFG_CLKDIS_BIT (1 << 6) /* Clock Disable */ |
| 251 | #define MAX14830_REV_ID (0xb0) |
| 252 | |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 253 | struct max310x_devtype { |
| 254 | char name[9]; |
| 255 | int nr; |
| 256 | int (*detect)(struct device *); |
| 257 | void (*power)(struct uart_port *, int); |
| 258 | }; |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 259 | |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 260 | struct max310x_one { |
| 261 | struct uart_port port; |
| 262 | struct work_struct tx_work; |
Alexander Shiyan | e7b8a3c | 2014-02-07 18:16:07 +0400 | [diff] [blame] | 263 | struct work_struct md_work; |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 264 | }; |
| 265 | |
| 266 | struct max310x_port { |
| 267 | struct uart_driver uart; |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 268 | struct max310x_devtype *devtype; |
| 269 | struct regmap *regmap; |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 270 | struct mutex mutex; |
Alexander Shiyan | d3a8a25 | 2014-02-10 22:18:31 +0400 | [diff] [blame] | 271 | struct clk *clk; |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 272 | #ifdef CONFIG_GPIOLIB |
| 273 | struct gpio_chip gpio; |
| 274 | #endif |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 275 | struct max310x_one p[0]; |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 276 | }; |
| 277 | |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 278 | static u8 max310x_port_read(struct uart_port *port, u8 reg) |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 279 | { |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 280 | struct max310x_port *s = dev_get_drvdata(port->dev); |
| 281 | unsigned int val = 0; |
| 282 | |
| 283 | regmap_read(s->regmap, port->iobase + reg, &val); |
| 284 | |
| 285 | return val; |
| 286 | } |
| 287 | |
| 288 | static void max310x_port_write(struct uart_port *port, u8 reg, u8 val) |
| 289 | { |
| 290 | struct max310x_port *s = dev_get_drvdata(port->dev); |
| 291 | |
| 292 | regmap_write(s->regmap, port->iobase + reg, val); |
| 293 | } |
| 294 | |
| 295 | static void max310x_port_update(struct uart_port *port, u8 reg, u8 mask, u8 val) |
| 296 | { |
| 297 | struct max310x_port *s = dev_get_drvdata(port->dev); |
| 298 | |
| 299 | regmap_update_bits(s->regmap, port->iobase + reg, mask, val); |
| 300 | } |
| 301 | |
| 302 | static int max3107_detect(struct device *dev) |
| 303 | { |
| 304 | struct max310x_port *s = dev_get_drvdata(dev); |
| 305 | unsigned int val = 0; |
| 306 | int ret; |
| 307 | |
| 308 | ret = regmap_read(s->regmap, MAX310X_REVID_REG, &val); |
| 309 | if (ret) |
| 310 | return ret; |
| 311 | |
| 312 | if (((val & MAX310x_REV_MASK) != MAX3107_REV_ID)) { |
| 313 | dev_err(dev, |
| 314 | "%s ID 0x%02x does not match\n", s->devtype->name, val); |
| 315 | return -ENODEV; |
| 316 | } |
| 317 | |
| 318 | return 0; |
| 319 | } |
| 320 | |
| 321 | static int max3108_detect(struct device *dev) |
| 322 | { |
| 323 | struct max310x_port *s = dev_get_drvdata(dev); |
| 324 | unsigned int val = 0; |
| 325 | int ret; |
| 326 | |
| 327 | /* MAX3108 have not REV ID register, we just check default value |
| 328 | * from clocksource register to make sure everything works. |
| 329 | */ |
| 330 | ret = regmap_read(s->regmap, MAX310X_CLKSRC_REG, &val); |
| 331 | if (ret) |
| 332 | return ret; |
| 333 | |
| 334 | if (val != (MAX310X_CLKSRC_EXTCLK_BIT | MAX310X_CLKSRC_PLLBYP_BIT)) { |
| 335 | dev_err(dev, "%s not present\n", s->devtype->name); |
| 336 | return -ENODEV; |
| 337 | } |
| 338 | |
| 339 | return 0; |
| 340 | } |
| 341 | |
Alexander Shiyan | 21fc509 | 2013-06-29 10:44:18 +0400 | [diff] [blame] | 342 | static int max3109_detect(struct device *dev) |
| 343 | { |
| 344 | struct max310x_port *s = dev_get_drvdata(dev); |
| 345 | unsigned int val = 0; |
| 346 | int ret; |
| 347 | |
| 348 | ret = regmap_read(s->regmap, MAX310X_REVID_REG, &val); |
| 349 | if (ret) |
| 350 | return ret; |
| 351 | |
| 352 | if (((val & MAX310x_REV_MASK) != MAX3109_REV_ID)) { |
| 353 | dev_err(dev, |
| 354 | "%s ID 0x%02x does not match\n", s->devtype->name, val); |
| 355 | return -ENODEV; |
| 356 | } |
| 357 | |
| 358 | return 0; |
| 359 | } |
| 360 | |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 361 | static void max310x_power(struct uart_port *port, int on) |
| 362 | { |
| 363 | max310x_port_update(port, MAX310X_MODE1_REG, |
| 364 | MAX310X_MODE1_FORCESLEEP_BIT, |
| 365 | on ? 0 : MAX310X_MODE1_FORCESLEEP_BIT); |
| 366 | if (on) |
| 367 | msleep(50); |
| 368 | } |
| 369 | |
Alexander Shiyan | 003236d | 2013-06-29 10:44:19 +0400 | [diff] [blame] | 370 | static int max14830_detect(struct device *dev) |
| 371 | { |
| 372 | struct max310x_port *s = dev_get_drvdata(dev); |
| 373 | unsigned int val = 0; |
| 374 | int ret; |
| 375 | |
| 376 | ret = regmap_write(s->regmap, MAX310X_GLOBALCMD_REG, |
| 377 | MAX310X_EXTREG_ENBL); |
| 378 | if (ret) |
| 379 | return ret; |
| 380 | |
| 381 | regmap_read(s->regmap, MAX310X_REVID_EXTREG, &val); |
| 382 | regmap_write(s->regmap, MAX310X_GLOBALCMD_REG, MAX310X_EXTREG_DSBL); |
| 383 | if (((val & MAX310x_REV_MASK) != MAX14830_REV_ID)) { |
| 384 | dev_err(dev, |
| 385 | "%s ID 0x%02x does not match\n", s->devtype->name, val); |
| 386 | return -ENODEV; |
| 387 | } |
| 388 | |
| 389 | return 0; |
| 390 | } |
| 391 | |
| 392 | static void max14830_power(struct uart_port *port, int on) |
| 393 | { |
| 394 | max310x_port_update(port, MAX310X_BRGCFG_REG, |
| 395 | MAX14830_BRGCFG_CLKDIS_BIT, |
| 396 | on ? 0 : MAX14830_BRGCFG_CLKDIS_BIT); |
| 397 | if (on) |
| 398 | msleep(50); |
| 399 | } |
| 400 | |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 401 | static const struct max310x_devtype max3107_devtype = { |
| 402 | .name = "MAX3107", |
| 403 | .nr = 1, |
| 404 | .detect = max3107_detect, |
| 405 | .power = max310x_power, |
| 406 | }; |
| 407 | |
| 408 | static const struct max310x_devtype max3108_devtype = { |
| 409 | .name = "MAX3108", |
| 410 | .nr = 1, |
| 411 | .detect = max3108_detect, |
| 412 | .power = max310x_power, |
| 413 | }; |
| 414 | |
Alexander Shiyan | 21fc509 | 2013-06-29 10:44:18 +0400 | [diff] [blame] | 415 | static const struct max310x_devtype max3109_devtype = { |
| 416 | .name = "MAX3109", |
| 417 | .nr = 2, |
| 418 | .detect = max3109_detect, |
| 419 | .power = max310x_power, |
| 420 | }; |
| 421 | |
Alexander Shiyan | 003236d | 2013-06-29 10:44:19 +0400 | [diff] [blame] | 422 | static const struct max310x_devtype max14830_devtype = { |
| 423 | .name = "MAX14830", |
| 424 | .nr = 4, |
| 425 | .detect = max14830_detect, |
| 426 | .power = max14830_power, |
| 427 | }; |
| 428 | |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 429 | static bool max310x_reg_writeable(struct device *dev, unsigned int reg) |
| 430 | { |
| 431 | switch (reg & 0x1f) { |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 432 | case MAX310X_IRQSTS_REG: |
| 433 | case MAX310X_LSR_IRQSTS_REG: |
| 434 | case MAX310X_SPCHR_IRQSTS_REG: |
| 435 | case MAX310X_STS_IRQSTS_REG: |
| 436 | case MAX310X_TXFIFOLVL_REG: |
| 437 | case MAX310X_RXFIFOLVL_REG: |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 438 | return false; |
| 439 | default: |
| 440 | break; |
| 441 | } |
| 442 | |
| 443 | return true; |
| 444 | } |
| 445 | |
| 446 | static bool max310x_reg_volatile(struct device *dev, unsigned int reg) |
| 447 | { |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 448 | switch (reg & 0x1f) { |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 449 | case MAX310X_RHR_REG: |
| 450 | case MAX310X_IRQSTS_REG: |
| 451 | case MAX310X_LSR_IRQSTS_REG: |
| 452 | case MAX310X_SPCHR_IRQSTS_REG: |
| 453 | case MAX310X_STS_IRQSTS_REG: |
| 454 | case MAX310X_TXFIFOLVL_REG: |
| 455 | case MAX310X_RXFIFOLVL_REG: |
| 456 | case MAX310X_GPIODATA_REG: |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 457 | case MAX310X_BRGDIVLSB_REG: |
| 458 | case MAX310X_REG_05: |
| 459 | case MAX310X_REG_1F: |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 460 | return true; |
| 461 | default: |
| 462 | break; |
| 463 | } |
| 464 | |
| 465 | return false; |
| 466 | } |
| 467 | |
| 468 | static bool max310x_reg_precious(struct device *dev, unsigned int reg) |
| 469 | { |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 470 | switch (reg & 0x1f) { |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 471 | case MAX310X_RHR_REG: |
| 472 | case MAX310X_IRQSTS_REG: |
| 473 | case MAX310X_SPCHR_IRQSTS_REG: |
| 474 | case MAX310X_STS_IRQSTS_REG: |
| 475 | return true; |
| 476 | default: |
| 477 | break; |
| 478 | } |
| 479 | |
| 480 | return false; |
| 481 | } |
| 482 | |
Alexander Shiyan | e97e155 | 2014-02-07 18:16:04 +0400 | [diff] [blame] | 483 | static int max310x_set_baud(struct uart_port *port, int baud) |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 484 | { |
Alexander Shiyan | e97e155 | 2014-02-07 18:16:04 +0400 | [diff] [blame] | 485 | unsigned int mode = 0, clk = port->uartclk, div = clk / baud; |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 486 | |
Alexander Shiyan | e97e155 | 2014-02-07 18:16:04 +0400 | [diff] [blame] | 487 | /* Check for minimal value for divider */ |
| 488 | if (div < 16) |
| 489 | div = 16; |
| 490 | |
| 491 | if (clk % baud && (div / 16) < 0x8000) { |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 492 | /* Mode x2 */ |
| 493 | mode = MAX310X_BRGCFG_2XMODE_BIT; |
Alexander Shiyan | e97e155 | 2014-02-07 18:16:04 +0400 | [diff] [blame] | 494 | clk = port->uartclk * 2; |
| 495 | div = clk / baud; |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 496 | |
Alexander Shiyan | e97e155 | 2014-02-07 18:16:04 +0400 | [diff] [blame] | 497 | if (clk % baud && (div / 16) < 0x8000) { |
| 498 | /* Mode x4 */ |
| 499 | mode = MAX310X_BRGCFG_4XMODE_BIT; |
| 500 | clk = port->uartclk * 4; |
| 501 | div = clk / baud; |
| 502 | } |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 503 | } |
| 504 | |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 505 | max310x_port_write(port, MAX310X_BRGDIVMSB_REG, (div / 16) >> 8); |
| 506 | max310x_port_write(port, MAX310X_BRGDIVLSB_REG, div / 16); |
| 507 | max310x_port_write(port, MAX310X_BRGCFG_REG, (div % 16) | mode); |
Alexander Shiyan | e97e155 | 2014-02-07 18:16:04 +0400 | [diff] [blame] | 508 | |
| 509 | return DIV_ROUND_CLOSEST(clk, div); |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 510 | } |
| 511 | |
Bill Pemberton | 9671f09 | 2012-11-19 13:21:50 -0500 | [diff] [blame] | 512 | static int max310x_update_best_err(unsigned long f, long *besterr) |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 513 | { |
| 514 | /* Use baudrate 115200 for calculate error */ |
| 515 | long err = f % (115200 * 16); |
| 516 | |
| 517 | if ((*besterr < 0) || (*besterr > err)) { |
| 518 | *besterr = err; |
| 519 | return 0; |
| 520 | } |
| 521 | |
| 522 | return 1; |
| 523 | } |
| 524 | |
Alexander Shiyan | d3a8a25 | 2014-02-10 22:18:31 +0400 | [diff] [blame] | 525 | static int max310x_set_ref_clk(struct max310x_port *s, unsigned long freq, |
| 526 | bool xtal) |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 527 | { |
| 528 | unsigned int div, clksrc, pllcfg = 0; |
| 529 | long besterr = -1; |
Alexander Shiyan | d3a8a25 | 2014-02-10 22:18:31 +0400 | [diff] [blame] | 530 | unsigned long fdiv, fmul, bestfreq = freq; |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 531 | |
| 532 | /* First, update error without PLL */ |
Alexander Shiyan | d3a8a25 | 2014-02-10 22:18:31 +0400 | [diff] [blame] | 533 | max310x_update_best_err(freq, &besterr); |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 534 | |
| 535 | /* Try all possible PLL dividers */ |
| 536 | for (div = 1; (div <= 63) && besterr; div++) { |
Alexander Shiyan | d3a8a25 | 2014-02-10 22:18:31 +0400 | [diff] [blame] | 537 | fdiv = DIV_ROUND_CLOSEST(freq, div); |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 538 | |
| 539 | /* Try multiplier 6 */ |
| 540 | fmul = fdiv * 6; |
| 541 | if ((fdiv >= 500000) && (fdiv <= 800000)) |
| 542 | if (!max310x_update_best_err(fmul, &besterr)) { |
| 543 | pllcfg = (0 << 6) | div; |
| 544 | bestfreq = fmul; |
| 545 | } |
| 546 | /* Try multiplier 48 */ |
| 547 | fmul = fdiv * 48; |
| 548 | if ((fdiv >= 850000) && (fdiv <= 1200000)) |
| 549 | if (!max310x_update_best_err(fmul, &besterr)) { |
| 550 | pllcfg = (1 << 6) | div; |
| 551 | bestfreq = fmul; |
| 552 | } |
| 553 | /* Try multiplier 96 */ |
| 554 | fmul = fdiv * 96; |
| 555 | if ((fdiv >= 425000) && (fdiv <= 1000000)) |
| 556 | if (!max310x_update_best_err(fmul, &besterr)) { |
| 557 | pllcfg = (2 << 6) | div; |
| 558 | bestfreq = fmul; |
| 559 | } |
| 560 | /* Try multiplier 144 */ |
| 561 | fmul = fdiv * 144; |
| 562 | if ((fdiv >= 390000) && (fdiv <= 667000)) |
| 563 | if (!max310x_update_best_err(fmul, &besterr)) { |
| 564 | pllcfg = (3 << 6) | div; |
| 565 | bestfreq = fmul; |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | /* Configure clock source */ |
Alexander Shiyan | d3a8a25 | 2014-02-10 22:18:31 +0400 | [diff] [blame] | 570 | clksrc = xtal ? MAX310X_CLKSRC_CRYST_BIT : MAX310X_CLKSRC_EXTCLK_BIT; |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 571 | |
| 572 | /* Configure PLL */ |
| 573 | if (pllcfg) { |
| 574 | clksrc |= MAX310X_CLKSRC_PLL_BIT; |
| 575 | regmap_write(s->regmap, MAX310X_PLLCFG_REG, pllcfg); |
| 576 | } else |
| 577 | clksrc |= MAX310X_CLKSRC_PLLBYP_BIT; |
| 578 | |
| 579 | regmap_write(s->regmap, MAX310X_CLKSRC_REG, clksrc); |
| 580 | |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 581 | /* Wait for crystal */ |
Alexander Shiyan | d3a8a25 | 2014-02-10 22:18:31 +0400 | [diff] [blame] | 582 | if (pllcfg && xtal) |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 583 | msleep(10); |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 584 | |
| 585 | return (int)bestfreq; |
| 586 | } |
| 587 | |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 588 | static void max310x_handle_rx(struct uart_port *port, unsigned int rxlen) |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 589 | { |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 590 | unsigned int sts, ch, flag; |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 591 | |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 592 | if (unlikely(rxlen >= port->fifosize)) { |
| 593 | dev_warn_ratelimited(port->dev, |
| 594 | "Port %i: Possible RX FIFO overrun\n", |
| 595 | port->line); |
| 596 | port->icount.buf_overrun++; |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 597 | /* Ensure sanity of RX level */ |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 598 | rxlen = port->fifosize; |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 599 | } |
| 600 | |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 601 | while (rxlen--) { |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 602 | ch = max310x_port_read(port, MAX310X_RHR_REG); |
| 603 | sts = max310x_port_read(port, MAX310X_LSR_IRQSTS_REG); |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 604 | |
| 605 | sts &= MAX310X_LSR_RXPAR_BIT | MAX310X_LSR_FRERR_BIT | |
| 606 | MAX310X_LSR_RXOVR_BIT | MAX310X_LSR_RXBRK_BIT; |
| 607 | |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 608 | port->icount.rx++; |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 609 | flag = TTY_NORMAL; |
| 610 | |
| 611 | if (unlikely(sts)) { |
| 612 | if (sts & MAX310X_LSR_RXBRK_BIT) { |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 613 | port->icount.brk++; |
| 614 | if (uart_handle_break(port)) |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 615 | continue; |
| 616 | } else if (sts & MAX310X_LSR_RXPAR_BIT) |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 617 | port->icount.parity++; |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 618 | else if (sts & MAX310X_LSR_FRERR_BIT) |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 619 | port->icount.frame++; |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 620 | else if (sts & MAX310X_LSR_RXOVR_BIT) |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 621 | port->icount.overrun++; |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 622 | |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 623 | sts &= port->read_status_mask; |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 624 | if (sts & MAX310X_LSR_RXBRK_BIT) |
| 625 | flag = TTY_BREAK; |
| 626 | else if (sts & MAX310X_LSR_RXPAR_BIT) |
| 627 | flag = TTY_PARITY; |
| 628 | else if (sts & MAX310X_LSR_FRERR_BIT) |
| 629 | flag = TTY_FRAME; |
| 630 | else if (sts & MAX310X_LSR_RXOVR_BIT) |
| 631 | flag = TTY_OVERRUN; |
| 632 | } |
| 633 | |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 634 | if (uart_handle_sysrq_char(port, ch)) |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 635 | continue; |
| 636 | |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 637 | if (sts & port->ignore_status_mask) |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 638 | continue; |
| 639 | |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 640 | uart_insert_char(port, sts, MAX310X_LSR_RXOVR_BIT, ch, flag); |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 641 | } |
| 642 | |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 643 | tty_flip_buffer_push(&port->state->port); |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 644 | } |
| 645 | |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 646 | static void max310x_handle_tx(struct uart_port *port) |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 647 | { |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 648 | struct circ_buf *xmit = &port->state->xmit; |
| 649 | unsigned int txlen, to_send; |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 650 | |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 651 | if (unlikely(port->x_char)) { |
| 652 | max310x_port_write(port, MAX310X_THR_REG, port->x_char); |
| 653 | port->icount.tx++; |
| 654 | port->x_char = 0; |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 655 | return; |
| 656 | } |
| 657 | |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 658 | if (uart_circ_empty(xmit) || uart_tx_stopped(port)) |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 659 | return; |
| 660 | |
| 661 | /* Get length of data pending in circular buffer */ |
| 662 | to_send = uart_circ_chars_pending(xmit); |
| 663 | if (likely(to_send)) { |
| 664 | /* Limit to size of TX FIFO */ |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 665 | txlen = max310x_port_read(port, MAX310X_TXFIFOLVL_REG); |
| 666 | txlen = port->fifosize - txlen; |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 667 | to_send = (to_send > txlen) ? txlen : to_send; |
| 668 | |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 669 | /* Add data to send */ |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 670 | port->icount.tx += to_send; |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 671 | while (to_send--) { |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 672 | max310x_port_write(port, MAX310X_THR_REG, |
| 673 | xmit->buf[xmit->tail]); |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 674 | xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); |
Joe Perches | fc811472 | 2013-10-08 16:14:21 -0700 | [diff] [blame] | 675 | } |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 679 | uart_write_wakeup(port); |
| 680 | } |
| 681 | |
| 682 | static void max310x_port_irq(struct max310x_port *s, int portno) |
| 683 | { |
| 684 | struct uart_port *port = &s->p[portno].port; |
| 685 | |
| 686 | do { |
| 687 | unsigned int ists, lsr, rxlen; |
| 688 | |
| 689 | /* Read IRQ status & RX FIFO level */ |
| 690 | ists = max310x_port_read(port, MAX310X_IRQSTS_REG); |
| 691 | rxlen = max310x_port_read(port, MAX310X_RXFIFOLVL_REG); |
| 692 | if (!ists && !rxlen) |
| 693 | break; |
| 694 | |
| 695 | if (ists & MAX310X_IRQ_CTS_BIT) { |
| 696 | lsr = max310x_port_read(port, MAX310X_LSR_IRQSTS_REG); |
| 697 | uart_handle_cts_change(port, |
| 698 | !!(lsr & MAX310X_LSR_CTS_BIT)); |
| 699 | } |
| 700 | if (rxlen) |
| 701 | max310x_handle_rx(port, rxlen); |
| 702 | if (ists & MAX310X_IRQ_TXEMPTY_BIT) { |
| 703 | mutex_lock(&s->mutex); |
| 704 | max310x_handle_tx(port); |
| 705 | mutex_unlock(&s->mutex); |
| 706 | } |
| 707 | } while (1); |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | static irqreturn_t max310x_ist(int irq, void *dev_id) |
| 711 | { |
| 712 | struct max310x_port *s = (struct max310x_port *)dev_id; |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 713 | |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 714 | if (s->uart.nr > 1) { |
| 715 | do { |
| 716 | unsigned int val = ~0; |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 717 | |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 718 | WARN_ON_ONCE(regmap_read(s->regmap, |
| 719 | MAX310X_GLOBALIRQ_REG, &val)); |
| 720 | val = ((1 << s->uart.nr) - 1) & ~val; |
| 721 | if (!val) |
| 722 | break; |
| 723 | max310x_port_irq(s, fls(val) - 1); |
| 724 | } while (1); |
| 725 | } else |
| 726 | max310x_port_irq(s, 0); |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 727 | |
| 728 | return IRQ_HANDLED; |
| 729 | } |
| 730 | |
| 731 | static void max310x_wq_proc(struct work_struct *ws) |
| 732 | { |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 733 | struct max310x_one *one = container_of(ws, struct max310x_one, tx_work); |
| 734 | struct max310x_port *s = dev_get_drvdata(one->port.dev); |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 735 | |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 736 | mutex_lock(&s->mutex); |
| 737 | max310x_handle_tx(&one->port); |
| 738 | mutex_unlock(&s->mutex); |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | static void max310x_start_tx(struct uart_port *port) |
| 742 | { |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 743 | struct max310x_one *one = container_of(port, struct max310x_one, port); |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 744 | |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 745 | if (!work_pending(&one->tx_work)) |
| 746 | schedule_work(&one->tx_work); |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | static unsigned int max310x_tx_empty(struct uart_port *port) |
| 750 | { |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 751 | unsigned int lvl, sts; |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 752 | |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 753 | lvl = max310x_port_read(port, MAX310X_TXFIFOLVL_REG); |
| 754 | sts = max310x_port_read(port, MAX310X_IRQSTS_REG); |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 755 | |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 756 | return ((sts & MAX310X_IRQ_TXEMPTY_BIT) && !lvl) ? TIOCSER_TEMT : 0; |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | static unsigned int max310x_get_mctrl(struct uart_port *port) |
| 760 | { |
| 761 | /* DCD and DSR are not wired and CTS/RTS is handled automatically |
| 762 | * so just indicate DSR and CAR asserted |
| 763 | */ |
| 764 | return TIOCM_DSR | TIOCM_CAR; |
| 765 | } |
| 766 | |
Alexander Shiyan | e7b8a3c | 2014-02-07 18:16:07 +0400 | [diff] [blame] | 767 | static void max310x_md_proc(struct work_struct *ws) |
| 768 | { |
| 769 | struct max310x_one *one = container_of(ws, struct max310x_one, md_work); |
| 770 | |
| 771 | max310x_port_update(&one->port, MAX310X_MODE2_REG, |
| 772 | MAX310X_MODE2_LOOPBACK_BIT, |
| 773 | (one->port.mctrl & TIOCM_LOOP) ? |
| 774 | MAX310X_MODE2_LOOPBACK_BIT : 0); |
| 775 | } |
| 776 | |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 777 | static void max310x_set_mctrl(struct uart_port *port, unsigned int mctrl) |
| 778 | { |
Alexander Shiyan | e7b8a3c | 2014-02-07 18:16:07 +0400 | [diff] [blame] | 779 | struct max310x_one *one = container_of(port, struct max310x_one, port); |
| 780 | |
| 781 | schedule_work(&one->md_work); |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | static void max310x_break_ctl(struct uart_port *port, int break_state) |
| 785 | { |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 786 | max310x_port_update(port, MAX310X_LCR_REG, |
| 787 | MAX310X_LCR_TXBREAK_BIT, |
| 788 | break_state ? MAX310X_LCR_TXBREAK_BIT : 0); |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | static void max310x_set_termios(struct uart_port *port, |
| 792 | struct ktermios *termios, |
| 793 | struct ktermios *old) |
| 794 | { |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 795 | unsigned int lcr, flow = 0; |
| 796 | int baud; |
| 797 | |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 798 | /* Mask termios capabilities we don't support */ |
| 799 | termios->c_cflag &= ~CMSPAR; |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 800 | |
| 801 | /* Word size */ |
| 802 | switch (termios->c_cflag & CSIZE) { |
| 803 | case CS5: |
| 804 | lcr = MAX310X_LCR_WORD_LEN_5; |
| 805 | break; |
| 806 | case CS6: |
| 807 | lcr = MAX310X_LCR_WORD_LEN_6; |
| 808 | break; |
| 809 | case CS7: |
| 810 | lcr = MAX310X_LCR_WORD_LEN_7; |
| 811 | break; |
| 812 | case CS8: |
| 813 | default: |
| 814 | lcr = MAX310X_LCR_WORD_LEN_8; |
| 815 | break; |
| 816 | } |
| 817 | |
| 818 | /* Parity */ |
| 819 | if (termios->c_cflag & PARENB) { |
| 820 | lcr |= MAX310X_LCR_PARITY_BIT; |
| 821 | if (!(termios->c_cflag & PARODD)) |
| 822 | lcr |= MAX310X_LCR_EVENPARITY_BIT; |
| 823 | } |
| 824 | |
| 825 | /* Stop bits */ |
| 826 | if (termios->c_cflag & CSTOPB) |
| 827 | lcr |= MAX310X_LCR_STOPLEN_BIT; /* 2 stops */ |
| 828 | |
| 829 | /* Update LCR register */ |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 830 | max310x_port_write(port, MAX310X_LCR_REG, lcr); |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 831 | |
| 832 | /* Set read status mask */ |
| 833 | port->read_status_mask = MAX310X_LSR_RXOVR_BIT; |
| 834 | if (termios->c_iflag & INPCK) |
| 835 | port->read_status_mask |= MAX310X_LSR_RXPAR_BIT | |
| 836 | MAX310X_LSR_FRERR_BIT; |
| 837 | if (termios->c_iflag & (BRKINT | PARMRK)) |
| 838 | port->read_status_mask |= MAX310X_LSR_RXBRK_BIT; |
| 839 | |
| 840 | /* Set status ignore mask */ |
| 841 | port->ignore_status_mask = 0; |
| 842 | if (termios->c_iflag & IGNBRK) |
| 843 | port->ignore_status_mask |= MAX310X_LSR_RXBRK_BIT; |
| 844 | if (!(termios->c_cflag & CREAD)) |
| 845 | port->ignore_status_mask |= MAX310X_LSR_RXPAR_BIT | |
| 846 | MAX310X_LSR_RXOVR_BIT | |
| 847 | MAX310X_LSR_FRERR_BIT | |
| 848 | MAX310X_LSR_RXBRK_BIT; |
| 849 | |
| 850 | /* Configure flow control */ |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 851 | max310x_port_write(port, MAX310X_XON1_REG, termios->c_cc[VSTART]); |
| 852 | max310x_port_write(port, MAX310X_XOFF1_REG, termios->c_cc[VSTOP]); |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 853 | if (termios->c_cflag & CRTSCTS) |
| 854 | flow |= MAX310X_FLOWCTRL_AUTOCTS_BIT | |
| 855 | MAX310X_FLOWCTRL_AUTORTS_BIT; |
| 856 | if (termios->c_iflag & IXON) |
| 857 | flow |= MAX310X_FLOWCTRL_SWFLOW3_BIT | |
| 858 | MAX310X_FLOWCTRL_SWFLOWEN_BIT; |
| 859 | if (termios->c_iflag & IXOFF) |
| 860 | flow |= MAX310X_FLOWCTRL_SWFLOW1_BIT | |
| 861 | MAX310X_FLOWCTRL_SWFLOWEN_BIT; |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 862 | max310x_port_write(port, MAX310X_FLOWCTRL_REG, flow); |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 863 | |
| 864 | /* Get baud rate generator configuration */ |
| 865 | baud = uart_get_baud_rate(port, termios, old, |
| 866 | port->uartclk / 16 / 0xffff, |
| 867 | port->uartclk / 4); |
| 868 | |
| 869 | /* Setup baudrate generator */ |
Alexander Shiyan | e97e155 | 2014-02-07 18:16:04 +0400 | [diff] [blame] | 870 | baud = max310x_set_baud(port, baud); |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 871 | |
| 872 | /* Update timeout according to new baud rate */ |
| 873 | uart_update_timeout(port, termios->c_cflag, baud); |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 874 | } |
| 875 | |
Alexander Shiyan | 55367c6 | 2014-02-10 22:18:34 +0400 | [diff] [blame] | 876 | static int max310x_ioctl(struct uart_port *port, unsigned int cmd, |
| 877 | unsigned long arg) |
| 878 | { |
Alexander Shiyan | 86a41c4 | 2014-02-13 23:12:48 +0400 | [diff] [blame^] | 879 | #if defined(TIOCSRS485) && defined(TIOCGRS485) |
Alexander Shiyan | 55367c6 | 2014-02-10 22:18:34 +0400 | [diff] [blame] | 880 | struct serial_rs485 rs485; |
| 881 | unsigned int val; |
| 882 | |
| 883 | switch (cmd) { |
| 884 | case TIOCSRS485: |
| 885 | if (copy_from_user(&rs485, (struct serial_rs485 *)arg, |
| 886 | sizeof(rs485))) |
| 887 | return -EFAULT; |
| 888 | if (rs485.delay_rts_before_send > 0x0f || |
| 889 | rs485.delay_rts_after_send > 0x0f) |
| 890 | return -ERANGE; |
| 891 | val = (rs485.delay_rts_before_send << 4) | |
| 892 | rs485.delay_rts_after_send; |
| 893 | max310x_port_write(port, MAX310X_HDPIXDELAY_REG, val); |
| 894 | if (rs485.flags & SER_RS485_ENABLED) { |
| 895 | max310x_port_update(port, MAX310X_MODE1_REG, |
| 896 | MAX310X_MODE1_TRNSCVCTRL_BIT, |
| 897 | MAX310X_MODE1_TRNSCVCTRL_BIT); |
| 898 | max310x_port_update(port, MAX310X_MODE2_REG, |
| 899 | MAX310X_MODE2_ECHOSUPR_BIT, |
| 900 | MAX310X_MODE2_ECHOSUPR_BIT); |
| 901 | } else { |
| 902 | max310x_port_update(port, MAX310X_MODE1_REG, |
| 903 | MAX310X_MODE1_TRNSCVCTRL_BIT, 0); |
| 904 | max310x_port_update(port, MAX310X_MODE2_REG, |
| 905 | MAX310X_MODE2_ECHOSUPR_BIT, 0); |
| 906 | } |
Alexander Shiyan | 86a41c4 | 2014-02-13 23:12:48 +0400 | [diff] [blame^] | 907 | return 0; |
Alexander Shiyan | 55367c6 | 2014-02-10 22:18:34 +0400 | [diff] [blame] | 908 | case TIOCGRS485: |
| 909 | memset(&rs485, 0, sizeof(rs485)); |
| 910 | val = max310x_port_read(port, MAX310X_MODE1_REG); |
| 911 | rs485.flags = (val & MAX310X_MODE1_TRNSCVCTRL_BIT) ? |
| 912 | SER_RS485_ENABLED : 0; |
| 913 | rs485.flags |= SER_RS485_RTS_ON_SEND; |
| 914 | val = max310x_port_read(port, MAX310X_HDPIXDELAY_REG); |
| 915 | rs485.delay_rts_before_send = val >> 4; |
| 916 | rs485.delay_rts_after_send = val & 0x0f; |
| 917 | if (copy_to_user((struct serial_rs485 *)arg, &rs485, |
| 918 | sizeof(rs485))) |
| 919 | return -EFAULT; |
Alexander Shiyan | 86a41c4 | 2014-02-13 23:12:48 +0400 | [diff] [blame^] | 920 | return 0; |
Alexander Shiyan | 55367c6 | 2014-02-10 22:18:34 +0400 | [diff] [blame] | 921 | default: |
Alexander Shiyan | 86a41c4 | 2014-02-13 23:12:48 +0400 | [diff] [blame^] | 922 | break; |
Alexander Shiyan | 55367c6 | 2014-02-10 22:18:34 +0400 | [diff] [blame] | 923 | } |
Alexander Shiyan | 86a41c4 | 2014-02-13 23:12:48 +0400 | [diff] [blame^] | 924 | #endif |
Alexander Shiyan | 55367c6 | 2014-02-10 22:18:34 +0400 | [diff] [blame] | 925 | |
Alexander Shiyan | 86a41c4 | 2014-02-13 23:12:48 +0400 | [diff] [blame^] | 926 | return -ENOIOCTLCMD; |
Alexander Shiyan | 55367c6 | 2014-02-10 22:18:34 +0400 | [diff] [blame] | 927 | } |
| 928 | |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 929 | static int max310x_startup(struct uart_port *port) |
| 930 | { |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 931 | struct max310x_port *s = dev_get_drvdata(port->dev); |
Alexander Shiyan | 55367c6 | 2014-02-10 22:18:34 +0400 | [diff] [blame] | 932 | unsigned int val; |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 933 | |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 934 | s->devtype->power(port, 1); |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 935 | |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 936 | /* Configure MODE1 register */ |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 937 | max310x_port_update(port, MAX310X_MODE1_REG, |
Alexander Shiyan | 55367c6 | 2014-02-10 22:18:34 +0400 | [diff] [blame] | 938 | MAX310X_MODE1_TRNSCVCTRL_BIT, 0); |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 939 | |
Alexander Shiyan | 55367c6 | 2014-02-10 22:18:34 +0400 | [diff] [blame] | 940 | /* Configure MODE2 register & Reset FIFOs*/ |
| 941 | val = MAX310X_MODE2_RXEMPTINV_BIT | MAX310X_MODE2_FIFORST_BIT; |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 942 | max310x_port_write(port, MAX310X_MODE2_REG, val); |
| 943 | max310x_port_update(port, MAX310X_MODE2_REG, |
| 944 | MAX310X_MODE2_FIFORST_BIT, 0); |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 945 | |
| 946 | /* Configure flow control levels */ |
| 947 | /* Flow control halt level 96, resume level 48 */ |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 948 | max310x_port_write(port, MAX310X_FLOWLVL_REG, |
| 949 | MAX310X_FLOWLVL_RES(48) | MAX310X_FLOWLVL_HALT(96)); |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 950 | |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 951 | /* Clear IRQ status register */ |
| 952 | max310x_port_read(port, MAX310X_IRQSTS_REG); |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 953 | |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 954 | /* Enable RX, TX, CTS change interrupts */ |
| 955 | val = MAX310X_IRQ_RXEMPTY_BIT | MAX310X_IRQ_TXEMPTY_BIT; |
| 956 | max310x_port_write(port, MAX310X_IRQEN_REG, val | MAX310X_IRQ_CTS_BIT); |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 957 | |
| 958 | return 0; |
| 959 | } |
| 960 | |
| 961 | static void max310x_shutdown(struct uart_port *port) |
| 962 | { |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 963 | struct max310x_port *s = dev_get_drvdata(port->dev); |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 964 | |
| 965 | /* Disable all interrupts */ |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 966 | max310x_port_write(port, MAX310X_IRQEN_REG, 0); |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 967 | |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 968 | s->devtype->power(port, 0); |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | static const char *max310x_type(struct uart_port *port) |
| 972 | { |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 973 | struct max310x_port *s = dev_get_drvdata(port->dev); |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 974 | |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 975 | return (port->type == PORT_MAX310X) ? s->devtype->name : NULL; |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 976 | } |
| 977 | |
| 978 | static int max310x_request_port(struct uart_port *port) |
| 979 | { |
| 980 | /* Do nothing */ |
| 981 | return 0; |
| 982 | } |
| 983 | |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 984 | static void max310x_config_port(struct uart_port *port, int flags) |
| 985 | { |
| 986 | if (flags & UART_CONFIG_TYPE) |
| 987 | port->type = PORT_MAX310X; |
| 988 | } |
| 989 | |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 990 | static int max310x_verify_port(struct uart_port *port, struct serial_struct *s) |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 991 | { |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 992 | if ((s->type != PORT_UNKNOWN) && (s->type != PORT_MAX310X)) |
| 993 | return -EINVAL; |
| 994 | if (s->irq != port->irq) |
| 995 | return -EINVAL; |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 996 | |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 997 | return 0; |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 998 | } |
| 999 | |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 1000 | static void max310x_null_void(struct uart_port *port) |
| 1001 | { |
| 1002 | /* Do nothing */ |
| 1003 | } |
| 1004 | |
| 1005 | static const struct uart_ops max310x_ops = { |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 1006 | .tx_empty = max310x_tx_empty, |
| 1007 | .set_mctrl = max310x_set_mctrl, |
| 1008 | .get_mctrl = max310x_get_mctrl, |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 1009 | .stop_tx = max310x_null_void, |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 1010 | .start_tx = max310x_start_tx, |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 1011 | .stop_rx = max310x_null_void, |
| 1012 | .enable_ms = max310x_null_void, |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 1013 | .break_ctl = max310x_break_ctl, |
| 1014 | .startup = max310x_startup, |
| 1015 | .shutdown = max310x_shutdown, |
| 1016 | .set_termios = max310x_set_termios, |
| 1017 | .type = max310x_type, |
| 1018 | .request_port = max310x_request_port, |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 1019 | .release_port = max310x_null_void, |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 1020 | .config_port = max310x_config_port, |
| 1021 | .verify_port = max310x_verify_port, |
Alexander Shiyan | 55367c6 | 2014-02-10 22:18:34 +0400 | [diff] [blame] | 1022 | .ioctl = max310x_ioctl, |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 1023 | }; |
| 1024 | |
Alexander Shiyan | c297829 | 2013-07-29 19:27:32 +0400 | [diff] [blame] | 1025 | static int __maybe_unused max310x_suspend(struct device *dev) |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 1026 | { |
Alexander Shiyan | c297829 | 2013-07-29 19:27:32 +0400 | [diff] [blame] | 1027 | struct max310x_port *s = dev_get_drvdata(dev); |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 1028 | int i; |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 1029 | |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 1030 | for (i = 0; i < s->uart.nr; i++) { |
| 1031 | uart_suspend_port(&s->uart, &s->p[i].port); |
| 1032 | s->devtype->power(&s->p[i].port, 0); |
| 1033 | } |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 1034 | |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 1035 | return 0; |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 1036 | } |
| 1037 | |
Alexander Shiyan | c297829 | 2013-07-29 19:27:32 +0400 | [diff] [blame] | 1038 | static int __maybe_unused max310x_resume(struct device *dev) |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 1039 | { |
Alexander Shiyan | c297829 | 2013-07-29 19:27:32 +0400 | [diff] [blame] | 1040 | struct max310x_port *s = dev_get_drvdata(dev); |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 1041 | int i; |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 1042 | |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 1043 | for (i = 0; i < s->uart.nr; i++) { |
| 1044 | s->devtype->power(&s->p[i].port, 1); |
| 1045 | uart_resume_port(&s->uart, &s->p[i].port); |
| 1046 | } |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 1047 | |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 1048 | return 0; |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 1049 | } |
| 1050 | |
Alexander Shiyan | 27027a7 | 2014-02-10 22:18:30 +0400 | [diff] [blame] | 1051 | static SIMPLE_DEV_PM_OPS(max310x_pm_ops, max310x_suspend, max310x_resume); |
| 1052 | |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 1053 | #ifdef CONFIG_GPIOLIB |
| 1054 | static int max310x_gpio_get(struct gpio_chip *chip, unsigned offset) |
| 1055 | { |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 1056 | unsigned int val; |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 1057 | struct max310x_port *s = container_of(chip, struct max310x_port, gpio); |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 1058 | struct uart_port *port = &s->p[offset / 4].port; |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 1059 | |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 1060 | val = max310x_port_read(port, MAX310X_GPIODATA_REG); |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 1061 | |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 1062 | return !!((val >> 4) & (1 << (offset % 4))); |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 1063 | } |
| 1064 | |
| 1065 | static void max310x_gpio_set(struct gpio_chip *chip, unsigned offset, int value) |
| 1066 | { |
| 1067 | struct max310x_port *s = container_of(chip, struct max310x_port, gpio); |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 1068 | struct uart_port *port = &s->p[offset / 4].port; |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 1069 | |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 1070 | max310x_port_update(port, MAX310X_GPIODATA_REG, 1 << (offset % 4), |
| 1071 | value ? 1 << (offset % 4) : 0); |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 1072 | } |
| 1073 | |
| 1074 | static int max310x_gpio_direction_input(struct gpio_chip *chip, unsigned offset) |
| 1075 | { |
| 1076 | struct max310x_port *s = container_of(chip, struct max310x_port, gpio); |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 1077 | struct uart_port *port = &s->p[offset / 4].port; |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 1078 | |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 1079 | max310x_port_update(port, MAX310X_GPIOCFG_REG, 1 << (offset % 4), 0); |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 1080 | |
| 1081 | return 0; |
| 1082 | } |
| 1083 | |
| 1084 | static int max310x_gpio_direction_output(struct gpio_chip *chip, |
| 1085 | unsigned offset, int value) |
| 1086 | { |
| 1087 | struct max310x_port *s = container_of(chip, struct max310x_port, gpio); |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 1088 | struct uart_port *port = &s->p[offset / 4].port; |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 1089 | |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 1090 | max310x_port_update(port, MAX310X_GPIODATA_REG, 1 << (offset % 4), |
| 1091 | value ? 1 << (offset % 4) : 0); |
| 1092 | max310x_port_update(port, MAX310X_GPIOCFG_REG, 1 << (offset % 4), |
| 1093 | 1 << (offset % 4)); |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 1094 | |
| 1095 | return 0; |
| 1096 | } |
| 1097 | #endif |
| 1098 | |
Alexander Shiyan | 27027a7 | 2014-02-10 22:18:30 +0400 | [diff] [blame] | 1099 | static int max310x_probe(struct device *dev, struct max310x_devtype *devtype, |
Alexander Shiyan | 58afc90 | 2014-02-10 22:18:36 +0400 | [diff] [blame] | 1100 | struct regmap *regmap, int irq, unsigned long flags) |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 1101 | { |
Alexander Shiyan | d3a8a25 | 2014-02-10 22:18:31 +0400 | [diff] [blame] | 1102 | int i, ret, fmin, fmax, freq, uartclk; |
| 1103 | struct clk *clk_osc, *clk_xtal; |
| 1104 | struct max310x_port *s; |
| 1105 | bool xtal = false; |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 1106 | |
Alexander Shiyan | 27027a7 | 2014-02-10 22:18:30 +0400 | [diff] [blame] | 1107 | if (IS_ERR(regmap)) |
| 1108 | return PTR_ERR(regmap); |
| 1109 | |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 1110 | /* Alloc port structure */ |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 1111 | s = devm_kzalloc(dev, sizeof(*s) + |
| 1112 | sizeof(struct max310x_one) * devtype->nr, GFP_KERNEL); |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 1113 | if (!s) { |
| 1114 | dev_err(dev, "Error allocating port structure\n"); |
| 1115 | return -ENOMEM; |
| 1116 | } |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 1117 | |
Alexander Shiyan | d3a8a25 | 2014-02-10 22:18:31 +0400 | [diff] [blame] | 1118 | clk_osc = devm_clk_get(dev, "osc"); |
| 1119 | clk_xtal = devm_clk_get(dev, "xtal"); |
| 1120 | if (!IS_ERR(clk_osc)) { |
| 1121 | s->clk = clk_osc; |
| 1122 | fmin = 500000; |
| 1123 | fmax = 35000000; |
| 1124 | } else if (!IS_ERR(clk_xtal)) { |
| 1125 | s->clk = clk_xtal; |
| 1126 | fmin = 1000000; |
| 1127 | fmax = 4000000; |
| 1128 | xtal = true; |
| 1129 | } else if (PTR_ERR(clk_osc) == -EPROBE_DEFER || |
| 1130 | PTR_ERR(clk_xtal) == -EPROBE_DEFER) { |
| 1131 | return -EPROBE_DEFER; |
| 1132 | } else { |
| 1133 | dev_err(dev, "Cannot get clock\n"); |
| 1134 | return -EINVAL; |
| 1135 | } |
| 1136 | |
| 1137 | ret = clk_prepare_enable(s->clk); |
| 1138 | if (ret) |
| 1139 | return ret; |
| 1140 | |
| 1141 | freq = clk_get_rate(s->clk); |
| 1142 | /* Check frequency limits */ |
| 1143 | if (freq < fmin || freq > fmax) { |
| 1144 | ret = -ERANGE; |
| 1145 | goto out_clk; |
| 1146 | } |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 1147 | |
Alexander Shiyan | 27027a7 | 2014-02-10 22:18:30 +0400 | [diff] [blame] | 1148 | s->regmap = regmap; |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 1149 | s->devtype = devtype; |
| 1150 | dev_set_drvdata(dev, s); |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 1151 | |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 1152 | /* Check device to ensure we are talking to what we expect */ |
| 1153 | ret = devtype->detect(dev); |
| 1154 | if (ret) |
Alexander Shiyan | d3a8a25 | 2014-02-10 22:18:31 +0400 | [diff] [blame] | 1155 | goto out_clk; |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 1156 | |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 1157 | for (i = 0; i < devtype->nr; i++) { |
| 1158 | unsigned int offs = i << 5; |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 1159 | |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 1160 | /* Reset port */ |
| 1161 | regmap_write(s->regmap, MAX310X_MODE2_REG + offs, |
| 1162 | MAX310X_MODE2_RST_BIT); |
| 1163 | /* Clear port reset */ |
| 1164 | regmap_write(s->regmap, MAX310X_MODE2_REG + offs, 0); |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 1165 | |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 1166 | /* Wait for port startup */ |
| 1167 | do { |
| 1168 | regmap_read(s->regmap, |
| 1169 | MAX310X_BRGDIVLSB_REG + offs, &ret); |
| 1170 | } while (ret != 0x01); |
| 1171 | |
| 1172 | regmap_update_bits(s->regmap, MAX310X_MODE1_REG + offs, |
| 1173 | MAX310X_MODE1_AUTOSLEEP_BIT, |
| 1174 | MAX310X_MODE1_AUTOSLEEP_BIT); |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 1175 | } |
| 1176 | |
Alexander Shiyan | d3a8a25 | 2014-02-10 22:18:31 +0400 | [diff] [blame] | 1177 | uartclk = max310x_set_ref_clk(s, freq, xtal); |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 1178 | dev_dbg(dev, "Reference clock set to %i Hz\n", uartclk); |
| 1179 | |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 1180 | /* Register UART driver */ |
| 1181 | s->uart.owner = THIS_MODULE; |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 1182 | s->uart.dev_name = "ttyMAX"; |
| 1183 | s->uart.major = MAX310X_MAJOR; |
| 1184 | s->uart.minor = MAX310X_MINOR; |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 1185 | s->uart.nr = devtype->nr; |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 1186 | ret = uart_register_driver(&s->uart); |
| 1187 | if (ret) { |
| 1188 | dev_err(dev, "Registering UART driver failed\n"); |
Alexander Shiyan | d3a8a25 | 2014-02-10 22:18:31 +0400 | [diff] [blame] | 1189 | goto out_clk; |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 1190 | } |
| 1191 | |
Alexander Shiyan | dba29a2 | 2014-02-10 22:18:32 +0400 | [diff] [blame] | 1192 | #ifdef CONFIG_GPIOLIB |
| 1193 | /* Setup GPIO cotroller */ |
| 1194 | s->gpio.owner = THIS_MODULE; |
| 1195 | s->gpio.dev = dev; |
| 1196 | s->gpio.label = dev_name(dev); |
| 1197 | s->gpio.direction_input = max310x_gpio_direction_input; |
| 1198 | s->gpio.get = max310x_gpio_get; |
| 1199 | s->gpio.direction_output= max310x_gpio_direction_output; |
| 1200 | s->gpio.set = max310x_gpio_set; |
| 1201 | s->gpio.base = -1; |
| 1202 | s->gpio.ngpio = devtype->nr * 4; |
| 1203 | s->gpio.can_sleep = 1; |
| 1204 | ret = gpiochip_add(&s->gpio); |
| 1205 | if (ret) |
| 1206 | goto out_uart; |
| 1207 | #endif |
| 1208 | |
Alexander Shiyan | 0fbae88 | 2014-02-10 22:18:33 +0400 | [diff] [blame] | 1209 | mutex_init(&s->mutex); |
| 1210 | |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 1211 | for (i = 0; i < devtype->nr; i++) { |
| 1212 | /* Initialize port data */ |
| 1213 | s->p[i].port.line = i; |
| 1214 | s->p[i].port.dev = dev; |
| 1215 | s->p[i].port.irq = irq; |
| 1216 | s->p[i].port.type = PORT_MAX310X; |
| 1217 | s->p[i].port.fifosize = MAX310X_FIFO_SIZE; |
Alexander Shiyan | e7b8a3c | 2014-02-07 18:16:07 +0400 | [diff] [blame] | 1218 | s->p[i].port.flags = UPF_FIXED_TYPE | UPF_LOW_LATENCY; |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 1219 | s->p[i].port.iotype = UPIO_PORT; |
| 1220 | s->p[i].port.iobase = i * 0x20; |
| 1221 | s->p[i].port.membase = (void __iomem *)~0; |
| 1222 | s->p[i].port.uartclk = uartclk; |
| 1223 | s->p[i].port.ops = &max310x_ops; |
| 1224 | /* Disable all interrupts */ |
| 1225 | max310x_port_write(&s->p[i].port, MAX310X_IRQEN_REG, 0); |
| 1226 | /* Clear IRQ status register */ |
| 1227 | max310x_port_read(&s->p[i].port, MAX310X_IRQSTS_REG); |
| 1228 | /* Enable IRQ pin */ |
| 1229 | max310x_port_update(&s->p[i].port, MAX310X_MODE1_REG, |
| 1230 | MAX310X_MODE1_IRQSEL_BIT, |
| 1231 | MAX310X_MODE1_IRQSEL_BIT); |
| 1232 | /* Initialize queue for start TX */ |
| 1233 | INIT_WORK(&s->p[i].tx_work, max310x_wq_proc); |
Alexander Shiyan | e7b8a3c | 2014-02-07 18:16:07 +0400 | [diff] [blame] | 1234 | /* Initialize queue for changing mode */ |
| 1235 | INIT_WORK(&s->p[i].md_work, max310x_md_proc); |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 1236 | /* Register port */ |
| 1237 | uart_add_one_port(&s->uart, &s->p[i].port); |
| 1238 | /* Go to suspend mode */ |
| 1239 | devtype->power(&s->p[i].port, 0); |
| 1240 | } |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 1241 | |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 1242 | /* Setup interrupt */ |
| 1243 | ret = devm_request_threaded_irq(dev, irq, NULL, max310x_ist, |
Alexander Shiyan | 58afc90 | 2014-02-10 22:18:36 +0400 | [diff] [blame] | 1244 | IRQF_ONESHOT | flags, dev_name(dev), s); |
Alexander Shiyan | d3a8a25 | 2014-02-10 22:18:31 +0400 | [diff] [blame] | 1245 | if (!ret) |
| 1246 | return 0; |
| 1247 | |
| 1248 | dev_err(dev, "Unable to reguest IRQ %i\n", irq); |
Alexander Shiyan | dba29a2 | 2014-02-10 22:18:32 +0400 | [diff] [blame] | 1249 | |
Alexander Shiyan | 0fbae88 | 2014-02-10 22:18:33 +0400 | [diff] [blame] | 1250 | mutex_destroy(&s->mutex); |
| 1251 | |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 1252 | #ifdef CONFIG_GPIOLIB |
Alexander Shiyan | dba29a2 | 2014-02-10 22:18:32 +0400 | [diff] [blame] | 1253 | WARN_ON(gpiochip_remove(&s->gpio)); |
Alexander Shiyan | d3a8a25 | 2014-02-10 22:18:31 +0400 | [diff] [blame] | 1254 | |
Alexander Shiyan | dba29a2 | 2014-02-10 22:18:32 +0400 | [diff] [blame] | 1255 | out_uart: |
Alexander Shiyan | d4f6b41 | 2014-02-13 22:08:29 +0400 | [diff] [blame] | 1256 | #endif |
Alexander Shiyan | dba29a2 | 2014-02-10 22:18:32 +0400 | [diff] [blame] | 1257 | uart_unregister_driver(&s->uart); |
| 1258 | |
Alexander Shiyan | d3a8a25 | 2014-02-10 22:18:31 +0400 | [diff] [blame] | 1259 | out_clk: |
| 1260 | clk_disable_unprepare(s->clk); |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 1261 | |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 1262 | return ret; |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 1263 | } |
| 1264 | |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 1265 | static int max310x_remove(struct device *dev) |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 1266 | { |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 1267 | struct max310x_port *s = dev_get_drvdata(dev); |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 1268 | int i, ret = 0; |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 1269 | |
Alexander Shiyan | dba29a2 | 2014-02-10 22:18:32 +0400 | [diff] [blame] | 1270 | #ifdef CONFIG_GPIOLIB |
| 1271 | ret = gpiochip_remove(&s->gpio); |
| 1272 | if (ret) |
| 1273 | return ret; |
| 1274 | #endif |
| 1275 | |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 1276 | for (i = 0; i < s->uart.nr; i++) { |
| 1277 | cancel_work_sync(&s->p[i].tx_work); |
Alexander Shiyan | e7b8a3c | 2014-02-07 18:16:07 +0400 | [diff] [blame] | 1278 | cancel_work_sync(&s->p[i].md_work); |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 1279 | uart_remove_one_port(&s->uart, &s->p[i].port); |
| 1280 | s->devtype->power(&s->p[i].port, 0); |
| 1281 | } |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 1282 | |
Alexander Shiyan | 0fbae88 | 2014-02-10 22:18:33 +0400 | [diff] [blame] | 1283 | mutex_destroy(&s->mutex); |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 1284 | uart_unregister_driver(&s->uart); |
Alexander Shiyan | d3a8a25 | 2014-02-10 22:18:31 +0400 | [diff] [blame] | 1285 | clk_disable_unprepare(s->clk); |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 1286 | |
Emil Goode | 23e7c6a | 2012-08-18 18:12:48 +0200 | [diff] [blame] | 1287 | return ret; |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 1288 | } |
| 1289 | |
Alexander Shiyan | 58afc90 | 2014-02-10 22:18:36 +0400 | [diff] [blame] | 1290 | static const struct of_device_id __maybe_unused max310x_dt_ids[] = { |
| 1291 | { .compatible = "maxim,max3107", .data = &max3107_devtype, }, |
| 1292 | { .compatible = "maxim,max3108", .data = &max3108_devtype, }, |
| 1293 | { .compatible = "maxim,max3109", .data = &max3109_devtype, }, |
| 1294 | { .compatible = "maxim,max14830", .data = &max14830_devtype }, |
| 1295 | { } |
| 1296 | }; |
| 1297 | MODULE_DEVICE_TABLE(of, max310x_dt_ids); |
| 1298 | |
Alexander Shiyan | 27027a7 | 2014-02-10 22:18:30 +0400 | [diff] [blame] | 1299 | static struct regmap_config regcfg = { |
| 1300 | .reg_bits = 8, |
| 1301 | .val_bits = 8, |
| 1302 | .write_flag_mask = 0x80, |
| 1303 | .cache_type = REGCACHE_RBTREE, |
| 1304 | .writeable_reg = max310x_reg_writeable, |
| 1305 | .volatile_reg = max310x_reg_volatile, |
| 1306 | .precious_reg = max310x_reg_precious, |
| 1307 | }; |
| 1308 | |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 1309 | #ifdef CONFIG_SPI_MASTER |
| 1310 | static int max310x_spi_probe(struct spi_device *spi) |
| 1311 | { |
Alexander Shiyan | 58afc90 | 2014-02-10 22:18:36 +0400 | [diff] [blame] | 1312 | struct max310x_devtype *devtype; |
| 1313 | unsigned long flags = 0; |
Alexander Shiyan | 27027a7 | 2014-02-10 22:18:30 +0400 | [diff] [blame] | 1314 | struct regmap *regmap; |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 1315 | int ret; |
| 1316 | |
| 1317 | /* Setup SPI bus */ |
| 1318 | spi->bits_per_word = 8; |
| 1319 | spi->mode = spi->mode ? : SPI_MODE_0; |
| 1320 | spi->max_speed_hz = spi->max_speed_hz ? : 26000000; |
| 1321 | ret = spi_setup(spi); |
Alexander Shiyan | 27027a7 | 2014-02-10 22:18:30 +0400 | [diff] [blame] | 1322 | if (ret) |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 1323 | return ret; |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 1324 | |
Alexander Shiyan | 58afc90 | 2014-02-10 22:18:36 +0400 | [diff] [blame] | 1325 | if (spi->dev.of_node) { |
| 1326 | const struct of_device_id *of_id = |
| 1327 | of_match_device(max310x_dt_ids, &spi->dev); |
| 1328 | |
| 1329 | devtype = (struct max310x_devtype *)of_id->data; |
| 1330 | } else { |
| 1331 | const struct spi_device_id *id_entry = spi_get_device_id(spi); |
| 1332 | |
| 1333 | devtype = (struct max310x_devtype *)id_entry->driver_data; |
| 1334 | flags = IRQF_TRIGGER_FALLING; |
| 1335 | } |
| 1336 | |
Alexander Shiyan | 27027a7 | 2014-02-10 22:18:30 +0400 | [diff] [blame] | 1337 | regcfg.max_register = devtype->nr * 0x20 - 1; |
| 1338 | regmap = devm_regmap_init_spi(spi, ®cfg); |
| 1339 | |
Alexander Shiyan | 58afc90 | 2014-02-10 22:18:36 +0400 | [diff] [blame] | 1340 | return max310x_probe(&spi->dev, devtype, regmap, spi->irq, flags); |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 1341 | } |
| 1342 | |
| 1343 | static int max310x_spi_remove(struct spi_device *spi) |
| 1344 | { |
| 1345 | return max310x_remove(&spi->dev); |
| 1346 | } |
| 1347 | |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 1348 | static const struct spi_device_id max310x_id_table[] = { |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 1349 | { "max3107", (kernel_ulong_t)&max3107_devtype, }, |
| 1350 | { "max3108", (kernel_ulong_t)&max3108_devtype, }, |
Alexander Shiyan | 21fc509 | 2013-06-29 10:44:18 +0400 | [diff] [blame] | 1351 | { "max3109", (kernel_ulong_t)&max3109_devtype, }, |
Alexander Shiyan | 003236d | 2013-06-29 10:44:19 +0400 | [diff] [blame] | 1352 | { "max14830", (kernel_ulong_t)&max14830_devtype, }, |
Axel Lin | 1838b8c | 2012-11-04 23:34:18 +0800 | [diff] [blame] | 1353 | { } |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 1354 | }; |
| 1355 | MODULE_DEVICE_TABLE(spi, max310x_id_table); |
| 1356 | |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 1357 | static struct spi_driver max310x_uart_driver = { |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 1358 | .driver = { |
Alexander Shiyan | 58afc90 | 2014-02-10 22:18:36 +0400 | [diff] [blame] | 1359 | .name = MAX310X_NAME, |
| 1360 | .owner = THIS_MODULE, |
| 1361 | .of_match_table = of_match_ptr(max310x_dt_ids), |
| 1362 | .pm = &max310x_pm_ops, |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 1363 | }, |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 1364 | .probe = max310x_spi_probe, |
| 1365 | .remove = max310x_spi_remove, |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 1366 | .id_table = max310x_id_table, |
| 1367 | }; |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 1368 | module_spi_driver(max310x_uart_driver); |
| 1369 | #endif |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 1370 | |
Alexander Shiyan | 10d8b34 | 2013-06-29 10:44:17 +0400 | [diff] [blame] | 1371 | MODULE_LICENSE("GPL"); |
Alexander Shiyan | f654441 | 2012-08-06 19:42:32 +0400 | [diff] [blame] | 1372 | MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>"); |
| 1373 | MODULE_DESCRIPTION("MAX310X serial driver"); |