2 * max3107.c - spi uart protocol driver for Maxim 3107
4 * by Christian Pellegrin <chripell@evolware.org>
6 * by Feng Tang <feng.tang@intel.com>
8 * Copyright (C) Aavamobile 2009
10 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
30 #include <linux/delay.h>
31 #include <linux/device.h>
32 #include <linux/serial_core.h>
33 #include <linux/serial.h>
34 #include <linux/tty.h>
35 #include <linux/tty_flip.h>
36 #include <linux/gpio.h>
37 #include <linux/spi/spi.h>
38 #include <linux/freezer.h>
41 static const struct baud_table brg26_ext
[] = {
42 { 300, MAX3107_BRG26_B300
},
43 { 600, MAX3107_BRG26_B600
},
44 { 1200, MAX3107_BRG26_B1200
},
45 { 2400, MAX3107_BRG26_B2400
},
46 { 4800, MAX3107_BRG26_B4800
},
47 { 9600, MAX3107_BRG26_B9600
},
48 { 19200, MAX3107_BRG26_B19200
},
49 { 57600, MAX3107_BRG26_B57600
},
50 { 115200, MAX3107_BRG26_B115200
},
51 { 230400, MAX3107_BRG26_B230400
},
52 { 460800, MAX3107_BRG26_B460800
},
53 { 921600, MAX3107_BRG26_B921600
},
57 static const struct baud_table brg13_int
[] = {
58 { 300, MAX3107_BRG13_IB300
},
59 { 600, MAX3107_BRG13_IB600
},
60 { 1200, MAX3107_BRG13_IB1200
},
61 { 2400, MAX3107_BRG13_IB2400
},
62 { 4800, MAX3107_BRG13_IB4800
},
63 { 9600, MAX3107_BRG13_IB9600
},
64 { 19200, MAX3107_BRG13_IB19200
},
65 { 57600, MAX3107_BRG13_IB57600
},
66 { 115200, MAX3107_BRG13_IB115200
},
67 { 230400, MAX3107_BRG13_IB230400
},
68 { 460800, MAX3107_BRG13_IB460800
},
69 { 921600, MAX3107_BRG13_IB921600
},
73 static u32
get_new_brg(int baud
, struct max3107_port
*s
)
76 const struct baud_table
*baud_tbl
= s
->baud_tbl
;
78 for (i
= 0; i
< 13; i
++) {
79 if (baud
== baud_tbl
[i
].baud
)
80 return baud_tbl
[i
].new_brg
;
86 /* Perform SPI transfer for write/read of device register(s) */
87 int max3107_rw(struct max3107_port
*s
, u8
*tx
, u8
*rx
, int len
)
89 struct spi_message spi_msg
;
90 struct spi_transfer spi_xfer
;
92 /* Initialize SPI ,message */
93 spi_message_init(&spi_msg
);
95 /* Initialize SPI transfer */
96 memset(&spi_xfer
, 0, sizeof spi_xfer
);
100 spi_xfer
.speed_hz
= MAX3107_SPI_SPEED
;
102 /* Add SPI transfer to SPI message */
103 spi_message_add_tail(&spi_xfer
, &spi_msg
);
105 #ifdef DBG_TRACE_SPI_DATA
108 pr_info("tx len %d:\n", spi_xfer
.len
);
109 for (i
= 0 ; i
< spi_xfer
.len
&& i
< 32 ; i
++)
110 pr_info(" %x", ((u8
*)spi_xfer
.tx_buf
)[i
]);
115 /* Perform synchronous SPI transfer */
116 if (spi_sync(s
->spi
, &spi_msg
)) {
117 dev_err(&s
->spi
->dev
, "spi_sync failure\n");
121 #ifdef DBG_TRACE_SPI_DATA
122 if (spi_xfer
.rx_buf
) {
124 pr_info("rx len %d:\n", spi_xfer
.len
);
125 for (i
= 0 ; i
< spi_xfer
.len
&& i
< 32 ; i
++)
126 pr_info(" %x", ((u8
*)spi_xfer
.rx_buf
)[i
]);
132 EXPORT_SYMBOL_GPL(max3107_rw
);
134 /* Puts received data to circular buffer */
135 static void put_data_to_circ_buf(struct max3107_port
*s
, unsigned char *data
,
138 struct uart_port
*port
= &s
->port
;
139 struct tty_struct
*tty
;
144 tty
= port
->state
->port
.tty
;
148 /* Insert received data */
149 tty_insert_flip_string(tty
, data
, len
);
150 /* Update RX counter */
151 port
->icount
.rx
+= len
;
154 /* Handle data receiving */
155 static void max3107_handlerx(struct max3107_port
*s
, u16 rxlvl
)
159 int len
; /* SPI transfer buffer length */
168 /* RX fifo is empty */
170 } else if (rxlvl
>= MAX3107_RX_FIFO_SIZE
) {
171 dev_warn(&s
->spi
->dev
, "Possible RX FIFO overrun %d\n", rxlvl
);
172 /* Ensure sanity of RX level */
173 rxlvl
= MAX3107_RX_FIFO_SIZE
;
175 if ((s
->rxbuf
== 0) || (s
->rxstr
== 0)) {
176 dev_warn(&s
->spi
->dev
, "Rx buffer/str isn't ready\n");
180 valid_str
= s
->rxstr
;
182 pr_debug("rxlvl %d\n", rxlvl
);
184 memset(buf
, 0, sizeof(u16
) * (MAX3107_RX_FIFO_SIZE
+ 2));
186 if (s
->irqen_reg
& MAX3107_IRQ_RXFIFO_BIT
) {
187 /* First disable RX FIFO interrupt */
188 pr_debug("Disabling RX INT\n");
189 buf
[0] = (MAX3107_WRITE_BIT
| MAX3107_IRQEN_REG
);
190 s
->irqen_reg
&= ~MAX3107_IRQ_RXFIFO_BIT
;
191 buf
[0] |= s
->irqen_reg
;
194 /* Just increase the length by amount of words in FIFO since
195 * buffer was zeroed and SPI transfer of 0x0000 means reading
199 /* Append RX level query */
200 buf
[len
] = MAX3107_RXFIFOLVL_REG
;
203 /* Perform the SPI transfer */
204 if (max3107_rw(s
, (u8
*)buf
, (u8
*)buf
, len
* 2)) {
205 dev_err(&s
->spi
->dev
, "SPI transfer for RX h failed\n");
209 /* Skip RX FIFO interrupt disabling word if it was added */
210 j
= ((len
- 1) - rxlvl
);
211 /* Read received words */
212 for (i
= 0; i
< rxlvl
; i
++, j
++)
213 valid_str
[i
] = (u8
)buf
[j
];
214 put_data_to_circ_buf(s
, valid_str
, rxlvl
);
215 /* Get new RX level */
216 rxlvl
= (buf
[len
- 1] & MAX3107_SPI_RX_DATA_MASK
);
220 /* RX still enabled, re-enable RX FIFO interrupt */
221 pr_debug("Enabling RX INT\n");
222 buf
[0] = (MAX3107_WRITE_BIT
| MAX3107_IRQEN_REG
);
223 s
->irqen_reg
|= MAX3107_IRQ_RXFIFO_BIT
;
224 buf
[0] |= s
->irqen_reg
;
225 if (max3107_rw(s
, (u8
*)buf
, NULL
, 2))
226 dev_err(&s
->spi
->dev
, "RX FIFO INT enabling failed\n");
229 /* Push the received data to receivers */
230 if (s
->port
.state
->port
.tty
)
231 tty_flip_buffer_push(s
->port
.state
->port
.tty
);
235 /* Handle data sending */
236 static void max3107_handletx(struct max3107_port
*s
)
238 struct circ_buf
*xmit
= &s
->port
.state
->xmit
;
241 int len
; /* SPI transfer buffer length */
244 if (!s
->tx_fifo_empty
)
245 /* Don't send more data before previous data is sent */
248 if (uart_circ_empty(xmit
) || uart_tx_stopped(&s
->port
))
249 /* No data to send or TX is stopped */
253 dev_warn(&s
->spi
->dev
, "Txbuf isn't ready\n");
257 /* Get length of data pending in circular buffer */
258 len
= uart_circ_chars_pending(xmit
);
260 /* Limit to size of TX FIFO */
261 if (len
> MAX3107_TX_FIFO_SIZE
)
262 len
= MAX3107_TX_FIFO_SIZE
;
264 pr_debug("txlen %d\n", len
);
266 /* Update TX counter */
267 s
->port
.icount
.tx
+= len
;
269 /* TX FIFO will no longer be empty */
270 s
->tx_fifo_empty
= 0;
273 if (s
->irqen_reg
& MAX3107_IRQ_TXEMPTY_BIT
) {
274 /* First disable TX empty interrupt */
275 pr_debug("Disabling TE INT\n");
276 buf
[i
] = (MAX3107_WRITE_BIT
| MAX3107_IRQEN_REG
);
277 s
->irqen_reg
&= ~MAX3107_IRQ_TXEMPTY_BIT
;
278 buf
[i
] |= s
->irqen_reg
;
282 /* Add data to send */
283 spin_lock_irqsave(&s
->port
.lock
, flags
);
284 for ( ; i
< len
; i
++) {
285 buf
[i
] = (MAX3107_WRITE_BIT
| MAX3107_THR_REG
);
286 buf
[i
] |= ((u16
)xmit
->buf
[xmit
->tail
] &
287 MAX3107_SPI_TX_DATA_MASK
);
288 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
290 spin_unlock_irqrestore(&s
->port
.lock
, flags
);
291 if (!(s
->irqen_reg
& MAX3107_IRQ_TXEMPTY_BIT
)) {
292 /* Enable TX empty interrupt */
293 pr_debug("Enabling TE INT\n");
294 buf
[i
] = (MAX3107_WRITE_BIT
| MAX3107_IRQEN_REG
);
295 s
->irqen_reg
|= MAX3107_IRQ_TXEMPTY_BIT
;
296 buf
[i
] |= s
->irqen_reg
;
300 if (!s
->tx_enabled
) {
302 pr_debug("Enable TX\n");
303 buf
[i
] = (MAX3107_WRITE_BIT
| MAX3107_MODE1_REG
);
304 spin_lock_irqsave(&s
->data_lock
, flags
);
305 s
->mode1_reg
&= ~MAX3107_MODE1_TXDIS_BIT
;
306 buf
[i
] |= s
->mode1_reg
;
307 spin_unlock_irqrestore(&s
->data_lock
, flags
);
313 /* Perform the SPI transfer */
314 if (max3107_rw(s
, (u8
*)buf
, NULL
, len
*2)) {
315 dev_err(&s
->spi
->dev
,
316 "SPI transfer TX handling failed\n");
321 /* Indicate wake up if circular buffer is getting low on data */
322 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
323 uart_write_wakeup(&s
->port
);
328 * Also reads and returns current RX FIFO level
330 static u16
handle_interrupt(struct max3107_port
*s
)
332 u16 buf
[4]; /* Buffer for SPI transfers */
337 /* Read IRQ status register */
338 buf
[0] = MAX3107_IRQSTS_REG
;
339 /* Read status IRQ status register */
340 buf
[1] = MAX3107_STS_IRQSTS_REG
;
341 /* Read LSR IRQ status register */
342 buf
[2] = MAX3107_LSR_IRQSTS_REG
;
344 buf
[3] = MAX3107_RXFIFOLVL_REG
;
346 if (max3107_rw(s
, (u8
*)buf
, (u8
*)buf
, 8)) {
347 dev_err(&s
->spi
->dev
,
348 "SPI transfer for INTR handling failed\n");
352 irq_status
= (u8
)buf
[0];
353 pr_debug("IRQSTS %x\n", irq_status
);
354 rx_level
= (buf
[3] & MAX3107_SPI_RX_DATA_MASK
);
356 if (irq_status
& MAX3107_IRQ_LSR_BIT
) {
358 if (buf
[2] & MAX3107_LSR_RXTO_BIT
)
359 /* RX timeout interrupt,
360 * handled by normal RX handling
362 pr_debug("RX TO INT\n");
365 if (irq_status
& MAX3107_IRQ_TXEMPTY_BIT
) {
366 /* Tx empty interrupt,
367 * disable TX and set tx_fifo_empty flag
369 pr_debug("TE INT, disabling TX\n");
370 buf
[0] = (MAX3107_WRITE_BIT
| MAX3107_MODE1_REG
);
371 spin_lock_irqsave(&s
->data_lock
, flags
);
372 s
->mode1_reg
|= MAX3107_MODE1_TXDIS_BIT
;
373 buf
[0] |= s
->mode1_reg
;
374 spin_unlock_irqrestore(&s
->data_lock
, flags
);
375 if (max3107_rw(s
, (u8
*)buf
, NULL
, 2))
376 dev_err(&s
->spi
->dev
, "SPI transfer TX dis failed\n");
378 s
->tx_fifo_empty
= 1;
381 if (irq_status
& MAX3107_IRQ_RXFIFO_BIT
)
382 /* RX FIFO interrupt,
383 * handled by normal RX handling
385 pr_debug("RFIFO INT\n");
387 /* Return RX level */
391 /* Trigger work thread*/
392 static void max3107_dowork(struct max3107_port
*s
)
394 if (!work_pending(&s
->work
) && !freezing(current
) && !s
->suspended
)
395 queue_work(s
->workqueue
, &s
->work
);
397 dev_warn(&s
->spi
->dev
, "interrup isn't serviced normally!\n");
401 static void max3107_work(struct work_struct
*w
)
403 struct max3107_port
*s
= container_of(w
, struct max3107_port
, work
);
405 int len
; /* SPI transfer buffer length */
406 u16 buf
[5]; /* Buffer for SPI transfers */
409 /* Start by reading current RX FIFO level */
410 buf
[0] = MAX3107_RXFIFOLVL_REG
;
411 if (max3107_rw(s
, (u8
*)buf
, (u8
*)buf
, 2)) {
412 dev_err(&s
->spi
->dev
, "SPI transfer RX lev failed\n");
415 rxlvl
= (buf
[0] & MAX3107_SPI_RX_DATA_MASK
);
419 pr_debug("rxlvl %d\n", rxlvl
);
422 max3107_handlerx(s
, rxlvl
);
426 /* Handle pending interrupts
427 * We also get new RX FIFO level since new data may
428 * have been received while pushing received data to
432 rxlvl
= handle_interrupt(s
);
438 /* Handle configuration changes */
440 spin_lock_irqsave(&s
->data_lock
, flags
);
441 if (s
->mode1_commit
) {
442 pr_debug("mode1_commit\n");
443 buf
[len
] = (MAX3107_WRITE_BIT
| MAX3107_MODE1_REG
);
444 buf
[len
++] |= s
->mode1_reg
;
448 pr_debug("lcr_commit\n");
449 buf
[len
] = (MAX3107_WRITE_BIT
| MAX3107_LCR_REG
);
450 buf
[len
++] |= s
->lcr_reg
;
454 pr_debug("brg_commit\n");
455 buf
[len
] = (MAX3107_WRITE_BIT
| MAX3107_BRGDIVMSB_REG
);
456 buf
[len
++] |= ((s
->brg_cfg
>> 16) &
457 MAX3107_SPI_TX_DATA_MASK
);
458 buf
[len
] = (MAX3107_WRITE_BIT
| MAX3107_BRGDIVLSB_REG
);
459 buf
[len
++] |= ((s
->brg_cfg
>> 8) &
460 MAX3107_SPI_TX_DATA_MASK
);
461 buf
[len
] = (MAX3107_WRITE_BIT
| MAX3107_BRGCFG_REG
);
462 buf
[len
++] |= ((s
->brg_cfg
) & 0xff);
465 spin_unlock_irqrestore(&s
->data_lock
, flags
);
468 if (max3107_rw(s
, (u8
*)buf
, NULL
, len
* 2))
469 dev_err(&s
->spi
->dev
,
470 "SPI transfer config failed\n");
473 /* Reloop if interrupt handling indicated data in RX FIFO */
479 static void max3107_set_sleep(struct max3107_port
*s
, int mode
)
481 u16 buf
[1]; /* Buffer for SPI transfer */
483 pr_debug("enter, mode %d\n", mode
);
485 buf
[0] = (MAX3107_WRITE_BIT
| MAX3107_MODE1_REG
);
486 spin_lock_irqsave(&s
->data_lock
, flags
);
488 case MAX3107_DISABLE_FORCED_SLEEP
:
489 s
->mode1_reg
&= ~MAX3107_MODE1_FORCESLEEP_BIT
;
491 case MAX3107_ENABLE_FORCED_SLEEP
:
492 s
->mode1_reg
|= MAX3107_MODE1_FORCESLEEP_BIT
;
494 case MAX3107_DISABLE_AUTOSLEEP
:
495 s
->mode1_reg
&= ~MAX3107_MODE1_AUTOSLEEP_BIT
;
497 case MAX3107_ENABLE_AUTOSLEEP
:
498 s
->mode1_reg
|= MAX3107_MODE1_AUTOSLEEP_BIT
;
501 spin_unlock_irqrestore(&s
->data_lock
, flags
);
502 dev_warn(&s
->spi
->dev
, "invalid sleep mode\n");
505 buf
[0] |= s
->mode1_reg
;
506 spin_unlock_irqrestore(&s
->data_lock
, flags
);
508 if (max3107_rw(s
, (u8
*)buf
, NULL
, 2))
509 dev_err(&s
->spi
->dev
, "SPI transfer sleep mode failed\n");
511 if (mode
== MAX3107_DISABLE_AUTOSLEEP
||
512 mode
== MAX3107_DISABLE_FORCED_SLEEP
)
513 msleep(MAX3107_WAKEUP_DELAY
);
516 /* Perform full register initialization */
517 static void max3107_register_init(struct max3107_port
*s
)
519 u16 buf
[11]; /* Buffer for SPI transfers */
521 /* 1. Configure baud rate, 9600 as default */
523 /* the below is default*/
525 s
->brg_cfg
= MAX3107_BRG26_B9600
;
526 s
->baud_tbl
= (struct baud_table
*)brg26_ext
;
528 s
->brg_cfg
= MAX3107_BRG13_IB9600
;
529 s
->baud_tbl
= (struct baud_table
*)brg13_int
;
535 buf
[0] = (MAX3107_WRITE_BIT
| MAX3107_BRGDIVMSB_REG
)
536 | ((s
->brg_cfg
>> 16) & MAX3107_SPI_TX_DATA_MASK
);
537 buf
[1] = (MAX3107_WRITE_BIT
| MAX3107_BRGDIVLSB_REG
)
538 | ((s
->brg_cfg
>> 8) & MAX3107_SPI_TX_DATA_MASK
);
539 buf
[2] = (MAX3107_WRITE_BIT
| MAX3107_BRGCFG_REG
)
540 | ((s
->brg_cfg
) & 0xff);
542 /* 2. Configure LCR register, 8N1 mode by default */
543 s
->lcr_reg
= MAX3107_LCR_WORD_LEN_8
;
544 buf
[3] = (MAX3107_WRITE_BIT
| MAX3107_LCR_REG
)
547 /* 3. Configure MODE 1 register */
550 s
->mode1_reg
|= MAX3107_MODE1_IRQSEL_BIT
;
552 s
->mode1_reg
|= MAX3107_MODE1_TXDIS_BIT
;
556 buf
[4] = (MAX3107_WRITE_BIT
| MAX3107_MODE1_REG
)
559 /* 4. Configure MODE 2 register */
560 buf
[5] = (MAX3107_WRITE_BIT
| MAX3107_MODE2_REG
);
562 /* Enable loopback */
563 buf
[5] |= MAX3107_MODE2_LOOPBACK_BIT
;
566 buf
[5] |= MAX3107_MODE2_FIFORST_BIT
;
567 s
->tx_fifo_empty
= 1;
569 /* 5. Configure FIFO trigger level register */
570 buf
[6] = (MAX3107_WRITE_BIT
| MAX3107_FIFOTRIGLVL_REG
);
571 /* RX FIFO trigger for 16 words, TX FIFO trigger not used */
572 buf
[6] |= (MAX3107_FIFOTRIGLVL_RX(16) | MAX3107_FIFOTRIGLVL_TX(0));
574 /* 6. Configure flow control levels */
575 buf
[7] = (MAX3107_WRITE_BIT
| MAX3107_FLOWLVL_REG
);
576 /* Flow control halt level 96, resume level 48 */
577 buf
[7] |= (MAX3107_FLOWLVL_RES(48) | MAX3107_FLOWLVL_HALT(96));
579 /* 7. Configure flow control */
580 buf
[8] = (MAX3107_WRITE_BIT
| MAX3107_FLOWCTRL_REG
);
581 /* Enable auto CTS and auto RTS flow control */
582 buf
[8] |= (MAX3107_FLOWCTRL_AUTOCTS_BIT
| MAX3107_FLOWCTRL_AUTORTS_BIT
);
584 /* 8. Configure RX timeout register */
585 buf
[9] = (MAX3107_WRITE_BIT
| MAX3107_RXTO_REG
);
586 /* Timeout after 48 character intervals */
589 /* 9. Configure LSR interrupt enable register */
590 buf
[10] = (MAX3107_WRITE_BIT
| MAX3107_LSR_IRQEN_REG
);
591 /* Enable RX timeout interrupt */
592 buf
[10] |= MAX3107_LSR_RXTO_BIT
;
594 /* Perform SPI transfer */
595 if (max3107_rw(s
, (u8
*)buf
, NULL
, 22))
596 dev_err(&s
->spi
->dev
, "SPI transfer for init failed\n");
598 /* 10. Clear IRQ status register by reading it */
599 buf
[0] = MAX3107_IRQSTS_REG
;
601 /* 11. Configure interrupt enable register */
602 /* Enable LSR interrupt */
603 s
->irqen_reg
= MAX3107_IRQ_LSR_BIT
;
604 /* Enable RX FIFO interrupt */
605 s
->irqen_reg
|= MAX3107_IRQ_RXFIFO_BIT
;
606 buf
[1] = (MAX3107_WRITE_BIT
| MAX3107_IRQEN_REG
)
609 /* 12. Clear FIFO reset that was set in step 6 */
610 buf
[2] = (MAX3107_WRITE_BIT
| MAX3107_MODE2_REG
);
612 /* Keep loopback enabled */
613 buf
[2] |= MAX3107_MODE2_LOOPBACK_BIT
;
616 /* Perform SPI transfer */
617 if (max3107_rw(s
, (u8
*)buf
, (u8
*)buf
, 6))
618 dev_err(&s
->spi
->dev
, "SPI transfer for init failed\n");
623 static irqreturn_t
max3107_irq(int irqno
, void *dev_id
)
625 struct max3107_port
*s
= dev_id
;
627 if (irqno
!= s
->spi
->irq
) {
635 /* Trigger work thread */
641 /* HW suspension function
643 * Currently autosleep is used to decrease current consumption, alternative
644 * approach would be to set the chip to reset mode if UART is not being
645 * used but that would mess the GPIOs
648 void max3107_hw_susp(struct max3107_port
*s
, int suspend
)
650 pr_debug("enter, suspend %d\n", suspend
);
653 /* Suspend requested,
654 * enable autosleep to decrease current consumption
657 max3107_set_sleep(s
, MAX3107_ENABLE_AUTOSLEEP
);
663 max3107_set_sleep(s
, MAX3107_DISABLE_AUTOSLEEP
);
666 EXPORT_SYMBOL_GPL(max3107_hw_susp
);
668 /* Modem status IRQ enabling */
669 static void max3107_enable_ms(struct uart_port
*port
)
671 /* Modem status not supported */
674 /* Data send function */
675 static void max3107_start_tx(struct uart_port
*port
)
677 struct max3107_port
*s
= container_of(port
, struct max3107_port
, port
);
679 /* Trigger work thread for sending data */
683 /* Function for checking that there is no pending transfers */
684 static unsigned int max3107_tx_empty(struct uart_port
*port
)
686 struct max3107_port
*s
= container_of(port
, struct max3107_port
, port
);
688 pr_debug("returning %d\n",
689 (s
->tx_fifo_empty
&& uart_circ_empty(&s
->port
.state
->xmit
)));
690 return s
->tx_fifo_empty
&& uart_circ_empty(&s
->port
.state
->xmit
);
693 /* Function for stopping RX */
694 static void max3107_stop_rx(struct uart_port
*port
)
696 struct max3107_port
*s
= container_of(port
, struct max3107_port
, port
);
699 /* Set RX disabled in MODE 1 register */
700 spin_lock_irqsave(&s
->data_lock
, flags
);
701 s
->mode1_reg
|= MAX3107_MODE1_RXDIS_BIT
;
703 spin_unlock_irqrestore(&s
->data_lock
, flags
);
704 /* Set RX disabled */
706 /* Trigger work thread for doing the actual configuration change */
710 /* Function for returning control pin states */
711 static unsigned int max3107_get_mctrl(struct uart_port
*port
)
713 /* DCD and DSR are not wired and CTS/RTS is handled automatically
714 * so just indicate DSR and CAR asserted
716 return TIOCM_DSR
| TIOCM_CAR
;
719 /* Function for setting control pin states */
720 static void max3107_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
722 /* DCD and DSR are not wired and CTS/RTS is hadnled automatically
727 /* Function for configuring UART parameters */
728 static void max3107_set_termios(struct uart_port
*port
,
729 struct ktermios
*termios
,
730 struct ktermios
*old
)
732 struct max3107_port
*s
= container_of(port
, struct max3107_port
, port
);
733 struct tty_struct
*tty
;
742 tty
= port
->state
->port
.tty
;
746 /* Get new LCR register values */
748 if ((termios
->c_cflag
& CSIZE
) == CS7
)
749 new_lcr
|= MAX3107_LCR_WORD_LEN_7
;
751 new_lcr
|= MAX3107_LCR_WORD_LEN_8
;
754 if (termios
->c_cflag
& PARENB
) {
755 new_lcr
|= MAX3107_LCR_PARITY_BIT
;
756 if (!(termios
->c_cflag
& PARODD
))
757 new_lcr
|= MAX3107_LCR_EVENPARITY_BIT
;
761 if (termios
->c_cflag
& CSTOPB
) {
763 new_lcr
|= MAX3107_LCR_STOPLEN_BIT
;
766 /* Mask termios capabilities we don't support */
767 termios
->c_cflag
&= ~CMSPAR
;
769 /* Set status ignore mask */
770 s
->port
.ignore_status_mask
= 0;
771 if (termios
->c_iflag
& IGNPAR
)
772 s
->port
.ignore_status_mask
|= MAX3107_ALL_ERRORS
;
774 /* Set low latency to immediately handle pushed data */
775 s
->port
.state
->port
.tty
->low_latency
= 1;
777 /* Get new baud rate generator configuration */
778 baud
= tty_get_baud_rate(tty
);
780 spin_lock_irqsave(&s
->data_lock
, flags
);
781 new_brg
= get_new_brg(baud
, s
);
782 /* if can't find the corrent config, use previous */
785 new_brg
= s
->brg_cfg
;
787 spin_unlock_irqrestore(&s
->data_lock
, flags
);
788 tty_termios_encode_baud_rate(termios
, baud
, baud
);
791 /* Update timeout according to new baud rate */
792 uart_update_timeout(port
, termios
->c_cflag
, baud
);
794 spin_lock_irqsave(&s
->data_lock
, flags
);
795 if (s
->lcr_reg
!= new_lcr
) {
796 s
->lcr_reg
= new_lcr
;
799 if (s
->brg_cfg
!= new_brg
) {
800 s
->brg_cfg
= new_brg
;
803 spin_unlock_irqrestore(&s
->data_lock
, flags
);
805 /* Trigger work thread for doing the actual configuration change */
809 /* Port shutdown function */
810 static void max3107_shutdown(struct uart_port
*port
)
812 struct max3107_port
*s
= container_of(port
, struct max3107_port
, port
);
814 if (s
->suspended
&& s
->pdata
->hw_suspend
)
815 s
->pdata
->hw_suspend(s
, 0);
817 /* Free the interrupt */
818 free_irq(s
->spi
->irq
, s
);
821 /* Flush and destroy work queue */
822 flush_workqueue(s
->workqueue
);
823 destroy_workqueue(s
->workqueue
);
828 if (s
->pdata
->hw_suspend
)
829 s
->pdata
->hw_suspend(s
, 1);
832 /* Port startup function */
833 static int max3107_startup(struct uart_port
*port
)
835 struct max3107_port
*s
= container_of(port
, struct max3107_port
, port
);
837 /* Initialize work queue */
838 s
->workqueue
= create_freezable_workqueue("max3107");
840 dev_err(&s
->spi
->dev
, "Workqueue creation failed\n");
843 INIT_WORK(&s
->work
, max3107_work
);
846 if (request_irq(s
->spi
->irq
, max3107_irq
, IRQF_TRIGGER_FALLING
,
848 dev_err(&s
->spi
->dev
, "IRQ reguest failed\n");
849 destroy_workqueue(s
->workqueue
);
855 if (s
->pdata
->hw_suspend
)
856 s
->pdata
->hw_suspend(s
, 0);
859 max3107_register_init(s
);
864 /* Port type function */
865 static const char *max3107_type(struct uart_port
*port
)
867 struct max3107_port
*s
= container_of(port
, struct max3107_port
, port
);
868 return s
->spi
->modalias
;
871 /* Port release function */
872 static void max3107_release_port(struct uart_port
*port
)
877 /* Port request function */
878 static int max3107_request_port(struct uart_port
*port
)
884 /* Port config function */
885 static void max3107_config_port(struct uart_port
*port
, int flags
)
887 struct max3107_port
*s
= container_of(port
, struct max3107_port
, port
);
888 s
->port
.type
= PORT_MAX3107
;
891 /* Port verify function */
892 static int max3107_verify_port(struct uart_port
*port
,
893 struct serial_struct
*ser
)
895 if (ser
->type
== PORT_UNKNOWN
|| ser
->type
== PORT_MAX3107
)
901 /* Port stop TX function */
902 static void max3107_stop_tx(struct uart_port
*port
)
907 /* Port break control function */
908 static void max3107_break_ctl(struct uart_port
*port
, int break_state
)
910 /* We don't support break control, do nothing */
915 static struct uart_ops max3107_ops
= {
916 .tx_empty
= max3107_tx_empty
,
917 .set_mctrl
= max3107_set_mctrl
,
918 .get_mctrl
= max3107_get_mctrl
,
919 .stop_tx
= max3107_stop_tx
,
920 .start_tx
= max3107_start_tx
,
921 .stop_rx
= max3107_stop_rx
,
922 .enable_ms
= max3107_enable_ms
,
923 .break_ctl
= max3107_break_ctl
,
924 .startup
= max3107_startup
,
925 .shutdown
= max3107_shutdown
,
926 .set_termios
= max3107_set_termios
,
927 .type
= max3107_type
,
928 .release_port
= max3107_release_port
,
929 .request_port
= max3107_request_port
,
930 .config_port
= max3107_config_port
,
931 .verify_port
= max3107_verify_port
,
934 /* UART driver data */
935 static struct uart_driver max3107_uart_driver
= {
936 .owner
= THIS_MODULE
,
937 .driver_name
= "ttyMAX",
938 .dev_name
= "ttyMAX",
942 static int driver_registered
= 0;
946 /* 'Generic' platform data */
947 static struct max3107_plat generic_plat_data
= {
950 .hw_suspend
= max3107_hw_susp
,
956 /*******************************************************************/
959 * max3107_probe - SPI bus probe entry point
960 * @spi: the spi device
962 * SPI wants us to probe this device and if appropriate claim it.
963 * Perform any platform specific requirements and then initialise
967 int max3107_probe(struct spi_device
*spi
, struct max3107_plat
*pdata
)
969 struct max3107_port
*s
;
970 u16 buf
[2]; /* Buffer for SPI transfers */
973 pr_info("enter max3107 probe\n");
975 /* Allocate port structure */
976 s
= kzalloc(sizeof(*s
), GFP_KERNEL
);
978 pr_err("Allocating port structure failed\n");
985 * +2 for RX FIFO interrupt
986 * disabling and RX level query
988 s
->rxbuf
= kzalloc(sizeof(u16
) * (MAX3107_RX_FIFO_SIZE
+2), GFP_KERNEL
);
990 pr_err("Allocating RX buffer failed\n");
994 s
->rxstr
= kzalloc(sizeof(u8
) * MAX3107_RX_FIFO_SIZE
, GFP_KERNEL
);
996 pr_err("Allocating RX buffer failed\n");
1001 * SPI transfer buffer
1002 * +3 for TX FIFO empty
1003 * interrupt disabling and
1004 * enabling and TX enabling
1006 s
->txbuf
= kzalloc(sizeof(u16
) * MAX3107_TX_FIFO_SIZE
+ 3, GFP_KERNEL
);
1008 pr_err("Allocating TX buffer failed\n");
1012 /* Initialize shared data lock */
1013 spin_lock_init(&s
->data_lock
);
1015 /* SPI intializations */
1016 dev_set_drvdata(&spi
->dev
, s
);
1017 spi
->mode
= SPI_MODE_0
;
1018 spi
->dev
.platform_data
= pdata
;
1019 spi
->bits_per_word
= 16;
1020 s
->ext_clk
= pdata
->ext_clk
;
1021 s
->loopback
= pdata
->loopback
;
1025 /* Check REV ID to ensure we are talking to what we expect */
1026 buf
[0] = MAX3107_REVID_REG
;
1027 if (max3107_rw(s
, (u8
*)buf
, (u8
*)buf
, 2)) {
1028 dev_err(&s
->spi
->dev
, "SPI transfer for REVID read failed\n");
1032 if ((buf
[0] & MAX3107_SPI_RX_DATA_MASK
) != MAX3107_REVID1
&&
1033 (buf
[0] & MAX3107_SPI_RX_DATA_MASK
) != MAX3107_REVID2
) {
1034 dev_err(&s
->spi
->dev
, "REVID %x does not match\n",
1035 (buf
[0] & MAX3107_SPI_RX_DATA_MASK
));
1040 /* Disable all interrupts */
1041 buf
[0] = (MAX3107_WRITE_BIT
| MAX3107_IRQEN_REG
| 0x0000);
1044 /* Configure clock source */
1045 buf
[1] = (MAX3107_WRITE_BIT
| MAX3107_CLKSRC_REG
);
1047 /* External clock */
1048 buf
[1] |= MAX3107_CLKSRC_EXTCLK_BIT
;
1052 buf
[1] |= MAX3107_CLKSRC_PLLBYP_BIT
;
1054 /* Perform SPI transfer */
1055 if (max3107_rw(s
, (u8
*)buf
, NULL
, 4)) {
1056 dev_err(&s
->spi
->dev
, "SPI transfer for init failed\n");
1061 /* Register UART driver */
1062 if (!driver_registered
) {
1063 retval
= uart_register_driver(&max3107_uart_driver
);
1065 dev_err(&s
->spi
->dev
, "Registering UART driver failed\n");
1068 driver_registered
= 1;
1071 /* Initialize UART port data */
1072 s
->port
.fifosize
= 128;
1073 s
->port
.ops
= &max3107_ops
;
1075 s
->port
.dev
= &spi
->dev
;
1076 s
->port
.uartclk
= 9600;
1077 s
->port
.flags
= UPF_SKIP_TEST
| UPF_BOOT_AUTOCONF
;
1078 s
->port
.irq
= s
->spi
->irq
;
1079 s
->port
.type
= PORT_MAX3107
;
1082 retval
= uart_add_one_port(&max3107_uart_driver
, &s
->port
);
1084 dev_err(&s
->spi
->dev
, "Adding UART port failed\n");
1088 if (pdata
->configure
) {
1089 retval
= pdata
->configure(s
);
1094 /* Go to suspend mode */
1095 if (pdata
->hw_suspend
)
1096 pdata
->hw_suspend(s
, 1);
1110 EXPORT_SYMBOL_GPL(max3107_probe
);
1112 /* Driver remove function */
1113 int max3107_remove(struct spi_device
*spi
)
1115 struct max3107_port
*s
= dev_get_drvdata(&spi
->dev
);
1117 pr_info("enter max3107 remove\n");
1120 if (uart_remove_one_port(&max3107_uart_driver
, &s
->port
))
1121 dev_warn(&s
->spi
->dev
, "Removing UART port failed\n");
1124 /* Free TxRx buffer */
1129 /* Free port structure */
1134 EXPORT_SYMBOL_GPL(max3107_remove
);
1136 /* Driver suspend function */
1137 int max3107_suspend(struct spi_device
*spi
, pm_message_t state
)
1140 struct max3107_port
*s
= dev_get_drvdata(&spi
->dev
);
1142 pr_debug("enter suspend\n");
1144 /* Suspend UART port */
1145 uart_suspend_port(&max3107_uart_driver
, &s
->port
);
1147 /* Go to suspend mode */
1148 if (s
->pdata
->hw_suspend
)
1149 s
->pdata
->hw_suspend(s
, 1);
1150 #endif /* CONFIG_PM */
1153 EXPORT_SYMBOL_GPL(max3107_suspend
);
1155 /* Driver resume function */
1156 int max3107_resume(struct spi_device
*spi
)
1159 struct max3107_port
*s
= dev_get_drvdata(&spi
->dev
);
1161 pr_debug("enter resume\n");
1163 /* Resume from suspend */
1164 if (s
->pdata
->hw_suspend
)
1165 s
->pdata
->hw_suspend(s
, 0);
1167 /* Resume UART port */
1168 uart_resume_port(&max3107_uart_driver
, &s
->port
);
1169 #endif /* CONFIG_PM */
1172 EXPORT_SYMBOL_GPL(max3107_resume
);
1174 static int max3107_probe_generic(struct spi_device
*spi
)
1176 return max3107_probe(spi
, &generic_plat_data
);
1179 /* Spi driver data */
1180 static struct spi_driver max3107_driver
= {
1183 .bus
= &spi_bus_type
,
1184 .owner
= THIS_MODULE
,
1186 .probe
= max3107_probe_generic
,
1187 .remove
= __devexit_p(max3107_remove
),
1188 .suspend
= max3107_suspend
,
1189 .resume
= max3107_resume
,
1192 /* Driver init function */
1193 static int __init
max3107_init(void)
1195 pr_info("enter max3107 init\n");
1196 return spi_register_driver(&max3107_driver
);
1199 /* Driver exit function */
1200 static void __exit
max3107_exit(void)
1202 pr_info("enter max3107 exit\n");
1203 /* Unregister UART driver */
1204 if (driver_registered
)
1205 uart_unregister_driver(&max3107_uart_driver
);
1206 spi_unregister_driver(&max3107_driver
);
1209 module_init(max3107_init
);
1210 module_exit(max3107_exit
);
1212 MODULE_DESCRIPTION("MAX3107 driver");
1213 MODULE_AUTHOR("Aavamobile");
1214 MODULE_ALIAS("spi:max3107");
1215 MODULE_LICENSE("GPL v2");