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 4 /* bit position in u8 */
68 #define IFX_SPI_CTS_BIT 6 /* bit position in u8 */
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_8(unsigned char *buf, int len, void *end)
164 /* don't swap buffer if SPI word width is 8 bits */
171 * @len : number of bytes (not words) in the buffer
172 * @end: end of buffer
174 * Swap the contents of a buffer into big endian format
176 static inline void swap_buf_16(unsigned char *buf, int len, void *end)
180 u16 *buf_16 = (u16 *)buf;
181 len = ((len + 1) >> 1);
182 if ((void *)&buf_16[len] > end) {
183 pr_err("swap_buf_16: swap exceeds boundary (%p > %p)!",
187 for (n = 0; n < len; n++) {
188 *buf_16 = cpu_to_be16(*buf_16);
196 * @len : number of bytes (not words) in the buffer
197 * @end: end of buffer
199 * Swap the contents of a buffer into big endian format
201 static inline void swap_buf_32(unsigned char *buf, int len, void *end)
205 u32 *buf_32 = (u32 *)buf;
206 len = (len + 3) >> 2;
208 if ((void *)&buf_32[len] > end) {
209 pr_err("swap_buf_32: swap exceeds boundary (%p > %p)!\n",
213 for (n = 0; n < len; n++) {
214 *buf_32 = cpu_to_be32(*buf_32);
220 * mrdy_assert - assert MRDY line
221 * @ifx_dev: our SPI device
223 * Assert mrdy and set timer to wait for SRDY interrupt, if SRDY is low
226 * FIXME: Can SRDY even go high as we are running this code ?
228 static void mrdy_assert(struct ifx_spi_device *ifx_dev)
230 int val = gpio_get_value(ifx_dev->gpio.srdy);
232 if (!test_and_set_bit(IFX_SPI_STATE_TIMER_PENDING,
234 mod_timer(&ifx_dev->spi_timer,jiffies + IFX_SPI_TIMEOUT_SEC*HZ);
238 ifx_spi_power_state_set(ifx_dev, IFX_SPI_POWER_DATA_PENDING);
239 mrdy_set_high(ifx_dev);
243 * ifx_spi_hangup - hang up an IFX device
244 * @ifx_dev: our SPI device
246 * Hang up the tty attached to the IFX device if one is currently
247 * open. If not take no action
249 static void ifx_spi_ttyhangup(struct ifx_spi_device *ifx_dev)
251 struct tty_port *pport = &ifx_dev->tty_port;
252 struct tty_struct *tty = tty_port_tty_get(pport);
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(unsigned long arg)
268 struct ifx_spi_device *ifx_dev = (struct ifx_spi_device *)arg;
270 dev_warn(&ifx_dev->spi_dev->dev, "*** SPI Timeout ***");
271 ifx_spi_ttyhangup(ifx_dev);
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) {
385 return IFX_SPI_HEADER_0;
386 } else if (h1 == 0xffff && h2 == 0xffff) {
387 /* spi_slave_cts remains as it was */
388 return IFX_SPI_HEADER_F;
391 *length = h1 & 0xfff; /* upper bits of byte are flags */
392 *more = (buffer[1] >> IFX_SPI_MORE_BIT) & 1;
393 *received_cts = (buffer[3] >> IFX_SPI_CTS_BIT) & 1;
398 * ifx_setup_spi_header - set header fields
399 * @txbuffer: pointer to start of SPI buffer
401 * @more: indicate if more to follow
403 * Format up an SPI header for a transfer
407 static void ifx_spi_setup_spi_header(unsigned char *txbuffer, int tx_count,
410 *(u16 *)(txbuffer) = tx_count;
411 *(u16 *)(txbuffer+2) = IFX_SPI_PAYLOAD_SIZE;
412 txbuffer[1] |= (more << IFX_SPI_MORE_BIT) & IFX_SPI_MORE_MASK;
416 * ifx_spi_wakeup_serial - SPI space made
417 * @port_data: our SPI device
419 * We have emptied the FIFO enough that we want to get more data
420 * queued into it. Poke the line discipline via tty_wakeup so that
421 * it will feed us more bits
423 static void ifx_spi_wakeup_serial(struct ifx_spi_device *ifx_dev)
425 struct tty_struct *tty;
427 tty = tty_port_tty_get(&ifx_dev->tty_port);
435 * ifx_spi_prepare_tx_buffer - prepare transmit frame
436 * @ifx_dev: our SPI device
438 * The transmit buffr needs a header and various other bits of
439 * information followed by as much data as we can pull from the FIFO
440 * and transfer. This function formats up a suitable buffer in the
443 * FIXME: performance - should we wake the tty when the queue is half
446 static int ifx_spi_prepare_tx_buffer(struct ifx_spi_device *ifx_dev)
451 unsigned char *tx_buffer;
453 tx_buffer = ifx_dev->tx_buffer;
454 memset(tx_buffer, 0, IFX_SPI_TRANSFER_SIZE);
456 /* make room for required SPI header */
457 tx_buffer += IFX_SPI_HEADER_OVERHEAD;
458 tx_count = IFX_SPI_HEADER_OVERHEAD;
460 /* clear to signal no more data if this turns out to be the
461 * last buffer sent in a sequence */
462 ifx_dev->spi_more = 0;
464 /* if modem cts is set, just send empty buffer */
465 if (!ifx_dev->spi_slave_cts) {
466 /* see if there's tx data */
467 queue_length = kfifo_len(&ifx_dev->tx_fifo);
468 if (queue_length != 0) {
469 /* data to mux -- see if there's room for it */
470 temp_count = min(queue_length, IFX_SPI_PAYLOAD_SIZE);
471 temp_count = kfifo_out_locked(&ifx_dev->tx_fifo,
472 tx_buffer, temp_count,
473 &ifx_dev->fifo_lock);
475 /* update buffer pointer and data count in message */
476 tx_buffer += temp_count;
477 tx_count += temp_count;
478 if (temp_count == queue_length)
479 /* poke port to get more data */
480 ifx_spi_wakeup_serial(ifx_dev);
481 else /* more data in port, use next SPI message */
482 ifx_dev->spi_more = 1;
485 /* have data and info for header -- set up SPI header in buffer */
486 /* spi header needs payload size, not entire buffer size */
487 ifx_spi_setup_spi_header(ifx_dev->tx_buffer,
488 tx_count-IFX_SPI_HEADER_OVERHEAD,
490 /* swap actual data in the buffer */
491 ifx_dev->swap_buf((ifx_dev->tx_buffer), tx_count,
492 &ifx_dev->tx_buffer[IFX_SPI_TRANSFER_SIZE]);
497 * ifx_spi_write - line discipline write
498 * @tty: our tty device
499 * @buf: pointer to buffer to write (kernel space)
500 * @count: size of buffer
502 * Write the characters we have been given into the FIFO. If the device
503 * is not active then activate it, when the SRDY line is asserted back
504 * this will commence I/O
506 static int ifx_spi_write(struct tty_struct *tty, const unsigned char *buf,
509 struct ifx_spi_device *ifx_dev = tty->driver_data;
510 unsigned char *tmp_buf = (unsigned char *)buf;
514 spin_lock_irqsave(&ifx_dev->fifo_lock, flags);
515 is_fifo_empty = kfifo_is_empty(&ifx_dev->tx_fifo);
516 int tx_count = kfifo_in(&ifx_dev->tx_fifo, tmp_buf, count);
517 spin_unlock_irqrestore(&ifx_dev->fifo_lock, flags);
519 mrdy_assert(ifx_dev);
525 * ifx_spi_chars_in_buffer - line discipline helper
526 * @tty: our tty device
528 * Report how much data we can accept before we drop bytes. As we use
529 * a simple FIFO this is nice and easy.
531 static int ifx_spi_write_room(struct tty_struct *tty)
533 struct ifx_spi_device *ifx_dev = tty->driver_data;
534 return IFX_SPI_FIFO_SIZE - kfifo_len(&ifx_dev->tx_fifo);
538 * ifx_spi_chars_in_buffer - line discipline helper
539 * @tty: our tty device
541 * Report how many characters we have buffered. In our case this is the
542 * number of bytes sitting in our transmit FIFO.
544 static int ifx_spi_chars_in_buffer(struct tty_struct *tty)
546 struct ifx_spi_device *ifx_dev = tty->driver_data;
547 return kfifo_len(&ifx_dev->tx_fifo);
552 * @port: our tty port
554 * tty port hang up. Called when tty_hangup processing is invoked either
555 * by loss of carrier, or by software (eg vhangup). Serialized against
556 * activate/shutdown by the tty layer.
558 static void ifx_spi_hangup(struct tty_struct *tty)
560 struct ifx_spi_device *ifx_dev = tty->driver_data;
561 tty_port_hangup(&ifx_dev->tty_port);
566 * @port: our tty port
568 * tty port activate method - called for first open. Serialized
569 * with hangup and shutdown by the tty layer.
571 static int ifx_port_activate(struct tty_port *port, struct tty_struct *tty)
573 struct ifx_spi_device *ifx_dev =
574 container_of(port, struct ifx_spi_device, tty_port);
576 /* clear any old data; can't do this in 'close' */
577 kfifo_reset(&ifx_dev->tx_fifo);
579 /* clear any flag which may be set in port shutdown procedure */
580 clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &ifx_dev->flags);
581 clear_bit(IFX_SPI_STATE_IO_READY, &ifx_dev->flags);
583 /* put port data into this tty */
584 tty->driver_data = ifx_dev;
586 /* allows flip string push from int context */
587 tty->low_latency = 1;
589 /* set flag to allows data transfer */
590 set_bit(IFX_SPI_STATE_IO_AVAILABLE, &ifx_dev->flags);
597 * @port: our tty port
599 * tty port shutdown method - called for last port close. Serialized
600 * with hangup and activate by the tty layer.
602 static void ifx_port_shutdown(struct tty_port *port)
604 struct ifx_spi_device *ifx_dev =
605 container_of(port, struct ifx_spi_device, tty_port);
607 clear_bit(IFX_SPI_STATE_IO_AVAILABLE, &ifx_dev->flags);
608 mrdy_set_low(ifx_dev);
609 clear_bit(IFX_SPI_STATE_TIMER_PENDING, &ifx_dev->flags);
610 tasklet_kill(&ifx_dev->io_work_tasklet);
613 static const struct tty_port_operations ifx_tty_port_ops = {
614 .activate = ifx_port_activate,
615 .shutdown = ifx_port_shutdown,
618 static const struct tty_operations ifx_spi_serial_ops = {
619 .open = ifx_spi_open,
620 .close = ifx_spi_close,
621 .write = ifx_spi_write,
622 .hangup = ifx_spi_hangup,
623 .write_room = ifx_spi_write_room,
624 .chars_in_buffer = ifx_spi_chars_in_buffer,
625 .tiocmget = ifx_spi_tiocmget,
626 .tiocmset = ifx_spi_tiocmset,
630 * ifx_spi_insert_fip_string - queue received data
631 * @ifx_ser: our SPI device
632 * @chars: buffer we have received
633 * @size: number of chars reeived
635 * Queue bytes to the tty assuming the tty side is currently open. If
636 * not the discard the data.
638 static void ifx_spi_insert_flip_string(struct ifx_spi_device *ifx_dev,
639 unsigned char *chars, size_t size)
641 struct tty_struct *tty = tty_port_tty_get(&ifx_dev->tty_port);
644 tty_insert_flip_string(tty, chars, size);
645 tty_flip_buffer_push(tty);
650 * ifx_spi_complete - SPI transfer completed
651 * @ctx: our SPI device
653 * An SPI transfer has completed. Process any received data and kick off
654 * any further transmits we can commence.
656 static void ifx_spi_complete(void *ctx)
658 struct ifx_spi_device *ifx_dev = ctx;
659 struct tty_struct *tty;
660 struct tty_ldisc *ldisc = NULL;
665 int local_write_pending = 0;
670 mrdy_set_low(ifx_dev);
672 if (!ifx_dev->spi_msg.status) {
673 /* check header validity, get comm flags */
674 ifx_dev->swap_buf(ifx_dev->rx_buffer, IFX_SPI_HEADER_OVERHEAD,
675 &ifx_dev->rx_buffer[IFX_SPI_HEADER_OVERHEAD]);
676 decode_result = ifx_spi_decode_spi_header(ifx_dev->rx_buffer,
677 &length, &more, &cts);
678 if (decode_result == IFX_SPI_HEADER_0) {
679 dev_dbg(&ifx_dev->spi_dev->dev,
680 "ignore input: invalid header 0");
681 ifx_dev->spi_slave_cts = 0;
683 } else if (decode_result == IFX_SPI_HEADER_F) {
684 dev_dbg(&ifx_dev->spi_dev->dev,
685 "ignore input: invalid header F");
689 ifx_dev->spi_slave_cts = cts;
691 actual_length = min((unsigned int)length,
692 ifx_dev->spi_msg.actual_length);
694 (ifx_dev->rx_buffer + IFX_SPI_HEADER_OVERHEAD),
696 &ifx_dev->rx_buffer[IFX_SPI_TRANSFER_SIZE]);
697 ifx_spi_insert_flip_string(
699 ifx_dev->rx_buffer + IFX_SPI_HEADER_OVERHEAD,
700 (size_t)actual_length);
702 dev_dbg(&ifx_dev->spi_dev->dev, "SPI transfer error %d",
703 ifx_dev->spi_msg.status);
707 if (ifx_dev->write_pending) {
708 ifx_dev->write_pending = 0;
709 local_write_pending = 1;
712 clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &(ifx_dev->flags));
714 queue_length = kfifo_len(&ifx_dev->tx_fifo);
715 srdy = gpio_get_value(ifx_dev->gpio.srdy);
717 ifx_spi_power_state_clear(ifx_dev, IFX_SPI_POWER_SRDY);
719 /* schedule output if there is more to do */
720 if (test_and_clear_bit(IFX_SPI_STATE_IO_READY, &ifx_dev->flags))
721 tasklet_schedule(&ifx_dev->io_work_tasklet);
723 if (more || ifx_dev->spi_more || queue_length > 0 ||
724 local_write_pending) {
725 if (ifx_dev->spi_slave_cts) {
727 mrdy_assert(ifx_dev);
729 mrdy_assert(ifx_dev);
732 * poke line discipline driver if any for more data
733 * may or may not get more data to write
734 * for now, say not busy
736 ifx_spi_power_state_clear(ifx_dev,
737 IFX_SPI_POWER_DATA_PENDING);
738 tty = tty_port_tty_get(&ifx_dev->tty_port);
740 ldisc = tty_ldisc_ref(tty);
742 ldisc->ops->write_wakeup(tty);
743 tty_ldisc_deref(ldisc);
752 * ifx_spio_io - I/O tasklet
753 * @data: our SPI device
755 * Queue data for transmission if possible and then kick off the
758 static void ifx_spi_io(unsigned long data)
761 struct ifx_spi_device *ifx_dev = (struct ifx_spi_device *) data;
763 if (!test_and_set_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &ifx_dev->flags) &&
764 test_bit(IFX_SPI_STATE_IO_AVAILABLE, &ifx_dev->flags)) {
765 if (ifx_dev->gpio.unack_srdy_int_nb > 0)
766 ifx_dev->gpio.unack_srdy_int_nb--;
768 ifx_spi_prepare_tx_buffer(ifx_dev);
770 spi_message_init(&ifx_dev->spi_msg);
771 INIT_LIST_HEAD(&ifx_dev->spi_msg.queue);
773 ifx_dev->spi_msg.context = ifx_dev;
774 ifx_dev->spi_msg.complete = ifx_spi_complete;
776 /* set up our spi transfer */
777 /* note len is BYTES, not transfers */
778 ifx_dev->spi_xfer.len = IFX_SPI_TRANSFER_SIZE;
779 ifx_dev->spi_xfer.cs_change = 0;
780 ifx_dev->spi_xfer.speed_hz = ifx_dev->spi_dev->max_speed_hz;
781 /* ifx_dev->spi_xfer.speed_hz = 390625; */
782 ifx_dev->spi_xfer.bits_per_word = spi_bpw;
784 ifx_dev->spi_xfer.tx_buf = ifx_dev->tx_buffer;
785 ifx_dev->spi_xfer.rx_buf = ifx_dev->rx_buffer;
790 if (ifx_dev->use_dma) {
791 ifx_dev->spi_msg.is_dma_mapped = 1;
792 ifx_dev->tx_dma = ifx_dev->tx_bus;
793 ifx_dev->rx_dma = ifx_dev->rx_bus;
794 ifx_dev->spi_xfer.tx_dma = ifx_dev->tx_dma;
795 ifx_dev->spi_xfer.rx_dma = ifx_dev->rx_dma;
797 ifx_dev->spi_msg.is_dma_mapped = 0;
798 ifx_dev->tx_dma = (dma_addr_t)0;
799 ifx_dev->rx_dma = (dma_addr_t)0;
800 ifx_dev->spi_xfer.tx_dma = (dma_addr_t)0;
801 ifx_dev->spi_xfer.rx_dma = (dma_addr_t)0;
804 spi_message_add_tail(&ifx_dev->spi_xfer, &ifx_dev->spi_msg);
806 /* Assert MRDY. This may have already been done by the write
809 mrdy_assert(ifx_dev);
811 retval = spi_async(ifx_dev->spi_dev, &ifx_dev->spi_msg);
813 clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS,
815 tasklet_schedule(&ifx_dev->io_work_tasklet);
819 ifx_dev->write_pending = 1;
823 * ifx_spi_free_port - free up the tty side
824 * @ifx_dev: IFX device going away
826 * Unregister and free up a port when the device goes away
828 static void ifx_spi_free_port(struct ifx_spi_device *ifx_dev)
830 if (ifx_dev->tty_dev)
831 tty_unregister_device(tty_drv, ifx_dev->minor);
832 tty_port_destroy(&ifx_dev->tty_port);
833 kfifo_free(&ifx_dev->tx_fifo);
837 * ifx_spi_create_port - create a new port
838 * @ifx_dev: our spi device
840 * Allocate and initialise the tty port that goes with this interface
841 * and add it to the tty layer so that it can be opened.
843 static int ifx_spi_create_port(struct ifx_spi_device *ifx_dev)
846 struct tty_port *pport = &ifx_dev->tty_port;
848 spin_lock_init(&ifx_dev->fifo_lock);
849 lockdep_set_class_and_subclass(&ifx_dev->fifo_lock,
852 if (kfifo_alloc(&ifx_dev->tx_fifo, IFX_SPI_FIFO_SIZE, GFP_KERNEL)) {
857 tty_port_init(pport);
858 pport->ops = &ifx_tty_port_ops;
859 ifx_dev->minor = IFX_SPI_TTY_ID;
860 ifx_dev->tty_dev = tty_port_register_device(pport, tty_drv,
861 ifx_dev->minor, &ifx_dev->spi_dev->dev);
862 if (IS_ERR(ifx_dev->tty_dev)) {
863 dev_dbg(&ifx_dev->spi_dev->dev,
864 "%s: registering tty device failed", __func__);
865 ret = PTR_ERR(ifx_dev->tty_dev);
871 tty_port_destroy(pport);
873 ifx_spi_free_port(ifx_dev);
878 * ifx_spi_handle_srdy - handle SRDY
879 * @ifx_dev: device asserting SRDY
881 * Check our device state and see what we need to kick off when SRDY
882 * is asserted. This usually means killing the timer and firing off the
885 static void ifx_spi_handle_srdy(struct ifx_spi_device *ifx_dev)
887 if (test_bit(IFX_SPI_STATE_TIMER_PENDING, &ifx_dev->flags)) {
888 del_timer(&ifx_dev->spi_timer);
889 clear_bit(IFX_SPI_STATE_TIMER_PENDING, &ifx_dev->flags);
892 ifx_spi_power_state_set(ifx_dev, IFX_SPI_POWER_SRDY);
894 if (!test_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &ifx_dev->flags))
895 tasklet_schedule(&ifx_dev->io_work_tasklet);
897 set_bit(IFX_SPI_STATE_IO_READY, &ifx_dev->flags);
901 * ifx_spi_srdy_interrupt - SRDY asserted
902 * @irq: our IRQ number
903 * @dev: our ifx device
905 * The modem asserted SRDY. Handle the srdy event
907 static irqreturn_t ifx_spi_srdy_interrupt(int irq, void *dev)
909 struct ifx_spi_device *ifx_dev = dev;
910 ifx_dev->gpio.unack_srdy_int_nb++;
911 ifx_spi_handle_srdy(ifx_dev);
916 * ifx_spi_reset_interrupt - Modem has changed reset state
917 * @irq: interrupt number
918 * @dev: our device pointer
920 * The modem has either entered or left reset state. Check the GPIO
923 * FIXME: review locking on MR_INPROGRESS versus
924 * parallel unsolicited reset/solicited reset
926 static irqreturn_t ifx_spi_reset_interrupt(int irq, void *dev)
928 struct ifx_spi_device *ifx_dev = dev;
929 int val = gpio_get_value(ifx_dev->gpio.reset_out);
930 int solreset = test_bit(MR_START, &ifx_dev->mdm_reset_state);
934 set_bit(MR_INPROGRESS, &ifx_dev->mdm_reset_state);
936 /* unsolicited reset */
937 ifx_spi_ttyhangup(ifx_dev);
941 clear_bit(MR_INPROGRESS, &ifx_dev->mdm_reset_state);
943 set_bit(MR_COMPLETE, &ifx_dev->mdm_reset_state);
944 wake_up(&ifx_dev->mdm_reset_wait);
951 * ifx_spi_free_device - free device
952 * @ifx_dev: device to free
954 * Free the IFX device
956 static void ifx_spi_free_device(struct ifx_spi_device *ifx_dev)
958 ifx_spi_free_port(ifx_dev);
959 dma_free_coherent(&ifx_dev->spi_dev->dev,
960 IFX_SPI_TRANSFER_SIZE,
963 dma_free_coherent(&ifx_dev->spi_dev->dev,
964 IFX_SPI_TRANSFER_SIZE,
970 * ifx_spi_reset - reset modem
971 * @ifx_dev: modem to reset
973 * Perform a reset on the modem
975 static int ifx_spi_reset(struct ifx_spi_device *ifx_dev)
979 * set up modem power, reset
981 * delays are required on some platforms for the modem
984 set_bit(MR_START, &ifx_dev->mdm_reset_state);
985 gpio_set_value(ifx_dev->gpio.po, 0);
986 gpio_set_value(ifx_dev->gpio.reset, 0);
988 gpio_set_value(ifx_dev->gpio.reset, 1);
990 gpio_set_value(ifx_dev->gpio.po, 1);
992 gpio_set_value(ifx_dev->gpio.po, 0);
993 ret = wait_event_timeout(ifx_dev->mdm_reset_wait,
994 test_bit(MR_COMPLETE,
995 &ifx_dev->mdm_reset_state),
998 dev_warn(&ifx_dev->spi_dev->dev, "Modem reset timeout: (state:%lx)",
999 ifx_dev->mdm_reset_state);
1001 ifx_dev->mdm_reset_state = 0;
1006 * ifx_spi_spi_probe - probe callback
1007 * @spi: our possible matching SPI device
1009 * Probe for a 6x60 modem on SPI bus. Perform any needed device and
1013 * - Support for multiple devices
1014 * - Split out MID specific GPIO handling eventually
1017 static int ifx_spi_spi_probe(struct spi_device *spi)
1021 struct ifx_modem_platform_data *pl_data;
1022 struct ifx_spi_device *ifx_dev;
1024 if (saved_ifx_dev) {
1025 dev_dbg(&spi->dev, "ignoring subsequent detection");
1029 pl_data = (struct ifx_modem_platform_data *)spi->dev.platform_data;
1031 dev_err(&spi->dev, "missing platform data!");
1035 /* initialize structure to hold our device variables */
1036 ifx_dev = kzalloc(sizeof(struct ifx_spi_device), GFP_KERNEL);
1038 dev_err(&spi->dev, "spi device allocation failed");
1041 saved_ifx_dev = ifx_dev;
1042 ifx_dev->spi_dev = spi;
1043 clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &ifx_dev->flags);
1044 spin_lock_init(&ifx_dev->write_lock);
1045 spin_lock_init(&ifx_dev->power_lock);
1046 ifx_dev->power_status = 0;
1047 init_timer(&ifx_dev->spi_timer);
1048 ifx_dev->spi_timer.function = ifx_spi_timeout;
1049 ifx_dev->spi_timer.data = (unsigned long)ifx_dev;
1050 ifx_dev->modem = pl_data->modem_type;
1051 ifx_dev->use_dma = pl_data->use_dma;
1052 ifx_dev->max_hz = pl_data->max_hz;
1053 /* initialize spi mode, etc */
1054 spi->max_speed_hz = ifx_dev->max_hz;
1055 spi->mode = IFX_SPI_MODE | (SPI_LOOP & spi->mode);
1056 spi->bits_per_word = spi_bpw;
1057 ret = spi_setup(spi);
1059 dev_err(&spi->dev, "SPI setup wasn't successful %d", ret);
1063 /* init swap_buf function according to word width configuration */
1064 if (spi->bits_per_word == 32)
1065 ifx_dev->swap_buf = swap_buf_32;
1066 else if (spi->bits_per_word == 16)
1067 ifx_dev->swap_buf = swap_buf_16;
1069 ifx_dev->swap_buf = swap_buf_8;
1071 /* ensure SPI protocol flags are initialized to enable transfer */
1072 ifx_dev->spi_more = 0;
1073 ifx_dev->spi_slave_cts = 0;
1075 /*initialize transfer and dma buffers */
1076 ifx_dev->tx_buffer = dma_alloc_coherent(ifx_dev->spi_dev->dev.parent,
1077 IFX_SPI_TRANSFER_SIZE,
1080 if (!ifx_dev->tx_buffer) {
1081 dev_err(&spi->dev, "DMA-TX buffer allocation failed");
1085 ifx_dev->rx_buffer = dma_alloc_coherent(ifx_dev->spi_dev->dev.parent,
1086 IFX_SPI_TRANSFER_SIZE,
1089 if (!ifx_dev->rx_buffer) {
1090 dev_err(&spi->dev, "DMA-RX buffer allocation failed");
1095 /* initialize waitq for modem reset */
1096 init_waitqueue_head(&ifx_dev->mdm_reset_wait);
1098 spi_set_drvdata(spi, ifx_dev);
1099 tasklet_init(&ifx_dev->io_work_tasklet, ifx_spi_io,
1100 (unsigned long)ifx_dev);
1102 set_bit(IFX_SPI_STATE_PRESENT, &ifx_dev->flags);
1104 /* create our tty port */
1105 ret = ifx_spi_create_port(ifx_dev);
1107 dev_err(&spi->dev, "create default tty port failed");
1111 ifx_dev->gpio.reset = pl_data->rst_pmu;
1112 ifx_dev->gpio.po = pl_data->pwr_on;
1113 ifx_dev->gpio.mrdy = pl_data->mrdy;
1114 ifx_dev->gpio.srdy = pl_data->srdy;
1115 ifx_dev->gpio.reset_out = pl_data->rst_out;
1117 dev_info(&spi->dev, "gpios %d, %d, %d, %d, %d",
1118 ifx_dev->gpio.reset, ifx_dev->gpio.po, ifx_dev->gpio.mrdy,
1119 ifx_dev->gpio.srdy, ifx_dev->gpio.reset_out);
1121 /* Configure gpios */
1122 ret = gpio_request(ifx_dev->gpio.reset, "ifxModem");
1124 dev_err(&spi->dev, "Unable to allocate GPIO%d (RESET)",
1125 ifx_dev->gpio.reset);
1128 ret += gpio_direction_output(ifx_dev->gpio.reset, 0);
1129 ret += gpio_export(ifx_dev->gpio.reset, 1);
1131 dev_err(&spi->dev, "Unable to configure GPIO%d (RESET)",
1132 ifx_dev->gpio.reset);
1137 ret = gpio_request(ifx_dev->gpio.po, "ifxModem");
1138 ret += gpio_direction_output(ifx_dev->gpio.po, 0);
1139 ret += gpio_export(ifx_dev->gpio.po, 1);
1141 dev_err(&spi->dev, "Unable to configure GPIO%d (ON)",
1147 ret = gpio_request(ifx_dev->gpio.mrdy, "ifxModem");
1149 dev_err(&spi->dev, "Unable to allocate GPIO%d (MRDY)",
1150 ifx_dev->gpio.mrdy);
1153 ret += gpio_export(ifx_dev->gpio.mrdy, 1);
1154 ret += gpio_direction_output(ifx_dev->gpio.mrdy, 0);
1156 dev_err(&spi->dev, "Unable to configure GPIO%d (MRDY)",
1157 ifx_dev->gpio.mrdy);
1162 ret = gpio_request(ifx_dev->gpio.srdy, "ifxModem");
1164 dev_err(&spi->dev, "Unable to allocate GPIO%d (SRDY)",
1165 ifx_dev->gpio.srdy);
1169 ret += gpio_export(ifx_dev->gpio.srdy, 1);
1170 ret += gpio_direction_input(ifx_dev->gpio.srdy);
1172 dev_err(&spi->dev, "Unable to configure GPIO%d (SRDY)",
1173 ifx_dev->gpio.srdy);
1178 ret = gpio_request(ifx_dev->gpio.reset_out, "ifxModem");
1180 dev_err(&spi->dev, "Unable to allocate GPIO%d (RESET_OUT)",
1181 ifx_dev->gpio.reset_out);
1184 ret += gpio_export(ifx_dev->gpio.reset_out, 1);
1185 ret += gpio_direction_input(ifx_dev->gpio.reset_out);
1187 dev_err(&spi->dev, "Unable to configure GPIO%d (RESET_OUT)",
1188 ifx_dev->gpio.reset_out);
1193 ret = request_irq(gpio_to_irq(ifx_dev->gpio.reset_out),
1194 ifx_spi_reset_interrupt,
1195 IRQF_TRIGGER_RISING|IRQF_TRIGGER_FALLING, DRVNAME,
1198 dev_err(&spi->dev, "Unable to get irq %x\n",
1199 gpio_to_irq(ifx_dev->gpio.reset_out));
1203 ret = ifx_spi_reset(ifx_dev);
1205 ret = request_irq(gpio_to_irq(ifx_dev->gpio.srdy),
1206 ifx_spi_srdy_interrupt,
1207 IRQF_TRIGGER_RISING, DRVNAME,
1210 dev_err(&spi->dev, "Unable to get irq %x",
1211 gpio_to_irq(ifx_dev->gpio.srdy));
1215 /* set pm runtime power state and register with power system */
1216 pm_runtime_set_active(&spi->dev);
1217 pm_runtime_enable(&spi->dev);
1219 /* handle case that modem is already signaling SRDY */
1220 /* no outgoing tty open at this point, this just satisfies the
1221 * modem's read and should reset communication properly
1223 srdy = gpio_get_value(ifx_dev->gpio.srdy);
1226 mrdy_assert(ifx_dev);
1227 ifx_spi_handle_srdy(ifx_dev);
1229 mrdy_set_low(ifx_dev);
1233 free_irq(gpio_to_irq(ifx_dev->gpio.reset_out), (void *)ifx_dev);
1235 gpio_free(ifx_dev->gpio.srdy);
1237 gpio_free(ifx_dev->gpio.mrdy);
1239 gpio_free(ifx_dev->gpio.reset);
1241 gpio_free(ifx_dev->gpio.po);
1243 gpio_free(ifx_dev->gpio.reset_out);
1245 ifx_spi_free_device(ifx_dev);
1246 saved_ifx_dev = NULL;
1251 * ifx_spi_spi_remove - SPI device was removed
1254 * FIXME: We should be shutting the device down here not in
1255 * the module unload path.
1258 static int ifx_spi_spi_remove(struct spi_device *spi)
1260 struct ifx_spi_device *ifx_dev = spi_get_drvdata(spi);
1262 tasklet_kill(&ifx_dev->io_work_tasklet);
1264 free_irq(gpio_to_irq(ifx_dev->gpio.reset_out), (void *)ifx_dev);
1265 free_irq(gpio_to_irq(ifx_dev->gpio.srdy), (void *)ifx_dev);
1267 gpio_free(ifx_dev->gpio.srdy);
1268 gpio_free(ifx_dev->gpio.mrdy);
1269 gpio_free(ifx_dev->gpio.reset);
1270 gpio_free(ifx_dev->gpio.po);
1271 gpio_free(ifx_dev->gpio.reset_out);
1273 /* free allocations */
1274 ifx_spi_free_device(ifx_dev);
1276 saved_ifx_dev = NULL;
1281 * ifx_spi_spi_shutdown - called on SPI shutdown
1284 * No action needs to be taken here
1287 static void ifx_spi_spi_shutdown(struct spi_device *spi)
1292 * various suspends and resumes have nothing to do
1293 * no hardware to save state for
1297 * ifx_spi_spi_suspend - suspend SPI on system suspend
1298 * @dev: device being suspended
1300 * Suspend the SPI side. No action needed on Intel MID platforms, may
1301 * need extending for other systems.
1303 static int ifx_spi_spi_suspend(struct spi_device *spi, pm_message_t msg)
1309 * ifx_spi_spi_resume - resume SPI side on system resume
1310 * @dev: device being suspended
1312 * Suspend the SPI side. No action needed on Intel MID platforms, may
1313 * need extending for other systems.
1315 static int ifx_spi_spi_resume(struct spi_device *spi)
1321 * ifx_spi_pm_suspend - suspend modem on system suspend
1322 * @dev: device being suspended
1324 * Suspend the modem. No action needed on Intel MID platforms, may
1325 * need extending for other systems.
1327 static int ifx_spi_pm_suspend(struct device *dev)
1333 * ifx_spi_pm_resume - resume modem on system resume
1334 * @dev: device being suspended
1336 * Allow the modem to resume. No action needed.
1338 * FIXME: do we need to reset anything here ?
1340 static int ifx_spi_pm_resume(struct device *dev)
1346 * ifx_spi_pm_runtime_resume - suspend modem
1347 * @dev: device being suspended
1349 * Allow the modem to resume. No action needed.
1351 static int ifx_spi_pm_runtime_resume(struct device *dev)
1357 * ifx_spi_pm_runtime_suspend - suspend modem
1358 * @dev: device being suspended
1360 * Allow the modem to suspend and thus suspend to continue up the
1363 static int ifx_spi_pm_runtime_suspend(struct device *dev)
1369 * ifx_spi_pm_runtime_idle - check if modem idle
1372 * Check conditions and queue runtime suspend if idle.
1374 static int ifx_spi_pm_runtime_idle(struct device *dev)
1376 struct spi_device *spi = to_spi_device(dev);
1377 struct ifx_spi_device *ifx_dev = spi_get_drvdata(spi);
1379 if (!ifx_dev->power_status)
1380 pm_runtime_suspend(dev);
1385 static const struct dev_pm_ops ifx_spi_pm = {
1386 .resume = ifx_spi_pm_resume,
1387 .suspend = ifx_spi_pm_suspend,
1388 .runtime_resume = ifx_spi_pm_runtime_resume,
1389 .runtime_suspend = ifx_spi_pm_runtime_suspend,
1390 .runtime_idle = ifx_spi_pm_runtime_idle
1393 static const struct spi_device_id ifx_id_table[] = {
1398 MODULE_DEVICE_TABLE(spi, ifx_id_table);
1400 /* spi operations */
1401 static struct spi_driver ifx_spi_driver = {
1405 .owner = THIS_MODULE},
1406 .probe = ifx_spi_spi_probe,
1407 .shutdown = ifx_spi_spi_shutdown,
1408 .remove = ifx_spi_spi_remove,
1409 .suspend = ifx_spi_spi_suspend,
1410 .resume = ifx_spi_spi_resume,
1411 .id_table = ifx_id_table
1415 * ifx_spi_exit - module exit
1417 * Unload the module.
1420 static void __exit ifx_spi_exit(void)
1423 tty_unregister_driver(tty_drv);
1424 spi_unregister_driver((void *)&ifx_spi_driver);
1428 * ifx_spi_init - module entry point
1430 * Initialise the SPI and tty interfaces for the IFX SPI driver
1431 * We need to initialize upper-edge spi driver after the tty
1432 * driver because otherwise the spi probe will race
1435 static int __init ifx_spi_init(void)
1439 tty_drv = alloc_tty_driver(1);
1441 pr_err("%s: alloc_tty_driver failed", DRVNAME);
1445 tty_drv->driver_name = DRVNAME;
1446 tty_drv->name = TTYNAME;
1447 tty_drv->minor_start = IFX_SPI_TTY_ID;
1448 tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
1449 tty_drv->subtype = SERIAL_TYPE_NORMAL;
1450 tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1451 tty_drv->init_termios = tty_std_termios;
1453 tty_set_operations(tty_drv, &ifx_spi_serial_ops);
1455 result = tty_register_driver(tty_drv);
1457 pr_err("%s: tty_register_driver failed(%d)",
1459 put_tty_driver(tty_drv);
1463 result = spi_register_driver((void *)&ifx_spi_driver);
1465 pr_err("%s: spi_register_driver failed(%d)",
1467 tty_unregister_driver(tty_drv);
1472 module_init(ifx_spi_init);
1473 module_exit(ifx_spi_exit);
1475 MODULE_AUTHOR("Intel");
1476 MODULE_DESCRIPTION("IFX6x60 spi driver");
1477 MODULE_LICENSE("GPL");
1478 MODULE_INFO(Version, "0.1-IFX6x60");