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