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

Configure Feed

Select the types of activity you want to include in your feed.

at v3.13-rc8 917 lines 20 kB view raw
1/* 2 * mrst_max3110.c - spi uart protocol driver for Maxim 3110 3 * 4 * Copyright (c) 2008-2010, Intel Corporation. 5 * 6 * This program is free software; you can redistribute it and/or modify it 7 * under the terms and conditions of the GNU General Public License, 8 * version 2, as published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 13 * more details. 14 * 15 * You should have received a copy of the GNU General Public License along with 16 * this program; if not, write to the Free Software Foundation, Inc., 17 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. 18 */ 19 20/* 21 * Note: 22 * 1. From Max3110 spec, the Rx FIFO has 8 words, while the Tx FIFO only has 23 * 1 word. If SPI master controller doesn't support sclk frequency change, 24 * then the char need be sent out one by one with some delay 25 * 26 * 2. Currently only RX available interrupt is used, no need for waiting TXE 27 * interrupt for a low speed UART device 28 */ 29 30#ifdef CONFIG_MAGIC_SYSRQ 31#define SUPPORT_SYSRQ 32#endif 33 34#include <linux/module.h> 35#include <linux/ioport.h> 36#include <linux/irq.h> 37#include <linux/init.h> 38#include <linux/console.h> 39#include <linux/tty.h> 40#include <linux/tty_flip.h> 41#include <linux/serial_core.h> 42#include <linux/serial_reg.h> 43 44#include <linux/kthread.h> 45#include <linux/spi/spi.h> 46#include <linux/pm.h> 47 48#include "mrst_max3110.h" 49 50#define PR_FMT "mrst_max3110: " 51 52#define UART_TX_NEEDED 1 53#define CON_TX_NEEDED 2 54#define BIT_IRQ_PENDING 3 55 56struct uart_max3110 { 57 struct uart_port port; 58 struct spi_device *spi; 59 char name[SPI_NAME_SIZE]; 60 61 wait_queue_head_t wq; 62 struct task_struct *main_thread; 63 struct task_struct *read_thread; 64 struct mutex thread_mutex; 65 struct mutex io_mutex; 66 67 u32 baud; 68 u16 cur_conf; 69 u8 clock; 70 u8 parity, word_7bits; 71 u16 irq; 72 73 unsigned long uart_flags; 74 75 /* console related */ 76 struct circ_buf con_xmit; 77}; 78 79/* global data structure, may need be removed */ 80static struct uart_max3110 *pmax; 81 82static int receive_chars(struct uart_max3110 *max, 83 unsigned short *str, int len); 84static int max3110_read_multi(struct uart_max3110 *max); 85static void max3110_con_receive(struct uart_max3110 *max); 86 87static int max3110_write_then_read(struct uart_max3110 *max, 88 const void *txbuf, void *rxbuf, unsigned len, int always_fast) 89{ 90 struct spi_device *spi = max->spi; 91 struct spi_message message; 92 struct spi_transfer x; 93 int ret; 94 95 mutex_lock(&max->io_mutex); 96 spi_message_init(&message); 97 memset(&x, 0, sizeof x); 98 x.len = len; 99 x.tx_buf = txbuf; 100 x.rx_buf = rxbuf; 101 spi_message_add_tail(&x, &message); 102 103 if (always_fast) 104 x.speed_hz = spi->max_speed_hz; 105 else if (max->baud) 106 x.speed_hz = max->baud; 107 108 /* Do the i/o */ 109 ret = spi_sync(spi, &message); 110 mutex_unlock(&max->io_mutex); 111 return ret; 112} 113 114/* Write a 16b word to the device */ 115static int max3110_out(struct uart_max3110 *max, const u16 out) 116{ 117 void *buf; 118 u16 *obuf, *ibuf; 119 int ret; 120 121 buf = kzalloc(8, GFP_KERNEL | GFP_DMA); 122 if (!buf) 123 return -ENOMEM; 124 125 obuf = buf; 126 ibuf = buf + 4; 127 *obuf = out; 128 ret = max3110_write_then_read(max, obuf, ibuf, 2, 1); 129 if (ret) { 130 pr_warning(PR_FMT "%s(): get err msg %d when sending 0x%x\n", 131 __func__, ret, out); 132 goto exit; 133 } 134 135 receive_chars(max, ibuf, 1); 136 137exit: 138 kfree(buf); 139 return ret; 140} 141 142/* 143 * This is usually used to read data from SPIC RX FIFO, which doesn't 144 * need any delay like flushing character out. 145 * 146 * Return how many valide bytes are read back 147 */ 148static int max3110_read_multi(struct uart_max3110 *max) 149{ 150 void *buf; 151 u16 *obuf, *ibuf; 152 int ret, blen; 153 154 blen = M3110_RX_FIFO_DEPTH * sizeof(u16); 155 buf = kzalloc(blen * 2, GFP_KERNEL | GFP_DMA); 156 if (!buf) { 157 pr_warning(PR_FMT "%s(): fail to alloc dma buffer\n", __func__); 158 return 0; 159 } 160 161 /* tx/rx always have the same length */ 162 obuf = buf; 163 ibuf = buf + blen; 164 165 if (max3110_write_then_read(max, obuf, ibuf, blen, 1)) { 166 kfree(buf); 167 return 0; 168 } 169 170 ret = receive_chars(max, ibuf, M3110_RX_FIFO_DEPTH); 171 172 kfree(buf); 173 return ret; 174} 175 176static void serial_m3110_con_putchar(struct uart_port *port, int ch) 177{ 178 struct uart_max3110 *max = 179 container_of(port, struct uart_max3110, port); 180 struct circ_buf *xmit = &max->con_xmit; 181 182 if (uart_circ_chars_free(xmit)) { 183 xmit->buf[xmit->head] = (char)ch; 184 xmit->head = (xmit->head + 1) & (PAGE_SIZE - 1); 185 } 186} 187 188/* 189 * Print a string to the serial port trying not to disturb 190 * any possible real use of the port... 191 * 192 * The console_lock must be held when we get here. 193 */ 194static void serial_m3110_con_write(struct console *co, 195 const char *s, unsigned int count) 196{ 197 if (!pmax) 198 return; 199 200 uart_console_write(&pmax->port, s, count, serial_m3110_con_putchar); 201 202 if (!test_and_set_bit(CON_TX_NEEDED, &pmax->uart_flags)) 203 wake_up(&pmax->wq); 204} 205 206static int __init 207serial_m3110_con_setup(struct console *co, char *options) 208{ 209 struct uart_max3110 *max = pmax; 210 int baud = 115200; 211 int bits = 8; 212 int parity = 'n'; 213 int flow = 'n'; 214 215 pr_info(PR_FMT "setting up console\n"); 216 217 if (co->index == -1) 218 co->index = 0; 219 220 if (!max) { 221 pr_err(PR_FMT "pmax is NULL, return"); 222 return -ENODEV; 223 } 224 225 if (options) 226 uart_parse_options(options, &baud, &parity, &bits, &flow); 227 228 return uart_set_options(&max->port, co, baud, parity, bits, flow); 229} 230 231static struct tty_driver *serial_m3110_con_device(struct console *co, 232 int *index) 233{ 234 struct uart_driver *p = co->data; 235 *index = co->index; 236 return p->tty_driver; 237} 238 239static struct uart_driver serial_m3110_reg; 240static struct console serial_m3110_console = { 241 .name = "ttyS", 242 .write = serial_m3110_con_write, 243 .device = serial_m3110_con_device, 244 .setup = serial_m3110_con_setup, 245 .flags = CON_PRINTBUFFER, 246 .index = -1, 247 .data = &serial_m3110_reg, 248}; 249 250static unsigned int serial_m3110_tx_empty(struct uart_port *port) 251{ 252 return 1; 253} 254 255static void serial_m3110_stop_tx(struct uart_port *port) 256{ 257 return; 258} 259 260/* stop_rx will be called in spin_lock env */ 261static void serial_m3110_stop_rx(struct uart_port *port) 262{ 263 return; 264} 265 266#define WORDS_PER_XFER 128 267static void send_circ_buf(struct uart_max3110 *max, 268 struct circ_buf *xmit) 269{ 270 void *buf; 271 u16 *obuf, *ibuf; 272 int i, len, blen, dma_size, left, ret = 0; 273 274 275 dma_size = WORDS_PER_XFER * sizeof(u16) * 2; 276 buf = kzalloc(dma_size, GFP_KERNEL | GFP_DMA); 277 if (!buf) 278 return; 279 obuf = buf; 280 ibuf = buf + dma_size/2; 281 282 while (!uart_circ_empty(xmit)) { 283 left = uart_circ_chars_pending(xmit); 284 while (left) { 285 len = min(left, WORDS_PER_XFER); 286 blen = len * sizeof(u16); 287 memset(ibuf, 0, blen); 288 289 for (i = 0; i < len; i++) { 290 obuf[i] = (u8)xmit->buf[xmit->tail] | WD_TAG; 291 xmit->tail = (xmit->tail + 1) & 292 (UART_XMIT_SIZE - 1); 293 } 294 295 /* Fail to send msg to console is not very critical */ 296 297 ret = max3110_write_then_read(max, obuf, ibuf, blen, 0); 298 if (ret) 299 pr_warning(PR_FMT "%s(): get err msg %d\n", 300 __func__, ret); 301 302 receive_chars(max, ibuf, len); 303 304 max->port.icount.tx += len; 305 left -= len; 306 } 307 } 308 309 kfree(buf); 310} 311 312static void transmit_char(struct uart_max3110 *max) 313{ 314 struct uart_port *port = &max->port; 315 struct circ_buf *xmit = &port->state->xmit; 316 317 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) 318 return; 319 320 send_circ_buf(max, xmit); 321 322 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 323 uart_write_wakeup(port); 324 325 if (uart_circ_empty(xmit)) 326 serial_m3110_stop_tx(port); 327} 328 329/* 330 * This will be called by uart_write() and tty_write, can't 331 * go to sleep 332 */ 333static void serial_m3110_start_tx(struct uart_port *port) 334{ 335 struct uart_max3110 *max = 336 container_of(port, struct uart_max3110, port); 337 338 if (!test_and_set_bit(UART_TX_NEEDED, &max->uart_flags)) 339 wake_up(&max->wq); 340} 341 342static int 343receive_chars(struct uart_max3110 *max, unsigned short *str, int len) 344{ 345 struct uart_port *port = &max->port; 346 struct tty_port *tport; 347 char buf[M3110_RX_FIFO_DEPTH]; 348 int r, w, usable; 349 350 /* If uart is not opened, just return */ 351 if (!port->state) 352 return 0; 353 354 tport = &port->state->port; 355 356 for (r = 0, w = 0; r < len; r++) { 357 if (str[r] & MAX3110_BREAK && 358 uart_handle_break(port)) 359 continue; 360 361 if (str[r] & MAX3110_READ_DATA_AVAILABLE) { 362 if (uart_handle_sysrq_char(port, str[r] & 0xff)) 363 continue; 364 365 buf[w++] = str[r] & 0xff; 366 } 367 } 368 369 if (!w) 370 return 0; 371 372 for (r = 0; w; r += usable, w -= usable) { 373 usable = tty_buffer_request_room(tport, w); 374 if (usable) { 375 tty_insert_flip_string(tport, buf + r, usable); 376 port->icount.rx += usable; 377 } 378 } 379 tty_flip_buffer_push(tport); 380 381 return r; 382} 383 384/* 385 * This routine will be used in read_thread or RX IRQ handling, 386 * it will first do one round buffer read(8 words), if there is some 387 * valid RX data, will try to read 5 more rounds till all data 388 * is read out. 389 * 390 * Use stack space as data buffer to save some system load, and chose 391 * 504 Btyes as a threadhold to do a bulk push to upper tty layer when 392 * receiving bulk data, a much bigger buffer may cause stack overflow 393 */ 394static void max3110_con_receive(struct uart_max3110 *max) 395{ 396 int loop = 1, num; 397 398 do { 399 num = max3110_read_multi(max); 400 401 if (num) { 402 loop = 5; 403 } 404 } while (--loop); 405} 406 407static int max3110_main_thread(void *_max) 408{ 409 struct uart_max3110 *max = _max; 410 wait_queue_head_t *wq = &max->wq; 411 int ret = 0; 412 struct circ_buf *xmit = &max->con_xmit; 413 414 pr_info(PR_FMT "start main thread\n"); 415 416 do { 417 wait_event_interruptible(*wq, 418 max->uart_flags || kthread_should_stop()); 419 420 mutex_lock(&max->thread_mutex); 421 422 if (test_and_clear_bit(BIT_IRQ_PENDING, &max->uart_flags)) 423 max3110_con_receive(max); 424 425 /* first handle console output */ 426 if (test_and_clear_bit(CON_TX_NEEDED, &max->uart_flags)) 427 send_circ_buf(max, xmit); 428 429 /* handle uart output */ 430 if (test_and_clear_bit(UART_TX_NEEDED, &max->uart_flags)) 431 transmit_char(max); 432 433 mutex_unlock(&max->thread_mutex); 434 435 } while (!kthread_should_stop()); 436 437 return ret; 438} 439 440static irqreturn_t serial_m3110_irq(int irq, void *dev_id) 441{ 442 struct uart_max3110 *max = dev_id; 443 444 /* max3110's irq is a falling edge, not level triggered, 445 * so no need to disable the irq */ 446 447 if (!test_and_set_bit(BIT_IRQ_PENDING, &max->uart_flags)) 448 wake_up(&max->wq); 449 450 return IRQ_HANDLED; 451} 452 453/* if don't use RX IRQ, then need a thread to polling read */ 454static int max3110_read_thread(void *_max) 455{ 456 struct uart_max3110 *max = _max; 457 458 pr_info(PR_FMT "start read thread\n"); 459 do { 460 /* 461 * If can't acquire the mutex, it means the main thread 462 * is running which will also perform the rx job 463 */ 464 if (mutex_trylock(&max->thread_mutex)) { 465 max3110_con_receive(max); 466 mutex_unlock(&max->thread_mutex); 467 } 468 469 set_current_state(TASK_INTERRUPTIBLE); 470 schedule_timeout(HZ / 20); 471 } while (!kthread_should_stop()); 472 473 return 0; 474} 475 476static int serial_m3110_startup(struct uart_port *port) 477{ 478 struct uart_max3110 *max = 479 container_of(port, struct uart_max3110, port); 480 u16 config = 0; 481 int ret = 0; 482 483 if (port->line != 0) { 484 pr_err(PR_FMT "uart port startup failed\n"); 485 return -1; 486 } 487 488 /* Disable all IRQ and config it to 115200, 8n1 */ 489 config = WC_TAG | WC_FIFO_ENABLE 490 | WC_1_STOPBITS 491 | WC_8BIT_WORD 492 | WC_BAUD_DR2; 493 494 /* as we use thread to handle tx/rx, need set low latency */ 495 port->state->port.low_latency = 1; 496 497 if (max->irq) { 498 /* Enable RX IRQ only */ 499 config |= WC_RXA_IRQ_ENABLE; 500 } else { 501 /* If IRQ is disabled, start a read thread for input data */ 502 max->read_thread = 503 kthread_run(max3110_read_thread, max, "max3110_read"); 504 if (IS_ERR(max->read_thread)) { 505 ret = PTR_ERR(max->read_thread); 506 max->read_thread = NULL; 507 pr_err(PR_FMT "Can't create read thread!\n"); 508 return ret; 509 } 510 } 511 512 ret = max3110_out(max, config); 513 if (ret) { 514 if (max->read_thread) 515 kthread_stop(max->read_thread); 516 max->read_thread = NULL; 517 return ret; 518 } 519 520 max->cur_conf = config; 521 return 0; 522} 523 524static void serial_m3110_shutdown(struct uart_port *port) 525{ 526 struct uart_max3110 *max = 527 container_of(port, struct uart_max3110, port); 528 u16 config; 529 530 if (max->read_thread) { 531 kthread_stop(max->read_thread); 532 max->read_thread = NULL; 533 } 534 535 /* Disable interrupts from this port */ 536 config = WC_TAG | WC_SW_SHDI; 537 max3110_out(max, config); 538} 539 540static void serial_m3110_release_port(struct uart_port *port) 541{ 542} 543 544static int serial_m3110_request_port(struct uart_port *port) 545{ 546 return 0; 547} 548 549static void serial_m3110_config_port(struct uart_port *port, int flags) 550{ 551 port->type = PORT_MAX3100; 552} 553 554static int 555serial_m3110_verify_port(struct uart_port *port, struct serial_struct *ser) 556{ 557 /* we don't want the core code to modify any port params */ 558 return -EINVAL; 559} 560 561 562static const char *serial_m3110_type(struct uart_port *port) 563{ 564 struct uart_max3110 *max = 565 container_of(port, struct uart_max3110, port); 566 return max->name; 567} 568 569static void 570serial_m3110_set_termios(struct uart_port *port, struct ktermios *termios, 571 struct ktermios *old) 572{ 573 struct uart_max3110 *max = 574 container_of(port, struct uart_max3110, port); 575 unsigned char cval; 576 unsigned int baud, parity = 0; 577 int clk_div = -1; 578 u16 new_conf = max->cur_conf; 579 580 switch (termios->c_cflag & CSIZE) { 581 case CS7: 582 cval = UART_LCR_WLEN7; 583 new_conf |= WC_7BIT_WORD; 584 break; 585 default: 586 /* We only support CS7 & CS8 */ 587 termios->c_cflag &= ~CSIZE; 588 termios->c_cflag |= CS8; 589 case CS8: 590 cval = UART_LCR_WLEN8; 591 new_conf |= WC_8BIT_WORD; 592 break; 593 } 594 595 baud = uart_get_baud_rate(port, termios, old, 0, 230400); 596 597 /* First calc the div for 1.8MHZ clock case */ 598 switch (baud) { 599 case 300: 600 clk_div = WC_BAUD_DR384; 601 break; 602 case 600: 603 clk_div = WC_BAUD_DR192; 604 break; 605 case 1200: 606 clk_div = WC_BAUD_DR96; 607 break; 608 case 2400: 609 clk_div = WC_BAUD_DR48; 610 break; 611 case 4800: 612 clk_div = WC_BAUD_DR24; 613 break; 614 case 9600: 615 clk_div = WC_BAUD_DR12; 616 break; 617 case 19200: 618 clk_div = WC_BAUD_DR6; 619 break; 620 case 38400: 621 clk_div = WC_BAUD_DR3; 622 break; 623 case 57600: 624 clk_div = WC_BAUD_DR2; 625 break; 626 case 115200: 627 clk_div = WC_BAUD_DR1; 628 break; 629 case 230400: 630 if (max->clock & MAX3110_HIGH_CLK) 631 break; 632 default: 633 /* Pick the previous baud rate */ 634 baud = max->baud; 635 clk_div = max->cur_conf & WC_BAUD_DIV_MASK; 636 tty_termios_encode_baud_rate(termios, baud, baud); 637 } 638 639 if (max->clock & MAX3110_HIGH_CLK) { 640 clk_div += 1; 641 /* High clk version max3110 doesn't support B300 */ 642 if (baud == 300) { 643 baud = 600; 644 clk_div = WC_BAUD_DR384; 645 } 646 if (baud == 230400) 647 clk_div = WC_BAUD_DR1; 648 tty_termios_encode_baud_rate(termios, baud, baud); 649 } 650 651 new_conf = (new_conf & ~WC_BAUD_DIV_MASK) | clk_div; 652 653 if (unlikely(termios->c_cflag & CMSPAR)) 654 termios->c_cflag &= ~CMSPAR; 655 656 if (termios->c_cflag & CSTOPB) 657 new_conf |= WC_2_STOPBITS; 658 else 659 new_conf &= ~WC_2_STOPBITS; 660 661 if (termios->c_cflag & PARENB) { 662 new_conf |= WC_PARITY_ENABLE; 663 parity |= UART_LCR_PARITY; 664 } else 665 new_conf &= ~WC_PARITY_ENABLE; 666 667 if (!(termios->c_cflag & PARODD)) 668 parity |= UART_LCR_EPAR; 669 max->parity = parity; 670 671 uart_update_timeout(port, termios->c_cflag, baud); 672 673 new_conf |= WC_TAG; 674 if (new_conf != max->cur_conf) { 675 if (!max3110_out(max, new_conf)) { 676 max->cur_conf = new_conf; 677 max->baud = baud; 678 } 679 } 680} 681 682/* Don't handle hw handshaking */ 683static unsigned int serial_m3110_get_mctrl(struct uart_port *port) 684{ 685 return TIOCM_DSR | TIOCM_CAR | TIOCM_DSR; 686} 687 688static void serial_m3110_set_mctrl(struct uart_port *port, unsigned int mctrl) 689{ 690} 691 692static void serial_m3110_break_ctl(struct uart_port *port, int break_state) 693{ 694} 695 696static void serial_m3110_pm(struct uart_port *port, unsigned int state, 697 unsigned int oldstate) 698{ 699} 700 701static void serial_m3110_enable_ms(struct uart_port *port) 702{ 703} 704 705static struct uart_ops serial_m3110_ops = { 706 .tx_empty = serial_m3110_tx_empty, 707 .set_mctrl = serial_m3110_set_mctrl, 708 .get_mctrl = serial_m3110_get_mctrl, 709 .stop_tx = serial_m3110_stop_tx, 710 .start_tx = serial_m3110_start_tx, 711 .stop_rx = serial_m3110_stop_rx, 712 .enable_ms = serial_m3110_enable_ms, 713 .break_ctl = serial_m3110_break_ctl, 714 .startup = serial_m3110_startup, 715 .shutdown = serial_m3110_shutdown, 716 .set_termios = serial_m3110_set_termios, 717 .pm = serial_m3110_pm, 718 .type = serial_m3110_type, 719 .release_port = serial_m3110_release_port, 720 .request_port = serial_m3110_request_port, 721 .config_port = serial_m3110_config_port, 722 .verify_port = serial_m3110_verify_port, 723}; 724 725static struct uart_driver serial_m3110_reg = { 726 .owner = THIS_MODULE, 727 .driver_name = "MRST serial", 728 .dev_name = "ttyS", 729 .major = TTY_MAJOR, 730 .minor = 64, 731 .nr = 1, 732 .cons = &serial_m3110_console, 733}; 734 735#ifdef CONFIG_PM_SLEEP 736static int serial_m3110_suspend(struct device *dev) 737{ 738 struct spi_device *spi = to_spi_device(dev); 739 struct uart_max3110 *max = spi_get_drvdata(spi); 740 741 if (max->irq > 0) 742 disable_irq(max->irq); 743 uart_suspend_port(&serial_m3110_reg, &max->port); 744 max3110_out(max, max->cur_conf | WC_SW_SHDI); 745 return 0; 746} 747 748static int serial_m3110_resume(struct device *dev) 749{ 750 struct spi_device *spi = to_spi_device(dev); 751 struct uart_max3110 *max = spi_get_drvdata(spi); 752 753 max3110_out(max, max->cur_conf); 754 uart_resume_port(&serial_m3110_reg, &max->port); 755 if (max->irq > 0) 756 enable_irq(max->irq); 757 return 0; 758} 759 760static SIMPLE_DEV_PM_OPS(serial_m3110_pm_ops, serial_m3110_suspend, 761 serial_m3110_resume); 762#define SERIAL_M3110_PM_OPS (&serial_m3110_pm_ops) 763 764#else 765#define SERIAL_M3110_PM_OPS NULL 766#endif 767 768static int serial_m3110_probe(struct spi_device *spi) 769{ 770 struct uart_max3110 *max; 771 void *buffer; 772 u16 res; 773 int ret = 0; 774 775 max = kzalloc(sizeof(*max), GFP_KERNEL); 776 if (!max) 777 return -ENOMEM; 778 779 /* Set spi info */ 780 spi->bits_per_word = 16; 781 max->clock = MAX3110_HIGH_CLK; 782 783 spi_setup(spi); 784 785 max->port.type = PORT_MAX3100; 786 max->port.fifosize = 2; /* Only have 16b buffer */ 787 max->port.ops = &serial_m3110_ops; 788 max->port.line = 0; 789 max->port.dev = &spi->dev; 790 max->port.uartclk = 115200; 791 792 max->spi = spi; 793 strcpy(max->name, spi->modalias); 794 max->irq = (u16)spi->irq; 795 796 mutex_init(&max->thread_mutex); 797 mutex_init(&max->io_mutex); 798 799 max->word_7bits = 0; 800 max->parity = 0; 801 max->baud = 0; 802 803 max->cur_conf = 0; 804 max->uart_flags = 0; 805 806 /* Check if reading configuration register returns something sane */ 807 808 res = RC_TAG; 809 ret = max3110_write_then_read(max, (u8 *)&res, (u8 *)&res, 2, 0); 810 if (ret < 0 || res == 0 || res == 0xffff) { 811 dev_dbg(&spi->dev, "MAX3111 deemed not present (conf reg %04x)", 812 res); 813 ret = -ENODEV; 814 goto err_get_page; 815 } 816 817 buffer = (void *)__get_free_page(GFP_KERNEL); 818 if (!buffer) { 819 ret = -ENOMEM; 820 goto err_get_page; 821 } 822 max->con_xmit.buf = buffer; 823 max->con_xmit.head = 0; 824 max->con_xmit.tail = 0; 825 826 init_waitqueue_head(&max->wq); 827 828 max->main_thread = kthread_run(max3110_main_thread, 829 max, "max3110_main"); 830 if (IS_ERR(max->main_thread)) { 831 ret = PTR_ERR(max->main_thread); 832 goto err_kthread; 833 } 834 835 if (max->irq) { 836 ret = request_irq(max->irq, serial_m3110_irq, 837 IRQ_TYPE_EDGE_FALLING, "max3110", max); 838 if (ret) { 839 max->irq = 0; 840 dev_warn(&spi->dev, 841 "unable to allocate IRQ, will use polling method\n"); 842 } 843 } 844 845 spi_set_drvdata(spi, max); 846 pmax = max; 847 848 /* Give membase a psudo value to pass serial_core's check */ 849 max->port.membase = (unsigned char __iomem *)0xff110000; 850 uart_add_one_port(&serial_m3110_reg, &max->port); 851 852 return 0; 853 854err_kthread: 855 free_page((unsigned long)buffer); 856err_get_page: 857 kfree(max); 858 return ret; 859} 860 861static int serial_m3110_remove(struct spi_device *dev) 862{ 863 struct uart_max3110 *max = spi_get_drvdata(dev); 864 865 if (!max) 866 return 0; 867 868 uart_remove_one_port(&serial_m3110_reg, &max->port); 869 870 free_page((unsigned long)max->con_xmit.buf); 871 872 if (max->irq) 873 free_irq(max->irq, max); 874 875 if (max->main_thread) 876 kthread_stop(max->main_thread); 877 878 kfree(max); 879 return 0; 880} 881 882static struct spi_driver uart_max3110_driver = { 883 .driver = { 884 .name = "spi_max3111", 885 .owner = THIS_MODULE, 886 .pm = SERIAL_M3110_PM_OPS, 887 }, 888 .probe = serial_m3110_probe, 889 .remove = serial_m3110_remove, 890}; 891 892static int __init serial_m3110_init(void) 893{ 894 int ret = 0; 895 896 ret = uart_register_driver(&serial_m3110_reg); 897 if (ret) 898 return ret; 899 900 ret = spi_register_driver(&uart_max3110_driver); 901 if (ret) 902 uart_unregister_driver(&serial_m3110_reg); 903 904 return ret; 905} 906 907static void __exit serial_m3110_exit(void) 908{ 909 spi_unregister_driver(&uart_max3110_driver); 910 uart_unregister_driver(&serial_m3110_reg); 911} 912 913module_init(serial_m3110_init); 914module_exit(serial_m3110_exit); 915 916MODULE_LICENSE("GPL v2"); 917MODULE_ALIAS("spi:max3110-uart");