1 // SPDX-License-Identifier: GPL-2.0+
6 * Notes: the MAX3100 doesn't provide an interrupt on CTS so we have
7 * to use polling for flow control. TX empty IRQ is unusable, since
8 * writing conf clears FIFO buffer and we cannot have this interrupt
9 * always asking us for attention.
11 * Example platform data:
13 static struct plat_max3100 max3100_plat_data = {
19 static struct spi_board_info spi_board_info[] = {
21 .modalias = "max3100",
22 .platform_data = &max3100_plat_data,
24 .max_speed_hz = 5*1000*1000,
29 * The initial minor number is 209 in the low-density serial port:
30 * mknod /dev/ttyMAX0 c 204 209
33 #define MAX3100_MAJOR 204
34 #define MAX3100_MINOR 209
35 /* 4 MAX3100s should be enough for everyone */
38 #include <linux/delay.h>
39 #include <linux/slab.h>
40 #include <linux/device.h>
41 #include <linux/module.h>
42 #include <linux/serial_core.h>
43 #include <linux/serial.h>
44 #include <linux/spi/spi.h>
45 #include <linux/freezer.h>
46 #include <linux/tty.h>
47 #include <linux/tty_flip.h>
49 #include <linux/serial_max3100.h>
51 #define MAX3100_C (1<<14)
52 #define MAX3100_D (0<<14)
53 #define MAX3100_W (1<<15)
54 #define MAX3100_RX (0<<15)
56 #define MAX3100_WC (MAX3100_W | MAX3100_C)
57 #define MAX3100_RC (MAX3100_RX | MAX3100_C)
58 #define MAX3100_WD (MAX3100_W | MAX3100_D)
59 #define MAX3100_RD (MAX3100_RX | MAX3100_D)
60 #define MAX3100_CMD (3 << 14)
62 #define MAX3100_T (1<<14)
63 #define MAX3100_R (1<<15)
65 #define MAX3100_FEN (1<<13)
66 #define MAX3100_SHDN (1<<12)
67 #define MAX3100_TM (1<<11)
68 #define MAX3100_RM (1<<10)
69 #define MAX3100_PM (1<<9)
70 #define MAX3100_RAM (1<<8)
71 #define MAX3100_IR (1<<7)
72 #define MAX3100_ST (1<<6)
73 #define MAX3100_PE (1<<5)
74 #define MAX3100_L (1<<4)
75 #define MAX3100_BAUD (0xf)
77 #define MAX3100_TE (1<<10)
78 #define MAX3100_RAFE (1<<10)
79 #define MAX3100_RTS (1<<9)
80 #define MAX3100_CTS (1<<9)
81 #define MAX3100_PT (1<<8)
82 #define MAX3100_DATA (0xff)
84 #define MAX3100_RT (MAX3100_R | MAX3100_T)
85 #define MAX3100_RTC (MAX3100_RT | MAX3100_CTS | MAX3100_RAFE)
87 /* the following simulate a status reg for ignore_status_mask */
88 #define MAX3100_STATUS_PE 1
89 #define MAX3100_STATUS_FE 2
90 #define MAX3100_STATUS_OE 4
93 struct uart_port port;
94 struct spi_device *spi;
96 int cts; /* last CTS received for flow ctrl */
97 int tx_empty; /* last TX empty bit */
99 spinlock_t conf_lock; /* shared data */
100 int conf_commit; /* need to make changes */
101 int conf; /* configuration for the MAX31000
102 * (bits 0-7, bits 8-11 are irqs) */
103 int rts_commit; /* need to change rts */
104 int rts; /* rts status */
105 int baud; /* current baud rate */
107 int parity; /* keeps track if we should send parity */
108 #define MAX3100_PARITY_ON 1
109 #define MAX3100_PARITY_ODD 2
110 #define MAX3100_7BIT 4
111 int rx_enabled; /* if we should rx chars */
113 int irq; /* irq assigned to the max3100 */
115 int minor; /* minor number */
116 int crystal; /* 1 if 3.6864Mhz crystal 0 for 1.8432 */
117 int loopback; /* 1 if we are in loopback mode */
119 /* for handling irqs: need workqueue since we do spi_sync */
120 struct workqueue_struct *workqueue;
121 struct work_struct work;
122 /* set to 1 to make the workhandler exit as soon as possible */
124 /* need to know we are suspending to avoid deadlock on workqueue */
127 /* hook for suspending MAX3100 via dedicated pin */
128 void (*max3100_hw_suspend) (int suspend);
130 /* poll time (in ms) for ctrl lines */
133 struct timer_list timer;
136 static struct max3100_port *max3100s[MAX_MAX3100]; /* the chips */
137 static DEFINE_MUTEX(max3100s_lock); /* race on probe */
139 static int max3100_do_parity(struct max3100_port *s, u16 c)
143 if (s->parity & MAX3100_PARITY_ODD)
148 if (s->parity & MAX3100_7BIT)
153 parity = parity ^ (hweight8(c) & 1);
157 static int max3100_check_parity(struct max3100_port *s, u16 c)
159 return max3100_do_parity(s, c) == ((c >> 8) & 1);
162 static void max3100_calc_parity(struct max3100_port *s, u16 *c)
164 if (s->parity & MAX3100_7BIT)
169 if (s->parity & MAX3100_PARITY_ON)
170 *c |= max3100_do_parity(s, *c) << 8;
173 static void max3100_work(struct work_struct *w);
175 static void max3100_dowork(struct max3100_port *s)
177 if (!s->force_end_work && !freezing(current) && !s->suspending)
178 queue_work(s->workqueue, &s->work);
181 static void max3100_timeout(struct timer_list *t)
183 struct max3100_port *s = from_timer(s, t, timer);
187 mod_timer(&s->timer, jiffies + s->poll_time);
191 static int max3100_sr(struct max3100_port *s, u16 tx, u16 *rx)
193 struct spi_message message;
196 struct spi_transfer tran = {
202 etx = cpu_to_be16(tx);
203 spi_message_init(&message);
204 spi_message_add_tail(&tran, &message);
205 status = spi_sync(s->spi, &message);
207 dev_warn(&s->spi->dev, "error while calling spi_sync\n");
210 *rx = be16_to_cpu(erx);
211 s->tx_empty = (*rx & MAX3100_T) > 0;
212 dev_dbg(&s->spi->dev, "%04x - %04x\n", tx, *rx);
216 static int max3100_handlerx(struct max3100_port *s, u16 rx)
218 unsigned int ch, flg, status = 0;
221 if (rx & MAX3100_R && s->rx_enabled) {
222 dev_dbg(&s->spi->dev, "%s\n", __func__);
223 ch = rx & (s->parity & MAX3100_7BIT ? 0x7f : 0xff);
224 if (rx & MAX3100_RAFE) {
225 s->port.icount.frame++;
227 status |= MAX3100_STATUS_FE;
229 if (s->parity & MAX3100_PARITY_ON) {
230 if (max3100_check_parity(s, rx)) {
234 s->port.icount.parity++;
236 status |= MAX3100_STATUS_PE;
243 uart_insert_char(&s->port, status, MAX3100_STATUS_OE, ch, flg);
247 cts = (rx & MAX3100_CTS) > 0;
250 uart_handle_cts_change(&s->port, cts ? TIOCM_CTS : 0);
256 static void max3100_work(struct work_struct *w)
258 struct max3100_port *s = container_of(w, struct max3100_port, work);
261 int conf, cconf, crts;
262 struct circ_buf *xmit = &s->port.state->xmit;
264 dev_dbg(&s->spi->dev, "%s\n", __func__);
268 spin_lock(&s->conf_lock);
270 cconf = s->conf_commit;
272 crts = s->rts_commit;
274 spin_unlock(&s->conf_lock);
276 max3100_sr(s, MAX3100_WC | conf, &rx);
278 max3100_sr(s, MAX3100_WD | MAX3100_TE |
279 (s->rts ? MAX3100_RTS : 0), &rx);
280 rxchars += max3100_handlerx(s, rx);
283 max3100_sr(s, MAX3100_RD, &rx);
284 rxchars += max3100_handlerx(s, rx);
286 if (rx & MAX3100_T) {
288 if (s->port.x_char) {
292 } else if (!uart_circ_empty(xmit) &&
293 !uart_tx_stopped(&s->port)) {
294 tx = xmit->buf[xmit->tail];
295 xmit->tail = (xmit->tail + 1) &
296 (UART_XMIT_SIZE - 1);
300 max3100_calc_parity(s, &tx);
301 tx |= MAX3100_WD | (s->rts ? MAX3100_RTS : 0);
302 max3100_sr(s, tx, &rx);
303 rxchars += max3100_handlerx(s, rx);
308 tty_flip_buffer_push(&s->port.state->port);
311 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
312 uart_write_wakeup(&s->port);
314 } while (!s->force_end_work &&
315 !freezing(current) &&
317 (!uart_circ_empty(xmit) &&
318 !uart_tx_stopped(&s->port))));
321 tty_flip_buffer_push(&s->port.state->port);
324 static irqreturn_t max3100_irq(int irqno, void *dev_id)
326 struct max3100_port *s = dev_id;
328 dev_dbg(&s->spi->dev, "%s\n", __func__);
334 static void max3100_enable_ms(struct uart_port *port)
336 struct max3100_port *s = container_of(port,
340 if (s->poll_time > 0)
341 mod_timer(&s->timer, jiffies);
342 dev_dbg(&s->spi->dev, "%s\n", __func__);
345 static void max3100_start_tx(struct uart_port *port)
347 struct max3100_port *s = container_of(port,
351 dev_dbg(&s->spi->dev, "%s\n", __func__);
356 static void max3100_stop_rx(struct uart_port *port)
358 struct max3100_port *s = container_of(port,
362 dev_dbg(&s->spi->dev, "%s\n", __func__);
365 spin_lock(&s->conf_lock);
366 s->conf &= ~MAX3100_RM;
368 spin_unlock(&s->conf_lock);
372 static unsigned int max3100_tx_empty(struct uart_port *port)
374 struct max3100_port *s = container_of(port,
378 dev_dbg(&s->spi->dev, "%s\n", __func__);
380 /* may not be truly up-to-date */
385 static unsigned int max3100_get_mctrl(struct uart_port *port)
387 struct max3100_port *s = container_of(port,
391 dev_dbg(&s->spi->dev, "%s\n", __func__);
393 /* may not be truly up-to-date */
395 /* always assert DCD and DSR since these lines are not wired */
396 return (s->cts ? TIOCM_CTS : 0) | TIOCM_DSR | TIOCM_CAR;
399 static void max3100_set_mctrl(struct uart_port *port, unsigned int mctrl)
401 struct max3100_port *s = container_of(port,
406 dev_dbg(&s->spi->dev, "%s\n", __func__);
408 rts = (mctrl & TIOCM_RTS) > 0;
410 spin_lock(&s->conf_lock);
416 spin_unlock(&s->conf_lock);
420 max3100_set_termios(struct uart_port *port, struct ktermios *termios,
421 struct ktermios *old)
423 struct max3100_port *s = container_of(port,
428 u32 param_new, param_mask, parity = 0;
430 dev_dbg(&s->spi->dev, "%s\n", __func__);
432 cflag = termios->c_cflag;
435 baud = tty_termios_baud_rate(termios);
436 param_new = s->conf & MAX3100_BAUD;
445 param_new = 14 + s->crystal;
448 param_new = 13 + s->crystal;
451 param_new = 12 + s->crystal;
454 param_new = 11 + s->crystal;
457 param_new = 10 + s->crystal;
460 param_new = 9 + s->crystal;
463 param_new = 8 + s->crystal;
466 param_new = 1 + s->crystal;
469 param_new = 0 + s->crystal;
480 tty_termios_encode_baud_rate(termios, baud, baud);
482 param_mask |= MAX3100_BAUD;
484 if ((cflag & CSIZE) == CS8) {
485 param_new &= ~MAX3100_L;
486 parity &= ~MAX3100_7BIT;
488 param_new |= MAX3100_L;
489 parity |= MAX3100_7BIT;
490 cflag = (cflag & ~CSIZE) | CS7;
492 param_mask |= MAX3100_L;
495 param_new |= MAX3100_ST;
497 param_new &= ~MAX3100_ST;
498 param_mask |= MAX3100_ST;
500 if (cflag & PARENB) {
501 param_new |= MAX3100_PE;
502 parity |= MAX3100_PARITY_ON;
504 param_new &= ~MAX3100_PE;
505 parity &= ~MAX3100_PARITY_ON;
507 param_mask |= MAX3100_PE;
510 parity |= MAX3100_PARITY_ODD;
512 parity &= ~MAX3100_PARITY_ODD;
514 /* mask termios capabilities we don't support */
516 termios->c_cflag = cflag;
518 s->port.ignore_status_mask = 0;
519 if (termios->c_iflag & IGNPAR)
520 s->port.ignore_status_mask |=
521 MAX3100_STATUS_PE | MAX3100_STATUS_FE |
524 /* we are sending char from a workqueue so enable */
525 s->port.state->port.low_latency = 1;
527 if (s->poll_time > 0)
528 del_timer_sync(&s->timer);
530 uart_update_timeout(port, termios->c_cflag, baud);
532 spin_lock(&s->conf_lock);
533 s->conf = (s->conf & ~param_mask) | (param_new & param_mask);
536 spin_unlock(&s->conf_lock);
539 if (UART_ENABLE_MS(&s->port, termios->c_cflag))
540 max3100_enable_ms(&s->port);
543 static void max3100_shutdown(struct uart_port *port)
545 struct max3100_port *s = container_of(port,
549 dev_dbg(&s->spi->dev, "%s\n", __func__);
554 s->force_end_work = 1;
556 if (s->poll_time > 0)
557 del_timer_sync(&s->timer);
560 flush_workqueue(s->workqueue);
561 destroy_workqueue(s->workqueue);
567 /* set shutdown mode to save power */
568 if (s->max3100_hw_suspend)
569 s->max3100_hw_suspend(1);
573 tx = MAX3100_WC | MAX3100_SHDN;
574 max3100_sr(s, tx, &rx);
578 static int max3100_startup(struct uart_port *port)
580 struct max3100_port *s = container_of(port,
585 dev_dbg(&s->spi->dev, "%s\n", __func__);
587 s->conf = MAX3100_RM;
588 s->baud = s->crystal ? 230400 : 115200;
594 s->force_end_work = 0;
598 sprintf(b, "max3100-%d", s->minor);
599 s->workqueue = create_freezable_workqueue(b);
601 dev_warn(&s->spi->dev, "cannot create workqueue\n");
604 INIT_WORK(&s->work, max3100_work);
606 if (request_irq(s->irq, max3100_irq,
607 IRQF_TRIGGER_FALLING, "max3100", s) < 0) {
608 dev_warn(&s->spi->dev, "cannot allocate irq %d\n", s->irq);
610 destroy_workqueue(s->workqueue);
618 max3100_sr(s, tx, &rx);
621 if (s->max3100_hw_suspend)
622 s->max3100_hw_suspend(0);
625 /* wait for clock to settle */
628 max3100_enable_ms(&s->port);
633 static const char *max3100_type(struct uart_port *port)
635 struct max3100_port *s = container_of(port,
639 dev_dbg(&s->spi->dev, "%s\n", __func__);
641 return s->port.type == PORT_MAX3100 ? "MAX3100" : NULL;
644 static void max3100_release_port(struct uart_port *port)
646 struct max3100_port *s = container_of(port,
650 dev_dbg(&s->spi->dev, "%s\n", __func__);
653 static void max3100_config_port(struct uart_port *port, int flags)
655 struct max3100_port *s = container_of(port,
659 dev_dbg(&s->spi->dev, "%s\n", __func__);
661 if (flags & UART_CONFIG_TYPE)
662 s->port.type = PORT_MAX3100;
665 static int max3100_verify_port(struct uart_port *port,
666 struct serial_struct *ser)
668 struct max3100_port *s = container_of(port,
673 dev_dbg(&s->spi->dev, "%s\n", __func__);
675 if (ser->type == PORT_UNKNOWN || ser->type == PORT_MAX3100)
680 static void max3100_stop_tx(struct uart_port *port)
682 struct max3100_port *s = container_of(port,
686 dev_dbg(&s->spi->dev, "%s\n", __func__);
689 static int max3100_request_port(struct uart_port *port)
691 struct max3100_port *s = container_of(port,
695 dev_dbg(&s->spi->dev, "%s\n", __func__);
699 static void max3100_break_ctl(struct uart_port *port, int break_state)
701 struct max3100_port *s = container_of(port,
705 dev_dbg(&s->spi->dev, "%s\n", __func__);
708 static const struct uart_ops max3100_ops = {
709 .tx_empty = max3100_tx_empty,
710 .set_mctrl = max3100_set_mctrl,
711 .get_mctrl = max3100_get_mctrl,
712 .stop_tx = max3100_stop_tx,
713 .start_tx = max3100_start_tx,
714 .stop_rx = max3100_stop_rx,
715 .enable_ms = max3100_enable_ms,
716 .break_ctl = max3100_break_ctl,
717 .startup = max3100_startup,
718 .shutdown = max3100_shutdown,
719 .set_termios = max3100_set_termios,
720 .type = max3100_type,
721 .release_port = max3100_release_port,
722 .request_port = max3100_request_port,
723 .config_port = max3100_config_port,
724 .verify_port = max3100_verify_port,
727 static struct uart_driver max3100_uart_driver = {
728 .owner = THIS_MODULE,
729 .driver_name = "ttyMAX",
730 .dev_name = "ttyMAX",
731 .major = MAX3100_MAJOR,
732 .minor = MAX3100_MINOR,
735 static int uart_driver_registered;
737 static int max3100_probe(struct spi_device *spi)
740 struct plat_max3100 *pdata;
743 mutex_lock(&max3100s_lock);
745 if (!uart_driver_registered) {
746 uart_driver_registered = 1;
747 retval = uart_register_driver(&max3100_uart_driver);
749 printk(KERN_ERR "Couldn't register max3100 uart driver\n");
750 mutex_unlock(&max3100s_lock);
755 for (i = 0; i < MAX_MAX3100; i++)
758 if (i == MAX_MAX3100) {
759 dev_warn(&spi->dev, "too many MAX3100 chips\n");
760 mutex_unlock(&max3100s_lock);
764 max3100s[i] = kzalloc(sizeof(struct max3100_port), GFP_KERNEL);
767 "kmalloc for max3100 structure %d failed!\n", i);
768 mutex_unlock(&max3100s_lock);
771 max3100s[i]->spi = spi;
772 max3100s[i]->irq = spi->irq;
773 spin_lock_init(&max3100s[i]->conf_lock);
774 spi_set_drvdata(spi, max3100s[i]);
775 pdata = dev_get_platdata(&spi->dev);
776 max3100s[i]->crystal = pdata->crystal;
777 max3100s[i]->loopback = pdata->loopback;
778 max3100s[i]->poll_time = msecs_to_jiffies(pdata->poll_time);
779 if (pdata->poll_time > 0 && max3100s[i]->poll_time == 0)
780 max3100s[i]->poll_time = 1;
781 max3100s[i]->max3100_hw_suspend = pdata->max3100_hw_suspend;
782 max3100s[i]->minor = i;
783 timer_setup(&max3100s[i]->timer, max3100_timeout, 0);
785 dev_dbg(&spi->dev, "%s: adding port %d\n", __func__, i);
786 max3100s[i]->port.irq = max3100s[i]->irq;
787 max3100s[i]->port.uartclk = max3100s[i]->crystal ? 3686400 : 1843200;
788 max3100s[i]->port.fifosize = 16;
789 max3100s[i]->port.ops = &max3100_ops;
790 max3100s[i]->port.flags = UPF_SKIP_TEST | UPF_BOOT_AUTOCONF;
791 max3100s[i]->port.line = i;
792 max3100s[i]->port.type = PORT_MAX3100;
793 max3100s[i]->port.dev = &spi->dev;
794 retval = uart_add_one_port(&max3100_uart_driver, &max3100s[i]->port);
797 "uart_add_one_port failed for line %d with error %d\n",
800 /* set shutdown mode to save power. Will be woken-up on open */
801 if (max3100s[i]->max3100_hw_suspend)
802 max3100s[i]->max3100_hw_suspend(1);
804 tx = MAX3100_WC | MAX3100_SHDN;
805 max3100_sr(max3100s[i], tx, &rx);
807 mutex_unlock(&max3100s_lock);
811 static int max3100_remove(struct spi_device *spi)
813 struct max3100_port *s = spi_get_drvdata(spi);
816 mutex_lock(&max3100s_lock);
818 /* find out the index for the chip we are removing */
819 for (i = 0; i < MAX_MAX3100; i++)
820 if (max3100s[i] == s) {
821 dev_dbg(&spi->dev, "%s: removing port %d\n", __func__, i);
822 uart_remove_one_port(&max3100_uart_driver, &max3100s[i]->port);
828 WARN_ON(i == MAX_MAX3100);
830 /* check if this is the last chip we have */
831 for (i = 0; i < MAX_MAX3100; i++)
833 mutex_unlock(&max3100s_lock);
836 pr_debug("removing max3100 driver\n");
837 uart_unregister_driver(&max3100_uart_driver);
839 mutex_unlock(&max3100s_lock);
843 #ifdef CONFIG_PM_SLEEP
845 static int max3100_suspend(struct device *dev)
847 struct max3100_port *s = dev_get_drvdata(dev);
849 dev_dbg(&s->spi->dev, "%s\n", __func__);
854 uart_suspend_port(&max3100_uart_driver, &s->port);
856 if (s->max3100_hw_suspend)
857 s->max3100_hw_suspend(1);
859 /* no HW suspend, so do SW one */
862 tx = MAX3100_WC | MAX3100_SHDN;
863 max3100_sr(s, tx, &rx);
868 static int max3100_resume(struct device *dev)
870 struct max3100_port *s = dev_get_drvdata(dev);
872 dev_dbg(&s->spi->dev, "%s\n", __func__);
874 if (s->max3100_hw_suspend)
875 s->max3100_hw_suspend(0);
876 uart_resume_port(&max3100_uart_driver, &s->port);
888 static SIMPLE_DEV_PM_OPS(max3100_pm_ops, max3100_suspend, max3100_resume);
889 #define MAX3100_PM_OPS (&max3100_pm_ops)
892 #define MAX3100_PM_OPS NULL
895 static struct spi_driver max3100_driver = {
898 .pm = MAX3100_PM_OPS,
900 .probe = max3100_probe,
901 .remove = max3100_remove,
904 module_spi_driver(max3100_driver);
906 MODULE_DESCRIPTION("MAX3100 driver");
908 MODULE_LICENSE("GPL");
909 MODULE_ALIAS("spi:max3100");