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