1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2008 Christian Pellegrin <chripell@evolware.org>
5 * Notes: the MAX3100 doesn't provide an interrupt on CTS so we have
6 * to use polling for flow control. TX empty IRQ is unusable, since
7 * writing conf clears FIFO buffer and we cannot have this interrupt
8 * always asking us for attention.
10 * The initial minor number is 209 in the low-density serial port:
11 * mknod /dev/ttyMAX0 c 204 209
14 #define MAX3100_MAJOR 204
15 #define MAX3100_MINOR 209
16 /* 4 MAX3100s should be enough for everyone */
19 #include <linux/container_of.h>
20 #include <linux/delay.h>
21 #include <linux/device.h>
22 #include <linux/freezer.h>
23 #include <linux/mod_devicetable.h>
24 #include <linux/module.h>
26 #include <linux/property.h>
27 #include <linux/serial_core.h>
28 #include <linux/serial.h>
29 #include <linux/slab.h>
30 #include <linux/spi/spi.h>
31 #include <linux/tty_flip.h>
32 #include <linux/tty.h>
33 #include <linux/types.h>
35 #include <linux/unaligned.h>
37 #define MAX3100_C (1<<14)
38 #define MAX3100_D (0<<14)
39 #define MAX3100_W (1<<15)
40 #define MAX3100_RX (0<<15)
42 #define MAX3100_WC (MAX3100_W | MAX3100_C)
43 #define MAX3100_RC (MAX3100_RX | MAX3100_C)
44 #define MAX3100_WD (MAX3100_W | MAX3100_D)
45 #define MAX3100_RD (MAX3100_RX | MAX3100_D)
46 #define MAX3100_CMD (3 << 14)
48 #define MAX3100_T (1<<14)
49 #define MAX3100_R (1<<15)
51 #define MAX3100_FEN (1<<13)
52 #define MAX3100_SHDN (1<<12)
53 #define MAX3100_TM (1<<11)
54 #define MAX3100_RM (1<<10)
55 #define MAX3100_PM (1<<9)
56 #define MAX3100_RAM (1<<8)
57 #define MAX3100_IR (1<<7)
58 #define MAX3100_ST (1<<6)
59 #define MAX3100_PE (1<<5)
60 #define MAX3100_L (1<<4)
61 #define MAX3100_BAUD (0xf)
63 #define MAX3100_TE (1<<10)
64 #define MAX3100_RAFE (1<<10)
65 #define MAX3100_RTS (1<<9)
66 #define MAX3100_CTS (1<<9)
67 #define MAX3100_PT (1<<8)
68 #define MAX3100_DATA (0xff)
70 #define MAX3100_RT (MAX3100_R | MAX3100_T)
71 #define MAX3100_RTC (MAX3100_RT | MAX3100_CTS | MAX3100_RAFE)
73 /* the following simulate a status reg for ignore_status_mask */
74 #define MAX3100_STATUS_PE 1
75 #define MAX3100_STATUS_FE 2
76 #define MAX3100_STATUS_OE 4
79 struct uart_port port
;
80 struct spi_device
*spi
;
82 int cts
; /* last CTS received for flow ctrl */
83 int tx_empty
; /* last TX empty bit */
85 spinlock_t conf_lock
; /* shared data */
86 int conf_commit
; /* need to make changes */
87 int conf
; /* configuration for the MAX31000
88 * (bits 0-7, bits 8-11 are irqs) */
89 int rts_commit
; /* need to change rts */
90 int rts
; /* rts status */
91 int baud
; /* current baud rate */
93 int parity
; /* keeps track if we should send parity */
94 #define MAX3100_PARITY_ON 1
95 #define MAX3100_PARITY_ODD 2
96 #define MAX3100_7BIT 4
97 int rx_enabled
; /* if we should rx chars */
99 int minor
; /* minor number */
100 int loopback_commit
; /* need to change loopback */
101 int loopback
; /* 1 if we are in loopback mode */
103 /* for handling irqs: need workqueue since we do spi_sync */
104 struct workqueue_struct
*workqueue
;
105 struct work_struct work
;
106 /* set to 1 to make the workhandler exit as soon as possible */
108 /* need to know we are suspending to avoid deadlock on workqueue */
111 struct timer_list timer
;
114 static inline struct max3100_port
*to_max3100_port(struct uart_port
*port
)
116 return container_of(port
, struct max3100_port
, port
);
119 static struct max3100_port
*max3100s
[MAX_MAX3100
]; /* the chips */
120 static DEFINE_MUTEX(max3100s_lock
); /* race on probe */
122 static int max3100_do_parity(struct max3100_port
*s
, u16 c
)
126 if (s
->parity
& MAX3100_PARITY_ODD
)
131 if (s
->parity
& MAX3100_7BIT
)
136 parity
= parity
^ (hweight8(c
) & 1);
140 static int max3100_check_parity(struct max3100_port
*s
, u16 c
)
142 return max3100_do_parity(s
, c
) == ((c
>> 8) & 1);
145 static void max3100_calc_parity(struct max3100_port
*s
, u16
*c
)
147 if (s
->parity
& MAX3100_7BIT
)
152 if (s
->parity
& MAX3100_PARITY_ON
)
153 *c
|= max3100_do_parity(s
, *c
) << 8;
156 static int max3100_sr(struct max3100_port
*s
, u16 tx
, u16
*rx
)
158 struct spi_message message
;
161 struct spi_transfer tran
= {
167 etx
= cpu_to_be16(tx
);
168 spi_message_init(&message
);
169 spi_message_add_tail(&tran
, &message
);
170 status
= spi_sync(s
->spi
, &message
);
172 dev_warn(&s
->spi
->dev
, "error while calling spi_sync\n");
175 *rx
= be16_to_cpu(erx
);
176 s
->tx_empty
= (*rx
& MAX3100_T
) > 0;
177 dev_dbg(&s
->spi
->dev
, "%04x - %04x\n", tx
, *rx
);
181 static int max3100_handlerx_unlocked(struct max3100_port
*s
, u16 rx
)
183 unsigned int status
= 0;
187 if (rx
& MAX3100_R
&& s
->rx_enabled
) {
188 dev_dbg(&s
->spi
->dev
, "%s\n", __func__
);
189 ch
= rx
& (s
->parity
& MAX3100_7BIT
? 0x7f : 0xff);
190 if (rx
& MAX3100_RAFE
) {
191 s
->port
.icount
.frame
++;
193 status
|= MAX3100_STATUS_FE
;
195 if (s
->parity
& MAX3100_PARITY_ON
) {
196 if (max3100_check_parity(s
, rx
)) {
200 s
->port
.icount
.parity
++;
202 status
|= MAX3100_STATUS_PE
;
209 uart_insert_char(&s
->port
, status
, MAX3100_STATUS_OE
, ch
, flg
);
213 cts
= (rx
& MAX3100_CTS
) > 0;
216 uart_handle_cts_change(&s
->port
, cts
);
222 static int max3100_handlerx(struct max3100_port
*s
, u16 rx
)
227 uart_port_lock_irqsave(&s
->port
, &flags
);
228 ret
= max3100_handlerx_unlocked(s
, rx
);
229 uart_port_unlock_irqrestore(&s
->port
, flags
);
233 static void max3100_work(struct work_struct
*w
)
235 struct max3100_port
*s
= container_of(w
, struct max3100_port
, work
);
236 struct tty_port
*tport
= &s
->port
.state
->port
;
238 int conf
, cconf
, cloopback
, crts
;
242 dev_dbg(&s
->spi
->dev
, "%s\n", __func__
);
246 spin_lock(&s
->conf_lock
);
248 cconf
= s
->conf_commit
;
250 cloopback
= s
->loopback_commit
;
251 s
->loopback_commit
= 0;
252 crts
= s
->rts_commit
;
254 spin_unlock(&s
->conf_lock
);
256 max3100_sr(s
, MAX3100_WC
| conf
, &rx
);
258 max3100_sr(s
, 0x4001, &rx
);
260 max3100_sr(s
, MAX3100_WD
| MAX3100_TE
|
261 (s
->rts
? MAX3100_RTS
: 0), &rx
);
262 rxchars
+= max3100_handlerx(s
, rx
);
265 max3100_sr(s
, MAX3100_RD
, &rx
);
266 rxchars
+= max3100_handlerx(s
, rx
);
268 if (rx
& MAX3100_T
) {
270 if (s
->port
.x_char
) {
274 } else if (!uart_tx_stopped(&s
->port
) &&
275 uart_fifo_get(&s
->port
, &ch
)) {
279 max3100_calc_parity(s
, &tx
);
280 tx
|= MAX3100_WD
| (s
->rts
? MAX3100_RTS
: 0);
281 max3100_sr(s
, tx
, &rx
);
282 rxchars
+= max3100_handlerx(s
, rx
);
287 tty_flip_buffer_push(&s
->port
.state
->port
);
290 if (kfifo_len(&tport
->xmit_fifo
) < WAKEUP_CHARS
)
291 uart_write_wakeup(&s
->port
);
293 } while (!s
->force_end_work
&&
294 !freezing(current
) &&
296 (!kfifo_is_empty(&tport
->xmit_fifo
) &&
297 !uart_tx_stopped(&s
->port
))));
300 tty_flip_buffer_push(&s
->port
.state
->port
);
303 static void max3100_dowork(struct max3100_port
*s
)
305 if (!s
->force_end_work
&& !freezing(current
) && !s
->suspending
)
306 queue_work(s
->workqueue
, &s
->work
);
309 static void max3100_timeout(struct timer_list
*t
)
311 struct max3100_port
*s
= from_timer(s
, t
, timer
);
314 mod_timer(&s
->timer
, jiffies
+ uart_poll_timeout(&s
->port
));
317 static irqreturn_t
max3100_irq(int irqno
, void *dev_id
)
319 struct max3100_port
*s
= dev_id
;
321 dev_dbg(&s
->spi
->dev
, "%s\n", __func__
);
327 static void max3100_enable_ms(struct uart_port
*port
)
329 struct max3100_port
*s
= to_max3100_port(port
);
331 mod_timer(&s
->timer
, jiffies
);
332 dev_dbg(&s
->spi
->dev
, "%s\n", __func__
);
335 static void max3100_start_tx(struct uart_port
*port
)
337 struct max3100_port
*s
= to_max3100_port(port
);
339 dev_dbg(&s
->spi
->dev
, "%s\n", __func__
);
344 static void max3100_stop_rx(struct uart_port
*port
)
346 struct max3100_port
*s
= to_max3100_port(port
);
348 dev_dbg(&s
->spi
->dev
, "%s\n", __func__
);
351 spin_lock(&s
->conf_lock
);
352 s
->conf
&= ~MAX3100_RM
;
354 spin_unlock(&s
->conf_lock
);
358 static unsigned int max3100_tx_empty(struct uart_port
*port
)
360 struct max3100_port
*s
= to_max3100_port(port
);
362 dev_dbg(&s
->spi
->dev
, "%s\n", __func__
);
364 /* may not be truly up-to-date */
369 static unsigned int max3100_get_mctrl(struct uart_port
*port
)
371 struct max3100_port
*s
= to_max3100_port(port
);
373 dev_dbg(&s
->spi
->dev
, "%s\n", __func__
);
375 /* may not be truly up-to-date */
377 /* always assert DCD and DSR since these lines are not wired */
378 return (s
->cts
? TIOCM_CTS
: 0) | TIOCM_DSR
| TIOCM_CAR
;
381 static void max3100_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
383 struct max3100_port
*s
= to_max3100_port(port
);
386 dev_dbg(&s
->spi
->dev
, "%s\n", __func__
);
388 loopback
= (mctrl
& TIOCM_LOOP
) > 0;
389 rts
= (mctrl
& TIOCM_RTS
) > 0;
391 spin_lock(&s
->conf_lock
);
392 if (s
->loopback
!= loopback
) {
393 s
->loopback
= loopback
;
394 s
->loopback_commit
= 1;
400 if (s
->loopback_commit
|| s
->rts_commit
)
402 spin_unlock(&s
->conf_lock
);
406 max3100_set_termios(struct uart_port
*port
, struct ktermios
*termios
,
407 const struct ktermios
*old
)
409 struct max3100_port
*s
= to_max3100_port(port
);
410 unsigned int baud
= port
->uartclk
/ 16;
411 unsigned int baud230400
= (baud
== 230400) ? 1 : 0;
413 u32 param_new
, param_mask
, parity
= 0;
415 dev_dbg(&s
->spi
->dev
, "%s\n", __func__
);
417 cflag
= termios
->c_cflag
;
420 baud
= tty_termios_baud_rate(termios
);
421 param_new
= s
->conf
& MAX3100_BAUD
;
430 param_new
= 14 + baud230400
;
433 param_new
= 13 + baud230400
;
436 param_new
= 12 + baud230400
;
439 param_new
= 11 + baud230400
;
442 param_new
= 10 + baud230400
;
445 param_new
= 9 + baud230400
;
448 param_new
= 8 + baud230400
;
451 param_new
= 1 + baud230400
;
454 param_new
= 0 + baud230400
;
465 tty_termios_encode_baud_rate(termios
, baud
, baud
);
467 param_mask
|= MAX3100_BAUD
;
469 if ((cflag
& CSIZE
) == CS8
) {
470 param_new
&= ~MAX3100_L
;
471 parity
&= ~MAX3100_7BIT
;
473 param_new
|= MAX3100_L
;
474 parity
|= MAX3100_7BIT
;
475 cflag
= (cflag
& ~CSIZE
) | CS7
;
477 param_mask
|= MAX3100_L
;
480 param_new
|= MAX3100_ST
;
482 param_new
&= ~MAX3100_ST
;
483 param_mask
|= MAX3100_ST
;
485 if (cflag
& PARENB
) {
486 param_new
|= MAX3100_PE
;
487 parity
|= MAX3100_PARITY_ON
;
489 param_new
&= ~MAX3100_PE
;
490 parity
&= ~MAX3100_PARITY_ON
;
492 param_mask
|= MAX3100_PE
;
495 parity
|= MAX3100_PARITY_ODD
;
497 parity
&= ~MAX3100_PARITY_ODD
;
499 /* mask termios capabilities we don't support */
501 termios
->c_cflag
= cflag
;
503 s
->port
.ignore_status_mask
= 0;
504 if (termios
->c_iflag
& IGNPAR
)
505 s
->port
.ignore_status_mask
|=
506 MAX3100_STATUS_PE
| MAX3100_STATUS_FE
|
509 del_timer_sync(&s
->timer
);
510 uart_update_timeout(port
, termios
->c_cflag
, baud
);
512 spin_lock(&s
->conf_lock
);
513 s
->conf
= (s
->conf
& ~param_mask
) | (param_new
& param_mask
);
516 spin_unlock(&s
->conf_lock
);
519 if (UART_ENABLE_MS(&s
->port
, termios
->c_cflag
))
520 max3100_enable_ms(&s
->port
);
523 static void max3100_shutdown(struct uart_port
*port
)
525 struct max3100_port
*s
= to_max3100_port(port
);
528 dev_dbg(&s
->spi
->dev
, "%s\n", __func__
);
533 s
->force_end_work
= 1;
535 del_timer_sync(&s
->timer
);
538 destroy_workqueue(s
->workqueue
);
542 free_irq(port
->irq
, s
);
544 /* set shutdown mode to save power */
545 max3100_sr(s
, MAX3100_WC
| MAX3100_SHDN
, &rx
);
548 static int max3100_startup(struct uart_port
*port
)
550 struct max3100_port
*s
= to_max3100_port(port
);
554 dev_dbg(&s
->spi
->dev
, "%s\n", __func__
);
556 s
->conf
= MAX3100_RM
;
557 s
->baud
= port
->uartclk
/ 16;
563 s
->force_end_work
= 0;
567 sprintf(b
, "max3100-%d", s
->minor
);
568 s
->workqueue
= create_freezable_workqueue(b
);
570 dev_warn(&s
->spi
->dev
, "cannot create workqueue\n");
573 INIT_WORK(&s
->work
, max3100_work
);
575 ret
= request_irq(port
->irq
, max3100_irq
, IRQF_TRIGGER_FALLING
, "max3100", s
);
577 dev_warn(&s
->spi
->dev
, "cannot allocate irq %d\n", port
->irq
);
579 destroy_workqueue(s
->workqueue
);
586 /* wait for clock to settle */
589 max3100_enable_ms(&s
->port
);
594 static const char *max3100_type(struct uart_port
*port
)
596 struct max3100_port
*s
= to_max3100_port(port
);
598 dev_dbg(&s
->spi
->dev
, "%s\n", __func__
);
600 return s
->port
.type
== PORT_MAX3100
? "MAX3100" : NULL
;
603 static void max3100_release_port(struct uart_port
*port
)
605 struct max3100_port
*s
= to_max3100_port(port
);
607 dev_dbg(&s
->spi
->dev
, "%s\n", __func__
);
610 static void max3100_config_port(struct uart_port
*port
, int flags
)
612 struct max3100_port
*s
= to_max3100_port(port
);
614 dev_dbg(&s
->spi
->dev
, "%s\n", __func__
);
616 if (flags
& UART_CONFIG_TYPE
)
617 s
->port
.type
= PORT_MAX3100
;
620 static int max3100_verify_port(struct uart_port
*port
,
621 struct serial_struct
*ser
)
623 struct max3100_port
*s
= to_max3100_port(port
);
626 dev_dbg(&s
->spi
->dev
, "%s\n", __func__
);
628 if (ser
->type
== PORT_UNKNOWN
|| ser
->type
== PORT_MAX3100
)
633 static void max3100_stop_tx(struct uart_port
*port
)
635 struct max3100_port
*s
= to_max3100_port(port
);
637 dev_dbg(&s
->spi
->dev
, "%s\n", __func__
);
640 static int max3100_request_port(struct uart_port
*port
)
642 struct max3100_port
*s
= to_max3100_port(port
);
644 dev_dbg(&s
->spi
->dev
, "%s\n", __func__
);
648 static void max3100_break_ctl(struct uart_port
*port
, int break_state
)
650 struct max3100_port
*s
= to_max3100_port(port
);
652 dev_dbg(&s
->spi
->dev
, "%s\n", __func__
);
655 static const struct uart_ops max3100_ops
= {
656 .tx_empty
= max3100_tx_empty
,
657 .set_mctrl
= max3100_set_mctrl
,
658 .get_mctrl
= max3100_get_mctrl
,
659 .stop_tx
= max3100_stop_tx
,
660 .start_tx
= max3100_start_tx
,
661 .stop_rx
= max3100_stop_rx
,
662 .enable_ms
= max3100_enable_ms
,
663 .break_ctl
= max3100_break_ctl
,
664 .startup
= max3100_startup
,
665 .shutdown
= max3100_shutdown
,
666 .set_termios
= max3100_set_termios
,
667 .type
= max3100_type
,
668 .release_port
= max3100_release_port
,
669 .request_port
= max3100_request_port
,
670 .config_port
= max3100_config_port
,
671 .verify_port
= max3100_verify_port
,
674 static struct uart_driver max3100_uart_driver
= {
675 .owner
= THIS_MODULE
,
676 .driver_name
= "ttyMAX",
677 .dev_name
= "ttyMAX",
678 .major
= MAX3100_MAJOR
,
679 .minor
= MAX3100_MINOR
,
682 static int uart_driver_registered
;
684 static int max3100_probe(struct spi_device
*spi
)
686 struct device
*dev
= &spi
->dev
;
690 mutex_lock(&max3100s_lock
);
692 if (!uart_driver_registered
) {
693 retval
= uart_register_driver(&max3100_uart_driver
);
695 mutex_unlock(&max3100s_lock
);
696 return dev_err_probe(dev
, retval
, "Couldn't register max3100 uart driver\n");
699 uart_driver_registered
= 1;
702 for (i
= 0; i
< MAX_MAX3100
; i
++)
705 if (i
== MAX_MAX3100
) {
706 mutex_unlock(&max3100s_lock
);
707 return dev_err_probe(dev
, -ENOMEM
, "too many MAX3100 chips\n");
710 max3100s
[i
] = kzalloc(sizeof(struct max3100_port
), GFP_KERNEL
);
712 mutex_unlock(&max3100s_lock
);
715 max3100s
[i
]->spi
= spi
;
716 spin_lock_init(&max3100s
[i
]->conf_lock
);
717 spi_set_drvdata(spi
, max3100s
[i
]);
718 max3100s
[i
]->minor
= i
;
719 timer_setup(&max3100s
[i
]->timer
, max3100_timeout
, 0);
721 dev_dbg(&spi
->dev
, "%s: adding port %d\n", __func__
, i
);
722 max3100s
[i
]->port
.irq
= spi
->irq
;
723 max3100s
[i
]->port
.fifosize
= 16;
724 max3100s
[i
]->port
.ops
= &max3100_ops
;
725 max3100s
[i
]->port
.flags
= UPF_SKIP_TEST
| UPF_BOOT_AUTOCONF
;
726 max3100s
[i
]->port
.line
= i
;
727 max3100s
[i
]->port
.type
= PORT_MAX3100
;
728 max3100s
[i
]->port
.dev
= &spi
->dev
;
730 /* Read clock frequency from a property, uart_add_one_port() will fail if it's not set */
731 device_property_read_u32(dev
, "clock-frequency", &max3100s
[i
]->port
.uartclk
);
733 retval
= uart_add_one_port(&max3100_uart_driver
, &max3100s
[i
]->port
);
735 dev_err_probe(dev
, retval
, "uart_add_one_port failed for line %d\n", i
);
737 /* set shutdown mode to save power. Will be woken-up on open */
738 max3100_sr(max3100s
[i
], MAX3100_WC
| MAX3100_SHDN
, &rx
);
739 mutex_unlock(&max3100s_lock
);
743 static void max3100_remove(struct spi_device
*spi
)
745 struct max3100_port
*s
= spi_get_drvdata(spi
);
748 mutex_lock(&max3100s_lock
);
750 /* find out the index for the chip we are removing */
751 for (i
= 0; i
< MAX_MAX3100
; i
++)
752 if (max3100s
[i
] == s
) {
753 dev_dbg(&spi
->dev
, "%s: removing port %d\n", __func__
, i
);
754 uart_remove_one_port(&max3100_uart_driver
, &max3100s
[i
]->port
);
760 WARN_ON(i
== MAX_MAX3100
);
762 /* check if this is the last chip we have */
763 for (i
= 0; i
< MAX_MAX3100
; i
++)
765 mutex_unlock(&max3100s_lock
);
768 pr_debug("removing max3100 driver\n");
769 uart_unregister_driver(&max3100_uart_driver
);
770 uart_driver_registered
= 0;
772 mutex_unlock(&max3100s_lock
);
775 static int max3100_suspend(struct device
*dev
)
777 struct max3100_port
*s
= dev_get_drvdata(dev
);
780 dev_dbg(&s
->spi
->dev
, "%s\n", __func__
);
782 disable_irq(s
->port
.irq
);
785 uart_suspend_port(&max3100_uart_driver
, &s
->port
);
787 /* no HW suspend, so do SW one */
788 max3100_sr(s
, MAX3100_WC
| MAX3100_SHDN
, &rx
);
792 static int max3100_resume(struct device
*dev
)
794 struct max3100_port
*s
= dev_get_drvdata(dev
);
796 dev_dbg(&s
->spi
->dev
, "%s\n", __func__
);
798 uart_resume_port(&max3100_uart_driver
, &s
->port
);
801 enable_irq(s
->port
.irq
);
810 static DEFINE_SIMPLE_DEV_PM_OPS(max3100_pm_ops
, max3100_suspend
, max3100_resume
);
812 static const struct spi_device_id max3100_spi_id
[] = {
816 MODULE_DEVICE_TABLE(spi
, max3100_spi_id
);
818 static const struct of_device_id max3100_of_match
[] = {
819 { .compatible
= "maxim,max3100" },
822 MODULE_DEVICE_TABLE(of
, max3100_of_match
);
824 static struct spi_driver max3100_driver
= {
827 .of_match_table
= max3100_of_match
,
828 .pm
= pm_sleep_ptr(&max3100_pm_ops
),
830 .probe
= max3100_probe
,
831 .remove
= max3100_remove
,
832 .id_table
= max3100_spi_id
,
835 module_spi_driver(max3100_driver
);
837 MODULE_DESCRIPTION("MAX3100 driver");
838 MODULE_AUTHOR("Christian Pellegrin <chripell@evolware.org>");
839 MODULE_LICENSE("GPL");