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