1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Maxim (Dallas) MAX3107/8/9, MAX14830 serial driver 4 * 5 * Copyright (C) 2012-2016 Alexander Shiyan <shc_work@mail.ru> 6 * 7 * Based on max3100.c, by Christian Pellegrin <chripell@evolware.org> 8 * Based on max3110.c, by Feng Tang <feng.tang@intel.com> 9 * Based on max3107.c, by Aavamobile 10 */ 11 12 #include <linux/bitfield.h> 13 #include <linux/bitops.h> 14 #include <linux/clk.h> 15 #include <linux/delay.h> 16 #include <linux/device.h> 17 #include <linux/gpio/driver.h> 18 #include <linux/hrtimer.h> 19 #include <linux/i2c.h> 20 #include <linux/module.h> 21 #include <linux/property.h> 22 #include <linux/regmap.h> 23 #include <linux/serial_core.h> 24 #include <linux/serial.h> 25 #include <linux/tty.h> 26 #include <linux/tty_flip.h> 27 #include <linux/spi/spi.h> 28 #include <linux/uaccess.h> 29 30 #define MAX310X_NAME "max310x" 31 #define MAX310X_MAJOR 204 32 #define MAX310X_MINOR 209 33 #define MAX310X_UART_NRMAX 16 34 #define MAX310X_MAX_PORTS 4 /* Maximum number of UART ports per IC. */ 35 36 /* MAX310X register definitions */ 37 #define MAX310X_RHR_REG (0x00) /* RX FIFO */ 38 #define MAX310X_THR_REG (0x00) /* TX FIFO */ 39 #define MAX310X_IRQEN_REG (0x01) /* IRQ enable */ 40 #define MAX310X_IRQSTS_REG (0x02) /* IRQ status */ 41 #define MAX310X_LSR_IRQEN_REG (0x03) /* LSR IRQ enable */ 42 #define MAX310X_LSR_IRQSTS_REG (0x04) /* LSR IRQ status */ 43 #define MAX310X_REG_05 (0x05) 44 #define MAX310X_SPCHR_IRQEN_REG MAX310X_REG_05 /* Special char IRQ en */ 45 #define MAX310X_SPCHR_IRQSTS_REG (0x06) /* Special char IRQ status */ 46 #define MAX310X_STS_IRQEN_REG (0x07) /* Status IRQ enable */ 47 #define MAX310X_STS_IRQSTS_REG (0x08) /* Status IRQ status */ 48 #define MAX310X_MODE1_REG (0x09) /* MODE1 */ 49 #define MAX310X_MODE2_REG (0x0a) /* MODE2 */ 50 #define MAX310X_LCR_REG (0x0b) /* LCR */ 51 #define MAX310X_RXTO_REG (0x0c) /* RX timeout */ 52 #define MAX310X_HDPIXDELAY_REG (0x0d) /* Auto transceiver delays */ 53 #define MAX310X_IRDA_REG (0x0e) /* IRDA settings */ 54 #define MAX310X_FLOWLVL_REG (0x0f) /* Flow control levels */ 55 #define MAX310X_FIFOTRIGLVL_REG (0x10) /* FIFO IRQ trigger levels */ 56 #define MAX310X_TXFIFOLVL_REG (0x11) /* TX FIFO level */ 57 #define MAX310X_RXFIFOLVL_REG (0x12) /* RX FIFO level */ 58 #define MAX310X_FLOWCTRL_REG (0x13) /* Flow control */ 59 #define MAX310X_XON1_REG (0x14) /* XON1 character */ 60 #define MAX310X_XON2_REG (0x15) /* XON2 character */ 61 #define MAX310X_XOFF1_REG (0x16) /* XOFF1 character */ 62 #define MAX310X_XOFF2_REG (0x17) /* XOFF2 character */ 63 #define MAX310X_GPIOCFG_REG (0x18) /* GPIO config */ 64 #define MAX310X_GPIODATA_REG (0x19) /* GPIO data */ 65 #define MAX310X_PLLCFG_REG (0x1a) /* PLL config */ 66 #define MAX310X_BRGCFG_REG (0x1b) /* Baud rate generator conf */ 67 #define MAX310X_BRGDIVLSB_REG (0x1c) /* Baud rate divisor LSB */ 68 #define MAX310X_BRGDIVMSB_REG (0x1d) /* Baud rate divisor MSB */ 69 #define MAX310X_CLKSRC_REG (0x1e) /* Clock source */ 70 #define MAX310X_REG_1F (0x1f) 71 #define MAX310X_EXTREG_START (0x20) /* Only relevant in SPI mode. */ 72 73 #define MAX310X_REVID_REG MAX310X_REG_1F /* Revision ID */ 74 75 #define MAX310X_GLOBALIRQ_REG MAX310X_REG_1F /* Global IRQ (RO) */ 76 #define MAX310X_GLOBALCMD_REG MAX310X_REG_1F /* Global Command (WO) */ 77 78 /* Extended registers */ 79 #define MAX310X_REVID_EXTREG (0x25) /* Revision ID 80 * (extended addressing space) 81 */ 82 /* IRQ register bits */ 83 #define MAX310X_IRQ_LSR_BIT (1 << 0) /* LSR interrupt */ 84 #define MAX310X_IRQ_SPCHR_BIT (1 << 1) /* Special char interrupt */ 85 #define MAX310X_IRQ_STS_BIT (1 << 2) /* Status interrupt */ 86 #define MAX310X_IRQ_RXFIFO_BIT (1 << 3) /* RX FIFO interrupt */ 87 #define MAX310X_IRQ_TXFIFO_BIT (1 << 4) /* TX FIFO interrupt */ 88 #define MAX310X_IRQ_TXEMPTY_BIT (1 << 5) /* TX FIFO empty interrupt */ 89 #define MAX310X_IRQ_RXEMPTY_BIT (1 << 6) /* RX FIFO empty interrupt */ 90 #define MAX310X_IRQ_CTS_BIT (1 << 7) /* CTS interrupt */ 91 92 /* LSR register bits */ 93 #define MAX310X_LSR_RXTO_BIT (1 << 0) /* RX timeout */ 94 #define MAX310X_LSR_RXOVR_BIT (1 << 1) /* RX overrun */ 95 #define MAX310X_LSR_RXPAR_BIT (1 << 2) /* RX parity error */ 96 #define MAX310X_LSR_FRERR_BIT (1 << 3) /* Frame error */ 97 #define MAX310X_LSR_RXBRK_BIT (1 << 4) /* RX break */ 98 #define MAX310X_LSR_RXNOISE_BIT (1 << 5) /* RX noise */ 99 #define MAX310X_LSR_CTS_BIT (1 << 7) /* CTS pin state */ 100 101 /* Special character register bits */ 102 #define MAX310X_SPCHR_XON1_BIT (1 << 0) /* XON1 character */ 103 #define MAX310X_SPCHR_XON2_BIT (1 << 1) /* XON2 character */ 104 #define MAX310X_SPCHR_XOFF1_BIT (1 << 2) /* XOFF1 character */ 105 #define MAX310X_SPCHR_XOFF2_BIT (1 << 3) /* XOFF2 character */ 106 #define MAX310X_SPCHR_BREAK_BIT (1 << 4) /* RX break */ 107 #define MAX310X_SPCHR_MULTIDROP_BIT (1 << 5) /* 9-bit multidrop addr char */ 108 109 /* Status register bits */ 110 #define MAX310X_STS_GPIO0_BIT (1 << 0) /* GPIO 0 interrupt */ 111 #define MAX310X_STS_GPIO1_BIT (1 << 1) /* GPIO 1 interrupt */ 112 #define MAX310X_STS_GPIO2_BIT (1 << 2) /* GPIO 2 interrupt */ 113 #define MAX310X_STS_GPIO3_BIT (1 << 3) /* GPIO 3 interrupt */ 114 #define MAX310X_STS_CLKREADY_BIT (1 << 5) /* Clock ready */ 115 #define MAX310X_STS_SLEEP_BIT (1 << 6) /* Sleep interrupt */ 116 117 /* MODE1 register bits */ 118 #define MAX310X_MODE1_RXDIS_BIT (1 << 0) /* RX disable */ 119 #define MAX310X_MODE1_TXDIS_BIT (1 << 1) /* TX disable */ 120 #define MAX310X_MODE1_TXHIZ_BIT (1 << 2) /* TX pin three-state */ 121 #define MAX310X_MODE1_RTSHIZ_BIT (1 << 3) /* RTS pin three-state */ 122 #define MAX310X_MODE1_TRNSCVCTRL_BIT (1 << 4) /* Transceiver ctrl enable */ 123 #define MAX310X_MODE1_FORCESLEEP_BIT (1 << 5) /* Force sleep mode */ 124 #define MAX310X_MODE1_AUTOSLEEP_BIT (1 << 6) /* Auto sleep enable */ 125 #define MAX310X_MODE1_IRQSEL_BIT (1 << 7) /* IRQ pin enable */ 126 127 /* MODE2 register bits */ 128 #define MAX310X_MODE2_RST_BIT (1 << 0) /* Chip reset */ 129 #define MAX310X_MODE2_FIFORST_BIT (1 << 1) /* FIFO reset */ 130 #define MAX310X_MODE2_RXTRIGINV_BIT (1 << 2) /* RX FIFO INT invert */ 131 #define MAX310X_MODE2_RXEMPTINV_BIT (1 << 3) /* RX FIFO empty INT invert */ 132 #define MAX310X_MODE2_SPCHR_BIT (1 << 4) /* Special chr detect enable */ 133 #define MAX310X_MODE2_LOOPBACK_BIT (1 << 5) /* Internal loopback enable */ 134 #define MAX310X_MODE2_MULTIDROP_BIT (1 << 6) /* 9-bit multidrop enable */ 135 #define MAX310X_MODE2_ECHOSUPR_BIT (1 << 7) /* ECHO suppression enable */ 136 137 /* LCR register bits */ 138 #define MAX310X_LCR_LENGTH0_BIT (1 << 0) /* Word length bit 0 */ 139 #define MAX310X_LCR_LENGTH1_BIT (1 << 1) /* Word length bit 1 140 * 141 * Word length bits table: 142 * 00 -> 5 bit words 143 * 01 -> 6 bit words 144 * 10 -> 7 bit words 145 * 11 -> 8 bit words 146 */ 147 #define MAX310X_LCR_STOPLEN_BIT (1 << 2) /* STOP length bit 148 * 149 * STOP length bit table: 150 * 0 -> 1 stop bit 151 * 1 -> 1-1.5 stop bits if 152 * word length is 5, 153 * 2 stop bits otherwise 154 */ 155 #define MAX310X_LCR_PARITY_BIT (1 << 3) /* Parity bit enable */ 156 #define MAX310X_LCR_EVENPARITY_BIT (1 << 4) /* Even parity bit enable */ 157 #define MAX310X_LCR_FORCEPARITY_BIT (1 << 5) /* 9-bit multidrop parity */ 158 #define MAX310X_LCR_TXBREAK_BIT (1 << 6) /* TX break enable */ 159 #define MAX310X_LCR_RTS_BIT (1 << 7) /* RTS pin control */ 160 /* LCR bits owned by termios; TX break and RTS are driven elsewhere */ 161 #define MAX310X_LCR_TERMIOS_MASK GENMASK(5, 0) 162 163 /* IRDA register bits */ 164 #define MAX310X_IRDA_IRDAEN_BIT (1 << 0) /* IRDA mode enable */ 165 #define MAX310X_IRDA_SIR_BIT (1 << 1) /* SIR mode enable */ 166 #define MAX310X_IRDA_RTSINVERT_BIT (1 << 2) /* Invert RTS output */ 167 168 /* HDPIXDELAY accessor macros */ 169 #define MAX310X_HDPIXDELAY_SETUP(val) (((val) & 0x0f) << 4) 170 #define MAX310X_HDPIXDELAY_HOLD(val) ((val) & 0x0f) 171 172 /* Flow control trigger level register masks */ 173 #define MAX310X_FLOWLVL_HALT_MASK GENMASK(3, 0) /* Flow control halt level */ 174 #define MAX310X_FLOWLVL_RES_MASK GENMASK(7, 4) /* Flow control resume level */ 175 #define MAX310X_FLOWLVL_HALT(words) ((words / 8) & 0x0f) 176 #define MAX310X_FLOWLVL_RES(words) (((words / 8) & 0x0f) << 4) 177 178 /* FIFO interrupt trigger level register masks */ 179 #define MAX310X_FIFOTRIGLVL_TX_MASK GENMASK(3, 0) /* TX FIFO trigger level */ 180 #define MAX310X_FIFOTRIGLVL_RX_MASK GENMASK(7, 4) /* RX FIFO trigger level */ 181 #define MAX310X_FIFOTRIGLVL_TX(words) ((words / 8) & 0x0f) 182 #define MAX310X_FIFOTRIGLVL_RX(words) (((words / 8) & 0x0f) << 4) 183 184 /* Flow control register bits */ 185 #define MAX310X_FLOWCTRL_AUTORTS_BIT (1 << 0) /* Auto RTS flow ctrl enable */ 186 #define MAX310X_FLOWCTRL_AUTOCTS_BIT (1 << 1) /* Auto CTS flow ctrl enable */ 187 #define MAX310X_FLOWCTRL_GPIADDR_BIT (1 << 2) /* Enables that GPIO inputs 188 * are used in conjunction with 189 * XOFF2 for definition of 190 * special character 191 */ 192 #define MAX310X_FLOWCTRL_SWFLOWEN_BIT (1 << 3) /* Auto SW flow ctrl enable */ 193 #define MAX310X_FLOWCTRL_SWFLOW0_BIT (1 << 4) /* SWFLOW bit 0 */ 194 #define MAX310X_FLOWCTRL_SWFLOW1_BIT (1 << 5) /* SWFLOW bit 1 195 * 196 * SWFLOW bits 1 & 0 table: 197 * 00 -> no transmitter flow 198 * control 199 * 01 -> receiver compares 200 * XON2 and XOFF2 201 * and controls 202 * transmitter 203 * 10 -> receiver compares 204 * XON1 and XOFF1 205 * and controls 206 * transmitter 207 * 11 -> receiver compares 208 * XON1, XON2, XOFF1 and 209 * XOFF2 and controls 210 * transmitter 211 */ 212 #define MAX310X_FLOWCTRL_SWFLOW2_BIT (1 << 6) /* SWFLOW bit 2 */ 213 #define MAX310X_FLOWCTRL_SWFLOW3_BIT (1 << 7) /* SWFLOW bit 3 214 * 215 * SWFLOW bits 3 & 2 table: 216 * 00 -> no received flow 217 * control 218 * 01 -> transmitter generates 219 * XON2 and XOFF2 220 * 10 -> transmitter generates 221 * XON1 and XOFF1 222 * 11 -> transmitter generates 223 * XON1, XON2, XOFF1 and 224 * XOFF2 225 */ 226 227 /* PLL configuration register masks */ 228 #define MAX310X_PLLCFG_PREDIV_MASK GENMASK(5, 0) /* PLL predivision value */ 229 #define MAX310X_PLLCFG_PLLFACTOR_MASK GENMASK(7, 6) /* PLL multiplication factor */ 230 231 /* Baud rate generator configuration register bits */ 232 #define MAX310X_BRGCFG_2XMODE_BIT (1 << 4) /* Double baud rate */ 233 #define MAX310X_BRGCFG_4XMODE_BIT (1 << 5) /* Quadruple baud rate */ 234 235 /* Clock source register bits */ 236 #define MAX310X_CLKSRC_CRYST_BIT (1 << 1) /* Crystal osc enable */ 237 #define MAX310X_CLKSRC_PLL_BIT (1 << 2) /* PLL enable */ 238 #define MAX310X_CLKSRC_PLLBYP_BIT (1 << 3) /* PLL bypass */ 239 #define MAX310X_CLKSRC_EXTCLK_BIT (1 << 4) /* External clock enable */ 240 #define MAX310X_CLKSRC_CLK2RTS_BIT (1 << 7) /* Baud clk to RTS pin */ 241 242 /* Global commands */ 243 #define MAX310X_EXTREG_ENBL (0xce) 244 #define MAX310X_EXTREG_DSBL (0xcd) 245 246 /* Misc definitions */ 247 #define MAX310X_FIFO_SIZE (128) 248 #define MAX310x_REV_MASK GENMASK(7, 3) 249 #define MAX310X_WRITE_BIT 0x80 250 251 /* Port startup definitions */ 252 #define MAX310X_PORT_STARTUP_SLEEP_US 10000 /* Delay between retries */ 253 #define MAX310X_PORT_STARTUP_TIMEOUT_US (20 * MAX310X_PORT_STARTUP_SLEEP_US) /* Total timeout */ 254 255 /* Crystal-related definitions */ 256 #define MAX310X_XTAL_SLEEP_US 10000 /* Delay between retries */ 257 #define MAX310X_XTAL_TIMEOUT_US (20 * MAX310X_XTAL_SLEEP_US) /* Total timeout */ 258 259 /* MAX3107 specific */ 260 #define MAX3107_REV_ID (0xa0) 261 262 /* MAX3109 specific */ 263 #define MAX3109_REV_ID (0xc0) 264 265 /* MAX14830 specific */ 266 #define MAX14830_BRGCFG_CLKDIS_BIT (1 << 6) /* Clock Disable */ 267 #define MAX14830_REV_ID (0xb0) 268 269 struct max310x_clk_config_t { 270 u8 prediv; /* Predivider */ 271 u8 pll_mult; /* PLL multiplier */ 272 unsigned int fref; /* 273 * Reference clock for fractional baud rate generator: 274 * PLL enabled: (freq / prediv) x pll_mult 275 * PLL disabled: freq 276 */ 277 unsigned int err; /* Computed error for selected parameters */ 278 }; 279 280 struct max310x_if_cfg { 281 int (*extended_reg_enable)(struct device *dev, bool enable); 282 u8 rev_id_offset; 283 }; 284 285 struct max310x_devtype { 286 struct { 287 unsigned short min; 288 unsigned short max; 289 } slave_addr; /* Relevant only in I2C mode. */ 290 int nr; 291 char name[9]; 292 u8 mode1; 293 u8 rev_id_val; 294 u8 rev_id_reg; /* Relevant only if rev_id_val is defined. */ 295 u8 power_reg; /* Register address for power/sleep control. */ 296 u8 power_bit; /* Bit for sleep or power-off mode (active high). */ 297 }; 298 299 /* Software-timed RS485 RTS envelope phase */ 300 enum max310x_tx_state { 301 MAX310X_TX_OFF, /* idle, RTS released */ 302 MAX310X_TX_WAIT_BEFORE_SEND, /* RTS asserted, before-send delay */ 303 MAX310X_TX_SEND, /* data in flight, awaiting TX-empty */ 304 MAX310X_TX_WAIT_AFTER_SEND, /* data drained, after-send hold */ 305 }; 306 307 struct max310x_one { 308 struct uart_port port; 309 struct work_struct tx_work; 310 struct work_struct md_work; 311 struct work_struct rs_work; 312 struct work_struct rts_work; 313 struct hrtimer tx_delay_tmr; 314 struct regmap *regmap; 315 unsigned int char_time_us; 316 unsigned int baud; 317 bool sw_rts_during_tx; 318 bool cancel_tx_delay_tmr; 319 bool tx_teardown; /* envelope being torn down */ 320 bool rs485_pending; /* rs_work not yet applied */ 321 bool tx_break; /* break_ctl() owns the transceiver */ 322 enum max310x_tx_state tx_state; 323 324 u8 rx_buf[MAX310X_FIFO_SIZE]; 325 }; 326 #define to_max310x_port(_port) \ 327 container_of(_port, struct max310x_one, port) 328 329 struct max310x_port { 330 const struct max310x_devtype *devtype; 331 const struct max310x_if_cfg *if_cfg; 332 struct regmap *regmap; 333 struct clk *clk; 334 #ifdef CONFIG_GPIOLIB 335 struct gpio_chip gpio; 336 #endif 337 struct max310x_one p[]; 338 }; 339 340 static struct uart_driver max310x_uart = { 341 .owner = THIS_MODULE, 342 .driver_name = MAX310X_NAME, 343 .dev_name = "ttyMAX", 344 .major = MAX310X_MAJOR, 345 .minor = MAX310X_MINOR, 346 .nr = MAX310X_UART_NRMAX, 347 }; 348 349 static DECLARE_BITMAP(max310x_lines, MAX310X_UART_NRMAX); 350 351 static u8 max310x_port_read(struct uart_port *port, u8 reg) 352 { 353 struct max310x_one *one = to_max310x_port(port); 354 unsigned int val = 0; 355 356 regmap_read(one->regmap, reg, &val); 357 358 return val; 359 } 360 361 static void max310x_port_write(struct uart_port *port, u8 reg, u8 val) 362 { 363 struct max310x_one *one = to_max310x_port(port); 364 365 regmap_write(one->regmap, reg, val); 366 } 367 368 static void max310x_port_update(struct uart_port *port, u8 reg, u8 mask, u8 val) 369 { 370 struct max310x_one *one = to_max310x_port(port); 371 372 regmap_update_bits(one->regmap, reg, mask, val); 373 } 374 375 static int max310x_detect(struct device *dev) 376 { 377 struct max310x_port *s = dev_get_drvdata(dev); 378 unsigned int val = 0; 379 int ret; 380 381 /* Check if variant supports REV ID register: */ 382 if (s->devtype->rev_id_val) { 383 u8 rev_id_reg = s->devtype->rev_id_reg; 384 385 /* Check if REV ID is in extended addressing space: */ 386 if (s->devtype->rev_id_reg >= MAX310X_EXTREG_START) { 387 ret = s->if_cfg->extended_reg_enable(dev, true); 388 if (ret) 389 return ret; 390 391 /* Adjust REV ID extended addressing space address: */ 392 if (s->if_cfg->rev_id_offset) 393 rev_id_reg -= s->if_cfg->rev_id_offset; 394 } 395 396 regmap_read(s->regmap, rev_id_reg, &val); 397 398 if (s->devtype->rev_id_reg >= MAX310X_EXTREG_START) { 399 ret = s->if_cfg->extended_reg_enable(dev, false); 400 if (ret) 401 return ret; 402 } 403 404 if (((val & MAX310x_REV_MASK) != s->devtype->rev_id_val)) 405 return dev_err_probe(dev, -ENODEV, 406 "%s ID 0x%02x does not match\n", 407 s->devtype->name, val); 408 } else { 409 /* 410 * For variant without REV ID register, just check default value 411 * from clocksource register to make sure everything works. 412 */ 413 ret = regmap_read(s->regmap, MAX310X_CLKSRC_REG, &val); 414 if (ret) 415 return ret; 416 417 if (val != (MAX310X_CLKSRC_EXTCLK_BIT | MAX310X_CLKSRC_PLLBYP_BIT)) 418 return dev_err_probe(dev, -ENODEV, 419 "%s not present\n", 420 s->devtype->name); 421 } 422 423 return 0; 424 } 425 426 static void max310x_power(struct uart_port *port, int on) 427 { 428 struct max310x_port *s = dev_get_drvdata(port->dev); 429 430 max310x_port_update(port, s->devtype->power_reg, s->devtype->power_bit, 431 on ? 0 : s->devtype->power_bit); 432 if (on) 433 msleep(50); 434 } 435 436 static const struct max310x_devtype max3107_devtype = { 437 .name = "MAX3107", 438 .nr = 1, 439 .mode1 = MAX310X_MODE1_AUTOSLEEP_BIT | MAX310X_MODE1_IRQSEL_BIT, 440 .rev_id_val = MAX3107_REV_ID, 441 .rev_id_reg = MAX310X_REVID_REG, 442 .power_reg = MAX310X_MODE1_REG, 443 .power_bit = MAX310X_MODE1_FORCESLEEP_BIT, 444 .slave_addr = { 445 .min = 0x2c, 446 .max = 0x2f, 447 }, 448 }; 449 450 static const struct max310x_devtype max3108_devtype = { 451 .name = "MAX3108", 452 .nr = 1, 453 .mode1 = MAX310X_MODE1_AUTOSLEEP_BIT, 454 .rev_id_val = 0, /* Unsupported. */ 455 .rev_id_reg = 0, /* Irrelevant when rev_id_val is not defined. */ 456 .power_reg = MAX310X_MODE1_REG, 457 .power_bit = MAX310X_MODE1_FORCESLEEP_BIT, 458 .slave_addr = { 459 .min = 0x60, 460 .max = 0x6f, 461 }, 462 }; 463 464 static const struct max310x_devtype max3109_devtype = { 465 .name = "MAX3109", 466 .nr = 2, 467 .mode1 = MAX310X_MODE1_AUTOSLEEP_BIT, 468 .rev_id_val = MAX3109_REV_ID, 469 .rev_id_reg = MAX310X_REVID_EXTREG, 470 .power_reg = MAX310X_MODE1_REG, 471 .power_bit = MAX310X_MODE1_FORCESLEEP_BIT, 472 .slave_addr = { 473 .min = 0x60, 474 .max = 0x6f, 475 }, 476 }; 477 478 static const struct max310x_devtype max14830_devtype = { 479 .name = "MAX14830", 480 .nr = 4, 481 .mode1 = MAX310X_MODE1_IRQSEL_BIT, 482 .rev_id_val = MAX14830_REV_ID, 483 .rev_id_reg = MAX310X_REVID_EXTREG, 484 .power_reg = MAX310X_BRGCFG_REG, 485 .power_bit = MAX14830_BRGCFG_CLKDIS_BIT, 486 .slave_addr = { 487 .min = 0x60, 488 .max = 0x6f, 489 }, 490 }; 491 492 static bool max310x_reg_writeable(struct device *dev, unsigned int reg) 493 { 494 switch (reg) { 495 case MAX310X_IRQSTS_REG: 496 case MAX310X_LSR_IRQSTS_REG: 497 case MAX310X_SPCHR_IRQSTS_REG: 498 case MAX310X_STS_IRQSTS_REG: 499 case MAX310X_TXFIFOLVL_REG: 500 case MAX310X_RXFIFOLVL_REG: 501 return false; 502 default: 503 return true; 504 } 505 } 506 507 static bool max310x_reg_volatile(struct device *dev, unsigned int reg) 508 { 509 switch (reg) { 510 case MAX310X_RHR_REG: 511 case MAX310X_IRQSTS_REG: 512 case MAX310X_LSR_IRQSTS_REG: 513 case MAX310X_SPCHR_IRQSTS_REG: 514 case MAX310X_STS_IRQSTS_REG: 515 case MAX310X_TXFIFOLVL_REG: 516 case MAX310X_RXFIFOLVL_REG: 517 case MAX310X_GPIODATA_REG: 518 case MAX310X_BRGDIVLSB_REG: 519 case MAX310X_REG_05: 520 case MAX310X_REG_1F: 521 return true; 522 default: 523 return false; 524 } 525 } 526 527 static bool max310x_reg_precious(struct device *dev, unsigned int reg) 528 { 529 switch (reg) { 530 case MAX310X_RHR_REG: 531 case MAX310X_IRQSTS_REG: 532 case MAX310X_SPCHR_IRQSTS_REG: 533 case MAX310X_STS_IRQSTS_REG: 534 return true; 535 default: 536 return false; 537 } 538 } 539 540 static bool max310x_reg_noinc(struct device *dev, unsigned int reg) 541 { 542 return reg == MAX310X_RHR_REG; 543 } 544 545 static int max310x_set_baud(struct uart_port *port, int baud) 546 { 547 unsigned int mode = 0, div = 0, frac = 0, c = 0, F = 0; 548 549 /* 550 * Calculate the integer divisor first. Select a proper mode 551 * in case if the requested baud is too high for the pre-defined 552 * clocks frequency. 553 */ 554 div = port->uartclk / baud; 555 if (div < 8) { 556 /* Mode x4 */ 557 c = 4; 558 mode = MAX310X_BRGCFG_4XMODE_BIT; 559 } else if (div < 16) { 560 /* Mode x2 */ 561 c = 8; 562 mode = MAX310X_BRGCFG_2XMODE_BIT; 563 } else { 564 c = 16; 565 } 566 567 /* Calculate the divisor in accordance with the fraction coefficient */ 568 div /= c; 569 F = c*baud; 570 571 /* Calculate the baud rate fraction */ 572 if (div > 0) 573 frac = (16*(port->uartclk % F)) / F; 574 else 575 div = 1; 576 577 max310x_port_write(port, MAX310X_BRGDIVMSB_REG, div >> 8); 578 max310x_port_write(port, MAX310X_BRGDIVLSB_REG, div); 579 max310x_port_write(port, MAX310X_BRGCFG_REG, frac | mode); 580 581 /* Return the actual baud rate we just programmed */ 582 return (16*port->uartclk) / (c*(16*div + frac)); 583 } 584 585 static void max310x_try_cfg(unsigned int fdiv, u8 div, u8 pll_mult, 586 struct max310x_clk_config_t *cfg) 587 { 588 unsigned int fmul = fdiv * pll_mult; 589 unsigned int err; 590 591 /* Use high-enough baudrate to calculate error */ 592 err = fmul % (460800 * 16); 593 594 if (cfg->err > err) { 595 cfg->err = err; 596 cfg->pll_mult = pll_mult; 597 cfg->prediv = div; 598 cfg->fref = fmul; 599 } 600 } 601 602 static u8 max310x_pll_mult_to_id(u8 pll_mult) 603 { 604 switch (pll_mult) { 605 case 144: return 3; 606 case 96: return 2; 607 case 48: return 1; 608 case 6: 609 default: return 0; 610 } 611 } 612 613 /* 614 * From table 7 in datasheet: PLLFactor Selector Guide 615 * 616 * +-----------+----------------+-------------------+-------------------+ 617 * | PLLFactor | MULTIPLICATION | fPLLIN | fREF | 618 * | (1 & 0) | FACTOR +---------+---------+---------+---------+ 619 * | | | MIN | MAX | MIN | MAX | 620 * +-----------+----------------+---------+---------+---------+---------+ 621 * | 0 | 6 | 500kHz | 800kHz | 3MHz | 4.8MHz | 622 * +-----------+----------------+---------+---------+---------+---------+ 623 * | 1 | 48 | 850kHz | 1.2MHz | 40.8MHz | 56MHz | 624 * +-----------+----------------+---------+---------+---------+---------+ 625 * | 2 | 96 | 425kHz | 1MHz | 40.8MHz | 96MHz | 626 * +-----------+----------------+---------+---------+---------+---------+ 627 * | 3 | 144 | 390kHz | 667kHz | 56MHz | 96MHz | 628 * +-----------+----------------+---------+---------+---------+---------+ 629 */ 630 static int max310x_set_ref_clk(struct device *dev, struct max310x_port *s, 631 unsigned int freq, unsigned int *fref, bool xtal) 632 { 633 unsigned int div, fdiv, clksrc, val; 634 struct max310x_clk_config_t cfg; 635 636 cfg.err = UINT_MAX; 637 cfg.prediv = 0; 638 cfg.fref = freq; 639 640 /* First, update error without PLL */ 641 max310x_try_cfg(freq, 1, 1, &cfg); 642 643 /* Try all possible PLL dividers */ 644 for (div = 1; (div <= 63) && cfg.err; div++) { 645 fdiv = DIV_ROUND_CLOSEST(freq, div); 646 647 if ((fdiv >= 500000) && (fdiv <= 800000)) 648 max310x_try_cfg(fdiv, div, 6, &cfg); /* PLL x6 */ 649 else if ((fdiv >= 850000) && (fdiv <= 1200000)) 650 max310x_try_cfg(fdiv, div, 48, &cfg); /* PLL x48 */ 651 652 if ((fdiv >= 425000) && (fdiv <= 1000000)) 653 max310x_try_cfg(fdiv, div, 96, &cfg); /* PLL x96 */ 654 655 if ((fdiv >= 390000) && (fdiv <= 667000)) 656 max310x_try_cfg(fdiv, div, 144, &cfg); /* PLL x144 */ 657 } 658 659 /* Configure clock source */ 660 clksrc = MAX310X_CLKSRC_EXTCLK_BIT | (xtal ? MAX310X_CLKSRC_CRYST_BIT : 0); 661 662 /* Configure PLL */ 663 if (cfg.prediv) { 664 u8 pll_id = max310x_pll_mult_to_id(cfg.pll_mult); 665 666 clksrc |= MAX310X_CLKSRC_PLL_BIT; 667 val = FIELD_PREP(MAX310X_PLLCFG_PLLFACTOR_MASK, pll_id) | 668 FIELD_PREP(MAX310X_PLLCFG_PREDIV_MASK, cfg.prediv); 669 regmap_write(s->regmap, MAX310X_PLLCFG_REG, val); 670 } else 671 clksrc |= MAX310X_CLKSRC_PLLBYP_BIT; 672 673 regmap_write(s->regmap, MAX310X_CLKSRC_REG, clksrc); 674 675 /* Wait for crystal */ 676 if (xtal) { 677 int ret; 678 679 ret = regmap_read_poll_timeout(s->regmap, MAX310X_STS_IRQSTS_REG, 680 val, val & MAX310X_STS_CLKREADY_BIT, 681 MAX310X_XTAL_SLEEP_US, 682 MAX310X_XTAL_TIMEOUT_US); 683 if (ret) 684 return dev_err_probe(dev, -EAGAIN, 685 "clock is not stable\n"); 686 } 687 688 *fref = cfg.fref; 689 690 return 0; 691 } 692 693 static void max310x_batch_write(struct uart_port *port, u8 *txbuf, unsigned int len) 694 { 695 struct max310x_one *one = to_max310x_port(port); 696 697 regmap_noinc_write(one->regmap, MAX310X_THR_REG, txbuf, len); 698 } 699 700 static void max310x_batch_read(struct uart_port *port, u8 *rxbuf, unsigned int len) 701 { 702 struct max310x_one *one = to_max310x_port(port); 703 704 regmap_noinc_read(one->regmap, MAX310X_RHR_REG, rxbuf, len); 705 } 706 707 static void max310x_rts_ctl(struct uart_port *port, bool rts_state) 708 { 709 max310x_port_update(port, MAX310X_LCR_REG, MAX310X_LCR_RTS_BIT, 710 rts_state ? MAX310X_LCR_RTS_BIT : 0); 711 } 712 713 /* RTS level for the transmitting or the idle phase of an RS485 envelope */ 714 static bool max310x_rts_level(struct uart_port *port, bool active) 715 { 716 return active ? (port->rs485.flags & SER_RS485_RTS_ON_SEND) : 717 (port->rs485.flags & SER_RS485_RTS_AFTER_SEND); 718 } 719 720 /* 721 * Drive the RS485 RTS line to match the current tx_state and break 722 * ownership. This is the only place that touches RTS, and it reads the 723 * state rather than a fixed assert/deassert intent, so a newer assert is 724 * never clobbered by a stale release and an expiring after-send hold never 725 * releases a break in progress. It also arms the before-send timer once the 726 * RTS edge is on the wire, so data is never shifted before RTS is asserted. 727 */ 728 static void max310x_rts_work_proc(struct work_struct *ws) 729 { 730 struct max310x_one *one = container_of(ws, struct max310x_one, rts_work); 731 struct uart_port *port = &one->port; 732 bool rts_on = READ_ONCE(one->tx_state) != MAX310X_TX_OFF || 733 READ_ONCE(one->tx_break); 734 735 max310x_rts_ctl(port, max310x_rts_level(port, rts_on)); 736 737 guard(spinlock_irqsave)(&port->lock); 738 if (READ_ONCE(one->tx_state) == MAX310X_TX_WAIT_BEFORE_SEND && 739 !one->cancel_tx_delay_tmr && !hrtimer_active(&one->tx_delay_tmr)) 740 hrtimer_start(&one->tx_delay_tmr, 741 ms_to_ktime(port->rs485.delay_rts_before_send), 742 HRTIMER_MODE_REL); 743 } 744 745 static void max310x_handle_rx(struct uart_port *port, unsigned int rxlen) 746 { 747 struct max310x_one *one = to_max310x_port(port); 748 unsigned int sts, i; 749 u8 ch, flag; 750 751 if (port->read_status_mask == MAX310X_LSR_RXOVR_BIT) { 752 /* 753 * We are just reading, happily ignoring any error conditions. 754 * Break condition, parity checking, framing errors -- they 755 * are all ignored. That means that we can do a batch-read. 756 * 757 * There is a small opportunity for race if the RX FIFO 758 * overruns while we're reading the buffer; the datasheets says 759 * that the LSR register applies to the "current" character. 760 * That's also the reason why we cannot do batched reads when 761 * asked to check the individual statuses. 762 */ 763 764 sts = max310x_port_read(port, MAX310X_LSR_IRQSTS_REG); 765 max310x_batch_read(port, one->rx_buf, rxlen); 766 767 port->icount.rx += rxlen; 768 flag = TTY_NORMAL; 769 sts &= port->read_status_mask; 770 771 if (sts & MAX310X_LSR_RXOVR_BIT) { 772 dev_warn_ratelimited(port->dev, "Hardware RX FIFO overrun\n"); 773 port->icount.overrun++; 774 } 775 776 for (i = 0; i < (rxlen - 1); ++i) 777 uart_insert_char(port, sts, 0, one->rx_buf[i], flag); 778 779 /* 780 * Handle the overrun case for the last character only, since 781 * the RxFIFO overflow happens after it is pushed to the FIFO 782 * tail. 783 */ 784 uart_insert_char(port, sts, MAX310X_LSR_RXOVR_BIT, 785 one->rx_buf[rxlen-1], flag); 786 787 } else { 788 if (unlikely(rxlen >= port->fifosize)) { 789 dev_warn_ratelimited(port->dev, "Possible RX FIFO overrun\n"); 790 port->icount.buf_overrun++; 791 /* Ensure sanity of RX level */ 792 rxlen = port->fifosize; 793 } 794 795 while (rxlen--) { 796 ch = max310x_port_read(port, MAX310X_RHR_REG); 797 sts = max310x_port_read(port, MAX310X_LSR_IRQSTS_REG); 798 799 sts &= MAX310X_LSR_RXPAR_BIT | MAX310X_LSR_FRERR_BIT | 800 MAX310X_LSR_RXOVR_BIT | MAX310X_LSR_RXBRK_BIT; 801 802 port->icount.rx++; 803 flag = TTY_NORMAL; 804 805 if (unlikely(sts)) { 806 if (sts & MAX310X_LSR_RXBRK_BIT) { 807 port->icount.brk++; 808 if (uart_handle_break(port)) 809 continue; 810 } else if (sts & MAX310X_LSR_RXPAR_BIT) 811 port->icount.parity++; 812 else if (sts & MAX310X_LSR_FRERR_BIT) 813 port->icount.frame++; 814 else if (sts & MAX310X_LSR_RXOVR_BIT) 815 port->icount.overrun++; 816 817 sts &= port->read_status_mask; 818 if (sts & MAX310X_LSR_RXBRK_BIT) 819 flag = TTY_BREAK; 820 else if (sts & MAX310X_LSR_RXPAR_BIT) 821 flag = TTY_PARITY; 822 else if (sts & MAX310X_LSR_FRERR_BIT) 823 flag = TTY_FRAME; 824 else if (sts & MAX310X_LSR_RXOVR_BIT) 825 flag = TTY_OVERRUN; 826 } 827 828 if (uart_handle_sysrq_char(port, ch)) 829 continue; 830 831 if (sts & port->ignore_status_mask) 832 continue; 833 834 uart_insert_char(port, sts, MAX310X_LSR_RXOVR_BIT, ch, flag); 835 } 836 } 837 838 tty_flip_buffer_push(&port->state->port); 839 } 840 841 static enum hrtimer_restart max310x_tmr_tx(struct hrtimer *timer) 842 { 843 struct max310x_one *one = container_of(timer, struct max310x_one, 844 tx_delay_tmr); 845 846 guard(spinlock_irqsave)(&one->port.lock); 847 if (!one->cancel_tx_delay_tmr) { 848 if (READ_ONCE(one->tx_state) == MAX310X_TX_WAIT_AFTER_SEND) { 849 /* After-send hold elapsed: drop RTS via the rts worker. */ 850 WRITE_ONCE(one->tx_state, MAX310X_TX_OFF); 851 schedule_work(&one->rts_work); 852 } else { 853 WRITE_ONCE(one->tx_state, MAX310X_TX_SEND); 854 schedule_work(&one->tx_work); 855 } 856 } 857 858 return HRTIMER_NORESTART; 859 } 860 861 static void max310x_delayed_stop_tx(struct uart_port *port) 862 { 863 struct max310x_one *one = to_max310x_port(port); 864 unsigned int txlvl; 865 866 if (READ_ONCE(one->tx_state) == MAX310X_TX_OFF) 867 return; 868 869 /* 870 * Data still queued for transmission defers the hold: a refill is 871 * coming and the envelope is not over. Stopped output does not 872 * count - it drains nowhere, and the envelope must end. 873 */ 874 if (!kfifo_is_empty(&port->state->port.xmit_fifo) && 875 !uart_tx_stopped(port)) 876 return; 877 878 /* 879 * The kfifo can be empty while the chip TX FIFO is still draining, so arm 880 * the after-send hold only once the chip FIFO is empty too - the TX-empty 881 * interrupt re-invokes us then. Otherwise the hold starts early and RTS 882 * drops mid-character, clipping the last byte(s). 883 */ 884 txlvl = max310x_port_read(port, MAX310X_TXFIFOLVL_REG); 885 if (txlvl) 886 return; 887 888 /* 889 * Runs from tx_work without port->lock, so re-check the state under it: 890 * shutdown() may have cancelled the envelope meanwhile. Only 891 * MAX310X_TX_SEND may arm the hold. 892 */ 893 guard(spinlock_irqsave)(&one->port.lock); 894 if (one->cancel_tx_delay_tmr || 895 READ_ONCE(one->tx_state) != MAX310X_TX_SEND) 896 return; 897 898 if (!hrtimer_active(&one->tx_delay_tmr)) { 899 /* 900 * Add one character for the byte still in the shift register - 901 * TX-empty fires as it enters, not as it leaves. 902 */ 903 ktime_t delay = us_to_ktime(one->char_time_us + 904 port->rs485.delay_rts_after_send * 905 USEC_PER_MSEC); 906 907 WRITE_ONCE(one->tx_state, MAX310X_TX_WAIT_AFTER_SEND); 908 hrtimer_start(&one->tx_delay_tmr, delay, HRTIMER_MODE_REL); 909 } 910 } 911 912 static void max310x_handle_tx(struct uart_port *port) 913 { 914 struct tty_port *tport = &port->state->port; 915 916 if (unlikely(port->x_char)) { 917 max310x_port_write(port, MAX310X_THR_REG, port->x_char); 918 port->icount.tx++; 919 port->x_char = 0; 920 return; 921 } 922 923 if (kfifo_is_empty(&tport->xmit_fifo) || uart_tx_stopped(port)) { 924 /* 925 * Unwind the envelope even if a reconfigure has moved the 926 * port to the hardware path mid-envelope: a stranded 927 * tx_state would make a later reconfigure believe a 928 * software envelope still owns RTS. 929 */ 930 max310x_delayed_stop_tx(port); 931 return; 932 } 933 934 /* 935 * It's a circ buffer -- wrap around. 936 * We could do that in one SPI transaction, but meh. 937 */ 938 while (!kfifo_is_empty(&tport->xmit_fifo)) { 939 unsigned int txlen, to_send; 940 unsigned char *tail; 941 942 /* Limit to space available in TX FIFO */ 943 txlen = max310x_port_read(port, MAX310X_TXFIFOLVL_REG); 944 txlen = port->fifosize - txlen; 945 if (!txlen) 946 break; 947 948 to_send = kfifo_out_linear_ptr(&tport->xmit_fifo, &tail, txlen); 949 max310x_batch_write(port, tail, to_send); 950 uart_xmit_advance(port, to_send); 951 } 952 953 if (kfifo_len(&tport->xmit_fifo) < WAKEUP_CHARS) 954 uart_write_wakeup(port); 955 } 956 957 /* 958 * Begin a software-timed RTS envelope: set the before-send phase and queue the 959 * rts worker to assert RTS. tx_state is set synchronously here (start_tx() holds 960 * port.lock) so close()/shutdown can see an envelope is in flight; rts_work then 961 * asserts RTS and arms the before-send timer (see there). 962 */ 963 static void max310x_delayed_start_tx(struct uart_port *port) 964 { 965 struct max310x_one *one = to_max310x_port(port); 966 967 WRITE_ONCE(one->tx_state, MAX310X_TX_WAIT_BEFORE_SEND); 968 one->cancel_tx_delay_tmr = false; 969 schedule_work(&one->rts_work); 970 } 971 972 /* called with port.lock taken and irqs off */ 973 static void max310x_start_tx(struct uart_port *port) 974 { 975 struct max310x_one *one = to_max310x_port(port); 976 977 /* A teardown is in progress; nothing may start an envelope or TX. */ 978 if (one->tx_teardown) 979 return; 980 981 /* 982 * An RS485 reconfigure is scheduled but not applied yet: transmitting 983 * now would use the old path against half-programmed registers - a 984 * TIOCSRS485 switching paths followed immediately by a write puts the 985 * data on the wire with the transceiver released. Leave the data in 986 * the kfifo; rs_work restarts TX once the configuration is applied. 987 */ 988 if (one->rs485_pending) 989 return; 990 991 if (READ_ONCE(one->sw_rts_during_tx)) { 992 /* 993 * The before- and after-send phases share one delay timer. If an 994 * after-send release is pending, cancel it before starting a new 995 * TX so the just-asserted RTS is not yanked; re-arming the timer 996 * for the before-send phase then supersedes the release. 997 */ 998 int res = 0; 999 1000 if (READ_ONCE(one->tx_state) == MAX310X_TX_WAIT_AFTER_SEND) 1001 res = hrtimer_try_to_cancel(&one->tx_delay_tmr); 1002 if (unlikely(res == -1)) { 1003 one->cancel_tx_delay_tmr = true; 1004 uart_port_unlock(port); 1005 hrtimer_cancel(&one->tx_delay_tmr); 1006 uart_port_lock(port); 1007 /* 1008 * The lock was dropped: a teardown may have run to 1009 * completion or a reconfigure may have been posted 1010 * meanwhile. Re-check before starting. 1011 */ 1012 if (one->tx_teardown || one->rs485_pending) 1013 return; 1014 } 1015 1016 /* Already sending: pump the new data, don't rewind. */ 1017 if (READ_ONCE(one->tx_state) == MAX310X_TX_SEND) { 1018 schedule_work(&one->tx_work); 1019 return; 1020 } 1021 1022 max310x_delayed_start_tx(port); 1023 } else { 1024 schedule_work(&one->tx_work); 1025 } 1026 } 1027 1028 static irqreturn_t max310x_port_irq(struct max310x_port *s, int portno) 1029 { 1030 struct uart_port *port = &s->p[portno].port; 1031 irqreturn_t res = IRQ_NONE; 1032 1033 do { 1034 unsigned int ists, lsr, rxlen; 1035 1036 /* Read IRQ status & RX FIFO level */ 1037 ists = max310x_port_read(port, MAX310X_IRQSTS_REG); 1038 rxlen = max310x_port_read(port, MAX310X_RXFIFOLVL_REG); 1039 if (!ists && !rxlen) 1040 break; 1041 1042 res = IRQ_HANDLED; 1043 1044 if (ists & MAX310X_IRQ_CTS_BIT) { 1045 lsr = max310x_port_read(port, MAX310X_LSR_IRQSTS_REG); 1046 uart_handle_cts_change(port, lsr & MAX310X_LSR_CTS_BIT); 1047 } 1048 if (rxlen) 1049 max310x_handle_rx(port, rxlen); 1050 /* 1051 * TXEMPTY latches on the FIFO becoming empty, so a stale 1052 * interrupt cannot pump data during an RTS setup delay. 1053 */ 1054 if (ists & MAX310X_IRQ_TXEMPTY_BIT) 1055 schedule_work(&s->p[portno].tx_work); 1056 } while (1); 1057 1058 return res; 1059 } 1060 1061 static irqreturn_t max310x_ist(int irq, void *dev_id) 1062 { 1063 struct max310x_port *s = (struct max310x_port *)dev_id; 1064 bool handled = false; 1065 1066 if (s->devtype->nr > 1) { 1067 bool done; 1068 1069 do { 1070 unsigned int val = ~0; 1071 unsigned long irq; 1072 unsigned int port; 1073 1074 done = true; 1075 1076 WARN_ON_ONCE(regmap_read(s->regmap, 1077 MAX310X_GLOBALIRQ_REG, &val)); 1078 1079 irq = val; 1080 1081 for_each_clear_bit(port, &irq, s->devtype->nr) { 1082 done = false; 1083 1084 if (max310x_port_irq(s, port) == IRQ_HANDLED) 1085 handled = true; 1086 } 1087 1088 } while (!done); 1089 } else { 1090 if (max310x_port_irq(s, 0) == IRQ_HANDLED) 1091 handled = true; 1092 } 1093 1094 return IRQ_RETVAL(handled); 1095 } 1096 1097 static void max310x_tx_proc(struct work_struct *ws) 1098 { 1099 struct max310x_one *one = container_of(ws, struct max310x_one, tx_work); 1100 1101 max310x_handle_tx(&one->port); 1102 } 1103 1104 static unsigned int max310x_tx_empty(struct uart_port *port) 1105 { 1106 u8 lvl = max310x_port_read(port, MAX310X_TXFIFOLVL_REG); 1107 1108 return lvl ? 0 : TIOCSER_TEMT; 1109 } 1110 1111 static unsigned int max310x_get_mctrl(struct uart_port *port) 1112 { 1113 /* 1114 * DCD and DSR are not wired and CTS/RTS is handled automatically 1115 * so just indicate DSR and CAR asserted 1116 */ 1117 return TIOCM_DSR | TIOCM_CAR; 1118 } 1119 1120 static void max310x_md_proc(struct work_struct *ws) 1121 { 1122 struct max310x_one *one = container_of(ws, struct max310x_one, md_work); 1123 1124 max310x_port_update(&one->port, MAX310X_MODE2_REG, 1125 MAX310X_MODE2_LOOPBACK_BIT, 1126 (one->port.mctrl & TIOCM_LOOP) ? 1127 MAX310X_MODE2_LOOPBACK_BIT : 0); 1128 } 1129 1130 static void max310x_set_mctrl(struct uart_port *port, unsigned int mctrl) 1131 { 1132 struct max310x_one *one = to_max310x_port(port); 1133 1134 schedule_work(&one->md_work); 1135 } 1136 1137 /* 1138 * A reconfigure can move the port off the hardware RTS path while the chip's 1139 * auto-RTS still owns a transmission in flight; tx_state does not track 1140 * those. Take such a transfer over before auto-RTS is disabled: enter 1141 * MAX310X_TX_SEND, so the caller keeps RTS driven across the handover and 1142 * the normal drain path arms the after-send hold. Returns true when a 1143 * transmission owns RTS - adopted here or started concurrently - and false 1144 * when the line is really idle. 1145 */ 1146 static bool max310x_adopt_hw_tx(struct max310x_one *one) 1147 { 1148 struct uart_port *port = &one->port; 1149 1150 if (!max310x_port_read(port, MAX310X_TXFIFOLVL_REG) && 1151 kfifo_is_empty(&port->state->port.xmit_fifo)) 1152 return false; 1153 1154 scoped_guard(spinlock_irqsave, &port->lock) { 1155 if (one->tx_teardown) 1156 return false; 1157 if (READ_ONCE(one->tx_state) != MAX310X_TX_OFF) 1158 return true; 1159 WRITE_ONCE(one->tx_state, MAX310X_TX_SEND); 1160 one->cancel_tx_delay_tmr = false; 1161 } 1162 1163 return true; 1164 } 1165 1166 /* 1167 * Pick hardware or software RTS timing for the current port. The chip can 1168 * deliver up to 15 bit-times of setup/hold delay via HDPIXDELAY; anything 1169 * longer (or any RTS polarity the chip cannot produce automatically) must 1170 * be driven by software via tx_delay_tmr and rts_work. 1171 */ 1172 static void max310x_set_rts_ctl_params(struct max310x_one *one) 1173 { 1174 const unsigned int max_bit_dly = 15; 1175 struct uart_port *port = &one->port; 1176 unsigned int setup = 0, hold = 0; 1177 u8 mode1 = 0, irda = 0; 1178 bool sw_rts = false; 1179 1180 if (!(port->rs485.flags & SER_RS485_ENABLED)) 1181 goto out; 1182 1183 /* Convert milliseconds to bit-times, rounding up. */ 1184 setup = DIV_ROUND_UP(one->baud * port->rs485.delay_rts_before_send, 1185 MSEC_PER_SEC); 1186 hold = DIV_ROUND_UP(one->baud * port->rs485.delay_rts_after_send, 1187 MSEC_PER_SEC); 1188 1189 /* 1190 * Compare in bit-times: a delay of exactly max_bit_dly bit-times 1191 * must stay on the hardware path, which a nanosecond ceiling 1192 * computed from the truncated per-bit time would reject. Without 1193 * a baud rate the conversion is meaningless - use software. 1194 */ 1195 if (!one->baud || setup > max_bit_dly || hold > max_bit_dly || 1196 !!(port->rs485.flags & SER_RS485_RTS_ON_SEND) == 1197 !!(port->rs485.flags & SER_RS485_RTS_AFTER_SEND)) { 1198 sw_rts = true; 1199 setup = 0; 1200 hold = 0; 1201 } 1202 1203 out: 1204 /* Assign once; a transient false would be seen by other readers. */ 1205 WRITE_ONCE(one->sw_rts_during_tx, sw_rts); 1206 1207 max310x_port_write(port, MAX310X_HDPIXDELAY_REG, 1208 MAX310X_HDPIXDELAY_SETUP(setup) | 1209 MAX310X_HDPIXDELAY_HOLD(hold)); 1210 1211 /* 1212 * A break owns the transceiver: break_ctl() disabled auto-RTS and 1213 * drives RTS manually, and restores it from the current 1214 * configuration when the break ends. Touching them here would 1215 * release the transceiver mid-break. 1216 */ 1217 if (one->tx_break) 1218 return; 1219 1220 if (port->rs485.flags & SER_RS485_ENABLED) { 1221 if (sw_rts) { 1222 /* 1223 * Only settle RTS at idle when no transmission owns it. 1224 * A reconfigure while one is in flight - rs_work runs 1225 * on every TIOCSRS485 - would otherwise release the 1226 * transceiver mid-character. A transfer the hardware 1227 * path owns is invisible to tx_state - adopt it 1228 * instead of settling. The reverse strand also 1229 * exists: an envelope the hardware path inherited 1230 * mid-envelope leaves tx_state set with nothing 1231 * driving the pin. 1232 */ 1233 if (READ_ONCE(one->tx_state) != MAX310X_TX_OFF || 1234 max310x_adopt_hw_tx(one)) { 1235 /* 1236 * The transfer may be running under auto-RTS: 1237 * assert before auto-RTS is disabled below so 1238 * the pin is never released under it, and arm 1239 * the after-send hold - with the chip FIFO 1240 * already drained no TX-empty interrupt 1241 * arrives to arm it. 1242 */ 1243 max310x_rts_ctl(port, 1244 max310x_rts_level(port, true)); 1245 max310x_delayed_stop_tx(port); 1246 } else { 1247 max310x_rts_ctl(port, 1248 max310x_rts_level(port, false)); 1249 /* 1250 * serial_core calls set_termios() without 1251 * port->lock, so an envelope may have started 1252 * while the idle level was written and the 1253 * settle can land after its RTS assert. 1254 * rts_work re-derives the level from 1255 * tx_state; requeue it to converge. 1256 */ 1257 if (READ_ONCE(one->tx_state) != MAX310X_TX_OFF) 1258 schedule_work(&one->rts_work); 1259 } 1260 } else { 1261 mode1 = MAX310X_MODE1_TRNSCVCTRL_BIT; 1262 /* 1263 * The auto-RTS engine asserts RTS high on send; for an 1264 * active-low RTS let IRDA.RTSINVERT invert the output 1265 * stage. 1266 */ 1267 if (!(port->rs485.flags & SER_RS485_RTS_ON_SEND)) 1268 irda = MAX310X_IRDA_RTSINVERT_BIT; 1269 } 1270 } else { 1271 max310x_rts_ctl(port, 0); 1272 } 1273 1274 max310x_port_update(port, MAX310X_MODE1_REG, 1275 MAX310X_MODE1_TRNSCVCTRL_BIT, mode1); 1276 max310x_port_update(port, MAX310X_IRDA_REG, 1277 MAX310X_IRDA_RTSINVERT_BIT, irda); 1278 } 1279 1280 static void max310x_break_ctl(struct uart_port *port, int break_state) 1281 { 1282 struct max310x_one *one = to_max310x_port(port); 1283 1284 one->tx_break = break_state; 1285 1286 max310x_port_update(port, MAX310X_LCR_REG, 1287 MAX310X_LCR_TXBREAK_BIT, 1288 break_state ? MAX310X_LCR_TXBREAK_BIT : 0); 1289 1290 /* 1291 * The chip's auto-RTS asserts the transceiver only while FIFO data is 1292 * shifting out, and a break is not FIFO data. Disable auto-RTS for the 1293 * break duration and drive RTS manually so the break reaches the wire; 1294 * restore auto-RTS when the break ends. For an active-low RTS, 1295 * IRDA.RTSINVERT already inverts the RTS_ output stage, so break_state 1296 * is driven as it is. 1297 */ 1298 if (break_state) { 1299 if (!(port->rs485.flags & SER_RS485_ENABLED)) 1300 return; 1301 if (READ_ONCE(one->sw_rts_during_tx)) { 1302 max310x_rts_ctl(port, max310x_rts_level(port, 1)); 1303 } else { 1304 max310x_port_update(port, MAX310X_MODE1_REG, 1305 MAX310X_MODE1_TRNSCVCTRL_BIT, 0); 1306 max310x_rts_ctl(port, 1); 1307 } 1308 } else { 1309 /* 1310 * Reapply the current configuration: a reconfigure that 1311 * arrived during the break was deferred by the tx_break 1312 * guard, and RS485 may have been disabled outright - the 1313 * manually driven RTS must not leak past the break. On the 1314 * software path the helper also settles the idle level; on 1315 * the hardware path release the manual RTS - auto-RTS owns 1316 * the pin again. 1317 */ 1318 max310x_set_rts_ctl_params(one); 1319 if (!READ_ONCE(one->sw_rts_during_tx)) 1320 max310x_rts_ctl(port, 0); 1321 } 1322 } 1323 1324 static void max310x_set_termios(struct uart_port *port, 1325 struct ktermios *termios, 1326 const struct ktermios *old) 1327 { 1328 unsigned int frame_bits = tty_get_frame_size(termios->c_cflag); 1329 unsigned int lcr = 0, flow = 0; 1330 int baud; 1331 1332 /* Mask termios capabilities we don't support */ 1333 termios->c_cflag &= ~CMSPAR; 1334 1335 /* Word size */ 1336 switch (termios->c_cflag & CSIZE) { 1337 case CS5: 1338 break; 1339 case CS6: 1340 lcr = MAX310X_LCR_LENGTH0_BIT; 1341 break; 1342 case CS7: 1343 lcr = MAX310X_LCR_LENGTH1_BIT; 1344 break; 1345 case CS8: 1346 default: 1347 lcr = MAX310X_LCR_LENGTH1_BIT | MAX310X_LCR_LENGTH0_BIT; 1348 break; 1349 } 1350 1351 /* Parity */ 1352 if (termios->c_cflag & PARENB) { 1353 lcr |= MAX310X_LCR_PARITY_BIT; 1354 if (!(termios->c_cflag & PARODD)) 1355 lcr |= MAX310X_LCR_EVENPARITY_BIT; 1356 } 1357 1358 /* Stop bits */ 1359 if (termios->c_cflag & CSTOPB) 1360 lcr |= MAX310X_LCR_STOPLEN_BIT; /* 2 stops */ 1361 1362 /* 1363 * Update LCR register. Leave the TX break and RTS bits alone: they are 1364 * driven by break_ctl() and by the software-timed RS485 RTS, and a 1365 * whole-register write here would end a break in progress or release 1366 * the transceiver mid-character. 1367 */ 1368 max310x_port_update(port, MAX310X_LCR_REG, MAX310X_LCR_TERMIOS_MASK, lcr); 1369 1370 /* Set read status mask */ 1371 port->read_status_mask = MAX310X_LSR_RXOVR_BIT; 1372 if (termios->c_iflag & INPCK) 1373 port->read_status_mask |= MAX310X_LSR_RXPAR_BIT | 1374 MAX310X_LSR_FRERR_BIT; 1375 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK)) 1376 port->read_status_mask |= MAX310X_LSR_RXBRK_BIT; 1377 1378 /* Set status ignore mask */ 1379 port->ignore_status_mask = 0; 1380 if (termios->c_iflag & IGNBRK) 1381 port->ignore_status_mask |= MAX310X_LSR_RXBRK_BIT; 1382 if (!(termios->c_cflag & CREAD)) 1383 port->ignore_status_mask |= MAX310X_LSR_RXPAR_BIT | 1384 MAX310X_LSR_RXOVR_BIT | 1385 MAX310X_LSR_FRERR_BIT | 1386 MAX310X_LSR_RXBRK_BIT; 1387 1388 /* Configure flow control */ 1389 max310x_port_write(port, MAX310X_XON1_REG, termios->c_cc[VSTART]); 1390 max310x_port_write(port, MAX310X_XOFF1_REG, termios->c_cc[VSTOP]); 1391 1392 /* 1393 * Disable transmitter before enabling AutoCTS or auto transmitter 1394 * flow control 1395 */ 1396 if (termios->c_cflag & CRTSCTS || termios->c_iflag & IXOFF) { 1397 max310x_port_update(port, MAX310X_MODE1_REG, 1398 MAX310X_MODE1_TXDIS_BIT, 1399 MAX310X_MODE1_TXDIS_BIT); 1400 } 1401 1402 port->status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS | UPSTAT_AUTOXOFF); 1403 1404 if (termios->c_cflag & CRTSCTS) { 1405 /* Enable AUTORTS and AUTOCTS */ 1406 port->status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS; 1407 flow |= MAX310X_FLOWCTRL_AUTOCTS_BIT | 1408 MAX310X_FLOWCTRL_AUTORTS_BIT; 1409 } 1410 if (termios->c_iflag & IXON) 1411 flow |= MAX310X_FLOWCTRL_SWFLOW3_BIT | 1412 MAX310X_FLOWCTRL_SWFLOWEN_BIT; 1413 if (termios->c_iflag & IXOFF) { 1414 port->status |= UPSTAT_AUTOXOFF; 1415 flow |= MAX310X_FLOWCTRL_SWFLOW1_BIT | 1416 MAX310X_FLOWCTRL_SWFLOWEN_BIT; 1417 } 1418 max310x_port_write(port, MAX310X_FLOWCTRL_REG, flow); 1419 1420 /* 1421 * Enable transmitter after disabling AutoCTS and auto transmitter 1422 * flow control 1423 */ 1424 if (!(termios->c_cflag & CRTSCTS) && !(termios->c_iflag & IXOFF)) { 1425 max310x_port_update(port, MAX310X_MODE1_REG, 1426 MAX310X_MODE1_TXDIS_BIT, 1427 0); 1428 } 1429 1430 /* Get baud rate generator configuration */ 1431 baud = uart_get_baud_rate(port, termios, old, 1432 port->uartclk / 16 / 0xffff, 1433 port->uartclk / 4); 1434 1435 /* Setup baudrate generator */ 1436 baud = max310x_set_baud(port, baud); 1437 1438 /* Update timeout according to new baud rate */ 1439 uart_update_timeout(port, termios->c_cflag, baud); 1440 1441 /* 1442 * Cache the new baud rate and the time it takes to clock out one 1443 * character before reprogramming the RS485 RTS delays: the 1444 * millisecond-to-bit-time conversion divides by the baud rate, and 1445 * taking over an in-flight transfer arms the after-send hold from 1446 * the character time. 1447 */ 1448 to_max310x_port(port)->baud = baud; 1449 to_max310x_port(port)->char_time_us = 1450 DIV_ROUND_UP(USEC_PER_SEC * frame_bits, baud); 1451 max310x_set_rts_ctl_params(to_max310x_port(port)); 1452 } 1453 1454 static void max310x_rs_proc(struct work_struct *ws) 1455 { 1456 struct max310x_one *one = container_of(ws, struct max310x_one, rs_work); 1457 unsigned int mode2 = 0; 1458 unsigned long flags; 1459 bool chip_tx_empty; 1460 1461 /* 1462 * Serialize against break_ctl() and set_termios(), which run under 1463 * port->mutex: the tx_break-guarded register writes must not 1464 * straddle a break starting or ending. 1465 */ 1466 guard(mutex)(&one->port.state->port.mutex); 1467 1468 /* Flush an rts_work that read tx_state before the disable cleared it. */ 1469 if (!(one->port.rs485.flags & SER_RS485_ENABLED)) 1470 cancel_work_sync(&one->rts_work); 1471 1472 max310x_set_rts_ctl_params(one); 1473 1474 if (one->port.rs485.flags & SER_RS485_ENABLED && 1475 !(one->port.rs485.flags & SER_RS485_RX_DURING_TX)) 1476 mode2 = MAX310X_MODE2_ECHOSUPR_BIT; 1477 1478 max310x_port_update(&one->port, MAX310X_MODE2_REG, 1479 MAX310X_MODE2_ECHOSUPR_BIT, mode2); 1480 1481 /* 1482 * The configuration is applied: release any TX that start_tx() 1483 * deferred while the reconfigure was pending, now on the right 1484 * path. start_tx() can drop and retake the lock through the 1485 * uart_port API - a raw spinlock guard here would unbalance it. 1486 */ 1487 chip_tx_empty = !max310x_port_read(&one->port, 1488 MAX310X_TXFIFOLVL_REG); 1489 1490 uart_port_lock_irqsave(&one->port, &flags); 1491 one->rs485_pending = false; 1492 if (one->port.x_char || 1493 !kfifo_is_empty(&one->port.state->port.xmit_fifo)) { 1494 /* 1495 * A send state adopted without chip data is the deferred 1496 * write itself: restart it as a fresh envelope so the 1497 * before-send delay is honoured. 1498 */ 1499 if (chip_tx_empty && 1500 READ_ONCE(one->tx_state) == MAX310X_TX_SEND) 1501 WRITE_ONCE(one->tx_state, MAX310X_TX_OFF); 1502 max310x_start_tx(&one->port); 1503 } 1504 uart_port_unlock_irqrestore(&one->port, flags); 1505 } 1506 1507 /* called with port.lock taken and irqs off */ 1508 static int max310x_rs485_config(struct uart_port *port, struct ktermios *termios, 1509 struct serial_rs485 *rs485) 1510 { 1511 struct max310x_one *one = to_max310x_port(port); 1512 1513 rs485->delay_rts_before_send = min(rs485->delay_rts_before_send, 100U); 1514 rs485->delay_rts_after_send = min(rs485->delay_rts_after_send, 100U); 1515 1516 /* 1517 * Make sure no SW-timed RTS toggle survives an RS485 disable, even 1518 * if the delay timer happens to be running right now. 1519 */ 1520 if (!(rs485->flags & SER_RS485_ENABLED)) { 1521 one->tx_teardown = true; 1522 one->cancel_tx_delay_tmr = true; 1523 if (hrtimer_try_to_cancel(&one->tx_delay_tmr) == -1) { 1524 uart_port_unlock(port); 1525 hrtimer_cancel(&one->tx_delay_tmr); 1526 uart_port_lock(port); 1527 } 1528 WRITE_ONCE(one->tx_state, MAX310X_TX_OFF); 1529 one->tx_teardown = false; 1530 } 1531 1532 port->rs485 = *rs485; 1533 1534 one->rs485_pending = true; 1535 schedule_work(&one->rs_work); 1536 1537 return 0; 1538 } 1539 1540 static int max310x_startup(struct uart_port *port) 1541 { 1542 struct max310x_one *one = to_max310x_port(port); 1543 unsigned int val; 1544 1545 max310x_power(port, 1); 1546 1547 /* 1548 * Clear a latched break: nothing clears TXBREAK when a port is 1549 * closed with a break still asserted, and set_termios() no longer 1550 * rewrites it. 1551 */ 1552 one->tx_break = false; 1553 max310x_port_update(port, MAX310X_LCR_REG, MAX310X_LCR_TXBREAK_BIT, 0); 1554 1555 /* Configure MODE1: re-enable the transmitter shutdown() stopped */ 1556 max310x_port_update(port, MAX310X_MODE1_REG, 1557 MAX310X_MODE1_TRNSCVCTRL_BIT | 1558 MAX310X_MODE1_TXDIS_BIT, 0); 1559 1560 /* Configure MODE2 register & Reset FIFOs*/ 1561 val = MAX310X_MODE2_RXEMPTINV_BIT | MAX310X_MODE2_FIFORST_BIT; 1562 max310x_port_write(port, MAX310X_MODE2_REG, val); 1563 max310x_port_update(port, MAX310X_MODE2_REG, 1564 MAX310X_MODE2_FIFORST_BIT, 0); 1565 1566 one->tx_teardown = false; 1567 1568 /* 1569 * Configure the RS485 RTS timing (HW auto-RTS vs software-driven) and 1570 * the RS485/RS232 mode bits. Don't hardcode HW auto-RTS here - let 1571 * max310x_set_rts_ctl_params() pick HW or SW per the configured 1572 * delays, otherwise the chip's auto-RTS would override the software 1573 * RTS hold and the after-send delay is lost. 1574 */ 1575 max310x_set_rts_ctl_params(one); 1576 1577 if (port->rs485.flags & SER_RS485_ENABLED && 1578 !(port->rs485.flags & SER_RS485_RX_DURING_TX)) 1579 max310x_port_update(port, MAX310X_MODE2_REG, 1580 MAX310X_MODE2_ECHOSUPR_BIT, 1581 MAX310X_MODE2_ECHOSUPR_BIT); 1582 1583 /* 1584 * Configure flow control levels: 1585 * resume: 48 1586 * halt: 96 1587 */ 1588 max310x_port_write(port, MAX310X_FLOWLVL_REG, 1589 MAX310X_FLOWLVL_RES(48) | MAX310X_FLOWLVL_HALT(96)); 1590 1591 /* Clear IRQ status register */ 1592 max310x_port_read(port, MAX310X_IRQSTS_REG); 1593 1594 /* Enable RX, TX, CTS change interrupts */ 1595 val = MAX310X_IRQ_RXEMPTY_BIT | MAX310X_IRQ_TXEMPTY_BIT; 1596 max310x_port_write(port, MAX310X_IRQEN_REG, val | MAX310X_IRQ_CTS_BIT); 1597 1598 return 0; 1599 } 1600 1601 static void max310x_shutdown(struct uart_port *port) 1602 { 1603 struct max310x_one *one = to_max310x_port(port); 1604 1605 /* 1606 * Gate new transmissions and interrupts first: a concurrent 1607 * start_tx() either sees the interlock or happens-before it, and 1608 * nothing below may start a new envelope. The timer and the works 1609 * stay live for now - they are what completes a running after-send 1610 * hold. 1611 */ 1612 scoped_guard(spinlock_irqsave, &port->lock) 1613 one->tx_teardown = true; 1614 max310x_port_write(port, MAX310X_IRQEN_REG, 0); 1615 1616 /* 1617 * Stop the transmitter: the character in flight completes, data 1618 * still queued is abandoned (the next startup() resets the FIFOs). 1619 */ 1620 max310x_port_update(port, MAX310X_MODE1_REG, 1621 MAX310X_MODE1_TXDIS_BIT, MAX310X_MODE1_TXDIS_BIT); 1622 1623 if (READ_ONCE(one->sw_rts_during_tx)) { 1624 /* 1625 * Honour a running after-send hold before the port is 1626 * powered down. The bound covers the final character - 1627 * tx_empty() cannot see the transmit shift register - plus 1628 * the hold itself. Data beyond that is abandoned: it is 1629 * only still queued when the tty layer was told not to 1630 * wait, and the release below drives RTS to idle 1631 * regardless. 1632 */ 1633 unsigned int tries = port->rs485.delay_rts_after_send + 1634 2 * DIV_ROUND_UP(one->char_time_us, USEC_PER_MSEC); 1635 1636 while (READ_ONCE(one->tx_state) != MAX310X_TX_OFF && tries-- > 0) 1637 fsleep(USEC_PER_MSEC); 1638 } else { 1639 /* 1640 * Let the character in flight finish (the chip has no 1641 * transmitter-idle status), then empty the FIFO: auto-RTS 1642 * releases only once the transmitter is empty, and the 1643 * power-off below would freeze an asserted pin until the 1644 * next open. The hold is at most 15 bit-times. 1645 */ 1646 fsleep(one->char_time_us); 1647 max310x_port_update(port, MAX310X_MODE2_REG, 1648 MAX310X_MODE2_FIFORST_BIT, 1649 MAX310X_MODE2_FIFORST_BIT); 1650 max310x_port_update(port, MAX310X_MODE2_REG, 1651 MAX310X_MODE2_FIFORST_BIT, 0); 1652 if (port->rs485.flags & SER_RS485_ENABLED && one->baud) { 1653 unsigned int hold = DIV_ROUND_UP(one->baud * 1654 port->rs485.delay_rts_after_send, 1655 MSEC_PER_SEC); 1656 1657 fsleep(max(DIV_ROUND_UP((hold + 1) * USEC_PER_SEC, 1658 one->baud), 100U)); 1659 } 1660 } 1661 1662 /* 1663 * The wind-down is over: cancel unconditionally. The SW/HW decision 1664 * is recomputed on every reconfigure, so a TIOCSRS485 can clear 1665 * sw_rts_during_tx while an envelope is still in flight, and 1666 * neither the timer nor the works may outlive the port. 1667 */ 1668 scoped_guard(spinlock_irqsave, &port->lock) 1669 one->cancel_tx_delay_tmr = true; 1670 cancel_work_sync(&one->tx_work); 1671 hrtimer_cancel(&one->tx_delay_tmr); 1672 cancel_work_sync(&one->rts_work); 1673 WRITE_ONCE(one->tx_state, MAX310X_TX_OFF); 1674 1675 if (READ_ONCE(one->sw_rts_during_tx)) { 1676 max310x_rts_ctl(port, max310x_rts_level(port, false)); 1677 /* 1678 * The power-off below stops the UART clock that moves the 1679 * RTS pin: released too late, the write reaches the register 1680 * but the pin stays at its old level until the next startup 1681 * restarts the clock. Give the release one bit time (at 1682 * least 100 us) to reach the pin - the measured propagation 1683 * is about one tick of the 16x oversampling clock. 1684 */ 1685 fsleep(max(one->char_time_us / 10, 100U)); 1686 } 1687 1688 max310x_power(port, 0); 1689 } 1690 1691 static const char *max310x_type(struct uart_port *port) 1692 { 1693 struct max310x_port *s = dev_get_drvdata(port->dev); 1694 1695 return (port->type == PORT_MAX310X) ? s->devtype->name : NULL; 1696 } 1697 1698 static int max310x_request_port(struct uart_port *port) 1699 { 1700 /* Do nothing */ 1701 return 0; 1702 } 1703 1704 static void max310x_config_port(struct uart_port *port, int flags) 1705 { 1706 if (flags & UART_CONFIG_TYPE) 1707 port->type = PORT_MAX310X; 1708 } 1709 1710 static int max310x_verify_port(struct uart_port *port, struct serial_struct *s) 1711 { 1712 if ((s->type != PORT_UNKNOWN) && (s->type != PORT_MAX310X)) 1713 return -EINVAL; 1714 if (s->irq != port->irq) 1715 return -EINVAL; 1716 1717 return 0; 1718 } 1719 1720 static void max310x_null_void(struct uart_port *port) 1721 { 1722 /* Do nothing */ 1723 } 1724 1725 static const struct uart_ops max310x_ops = { 1726 .tx_empty = max310x_tx_empty, 1727 .set_mctrl = max310x_set_mctrl, 1728 .get_mctrl = max310x_get_mctrl, 1729 .stop_tx = max310x_null_void, 1730 .start_tx = max310x_start_tx, 1731 .stop_rx = max310x_null_void, 1732 .break_ctl = max310x_break_ctl, 1733 .startup = max310x_startup, 1734 .shutdown = max310x_shutdown, 1735 .set_termios = max310x_set_termios, 1736 .type = max310x_type, 1737 .request_port = max310x_request_port, 1738 .release_port = max310x_null_void, 1739 .config_port = max310x_config_port, 1740 .verify_port = max310x_verify_port, 1741 }; 1742 1743 static int __maybe_unused max310x_suspend(struct device *dev) 1744 { 1745 struct max310x_port *s = dev_get_drvdata(dev); 1746 int i; 1747 1748 for (i = 0; i < s->devtype->nr; i++) { 1749 uart_suspend_port(&max310x_uart, &s->p[i].port); 1750 max310x_power(&s->p[i].port, 0); 1751 } 1752 1753 return 0; 1754 } 1755 1756 static int __maybe_unused max310x_resume(struct device *dev) 1757 { 1758 struct max310x_port *s = dev_get_drvdata(dev); 1759 int i; 1760 1761 for (i = 0; i < s->devtype->nr; i++) { 1762 max310x_power(&s->p[i].port, 1); 1763 uart_resume_port(&max310x_uart, &s->p[i].port); 1764 } 1765 1766 return 0; 1767 } 1768 1769 static SIMPLE_DEV_PM_OPS(max310x_pm_ops, max310x_suspend, max310x_resume); 1770 1771 #ifdef CONFIG_GPIOLIB 1772 static int max310x_gpio_get(struct gpio_chip *chip, unsigned int offset) 1773 { 1774 unsigned int val; 1775 struct max310x_port *s = gpiochip_get_data(chip); 1776 struct uart_port *port = &s->p[offset / 4].port; 1777 1778 val = max310x_port_read(port, MAX310X_GPIODATA_REG); 1779 1780 return !!((val >> 4) & (1 << (offset % 4))); 1781 } 1782 1783 static int max310x_gpio_set(struct gpio_chip *chip, unsigned int offset, 1784 int value) 1785 { 1786 struct max310x_port *s = gpiochip_get_data(chip); 1787 struct uart_port *port = &s->p[offset / 4].port; 1788 1789 max310x_port_update(port, MAX310X_GPIODATA_REG, 1 << (offset % 4), 1790 value ? 1 << (offset % 4) : 0); 1791 1792 return 0; 1793 } 1794 1795 static int max310x_gpio_get_direction(struct gpio_chip *chip, unsigned int offset) 1796 { 1797 struct max310x_port *s = gpiochip_get_data(chip); 1798 struct uart_port *port = &s->p[offset / 4].port; 1799 unsigned int val; 1800 1801 val = max310x_port_read(port, MAX310X_GPIOCFG_REG); 1802 1803 return val & BIT(offset % 4) ? GPIO_LINE_DIRECTION_OUT : GPIO_LINE_DIRECTION_IN; 1804 } 1805 1806 static int max310x_gpio_direction_input(struct gpio_chip *chip, unsigned int offset) 1807 { 1808 struct max310x_port *s = gpiochip_get_data(chip); 1809 struct uart_port *port = &s->p[offset / 4].port; 1810 1811 max310x_port_update(port, MAX310X_GPIOCFG_REG, 1 << (offset % 4), 0); 1812 1813 return 0; 1814 } 1815 1816 static int max310x_gpio_direction_output(struct gpio_chip *chip, 1817 unsigned int offset, int value) 1818 { 1819 struct max310x_port *s = gpiochip_get_data(chip); 1820 struct uart_port *port = &s->p[offset / 4].port; 1821 1822 max310x_port_update(port, MAX310X_GPIODATA_REG, 1 << (offset % 4), 1823 value ? 1 << (offset % 4) : 0); 1824 max310x_port_update(port, MAX310X_GPIOCFG_REG, 1 << (offset % 4), 1825 1 << (offset % 4)); 1826 1827 return 0; 1828 } 1829 1830 static int max310x_gpio_set_config(struct gpio_chip *chip, unsigned int offset, 1831 unsigned long config) 1832 { 1833 struct max310x_port *s = gpiochip_get_data(chip); 1834 struct uart_port *port = &s->p[offset / 4].port; 1835 1836 switch (pinconf_to_config_param(config)) { 1837 case PIN_CONFIG_DRIVE_OPEN_DRAIN: 1838 max310x_port_update(port, MAX310X_GPIOCFG_REG, 1839 1 << ((offset % 4) + 4), 1840 1 << ((offset % 4) + 4)); 1841 return 0; 1842 case PIN_CONFIG_DRIVE_PUSH_PULL: 1843 max310x_port_update(port, MAX310X_GPIOCFG_REG, 1844 1 << ((offset % 4) + 4), 0); 1845 return 0; 1846 default: 1847 return -ENOTSUPP; 1848 } 1849 } 1850 #endif 1851 1852 static const struct serial_rs485 max310x_rs485_supported = { 1853 .flags = SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND | 1854 SER_RS485_RTS_AFTER_SEND | SER_RS485_RX_DURING_TX, 1855 .delay_rts_before_send = 1, 1856 .delay_rts_after_send = 1, 1857 }; 1858 1859 static int max310x_probe(struct device *dev, const struct max310x_devtype *devtype, 1860 const struct max310x_if_cfg *if_cfg, 1861 struct regmap *regmaps[], int irq) 1862 { 1863 unsigned int fmin, fmax, freq; 1864 int i, ret; 1865 struct max310x_port *s; 1866 unsigned int uartclk = 0; 1867 bool xtal; 1868 1869 for (i = 0; i < devtype->nr; i++) 1870 if (IS_ERR(regmaps[i])) 1871 return PTR_ERR(regmaps[i]); 1872 1873 /* Alloc port structure */ 1874 s = devm_kzalloc(dev, struct_size(s, p, devtype->nr), GFP_KERNEL); 1875 if (!s) 1876 return -ENOMEM; 1877 1878 /* Always ask for fixed clock rate from a property. */ 1879 device_property_read_u32(dev, "clock-frequency", &uartclk); 1880 1881 xtal = device_property_match_string(dev, "clock-names", "osc") < 0; 1882 if (xtal) 1883 s->clk = devm_clk_get_optional(dev, "xtal"); 1884 else 1885 s->clk = devm_clk_get_optional(dev, "osc"); 1886 if (IS_ERR(s->clk)) 1887 return PTR_ERR(s->clk); 1888 1889 ret = clk_prepare_enable(s->clk); 1890 if (ret) 1891 return ret; 1892 1893 freq = clk_get_rate(s->clk); 1894 if (freq == 0) 1895 freq = uartclk; 1896 if (freq == 0) { 1897 ret = dev_err_probe(dev, -EINVAL, "Cannot get clock rate\n"); 1898 goto out_clk; 1899 } 1900 1901 if (xtal) { 1902 fmin = 1000000; 1903 fmax = 4000000; 1904 } else { 1905 fmin = 500000; 1906 fmax = 35000000; 1907 } 1908 1909 /* Check frequency limits */ 1910 if (freq < fmin || freq > fmax) { 1911 ret = -ERANGE; 1912 goto out_clk; 1913 } 1914 1915 s->regmap = regmaps[0]; 1916 s->devtype = devtype; 1917 s->if_cfg = if_cfg; 1918 dev_set_drvdata(dev, s); 1919 1920 /* Check device to ensure we are talking to what we expect */ 1921 ret = max310x_detect(dev); 1922 if (ret) 1923 goto out_clk; 1924 1925 for (i = 0; i < devtype->nr; i++) { 1926 unsigned int val; 1927 1928 /* Reset port */ 1929 regmap_write(regmaps[i], MAX310X_MODE2_REG, 1930 MAX310X_MODE2_RST_BIT); 1931 /* Clear port reset */ 1932 regmap_write(regmaps[i], MAX310X_MODE2_REG, 0); 1933 1934 /* Wait for port startup */ 1935 ret = regmap_read_poll_timeout(regmaps[i], MAX310X_BRGDIVLSB_REG, 1936 val, val == 0x01, 1937 MAX310X_PORT_STARTUP_SLEEP_US, 1938 MAX310X_PORT_STARTUP_TIMEOUT_US); 1939 if (ret) { 1940 ret = dev_err_probe(dev, -EAGAIN, "port reset failed\n"); 1941 goto out_uart; 1942 } 1943 1944 regmap_write(regmaps[i], MAX310X_MODE1_REG, devtype->mode1); 1945 } 1946 1947 ret = max310x_set_ref_clk(dev, s, freq, &uartclk, xtal); 1948 if (ret < 0) 1949 goto out_uart; 1950 1951 dev_dbg(dev, "Reference clock set to %i Hz\n", uartclk); 1952 1953 /* 1954 * Set up each port's state before registering the gpiochip, 1955 * since the gpiochip callbacks will read s->p[i].regmap as 1956 * soon as gpiolib exposes the controller. 1957 */ 1958 for (i = 0; i < devtype->nr; i++) { 1959 s->p[i].port.dev = dev; 1960 s->p[i].port.irq = irq; 1961 s->p[i].port.type = PORT_MAX310X; 1962 s->p[i].port.fifosize = MAX310X_FIFO_SIZE; 1963 s->p[i].port.flags = UPF_FIXED_TYPE | UPF_LOW_LATENCY; 1964 s->p[i].port.iotype = UPIO_BUS; 1965 s->p[i].port.uartclk = uartclk; 1966 s->p[i].port.rs485_config = max310x_rs485_config; 1967 s->p[i].port.rs485_supported = max310x_rs485_supported; 1968 s->p[i].port.ops = &max310x_ops; 1969 s->p[i].regmap = regmaps[i]; 1970 1971 /* Disable all interrupts */ 1972 max310x_port_write(&s->p[i].port, MAX310X_IRQEN_REG, 0); 1973 /* Clear IRQ status register */ 1974 max310x_port_read(&s->p[i].port, MAX310X_IRQSTS_REG); 1975 /* Initialize queue for start TX */ 1976 INIT_WORK(&s->p[i].tx_work, max310x_tx_proc); 1977 /* Initialize queue for changing LOOPBACK mode */ 1978 INIT_WORK(&s->p[i].md_work, max310x_md_proc); 1979 /* Initialize queue for changing RS485 mode */ 1980 INIT_WORK(&s->p[i].rs_work, max310x_rs_proc); 1981 /* Initialize queue for software-driven RTS toggling */ 1982 INIT_WORK(&s->p[i].rts_work, max310x_rts_work_proc); 1983 hrtimer_setup(&s->p[i].tx_delay_tmr, max310x_tmr_tx, 1984 CLOCK_MONOTONIC, HRTIMER_MODE_REL); 1985 s->p[i].tx_state = MAX310X_TX_OFF; 1986 } 1987 1988 #ifdef CONFIG_GPIOLIB 1989 /* 1990 * Register the GPIO controller before adding the UART ports so 1991 * that consumers referencing the chip's own GPIOs from device 1992 * tree (for example rs485-term-gpios = <&max310x ...>) can 1993 * resolve them at uart_add_one_port() time instead of receiving 1994 * -EPROBE_DEFER from their own provider. 1995 */ 1996 s->gpio.owner = THIS_MODULE; 1997 s->gpio.parent = dev; 1998 s->gpio.label = devtype->name; 1999 s->gpio.get_direction = max310x_gpio_get_direction; 2000 s->gpio.direction_input = max310x_gpio_direction_input; 2001 s->gpio.get = max310x_gpio_get; 2002 s->gpio.direction_output= max310x_gpio_direction_output; 2003 s->gpio.set = max310x_gpio_set; 2004 s->gpio.set_config = max310x_gpio_set_config; 2005 s->gpio.base = -1; 2006 s->gpio.ngpio = devtype->nr * 4; 2007 s->gpio.can_sleep = 1; 2008 ret = devm_gpiochip_add_data(dev, &s->gpio, s); 2009 if (ret) 2010 goto out_uart; 2011 #endif 2012 2013 for (i = 0; i < devtype->nr; i++) { 2014 struct fwnode_handle *saved_fwnode = dev_fwnode(dev); 2015 struct device_node *port_np = NULL; 2016 struct device_node *child; 2017 unsigned int line; 2018 2019 line = find_first_zero_bit(max310x_lines, MAX310X_UART_NRMAX); 2020 if (line == MAX310X_UART_NRMAX) { 2021 ret = -ERANGE; 2022 goto out_uart; 2023 } 2024 s->p[i].port.line = line; 2025 2026 /* Locate the matching "serial@i" DT subnode, if any. */ 2027 for_each_available_child_of_node(dev->of_node, child) { 2028 u32 reg; 2029 2030 if (!of_node_name_eq(child, "serial")) 2031 continue; 2032 if (of_property_read_u32(child, "reg", ®)) 2033 continue; 2034 if (reg == i) { 2035 port_np = child; 2036 break; 2037 } 2038 } 2039 2040 /* 2041 * Temporarily retarget dev's fwnode to the per-port subnode 2042 * so uart_get_rs485_mode() picks up the per-port properties. 2043 * For single-port variants, fall back to the chip's own 2044 * fwnode so legacy DTs that declare rs485 properties at the 2045 * top level keep working. 2046 */ 2047 if (port_np) { 2048 device_set_node(dev, of_fwnode_handle(port_np)); 2049 ret = uart_get_rs485_mode(&s->p[i].port); 2050 device_set_node(dev, saved_fwnode); 2051 of_node_put(port_np); 2052 if (ret) 2053 goto out_uart; 2054 } else if (devtype->nr == 1) { 2055 ret = uart_get_rs485_mode(&s->p[i].port); 2056 if (ret) 2057 goto out_uart; 2058 } 2059 2060 /* Register port */ 2061 ret = uart_add_one_port(&max310x_uart, &s->p[i].port); 2062 if (ret) 2063 goto out_uart; 2064 2065 set_bit(line, max310x_lines); 2066 2067 /* Go to suspend mode */ 2068 max310x_power(&s->p[i].port, 0); 2069 } 2070 2071 /* Setup interrupt */ 2072 ret = devm_request_threaded_irq(dev, irq, NULL, max310x_ist, 2073 IRQF_ONESHOT | IRQF_SHARED, dev_name(dev), s); 2074 if (!ret) 2075 return 0; 2076 2077 dev_err(dev, "Unable to request IRQ %i\n", irq); 2078 2079 out_uart: 2080 for (i = 0; i < devtype->nr; i++) { 2081 if (test_and_clear_bit(s->p[i].port.line, max310x_lines)) 2082 uart_remove_one_port(&max310x_uart, &s->p[i].port); 2083 } 2084 2085 out_clk: 2086 clk_disable_unprepare(s->clk); 2087 2088 return ret; 2089 } 2090 2091 static void max310x_remove(struct device *dev) 2092 { 2093 struct max310x_port *s = dev_get_drvdata(dev); 2094 int i; 2095 2096 for (i = 0; i < s->devtype->nr; i++) { 2097 hrtimer_cancel(&s->p[i].tx_delay_tmr); 2098 cancel_work_sync(&s->p[i].rts_work); 2099 cancel_work_sync(&s->p[i].tx_work); 2100 cancel_work_sync(&s->p[i].md_work); 2101 cancel_work_sync(&s->p[i].rs_work); 2102 2103 if (test_and_clear_bit(s->p[i].port.line, max310x_lines)) 2104 uart_remove_one_port(&max310x_uart, &s->p[i].port); 2105 2106 max310x_power(&s->p[i].port, 0); 2107 } 2108 2109 clk_disable_unprepare(s->clk); 2110 } 2111 2112 static const struct of_device_id __maybe_unused max310x_dt_ids[] = { 2113 { .compatible = "maxim,max3107", .data = &max3107_devtype, }, 2114 { .compatible = "maxim,max3108", .data = &max3108_devtype, }, 2115 { .compatible = "maxim,max3109", .data = &max3109_devtype, }, 2116 { .compatible = "maxim,max14830", .data = &max14830_devtype }, 2117 { } 2118 }; 2119 MODULE_DEVICE_TABLE(of, max310x_dt_ids); 2120 2121 static const char *max310x_regmap_name(u8 port_id) 2122 { 2123 switch (port_id) { 2124 case 0: return "port0"; 2125 case 1: return "port1"; 2126 case 2: return "port2"; 2127 case 3: return "port3"; 2128 default: 2129 WARN_ON(true); 2130 return NULL; 2131 } 2132 } 2133 2134 #if IS_ENABLED(CONFIG_SPI_MASTER) 2135 2136 static struct regmap_config regcfg = { 2137 .reg_bits = 8, 2138 .val_bits = 8, 2139 .write_flag_mask = MAX310X_WRITE_BIT, 2140 .cache_type = REGCACHE_MAPLE, 2141 .max_register = MAX310X_REG_1F, 2142 .writeable_reg = max310x_reg_writeable, 2143 .volatile_reg = max310x_reg_volatile, 2144 .precious_reg = max310x_reg_precious, 2145 .writeable_noinc_reg = max310x_reg_noinc, 2146 .readable_noinc_reg = max310x_reg_noinc, 2147 .max_raw_read = MAX310X_FIFO_SIZE, 2148 .max_raw_write = MAX310X_FIFO_SIZE, 2149 }; 2150 2151 static int max310x_spi_extended_reg_enable(struct device *dev, bool enable) 2152 { 2153 struct max310x_port *s = dev_get_drvdata(dev); 2154 2155 return regmap_write(s->regmap, MAX310X_GLOBALCMD_REG, 2156 enable ? MAX310X_EXTREG_ENBL : MAX310X_EXTREG_DSBL); 2157 } 2158 2159 static const struct max310x_if_cfg __maybe_unused max310x_spi_if_cfg = { 2160 .extended_reg_enable = max310x_spi_extended_reg_enable, 2161 .rev_id_offset = MAX310X_EXTREG_START, 2162 }; 2163 2164 static int max310x_spi_probe(struct spi_device *spi) 2165 { 2166 const struct max310x_devtype *devtype; 2167 struct regmap *regmaps[MAX310X_MAX_PORTS]; 2168 unsigned int i; 2169 int ret; 2170 2171 /* Setup SPI bus */ 2172 spi->bits_per_word = 8; 2173 spi->mode = spi->mode ? : SPI_MODE_0; 2174 spi->max_speed_hz = spi->max_speed_hz ? : 26000000; 2175 ret = spi_setup(spi); 2176 if (ret) 2177 return ret; 2178 2179 devtype = spi_get_device_match_data(spi); 2180 if (!devtype) 2181 return dev_err_probe(&spi->dev, -ENODEV, "Failed to match device\n"); 2182 2183 for (i = 0; i < devtype->nr; i++) { 2184 u8 port_mask = i * 0x20; 2185 2186 regcfg.name = max310x_regmap_name(i); 2187 regcfg.read_flag_mask = port_mask; 2188 regcfg.write_flag_mask = port_mask | MAX310X_WRITE_BIT; 2189 regmaps[i] = devm_regmap_init_spi(spi, ®cfg); 2190 } 2191 2192 return max310x_probe(&spi->dev, devtype, &max310x_spi_if_cfg, regmaps, spi->irq); 2193 } 2194 2195 static void max310x_spi_remove(struct spi_device *spi) 2196 { 2197 max310x_remove(&spi->dev); 2198 } 2199 2200 static const struct spi_device_id max310x_id_table[] = { 2201 { "max3107", (kernel_ulong_t)&max3107_devtype, }, 2202 { "max3108", (kernel_ulong_t)&max3108_devtype, }, 2203 { "max3109", (kernel_ulong_t)&max3109_devtype, }, 2204 { "max14830", (kernel_ulong_t)&max14830_devtype, }, 2205 { } 2206 }; 2207 MODULE_DEVICE_TABLE(spi, max310x_id_table); 2208 2209 static struct spi_driver max310x_spi_driver = { 2210 .driver = { 2211 .name = MAX310X_NAME, 2212 .of_match_table = max310x_dt_ids, 2213 .pm = &max310x_pm_ops, 2214 }, 2215 .probe = max310x_spi_probe, 2216 .remove = max310x_spi_remove, 2217 .id_table = max310x_id_table, 2218 }; 2219 #endif 2220 2221 #if IS_ENABLED(CONFIG_I2C) 2222 2223 static int max310x_i2c_extended_reg_enable(struct device *dev, bool enable) 2224 { 2225 return 0; 2226 } 2227 2228 static struct regmap_config regcfg_i2c = { 2229 .reg_bits = 8, 2230 .val_bits = 8, 2231 .cache_type = REGCACHE_MAPLE, 2232 .writeable_reg = max310x_reg_writeable, 2233 .volatile_reg = max310x_reg_volatile, 2234 .precious_reg = max310x_reg_precious, 2235 .max_register = MAX310X_REVID_EXTREG, 2236 .writeable_noinc_reg = max310x_reg_noinc, 2237 .readable_noinc_reg = max310x_reg_noinc, 2238 .max_raw_read = MAX310X_FIFO_SIZE, 2239 .max_raw_write = MAX310X_FIFO_SIZE, 2240 }; 2241 2242 static const struct max310x_if_cfg max310x_i2c_if_cfg = { 2243 .extended_reg_enable = max310x_i2c_extended_reg_enable, 2244 .rev_id_offset = 0, /* No offset in I2C mode. */ 2245 }; 2246 2247 static unsigned short max310x_i2c_slave_addr(unsigned short addr, 2248 unsigned int nr) 2249 { 2250 /* 2251 * For MAX14830 and MAX3109, the slave address depends on what the 2252 * A0 and A1 pins are tied to. 2253 * See Table I2C Address Map of the datasheet. 2254 * Based on that table, the following formulas were determined: 2255 * UART1 - UART0 = 0x10 2256 * UART2 - UART1 = 0x20 + 0x10 2257 * UART3 - UART2 = 0x10 2258 */ 2259 2260 addr -= nr * 0x10; 2261 2262 if (nr >= 2) 2263 addr -= 0x20; 2264 2265 return addr; 2266 } 2267 2268 static int max310x_i2c_probe(struct i2c_client *client) 2269 { 2270 const struct max310x_devtype *devtype; 2271 struct i2c_client *port_client; 2272 struct regmap *regmaps[MAX310X_MAX_PORTS]; 2273 unsigned int i; 2274 u8 port_addr; 2275 2276 devtype = i2c_get_match_data(client); 2277 if (!devtype) 2278 return dev_err_probe(&client->dev, -ENODEV, "Failed to match device\n"); 2279 2280 if (client->addr < devtype->slave_addr.min || 2281 client->addr > devtype->slave_addr.max) 2282 return dev_err_probe(&client->dev, -EINVAL, 2283 "Slave addr 0x%x outside of range [0x%x, 0x%x]\n", 2284 client->addr, devtype->slave_addr.min, 2285 devtype->slave_addr.max); 2286 2287 regcfg_i2c.name = max310x_regmap_name(0); 2288 regmaps[0] = devm_regmap_init_i2c(client, ®cfg_i2c); 2289 2290 for (i = 1; i < devtype->nr; i++) { 2291 port_addr = max310x_i2c_slave_addr(client->addr, i); 2292 port_client = devm_i2c_new_dummy_device(&client->dev, 2293 client->adapter, 2294 port_addr); 2295 if (IS_ERR(port_client)) 2296 return PTR_ERR(port_client); 2297 2298 regcfg_i2c.name = max310x_regmap_name(i); 2299 regmaps[i] = devm_regmap_init_i2c(port_client, ®cfg_i2c); 2300 } 2301 2302 return max310x_probe(&client->dev, devtype, &max310x_i2c_if_cfg, 2303 regmaps, client->irq); 2304 } 2305 2306 static void max310x_i2c_remove(struct i2c_client *client) 2307 { 2308 max310x_remove(&client->dev); 2309 } 2310 2311 static const struct i2c_device_id max310x_i2c_id_table[] = { 2312 { .name = "max3107", .driver_data = (kernel_ulong_t)&max3107_devtype }, 2313 { .name = "max3108", .driver_data = (kernel_ulong_t)&max3108_devtype }, 2314 { .name = "max3109", .driver_data = (kernel_ulong_t)&max3109_devtype }, 2315 { .name = "max14830", .driver_data = (kernel_ulong_t)&max14830_devtype }, 2316 { } 2317 }; 2318 MODULE_DEVICE_TABLE(i2c, max310x_i2c_id_table); 2319 2320 static struct i2c_driver max310x_i2c_driver = { 2321 .driver = { 2322 .name = MAX310X_NAME, 2323 .of_match_table = max310x_dt_ids, 2324 .pm = &max310x_pm_ops, 2325 }, 2326 .probe = max310x_i2c_probe, 2327 .remove = max310x_i2c_remove, 2328 .id_table = max310x_i2c_id_table, 2329 }; 2330 #endif 2331 2332 static int __init max310x_uart_init(void) 2333 { 2334 int ret; 2335 2336 bitmap_zero(max310x_lines, MAX310X_UART_NRMAX); 2337 2338 ret = uart_register_driver(&max310x_uart); 2339 if (ret) 2340 return ret; 2341 2342 #if IS_ENABLED(CONFIG_SPI_MASTER) 2343 ret = spi_register_driver(&max310x_spi_driver); 2344 if (ret) 2345 goto err_spi_register; 2346 #endif 2347 2348 #if IS_ENABLED(CONFIG_I2C) 2349 ret = i2c_add_driver(&max310x_i2c_driver); 2350 if (ret) 2351 goto err_i2c_register; 2352 #endif 2353 2354 return 0; 2355 2356 #if IS_ENABLED(CONFIG_I2C) 2357 err_i2c_register: 2358 #endif 2359 #if IS_ENABLED(CONFIG_SPI_MASTER) 2360 spi_unregister_driver(&max310x_spi_driver); 2361 err_spi_register: 2362 #endif 2363 uart_unregister_driver(&max310x_uart); 2364 2365 return ret; 2366 } 2367 module_init(max310x_uart_init); 2368 2369 static void __exit max310x_uart_exit(void) 2370 { 2371 #if IS_ENABLED(CONFIG_I2C) 2372 i2c_del_driver(&max310x_i2c_driver); 2373 #endif 2374 2375 #if IS_ENABLED(CONFIG_SPI_MASTER) 2376 spi_unregister_driver(&max310x_spi_driver); 2377 #endif 2378 2379 uart_unregister_driver(&max310x_uart); 2380 } 2381 module_exit(max310x_uart_exit); 2382 2383 MODULE_LICENSE("GPL"); 2384 MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>"); 2385 MODULE_DESCRIPTION("MAX310X serial driver"); 2386