]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/tty/serial/ifx6x60.c
TTY: add tty_port_tty_wakeup helper
[karo-tx-linux.git] / drivers / tty / serial / ifx6x60.c
1 /****************************************************************************
2  *
3  * Driver for the IFX 6x60 spi modem.
4  *
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>
9  *
10  * Copyright (C) 2009, 2010 Intel Corp
11  * Russ Gorby <russ.gorby@intel.com>
12  *
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.
16  *
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.
21  *
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,
25  * USA
26  *
27  * Driver modified by Intel from Option gtm501l_spi.c
28  *
29  * Notes
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.
37  *
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>
52 #include <linux/fs.h>
53 #include <linux/ip.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>
59 #include <linux/pm.h>
60 #include <linux/pm_runtime.h>
61 #include <linux/spi/ifx_modem.h>
62 #include <linux/delay.h>
63 #include <linux/reboot.h>
64
65 #include "ifx6x60.h"
66
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)
75
76 #define PO_POST_DELAY           200
77 #define IFX_MDM_RST_PMU 4
78
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);
84
85 /* local variables */
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;
90
91 static struct notifier_block ifx_modem_reboot_notifier_block = {
92         .notifier_call = ifx_modem_reboot_callback,
93 };
94
95 static int ifx_modem_power_off(struct ifx_spi_device *ifx_dev)
96 {
97         gpio_set_value(IFX_MDM_RST_PMU, 1);
98         msleep(PO_POST_DELAY);
99
100         return 0;
101 }
102
103 static int ifx_modem_reboot_callback(struct notifier_block *nfb,
104                                  unsigned long event, void *data)
105 {
106         if (saved_ifx_dev)
107                 ifx_modem_power_off(saved_ifx_dev);
108         else
109                 pr_warn("no ifx modem active;\n");
110
111         return NOTIFY_OK;
112 }
113
114 /* GPIO/GPE settings */
115
116 /**
117  *      mrdy_set_high           -       set MRDY GPIO
118  *      @ifx: device we are controlling
119  *
120  */
121 static inline void mrdy_set_high(struct ifx_spi_device *ifx)
122 {
123         gpio_set_value(ifx->gpio.mrdy, 1);
124 }
125
126 /**
127  *      mrdy_set_low            -       clear MRDY GPIO
128  *      @ifx: device we are controlling
129  *
130  */
131 static inline void mrdy_set_low(struct ifx_spi_device *ifx)
132 {
133         gpio_set_value(ifx->gpio.mrdy, 0);
134 }
135
136 /**
137  *      ifx_spi_power_state_set
138  *      @ifx_dev: our SPI device
139  *      @val: bits to set
140  *
141  *      Set bit in power status and signal power system if status becomes non-0
142  */
143 static void
144 ifx_spi_power_state_set(struct ifx_spi_device *ifx_dev, unsigned char val)
145 {
146         unsigned long flags;
147
148         spin_lock_irqsave(&ifx_dev->power_lock, flags);
149
150         /*
151          * if power status is already non-0, just update, else
152          * tell power system
153          */
154         if (!ifx_dev->power_status)
155                 pm_runtime_get(&ifx_dev->spi_dev->dev);
156         ifx_dev->power_status |= val;
157
158         spin_unlock_irqrestore(&ifx_dev->power_lock, flags);
159 }
160
161 /**
162  *      ifx_spi_power_state_clear       -       clear power bit
163  *      @ifx_dev: our SPI device
164  *      @val: bits to clear
165  *
166  *      clear bit in power status and signal power system if status becomes 0
167  */
168 static void
169 ifx_spi_power_state_clear(struct ifx_spi_device *ifx_dev, unsigned char val)
170 {
171         unsigned long flags;
172
173         spin_lock_irqsave(&ifx_dev->power_lock, flags);
174
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);
179         }
180
181         spin_unlock_irqrestore(&ifx_dev->power_lock, flags);
182 }
183
184 /**
185  *      swap_buf_8
186  *      @buf: our buffer
187  *      @len : number of bytes (not words) in the buffer
188  *      @end: end of buffer
189  *
190  *      Swap the contents of a buffer into big endian format
191  */
192 static inline void swap_buf_8(unsigned char *buf, int len, void *end)
193 {
194         /* don't swap buffer if SPI word width is 8 bits */
195         return;
196 }
197
198 /**
199  *      swap_buf_16
200  *      @buf: our buffer
201  *      @len : number of bytes (not words) in the buffer
202  *      @end: end of buffer
203  *
204  *      Swap the contents of a buffer into big endian format
205  */
206 static inline void swap_buf_16(unsigned char *buf, int len, void *end)
207 {
208         int n;
209
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)!",
214                        &buf_16[len], end);
215                 return;
216         }
217         for (n = 0; n < len; n++) {
218                 *buf_16 = cpu_to_be16(*buf_16);
219                 buf_16++;
220         }
221 }
222
223 /**
224  *      swap_buf_32
225  *      @buf: our buffer
226  *      @len : number of bytes (not words) in the buffer
227  *      @end: end of buffer
228  *
229  *      Swap the contents of a buffer into big endian format
230  */
231 static inline void swap_buf_32(unsigned char *buf, int len, void *end)
232 {
233         int n;
234
235         u32 *buf_32 = (u32 *)buf;
236         len = (len + 3) >> 2;
237
238         if ((void *)&buf_32[len] > end) {
239                 pr_err("swap_buf_32: swap exceeds boundary (%p > %p)!\n",
240                        &buf_32[len], end);
241                 return;
242         }
243         for (n = 0; n < len; n++) {
244                 *buf_32 = cpu_to_be32(*buf_32);
245                 buf_32++;
246         }
247 }
248
249 /**
250  *      mrdy_assert             -       assert MRDY line
251  *      @ifx_dev: our SPI device
252  *
253  *      Assert mrdy and set timer to wait for SRDY interrupt, if SRDY is low
254  *      now.
255  *
256  *      FIXME: Can SRDY even go high as we are running this code ?
257  */
258 static void mrdy_assert(struct ifx_spi_device *ifx_dev)
259 {
260         int val = gpio_get_value(ifx_dev->gpio.srdy);
261         if (!val) {
262                 if (!test_and_set_bit(IFX_SPI_STATE_TIMER_PENDING,
263                                       &ifx_dev->flags)) {
264                         mod_timer(&ifx_dev->spi_timer,jiffies + IFX_SPI_TIMEOUT_SEC*HZ);
265
266                 }
267         }
268         ifx_spi_power_state_set(ifx_dev, IFX_SPI_POWER_DATA_PENDING);
269         mrdy_set_high(ifx_dev);
270 }
271
272 /**
273  *      ifx_spi_hangup          -       hang up an IFX device
274  *      @ifx_dev: our SPI device
275  *
276  *      Hang up the tty attached to the IFX device if one is currently
277  *      open. If not take no action
278  */
279 static void ifx_spi_ttyhangup(struct ifx_spi_device *ifx_dev)
280 {
281         struct tty_port *pport = &ifx_dev->tty_port;
282         struct tty_struct *tty = tty_port_tty_get(pport);
283         if (tty) {
284                 tty_hangup(tty);
285                 tty_kref_put(tty);
286         }
287 }
288
289 /**
290  *      ifx_spi_timeout         -       SPI timeout
291  *      @arg: our SPI device
292  *
293  *      The SPI has timed out: hang up the tty. Users will then see a hangup
294  *      and error events.
295  */
296 static void ifx_spi_timeout(unsigned long arg)
297 {
298         struct ifx_spi_device *ifx_dev = (struct ifx_spi_device *)arg;
299
300         dev_warn(&ifx_dev->spi_dev->dev, "*** SPI Timeout ***");
301         ifx_spi_ttyhangup(ifx_dev);
302         mrdy_set_low(ifx_dev);
303         clear_bit(IFX_SPI_STATE_TIMER_PENDING, &ifx_dev->flags);
304 }
305
306 /* char/tty operations */
307
308 /**
309  *      ifx_spi_tiocmget        -       get modem lines
310  *      @tty: our tty device
311  *      @filp: file handle issuing the request
312  *
313  *      Map the signal state into Linux modem flags and report the value
314  *      in Linux terms
315  */
316 static int ifx_spi_tiocmget(struct tty_struct *tty)
317 {
318         unsigned int value;
319         struct ifx_spi_device *ifx_dev = tty->driver_data;
320
321         value =
322         (test_bit(IFX_SPI_RTS, &ifx_dev->signal_state) ? TIOCM_RTS : 0) |
323         (test_bit(IFX_SPI_DTR, &ifx_dev->signal_state) ? TIOCM_DTR : 0) |
324         (test_bit(IFX_SPI_CTS, &ifx_dev->signal_state) ? TIOCM_CTS : 0) |
325         (test_bit(IFX_SPI_DSR, &ifx_dev->signal_state) ? TIOCM_DSR : 0) |
326         (test_bit(IFX_SPI_DCD, &ifx_dev->signal_state) ? TIOCM_CAR : 0) |
327         (test_bit(IFX_SPI_RI, &ifx_dev->signal_state) ? TIOCM_RNG : 0);
328         return value;
329 }
330
331 /**
332  *      ifx_spi_tiocmset        -       set modem bits
333  *      @tty: the tty structure
334  *      @set: bits to set
335  *      @clear: bits to clear
336  *
337  *      The IFX6x60 only supports DTR and RTS. Set them accordingly
338  *      and flag that an update to the modem is needed.
339  *
340  *      FIXME: do we need to kick the tranfers when we do this ?
341  */
342 static int ifx_spi_tiocmset(struct tty_struct *tty,
343                             unsigned int set, unsigned int clear)
344 {
345         struct ifx_spi_device *ifx_dev = tty->driver_data;
346
347         if (set & TIOCM_RTS)
348                 set_bit(IFX_SPI_RTS, &ifx_dev->signal_state);
349         if (set & TIOCM_DTR)
350                 set_bit(IFX_SPI_DTR, &ifx_dev->signal_state);
351         if (clear & TIOCM_RTS)
352                 clear_bit(IFX_SPI_RTS, &ifx_dev->signal_state);
353         if (clear & TIOCM_DTR)
354                 clear_bit(IFX_SPI_DTR, &ifx_dev->signal_state);
355
356         set_bit(IFX_SPI_UPDATE, &ifx_dev->signal_state);
357         return 0;
358 }
359
360 /**
361  *      ifx_spi_open    -       called on tty open
362  *      @tty: our tty device
363  *      @filp: file handle being associated with the tty
364  *
365  *      Open the tty interface. We let the tty_port layer do all the work
366  *      for us.
367  *
368  *      FIXME: Remove single device assumption and saved_ifx_dev
369  */
370 static int ifx_spi_open(struct tty_struct *tty, struct file *filp)
371 {
372         return tty_port_open(&saved_ifx_dev->tty_port, tty, filp);
373 }
374
375 /**
376  *      ifx_spi_close   -       called when our tty closes
377  *      @tty: the tty being closed
378  *      @filp: the file handle being closed
379  *
380  *      Perform the close of the tty. We use the tty_port layer to do all
381  *      our hard work.
382  */
383 static void ifx_spi_close(struct tty_struct *tty, struct file *filp)
384 {
385         struct ifx_spi_device *ifx_dev = tty->driver_data;
386         tty_port_close(&ifx_dev->tty_port, tty, filp);
387         /* FIXME: should we do an ifx_spi_reset here ? */
388 }
389
390 /**
391  *      ifx_decode_spi_header   -       decode received header
392  *      @buffer: the received data
393  *      @length: decoded length
394  *      @more: decoded more flag
395  *      @received_cts: status of cts we received
396  *
397  *      Note how received_cts is handled -- if header is all F it is left
398  *      the same as it was, if header is all 0 it is set to 0 otherwise it is
399  *      taken from the incoming header.
400  *
401  *      FIXME: endianness
402  */
403 static int ifx_spi_decode_spi_header(unsigned char *buffer, int *length,
404                         unsigned char *more, unsigned char *received_cts)
405 {
406         u16 h1;
407         u16 h2;
408         u16 *in_buffer = (u16 *)buffer;
409
410         h1 = *in_buffer;
411         h2 = *(in_buffer+1);
412
413         if (h1 == 0 && h2 == 0) {
414                 *received_cts = 0;
415                 return IFX_SPI_HEADER_0;
416         } else if (h1 == 0xffff && h2 == 0xffff) {
417                 /* spi_slave_cts remains as it was */
418                 return IFX_SPI_HEADER_F;
419         }
420
421         *length = h1 & 0xfff;   /* upper bits of byte are flags */
422         *more = (buffer[1] >> IFX_SPI_MORE_BIT) & 1;
423         *received_cts = (buffer[3] >> IFX_SPI_CTS_BIT) & 1;
424         return 0;
425 }
426
427 /**
428  *      ifx_setup_spi_header    -       set header fields
429  *      @txbuffer: pointer to start of SPI buffer
430  *      @tx_count: bytes
431  *      @more: indicate if more to follow
432  *
433  *      Format up an SPI header for a transfer
434  *
435  *      FIXME: endianness?
436  */
437 static void ifx_spi_setup_spi_header(unsigned char *txbuffer, int tx_count,
438                                         unsigned char more)
439 {
440         *(u16 *)(txbuffer) = tx_count;
441         *(u16 *)(txbuffer+2) = IFX_SPI_PAYLOAD_SIZE;
442         txbuffer[1] |= (more << IFX_SPI_MORE_BIT) & IFX_SPI_MORE_MASK;
443 }
444
445 /**
446  *      ifx_spi_prepare_tx_buffer       -       prepare transmit frame
447  *      @ifx_dev: our SPI device
448  *
449  *      The transmit buffr needs a header and various other bits of
450  *      information followed by as much data as we can pull from the FIFO
451  *      and transfer. This function formats up a suitable buffer in the
452  *      ifx_dev->tx_buffer
453  *
454  *      FIXME: performance - should we wake the tty when the queue is half
455  *                           empty ?
456  */
457 static int ifx_spi_prepare_tx_buffer(struct ifx_spi_device *ifx_dev)
458 {
459         int temp_count;
460         int queue_length;
461         int tx_count;
462         unsigned char *tx_buffer;
463
464         tx_buffer = ifx_dev->tx_buffer;
465
466         /* make room for required SPI header */
467         tx_buffer += IFX_SPI_HEADER_OVERHEAD;
468         tx_count = IFX_SPI_HEADER_OVERHEAD;
469
470         /* clear to signal no more data if this turns out to be the
471          * last buffer sent in a sequence */
472         ifx_dev->spi_more = 0;
473
474         /* if modem cts is set, just send empty buffer */
475         if (!ifx_dev->spi_slave_cts) {
476                 /* see if there's tx data */
477                 queue_length = kfifo_len(&ifx_dev->tx_fifo);
478                 if (queue_length != 0) {
479                         /* data to mux -- see if there's room for it */
480                         temp_count = min(queue_length, IFX_SPI_PAYLOAD_SIZE);
481                         temp_count = kfifo_out_locked(&ifx_dev->tx_fifo,
482                                         tx_buffer, temp_count,
483                                         &ifx_dev->fifo_lock);
484
485                         /* update buffer pointer and data count in message */
486                         tx_buffer += temp_count;
487                         tx_count += temp_count;
488                         if (temp_count == queue_length)
489                                 /* poke port to get more data */
490                                 tty_port_tty_wakeup(&ifx_dev->tty_port);
491                         else /* more data in port, use next SPI message */
492                                 ifx_dev->spi_more = 1;
493                 }
494         }
495         /* have data and info for header -- set up SPI header in buffer */
496         /* spi header needs payload size, not entire buffer size */
497         ifx_spi_setup_spi_header(ifx_dev->tx_buffer,
498                                         tx_count-IFX_SPI_HEADER_OVERHEAD,
499                                         ifx_dev->spi_more);
500         /* swap actual data in the buffer */
501         ifx_dev->swap_buf((ifx_dev->tx_buffer), tx_count,
502                 &ifx_dev->tx_buffer[IFX_SPI_TRANSFER_SIZE]);
503         return tx_count;
504 }
505
506 /**
507  *      ifx_spi_write           -       line discipline write
508  *      @tty: our tty device
509  *      @buf: pointer to buffer to write (kernel space)
510  *      @count: size of buffer
511  *
512  *      Write the characters we have been given into the FIFO. If the device
513  *      is not active then activate it, when the SRDY line is asserted back
514  *      this will commence I/O
515  */
516 static int ifx_spi_write(struct tty_struct *tty, const unsigned char *buf,
517                          int count)
518 {
519         struct ifx_spi_device *ifx_dev = tty->driver_data;
520         unsigned char *tmp_buf = (unsigned char *)buf;
521         unsigned long flags;
522         bool is_fifo_empty;
523         int tx_count;
524
525         spin_lock_irqsave(&ifx_dev->fifo_lock, flags);
526         is_fifo_empty = kfifo_is_empty(&ifx_dev->tx_fifo);
527         tx_count = kfifo_in(&ifx_dev->tx_fifo, tmp_buf, count);
528         spin_unlock_irqrestore(&ifx_dev->fifo_lock, flags);
529         if (is_fifo_empty)
530                 mrdy_assert(ifx_dev);
531
532         return tx_count;
533 }
534
535 /**
536  *      ifx_spi_chars_in_buffer -       line discipline helper
537  *      @tty: our tty device
538  *
539  *      Report how much data we can accept before we drop bytes. As we use
540  *      a simple FIFO this is nice and easy.
541  */
542 static int ifx_spi_write_room(struct tty_struct *tty)
543 {
544         struct ifx_spi_device *ifx_dev = tty->driver_data;
545         return IFX_SPI_FIFO_SIZE - kfifo_len(&ifx_dev->tx_fifo);
546 }
547
548 /**
549  *      ifx_spi_chars_in_buffer -       line discipline helper
550  *      @tty: our tty device
551  *
552  *      Report how many characters we have buffered. In our case this is the
553  *      number of bytes sitting in our transmit FIFO.
554  */
555 static int ifx_spi_chars_in_buffer(struct tty_struct *tty)
556 {
557         struct ifx_spi_device *ifx_dev = tty->driver_data;
558         return kfifo_len(&ifx_dev->tx_fifo);
559 }
560
561 /**
562  *      ifx_port_hangup
563  *      @port: our tty port
564  *
565  *      tty port hang up. Called when tty_hangup processing is invoked either
566  *      by loss of carrier, or by software (eg vhangup). Serialized against
567  *      activate/shutdown by the tty layer.
568  */
569 static void ifx_spi_hangup(struct tty_struct *tty)
570 {
571         struct ifx_spi_device *ifx_dev = tty->driver_data;
572         tty_port_hangup(&ifx_dev->tty_port);
573 }
574
575 /**
576  *      ifx_port_activate
577  *      @port: our tty port
578  *
579  *      tty port activate method - called for first open. Serialized
580  *      with hangup and shutdown by the tty layer.
581  */
582 static int ifx_port_activate(struct tty_port *port, struct tty_struct *tty)
583 {
584         struct ifx_spi_device *ifx_dev =
585                 container_of(port, struct ifx_spi_device, tty_port);
586
587         /* clear any old data; can't do this in 'close' */
588         kfifo_reset(&ifx_dev->tx_fifo);
589
590         /* clear any flag which may be set in port shutdown procedure */
591         clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &ifx_dev->flags);
592         clear_bit(IFX_SPI_STATE_IO_READY, &ifx_dev->flags);
593
594         /* put port data into this tty */
595         tty->driver_data = ifx_dev;
596
597         /* allows flip string push from int context */
598         port->low_latency = 1;
599
600         /* set flag to allows data transfer */
601         set_bit(IFX_SPI_STATE_IO_AVAILABLE, &ifx_dev->flags);
602
603         return 0;
604 }
605
606 /**
607  *      ifx_port_shutdown
608  *      @port: our tty port
609  *
610  *      tty port shutdown method - called for last port close. Serialized
611  *      with hangup and activate by the tty layer.
612  */
613 static void ifx_port_shutdown(struct tty_port *port)
614 {
615         struct ifx_spi_device *ifx_dev =
616                 container_of(port, struct ifx_spi_device, tty_port);
617
618         clear_bit(IFX_SPI_STATE_IO_AVAILABLE, &ifx_dev->flags);
619         mrdy_set_low(ifx_dev);
620         del_timer(&ifx_dev->spi_timer);
621         clear_bit(IFX_SPI_STATE_TIMER_PENDING, &ifx_dev->flags);
622         tasklet_kill(&ifx_dev->io_work_tasklet);
623 }
624
625 static const struct tty_port_operations ifx_tty_port_ops = {
626         .activate = ifx_port_activate,
627         .shutdown = ifx_port_shutdown,
628 };
629
630 static const struct tty_operations ifx_spi_serial_ops = {
631         .open = ifx_spi_open,
632         .close = ifx_spi_close,
633         .write = ifx_spi_write,
634         .hangup = ifx_spi_hangup,
635         .write_room = ifx_spi_write_room,
636         .chars_in_buffer = ifx_spi_chars_in_buffer,
637         .tiocmget = ifx_spi_tiocmget,
638         .tiocmset = ifx_spi_tiocmset,
639 };
640
641 /**
642  *      ifx_spi_insert_fip_string       -       queue received data
643  *      @ifx_ser: our SPI device
644  *      @chars: buffer we have received
645  *      @size: number of chars reeived
646  *
647  *      Queue bytes to the tty assuming the tty side is currently open. If
648  *      not the discard the data.
649  */
650 static void ifx_spi_insert_flip_string(struct ifx_spi_device *ifx_dev,
651                                     unsigned char *chars, size_t size)
652 {
653         tty_insert_flip_string(&ifx_dev->tty_port, chars, size);
654         tty_flip_buffer_push(&ifx_dev->tty_port);
655 }
656
657 /**
658  *      ifx_spi_complete        -       SPI transfer completed
659  *      @ctx: our SPI device
660  *
661  *      An SPI transfer has completed. Process any received data and kick off
662  *      any further transmits we can commence.
663  */
664 static void ifx_spi_complete(void *ctx)
665 {
666         struct ifx_spi_device *ifx_dev = ctx;
667         int length;
668         int actual_length;
669         unsigned char more;
670         unsigned char cts;
671         int local_write_pending = 0;
672         int queue_length;
673         int srdy;
674         int decode_result;
675
676         mrdy_set_low(ifx_dev);
677
678         if (!ifx_dev->spi_msg.status) {
679                 /* check header validity, get comm flags */
680                 ifx_dev->swap_buf(ifx_dev->rx_buffer, IFX_SPI_HEADER_OVERHEAD,
681                         &ifx_dev->rx_buffer[IFX_SPI_HEADER_OVERHEAD]);
682                 decode_result = ifx_spi_decode_spi_header(ifx_dev->rx_buffer,
683                                 &length, &more, &cts);
684                 if (decode_result == IFX_SPI_HEADER_0) {
685                         dev_dbg(&ifx_dev->spi_dev->dev,
686                                 "ignore input: invalid header 0");
687                         ifx_dev->spi_slave_cts = 0;
688                         goto complete_exit;
689                 } else if (decode_result == IFX_SPI_HEADER_F) {
690                         dev_dbg(&ifx_dev->spi_dev->dev,
691                                 "ignore input: invalid header F");
692                         goto complete_exit;
693                 }
694
695                 ifx_dev->spi_slave_cts = cts;
696
697                 actual_length = min((unsigned int)length,
698                                         ifx_dev->spi_msg.actual_length);
699                 ifx_dev->swap_buf(
700                         (ifx_dev->rx_buffer + IFX_SPI_HEADER_OVERHEAD),
701                          actual_length,
702                          &ifx_dev->rx_buffer[IFX_SPI_TRANSFER_SIZE]);
703                 ifx_spi_insert_flip_string(
704                         ifx_dev,
705                         ifx_dev->rx_buffer + IFX_SPI_HEADER_OVERHEAD,
706                         (size_t)actual_length);
707         } else {
708                 dev_dbg(&ifx_dev->spi_dev->dev, "SPI transfer error %d",
709                        ifx_dev->spi_msg.status);
710         }
711
712 complete_exit:
713         if (ifx_dev->write_pending) {
714                 ifx_dev->write_pending = 0;
715                 local_write_pending = 1;
716         }
717
718         clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &(ifx_dev->flags));
719
720         queue_length = kfifo_len(&ifx_dev->tx_fifo);
721         srdy = gpio_get_value(ifx_dev->gpio.srdy);
722         if (!srdy)
723                 ifx_spi_power_state_clear(ifx_dev, IFX_SPI_POWER_SRDY);
724
725         /* schedule output if there is more to do */
726         if (test_and_clear_bit(IFX_SPI_STATE_IO_READY, &ifx_dev->flags))
727                 tasklet_schedule(&ifx_dev->io_work_tasklet);
728         else {
729                 if (more || ifx_dev->spi_more || queue_length > 0 ||
730                         local_write_pending) {
731                         if (ifx_dev->spi_slave_cts) {
732                                 if (more)
733                                         mrdy_assert(ifx_dev);
734                         } else
735                                 mrdy_assert(ifx_dev);
736                 } else {
737                         /*
738                          * poke line discipline driver if any for more data
739                          * may or may not get more data to write
740                          * for now, say not busy
741                          */
742                         ifx_spi_power_state_clear(ifx_dev,
743                                                   IFX_SPI_POWER_DATA_PENDING);
744                         tty_port_tty_wakeup(&ifx_dev->tty_port);
745                 }
746         }
747 }
748
749 /**
750  *      ifx_spio_io             -       I/O tasklet
751  *      @data: our SPI device
752  *
753  *      Queue data for transmission if possible and then kick off the
754  *      transfer.
755  */
756 static void ifx_spi_io(unsigned long data)
757 {
758         int retval;
759         struct ifx_spi_device *ifx_dev = (struct ifx_spi_device *) data;
760
761         if (!test_and_set_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &ifx_dev->flags) &&
762                 test_bit(IFX_SPI_STATE_IO_AVAILABLE, &ifx_dev->flags)) {
763                 if (ifx_dev->gpio.unack_srdy_int_nb > 0)
764                         ifx_dev->gpio.unack_srdy_int_nb--;
765
766                 ifx_spi_prepare_tx_buffer(ifx_dev);
767
768                 spi_message_init(&ifx_dev->spi_msg);
769                 INIT_LIST_HEAD(&ifx_dev->spi_msg.queue);
770
771                 ifx_dev->spi_msg.context = ifx_dev;
772                 ifx_dev->spi_msg.complete = ifx_spi_complete;
773
774                 /* set up our spi transfer */
775                 /* note len is BYTES, not transfers */
776                 ifx_dev->spi_xfer.len = IFX_SPI_TRANSFER_SIZE;
777                 ifx_dev->spi_xfer.cs_change = 0;
778                 ifx_dev->spi_xfer.speed_hz = ifx_dev->spi_dev->max_speed_hz;
779                 /* ifx_dev->spi_xfer.speed_hz = 390625; */
780                 ifx_dev->spi_xfer.bits_per_word =
781                         ifx_dev->spi_dev->bits_per_word;
782
783                 ifx_dev->spi_xfer.tx_buf = ifx_dev->tx_buffer;
784                 ifx_dev->spi_xfer.rx_buf = ifx_dev->rx_buffer;
785
786                 /*
787                  * setup dma pointers
788                  */
789                 if (ifx_dev->use_dma) {
790                         ifx_dev->spi_msg.is_dma_mapped = 1;
791                         ifx_dev->tx_dma = ifx_dev->tx_bus;
792                         ifx_dev->rx_dma = ifx_dev->rx_bus;
793                         ifx_dev->spi_xfer.tx_dma = ifx_dev->tx_dma;
794                         ifx_dev->spi_xfer.rx_dma = ifx_dev->rx_dma;
795                 } else {
796                         ifx_dev->spi_msg.is_dma_mapped = 0;
797                         ifx_dev->tx_dma = (dma_addr_t)0;
798                         ifx_dev->rx_dma = (dma_addr_t)0;
799                         ifx_dev->spi_xfer.tx_dma = (dma_addr_t)0;
800                         ifx_dev->spi_xfer.rx_dma = (dma_addr_t)0;
801                 }
802
803                 spi_message_add_tail(&ifx_dev->spi_xfer, &ifx_dev->spi_msg);
804
805                 /* Assert MRDY. This may have already been done by the write
806                  * routine.
807                  */
808                 mrdy_assert(ifx_dev);
809
810                 retval = spi_async(ifx_dev->spi_dev, &ifx_dev->spi_msg);
811                 if (retval) {
812                         clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS,
813                                   &ifx_dev->flags);
814                         tasklet_schedule(&ifx_dev->io_work_tasklet);
815                         return;
816                 }
817         } else
818                 ifx_dev->write_pending = 1;
819 }
820
821 /**
822  *      ifx_spi_free_port       -       free up the tty side
823  *      @ifx_dev: IFX device going away
824  *
825  *      Unregister and free up a port when the device goes away
826  */
827 static void ifx_spi_free_port(struct ifx_spi_device *ifx_dev)
828 {
829         if (ifx_dev->tty_dev)
830                 tty_unregister_device(tty_drv, ifx_dev->minor);
831         tty_port_destroy(&ifx_dev->tty_port);
832         kfifo_free(&ifx_dev->tx_fifo);
833 }
834
835 /**
836  *      ifx_spi_create_port     -       create a new port
837  *      @ifx_dev: our spi device
838  *
839  *      Allocate and initialise the tty port that goes with this interface
840  *      and add it to the tty layer so that it can be opened.
841  */
842 static int ifx_spi_create_port(struct ifx_spi_device *ifx_dev)
843 {
844         int ret = 0;
845         struct tty_port *pport = &ifx_dev->tty_port;
846
847         spin_lock_init(&ifx_dev->fifo_lock);
848         lockdep_set_class_and_subclass(&ifx_dev->fifo_lock,
849                 &ifx_spi_key, 0);
850
851         if (kfifo_alloc(&ifx_dev->tx_fifo, IFX_SPI_FIFO_SIZE, GFP_KERNEL)) {
852                 ret = -ENOMEM;
853                 goto error_ret;
854         }
855
856         tty_port_init(pport);
857         pport->ops = &ifx_tty_port_ops;
858         ifx_dev->minor = IFX_SPI_TTY_ID;
859         ifx_dev->tty_dev = tty_port_register_device(pport, tty_drv,
860                         ifx_dev->minor, &ifx_dev->spi_dev->dev);
861         if (IS_ERR(ifx_dev->tty_dev)) {
862                 dev_dbg(&ifx_dev->spi_dev->dev,
863                         "%s: registering tty device failed", __func__);
864                 ret = PTR_ERR(ifx_dev->tty_dev);
865                 goto error_port;
866         }
867         return 0;
868
869 error_port:
870         tty_port_destroy(pport);
871 error_ret:
872         ifx_spi_free_port(ifx_dev);
873         return ret;
874 }
875
876 /**
877  *      ifx_spi_handle_srdy             -       handle SRDY
878  *      @ifx_dev: device asserting SRDY
879  *
880  *      Check our device state and see what we need to kick off when SRDY
881  *      is asserted. This usually means killing the timer and firing off the
882  *      I/O processing.
883  */
884 static void ifx_spi_handle_srdy(struct ifx_spi_device *ifx_dev)
885 {
886         if (test_bit(IFX_SPI_STATE_TIMER_PENDING, &ifx_dev->flags)) {
887                 del_timer(&ifx_dev->spi_timer);
888                 clear_bit(IFX_SPI_STATE_TIMER_PENDING, &ifx_dev->flags);
889         }
890
891         ifx_spi_power_state_set(ifx_dev, IFX_SPI_POWER_SRDY);
892
893         if (!test_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &ifx_dev->flags))
894                 tasklet_schedule(&ifx_dev->io_work_tasklet);
895         else
896                 set_bit(IFX_SPI_STATE_IO_READY, &ifx_dev->flags);
897 }
898
899 /**
900  *      ifx_spi_srdy_interrupt  -       SRDY asserted
901  *      @irq: our IRQ number
902  *      @dev: our ifx device
903  *
904  *      The modem asserted SRDY. Handle the srdy event
905  */
906 static irqreturn_t ifx_spi_srdy_interrupt(int irq, void *dev)
907 {
908         struct ifx_spi_device *ifx_dev = dev;
909         ifx_dev->gpio.unack_srdy_int_nb++;
910         ifx_spi_handle_srdy(ifx_dev);
911         return IRQ_HANDLED;
912 }
913
914 /**
915  *      ifx_spi_reset_interrupt -       Modem has changed reset state
916  *      @irq: interrupt number
917  *      @dev: our device pointer
918  *
919  *      The modem has either entered or left reset state. Check the GPIO
920  *      line to see which.
921  *
922  *      FIXME: review locking on MR_INPROGRESS versus
923  *      parallel unsolicited reset/solicited reset
924  */
925 static irqreturn_t ifx_spi_reset_interrupt(int irq, void *dev)
926 {
927         struct ifx_spi_device *ifx_dev = dev;
928         int val = gpio_get_value(ifx_dev->gpio.reset_out);
929         int solreset = test_bit(MR_START, &ifx_dev->mdm_reset_state);
930
931         if (val == 0) {
932                 /* entered reset */
933                 set_bit(MR_INPROGRESS, &ifx_dev->mdm_reset_state);
934                 if (!solreset) {
935                         /* unsolicited reset  */
936                         ifx_spi_ttyhangup(ifx_dev);
937                 }
938         } else {
939                 /* exited reset */
940                 clear_bit(MR_INPROGRESS, &ifx_dev->mdm_reset_state);
941                 if (solreset) {
942                         set_bit(MR_COMPLETE, &ifx_dev->mdm_reset_state);
943                         wake_up(&ifx_dev->mdm_reset_wait);
944                 }
945         }
946         return IRQ_HANDLED;
947 }
948
949 /**
950  *      ifx_spi_free_device - free device
951  *      @ifx_dev: device to free
952  *
953  *      Free the IFX device
954  */
955 static void ifx_spi_free_device(struct ifx_spi_device *ifx_dev)
956 {
957         ifx_spi_free_port(ifx_dev);
958         dma_free_coherent(&ifx_dev->spi_dev->dev,
959                                 IFX_SPI_TRANSFER_SIZE,
960                                 ifx_dev->tx_buffer,
961                                 ifx_dev->tx_bus);
962         dma_free_coherent(&ifx_dev->spi_dev->dev,
963                                 IFX_SPI_TRANSFER_SIZE,
964                                 ifx_dev->rx_buffer,
965                                 ifx_dev->rx_bus);
966 }
967
968 /**
969  *      ifx_spi_reset   -       reset modem
970  *      @ifx_dev: modem to reset
971  *
972  *      Perform a reset on the modem
973  */
974 static int ifx_spi_reset(struct ifx_spi_device *ifx_dev)
975 {
976         int ret;
977         /*
978          * set up modem power, reset
979          *
980          * delays are required on some platforms for the modem
981          * to reset properly
982          */
983         set_bit(MR_START, &ifx_dev->mdm_reset_state);
984         gpio_set_value(ifx_dev->gpio.po, 0);
985         gpio_set_value(ifx_dev->gpio.reset, 0);
986         msleep(25);
987         gpio_set_value(ifx_dev->gpio.reset, 1);
988         msleep(1);
989         gpio_set_value(ifx_dev->gpio.po, 1);
990         msleep(1);
991         gpio_set_value(ifx_dev->gpio.po, 0);
992         ret = wait_event_timeout(ifx_dev->mdm_reset_wait,
993                                  test_bit(MR_COMPLETE,
994                                           &ifx_dev->mdm_reset_state),
995                                  IFX_RESET_TIMEOUT);
996         if (!ret)
997                 dev_warn(&ifx_dev->spi_dev->dev, "Modem reset timeout: (state:%lx)",
998                          ifx_dev->mdm_reset_state);
999
1000         ifx_dev->mdm_reset_state = 0;
1001         return ret;
1002 }
1003
1004 /**
1005  *      ifx_spi_spi_probe       -       probe callback
1006  *      @spi: our possible matching SPI device
1007  *
1008  *      Probe for a 6x60 modem on SPI bus. Perform any needed device and
1009  *      GPIO setup.
1010  *
1011  *      FIXME:
1012  *      -       Support for multiple devices
1013  *      -       Split out MID specific GPIO handling eventually
1014  */
1015
1016 static int ifx_spi_spi_probe(struct spi_device *spi)
1017 {
1018         int ret;
1019         int srdy;
1020         struct ifx_modem_platform_data *pl_data;
1021         struct ifx_spi_device *ifx_dev;
1022
1023         if (saved_ifx_dev) {
1024                 dev_dbg(&spi->dev, "ignoring subsequent detection");
1025                 return -ENODEV;
1026         }
1027
1028         pl_data = (struct ifx_modem_platform_data *)spi->dev.platform_data;
1029         if (!pl_data) {
1030                 dev_err(&spi->dev, "missing platform data!");
1031                 return -ENODEV;
1032         }
1033
1034         /* initialize structure to hold our device variables */
1035         ifx_dev = kzalloc(sizeof(struct ifx_spi_device), GFP_KERNEL);
1036         if (!ifx_dev) {
1037                 dev_err(&spi->dev, "spi device allocation failed");
1038                 return -ENOMEM;
1039         }
1040         saved_ifx_dev = ifx_dev;
1041         ifx_dev->spi_dev = spi;
1042         clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &ifx_dev->flags);
1043         spin_lock_init(&ifx_dev->write_lock);
1044         spin_lock_init(&ifx_dev->power_lock);
1045         ifx_dev->power_status = 0;
1046         init_timer(&ifx_dev->spi_timer);
1047         ifx_dev->spi_timer.function = ifx_spi_timeout;
1048         ifx_dev->spi_timer.data = (unsigned long)ifx_dev;
1049         ifx_dev->modem = pl_data->modem_type;
1050         ifx_dev->use_dma = pl_data->use_dma;
1051         ifx_dev->max_hz = pl_data->max_hz;
1052         /* initialize spi mode, etc */
1053         spi->max_speed_hz = ifx_dev->max_hz;
1054         spi->mode = IFX_SPI_MODE | (SPI_LOOP & spi->mode);
1055         spi->bits_per_word = spi_bpw;
1056         ret = spi_setup(spi);
1057         if (ret) {
1058                 dev_err(&spi->dev, "SPI setup wasn't successful %d", ret);
1059                 return -ENODEV;
1060         }
1061
1062         /* init swap_buf function according to word width configuration */
1063         if (spi->bits_per_word == 32)
1064                 ifx_dev->swap_buf = swap_buf_32;
1065         else if (spi->bits_per_word == 16)
1066                 ifx_dev->swap_buf = swap_buf_16;
1067         else
1068                 ifx_dev->swap_buf = swap_buf_8;
1069
1070         /* ensure SPI protocol flags are initialized to enable transfer */
1071         ifx_dev->spi_more = 0;
1072         ifx_dev->spi_slave_cts = 0;
1073
1074         /*initialize transfer and dma buffers */
1075         ifx_dev->tx_buffer = dma_alloc_coherent(ifx_dev->spi_dev->dev.parent,
1076                                 IFX_SPI_TRANSFER_SIZE,
1077                                 &ifx_dev->tx_bus,
1078                                 GFP_KERNEL);
1079         if (!ifx_dev->tx_buffer) {
1080                 dev_err(&spi->dev, "DMA-TX buffer allocation failed");
1081                 ret = -ENOMEM;
1082                 goto error_ret;
1083         }
1084         ifx_dev->rx_buffer = dma_alloc_coherent(ifx_dev->spi_dev->dev.parent,
1085                                 IFX_SPI_TRANSFER_SIZE,
1086                                 &ifx_dev->rx_bus,
1087                                 GFP_KERNEL);
1088         if (!ifx_dev->rx_buffer) {
1089                 dev_err(&spi->dev, "DMA-RX buffer allocation failed");
1090                 ret = -ENOMEM;
1091                 goto error_ret;
1092         }
1093
1094         /* initialize waitq for modem reset */
1095         init_waitqueue_head(&ifx_dev->mdm_reset_wait);
1096
1097         spi_set_drvdata(spi, ifx_dev);
1098         tasklet_init(&ifx_dev->io_work_tasklet, ifx_spi_io,
1099                                                 (unsigned long)ifx_dev);
1100
1101         set_bit(IFX_SPI_STATE_PRESENT, &ifx_dev->flags);
1102
1103         /* create our tty port */
1104         ret = ifx_spi_create_port(ifx_dev);
1105         if (ret != 0) {
1106                 dev_err(&spi->dev, "create default tty port failed");
1107                 goto error_ret;
1108         }
1109
1110         ifx_dev->gpio.reset = pl_data->rst_pmu;
1111         ifx_dev->gpio.po = pl_data->pwr_on;
1112         ifx_dev->gpio.mrdy = pl_data->mrdy;
1113         ifx_dev->gpio.srdy = pl_data->srdy;
1114         ifx_dev->gpio.reset_out = pl_data->rst_out;
1115
1116         dev_info(&spi->dev, "gpios %d, %d, %d, %d, %d",
1117                  ifx_dev->gpio.reset, ifx_dev->gpio.po, ifx_dev->gpio.mrdy,
1118                  ifx_dev->gpio.srdy, ifx_dev->gpio.reset_out);
1119
1120         /* Configure gpios */
1121         ret = gpio_request(ifx_dev->gpio.reset, "ifxModem");
1122         if (ret < 0) {
1123                 dev_err(&spi->dev, "Unable to allocate GPIO%d (RESET)",
1124                         ifx_dev->gpio.reset);
1125                 goto error_ret;
1126         }
1127         ret += gpio_direction_output(ifx_dev->gpio.reset, 0);
1128         ret += gpio_export(ifx_dev->gpio.reset, 1);
1129         if (ret) {
1130                 dev_err(&spi->dev, "Unable to configure GPIO%d (RESET)",
1131                         ifx_dev->gpio.reset);
1132                 ret = -EBUSY;
1133                 goto error_ret2;
1134         }
1135
1136         ret = gpio_request(ifx_dev->gpio.po, "ifxModem");
1137         ret += gpio_direction_output(ifx_dev->gpio.po, 0);
1138         ret += gpio_export(ifx_dev->gpio.po, 1);
1139         if (ret) {
1140                 dev_err(&spi->dev, "Unable to configure GPIO%d (ON)",
1141                         ifx_dev->gpio.po);
1142                 ret = -EBUSY;
1143                 goto error_ret3;
1144         }
1145
1146         ret = gpio_request(ifx_dev->gpio.mrdy, "ifxModem");
1147         if (ret < 0) {
1148                 dev_err(&spi->dev, "Unable to allocate GPIO%d (MRDY)",
1149                         ifx_dev->gpio.mrdy);
1150                 goto error_ret3;
1151         }
1152         ret += gpio_export(ifx_dev->gpio.mrdy, 1);
1153         ret += gpio_direction_output(ifx_dev->gpio.mrdy, 0);
1154         if (ret) {
1155                 dev_err(&spi->dev, "Unable to configure GPIO%d (MRDY)",
1156                         ifx_dev->gpio.mrdy);
1157                 ret = -EBUSY;
1158                 goto error_ret4;
1159         }
1160
1161         ret = gpio_request(ifx_dev->gpio.srdy, "ifxModem");
1162         if (ret < 0) {
1163                 dev_err(&spi->dev, "Unable to allocate GPIO%d (SRDY)",
1164                         ifx_dev->gpio.srdy);
1165                 ret = -EBUSY;
1166                 goto error_ret4;
1167         }
1168         ret += gpio_export(ifx_dev->gpio.srdy, 1);
1169         ret += gpio_direction_input(ifx_dev->gpio.srdy);
1170         if (ret) {
1171                 dev_err(&spi->dev, "Unable to configure GPIO%d (SRDY)",
1172                         ifx_dev->gpio.srdy);
1173                 ret = -EBUSY;
1174                 goto error_ret5;
1175         }
1176
1177         ret = gpio_request(ifx_dev->gpio.reset_out, "ifxModem");
1178         if (ret < 0) {
1179                 dev_err(&spi->dev, "Unable to allocate GPIO%d (RESET_OUT)",
1180                         ifx_dev->gpio.reset_out);
1181                 goto error_ret5;
1182         }
1183         ret += gpio_export(ifx_dev->gpio.reset_out, 1);
1184         ret += gpio_direction_input(ifx_dev->gpio.reset_out);
1185         if (ret) {
1186                 dev_err(&spi->dev, "Unable to configure GPIO%d (RESET_OUT)",
1187                         ifx_dev->gpio.reset_out);
1188                 ret = -EBUSY;
1189                 goto error_ret6;
1190         }
1191
1192         ret = request_irq(gpio_to_irq(ifx_dev->gpio.reset_out),
1193                           ifx_spi_reset_interrupt,
1194                           IRQF_TRIGGER_RISING|IRQF_TRIGGER_FALLING, DRVNAME,
1195                 (void *)ifx_dev);
1196         if (ret) {
1197                 dev_err(&spi->dev, "Unable to get irq %x\n",
1198                         gpio_to_irq(ifx_dev->gpio.reset_out));
1199                 goto error_ret6;
1200         }
1201
1202         ret = ifx_spi_reset(ifx_dev);
1203
1204         ret = request_irq(gpio_to_irq(ifx_dev->gpio.srdy),
1205                           ifx_spi_srdy_interrupt,
1206                           IRQF_TRIGGER_RISING, DRVNAME,
1207                           (void *)ifx_dev);
1208         if (ret) {
1209                 dev_err(&spi->dev, "Unable to get irq %x",
1210                         gpio_to_irq(ifx_dev->gpio.srdy));
1211                 goto error_ret7;
1212         }
1213
1214         /* set pm runtime power state and register with power system */
1215         pm_runtime_set_active(&spi->dev);
1216         pm_runtime_enable(&spi->dev);
1217
1218         /* handle case that modem is already signaling SRDY */
1219         /* no outgoing tty open at this point, this just satisfies the
1220          * modem's read and should reset communication properly
1221          */
1222         srdy = gpio_get_value(ifx_dev->gpio.srdy);
1223
1224         if (srdy) {
1225                 mrdy_assert(ifx_dev);
1226                 ifx_spi_handle_srdy(ifx_dev);
1227         } else
1228                 mrdy_set_low(ifx_dev);
1229         return 0;
1230
1231 error_ret7:
1232         free_irq(gpio_to_irq(ifx_dev->gpio.reset_out), (void *)ifx_dev);
1233 error_ret6:
1234         gpio_free(ifx_dev->gpio.srdy);
1235 error_ret5:
1236         gpio_free(ifx_dev->gpio.mrdy);
1237 error_ret4:
1238         gpio_free(ifx_dev->gpio.reset);
1239 error_ret3:
1240         gpio_free(ifx_dev->gpio.po);
1241 error_ret2:
1242         gpio_free(ifx_dev->gpio.reset_out);
1243 error_ret:
1244         ifx_spi_free_device(ifx_dev);
1245         saved_ifx_dev = NULL;
1246         return ret;
1247 }
1248
1249 /**
1250  *      ifx_spi_spi_remove      -       SPI device was removed
1251  *      @spi: SPI device
1252  *
1253  *      FIXME: We should be shutting the device down here not in
1254  *      the module unload path.
1255  */
1256
1257 static int ifx_spi_spi_remove(struct spi_device *spi)
1258 {
1259         struct ifx_spi_device *ifx_dev = spi_get_drvdata(spi);
1260         /* stop activity */
1261         tasklet_kill(&ifx_dev->io_work_tasklet);
1262         /* free irq */
1263         free_irq(gpio_to_irq(ifx_dev->gpio.reset_out), (void *)ifx_dev);
1264         free_irq(gpio_to_irq(ifx_dev->gpio.srdy), (void *)ifx_dev);
1265
1266         gpio_free(ifx_dev->gpio.srdy);
1267         gpio_free(ifx_dev->gpio.mrdy);
1268         gpio_free(ifx_dev->gpio.reset);
1269         gpio_free(ifx_dev->gpio.po);
1270         gpio_free(ifx_dev->gpio.reset_out);
1271
1272         /* free allocations */
1273         ifx_spi_free_device(ifx_dev);
1274
1275         saved_ifx_dev = NULL;
1276         return 0;
1277 }
1278
1279 /**
1280  *      ifx_spi_spi_shutdown    -       called on SPI shutdown
1281  *      @spi: SPI device
1282  *
1283  *      No action needs to be taken here
1284  */
1285
1286 static void ifx_spi_spi_shutdown(struct spi_device *spi)
1287 {
1288         struct ifx_spi_device *ifx_dev = spi_get_drvdata(spi);
1289
1290         ifx_modem_power_off(ifx_dev);
1291 }
1292
1293 /*
1294  * various suspends and resumes have nothing to do
1295  * no hardware to save state for
1296  */
1297
1298 /**
1299  *      ifx_spi_spi_suspend     -       suspend SPI on system suspend
1300  *      @dev: device being suspended
1301  *
1302  *      Suspend the SPI side. No action needed on Intel MID platforms, may
1303  *      need extending for other systems.
1304  */
1305 static int ifx_spi_spi_suspend(struct spi_device *spi, pm_message_t msg)
1306 {
1307         return 0;
1308 }
1309
1310 /**
1311  *      ifx_spi_spi_resume      -       resume SPI side on system resume
1312  *      @dev: device being suspended
1313  *
1314  *      Suspend the SPI side. No action needed on Intel MID platforms, may
1315  *      need extending for other systems.
1316  */
1317 static int ifx_spi_spi_resume(struct spi_device *spi)
1318 {
1319         return 0;
1320 }
1321
1322 /**
1323  *      ifx_spi_pm_suspend      -       suspend modem on system suspend
1324  *      @dev: device being suspended
1325  *
1326  *      Suspend the modem. No action needed on Intel MID platforms, may
1327  *      need extending for other systems.
1328  */
1329 static int ifx_spi_pm_suspend(struct device *dev)
1330 {
1331         return 0;
1332 }
1333
1334 /**
1335  *      ifx_spi_pm_resume       -       resume modem on system resume
1336  *      @dev: device being suspended
1337  *
1338  *      Allow the modem to resume. No action needed.
1339  *
1340  *      FIXME: do we need to reset anything here ?
1341  */
1342 static int ifx_spi_pm_resume(struct device *dev)
1343 {
1344         return 0;
1345 }
1346
1347 /**
1348  *      ifx_spi_pm_runtime_resume       -       suspend modem
1349  *      @dev: device being suspended
1350  *
1351  *      Allow the modem to resume. No action needed.
1352  */
1353 static int ifx_spi_pm_runtime_resume(struct device *dev)
1354 {
1355         return 0;
1356 }
1357
1358 /**
1359  *      ifx_spi_pm_runtime_suspend      -       suspend modem
1360  *      @dev: device being suspended
1361  *
1362  *      Allow the modem to suspend and thus suspend to continue up the
1363  *      device tree.
1364  */
1365 static int ifx_spi_pm_runtime_suspend(struct device *dev)
1366 {
1367         return 0;
1368 }
1369
1370 /**
1371  *      ifx_spi_pm_runtime_idle         -       check if modem idle
1372  *      @dev: our device
1373  *
1374  *      Check conditions and queue runtime suspend if idle.
1375  */
1376 static int ifx_spi_pm_runtime_idle(struct device *dev)
1377 {
1378         struct spi_device *spi = to_spi_device(dev);
1379         struct ifx_spi_device *ifx_dev = spi_get_drvdata(spi);
1380
1381         if (!ifx_dev->power_status)
1382                 pm_runtime_suspend(dev);
1383
1384         return 0;
1385 }
1386
1387 static const struct dev_pm_ops ifx_spi_pm = {
1388         .resume = ifx_spi_pm_resume,
1389         .suspend = ifx_spi_pm_suspend,
1390         .runtime_resume = ifx_spi_pm_runtime_resume,
1391         .runtime_suspend = ifx_spi_pm_runtime_suspend,
1392         .runtime_idle = ifx_spi_pm_runtime_idle
1393 };
1394
1395 static const struct spi_device_id ifx_id_table[] = {
1396         {"ifx6160", 0},
1397         {"ifx6260", 0},
1398         { }
1399 };
1400 MODULE_DEVICE_TABLE(spi, ifx_id_table);
1401
1402 /* spi operations */
1403 static struct spi_driver ifx_spi_driver = {
1404         .driver = {
1405                 .name = DRVNAME,
1406                 .pm = &ifx_spi_pm,
1407                 .owner = THIS_MODULE},
1408         .probe = ifx_spi_spi_probe,
1409         .shutdown = ifx_spi_spi_shutdown,
1410         .remove = ifx_spi_spi_remove,
1411         .suspend = ifx_spi_spi_suspend,
1412         .resume = ifx_spi_spi_resume,
1413         .id_table = ifx_id_table
1414 };
1415
1416 /**
1417  *      ifx_spi_exit    -       module exit
1418  *
1419  *      Unload the module.
1420  */
1421
1422 static void __exit ifx_spi_exit(void)
1423 {
1424         /* unregister */
1425         tty_unregister_driver(tty_drv);
1426         put_tty_driver(tty_drv);
1427         spi_unregister_driver((void *)&ifx_spi_driver);
1428         unregister_reboot_notifier(&ifx_modem_reboot_notifier_block);
1429 }
1430
1431 /**
1432  *      ifx_spi_init            -       module entry point
1433  *
1434  *      Initialise the SPI and tty interfaces for the IFX SPI driver
1435  *      We need to initialize upper-edge spi driver after the tty
1436  *      driver because otherwise the spi probe will race
1437  */
1438
1439 static int __init ifx_spi_init(void)
1440 {
1441         int result;
1442
1443         tty_drv = alloc_tty_driver(1);
1444         if (!tty_drv) {
1445                 pr_err("%s: alloc_tty_driver failed", DRVNAME);
1446                 return -ENOMEM;
1447         }
1448
1449         tty_drv->driver_name = DRVNAME;
1450         tty_drv->name = TTYNAME;
1451         tty_drv->minor_start = IFX_SPI_TTY_ID;
1452         tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
1453         tty_drv->subtype = SERIAL_TYPE_NORMAL;
1454         tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1455         tty_drv->init_termios = tty_std_termios;
1456
1457         tty_set_operations(tty_drv, &ifx_spi_serial_ops);
1458
1459         result = tty_register_driver(tty_drv);
1460         if (result) {
1461                 pr_err("%s: tty_register_driver failed(%d)",
1462                         DRVNAME, result);
1463                 goto err_free_tty;
1464         }
1465
1466         result = spi_register_driver((void *)&ifx_spi_driver);
1467         if (result) {
1468                 pr_err("%s: spi_register_driver failed(%d)",
1469                         DRVNAME, result);
1470                 goto err_unreg_tty;
1471         }
1472
1473         result = register_reboot_notifier(&ifx_modem_reboot_notifier_block);
1474         if (result) {
1475                 pr_err("%s: register ifx modem reboot notifier failed(%d)",
1476                         DRVNAME, result);
1477                 goto err_unreg_spi;
1478         }
1479
1480         return 0;
1481 err_unreg_spi:
1482         spi_unregister_driver((void *)&ifx_spi_driver);
1483 err_unreg_tty:
1484         tty_unregister_driver(tty_drv);
1485 err_free_tty:
1486         put_tty_driver(tty_drv);
1487
1488         return result;
1489 }
1490
1491 module_init(ifx_spi_init);
1492 module_exit(ifx_spi_exit);
1493
1494 MODULE_AUTHOR("Intel");
1495 MODULE_DESCRIPTION("IFX6x60 spi driver");
1496 MODULE_LICENSE("GPL");
1497 MODULE_INFO(Version, "0.1-IFX6x60");