Loading...
1// SPDX-License-Identifier: GPL-2.0+
2/*
3 *
4 * Copyright (C) 2008 Christian Pellegrin <chripell@evolware.org>
5 *
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.
10 *
11 * Example platform data:
12
13 static struct plat_max3100 max3100_plat_data = {
14 .loopback = 0,
15 .crystal = 0,
16 .poll_time = 100,
17 };
18
19 static struct spi_board_info spi_board_info[] = {
20 {
21 .modalias = "max3100",
22 .platform_data = &max3100_plat_data,
23 .irq = IRQ_EINT12,
24 .max_speed_hz = 5*1000*1000,
25 .chip_select = 0,
26 },
27 };
28
29 * The initial minor number is 209 in the low-density serial port:
30 * mknod /dev/ttyMAX0 c 204 209
31 */
32
33#define MAX3100_MAJOR 204
34#define MAX3100_MINOR 209
35/* 4 MAX3100s should be enough for everyone */
36#define MAX_MAX3100 4
37
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>
48
49#include <linux/serial_max3100.h>
50
51#define MAX3100_C (1<<14)
52#define MAX3100_D (0<<14)
53#define MAX3100_W (1<<15)
54#define MAX3100_RX (0<<15)
55
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)
61
62#define MAX3100_T (1<<14)
63#define MAX3100_R (1<<15)
64
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)
76
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)
83
84#define MAX3100_RT (MAX3100_R | MAX3100_T)
85#define MAX3100_RTC (MAX3100_RT | MAX3100_CTS | MAX3100_RAFE)
86
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
91
92struct max3100_port {
93 struct uart_port port;
94 struct spi_device *spi;
95
96 int cts; /* last CTS received for flow ctrl */
97 int tx_empty; /* last TX empty bit */
98
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 */
106
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 */
112
113 int irq; /* irq assigned to the max3100 */
114
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 */
118
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 */
123 int force_end_work;
124 /* need to know we are suspending to avoid deadlock on workqueue */
125 int suspending;
126
127 /* hook for suspending MAX3100 via dedicated pin */
128 void (*max3100_hw_suspend) (int suspend);
129
130 /* poll time (in ms) for ctrl lines */
131 int poll_time;
132 /* and its timer */
133 struct timer_list timer;
134};
135
136static struct max3100_port *max3100s[MAX_MAX3100]; /* the chips */
137static DEFINE_MUTEX(max3100s_lock); /* race on probe */
138
139static int max3100_do_parity(struct max3100_port *s, u16 c)
140{
141 int parity;
142
143 if (s->parity & MAX3100_PARITY_ODD)
144 parity = 1;
145 else
146 parity = 0;
147
148 if (s->parity & MAX3100_7BIT)
149 c &= 0x7f;
150 else
151 c &= 0xff;
152
153 parity = parity ^ (hweight8(c) & 1);
154 return parity;
155}
156
157static int max3100_check_parity(struct max3100_port *s, u16 c)
158{
159 return max3100_do_parity(s, c) == ((c >> 8) & 1);
160}
161
162static void max3100_calc_parity(struct max3100_port *s, u16 *c)
163{
164 if (s->parity & MAX3100_7BIT)
165 *c &= 0x7f;
166 else
167 *c &= 0xff;
168
169 if (s->parity & MAX3100_PARITY_ON)
170 *c |= max3100_do_parity(s, *c) << 8;
171}
172
173static void max3100_work(struct work_struct *w);
174
175static void max3100_dowork(struct max3100_port *s)
176{
177 if (!s->force_end_work && !freezing(current) && !s->suspending)
178 queue_work(s->workqueue, &s->work);
179}
180
181static void max3100_timeout(struct timer_list *t)
182{
183 struct max3100_port *s = from_timer(s, t, timer);
184
185 if (s->port.state) {
186 max3100_dowork(s);
187 mod_timer(&s->timer, jiffies + s->poll_time);
188 }
189}
190
191static int max3100_sr(struct max3100_port *s, u16 tx, u16 *rx)
192{
193 struct spi_message message;
194 u16 etx, erx;
195 int status;
196 struct spi_transfer tran = {
197 .tx_buf = &etx,
198 .rx_buf = &erx,
199 .len = 2,
200 };
201
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);
206 if (status) {
207 dev_warn(&s->spi->dev, "error while calling spi_sync\n");
208 return -EIO;
209 }
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);
213 return 0;
214}
215
216static int max3100_handlerx(struct max3100_port *s, u16 rx)
217{
218 unsigned int status = 0;
219 int ret = 0, cts;
220 u8 ch, flg;
221
222 if (rx & MAX3100_R && s->rx_enabled) {
223 dev_dbg(&s->spi->dev, "%s\n", __func__);
224 ch = rx & (s->parity & MAX3100_7BIT ? 0x7f : 0xff);
225 if (rx & MAX3100_RAFE) {
226 s->port.icount.frame++;
227 flg = TTY_FRAME;
228 status |= MAX3100_STATUS_FE;
229 } else {
230 if (s->parity & MAX3100_PARITY_ON) {
231 if (max3100_check_parity(s, rx)) {
232 s->port.icount.rx++;
233 flg = TTY_NORMAL;
234 } else {
235 s->port.icount.parity++;
236 flg = TTY_PARITY;
237 status |= MAX3100_STATUS_PE;
238 }
239 } else {
240 s->port.icount.rx++;
241 flg = TTY_NORMAL;
242 }
243 }
244 uart_insert_char(&s->port, status, MAX3100_STATUS_OE, ch, flg);
245 ret = 1;
246 }
247
248 cts = (rx & MAX3100_CTS) > 0;
249 if (s->cts != cts) {
250 s->cts = cts;
251 uart_handle_cts_change(&s->port, cts);
252 }
253
254 return ret;
255}
256
257static void max3100_work(struct work_struct *w)
258{
259 struct max3100_port *s = container_of(w, struct max3100_port, work);
260 int rxchars;
261 u16 tx, rx;
262 int conf, cconf, crts;
263 struct circ_buf *xmit = &s->port.state->xmit;
264
265 dev_dbg(&s->spi->dev, "%s\n", __func__);
266
267 rxchars = 0;
268 do {
269 spin_lock(&s->conf_lock);
270 conf = s->conf;
271 cconf = s->conf_commit;
272 s->conf_commit = 0;
273 crts = s->rts_commit;
274 s->rts_commit = 0;
275 spin_unlock(&s->conf_lock);
276 if (cconf)
277 max3100_sr(s, MAX3100_WC | conf, &rx);
278 if (crts) {
279 max3100_sr(s, MAX3100_WD | MAX3100_TE |
280 (s->rts ? MAX3100_RTS : 0), &rx);
281 rxchars += max3100_handlerx(s, rx);
282 }
283
284 max3100_sr(s, MAX3100_RD, &rx);
285 rxchars += max3100_handlerx(s, rx);
286
287 if (rx & MAX3100_T) {
288 tx = 0xffff;
289 if (s->port.x_char) {
290 tx = s->port.x_char;
291 s->port.icount.tx++;
292 s->port.x_char = 0;
293 } else if (!uart_circ_empty(xmit) &&
294 !uart_tx_stopped(&s->port)) {
295 tx = xmit->buf[xmit->tail];
296 uart_xmit_advance(&s->port, 1);
297 }
298 if (tx != 0xffff) {
299 max3100_calc_parity(s, &tx);
300 tx |= MAX3100_WD | (s->rts ? MAX3100_RTS : 0);
301 max3100_sr(s, tx, &rx);
302 rxchars += max3100_handlerx(s, rx);
303 }
304 }
305
306 if (rxchars > 16) {
307 tty_flip_buffer_push(&s->port.state->port);
308 rxchars = 0;
309 }
310 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
311 uart_write_wakeup(&s->port);
312
313 } while (!s->force_end_work &&
314 !freezing(current) &&
315 ((rx & MAX3100_R) ||
316 (!uart_circ_empty(xmit) &&
317 !uart_tx_stopped(&s->port))));
318
319 if (rxchars > 0)
320 tty_flip_buffer_push(&s->port.state->port);
321}
322
323static irqreturn_t max3100_irq(int irqno, void *dev_id)
324{
325 struct max3100_port *s = dev_id;
326
327 dev_dbg(&s->spi->dev, "%s\n", __func__);
328
329 max3100_dowork(s);
330 return IRQ_HANDLED;
331}
332
333static void max3100_enable_ms(struct uart_port *port)
334{
335 struct max3100_port *s = container_of(port,
336 struct max3100_port,
337 port);
338
339 if (s->poll_time > 0)
340 mod_timer(&s->timer, jiffies);
341 dev_dbg(&s->spi->dev, "%s\n", __func__);
342}
343
344static void max3100_start_tx(struct uart_port *port)
345{
346 struct max3100_port *s = container_of(port,
347 struct max3100_port,
348 port);
349
350 dev_dbg(&s->spi->dev, "%s\n", __func__);
351
352 max3100_dowork(s);
353}
354
355static void max3100_stop_rx(struct uart_port *port)
356{
357 struct max3100_port *s = container_of(port,
358 struct max3100_port,
359 port);
360
361 dev_dbg(&s->spi->dev, "%s\n", __func__);
362
363 s->rx_enabled = 0;
364 spin_lock(&s->conf_lock);
365 s->conf &= ~MAX3100_RM;
366 s->conf_commit = 1;
367 spin_unlock(&s->conf_lock);
368 max3100_dowork(s);
369}
370
371static unsigned int max3100_tx_empty(struct uart_port *port)
372{
373 struct max3100_port *s = container_of(port,
374 struct max3100_port,
375 port);
376
377 dev_dbg(&s->spi->dev, "%s\n", __func__);
378
379 /* may not be truly up-to-date */
380 max3100_dowork(s);
381 return s->tx_empty;
382}
383
384static unsigned int max3100_get_mctrl(struct uart_port *port)
385{
386 struct max3100_port *s = container_of(port,
387 struct max3100_port,
388 port);
389
390 dev_dbg(&s->spi->dev, "%s\n", __func__);
391
392 /* may not be truly up-to-date */
393 max3100_dowork(s);
394 /* always assert DCD and DSR since these lines are not wired */
395 return (s->cts ? TIOCM_CTS : 0) | TIOCM_DSR | TIOCM_CAR;
396}
397
398static void max3100_set_mctrl(struct uart_port *port, unsigned int mctrl)
399{
400 struct max3100_port *s = container_of(port,
401 struct max3100_port,
402 port);
403 int rts;
404
405 dev_dbg(&s->spi->dev, "%s\n", __func__);
406
407 rts = (mctrl & TIOCM_RTS) > 0;
408
409 spin_lock(&s->conf_lock);
410 if (s->rts != rts) {
411 s->rts = rts;
412 s->rts_commit = 1;
413 max3100_dowork(s);
414 }
415 spin_unlock(&s->conf_lock);
416}
417
418static void
419max3100_set_termios(struct uart_port *port, struct ktermios *termios,
420 const struct ktermios *old)
421{
422 struct max3100_port *s = container_of(port,
423 struct max3100_port,
424 port);
425 int baud = 0;
426 unsigned cflag;
427 u32 param_new, param_mask, parity = 0;
428
429 dev_dbg(&s->spi->dev, "%s\n", __func__);
430
431 cflag = termios->c_cflag;
432 param_mask = 0;
433
434 baud = tty_termios_baud_rate(termios);
435 param_new = s->conf & MAX3100_BAUD;
436 switch (baud) {
437 case 300:
438 if (s->crystal)
439 baud = s->baud;
440 else
441 param_new = 15;
442 break;
443 case 600:
444 param_new = 14 + s->crystal;
445 break;
446 case 1200:
447 param_new = 13 + s->crystal;
448 break;
449 case 2400:
450 param_new = 12 + s->crystal;
451 break;
452 case 4800:
453 param_new = 11 + s->crystal;
454 break;
455 case 9600:
456 param_new = 10 + s->crystal;
457 break;
458 case 19200:
459 param_new = 9 + s->crystal;
460 break;
461 case 38400:
462 param_new = 8 + s->crystal;
463 break;
464 case 57600:
465 param_new = 1 + s->crystal;
466 break;
467 case 115200:
468 param_new = 0 + s->crystal;
469 break;
470 case 230400:
471 if (s->crystal)
472 param_new = 0;
473 else
474 baud = s->baud;
475 break;
476 default:
477 baud = s->baud;
478 }
479 tty_termios_encode_baud_rate(termios, baud, baud);
480 s->baud = baud;
481 param_mask |= MAX3100_BAUD;
482
483 if ((cflag & CSIZE) == CS8) {
484 param_new &= ~MAX3100_L;
485 parity &= ~MAX3100_7BIT;
486 } else {
487 param_new |= MAX3100_L;
488 parity |= MAX3100_7BIT;
489 cflag = (cflag & ~CSIZE) | CS7;
490 }
491 param_mask |= MAX3100_L;
492
493 if (cflag & CSTOPB)
494 param_new |= MAX3100_ST;
495 else
496 param_new &= ~MAX3100_ST;
497 param_mask |= MAX3100_ST;
498
499 if (cflag & PARENB) {
500 param_new |= MAX3100_PE;
501 parity |= MAX3100_PARITY_ON;
502 } else {
503 param_new &= ~MAX3100_PE;
504 parity &= ~MAX3100_PARITY_ON;
505 }
506 param_mask |= MAX3100_PE;
507
508 if (cflag & PARODD)
509 parity |= MAX3100_PARITY_ODD;
510 else
511 parity &= ~MAX3100_PARITY_ODD;
512
513 /* mask termios capabilities we don't support */
514 cflag &= ~CMSPAR;
515 termios->c_cflag = cflag;
516
517 s->port.ignore_status_mask = 0;
518 if (termios->c_iflag & IGNPAR)
519 s->port.ignore_status_mask |=
520 MAX3100_STATUS_PE | MAX3100_STATUS_FE |
521 MAX3100_STATUS_OE;
522
523 if (s->poll_time > 0)
524 del_timer_sync(&s->timer);
525
526 uart_update_timeout(port, termios->c_cflag, baud);
527
528 spin_lock(&s->conf_lock);
529 s->conf = (s->conf & ~param_mask) | (param_new & param_mask);
530 s->conf_commit = 1;
531 s->parity = parity;
532 spin_unlock(&s->conf_lock);
533 max3100_dowork(s);
534
535 if (UART_ENABLE_MS(&s->port, termios->c_cflag))
536 max3100_enable_ms(&s->port);
537}
538
539static void max3100_shutdown(struct uart_port *port)
540{
541 struct max3100_port *s = container_of(port,
542 struct max3100_port,
543 port);
544
545 dev_dbg(&s->spi->dev, "%s\n", __func__);
546
547 if (s->suspending)
548 return;
549
550 s->force_end_work = 1;
551
552 if (s->poll_time > 0)
553 del_timer_sync(&s->timer);
554
555 if (s->workqueue) {
556 destroy_workqueue(s->workqueue);
557 s->workqueue = NULL;
558 }
559 if (s->irq)
560 free_irq(s->irq, s);
561
562 /* set shutdown mode to save power */
563 if (s->max3100_hw_suspend)
564 s->max3100_hw_suspend(1);
565 else {
566 u16 tx, rx;
567
568 tx = MAX3100_WC | MAX3100_SHDN;
569 max3100_sr(s, tx, &rx);
570 }
571}
572
573static int max3100_startup(struct uart_port *port)
574{
575 struct max3100_port *s = container_of(port,
576 struct max3100_port,
577 port);
578 char b[12];
579
580 dev_dbg(&s->spi->dev, "%s\n", __func__);
581
582 s->conf = MAX3100_RM;
583 s->baud = s->crystal ? 230400 : 115200;
584 s->rx_enabled = 1;
585
586 if (s->suspending)
587 return 0;
588
589 s->force_end_work = 0;
590 s->parity = 0;
591 s->rts = 0;
592
593 sprintf(b, "max3100-%d", s->minor);
594 s->workqueue = create_freezable_workqueue(b);
595 if (!s->workqueue) {
596 dev_warn(&s->spi->dev, "cannot create workqueue\n");
597 return -EBUSY;
598 }
599 INIT_WORK(&s->work, max3100_work);
600
601 if (request_irq(s->irq, max3100_irq,
602 IRQF_TRIGGER_FALLING, "max3100", s) < 0) {
603 dev_warn(&s->spi->dev, "cannot allocate irq %d\n", s->irq);
604 s->irq = 0;
605 destroy_workqueue(s->workqueue);
606 s->workqueue = NULL;
607 return -EBUSY;
608 }
609
610 if (s->loopback) {
611 u16 tx, rx;
612 tx = 0x4001;
613 max3100_sr(s, tx, &rx);
614 }
615
616 if (s->max3100_hw_suspend)
617 s->max3100_hw_suspend(0);
618 s->conf_commit = 1;
619 max3100_dowork(s);
620 /* wait for clock to settle */
621 msleep(50);
622
623 max3100_enable_ms(&s->port);
624
625 return 0;
626}
627
628static const char *max3100_type(struct uart_port *port)
629{
630 struct max3100_port *s = container_of(port,
631 struct max3100_port,
632 port);
633
634 dev_dbg(&s->spi->dev, "%s\n", __func__);
635
636 return s->port.type == PORT_MAX3100 ? "MAX3100" : NULL;
637}
638
639static void max3100_release_port(struct uart_port *port)
640{
641 struct max3100_port *s = container_of(port,
642 struct max3100_port,
643 port);
644
645 dev_dbg(&s->spi->dev, "%s\n", __func__);
646}
647
648static void max3100_config_port(struct uart_port *port, int flags)
649{
650 struct max3100_port *s = container_of(port,
651 struct max3100_port,
652 port);
653
654 dev_dbg(&s->spi->dev, "%s\n", __func__);
655
656 if (flags & UART_CONFIG_TYPE)
657 s->port.type = PORT_MAX3100;
658}
659
660static int max3100_verify_port(struct uart_port *port,
661 struct serial_struct *ser)
662{
663 struct max3100_port *s = container_of(port,
664 struct max3100_port,
665 port);
666 int ret = -EINVAL;
667
668 dev_dbg(&s->spi->dev, "%s\n", __func__);
669
670 if (ser->type == PORT_UNKNOWN || ser->type == PORT_MAX3100)
671 ret = 0;
672 return ret;
673}
674
675static void max3100_stop_tx(struct uart_port *port)
676{
677 struct max3100_port *s = container_of(port,
678 struct max3100_port,
679 port);
680
681 dev_dbg(&s->spi->dev, "%s\n", __func__);
682}
683
684static int max3100_request_port(struct uart_port *port)
685{
686 struct max3100_port *s = container_of(port,
687 struct max3100_port,
688 port);
689
690 dev_dbg(&s->spi->dev, "%s\n", __func__);
691 return 0;
692}
693
694static void max3100_break_ctl(struct uart_port *port, int break_state)
695{
696 struct max3100_port *s = container_of(port,
697 struct max3100_port,
698 port);
699
700 dev_dbg(&s->spi->dev, "%s\n", __func__);
701}
702
703static const struct uart_ops max3100_ops = {
704 .tx_empty = max3100_tx_empty,
705 .set_mctrl = max3100_set_mctrl,
706 .get_mctrl = max3100_get_mctrl,
707 .stop_tx = max3100_stop_tx,
708 .start_tx = max3100_start_tx,
709 .stop_rx = max3100_stop_rx,
710 .enable_ms = max3100_enable_ms,
711 .break_ctl = max3100_break_ctl,
712 .startup = max3100_startup,
713 .shutdown = max3100_shutdown,
714 .set_termios = max3100_set_termios,
715 .type = max3100_type,
716 .release_port = max3100_release_port,
717 .request_port = max3100_request_port,
718 .config_port = max3100_config_port,
719 .verify_port = max3100_verify_port,
720};
721
722static struct uart_driver max3100_uart_driver = {
723 .owner = THIS_MODULE,
724 .driver_name = "ttyMAX",
725 .dev_name = "ttyMAX",
726 .major = MAX3100_MAJOR,
727 .minor = MAX3100_MINOR,
728 .nr = MAX_MAX3100,
729};
730static int uart_driver_registered;
731
732static int max3100_probe(struct spi_device *spi)
733{
734 int i, retval;
735 struct plat_max3100 *pdata;
736 u16 tx, rx;
737
738 mutex_lock(&max3100s_lock);
739
740 if (!uart_driver_registered) {
741 uart_driver_registered = 1;
742 retval = uart_register_driver(&max3100_uart_driver);
743 if (retval) {
744 printk(KERN_ERR "Couldn't register max3100 uart driver\n");
745 mutex_unlock(&max3100s_lock);
746 return retval;
747 }
748 }
749
750 for (i = 0; i < MAX_MAX3100; i++)
751 if (!max3100s[i])
752 break;
753 if (i == MAX_MAX3100) {
754 dev_warn(&spi->dev, "too many MAX3100 chips\n");
755 mutex_unlock(&max3100s_lock);
756 return -ENOMEM;
757 }
758
759 max3100s[i] = kzalloc(sizeof(struct max3100_port), GFP_KERNEL);
760 if (!max3100s[i]) {
761 dev_warn(&spi->dev,
762 "kmalloc for max3100 structure %d failed!\n", i);
763 mutex_unlock(&max3100s_lock);
764 return -ENOMEM;
765 }
766 max3100s[i]->spi = spi;
767 max3100s[i]->irq = spi->irq;
768 spin_lock_init(&max3100s[i]->conf_lock);
769 spi_set_drvdata(spi, max3100s[i]);
770 pdata = dev_get_platdata(&spi->dev);
771 max3100s[i]->crystal = pdata->crystal;
772 max3100s[i]->loopback = pdata->loopback;
773 max3100s[i]->poll_time = msecs_to_jiffies(pdata->poll_time);
774 if (pdata->poll_time > 0 && max3100s[i]->poll_time == 0)
775 max3100s[i]->poll_time = 1;
776 max3100s[i]->max3100_hw_suspend = pdata->max3100_hw_suspend;
777 max3100s[i]->minor = i;
778 timer_setup(&max3100s[i]->timer, max3100_timeout, 0);
779
780 dev_dbg(&spi->dev, "%s: adding port %d\n", __func__, i);
781 max3100s[i]->port.irq = max3100s[i]->irq;
782 max3100s[i]->port.uartclk = max3100s[i]->crystal ? 3686400 : 1843200;
783 max3100s[i]->port.fifosize = 16;
784 max3100s[i]->port.ops = &max3100_ops;
785 max3100s[i]->port.flags = UPF_SKIP_TEST | UPF_BOOT_AUTOCONF;
786 max3100s[i]->port.line = i;
787 max3100s[i]->port.type = PORT_MAX3100;
788 max3100s[i]->port.dev = &spi->dev;
789 retval = uart_add_one_port(&max3100_uart_driver, &max3100s[i]->port);
790 if (retval < 0)
791 dev_warn(&spi->dev,
792 "uart_add_one_port failed for line %d with error %d\n",
793 i, retval);
794
795 /* set shutdown mode to save power. Will be woken-up on open */
796 if (max3100s[i]->max3100_hw_suspend)
797 max3100s[i]->max3100_hw_suspend(1);
798 else {
799 tx = MAX3100_WC | MAX3100_SHDN;
800 max3100_sr(max3100s[i], tx, &rx);
801 }
802 mutex_unlock(&max3100s_lock);
803 return 0;
804}
805
806static void max3100_remove(struct spi_device *spi)
807{
808 struct max3100_port *s = spi_get_drvdata(spi);
809 int i;
810
811 mutex_lock(&max3100s_lock);
812
813 /* find out the index for the chip we are removing */
814 for (i = 0; i < MAX_MAX3100; i++)
815 if (max3100s[i] == s) {
816 dev_dbg(&spi->dev, "%s: removing port %d\n", __func__, i);
817 uart_remove_one_port(&max3100_uart_driver, &max3100s[i]->port);
818 kfree(max3100s[i]);
819 max3100s[i] = NULL;
820 break;
821 }
822
823 WARN_ON(i == MAX_MAX3100);
824
825 /* check if this is the last chip we have */
826 for (i = 0; i < MAX_MAX3100; i++)
827 if (max3100s[i]) {
828 mutex_unlock(&max3100s_lock);
829 return;
830 }
831 pr_debug("removing max3100 driver\n");
832 uart_unregister_driver(&max3100_uart_driver);
833
834 mutex_unlock(&max3100s_lock);
835}
836
837#ifdef CONFIG_PM_SLEEP
838
839static int max3100_suspend(struct device *dev)
840{
841 struct max3100_port *s = dev_get_drvdata(dev);
842
843 dev_dbg(&s->spi->dev, "%s\n", __func__);
844
845 disable_irq(s->irq);
846
847 s->suspending = 1;
848 uart_suspend_port(&max3100_uart_driver, &s->port);
849
850 if (s->max3100_hw_suspend)
851 s->max3100_hw_suspend(1);
852 else {
853 /* no HW suspend, so do SW one */
854 u16 tx, rx;
855
856 tx = MAX3100_WC | MAX3100_SHDN;
857 max3100_sr(s, tx, &rx);
858 }
859 return 0;
860}
861
862static int max3100_resume(struct device *dev)
863{
864 struct max3100_port *s = dev_get_drvdata(dev);
865
866 dev_dbg(&s->spi->dev, "%s\n", __func__);
867
868 if (s->max3100_hw_suspend)
869 s->max3100_hw_suspend(0);
870 uart_resume_port(&max3100_uart_driver, &s->port);
871 s->suspending = 0;
872
873 enable_irq(s->irq);
874
875 s->conf_commit = 1;
876 if (s->workqueue)
877 max3100_dowork(s);
878
879 return 0;
880}
881
882static SIMPLE_DEV_PM_OPS(max3100_pm_ops, max3100_suspend, max3100_resume);
883#define MAX3100_PM_OPS (&max3100_pm_ops)
884
885#else
886#define MAX3100_PM_OPS NULL
887#endif
888
889static struct spi_driver max3100_driver = {
890 .driver = {
891 .name = "max3100",
892 .pm = MAX3100_PM_OPS,
893 },
894 .probe = max3100_probe,
895 .remove = max3100_remove,
896};
897
898module_spi_driver(max3100_driver);
899
900MODULE_DESCRIPTION("MAX3100 driver");
901MODULE_AUTHOR("Christian Pellegrin <chripell@evolware.org>");
902MODULE_LICENSE("GPL");
903MODULE_ALIAS("spi:max3100");
1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2008 Christian Pellegrin <chripell@evolware.org>
4 *
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.
9 *
10 * The initial minor number is 209 in the low-density serial port:
11 * mknod /dev/ttyMAX0 c 204 209
12 */
13
14#define MAX3100_MAJOR 204
15#define MAX3100_MINOR 209
16/* 4 MAX3100s should be enough for everyone */
17#define MAX_MAX3100 4
18
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>
25#include <linux/pm.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>
34
35#include <linux/unaligned.h>
36
37#define MAX3100_C (1<<14)
38#define MAX3100_D (0<<14)
39#define MAX3100_W (1<<15)
40#define MAX3100_RX (0<<15)
41
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)
47
48#define MAX3100_T (1<<14)
49#define MAX3100_R (1<<15)
50
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)
62
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)
69
70#define MAX3100_RT (MAX3100_R | MAX3100_T)
71#define MAX3100_RTC (MAX3100_RT | MAX3100_CTS | MAX3100_RAFE)
72
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
77
78struct max3100_port {
79 struct uart_port port;
80 struct spi_device *spi;
81
82 int cts; /* last CTS received for flow ctrl */
83 int tx_empty; /* last TX empty bit */
84
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 */
92
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 */
98
99 int minor; /* minor number */
100 int loopback_commit; /* need to change loopback */
101 int loopback; /* 1 if we are in loopback mode */
102
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 */
107 int force_end_work;
108 /* need to know we are suspending to avoid deadlock on workqueue */
109 int suspending;
110
111 struct timer_list timer;
112};
113
114static inline struct max3100_port *to_max3100_port(struct uart_port *port)
115{
116 return container_of(port, struct max3100_port, port);
117}
118
119static struct max3100_port *max3100s[MAX_MAX3100]; /* the chips */
120static DEFINE_MUTEX(max3100s_lock); /* race on probe */
121
122static int max3100_do_parity(struct max3100_port *s, u16 c)
123{
124 int parity;
125
126 if (s->parity & MAX3100_PARITY_ODD)
127 parity = 1;
128 else
129 parity = 0;
130
131 if (s->parity & MAX3100_7BIT)
132 c &= 0x7f;
133 else
134 c &= 0xff;
135
136 parity = parity ^ (hweight8(c) & 1);
137 return parity;
138}
139
140static int max3100_check_parity(struct max3100_port *s, u16 c)
141{
142 return max3100_do_parity(s, c) == ((c >> 8) & 1);
143}
144
145static void max3100_calc_parity(struct max3100_port *s, u16 *c)
146{
147 if (s->parity & MAX3100_7BIT)
148 *c &= 0x7f;
149 else
150 *c &= 0xff;
151
152 if (s->parity & MAX3100_PARITY_ON)
153 *c |= max3100_do_parity(s, *c) << 8;
154}
155
156static int max3100_sr(struct max3100_port *s, u16 tx, u16 *rx)
157{
158 struct spi_message message;
159 __be16 etx, erx;
160 int status;
161 struct spi_transfer tran = {
162 .tx_buf = &etx,
163 .rx_buf = &erx,
164 .len = 2,
165 };
166
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);
171 if (status) {
172 dev_warn(&s->spi->dev, "error while calling spi_sync\n");
173 return -EIO;
174 }
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);
178 return 0;
179}
180
181static int max3100_handlerx_unlocked(struct max3100_port *s, u16 rx)
182{
183 unsigned int status = 0;
184 int ret = 0, cts;
185 u8 ch, flg;
186
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++;
192 flg = TTY_FRAME;
193 status |= MAX3100_STATUS_FE;
194 } else {
195 if (s->parity & MAX3100_PARITY_ON) {
196 if (max3100_check_parity(s, rx)) {
197 s->port.icount.rx++;
198 flg = TTY_NORMAL;
199 } else {
200 s->port.icount.parity++;
201 flg = TTY_PARITY;
202 status |= MAX3100_STATUS_PE;
203 }
204 } else {
205 s->port.icount.rx++;
206 flg = TTY_NORMAL;
207 }
208 }
209 uart_insert_char(&s->port, status, MAX3100_STATUS_OE, ch, flg);
210 ret = 1;
211 }
212
213 cts = (rx & MAX3100_CTS) > 0;
214 if (s->cts != cts) {
215 s->cts = cts;
216 uart_handle_cts_change(&s->port, cts);
217 }
218
219 return ret;
220}
221
222static int max3100_handlerx(struct max3100_port *s, u16 rx)
223{
224 unsigned long flags;
225 int ret;
226
227 uart_port_lock_irqsave(&s->port, &flags);
228 ret = max3100_handlerx_unlocked(s, rx);
229 uart_port_unlock_irqrestore(&s->port, flags);
230 return ret;
231}
232
233static void max3100_work(struct work_struct *w)
234{
235 struct max3100_port *s = container_of(w, struct max3100_port, work);
236 struct tty_port *tport = &s->port.state->port;
237 unsigned char ch;
238 int conf, cconf, cloopback, crts;
239 int rxchars;
240 u16 tx, rx;
241
242 dev_dbg(&s->spi->dev, "%s\n", __func__);
243
244 rxchars = 0;
245 do {
246 spin_lock(&s->conf_lock);
247 conf = s->conf;
248 cconf = s->conf_commit;
249 s->conf_commit = 0;
250 cloopback = s->loopback_commit;
251 s->loopback_commit = 0;
252 crts = s->rts_commit;
253 s->rts_commit = 0;
254 spin_unlock(&s->conf_lock);
255 if (cconf)
256 max3100_sr(s, MAX3100_WC | conf, &rx);
257 if (cloopback)
258 max3100_sr(s, 0x4001, &rx);
259 if (crts) {
260 max3100_sr(s, MAX3100_WD | MAX3100_TE |
261 (s->rts ? MAX3100_RTS : 0), &rx);
262 rxchars += max3100_handlerx(s, rx);
263 }
264
265 max3100_sr(s, MAX3100_RD, &rx);
266 rxchars += max3100_handlerx(s, rx);
267
268 if (rx & MAX3100_T) {
269 tx = 0xffff;
270 if (s->port.x_char) {
271 tx = s->port.x_char;
272 s->port.icount.tx++;
273 s->port.x_char = 0;
274 } else if (!uart_tx_stopped(&s->port) &&
275 uart_fifo_get(&s->port, &ch)) {
276 tx = ch;
277 }
278 if (tx != 0xffff) {
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);
283 }
284 }
285
286 if (rxchars > 16) {
287 tty_flip_buffer_push(&s->port.state->port);
288 rxchars = 0;
289 }
290 if (kfifo_len(&tport->xmit_fifo) < WAKEUP_CHARS)
291 uart_write_wakeup(&s->port);
292
293 } while (!s->force_end_work &&
294 !freezing(current) &&
295 ((rx & MAX3100_R) ||
296 (!kfifo_is_empty(&tport->xmit_fifo) &&
297 !uart_tx_stopped(&s->port))));
298
299 if (rxchars > 0)
300 tty_flip_buffer_push(&s->port.state->port);
301}
302
303static void max3100_dowork(struct max3100_port *s)
304{
305 if (!s->force_end_work && !freezing(current) && !s->suspending)
306 queue_work(s->workqueue, &s->work);
307}
308
309static void max3100_timeout(struct timer_list *t)
310{
311 struct max3100_port *s = from_timer(s, t, timer);
312
313 max3100_dowork(s);
314 mod_timer(&s->timer, jiffies + uart_poll_timeout(&s->port));
315}
316
317static irqreturn_t max3100_irq(int irqno, void *dev_id)
318{
319 struct max3100_port *s = dev_id;
320
321 dev_dbg(&s->spi->dev, "%s\n", __func__);
322
323 max3100_dowork(s);
324 return IRQ_HANDLED;
325}
326
327static void max3100_enable_ms(struct uart_port *port)
328{
329 struct max3100_port *s = to_max3100_port(port);
330
331 mod_timer(&s->timer, jiffies);
332 dev_dbg(&s->spi->dev, "%s\n", __func__);
333}
334
335static void max3100_start_tx(struct uart_port *port)
336{
337 struct max3100_port *s = to_max3100_port(port);
338
339 dev_dbg(&s->spi->dev, "%s\n", __func__);
340
341 max3100_dowork(s);
342}
343
344static void max3100_stop_rx(struct uart_port *port)
345{
346 struct max3100_port *s = to_max3100_port(port);
347
348 dev_dbg(&s->spi->dev, "%s\n", __func__);
349
350 s->rx_enabled = 0;
351 spin_lock(&s->conf_lock);
352 s->conf &= ~MAX3100_RM;
353 s->conf_commit = 1;
354 spin_unlock(&s->conf_lock);
355 max3100_dowork(s);
356}
357
358static unsigned int max3100_tx_empty(struct uart_port *port)
359{
360 struct max3100_port *s = to_max3100_port(port);
361
362 dev_dbg(&s->spi->dev, "%s\n", __func__);
363
364 /* may not be truly up-to-date */
365 max3100_dowork(s);
366 return s->tx_empty;
367}
368
369static unsigned int max3100_get_mctrl(struct uart_port *port)
370{
371 struct max3100_port *s = to_max3100_port(port);
372
373 dev_dbg(&s->spi->dev, "%s\n", __func__);
374
375 /* may not be truly up-to-date */
376 max3100_dowork(s);
377 /* always assert DCD and DSR since these lines are not wired */
378 return (s->cts ? TIOCM_CTS : 0) | TIOCM_DSR | TIOCM_CAR;
379}
380
381static void max3100_set_mctrl(struct uart_port *port, unsigned int mctrl)
382{
383 struct max3100_port *s = to_max3100_port(port);
384 int loopback, rts;
385
386 dev_dbg(&s->spi->dev, "%s\n", __func__);
387
388 loopback = (mctrl & TIOCM_LOOP) > 0;
389 rts = (mctrl & TIOCM_RTS) > 0;
390
391 spin_lock(&s->conf_lock);
392 if (s->loopback != loopback) {
393 s->loopback = loopback;
394 s->loopback_commit = 1;
395 }
396 if (s->rts != rts) {
397 s->rts = rts;
398 s->rts_commit = 1;
399 }
400 if (s->loopback_commit || s->rts_commit)
401 max3100_dowork(s);
402 spin_unlock(&s->conf_lock);
403}
404
405static void
406max3100_set_termios(struct uart_port *port, struct ktermios *termios,
407 const struct ktermios *old)
408{
409 struct max3100_port *s = to_max3100_port(port);
410 unsigned int baud = port->uartclk / 16;
411 unsigned int baud230400 = (baud == 230400) ? 1 : 0;
412 unsigned cflag;
413 u32 param_new, param_mask, parity = 0;
414
415 dev_dbg(&s->spi->dev, "%s\n", __func__);
416
417 cflag = termios->c_cflag;
418 param_mask = 0;
419
420 baud = tty_termios_baud_rate(termios);
421 param_new = s->conf & MAX3100_BAUD;
422 switch (baud) {
423 case 300:
424 if (baud230400)
425 baud = s->baud;
426 else
427 param_new = 15;
428 break;
429 case 600:
430 param_new = 14 + baud230400;
431 break;
432 case 1200:
433 param_new = 13 + baud230400;
434 break;
435 case 2400:
436 param_new = 12 + baud230400;
437 break;
438 case 4800:
439 param_new = 11 + baud230400;
440 break;
441 case 9600:
442 param_new = 10 + baud230400;
443 break;
444 case 19200:
445 param_new = 9 + baud230400;
446 break;
447 case 38400:
448 param_new = 8 + baud230400;
449 break;
450 case 57600:
451 param_new = 1 + baud230400;
452 break;
453 case 115200:
454 param_new = 0 + baud230400;
455 break;
456 case 230400:
457 if (baud230400)
458 param_new = 0;
459 else
460 baud = s->baud;
461 break;
462 default:
463 baud = s->baud;
464 }
465 tty_termios_encode_baud_rate(termios, baud, baud);
466 s->baud = baud;
467 param_mask |= MAX3100_BAUD;
468
469 if ((cflag & CSIZE) == CS8) {
470 param_new &= ~MAX3100_L;
471 parity &= ~MAX3100_7BIT;
472 } else {
473 param_new |= MAX3100_L;
474 parity |= MAX3100_7BIT;
475 cflag = (cflag & ~CSIZE) | CS7;
476 }
477 param_mask |= MAX3100_L;
478
479 if (cflag & CSTOPB)
480 param_new |= MAX3100_ST;
481 else
482 param_new &= ~MAX3100_ST;
483 param_mask |= MAX3100_ST;
484
485 if (cflag & PARENB) {
486 param_new |= MAX3100_PE;
487 parity |= MAX3100_PARITY_ON;
488 } else {
489 param_new &= ~MAX3100_PE;
490 parity &= ~MAX3100_PARITY_ON;
491 }
492 param_mask |= MAX3100_PE;
493
494 if (cflag & PARODD)
495 parity |= MAX3100_PARITY_ODD;
496 else
497 parity &= ~MAX3100_PARITY_ODD;
498
499 /* mask termios capabilities we don't support */
500 cflag &= ~CMSPAR;
501 termios->c_cflag = cflag;
502
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 |
507 MAX3100_STATUS_OE;
508
509 del_timer_sync(&s->timer);
510 uart_update_timeout(port, termios->c_cflag, baud);
511
512 spin_lock(&s->conf_lock);
513 s->conf = (s->conf & ~param_mask) | (param_new & param_mask);
514 s->conf_commit = 1;
515 s->parity = parity;
516 spin_unlock(&s->conf_lock);
517 max3100_dowork(s);
518
519 if (UART_ENABLE_MS(&s->port, termios->c_cflag))
520 max3100_enable_ms(&s->port);
521}
522
523static void max3100_shutdown(struct uart_port *port)
524{
525 struct max3100_port *s = to_max3100_port(port);
526 u16 rx;
527
528 dev_dbg(&s->spi->dev, "%s\n", __func__);
529
530 if (s->suspending)
531 return;
532
533 s->force_end_work = 1;
534
535 del_timer_sync(&s->timer);
536
537 if (s->workqueue) {
538 destroy_workqueue(s->workqueue);
539 s->workqueue = NULL;
540 }
541 if (port->irq)
542 free_irq(port->irq, s);
543
544 /* set shutdown mode to save power */
545 max3100_sr(s, MAX3100_WC | MAX3100_SHDN, &rx);
546}
547
548static int max3100_startup(struct uart_port *port)
549{
550 struct max3100_port *s = to_max3100_port(port);
551 char b[12];
552 int ret;
553
554 dev_dbg(&s->spi->dev, "%s\n", __func__);
555
556 s->conf = MAX3100_RM;
557 s->baud = port->uartclk / 16;
558 s->rx_enabled = 1;
559
560 if (s->suspending)
561 return 0;
562
563 s->force_end_work = 0;
564 s->parity = 0;
565 s->rts = 0;
566
567 sprintf(b, "max3100-%d", s->minor);
568 s->workqueue = create_freezable_workqueue(b);
569 if (!s->workqueue) {
570 dev_warn(&s->spi->dev, "cannot create workqueue\n");
571 return -EBUSY;
572 }
573 INIT_WORK(&s->work, max3100_work);
574
575 ret = request_irq(port->irq, max3100_irq, IRQF_TRIGGER_FALLING, "max3100", s);
576 if (ret < 0) {
577 dev_warn(&s->spi->dev, "cannot allocate irq %d\n", port->irq);
578 port->irq = 0;
579 destroy_workqueue(s->workqueue);
580 s->workqueue = NULL;
581 return -EBUSY;
582 }
583
584 s->conf_commit = 1;
585 max3100_dowork(s);
586 /* wait for clock to settle */
587 msleep(50);
588
589 max3100_enable_ms(&s->port);
590
591 return 0;
592}
593
594static const char *max3100_type(struct uart_port *port)
595{
596 struct max3100_port *s = to_max3100_port(port);
597
598 dev_dbg(&s->spi->dev, "%s\n", __func__);
599
600 return s->port.type == PORT_MAX3100 ? "MAX3100" : NULL;
601}
602
603static void max3100_release_port(struct uart_port *port)
604{
605 struct max3100_port *s = to_max3100_port(port);
606
607 dev_dbg(&s->spi->dev, "%s\n", __func__);
608}
609
610static void max3100_config_port(struct uart_port *port, int flags)
611{
612 struct max3100_port *s = to_max3100_port(port);
613
614 dev_dbg(&s->spi->dev, "%s\n", __func__);
615
616 if (flags & UART_CONFIG_TYPE)
617 s->port.type = PORT_MAX3100;
618}
619
620static int max3100_verify_port(struct uart_port *port,
621 struct serial_struct *ser)
622{
623 struct max3100_port *s = to_max3100_port(port);
624 int ret = -EINVAL;
625
626 dev_dbg(&s->spi->dev, "%s\n", __func__);
627
628 if (ser->type == PORT_UNKNOWN || ser->type == PORT_MAX3100)
629 ret = 0;
630 return ret;
631}
632
633static void max3100_stop_tx(struct uart_port *port)
634{
635 struct max3100_port *s = to_max3100_port(port);
636
637 dev_dbg(&s->spi->dev, "%s\n", __func__);
638}
639
640static int max3100_request_port(struct uart_port *port)
641{
642 struct max3100_port *s = to_max3100_port(port);
643
644 dev_dbg(&s->spi->dev, "%s\n", __func__);
645 return 0;
646}
647
648static void max3100_break_ctl(struct uart_port *port, int break_state)
649{
650 struct max3100_port *s = to_max3100_port(port);
651
652 dev_dbg(&s->spi->dev, "%s\n", __func__);
653}
654
655static 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,
672};
673
674static 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,
680 .nr = MAX_MAX3100,
681};
682static int uart_driver_registered;
683
684static int max3100_probe(struct spi_device *spi)
685{
686 struct device *dev = &spi->dev;
687 int i, retval;
688 u16 rx;
689
690 mutex_lock(&max3100s_lock);
691
692 if (!uart_driver_registered) {
693 retval = uart_register_driver(&max3100_uart_driver);
694 if (retval) {
695 mutex_unlock(&max3100s_lock);
696 return dev_err_probe(dev, retval, "Couldn't register max3100 uart driver\n");
697 }
698
699 uart_driver_registered = 1;
700 }
701
702 for (i = 0; i < MAX_MAX3100; i++)
703 if (!max3100s[i])
704 break;
705 if (i == MAX_MAX3100) {
706 mutex_unlock(&max3100s_lock);
707 return dev_err_probe(dev, -ENOMEM, "too many MAX3100 chips\n");
708 }
709
710 max3100s[i] = kzalloc(sizeof(struct max3100_port), GFP_KERNEL);
711 if (!max3100s[i]) {
712 mutex_unlock(&max3100s_lock);
713 return -ENOMEM;
714 }
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);
720
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;
729
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);
732
733 retval = uart_add_one_port(&max3100_uart_driver, &max3100s[i]->port);
734 if (retval < 0)
735 dev_err_probe(dev, retval, "uart_add_one_port failed for line %d\n", i);
736
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);
740 return 0;
741}
742
743static void max3100_remove(struct spi_device *spi)
744{
745 struct max3100_port *s = spi_get_drvdata(spi);
746 int i;
747
748 mutex_lock(&max3100s_lock);
749
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);
755 kfree(max3100s[i]);
756 max3100s[i] = NULL;
757 break;
758 }
759
760 WARN_ON(i == MAX_MAX3100);
761
762 /* check if this is the last chip we have */
763 for (i = 0; i < MAX_MAX3100; i++)
764 if (max3100s[i]) {
765 mutex_unlock(&max3100s_lock);
766 return;
767 }
768 pr_debug("removing max3100 driver\n");
769 uart_unregister_driver(&max3100_uart_driver);
770 uart_driver_registered = 0;
771
772 mutex_unlock(&max3100s_lock);
773}
774
775static int max3100_suspend(struct device *dev)
776{
777 struct max3100_port *s = dev_get_drvdata(dev);
778 u16 rx;
779
780 dev_dbg(&s->spi->dev, "%s\n", __func__);
781
782 disable_irq(s->port.irq);
783
784 s->suspending = 1;
785 uart_suspend_port(&max3100_uart_driver, &s->port);
786
787 /* no HW suspend, so do SW one */
788 max3100_sr(s, MAX3100_WC | MAX3100_SHDN, &rx);
789 return 0;
790}
791
792static int max3100_resume(struct device *dev)
793{
794 struct max3100_port *s = dev_get_drvdata(dev);
795
796 dev_dbg(&s->spi->dev, "%s\n", __func__);
797
798 uart_resume_port(&max3100_uart_driver, &s->port);
799 s->suspending = 0;
800
801 enable_irq(s->port.irq);
802
803 s->conf_commit = 1;
804 if (s->workqueue)
805 max3100_dowork(s);
806
807 return 0;
808}
809
810static DEFINE_SIMPLE_DEV_PM_OPS(max3100_pm_ops, max3100_suspend, max3100_resume);
811
812static const struct spi_device_id max3100_spi_id[] = {
813 { "max3100" },
814 { }
815};
816MODULE_DEVICE_TABLE(spi, max3100_spi_id);
817
818static const struct of_device_id max3100_of_match[] = {
819 { .compatible = "maxim,max3100" },
820 { }
821};
822MODULE_DEVICE_TABLE(of, max3100_of_match);
823
824static struct spi_driver max3100_driver = {
825 .driver = {
826 .name = "max3100",
827 .of_match_table = max3100_of_match,
828 .pm = pm_sleep_ptr(&max3100_pm_ops),
829 },
830 .probe = max3100_probe,
831 .remove = max3100_remove,
832 .id_table = max3100_spi_id,
833};
834
835module_spi_driver(max3100_driver);
836
837MODULE_DESCRIPTION("MAX3100 driver");
838MODULE_AUTHOR("Christian Pellegrin <chripell@evolware.org>");
839MODULE_LICENSE("GPL");