blob: 180b77334fdd8dd3e88f83d2731776e8b8448e8b [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/**
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100273 * 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 ***");
Jiri Slabyaa27a092013-03-07 13:12:30 +0100284 tty_port_tty_hangup(&ifx_dev->tty_port, false);
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100285 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 */
Alan Cox60b33c12011-02-14 16:26:14 +0000299static int ifx_spi_tiocmget(struct tty_struct *tty)
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100300{
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
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100317 * @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 */
Alan Cox20b9d172011-02-14 16:26:50 +0000325static int ifx_spi_tiocmset(struct tty_struct *tty,
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100326 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;
Arnd Bergmann0b8053f2016-02-25 21:47:57 +0100398 *more = 0;
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100399 return IFX_SPI_HEADER_0;
400 } else if (h1 == 0xffff && h2 == 0xffff) {
Arnd Bergmann0b8053f2016-02-25 21:47:57 +0100401 *more = 0;
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100402 /* spi_slave_cts remains as it was */
403 return IFX_SPI_HEADER_F;
404 }
405
406 *length = h1 & 0xfff; /* upper bits of byte are flags */
407 *more = (buffer[1] >> IFX_SPI_MORE_BIT) & 1;
408 *received_cts = (buffer[3] >> IFX_SPI_CTS_BIT) & 1;
409 return 0;
410}
411
412/**
413 * ifx_setup_spi_header - set header fields
414 * @txbuffer: pointer to start of SPI buffer
415 * @tx_count: bytes
416 * @more: indicate if more to follow
417 *
418 * Format up an SPI header for a transfer
419 *
420 * FIXME: endianness?
421 */
422static void ifx_spi_setup_spi_header(unsigned char *txbuffer, int tx_count,
423 unsigned char more)
424{
425 *(u16 *)(txbuffer) = tx_count;
426 *(u16 *)(txbuffer+2) = IFX_SPI_PAYLOAD_SIZE;
427 txbuffer[1] |= (more << IFX_SPI_MORE_BIT) & IFX_SPI_MORE_MASK;
428}
429
430/**
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100431 * ifx_spi_prepare_tx_buffer - prepare transmit frame
432 * @ifx_dev: our SPI device
433 *
434 * The transmit buffr needs a header and various other bits of
435 * information followed by as much data as we can pull from the FIFO
436 * and transfer. This function formats up a suitable buffer in the
437 * ifx_dev->tx_buffer
438 *
439 * FIXME: performance - should we wake the tty when the queue is half
440 * empty ?
441 */
442static int ifx_spi_prepare_tx_buffer(struct ifx_spi_device *ifx_dev)
443{
444 int temp_count;
445 int queue_length;
446 int tx_count;
447 unsigned char *tx_buffer;
448
449 tx_buffer = ifx_dev->tx_buffer;
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100450
451 /* make room for required SPI header */
452 tx_buffer += IFX_SPI_HEADER_OVERHEAD;
453 tx_count = IFX_SPI_HEADER_OVERHEAD;
454
455 /* clear to signal no more data if this turns out to be the
456 * last buffer sent in a sequence */
457 ifx_dev->spi_more = 0;
458
459 /* if modem cts is set, just send empty buffer */
460 if (!ifx_dev->spi_slave_cts) {
461 /* see if there's tx data */
462 queue_length = kfifo_len(&ifx_dev->tx_fifo);
463 if (queue_length != 0) {
464 /* data to mux -- see if there's room for it */
465 temp_count = min(queue_length, IFX_SPI_PAYLOAD_SIZE);
466 temp_count = kfifo_out_locked(&ifx_dev->tx_fifo,
467 tx_buffer, temp_count,
468 &ifx_dev->fifo_lock);
469
470 /* update buffer pointer and data count in message */
471 tx_buffer += temp_count;
472 tx_count += temp_count;
473 if (temp_count == queue_length)
474 /* poke port to get more data */
Jiri Slaby6aad04f2013-03-07 13:12:29 +0100475 tty_port_tty_wakeup(&ifx_dev->tty_port);
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100476 else /* more data in port, use next SPI message */
477 ifx_dev->spi_more = 1;
478 }
479 }
480 /* have data and info for header -- set up SPI header in buffer */
481 /* spi header needs payload size, not entire buffer size */
482 ifx_spi_setup_spi_header(ifx_dev->tx_buffer,
483 tx_count-IFX_SPI_HEADER_OVERHEAD,
484 ifx_dev->spi_more);
485 /* swap actual data in the buffer */
chao bi319fb0d2012-10-25 09:02:32 +0800486 ifx_dev->swap_buf((ifx_dev->tx_buffer), tx_count,
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100487 &ifx_dev->tx_buffer[IFX_SPI_TRANSFER_SIZE]);
488 return tx_count;
489}
490
491/**
492 * ifx_spi_write - line discipline write
493 * @tty: our tty device
494 * @buf: pointer to buffer to write (kernel space)
495 * @count: size of buffer
496 *
497 * Write the characters we have been given into the FIFO. If the device
498 * is not active then activate it, when the SRDY line is asserted back
499 * this will commence I/O
500 */
501static int ifx_spi_write(struct tty_struct *tty, const unsigned char *buf,
502 int count)
503{
504 struct ifx_spi_device *ifx_dev = tty->driver_data;
505 unsigned char *tmp_buf = (unsigned char *)buf;
Jun Chene8823f12012-11-07 12:04:04 -0500506 unsigned long flags;
507 bool is_fifo_empty;
Randy Dunlapbc6835a2012-11-26 11:07:25 -0800508 int tx_count;
Jun Chene8823f12012-11-07 12:04:04 -0500509
510 spin_lock_irqsave(&ifx_dev->fifo_lock, flags);
511 is_fifo_empty = kfifo_is_empty(&ifx_dev->tx_fifo);
Randy Dunlapbc6835a2012-11-26 11:07:25 -0800512 tx_count = kfifo_in(&ifx_dev->tx_fifo, tmp_buf, count);
Jun Chene8823f12012-11-07 12:04:04 -0500513 spin_unlock_irqrestore(&ifx_dev->fifo_lock, flags);
514 if (is_fifo_empty)
515 mrdy_assert(ifx_dev);
516
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100517 return tx_count;
518}
519
520/**
521 * ifx_spi_chars_in_buffer - line discipline helper
522 * @tty: our tty device
523 *
524 * Report how much data we can accept before we drop bytes. As we use
525 * a simple FIFO this is nice and easy.
526 */
527static int ifx_spi_write_room(struct tty_struct *tty)
528{
529 struct ifx_spi_device *ifx_dev = tty->driver_data;
530 return IFX_SPI_FIFO_SIZE - kfifo_len(&ifx_dev->tx_fifo);
531}
532
533/**
534 * ifx_spi_chars_in_buffer - line discipline helper
535 * @tty: our tty device
536 *
537 * Report how many characters we have buffered. In our case this is the
538 * number of bytes sitting in our transmit FIFO.
539 */
540static int ifx_spi_chars_in_buffer(struct tty_struct *tty)
541{
542 struct ifx_spi_device *ifx_dev = tty->driver_data;
543 return kfifo_len(&ifx_dev->tx_fifo);
544}
545
546/**
547 * ifx_port_hangup
548 * @port: our tty port
549 *
550 * tty port hang up. Called when tty_hangup processing is invoked either
551 * by loss of carrier, or by software (eg vhangup). Serialized against
552 * activate/shutdown by the tty layer.
553 */
554static void ifx_spi_hangup(struct tty_struct *tty)
555{
556 struct ifx_spi_device *ifx_dev = tty->driver_data;
557 tty_port_hangup(&ifx_dev->tty_port);
558}
559
560/**
561 * ifx_port_activate
562 * @port: our tty port
563 *
564 * tty port activate method - called for first open. Serialized
565 * with hangup and shutdown by the tty layer.
566 */
567static int ifx_port_activate(struct tty_port *port, struct tty_struct *tty)
568{
569 struct ifx_spi_device *ifx_dev =
570 container_of(port, struct ifx_spi_device, tty_port);
571
572 /* clear any old data; can't do this in 'close' */
573 kfifo_reset(&ifx_dev->tx_fifo);
574
chao bi31fe9902012-10-31 16:54:07 +0800575 /* clear any flag which may be set in port shutdown procedure */
576 clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &ifx_dev->flags);
577 clear_bit(IFX_SPI_STATE_IO_READY, &ifx_dev->flags);
578
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100579 /* put port data into this tty */
580 tty->driver_data = ifx_dev;
581
582 /* allows flip string push from int context */
Jiri Slabyd6c53c02013-01-03 15:53:05 +0100583 port->low_latency = 1;
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100584
chao bi31fe9902012-10-31 16:54:07 +0800585 /* set flag to allows data transfer */
586 set_bit(IFX_SPI_STATE_IO_AVAILABLE, &ifx_dev->flags);
587
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100588 return 0;
589}
590
591/**
592 * ifx_port_shutdown
593 * @port: our tty port
594 *
595 * tty port shutdown method - called for last port close. Serialized
596 * with hangup and activate by the tty layer.
597 */
598static void ifx_port_shutdown(struct tty_port *port)
599{
600 struct ifx_spi_device *ifx_dev =
601 container_of(port, struct ifx_spi_device, tty_port);
602
chao bi31fe9902012-10-31 16:54:07 +0800603 clear_bit(IFX_SPI_STATE_IO_AVAILABLE, &ifx_dev->flags);
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100604 mrdy_set_low(ifx_dev);
chao bi014b9b42012-12-12 11:40:56 +0800605 del_timer(&ifx_dev->spi_timer);
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100606 clear_bit(IFX_SPI_STATE_TIMER_PENDING, &ifx_dev->flags);
607 tasklet_kill(&ifx_dev->io_work_tasklet);
608}
609
610static const struct tty_port_operations ifx_tty_port_ops = {
611 .activate = ifx_port_activate,
612 .shutdown = ifx_port_shutdown,
613};
614
615static const struct tty_operations ifx_spi_serial_ops = {
616 .open = ifx_spi_open,
617 .close = ifx_spi_close,
618 .write = ifx_spi_write,
619 .hangup = ifx_spi_hangup,
620 .write_room = ifx_spi_write_room,
621 .chars_in_buffer = ifx_spi_chars_in_buffer,
622 .tiocmget = ifx_spi_tiocmget,
623 .tiocmset = ifx_spi_tiocmset,
624};
625
626/**
627 * ifx_spi_insert_fip_string - queue received data
628 * @ifx_ser: our SPI device
629 * @chars: buffer we have received
630 * @size: number of chars reeived
631 *
632 * Queue bytes to the tty assuming the tty side is currently open. If
633 * not the discard the data.
634 */
635static void ifx_spi_insert_flip_string(struct ifx_spi_device *ifx_dev,
636 unsigned char *chars, size_t size)
637{
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100638 tty_insert_flip_string(&ifx_dev->tty_port, chars, size);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100639 tty_flip_buffer_push(&ifx_dev->tty_port);
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100640}
641
642/**
643 * ifx_spi_complete - SPI transfer completed
644 * @ctx: our SPI device
645 *
646 * An SPI transfer has completed. Process any received data and kick off
647 * any further transmits we can commence.
648 */
649static void ifx_spi_complete(void *ctx)
650{
651 struct ifx_spi_device *ifx_dev = ctx;
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100652 int length;
653 int actual_length;
Jiri Slabyeeeabba2016-03-31 10:23:34 +0200654 unsigned char more = 0;
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100655 unsigned char cts;
656 int local_write_pending = 0;
657 int queue_length;
658 int srdy;
659 int decode_result;
660
661 mrdy_set_low(ifx_dev);
662
663 if (!ifx_dev->spi_msg.status) {
664 /* check header validity, get comm flags */
chao bi319fb0d2012-10-25 09:02:32 +0800665 ifx_dev->swap_buf(ifx_dev->rx_buffer, IFX_SPI_HEADER_OVERHEAD,
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100666 &ifx_dev->rx_buffer[IFX_SPI_HEADER_OVERHEAD]);
667 decode_result = ifx_spi_decode_spi_header(ifx_dev->rx_buffer,
668 &length, &more, &cts);
669 if (decode_result == IFX_SPI_HEADER_0) {
670 dev_dbg(&ifx_dev->spi_dev->dev,
671 "ignore input: invalid header 0");
672 ifx_dev->spi_slave_cts = 0;
673 goto complete_exit;
674 } else if (decode_result == IFX_SPI_HEADER_F) {
675 dev_dbg(&ifx_dev->spi_dev->dev,
676 "ignore input: invalid header F");
677 goto complete_exit;
678 }
679
680 ifx_dev->spi_slave_cts = cts;
681
682 actual_length = min((unsigned int)length,
683 ifx_dev->spi_msg.actual_length);
chao bi319fb0d2012-10-25 09:02:32 +0800684 ifx_dev->swap_buf(
685 (ifx_dev->rx_buffer + IFX_SPI_HEADER_OVERHEAD),
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100686 actual_length,
687 &ifx_dev->rx_buffer[IFX_SPI_TRANSFER_SIZE]);
688 ifx_spi_insert_flip_string(
689 ifx_dev,
690 ifx_dev->rx_buffer + IFX_SPI_HEADER_OVERHEAD,
691 (size_t)actual_length);
692 } else {
Arnd Bergmann0b8053f2016-02-25 21:47:57 +0100693 more = 0;
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100694 dev_dbg(&ifx_dev->spi_dev->dev, "SPI transfer error %d",
695 ifx_dev->spi_msg.status);
696 }
697
698complete_exit:
699 if (ifx_dev->write_pending) {
700 ifx_dev->write_pending = 0;
701 local_write_pending = 1;
702 }
703
704 clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &(ifx_dev->flags));
705
706 queue_length = kfifo_len(&ifx_dev->tx_fifo);
707 srdy = gpio_get_value(ifx_dev->gpio.srdy);
708 if (!srdy)
709 ifx_spi_power_state_clear(ifx_dev, IFX_SPI_POWER_SRDY);
710
711 /* schedule output if there is more to do */
712 if (test_and_clear_bit(IFX_SPI_STATE_IO_READY, &ifx_dev->flags))
713 tasklet_schedule(&ifx_dev->io_work_tasklet);
714 else {
715 if (more || ifx_dev->spi_more || queue_length > 0 ||
716 local_write_pending) {
717 if (ifx_dev->spi_slave_cts) {
718 if (more)
719 mrdy_assert(ifx_dev);
720 } else
721 mrdy_assert(ifx_dev);
722 } else {
723 /*
724 * poke line discipline driver if any for more data
725 * may or may not get more data to write
726 * for now, say not busy
727 */
728 ifx_spi_power_state_clear(ifx_dev,
729 IFX_SPI_POWER_DATA_PENDING);
Jiri Slaby6aad04f2013-03-07 13:12:29 +0100730 tty_port_tty_wakeup(&ifx_dev->tty_port);
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100731 }
732 }
733}
734
735/**
736 * ifx_spio_io - I/O tasklet
737 * @data: our SPI device
738 *
739 * Queue data for transmission if possible and then kick off the
740 * transfer.
741 */
742static void ifx_spi_io(unsigned long data)
743{
744 int retval;
745 struct ifx_spi_device *ifx_dev = (struct ifx_spi_device *) data;
746
chao bi31fe9902012-10-31 16:54:07 +0800747 if (!test_and_set_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &ifx_dev->flags) &&
748 test_bit(IFX_SPI_STATE_IO_AVAILABLE, &ifx_dev->flags)) {
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100749 if (ifx_dev->gpio.unack_srdy_int_nb > 0)
750 ifx_dev->gpio.unack_srdy_int_nb--;
751
752 ifx_spi_prepare_tx_buffer(ifx_dev);
753
754 spi_message_init(&ifx_dev->spi_msg);
755 INIT_LIST_HEAD(&ifx_dev->spi_msg.queue);
756
757 ifx_dev->spi_msg.context = ifx_dev;
758 ifx_dev->spi_msg.complete = ifx_spi_complete;
759
760 /* set up our spi transfer */
761 /* note len is BYTES, not transfers */
762 ifx_dev->spi_xfer.len = IFX_SPI_TRANSFER_SIZE;
763 ifx_dev->spi_xfer.cs_change = 0;
Russ Gorby1b79b4402011-02-07 12:02:30 -0800764 ifx_dev->spi_xfer.speed_hz = ifx_dev->spi_dev->max_speed_hz;
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100765 /* ifx_dev->spi_xfer.speed_hz = 390625; */
channing5dd070d2013-01-16 13:14:20 +0800766 ifx_dev->spi_xfer.bits_per_word =
767 ifx_dev->spi_dev->bits_per_word;
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100768
769 ifx_dev->spi_xfer.tx_buf = ifx_dev->tx_buffer;
770 ifx_dev->spi_xfer.rx_buf = ifx_dev->rx_buffer;
771
772 /*
773 * setup dma pointers
774 */
Russ Gorby2f1522e2011-02-02 12:56:58 -0800775 if (ifx_dev->use_dma) {
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100776 ifx_dev->spi_msg.is_dma_mapped = 1;
777 ifx_dev->tx_dma = ifx_dev->tx_bus;
778 ifx_dev->rx_dma = ifx_dev->rx_bus;
779 ifx_dev->spi_xfer.tx_dma = ifx_dev->tx_dma;
780 ifx_dev->spi_xfer.rx_dma = ifx_dev->rx_dma;
781 } else {
782 ifx_dev->spi_msg.is_dma_mapped = 0;
783 ifx_dev->tx_dma = (dma_addr_t)0;
784 ifx_dev->rx_dma = (dma_addr_t)0;
785 ifx_dev->spi_xfer.tx_dma = (dma_addr_t)0;
786 ifx_dev->spi_xfer.rx_dma = (dma_addr_t)0;
787 }
788
789 spi_message_add_tail(&ifx_dev->spi_xfer, &ifx_dev->spi_msg);
790
791 /* Assert MRDY. This may have already been done by the write
792 * routine.
793 */
794 mrdy_assert(ifx_dev);
795
796 retval = spi_async(ifx_dev->spi_dev, &ifx_dev->spi_msg);
797 if (retval) {
798 clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS,
799 &ifx_dev->flags);
800 tasklet_schedule(&ifx_dev->io_work_tasklet);
801 return;
802 }
803 } else
804 ifx_dev->write_pending = 1;
805}
806
807/**
808 * ifx_spi_free_port - free up the tty side
809 * @ifx_dev: IFX device going away
810 *
811 * Unregister and free up a port when the device goes away
812 */
813static void ifx_spi_free_port(struct ifx_spi_device *ifx_dev)
814{
815 if (ifx_dev->tty_dev)
816 tty_unregister_device(tty_drv, ifx_dev->minor);
Jiri Slaby191c5f12012-11-15 09:49:56 +0100817 tty_port_destroy(&ifx_dev->tty_port);
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100818 kfifo_free(&ifx_dev->tx_fifo);
819}
820
821/**
822 * ifx_spi_create_port - create a new port
823 * @ifx_dev: our spi device
824 *
825 * Allocate and initialise the tty port that goes with this interface
826 * and add it to the tty layer so that it can be opened.
827 */
828static int ifx_spi_create_port(struct ifx_spi_device *ifx_dev)
829{
830 int ret = 0;
831 struct tty_port *pport = &ifx_dev->tty_port;
832
833 spin_lock_init(&ifx_dev->fifo_lock);
834 lockdep_set_class_and_subclass(&ifx_dev->fifo_lock,
835 &ifx_spi_key, 0);
836
837 if (kfifo_alloc(&ifx_dev->tx_fifo, IFX_SPI_FIFO_SIZE, GFP_KERNEL)) {
838 ret = -ENOMEM;
839 goto error_ret;
840 }
841
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100842 tty_port_init(pport);
Russ Gorbyb68f23b2011-02-07 12:02:27 -0800843 pport->ops = &ifx_tty_port_ops;
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100844 ifx_dev->minor = IFX_SPI_TTY_ID;
Jiri Slaby734cc172012-08-07 21:47:47 +0200845 ifx_dev->tty_dev = tty_port_register_device(pport, tty_drv,
846 ifx_dev->minor, &ifx_dev->spi_dev->dev);
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100847 if (IS_ERR(ifx_dev->tty_dev)) {
848 dev_dbg(&ifx_dev->spi_dev->dev,
849 "%s: registering tty device failed", __func__);
850 ret = PTR_ERR(ifx_dev->tty_dev);
Jiri Slaby191c5f12012-11-15 09:49:56 +0100851 goto error_port;
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100852 }
853 return 0;
854
Jiri Slaby191c5f12012-11-15 09:49:56 +0100855error_port:
856 tty_port_destroy(pport);
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100857error_ret:
858 ifx_spi_free_port(ifx_dev);
859 return ret;
860}
861
862/**
863 * ifx_spi_handle_srdy - handle SRDY
864 * @ifx_dev: device asserting SRDY
865 *
866 * Check our device state and see what we need to kick off when SRDY
867 * is asserted. This usually means killing the timer and firing off the
868 * I/O processing.
869 */
870static void ifx_spi_handle_srdy(struct ifx_spi_device *ifx_dev)
871{
872 if (test_bit(IFX_SPI_STATE_TIMER_PENDING, &ifx_dev->flags)) {
Jun Chen2e308022012-10-19 09:51:30 -0400873 del_timer(&ifx_dev->spi_timer);
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100874 clear_bit(IFX_SPI_STATE_TIMER_PENDING, &ifx_dev->flags);
875 }
876
877 ifx_spi_power_state_set(ifx_dev, IFX_SPI_POWER_SRDY);
878
879 if (!test_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &ifx_dev->flags))
880 tasklet_schedule(&ifx_dev->io_work_tasklet);
881 else
882 set_bit(IFX_SPI_STATE_IO_READY, &ifx_dev->flags);
883}
884
885/**
886 * ifx_spi_srdy_interrupt - SRDY asserted
887 * @irq: our IRQ number
888 * @dev: our ifx device
889 *
890 * The modem asserted SRDY. Handle the srdy event
891 */
892static irqreturn_t ifx_spi_srdy_interrupt(int irq, void *dev)
893{
894 struct ifx_spi_device *ifx_dev = dev;
895 ifx_dev->gpio.unack_srdy_int_nb++;
896 ifx_spi_handle_srdy(ifx_dev);
897 return IRQ_HANDLED;
898}
899
900/**
901 * ifx_spi_reset_interrupt - Modem has changed reset state
902 * @irq: interrupt number
903 * @dev: our device pointer
904 *
905 * The modem has either entered or left reset state. Check the GPIO
906 * line to see which.
907 *
908 * FIXME: review locking on MR_INPROGRESS versus
909 * parallel unsolicited reset/solicited reset
910 */
911static irqreturn_t ifx_spi_reset_interrupt(int irq, void *dev)
912{
913 struct ifx_spi_device *ifx_dev = dev;
914 int val = gpio_get_value(ifx_dev->gpio.reset_out);
915 int solreset = test_bit(MR_START, &ifx_dev->mdm_reset_state);
916
917 if (val == 0) {
918 /* entered reset */
919 set_bit(MR_INPROGRESS, &ifx_dev->mdm_reset_state);
920 if (!solreset) {
921 /* unsolicited reset */
Jiri Slabyaa27a092013-03-07 13:12:30 +0100922 tty_port_tty_hangup(&ifx_dev->tty_port, false);
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100923 }
924 } else {
925 /* exited reset */
926 clear_bit(MR_INPROGRESS, &ifx_dev->mdm_reset_state);
927 if (solreset) {
928 set_bit(MR_COMPLETE, &ifx_dev->mdm_reset_state);
929 wake_up(&ifx_dev->mdm_reset_wait);
930 }
931 }
932 return IRQ_HANDLED;
933}
934
935/**
936 * ifx_spi_free_device - free device
937 * @ifx_dev: device to free
938 *
939 * Free the IFX device
940 */
941static void ifx_spi_free_device(struct ifx_spi_device *ifx_dev)
942{
943 ifx_spi_free_port(ifx_dev);
944 dma_free_coherent(&ifx_dev->spi_dev->dev,
945 IFX_SPI_TRANSFER_SIZE,
946 ifx_dev->tx_buffer,
947 ifx_dev->tx_bus);
948 dma_free_coherent(&ifx_dev->spi_dev->dev,
949 IFX_SPI_TRANSFER_SIZE,
950 ifx_dev->rx_buffer,
951 ifx_dev->rx_bus);
952}
953
954/**
955 * ifx_spi_reset - reset modem
956 * @ifx_dev: modem to reset
957 *
958 * Perform a reset on the modem
959 */
960static int ifx_spi_reset(struct ifx_spi_device *ifx_dev)
961{
962 int ret;
963 /*
964 * set up modem power, reset
965 *
966 * delays are required on some platforms for the modem
967 * to reset properly
968 */
969 set_bit(MR_START, &ifx_dev->mdm_reset_state);
970 gpio_set_value(ifx_dev->gpio.po, 0);
971 gpio_set_value(ifx_dev->gpio.reset, 0);
972 msleep(25);
973 gpio_set_value(ifx_dev->gpio.reset, 1);
974 msleep(1);
975 gpio_set_value(ifx_dev->gpio.po, 1);
976 msleep(1);
977 gpio_set_value(ifx_dev->gpio.po, 0);
978 ret = wait_event_timeout(ifx_dev->mdm_reset_wait,
979 test_bit(MR_COMPLETE,
980 &ifx_dev->mdm_reset_state),
981 IFX_RESET_TIMEOUT);
982 if (!ret)
983 dev_warn(&ifx_dev->spi_dev->dev, "Modem reset timeout: (state:%lx)",
984 ifx_dev->mdm_reset_state);
985
986 ifx_dev->mdm_reset_state = 0;
987 return ret;
988}
989
990/**
991 * ifx_spi_spi_probe - probe callback
992 * @spi: our possible matching SPI device
993 *
994 * Probe for a 6x60 modem on SPI bus. Perform any needed device and
995 * GPIO setup.
996 *
997 * FIXME:
998 * - Support for multiple devices
999 * - Split out MID specific GPIO handling eventually
1000 */
1001
1002static int ifx_spi_spi_probe(struct spi_device *spi)
1003{
1004 int ret;
1005 int srdy;
Russ Gorby2f1522e2011-02-02 12:56:58 -08001006 struct ifx_modem_platform_data *pl_data;
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001007 struct ifx_spi_device *ifx_dev;
1008
1009 if (saved_ifx_dev) {
1010 dev_dbg(&spi->dev, "ignoring subsequent detection");
1011 return -ENODEV;
1012 }
1013
Jingoo Han7a89c792013-09-09 14:09:55 +09001014 pl_data = dev_get_platdata(&spi->dev);
Russ Gorby2f1522e2011-02-02 12:56:58 -08001015 if (!pl_data) {
1016 dev_err(&spi->dev, "missing platform data!");
1017 return -ENODEV;
1018 }
1019
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001020 /* initialize structure to hold our device variables */
1021 ifx_dev = kzalloc(sizeof(struct ifx_spi_device), GFP_KERNEL);
1022 if (!ifx_dev) {
1023 dev_err(&spi->dev, "spi device allocation failed");
1024 return -ENOMEM;
1025 }
1026 saved_ifx_dev = ifx_dev;
1027 ifx_dev->spi_dev = spi;
1028 clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &ifx_dev->flags);
1029 spin_lock_init(&ifx_dev->write_lock);
1030 spin_lock_init(&ifx_dev->power_lock);
1031 ifx_dev->power_status = 0;
1032 init_timer(&ifx_dev->spi_timer);
1033 ifx_dev->spi_timer.function = ifx_spi_timeout;
1034 ifx_dev->spi_timer.data = (unsigned long)ifx_dev;
Russ Gorby2f1522e2011-02-02 12:56:58 -08001035 ifx_dev->modem = pl_data->modem_type;
1036 ifx_dev->use_dma = pl_data->use_dma;
1037 ifx_dev->max_hz = pl_data->max_hz;
Russ Gorby2aff8d92011-02-07 12:02:31 -08001038 /* initialize spi mode, etc */
Russ Gorby1b79b4402011-02-07 12:02:30 -08001039 spi->max_speed_hz = ifx_dev->max_hz;
Russ Gorby2aff8d92011-02-07 12:02:31 -08001040 spi->mode = IFX_SPI_MODE | (SPI_LOOP & spi->mode);
1041 spi->bits_per_word = spi_bpw;
1042 ret = spi_setup(spi);
1043 if (ret) {
1044 dev_err(&spi->dev, "SPI setup wasn't successful %d", ret);
1045 return -ENODEV;
1046 }
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001047
chao bi319fb0d2012-10-25 09:02:32 +08001048 /* init swap_buf function according to word width configuration */
1049 if (spi->bits_per_word == 32)
1050 ifx_dev->swap_buf = swap_buf_32;
1051 else if (spi->bits_per_word == 16)
1052 ifx_dev->swap_buf = swap_buf_16;
1053 else
1054 ifx_dev->swap_buf = swap_buf_8;
1055
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001056 /* ensure SPI protocol flags are initialized to enable transfer */
1057 ifx_dev->spi_more = 0;
1058 ifx_dev->spi_slave_cts = 0;
1059
1060 /*initialize transfer and dma buffers */
Russ Gorby5fc324952011-02-07 12:02:28 -08001061 ifx_dev->tx_buffer = dma_alloc_coherent(ifx_dev->spi_dev->dev.parent,
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001062 IFX_SPI_TRANSFER_SIZE,
1063 &ifx_dev->tx_bus,
1064 GFP_KERNEL);
1065 if (!ifx_dev->tx_buffer) {
1066 dev_err(&spi->dev, "DMA-TX buffer allocation failed");
1067 ret = -ENOMEM;
1068 goto error_ret;
1069 }
Russ Gorby5fc324952011-02-07 12:02:28 -08001070 ifx_dev->rx_buffer = dma_alloc_coherent(ifx_dev->spi_dev->dev.parent,
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001071 IFX_SPI_TRANSFER_SIZE,
1072 &ifx_dev->rx_bus,
1073 GFP_KERNEL);
1074 if (!ifx_dev->rx_buffer) {
1075 dev_err(&spi->dev, "DMA-RX buffer allocation failed");
1076 ret = -ENOMEM;
1077 goto error_ret;
1078 }
1079
1080 /* initialize waitq for modem reset */
1081 init_waitqueue_head(&ifx_dev->mdm_reset_wait);
1082
1083 spi_set_drvdata(spi, ifx_dev);
1084 tasklet_init(&ifx_dev->io_work_tasklet, ifx_spi_io,
1085 (unsigned long)ifx_dev);
1086
1087 set_bit(IFX_SPI_STATE_PRESENT, &ifx_dev->flags);
1088
1089 /* create our tty port */
1090 ret = ifx_spi_create_port(ifx_dev);
1091 if (ret != 0) {
1092 dev_err(&spi->dev, "create default tty port failed");
1093 goto error_ret;
1094 }
1095
Russ Gorby2f1522e2011-02-02 12:56:58 -08001096 ifx_dev->gpio.reset = pl_data->rst_pmu;
1097 ifx_dev->gpio.po = pl_data->pwr_on;
1098 ifx_dev->gpio.mrdy = pl_data->mrdy;
1099 ifx_dev->gpio.srdy = pl_data->srdy;
1100 ifx_dev->gpio.reset_out = pl_data->rst_out;
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001101
1102 dev_info(&spi->dev, "gpios %d, %d, %d, %d, %d",
1103 ifx_dev->gpio.reset, ifx_dev->gpio.po, ifx_dev->gpio.mrdy,
1104 ifx_dev->gpio.srdy, ifx_dev->gpio.reset_out);
1105
1106 /* Configure gpios */
1107 ret = gpio_request(ifx_dev->gpio.reset, "ifxModem");
1108 if (ret < 0) {
1109 dev_err(&spi->dev, "Unable to allocate GPIO%d (RESET)",
1110 ifx_dev->gpio.reset);
1111 goto error_ret;
1112 }
1113 ret += gpio_direction_output(ifx_dev->gpio.reset, 0);
1114 ret += gpio_export(ifx_dev->gpio.reset, 1);
1115 if (ret) {
1116 dev_err(&spi->dev, "Unable to configure GPIO%d (RESET)",
1117 ifx_dev->gpio.reset);
1118 ret = -EBUSY;
1119 goto error_ret2;
1120 }
1121
1122 ret = gpio_request(ifx_dev->gpio.po, "ifxModem");
1123 ret += gpio_direction_output(ifx_dev->gpio.po, 0);
1124 ret += gpio_export(ifx_dev->gpio.po, 1);
1125 if (ret) {
1126 dev_err(&spi->dev, "Unable to configure GPIO%d (ON)",
1127 ifx_dev->gpio.po);
1128 ret = -EBUSY;
1129 goto error_ret3;
1130 }
1131
1132 ret = gpio_request(ifx_dev->gpio.mrdy, "ifxModem");
1133 if (ret < 0) {
1134 dev_err(&spi->dev, "Unable to allocate GPIO%d (MRDY)",
1135 ifx_dev->gpio.mrdy);
1136 goto error_ret3;
1137 }
1138 ret += gpio_export(ifx_dev->gpio.mrdy, 1);
1139 ret += gpio_direction_output(ifx_dev->gpio.mrdy, 0);
1140 if (ret) {
1141 dev_err(&spi->dev, "Unable to configure GPIO%d (MRDY)",
1142 ifx_dev->gpio.mrdy);
1143 ret = -EBUSY;
1144 goto error_ret4;
1145 }
1146
1147 ret = gpio_request(ifx_dev->gpio.srdy, "ifxModem");
1148 if (ret < 0) {
1149 dev_err(&spi->dev, "Unable to allocate GPIO%d (SRDY)",
1150 ifx_dev->gpio.srdy);
1151 ret = -EBUSY;
1152 goto error_ret4;
1153 }
1154 ret += gpio_export(ifx_dev->gpio.srdy, 1);
1155 ret += gpio_direction_input(ifx_dev->gpio.srdy);
1156 if (ret) {
1157 dev_err(&spi->dev, "Unable to configure GPIO%d (SRDY)",
1158 ifx_dev->gpio.srdy);
1159 ret = -EBUSY;
1160 goto error_ret5;
1161 }
1162
1163 ret = gpio_request(ifx_dev->gpio.reset_out, "ifxModem");
1164 if (ret < 0) {
1165 dev_err(&spi->dev, "Unable to allocate GPIO%d (RESET_OUT)",
1166 ifx_dev->gpio.reset_out);
1167 goto error_ret5;
1168 }
1169 ret += gpio_export(ifx_dev->gpio.reset_out, 1);
1170 ret += gpio_direction_input(ifx_dev->gpio.reset_out);
1171 if (ret) {
1172 dev_err(&spi->dev, "Unable to configure GPIO%d (RESET_OUT)",
1173 ifx_dev->gpio.reset_out);
1174 ret = -EBUSY;
1175 goto error_ret6;
1176 }
1177
1178 ret = request_irq(gpio_to_irq(ifx_dev->gpio.reset_out),
1179 ifx_spi_reset_interrupt,
1180 IRQF_TRIGGER_RISING|IRQF_TRIGGER_FALLING, DRVNAME,
Geert Uytterhoevencbbcd1f2015-04-23 16:15:40 +02001181 ifx_dev);
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001182 if (ret) {
1183 dev_err(&spi->dev, "Unable to get irq %x\n",
1184 gpio_to_irq(ifx_dev->gpio.reset_out));
1185 goto error_ret6;
1186 }
1187
1188 ret = ifx_spi_reset(ifx_dev);
1189
1190 ret = request_irq(gpio_to_irq(ifx_dev->gpio.srdy),
Geert Uytterhoevencbbcd1f2015-04-23 16:15:40 +02001191 ifx_spi_srdy_interrupt, IRQF_TRIGGER_RISING, DRVNAME,
1192 ifx_dev);
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001193 if (ret) {
1194 dev_err(&spi->dev, "Unable to get irq %x",
1195 gpio_to_irq(ifx_dev->gpio.srdy));
Vasiliy Kulikovbadb9532010-11-19 21:42:03 +03001196 goto error_ret7;
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001197 }
1198
1199 /* set pm runtime power state and register with power system */
1200 pm_runtime_set_active(&spi->dev);
1201 pm_runtime_enable(&spi->dev);
1202
1203 /* handle case that modem is already signaling SRDY */
1204 /* no outgoing tty open at this point, this just satisfies the
1205 * modem's read and should reset communication properly
1206 */
1207 srdy = gpio_get_value(ifx_dev->gpio.srdy);
1208
1209 if (srdy) {
1210 mrdy_assert(ifx_dev);
1211 ifx_spi_handle_srdy(ifx_dev);
1212 } else
1213 mrdy_set_low(ifx_dev);
1214 return 0;
1215
Vasiliy Kulikovbadb9532010-11-19 21:42:03 +03001216error_ret7:
Geert Uytterhoevencbbcd1f2015-04-23 16:15:40 +02001217 free_irq(gpio_to_irq(ifx_dev->gpio.reset_out), ifx_dev);
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001218error_ret6:
1219 gpio_free(ifx_dev->gpio.srdy);
1220error_ret5:
1221 gpio_free(ifx_dev->gpio.mrdy);
1222error_ret4:
1223 gpio_free(ifx_dev->gpio.reset);
1224error_ret3:
1225 gpio_free(ifx_dev->gpio.po);
1226error_ret2:
1227 gpio_free(ifx_dev->gpio.reset_out);
1228error_ret:
1229 ifx_spi_free_device(ifx_dev);
1230 saved_ifx_dev = NULL;
1231 return ret;
1232}
1233
1234/**
1235 * ifx_spi_spi_remove - SPI device was removed
1236 * @spi: SPI device
1237 *
1238 * FIXME: We should be shutting the device down here not in
1239 * the module unload path.
1240 */
1241
1242static int ifx_spi_spi_remove(struct spi_device *spi)
1243{
1244 struct ifx_spi_device *ifx_dev = spi_get_drvdata(spi);
1245 /* stop activity */
1246 tasklet_kill(&ifx_dev->io_work_tasklet);
Chuhong Yuana45d1052019-11-18 10:48:33 +08001247
1248 pm_runtime_disable(&spi->dev);
1249
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001250 /* free irq */
Geert Uytterhoevencbbcd1f2015-04-23 16:15:40 +02001251 free_irq(gpio_to_irq(ifx_dev->gpio.reset_out), ifx_dev);
1252 free_irq(gpio_to_irq(ifx_dev->gpio.srdy), ifx_dev);
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001253
1254 gpio_free(ifx_dev->gpio.srdy);
1255 gpio_free(ifx_dev->gpio.mrdy);
1256 gpio_free(ifx_dev->gpio.reset);
1257 gpio_free(ifx_dev->gpio.po);
1258 gpio_free(ifx_dev->gpio.reset_out);
1259
1260 /* free allocations */
1261 ifx_spi_free_device(ifx_dev);
1262
1263 saved_ifx_dev = NULL;
1264 return 0;
1265}
1266
1267/**
1268 * ifx_spi_spi_shutdown - called on SPI shutdown
1269 * @spi: SPI device
1270 *
1271 * No action needs to be taken here
1272 */
1273
1274static void ifx_spi_spi_shutdown(struct spi_device *spi)
1275{
Jun Chen72d47242012-11-23 06:07:27 -05001276 struct ifx_spi_device *ifx_dev = spi_get_drvdata(spi);
1277
1278 ifx_modem_power_off(ifx_dev);
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001279}
1280
1281/*
1282 * various suspends and resumes have nothing to do
1283 * no hardware to save state for
1284 */
1285
1286/**
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001287 * ifx_spi_pm_suspend - suspend modem on system suspend
1288 * @dev: device being suspended
1289 *
1290 * Suspend the modem. No action needed on Intel MID platforms, may
1291 * need extending for other systems.
1292 */
1293static int ifx_spi_pm_suspend(struct device *dev)
1294{
1295 return 0;
1296}
1297
1298/**
1299 * ifx_spi_pm_resume - resume modem on system resume
1300 * @dev: device being suspended
1301 *
1302 * Allow the modem to resume. No action needed.
1303 *
1304 * FIXME: do we need to reset anything here ?
1305 */
1306static int ifx_spi_pm_resume(struct device *dev)
1307{
1308 return 0;
1309}
1310
1311/**
1312 * ifx_spi_pm_runtime_resume - suspend modem
1313 * @dev: device being suspended
1314 *
1315 * Allow the modem to resume. No action needed.
1316 */
1317static int ifx_spi_pm_runtime_resume(struct device *dev)
1318{
1319 return 0;
1320}
1321
1322/**
1323 * ifx_spi_pm_runtime_suspend - suspend modem
1324 * @dev: device being suspended
1325 *
1326 * Allow the modem to suspend and thus suspend to continue up the
1327 * device tree.
1328 */
1329static int ifx_spi_pm_runtime_suspend(struct device *dev)
1330{
1331 return 0;
1332}
1333
1334/**
1335 * ifx_spi_pm_runtime_idle - check if modem idle
1336 * @dev: our device
1337 *
1338 * Check conditions and queue runtime suspend if idle.
1339 */
1340static int ifx_spi_pm_runtime_idle(struct device *dev)
1341{
1342 struct spi_device *spi = to_spi_device(dev);
1343 struct ifx_spi_device *ifx_dev = spi_get_drvdata(spi);
1344
1345 if (!ifx_dev->power_status)
1346 pm_runtime_suspend(dev);
1347
1348 return 0;
1349}
1350
1351static const struct dev_pm_ops ifx_spi_pm = {
1352 .resume = ifx_spi_pm_resume,
1353 .suspend = ifx_spi_pm_suspend,
1354 .runtime_resume = ifx_spi_pm_runtime_resume,
1355 .runtime_suspend = ifx_spi_pm_runtime_suspend,
1356 .runtime_idle = ifx_spi_pm_runtime_idle
1357};
1358
1359static const struct spi_device_id ifx_id_table[] = {
1360 {"ifx6160", 0},
1361 {"ifx6260", 0},
1362 { }
1363};
1364MODULE_DEVICE_TABLE(spi, ifx_id_table);
1365
1366/* spi operations */
Fengguang Wu7d9739c2012-08-07 13:12:47 +08001367static struct spi_driver ifx_spi_driver = {
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001368 .driver = {
Russ Gorby8115be02011-02-07 12:02:32 -08001369 .name = DRVNAME,
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001370 .pm = &ifx_spi_pm,
Andrew F. Davis3821a062015-10-23 08:59:11 -05001371 },
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001372 .probe = ifx_spi_spi_probe,
1373 .shutdown = ifx_spi_spi_shutdown,
Bill Pemberton2d47b712012-11-19 13:21:34 -05001374 .remove = ifx_spi_spi_remove,
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001375 .id_table = ifx_id_table
1376};
1377
1378/**
1379 * ifx_spi_exit - module exit
1380 *
1381 * Unload the module.
1382 */
1383
1384static void __exit ifx_spi_exit(void)
1385{
1386 /* unregister */
Johan Hovold3802abc2017-04-26 12:24:21 +02001387 spi_unregister_driver(&ifx_spi_driver);
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001388 tty_unregister_driver(tty_drv);
Jun Chen72d47242012-11-23 06:07:27 -05001389 put_tty_driver(tty_drv);
Jun Chen72d47242012-11-23 06:07:27 -05001390 unregister_reboot_notifier(&ifx_modem_reboot_notifier_block);
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001391}
1392
1393/**
1394 * ifx_spi_init - module entry point
1395 *
1396 * Initialise the SPI and tty interfaces for the IFX SPI driver
1397 * We need to initialize upper-edge spi driver after the tty
1398 * driver because otherwise the spi probe will race
1399 */
1400
1401static int __init ifx_spi_init(void)
1402{
1403 int result;
1404
1405 tty_drv = alloc_tty_driver(1);
1406 if (!tty_drv) {
1407 pr_err("%s: alloc_tty_driver failed", DRVNAME);
1408 return -ENOMEM;
1409 }
1410
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001411 tty_drv->driver_name = DRVNAME;
1412 tty_drv->name = TTYNAME;
1413 tty_drv->minor_start = IFX_SPI_TTY_ID;
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001414 tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
1415 tty_drv->subtype = SERIAL_TYPE_NORMAL;
1416 tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1417 tty_drv->init_termios = tty_std_termios;
1418
1419 tty_set_operations(tty_drv, &ifx_spi_serial_ops);
1420
1421 result = tty_register_driver(tty_drv);
1422 if (result) {
1423 pr_err("%s: tty_register_driver failed(%d)",
1424 DRVNAME, result);
Jun Chen72d47242012-11-23 06:07:27 -05001425 goto err_free_tty;
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001426 }
1427
Geert Uytterhoeven9a499db2015-04-23 16:15:39 +02001428 result = spi_register_driver(&ifx_spi_driver);
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001429 if (result) {
1430 pr_err("%s: spi_register_driver failed(%d)",
1431 DRVNAME, result);
Jun Chen72d47242012-11-23 06:07:27 -05001432 goto err_unreg_tty;
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001433 }
Jun Chen72d47242012-11-23 06:07:27 -05001434
1435 result = register_reboot_notifier(&ifx_modem_reboot_notifier_block);
1436 if (result) {
1437 pr_err("%s: register ifx modem reboot notifier failed(%d)",
1438 DRVNAME, result);
1439 goto err_unreg_spi;
1440 }
1441
1442 return 0;
1443err_unreg_spi:
Geert Uytterhoeven9a499db2015-04-23 16:15:39 +02001444 spi_unregister_driver(&ifx_spi_driver);
Jun Chen72d47242012-11-23 06:07:27 -05001445err_unreg_tty:
1446 tty_unregister_driver(tty_drv);
1447err_free_tty:
1448 put_tty_driver(tty_drv);
1449
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001450 return result;
1451}
1452
1453module_init(ifx_spi_init);
1454module_exit(ifx_spi_exit);
1455
1456MODULE_AUTHOR("Intel");
1457MODULE_DESCRIPTION("IFX6x60 spi driver");
1458MODULE_LICENSE("GPL");
1459MODULE_INFO(Version, "0.1-IFX6x60");