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