1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39#include <linux/dma-mapping.h>
40#include <linux/module.h>
41#include <linux/termios.h>
42#include <linux/tty.h>
43#include <linux/device.h>
44#include <linux/spi/spi.h>
45#include <linux/kfifo.h>
46#include <linux/tty_flip.h>
47#include <linux/timer.h>
48#include <linux/serial.h>
49#include <linux/interrupt.h>
50#include <linux/irq.h>
51#include <linux/rfkill.h>
52#include <linux/fs.h>
53#include <linux/ip.h>
54#include <linux/dmapool.h>
55#include <linux/gpio.h>
56#include <linux/sched.h>
57#include <linux/time.h>
58#include <linux/wait.h>
59#include <linux/pm.h>
60#include <linux/pm_runtime.h>
61#include <linux/spi/ifx_modem.h>
62#include <linux/delay.h>
63
64#include "ifx6x60.h"
65
66#define IFX_SPI_MORE_MASK 0x10
67#define IFX_SPI_MORE_BIT 12
68#define IFX_SPI_CTS_BIT 13
69#define IFX_SPI_MODE SPI_MODE_1
70#define IFX_SPI_TTY_ID 0
71#define IFX_SPI_TIMEOUT_SEC 2
72#define IFX_SPI_HEADER_0 (-1)
73#define IFX_SPI_HEADER_F (-2)
74
75
76static void ifx_spi_handle_srdy(struct ifx_spi_device *ifx_dev);
77
78
79static int spi_bpw = 16;
80static struct tty_driver *tty_drv;
81static struct ifx_spi_device *saved_ifx_dev;
82static struct lock_class_key ifx_spi_key;
83
84
85
86
87
88
89
90
91static inline void mrdy_set_high(struct ifx_spi_device *ifx)
92{
93 gpio_set_value(ifx->gpio.mrdy, 1);
94}
95
96
97
98
99
100
101static inline void mrdy_set_low(struct ifx_spi_device *ifx)
102{
103 gpio_set_value(ifx->gpio.mrdy, 0);
104}
105
106
107
108
109
110
111
112
113static void
114ifx_spi_power_state_set(struct ifx_spi_device *ifx_dev, unsigned char val)
115{
116 unsigned long flags;
117
118 spin_lock_irqsave(&ifx_dev->power_lock, flags);
119
120
121
122
123
124 if (!ifx_dev->power_status)
125 pm_runtime_get(&ifx_dev->spi_dev->dev);
126 ifx_dev->power_status |= val;
127
128 spin_unlock_irqrestore(&ifx_dev->power_lock, flags);
129}
130
131
132
133
134
135
136
137
138static void
139ifx_spi_power_state_clear(struct ifx_spi_device *ifx_dev, unsigned char val)
140{
141 unsigned long flags;
142
143 spin_lock_irqsave(&ifx_dev->power_lock, flags);
144
145 if (ifx_dev->power_status) {
146 ifx_dev->power_status &= ~val;
147 if (!ifx_dev->power_status)
148 pm_runtime_put(&ifx_dev->spi_dev->dev);
149 }
150
151 spin_unlock_irqrestore(&ifx_dev->power_lock, flags);
152}
153
154
155
156
157
158
159
160
161
162static inline void swap_buf(u16 *buf, int len, void *end)
163{
164 int n;
165
166 len = ((len + 1) >> 1);
167 if ((void *)&buf[len] > end) {
168 pr_err("swap_buf: swap exceeds boundary (%p > %p)!",
169 &buf[len], end);
170 return;
171 }
172 for (n = 0; n < len; n++) {
173 *buf = cpu_to_be16(*buf);
174 buf++;
175 }
176}
177
178
179
180
181
182
183
184
185
186
187static void mrdy_assert(struct ifx_spi_device *ifx_dev)
188{
189 int val = gpio_get_value(ifx_dev->gpio.srdy);
190 if (!val) {
191 if (!test_and_set_bit(IFX_SPI_STATE_TIMER_PENDING,
192 &ifx_dev->flags)) {
193 ifx_dev->spi_timer.expires =
194 jiffies + IFX_SPI_TIMEOUT_SEC*HZ;
195 add_timer(&ifx_dev->spi_timer);
196
197 }
198 }
199 ifx_spi_power_state_set(ifx_dev, IFX_SPI_POWER_DATA_PENDING);
200 mrdy_set_high(ifx_dev);
201}
202
203
204
205
206
207
208
209
210static void ifx_spi_ttyhangup(struct ifx_spi_device *ifx_dev)
211{
212 struct tty_port *pport = &ifx_dev->tty_port;
213 struct tty_struct *tty = tty_port_tty_get(pport);
214 if (tty) {
215 tty_hangup(tty);
216 tty_kref_put(tty);
217 }
218}
219
220
221
222
223
224
225
226
227static void ifx_spi_timeout(unsigned long arg)
228{
229 struct ifx_spi_device *ifx_dev = (struct ifx_spi_device *)arg;
230
231 dev_warn(&ifx_dev->spi_dev->dev, "*** SPI Timeout ***");
232 ifx_spi_ttyhangup(ifx_dev);
233 mrdy_set_low(ifx_dev);
234 clear_bit(IFX_SPI_STATE_TIMER_PENDING, &ifx_dev->flags);
235}
236
237
238
239
240
241
242
243
244
245
246
247static int ifx_spi_tiocmget(struct tty_struct *tty)
248{
249 unsigned int value;
250 struct ifx_spi_device *ifx_dev = tty->driver_data;
251
252 value =
253 (test_bit(IFX_SPI_RTS, &ifx_dev->signal_state) ? TIOCM_RTS : 0) |
254 (test_bit(IFX_SPI_DTR, &ifx_dev->signal_state) ? TIOCM_DTR : 0) |
255 (test_bit(IFX_SPI_CTS, &ifx_dev->signal_state) ? TIOCM_CTS : 0) |
256 (test_bit(IFX_SPI_DSR, &ifx_dev->signal_state) ? TIOCM_DSR : 0) |
257 (test_bit(IFX_SPI_DCD, &ifx_dev->signal_state) ? TIOCM_CAR : 0) |
258 (test_bit(IFX_SPI_RI, &ifx_dev->signal_state) ? TIOCM_RNG : 0);
259 return value;
260}
261
262
263
264
265
266
267
268
269
270
271
272
273static int ifx_spi_tiocmset(struct tty_struct *tty,
274 unsigned int set, unsigned int clear)
275{
276 struct ifx_spi_device *ifx_dev = tty->driver_data;
277
278 if (set & TIOCM_RTS)
279 set_bit(IFX_SPI_RTS, &ifx_dev->signal_state);
280 if (set & TIOCM_DTR)
281 set_bit(IFX_SPI_DTR, &ifx_dev->signal_state);
282 if (clear & TIOCM_RTS)
283 clear_bit(IFX_SPI_RTS, &ifx_dev->signal_state);
284 if (clear & TIOCM_DTR)
285 clear_bit(IFX_SPI_DTR, &ifx_dev->signal_state);
286
287 set_bit(IFX_SPI_UPDATE, &ifx_dev->signal_state);
288 return 0;
289}
290
291
292
293
294
295
296
297
298
299
300
301static int ifx_spi_open(struct tty_struct *tty, struct file *filp)
302{
303 return tty_port_open(&saved_ifx_dev->tty_port, tty, filp);
304}
305
306
307
308
309
310
311
312
313
314static void ifx_spi_close(struct tty_struct *tty, struct file *filp)
315{
316 struct ifx_spi_device *ifx_dev = tty->driver_data;
317 tty_port_close(&ifx_dev->tty_port, tty, filp);
318
319}
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334static int ifx_spi_decode_spi_header(unsigned char *buffer, int *length,
335 unsigned char *more, unsigned char *received_cts)
336{
337 u16 h1;
338 u16 h2;
339 u16 *in_buffer = (u16 *)buffer;
340
341 h1 = *in_buffer;
342 h2 = *(in_buffer+1);
343
344 if (h1 == 0 && h2 == 0) {
345 *received_cts = 0;
346 return IFX_SPI_HEADER_0;
347 } else if (h1 == 0xffff && h2 == 0xffff) {
348
349 return IFX_SPI_HEADER_F;
350 }
351
352 *length = h1 & 0xfff;
353 *more = (buffer[1] >> IFX_SPI_MORE_BIT) & 1;
354 *received_cts = (buffer[3] >> IFX_SPI_CTS_BIT) & 1;
355 return 0;
356}
357
358
359
360
361
362
363
364
365
366
367
368static void ifx_spi_setup_spi_header(unsigned char *txbuffer, int tx_count,
369 unsigned char more)
370{
371 *(u16 *)(txbuffer) = tx_count;
372 *(u16 *)(txbuffer+2) = IFX_SPI_PAYLOAD_SIZE;
373 txbuffer[1] |= (more << IFX_SPI_MORE_BIT) & IFX_SPI_MORE_MASK;
374}
375
376
377
378
379
380
381
382
383
384static void ifx_spi_wakeup_serial(struct ifx_spi_device *ifx_dev)
385{
386 struct tty_struct *tty;
387
388 tty = tty_port_tty_get(&ifx_dev->tty_port);
389 if (!tty)
390 return;
391 tty_wakeup(tty);
392 tty_kref_put(tty);
393}
394
395
396
397
398
399
400
401
402
403
404
405
406
407static int ifx_spi_prepare_tx_buffer(struct ifx_spi_device *ifx_dev)
408{
409 int temp_count;
410 int queue_length;
411 int tx_count;
412 unsigned char *tx_buffer;
413
414 tx_buffer = ifx_dev->tx_buffer;
415 memset(tx_buffer, 0, IFX_SPI_TRANSFER_SIZE);
416
417
418 tx_buffer += IFX_SPI_HEADER_OVERHEAD;
419 tx_count = IFX_SPI_HEADER_OVERHEAD;
420
421
422
423 ifx_dev->spi_more = 0;
424
425
426 if (!ifx_dev->spi_slave_cts) {
427
428 queue_length = kfifo_len(&ifx_dev->tx_fifo);
429 if (queue_length != 0) {
430
431 temp_count = min(queue_length, IFX_SPI_PAYLOAD_SIZE);
432 temp_count = kfifo_out_locked(&ifx_dev->tx_fifo,
433 tx_buffer, temp_count,
434 &ifx_dev->fifo_lock);
435
436
437 tx_buffer += temp_count;
438 tx_count += temp_count;
439 if (temp_count == queue_length)
440
441 ifx_spi_wakeup_serial(ifx_dev);
442 else
443 ifx_dev->spi_more = 1;
444 }
445 }
446
447
448 ifx_spi_setup_spi_header(ifx_dev->tx_buffer,
449 tx_count-IFX_SPI_HEADER_OVERHEAD,
450 ifx_dev->spi_more);
451
452 swap_buf((u16 *)(ifx_dev->tx_buffer), tx_count,
453 &ifx_dev->tx_buffer[IFX_SPI_TRANSFER_SIZE]);
454 return tx_count;
455}
456
457
458
459
460
461
462
463
464
465
466
467static int ifx_spi_write(struct tty_struct *tty, const unsigned char *buf,
468 int count)
469{
470 struct ifx_spi_device *ifx_dev = tty->driver_data;
471 unsigned char *tmp_buf = (unsigned char *)buf;
472 int tx_count = kfifo_in_locked(&ifx_dev->tx_fifo, tmp_buf, count,
473 &ifx_dev->fifo_lock);
474 mrdy_assert(ifx_dev);
475 return tx_count;
476}
477
478
479
480
481
482
483
484
485static int ifx_spi_write_room(struct tty_struct *tty)
486{
487 struct ifx_spi_device *ifx_dev = tty->driver_data;
488 return IFX_SPI_FIFO_SIZE - kfifo_len(&ifx_dev->tx_fifo);
489}
490
491
492
493
494
495
496
497
498static int ifx_spi_chars_in_buffer(struct tty_struct *tty)
499{
500 struct ifx_spi_device *ifx_dev = tty->driver_data;
501 return kfifo_len(&ifx_dev->tx_fifo);
502}
503
504
505
506
507
508
509
510
511
512static void ifx_spi_hangup(struct tty_struct *tty)
513{
514 struct ifx_spi_device *ifx_dev = tty->driver_data;
515 tty_port_hangup(&ifx_dev->tty_port);
516}
517
518
519
520
521
522
523
524
525static int ifx_port_activate(struct tty_port *port, struct tty_struct *tty)
526{
527 struct ifx_spi_device *ifx_dev =
528 container_of(port, struct ifx_spi_device, tty_port);
529
530
531 kfifo_reset(&ifx_dev->tx_fifo);
532
533
534 tty->driver_data = ifx_dev;
535
536
537 tty->low_latency = 1;
538
539 return 0;
540}
541
542
543
544
545
546
547
548
549static void ifx_port_shutdown(struct tty_port *port)
550{
551 struct ifx_spi_device *ifx_dev =
552 container_of(port, struct ifx_spi_device, tty_port);
553
554 mrdy_set_low(ifx_dev);
555 clear_bit(IFX_SPI_STATE_TIMER_PENDING, &ifx_dev->flags);
556 tasklet_kill(&ifx_dev->io_work_tasklet);
557}
558
559static const struct tty_port_operations ifx_tty_port_ops = {
560 .activate = ifx_port_activate,
561 .shutdown = ifx_port_shutdown,
562};
563
564static const struct tty_operations ifx_spi_serial_ops = {
565 .open = ifx_spi_open,
566 .close = ifx_spi_close,
567 .write = ifx_spi_write,
568 .hangup = ifx_spi_hangup,
569 .write_room = ifx_spi_write_room,
570 .chars_in_buffer = ifx_spi_chars_in_buffer,
571 .tiocmget = ifx_spi_tiocmget,
572 .tiocmset = ifx_spi_tiocmset,
573};
574
575
576
577
578
579
580
581
582
583
584static void ifx_spi_insert_flip_string(struct ifx_spi_device *ifx_dev,
585 unsigned char *chars, size_t size)
586{
587 struct tty_struct *tty = tty_port_tty_get(&ifx_dev->tty_port);
588 if (!tty)
589 return;
590 tty_insert_flip_string(tty, chars, size);
591 tty_flip_buffer_push(tty);
592 tty_kref_put(tty);
593}
594
595
596
597
598
599
600
601
602static void ifx_spi_complete(void *ctx)
603{
604 struct ifx_spi_device *ifx_dev = ctx;
605 struct tty_struct *tty;
606 struct tty_ldisc *ldisc = NULL;
607 int length;
608 int actual_length;
609 unsigned char more;
610 unsigned char cts;
611 int local_write_pending = 0;
612 int queue_length;
613 int srdy;
614 int decode_result;
615
616 mrdy_set_low(ifx_dev);
617
618 if (!ifx_dev->spi_msg.status) {
619
620 swap_buf((u16 *)ifx_dev->rx_buffer, IFX_SPI_HEADER_OVERHEAD,
621 &ifx_dev->rx_buffer[IFX_SPI_HEADER_OVERHEAD]);
622 decode_result = ifx_spi_decode_spi_header(ifx_dev->rx_buffer,
623 &length, &more, &cts);
624 if (decode_result == IFX_SPI_HEADER_0) {
625 dev_dbg(&ifx_dev->spi_dev->dev,
626 "ignore input: invalid header 0");
627 ifx_dev->spi_slave_cts = 0;
628 goto complete_exit;
629 } else if (decode_result == IFX_SPI_HEADER_F) {
630 dev_dbg(&ifx_dev->spi_dev->dev,
631 "ignore input: invalid header F");
632 goto complete_exit;
633 }
634
635 ifx_dev->spi_slave_cts = cts;
636
637 actual_length = min((unsigned int)length,
638 ifx_dev->spi_msg.actual_length);
639 swap_buf((u16 *)(ifx_dev->rx_buffer + IFX_SPI_HEADER_OVERHEAD),
640 actual_length,
641 &ifx_dev->rx_buffer[IFX_SPI_TRANSFER_SIZE]);
642 ifx_spi_insert_flip_string(
643 ifx_dev,
644 ifx_dev->rx_buffer + IFX_SPI_HEADER_OVERHEAD,
645 (size_t)actual_length);
646 } else {
647 dev_dbg(&ifx_dev->spi_dev->dev, "SPI transfer error %d",
648 ifx_dev->spi_msg.status);
649 }
650
651complete_exit:
652 if (ifx_dev->write_pending) {
653 ifx_dev->write_pending = 0;
654 local_write_pending = 1;
655 }
656
657 clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &(ifx_dev->flags));
658
659 queue_length = kfifo_len(&ifx_dev->tx_fifo);
660 srdy = gpio_get_value(ifx_dev->gpio.srdy);
661 if (!srdy)
662 ifx_spi_power_state_clear(ifx_dev, IFX_SPI_POWER_SRDY);
663
664
665 if (test_and_clear_bit(IFX_SPI_STATE_IO_READY, &ifx_dev->flags))
666 tasklet_schedule(&ifx_dev->io_work_tasklet);
667 else {
668 if (more || ifx_dev->spi_more || queue_length > 0 ||
669 local_write_pending) {
670 if (ifx_dev->spi_slave_cts) {
671 if (more)
672 mrdy_assert(ifx_dev);
673 } else
674 mrdy_assert(ifx_dev);
675 } else {
676
677
678
679
680
681 ifx_spi_power_state_clear(ifx_dev,
682 IFX_SPI_POWER_DATA_PENDING);
683 tty = tty_port_tty_get(&ifx_dev->tty_port);
684 if (tty) {
685 ldisc = tty_ldisc_ref(tty);
686 if (ldisc) {
687 ldisc->ops->write_wakeup(tty);
688 tty_ldisc_deref(ldisc);
689 }
690 tty_kref_put(tty);
691 }
692 }
693 }
694}
695
696
697
698
699
700
701
702
703static void ifx_spi_io(unsigned long data)
704{
705 int retval;
706 struct ifx_spi_device *ifx_dev = (struct ifx_spi_device *) data;
707
708 if (!test_and_set_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &ifx_dev->flags)) {
709 if (ifx_dev->gpio.unack_srdy_int_nb > 0)
710 ifx_dev->gpio.unack_srdy_int_nb--;
711
712 ifx_spi_prepare_tx_buffer(ifx_dev);
713
714 spi_message_init(&ifx_dev->spi_msg);
715 INIT_LIST_HEAD(&ifx_dev->spi_msg.queue);
716
717 ifx_dev->spi_msg.context = ifx_dev;
718 ifx_dev->spi_msg.complete = ifx_spi_complete;
719
720
721
722 ifx_dev->spi_xfer.len = IFX_SPI_TRANSFER_SIZE;
723 ifx_dev->spi_xfer.cs_change = 0;
724 ifx_dev->spi_xfer.speed_hz = ifx_dev->spi_dev->max_speed_hz;
725
726 ifx_dev->spi_xfer.bits_per_word = spi_bpw;
727
728 ifx_dev->spi_xfer.tx_buf = ifx_dev->tx_buffer;
729 ifx_dev->spi_xfer.rx_buf = ifx_dev->rx_buffer;
730
731
732
733
734 if (ifx_dev->use_dma) {
735 ifx_dev->spi_msg.is_dma_mapped = 1;
736 ifx_dev->tx_dma = ifx_dev->tx_bus;
737 ifx_dev->rx_dma = ifx_dev->rx_bus;
738 ifx_dev->spi_xfer.tx_dma = ifx_dev->tx_dma;
739 ifx_dev->spi_xfer.rx_dma = ifx_dev->rx_dma;
740 } else {
741 ifx_dev->spi_msg.is_dma_mapped = 0;
742 ifx_dev->tx_dma = (dma_addr_t)0;
743 ifx_dev->rx_dma = (dma_addr_t)0;
744 ifx_dev->spi_xfer.tx_dma = (dma_addr_t)0;
745 ifx_dev->spi_xfer.rx_dma = (dma_addr_t)0;
746 }
747
748 spi_message_add_tail(&ifx_dev->spi_xfer, &ifx_dev->spi_msg);
749
750
751
752
753 mrdy_assert(ifx_dev);
754
755 retval = spi_async(ifx_dev->spi_dev, &ifx_dev->spi_msg);
756 if (retval) {
757 clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS,
758 &ifx_dev->flags);
759 tasklet_schedule(&ifx_dev->io_work_tasklet);
760 return;
761 }
762 } else
763 ifx_dev->write_pending = 1;
764}
765
766
767
768
769
770
771
772static void ifx_spi_free_port(struct ifx_spi_device *ifx_dev)
773{
774 if (ifx_dev->tty_dev)
775 tty_unregister_device(tty_drv, ifx_dev->minor);
776 kfifo_free(&ifx_dev->tx_fifo);
777}
778
779
780
781
782
783
784
785
786static int ifx_spi_create_port(struct ifx_spi_device *ifx_dev)
787{
788 int ret = 0;
789 struct tty_port *pport = &ifx_dev->tty_port;
790
791 spin_lock_init(&ifx_dev->fifo_lock);
792 lockdep_set_class_and_subclass(&ifx_dev->fifo_lock,
793 &ifx_spi_key, 0);
794
795 if (kfifo_alloc(&ifx_dev->tx_fifo, IFX_SPI_FIFO_SIZE, GFP_KERNEL)) {
796 ret = -ENOMEM;
797 goto error_ret;
798 }
799
800 tty_port_init(pport);
801 pport->ops = &ifx_tty_port_ops;
802 ifx_dev->minor = IFX_SPI_TTY_ID;
803 ifx_dev->tty_dev = tty_register_device(tty_drv, ifx_dev->minor,
804 &ifx_dev->spi_dev->dev);
805 if (IS_ERR(ifx_dev->tty_dev)) {
806 dev_dbg(&ifx_dev->spi_dev->dev,
807 "%s: registering tty device failed", __func__);
808 ret = PTR_ERR(ifx_dev->tty_dev);
809 goto error_ret;
810 }
811 return 0;
812
813error_ret:
814 ifx_spi_free_port(ifx_dev);
815 return ret;
816}
817
818
819
820
821
822
823
824
825
826static void ifx_spi_handle_srdy(struct ifx_spi_device *ifx_dev)
827{
828 if (test_bit(IFX_SPI_STATE_TIMER_PENDING, &ifx_dev->flags)) {
829 del_timer_sync(&ifx_dev->spi_timer);
830 clear_bit(IFX_SPI_STATE_TIMER_PENDING, &ifx_dev->flags);
831 }
832
833 ifx_spi_power_state_set(ifx_dev, IFX_SPI_POWER_SRDY);
834
835 if (!test_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &ifx_dev->flags))
836 tasklet_schedule(&ifx_dev->io_work_tasklet);
837 else
838 set_bit(IFX_SPI_STATE_IO_READY, &ifx_dev->flags);
839}
840
841
842
843
844
845
846
847
848static irqreturn_t ifx_spi_srdy_interrupt(int irq, void *dev)
849{
850 struct ifx_spi_device *ifx_dev = dev;
851 ifx_dev->gpio.unack_srdy_int_nb++;
852 ifx_spi_handle_srdy(ifx_dev);
853 return IRQ_HANDLED;
854}
855
856
857
858
859
860
861
862
863
864
865
866
867static irqreturn_t ifx_spi_reset_interrupt(int irq, void *dev)
868{
869 struct ifx_spi_device *ifx_dev = dev;
870 int val = gpio_get_value(ifx_dev->gpio.reset_out);
871 int solreset = test_bit(MR_START, &ifx_dev->mdm_reset_state);
872
873 if (val == 0) {
874
875 set_bit(MR_INPROGRESS, &ifx_dev->mdm_reset_state);
876 if (!solreset) {
877
878 ifx_spi_ttyhangup(ifx_dev);
879 }
880 } else {
881
882 clear_bit(MR_INPROGRESS, &ifx_dev->mdm_reset_state);
883 if (solreset) {
884 set_bit(MR_COMPLETE, &ifx_dev->mdm_reset_state);
885 wake_up(&ifx_dev->mdm_reset_wait);
886 }
887 }
888 return IRQ_HANDLED;
889}
890
891
892
893
894
895
896
897static void ifx_spi_free_device(struct ifx_spi_device *ifx_dev)
898{
899 ifx_spi_free_port(ifx_dev);
900 dma_free_coherent(&ifx_dev->spi_dev->dev,
901 IFX_SPI_TRANSFER_SIZE,
902 ifx_dev->tx_buffer,
903 ifx_dev->tx_bus);
904 dma_free_coherent(&ifx_dev->spi_dev->dev,
905 IFX_SPI_TRANSFER_SIZE,
906 ifx_dev->rx_buffer,
907 ifx_dev->rx_bus);
908}
909
910
911
912
913
914
915
916static int ifx_spi_reset(struct ifx_spi_device *ifx_dev)
917{
918 int ret;
919
920
921
922
923
924
925 set_bit(MR_START, &ifx_dev->mdm_reset_state);
926 gpio_set_value(ifx_dev->gpio.po, 0);
927 gpio_set_value(ifx_dev->gpio.reset, 0);
928 msleep(25);
929 gpio_set_value(ifx_dev->gpio.reset, 1);
930 msleep(1);
931 gpio_set_value(ifx_dev->gpio.po, 1);
932 msleep(1);
933 gpio_set_value(ifx_dev->gpio.po, 0);
934 ret = wait_event_timeout(ifx_dev->mdm_reset_wait,
935 test_bit(MR_COMPLETE,
936 &ifx_dev->mdm_reset_state),
937 IFX_RESET_TIMEOUT);
938 if (!ret)
939 dev_warn(&ifx_dev->spi_dev->dev, "Modem reset timeout: (state:%lx)",
940 ifx_dev->mdm_reset_state);
941
942 ifx_dev->mdm_reset_state = 0;
943 return ret;
944}
945
946
947
948
949
950
951
952
953
954
955
956
957
958static int ifx_spi_spi_probe(struct spi_device *spi)
959{
960 int ret;
961 int srdy;
962 struct ifx_modem_platform_data *pl_data;
963 struct ifx_spi_device *ifx_dev;
964
965 if (saved_ifx_dev) {
966 dev_dbg(&spi->dev, "ignoring subsequent detection");
967 return -ENODEV;
968 }
969
970 pl_data = (struct ifx_modem_platform_data *)spi->dev.platform_data;
971 if (!pl_data) {
972 dev_err(&spi->dev, "missing platform data!");
973 return -ENODEV;
974 }
975
976
977 ifx_dev = kzalloc(sizeof(struct ifx_spi_device), GFP_KERNEL);
978 if (!ifx_dev) {
979 dev_err(&spi->dev, "spi device allocation failed");
980 return -ENOMEM;
981 }
982 saved_ifx_dev = ifx_dev;
983 ifx_dev->spi_dev = spi;
984 clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &ifx_dev->flags);
985 spin_lock_init(&ifx_dev->write_lock);
986 spin_lock_init(&ifx_dev->power_lock);
987 ifx_dev->power_status = 0;
988 init_timer(&ifx_dev->spi_timer);
989 ifx_dev->spi_timer.function = ifx_spi_timeout;
990 ifx_dev->spi_timer.data = (unsigned long)ifx_dev;
991 ifx_dev->modem = pl_data->modem_type;
992 ifx_dev->use_dma = pl_data->use_dma;
993 ifx_dev->max_hz = pl_data->max_hz;
994
995 spi->max_speed_hz = ifx_dev->max_hz;
996 spi->mode = IFX_SPI_MODE | (SPI_LOOP & spi->mode);
997 spi->bits_per_word = spi_bpw;
998 ret = spi_setup(spi);
999 if (ret) {
1000 dev_err(&spi->dev, "SPI setup wasn't successful %d", ret);
1001 return -ENODEV;
1002 }
1003
1004
1005 ifx_dev->spi_more = 0;
1006 ifx_dev->spi_slave_cts = 0;
1007
1008
1009 ifx_dev->tx_buffer = dma_alloc_coherent(ifx_dev->spi_dev->dev.parent,
1010 IFX_SPI_TRANSFER_SIZE,
1011 &ifx_dev->tx_bus,
1012 GFP_KERNEL);
1013 if (!ifx_dev->tx_buffer) {
1014 dev_err(&spi->dev, "DMA-TX buffer allocation failed");
1015 ret = -ENOMEM;
1016 goto error_ret;
1017 }
1018 ifx_dev->rx_buffer = dma_alloc_coherent(ifx_dev->spi_dev->dev.parent,
1019 IFX_SPI_TRANSFER_SIZE,
1020 &ifx_dev->rx_bus,
1021 GFP_KERNEL);
1022 if (!ifx_dev->rx_buffer) {
1023 dev_err(&spi->dev, "DMA-RX buffer allocation failed");
1024 ret = -ENOMEM;
1025 goto error_ret;
1026 }
1027
1028
1029 init_waitqueue_head(&ifx_dev->mdm_reset_wait);
1030
1031 spi_set_drvdata(spi, ifx_dev);
1032 tasklet_init(&ifx_dev->io_work_tasklet, ifx_spi_io,
1033 (unsigned long)ifx_dev);
1034
1035 set_bit(IFX_SPI_STATE_PRESENT, &ifx_dev->flags);
1036
1037
1038 ret = ifx_spi_create_port(ifx_dev);
1039 if (ret != 0) {
1040 dev_err(&spi->dev, "create default tty port failed");
1041 goto error_ret;
1042 }
1043
1044 ifx_dev->gpio.reset = pl_data->rst_pmu;
1045 ifx_dev->gpio.po = pl_data->pwr_on;
1046 ifx_dev->gpio.mrdy = pl_data->mrdy;
1047 ifx_dev->gpio.srdy = pl_data->srdy;
1048 ifx_dev->gpio.reset_out = pl_data->rst_out;
1049
1050 dev_info(&spi->dev, "gpios %d, %d, %d, %d, %d",
1051 ifx_dev->gpio.reset, ifx_dev->gpio.po, ifx_dev->gpio.mrdy,
1052 ifx_dev->gpio.srdy, ifx_dev->gpio.reset_out);
1053
1054
1055 ret = gpio_request(ifx_dev->gpio.reset, "ifxModem");
1056 if (ret < 0) {
1057 dev_err(&spi->dev, "Unable to allocate GPIO%d (RESET)",
1058 ifx_dev->gpio.reset);
1059 goto error_ret;
1060 }
1061 ret += gpio_direction_output(ifx_dev->gpio.reset, 0);
1062 ret += gpio_export(ifx_dev->gpio.reset, 1);
1063 if (ret) {
1064 dev_err(&spi->dev, "Unable to configure GPIO%d (RESET)",
1065 ifx_dev->gpio.reset);
1066 ret = -EBUSY;
1067 goto error_ret2;
1068 }
1069
1070 ret = gpio_request(ifx_dev->gpio.po, "ifxModem");
1071 ret += gpio_direction_output(ifx_dev->gpio.po, 0);
1072 ret += gpio_export(ifx_dev->gpio.po, 1);
1073 if (ret) {
1074 dev_err(&spi->dev, "Unable to configure GPIO%d (ON)",
1075 ifx_dev->gpio.po);
1076 ret = -EBUSY;
1077 goto error_ret3;
1078 }
1079
1080 ret = gpio_request(ifx_dev->gpio.mrdy, "ifxModem");
1081 if (ret < 0) {
1082 dev_err(&spi->dev, "Unable to allocate GPIO%d (MRDY)",
1083 ifx_dev->gpio.mrdy);
1084 goto error_ret3;
1085 }
1086 ret += gpio_export(ifx_dev->gpio.mrdy, 1);
1087 ret += gpio_direction_output(ifx_dev->gpio.mrdy, 0);
1088 if (ret) {
1089 dev_err(&spi->dev, "Unable to configure GPIO%d (MRDY)",
1090 ifx_dev->gpio.mrdy);
1091 ret = -EBUSY;
1092 goto error_ret4;
1093 }
1094
1095 ret = gpio_request(ifx_dev->gpio.srdy, "ifxModem");
1096 if (ret < 0) {
1097 dev_err(&spi->dev, "Unable to allocate GPIO%d (SRDY)",
1098 ifx_dev->gpio.srdy);
1099 ret = -EBUSY;
1100 goto error_ret4;
1101 }
1102 ret += gpio_export(ifx_dev->gpio.srdy, 1);
1103 ret += gpio_direction_input(ifx_dev->gpio.srdy);
1104 if (ret) {
1105 dev_err(&spi->dev, "Unable to configure GPIO%d (SRDY)",
1106 ifx_dev->gpio.srdy);
1107 ret = -EBUSY;
1108 goto error_ret5;
1109 }
1110
1111 ret = gpio_request(ifx_dev->gpio.reset_out, "ifxModem");
1112 if (ret < 0) {
1113 dev_err(&spi->dev, "Unable to allocate GPIO%d (RESET_OUT)",
1114 ifx_dev->gpio.reset_out);
1115 goto error_ret5;
1116 }
1117 ret += gpio_export(ifx_dev->gpio.reset_out, 1);
1118 ret += gpio_direction_input(ifx_dev->gpio.reset_out);
1119 if (ret) {
1120 dev_err(&spi->dev, "Unable to configure GPIO%d (RESET_OUT)",
1121 ifx_dev->gpio.reset_out);
1122 ret = -EBUSY;
1123 goto error_ret6;
1124 }
1125
1126 ret = request_irq(gpio_to_irq(ifx_dev->gpio.reset_out),
1127 ifx_spi_reset_interrupt,
1128 IRQF_TRIGGER_RISING|IRQF_TRIGGER_FALLING, DRVNAME,
1129 (void *)ifx_dev);
1130 if (ret) {
1131 dev_err(&spi->dev, "Unable to get irq %x\n",
1132 gpio_to_irq(ifx_dev->gpio.reset_out));
1133 goto error_ret6;
1134 }
1135
1136 ret = ifx_spi_reset(ifx_dev);
1137
1138 ret = request_irq(gpio_to_irq(ifx_dev->gpio.srdy),
1139 ifx_spi_srdy_interrupt,
1140 IRQF_TRIGGER_RISING, DRVNAME,
1141 (void *)ifx_dev);
1142 if (ret) {
1143 dev_err(&spi->dev, "Unable to get irq %x",
1144 gpio_to_irq(ifx_dev->gpio.srdy));
1145 goto error_ret7;
1146 }
1147
1148
1149 pm_runtime_set_active(&spi->dev);
1150 pm_runtime_enable(&spi->dev);
1151
1152
1153
1154
1155
1156 srdy = gpio_get_value(ifx_dev->gpio.srdy);
1157
1158 if (srdy) {
1159 mrdy_assert(ifx_dev);
1160 ifx_spi_handle_srdy(ifx_dev);
1161 } else
1162 mrdy_set_low(ifx_dev);
1163 return 0;
1164
1165error_ret7:
1166 free_irq(gpio_to_irq(ifx_dev->gpio.reset_out), (void *)ifx_dev);
1167error_ret6:
1168 gpio_free(ifx_dev->gpio.srdy);
1169error_ret5:
1170 gpio_free(ifx_dev->gpio.mrdy);
1171error_ret4:
1172 gpio_free(ifx_dev->gpio.reset);
1173error_ret3:
1174 gpio_free(ifx_dev->gpio.po);
1175error_ret2:
1176 gpio_free(ifx_dev->gpio.reset_out);
1177error_ret:
1178 ifx_spi_free_device(ifx_dev);
1179 saved_ifx_dev = NULL;
1180 return ret;
1181}
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191static int ifx_spi_spi_remove(struct spi_device *spi)
1192{
1193 struct ifx_spi_device *ifx_dev = spi_get_drvdata(spi);
1194
1195 tasklet_kill(&ifx_dev->io_work_tasklet);
1196
1197 free_irq(gpio_to_irq(ifx_dev->gpio.reset_out), (void *)ifx_dev);
1198 free_irq(gpio_to_irq(ifx_dev->gpio.srdy), (void *)ifx_dev);
1199
1200 gpio_free(ifx_dev->gpio.srdy);
1201 gpio_free(ifx_dev->gpio.mrdy);
1202 gpio_free(ifx_dev->gpio.reset);
1203 gpio_free(ifx_dev->gpio.po);
1204 gpio_free(ifx_dev->gpio.reset_out);
1205
1206
1207 ifx_spi_free_device(ifx_dev);
1208
1209 saved_ifx_dev = NULL;
1210 return 0;
1211}
1212
1213
1214
1215
1216
1217
1218
1219
1220static void ifx_spi_spi_shutdown(struct spi_device *spi)
1221{
1222}
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236static int ifx_spi_spi_suspend(struct spi_device *spi, pm_message_t msg)
1237{
1238 return 0;
1239}
1240
1241
1242
1243
1244
1245
1246
1247
1248static int ifx_spi_spi_resume(struct spi_device *spi)
1249{
1250 return 0;
1251}
1252
1253
1254
1255
1256
1257
1258
1259
1260static int ifx_spi_pm_suspend(struct device *dev)
1261{
1262 return 0;
1263}
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273static int ifx_spi_pm_resume(struct device *dev)
1274{
1275 return 0;
1276}
1277
1278
1279
1280
1281
1282
1283
1284static int ifx_spi_pm_runtime_resume(struct device *dev)
1285{
1286 return 0;
1287}
1288
1289
1290
1291
1292
1293
1294
1295
1296static int ifx_spi_pm_runtime_suspend(struct device *dev)
1297{
1298 return 0;
1299}
1300
1301
1302
1303
1304
1305
1306
1307static int ifx_spi_pm_runtime_idle(struct device *dev)
1308{
1309 struct spi_device *spi = to_spi_device(dev);
1310 struct ifx_spi_device *ifx_dev = spi_get_drvdata(spi);
1311
1312 if (!ifx_dev->power_status)
1313 pm_runtime_suspend(dev);
1314
1315 return 0;
1316}
1317
1318static const struct dev_pm_ops ifx_spi_pm = {
1319 .resume = ifx_spi_pm_resume,
1320 .suspend = ifx_spi_pm_suspend,
1321 .runtime_resume = ifx_spi_pm_runtime_resume,
1322 .runtime_suspend = ifx_spi_pm_runtime_suspend,
1323 .runtime_idle = ifx_spi_pm_runtime_idle
1324};
1325
1326static const struct spi_device_id ifx_id_table[] = {
1327 {"ifx6160", 0},
1328 {"ifx6260", 0},
1329 { }
1330};
1331MODULE_DEVICE_TABLE(spi, ifx_id_table);
1332
1333
1334static struct spi_driver ifx_spi_driver = {
1335 .driver = {
1336 .name = DRVNAME,
1337 .pm = &ifx_spi_pm,
1338 .owner = THIS_MODULE},
1339 .probe = ifx_spi_spi_probe,
1340 .shutdown = ifx_spi_spi_shutdown,
1341 .remove = __devexit_p(ifx_spi_spi_remove),
1342 .suspend = ifx_spi_spi_suspend,
1343 .resume = ifx_spi_spi_resume,
1344 .id_table = ifx_id_table
1345};
1346
1347
1348
1349
1350
1351
1352
1353static void __exit ifx_spi_exit(void)
1354{
1355
1356 tty_unregister_driver(tty_drv);
1357 spi_unregister_driver((void *)&ifx_spi_driver);
1358}
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368static int __init ifx_spi_init(void)
1369{
1370 int result;
1371
1372 tty_drv = alloc_tty_driver(1);
1373 if (!tty_drv) {
1374 pr_err("%s: alloc_tty_driver failed", DRVNAME);
1375 return -ENOMEM;
1376 }
1377
1378 tty_drv->driver_name = DRVNAME;
1379 tty_drv->name = TTYNAME;
1380 tty_drv->minor_start = IFX_SPI_TTY_ID;
1381 tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
1382 tty_drv->subtype = SERIAL_TYPE_NORMAL;
1383 tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1384 tty_drv->init_termios = tty_std_termios;
1385
1386 tty_set_operations(tty_drv, &ifx_spi_serial_ops);
1387
1388 result = tty_register_driver(tty_drv);
1389 if (result) {
1390 pr_err("%s: tty_register_driver failed(%d)",
1391 DRVNAME, result);
1392 put_tty_driver(tty_drv);
1393 return result;
1394 }
1395
1396 result = spi_register_driver((void *)&ifx_spi_driver);
1397 if (result) {
1398 pr_err("%s: spi_register_driver failed(%d)",
1399 DRVNAME, result);
1400 tty_unregister_driver(tty_drv);
1401 }
1402 return result;
1403}
1404
1405module_init(ifx_spi_init);
1406module_exit(ifx_spi_exit);
1407
1408MODULE_AUTHOR("Intel");
1409MODULE_DESCRIPTION("IFX6x60 spi driver");
1410MODULE_LICENSE("GPL");
1411MODULE_INFO(Version, "0.1-IFX6x60");
1412