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