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