Merge git://git.kernel.org/pub/scm/linux/kernel/git/steve/gfs2-2.6-nmw
[pandora-kernel.git] / drivers / serial / mrst_max3110.c
1 /*
2  *  max3110.c - spi uart protocol driver for Maxim 3110 on Moorestown
3  *
4  *  Copyright (C) Intel 2008 Feng Tang <feng.tang@intel.com>
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 availabe interrrupt is used, no need for waiting TXE
27  *    interrupt for a low speed UART device
28  */
29
30 #include <linux/module.h>
31 #include <linux/ioport.h>
32 #include <linux/irq.h>
33 #include <linux/init.h>
34 #include <linux/console.h>
35 #include <linux/sysrq.h>
36 #include <linux/platform_device.h>
37 #include <linux/tty.h>
38 #include <linux/tty_flip.h>
39 #include <linux/serial_core.h>
40 #include <linux/serial_reg.h>
41
42 #include <linux/kthread.h>
43 #include <linux/delay.h>
44 #include <asm/atomic.h>
45 #include <linux/spi/spi.h>
46 #include <linux/spi/dw_spi.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
56 struct uart_max3110 {
57         struct uart_port port;
58         struct spi_device *spi;
59         char *name;
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
66         u32 baud;
67         u16 cur_conf;
68         u8 clock;
69         u8 parity, word_7bits;
70
71         unsigned long uart_flags;
72
73         /* console related */
74         struct circ_buf con_xmit;
75
76         /* irq related */
77         u16 irq;
78 };
79
80 /* global data structure, may need be removed */
81 struct uart_max3110 *pmax;
82 static inline void receive_char(struct uart_max3110 *max, u8 ch);
83 static void receive_chars(struct uart_max3110 *max,
84                                 unsigned char *str, int len);
85 static int max3110_read_multi(struct uart_max3110 *max, int len, u8 *buf);
86 static void max3110_console_receive(struct uart_max3110 *max);
87
88 int max3110_write_then_read(struct uart_max3110 *max,
89                 const u8 *txbuf, u8 *rxbuf, unsigned len, int always_fast)
90 {
91         struct spi_device *spi = max->spi;
92         struct spi_message      message;
93         struct spi_transfer     x;
94         int ret;
95
96         if (!txbuf || !rxbuf)
97                 return -EINVAL;
98
99         spi_message_init(&message);
100         memset(&x, 0, sizeof x);
101         x.len = len;
102         x.tx_buf = txbuf;
103         x.rx_buf = rxbuf;
104         spi_message_add_tail(&x, &message);
105
106         if (always_fast)
107                 x.speed_hz = 3125000;
108         else if (max->baud)
109                 x.speed_hz = max->baud;
110
111         /* Do the i/o */
112         ret = spi_sync(spi, &message);
113         return ret;
114 }
115
116 /* Write a u16 to the device, and return one u16 read back */
117 int max3110_out(struct uart_max3110 *max, const u16 out)
118 {
119         u16 tmp;
120         int ret;
121
122         ret = max3110_write_then_read(max, (u8 *)&out, (u8 *)&tmp, 2, 1);
123         if (ret)
124                 return ret;
125
126         /* If some valid data is read back */
127         if (tmp & MAX3110_READ_DATA_AVAILABLE)
128                 receive_char(max, (tmp & 0xff));
129
130         return ret;
131 }
132
133 #define MAX_READ_LEN    20
134 /*
135  * This is usually used to read data from SPIC RX FIFO, which doesn't
136  * need any delay like flushing character out. It returns how many
137  * valide bytes are read back
138  */
139 static int max3110_read_multi(struct uart_max3110 *max, int len, u8 *buf)
140 {
141         u16 out[MAX_READ_LEN], in[MAX_READ_LEN];
142         u8 *pbuf, valid_str[MAX_READ_LEN];
143         int i, j, bytelen;
144
145         if (len > MAX_READ_LEN) {
146                 pr_err(PR_FMT "read len %d is too large\n", len);
147                 return 0;
148         }
149
150         bytelen = len * 2;
151         memset(out, 0, bytelen);
152         memset(in, 0, bytelen);
153
154         if (max3110_write_then_read(max, (u8 *)out, (u8 *)in, bytelen, 1))
155                 return 0;
156
157         /* If caller don't provide a buffer, then handle received char */
158         pbuf = buf ? buf : valid_str;
159
160         for (i = 0, j = 0; i < len; i++) {
161                 if (in[i] & MAX3110_READ_DATA_AVAILABLE)
162                         pbuf[j++] = (u8)(in[i] & 0xff);
163         }
164
165         if (j && (pbuf == valid_str))
166                 receive_chars(max, valid_str, j);
167
168         return j;
169 }
170
171 static void serial_m3110_con_putchar(struct uart_port *port, int ch)
172 {
173         struct uart_max3110 *max =
174                 container_of(port, struct uart_max3110, port);
175         struct circ_buf *xmit = &max->con_xmit;
176
177         if (uart_circ_chars_free(xmit)) {
178                 xmit->buf[xmit->head] = (char)ch;
179                 xmit->head = (xmit->head + 1) & (PAGE_SIZE - 1);
180         }
181
182
183         if (!test_and_set_bit(CON_TX_NEEDED, &max->uart_flags))
184                 wake_up_process(max->main_thread);
185 }
186
187 /*
188  * Print a string to the serial port trying not to disturb
189  * any possible real use of the port...
190  *
191  *      The console_lock must be held when we get here.
192  */
193 static void serial_m3110_con_write(struct console *co,
194                                 const char *s, unsigned int count)
195 {
196         if (!pmax)
197                 return;
198
199         uart_console_write(&pmax->port, s, count, serial_m3110_con_putchar);
200 }
201
202 static int __init
203 serial_m3110_con_setup(struct console *co, char *options)
204 {
205         struct uart_max3110 *max = pmax;
206         int baud = 115200;
207         int bits = 8;
208         int parity = 'n';
209         int flow = 'n';
210
211         pr_info(PR_FMT "setting up console\n");
212
213         if (!max) {
214                 pr_err(PR_FMT "pmax is NULL, return");
215                 return -ENODEV;
216         }
217
218         if (options)
219                 uart_parse_options(options, &baud, &parity, &bits, &flow);
220
221         return uart_set_options(&max->port, co, baud, parity, bits, flow);
222 }
223
224 static struct tty_driver *serial_m3110_con_device(struct console *co,
225                                                         int *index)
226 {
227         struct uart_driver *p = co->data;
228         *index = co->index;
229         return p->tty_driver;
230 }
231
232 static struct uart_driver serial_m3110_reg;
233 static struct console serial_m3110_console = {
234         .name           = "ttyS",
235         .write          = serial_m3110_con_write,
236         .device         = serial_m3110_con_device,
237         .setup          = serial_m3110_con_setup,
238         .flags          = CON_PRINTBUFFER,
239         .index          = -1,
240         .data           = &serial_m3110_reg,
241 };
242
243 #define MRST_CONSOLE    (&serial_m3110_console)
244
245 static unsigned int serial_m3110_tx_empty(struct uart_port *port)
246 {
247         return 1;
248 }
249
250 static void serial_m3110_stop_tx(struct uart_port *port)
251 {
252         return;
253 }
254
255 /* stop_rx will be called in spin_lock env */
256 static void serial_m3110_stop_rx(struct uart_port *port)
257 {
258         return;
259 }
260
261 #define WORDS_PER_XFER  128
262 static inline void send_circ_buf(struct uart_max3110 *max,
263                                 struct circ_buf *xmit)
264 {
265         int len, left = 0;
266         u16 obuf[WORDS_PER_XFER], ibuf[WORDS_PER_XFER];
267         u8 valid_str[WORDS_PER_XFER];
268         int i, j;
269
270         while (!uart_circ_empty(xmit)) {
271                 left = uart_circ_chars_pending(xmit);
272                 while (left) {
273                         len = (left >= WORDS_PER_XFER) ? WORDS_PER_XFER : left;
274
275                         memset(obuf, 0, len * 2);
276                         memset(ibuf, 0, len * 2);
277                         for (i = 0; i < len; i++) {
278                                 obuf[i] = (u8)xmit->buf[xmit->tail] | WD_TAG;
279                                 xmit->tail = (xmit->tail + 1) &
280                                                 (UART_XMIT_SIZE - 1);
281                         }
282                         max3110_write_then_read(max, (u8 *)obuf,
283                                                 (u8 *)ibuf, len * 2, 0);
284
285                         for (i = 0, j = 0; i < len; i++) {
286                                 if (ibuf[i] & MAX3110_READ_DATA_AVAILABLE)
287                                         valid_str[j++] = (u8)(ibuf[i] & 0xff);
288                         }
289
290                         if (j)
291                                 receive_chars(max, valid_str, j);
292
293                         max->port.icount.tx += len;
294                         left -= len;
295                 }
296         }
297 }
298
299 static void transmit_char(struct uart_max3110 *max)
300 {
301         struct uart_port *port = &max->port;
302         struct circ_buf *xmit = &port->state->xmit;
303
304         if (uart_circ_empty(xmit) || uart_tx_stopped(port))
305                 return;
306
307         send_circ_buf(max, xmit);
308
309         if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
310                 uart_write_wakeup(port);
311
312         if (uart_circ_empty(xmit))
313                 serial_m3110_stop_tx(port);
314 }
315
316 /* This will be called by uart_write() and tty_write, can't
317  * go to sleep */
318 static void serial_m3110_start_tx(struct uart_port *port)
319 {
320         struct uart_max3110 *max =
321                 container_of(port, struct uart_max3110, port);
322
323         if (!test_and_set_bit(UART_TX_NEEDED, &max->uart_flags))
324                 wake_up_process(max->main_thread);
325 }
326
327 static void receive_chars(struct uart_max3110 *max, unsigned char *str, int len)
328 {
329         struct uart_port *port = &max->port;
330         struct tty_struct *tty;
331         int usable;
332
333         /* If uart is not opened, just return */
334         if (!port->state)
335                 return;
336
337         tty = port->state->port.tty;
338         if (!tty)
339                 return; /* receive some char before the tty is opened */
340
341         while (len) {
342                 usable = tty_buffer_request_room(tty, len);
343                 if (usable) {
344                         tty_insert_flip_string(tty, str, usable);
345                         str += usable;
346                         port->icount.rx += usable;
347                         tty_flip_buffer_push(tty);
348                 }
349                 len -= usable;
350         }
351 }
352
353 static inline void receive_char(struct uart_max3110 *max, u8 ch)
354 {
355         receive_chars(max, &ch, 1);
356 }
357
358 static void max3110_console_receive(struct uart_max3110 *max)
359 {
360         int loop = 1, num, total = 0;
361         u8 recv_buf[512], *pbuf;
362
363         pbuf = recv_buf;
364         do {
365                 num = max3110_read_multi(max, 8, pbuf);
366
367                 if (num) {
368                         loop = 10;
369                         pbuf += num;
370                         total += num;
371
372                         if (total >= 500) {
373                                 receive_chars(max, recv_buf, total);
374                                 pbuf = recv_buf;
375                                 total = 0;
376                         }
377                 }
378         } while (--loop);
379
380         if (total)
381                 receive_chars(max, recv_buf, total);
382 }
383
384 static int max3110_main_thread(void *_max)
385 {
386         struct uart_max3110 *max = _max;
387         wait_queue_head_t *wq = &max->wq;
388         int ret = 0;
389         struct circ_buf *xmit = &max->con_xmit;
390
391         init_waitqueue_head(wq);
392         pr_info(PR_FMT "start main thread\n");
393
394         do {
395                 wait_event_interruptible(*wq, max->uart_flags || kthread_should_stop());
396
397                 mutex_lock(&max->thread_mutex);
398
399                 if (test_and_clear_bit(BIT_IRQ_PENDING, &max->uart_flags))
400                         max3110_console_receive(max);
401
402                 /* first handle console output */
403                 if (test_and_clear_bit(CON_TX_NEEDED, &max->uart_flags))
404                         send_circ_buf(max, xmit);
405
406                 /* handle uart output */
407                 if (test_and_clear_bit(UART_TX_NEEDED, &max->uart_flags))
408                         transmit_char(max);
409
410                 mutex_unlock(&max->thread_mutex);
411
412         } while (!kthread_should_stop());
413
414         return ret;
415 }
416
417 #ifdef CONFIG_MRST_MAX3110_IRQ
418 static irqreturn_t serial_m3110_irq(int irq, void *dev_id)
419 {
420         struct uart_max3110 *max = dev_id;
421
422         /* max3110's irq is a falling edge, not level triggered,
423          * so no need to disable the irq */
424         if (!test_and_set_bit(BIT_IRQ_PENDING, &max->uart_flags))
425                 wake_up_process(max->main_thread);
426
427         return IRQ_HANDLED;
428 }
429 #else
430 /* if don't use RX IRQ, then need a thread to polling read */
431 static int max3110_read_thread(void *_max)
432 {
433         struct uart_max3110 *max = _max;
434
435         pr_info(PR_FMT "start read thread\n");
436         do {
437                 mutex_lock(&max->thread_mutex);
438                 max3110_console_receive(max);
439                 mutex_unlock(&max->thread_mutex);
440
441                 set_current_state(TASK_INTERRUPTIBLE);
442                 schedule_timeout(HZ / 20);
443         } while (!kthread_should_stop());
444
445         return 0;
446 }
447 #endif
448
449 static int serial_m3110_startup(struct uart_port *port)
450 {
451         struct uart_max3110 *max =
452                 container_of(port, struct uart_max3110, port);
453         u16 config = 0;
454         int ret = 0;
455
456         if (port->line != 0)
457                 pr_err(PR_FMT "uart port startup failed\n");
458
459         /* firstly disable all IRQ and config it to 115200, 8n1 */
460         config = WC_TAG | WC_FIFO_ENABLE
461                         | WC_1_STOPBITS
462                         | WC_8BIT_WORD
463                         | WC_BAUD_DR2;
464         ret = max3110_out(max, config);
465
466         /* as we use thread to handle tx/rx, need set low latency */
467         port->state->port.tty->low_latency = 1;
468
469 #ifdef CONFIG_MRST_MAX3110_IRQ
470         ret = request_irq(max->irq, serial_m3110_irq,
471                                 IRQ_TYPE_EDGE_FALLING, "max3110", max);
472         if (ret)
473                 return ret;
474
475         /* enable RX IRQ only */
476         config |= WC_RXA_IRQ_ENABLE;
477         max3110_out(max, config);
478 #else
479         /* if IRQ is disabled, start a read thread for input data */
480         max->read_thread =
481                 kthread_run(max3110_read_thread, max, "max3110_read");
482 #endif
483
484         max->cur_conf = config;
485         return 0;
486 }
487
488 static void serial_m3110_shutdown(struct uart_port *port)
489 {
490         struct uart_max3110 *max =
491                 container_of(port, struct uart_max3110, port);
492         u16 config;
493
494         if (max->read_thread) {
495                 kthread_stop(max->read_thread);
496                 max->read_thread = NULL;
497         }
498
499 #ifdef CONFIG_MRST_MAX3110_IRQ
500         free_irq(max->irq, max);
501 #endif
502
503         /* Disable interrupts from this port */
504         config = WC_TAG | WC_SW_SHDI;
505         max3110_out(max, config);
506 }
507
508 static void serial_m3110_release_port(struct uart_port *port)
509 {
510 }
511
512 static int serial_m3110_request_port(struct uart_port *port)
513 {
514         return 0;
515 }
516
517 static void serial_m3110_config_port(struct uart_port *port, int flags)
518 {
519         /* give it fake type */
520         port->type = PORT_PXA;
521 }
522
523 static int
524 serial_m3110_verify_port(struct uart_port *port, struct serial_struct *ser)
525 {
526         /* we don't want the core code to modify any port params */
527         return -EINVAL;
528 }
529
530
531 static const char *serial_m3110_type(struct uart_port *port)
532 {
533         struct uart_max3110 *max =
534                 container_of(port, struct uart_max3110, port);
535         return max->name;
536 }
537
538 static void
539 serial_m3110_set_termios(struct uart_port *port, struct ktermios *termios,
540                        struct ktermios *old)
541 {
542         struct uart_max3110 *max =
543                 container_of(port, struct uart_max3110, port);
544         unsigned char cval;
545         unsigned int baud, parity = 0;
546         int clk_div = -1;
547         u16 new_conf = max->cur_conf;
548
549         switch (termios->c_cflag & CSIZE) {
550         case CS7:
551                 cval = UART_LCR_WLEN7;
552                 new_conf |= WC_7BIT_WORD;
553                 break;
554         default:
555         case CS8:
556                 cval = UART_LCR_WLEN8;
557                 new_conf |= WC_8BIT_WORD;
558                 break;
559         }
560
561         baud = uart_get_baud_rate(port, termios, old, 0, 230400);
562
563         /* first calc the div for 1.8MHZ clock case */
564         switch (baud) {
565         case 300:
566                 clk_div = WC_BAUD_DR384;
567                 break;
568         case 600:
569                 clk_div = WC_BAUD_DR192;
570                 break;
571         case 1200:
572                 clk_div = WC_BAUD_DR96;
573                 break;
574         case 2400:
575                 clk_div = WC_BAUD_DR48;
576                 break;
577         case 4800:
578                 clk_div = WC_BAUD_DR24;
579                 break;
580         case 9600:
581                 clk_div = WC_BAUD_DR12;
582                 break;
583         case 19200:
584                 clk_div = WC_BAUD_DR6;
585                 break;
586         case 38400:
587                 clk_div = WC_BAUD_DR3;
588                 break;
589         case 57600:
590                 clk_div = WC_BAUD_DR2;
591                 break;
592         case 115200:
593                 clk_div = WC_BAUD_DR1;
594                 break;
595         case 230400:
596                 if (max->clock & MAX3110_HIGH_CLK)
597                         break;
598         default:
599                 /* pick the previous baud rate */
600                 baud = max->baud;
601                 clk_div = max->cur_conf & WC_BAUD_DIV_MASK;
602                 tty_termios_encode_baud_rate(termios, baud, baud);
603         }
604
605         if (max->clock & MAX3110_HIGH_CLK) {
606                 clk_div += 1;
607                 /* high clk version max3110 doesn't support B300 */
608                 if (baud == 300)
609                         baud = 600;
610                 if (baud == 230400)
611                         clk_div = WC_BAUD_DR1;
612                 tty_termios_encode_baud_rate(termios, baud, baud);
613         }
614
615         new_conf = (new_conf & ~WC_BAUD_DIV_MASK) | clk_div;
616         if (termios->c_cflag & CSTOPB)
617                 new_conf |= WC_2_STOPBITS;
618         else
619                 new_conf &= ~WC_2_STOPBITS;
620
621         if (termios->c_cflag & PARENB) {
622                 new_conf |= WC_PARITY_ENABLE;
623                 parity |= UART_LCR_PARITY;
624         } else
625                 new_conf &= ~WC_PARITY_ENABLE;
626
627         if (!(termios->c_cflag & PARODD))
628                 parity |= UART_LCR_EPAR;
629         max->parity = parity;
630
631         uart_update_timeout(port, termios->c_cflag, baud);
632
633         new_conf |= WC_TAG;
634         if (new_conf != max->cur_conf) {
635                 max3110_out(max, new_conf);
636                 max->cur_conf = new_conf;
637                 max->baud = baud;
638         }
639 }
640
641 /* don't handle hw handshaking */
642 static unsigned int serial_m3110_get_mctrl(struct uart_port *port)
643 {
644         return TIOCM_DSR | TIOCM_CAR | TIOCM_DSR;
645 }
646
647 static void serial_m3110_set_mctrl(struct uart_port *port, unsigned int mctrl)
648 {
649 }
650
651 static void serial_m3110_break_ctl(struct uart_port *port, int break_state)
652 {
653 }
654
655 static void serial_m3110_pm(struct uart_port *port, unsigned int state,
656                         unsigned int oldstate)
657 {
658 }
659
660 static void serial_m3110_enable_ms(struct uart_port *port)
661 {
662 }
663
664 struct uart_ops serial_m3110_ops = {
665         .tx_empty       = serial_m3110_tx_empty,
666         .set_mctrl      = serial_m3110_set_mctrl,
667         .get_mctrl      = serial_m3110_get_mctrl,
668         .stop_tx        = serial_m3110_stop_tx,
669         .start_tx       = serial_m3110_start_tx,
670         .stop_rx        = serial_m3110_stop_rx,
671         .enable_ms      = serial_m3110_enable_ms,
672         .break_ctl      = serial_m3110_break_ctl,
673         .startup        = serial_m3110_startup,
674         .shutdown       = serial_m3110_shutdown,
675         .set_termios    = serial_m3110_set_termios,     /* must have */
676         .pm             = serial_m3110_pm,
677         .type           = serial_m3110_type,
678         .release_port   = serial_m3110_release_port,
679         .request_port   = serial_m3110_request_port,
680         .config_port    = serial_m3110_config_port,
681         .verify_port    = serial_m3110_verify_port,
682 };
683
684 static struct uart_driver serial_m3110_reg = {
685         .owner          = THIS_MODULE,
686         .driver_name    = "MRST serial",
687         .dev_name       = "ttyS",
688         .major          = TTY_MAJOR,
689         .minor          = 64,
690         .nr             = 1,
691         .cons           = MRST_CONSOLE,
692 };
693
694 static int serial_m3110_suspend(struct spi_device *spi, pm_message_t state)
695 {
696         return 0;
697 }
698
699 static int serial_m3110_resume(struct spi_device *spi)
700 {
701         return 0;
702 }
703
704 static struct dw_spi_chip spi0_uart = {
705         .poll_mode = 1,
706         .enable_dma = 0,
707         .type = SPI_FRF_SPI,
708 };
709
710 static int serial_m3110_probe(struct spi_device *spi)
711 {
712         struct uart_max3110 *max;
713         int ret;
714         unsigned char *buffer;
715         u16 res;
716         max = kzalloc(sizeof(*max), GFP_KERNEL);
717         if (!max)
718                 return -ENOMEM;
719
720         /* set spi info */
721         spi->mode = SPI_MODE_0;
722         spi->bits_per_word = 16;
723         max->clock = MAX3110_HIGH_CLK;
724         spi->controller_data = &spi0_uart;
725
726         spi_setup(spi);
727
728         max->port.type = PORT_PXA;      /* need apply for a max3110 type */
729         max->port.fifosize = 2;         /* only have 16b buffer */
730         max->port.ops = &serial_m3110_ops;
731         max->port.line = 0;
732         max->port.dev = &spi->dev;
733         max->port.uartclk = 115200;
734
735         max->spi = spi;
736         max->name = spi->modalias;      /* use spi name as the name */
737         max->irq = (u16)spi->irq;
738
739         mutex_init(&max->thread_mutex);
740
741         max->word_7bits = 0;
742         max->parity = 0;
743         max->baud = 0;
744
745         max->cur_conf = 0;
746         max->uart_flags = 0;
747
748         /* Check if reading configuration register returns something sane */
749
750         res = RC_TAG;
751         ret = max3110_write_then_read(max, (u8 *)&res, (u8 *)&res, 2, 0);
752         if (ret < 0 || res == 0 || res == 0xffff) {
753                 printk(KERN_ERR "MAX3111 deemed not present (conf reg %04x)",
754                                                                         res);
755                 ret = -ENODEV;
756                 goto err_get_page;
757         }
758         buffer = (unsigned char *)__get_free_page(GFP_KERNEL);
759         if (!buffer) {
760                 ret = -ENOMEM;
761                 goto err_get_page;
762         }
763         max->con_xmit.buf = (unsigned char *)buffer;
764         max->con_xmit.head = max->con_xmit.tail = 0;
765
766         max->main_thread = kthread_run(max3110_main_thread,
767                                         max, "max3110_main");
768         if (IS_ERR(max->main_thread)) {
769                 ret = PTR_ERR(max->main_thread);
770                 goto err_kthread;
771         }
772
773         pmax = max;
774         /* give membase a psudo value to pass serial_core's check */
775         max->port.membase = (void *)0xff110000;
776         uart_add_one_port(&serial_m3110_reg, &max->port);
777
778         return 0;
779
780 err_kthread:
781         free_page((unsigned long)buffer);
782 err_get_page:
783         pmax = NULL;
784         kfree(max);
785         return ret;
786 }
787
788 static int max3110_remove(struct spi_device *dev)
789 {
790         struct uart_max3110 *max = pmax;
791
792         if (!pmax)
793                 return 0;
794
795         pmax = NULL;
796         uart_remove_one_port(&serial_m3110_reg, &max->port);
797
798         free_page((unsigned long)max->con_xmit.buf);
799
800         if (max->main_thread)
801                 kthread_stop(max->main_thread);
802
803         kfree(max);
804         return 0;
805 }
806
807 static struct spi_driver uart_max3110_driver = {
808         .driver = {
809                         .name   = "spi_max3111",
810                         .bus    = &spi_bus_type,
811                         .owner  = THIS_MODULE,
812         },
813         .probe          = serial_m3110_probe,
814         .remove         = __devexit_p(max3110_remove),
815         .suspend        = serial_m3110_suspend,
816         .resume         = serial_m3110_resume,
817 };
818
819
820 int __init serial_m3110_init(void)
821 {
822         int ret = 0;
823
824         ret = uart_register_driver(&serial_m3110_reg);
825         if (ret)
826                 return ret;
827
828         ret = spi_register_driver(&uart_max3110_driver);
829         if (ret)
830                 uart_unregister_driver(&serial_m3110_reg);
831
832         return ret;
833 }
834
835 void __exit serial_m3110_exit(void)
836 {
837         spi_unregister_driver(&uart_max3110_driver);
838         uart_unregister_driver(&serial_m3110_reg);
839 }
840
841 module_init(serial_m3110_init);
842 module_exit(serial_m3110_exit);
843
844 MODULE_LICENSE("GPL");
845 MODULE_ALIAS("max3110-uart");