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>
66 #define IFX_SPI_MORE_MASK 0x10
67 #define IFX_SPI_MORE_BIT 12 /* bit position in u16 */
68 #define IFX_SPI_CTS_BIT 13 /* bit position in u16 */
69 #define IFX_SPI_MODE SPI_MODE_1
70 #define IFX_SPI_TTY_ID 0
71 #define IFX_SPI_TIMEOUT_SEC 2
72 #define IFX_SPI_HEADER_0 (-1)
73 #define IFX_SPI_HEADER_F (-2)
75 /* forward reference */
76 static void ifx_spi_handle_srdy(struct ifx_spi_device
*ifx_dev
);
79 static int spi_bpw
= 16; /* 8, 16 or 32 bit word length */
80 static struct tty_driver
*tty_drv
;
81 static struct ifx_spi_device
*saved_ifx_dev
;
82 static struct lock_class_key ifx_spi_key
;
84 /* GPIO/GPE settings */
87 * mrdy_set_high - set MRDY GPIO
88 * @ifx: device we are controlling
91 static inline void mrdy_set_high(struct ifx_spi_device
*ifx
)
93 gpio_set_value(ifx
->gpio
.mrdy
, 1);
97 * mrdy_set_low - clear MRDY GPIO
98 * @ifx: device we are controlling
101 static inline void mrdy_set_low(struct ifx_spi_device
*ifx
)
103 gpio_set_value(ifx
->gpio
.mrdy
, 0);
107 * ifx_spi_power_state_set
108 * @ifx_dev: our SPI device
111 * Set bit in power status and signal power system if status becomes non-0
114 ifx_spi_power_state_set(struct ifx_spi_device
*ifx_dev
, unsigned char val
)
118 spin_lock_irqsave(&ifx_dev
->power_lock
, flags
);
121 * if power status is already non-0, just update, else
124 if (!ifx_dev
->power_status
)
125 pm_runtime_get(&ifx_dev
->spi_dev
->dev
);
126 ifx_dev
->power_status
|= val
;
128 spin_unlock_irqrestore(&ifx_dev
->power_lock
, flags
);
132 * ifx_spi_power_state_clear - clear power bit
133 * @ifx_dev: our SPI device
134 * @val: bits to clear
136 * clear bit in power status and signal power system if status becomes 0
139 ifx_spi_power_state_clear(struct ifx_spi_device
*ifx_dev
, unsigned char val
)
143 spin_lock_irqsave(&ifx_dev
->power_lock
, flags
);
145 if (ifx_dev
->power_status
) {
146 ifx_dev
->power_status
&= ~val
;
147 if (!ifx_dev
->power_status
)
148 pm_runtime_put(&ifx_dev
->spi_dev
->dev
);
151 spin_unlock_irqrestore(&ifx_dev
->power_lock
, flags
);
157 * @len : number of bytes (not words) in the buffer
158 * @end: end of buffer
160 * Swap the contents of a buffer into big endian format
162 static inline void swap_buf(u16
*buf
, int len
, void *end
)
166 len
= ((len
+ 1) >> 1);
167 if ((void *)&buf
[len
] > end
) {
168 pr_err("swap_buf: swap exceeds boundary (%p > %p)!",
172 for (n
= 0; n
< len
; n
++) {
173 *buf
= cpu_to_be16(*buf
);
179 * mrdy_assert - assert MRDY line
180 * @ifx_dev: our SPI device
182 * Assert mrdy and set timer to wait for SRDY interrupt, if SRDY is low
185 * FIXME: Can SRDY even go high as we are running this code ?
187 static void mrdy_assert(struct ifx_spi_device
*ifx_dev
)
189 int val
= gpio_get_value(ifx_dev
->gpio
.srdy
);
191 if (!test_and_set_bit(IFX_SPI_STATE_TIMER_PENDING
,
193 ifx_dev
->spi_timer
.expires
=
194 jiffies
+ IFX_SPI_TIMEOUT_SEC
*HZ
;
195 add_timer(&ifx_dev
->spi_timer
);
199 ifx_spi_power_state_set(ifx_dev
, IFX_SPI_POWER_DATA_PENDING
);
200 mrdy_set_high(ifx_dev
);
204 * ifx_spi_hangup - hang up an IFX device
205 * @ifx_dev: our SPI device
207 * Hang up the tty attached to the IFX device if one is currently
208 * open. If not take no action
210 static void ifx_spi_ttyhangup(struct ifx_spi_device
*ifx_dev
)
212 struct tty_port
*pport
= &ifx_dev
->tty_port
;
213 struct tty_struct
*tty
= tty_port_tty_get(pport
);
221 * ifx_spi_timeout - SPI timeout
222 * @arg: our SPI device
224 * The SPI has timed out: hang up the tty. Users will then see a hangup
227 static void ifx_spi_timeout(unsigned long arg
)
229 struct ifx_spi_device
*ifx_dev
= (struct ifx_spi_device
*)arg
;
231 dev_warn(&ifx_dev
->spi_dev
->dev
, "*** SPI Timeout ***");
232 ifx_spi_ttyhangup(ifx_dev
);
233 mrdy_set_low(ifx_dev
);
234 clear_bit(IFX_SPI_STATE_TIMER_PENDING
, &ifx_dev
->flags
);
237 /* char/tty operations */
240 * ifx_spi_tiocmget - get modem lines
241 * @tty: our tty device
242 * @filp: file handle issuing the request
244 * Map the signal state into Linux modem flags and report the value
247 static int ifx_spi_tiocmget(struct tty_struct
*tty
)
250 struct ifx_spi_device
*ifx_dev
= tty
->driver_data
;
253 (test_bit(IFX_SPI_RTS
, &ifx_dev
->signal_state
) ? TIOCM_RTS
: 0) |
254 (test_bit(IFX_SPI_DTR
, &ifx_dev
->signal_state
) ? TIOCM_DTR
: 0) |
255 (test_bit(IFX_SPI_CTS
, &ifx_dev
->signal_state
) ? TIOCM_CTS
: 0) |
256 (test_bit(IFX_SPI_DSR
, &ifx_dev
->signal_state
) ? TIOCM_DSR
: 0) |
257 (test_bit(IFX_SPI_DCD
, &ifx_dev
->signal_state
) ? TIOCM_CAR
: 0) |
258 (test_bit(IFX_SPI_RI
, &ifx_dev
->signal_state
) ? TIOCM_RNG
: 0);
263 * ifx_spi_tiocmset - set modem bits
264 * @tty: the tty structure
266 * @clear: bits to clear
268 * The IFX6x60 only supports DTR and RTS. Set them accordingly
269 * and flag that an update to the modem is needed.
271 * FIXME: do we need to kick the tranfers when we do this ?
273 static int ifx_spi_tiocmset(struct tty_struct
*tty
,
274 unsigned int set
, unsigned int clear
)
276 struct ifx_spi_device
*ifx_dev
= tty
->driver_data
;
279 set_bit(IFX_SPI_RTS
, &ifx_dev
->signal_state
);
281 set_bit(IFX_SPI_DTR
, &ifx_dev
->signal_state
);
282 if (clear
& TIOCM_RTS
)
283 clear_bit(IFX_SPI_RTS
, &ifx_dev
->signal_state
);
284 if (clear
& TIOCM_DTR
)
285 clear_bit(IFX_SPI_DTR
, &ifx_dev
->signal_state
);
287 set_bit(IFX_SPI_UPDATE
, &ifx_dev
->signal_state
);
292 * ifx_spi_open - called on tty open
293 * @tty: our tty device
294 * @filp: file handle being associated with the tty
296 * Open the tty interface. We let the tty_port layer do all the work
299 * FIXME: Remove single device assumption and saved_ifx_dev
301 static int ifx_spi_open(struct tty_struct
*tty
, struct file
*filp
)
303 return tty_port_open(&saved_ifx_dev
->tty_port
, tty
, filp
);
307 * ifx_spi_close - called when our tty closes
308 * @tty: the tty being closed
309 * @filp: the file handle being closed
311 * Perform the close of the tty. We use the tty_port layer to do all
314 static void ifx_spi_close(struct tty_struct
*tty
, struct file
*filp
)
316 struct ifx_spi_device
*ifx_dev
= tty
->driver_data
;
317 tty_port_close(&ifx_dev
->tty_port
, tty
, filp
);
318 /* FIXME: should we do an ifx_spi_reset here ? */
322 * ifx_decode_spi_header - decode received header
323 * @buffer: the received data
324 * @length: decoded length
325 * @more: decoded more flag
326 * @received_cts: status of cts we received
328 * Note how received_cts is handled -- if header is all F it is left
329 * the same as it was, if header is all 0 it is set to 0 otherwise it is
330 * taken from the incoming header.
334 static int ifx_spi_decode_spi_header(unsigned char *buffer
, int *length
,
335 unsigned char *more
, unsigned char *received_cts
)
339 u16
*in_buffer
= (u16
*)buffer
;
344 if (h1
== 0 && h2
== 0) {
346 return IFX_SPI_HEADER_0
;
347 } else if (h1
== 0xffff && h2
== 0xffff) {
348 /* spi_slave_cts remains as it was */
349 return IFX_SPI_HEADER_F
;
352 *length
= h1
& 0xfff; /* upper bits of byte are flags */
353 *more
= (buffer
[1] >> IFX_SPI_MORE_BIT
) & 1;
354 *received_cts
= (buffer
[3] >> IFX_SPI_CTS_BIT
) & 1;
359 * ifx_setup_spi_header - set header fields
360 * @txbuffer: pointer to start of SPI buffer
362 * @more: indicate if more to follow
364 * Format up an SPI header for a transfer
368 static void ifx_spi_setup_spi_header(unsigned char *txbuffer
, int tx_count
,
371 *(u16
*)(txbuffer
) = tx_count
;
372 *(u16
*)(txbuffer
+2) = IFX_SPI_PAYLOAD_SIZE
;
373 txbuffer
[1] |= (more
<< IFX_SPI_MORE_BIT
) & IFX_SPI_MORE_MASK
;
377 * ifx_spi_wakeup_serial - SPI space made
378 * @port_data: our SPI device
380 * We have emptied the FIFO enough that we want to get more data
381 * queued into it. Poke the line discipline via tty_wakeup so that
382 * it will feed us more bits
384 static void ifx_spi_wakeup_serial(struct ifx_spi_device
*ifx_dev
)
386 struct tty_struct
*tty
;
388 tty
= tty_port_tty_get(&ifx_dev
->tty_port
);
396 * ifx_spi_prepare_tx_buffer - prepare transmit frame
397 * @ifx_dev: our SPI device
399 * The transmit buffr needs a header and various other bits of
400 * information followed by as much data as we can pull from the FIFO
401 * and transfer. This function formats up a suitable buffer in the
404 * FIXME: performance - should we wake the tty when the queue is half
407 static int ifx_spi_prepare_tx_buffer(struct ifx_spi_device
*ifx_dev
)
412 unsigned char *tx_buffer
;
414 tx_buffer
= ifx_dev
->tx_buffer
;
415 memset(tx_buffer
, 0, IFX_SPI_TRANSFER_SIZE
);
417 /* make room for required SPI header */
418 tx_buffer
+= IFX_SPI_HEADER_OVERHEAD
;
419 tx_count
= IFX_SPI_HEADER_OVERHEAD
;
421 /* clear to signal no more data if this turns out to be the
422 * last buffer sent in a sequence */
423 ifx_dev
->spi_more
= 0;
425 /* if modem cts is set, just send empty buffer */
426 if (!ifx_dev
->spi_slave_cts
) {
427 /* see if there's tx data */
428 queue_length
= kfifo_len(&ifx_dev
->tx_fifo
);
429 if (queue_length
!= 0) {
430 /* data to mux -- see if there's room for it */
431 temp_count
= min(queue_length
, IFX_SPI_PAYLOAD_SIZE
);
432 temp_count
= kfifo_out_locked(&ifx_dev
->tx_fifo
,
433 tx_buffer
, temp_count
,
434 &ifx_dev
->fifo_lock
);
436 /* update buffer pointer and data count in message */
437 tx_buffer
+= temp_count
;
438 tx_count
+= temp_count
;
439 if (temp_count
== queue_length
)
440 /* poke port to get more data */
441 ifx_spi_wakeup_serial(ifx_dev
);
442 else /* more data in port, use next SPI message */
443 ifx_dev
->spi_more
= 1;
446 /* have data and info for header -- set up SPI header in buffer */
447 /* spi header needs payload size, not entire buffer size */
448 ifx_spi_setup_spi_header(ifx_dev
->tx_buffer
,
449 tx_count
-IFX_SPI_HEADER_OVERHEAD
,
451 /* swap actual data in the buffer */
452 swap_buf((u16
*)(ifx_dev
->tx_buffer
), tx_count
,
453 &ifx_dev
->tx_buffer
[IFX_SPI_TRANSFER_SIZE
]);
458 * ifx_spi_write - line discipline write
459 * @tty: our tty device
460 * @buf: pointer to buffer to write (kernel space)
461 * @count: size of buffer
463 * Write the characters we have been given into the FIFO. If the device
464 * is not active then activate it, when the SRDY line is asserted back
465 * this will commence I/O
467 static int ifx_spi_write(struct tty_struct
*tty
, const unsigned char *buf
,
470 struct ifx_spi_device
*ifx_dev
= tty
->driver_data
;
471 unsigned char *tmp_buf
= (unsigned char *)buf
;
472 int tx_count
= kfifo_in_locked(&ifx_dev
->tx_fifo
, tmp_buf
, count
,
473 &ifx_dev
->fifo_lock
);
474 mrdy_assert(ifx_dev
);
479 * ifx_spi_chars_in_buffer - line discipline helper
480 * @tty: our tty device
482 * Report how much data we can accept before we drop bytes. As we use
483 * a simple FIFO this is nice and easy.
485 static int ifx_spi_write_room(struct tty_struct
*tty
)
487 struct ifx_spi_device
*ifx_dev
= tty
->driver_data
;
488 return IFX_SPI_FIFO_SIZE
- kfifo_len(&ifx_dev
->tx_fifo
);
492 * ifx_spi_chars_in_buffer - line discipline helper
493 * @tty: our tty device
495 * Report how many characters we have buffered. In our case this is the
496 * number of bytes sitting in our transmit FIFO.
498 static int ifx_spi_chars_in_buffer(struct tty_struct
*tty
)
500 struct ifx_spi_device
*ifx_dev
= tty
->driver_data
;
501 return kfifo_len(&ifx_dev
->tx_fifo
);
506 * @port: our tty port
508 * tty port hang up. Called when tty_hangup processing is invoked either
509 * by loss of carrier, or by software (eg vhangup). Serialized against
510 * activate/shutdown by the tty layer.
512 static void ifx_spi_hangup(struct tty_struct
*tty
)
514 struct ifx_spi_device
*ifx_dev
= tty
->driver_data
;
515 tty_port_hangup(&ifx_dev
->tty_port
);
520 * @port: our tty port
522 * tty port activate method - called for first open. Serialized
523 * with hangup and shutdown by the tty layer.
525 static int ifx_port_activate(struct tty_port
*port
, struct tty_struct
*tty
)
527 struct ifx_spi_device
*ifx_dev
=
528 container_of(port
, struct ifx_spi_device
, tty_port
);
530 /* clear any old data; can't do this in 'close' */
531 kfifo_reset(&ifx_dev
->tx_fifo
);
533 /* put port data into this tty */
534 tty
->driver_data
= ifx_dev
;
536 /* allows flip string push from int context */
537 tty
->low_latency
= 1;
544 * @port: our tty port
546 * tty port shutdown method - called for last port close. Serialized
547 * with hangup and activate by the tty layer.
549 static void ifx_port_shutdown(struct tty_port
*port
)
551 struct ifx_spi_device
*ifx_dev
=
552 container_of(port
, struct ifx_spi_device
, tty_port
);
554 mrdy_set_low(ifx_dev
);
555 del_timer(&ifx_dev
->spi_timer
);
556 clear_bit(IFX_SPI_STATE_TIMER_PENDING
, &ifx_dev
->flags
);
557 tasklet_kill(&ifx_dev
->io_work_tasklet
);
560 static const struct tty_port_operations ifx_tty_port_ops
= {
561 .activate
= ifx_port_activate
,
562 .shutdown
= ifx_port_shutdown
,
565 static const struct tty_operations ifx_spi_serial_ops
= {
566 .open
= ifx_spi_open
,
567 .close
= ifx_spi_close
,
568 .write
= ifx_spi_write
,
569 .hangup
= ifx_spi_hangup
,
570 .write_room
= ifx_spi_write_room
,
571 .chars_in_buffer
= ifx_spi_chars_in_buffer
,
572 .tiocmget
= ifx_spi_tiocmget
,
573 .tiocmset
= ifx_spi_tiocmset
,
577 * ifx_spi_insert_fip_string - queue received data
578 * @ifx_ser: our SPI device
579 * @chars: buffer we have received
580 * @size: number of chars reeived
582 * Queue bytes to the tty assuming the tty side is currently open. If
583 * not the discard the data.
585 static void ifx_spi_insert_flip_string(struct ifx_spi_device
*ifx_dev
,
586 unsigned char *chars
, size_t size
)
588 struct tty_struct
*tty
= tty_port_tty_get(&ifx_dev
->tty_port
);
591 tty_insert_flip_string(tty
, chars
, size
);
592 tty_flip_buffer_push(tty
);
597 * ifx_spi_complete - SPI transfer completed
598 * @ctx: our SPI device
600 * An SPI transfer has completed. Process any received data and kick off
601 * any further transmits we can commence.
603 static void ifx_spi_complete(void *ctx
)
605 struct ifx_spi_device
*ifx_dev
= ctx
;
606 struct tty_struct
*tty
;
607 struct tty_ldisc
*ldisc
= NULL
;
612 int local_write_pending
= 0;
617 mrdy_set_low(ifx_dev
);
619 if (!ifx_dev
->spi_msg
.status
) {
620 /* check header validity, get comm flags */
621 swap_buf((u16
*)ifx_dev
->rx_buffer
, IFX_SPI_HEADER_OVERHEAD
,
622 &ifx_dev
->rx_buffer
[IFX_SPI_HEADER_OVERHEAD
]);
623 decode_result
= ifx_spi_decode_spi_header(ifx_dev
->rx_buffer
,
624 &length
, &more
, &cts
);
625 if (decode_result
== IFX_SPI_HEADER_0
) {
626 dev_dbg(&ifx_dev
->spi_dev
->dev
,
627 "ignore input: invalid header 0");
628 ifx_dev
->spi_slave_cts
= 0;
630 } else if (decode_result
== IFX_SPI_HEADER_F
) {
631 dev_dbg(&ifx_dev
->spi_dev
->dev
,
632 "ignore input: invalid header F");
636 ifx_dev
->spi_slave_cts
= cts
;
638 actual_length
= min((unsigned int)length
,
639 ifx_dev
->spi_msg
.actual_length
);
640 swap_buf((u16
*)(ifx_dev
->rx_buffer
+ IFX_SPI_HEADER_OVERHEAD
),
642 &ifx_dev
->rx_buffer
[IFX_SPI_TRANSFER_SIZE
]);
643 ifx_spi_insert_flip_string(
645 ifx_dev
->rx_buffer
+ IFX_SPI_HEADER_OVERHEAD
,
646 (size_t)actual_length
);
648 dev_dbg(&ifx_dev
->spi_dev
->dev
, "SPI transfer error %d",
649 ifx_dev
->spi_msg
.status
);
653 if (ifx_dev
->write_pending
) {
654 ifx_dev
->write_pending
= 0;
655 local_write_pending
= 1;
658 clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS
, &(ifx_dev
->flags
));
660 queue_length
= kfifo_len(&ifx_dev
->tx_fifo
);
661 srdy
= gpio_get_value(ifx_dev
->gpio
.srdy
);
663 ifx_spi_power_state_clear(ifx_dev
, IFX_SPI_POWER_SRDY
);
665 /* schedule output if there is more to do */
666 if (test_and_clear_bit(IFX_SPI_STATE_IO_READY
, &ifx_dev
->flags
))
667 tasklet_schedule(&ifx_dev
->io_work_tasklet
);
669 if (more
|| ifx_dev
->spi_more
|| queue_length
> 0 ||
670 local_write_pending
) {
671 if (ifx_dev
->spi_slave_cts
) {
673 mrdy_assert(ifx_dev
);
675 mrdy_assert(ifx_dev
);
678 * poke line discipline driver if any for more data
679 * may or may not get more data to write
680 * for now, say not busy
682 ifx_spi_power_state_clear(ifx_dev
,
683 IFX_SPI_POWER_DATA_PENDING
);
684 tty
= tty_port_tty_get(&ifx_dev
->tty_port
);
686 ldisc
= tty_ldisc_ref(tty
);
688 ldisc
->ops
->write_wakeup(tty
);
689 tty_ldisc_deref(ldisc
);
698 * ifx_spio_io - I/O tasklet
699 * @data: our SPI device
701 * Queue data for transmission if possible and then kick off the
704 static void ifx_spi_io(unsigned long data
)
707 struct ifx_spi_device
*ifx_dev
= (struct ifx_spi_device
*) data
;
709 if (!test_and_set_bit(IFX_SPI_STATE_IO_IN_PROGRESS
, &ifx_dev
->flags
)) {
710 if (ifx_dev
->gpio
.unack_srdy_int_nb
> 0)
711 ifx_dev
->gpio
.unack_srdy_int_nb
--;
713 ifx_spi_prepare_tx_buffer(ifx_dev
);
715 spi_message_init(&ifx_dev
->spi_msg
);
716 INIT_LIST_HEAD(&ifx_dev
->spi_msg
.queue
);
718 ifx_dev
->spi_msg
.context
= ifx_dev
;
719 ifx_dev
->spi_msg
.complete
= ifx_spi_complete
;
721 /* set up our spi transfer */
722 /* note len is BYTES, not transfers */
723 ifx_dev
->spi_xfer
.len
= IFX_SPI_TRANSFER_SIZE
;
724 ifx_dev
->spi_xfer
.cs_change
= 0;
725 ifx_dev
->spi_xfer
.speed_hz
= ifx_dev
->spi_dev
->max_speed_hz
;
726 /* ifx_dev->spi_xfer.speed_hz = 390625; */
727 ifx_dev
->spi_xfer
.bits_per_word
= spi_bpw
;
729 ifx_dev
->spi_xfer
.tx_buf
= ifx_dev
->tx_buffer
;
730 ifx_dev
->spi_xfer
.rx_buf
= ifx_dev
->rx_buffer
;
735 if (ifx_dev
->use_dma
) {
736 ifx_dev
->spi_msg
.is_dma_mapped
= 1;
737 ifx_dev
->tx_dma
= ifx_dev
->tx_bus
;
738 ifx_dev
->rx_dma
= ifx_dev
->rx_bus
;
739 ifx_dev
->spi_xfer
.tx_dma
= ifx_dev
->tx_dma
;
740 ifx_dev
->spi_xfer
.rx_dma
= ifx_dev
->rx_dma
;
742 ifx_dev
->spi_msg
.is_dma_mapped
= 0;
743 ifx_dev
->tx_dma
= (dma_addr_t
)0;
744 ifx_dev
->rx_dma
= (dma_addr_t
)0;
745 ifx_dev
->spi_xfer
.tx_dma
= (dma_addr_t
)0;
746 ifx_dev
->spi_xfer
.rx_dma
= (dma_addr_t
)0;
749 spi_message_add_tail(&ifx_dev
->spi_xfer
, &ifx_dev
->spi_msg
);
751 /* Assert MRDY. This may have already been done by the write
754 mrdy_assert(ifx_dev
);
756 retval
= spi_async(ifx_dev
->spi_dev
, &ifx_dev
->spi_msg
);
758 clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS
,
760 tasklet_schedule(&ifx_dev
->io_work_tasklet
);
764 ifx_dev
->write_pending
= 1;
768 * ifx_spi_free_port - free up the tty side
769 * @ifx_dev: IFX device going away
771 * Unregister and free up a port when the device goes away
773 static void ifx_spi_free_port(struct ifx_spi_device
*ifx_dev
)
775 if (ifx_dev
->tty_dev
)
776 tty_unregister_device(tty_drv
, ifx_dev
->minor
);
777 kfifo_free(&ifx_dev
->tx_fifo
);
781 * ifx_spi_create_port - create a new port
782 * @ifx_dev: our spi device
784 * Allocate and initialise the tty port that goes with this interface
785 * and add it to the tty layer so that it can be opened.
787 static int ifx_spi_create_port(struct ifx_spi_device
*ifx_dev
)
790 struct tty_port
*pport
= &ifx_dev
->tty_port
;
792 spin_lock_init(&ifx_dev
->fifo_lock
);
793 lockdep_set_class_and_subclass(&ifx_dev
->fifo_lock
,
796 if (kfifo_alloc(&ifx_dev
->tx_fifo
, IFX_SPI_FIFO_SIZE
, GFP_KERNEL
)) {
801 tty_port_init(pport
);
802 pport
->ops
= &ifx_tty_port_ops
;
803 ifx_dev
->minor
= IFX_SPI_TTY_ID
;
804 ifx_dev
->tty_dev
= tty_register_device(tty_drv
, ifx_dev
->minor
,
805 &ifx_dev
->spi_dev
->dev
);
806 if (IS_ERR(ifx_dev
->tty_dev
)) {
807 dev_dbg(&ifx_dev
->spi_dev
->dev
,
808 "%s: registering tty device failed", __func__
);
809 ret
= PTR_ERR(ifx_dev
->tty_dev
);
815 ifx_spi_free_port(ifx_dev
);
820 * ifx_spi_handle_srdy - handle SRDY
821 * @ifx_dev: device asserting SRDY
823 * Check our device state and see what we need to kick off when SRDY
824 * is asserted. This usually means killing the timer and firing off the
827 static void ifx_spi_handle_srdy(struct ifx_spi_device
*ifx_dev
)
829 if (test_bit(IFX_SPI_STATE_TIMER_PENDING
, &ifx_dev
->flags
)) {
830 del_timer_sync(&ifx_dev
->spi_timer
);
831 clear_bit(IFX_SPI_STATE_TIMER_PENDING
, &ifx_dev
->flags
);
834 ifx_spi_power_state_set(ifx_dev
, IFX_SPI_POWER_SRDY
);
836 if (!test_bit(IFX_SPI_STATE_IO_IN_PROGRESS
, &ifx_dev
->flags
))
837 tasklet_schedule(&ifx_dev
->io_work_tasklet
);
839 set_bit(IFX_SPI_STATE_IO_READY
, &ifx_dev
->flags
);
843 * ifx_spi_srdy_interrupt - SRDY asserted
844 * @irq: our IRQ number
845 * @dev: our ifx device
847 * The modem asserted SRDY. Handle the srdy event
849 static irqreturn_t
ifx_spi_srdy_interrupt(int irq
, void *dev
)
851 struct ifx_spi_device
*ifx_dev
= dev
;
852 ifx_dev
->gpio
.unack_srdy_int_nb
++;
853 ifx_spi_handle_srdy(ifx_dev
);
858 * ifx_spi_reset_interrupt - Modem has changed reset state
859 * @irq: interrupt number
860 * @dev: our device pointer
862 * The modem has either entered or left reset state. Check the GPIO
865 * FIXME: review locking on MR_INPROGRESS versus
866 * parallel unsolicited reset/solicited reset
868 static irqreturn_t
ifx_spi_reset_interrupt(int irq
, void *dev
)
870 struct ifx_spi_device
*ifx_dev
= dev
;
871 int val
= gpio_get_value(ifx_dev
->gpio
.reset_out
);
872 int solreset
= test_bit(MR_START
, &ifx_dev
->mdm_reset_state
);
876 set_bit(MR_INPROGRESS
, &ifx_dev
->mdm_reset_state
);
878 /* unsolicited reset */
879 ifx_spi_ttyhangup(ifx_dev
);
883 clear_bit(MR_INPROGRESS
, &ifx_dev
->mdm_reset_state
);
885 set_bit(MR_COMPLETE
, &ifx_dev
->mdm_reset_state
);
886 wake_up(&ifx_dev
->mdm_reset_wait
);
893 * ifx_spi_free_device - free device
894 * @ifx_dev: device to free
896 * Free the IFX device
898 static void ifx_spi_free_device(struct ifx_spi_device
*ifx_dev
)
900 ifx_spi_free_port(ifx_dev
);
901 dma_free_coherent(&ifx_dev
->spi_dev
->dev
,
902 IFX_SPI_TRANSFER_SIZE
,
905 dma_free_coherent(&ifx_dev
->spi_dev
->dev
,
906 IFX_SPI_TRANSFER_SIZE
,
912 * ifx_spi_reset - reset modem
913 * @ifx_dev: modem to reset
915 * Perform a reset on the modem
917 static int ifx_spi_reset(struct ifx_spi_device
*ifx_dev
)
921 * set up modem power, reset
923 * delays are required on some platforms for the modem
926 set_bit(MR_START
, &ifx_dev
->mdm_reset_state
);
927 gpio_set_value(ifx_dev
->gpio
.po
, 0);
928 gpio_set_value(ifx_dev
->gpio
.reset
, 0);
930 gpio_set_value(ifx_dev
->gpio
.reset
, 1);
932 gpio_set_value(ifx_dev
->gpio
.po
, 1);
934 gpio_set_value(ifx_dev
->gpio
.po
, 0);
935 ret
= wait_event_timeout(ifx_dev
->mdm_reset_wait
,
936 test_bit(MR_COMPLETE
,
937 &ifx_dev
->mdm_reset_state
),
940 dev_warn(&ifx_dev
->spi_dev
->dev
, "Modem reset timeout: (state:%lx)",
941 ifx_dev
->mdm_reset_state
);
943 ifx_dev
->mdm_reset_state
= 0;
948 * ifx_spi_spi_probe - probe callback
949 * @spi: our possible matching SPI device
951 * Probe for a 6x60 modem on SPI bus. Perform any needed device and
955 * - Support for multiple devices
956 * - Split out MID specific GPIO handling eventually
959 static int ifx_spi_spi_probe(struct spi_device
*spi
)
963 struct ifx_modem_platform_data
*pl_data
;
964 struct ifx_spi_device
*ifx_dev
;
967 dev_dbg(&spi
->dev
, "ignoring subsequent detection");
971 pl_data
= (struct ifx_modem_platform_data
*)spi
->dev
.platform_data
;
973 dev_err(&spi
->dev
, "missing platform data!");
977 /* initialize structure to hold our device variables */
978 ifx_dev
= kzalloc(sizeof(struct ifx_spi_device
), GFP_KERNEL
);
980 dev_err(&spi
->dev
, "spi device allocation failed");
983 saved_ifx_dev
= ifx_dev
;
984 ifx_dev
->spi_dev
= spi
;
985 clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS
, &ifx_dev
->flags
);
986 spin_lock_init(&ifx_dev
->write_lock
);
987 spin_lock_init(&ifx_dev
->power_lock
);
988 ifx_dev
->power_status
= 0;
989 init_timer(&ifx_dev
->spi_timer
);
990 ifx_dev
->spi_timer
.function
= ifx_spi_timeout
;
991 ifx_dev
->spi_timer
.data
= (unsigned long)ifx_dev
;
992 ifx_dev
->modem
= pl_data
->modem_type
;
993 ifx_dev
->use_dma
= pl_data
->use_dma
;
994 ifx_dev
->max_hz
= pl_data
->max_hz
;
995 /* initialize spi mode, etc */
996 spi
->max_speed_hz
= ifx_dev
->max_hz
;
997 spi
->mode
= IFX_SPI_MODE
| (SPI_LOOP
& spi
->mode
);
998 spi
->bits_per_word
= spi_bpw
;
999 ret
= spi_setup(spi
);
1001 dev_err(&spi
->dev
, "SPI setup wasn't successful %d", ret
);
1005 /* ensure SPI protocol flags are initialized to enable transfer */
1006 ifx_dev
->spi_more
= 0;
1007 ifx_dev
->spi_slave_cts
= 0;
1009 /*initialize transfer and dma buffers */
1010 ifx_dev
->tx_buffer
= dma_alloc_coherent(ifx_dev
->spi_dev
->dev
.parent
,
1011 IFX_SPI_TRANSFER_SIZE
,
1014 if (!ifx_dev
->tx_buffer
) {
1015 dev_err(&spi
->dev
, "DMA-TX buffer allocation failed");
1019 ifx_dev
->rx_buffer
= dma_alloc_coherent(ifx_dev
->spi_dev
->dev
.parent
,
1020 IFX_SPI_TRANSFER_SIZE
,
1023 if (!ifx_dev
->rx_buffer
) {
1024 dev_err(&spi
->dev
, "DMA-RX buffer allocation failed");
1029 /* initialize waitq for modem reset */
1030 init_waitqueue_head(&ifx_dev
->mdm_reset_wait
);
1032 spi_set_drvdata(spi
, ifx_dev
);
1033 tasklet_init(&ifx_dev
->io_work_tasklet
, ifx_spi_io
,
1034 (unsigned long)ifx_dev
);
1036 set_bit(IFX_SPI_STATE_PRESENT
, &ifx_dev
->flags
);
1038 /* create our tty port */
1039 ret
= ifx_spi_create_port(ifx_dev
);
1041 dev_err(&spi
->dev
, "create default tty port failed");
1045 ifx_dev
->gpio
.reset
= pl_data
->rst_pmu
;
1046 ifx_dev
->gpio
.po
= pl_data
->pwr_on
;
1047 ifx_dev
->gpio
.mrdy
= pl_data
->mrdy
;
1048 ifx_dev
->gpio
.srdy
= pl_data
->srdy
;
1049 ifx_dev
->gpio
.reset_out
= pl_data
->rst_out
;
1051 dev_info(&spi
->dev
, "gpios %d, %d, %d, %d, %d",
1052 ifx_dev
->gpio
.reset
, ifx_dev
->gpio
.po
, ifx_dev
->gpio
.mrdy
,
1053 ifx_dev
->gpio
.srdy
, ifx_dev
->gpio
.reset_out
);
1055 /* Configure gpios */
1056 ret
= gpio_request(ifx_dev
->gpio
.reset
, "ifxModem");
1058 dev_err(&spi
->dev
, "Unable to allocate GPIO%d (RESET)",
1059 ifx_dev
->gpio
.reset
);
1062 ret
+= gpio_direction_output(ifx_dev
->gpio
.reset
, 0);
1063 ret
+= gpio_export(ifx_dev
->gpio
.reset
, 1);
1065 dev_err(&spi
->dev
, "Unable to configure GPIO%d (RESET)",
1066 ifx_dev
->gpio
.reset
);
1071 ret
= gpio_request(ifx_dev
->gpio
.po
, "ifxModem");
1072 ret
+= gpio_direction_output(ifx_dev
->gpio
.po
, 0);
1073 ret
+= gpio_export(ifx_dev
->gpio
.po
, 1);
1075 dev_err(&spi
->dev
, "Unable to configure GPIO%d (ON)",
1081 ret
= gpio_request(ifx_dev
->gpio
.mrdy
, "ifxModem");
1083 dev_err(&spi
->dev
, "Unable to allocate GPIO%d (MRDY)",
1084 ifx_dev
->gpio
.mrdy
);
1087 ret
+= gpio_export(ifx_dev
->gpio
.mrdy
, 1);
1088 ret
+= gpio_direction_output(ifx_dev
->gpio
.mrdy
, 0);
1090 dev_err(&spi
->dev
, "Unable to configure GPIO%d (MRDY)",
1091 ifx_dev
->gpio
.mrdy
);
1096 ret
= gpio_request(ifx_dev
->gpio
.srdy
, "ifxModem");
1098 dev_err(&spi
->dev
, "Unable to allocate GPIO%d (SRDY)",
1099 ifx_dev
->gpio
.srdy
);
1103 ret
+= gpio_export(ifx_dev
->gpio
.srdy
, 1);
1104 ret
+= gpio_direction_input(ifx_dev
->gpio
.srdy
);
1106 dev_err(&spi
->dev
, "Unable to configure GPIO%d (SRDY)",
1107 ifx_dev
->gpio
.srdy
);
1112 ret
= gpio_request(ifx_dev
->gpio
.reset_out
, "ifxModem");
1114 dev_err(&spi
->dev
, "Unable to allocate GPIO%d (RESET_OUT)",
1115 ifx_dev
->gpio
.reset_out
);
1118 ret
+= gpio_export(ifx_dev
->gpio
.reset_out
, 1);
1119 ret
+= gpio_direction_input(ifx_dev
->gpio
.reset_out
);
1121 dev_err(&spi
->dev
, "Unable to configure GPIO%d (RESET_OUT)",
1122 ifx_dev
->gpio
.reset_out
);
1127 ret
= request_irq(gpio_to_irq(ifx_dev
->gpio
.reset_out
),
1128 ifx_spi_reset_interrupt
,
1129 IRQF_TRIGGER_RISING
|IRQF_TRIGGER_FALLING
, DRVNAME
,
1132 dev_err(&spi
->dev
, "Unable to get irq %x\n",
1133 gpio_to_irq(ifx_dev
->gpio
.reset_out
));
1137 ret
= ifx_spi_reset(ifx_dev
);
1139 ret
= request_irq(gpio_to_irq(ifx_dev
->gpio
.srdy
),
1140 ifx_spi_srdy_interrupt
,
1141 IRQF_TRIGGER_RISING
, DRVNAME
,
1144 dev_err(&spi
->dev
, "Unable to get irq %x",
1145 gpio_to_irq(ifx_dev
->gpio
.srdy
));
1149 /* set pm runtime power state and register with power system */
1150 pm_runtime_set_active(&spi
->dev
);
1151 pm_runtime_enable(&spi
->dev
);
1153 /* handle case that modem is already signaling SRDY */
1154 /* no outgoing tty open at this point, this just satisfies the
1155 * modem's read and should reset communication properly
1157 srdy
= gpio_get_value(ifx_dev
->gpio
.srdy
);
1160 mrdy_assert(ifx_dev
);
1161 ifx_spi_handle_srdy(ifx_dev
);
1163 mrdy_set_low(ifx_dev
);
1167 free_irq(gpio_to_irq(ifx_dev
->gpio
.reset_out
), (void *)ifx_dev
);
1169 gpio_free(ifx_dev
->gpio
.srdy
);
1171 gpio_free(ifx_dev
->gpio
.mrdy
);
1173 gpio_free(ifx_dev
->gpio
.reset
);
1175 gpio_free(ifx_dev
->gpio
.po
);
1177 gpio_free(ifx_dev
->gpio
.reset_out
);
1179 ifx_spi_free_device(ifx_dev
);
1180 saved_ifx_dev
= NULL
;
1185 * ifx_spi_spi_remove - SPI device was removed
1188 * FIXME: We should be shutting the device down here not in
1189 * the module unload path.
1192 static int ifx_spi_spi_remove(struct spi_device
*spi
)
1194 struct ifx_spi_device
*ifx_dev
= spi_get_drvdata(spi
);
1196 tasklet_kill(&ifx_dev
->io_work_tasklet
);
1198 free_irq(gpio_to_irq(ifx_dev
->gpio
.reset_out
), (void *)ifx_dev
);
1199 free_irq(gpio_to_irq(ifx_dev
->gpio
.srdy
), (void *)ifx_dev
);
1201 gpio_free(ifx_dev
->gpio
.srdy
);
1202 gpio_free(ifx_dev
->gpio
.mrdy
);
1203 gpio_free(ifx_dev
->gpio
.reset
);
1204 gpio_free(ifx_dev
->gpio
.po
);
1205 gpio_free(ifx_dev
->gpio
.reset_out
);
1207 /* free allocations */
1208 ifx_spi_free_device(ifx_dev
);
1210 saved_ifx_dev
= NULL
;
1215 * ifx_spi_spi_shutdown - called on SPI shutdown
1218 * No action needs to be taken here
1221 static void ifx_spi_spi_shutdown(struct spi_device
*spi
)
1226 * various suspends and resumes have nothing to do
1227 * no hardware to save state for
1231 * ifx_spi_spi_suspend - suspend SPI on system suspend
1232 * @dev: device being suspended
1234 * Suspend the SPI side. No action needed on Intel MID platforms, may
1235 * need extending for other systems.
1237 static int ifx_spi_spi_suspend(struct spi_device
*spi
, pm_message_t msg
)
1243 * ifx_spi_spi_resume - resume SPI side on system resume
1244 * @dev: device being suspended
1246 * Suspend the SPI side. No action needed on Intel MID platforms, may
1247 * need extending for other systems.
1249 static int ifx_spi_spi_resume(struct spi_device
*spi
)
1255 * ifx_spi_pm_suspend - suspend modem on system suspend
1256 * @dev: device being suspended
1258 * Suspend the modem. No action needed on Intel MID platforms, may
1259 * need extending for other systems.
1261 static int ifx_spi_pm_suspend(struct device
*dev
)
1267 * ifx_spi_pm_resume - resume modem on system resume
1268 * @dev: device being suspended
1270 * Allow the modem to resume. No action needed.
1272 * FIXME: do we need to reset anything here ?
1274 static int ifx_spi_pm_resume(struct device
*dev
)
1280 * ifx_spi_pm_runtime_resume - suspend modem
1281 * @dev: device being suspended
1283 * Allow the modem to resume. No action needed.
1285 static int ifx_spi_pm_runtime_resume(struct device
*dev
)
1291 * ifx_spi_pm_runtime_suspend - suspend modem
1292 * @dev: device being suspended
1294 * Allow the modem to suspend and thus suspend to continue up the
1297 static int ifx_spi_pm_runtime_suspend(struct device
*dev
)
1303 * ifx_spi_pm_runtime_idle - check if modem idle
1306 * Check conditions and queue runtime suspend if idle.
1308 static int ifx_spi_pm_runtime_idle(struct device
*dev
)
1310 struct spi_device
*spi
= to_spi_device(dev
);
1311 struct ifx_spi_device
*ifx_dev
= spi_get_drvdata(spi
);
1313 if (!ifx_dev
->power_status
)
1314 pm_runtime_suspend(dev
);
1319 static const struct dev_pm_ops ifx_spi_pm
= {
1320 .resume
= ifx_spi_pm_resume
,
1321 .suspend
= ifx_spi_pm_suspend
,
1322 .runtime_resume
= ifx_spi_pm_runtime_resume
,
1323 .runtime_suspend
= ifx_spi_pm_runtime_suspend
,
1324 .runtime_idle
= ifx_spi_pm_runtime_idle
1327 static const struct spi_device_id ifx_id_table
[] = {
1332 MODULE_DEVICE_TABLE(spi
, ifx_id_table
);
1334 /* spi operations */
1335 static const struct spi_driver ifx_spi_driver
= {
1339 .owner
= THIS_MODULE
},
1340 .probe
= ifx_spi_spi_probe
,
1341 .shutdown
= ifx_spi_spi_shutdown
,
1342 .remove
= __devexit_p(ifx_spi_spi_remove
),
1343 .suspend
= ifx_spi_spi_suspend
,
1344 .resume
= ifx_spi_spi_resume
,
1345 .id_table
= ifx_id_table
1349 * ifx_spi_exit - module exit
1351 * Unload the module.
1354 static void __exit
ifx_spi_exit(void)
1357 tty_unregister_driver(tty_drv
);
1358 spi_unregister_driver((void *)&ifx_spi_driver
);
1362 * ifx_spi_init - module entry point
1364 * Initialise the SPI and tty interfaces for the IFX SPI driver
1365 * We need to initialize upper-edge spi driver after the tty
1366 * driver because otherwise the spi probe will race
1369 static int __init
ifx_spi_init(void)
1373 tty_drv
= alloc_tty_driver(1);
1375 pr_err("%s: alloc_tty_driver failed", DRVNAME
);
1379 tty_drv
->driver_name
= DRVNAME
;
1380 tty_drv
->name
= TTYNAME
;
1381 tty_drv
->minor_start
= IFX_SPI_TTY_ID
;
1382 tty_drv
->type
= TTY_DRIVER_TYPE_SERIAL
;
1383 tty_drv
->subtype
= SERIAL_TYPE_NORMAL
;
1384 tty_drv
->flags
= TTY_DRIVER_REAL_RAW
| TTY_DRIVER_DYNAMIC_DEV
;
1385 tty_drv
->init_termios
= tty_std_termios
;
1387 tty_set_operations(tty_drv
, &ifx_spi_serial_ops
);
1389 result
= tty_register_driver(tty_drv
);
1391 pr_err("%s: tty_register_driver failed(%d)",
1393 put_tty_driver(tty_drv
);
1397 result
= spi_register_driver((void *)&ifx_spi_driver
);
1399 pr_err("%s: spi_register_driver failed(%d)",
1401 tty_unregister_driver(tty_drv
);
1406 module_init(ifx_spi_init
);
1407 module_exit(ifx_spi_exit
);
1409 MODULE_AUTHOR("Intel");
1410 MODULE_DESCRIPTION("IFX6x60 spi driver");
1411 MODULE_LICENSE("GPL");
1412 MODULE_INFO(Version
, "0.1-IFX6x60");