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