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