1 // SPDX-License-Identifier: GPL-2.0
2 /****************************************************************************
4 * Driver for the IFX 6x60 spi modem.
6 * Copyright (C) 2008 Option International
7 * Copyright (C) 2008 Filip Aben <f.aben@option.com>
8 * Denis Joseph Barrow <d.barow@option.com>
9 * Jan Dumon <j.dumon@option.com>
11 * Copyright (C) 2009, 2010 Intel Corp
12 * Russ Gorby <russ.gorby@intel.com>
14 * Driver modified by Intel from Option gtm501l_spi.c
17 * o The driver currently assumes a single device only. If you need to
18 * change this then look for saved_ifx_dev and add a device lookup
19 * o The driver is intended to be big-endian safe but has never been
20 * tested that way (no suitable hardware). There are a couple of FIXME
21 * notes by areas that may need addressing
22 * o Some of the GPIO naming/setup assumptions may need revisiting if
23 * you need to use this driver for another platform.
25 *****************************************************************************/
26 #include <linux/dma-mapping.h>
27 #include <linux/module.h>
28 #include <linux/termios.h>
29 #include <linux/tty.h>
30 #include <linux/device.h>
31 #include <linux/spi/spi.h>
32 #include <linux/kfifo.h>
33 #include <linux/tty_flip.h>
34 #include <linux/timer.h>
35 #include <linux/serial.h>
36 #include <linux/interrupt.h>
37 #include <linux/irq.h>
38 #include <linux/rfkill.h>
41 #include <linux/dmapool.h>
42 #include <linux/gpio.h>
43 #include <linux/sched.h>
44 #include <linux/time.h>
45 #include <linux/wait.h>
47 #include <linux/pm_runtime.h>
48 #include <linux/spi/ifx_modem.h>
49 #include <linux/delay.h>
50 #include <linux/reboot.h>
54 #define IFX_SPI_MORE_MASK 0x10
55 #define IFX_SPI_MORE_BIT 4 /* bit position in u8 */
56 #define IFX_SPI_CTS_BIT 6 /* bit position in u8 */
57 #define IFX_SPI_MODE SPI_MODE_1
58 #define IFX_SPI_TTY_ID 0
59 #define IFX_SPI_TIMEOUT_SEC 2
60 #define IFX_SPI_HEADER_0 (-1)
61 #define IFX_SPI_HEADER_F (-2)
63 #define PO_POST_DELAY 200
64 #define IFX_MDM_RST_PMU 4
66 /* forward reference */
67 static void ifx_spi_handle_srdy(struct ifx_spi_device
*ifx_dev
);
68 static int ifx_modem_reboot_callback(struct notifier_block
*nfb
,
69 unsigned long event
, void *data
);
70 static int ifx_modem_power_off(struct ifx_spi_device
*ifx_dev
);
73 static int spi_bpw
= 16; /* 8, 16 or 32 bit word length */
74 static struct tty_driver
*tty_drv
;
75 static struct ifx_spi_device
*saved_ifx_dev
;
76 static struct lock_class_key ifx_spi_key
;
78 static struct notifier_block ifx_modem_reboot_notifier_block
= {
79 .notifier_call
= ifx_modem_reboot_callback
,
82 static int ifx_modem_power_off(struct ifx_spi_device
*ifx_dev
)
84 gpio_set_value(IFX_MDM_RST_PMU
, 1);
85 msleep(PO_POST_DELAY
);
90 static int ifx_modem_reboot_callback(struct notifier_block
*nfb
,
91 unsigned long event
, void *data
)
94 ifx_modem_power_off(saved_ifx_dev
);
96 pr_warn("no ifx modem active;\n");
101 /* GPIO/GPE settings */
104 * mrdy_set_high - set MRDY GPIO
105 * @ifx: device we are controlling
108 static inline void mrdy_set_high(struct ifx_spi_device
*ifx
)
110 gpio_set_value(ifx
->gpio
.mrdy
, 1);
114 * mrdy_set_low - clear MRDY GPIO
115 * @ifx: device we are controlling
118 static inline void mrdy_set_low(struct ifx_spi_device
*ifx
)
120 gpio_set_value(ifx
->gpio
.mrdy
, 0);
124 * ifx_spi_power_state_set
125 * @ifx_dev: our SPI device
128 * Set bit in power status and signal power system if status becomes non-0
131 ifx_spi_power_state_set(struct ifx_spi_device
*ifx_dev
, unsigned char val
)
135 spin_lock_irqsave(&ifx_dev
->power_lock
, flags
);
138 * if power status is already non-0, just update, else
141 if (!ifx_dev
->power_status
)
142 pm_runtime_get(&ifx_dev
->spi_dev
->dev
);
143 ifx_dev
->power_status
|= val
;
145 spin_unlock_irqrestore(&ifx_dev
->power_lock
, flags
);
149 * ifx_spi_power_state_clear - clear power bit
150 * @ifx_dev: our SPI device
151 * @val: bits to clear
153 * clear bit in power status and signal power system if status becomes 0
156 ifx_spi_power_state_clear(struct ifx_spi_device
*ifx_dev
, unsigned char val
)
160 spin_lock_irqsave(&ifx_dev
->power_lock
, flags
);
162 if (ifx_dev
->power_status
) {
163 ifx_dev
->power_status
&= ~val
;
164 if (!ifx_dev
->power_status
)
165 pm_runtime_put(&ifx_dev
->spi_dev
->dev
);
168 spin_unlock_irqrestore(&ifx_dev
->power_lock
, flags
);
174 * @len : number of bytes (not words) in the buffer
175 * @end: end of buffer
177 * Swap the contents of a buffer into big endian format
179 static inline void swap_buf_8(unsigned char *buf
, int len
, void *end
)
181 /* don't swap buffer if SPI word width is 8 bits */
188 * @len : number of bytes (not words) in the buffer
189 * @end: end of buffer
191 * Swap the contents of a buffer into big endian format
193 static inline void swap_buf_16(unsigned char *buf
, int len
, void *end
)
197 u16
*buf_16
= (u16
*)buf
;
198 len
= ((len
+ 1) >> 1);
199 if ((void *)&buf_16
[len
] > end
) {
200 pr_err("swap_buf_16: swap exceeds boundary (%p > %p)!",
204 for (n
= 0; n
< len
; n
++) {
205 *buf_16
= cpu_to_be16(*buf_16
);
213 * @len : number of bytes (not words) in the buffer
214 * @end: end of buffer
216 * Swap the contents of a buffer into big endian format
218 static inline void swap_buf_32(unsigned char *buf
, int len
, void *end
)
222 u32
*buf_32
= (u32
*)buf
;
223 len
= (len
+ 3) >> 2;
225 if ((void *)&buf_32
[len
] > end
) {
226 pr_err("swap_buf_32: swap exceeds boundary (%p > %p)!\n",
230 for (n
= 0; n
< len
; n
++) {
231 *buf_32
= cpu_to_be32(*buf_32
);
237 * mrdy_assert - assert MRDY line
238 * @ifx_dev: our SPI device
240 * Assert mrdy and set timer to wait for SRDY interrupt, if SRDY is low
243 * FIXME: Can SRDY even go high as we are running this code ?
245 static void mrdy_assert(struct ifx_spi_device
*ifx_dev
)
247 int val
= gpio_get_value(ifx_dev
->gpio
.srdy
);
249 if (!test_and_set_bit(IFX_SPI_STATE_TIMER_PENDING
,
251 mod_timer(&ifx_dev
->spi_timer
,jiffies
+ IFX_SPI_TIMEOUT_SEC
*HZ
);
255 ifx_spi_power_state_set(ifx_dev
, IFX_SPI_POWER_DATA_PENDING
);
256 mrdy_set_high(ifx_dev
);
260 * ifx_spi_timeout - SPI timeout
261 * @arg: our SPI device
263 * The SPI has timed out: hang up the tty. Users will then see a hangup
266 static void ifx_spi_timeout(struct timer_list
*t
)
268 struct ifx_spi_device
*ifx_dev
= from_timer(ifx_dev
, t
, spi_timer
);
270 dev_warn(&ifx_dev
->spi_dev
->dev
, "*** SPI Timeout ***");
271 tty_port_tty_hangup(&ifx_dev
->tty_port
, false);
272 mrdy_set_low(ifx_dev
);
273 clear_bit(IFX_SPI_STATE_TIMER_PENDING
, &ifx_dev
->flags
);
276 /* char/tty operations */
279 * ifx_spi_tiocmget - get modem lines
280 * @tty: our tty device
281 * @filp: file handle issuing the request
283 * Map the signal state into Linux modem flags and report the value
286 static int ifx_spi_tiocmget(struct tty_struct
*tty
)
289 struct ifx_spi_device
*ifx_dev
= tty
->driver_data
;
292 (test_bit(IFX_SPI_RTS
, &ifx_dev
->signal_state
) ? TIOCM_RTS
: 0) |
293 (test_bit(IFX_SPI_DTR
, &ifx_dev
->signal_state
) ? TIOCM_DTR
: 0) |
294 (test_bit(IFX_SPI_CTS
, &ifx_dev
->signal_state
) ? TIOCM_CTS
: 0) |
295 (test_bit(IFX_SPI_DSR
, &ifx_dev
->signal_state
) ? TIOCM_DSR
: 0) |
296 (test_bit(IFX_SPI_DCD
, &ifx_dev
->signal_state
) ? TIOCM_CAR
: 0) |
297 (test_bit(IFX_SPI_RI
, &ifx_dev
->signal_state
) ? TIOCM_RNG
: 0);
302 * ifx_spi_tiocmset - set modem bits
303 * @tty: the tty structure
305 * @clear: bits to clear
307 * The IFX6x60 only supports DTR and RTS. Set them accordingly
308 * and flag that an update to the modem is needed.
310 * FIXME: do we need to kick the tranfers when we do this ?
312 static int ifx_spi_tiocmset(struct tty_struct
*tty
,
313 unsigned int set
, unsigned int clear
)
315 struct ifx_spi_device
*ifx_dev
= tty
->driver_data
;
318 set_bit(IFX_SPI_RTS
, &ifx_dev
->signal_state
);
320 set_bit(IFX_SPI_DTR
, &ifx_dev
->signal_state
);
321 if (clear
& TIOCM_RTS
)
322 clear_bit(IFX_SPI_RTS
, &ifx_dev
->signal_state
);
323 if (clear
& TIOCM_DTR
)
324 clear_bit(IFX_SPI_DTR
, &ifx_dev
->signal_state
);
326 set_bit(IFX_SPI_UPDATE
, &ifx_dev
->signal_state
);
331 * ifx_spi_open - called on tty open
332 * @tty: our tty device
333 * @filp: file handle being associated with the tty
335 * Open the tty interface. We let the tty_port layer do all the work
338 * FIXME: Remove single device assumption and saved_ifx_dev
340 static int ifx_spi_open(struct tty_struct
*tty
, struct file
*filp
)
342 return tty_port_open(&saved_ifx_dev
->tty_port
, tty
, filp
);
346 * ifx_spi_close - called when our tty closes
347 * @tty: the tty being closed
348 * @filp: the file handle being closed
350 * Perform the close of the tty. We use the tty_port layer to do all
353 static void ifx_spi_close(struct tty_struct
*tty
, struct file
*filp
)
355 struct ifx_spi_device
*ifx_dev
= tty
->driver_data
;
356 tty_port_close(&ifx_dev
->tty_port
, tty
, filp
);
357 /* FIXME: should we do an ifx_spi_reset here ? */
361 * ifx_decode_spi_header - decode received header
362 * @buffer: the received data
363 * @length: decoded length
364 * @more: decoded more flag
365 * @received_cts: status of cts we received
367 * Note how received_cts is handled -- if header is all F it is left
368 * the same as it was, if header is all 0 it is set to 0 otherwise it is
369 * taken from the incoming header.
373 static int ifx_spi_decode_spi_header(unsigned char *buffer
, int *length
,
374 unsigned char *more
, unsigned char *received_cts
)
378 u16
*in_buffer
= (u16
*)buffer
;
383 if (h1
== 0 && h2
== 0) {
386 return IFX_SPI_HEADER_0
;
387 } else if (h1
== 0xffff && h2
== 0xffff) {
389 /* spi_slave_cts remains as it was */
390 return IFX_SPI_HEADER_F
;
393 *length
= h1
& 0xfff; /* upper bits of byte are flags */
394 *more
= (buffer
[1] >> IFX_SPI_MORE_BIT
) & 1;
395 *received_cts
= (buffer
[3] >> IFX_SPI_CTS_BIT
) & 1;
400 * ifx_setup_spi_header - set header fields
401 * @txbuffer: pointer to start of SPI buffer
403 * @more: indicate if more to follow
405 * Format up an SPI header for a transfer
409 static void ifx_spi_setup_spi_header(unsigned char *txbuffer
, int tx_count
,
412 *(u16
*)(txbuffer
) = tx_count
;
413 *(u16
*)(txbuffer
+2) = IFX_SPI_PAYLOAD_SIZE
;
414 txbuffer
[1] |= (more
<< IFX_SPI_MORE_BIT
) & IFX_SPI_MORE_MASK
;
418 * ifx_spi_prepare_tx_buffer - prepare transmit frame
419 * @ifx_dev: our SPI device
421 * The transmit buffr needs a header and various other bits of
422 * information followed by as much data as we can pull from the FIFO
423 * and transfer. This function formats up a suitable buffer in the
426 * FIXME: performance - should we wake the tty when the queue is half
429 static int ifx_spi_prepare_tx_buffer(struct ifx_spi_device
*ifx_dev
)
434 unsigned char *tx_buffer
;
436 tx_buffer
= ifx_dev
->tx_buffer
;
438 /* make room for required SPI header */
439 tx_buffer
+= IFX_SPI_HEADER_OVERHEAD
;
440 tx_count
= IFX_SPI_HEADER_OVERHEAD
;
442 /* clear to signal no more data if this turns out to be the
443 * last buffer sent in a sequence */
444 ifx_dev
->spi_more
= 0;
446 /* if modem cts is set, just send empty buffer */
447 if (!ifx_dev
->spi_slave_cts
) {
448 /* see if there's tx data */
449 queue_length
= kfifo_len(&ifx_dev
->tx_fifo
);
450 if (queue_length
!= 0) {
451 /* data to mux -- see if there's room for it */
452 temp_count
= min(queue_length
, IFX_SPI_PAYLOAD_SIZE
);
453 temp_count
= kfifo_out_locked(&ifx_dev
->tx_fifo
,
454 tx_buffer
, temp_count
,
455 &ifx_dev
->fifo_lock
);
457 /* update buffer pointer and data count in message */
458 tx_buffer
+= temp_count
;
459 tx_count
+= temp_count
;
460 if (temp_count
== queue_length
)
461 /* poke port to get more data */
462 tty_port_tty_wakeup(&ifx_dev
->tty_port
);
463 else /* more data in port, use next SPI message */
464 ifx_dev
->spi_more
= 1;
467 /* have data and info for header -- set up SPI header in buffer */
468 /* spi header needs payload size, not entire buffer size */
469 ifx_spi_setup_spi_header(ifx_dev
->tx_buffer
,
470 tx_count
-IFX_SPI_HEADER_OVERHEAD
,
472 /* swap actual data in the buffer */
473 ifx_dev
->swap_buf((ifx_dev
->tx_buffer
), tx_count
,
474 &ifx_dev
->tx_buffer
[IFX_SPI_TRANSFER_SIZE
]);
479 * ifx_spi_write - line discipline write
480 * @tty: our tty device
481 * @buf: pointer to buffer to write (kernel space)
482 * @count: size of buffer
484 * Write the characters we have been given into the FIFO. If the device
485 * is not active then activate it, when the SRDY line is asserted back
486 * this will commence I/O
488 static int ifx_spi_write(struct tty_struct
*tty
, const unsigned char *buf
,
491 struct ifx_spi_device
*ifx_dev
= tty
->driver_data
;
492 unsigned char *tmp_buf
= (unsigned char *)buf
;
497 spin_lock_irqsave(&ifx_dev
->fifo_lock
, flags
);
498 is_fifo_empty
= kfifo_is_empty(&ifx_dev
->tx_fifo
);
499 tx_count
= kfifo_in(&ifx_dev
->tx_fifo
, tmp_buf
, count
);
500 spin_unlock_irqrestore(&ifx_dev
->fifo_lock
, flags
);
502 mrdy_assert(ifx_dev
);
508 * ifx_spi_chars_in_buffer - line discipline helper
509 * @tty: our tty device
511 * Report how much data we can accept before we drop bytes. As we use
512 * a simple FIFO this is nice and easy.
514 static int ifx_spi_write_room(struct tty_struct
*tty
)
516 struct ifx_spi_device
*ifx_dev
= tty
->driver_data
;
517 return IFX_SPI_FIFO_SIZE
- kfifo_len(&ifx_dev
->tx_fifo
);
521 * ifx_spi_chars_in_buffer - line discipline helper
522 * @tty: our tty device
524 * Report how many characters we have buffered. In our case this is the
525 * number of bytes sitting in our transmit FIFO.
527 static int ifx_spi_chars_in_buffer(struct tty_struct
*tty
)
529 struct ifx_spi_device
*ifx_dev
= tty
->driver_data
;
530 return kfifo_len(&ifx_dev
->tx_fifo
);
535 * @port: our tty port
537 * tty port hang up. Called when tty_hangup processing is invoked either
538 * by loss of carrier, or by software (eg vhangup). Serialized against
539 * activate/shutdown by the tty layer.
541 static void ifx_spi_hangup(struct tty_struct
*tty
)
543 struct ifx_spi_device
*ifx_dev
= tty
->driver_data
;
544 tty_port_hangup(&ifx_dev
->tty_port
);
549 * @port: our tty port
551 * tty port activate method - called for first open. Serialized
552 * with hangup and shutdown by the tty layer.
554 static int ifx_port_activate(struct tty_port
*port
, struct tty_struct
*tty
)
556 struct ifx_spi_device
*ifx_dev
=
557 container_of(port
, struct ifx_spi_device
, tty_port
);
559 /* clear any old data; can't do this in 'close' */
560 kfifo_reset(&ifx_dev
->tx_fifo
);
562 /* clear any flag which may be set in port shutdown procedure */
563 clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS
, &ifx_dev
->flags
);
564 clear_bit(IFX_SPI_STATE_IO_READY
, &ifx_dev
->flags
);
566 /* put port data into this tty */
567 tty
->driver_data
= ifx_dev
;
569 /* allows flip string push from int context */
570 port
->low_latency
= 1;
572 /* set flag to allows data transfer */
573 set_bit(IFX_SPI_STATE_IO_AVAILABLE
, &ifx_dev
->flags
);
580 * @port: our tty port
582 * tty port shutdown method - called for last port close. Serialized
583 * with hangup and activate by the tty layer.
585 static void ifx_port_shutdown(struct tty_port
*port
)
587 struct ifx_spi_device
*ifx_dev
=
588 container_of(port
, struct ifx_spi_device
, tty_port
);
590 clear_bit(IFX_SPI_STATE_IO_AVAILABLE
, &ifx_dev
->flags
);
591 mrdy_set_low(ifx_dev
);
592 del_timer(&ifx_dev
->spi_timer
);
593 clear_bit(IFX_SPI_STATE_TIMER_PENDING
, &ifx_dev
->flags
);
594 tasklet_kill(&ifx_dev
->io_work_tasklet
);
597 static const struct tty_port_operations ifx_tty_port_ops
= {
598 .activate
= ifx_port_activate
,
599 .shutdown
= ifx_port_shutdown
,
602 static const struct tty_operations ifx_spi_serial_ops
= {
603 .open
= ifx_spi_open
,
604 .close
= ifx_spi_close
,
605 .write
= ifx_spi_write
,
606 .hangup
= ifx_spi_hangup
,
607 .write_room
= ifx_spi_write_room
,
608 .chars_in_buffer
= ifx_spi_chars_in_buffer
,
609 .tiocmget
= ifx_spi_tiocmget
,
610 .tiocmset
= ifx_spi_tiocmset
,
614 * ifx_spi_insert_fip_string - queue received data
615 * @ifx_ser: our SPI device
616 * @chars: buffer we have received
617 * @size: number of chars reeived
619 * Queue bytes to the tty assuming the tty side is currently open. If
620 * not the discard the data.
622 static void ifx_spi_insert_flip_string(struct ifx_spi_device
*ifx_dev
,
623 unsigned char *chars
, size_t size
)
625 tty_insert_flip_string(&ifx_dev
->tty_port
, chars
, size
);
626 tty_flip_buffer_push(&ifx_dev
->tty_port
);
630 * ifx_spi_complete - SPI transfer completed
631 * @ctx: our SPI device
633 * An SPI transfer has completed. Process any received data and kick off
634 * any further transmits we can commence.
636 static void ifx_spi_complete(void *ctx
)
638 struct ifx_spi_device
*ifx_dev
= ctx
;
641 unsigned char more
= 0;
643 int local_write_pending
= 0;
648 mrdy_set_low(ifx_dev
);
650 if (!ifx_dev
->spi_msg
.status
) {
651 /* check header validity, get comm flags */
652 ifx_dev
->swap_buf(ifx_dev
->rx_buffer
, IFX_SPI_HEADER_OVERHEAD
,
653 &ifx_dev
->rx_buffer
[IFX_SPI_HEADER_OVERHEAD
]);
654 decode_result
= ifx_spi_decode_spi_header(ifx_dev
->rx_buffer
,
655 &length
, &more
, &cts
);
656 if (decode_result
== IFX_SPI_HEADER_0
) {
657 dev_dbg(&ifx_dev
->spi_dev
->dev
,
658 "ignore input: invalid header 0");
659 ifx_dev
->spi_slave_cts
= 0;
661 } else if (decode_result
== IFX_SPI_HEADER_F
) {
662 dev_dbg(&ifx_dev
->spi_dev
->dev
,
663 "ignore input: invalid header F");
667 ifx_dev
->spi_slave_cts
= cts
;
669 actual_length
= min((unsigned int)length
,
670 ifx_dev
->spi_msg
.actual_length
);
672 (ifx_dev
->rx_buffer
+ IFX_SPI_HEADER_OVERHEAD
),
674 &ifx_dev
->rx_buffer
[IFX_SPI_TRANSFER_SIZE
]);
675 ifx_spi_insert_flip_string(
677 ifx_dev
->rx_buffer
+ IFX_SPI_HEADER_OVERHEAD
,
678 (size_t)actual_length
);
681 dev_dbg(&ifx_dev
->spi_dev
->dev
, "SPI transfer error %d",
682 ifx_dev
->spi_msg
.status
);
686 if (ifx_dev
->write_pending
) {
687 ifx_dev
->write_pending
= 0;
688 local_write_pending
= 1;
691 clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS
, &(ifx_dev
->flags
));
693 queue_length
= kfifo_len(&ifx_dev
->tx_fifo
);
694 srdy
= gpio_get_value(ifx_dev
->gpio
.srdy
);
696 ifx_spi_power_state_clear(ifx_dev
, IFX_SPI_POWER_SRDY
);
698 /* schedule output if there is more to do */
699 if (test_and_clear_bit(IFX_SPI_STATE_IO_READY
, &ifx_dev
->flags
))
700 tasklet_schedule(&ifx_dev
->io_work_tasklet
);
702 if (more
|| ifx_dev
->spi_more
|| queue_length
> 0 ||
703 local_write_pending
) {
704 if (ifx_dev
->spi_slave_cts
) {
706 mrdy_assert(ifx_dev
);
708 mrdy_assert(ifx_dev
);
711 * poke line discipline driver if any for more data
712 * may or may not get more data to write
713 * for now, say not busy
715 ifx_spi_power_state_clear(ifx_dev
,
716 IFX_SPI_POWER_DATA_PENDING
);
717 tty_port_tty_wakeup(&ifx_dev
->tty_port
);
723 * ifx_spio_io - I/O tasklet
724 * @data: our SPI device
726 * Queue data for transmission if possible and then kick off the
729 static void ifx_spi_io(unsigned long data
)
732 struct ifx_spi_device
*ifx_dev
= (struct ifx_spi_device
*) data
;
734 if (!test_and_set_bit(IFX_SPI_STATE_IO_IN_PROGRESS
, &ifx_dev
->flags
) &&
735 test_bit(IFX_SPI_STATE_IO_AVAILABLE
, &ifx_dev
->flags
)) {
736 if (ifx_dev
->gpio
.unack_srdy_int_nb
> 0)
737 ifx_dev
->gpio
.unack_srdy_int_nb
--;
739 ifx_spi_prepare_tx_buffer(ifx_dev
);
741 spi_message_init(&ifx_dev
->spi_msg
);
742 INIT_LIST_HEAD(&ifx_dev
->spi_msg
.queue
);
744 ifx_dev
->spi_msg
.context
= ifx_dev
;
745 ifx_dev
->spi_msg
.complete
= ifx_spi_complete
;
747 /* set up our spi transfer */
748 /* note len is BYTES, not transfers */
749 ifx_dev
->spi_xfer
.len
= IFX_SPI_TRANSFER_SIZE
;
750 ifx_dev
->spi_xfer
.cs_change
= 0;
751 ifx_dev
->spi_xfer
.speed_hz
= ifx_dev
->spi_dev
->max_speed_hz
;
752 /* ifx_dev->spi_xfer.speed_hz = 390625; */
753 ifx_dev
->spi_xfer
.bits_per_word
=
754 ifx_dev
->spi_dev
->bits_per_word
;
756 ifx_dev
->spi_xfer
.tx_buf
= ifx_dev
->tx_buffer
;
757 ifx_dev
->spi_xfer
.rx_buf
= ifx_dev
->rx_buffer
;
762 if (ifx_dev
->use_dma
) {
763 ifx_dev
->spi_msg
.is_dma_mapped
= 1;
764 ifx_dev
->tx_dma
= ifx_dev
->tx_bus
;
765 ifx_dev
->rx_dma
= ifx_dev
->rx_bus
;
766 ifx_dev
->spi_xfer
.tx_dma
= ifx_dev
->tx_dma
;
767 ifx_dev
->spi_xfer
.rx_dma
= ifx_dev
->rx_dma
;
769 ifx_dev
->spi_msg
.is_dma_mapped
= 0;
770 ifx_dev
->tx_dma
= (dma_addr_t
)0;
771 ifx_dev
->rx_dma
= (dma_addr_t
)0;
772 ifx_dev
->spi_xfer
.tx_dma
= (dma_addr_t
)0;
773 ifx_dev
->spi_xfer
.rx_dma
= (dma_addr_t
)0;
776 spi_message_add_tail(&ifx_dev
->spi_xfer
, &ifx_dev
->spi_msg
);
778 /* Assert MRDY. This may have already been done by the write
781 mrdy_assert(ifx_dev
);
783 retval
= spi_async(ifx_dev
->spi_dev
, &ifx_dev
->spi_msg
);
785 clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS
,
787 tasklet_schedule(&ifx_dev
->io_work_tasklet
);
791 ifx_dev
->write_pending
= 1;
795 * ifx_spi_free_port - free up the tty side
796 * @ifx_dev: IFX device going away
798 * Unregister and free up a port when the device goes away
800 static void ifx_spi_free_port(struct ifx_spi_device
*ifx_dev
)
802 if (ifx_dev
->tty_dev
)
803 tty_unregister_device(tty_drv
, ifx_dev
->minor
);
804 tty_port_destroy(&ifx_dev
->tty_port
);
805 kfifo_free(&ifx_dev
->tx_fifo
);
809 * ifx_spi_create_port - create a new port
810 * @ifx_dev: our spi device
812 * Allocate and initialise the tty port that goes with this interface
813 * and add it to the tty layer so that it can be opened.
815 static int ifx_spi_create_port(struct ifx_spi_device
*ifx_dev
)
818 struct tty_port
*pport
= &ifx_dev
->tty_port
;
820 spin_lock_init(&ifx_dev
->fifo_lock
);
821 lockdep_set_class_and_subclass(&ifx_dev
->fifo_lock
,
824 if (kfifo_alloc(&ifx_dev
->tx_fifo
, IFX_SPI_FIFO_SIZE
, GFP_KERNEL
)) {
829 tty_port_init(pport
);
830 pport
->ops
= &ifx_tty_port_ops
;
831 ifx_dev
->minor
= IFX_SPI_TTY_ID
;
832 ifx_dev
->tty_dev
= tty_port_register_device(pport
, tty_drv
,
833 ifx_dev
->minor
, &ifx_dev
->spi_dev
->dev
);
834 if (IS_ERR(ifx_dev
->tty_dev
)) {
835 dev_dbg(&ifx_dev
->spi_dev
->dev
,
836 "%s: registering tty device failed", __func__
);
837 ret
= PTR_ERR(ifx_dev
->tty_dev
);
843 tty_port_destroy(pport
);
845 ifx_spi_free_port(ifx_dev
);
850 * ifx_spi_handle_srdy - handle SRDY
851 * @ifx_dev: device asserting SRDY
853 * Check our device state and see what we need to kick off when SRDY
854 * is asserted. This usually means killing the timer and firing off the
857 static void ifx_spi_handle_srdy(struct ifx_spi_device
*ifx_dev
)
859 if (test_bit(IFX_SPI_STATE_TIMER_PENDING
, &ifx_dev
->flags
)) {
860 del_timer(&ifx_dev
->spi_timer
);
861 clear_bit(IFX_SPI_STATE_TIMER_PENDING
, &ifx_dev
->flags
);
864 ifx_spi_power_state_set(ifx_dev
, IFX_SPI_POWER_SRDY
);
866 if (!test_bit(IFX_SPI_STATE_IO_IN_PROGRESS
, &ifx_dev
->flags
))
867 tasklet_schedule(&ifx_dev
->io_work_tasklet
);
869 set_bit(IFX_SPI_STATE_IO_READY
, &ifx_dev
->flags
);
873 * ifx_spi_srdy_interrupt - SRDY asserted
874 * @irq: our IRQ number
875 * @dev: our ifx device
877 * The modem asserted SRDY. Handle the srdy event
879 static irqreturn_t
ifx_spi_srdy_interrupt(int irq
, void *dev
)
881 struct ifx_spi_device
*ifx_dev
= dev
;
882 ifx_dev
->gpio
.unack_srdy_int_nb
++;
883 ifx_spi_handle_srdy(ifx_dev
);
888 * ifx_spi_reset_interrupt - Modem has changed reset state
889 * @irq: interrupt number
890 * @dev: our device pointer
892 * The modem has either entered or left reset state. Check the GPIO
895 * FIXME: review locking on MR_INPROGRESS versus
896 * parallel unsolicited reset/solicited reset
898 static irqreturn_t
ifx_spi_reset_interrupt(int irq
, void *dev
)
900 struct ifx_spi_device
*ifx_dev
= dev
;
901 int val
= gpio_get_value(ifx_dev
->gpio
.reset_out
);
902 int solreset
= test_bit(MR_START
, &ifx_dev
->mdm_reset_state
);
906 set_bit(MR_INPROGRESS
, &ifx_dev
->mdm_reset_state
);
908 /* unsolicited reset */
909 tty_port_tty_hangup(&ifx_dev
->tty_port
, false);
913 clear_bit(MR_INPROGRESS
, &ifx_dev
->mdm_reset_state
);
915 set_bit(MR_COMPLETE
, &ifx_dev
->mdm_reset_state
);
916 wake_up(&ifx_dev
->mdm_reset_wait
);
923 * ifx_spi_free_device - free device
924 * @ifx_dev: device to free
926 * Free the IFX device
928 static void ifx_spi_free_device(struct ifx_spi_device
*ifx_dev
)
930 ifx_spi_free_port(ifx_dev
);
931 dma_free_coherent(&ifx_dev
->spi_dev
->dev
,
932 IFX_SPI_TRANSFER_SIZE
,
935 dma_free_coherent(&ifx_dev
->spi_dev
->dev
,
936 IFX_SPI_TRANSFER_SIZE
,
942 * ifx_spi_reset - reset modem
943 * @ifx_dev: modem to reset
945 * Perform a reset on the modem
947 static int ifx_spi_reset(struct ifx_spi_device
*ifx_dev
)
951 * set up modem power, reset
953 * delays are required on some platforms for the modem
956 set_bit(MR_START
, &ifx_dev
->mdm_reset_state
);
957 gpio_set_value(ifx_dev
->gpio
.po
, 0);
958 gpio_set_value(ifx_dev
->gpio
.reset
, 0);
960 gpio_set_value(ifx_dev
->gpio
.reset
, 1);
962 gpio_set_value(ifx_dev
->gpio
.po
, 1);
964 gpio_set_value(ifx_dev
->gpio
.po
, 0);
965 ret
= wait_event_timeout(ifx_dev
->mdm_reset_wait
,
966 test_bit(MR_COMPLETE
,
967 &ifx_dev
->mdm_reset_state
),
970 dev_warn(&ifx_dev
->spi_dev
->dev
, "Modem reset timeout: (state:%lx)",
971 ifx_dev
->mdm_reset_state
);
973 ifx_dev
->mdm_reset_state
= 0;
978 * ifx_spi_spi_probe - probe callback
979 * @spi: our possible matching SPI device
981 * Probe for a 6x60 modem on SPI bus. Perform any needed device and
985 * - Support for multiple devices
986 * - Split out MID specific GPIO handling eventually
989 static int ifx_spi_spi_probe(struct spi_device
*spi
)
993 struct ifx_modem_platform_data
*pl_data
;
994 struct ifx_spi_device
*ifx_dev
;
997 dev_dbg(&spi
->dev
, "ignoring subsequent detection");
1001 pl_data
= dev_get_platdata(&spi
->dev
);
1003 dev_err(&spi
->dev
, "missing platform data!");
1007 /* initialize structure to hold our device variables */
1008 ifx_dev
= kzalloc(sizeof(struct ifx_spi_device
), GFP_KERNEL
);
1010 dev_err(&spi
->dev
, "spi device allocation failed");
1013 saved_ifx_dev
= ifx_dev
;
1014 ifx_dev
->spi_dev
= spi
;
1015 clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS
, &ifx_dev
->flags
);
1016 spin_lock_init(&ifx_dev
->write_lock
);
1017 spin_lock_init(&ifx_dev
->power_lock
);
1018 ifx_dev
->power_status
= 0;
1019 timer_setup(&ifx_dev
->spi_timer
, ifx_spi_timeout
, 0);
1020 ifx_dev
->modem
= pl_data
->modem_type
;
1021 ifx_dev
->use_dma
= pl_data
->use_dma
;
1022 ifx_dev
->max_hz
= pl_data
->max_hz
;
1023 /* initialize spi mode, etc */
1024 spi
->max_speed_hz
= ifx_dev
->max_hz
;
1025 spi
->mode
= IFX_SPI_MODE
| (SPI_LOOP
& spi
->mode
);
1026 spi
->bits_per_word
= spi_bpw
;
1027 ret
= spi_setup(spi
);
1029 dev_err(&spi
->dev
, "SPI setup wasn't successful %d", ret
);
1034 /* init swap_buf function according to word width configuration */
1035 if (spi
->bits_per_word
== 32)
1036 ifx_dev
->swap_buf
= swap_buf_32
;
1037 else if (spi
->bits_per_word
== 16)
1038 ifx_dev
->swap_buf
= swap_buf_16
;
1040 ifx_dev
->swap_buf
= swap_buf_8
;
1042 /* ensure SPI protocol flags are initialized to enable transfer */
1043 ifx_dev
->spi_more
= 0;
1044 ifx_dev
->spi_slave_cts
= 0;
1046 /*initialize transfer and dma buffers */
1047 ifx_dev
->tx_buffer
= dma_alloc_coherent(ifx_dev
->spi_dev
->dev
.parent
,
1048 IFX_SPI_TRANSFER_SIZE
,
1051 if (!ifx_dev
->tx_buffer
) {
1052 dev_err(&spi
->dev
, "DMA-TX buffer allocation failed");
1056 ifx_dev
->rx_buffer
= dma_alloc_coherent(ifx_dev
->spi_dev
->dev
.parent
,
1057 IFX_SPI_TRANSFER_SIZE
,
1060 if (!ifx_dev
->rx_buffer
) {
1061 dev_err(&spi
->dev
, "DMA-RX buffer allocation failed");
1066 /* initialize waitq for modem reset */
1067 init_waitqueue_head(&ifx_dev
->mdm_reset_wait
);
1069 spi_set_drvdata(spi
, ifx_dev
);
1070 tasklet_init(&ifx_dev
->io_work_tasklet
, ifx_spi_io
,
1071 (unsigned long)ifx_dev
);
1073 set_bit(IFX_SPI_STATE_PRESENT
, &ifx_dev
->flags
);
1075 /* create our tty port */
1076 ret
= ifx_spi_create_port(ifx_dev
);
1078 dev_err(&spi
->dev
, "create default tty port failed");
1082 ifx_dev
->gpio
.reset
= pl_data
->rst_pmu
;
1083 ifx_dev
->gpio
.po
= pl_data
->pwr_on
;
1084 ifx_dev
->gpio
.mrdy
= pl_data
->mrdy
;
1085 ifx_dev
->gpio
.srdy
= pl_data
->srdy
;
1086 ifx_dev
->gpio
.reset_out
= pl_data
->rst_out
;
1088 dev_info(&spi
->dev
, "gpios %d, %d, %d, %d, %d",
1089 ifx_dev
->gpio
.reset
, ifx_dev
->gpio
.po
, ifx_dev
->gpio
.mrdy
,
1090 ifx_dev
->gpio
.srdy
, ifx_dev
->gpio
.reset_out
);
1092 /* Configure gpios */
1093 ret
= gpio_request(ifx_dev
->gpio
.reset
, "ifxModem");
1095 dev_err(&spi
->dev
, "Unable to allocate GPIO%d (RESET)",
1096 ifx_dev
->gpio
.reset
);
1099 ret
+= gpio_direction_output(ifx_dev
->gpio
.reset
, 0);
1100 ret
+= gpio_export(ifx_dev
->gpio
.reset
, 1);
1102 dev_err(&spi
->dev
, "Unable to configure GPIO%d (RESET)",
1103 ifx_dev
->gpio
.reset
);
1108 ret
= gpio_request(ifx_dev
->gpio
.po
, "ifxModem");
1109 ret
+= gpio_direction_output(ifx_dev
->gpio
.po
, 0);
1110 ret
+= gpio_export(ifx_dev
->gpio
.po
, 1);
1112 dev_err(&spi
->dev
, "Unable to configure GPIO%d (ON)",
1118 ret
= gpio_request(ifx_dev
->gpio
.mrdy
, "ifxModem");
1120 dev_err(&spi
->dev
, "Unable to allocate GPIO%d (MRDY)",
1121 ifx_dev
->gpio
.mrdy
);
1124 ret
+= gpio_export(ifx_dev
->gpio
.mrdy
, 1);
1125 ret
+= gpio_direction_output(ifx_dev
->gpio
.mrdy
, 0);
1127 dev_err(&spi
->dev
, "Unable to configure GPIO%d (MRDY)",
1128 ifx_dev
->gpio
.mrdy
);
1133 ret
= gpio_request(ifx_dev
->gpio
.srdy
, "ifxModem");
1135 dev_err(&spi
->dev
, "Unable to allocate GPIO%d (SRDY)",
1136 ifx_dev
->gpio
.srdy
);
1140 ret
+= gpio_export(ifx_dev
->gpio
.srdy
, 1);
1141 ret
+= gpio_direction_input(ifx_dev
->gpio
.srdy
);
1143 dev_err(&spi
->dev
, "Unable to configure GPIO%d (SRDY)",
1144 ifx_dev
->gpio
.srdy
);
1149 ret
= gpio_request(ifx_dev
->gpio
.reset_out
, "ifxModem");
1151 dev_err(&spi
->dev
, "Unable to allocate GPIO%d (RESET_OUT)",
1152 ifx_dev
->gpio
.reset_out
);
1155 ret
+= gpio_export(ifx_dev
->gpio
.reset_out
, 1);
1156 ret
+= gpio_direction_input(ifx_dev
->gpio
.reset_out
);
1158 dev_err(&spi
->dev
, "Unable to configure GPIO%d (RESET_OUT)",
1159 ifx_dev
->gpio
.reset_out
);
1164 ret
= request_irq(gpio_to_irq(ifx_dev
->gpio
.reset_out
),
1165 ifx_spi_reset_interrupt
,
1166 IRQF_TRIGGER_RISING
|IRQF_TRIGGER_FALLING
, DRVNAME
,
1169 dev_err(&spi
->dev
, "Unable to get irq %x\n",
1170 gpio_to_irq(ifx_dev
->gpio
.reset_out
));
1174 ret
= ifx_spi_reset(ifx_dev
);
1176 ret
= request_irq(gpio_to_irq(ifx_dev
->gpio
.srdy
),
1177 ifx_spi_srdy_interrupt
, IRQF_TRIGGER_RISING
, DRVNAME
,
1180 dev_err(&spi
->dev
, "Unable to get irq %x",
1181 gpio_to_irq(ifx_dev
->gpio
.srdy
));
1185 /* set pm runtime power state and register with power system */
1186 pm_runtime_set_active(&spi
->dev
);
1187 pm_runtime_enable(&spi
->dev
);
1189 /* handle case that modem is already signaling SRDY */
1190 /* no outgoing tty open at this point, this just satisfies the
1191 * modem's read and should reset communication properly
1193 srdy
= gpio_get_value(ifx_dev
->gpio
.srdy
);
1196 mrdy_assert(ifx_dev
);
1197 ifx_spi_handle_srdy(ifx_dev
);
1199 mrdy_set_low(ifx_dev
);
1203 free_irq(gpio_to_irq(ifx_dev
->gpio
.reset_out
), ifx_dev
);
1205 gpio_free(ifx_dev
->gpio
.srdy
);
1207 gpio_free(ifx_dev
->gpio
.mrdy
);
1209 gpio_free(ifx_dev
->gpio
.reset
);
1211 gpio_free(ifx_dev
->gpio
.po
);
1213 gpio_free(ifx_dev
->gpio
.reset_out
);
1215 ifx_spi_free_device(ifx_dev
);
1216 saved_ifx_dev
= NULL
;
1221 * ifx_spi_spi_remove - SPI device was removed
1224 * FIXME: We should be shutting the device down here not in
1225 * the module unload path.
1228 static int ifx_spi_spi_remove(struct spi_device
*spi
)
1230 struct ifx_spi_device
*ifx_dev
= spi_get_drvdata(spi
);
1232 tasklet_kill(&ifx_dev
->io_work_tasklet
);
1234 free_irq(gpio_to_irq(ifx_dev
->gpio
.reset_out
), ifx_dev
);
1235 free_irq(gpio_to_irq(ifx_dev
->gpio
.srdy
), ifx_dev
);
1237 gpio_free(ifx_dev
->gpio
.srdy
);
1238 gpio_free(ifx_dev
->gpio
.mrdy
);
1239 gpio_free(ifx_dev
->gpio
.reset
);
1240 gpio_free(ifx_dev
->gpio
.po
);
1241 gpio_free(ifx_dev
->gpio
.reset_out
);
1243 /* free allocations */
1244 ifx_spi_free_device(ifx_dev
);
1246 saved_ifx_dev
= NULL
;
1251 * ifx_spi_spi_shutdown - called on SPI shutdown
1254 * No action needs to be taken here
1257 static void ifx_spi_spi_shutdown(struct spi_device
*spi
)
1259 struct ifx_spi_device
*ifx_dev
= spi_get_drvdata(spi
);
1261 ifx_modem_power_off(ifx_dev
);
1265 * various suspends and resumes have nothing to do
1266 * no hardware to save state for
1270 * ifx_spi_pm_suspend - suspend modem on system suspend
1271 * @dev: device being suspended
1273 * Suspend the modem. No action needed on Intel MID platforms, may
1274 * need extending for other systems.
1276 static int ifx_spi_pm_suspend(struct device
*dev
)
1282 * ifx_spi_pm_resume - resume modem on system resume
1283 * @dev: device being suspended
1285 * Allow the modem to resume. No action needed.
1287 * FIXME: do we need to reset anything here ?
1289 static int ifx_spi_pm_resume(struct device
*dev
)
1295 * ifx_spi_pm_runtime_resume - suspend modem
1296 * @dev: device being suspended
1298 * Allow the modem to resume. No action needed.
1300 static int ifx_spi_pm_runtime_resume(struct device
*dev
)
1306 * ifx_spi_pm_runtime_suspend - suspend modem
1307 * @dev: device being suspended
1309 * Allow the modem to suspend and thus suspend to continue up the
1312 static int ifx_spi_pm_runtime_suspend(struct device
*dev
)
1318 * ifx_spi_pm_runtime_idle - check if modem idle
1321 * Check conditions and queue runtime suspend if idle.
1323 static int ifx_spi_pm_runtime_idle(struct device
*dev
)
1325 struct spi_device
*spi
= to_spi_device(dev
);
1326 struct ifx_spi_device
*ifx_dev
= spi_get_drvdata(spi
);
1328 if (!ifx_dev
->power_status
)
1329 pm_runtime_suspend(dev
);
1334 static const struct dev_pm_ops ifx_spi_pm
= {
1335 .resume
= ifx_spi_pm_resume
,
1336 .suspend
= ifx_spi_pm_suspend
,
1337 .runtime_resume
= ifx_spi_pm_runtime_resume
,
1338 .runtime_suspend
= ifx_spi_pm_runtime_suspend
,
1339 .runtime_idle
= ifx_spi_pm_runtime_idle
1342 static const struct spi_device_id ifx_id_table
[] = {
1347 MODULE_DEVICE_TABLE(spi
, ifx_id_table
);
1349 /* spi operations */
1350 static struct spi_driver ifx_spi_driver
= {
1355 .probe
= ifx_spi_spi_probe
,
1356 .shutdown
= ifx_spi_spi_shutdown
,
1357 .remove
= ifx_spi_spi_remove
,
1358 .id_table
= ifx_id_table
1362 * ifx_spi_exit - module exit
1364 * Unload the module.
1367 static void __exit
ifx_spi_exit(void)
1370 spi_unregister_driver(&ifx_spi_driver
);
1371 tty_unregister_driver(tty_drv
);
1372 put_tty_driver(tty_drv
);
1373 unregister_reboot_notifier(&ifx_modem_reboot_notifier_block
);
1377 * ifx_spi_init - module entry point
1379 * Initialise the SPI and tty interfaces for the IFX SPI driver
1380 * We need to initialize upper-edge spi driver after the tty
1381 * driver because otherwise the spi probe will race
1384 static int __init
ifx_spi_init(void)
1388 tty_drv
= alloc_tty_driver(1);
1390 pr_err("%s: alloc_tty_driver failed", DRVNAME
);
1394 tty_drv
->driver_name
= DRVNAME
;
1395 tty_drv
->name
= TTYNAME
;
1396 tty_drv
->minor_start
= IFX_SPI_TTY_ID
;
1397 tty_drv
->type
= TTY_DRIVER_TYPE_SERIAL
;
1398 tty_drv
->subtype
= SERIAL_TYPE_NORMAL
;
1399 tty_drv
->flags
= TTY_DRIVER_REAL_RAW
| TTY_DRIVER_DYNAMIC_DEV
;
1400 tty_drv
->init_termios
= tty_std_termios
;
1402 tty_set_operations(tty_drv
, &ifx_spi_serial_ops
);
1404 result
= tty_register_driver(tty_drv
);
1406 pr_err("%s: tty_register_driver failed(%d)",
1411 result
= spi_register_driver(&ifx_spi_driver
);
1413 pr_err("%s: spi_register_driver failed(%d)",
1418 result
= register_reboot_notifier(&ifx_modem_reboot_notifier_block
);
1420 pr_err("%s: register ifx modem reboot notifier failed(%d)",
1427 spi_unregister_driver(&ifx_spi_driver
);
1429 tty_unregister_driver(tty_drv
);
1431 put_tty_driver(tty_drv
);
1436 module_init(ifx_spi_init
);
1437 module_exit(ifx_spi_exit
);
1439 MODULE_AUTHOR("Intel");
1440 MODULE_DESCRIPTION("IFX6x60 spi driver");
1441 MODULE_LICENSE("GPL");
1442 MODULE_INFO(Version
, "0.1-IFX6x60");