1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * 4 * Copyright (C) 2008 Christian Pellegrin <chripell@evolware.org> 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * 12 * Notes: the MAX3100 doesn't provide an interrupt on CTS so we have 13 * to use polling for flow control. TX empty IRQ is unusable, since 14 * writing conf clears FIFO buffer and we cannot have this interrupt 15 * always asking us for attention. 16 * 17 * Example platform data: 18 19 static struct plat_max3100 max3100_plat_data = { 20 .loopback = 0, 21 .crystal = 0, 22 .poll_time = 100, 23 }; 24 25 static struct spi_board_info spi_board_info[] = { 26 { 27 .modalias = "max3100", 28 .platform_data = &max3100_plat_data, 29 .irq = IRQ_EINT12, 30 .max_speed_hz = 5*1000*1000, 31 .chip_select = 0, 32 }, 33 }; 34 35 * The initial minor number is 209 in the low-density serial port: 36 * mknod /dev/ttyMAX0 c 204 209 37 */ 38 39 #define MAX3100_MAJOR 204 40 #define MAX3100_MINOR 209 41 /* 4 MAX3100s should be enough for everyone */ 42 #define MAX_MAX3100 4 43 44 #include <linux/delay.h> 45 #include <linux/slab.h> 46 #include <linux/device.h> 47 #include <linux/module.h> 48 #include <linux/serial_core.h> 49 #include <linux/serial.h> 50 #include <linux/spi/spi.h> 51 #include <linux/freezer.h> 52 #include <linux/tty.h> 53 #include <linux/tty_flip.h> 54 55 #include <linux/serial_max3100.h> 56 57 #define MAX3100_C (1<<14) 58 #define MAX3100_D (0<<14) 59 #define MAX3100_W (1<<15) 60 #define MAX3100_RX (0<<15) 61 62 #define MAX3100_WC (MAX3100_W | MAX3100_C) 63 #define MAX3100_RC (MAX3100_RX | MAX3100_C) 64 #define MAX3100_WD (MAX3100_W | MAX3100_D) 65 #define MAX3100_RD (MAX3100_RX | MAX3100_D) 66 #define MAX3100_CMD (3 << 14) 67 68 #define MAX3100_T (1<<14) 69 #define MAX3100_R (1<<15) 70 71 #define MAX3100_FEN (1<<13) 72 #define MAX3100_SHDN (1<<12) 73 #define MAX3100_TM (1<<11) 74 #define MAX3100_RM (1<<10) 75 #define MAX3100_PM (1<<9) 76 #define MAX3100_RAM (1<<8) 77 #define MAX3100_IR (1<<7) 78 #define MAX3100_ST (1<<6) 79 #define MAX3100_PE (1<<5) 80 #define MAX3100_L (1<<4) 81 #define MAX3100_BAUD (0xf) 82 83 #define MAX3100_TE (1<<10) 84 #define MAX3100_RAFE (1<<10) 85 #define MAX3100_RTS (1<<9) 86 #define MAX3100_CTS (1<<9) 87 #define MAX3100_PT (1<<8) 88 #define MAX3100_DATA (0xff) 89 90 #define MAX3100_RT (MAX3100_R | MAX3100_T) 91 #define MAX3100_RTC (MAX3100_RT | MAX3100_CTS | MAX3100_RAFE) 92 93 /* the following simulate a status reg for ignore_status_mask */ 94 #define MAX3100_STATUS_PE 1 95 #define MAX3100_STATUS_FE 2 96 #define MAX3100_STATUS_OE 4 97 98 struct max3100_port { 99 struct uart_port port; 100 struct spi_device *spi; 101 102 int cts; /* last CTS received for flow ctrl */ 103 int tx_empty; /* last TX empty bit */ 104 105 spinlock_t conf_lock; /* shared data */ 106 int conf_commit; /* need to make changes */ 107 int conf; /* configuration for the MAX31000 108 * (bits 0-7, bits 8-11 are irqs) */ 109 int rts_commit; /* need to change rts */ 110 int rts; /* rts status */ 111 int baud; /* current baud rate */ 112 113 int parity; /* keeps track if we should send parity */ 114 #define MAX3100_PARITY_ON 1 115 #define MAX3100_PARITY_ODD 2 116 #define MAX3100_7BIT 4 117 int rx_enabled; /* if we should rx chars */ 118 119 int irq; /* irq assigned to the max3100 */ 120 121 int minor; /* minor number */ 122 int crystal; /* 1 if 3.6864Mhz crystal 0 for 1.8432 */ 123 int loopback; /* 1 if we are in loopback mode */ 124 125 /* for handling irqs: need workqueue since we do spi_sync */ 126 struct workqueue_struct *workqueue; 127 struct work_struct work; 128 /* set to 1 to make the workhandler exit as soon as possible */ 129 int force_end_work; 130 /* need to know we are suspending to avoid deadlock on workqueue */ 131 int suspending; 132 133 /* hook for suspending MAX3100 via dedicated pin */ 134 void (*max3100_hw_suspend) (int suspend); 135 136 /* poll time (in ms) for ctrl lines */ 137 int poll_time; 138 /* and its timer */ 139 struct timer_list timer; 140 }; 141 142 static struct max3100_port *max3100s[MAX_MAX3100]; /* the chips */ 143 static DEFINE_MUTEX(max3100s_lock); /* race on probe */ 144 145 static int max3100_do_parity(struct max3100_port *s, u16 c) 146 { 147 int parity; 148 149 if (s->parity & MAX3100_PARITY_ODD) 150 parity = 1; 151 else 152 parity = 0; 153 154 if (s->parity & MAX3100_7BIT) 155 c &= 0x7f; 156 else 157 c &= 0xff; 158 159 parity = parity ^ (hweight8(c) & 1); 160 return parity; 161 } 162 163 static int max3100_check_parity(struct max3100_port *s, u16 c) 164 { 165 return max3100_do_parity(s, c) == ((c >> 8) & 1); 166 } 167 168 static void max3100_calc_parity(struct max3100_port *s, u16 *c) 169 { 170 if (s->parity & MAX3100_7BIT) 171 *c &= 0x7f; 172 else 173 *c &= 0xff; 174 175 if (s->parity & MAX3100_PARITY_ON) 176 *c |= max3100_do_parity(s, *c) << 8; 177 } 178 179 static void max3100_work(struct work_struct *w); 180 181 static void max3100_dowork(struct max3100_port *s) 182 { 183 if (!s->force_end_work && !freezing(current) && !s->suspending) 184 queue_work(s->workqueue, &s->work); 185 } 186 187 static void max3100_timeout(unsigned long data) 188 { 189 struct max3100_port *s = (struct max3100_port *)data; 190 191 if (s->port.state) { 192 max3100_dowork(s); 193 mod_timer(&s->timer, jiffies + s->poll_time); 194 } 195 } 196 197 static int max3100_sr(struct max3100_port *s, u16 tx, u16 *rx) 198 { 199 struct spi_message message; 200 u16 etx, erx; 201 int status; 202 struct spi_transfer tran = { 203 .tx_buf = &etx, 204 .rx_buf = &erx, 205 .len = 2, 206 }; 207 208 etx = cpu_to_be16(tx); 209 spi_message_init(&message); 210 spi_message_add_tail(&tran, &message); 211 status = spi_sync(s->spi, &message); 212 if (status) { 213 dev_warn(&s->spi->dev, "error while calling spi_sync\n"); 214 return -EIO; 215 } 216 *rx = be16_to_cpu(erx); 217 s->tx_empty = (*rx & MAX3100_T) > 0; 218 dev_dbg(&s->spi->dev, "%04x - %04x\n", tx, *rx); 219 return 0; 220 } 221 222 static int max3100_handlerx(struct max3100_port *s, u16 rx) 223 { 224 unsigned int ch, flg, status = 0; 225 int ret = 0, cts; 226 227 if (rx & MAX3100_R && s->rx_enabled) { 228 dev_dbg(&s->spi->dev, "%s\n", __func__); 229 ch = rx & (s->parity & MAX3100_7BIT ? 0x7f : 0xff); 230 if (rx & MAX3100_RAFE) { 231 s->port.icount.frame++; 232 flg = TTY_FRAME; 233 status |= MAX3100_STATUS_FE; 234 } else { 235 if (s->parity & MAX3100_PARITY_ON) { 236 if (max3100_check_parity(s, rx)) { 237 s->port.icount.rx++; 238 flg = TTY_NORMAL; 239 } else { 240 s->port.icount.parity++; 241 flg = TTY_PARITY; 242 status |= MAX3100_STATUS_PE; 243 } 244 } else { 245 s->port.icount.rx++; 246 flg = TTY_NORMAL; 247 } 248 } 249 uart_insert_char(&s->port, status, MAX3100_STATUS_OE, ch, flg); 250 ret = 1; 251 } 252 253 cts = (rx & MAX3100_CTS) > 0; 254 if (s->cts != cts) { 255 s->cts = cts; 256 uart_handle_cts_change(&s->port, cts ? TIOCM_CTS : 0); 257 } 258 259 return ret; 260 } 261 262 static void max3100_work(struct work_struct *w) 263 { 264 struct max3100_port *s = container_of(w, struct max3100_port, work); 265 int rxchars; 266 u16 tx, rx; 267 int conf, cconf, crts; 268 struct circ_buf *xmit = &s->port.state->xmit; 269 270 dev_dbg(&s->spi->dev, "%s\n", __func__); 271 272 rxchars = 0; 273 do { 274 spin_lock(&s->conf_lock); 275 conf = s->conf; 276 cconf = s->conf_commit; 277 s->conf_commit = 0; 278 crts = s->rts_commit; 279 s->rts_commit = 0; 280 spin_unlock(&s->conf_lock); 281 if (cconf) 282 max3100_sr(s, MAX3100_WC | conf, &rx); 283 if (crts) { 284 max3100_sr(s, MAX3100_WD | MAX3100_TE | 285 (s->rts ? MAX3100_RTS : 0), &rx); 286 rxchars += max3100_handlerx(s, rx); 287 } 288 289 max3100_sr(s, MAX3100_RD, &rx); 290 rxchars += max3100_handlerx(s, rx); 291 292 if (rx & MAX3100_T) { 293 tx = 0xffff; 294 if (s->port.x_char) { 295 tx = s->port.x_char; 296 s->port.icount.tx++; 297 s->port.x_char = 0; 298 } else if (!uart_circ_empty(xmit) && 299 !uart_tx_stopped(&s->port)) { 300 tx = xmit->buf[xmit->tail]; 301 xmit->tail = (xmit->tail + 1) & 302 (UART_XMIT_SIZE - 1); 303 s->port.icount.tx++; 304 } 305 if (tx != 0xffff) { 306 max3100_calc_parity(s, &tx); 307 tx |= MAX3100_WD | (s->rts ? MAX3100_RTS : 0); 308 max3100_sr(s, tx, &rx); 309 rxchars += max3100_handlerx(s, rx); 310 } 311 } 312 313 if (rxchars > 16) { 314 tty_flip_buffer_push(&s->port.state->port); 315 rxchars = 0; 316 } 317 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 318 uart_write_wakeup(&s->port); 319 320 } while (!s->force_end_work && 321 !freezing(current) && 322 ((rx & MAX3100_R) || 323 (!uart_circ_empty(xmit) && 324 !uart_tx_stopped(&s->port)))); 325 326 if (rxchars > 0) 327 tty_flip_buffer_push(&s->port.state->port); 328 } 329 330 static irqreturn_t max3100_irq(int irqno, void *dev_id) 331 { 332 struct max3100_port *s = dev_id; 333 334 dev_dbg(&s->spi->dev, "%s\n", __func__); 335 336 max3100_dowork(s); 337 return IRQ_HANDLED; 338 } 339 340 static void max3100_enable_ms(struct uart_port *port) 341 { 342 struct max3100_port *s = container_of(port, 343 struct max3100_port, 344 port); 345 346 if (s->poll_time > 0) 347 mod_timer(&s->timer, jiffies); 348 dev_dbg(&s->spi->dev, "%s\n", __func__); 349 } 350 351 static void max3100_start_tx(struct uart_port *port) 352 { 353 struct max3100_port *s = container_of(port, 354 struct max3100_port, 355 port); 356 357 dev_dbg(&s->spi->dev, "%s\n", __func__); 358 359 max3100_dowork(s); 360 } 361 362 static void max3100_stop_rx(struct uart_port *port) 363 { 364 struct max3100_port *s = container_of(port, 365 struct max3100_port, 366 port); 367 368 dev_dbg(&s->spi->dev, "%s\n", __func__); 369 370 s->rx_enabled = 0; 371 spin_lock(&s->conf_lock); 372 s->conf &= ~MAX3100_RM; 373 s->conf_commit = 1; 374 spin_unlock(&s->conf_lock); 375 max3100_dowork(s); 376 } 377 378 static unsigned int max3100_tx_empty(struct uart_port *port) 379 { 380 struct max3100_port *s = container_of(port, 381 struct max3100_port, 382 port); 383 384 dev_dbg(&s->spi->dev, "%s\n", __func__); 385 386 /* may not be truly up-to-date */ 387 max3100_dowork(s); 388 return s->tx_empty; 389 } 390 391 static unsigned int max3100_get_mctrl(struct uart_port *port) 392 { 393 struct max3100_port *s = container_of(port, 394 struct max3100_port, 395 port); 396 397 dev_dbg(&s->spi->dev, "%s\n", __func__); 398 399 /* may not be truly up-to-date */ 400 max3100_dowork(s); 401 /* always assert DCD and DSR since these lines are not wired */ 402 return (s->cts ? TIOCM_CTS : 0) | TIOCM_DSR | TIOCM_CAR; 403 } 404 405 static void max3100_set_mctrl(struct uart_port *port, unsigned int mctrl) 406 { 407 struct max3100_port *s = container_of(port, 408 struct max3100_port, 409 port); 410 int rts; 411 412 dev_dbg(&s->spi->dev, "%s\n", __func__); 413 414 rts = (mctrl & TIOCM_RTS) > 0; 415 416 spin_lock(&s->conf_lock); 417 if (s->rts != rts) { 418 s->rts = rts; 419 s->rts_commit = 1; 420 max3100_dowork(s); 421 } 422 spin_unlock(&s->conf_lock); 423 } 424 425 static void 426 max3100_set_termios(struct uart_port *port, struct ktermios *termios, 427 struct ktermios *old) 428 { 429 struct max3100_port *s = container_of(port, 430 struct max3100_port, 431 port); 432 int baud = 0; 433 unsigned cflag; 434 u32 param_new, param_mask, parity = 0; 435 436 dev_dbg(&s->spi->dev, "%s\n", __func__); 437 438 cflag = termios->c_cflag; 439 param_mask = 0; 440 441 baud = tty_termios_baud_rate(termios); 442 param_new = s->conf & MAX3100_BAUD; 443 switch (baud) { 444 case 300: 445 if (s->crystal) 446 baud = s->baud; 447 else 448 param_new = 15; 449 break; 450 case 600: 451 param_new = 14 + s->crystal; 452 break; 453 case 1200: 454 param_new = 13 + s->crystal; 455 break; 456 case 2400: 457 param_new = 12 + s->crystal; 458 break; 459 case 4800: 460 param_new = 11 + s->crystal; 461 break; 462 case 9600: 463 param_new = 10 + s->crystal; 464 break; 465 case 19200: 466 param_new = 9 + s->crystal; 467 break; 468 case 38400: 469 param_new = 8 + s->crystal; 470 break; 471 case 57600: 472 param_new = 1 + s->crystal; 473 break; 474 case 115200: 475 param_new = 0 + s->crystal; 476 break; 477 case 230400: 478 if (s->crystal) 479 param_new = 0; 480 else 481 baud = s->baud; 482 break; 483 default: 484 baud = s->baud; 485 } 486 tty_termios_encode_baud_rate(termios, baud, baud); 487 s->baud = baud; 488 param_mask |= MAX3100_BAUD; 489 490 if ((cflag & CSIZE) == CS8) { 491 param_new &= ~MAX3100_L; 492 parity &= ~MAX3100_7BIT; 493 } else { 494 param_new |= MAX3100_L; 495 parity |= MAX3100_7BIT; 496 cflag = (cflag & ~CSIZE) | CS7; 497 } 498 param_mask |= MAX3100_L; 499 500 if (cflag & CSTOPB) 501 param_new |= MAX3100_ST; 502 else 503 param_new &= ~MAX3100_ST; 504 param_mask |= MAX3100_ST; 505 506 if (cflag & PARENB) { 507 param_new |= MAX3100_PE; 508 parity |= MAX3100_PARITY_ON; 509 } else { 510 param_new &= ~MAX3100_PE; 511 parity &= ~MAX3100_PARITY_ON; 512 } 513 param_mask |= MAX3100_PE; 514 515 if (cflag & PARODD) 516 parity |= MAX3100_PARITY_ODD; 517 else 518 parity &= ~MAX3100_PARITY_ODD; 519 520 /* mask termios capabilities we don't support */ 521 cflag &= ~CMSPAR; 522 termios->c_cflag = cflag; 523 524 s->port.ignore_status_mask = 0; 525 if (termios->c_iflag & IGNPAR) 526 s->port.ignore_status_mask |= 527 MAX3100_STATUS_PE | MAX3100_STATUS_FE | 528 MAX3100_STATUS_OE; 529 530 /* we are sending char from a workqueue so enable */ 531 s->port.state->port.low_latency = 1; 532 533 if (s->poll_time > 0) 534 del_timer_sync(&s->timer); 535 536 uart_update_timeout(port, termios->c_cflag, baud); 537 538 spin_lock(&s->conf_lock); 539 s->conf = (s->conf & ~param_mask) | (param_new & param_mask); 540 s->conf_commit = 1; 541 s->parity = parity; 542 spin_unlock(&s->conf_lock); 543 max3100_dowork(s); 544 545 if (UART_ENABLE_MS(&s->port, termios->c_cflag)) 546 max3100_enable_ms(&s->port); 547 } 548 549 static void max3100_shutdown(struct uart_port *port) 550 { 551 struct max3100_port *s = container_of(port, 552 struct max3100_port, 553 port); 554 555 dev_dbg(&s->spi->dev, "%s\n", __func__); 556 557 if (s->suspending) 558 return; 559 560 s->force_end_work = 1; 561 562 if (s->poll_time > 0) 563 del_timer_sync(&s->timer); 564 565 if (s->workqueue) { 566 flush_workqueue(s->workqueue); 567 destroy_workqueue(s->workqueue); 568 s->workqueue = NULL; 569 } 570 if (s->irq) 571 free_irq(s->irq, s); 572 573 /* set shutdown mode to save power */ 574 if (s->max3100_hw_suspend) 575 s->max3100_hw_suspend(1); 576 else { 577 u16 tx, rx; 578 579 tx = MAX3100_WC | MAX3100_SHDN; 580 max3100_sr(s, tx, &rx); 581 } 582 } 583 584 static int max3100_startup(struct uart_port *port) 585 { 586 struct max3100_port *s = container_of(port, 587 struct max3100_port, 588 port); 589 char b[12]; 590 591 dev_dbg(&s->spi->dev, "%s\n", __func__); 592 593 s->conf = MAX3100_RM; 594 s->baud = s->crystal ? 230400 : 115200; 595 s->rx_enabled = 1; 596 597 if (s->suspending) 598 return 0; 599 600 s->force_end_work = 0; 601 s->parity = 0; 602 s->rts = 0; 603 604 sprintf(b, "max3100-%d", s->minor); 605 s->workqueue = create_freezable_workqueue(b); 606 if (!s->workqueue) { 607 dev_warn(&s->spi->dev, "cannot create workqueue\n"); 608 return -EBUSY; 609 } 610 INIT_WORK(&s->work, max3100_work); 611 612 if (request_irq(s->irq, max3100_irq, 613 IRQF_TRIGGER_FALLING, "max3100", s) < 0) { 614 dev_warn(&s->spi->dev, "cannot allocate irq %d\n", s->irq); 615 s->irq = 0; 616 destroy_workqueue(s->workqueue); 617 s->workqueue = NULL; 618 return -EBUSY; 619 } 620 621 if (s->loopback) { 622 u16 tx, rx; 623 tx = 0x4001; 624 max3100_sr(s, tx, &rx); 625 } 626 627 if (s->max3100_hw_suspend) 628 s->max3100_hw_suspend(0); 629 s->conf_commit = 1; 630 max3100_dowork(s); 631 /* wait for clock to settle */ 632 msleep(50); 633 634 max3100_enable_ms(&s->port); 635 636 return 0; 637 } 638 639 static const char *max3100_type(struct uart_port *port) 640 { 641 struct max3100_port *s = container_of(port, 642 struct max3100_port, 643 port); 644 645 dev_dbg(&s->spi->dev, "%s\n", __func__); 646 647 return s->port.type == PORT_MAX3100 ? "MAX3100" : NULL; 648 } 649 650 static void max3100_release_port(struct uart_port *port) 651 { 652 struct max3100_port *s = container_of(port, 653 struct max3100_port, 654 port); 655 656 dev_dbg(&s->spi->dev, "%s\n", __func__); 657 } 658 659 static void max3100_config_port(struct uart_port *port, int flags) 660 { 661 struct max3100_port *s = container_of(port, 662 struct max3100_port, 663 port); 664 665 dev_dbg(&s->spi->dev, "%s\n", __func__); 666 667 if (flags & UART_CONFIG_TYPE) 668 s->port.type = PORT_MAX3100; 669 } 670 671 static int max3100_verify_port(struct uart_port *port, 672 struct serial_struct *ser) 673 { 674 struct max3100_port *s = container_of(port, 675 struct max3100_port, 676 port); 677 int ret = -EINVAL; 678 679 dev_dbg(&s->spi->dev, "%s\n", __func__); 680 681 if (ser->type == PORT_UNKNOWN || ser->type == PORT_MAX3100) 682 ret = 0; 683 return ret; 684 } 685 686 static void max3100_stop_tx(struct uart_port *port) 687 { 688 struct max3100_port *s = container_of(port, 689 struct max3100_port, 690 port); 691 692 dev_dbg(&s->spi->dev, "%s\n", __func__); 693 } 694 695 static int max3100_request_port(struct uart_port *port) 696 { 697 struct max3100_port *s = container_of(port, 698 struct max3100_port, 699 port); 700 701 dev_dbg(&s->spi->dev, "%s\n", __func__); 702 return 0; 703 } 704 705 static void max3100_break_ctl(struct uart_port *port, int break_state) 706 { 707 struct max3100_port *s = container_of(port, 708 struct max3100_port, 709 port); 710 711 dev_dbg(&s->spi->dev, "%s\n", __func__); 712 } 713 714 static const struct uart_ops max3100_ops = { 715 .tx_empty = max3100_tx_empty, 716 .set_mctrl = max3100_set_mctrl, 717 .get_mctrl = max3100_get_mctrl, 718 .stop_tx = max3100_stop_tx, 719 .start_tx = max3100_start_tx, 720 .stop_rx = max3100_stop_rx, 721 .enable_ms = max3100_enable_ms, 722 .break_ctl = max3100_break_ctl, 723 .startup = max3100_startup, 724 .shutdown = max3100_shutdown, 725 .set_termios = max3100_set_termios, 726 .type = max3100_type, 727 .release_port = max3100_release_port, 728 .request_port = max3100_request_port, 729 .config_port = max3100_config_port, 730 .verify_port = max3100_verify_port, 731 }; 732 733 static struct uart_driver max3100_uart_driver = { 734 .owner = THIS_MODULE, 735 .driver_name = "ttyMAX", 736 .dev_name = "ttyMAX", 737 .major = MAX3100_MAJOR, 738 .minor = MAX3100_MINOR, 739 .nr = MAX_MAX3100, 740 }; 741 static int uart_driver_registered; 742 743 static int max3100_probe(struct spi_device *spi) 744 { 745 int i, retval; 746 struct plat_max3100 *pdata; 747 u16 tx, rx; 748 749 mutex_lock(&max3100s_lock); 750 751 if (!uart_driver_registered) { 752 uart_driver_registered = 1; 753 retval = uart_register_driver(&max3100_uart_driver); 754 if (retval) { 755 printk(KERN_ERR "Couldn't register max3100 uart driver\n"); 756 mutex_unlock(&max3100s_lock); 757 return retval; 758 } 759 } 760 761 for (i = 0; i < MAX_MAX3100; i++) 762 if (!max3100s[i]) 763 break; 764 if (i == MAX_MAX3100) { 765 dev_warn(&spi->dev, "too many MAX3100 chips\n"); 766 mutex_unlock(&max3100s_lock); 767 return -ENOMEM; 768 } 769 770 max3100s[i] = kzalloc(sizeof(struct max3100_port), GFP_KERNEL); 771 if (!max3100s[i]) { 772 dev_warn(&spi->dev, 773 "kmalloc for max3100 structure %d failed!\n", i); 774 mutex_unlock(&max3100s_lock); 775 return -ENOMEM; 776 } 777 max3100s[i]->spi = spi; 778 max3100s[i]->irq = spi->irq; 779 spin_lock_init(&max3100s[i]->conf_lock); 780 spi_set_drvdata(spi, max3100s[i]); 781 pdata = dev_get_platdata(&spi->dev); 782 max3100s[i]->crystal = pdata->crystal; 783 max3100s[i]->loopback = pdata->loopback; 784 max3100s[i]->poll_time = msecs_to_jiffies(pdata->poll_time); 785 if (pdata->poll_time > 0 && max3100s[i]->poll_time == 0) 786 max3100s[i]->poll_time = 1; 787 max3100s[i]->max3100_hw_suspend = pdata->max3100_hw_suspend; 788 max3100s[i]->minor = i; 789 setup_timer(&max3100s[i]->timer, max3100_timeout, 790 (unsigned long)max3100s[i]); 791 792 dev_dbg(&spi->dev, "%s: adding port %d\n", __func__, i); 793 max3100s[i]->port.irq = max3100s[i]->irq; 794 max3100s[i]->port.uartclk = max3100s[i]->crystal ? 3686400 : 1843200; 795 max3100s[i]->port.fifosize = 16; 796 max3100s[i]->port.ops = &max3100_ops; 797 max3100s[i]->port.flags = UPF_SKIP_TEST | UPF_BOOT_AUTOCONF; 798 max3100s[i]->port.line = i; 799 max3100s[i]->port.type = PORT_MAX3100; 800 max3100s[i]->port.dev = &spi->dev; 801 retval = uart_add_one_port(&max3100_uart_driver, &max3100s[i]->port); 802 if (retval < 0) 803 dev_warn(&spi->dev, 804 "uart_add_one_port failed for line %d with error %d\n", 805 i, retval); 806 807 /* set shutdown mode to save power. Will be woken-up on open */ 808 if (max3100s[i]->max3100_hw_suspend) 809 max3100s[i]->max3100_hw_suspend(1); 810 else { 811 tx = MAX3100_WC | MAX3100_SHDN; 812 max3100_sr(max3100s[i], tx, &rx); 813 } 814 mutex_unlock(&max3100s_lock); 815 return 0; 816 } 817 818 static int max3100_remove(struct spi_device *spi) 819 { 820 struct max3100_port *s = spi_get_drvdata(spi); 821 int i; 822 823 mutex_lock(&max3100s_lock); 824 825 /* find out the index for the chip we are removing */ 826 for (i = 0; i < MAX_MAX3100; i++) 827 if (max3100s[i] == s) { 828 dev_dbg(&spi->dev, "%s: removing port %d\n", __func__, i); 829 uart_remove_one_port(&max3100_uart_driver, &max3100s[i]->port); 830 kfree(max3100s[i]); 831 max3100s[i] = NULL; 832 break; 833 } 834 835 WARN_ON(i == MAX_MAX3100); 836 837 /* check if this is the last chip we have */ 838 for (i = 0; i < MAX_MAX3100; i++) 839 if (max3100s[i]) { 840 mutex_unlock(&max3100s_lock); 841 return 0; 842 } 843 pr_debug("removing max3100 driver\n"); 844 uart_unregister_driver(&max3100_uart_driver); 845 846 mutex_unlock(&max3100s_lock); 847 return 0; 848 } 849 850 #ifdef CONFIG_PM_SLEEP 851 852 static int max3100_suspend(struct device *dev) 853 { 854 struct max3100_port *s = dev_get_drvdata(dev); 855 856 dev_dbg(&s->spi->dev, "%s\n", __func__); 857 858 disable_irq(s->irq); 859 860 s->suspending = 1; 861 uart_suspend_port(&max3100_uart_driver, &s->port); 862 863 if (s->max3100_hw_suspend) 864 s->max3100_hw_suspend(1); 865 else { 866 /* no HW suspend, so do SW one */ 867 u16 tx, rx; 868 869 tx = MAX3100_WC | MAX3100_SHDN; 870 max3100_sr(s, tx, &rx); 871 } 872 return 0; 873 } 874 875 static int max3100_resume(struct device *dev) 876 { 877 struct max3100_port *s = dev_get_drvdata(dev); 878 879 dev_dbg(&s->spi->dev, "%s\n", __func__); 880 881 if (s->max3100_hw_suspend) 882 s->max3100_hw_suspend(0); 883 uart_resume_port(&max3100_uart_driver, &s->port); 884 s->suspending = 0; 885 886 enable_irq(s->irq); 887 888 s->conf_commit = 1; 889 if (s->workqueue) 890 max3100_dowork(s); 891 892 return 0; 893 } 894 895 static SIMPLE_DEV_PM_OPS(max3100_pm_ops, max3100_suspend, max3100_resume); 896 #define MAX3100_PM_OPS (&max3100_pm_ops) 897 898 #else 899 #define MAX3100_PM_OPS NULL 900 #endif 901 902 static struct spi_driver max3100_driver = { 903 .driver = { 904 .name = "max3100", 905 .pm = MAX3100_PM_OPS, 906 }, 907 .probe = max3100_probe, 908 .remove = max3100_remove, 909 }; 910 911 module_spi_driver(max3100_driver); 912 913 MODULE_DESCRIPTION("MAX3100 driver"); 914 MODULE_AUTHOR("Christian Pellegrin <chripell@evolware.org>"); 915 MODULE_LICENSE("GPL"); 916 MODULE_ALIAS("spi:max3100"); 917