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