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