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/gpio.h>
35 #include <linux/spi/spi.h>
36 #include <linux/freezer.h>
37 #include <linux/module.h>
40 static const struct baud_table brg26_ext
[] = {
41 { 300, MAX3107_BRG26_B300
},
42 { 600, MAX3107_BRG26_B600
},
43 { 1200, MAX3107_BRG26_B1200
},
44 { 2400, MAX3107_BRG26_B2400
},
45 { 4800, MAX3107_BRG26_B4800
},
46 { 9600, MAX3107_BRG26_B9600
},
47 { 19200, MAX3107_BRG26_B19200
},
48 { 57600, MAX3107_BRG26_B57600
},
49 { 115200, MAX3107_BRG26_B115200
},
50 { 230400, MAX3107_BRG26_B230400
},
51 { 460800, MAX3107_BRG26_B460800
},
52 { 921600, MAX3107_BRG26_B921600
},
56 static const struct baud_table brg13_int
[] = {
57 { 300, MAX3107_BRG13_IB300
},
58 { 600, MAX3107_BRG13_IB600
},
59 { 1200, MAX3107_BRG13_IB1200
},
60 { 2400, MAX3107_BRG13_IB2400
},
61 { 4800, MAX3107_BRG13_IB4800
},
62 { 9600, MAX3107_BRG13_IB9600
},
63 { 19200, MAX3107_BRG13_IB19200
},
64 { 57600, MAX3107_BRG13_IB57600
},
65 { 115200, MAX3107_BRG13_IB115200
},
66 { 230400, MAX3107_BRG13_IB230400
},
67 { 460800, MAX3107_BRG13_IB460800
},
68 { 921600, MAX3107_BRG13_IB921600
},
72 static u32
get_new_brg(int baud
, struct max3107_port
*s
)
75 const struct baud_table
*baud_tbl
= s
->baud_tbl
;
77 for (i
= 0; i
< 13; i
++) {
78 if (baud
== baud_tbl
[i
].baud
)
79 return baud_tbl
[i
].new_brg
;
85 /* Perform SPI transfer for write/read of device register(s) */
86 int max3107_rw(struct max3107_port
*s
, u8
*tx
, u8
*rx
, int len
)
88 struct spi_message spi_msg
;
89 struct spi_transfer spi_xfer
;
91 /* Initialize SPI ,message */
92 spi_message_init(&spi_msg
);
94 /* Initialize SPI transfer */
95 memset(&spi_xfer
, 0, sizeof spi_xfer
);
99 spi_xfer
.speed_hz
= MAX3107_SPI_SPEED
;
101 /* Add SPI transfer to SPI message */
102 spi_message_add_tail(&spi_xfer
, &spi_msg
);
104 #ifdef DBG_TRACE_SPI_DATA
107 pr_info("tx len %d:\n", spi_xfer
.len
);
108 for (i
= 0 ; i
< spi_xfer
.len
&& i
< 32 ; i
++)
109 pr_info(" %x", ((u8
*)spi_xfer
.tx_buf
)[i
]);
114 /* Perform synchronous SPI transfer */
115 if (spi_sync(s
->spi
, &spi_msg
)) {
116 dev_err(&s
->spi
->dev
, "spi_sync failure\n");
120 #ifdef DBG_TRACE_SPI_DATA
121 if (spi_xfer
.rx_buf
) {
123 pr_info("rx len %d:\n", spi_xfer
.len
);
124 for (i
= 0 ; i
< spi_xfer
.len
&& i
< 32 ; i
++)
125 pr_info(" %x", ((u8
*)spi_xfer
.rx_buf
)[i
]);
131 EXPORT_SYMBOL_GPL(max3107_rw
);
133 /* Puts received data to circular buffer */
134 static void put_data_to_circ_buf(struct max3107_port
*s
, unsigned char *data
,
137 struct uart_port
*port
= &s
->port
;
138 struct tty_struct
*tty
;
143 tty
= port
->state
->port
.tty
;
147 /* Insert received data */
148 tty_insert_flip_string(tty
, data
, len
);
149 /* Update RX counter */
150 port
->icount
.rx
+= len
;
153 /* Handle data receiving */
154 static void max3107_handlerx(struct max3107_port
*s
, u16 rxlvl
)
158 int len
; /* SPI transfer buffer length */
167 /* RX fifo is empty */
169 } else if (rxlvl
>= MAX3107_RX_FIFO_SIZE
) {
170 dev_warn(&s
->spi
->dev
, "Possible RX FIFO overrun %d\n", rxlvl
);
171 /* Ensure sanity of RX level */
172 rxlvl
= MAX3107_RX_FIFO_SIZE
;
174 if ((s
->rxbuf
== 0) || (s
->rxstr
== 0)) {
175 dev_warn(&s
->spi
->dev
, "Rx buffer/str isn't ready\n");
179 valid_str
= s
->rxstr
;
181 pr_debug("rxlvl %d\n", rxlvl
);
183 memset(buf
, 0, sizeof(u16
) * (MAX3107_RX_FIFO_SIZE
+ 2));
185 if (s
->irqen_reg
& MAX3107_IRQ_RXFIFO_BIT
) {
186 /* First disable RX FIFO interrupt */
187 pr_debug("Disabling RX INT\n");
188 buf
[0] = (MAX3107_WRITE_BIT
| MAX3107_IRQEN_REG
);
189 s
->irqen_reg
&= ~MAX3107_IRQ_RXFIFO_BIT
;
190 buf
[0] |= s
->irqen_reg
;
193 /* Just increase the length by amount of words in FIFO since
194 * buffer was zeroed and SPI transfer of 0x0000 means reading
198 /* Append RX level query */
199 buf
[len
] = MAX3107_RXFIFOLVL_REG
;
202 /* Perform the SPI transfer */
203 if (max3107_rw(s
, (u8
*)buf
, (u8
*)buf
, len
* 2)) {
204 dev_err(&s
->spi
->dev
, "SPI transfer for RX h failed\n");
208 /* Skip RX FIFO interrupt disabling word if it was added */
209 j
= ((len
- 1) - rxlvl
);
210 /* Read received words */
211 for (i
= 0; i
< rxlvl
; i
++, j
++)
212 valid_str
[i
] = (u8
)buf
[j
];
213 put_data_to_circ_buf(s
, valid_str
, rxlvl
);
214 /* Get new RX level */
215 rxlvl
= (buf
[len
- 1] & MAX3107_SPI_RX_DATA_MASK
);
219 /* RX still enabled, re-enable RX FIFO interrupt */
220 pr_debug("Enabling RX INT\n");
221 buf
[0] = (MAX3107_WRITE_BIT
| MAX3107_IRQEN_REG
);
222 s
->irqen_reg
|= MAX3107_IRQ_RXFIFO_BIT
;
223 buf
[0] |= s
->irqen_reg
;
224 if (max3107_rw(s
, (u8
*)buf
, NULL
, 2))
225 dev_err(&s
->spi
->dev
, "RX FIFO INT enabling failed\n");
228 /* Push the received data to receivers */
229 if (s
->port
.state
->port
.tty
)
230 tty_flip_buffer_push(s
->port
.state
->port
.tty
);
234 /* Handle data sending */
235 static void max3107_handletx(struct max3107_port
*s
)
237 struct circ_buf
*xmit
= &s
->port
.state
->xmit
;
240 int len
; /* SPI transfer buffer length */
243 if (!s
->tx_fifo_empty
)
244 /* Don't send more data before previous data is sent */
247 if (uart_circ_empty(xmit
) || uart_tx_stopped(&s
->port
))
248 /* No data to send or TX is stopped */
252 dev_warn(&s
->spi
->dev
, "Txbuf isn't ready\n");
256 /* Get length of data pending in circular buffer */
257 len
= uart_circ_chars_pending(xmit
);
259 /* Limit to size of TX FIFO */
260 if (len
> MAX3107_TX_FIFO_SIZE
)
261 len
= MAX3107_TX_FIFO_SIZE
;
263 pr_debug("txlen %d\n", len
);
265 /* Update TX counter */
266 s
->port
.icount
.tx
+= len
;
268 /* TX FIFO will no longer be empty */
269 s
->tx_fifo_empty
= 0;
272 if (s
->irqen_reg
& MAX3107_IRQ_TXEMPTY_BIT
) {
273 /* First disable TX empty interrupt */
274 pr_debug("Disabling TE INT\n");
275 buf
[i
] = (MAX3107_WRITE_BIT
| MAX3107_IRQEN_REG
);
276 s
->irqen_reg
&= ~MAX3107_IRQ_TXEMPTY_BIT
;
277 buf
[i
] |= s
->irqen_reg
;
281 /* Add data to send */
282 spin_lock_irqsave(&s
->port
.lock
, flags
);
283 for ( ; i
< len
; i
++) {
284 buf
[i
] = (MAX3107_WRITE_BIT
| MAX3107_THR_REG
);
285 buf
[i
] |= ((u16
)xmit
->buf
[xmit
->tail
] &
286 MAX3107_SPI_TX_DATA_MASK
);
287 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
289 spin_unlock_irqrestore(&s
->port
.lock
, flags
);
290 if (!(s
->irqen_reg
& MAX3107_IRQ_TXEMPTY_BIT
)) {
291 /* Enable TX empty interrupt */
292 pr_debug("Enabling TE INT\n");
293 buf
[i
] = (MAX3107_WRITE_BIT
| MAX3107_IRQEN_REG
);
294 s
->irqen_reg
|= MAX3107_IRQ_TXEMPTY_BIT
;
295 buf
[i
] |= s
->irqen_reg
;
299 if (!s
->tx_enabled
) {
301 pr_debug("Enable TX\n");
302 buf
[i
] = (MAX3107_WRITE_BIT
| MAX3107_MODE1_REG
);
303 spin_lock_irqsave(&s
->data_lock
, flags
);
304 s
->mode1_reg
&= ~MAX3107_MODE1_TXDIS_BIT
;
305 buf
[i
] |= s
->mode1_reg
;
306 spin_unlock_irqrestore(&s
->data_lock
, flags
);
312 /* Perform the SPI transfer */
313 if (max3107_rw(s
, (u8
*)buf
, NULL
, len
*2)) {
314 dev_err(&s
->spi
->dev
,
315 "SPI transfer TX handling failed\n");
320 /* Indicate wake up if circular buffer is getting low on data */
321 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
322 uart_write_wakeup(&s
->port
);
327 * Also reads and returns current RX FIFO level
329 static u16
handle_interrupt(struct max3107_port
*s
)
331 u16 buf
[4]; /* Buffer for SPI transfers */
336 /* Read IRQ status register */
337 buf
[0] = MAX3107_IRQSTS_REG
;
338 /* Read status IRQ status register */
339 buf
[1] = MAX3107_STS_IRQSTS_REG
;
340 /* Read LSR IRQ status register */
341 buf
[2] = MAX3107_LSR_IRQSTS_REG
;
343 buf
[3] = MAX3107_RXFIFOLVL_REG
;
345 if (max3107_rw(s
, (u8
*)buf
, (u8
*)buf
, 8)) {
346 dev_err(&s
->spi
->dev
,
347 "SPI transfer for INTR handling failed\n");
351 irq_status
= (u8
)buf
[0];
352 pr_debug("IRQSTS %x\n", irq_status
);
353 rx_level
= (buf
[3] & MAX3107_SPI_RX_DATA_MASK
);
355 if (irq_status
& MAX3107_IRQ_LSR_BIT
) {
357 if (buf
[2] & MAX3107_LSR_RXTO_BIT
)
358 /* RX timeout interrupt,
359 * handled by normal RX handling
361 pr_debug("RX TO INT\n");
364 if (irq_status
& MAX3107_IRQ_TXEMPTY_BIT
) {
365 /* Tx empty interrupt,
366 * disable TX and set tx_fifo_empty flag
368 pr_debug("TE INT, disabling TX\n");
369 buf
[0] = (MAX3107_WRITE_BIT
| MAX3107_MODE1_REG
);
370 spin_lock_irqsave(&s
->data_lock
, flags
);
371 s
->mode1_reg
|= MAX3107_MODE1_TXDIS_BIT
;
372 buf
[0] |= s
->mode1_reg
;
373 spin_unlock_irqrestore(&s
->data_lock
, flags
);
374 if (max3107_rw(s
, (u8
*)buf
, NULL
, 2))
375 dev_err(&s
->spi
->dev
, "SPI transfer TX dis failed\n");
377 s
->tx_fifo_empty
= 1;
380 if (irq_status
& MAX3107_IRQ_RXFIFO_BIT
)
381 /* RX FIFO interrupt,
382 * handled by normal RX handling
384 pr_debug("RFIFO INT\n");
386 /* Return RX level */
390 /* Trigger work thread*/
391 static void max3107_dowork(struct max3107_port
*s
)
393 if (!work_pending(&s
->work
) && !freezing(current
) && !s
->suspended
)
394 queue_work(s
->workqueue
, &s
->work
);
396 dev_warn(&s
->spi
->dev
, "interrup isn't serviced normally!\n");
400 static void max3107_work(struct work_struct
*w
)
402 struct max3107_port
*s
= container_of(w
, struct max3107_port
, work
);
404 int len
; /* SPI transfer buffer length */
405 u16 buf
[5]; /* Buffer for SPI transfers */
408 /* Start by reading current RX FIFO level */
409 buf
[0] = MAX3107_RXFIFOLVL_REG
;
410 if (max3107_rw(s
, (u8
*)buf
, (u8
*)buf
, 2)) {
411 dev_err(&s
->spi
->dev
, "SPI transfer RX lev failed\n");
414 rxlvl
= (buf
[0] & MAX3107_SPI_RX_DATA_MASK
);
418 pr_debug("rxlvl %d\n", rxlvl
);
421 max3107_handlerx(s
, rxlvl
);
425 /* Handle pending interrupts
426 * We also get new RX FIFO level since new data may
427 * have been received while pushing received data to
431 rxlvl
= handle_interrupt(s
);
437 /* Handle configuration changes */
439 spin_lock_irqsave(&s
->data_lock
, flags
);
440 if (s
->mode1_commit
) {
441 pr_debug("mode1_commit\n");
442 buf
[len
] = (MAX3107_WRITE_BIT
| MAX3107_MODE1_REG
);
443 buf
[len
++] |= s
->mode1_reg
;
447 pr_debug("lcr_commit\n");
448 buf
[len
] = (MAX3107_WRITE_BIT
| MAX3107_LCR_REG
);
449 buf
[len
++] |= s
->lcr_reg
;
453 pr_debug("brg_commit\n");
454 buf
[len
] = (MAX3107_WRITE_BIT
| MAX3107_BRGDIVMSB_REG
);
455 buf
[len
++] |= ((s
->brg_cfg
>> 16) &
456 MAX3107_SPI_TX_DATA_MASK
);
457 buf
[len
] = (MAX3107_WRITE_BIT
| MAX3107_BRGDIVLSB_REG
);
458 buf
[len
++] |= ((s
->brg_cfg
>> 8) &
459 MAX3107_SPI_TX_DATA_MASK
);
460 buf
[len
] = (MAX3107_WRITE_BIT
| MAX3107_BRGCFG_REG
);
461 buf
[len
++] |= ((s
->brg_cfg
) & 0xff);
464 spin_unlock_irqrestore(&s
->data_lock
, flags
);
467 if (max3107_rw(s
, (u8
*)buf
, NULL
, len
* 2))
468 dev_err(&s
->spi
->dev
,
469 "SPI transfer config failed\n");
472 /* Reloop if interrupt handling indicated data in RX FIFO */
478 static void max3107_set_sleep(struct max3107_port
*s
, int mode
)
480 u16 buf
[1]; /* Buffer for SPI transfer */
482 pr_debug("enter, mode %d\n", mode
);
484 buf
[0] = (MAX3107_WRITE_BIT
| MAX3107_MODE1_REG
);
485 spin_lock_irqsave(&s
->data_lock
, flags
);
487 case MAX3107_DISABLE_FORCED_SLEEP
:
488 s
->mode1_reg
&= ~MAX3107_MODE1_FORCESLEEP_BIT
;
490 case MAX3107_ENABLE_FORCED_SLEEP
:
491 s
->mode1_reg
|= MAX3107_MODE1_FORCESLEEP_BIT
;
493 case MAX3107_DISABLE_AUTOSLEEP
:
494 s
->mode1_reg
&= ~MAX3107_MODE1_AUTOSLEEP_BIT
;
496 case MAX3107_ENABLE_AUTOSLEEP
:
497 s
->mode1_reg
|= MAX3107_MODE1_AUTOSLEEP_BIT
;
500 spin_unlock_irqrestore(&s
->data_lock
, flags
);
501 dev_warn(&s
->spi
->dev
, "invalid sleep mode\n");
504 buf
[0] |= s
->mode1_reg
;
505 spin_unlock_irqrestore(&s
->data_lock
, flags
);
507 if (max3107_rw(s
, (u8
*)buf
, NULL
, 2))
508 dev_err(&s
->spi
->dev
, "SPI transfer sleep mode failed\n");
510 if (mode
== MAX3107_DISABLE_AUTOSLEEP
||
511 mode
== MAX3107_DISABLE_FORCED_SLEEP
)
512 msleep(MAX3107_WAKEUP_DELAY
);
515 /* Perform full register initialization */
516 static void max3107_register_init(struct max3107_port
*s
)
518 u16 buf
[11]; /* Buffer for SPI transfers */
520 /* 1. Configure baud rate, 9600 as default */
522 /* the below is default*/
524 s
->brg_cfg
= MAX3107_BRG26_B9600
;
525 s
->baud_tbl
= (struct baud_table
*)brg26_ext
;
527 s
->brg_cfg
= MAX3107_BRG13_IB9600
;
528 s
->baud_tbl
= (struct baud_table
*)brg13_int
;
534 buf
[0] = (MAX3107_WRITE_BIT
| MAX3107_BRGDIVMSB_REG
)
535 | ((s
->brg_cfg
>> 16) & MAX3107_SPI_TX_DATA_MASK
);
536 buf
[1] = (MAX3107_WRITE_BIT
| MAX3107_BRGDIVLSB_REG
)
537 | ((s
->brg_cfg
>> 8) & MAX3107_SPI_TX_DATA_MASK
);
538 buf
[2] = (MAX3107_WRITE_BIT
| MAX3107_BRGCFG_REG
)
539 | ((s
->brg_cfg
) & 0xff);
541 /* 2. Configure LCR register, 8N1 mode by default */
542 s
->lcr_reg
= MAX3107_LCR_WORD_LEN_8
;
543 buf
[3] = (MAX3107_WRITE_BIT
| MAX3107_LCR_REG
)
546 /* 3. Configure MODE 1 register */
549 s
->mode1_reg
|= MAX3107_MODE1_IRQSEL_BIT
;
551 s
->mode1_reg
|= MAX3107_MODE1_TXDIS_BIT
;
555 buf
[4] = (MAX3107_WRITE_BIT
| MAX3107_MODE1_REG
)
558 /* 4. Configure MODE 2 register */
559 buf
[5] = (MAX3107_WRITE_BIT
| MAX3107_MODE2_REG
);
561 /* Enable loopback */
562 buf
[5] |= MAX3107_MODE2_LOOPBACK_BIT
;
565 buf
[5] |= MAX3107_MODE2_FIFORST_BIT
;
566 s
->tx_fifo_empty
= 1;
568 /* 5. Configure FIFO trigger level register */
569 buf
[6] = (MAX3107_WRITE_BIT
| MAX3107_FIFOTRIGLVL_REG
);
570 /* RX FIFO trigger for 16 words, TX FIFO trigger not used */
571 buf
[6] |= (MAX3107_FIFOTRIGLVL_RX(16) | MAX3107_FIFOTRIGLVL_TX(0));
573 /* 6. Configure flow control levels */
574 buf
[7] = (MAX3107_WRITE_BIT
| MAX3107_FLOWLVL_REG
);
575 /* Flow control halt level 96, resume level 48 */
576 buf
[7] |= (MAX3107_FLOWLVL_RES(48) | MAX3107_FLOWLVL_HALT(96));
578 /* 7. Configure flow control */
579 buf
[8] = (MAX3107_WRITE_BIT
| MAX3107_FLOWCTRL_REG
);
580 /* Enable auto CTS and auto RTS flow control */
581 buf
[8] |= (MAX3107_FLOWCTRL_AUTOCTS_BIT
| MAX3107_FLOWCTRL_AUTORTS_BIT
);
583 /* 8. Configure RX timeout register */
584 buf
[9] = (MAX3107_WRITE_BIT
| MAX3107_RXTO_REG
);
585 /* Timeout after 48 character intervals */
588 /* 9. Configure LSR interrupt enable register */
589 buf
[10] = (MAX3107_WRITE_BIT
| MAX3107_LSR_IRQEN_REG
);
590 /* Enable RX timeout interrupt */
591 buf
[10] |= MAX3107_LSR_RXTO_BIT
;
593 /* Perform SPI transfer */
594 if (max3107_rw(s
, (u8
*)buf
, NULL
, 22))
595 dev_err(&s
->spi
->dev
, "SPI transfer for init failed\n");
597 /* 10. Clear IRQ status register by reading it */
598 buf
[0] = MAX3107_IRQSTS_REG
;
600 /* 11. Configure interrupt enable register */
601 /* Enable LSR interrupt */
602 s
->irqen_reg
= MAX3107_IRQ_LSR_BIT
;
603 /* Enable RX FIFO interrupt */
604 s
->irqen_reg
|= MAX3107_IRQ_RXFIFO_BIT
;
605 buf
[1] = (MAX3107_WRITE_BIT
| MAX3107_IRQEN_REG
)
608 /* 12. Clear FIFO reset that was set in step 6 */
609 buf
[2] = (MAX3107_WRITE_BIT
| MAX3107_MODE2_REG
);
611 /* Keep loopback enabled */
612 buf
[2] |= MAX3107_MODE2_LOOPBACK_BIT
;
615 /* Perform SPI transfer */
616 if (max3107_rw(s
, (u8
*)buf
, (u8
*)buf
, 6))
617 dev_err(&s
->spi
->dev
, "SPI transfer for init failed\n");
622 static irqreturn_t
max3107_irq(int irqno
, void *dev_id
)
624 struct max3107_port
*s
= dev_id
;
626 if (irqno
!= s
->spi
->irq
) {
634 /* Trigger work thread */
640 /* HW suspension function
642 * Currently autosleep is used to decrease current consumption, alternative
643 * approach would be to set the chip to reset mode if UART is not being
644 * used but that would mess the GPIOs
647 void max3107_hw_susp(struct max3107_port
*s
, int suspend
)
649 pr_debug("enter, suspend %d\n", suspend
);
652 /* Suspend requested,
653 * enable autosleep to decrease current consumption
656 max3107_set_sleep(s
, MAX3107_ENABLE_AUTOSLEEP
);
662 max3107_set_sleep(s
, MAX3107_DISABLE_AUTOSLEEP
);
665 EXPORT_SYMBOL_GPL(max3107_hw_susp
);
667 /* Modem status IRQ enabling */
668 static void max3107_enable_ms(struct uart_port
*port
)
670 /* Modem status not supported */
673 /* Data send function */
674 static void max3107_start_tx(struct uart_port
*port
)
676 struct max3107_port
*s
= container_of(port
, struct max3107_port
, port
);
678 /* Trigger work thread for sending data */
682 /* Function for checking that there is no pending transfers */
683 static unsigned int max3107_tx_empty(struct uart_port
*port
)
685 struct max3107_port
*s
= container_of(port
, struct max3107_port
, port
);
687 pr_debug("returning %d\n",
688 (s
->tx_fifo_empty
&& uart_circ_empty(&s
->port
.state
->xmit
)));
689 return s
->tx_fifo_empty
&& uart_circ_empty(&s
->port
.state
->xmit
);
692 /* Function for stopping RX */
693 static void max3107_stop_rx(struct uart_port
*port
)
695 struct max3107_port
*s
= container_of(port
, struct max3107_port
, port
);
698 /* Set RX disabled in MODE 1 register */
699 spin_lock_irqsave(&s
->data_lock
, flags
);
700 s
->mode1_reg
|= MAX3107_MODE1_RXDIS_BIT
;
702 spin_unlock_irqrestore(&s
->data_lock
, flags
);
703 /* Set RX disabled */
705 /* Trigger work thread for doing the actual configuration change */
709 /* Function for returning control pin states */
710 static unsigned int max3107_get_mctrl(struct uart_port
*port
)
712 /* DCD and DSR are not wired and CTS/RTS is handled automatically
713 * so just indicate DSR and CAR asserted
715 return TIOCM_DSR
| TIOCM_CAR
;
718 /* Function for setting control pin states */
719 static void max3107_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
721 /* DCD and DSR are not wired and CTS/RTS is hadnled automatically
726 /* Function for configuring UART parameters */
727 static void max3107_set_termios(struct uart_port
*port
,
728 struct ktermios
*termios
,
729 struct ktermios
*old
)
731 struct max3107_port
*s
= container_of(port
, struct max3107_port
, port
);
732 struct tty_struct
*tty
;
741 tty
= port
->state
->port
.tty
;
745 /* Get new LCR register values */
747 if ((termios
->c_cflag
& CSIZE
) == CS7
)
748 new_lcr
|= MAX3107_LCR_WORD_LEN_7
;
750 new_lcr
|= MAX3107_LCR_WORD_LEN_8
;
753 if (termios
->c_cflag
& PARENB
) {
754 new_lcr
|= MAX3107_LCR_PARITY_BIT
;
755 if (!(termios
->c_cflag
& PARODD
))
756 new_lcr
|= MAX3107_LCR_EVENPARITY_BIT
;
760 if (termios
->c_cflag
& CSTOPB
) {
762 new_lcr
|= MAX3107_LCR_STOPLEN_BIT
;
765 /* Mask termios capabilities we don't support */
766 termios
->c_cflag
&= ~CMSPAR
;
768 /* Set status ignore mask */
769 s
->port
.ignore_status_mask
= 0;
770 if (termios
->c_iflag
& IGNPAR
)
771 s
->port
.ignore_status_mask
|= MAX3107_ALL_ERRORS
;
773 /* Set low latency to immediately handle pushed data */
774 s
->port
.state
->port
.tty
->low_latency
= 1;
776 /* Get new baud rate generator configuration */
777 baud
= tty_get_baud_rate(tty
);
779 spin_lock_irqsave(&s
->data_lock
, flags
);
780 new_brg
= get_new_brg(baud
, s
);
781 /* if can't find the corrent config, use previous */
784 new_brg
= s
->brg_cfg
;
786 spin_unlock_irqrestore(&s
->data_lock
, flags
);
787 tty_termios_encode_baud_rate(termios
, baud
, baud
);
790 /* Update timeout according to new baud rate */
791 uart_update_timeout(port
, termios
->c_cflag
, baud
);
793 spin_lock_irqsave(&s
->data_lock
, flags
);
794 if (s
->lcr_reg
!= new_lcr
) {
795 s
->lcr_reg
= new_lcr
;
798 if (s
->brg_cfg
!= new_brg
) {
799 s
->brg_cfg
= new_brg
;
802 spin_unlock_irqrestore(&s
->data_lock
, flags
);
804 /* Trigger work thread for doing the actual configuration change */
808 /* Port shutdown function */
809 static void max3107_shutdown(struct uart_port
*port
)
811 struct max3107_port
*s
= container_of(port
, struct max3107_port
, port
);
813 if (s
->suspended
&& s
->pdata
->hw_suspend
)
814 s
->pdata
->hw_suspend(s
, 0);
816 /* Free the interrupt */
817 free_irq(s
->spi
->irq
, s
);
820 /* Flush and destroy work queue */
821 flush_workqueue(s
->workqueue
);
822 destroy_workqueue(s
->workqueue
);
827 if (s
->pdata
->hw_suspend
)
828 s
->pdata
->hw_suspend(s
, 1);
831 /* Port startup function */
832 static int max3107_startup(struct uart_port
*port
)
834 struct max3107_port
*s
= container_of(port
, struct max3107_port
, port
);
836 /* Initialize work queue */
837 s
->workqueue
= create_freezable_workqueue("max3107");
839 dev_err(&s
->spi
->dev
, "Workqueue creation failed\n");
842 INIT_WORK(&s
->work
, max3107_work
);
845 if (request_irq(s
->spi
->irq
, max3107_irq
, IRQF_TRIGGER_FALLING
,
847 dev_err(&s
->spi
->dev
, "IRQ reguest failed\n");
848 destroy_workqueue(s
->workqueue
);
854 if (s
->pdata
->hw_suspend
)
855 s
->pdata
->hw_suspend(s
, 0);
858 max3107_register_init(s
);
863 /* Port type function */
864 static const char *max3107_type(struct uart_port
*port
)
866 struct max3107_port
*s
= container_of(port
, struct max3107_port
, port
);
867 return s
->spi
->modalias
;
870 /* Port release function */
871 static void max3107_release_port(struct uart_port
*port
)
876 /* Port request function */
877 static int max3107_request_port(struct uart_port
*port
)
883 /* Port config function */
884 static void max3107_config_port(struct uart_port
*port
, int flags
)
886 struct max3107_port
*s
= container_of(port
, struct max3107_port
, port
);
887 s
->port
.type
= PORT_MAX3107
;
890 /* Port verify function */
891 static int max3107_verify_port(struct uart_port
*port
,
892 struct serial_struct
*ser
)
894 if (ser
->type
== PORT_UNKNOWN
|| ser
->type
== PORT_MAX3107
)
900 /* Port stop TX function */
901 static void max3107_stop_tx(struct uart_port
*port
)
906 /* Port break control function */
907 static void max3107_break_ctl(struct uart_port
*port
, int break_state
)
909 /* We don't support break control, do nothing */
914 static struct uart_ops max3107_ops
= {
915 .tx_empty
= max3107_tx_empty
,
916 .set_mctrl
= max3107_set_mctrl
,
917 .get_mctrl
= max3107_get_mctrl
,
918 .stop_tx
= max3107_stop_tx
,
919 .start_tx
= max3107_start_tx
,
920 .stop_rx
= max3107_stop_rx
,
921 .enable_ms
= max3107_enable_ms
,
922 .break_ctl
= max3107_break_ctl
,
923 .startup
= max3107_startup
,
924 .shutdown
= max3107_shutdown
,
925 .set_termios
= max3107_set_termios
,
926 .type
= max3107_type
,
927 .release_port
= max3107_release_port
,
928 .request_port
= max3107_request_port
,
929 .config_port
= max3107_config_port
,
930 .verify_port
= max3107_verify_port
,
933 /* UART driver data */
934 static struct uart_driver max3107_uart_driver
= {
935 .owner
= THIS_MODULE
,
936 .driver_name
= "ttyMAX",
937 .dev_name
= "ttyMAX",
941 static int driver_registered
= 0;
945 /* 'Generic' platform data */
946 static struct max3107_plat generic_plat_data
= {
949 .hw_suspend
= max3107_hw_susp
,
955 /*******************************************************************/
958 * max3107_probe - SPI bus probe entry point
959 * @spi: the spi device
961 * SPI wants us to probe this device and if appropriate claim it.
962 * Perform any platform specific requirements and then initialise
966 int max3107_probe(struct spi_device
*spi
, struct max3107_plat
*pdata
)
968 struct max3107_port
*s
;
969 u16 buf
[2]; /* Buffer for SPI transfers */
972 pr_info("enter max3107 probe\n");
974 /* Allocate port structure */
975 s
= kzalloc(sizeof(*s
), GFP_KERNEL
);
977 pr_err("Allocating port structure failed\n");
984 * +2 for RX FIFO interrupt
985 * disabling and RX level query
987 s
->rxbuf
= kzalloc(sizeof(u16
) * (MAX3107_RX_FIFO_SIZE
+2), GFP_KERNEL
);
989 pr_err("Allocating RX buffer failed\n");
993 s
->rxstr
= kzalloc(sizeof(u8
) * MAX3107_RX_FIFO_SIZE
, GFP_KERNEL
);
995 pr_err("Allocating RX buffer failed\n");
1000 * SPI transfer buffer
1001 * +3 for TX FIFO empty
1002 * interrupt disabling and
1003 * enabling and TX enabling
1005 s
->txbuf
= kzalloc(sizeof(u16
) * MAX3107_TX_FIFO_SIZE
+ 3, GFP_KERNEL
);
1007 pr_err("Allocating TX buffer failed\n");
1011 /* Initialize shared data lock */
1012 spin_lock_init(&s
->data_lock
);
1014 /* SPI intializations */
1015 dev_set_drvdata(&spi
->dev
, s
);
1016 spi
->mode
= SPI_MODE_0
;
1017 spi
->dev
.platform_data
= pdata
;
1018 spi
->bits_per_word
= 16;
1019 s
->ext_clk
= pdata
->ext_clk
;
1020 s
->loopback
= pdata
->loopback
;
1024 /* Check REV ID to ensure we are talking to what we expect */
1025 buf
[0] = MAX3107_REVID_REG
;
1026 if (max3107_rw(s
, (u8
*)buf
, (u8
*)buf
, 2)) {
1027 dev_err(&s
->spi
->dev
, "SPI transfer for REVID read failed\n");
1031 if ((buf
[0] & MAX3107_SPI_RX_DATA_MASK
) != MAX3107_REVID1
&&
1032 (buf
[0] & MAX3107_SPI_RX_DATA_MASK
) != MAX3107_REVID2
) {
1033 dev_err(&s
->spi
->dev
, "REVID %x does not match\n",
1034 (buf
[0] & MAX3107_SPI_RX_DATA_MASK
));
1039 /* Disable all interrupts */
1040 buf
[0] = (MAX3107_WRITE_BIT
| MAX3107_IRQEN_REG
| 0x0000);
1043 /* Configure clock source */
1044 buf
[1] = (MAX3107_WRITE_BIT
| MAX3107_CLKSRC_REG
);
1046 /* External clock */
1047 buf
[1] |= MAX3107_CLKSRC_EXTCLK_BIT
;
1051 buf
[1] |= MAX3107_CLKSRC_PLLBYP_BIT
;
1053 /* Perform SPI transfer */
1054 if (max3107_rw(s
, (u8
*)buf
, NULL
, 4)) {
1055 dev_err(&s
->spi
->dev
, "SPI transfer for init failed\n");
1060 /* Register UART driver */
1061 if (!driver_registered
) {
1062 retval
= uart_register_driver(&max3107_uart_driver
);
1064 dev_err(&s
->spi
->dev
, "Registering UART driver failed\n");
1067 driver_registered
= 1;
1070 /* Initialize UART port data */
1071 s
->port
.fifosize
= 128;
1072 s
->port
.ops
= &max3107_ops
;
1074 s
->port
.dev
= &spi
->dev
;
1075 s
->port
.uartclk
= 9600;
1076 s
->port
.flags
= UPF_SKIP_TEST
| UPF_BOOT_AUTOCONF
;
1077 s
->port
.irq
= s
->spi
->irq
;
1078 s
->port
.type
= PORT_MAX3107
;
1081 retval
= uart_add_one_port(&max3107_uart_driver
, &s
->port
);
1083 dev_err(&s
->spi
->dev
, "Adding UART port failed\n");
1087 if (pdata
->configure
) {
1088 retval
= pdata
->configure(s
);
1093 /* Go to suspend mode */
1094 if (pdata
->hw_suspend
)
1095 pdata
->hw_suspend(s
, 1);
1109 EXPORT_SYMBOL_GPL(max3107_probe
);
1111 /* Driver remove function */
1112 int max3107_remove(struct spi_device
*spi
)
1114 struct max3107_port
*s
= dev_get_drvdata(&spi
->dev
);
1116 pr_info("enter max3107 remove\n");
1119 if (uart_remove_one_port(&max3107_uart_driver
, &s
->port
))
1120 dev_warn(&s
->spi
->dev
, "Removing UART port failed\n");
1123 /* Free TxRx buffer */
1128 /* Free port structure */
1133 EXPORT_SYMBOL_GPL(max3107_remove
);
1135 /* Driver suspend function */
1136 int max3107_suspend(struct spi_device
*spi
, pm_message_t state
)
1139 struct max3107_port
*s
= dev_get_drvdata(&spi
->dev
);
1141 pr_debug("enter suspend\n");
1143 /* Suspend UART port */
1144 uart_suspend_port(&max3107_uart_driver
, &s
->port
);
1146 /* Go to suspend mode */
1147 if (s
->pdata
->hw_suspend
)
1148 s
->pdata
->hw_suspend(s
, 1);
1149 #endif /* CONFIG_PM */
1152 EXPORT_SYMBOL_GPL(max3107_suspend
);
1154 /* Driver resume function */
1155 int max3107_resume(struct spi_device
*spi
)
1158 struct max3107_port
*s
= dev_get_drvdata(&spi
->dev
);
1160 pr_debug("enter resume\n");
1162 /* Resume from suspend */
1163 if (s
->pdata
->hw_suspend
)
1164 s
->pdata
->hw_suspend(s
, 0);
1166 /* Resume UART port */
1167 uart_resume_port(&max3107_uart_driver
, &s
->port
);
1168 #endif /* CONFIG_PM */
1171 EXPORT_SYMBOL_GPL(max3107_resume
);
1173 static int max3107_probe_generic(struct spi_device
*spi
)
1175 return max3107_probe(spi
, &generic_plat_data
);
1178 /* Spi driver data */
1179 static struct spi_driver max3107_driver
= {
1182 .bus
= &spi_bus_type
,
1183 .owner
= THIS_MODULE
,
1185 .probe
= max3107_probe_generic
,
1186 .remove
= __devexit_p(max3107_remove
),
1187 .suspend
= max3107_suspend
,
1188 .resume
= max3107_resume
,
1191 /* Driver init function */
1192 static int __init
max3107_init(void)
1194 pr_info("enter max3107 init\n");
1195 return spi_register_driver(&max3107_driver
);
1198 /* Driver exit function */
1199 static void __exit
max3107_exit(void)
1201 pr_info("enter max3107 exit\n");
1202 /* Unregister UART driver */
1203 if (driver_registered
)
1204 uart_unregister_driver(&max3107_uart_driver
);
1205 spi_unregister_driver(&max3107_driver
);
1208 module_init(max3107_init
);
1209 module_exit(max3107_exit
);
1211 MODULE_DESCRIPTION("MAX3107 driver");
1212 MODULE_AUTHOR("Aavamobile");
1213 MODULE_ALIAS("spi:max3107");
1214 MODULE_LICENSE("GPL v2");