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 = {
24 static struct spi_board_info spi_board_info[] = {
26 .modalias = "max3100",
27 .platform_data = &max3100_plat_data,
29 .max_speed_hz = 5*1000*1000,
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 */
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>
51 #include <linux/tty.h>
52 #include <linux/tty_flip.h>
54 #include <linux/serial_max3100.h>
56 #define MAX3100_C (1<<14)
57 #define MAX3100_D (0<<14)
58 #define MAX3100_W (1<<15)
59 #define MAX3100_RX (0<<15)
61 #define MAX3100_WC (MAX3100_W | MAX3100_C)
62 #define MAX3100_RC (MAX3100_RX | MAX3100_C)
63 #define MAX3100_WD (MAX3100_W | MAX3100_D)
64 #define MAX3100_RD (MAX3100_RX | MAX3100_D)
65 #define MAX3100_CMD (3 << 14)
67 #define MAX3100_T (1<<14)
68 #define MAX3100_R (1<<15)
70 #define MAX3100_FEN (1<<13)
71 #define MAX3100_SHDN (1<<12)
72 #define MAX3100_TM (1<<11)
73 #define MAX3100_RM (1<<10)
74 #define MAX3100_PM (1<<9)
75 #define MAX3100_RAM (1<<8)
76 #define MAX3100_IR (1<<7)
77 #define MAX3100_ST (1<<6)
78 #define MAX3100_PE (1<<5)
79 #define MAX3100_L (1<<4)
80 #define MAX3100_BAUD (0xf)
82 #define MAX3100_TE (1<<10)
83 #define MAX3100_RAFE (1<<10)
84 #define MAX3100_RTS (1<<9)
85 #define MAX3100_CTS (1<<9)
86 #define MAX3100_PT (1<<8)
87 #define MAX3100_DATA (0xff)
89 #define MAX3100_RT (MAX3100_R | MAX3100_T)
90 #define MAX3100_RTC (MAX3100_RT | MAX3100_CTS | MAX3100_RAFE)
92 /* the following simulate a status reg for ignore_status_mask */
93 #define MAX3100_STATUS_PE 1
94 #define MAX3100_STATUS_FE 2
95 #define MAX3100_STATUS_OE 4
98 struct uart_port port;
99 struct spi_device *spi;
101 int cts; /* last CTS received for flow ctrl */
102 int tx_empty; /* last TX empty bit */
104 spinlock_t conf_lock; /* shared data */
105 int conf_commit; /* need to make changes */
106 int conf; /* configuration for the MAX31000
107 * (bits 0-7, bits 8-11 are irqs) */
108 int rts_commit; /* need to change rts */
109 int rts; /* rts status */
110 int baud; /* current baud rate */
112 int parity; /* keeps track if we should send parity */
113 #define MAX3100_PARITY_ON 1
114 #define MAX3100_PARITY_ODD 2
115 #define MAX3100_7BIT 4
116 int rx_enabled; /* if we should rx chars */
118 int irq; /* irq assigned to the max3100 */
120 int minor; /* minor number */
121 int crystal; /* 1 if 3.6864Mhz crystal 0 for 1.8432 */
122 int loopback; /* 1 if we are in loopback mode */
124 /* for handling irqs: need workqueue since we do spi_sync */
125 struct workqueue_struct *workqueue;
126 struct work_struct work;
127 /* set to 1 to make the workhandler exit as soon as possible */
129 /* need to know we are suspending to avoid deadlock on workqueue */
132 /* hook for suspending MAX3100 via dedicated pin */
133 void (*max3100_hw_suspend) (int suspend);
135 /* poll time (in ms) for ctrl lines */
138 struct timer_list timer;
141 static struct max3100_port *max3100s[MAX_MAX3100]; /* the chips */
142 static DEFINE_MUTEX(max3100s_lock); /* race on probe */
144 static int max3100_do_parity(struct max3100_port *s, u16 c)
148 if (s->parity & MAX3100_PARITY_ODD)
153 if (s->parity & MAX3100_7BIT)
158 parity = parity ^ (hweight8(c) & 1);
162 static int max3100_check_parity(struct max3100_port *s, u16 c)
164 return max3100_do_parity(s, c) == ((c >> 8) & 1);
167 static void max3100_calc_parity(struct max3100_port *s, u16 *c)
169 if (s->parity & MAX3100_7BIT)
174 if (s->parity & MAX3100_PARITY_ON)
175 *c |= max3100_do_parity(s, *c) << 8;
178 static void max3100_work(struct work_struct *w);
180 static void max3100_dowork(struct max3100_port *s)
182 if (!s->force_end_work && !freezing(current) && !s->suspending)
183 queue_work(s->workqueue, &s->work);
186 static void max3100_timeout(unsigned long data)
188 struct max3100_port *s = (struct max3100_port *)data;
192 mod_timer(&s->timer, jiffies + s->poll_time);
196 static int max3100_sr(struct max3100_port *s, u16 tx, u16 *rx)
198 struct spi_message message;
201 struct spi_transfer tran = {
207 etx = cpu_to_be16(tx);
208 spi_message_init(&message);
209 spi_message_add_tail(&tran, &message);
210 status = spi_sync(s->spi, &message);
212 dev_warn(&s->spi->dev, "error while calling spi_sync\n");
215 *rx = be16_to_cpu(erx);
216 s->tx_empty = (*rx & MAX3100_T) > 0;
217 dev_dbg(&s->spi->dev, "%04x - %04x\n", tx, *rx);
221 static int max3100_handlerx(struct max3100_port *s, u16 rx)
223 unsigned int ch, flg, status = 0;
226 if (rx & MAX3100_R && s->rx_enabled) {
227 dev_dbg(&s->spi->dev, "%s\n", __func__);
228 ch = rx & (s->parity & MAX3100_7BIT ? 0x7f : 0xff);
229 if (rx & MAX3100_RAFE) {
230 s->port.icount.frame++;
232 status |= MAX3100_STATUS_FE;
234 if (s->parity & MAX3100_PARITY_ON) {
235 if (max3100_check_parity(s, rx)) {
239 s->port.icount.parity++;
241 status |= MAX3100_STATUS_PE;
248 uart_insert_char(&s->port, status, MAX3100_STATUS_OE, ch, flg);
252 cts = (rx & MAX3100_CTS) > 0;
255 uart_handle_cts_change(&s->port, cts ? TIOCM_CTS : 0);
261 static void max3100_work(struct work_struct *w)
263 struct max3100_port *s = container_of(w, struct max3100_port, work);
266 int conf, cconf, rts, crts;
267 struct circ_buf *xmit = &s->port.state->xmit;
269 dev_dbg(&s->spi->dev, "%s\n", __func__);
273 spin_lock(&s->conf_lock);
275 cconf = s->conf_commit;
278 crts = s->rts_commit;
280 spin_unlock(&s->conf_lock);
282 max3100_sr(s, MAX3100_WC | conf, &rx);
284 max3100_sr(s, MAX3100_WD | MAX3100_TE |
285 (s->rts ? MAX3100_RTS : 0), &rx);
286 rxchars += max3100_handlerx(s, rx);
289 max3100_sr(s, MAX3100_RD, &rx);
290 rxchars += max3100_handlerx(s, rx);
292 if (rx & MAX3100_T) {
294 if (s->port.x_char) {
298 } else if (!uart_circ_empty(xmit) &&
299 !uart_tx_stopped(&s->port)) {
300 tx = xmit->buf[xmit->tail];
301 xmit->tail = (xmit->tail + 1) &
302 (UART_XMIT_SIZE - 1);
306 max3100_calc_parity(s, &tx);
307 tx |= MAX3100_WD | (s->rts ? MAX3100_RTS : 0);
308 max3100_sr(s, tx, &rx);
309 rxchars += max3100_handlerx(s, rx);
314 tty_flip_buffer_push(&s->port.state->port);
317 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
318 uart_write_wakeup(&s->port);
320 } while (!s->force_end_work &&
321 !freezing(current) &&
323 (!uart_circ_empty(xmit) &&
324 !uart_tx_stopped(&s->port))));
327 tty_flip_buffer_push(&s->port.state->port);
330 static irqreturn_t max3100_irq(int irqno, void *dev_id)
332 struct max3100_port *s = dev_id;
334 dev_dbg(&s->spi->dev, "%s\n", __func__);
340 static void max3100_enable_ms(struct uart_port *port)
342 struct max3100_port *s = container_of(port,
346 if (s->poll_time > 0)
347 mod_timer(&s->timer, jiffies);
348 dev_dbg(&s->spi->dev, "%s\n", __func__);
351 static void max3100_start_tx(struct uart_port *port)
353 struct max3100_port *s = container_of(port,
357 dev_dbg(&s->spi->dev, "%s\n", __func__);
362 static void max3100_stop_rx(struct uart_port *port)
364 struct max3100_port *s = container_of(port,
368 dev_dbg(&s->spi->dev, "%s\n", __func__);
371 spin_lock(&s->conf_lock);
372 s->conf &= ~MAX3100_RM;
374 spin_unlock(&s->conf_lock);
378 static unsigned int max3100_tx_empty(struct uart_port *port)
380 struct max3100_port *s = container_of(port,
384 dev_dbg(&s->spi->dev, "%s\n", __func__);
386 /* may not be truly up-to-date */
391 static unsigned int max3100_get_mctrl(struct uart_port *port)
393 struct max3100_port *s = container_of(port,
397 dev_dbg(&s->spi->dev, "%s\n", __func__);
399 /* may not be truly up-to-date */
401 /* always assert DCD and DSR since these lines are not wired */
402 return (s->cts ? TIOCM_CTS : 0) | TIOCM_DSR | TIOCM_CAR;
405 static void max3100_set_mctrl(struct uart_port *port, unsigned int mctrl)
407 struct max3100_port *s = container_of(port,
412 dev_dbg(&s->spi->dev, "%s\n", __func__);
414 rts = (mctrl & TIOCM_RTS) > 0;
416 spin_lock(&s->conf_lock);
422 spin_unlock(&s->conf_lock);
426 max3100_set_termios(struct uart_port *port, struct ktermios *termios,
427 struct ktermios *old)
429 struct max3100_port *s = container_of(port,
434 u32 param_new, param_mask, parity = 0;
436 dev_dbg(&s->spi->dev, "%s\n", __func__);
438 cflag = termios->c_cflag;
442 baud = tty_termios_baud_rate(termios);
443 param_new = s->conf & MAX3100_BAUD;
452 param_new = 14 + s->crystal;
455 param_new = 13 + s->crystal;
458 param_new = 12 + s->crystal;
461 param_new = 11 + s->crystal;
464 param_new = 10 + s->crystal;
467 param_new = 9 + s->crystal;
470 param_new = 8 + s->crystal;
473 param_new = 1 + s->crystal;
476 param_new = 0 + s->crystal;
487 tty_termios_encode_baud_rate(termios, baud, baud);
489 param_mask |= MAX3100_BAUD;
491 if ((cflag & CSIZE) == CS8) {
492 param_new &= ~MAX3100_L;
493 parity &= ~MAX3100_7BIT;
495 param_new |= MAX3100_L;
496 parity |= MAX3100_7BIT;
497 cflag = (cflag & ~CSIZE) | CS7;
499 param_mask |= MAX3100_L;
502 param_new |= MAX3100_ST;
504 param_new &= ~MAX3100_ST;
505 param_mask |= MAX3100_ST;
507 if (cflag & PARENB) {
508 param_new |= MAX3100_PE;
509 parity |= MAX3100_PARITY_ON;
511 param_new &= ~MAX3100_PE;
512 parity &= ~MAX3100_PARITY_ON;
514 param_mask |= MAX3100_PE;
517 parity |= MAX3100_PARITY_ODD;
519 parity &= ~MAX3100_PARITY_ODD;
521 /* mask termios capabilities we don't support */
523 termios->c_cflag = cflag;
525 s->port.ignore_status_mask = 0;
526 if (termios->c_iflag & IGNPAR)
527 s->port.ignore_status_mask |=
528 MAX3100_STATUS_PE | MAX3100_STATUS_FE |
531 /* we are sending char from a workqueue so enable */
532 s->port.state->port.low_latency = 1;
534 if (s->poll_time > 0)
535 del_timer_sync(&s->timer);
537 uart_update_timeout(port, termios->c_cflag, baud);
539 spin_lock(&s->conf_lock);
540 s->conf = (s->conf & ~param_mask) | (param_new & param_mask);
543 spin_unlock(&s->conf_lock);
546 if (UART_ENABLE_MS(&s->port, termios->c_cflag))
547 max3100_enable_ms(&s->port);
550 static void max3100_shutdown(struct uart_port *port)
552 struct max3100_port *s = container_of(port,
556 dev_dbg(&s->spi->dev, "%s\n", __func__);
561 s->force_end_work = 1;
563 if (s->poll_time > 0)
564 del_timer_sync(&s->timer);
567 flush_workqueue(s->workqueue);
568 destroy_workqueue(s->workqueue);
574 /* set shutdown mode to save power */
575 if (s->max3100_hw_suspend)
576 s->max3100_hw_suspend(1);
580 tx = MAX3100_WC | MAX3100_SHDN;
581 max3100_sr(s, tx, &rx);
585 static int max3100_startup(struct uart_port *port)
587 struct max3100_port *s = container_of(port,
592 dev_dbg(&s->spi->dev, "%s\n", __func__);
594 s->conf = MAX3100_RM;
595 s->baud = s->crystal ? 230400 : 115200;
601 s->force_end_work = 0;
605 sprintf(b, "max3100-%d", s->minor);
606 s->workqueue = create_freezable_workqueue(b);
608 dev_warn(&s->spi->dev, "cannot create workqueue\n");
611 INIT_WORK(&s->work, max3100_work);
613 if (request_irq(s->irq, max3100_irq,
614 IRQF_TRIGGER_FALLING, "max3100", s) < 0) {
615 dev_warn(&s->spi->dev, "cannot allocate irq %d\n", s->irq);
617 destroy_workqueue(s->workqueue);
625 max3100_sr(s, tx, &rx);
628 if (s->max3100_hw_suspend)
629 s->max3100_hw_suspend(0);
632 /* wait for clock to settle */
635 max3100_enable_ms(&s->port);
640 static const char *max3100_type(struct uart_port *port)
642 struct max3100_port *s = container_of(port,
646 dev_dbg(&s->spi->dev, "%s\n", __func__);
648 return s->port.type == PORT_MAX3100 ? "MAX3100" : NULL;
651 static void max3100_release_port(struct uart_port *port)
653 struct max3100_port *s = container_of(port,
657 dev_dbg(&s->spi->dev, "%s\n", __func__);
660 static void max3100_config_port(struct uart_port *port, int flags)
662 struct max3100_port *s = container_of(port,
666 dev_dbg(&s->spi->dev, "%s\n", __func__);
668 if (flags & UART_CONFIG_TYPE)
669 s->port.type = PORT_MAX3100;
672 static int max3100_verify_port(struct uart_port *port,
673 struct serial_struct *ser)
675 struct max3100_port *s = container_of(port,
680 dev_dbg(&s->spi->dev, "%s\n", __func__);
682 if (ser->type == PORT_UNKNOWN || ser->type == PORT_MAX3100)
687 static void max3100_stop_tx(struct uart_port *port)
689 struct max3100_port *s = container_of(port,
693 dev_dbg(&s->spi->dev, "%s\n", __func__);
696 static int max3100_request_port(struct uart_port *port)
698 struct max3100_port *s = container_of(port,
702 dev_dbg(&s->spi->dev, "%s\n", __func__);
706 static void max3100_break_ctl(struct uart_port *port, int break_state)
708 struct max3100_port *s = container_of(port,
712 dev_dbg(&s->spi->dev, "%s\n", __func__);
715 static struct uart_ops max3100_ops = {
716 .tx_empty = max3100_tx_empty,
717 .set_mctrl = max3100_set_mctrl,
718 .get_mctrl = max3100_get_mctrl,
719 .stop_tx = max3100_stop_tx,
720 .start_tx = max3100_start_tx,
721 .stop_rx = max3100_stop_rx,
722 .enable_ms = max3100_enable_ms,
723 .break_ctl = max3100_break_ctl,
724 .startup = max3100_startup,
725 .shutdown = max3100_shutdown,
726 .set_termios = max3100_set_termios,
727 .type = max3100_type,
728 .release_port = max3100_release_port,
729 .request_port = max3100_request_port,
730 .config_port = max3100_config_port,
731 .verify_port = max3100_verify_port,
734 static struct uart_driver max3100_uart_driver = {
735 .owner = THIS_MODULE,
736 .driver_name = "ttyMAX",
737 .dev_name = "ttyMAX",
738 .major = MAX3100_MAJOR,
739 .minor = MAX3100_MINOR,
742 static int uart_driver_registered;
744 static int max3100_probe(struct spi_device *spi)
747 struct plat_max3100 *pdata;
750 mutex_lock(&max3100s_lock);
752 if (!uart_driver_registered) {
753 uart_driver_registered = 1;
754 retval = uart_register_driver(&max3100_uart_driver);
756 printk(KERN_ERR "Couldn't register max3100 uart driver\n");
757 mutex_unlock(&max3100s_lock);
762 for (i = 0; i < MAX_MAX3100; i++)
765 if (i == MAX_MAX3100) {
766 dev_warn(&spi->dev, "too many MAX3100 chips\n");
767 mutex_unlock(&max3100s_lock);
771 max3100s[i] = kzalloc(sizeof(struct max3100_port), GFP_KERNEL);
774 "kmalloc for max3100 structure %d failed!\n", i);
775 mutex_unlock(&max3100s_lock);
778 max3100s[i]->spi = spi;
779 max3100s[i]->irq = spi->irq;
780 spin_lock_init(&max3100s[i]->conf_lock);
781 spi_set_drvdata(spi, max3100s[i]);
782 pdata = dev_get_platdata(&spi->dev);
783 max3100s[i]->crystal = pdata->crystal;
784 max3100s[i]->loopback = pdata->loopback;
785 max3100s[i]->poll_time = pdata->poll_time * HZ / 1000;
786 if (pdata->poll_time > 0 && max3100s[i]->poll_time == 0)
787 max3100s[i]->poll_time = 1;
788 max3100s[i]->max3100_hw_suspend = pdata->max3100_hw_suspend;
789 max3100s[i]->minor = i;
790 init_timer(&max3100s[i]->timer);
791 max3100s[i]->timer.function = max3100_timeout;
792 max3100s[i]->timer.data = (unsigned long) max3100s[i];
794 dev_dbg(&spi->dev, "%s: adding port %d\n", __func__, i);
795 max3100s[i]->port.irq = max3100s[i]->irq;
796 max3100s[i]->port.uartclk = max3100s[i]->crystal ? 3686400 : 1843200;
797 max3100s[i]->port.fifosize = 16;
798 max3100s[i]->port.ops = &max3100_ops;
799 max3100s[i]->port.flags = UPF_SKIP_TEST | UPF_BOOT_AUTOCONF;
800 max3100s[i]->port.line = i;
801 max3100s[i]->port.type = PORT_MAX3100;
802 max3100s[i]->port.dev = &spi->dev;
803 retval = uart_add_one_port(&max3100_uart_driver, &max3100s[i]->port);
806 "uart_add_one_port failed for line %d with error %d\n",
809 /* set shutdown mode to save power. Will be woken-up on open */
810 if (max3100s[i]->max3100_hw_suspend)
811 max3100s[i]->max3100_hw_suspend(1);
813 tx = MAX3100_WC | MAX3100_SHDN;
814 max3100_sr(max3100s[i], tx, &rx);
816 mutex_unlock(&max3100s_lock);
820 static int max3100_remove(struct spi_device *spi)
822 struct max3100_port *s = spi_get_drvdata(spi);
825 mutex_lock(&max3100s_lock);
827 /* find out the index for the chip we are removing */
828 for (i = 0; i < MAX_MAX3100; i++)
829 if (max3100s[i] == s) {
830 dev_dbg(&spi->dev, "%s: removing port %d\n", __func__, i);
831 uart_remove_one_port(&max3100_uart_driver, &max3100s[i]->port);
837 WARN_ON(i == MAX_MAX3100);
839 /* check if this is the last chip we have */
840 for (i = 0; i < MAX_MAX3100; i++)
842 mutex_unlock(&max3100s_lock);
845 pr_debug("removing max3100 driver\n");
846 uart_unregister_driver(&max3100_uart_driver);
848 mutex_unlock(&max3100s_lock);
852 #ifdef CONFIG_PM_SLEEP
854 static int max3100_suspend(struct device *dev)
856 struct max3100_port *s = dev_get_drvdata(dev);
858 dev_dbg(&s->spi->dev, "%s\n", __func__);
863 uart_suspend_port(&max3100_uart_driver, &s->port);
865 if (s->max3100_hw_suspend)
866 s->max3100_hw_suspend(1);
868 /* no HW suspend, so do SW one */
871 tx = MAX3100_WC | MAX3100_SHDN;
872 max3100_sr(s, tx, &rx);
877 static int max3100_resume(struct device *dev)
879 struct max3100_port *s = dev_get_drvdata(dev);
881 dev_dbg(&s->spi->dev, "%s\n", __func__);
883 if (s->max3100_hw_suspend)
884 s->max3100_hw_suspend(0);
885 uart_resume_port(&max3100_uart_driver, &s->port);
897 static SIMPLE_DEV_PM_OPS(max3100_pm_ops, max3100_suspend, max3100_resume);
898 #define MAX3100_PM_OPS (&max3100_pm_ops)
901 #define MAX3100_PM_OPS NULL
904 static struct spi_driver max3100_driver = {
907 .owner = THIS_MODULE,
908 .pm = MAX3100_PM_OPS,
910 .probe = max3100_probe,
911 .remove = max3100_remove,
914 module_spi_driver(max3100_driver);
916 MODULE_DESCRIPTION("MAX3100 driver");
917 MODULE_AUTHOR("Christian Pellegrin <chripell@evolware.org>");
918 MODULE_LICENSE("GPL");
919 MODULE_ALIAS("spi:max3100");