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