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