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