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