blob: 7d16fe41932f2526c5fbbdd7377410c723ab62eb [file] [log] [blame]
Greg Kroah-Hartmane3b3d0f2017-11-06 18:11:51 +01001// SPDX-License-Identifier: GPL-2.0
Russ Gorbyaf3b8882010-10-26 14:13:52 +01002/****************************************************************************
3 *
4 * Driver for the IFX 6x60 spi modem.
5 *
6 * Copyright (C) 2008 Option International
7 * Copyright (C) 2008 Filip Aben <f.aben@option.com>
8 * Denis Joseph Barrow <d.barow@option.com>
9 * Jan Dumon <j.dumon@option.com>
10 *
11 * Copyright (C) 2009, 2010 Intel Corp
Russ Gorby2f1522e2011-02-02 12:56:58 -080012 * Russ Gorby <russ.gorby@intel.com>
Russ Gorbyaf3b8882010-10-26 14:13:52 +010013 *
Russ Gorbyaf3b8882010-10-26 14:13:52 +010014 * Driver modified by Intel from Option gtm501l_spi.c
15 *
16 * Notes
17 * o The driver currently assumes a single device only. If you need to
18 * change this then look for saved_ifx_dev and add a device lookup
19 * o The driver is intended to be big-endian safe but has never been
20 * tested that way (no suitable hardware). There are a couple of FIXME
21 * notes by areas that may need addressing
22 * o Some of the GPIO naming/setup assumptions may need revisiting if
23 * you need to use this driver for another platform.
24 *
25 *****************************************************************************/
Alexey Dobriyanb7f080c2011-06-16 11:01:34 +000026#include <linux/dma-mapping.h>
Russ Gorbyaf3b8882010-10-26 14:13:52 +010027#include <linux/module.h>
28#include <linux/termios.h>
29#include <linux/tty.h>
30#include <linux/device.h>
31#include <linux/spi/spi.h>
Russ Gorbyaf3b8882010-10-26 14:13:52 +010032#include <linux/kfifo.h>
33#include <linux/tty_flip.h>
34#include <linux/timer.h>
35#include <linux/serial.h>
36#include <linux/interrupt.h>
37#include <linux/irq.h>
38#include <linux/rfkill.h>
39#include <linux/fs.h>
40#include <linux/ip.h>
41#include <linux/dmapool.h>
Linus Walleij9a8da602020-03-11 09:31:31 +010042#include <linux/gpio/consumer.h>
Russ Gorbyaf3b8882010-10-26 14:13:52 +010043#include <linux/sched.h>
44#include <linux/time.h>
45#include <linux/wait.h>
Russ Gorbyaf3b8882010-10-26 14:13:52 +010046#include <linux/pm.h>
47#include <linux/pm_runtime.h>
48#include <linux/spi/ifx_modem.h>
Alan Cox83abd0d2010-11-12 10:46:23 +000049#include <linux/delay.h>
Jun Chen72d47242012-11-23 06:07:27 -050050#include <linux/reboot.h>
Russ Gorbyaf3b8882010-10-26 14:13:52 +010051
52#include "ifx6x60.h"
53
54#define IFX_SPI_MORE_MASK 0x10
chao bi1b2f8a92012-11-06 11:13:59 +080055#define IFX_SPI_MORE_BIT 4 /* bit position in u8 */
56#define IFX_SPI_CTS_BIT 6 /* bit position in u8 */
Russ Gorby2aff8d92011-02-07 12:02:31 -080057#define IFX_SPI_MODE SPI_MODE_1
Russ Gorbyaf3b8882010-10-26 14:13:52 +010058#define IFX_SPI_TTY_ID 0
59#define IFX_SPI_TIMEOUT_SEC 2
60#define IFX_SPI_HEADER_0 (-1)
61#define IFX_SPI_HEADER_F (-2)
62
Jun Chen72d47242012-11-23 06:07:27 -050063#define PO_POST_DELAY 200
Jun Chen72d47242012-11-23 06:07:27 -050064
Russ Gorbyaf3b8882010-10-26 14:13:52 +010065/* forward reference */
66static void ifx_spi_handle_srdy(struct ifx_spi_device *ifx_dev);
Jun Chen72d47242012-11-23 06:07:27 -050067static int ifx_modem_reboot_callback(struct notifier_block *nfb,
68 unsigned long event, void *data);
69static int ifx_modem_power_off(struct ifx_spi_device *ifx_dev);
Russ Gorbyaf3b8882010-10-26 14:13:52 +010070
71/* local variables */
Russ Gorbyf0891402011-02-07 12:02:29 -080072static int spi_bpw = 16; /* 8, 16 or 32 bit word length */
Russ Gorbyaf3b8882010-10-26 14:13:52 +010073static struct tty_driver *tty_drv;
74static struct ifx_spi_device *saved_ifx_dev;
75static struct lock_class_key ifx_spi_key;
76
Jun Chen72d47242012-11-23 06:07:27 -050077static struct notifier_block ifx_modem_reboot_notifier_block = {
78 .notifier_call = ifx_modem_reboot_callback,
79};
80
81static int ifx_modem_power_off(struct ifx_spi_device *ifx_dev)
82{
Linus Walleij9a8da602020-03-11 09:31:31 +010083 gpiod_set_value(ifx_dev->gpio.pmu_reset, 1);
Jun Chen72d47242012-11-23 06:07:27 -050084 msleep(PO_POST_DELAY);
85
86 return 0;
87}
88
89static int ifx_modem_reboot_callback(struct notifier_block *nfb,
90 unsigned long event, void *data)
91{
92 if (saved_ifx_dev)
93 ifx_modem_power_off(saved_ifx_dev);
94 else
95 pr_warn("no ifx modem active;\n");
96
97 return NOTIFY_OK;
98}
99
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100100/* GPIO/GPE settings */
101
102/**
103 * mrdy_set_high - set MRDY GPIO
104 * @ifx: device we are controlling
105 *
106 */
107static inline void mrdy_set_high(struct ifx_spi_device *ifx)
108{
Linus Walleij9a8da602020-03-11 09:31:31 +0100109 gpiod_set_value(ifx->gpio.mrdy, 1);
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100110}
111
112/**
113 * mrdy_set_low - clear MRDY GPIO
114 * @ifx: device we are controlling
115 *
116 */
117static inline void mrdy_set_low(struct ifx_spi_device *ifx)
118{
Linus Walleij9a8da602020-03-11 09:31:31 +0100119 gpiod_set_value(ifx->gpio.mrdy, 0);
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100120}
121
122/**
123 * ifx_spi_power_state_set
124 * @ifx_dev: our SPI device
125 * @val: bits to set
126 *
127 * Set bit in power status and signal power system if status becomes non-0
128 */
129static void
130ifx_spi_power_state_set(struct ifx_spi_device *ifx_dev, unsigned char val)
131{
132 unsigned long flags;
133
134 spin_lock_irqsave(&ifx_dev->power_lock, flags);
135
136 /*
137 * if power status is already non-0, just update, else
138 * tell power system
139 */
140 if (!ifx_dev->power_status)
141 pm_runtime_get(&ifx_dev->spi_dev->dev);
142 ifx_dev->power_status |= val;
143
144 spin_unlock_irqrestore(&ifx_dev->power_lock, flags);
145}
146
147/**
148 * ifx_spi_power_state_clear - clear power bit
149 * @ifx_dev: our SPI device
150 * @val: bits to clear
151 *
152 * clear bit in power status and signal power system if status becomes 0
153 */
154static void
155ifx_spi_power_state_clear(struct ifx_spi_device *ifx_dev, unsigned char val)
156{
157 unsigned long flags;
158
159 spin_lock_irqsave(&ifx_dev->power_lock, flags);
160
161 if (ifx_dev->power_status) {
162 ifx_dev->power_status &= ~val;
163 if (!ifx_dev->power_status)
164 pm_runtime_put(&ifx_dev->spi_dev->dev);
165 }
166
167 spin_unlock_irqrestore(&ifx_dev->power_lock, flags);
168}
169
170/**
chao bi319fb0d2012-10-25 09:02:32 +0800171 * swap_buf_8
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100172 * @buf: our buffer
173 * @len : number of bytes (not words) in the buffer
174 * @end: end of buffer
175 *
176 * Swap the contents of a buffer into big endian format
177 */
chao bi319fb0d2012-10-25 09:02:32 +0800178static inline void swap_buf_8(unsigned char *buf, int len, void *end)
179{
180 /* don't swap buffer if SPI word width is 8 bits */
181 return;
182}
183
184/**
185 * swap_buf_16
186 * @buf: our buffer
187 * @len : number of bytes (not words) in the buffer
188 * @end: end of buffer
189 *
190 * Swap the contents of a buffer into big endian format
191 */
192static inline void swap_buf_16(unsigned char *buf, int len, void *end)
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100193{
194 int n;
195
chao bi319fb0d2012-10-25 09:02:32 +0800196 u16 *buf_16 = (u16 *)buf;
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100197 len = ((len + 1) >> 1);
chao bi319fb0d2012-10-25 09:02:32 +0800198 if ((void *)&buf_16[len] > end) {
199 pr_err("swap_buf_16: swap exceeds boundary (%p > %p)!",
200 &buf_16[len], end);
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100201 return;
202 }
203 for (n = 0; n < len; n++) {
chao bi319fb0d2012-10-25 09:02:32 +0800204 *buf_16 = cpu_to_be16(*buf_16);
205 buf_16++;
206 }
207}
208
209/**
210 * swap_buf_32
211 * @buf: our buffer
212 * @len : number of bytes (not words) in the buffer
213 * @end: end of buffer
214 *
215 * Swap the contents of a buffer into big endian format
216 */
217static inline void swap_buf_32(unsigned char *buf, int len, void *end)
218{
219 int n;
220
221 u32 *buf_32 = (u32 *)buf;
222 len = (len + 3) >> 2;
223
224 if ((void *)&buf_32[len] > end) {
225 pr_err("swap_buf_32: swap exceeds boundary (%p > %p)!\n",
226 &buf_32[len], end);
227 return;
228 }
229 for (n = 0; n < len; n++) {
230 *buf_32 = cpu_to_be32(*buf_32);
231 buf_32++;
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100232 }
233}
234
235/**
236 * mrdy_assert - assert MRDY line
237 * @ifx_dev: our SPI device
238 *
239 * Assert mrdy and set timer to wait for SRDY interrupt, if SRDY is low
240 * now.
241 *
242 * FIXME: Can SRDY even go high as we are running this code ?
243 */
244static void mrdy_assert(struct ifx_spi_device *ifx_dev)
245{
Linus Walleij9a8da602020-03-11 09:31:31 +0100246 int val = gpiod_get_value(ifx_dev->gpio.srdy);
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100247 if (!val) {
248 if (!test_and_set_bit(IFX_SPI_STATE_TIMER_PENDING,
249 &ifx_dev->flags)) {
Jun Chenc73ba2a2012-10-22 10:23:07 -0400250 mod_timer(&ifx_dev->spi_timer,jiffies + IFX_SPI_TIMEOUT_SEC*HZ);
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100251
252 }
253 }
254 ifx_spi_power_state_set(ifx_dev, IFX_SPI_POWER_DATA_PENDING);
255 mrdy_set_high(ifx_dev);
256}
257
258/**
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100259 * ifx_spi_timeout - SPI timeout
260 * @arg: our SPI device
261 *
262 * The SPI has timed out: hang up the tty. Users will then see a hangup
263 * and error events.
264 */
Kees Cooke99e88a2017-10-16 14:43:17 -0700265static void ifx_spi_timeout(struct timer_list *t)
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100266{
Kees Cooke99e88a2017-10-16 14:43:17 -0700267 struct ifx_spi_device *ifx_dev = from_timer(ifx_dev, t, spi_timer);
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100268
269 dev_warn(&ifx_dev->spi_dev->dev, "*** SPI Timeout ***");
Jiri Slabyaa27a092013-03-07 13:12:30 +0100270 tty_port_tty_hangup(&ifx_dev->tty_port, false);
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100271 mrdy_set_low(ifx_dev);
272 clear_bit(IFX_SPI_STATE_TIMER_PENDING, &ifx_dev->flags);
273}
274
275/* char/tty operations */
276
277/**
278 * ifx_spi_tiocmget - get modem lines
279 * @tty: our tty device
280 * @filp: file handle issuing the request
281 *
282 * Map the signal state into Linux modem flags and report the value
283 * in Linux terms
284 */
Alan Cox60b33c12011-02-14 16:26:14 +0000285static int ifx_spi_tiocmget(struct tty_struct *tty)
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100286{
287 unsigned int value;
288 struct ifx_spi_device *ifx_dev = tty->driver_data;
289
290 value =
291 (test_bit(IFX_SPI_RTS, &ifx_dev->signal_state) ? TIOCM_RTS : 0) |
292 (test_bit(IFX_SPI_DTR, &ifx_dev->signal_state) ? TIOCM_DTR : 0) |
293 (test_bit(IFX_SPI_CTS, &ifx_dev->signal_state) ? TIOCM_CTS : 0) |
294 (test_bit(IFX_SPI_DSR, &ifx_dev->signal_state) ? TIOCM_DSR : 0) |
295 (test_bit(IFX_SPI_DCD, &ifx_dev->signal_state) ? TIOCM_CAR : 0) |
296 (test_bit(IFX_SPI_RI, &ifx_dev->signal_state) ? TIOCM_RNG : 0);
297 return value;
298}
299
300/**
301 * ifx_spi_tiocmset - set modem bits
302 * @tty: the tty structure
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100303 * @set: bits to set
304 * @clear: bits to clear
305 *
306 * The IFX6x60 only supports DTR and RTS. Set them accordingly
307 * and flag that an update to the modem is needed.
308 *
309 * FIXME: do we need to kick the tranfers when we do this ?
310 */
Alan Cox20b9d172011-02-14 16:26:50 +0000311static int ifx_spi_tiocmset(struct tty_struct *tty,
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100312 unsigned int set, unsigned int clear)
313{
314 struct ifx_spi_device *ifx_dev = tty->driver_data;
315
316 if (set & TIOCM_RTS)
317 set_bit(IFX_SPI_RTS, &ifx_dev->signal_state);
318 if (set & TIOCM_DTR)
319 set_bit(IFX_SPI_DTR, &ifx_dev->signal_state);
320 if (clear & TIOCM_RTS)
321 clear_bit(IFX_SPI_RTS, &ifx_dev->signal_state);
322 if (clear & TIOCM_DTR)
323 clear_bit(IFX_SPI_DTR, &ifx_dev->signal_state);
324
325 set_bit(IFX_SPI_UPDATE, &ifx_dev->signal_state);
326 return 0;
327}
328
329/**
330 * ifx_spi_open - called on tty open
331 * @tty: our tty device
332 * @filp: file handle being associated with the tty
333 *
334 * Open the tty interface. We let the tty_port layer do all the work
335 * for us.
336 *
337 * FIXME: Remove single device assumption and saved_ifx_dev
338 */
339static int ifx_spi_open(struct tty_struct *tty, struct file *filp)
340{
341 return tty_port_open(&saved_ifx_dev->tty_port, tty, filp);
342}
343
344/**
345 * ifx_spi_close - called when our tty closes
346 * @tty: the tty being closed
347 * @filp: the file handle being closed
348 *
349 * Perform the close of the tty. We use the tty_port layer to do all
350 * our hard work.
351 */
352static void ifx_spi_close(struct tty_struct *tty, struct file *filp)
353{
354 struct ifx_spi_device *ifx_dev = tty->driver_data;
355 tty_port_close(&ifx_dev->tty_port, tty, filp);
356 /* FIXME: should we do an ifx_spi_reset here ? */
357}
358
359/**
360 * ifx_decode_spi_header - decode received header
361 * @buffer: the received data
362 * @length: decoded length
363 * @more: decoded more flag
364 * @received_cts: status of cts we received
365 *
366 * Note how received_cts is handled -- if header is all F it is left
367 * the same as it was, if header is all 0 it is set to 0 otherwise it is
368 * taken from the incoming header.
369 *
370 * FIXME: endianness
371 */
372static int ifx_spi_decode_spi_header(unsigned char *buffer, int *length,
373 unsigned char *more, unsigned char *received_cts)
374{
375 u16 h1;
376 u16 h2;
377 u16 *in_buffer = (u16 *)buffer;
378
379 h1 = *in_buffer;
380 h2 = *(in_buffer+1);
381
382 if (h1 == 0 && h2 == 0) {
383 *received_cts = 0;
Arnd Bergmann0b8053f2016-02-25 21:47:57 +0100384 *more = 0;
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100385 return IFX_SPI_HEADER_0;
386 } else if (h1 == 0xffff && h2 == 0xffff) {
Arnd Bergmann0b8053f2016-02-25 21:47:57 +0100387 *more = 0;
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100388 /* spi_slave_cts remains as it was */
389 return IFX_SPI_HEADER_F;
390 }
391
392 *length = h1 & 0xfff; /* upper bits of byte are flags */
393 *more = (buffer[1] >> IFX_SPI_MORE_BIT) & 1;
394 *received_cts = (buffer[3] >> IFX_SPI_CTS_BIT) & 1;
395 return 0;
396}
397
398/**
399 * ifx_setup_spi_header - set header fields
400 * @txbuffer: pointer to start of SPI buffer
401 * @tx_count: bytes
402 * @more: indicate if more to follow
403 *
404 * Format up an SPI header for a transfer
405 *
406 * FIXME: endianness?
407 */
408static void ifx_spi_setup_spi_header(unsigned char *txbuffer, int tx_count,
409 unsigned char more)
410{
411 *(u16 *)(txbuffer) = tx_count;
412 *(u16 *)(txbuffer+2) = IFX_SPI_PAYLOAD_SIZE;
413 txbuffer[1] |= (more << IFX_SPI_MORE_BIT) & IFX_SPI_MORE_MASK;
414}
415
416/**
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100417 * ifx_spi_prepare_tx_buffer - prepare transmit frame
418 * @ifx_dev: our SPI device
419 *
420 * The transmit buffr needs a header and various other bits of
421 * information followed by as much data as we can pull from the FIFO
422 * and transfer. This function formats up a suitable buffer in the
423 * ifx_dev->tx_buffer
424 *
425 * FIXME: performance - should we wake the tty when the queue is half
426 * empty ?
427 */
428static int ifx_spi_prepare_tx_buffer(struct ifx_spi_device *ifx_dev)
429{
430 int temp_count;
431 int queue_length;
432 int tx_count;
433 unsigned char *tx_buffer;
434
435 tx_buffer = ifx_dev->tx_buffer;
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100436
437 /* make room for required SPI header */
438 tx_buffer += IFX_SPI_HEADER_OVERHEAD;
439 tx_count = IFX_SPI_HEADER_OVERHEAD;
440
441 /* clear to signal no more data if this turns out to be the
442 * last buffer sent in a sequence */
443 ifx_dev->spi_more = 0;
444
445 /* if modem cts is set, just send empty buffer */
446 if (!ifx_dev->spi_slave_cts) {
447 /* see if there's tx data */
448 queue_length = kfifo_len(&ifx_dev->tx_fifo);
449 if (queue_length != 0) {
450 /* data to mux -- see if there's room for it */
451 temp_count = min(queue_length, IFX_SPI_PAYLOAD_SIZE);
452 temp_count = kfifo_out_locked(&ifx_dev->tx_fifo,
453 tx_buffer, temp_count,
454 &ifx_dev->fifo_lock);
455
456 /* update buffer pointer and data count in message */
457 tx_buffer += temp_count;
458 tx_count += temp_count;
459 if (temp_count == queue_length)
460 /* poke port to get more data */
Jiri Slaby6aad04f2013-03-07 13:12:29 +0100461 tty_port_tty_wakeup(&ifx_dev->tty_port);
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100462 else /* more data in port, use next SPI message */
463 ifx_dev->spi_more = 1;
464 }
465 }
466 /* have data and info for header -- set up SPI header in buffer */
467 /* spi header needs payload size, not entire buffer size */
468 ifx_spi_setup_spi_header(ifx_dev->tx_buffer,
469 tx_count-IFX_SPI_HEADER_OVERHEAD,
470 ifx_dev->spi_more);
471 /* swap actual data in the buffer */
chao bi319fb0d2012-10-25 09:02:32 +0800472 ifx_dev->swap_buf((ifx_dev->tx_buffer), tx_count,
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100473 &ifx_dev->tx_buffer[IFX_SPI_TRANSFER_SIZE]);
474 return tx_count;
475}
476
477/**
478 * ifx_spi_write - line discipline write
479 * @tty: our tty device
480 * @buf: pointer to buffer to write (kernel space)
481 * @count: size of buffer
482 *
483 * Write the characters we have been given into the FIFO. If the device
484 * is not active then activate it, when the SRDY line is asserted back
485 * this will commence I/O
486 */
487static int ifx_spi_write(struct tty_struct *tty, const unsigned char *buf,
488 int count)
489{
490 struct ifx_spi_device *ifx_dev = tty->driver_data;
491 unsigned char *tmp_buf = (unsigned char *)buf;
Jun Chene8823f12012-11-07 12:04:04 -0500492 unsigned long flags;
493 bool is_fifo_empty;
Randy Dunlapbc6835a2012-11-26 11:07:25 -0800494 int tx_count;
Jun Chene8823f12012-11-07 12:04:04 -0500495
496 spin_lock_irqsave(&ifx_dev->fifo_lock, flags);
497 is_fifo_empty = kfifo_is_empty(&ifx_dev->tx_fifo);
Randy Dunlapbc6835a2012-11-26 11:07:25 -0800498 tx_count = kfifo_in(&ifx_dev->tx_fifo, tmp_buf, count);
Jun Chene8823f12012-11-07 12:04:04 -0500499 spin_unlock_irqrestore(&ifx_dev->fifo_lock, flags);
500 if (is_fifo_empty)
501 mrdy_assert(ifx_dev);
502
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100503 return tx_count;
504}
505
506/**
507 * ifx_spi_chars_in_buffer - line discipline helper
508 * @tty: our tty device
509 *
510 * Report how much data we can accept before we drop bytes. As we use
511 * a simple FIFO this is nice and easy.
512 */
513static int ifx_spi_write_room(struct tty_struct *tty)
514{
515 struct ifx_spi_device *ifx_dev = tty->driver_data;
516 return IFX_SPI_FIFO_SIZE - kfifo_len(&ifx_dev->tx_fifo);
517}
518
519/**
520 * ifx_spi_chars_in_buffer - line discipline helper
521 * @tty: our tty device
522 *
523 * Report how many characters we have buffered. In our case this is the
524 * number of bytes sitting in our transmit FIFO.
525 */
526static int ifx_spi_chars_in_buffer(struct tty_struct *tty)
527{
528 struct ifx_spi_device *ifx_dev = tty->driver_data;
529 return kfifo_len(&ifx_dev->tx_fifo);
530}
531
532/**
533 * ifx_port_hangup
534 * @port: our tty port
535 *
536 * tty port hang up. Called when tty_hangup processing is invoked either
537 * by loss of carrier, or by software (eg vhangup). Serialized against
538 * activate/shutdown by the tty layer.
539 */
540static void ifx_spi_hangup(struct tty_struct *tty)
541{
542 struct ifx_spi_device *ifx_dev = tty->driver_data;
543 tty_port_hangup(&ifx_dev->tty_port);
544}
545
546/**
547 * ifx_port_activate
548 * @port: our tty port
549 *
550 * tty port activate method - called for first open. Serialized
551 * with hangup and shutdown by the tty layer.
552 */
553static int ifx_port_activate(struct tty_port *port, struct tty_struct *tty)
554{
555 struct ifx_spi_device *ifx_dev =
556 container_of(port, struct ifx_spi_device, tty_port);
557
558 /* clear any old data; can't do this in 'close' */
559 kfifo_reset(&ifx_dev->tx_fifo);
560
chao bi31fe9902012-10-31 16:54:07 +0800561 /* clear any flag which may be set in port shutdown procedure */
562 clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &ifx_dev->flags);
563 clear_bit(IFX_SPI_STATE_IO_READY, &ifx_dev->flags);
564
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100565 /* put port data into this tty */
566 tty->driver_data = ifx_dev;
567
568 /* allows flip string push from int context */
Jiri Slabyd6c53c02013-01-03 15:53:05 +0100569 port->low_latency = 1;
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100570
chao bi31fe9902012-10-31 16:54:07 +0800571 /* set flag to allows data transfer */
572 set_bit(IFX_SPI_STATE_IO_AVAILABLE, &ifx_dev->flags);
573
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100574 return 0;
575}
576
577/**
578 * ifx_port_shutdown
579 * @port: our tty port
580 *
581 * tty port shutdown method - called for last port close. Serialized
582 * with hangup and activate by the tty layer.
583 */
584static void ifx_port_shutdown(struct tty_port *port)
585{
586 struct ifx_spi_device *ifx_dev =
587 container_of(port, struct ifx_spi_device, tty_port);
588
chao bi31fe9902012-10-31 16:54:07 +0800589 clear_bit(IFX_SPI_STATE_IO_AVAILABLE, &ifx_dev->flags);
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100590 mrdy_set_low(ifx_dev);
chao bi014b9b42012-12-12 11:40:56 +0800591 del_timer(&ifx_dev->spi_timer);
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100592 clear_bit(IFX_SPI_STATE_TIMER_PENDING, &ifx_dev->flags);
593 tasklet_kill(&ifx_dev->io_work_tasklet);
594}
595
596static const struct tty_port_operations ifx_tty_port_ops = {
597 .activate = ifx_port_activate,
598 .shutdown = ifx_port_shutdown,
599};
600
601static const struct tty_operations ifx_spi_serial_ops = {
602 .open = ifx_spi_open,
603 .close = ifx_spi_close,
604 .write = ifx_spi_write,
605 .hangup = ifx_spi_hangup,
606 .write_room = ifx_spi_write_room,
607 .chars_in_buffer = ifx_spi_chars_in_buffer,
608 .tiocmget = ifx_spi_tiocmget,
609 .tiocmset = ifx_spi_tiocmset,
610};
611
612/**
613 * ifx_spi_insert_fip_string - queue received data
614 * @ifx_ser: our SPI device
615 * @chars: buffer we have received
616 * @size: number of chars reeived
617 *
618 * Queue bytes to the tty assuming the tty side is currently open. If
619 * not the discard the data.
620 */
621static void ifx_spi_insert_flip_string(struct ifx_spi_device *ifx_dev,
622 unsigned char *chars, size_t size)
623{
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100624 tty_insert_flip_string(&ifx_dev->tty_port, chars, size);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100625 tty_flip_buffer_push(&ifx_dev->tty_port);
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100626}
627
628/**
629 * ifx_spi_complete - SPI transfer completed
630 * @ctx: our SPI device
631 *
632 * An SPI transfer has completed. Process any received data and kick off
633 * any further transmits we can commence.
634 */
635static void ifx_spi_complete(void *ctx)
636{
637 struct ifx_spi_device *ifx_dev = ctx;
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100638 int length;
639 int actual_length;
Jiri Slabyeeeabba2016-03-31 10:23:34 +0200640 unsigned char more = 0;
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100641 unsigned char cts;
642 int local_write_pending = 0;
643 int queue_length;
644 int srdy;
645 int decode_result;
646
647 mrdy_set_low(ifx_dev);
648
649 if (!ifx_dev->spi_msg.status) {
650 /* check header validity, get comm flags */
chao bi319fb0d2012-10-25 09:02:32 +0800651 ifx_dev->swap_buf(ifx_dev->rx_buffer, IFX_SPI_HEADER_OVERHEAD,
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100652 &ifx_dev->rx_buffer[IFX_SPI_HEADER_OVERHEAD]);
653 decode_result = ifx_spi_decode_spi_header(ifx_dev->rx_buffer,
654 &length, &more, &cts);
655 if (decode_result == IFX_SPI_HEADER_0) {
656 dev_dbg(&ifx_dev->spi_dev->dev,
657 "ignore input: invalid header 0");
658 ifx_dev->spi_slave_cts = 0;
659 goto complete_exit;
660 } else if (decode_result == IFX_SPI_HEADER_F) {
661 dev_dbg(&ifx_dev->spi_dev->dev,
662 "ignore input: invalid header F");
663 goto complete_exit;
664 }
665
666 ifx_dev->spi_slave_cts = cts;
667
668 actual_length = min((unsigned int)length,
669 ifx_dev->spi_msg.actual_length);
chao bi319fb0d2012-10-25 09:02:32 +0800670 ifx_dev->swap_buf(
671 (ifx_dev->rx_buffer + IFX_SPI_HEADER_OVERHEAD),
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100672 actual_length,
673 &ifx_dev->rx_buffer[IFX_SPI_TRANSFER_SIZE]);
674 ifx_spi_insert_flip_string(
675 ifx_dev,
676 ifx_dev->rx_buffer + IFX_SPI_HEADER_OVERHEAD,
677 (size_t)actual_length);
678 } else {
Arnd Bergmann0b8053f2016-02-25 21:47:57 +0100679 more = 0;
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100680 dev_dbg(&ifx_dev->spi_dev->dev, "SPI transfer error %d",
681 ifx_dev->spi_msg.status);
682 }
683
684complete_exit:
685 if (ifx_dev->write_pending) {
686 ifx_dev->write_pending = 0;
687 local_write_pending = 1;
688 }
689
690 clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &(ifx_dev->flags));
691
692 queue_length = kfifo_len(&ifx_dev->tx_fifo);
Linus Walleij9a8da602020-03-11 09:31:31 +0100693 srdy = gpiod_get_value(ifx_dev->gpio.srdy);
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100694 if (!srdy)
695 ifx_spi_power_state_clear(ifx_dev, IFX_SPI_POWER_SRDY);
696
697 /* schedule output if there is more to do */
698 if (test_and_clear_bit(IFX_SPI_STATE_IO_READY, &ifx_dev->flags))
699 tasklet_schedule(&ifx_dev->io_work_tasklet);
700 else {
701 if (more || ifx_dev->spi_more || queue_length > 0 ||
702 local_write_pending) {
703 if (ifx_dev->spi_slave_cts) {
704 if (more)
705 mrdy_assert(ifx_dev);
706 } else
707 mrdy_assert(ifx_dev);
708 } else {
709 /*
710 * poke line discipline driver if any for more data
711 * may or may not get more data to write
712 * for now, say not busy
713 */
714 ifx_spi_power_state_clear(ifx_dev,
715 IFX_SPI_POWER_DATA_PENDING);
Jiri Slaby6aad04f2013-03-07 13:12:29 +0100716 tty_port_tty_wakeup(&ifx_dev->tty_port);
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100717 }
718 }
719}
720
721/**
722 * ifx_spio_io - I/O tasklet
723 * @data: our SPI device
724 *
725 * Queue data for transmission if possible and then kick off the
726 * transfer.
727 */
728static void ifx_spi_io(unsigned long data)
729{
730 int retval;
731 struct ifx_spi_device *ifx_dev = (struct ifx_spi_device *) data;
732
chao bi31fe9902012-10-31 16:54:07 +0800733 if (!test_and_set_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &ifx_dev->flags) &&
734 test_bit(IFX_SPI_STATE_IO_AVAILABLE, &ifx_dev->flags)) {
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100735 if (ifx_dev->gpio.unack_srdy_int_nb > 0)
736 ifx_dev->gpio.unack_srdy_int_nb--;
737
738 ifx_spi_prepare_tx_buffer(ifx_dev);
739
740 spi_message_init(&ifx_dev->spi_msg);
741 INIT_LIST_HEAD(&ifx_dev->spi_msg.queue);
742
743 ifx_dev->spi_msg.context = ifx_dev;
744 ifx_dev->spi_msg.complete = ifx_spi_complete;
745
746 /* set up our spi transfer */
747 /* note len is BYTES, not transfers */
748 ifx_dev->spi_xfer.len = IFX_SPI_TRANSFER_SIZE;
749 ifx_dev->spi_xfer.cs_change = 0;
Russ Gorby1b79b442011-02-07 12:02:30 -0800750 ifx_dev->spi_xfer.speed_hz = ifx_dev->spi_dev->max_speed_hz;
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100751 /* ifx_dev->spi_xfer.speed_hz = 390625; */
channing5dd070d2013-01-16 13:14:20 +0800752 ifx_dev->spi_xfer.bits_per_word =
753 ifx_dev->spi_dev->bits_per_word;
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100754
755 ifx_dev->spi_xfer.tx_buf = ifx_dev->tx_buffer;
756 ifx_dev->spi_xfer.rx_buf = ifx_dev->rx_buffer;
757
758 /*
759 * setup dma pointers
760 */
Russ Gorby2f1522e2011-02-02 12:56:58 -0800761 if (ifx_dev->use_dma) {
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100762 ifx_dev->spi_msg.is_dma_mapped = 1;
763 ifx_dev->tx_dma = ifx_dev->tx_bus;
764 ifx_dev->rx_dma = ifx_dev->rx_bus;
765 ifx_dev->spi_xfer.tx_dma = ifx_dev->tx_dma;
766 ifx_dev->spi_xfer.rx_dma = ifx_dev->rx_dma;
767 } else {
768 ifx_dev->spi_msg.is_dma_mapped = 0;
769 ifx_dev->tx_dma = (dma_addr_t)0;
770 ifx_dev->rx_dma = (dma_addr_t)0;
771 ifx_dev->spi_xfer.tx_dma = (dma_addr_t)0;
772 ifx_dev->spi_xfer.rx_dma = (dma_addr_t)0;
773 }
774
775 spi_message_add_tail(&ifx_dev->spi_xfer, &ifx_dev->spi_msg);
776
777 /* Assert MRDY. This may have already been done by the write
778 * routine.
779 */
780 mrdy_assert(ifx_dev);
781
782 retval = spi_async(ifx_dev->spi_dev, &ifx_dev->spi_msg);
783 if (retval) {
784 clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS,
785 &ifx_dev->flags);
786 tasklet_schedule(&ifx_dev->io_work_tasklet);
787 return;
788 }
789 } else
790 ifx_dev->write_pending = 1;
791}
792
793/**
794 * ifx_spi_free_port - free up the tty side
795 * @ifx_dev: IFX device going away
796 *
797 * Unregister and free up a port when the device goes away
798 */
799static void ifx_spi_free_port(struct ifx_spi_device *ifx_dev)
800{
801 if (ifx_dev->tty_dev)
802 tty_unregister_device(tty_drv, ifx_dev->minor);
Jiri Slaby191c5f12012-11-15 09:49:56 +0100803 tty_port_destroy(&ifx_dev->tty_port);
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100804 kfifo_free(&ifx_dev->tx_fifo);
805}
806
807/**
808 * ifx_spi_create_port - create a new port
809 * @ifx_dev: our spi device
810 *
811 * Allocate and initialise the tty port that goes with this interface
812 * and add it to the tty layer so that it can be opened.
813 */
814static int ifx_spi_create_port(struct ifx_spi_device *ifx_dev)
815{
816 int ret = 0;
817 struct tty_port *pport = &ifx_dev->tty_port;
818
819 spin_lock_init(&ifx_dev->fifo_lock);
820 lockdep_set_class_and_subclass(&ifx_dev->fifo_lock,
821 &ifx_spi_key, 0);
822
823 if (kfifo_alloc(&ifx_dev->tx_fifo, IFX_SPI_FIFO_SIZE, GFP_KERNEL)) {
824 ret = -ENOMEM;
825 goto error_ret;
826 }
827
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100828 tty_port_init(pport);
Russ Gorbyb68f23b2011-02-07 12:02:27 -0800829 pport->ops = &ifx_tty_port_ops;
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100830 ifx_dev->minor = IFX_SPI_TTY_ID;
Jiri Slaby734cc172012-08-07 21:47:47 +0200831 ifx_dev->tty_dev = tty_port_register_device(pport, tty_drv,
832 ifx_dev->minor, &ifx_dev->spi_dev->dev);
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100833 if (IS_ERR(ifx_dev->tty_dev)) {
834 dev_dbg(&ifx_dev->spi_dev->dev,
835 "%s: registering tty device failed", __func__);
836 ret = PTR_ERR(ifx_dev->tty_dev);
Jiri Slaby191c5f12012-11-15 09:49:56 +0100837 goto error_port;
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100838 }
839 return 0;
840
Jiri Slaby191c5f12012-11-15 09:49:56 +0100841error_port:
842 tty_port_destroy(pport);
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100843error_ret:
844 ifx_spi_free_port(ifx_dev);
845 return ret;
846}
847
848/**
849 * ifx_spi_handle_srdy - handle SRDY
850 * @ifx_dev: device asserting SRDY
851 *
852 * Check our device state and see what we need to kick off when SRDY
853 * is asserted. This usually means killing the timer and firing off the
854 * I/O processing.
855 */
856static void ifx_spi_handle_srdy(struct ifx_spi_device *ifx_dev)
857{
858 if (test_bit(IFX_SPI_STATE_TIMER_PENDING, &ifx_dev->flags)) {
Jun Chen2e308022012-10-19 09:51:30 -0400859 del_timer(&ifx_dev->spi_timer);
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100860 clear_bit(IFX_SPI_STATE_TIMER_PENDING, &ifx_dev->flags);
861 }
862
863 ifx_spi_power_state_set(ifx_dev, IFX_SPI_POWER_SRDY);
864
865 if (!test_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &ifx_dev->flags))
866 tasklet_schedule(&ifx_dev->io_work_tasklet);
867 else
868 set_bit(IFX_SPI_STATE_IO_READY, &ifx_dev->flags);
869}
870
871/**
872 * ifx_spi_srdy_interrupt - SRDY asserted
873 * @irq: our IRQ number
874 * @dev: our ifx device
875 *
876 * The modem asserted SRDY. Handle the srdy event
877 */
878static irqreturn_t ifx_spi_srdy_interrupt(int irq, void *dev)
879{
880 struct ifx_spi_device *ifx_dev = dev;
881 ifx_dev->gpio.unack_srdy_int_nb++;
882 ifx_spi_handle_srdy(ifx_dev);
883 return IRQ_HANDLED;
884}
885
886/**
887 * ifx_spi_reset_interrupt - Modem has changed reset state
888 * @irq: interrupt number
889 * @dev: our device pointer
890 *
891 * The modem has either entered or left reset state. Check the GPIO
892 * line to see which.
893 *
894 * FIXME: review locking on MR_INPROGRESS versus
895 * parallel unsolicited reset/solicited reset
896 */
897static irqreturn_t ifx_spi_reset_interrupt(int irq, void *dev)
898{
899 struct ifx_spi_device *ifx_dev = dev;
Linus Walleij9a8da602020-03-11 09:31:31 +0100900 int val = gpiod_get_value(ifx_dev->gpio.reset_out);
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100901 int solreset = test_bit(MR_START, &ifx_dev->mdm_reset_state);
902
903 if (val == 0) {
904 /* entered reset */
905 set_bit(MR_INPROGRESS, &ifx_dev->mdm_reset_state);
906 if (!solreset) {
907 /* unsolicited reset */
Jiri Slabyaa27a092013-03-07 13:12:30 +0100908 tty_port_tty_hangup(&ifx_dev->tty_port, false);
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100909 }
910 } else {
911 /* exited reset */
912 clear_bit(MR_INPROGRESS, &ifx_dev->mdm_reset_state);
913 if (solreset) {
914 set_bit(MR_COMPLETE, &ifx_dev->mdm_reset_state);
915 wake_up(&ifx_dev->mdm_reset_wait);
916 }
917 }
918 return IRQ_HANDLED;
919}
920
921/**
922 * ifx_spi_free_device - free device
923 * @ifx_dev: device to free
924 *
925 * Free the IFX device
926 */
927static void ifx_spi_free_device(struct ifx_spi_device *ifx_dev)
928{
929 ifx_spi_free_port(ifx_dev);
930 dma_free_coherent(&ifx_dev->spi_dev->dev,
931 IFX_SPI_TRANSFER_SIZE,
932 ifx_dev->tx_buffer,
933 ifx_dev->tx_bus);
934 dma_free_coherent(&ifx_dev->spi_dev->dev,
935 IFX_SPI_TRANSFER_SIZE,
936 ifx_dev->rx_buffer,
937 ifx_dev->rx_bus);
938}
939
940/**
941 * ifx_spi_reset - reset modem
942 * @ifx_dev: modem to reset
943 *
944 * Perform a reset on the modem
945 */
946static int ifx_spi_reset(struct ifx_spi_device *ifx_dev)
947{
948 int ret;
949 /*
950 * set up modem power, reset
951 *
952 * delays are required on some platforms for the modem
953 * to reset properly
954 */
955 set_bit(MR_START, &ifx_dev->mdm_reset_state);
Linus Walleij9a8da602020-03-11 09:31:31 +0100956 gpiod_set_value(ifx_dev->gpio.po, 0);
957 gpiod_set_value(ifx_dev->gpio.reset, 0);
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100958 msleep(25);
Linus Walleij9a8da602020-03-11 09:31:31 +0100959 gpiod_set_value(ifx_dev->gpio.reset, 1);
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100960 msleep(1);
Linus Walleij9a8da602020-03-11 09:31:31 +0100961 gpiod_set_value(ifx_dev->gpio.po, 1);
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100962 msleep(1);
Linus Walleij9a8da602020-03-11 09:31:31 +0100963 gpiod_set_value(ifx_dev->gpio.po, 0);
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100964 ret = wait_event_timeout(ifx_dev->mdm_reset_wait,
965 test_bit(MR_COMPLETE,
966 &ifx_dev->mdm_reset_state),
967 IFX_RESET_TIMEOUT);
968 if (!ret)
969 dev_warn(&ifx_dev->spi_dev->dev, "Modem reset timeout: (state:%lx)",
970 ifx_dev->mdm_reset_state);
971
972 ifx_dev->mdm_reset_state = 0;
973 return ret;
974}
975
976/**
977 * ifx_spi_spi_probe - probe callback
978 * @spi: our possible matching SPI device
979 *
980 * Probe for a 6x60 modem on SPI bus. Perform any needed device and
981 * GPIO setup.
982 *
983 * FIXME:
984 * - Support for multiple devices
985 * - Split out MID specific GPIO handling eventually
986 */
987
988static int ifx_spi_spi_probe(struct spi_device *spi)
989{
990 int ret;
991 int srdy;
Russ Gorby2f1522e2011-02-02 12:56:58 -0800992 struct ifx_modem_platform_data *pl_data;
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100993 struct ifx_spi_device *ifx_dev;
Linus Walleijb685e6f2020-03-11 09:31:30 +0100994 struct device *dev = &spi->dev;
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100995
996 if (saved_ifx_dev) {
Linus Walleijb685e6f2020-03-11 09:31:30 +0100997 dev_dbg(dev, "ignoring subsequent detection");
Russ Gorbyaf3b8882010-10-26 14:13:52 +0100998 return -ENODEV;
999 }
1000
Linus Walleijb685e6f2020-03-11 09:31:30 +01001001 pl_data = dev_get_platdata(dev);
Russ Gorby2f1522e2011-02-02 12:56:58 -08001002 if (!pl_data) {
Linus Walleijb685e6f2020-03-11 09:31:30 +01001003 dev_err(dev, "missing platform data!");
Russ Gorby2f1522e2011-02-02 12:56:58 -08001004 return -ENODEV;
1005 }
1006
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001007 /* initialize structure to hold our device variables */
1008 ifx_dev = kzalloc(sizeof(struct ifx_spi_device), GFP_KERNEL);
1009 if (!ifx_dev) {
Linus Walleijb685e6f2020-03-11 09:31:30 +01001010 dev_err(dev, "spi device allocation failed");
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001011 return -ENOMEM;
1012 }
1013 saved_ifx_dev = ifx_dev;
1014 ifx_dev->spi_dev = spi;
1015 clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &ifx_dev->flags);
1016 spin_lock_init(&ifx_dev->write_lock);
1017 spin_lock_init(&ifx_dev->power_lock);
1018 ifx_dev->power_status = 0;
Kees Cooke99e88a2017-10-16 14:43:17 -07001019 timer_setup(&ifx_dev->spi_timer, ifx_spi_timeout, 0);
Russ Gorby2f1522e2011-02-02 12:56:58 -08001020 ifx_dev->modem = pl_data->modem_type;
1021 ifx_dev->use_dma = pl_data->use_dma;
1022 ifx_dev->max_hz = pl_data->max_hz;
Russ Gorby2aff8d92011-02-07 12:02:31 -08001023 /* initialize spi mode, etc */
Russ Gorby1b79b442011-02-07 12:02:30 -08001024 spi->max_speed_hz = ifx_dev->max_hz;
Russ Gorby2aff8d92011-02-07 12:02:31 -08001025 spi->mode = IFX_SPI_MODE | (SPI_LOOP & spi->mode);
1026 spi->bits_per_word = spi_bpw;
1027 ret = spi_setup(spi);
1028 if (ret) {
Linus Walleijb685e6f2020-03-11 09:31:30 +01001029 dev_err(dev, "SPI setup wasn't successful %d", ret);
Souptick Joarder0a940b02016-10-28 17:30:16 +05301030 kfree(ifx_dev);
Russ Gorby2aff8d92011-02-07 12:02:31 -08001031 return -ENODEV;
1032 }
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001033
chao bi319fb0d2012-10-25 09:02:32 +08001034 /* init swap_buf function according to word width configuration */
1035 if (spi->bits_per_word == 32)
1036 ifx_dev->swap_buf = swap_buf_32;
1037 else if (spi->bits_per_word == 16)
1038 ifx_dev->swap_buf = swap_buf_16;
1039 else
1040 ifx_dev->swap_buf = swap_buf_8;
1041
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001042 /* ensure SPI protocol flags are initialized to enable transfer */
1043 ifx_dev->spi_more = 0;
1044 ifx_dev->spi_slave_cts = 0;
1045
1046 /*initialize transfer and dma buffers */
Russ Gorby5fc324952011-02-07 12:02:28 -08001047 ifx_dev->tx_buffer = dma_alloc_coherent(ifx_dev->spi_dev->dev.parent,
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001048 IFX_SPI_TRANSFER_SIZE,
1049 &ifx_dev->tx_bus,
1050 GFP_KERNEL);
1051 if (!ifx_dev->tx_buffer) {
Linus Walleijb685e6f2020-03-11 09:31:30 +01001052 dev_err(dev, "DMA-TX buffer allocation failed");
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001053 ret = -ENOMEM;
1054 goto error_ret;
1055 }
Russ Gorby5fc324952011-02-07 12:02:28 -08001056 ifx_dev->rx_buffer = dma_alloc_coherent(ifx_dev->spi_dev->dev.parent,
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001057 IFX_SPI_TRANSFER_SIZE,
1058 &ifx_dev->rx_bus,
1059 GFP_KERNEL);
1060 if (!ifx_dev->rx_buffer) {
Linus Walleijb685e6f2020-03-11 09:31:30 +01001061 dev_err(dev, "DMA-RX buffer allocation failed");
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001062 ret = -ENOMEM;
1063 goto error_ret;
1064 }
1065
1066 /* initialize waitq for modem reset */
1067 init_waitqueue_head(&ifx_dev->mdm_reset_wait);
1068
1069 spi_set_drvdata(spi, ifx_dev);
1070 tasklet_init(&ifx_dev->io_work_tasklet, ifx_spi_io,
1071 (unsigned long)ifx_dev);
1072
1073 set_bit(IFX_SPI_STATE_PRESENT, &ifx_dev->flags);
1074
1075 /* create our tty port */
1076 ret = ifx_spi_create_port(ifx_dev);
1077 if (ret != 0) {
Linus Walleijb685e6f2020-03-11 09:31:30 +01001078 dev_err(dev, "create default tty port failed");
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001079 goto error_ret;
1080 }
1081
Linus Walleij9a8da602020-03-11 09:31:31 +01001082 ifx_dev->gpio.reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
1083 if (IS_ERR(ifx_dev->gpio.reset)) {
1084 dev_err(dev, "could not obtain reset GPIO\n");
1085 ret = PTR_ERR(ifx_dev->gpio.reset);
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001086 goto error_ret;
1087 }
Linus Walleij9a8da602020-03-11 09:31:31 +01001088 gpiod_set_consumer_name(ifx_dev->gpio.reset, "ifxModem reset");
1089 ifx_dev->gpio.po = devm_gpiod_get(dev, "power", GPIOD_OUT_LOW);
1090 if (IS_ERR(ifx_dev->gpio.po)) {
1091 dev_err(dev, "could not obtain power GPIO\n");
1092 ret = PTR_ERR(ifx_dev->gpio.po);
1093 goto error_ret;
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001094 }
Linus Walleij9a8da602020-03-11 09:31:31 +01001095 gpiod_set_consumer_name(ifx_dev->gpio.po, "ifxModem power");
1096 ifx_dev->gpio.mrdy = devm_gpiod_get(dev, "mrdy", GPIOD_OUT_LOW);
1097 if (IS_ERR(ifx_dev->gpio.mrdy)) {
1098 dev_err(dev, "could not obtain mrdy GPIO\n");
1099 ret = PTR_ERR(ifx_dev->gpio.mrdy);
1100 goto error_ret;
1101 }
1102 gpiod_set_consumer_name(ifx_dev->gpio.mrdy, "ifxModem mrdy");
1103 ifx_dev->gpio.srdy = devm_gpiod_get(dev, "srdy", GPIOD_IN);
1104 if (IS_ERR(ifx_dev->gpio.srdy)) {
1105 dev_err(dev, "could not obtain srdy GPIO\n");
1106 ret = PTR_ERR(ifx_dev->gpio.srdy);
1107 goto error_ret;
1108 }
1109 gpiod_set_consumer_name(ifx_dev->gpio.srdy, "ifxModem srdy");
1110 ifx_dev->gpio.reset_out = devm_gpiod_get(dev, "rst_out", GPIOD_IN);
1111 if (IS_ERR(ifx_dev->gpio.reset_out)) {
1112 dev_err(dev, "could not obtain rst_out GPIO\n");
1113 ret = PTR_ERR(ifx_dev->gpio.reset_out);
1114 goto error_ret;
1115 }
1116 gpiod_set_consumer_name(ifx_dev->gpio.reset_out, "ifxModem reset out");
1117 ifx_dev->gpio.pmu_reset = devm_gpiod_get(dev, "pmu_reset", GPIOD_ASIS);
1118 if (IS_ERR(ifx_dev->gpio.pmu_reset)) {
1119 dev_err(dev, "could not obtain pmu_reset GPIO\n");
1120 ret = PTR_ERR(ifx_dev->gpio.pmu_reset);
1121 goto error_ret;
1122 }
1123 gpiod_set_consumer_name(ifx_dev->gpio.pmu_reset, "ifxModem PMU reset");
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001124
Linus Walleij9a8da602020-03-11 09:31:31 +01001125 ret = request_irq(gpiod_to_irq(ifx_dev->gpio.reset_out),
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001126 ifx_spi_reset_interrupt,
1127 IRQF_TRIGGER_RISING|IRQF_TRIGGER_FALLING, DRVNAME,
Geert Uytterhoevencbbcd1f2015-04-23 16:15:40 +02001128 ifx_dev);
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001129 if (ret) {
Linus Walleijb685e6f2020-03-11 09:31:30 +01001130 dev_err(dev, "Unable to get irq %x\n",
Linus Walleij9a8da602020-03-11 09:31:31 +01001131 gpiod_to_irq(ifx_dev->gpio.reset_out));
1132 goto error_ret;
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001133 }
1134
1135 ret = ifx_spi_reset(ifx_dev);
1136
Linus Walleij9a8da602020-03-11 09:31:31 +01001137 ret = request_irq(gpiod_to_irq(ifx_dev->gpio.srdy),
Geert Uytterhoevencbbcd1f2015-04-23 16:15:40 +02001138 ifx_spi_srdy_interrupt, IRQF_TRIGGER_RISING, DRVNAME,
1139 ifx_dev);
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001140 if (ret) {
Linus Walleijb685e6f2020-03-11 09:31:30 +01001141 dev_err(dev, "Unable to get irq %x",
Linus Walleij9a8da602020-03-11 09:31:31 +01001142 gpiod_to_irq(ifx_dev->gpio.srdy));
1143 goto error_ret2;
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001144 }
1145
1146 /* set pm runtime power state and register with power system */
Linus Walleijb685e6f2020-03-11 09:31:30 +01001147 pm_runtime_set_active(dev);
1148 pm_runtime_enable(dev);
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001149
1150 /* handle case that modem is already signaling SRDY */
1151 /* no outgoing tty open at this point, this just satisfies the
1152 * modem's read and should reset communication properly
1153 */
Linus Walleij9a8da602020-03-11 09:31:31 +01001154 srdy = gpiod_get_value(ifx_dev->gpio.srdy);
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001155
1156 if (srdy) {
1157 mrdy_assert(ifx_dev);
1158 ifx_spi_handle_srdy(ifx_dev);
1159 } else
1160 mrdy_set_low(ifx_dev);
1161 return 0;
1162
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001163error_ret2:
Linus Walleij9a8da602020-03-11 09:31:31 +01001164 free_irq(gpiod_to_irq(ifx_dev->gpio.reset_out), ifx_dev);
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001165error_ret:
1166 ifx_spi_free_device(ifx_dev);
1167 saved_ifx_dev = NULL;
1168 return ret;
1169}
1170
1171/**
1172 * ifx_spi_spi_remove - SPI device was removed
1173 * @spi: SPI device
1174 *
1175 * FIXME: We should be shutting the device down here not in
1176 * the module unload path.
1177 */
1178
1179static int ifx_spi_spi_remove(struct spi_device *spi)
1180{
1181 struct ifx_spi_device *ifx_dev = spi_get_drvdata(spi);
1182 /* stop activity */
1183 tasklet_kill(&ifx_dev->io_work_tasklet);
Chuhong Yuan50b2b572019-11-18 10:48:33 +08001184
1185 pm_runtime_disable(&spi->dev);
1186
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001187 /* free irq */
Linus Walleij9a8da602020-03-11 09:31:31 +01001188 free_irq(gpiod_to_irq(ifx_dev->gpio.reset_out), ifx_dev);
1189 free_irq(gpiod_to_irq(ifx_dev->gpio.srdy), ifx_dev);
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001190
1191 /* free allocations */
1192 ifx_spi_free_device(ifx_dev);
1193
1194 saved_ifx_dev = NULL;
1195 return 0;
1196}
1197
1198/**
1199 * ifx_spi_spi_shutdown - called on SPI shutdown
1200 * @spi: SPI device
1201 *
1202 * No action needs to be taken here
1203 */
1204
1205static void ifx_spi_spi_shutdown(struct spi_device *spi)
1206{
Jun Chen72d47242012-11-23 06:07:27 -05001207 struct ifx_spi_device *ifx_dev = spi_get_drvdata(spi);
1208
1209 ifx_modem_power_off(ifx_dev);
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001210}
1211
1212/*
1213 * various suspends and resumes have nothing to do
1214 * no hardware to save state for
1215 */
1216
1217/**
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001218 * ifx_spi_pm_suspend - suspend modem on system suspend
1219 * @dev: device being suspended
1220 *
1221 * Suspend the modem. No action needed on Intel MID platforms, may
1222 * need extending for other systems.
1223 */
1224static int ifx_spi_pm_suspend(struct device *dev)
1225{
1226 return 0;
1227}
1228
1229/**
1230 * ifx_spi_pm_resume - resume modem on system resume
1231 * @dev: device being suspended
1232 *
1233 * Allow the modem to resume. No action needed.
1234 *
1235 * FIXME: do we need to reset anything here ?
1236 */
1237static int ifx_spi_pm_resume(struct device *dev)
1238{
1239 return 0;
1240}
1241
1242/**
1243 * ifx_spi_pm_runtime_resume - suspend modem
1244 * @dev: device being suspended
1245 *
1246 * Allow the modem to resume. No action needed.
1247 */
1248static int ifx_spi_pm_runtime_resume(struct device *dev)
1249{
1250 return 0;
1251}
1252
1253/**
1254 * ifx_spi_pm_runtime_suspend - suspend modem
1255 * @dev: device being suspended
1256 *
1257 * Allow the modem to suspend and thus suspend to continue up the
1258 * device tree.
1259 */
1260static int ifx_spi_pm_runtime_suspend(struct device *dev)
1261{
1262 return 0;
1263}
1264
1265/**
1266 * ifx_spi_pm_runtime_idle - check if modem idle
1267 * @dev: our device
1268 *
1269 * Check conditions and queue runtime suspend if idle.
1270 */
1271static int ifx_spi_pm_runtime_idle(struct device *dev)
1272{
1273 struct spi_device *spi = to_spi_device(dev);
1274 struct ifx_spi_device *ifx_dev = spi_get_drvdata(spi);
1275
1276 if (!ifx_dev->power_status)
1277 pm_runtime_suspend(dev);
1278
1279 return 0;
1280}
1281
1282static const struct dev_pm_ops ifx_spi_pm = {
1283 .resume = ifx_spi_pm_resume,
1284 .suspend = ifx_spi_pm_suspend,
1285 .runtime_resume = ifx_spi_pm_runtime_resume,
1286 .runtime_suspend = ifx_spi_pm_runtime_suspend,
1287 .runtime_idle = ifx_spi_pm_runtime_idle
1288};
1289
1290static const struct spi_device_id ifx_id_table[] = {
1291 {"ifx6160", 0},
1292 {"ifx6260", 0},
1293 { }
1294};
1295MODULE_DEVICE_TABLE(spi, ifx_id_table);
1296
1297/* spi operations */
Fengguang Wu7d9739c2012-08-07 13:12:47 +08001298static struct spi_driver ifx_spi_driver = {
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001299 .driver = {
Russ Gorby8115be02011-02-07 12:02:32 -08001300 .name = DRVNAME,
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001301 .pm = &ifx_spi_pm,
Andrew F. Davis3821a062015-10-23 08:59:11 -05001302 },
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001303 .probe = ifx_spi_spi_probe,
1304 .shutdown = ifx_spi_spi_shutdown,
Bill Pemberton2d47b712012-11-19 13:21:34 -05001305 .remove = ifx_spi_spi_remove,
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001306 .id_table = ifx_id_table
1307};
1308
1309/**
1310 * ifx_spi_exit - module exit
1311 *
1312 * Unload the module.
1313 */
1314
1315static void __exit ifx_spi_exit(void)
1316{
1317 /* unregister */
Johan Hovold1e948472017-04-26 12:24:21 +02001318 spi_unregister_driver(&ifx_spi_driver);
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001319 tty_unregister_driver(tty_drv);
Jun Chen72d47242012-11-23 06:07:27 -05001320 put_tty_driver(tty_drv);
Jun Chen72d47242012-11-23 06:07:27 -05001321 unregister_reboot_notifier(&ifx_modem_reboot_notifier_block);
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001322}
1323
1324/**
1325 * ifx_spi_init - module entry point
1326 *
1327 * Initialise the SPI and tty interfaces for the IFX SPI driver
1328 * We need to initialize upper-edge spi driver after the tty
1329 * driver because otherwise the spi probe will race
1330 */
1331
1332static int __init ifx_spi_init(void)
1333{
1334 int result;
1335
1336 tty_drv = alloc_tty_driver(1);
1337 if (!tty_drv) {
1338 pr_err("%s: alloc_tty_driver failed", DRVNAME);
1339 return -ENOMEM;
1340 }
1341
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001342 tty_drv->driver_name = DRVNAME;
1343 tty_drv->name = TTYNAME;
1344 tty_drv->minor_start = IFX_SPI_TTY_ID;
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001345 tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
1346 tty_drv->subtype = SERIAL_TYPE_NORMAL;
1347 tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1348 tty_drv->init_termios = tty_std_termios;
1349
1350 tty_set_operations(tty_drv, &ifx_spi_serial_ops);
1351
1352 result = tty_register_driver(tty_drv);
1353 if (result) {
1354 pr_err("%s: tty_register_driver failed(%d)",
1355 DRVNAME, result);
Jun Chen72d47242012-11-23 06:07:27 -05001356 goto err_free_tty;
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001357 }
1358
Geert Uytterhoeven9a499db2015-04-23 16:15:39 +02001359 result = spi_register_driver(&ifx_spi_driver);
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001360 if (result) {
1361 pr_err("%s: spi_register_driver failed(%d)",
1362 DRVNAME, result);
Jun Chen72d47242012-11-23 06:07:27 -05001363 goto err_unreg_tty;
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001364 }
Jun Chen72d47242012-11-23 06:07:27 -05001365
1366 result = register_reboot_notifier(&ifx_modem_reboot_notifier_block);
1367 if (result) {
1368 pr_err("%s: register ifx modem reboot notifier failed(%d)",
1369 DRVNAME, result);
1370 goto err_unreg_spi;
1371 }
1372
1373 return 0;
1374err_unreg_spi:
Geert Uytterhoeven9a499db2015-04-23 16:15:39 +02001375 spi_unregister_driver(&ifx_spi_driver);
Jun Chen72d47242012-11-23 06:07:27 -05001376err_unreg_tty:
1377 tty_unregister_driver(tty_drv);
1378err_free_tty:
1379 put_tty_driver(tty_drv);
1380
Russ Gorbyaf3b8882010-10-26 14:13:52 +01001381 return result;
1382}
1383
1384module_init(ifx_spi_init);
1385module_exit(ifx_spi_exit);
1386
1387MODULE_AUTHOR("Intel");
1388MODULE_DESCRIPTION("IFX6x60 spi driver");
1389MODULE_LICENSE("GPL");
1390MODULE_INFO(Version, "0.1-IFX6x60");