1 /****************************************************************************
3 * Driver for the IFX 6x60 spi modem.
5 * Copyright (C) 2008 Option International
6 * Copyright (C) 2008 Filip Aben <f.aben@option.com>
7 * Denis Joseph Barrow <d.barow@option.com>
8 * Jan Dumon <j.dumon@option.com>
10 * Copyright (C) 2009, 2010 Intel Corp
11 * Russ Gorby <russ.gorby@intel.com>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
27 * Driver modified by Intel from Option gtm501l_spi.c
30 * o The driver currently assumes a single device only. If you need to
31 * change this then look for saved_ifx_dev and add a device lookup
32 * o The driver is intended to be big-endian safe but has never been
33 * tested that way (no suitable hardware). There are a couple of FIXME
34 * notes by areas that may need addressing
35 * o Some of the GPIO naming/setup assumptions may need revisiting if
36 * you need to use this driver for another platform.
38 *****************************************************************************/
39 #include <linux/dma-mapping.h>
40 #include <linux/module.h>
41 #include <linux/termios.h>
42 #include <linux/tty.h>
43 #include <linux/device.h>
44 #include <linux/spi/spi.h>
45 #include <linux/kfifo.h>
46 #include <linux/tty_flip.h>
47 #include <linux/timer.h>
48 #include <linux/serial.h>
49 #include <linux/interrupt.h>
50 #include <linux/irq.h>
51 #include <linux/rfkill.h>
54 #include <linux/dmapool.h>
55 #include <linux/gpio.h>
56 #include <linux/sched.h>
57 #include <linux/time.h>
58 #include <linux/wait.h>
60 #include <linux/pm_runtime.h>
61 #include <linux/spi/ifx_modem.h>
62 #include <linux/delay.h>
63 #include <linux/reboot.h>
67 #define IFX_SPI_MORE_MASK 0x10
68 #define IFX_SPI_MORE_BIT 4 /* bit position in u8 */
69 #define IFX_SPI_CTS_BIT 6 /* bit position in u8 */
70 #define IFX_SPI_MODE SPI_MODE_1
71 #define IFX_SPI_TTY_ID 0
72 #define IFX_SPI_TIMEOUT_SEC 2
73 #define IFX_SPI_HEADER_0 (-1)
74 #define IFX_SPI_HEADER_F (-2)
76 #define PO_POST_DELAY 200
77 #define IFX_MDM_RST_PMU 4
79 /* forward reference */
80 static void ifx_spi_handle_srdy(struct ifx_spi_device
*ifx_dev
);
81 static int ifx_modem_reboot_callback(struct notifier_block
*nfb
,
82 unsigned long event
, void *data
);
83 static int ifx_modem_power_off(struct ifx_spi_device
*ifx_dev
);
86 static int spi_bpw
= 16; /* 8, 16 or 32 bit word length */
87 static struct tty_driver
*tty_drv
;
88 static struct ifx_spi_device
*saved_ifx_dev
;
89 static struct lock_class_key ifx_spi_key
;
91 static struct notifier_block ifx_modem_reboot_notifier_block
= {
92 .notifier_call
= ifx_modem_reboot_callback
,
95 static int ifx_modem_power_off(struct ifx_spi_device
*ifx_dev
)
97 gpio_set_value(IFX_MDM_RST_PMU
, 1);
98 msleep(PO_POST_DELAY
);
103 static int ifx_modem_reboot_callback(struct notifier_block
*nfb
,
104 unsigned long event
, void *data
)
107 ifx_modem_power_off(saved_ifx_dev
);
109 pr_warn("no ifx modem active;\n");
114 /* GPIO/GPE settings */
117 * mrdy_set_high - set MRDY GPIO
118 * @ifx: device we are controlling
121 static inline void mrdy_set_high(struct ifx_spi_device
*ifx
)
123 gpio_set_value(ifx
->gpio
.mrdy
, 1);
127 * mrdy_set_low - clear MRDY GPIO
128 * @ifx: device we are controlling
131 static inline void mrdy_set_low(struct ifx_spi_device
*ifx
)
133 gpio_set_value(ifx
->gpio
.mrdy
, 0);
137 * ifx_spi_power_state_set
138 * @ifx_dev: our SPI device
141 * Set bit in power status and signal power system if status becomes non-0
144 ifx_spi_power_state_set(struct ifx_spi_device
*ifx_dev
, unsigned char val
)
148 spin_lock_irqsave(&ifx_dev
->power_lock
, flags
);
151 * if power status is already non-0, just update, else
154 if (!ifx_dev
->power_status
)
155 pm_runtime_get(&ifx_dev
->spi_dev
->dev
);
156 ifx_dev
->power_status
|= val
;
158 spin_unlock_irqrestore(&ifx_dev
->power_lock
, flags
);
162 * ifx_spi_power_state_clear - clear power bit
163 * @ifx_dev: our SPI device
164 * @val: bits to clear
166 * clear bit in power status and signal power system if status becomes 0
169 ifx_spi_power_state_clear(struct ifx_spi_device
*ifx_dev
, unsigned char val
)
173 spin_lock_irqsave(&ifx_dev
->power_lock
, flags
);
175 if (ifx_dev
->power_status
) {
176 ifx_dev
->power_status
&= ~val
;
177 if (!ifx_dev
->power_status
)
178 pm_runtime_put(&ifx_dev
->spi_dev
->dev
);
181 spin_unlock_irqrestore(&ifx_dev
->power_lock
, flags
);
187 * @len : number of bytes (not words) in the buffer
188 * @end: end of buffer
190 * Swap the contents of a buffer into big endian format
192 static inline void swap_buf_8(unsigned char *buf
, int len
, void *end
)
194 /* don't swap buffer if SPI word width is 8 bits */
201 * @len : number of bytes (not words) in the buffer
202 * @end: end of buffer
204 * Swap the contents of a buffer into big endian format
206 static inline void swap_buf_16(unsigned char *buf
, int len
, void *end
)
210 u16
*buf_16
= (u16
*)buf
;
211 len
= ((len
+ 1) >> 1);
212 if ((void *)&buf_16
[len
] > end
) {
213 pr_err("swap_buf_16: swap exceeds boundary (%p > %p)!",
217 for (n
= 0; n
< len
; n
++) {
218 *buf_16
= cpu_to_be16(*buf_16
);
226 * @len : number of bytes (not words) in the buffer
227 * @end: end of buffer
229 * Swap the contents of a buffer into big endian format
231 static inline void swap_buf_32(unsigned char *buf
, int len
, void *end
)
235 u32
*buf_32
= (u32
*)buf
;
236 len
= (len
+ 3) >> 2;
238 if ((void *)&buf_32
[len
] > end
) {
239 pr_err("swap_buf_32: swap exceeds boundary (%p > %p)!\n",
243 for (n
= 0; n
< len
; n
++) {
244 *buf_32
= cpu_to_be32(*buf_32
);
250 * mrdy_assert - assert MRDY line
251 * @ifx_dev: our SPI device
253 * Assert mrdy and set timer to wait for SRDY interrupt, if SRDY is low
256 * FIXME: Can SRDY even go high as we are running this code ?
258 static void mrdy_assert(struct ifx_spi_device
*ifx_dev
)
260 int val
= gpio_get_value(ifx_dev
->gpio
.srdy
);
262 if (!test_and_set_bit(IFX_SPI_STATE_TIMER_PENDING
,
264 mod_timer(&ifx_dev
->spi_timer
,jiffies
+ IFX_SPI_TIMEOUT_SEC
*HZ
);
268 ifx_spi_power_state_set(ifx_dev
, IFX_SPI_POWER_DATA_PENDING
);
269 mrdy_set_high(ifx_dev
);
273 * ifx_spi_timeout - SPI timeout
274 * @arg: our SPI device
276 * The SPI has timed out: hang up the tty. Users will then see a hangup
279 static void ifx_spi_timeout(unsigned long arg
)
281 struct ifx_spi_device
*ifx_dev
= (struct ifx_spi_device
*)arg
;
283 dev_warn(&ifx_dev
->spi_dev
->dev
, "*** SPI Timeout ***");
284 tty_port_tty_hangup(&ifx_dev
->tty_port
, false);
285 mrdy_set_low(ifx_dev
);
286 clear_bit(IFX_SPI_STATE_TIMER_PENDING
, &ifx_dev
->flags
);
289 /* char/tty operations */
292 * ifx_spi_tiocmget - get modem lines
293 * @tty: our tty device
294 * @filp: file handle issuing the request
296 * Map the signal state into Linux modem flags and report the value
299 static int ifx_spi_tiocmget(struct tty_struct
*tty
)
302 struct ifx_spi_device
*ifx_dev
= tty
->driver_data
;
305 (test_bit(IFX_SPI_RTS
, &ifx_dev
->signal_state
) ? TIOCM_RTS
: 0) |
306 (test_bit(IFX_SPI_DTR
, &ifx_dev
->signal_state
) ? TIOCM_DTR
: 0) |
307 (test_bit(IFX_SPI_CTS
, &ifx_dev
->signal_state
) ? TIOCM_CTS
: 0) |
308 (test_bit(IFX_SPI_DSR
, &ifx_dev
->signal_state
) ? TIOCM_DSR
: 0) |
309 (test_bit(IFX_SPI_DCD
, &ifx_dev
->signal_state
) ? TIOCM_CAR
: 0) |
310 (test_bit(IFX_SPI_RI
, &ifx_dev
->signal_state
) ? TIOCM_RNG
: 0);
315 * ifx_spi_tiocmset - set modem bits
316 * @tty: the tty structure
318 * @clear: bits to clear
320 * The IFX6x60 only supports DTR and RTS. Set them accordingly
321 * and flag that an update to the modem is needed.
323 * FIXME: do we need to kick the tranfers when we do this ?
325 static int ifx_spi_tiocmset(struct tty_struct
*tty
,
326 unsigned int set
, unsigned int clear
)
328 struct ifx_spi_device
*ifx_dev
= tty
->driver_data
;
331 set_bit(IFX_SPI_RTS
, &ifx_dev
->signal_state
);
333 set_bit(IFX_SPI_DTR
, &ifx_dev
->signal_state
);
334 if (clear
& TIOCM_RTS
)
335 clear_bit(IFX_SPI_RTS
, &ifx_dev
->signal_state
);
336 if (clear
& TIOCM_DTR
)
337 clear_bit(IFX_SPI_DTR
, &ifx_dev
->signal_state
);
339 set_bit(IFX_SPI_UPDATE
, &ifx_dev
->signal_state
);
344 * ifx_spi_open - called on tty open
345 * @tty: our tty device
346 * @filp: file handle being associated with the tty
348 * Open the tty interface. We let the tty_port layer do all the work
351 * FIXME: Remove single device assumption and saved_ifx_dev
353 static int ifx_spi_open(struct tty_struct
*tty
, struct file
*filp
)
355 return tty_port_open(&saved_ifx_dev
->tty_port
, tty
, filp
);
359 * ifx_spi_close - called when our tty closes
360 * @tty: the tty being closed
361 * @filp: the file handle being closed
363 * Perform the close of the tty. We use the tty_port layer to do all
366 static void ifx_spi_close(struct tty_struct
*tty
, struct file
*filp
)
368 struct ifx_spi_device
*ifx_dev
= tty
->driver_data
;
369 tty_port_close(&ifx_dev
->tty_port
, tty
, filp
);
370 /* FIXME: should we do an ifx_spi_reset here ? */
374 * ifx_decode_spi_header - decode received header
375 * @buffer: the received data
376 * @length: decoded length
377 * @more: decoded more flag
378 * @received_cts: status of cts we received
380 * Note how received_cts is handled -- if header is all F it is left
381 * the same as it was, if header is all 0 it is set to 0 otherwise it is
382 * taken from the incoming header.
386 static int ifx_spi_decode_spi_header(unsigned char *buffer
, int *length
,
387 unsigned char *more
, unsigned char *received_cts
)
391 u16
*in_buffer
= (u16
*)buffer
;
396 if (h1
== 0 && h2
== 0) {
399 return IFX_SPI_HEADER_0
;
400 } else if (h1
== 0xffff && h2
== 0xffff) {
402 /* spi_slave_cts remains as it was */
403 return IFX_SPI_HEADER_F
;
406 *length
= h1
& 0xfff; /* upper bits of byte are flags */
407 *more
= (buffer
[1] >> IFX_SPI_MORE_BIT
) & 1;
408 *received_cts
= (buffer
[3] >> IFX_SPI_CTS_BIT
) & 1;
413 * ifx_setup_spi_header - set header fields
414 * @txbuffer: pointer to start of SPI buffer
416 * @more: indicate if more to follow
418 * Format up an SPI header for a transfer
422 static void ifx_spi_setup_spi_header(unsigned char *txbuffer
, int tx_count
,
425 *(u16
*)(txbuffer
) = tx_count
;
426 *(u16
*)(txbuffer
+2) = IFX_SPI_PAYLOAD_SIZE
;
427 txbuffer
[1] |= (more
<< IFX_SPI_MORE_BIT
) & IFX_SPI_MORE_MASK
;
431 * ifx_spi_prepare_tx_buffer - prepare transmit frame
432 * @ifx_dev: our SPI device
434 * The transmit buffr needs a header and various other bits of
435 * information followed by as much data as we can pull from the FIFO
436 * and transfer. This function formats up a suitable buffer in the
439 * FIXME: performance - should we wake the tty when the queue is half
442 static int ifx_spi_prepare_tx_buffer(struct ifx_spi_device
*ifx_dev
)
447 unsigned char *tx_buffer
;
449 tx_buffer
= ifx_dev
->tx_buffer
;
451 /* make room for required SPI header */
452 tx_buffer
+= IFX_SPI_HEADER_OVERHEAD
;
453 tx_count
= IFX_SPI_HEADER_OVERHEAD
;
455 /* clear to signal no more data if this turns out to be the
456 * last buffer sent in a sequence */
457 ifx_dev
->spi_more
= 0;
459 /* if modem cts is set, just send empty buffer */
460 if (!ifx_dev
->spi_slave_cts
) {
461 /* see if there's tx data */
462 queue_length
= kfifo_len(&ifx_dev
->tx_fifo
);
463 if (queue_length
!= 0) {
464 /* data to mux -- see if there's room for it */
465 temp_count
= min(queue_length
, IFX_SPI_PAYLOAD_SIZE
);
466 temp_count
= kfifo_out_locked(&ifx_dev
->tx_fifo
,
467 tx_buffer
, temp_count
,
468 &ifx_dev
->fifo_lock
);
470 /* update buffer pointer and data count in message */
471 tx_buffer
+= temp_count
;
472 tx_count
+= temp_count
;
473 if (temp_count
== queue_length
)
474 /* poke port to get more data */
475 tty_port_tty_wakeup(&ifx_dev
->tty_port
);
476 else /* more data in port, use next SPI message */
477 ifx_dev
->spi_more
= 1;
480 /* have data and info for header -- set up SPI header in buffer */
481 /* spi header needs payload size, not entire buffer size */
482 ifx_spi_setup_spi_header(ifx_dev
->tx_buffer
,
483 tx_count
-IFX_SPI_HEADER_OVERHEAD
,
485 /* swap actual data in the buffer */
486 ifx_dev
->swap_buf((ifx_dev
->tx_buffer
), tx_count
,
487 &ifx_dev
->tx_buffer
[IFX_SPI_TRANSFER_SIZE
]);
492 * ifx_spi_write - line discipline write
493 * @tty: our tty device
494 * @buf: pointer to buffer to write (kernel space)
495 * @count: size of buffer
497 * Write the characters we have been given into the FIFO. If the device
498 * is not active then activate it, when the SRDY line is asserted back
499 * this will commence I/O
501 static int ifx_spi_write(struct tty_struct
*tty
, const unsigned char *buf
,
504 struct ifx_spi_device
*ifx_dev
= tty
->driver_data
;
505 unsigned char *tmp_buf
= (unsigned char *)buf
;
510 spin_lock_irqsave(&ifx_dev
->fifo_lock
, flags
);
511 is_fifo_empty
= kfifo_is_empty(&ifx_dev
->tx_fifo
);
512 tx_count
= kfifo_in(&ifx_dev
->tx_fifo
, tmp_buf
, count
);
513 spin_unlock_irqrestore(&ifx_dev
->fifo_lock
, flags
);
515 mrdy_assert(ifx_dev
);
521 * ifx_spi_chars_in_buffer - line discipline helper
522 * @tty: our tty device
524 * Report how much data we can accept before we drop bytes. As we use
525 * a simple FIFO this is nice and easy.
527 static int ifx_spi_write_room(struct tty_struct
*tty
)
529 struct ifx_spi_device
*ifx_dev
= tty
->driver_data
;
530 return IFX_SPI_FIFO_SIZE
- kfifo_len(&ifx_dev
->tx_fifo
);
534 * ifx_spi_chars_in_buffer - line discipline helper
535 * @tty: our tty device
537 * Report how many characters we have buffered. In our case this is the
538 * number of bytes sitting in our transmit FIFO.
540 static int ifx_spi_chars_in_buffer(struct tty_struct
*tty
)
542 struct ifx_spi_device
*ifx_dev
= tty
->driver_data
;
543 return kfifo_len(&ifx_dev
->tx_fifo
);
548 * @port: our tty port
550 * tty port hang up. Called when tty_hangup processing is invoked either
551 * by loss of carrier, or by software (eg vhangup). Serialized against
552 * activate/shutdown by the tty layer.
554 static void ifx_spi_hangup(struct tty_struct
*tty
)
556 struct ifx_spi_device
*ifx_dev
= tty
->driver_data
;
557 tty_port_hangup(&ifx_dev
->tty_port
);
562 * @port: our tty port
564 * tty port activate method - called for first open. Serialized
565 * with hangup and shutdown by the tty layer.
567 static int ifx_port_activate(struct tty_port
*port
, struct tty_struct
*tty
)
569 struct ifx_spi_device
*ifx_dev
=
570 container_of(port
, struct ifx_spi_device
, tty_port
);
572 /* clear any old data; can't do this in 'close' */
573 kfifo_reset(&ifx_dev
->tx_fifo
);
575 /* clear any flag which may be set in port shutdown procedure */
576 clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS
, &ifx_dev
->flags
);
577 clear_bit(IFX_SPI_STATE_IO_READY
, &ifx_dev
->flags
);
579 /* put port data into this tty */
580 tty
->driver_data
= ifx_dev
;
582 /* allows flip string push from int context */
583 port
->low_latency
= 1;
585 /* set flag to allows data transfer */
586 set_bit(IFX_SPI_STATE_IO_AVAILABLE
, &ifx_dev
->flags
);
593 * @port: our tty port
595 * tty port shutdown method - called for last port close. Serialized
596 * with hangup and activate by the tty layer.
598 static void ifx_port_shutdown(struct tty_port
*port
)
600 struct ifx_spi_device
*ifx_dev
=
601 container_of(port
, struct ifx_spi_device
, tty_port
);
603 clear_bit(IFX_SPI_STATE_IO_AVAILABLE
, &ifx_dev
->flags
);
604 mrdy_set_low(ifx_dev
);
605 del_timer(&ifx_dev
->spi_timer
);
606 clear_bit(IFX_SPI_STATE_TIMER_PENDING
, &ifx_dev
->flags
);
607 tasklet_kill(&ifx_dev
->io_work_tasklet
);
610 static const struct tty_port_operations ifx_tty_port_ops
= {
611 .activate
= ifx_port_activate
,
612 .shutdown
= ifx_port_shutdown
,
615 static const struct tty_operations ifx_spi_serial_ops
= {
616 .open
= ifx_spi_open
,
617 .close
= ifx_spi_close
,
618 .write
= ifx_spi_write
,
619 .hangup
= ifx_spi_hangup
,
620 .write_room
= ifx_spi_write_room
,
621 .chars_in_buffer
= ifx_spi_chars_in_buffer
,
622 .tiocmget
= ifx_spi_tiocmget
,
623 .tiocmset
= ifx_spi_tiocmset
,
627 * ifx_spi_insert_fip_string - queue received data
628 * @ifx_ser: our SPI device
629 * @chars: buffer we have received
630 * @size: number of chars reeived
632 * Queue bytes to the tty assuming the tty side is currently open. If
633 * not the discard the data.
635 static void ifx_spi_insert_flip_string(struct ifx_spi_device
*ifx_dev
,
636 unsigned char *chars
, size_t size
)
638 tty_insert_flip_string(&ifx_dev
->tty_port
, chars
, size
);
639 tty_flip_buffer_push(&ifx_dev
->tty_port
);
643 * ifx_spi_complete - SPI transfer completed
644 * @ctx: our SPI device
646 * An SPI transfer has completed. Process any received data and kick off
647 * any further transmits we can commence.
649 static void ifx_spi_complete(void *ctx
)
651 struct ifx_spi_device
*ifx_dev
= ctx
;
654 unsigned char more
= 0;
656 int local_write_pending
= 0;
661 mrdy_set_low(ifx_dev
);
663 if (!ifx_dev
->spi_msg
.status
) {
664 /* check header validity, get comm flags */
665 ifx_dev
->swap_buf(ifx_dev
->rx_buffer
, IFX_SPI_HEADER_OVERHEAD
,
666 &ifx_dev
->rx_buffer
[IFX_SPI_HEADER_OVERHEAD
]);
667 decode_result
= ifx_spi_decode_spi_header(ifx_dev
->rx_buffer
,
668 &length
, &more
, &cts
);
669 if (decode_result
== IFX_SPI_HEADER_0
) {
670 dev_dbg(&ifx_dev
->spi_dev
->dev
,
671 "ignore input: invalid header 0");
672 ifx_dev
->spi_slave_cts
= 0;
674 } else if (decode_result
== IFX_SPI_HEADER_F
) {
675 dev_dbg(&ifx_dev
->spi_dev
->dev
,
676 "ignore input: invalid header F");
680 ifx_dev
->spi_slave_cts
= cts
;
682 actual_length
= min((unsigned int)length
,
683 ifx_dev
->spi_msg
.actual_length
);
685 (ifx_dev
->rx_buffer
+ IFX_SPI_HEADER_OVERHEAD
),
687 &ifx_dev
->rx_buffer
[IFX_SPI_TRANSFER_SIZE
]);
688 ifx_spi_insert_flip_string(
690 ifx_dev
->rx_buffer
+ IFX_SPI_HEADER_OVERHEAD
,
691 (size_t)actual_length
);
694 dev_dbg(&ifx_dev
->spi_dev
->dev
, "SPI transfer error %d",
695 ifx_dev
->spi_msg
.status
);
699 if (ifx_dev
->write_pending
) {
700 ifx_dev
->write_pending
= 0;
701 local_write_pending
= 1;
704 clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS
, &(ifx_dev
->flags
));
706 queue_length
= kfifo_len(&ifx_dev
->tx_fifo
);
707 srdy
= gpio_get_value(ifx_dev
->gpio
.srdy
);
709 ifx_spi_power_state_clear(ifx_dev
, IFX_SPI_POWER_SRDY
);
711 /* schedule output if there is more to do */
712 if (test_and_clear_bit(IFX_SPI_STATE_IO_READY
, &ifx_dev
->flags
))
713 tasklet_schedule(&ifx_dev
->io_work_tasklet
);
715 if (more
|| ifx_dev
->spi_more
|| queue_length
> 0 ||
716 local_write_pending
) {
717 if (ifx_dev
->spi_slave_cts
) {
719 mrdy_assert(ifx_dev
);
721 mrdy_assert(ifx_dev
);
724 * poke line discipline driver if any for more data
725 * may or may not get more data to write
726 * for now, say not busy
728 ifx_spi_power_state_clear(ifx_dev
,
729 IFX_SPI_POWER_DATA_PENDING
);
730 tty_port_tty_wakeup(&ifx_dev
->tty_port
);
736 * ifx_spio_io - I/O tasklet
737 * @data: our SPI device
739 * Queue data for transmission if possible and then kick off the
742 static void ifx_spi_io(unsigned long data
)
745 struct ifx_spi_device
*ifx_dev
= (struct ifx_spi_device
*) data
;
747 if (!test_and_set_bit(IFX_SPI_STATE_IO_IN_PROGRESS
, &ifx_dev
->flags
) &&
748 test_bit(IFX_SPI_STATE_IO_AVAILABLE
, &ifx_dev
->flags
)) {
749 if (ifx_dev
->gpio
.unack_srdy_int_nb
> 0)
750 ifx_dev
->gpio
.unack_srdy_int_nb
--;
752 ifx_spi_prepare_tx_buffer(ifx_dev
);
754 spi_message_init(&ifx_dev
->spi_msg
);
755 INIT_LIST_HEAD(&ifx_dev
->spi_msg
.queue
);
757 ifx_dev
->spi_msg
.context
= ifx_dev
;
758 ifx_dev
->spi_msg
.complete
= ifx_spi_complete
;
760 /* set up our spi transfer */
761 /* note len is BYTES, not transfers */
762 ifx_dev
->spi_xfer
.len
= IFX_SPI_TRANSFER_SIZE
;
763 ifx_dev
->spi_xfer
.cs_change
= 0;
764 ifx_dev
->spi_xfer
.speed_hz
= ifx_dev
->spi_dev
->max_speed_hz
;
765 /* ifx_dev->spi_xfer.speed_hz = 390625; */
766 ifx_dev
->spi_xfer
.bits_per_word
=
767 ifx_dev
->spi_dev
->bits_per_word
;
769 ifx_dev
->spi_xfer
.tx_buf
= ifx_dev
->tx_buffer
;
770 ifx_dev
->spi_xfer
.rx_buf
= ifx_dev
->rx_buffer
;
775 if (ifx_dev
->use_dma
) {
776 ifx_dev
->spi_msg
.is_dma_mapped
= 1;
777 ifx_dev
->tx_dma
= ifx_dev
->tx_bus
;
778 ifx_dev
->rx_dma
= ifx_dev
->rx_bus
;
779 ifx_dev
->spi_xfer
.tx_dma
= ifx_dev
->tx_dma
;
780 ifx_dev
->spi_xfer
.rx_dma
= ifx_dev
->rx_dma
;
782 ifx_dev
->spi_msg
.is_dma_mapped
= 0;
783 ifx_dev
->tx_dma
= (dma_addr_t
)0;
784 ifx_dev
->rx_dma
= (dma_addr_t
)0;
785 ifx_dev
->spi_xfer
.tx_dma
= (dma_addr_t
)0;
786 ifx_dev
->spi_xfer
.rx_dma
= (dma_addr_t
)0;
789 spi_message_add_tail(&ifx_dev
->spi_xfer
, &ifx_dev
->spi_msg
);
791 /* Assert MRDY. This may have already been done by the write
794 mrdy_assert(ifx_dev
);
796 retval
= spi_async(ifx_dev
->spi_dev
, &ifx_dev
->spi_msg
);
798 clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS
,
800 tasklet_schedule(&ifx_dev
->io_work_tasklet
);
804 ifx_dev
->write_pending
= 1;
808 * ifx_spi_free_port - free up the tty side
809 * @ifx_dev: IFX device going away
811 * Unregister and free up a port when the device goes away
813 static void ifx_spi_free_port(struct ifx_spi_device
*ifx_dev
)
815 if (ifx_dev
->tty_dev
)
816 tty_unregister_device(tty_drv
, ifx_dev
->minor
);
817 tty_port_destroy(&ifx_dev
->tty_port
);
818 kfifo_free(&ifx_dev
->tx_fifo
);
822 * ifx_spi_create_port - create a new port
823 * @ifx_dev: our spi device
825 * Allocate and initialise the tty port that goes with this interface
826 * and add it to the tty layer so that it can be opened.
828 static int ifx_spi_create_port(struct ifx_spi_device
*ifx_dev
)
831 struct tty_port
*pport
= &ifx_dev
->tty_port
;
833 spin_lock_init(&ifx_dev
->fifo_lock
);
834 lockdep_set_class_and_subclass(&ifx_dev
->fifo_lock
,
837 if (kfifo_alloc(&ifx_dev
->tx_fifo
, IFX_SPI_FIFO_SIZE
, GFP_KERNEL
)) {
842 tty_port_init(pport
);
843 pport
->ops
= &ifx_tty_port_ops
;
844 ifx_dev
->minor
= IFX_SPI_TTY_ID
;
845 ifx_dev
->tty_dev
= tty_port_register_device(pport
, tty_drv
,
846 ifx_dev
->minor
, &ifx_dev
->spi_dev
->dev
);
847 if (IS_ERR(ifx_dev
->tty_dev
)) {
848 dev_dbg(&ifx_dev
->spi_dev
->dev
,
849 "%s: registering tty device failed", __func__
);
850 ret
= PTR_ERR(ifx_dev
->tty_dev
);
856 tty_port_destroy(pport
);
858 ifx_spi_free_port(ifx_dev
);
863 * ifx_spi_handle_srdy - handle SRDY
864 * @ifx_dev: device asserting SRDY
866 * Check our device state and see what we need to kick off when SRDY
867 * is asserted. This usually means killing the timer and firing off the
870 static void ifx_spi_handle_srdy(struct ifx_spi_device
*ifx_dev
)
872 if (test_bit(IFX_SPI_STATE_TIMER_PENDING
, &ifx_dev
->flags
)) {
873 del_timer(&ifx_dev
->spi_timer
);
874 clear_bit(IFX_SPI_STATE_TIMER_PENDING
, &ifx_dev
->flags
);
877 ifx_spi_power_state_set(ifx_dev
, IFX_SPI_POWER_SRDY
);
879 if (!test_bit(IFX_SPI_STATE_IO_IN_PROGRESS
, &ifx_dev
->flags
))
880 tasklet_schedule(&ifx_dev
->io_work_tasklet
);
882 set_bit(IFX_SPI_STATE_IO_READY
, &ifx_dev
->flags
);
886 * ifx_spi_srdy_interrupt - SRDY asserted
887 * @irq: our IRQ number
888 * @dev: our ifx device
890 * The modem asserted SRDY. Handle the srdy event
892 static irqreturn_t
ifx_spi_srdy_interrupt(int irq
, void *dev
)
894 struct ifx_spi_device
*ifx_dev
= dev
;
895 ifx_dev
->gpio
.unack_srdy_int_nb
++;
896 ifx_spi_handle_srdy(ifx_dev
);
901 * ifx_spi_reset_interrupt - Modem has changed reset state
902 * @irq: interrupt number
903 * @dev: our device pointer
905 * The modem has either entered or left reset state. Check the GPIO
908 * FIXME: review locking on MR_INPROGRESS versus
909 * parallel unsolicited reset/solicited reset
911 static irqreturn_t
ifx_spi_reset_interrupt(int irq
, void *dev
)
913 struct ifx_spi_device
*ifx_dev
= dev
;
914 int val
= gpio_get_value(ifx_dev
->gpio
.reset_out
);
915 int solreset
= test_bit(MR_START
, &ifx_dev
->mdm_reset_state
);
919 set_bit(MR_INPROGRESS
, &ifx_dev
->mdm_reset_state
);
921 /* unsolicited reset */
922 tty_port_tty_hangup(&ifx_dev
->tty_port
, false);
926 clear_bit(MR_INPROGRESS
, &ifx_dev
->mdm_reset_state
);
928 set_bit(MR_COMPLETE
, &ifx_dev
->mdm_reset_state
);
929 wake_up(&ifx_dev
->mdm_reset_wait
);
936 * ifx_spi_free_device - free device
937 * @ifx_dev: device to free
939 * Free the IFX device
941 static void ifx_spi_free_device(struct ifx_spi_device
*ifx_dev
)
943 ifx_spi_free_port(ifx_dev
);
944 dma_free_coherent(&ifx_dev
->spi_dev
->dev
,
945 IFX_SPI_TRANSFER_SIZE
,
948 dma_free_coherent(&ifx_dev
->spi_dev
->dev
,
949 IFX_SPI_TRANSFER_SIZE
,
955 * ifx_spi_reset - reset modem
956 * @ifx_dev: modem to reset
958 * Perform a reset on the modem
960 static int ifx_spi_reset(struct ifx_spi_device
*ifx_dev
)
964 * set up modem power, reset
966 * delays are required on some platforms for the modem
969 set_bit(MR_START
, &ifx_dev
->mdm_reset_state
);
970 gpio_set_value(ifx_dev
->gpio
.po
, 0);
971 gpio_set_value(ifx_dev
->gpio
.reset
, 0);
973 gpio_set_value(ifx_dev
->gpio
.reset
, 1);
975 gpio_set_value(ifx_dev
->gpio
.po
, 1);
977 gpio_set_value(ifx_dev
->gpio
.po
, 0);
978 ret
= wait_event_timeout(ifx_dev
->mdm_reset_wait
,
979 test_bit(MR_COMPLETE
,
980 &ifx_dev
->mdm_reset_state
),
983 dev_warn(&ifx_dev
->spi_dev
->dev
, "Modem reset timeout: (state:%lx)",
984 ifx_dev
->mdm_reset_state
);
986 ifx_dev
->mdm_reset_state
= 0;
991 * ifx_spi_spi_probe - probe callback
992 * @spi: our possible matching SPI device
994 * Probe for a 6x60 modem on SPI bus. Perform any needed device and
998 * - Support for multiple devices
999 * - Split out MID specific GPIO handling eventually
1002 static int ifx_spi_spi_probe(struct spi_device
*spi
)
1006 struct ifx_modem_platform_data
*pl_data
;
1007 struct ifx_spi_device
*ifx_dev
;
1009 if (saved_ifx_dev
) {
1010 dev_dbg(&spi
->dev
, "ignoring subsequent detection");
1014 pl_data
= dev_get_platdata(&spi
->dev
);
1016 dev_err(&spi
->dev
, "missing platform data!");
1020 /* initialize structure to hold our device variables */
1021 ifx_dev
= kzalloc(sizeof(struct ifx_spi_device
), GFP_KERNEL
);
1023 dev_err(&spi
->dev
, "spi device allocation failed");
1026 saved_ifx_dev
= ifx_dev
;
1027 ifx_dev
->spi_dev
= spi
;
1028 clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS
, &ifx_dev
->flags
);
1029 spin_lock_init(&ifx_dev
->write_lock
);
1030 spin_lock_init(&ifx_dev
->power_lock
);
1031 ifx_dev
->power_status
= 0;
1032 init_timer(&ifx_dev
->spi_timer
);
1033 ifx_dev
->spi_timer
.function
= ifx_spi_timeout
;
1034 ifx_dev
->spi_timer
.data
= (unsigned long)ifx_dev
;
1035 ifx_dev
->modem
= pl_data
->modem_type
;
1036 ifx_dev
->use_dma
= pl_data
->use_dma
;
1037 ifx_dev
->max_hz
= pl_data
->max_hz
;
1038 /* initialize spi mode, etc */
1039 spi
->max_speed_hz
= ifx_dev
->max_hz
;
1040 spi
->mode
= IFX_SPI_MODE
| (SPI_LOOP
& spi
->mode
);
1041 spi
->bits_per_word
= spi_bpw
;
1042 ret
= spi_setup(spi
);
1044 dev_err(&spi
->dev
, "SPI setup wasn't successful %d", ret
);
1049 /* init swap_buf function according to word width configuration */
1050 if (spi
->bits_per_word
== 32)
1051 ifx_dev
->swap_buf
= swap_buf_32
;
1052 else if (spi
->bits_per_word
== 16)
1053 ifx_dev
->swap_buf
= swap_buf_16
;
1055 ifx_dev
->swap_buf
= swap_buf_8
;
1057 /* ensure SPI protocol flags are initialized to enable transfer */
1058 ifx_dev
->spi_more
= 0;
1059 ifx_dev
->spi_slave_cts
= 0;
1061 /*initialize transfer and dma buffers */
1062 ifx_dev
->tx_buffer
= dma_alloc_coherent(ifx_dev
->spi_dev
->dev
.parent
,
1063 IFX_SPI_TRANSFER_SIZE
,
1066 if (!ifx_dev
->tx_buffer
) {
1067 dev_err(&spi
->dev
, "DMA-TX buffer allocation failed");
1071 ifx_dev
->rx_buffer
= dma_alloc_coherent(ifx_dev
->spi_dev
->dev
.parent
,
1072 IFX_SPI_TRANSFER_SIZE
,
1075 if (!ifx_dev
->rx_buffer
) {
1076 dev_err(&spi
->dev
, "DMA-RX buffer allocation failed");
1081 /* initialize waitq for modem reset */
1082 init_waitqueue_head(&ifx_dev
->mdm_reset_wait
);
1084 spi_set_drvdata(spi
, ifx_dev
);
1085 tasklet_init(&ifx_dev
->io_work_tasklet
, ifx_spi_io
,
1086 (unsigned long)ifx_dev
);
1088 set_bit(IFX_SPI_STATE_PRESENT
, &ifx_dev
->flags
);
1090 /* create our tty port */
1091 ret
= ifx_spi_create_port(ifx_dev
);
1093 dev_err(&spi
->dev
, "create default tty port failed");
1097 ifx_dev
->gpio
.reset
= pl_data
->rst_pmu
;
1098 ifx_dev
->gpio
.po
= pl_data
->pwr_on
;
1099 ifx_dev
->gpio
.mrdy
= pl_data
->mrdy
;
1100 ifx_dev
->gpio
.srdy
= pl_data
->srdy
;
1101 ifx_dev
->gpio
.reset_out
= pl_data
->rst_out
;
1103 dev_info(&spi
->dev
, "gpios %d, %d, %d, %d, %d",
1104 ifx_dev
->gpio
.reset
, ifx_dev
->gpio
.po
, ifx_dev
->gpio
.mrdy
,
1105 ifx_dev
->gpio
.srdy
, ifx_dev
->gpio
.reset_out
);
1107 /* Configure gpios */
1108 ret
= gpio_request(ifx_dev
->gpio
.reset
, "ifxModem");
1110 dev_err(&spi
->dev
, "Unable to allocate GPIO%d (RESET)",
1111 ifx_dev
->gpio
.reset
);
1114 ret
+= gpio_direction_output(ifx_dev
->gpio
.reset
, 0);
1115 ret
+= gpio_export(ifx_dev
->gpio
.reset
, 1);
1117 dev_err(&spi
->dev
, "Unable to configure GPIO%d (RESET)",
1118 ifx_dev
->gpio
.reset
);
1123 ret
= gpio_request(ifx_dev
->gpio
.po
, "ifxModem");
1124 ret
+= gpio_direction_output(ifx_dev
->gpio
.po
, 0);
1125 ret
+= gpio_export(ifx_dev
->gpio
.po
, 1);
1127 dev_err(&spi
->dev
, "Unable to configure GPIO%d (ON)",
1133 ret
= gpio_request(ifx_dev
->gpio
.mrdy
, "ifxModem");
1135 dev_err(&spi
->dev
, "Unable to allocate GPIO%d (MRDY)",
1136 ifx_dev
->gpio
.mrdy
);
1139 ret
+= gpio_export(ifx_dev
->gpio
.mrdy
, 1);
1140 ret
+= gpio_direction_output(ifx_dev
->gpio
.mrdy
, 0);
1142 dev_err(&spi
->dev
, "Unable to configure GPIO%d (MRDY)",
1143 ifx_dev
->gpio
.mrdy
);
1148 ret
= gpio_request(ifx_dev
->gpio
.srdy
, "ifxModem");
1150 dev_err(&spi
->dev
, "Unable to allocate GPIO%d (SRDY)",
1151 ifx_dev
->gpio
.srdy
);
1155 ret
+= gpio_export(ifx_dev
->gpio
.srdy
, 1);
1156 ret
+= gpio_direction_input(ifx_dev
->gpio
.srdy
);
1158 dev_err(&spi
->dev
, "Unable to configure GPIO%d (SRDY)",
1159 ifx_dev
->gpio
.srdy
);
1164 ret
= gpio_request(ifx_dev
->gpio
.reset_out
, "ifxModem");
1166 dev_err(&spi
->dev
, "Unable to allocate GPIO%d (RESET_OUT)",
1167 ifx_dev
->gpio
.reset_out
);
1170 ret
+= gpio_export(ifx_dev
->gpio
.reset_out
, 1);
1171 ret
+= gpio_direction_input(ifx_dev
->gpio
.reset_out
);
1173 dev_err(&spi
->dev
, "Unable to configure GPIO%d (RESET_OUT)",
1174 ifx_dev
->gpio
.reset_out
);
1179 ret
= request_irq(gpio_to_irq(ifx_dev
->gpio
.reset_out
),
1180 ifx_spi_reset_interrupt
,
1181 IRQF_TRIGGER_RISING
|IRQF_TRIGGER_FALLING
, DRVNAME
,
1184 dev_err(&spi
->dev
, "Unable to get irq %x\n",
1185 gpio_to_irq(ifx_dev
->gpio
.reset_out
));
1189 ret
= ifx_spi_reset(ifx_dev
);
1191 ret
= request_irq(gpio_to_irq(ifx_dev
->gpio
.srdy
),
1192 ifx_spi_srdy_interrupt
, IRQF_TRIGGER_RISING
, DRVNAME
,
1195 dev_err(&spi
->dev
, "Unable to get irq %x",
1196 gpio_to_irq(ifx_dev
->gpio
.srdy
));
1200 /* set pm runtime power state and register with power system */
1201 pm_runtime_set_active(&spi
->dev
);
1202 pm_runtime_enable(&spi
->dev
);
1204 /* handle case that modem is already signaling SRDY */
1205 /* no outgoing tty open at this point, this just satisfies the
1206 * modem's read and should reset communication properly
1208 srdy
= gpio_get_value(ifx_dev
->gpio
.srdy
);
1211 mrdy_assert(ifx_dev
);
1212 ifx_spi_handle_srdy(ifx_dev
);
1214 mrdy_set_low(ifx_dev
);
1218 free_irq(gpio_to_irq(ifx_dev
->gpio
.reset_out
), ifx_dev
);
1220 gpio_free(ifx_dev
->gpio
.srdy
);
1222 gpio_free(ifx_dev
->gpio
.mrdy
);
1224 gpio_free(ifx_dev
->gpio
.reset
);
1226 gpio_free(ifx_dev
->gpio
.po
);
1228 gpio_free(ifx_dev
->gpio
.reset_out
);
1230 ifx_spi_free_device(ifx_dev
);
1231 saved_ifx_dev
= NULL
;
1236 * ifx_spi_spi_remove - SPI device was removed
1239 * FIXME: We should be shutting the device down here not in
1240 * the module unload path.
1243 static int ifx_spi_spi_remove(struct spi_device
*spi
)
1245 struct ifx_spi_device
*ifx_dev
= spi_get_drvdata(spi
);
1247 tasklet_kill(&ifx_dev
->io_work_tasklet
);
1249 free_irq(gpio_to_irq(ifx_dev
->gpio
.reset_out
), ifx_dev
);
1250 free_irq(gpio_to_irq(ifx_dev
->gpio
.srdy
), ifx_dev
);
1252 gpio_free(ifx_dev
->gpio
.srdy
);
1253 gpio_free(ifx_dev
->gpio
.mrdy
);
1254 gpio_free(ifx_dev
->gpio
.reset
);
1255 gpio_free(ifx_dev
->gpio
.po
);
1256 gpio_free(ifx_dev
->gpio
.reset_out
);
1258 /* free allocations */
1259 ifx_spi_free_device(ifx_dev
);
1261 saved_ifx_dev
= NULL
;
1266 * ifx_spi_spi_shutdown - called on SPI shutdown
1269 * No action needs to be taken here
1272 static void ifx_spi_spi_shutdown(struct spi_device
*spi
)
1274 struct ifx_spi_device
*ifx_dev
= spi_get_drvdata(spi
);
1276 ifx_modem_power_off(ifx_dev
);
1280 * various suspends and resumes have nothing to do
1281 * no hardware to save state for
1285 * ifx_spi_pm_suspend - suspend modem on system suspend
1286 * @dev: device being suspended
1288 * Suspend the modem. No action needed on Intel MID platforms, may
1289 * need extending for other systems.
1291 static int ifx_spi_pm_suspend(struct device
*dev
)
1297 * ifx_spi_pm_resume - resume modem on system resume
1298 * @dev: device being suspended
1300 * Allow the modem to resume. No action needed.
1302 * FIXME: do we need to reset anything here ?
1304 static int ifx_spi_pm_resume(struct device
*dev
)
1310 * ifx_spi_pm_runtime_resume - suspend modem
1311 * @dev: device being suspended
1313 * Allow the modem to resume. No action needed.
1315 static int ifx_spi_pm_runtime_resume(struct device
*dev
)
1321 * ifx_spi_pm_runtime_suspend - suspend modem
1322 * @dev: device being suspended
1324 * Allow the modem to suspend and thus suspend to continue up the
1327 static int ifx_spi_pm_runtime_suspend(struct device
*dev
)
1333 * ifx_spi_pm_runtime_idle - check if modem idle
1336 * Check conditions and queue runtime suspend if idle.
1338 static int ifx_spi_pm_runtime_idle(struct device
*dev
)
1340 struct spi_device
*spi
= to_spi_device(dev
);
1341 struct ifx_spi_device
*ifx_dev
= spi_get_drvdata(spi
);
1343 if (!ifx_dev
->power_status
)
1344 pm_runtime_suspend(dev
);
1349 static const struct dev_pm_ops ifx_spi_pm
= {
1350 .resume
= ifx_spi_pm_resume
,
1351 .suspend
= ifx_spi_pm_suspend
,
1352 .runtime_resume
= ifx_spi_pm_runtime_resume
,
1353 .runtime_suspend
= ifx_spi_pm_runtime_suspend
,
1354 .runtime_idle
= ifx_spi_pm_runtime_idle
1357 static const struct spi_device_id ifx_id_table
[] = {
1362 MODULE_DEVICE_TABLE(spi
, ifx_id_table
);
1364 /* spi operations */
1365 static struct spi_driver ifx_spi_driver
= {
1370 .probe
= ifx_spi_spi_probe
,
1371 .shutdown
= ifx_spi_spi_shutdown
,
1372 .remove
= ifx_spi_spi_remove
,
1373 .id_table
= ifx_id_table
1377 * ifx_spi_exit - module exit
1379 * Unload the module.
1382 static void __exit
ifx_spi_exit(void)
1385 tty_unregister_driver(tty_drv
);
1386 put_tty_driver(tty_drv
);
1387 spi_unregister_driver(&ifx_spi_driver
);
1388 unregister_reboot_notifier(&ifx_modem_reboot_notifier_block
);
1392 * ifx_spi_init - module entry point
1394 * Initialise the SPI and tty interfaces for the IFX SPI driver
1395 * We need to initialize upper-edge spi driver after the tty
1396 * driver because otherwise the spi probe will race
1399 static int __init
ifx_spi_init(void)
1403 tty_drv
= alloc_tty_driver(1);
1405 pr_err("%s: alloc_tty_driver failed", DRVNAME
);
1409 tty_drv
->driver_name
= DRVNAME
;
1410 tty_drv
->name
= TTYNAME
;
1411 tty_drv
->minor_start
= IFX_SPI_TTY_ID
;
1412 tty_drv
->type
= TTY_DRIVER_TYPE_SERIAL
;
1413 tty_drv
->subtype
= SERIAL_TYPE_NORMAL
;
1414 tty_drv
->flags
= TTY_DRIVER_REAL_RAW
| TTY_DRIVER_DYNAMIC_DEV
;
1415 tty_drv
->init_termios
= tty_std_termios
;
1417 tty_set_operations(tty_drv
, &ifx_spi_serial_ops
);
1419 result
= tty_register_driver(tty_drv
);
1421 pr_err("%s: tty_register_driver failed(%d)",
1426 result
= spi_register_driver(&ifx_spi_driver
);
1428 pr_err("%s: spi_register_driver failed(%d)",
1433 result
= register_reboot_notifier(&ifx_modem_reboot_notifier_block
);
1435 pr_err("%s: register ifx modem reboot notifier failed(%d)",
1442 spi_unregister_driver(&ifx_spi_driver
);
1444 tty_unregister_driver(tty_drv
);
1446 put_tty_driver(tty_drv
);
1451 module_init(ifx_spi_init
);
1452 module_exit(ifx_spi_exit
);
1454 MODULE_AUTHOR("Intel");
1455 MODULE_DESCRIPTION("IFX6x60 spi driver");
1456 MODULE_LICENSE("GPL");
1457 MODULE_INFO(Version
, "0.1-IFX6x60");