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 return IFX_SPI_HEADER_0;
399 } else if (h1 == 0xffff && h2 == 0xffff) {
400
401 return IFX_SPI_HEADER_F;
402 }
403
404 *length = h1 & 0xfff;
405 *more = (buffer[1] >> IFX_SPI_MORE_BIT) & 1;
406 *received_cts = (buffer[3] >> IFX_SPI_CTS_BIT) & 1;
407 return 0;
408}
409
410
411
412
413
414
415
416
417
418
419
420static void ifx_spi_setup_spi_header(unsigned char *txbuffer, int tx_count,
421 unsigned char more)
422{
423 *(u16 *)(txbuffer) = tx_count;
424 *(u16 *)(txbuffer+2) = IFX_SPI_PAYLOAD_SIZE;
425 txbuffer[1] |= (more << IFX_SPI_MORE_BIT) & IFX_SPI_MORE_MASK;
426}
427
428
429
430
431
432
433
434
435
436
437
438
439
440static int ifx_spi_prepare_tx_buffer(struct ifx_spi_device *ifx_dev)
441{
442 int temp_count;
443 int queue_length;
444 int tx_count;
445 unsigned char *tx_buffer;
446
447 tx_buffer = ifx_dev->tx_buffer;
448
449
450 tx_buffer += IFX_SPI_HEADER_OVERHEAD;
451 tx_count = IFX_SPI_HEADER_OVERHEAD;
452
453
454
455 ifx_dev->spi_more = 0;
456
457
458 if (!ifx_dev->spi_slave_cts) {
459
460 queue_length = kfifo_len(&ifx_dev->tx_fifo);
461 if (queue_length != 0) {
462
463 temp_count = min(queue_length, IFX_SPI_PAYLOAD_SIZE);
464 temp_count = kfifo_out_locked(&ifx_dev->tx_fifo,
465 tx_buffer, temp_count,
466 &ifx_dev->fifo_lock);
467
468
469 tx_buffer += temp_count;
470 tx_count += temp_count;
471 if (temp_count == queue_length)
472
473 tty_port_tty_wakeup(&ifx_dev->tty_port);
474 else
475 ifx_dev->spi_more = 1;
476 }
477 }
478
479
480 ifx_spi_setup_spi_header(ifx_dev->tx_buffer,
481 tx_count-IFX_SPI_HEADER_OVERHEAD,
482 ifx_dev->spi_more);
483
484 ifx_dev->swap_buf((ifx_dev->tx_buffer), tx_count,
485 &ifx_dev->tx_buffer[IFX_SPI_TRANSFER_SIZE]);
486 return tx_count;
487}
488
489
490
491
492
493
494
495
496
497
498
499static int ifx_spi_write(struct tty_struct *tty, const unsigned char *buf,
500 int count)
501{
502 struct ifx_spi_device *ifx_dev = tty->driver_data;
503 unsigned char *tmp_buf = (unsigned char *)buf;
504 unsigned long flags;
505 bool is_fifo_empty;
506 int tx_count;
507
508 spin_lock_irqsave(&ifx_dev->fifo_lock, flags);
509 is_fifo_empty = kfifo_is_empty(&ifx_dev->tx_fifo);
510 tx_count = kfifo_in(&ifx_dev->tx_fifo, tmp_buf, count);
511 spin_unlock_irqrestore(&ifx_dev->fifo_lock, flags);
512 if (is_fifo_empty)
513 mrdy_assert(ifx_dev);
514
515 return tx_count;
516}
517
518
519
520
521
522
523
524
525static int ifx_spi_write_room(struct tty_struct *tty)
526{
527 struct ifx_spi_device *ifx_dev = tty->driver_data;
528 return IFX_SPI_FIFO_SIZE - kfifo_len(&ifx_dev->tx_fifo);
529}
530
531
532
533
534
535
536
537
538static int ifx_spi_chars_in_buffer(struct tty_struct *tty)
539{
540 struct ifx_spi_device *ifx_dev = tty->driver_data;
541 return kfifo_len(&ifx_dev->tx_fifo);
542}
543
544
545
546
547
548
549
550
551
552static void ifx_spi_hangup(struct tty_struct *tty)
553{
554 struct ifx_spi_device *ifx_dev = tty->driver_data;
555 tty_port_hangup(&ifx_dev->tty_port);
556}
557
558
559
560
561
562
563
564
565static int ifx_port_activate(struct tty_port *port, struct tty_struct *tty)
566{
567 struct ifx_spi_device *ifx_dev =
568 container_of(port, struct ifx_spi_device, tty_port);
569
570
571 kfifo_reset(&ifx_dev->tx_fifo);
572
573
574 clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &ifx_dev->flags);
575 clear_bit(IFX_SPI_STATE_IO_READY, &ifx_dev->flags);
576
577
578 tty->driver_data = ifx_dev;
579
580
581 port->low_latency = 1;
582
583
584 set_bit(IFX_SPI_STATE_IO_AVAILABLE, &ifx_dev->flags);
585
586 return 0;
587}
588
589
590
591
592
593
594
595
596static void ifx_port_shutdown(struct tty_port *port)
597{
598 struct ifx_spi_device *ifx_dev =
599 container_of(port, struct ifx_spi_device, tty_port);
600
601 clear_bit(IFX_SPI_STATE_IO_AVAILABLE, &ifx_dev->flags);
602 mrdy_set_low(ifx_dev);
603 del_timer(&ifx_dev->spi_timer);
604 clear_bit(IFX_SPI_STATE_TIMER_PENDING, &ifx_dev->flags);
605 tasklet_kill(&ifx_dev->io_work_tasklet);
606}
607
608static const struct tty_port_operations ifx_tty_port_ops = {
609 .activate = ifx_port_activate,
610 .shutdown = ifx_port_shutdown,
611};
612
613static const struct tty_operations ifx_spi_serial_ops = {
614 .open = ifx_spi_open,
615 .close = ifx_spi_close,
616 .write = ifx_spi_write,
617 .hangup = ifx_spi_hangup,
618 .write_room = ifx_spi_write_room,
619 .chars_in_buffer = ifx_spi_chars_in_buffer,
620 .tiocmget = ifx_spi_tiocmget,
621 .tiocmset = ifx_spi_tiocmset,
622};
623
624
625
626
627
628
629
630
631
632
633static void ifx_spi_insert_flip_string(struct ifx_spi_device *ifx_dev,
634 unsigned char *chars, size_t size)
635{
636 tty_insert_flip_string(&ifx_dev->tty_port, chars, size);
637 tty_flip_buffer_push(&ifx_dev->tty_port);
638}
639
640
641
642
643
644
645
646
647static void ifx_spi_complete(void *ctx)
648{
649 struct ifx_spi_device *ifx_dev = ctx;
650 int length;
651 int actual_length;
652 unsigned char more;
653 unsigned char cts;
654 int local_write_pending = 0;
655 int queue_length;
656 int srdy;
657 int decode_result;
658
659 mrdy_set_low(ifx_dev);
660
661 if (!ifx_dev->spi_msg.status) {
662
663 ifx_dev->swap_buf(ifx_dev->rx_buffer, IFX_SPI_HEADER_OVERHEAD,
664 &ifx_dev->rx_buffer[IFX_SPI_HEADER_OVERHEAD]);
665 decode_result = ifx_spi_decode_spi_header(ifx_dev->rx_buffer,
666 &length, &more, &cts);
667 if (decode_result == IFX_SPI_HEADER_0) {
668 dev_dbg(&ifx_dev->spi_dev->dev,
669 "ignore input: invalid header 0");
670 ifx_dev->spi_slave_cts = 0;
671 goto complete_exit;
672 } else if (decode_result == IFX_SPI_HEADER_F) {
673 dev_dbg(&ifx_dev->spi_dev->dev,
674 "ignore input: invalid header F");
675 goto complete_exit;
676 }
677
678 ifx_dev->spi_slave_cts = cts;
679
680 actual_length = min((unsigned int)length,
681 ifx_dev->spi_msg.actual_length);
682 ifx_dev->swap_buf(
683 (ifx_dev->rx_buffer + IFX_SPI_HEADER_OVERHEAD),
684 actual_length,
685 &ifx_dev->rx_buffer[IFX_SPI_TRANSFER_SIZE]);
686 ifx_spi_insert_flip_string(
687 ifx_dev,
688 ifx_dev->rx_buffer + IFX_SPI_HEADER_OVERHEAD,
689 (size_t)actual_length);
690 } else {
691 dev_dbg(&ifx_dev->spi_dev->dev, "SPI transfer error %d",
692 ifx_dev->spi_msg.status);
693 }
694
695complete_exit:
696 if (ifx_dev->write_pending) {
697 ifx_dev->write_pending = 0;
698 local_write_pending = 1;
699 }
700
701 clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &(ifx_dev->flags));
702
703 queue_length = kfifo_len(&ifx_dev->tx_fifo);
704 srdy = gpio_get_value(ifx_dev->gpio.srdy);
705 if (!srdy)
706 ifx_spi_power_state_clear(ifx_dev, IFX_SPI_POWER_SRDY);
707
708
709 if (test_and_clear_bit(IFX_SPI_STATE_IO_READY, &ifx_dev->flags))
710 tasklet_schedule(&ifx_dev->io_work_tasklet);
711 else {
712 if (more || ifx_dev->spi_more || queue_length > 0 ||
713 local_write_pending) {
714 if (ifx_dev->spi_slave_cts) {
715 if (more)
716 mrdy_assert(ifx_dev);
717 } else
718 mrdy_assert(ifx_dev);
719 } else {
720
721
722
723
724
725 ifx_spi_power_state_clear(ifx_dev,
726 IFX_SPI_POWER_DATA_PENDING);
727 tty_port_tty_wakeup(&ifx_dev->tty_port);
728 }
729 }
730}
731
732
733
734
735
736
737
738
739static void ifx_spi_io(unsigned long data)
740{
741 int retval;
742 struct ifx_spi_device *ifx_dev = (struct ifx_spi_device *) data;
743
744 if (!test_and_set_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &ifx_dev->flags) &&
745 test_bit(IFX_SPI_STATE_IO_AVAILABLE, &ifx_dev->flags)) {
746 if (ifx_dev->gpio.unack_srdy_int_nb > 0)
747 ifx_dev->gpio.unack_srdy_int_nb--;
748
749 ifx_spi_prepare_tx_buffer(ifx_dev);
750
751 spi_message_init(&ifx_dev->spi_msg);
752 INIT_LIST_HEAD(&ifx_dev->spi_msg.queue);
753
754 ifx_dev->spi_msg.context = ifx_dev;
755 ifx_dev->spi_msg.complete = ifx_spi_complete;
756
757
758
759 ifx_dev->spi_xfer.len = IFX_SPI_TRANSFER_SIZE;
760 ifx_dev->spi_xfer.cs_change = 0;
761 ifx_dev->spi_xfer.speed_hz = ifx_dev->spi_dev->max_speed_hz;
762
763 ifx_dev->spi_xfer.bits_per_word =
764 ifx_dev->spi_dev->bits_per_word;
765
766 ifx_dev->spi_xfer.tx_buf = ifx_dev->tx_buffer;
767 ifx_dev->spi_xfer.rx_buf = ifx_dev->rx_buffer;
768
769
770
771
772 if (ifx_dev->use_dma) {
773 ifx_dev->spi_msg.is_dma_mapped = 1;
774 ifx_dev->tx_dma = ifx_dev->tx_bus;
775 ifx_dev->rx_dma = ifx_dev->rx_bus;
776 ifx_dev->spi_xfer.tx_dma = ifx_dev->tx_dma;
777 ifx_dev->spi_xfer.rx_dma = ifx_dev->rx_dma;
778 } else {
779 ifx_dev->spi_msg.is_dma_mapped = 0;
780 ifx_dev->tx_dma = (dma_addr_t)0;
781 ifx_dev->rx_dma = (dma_addr_t)0;
782 ifx_dev->spi_xfer.tx_dma = (dma_addr_t)0;
783 ifx_dev->spi_xfer.rx_dma = (dma_addr_t)0;
784 }
785
786 spi_message_add_tail(&ifx_dev->spi_xfer, &ifx_dev->spi_msg);
787
788
789
790
791 mrdy_assert(ifx_dev);
792
793 retval = spi_async(ifx_dev->spi_dev, &ifx_dev->spi_msg);
794 if (retval) {
795 clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS,
796 &ifx_dev->flags);
797 tasklet_schedule(&ifx_dev->io_work_tasklet);
798 return;
799 }
800 } else
801 ifx_dev->write_pending = 1;
802}
803
804
805
806
807
808
809
810static void ifx_spi_free_port(struct ifx_spi_device *ifx_dev)
811{
812 if (ifx_dev->tty_dev)
813 tty_unregister_device(tty_drv, ifx_dev->minor);
814 tty_port_destroy(&ifx_dev->tty_port);
815 kfifo_free(&ifx_dev->tx_fifo);
816}
817
818
819
820
821
822
823
824
825static int ifx_spi_create_port(struct ifx_spi_device *ifx_dev)
826{
827 int ret = 0;
828 struct tty_port *pport = &ifx_dev->tty_port;
829
830 spin_lock_init(&ifx_dev->fifo_lock);
831 lockdep_set_class_and_subclass(&ifx_dev->fifo_lock,
832 &ifx_spi_key, 0);
833
834 if (kfifo_alloc(&ifx_dev->tx_fifo, IFX_SPI_FIFO_SIZE, GFP_KERNEL)) {
835 ret = -ENOMEM;
836 goto error_ret;
837 }
838
839 tty_port_init(pport);
840 pport->ops = &ifx_tty_port_ops;
841 ifx_dev->minor = IFX_SPI_TTY_ID;
842 ifx_dev->tty_dev = tty_port_register_device(pport, tty_drv,
843 ifx_dev->minor, &ifx_dev->spi_dev->dev);
844 if (IS_ERR(ifx_dev->tty_dev)) {
845 dev_dbg(&ifx_dev->spi_dev->dev,
846 "%s: registering tty device failed", __func__);
847 ret = PTR_ERR(ifx_dev->tty_dev);
848 goto error_port;
849 }
850 return 0;
851
852error_port:
853 tty_port_destroy(pport);
854error_ret:
855 ifx_spi_free_port(ifx_dev);
856 return ret;
857}
858
859
860
861
862
863
864
865
866
867static void ifx_spi_handle_srdy(struct ifx_spi_device *ifx_dev)
868{
869 if (test_bit(IFX_SPI_STATE_TIMER_PENDING, &ifx_dev->flags)) {
870 del_timer(&ifx_dev->spi_timer);
871 clear_bit(IFX_SPI_STATE_TIMER_PENDING, &ifx_dev->flags);
872 }
873
874 ifx_spi_power_state_set(ifx_dev, IFX_SPI_POWER_SRDY);
875
876 if (!test_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &ifx_dev->flags))
877 tasklet_schedule(&ifx_dev->io_work_tasklet);
878 else
879 set_bit(IFX_SPI_STATE_IO_READY, &ifx_dev->flags);
880}
881
882
883
884
885
886
887
888
889static irqreturn_t ifx_spi_srdy_interrupt(int irq, void *dev)
890{
891 struct ifx_spi_device *ifx_dev = dev;
892 ifx_dev->gpio.unack_srdy_int_nb++;
893 ifx_spi_handle_srdy(ifx_dev);
894 return IRQ_HANDLED;
895}
896
897
898
899
900
901
902
903
904
905
906
907
908static irqreturn_t ifx_spi_reset_interrupt(int irq, void *dev)
909{
910 struct ifx_spi_device *ifx_dev = dev;
911 int val = gpio_get_value(ifx_dev->gpio.reset_out);
912 int solreset = test_bit(MR_START, &ifx_dev->mdm_reset_state);
913
914 if (val == 0) {
915
916 set_bit(MR_INPROGRESS, &ifx_dev->mdm_reset_state);
917 if (!solreset) {
918
919 tty_port_tty_hangup(&ifx_dev->tty_port, false);
920 }
921 } else {
922
923 clear_bit(MR_INPROGRESS, &ifx_dev->mdm_reset_state);
924 if (solreset) {
925 set_bit(MR_COMPLETE, &ifx_dev->mdm_reset_state);
926 wake_up(&ifx_dev->mdm_reset_wait);
927 }
928 }
929 return IRQ_HANDLED;
930}
931
932
933
934
935
936
937
938static void ifx_spi_free_device(struct ifx_spi_device *ifx_dev)
939{
940 ifx_spi_free_port(ifx_dev);
941 dma_free_coherent(&ifx_dev->spi_dev->dev,
942 IFX_SPI_TRANSFER_SIZE,
943 ifx_dev->tx_buffer,
944 ifx_dev->tx_bus);
945 dma_free_coherent(&ifx_dev->spi_dev->dev,
946 IFX_SPI_TRANSFER_SIZE,
947 ifx_dev->rx_buffer,
948 ifx_dev->rx_bus);
949}
950
951
952
953
954
955
956
957static int ifx_spi_reset(struct ifx_spi_device *ifx_dev)
958{
959 int ret;
960
961
962
963
964
965
966 set_bit(MR_START, &ifx_dev->mdm_reset_state);
967 gpio_set_value(ifx_dev->gpio.po, 0);
968 gpio_set_value(ifx_dev->gpio.reset, 0);
969 msleep(25);
970 gpio_set_value(ifx_dev->gpio.reset, 1);
971 msleep(1);
972 gpio_set_value(ifx_dev->gpio.po, 1);
973 msleep(1);
974 gpio_set_value(ifx_dev->gpio.po, 0);
975 ret = wait_event_timeout(ifx_dev->mdm_reset_wait,
976 test_bit(MR_COMPLETE,
977 &ifx_dev->mdm_reset_state),
978 IFX_RESET_TIMEOUT);
979 if (!ret)
980 dev_warn(&ifx_dev->spi_dev->dev, "Modem reset timeout: (state:%lx)",
981 ifx_dev->mdm_reset_state);
982
983 ifx_dev->mdm_reset_state = 0;
984 return ret;
985}
986
987
988
989
990
991
992
993
994
995
996
997
998
999static int ifx_spi_spi_probe(struct spi_device *spi)
1000{
1001 int ret;
1002 int srdy;
1003 struct ifx_modem_platform_data *pl_data;
1004 struct ifx_spi_device *ifx_dev;
1005
1006 if (saved_ifx_dev) {
1007 dev_dbg(&spi->dev, "ignoring subsequent detection");
1008 return -ENODEV;
1009 }
1010
1011 pl_data = dev_get_platdata(&spi->dev);
1012 if (!pl_data) {
1013 dev_err(&spi->dev, "missing platform data!");
1014 return -ENODEV;
1015 }
1016
1017
1018 ifx_dev = kzalloc(sizeof(struct ifx_spi_device), GFP_KERNEL);
1019 if (!ifx_dev) {
1020 dev_err(&spi->dev, "spi device allocation failed");
1021 return -ENOMEM;
1022 }
1023 saved_ifx_dev = ifx_dev;
1024 ifx_dev->spi_dev = spi;
1025 clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &ifx_dev->flags);
1026 spin_lock_init(&ifx_dev->write_lock);
1027 spin_lock_init(&ifx_dev->power_lock);
1028 ifx_dev->power_status = 0;
1029 init_timer(&ifx_dev->spi_timer);
1030 ifx_dev->spi_timer.function = ifx_spi_timeout;
1031 ifx_dev->spi_timer.data = (unsigned long)ifx_dev;
1032 ifx_dev->modem = pl_data->modem_type;
1033 ifx_dev->use_dma = pl_data->use_dma;
1034 ifx_dev->max_hz = pl_data->max_hz;
1035
1036 spi->max_speed_hz = ifx_dev->max_hz;
1037 spi->mode = IFX_SPI_MODE | (SPI_LOOP & spi->mode);
1038 spi->bits_per_word = spi_bpw;
1039 ret = spi_setup(spi);
1040 if (ret) {
1041 dev_err(&spi->dev, "SPI setup wasn't successful %d", ret);
1042 return -ENODEV;
1043 }
1044
1045
1046 if (spi->bits_per_word == 32)
1047 ifx_dev->swap_buf = swap_buf_32;
1048 else if (spi->bits_per_word == 16)
1049 ifx_dev->swap_buf = swap_buf_16;
1050 else
1051 ifx_dev->swap_buf = swap_buf_8;
1052
1053
1054 ifx_dev->spi_more = 0;
1055 ifx_dev->spi_slave_cts = 0;
1056
1057
1058 ifx_dev->tx_buffer = dma_alloc_coherent(ifx_dev->spi_dev->dev.parent,
1059 IFX_SPI_TRANSFER_SIZE,
1060 &ifx_dev->tx_bus,
1061 GFP_KERNEL);
1062 if (!ifx_dev->tx_buffer) {
1063 dev_err(&spi->dev, "DMA-TX buffer allocation failed");
1064 ret = -ENOMEM;
1065 goto error_ret;
1066 }
1067 ifx_dev->rx_buffer = dma_alloc_coherent(ifx_dev->spi_dev->dev.parent,
1068 IFX_SPI_TRANSFER_SIZE,
1069 &ifx_dev->rx_bus,
1070 GFP_KERNEL);
1071 if (!ifx_dev->rx_buffer) {
1072 dev_err(&spi->dev, "DMA-RX buffer allocation failed");
1073 ret = -ENOMEM;
1074 goto error_ret;
1075 }
1076
1077
1078 init_waitqueue_head(&ifx_dev->mdm_reset_wait);
1079
1080 spi_set_drvdata(spi, ifx_dev);
1081 tasklet_init(&ifx_dev->io_work_tasklet, ifx_spi_io,
1082 (unsigned long)ifx_dev);
1083
1084 set_bit(IFX_SPI_STATE_PRESENT, &ifx_dev->flags);
1085
1086
1087 ret = ifx_spi_create_port(ifx_dev);
1088 if (ret != 0) {
1089 dev_err(&spi->dev, "create default tty port failed");
1090 goto error_ret;
1091 }
1092
1093 ifx_dev->gpio.reset = pl_data->rst_pmu;
1094 ifx_dev->gpio.po = pl_data->pwr_on;
1095 ifx_dev->gpio.mrdy = pl_data->mrdy;
1096 ifx_dev->gpio.srdy = pl_data->srdy;
1097 ifx_dev->gpio.reset_out = pl_data->rst_out;
1098
1099 dev_info(&spi->dev, "gpios %d, %d, %d, %d, %d",
1100 ifx_dev->gpio.reset, ifx_dev->gpio.po, ifx_dev->gpio.mrdy,
1101 ifx_dev->gpio.srdy, ifx_dev->gpio.reset_out);
1102
1103
1104 ret = gpio_request(ifx_dev->gpio.reset, "ifxModem");
1105 if (ret < 0) {
1106 dev_err(&spi->dev, "Unable to allocate GPIO%d (RESET)",
1107 ifx_dev->gpio.reset);
1108 goto error_ret;
1109 }
1110 ret += gpio_direction_output(ifx_dev->gpio.reset, 0);
1111 ret += gpio_export(ifx_dev->gpio.reset, 1);
1112 if (ret) {
1113 dev_err(&spi->dev, "Unable to configure GPIO%d (RESET)",
1114 ifx_dev->gpio.reset);
1115 ret = -EBUSY;
1116 goto error_ret2;
1117 }
1118
1119 ret = gpio_request(ifx_dev->gpio.po, "ifxModem");
1120 ret += gpio_direction_output(ifx_dev->gpio.po, 0);
1121 ret += gpio_export(ifx_dev->gpio.po, 1);
1122 if (ret) {
1123 dev_err(&spi->dev, "Unable to configure GPIO%d (ON)",
1124 ifx_dev->gpio.po);
1125 ret = -EBUSY;
1126 goto error_ret3;
1127 }
1128
1129 ret = gpio_request(ifx_dev->gpio.mrdy, "ifxModem");
1130 if (ret < 0) {
1131 dev_err(&spi->dev, "Unable to allocate GPIO%d (MRDY)",
1132 ifx_dev->gpio.mrdy);
1133 goto error_ret3;
1134 }
1135 ret += gpio_export(ifx_dev->gpio.mrdy, 1);
1136 ret += gpio_direction_output(ifx_dev->gpio.mrdy, 0);
1137 if (ret) {
1138 dev_err(&spi->dev, "Unable to configure GPIO%d (MRDY)",
1139 ifx_dev->gpio.mrdy);
1140 ret = -EBUSY;
1141 goto error_ret4;
1142 }
1143
1144 ret = gpio_request(ifx_dev->gpio.srdy, "ifxModem");
1145 if (ret < 0) {
1146 dev_err(&spi->dev, "Unable to allocate GPIO%d (SRDY)",
1147 ifx_dev->gpio.srdy);
1148 ret = -EBUSY;
1149 goto error_ret4;
1150 }
1151 ret += gpio_export(ifx_dev->gpio.srdy, 1);
1152 ret += gpio_direction_input(ifx_dev->gpio.srdy);
1153 if (ret) {
1154 dev_err(&spi->dev, "Unable to configure GPIO%d (SRDY)",
1155 ifx_dev->gpio.srdy);
1156 ret = -EBUSY;
1157 goto error_ret5;
1158 }
1159
1160 ret = gpio_request(ifx_dev->gpio.reset_out, "ifxModem");
1161 if (ret < 0) {
1162 dev_err(&spi->dev, "Unable to allocate GPIO%d (RESET_OUT)",
1163 ifx_dev->gpio.reset_out);
1164 goto error_ret5;
1165 }
1166 ret += gpio_export(ifx_dev->gpio.reset_out, 1);
1167 ret += gpio_direction_input(ifx_dev->gpio.reset_out);
1168 if (ret) {
1169 dev_err(&spi->dev, "Unable to configure GPIO%d (RESET_OUT)",
1170 ifx_dev->gpio.reset_out);
1171 ret = -EBUSY;
1172 goto error_ret6;
1173 }
1174
1175 ret = request_irq(gpio_to_irq(ifx_dev->gpio.reset_out),
1176 ifx_spi_reset_interrupt,
1177 IRQF_TRIGGER_RISING|IRQF_TRIGGER_FALLING, DRVNAME,
1178 (void *)ifx_dev);
1179 if (ret) {
1180 dev_err(&spi->dev, "Unable to get irq %x\n",
1181 gpio_to_irq(ifx_dev->gpio.reset_out));
1182 goto error_ret6;
1183 }
1184
1185 ret = ifx_spi_reset(ifx_dev);
1186
1187 ret = request_irq(gpio_to_irq(ifx_dev->gpio.srdy),
1188 ifx_spi_srdy_interrupt,
1189 IRQF_TRIGGER_RISING, DRVNAME,
1190 (void *)ifx_dev);
1191 if (ret) {
1192 dev_err(&spi->dev, "Unable to get irq %x",
1193 gpio_to_irq(ifx_dev->gpio.srdy));
1194 goto error_ret7;
1195 }
1196
1197
1198 pm_runtime_set_active(&spi->dev);
1199 pm_runtime_enable(&spi->dev);
1200
1201
1202
1203
1204
1205 srdy = gpio_get_value(ifx_dev->gpio.srdy);
1206
1207 if (srdy) {
1208 mrdy_assert(ifx_dev);
1209 ifx_spi_handle_srdy(ifx_dev);
1210 } else
1211 mrdy_set_low(ifx_dev);
1212 return 0;
1213
1214error_ret7:
1215 free_irq(gpio_to_irq(ifx_dev->gpio.reset_out), (void *)ifx_dev);
1216error_ret6:
1217 gpio_free(ifx_dev->gpio.srdy);
1218error_ret5:
1219 gpio_free(ifx_dev->gpio.mrdy);
1220error_ret4:
1221 gpio_free(ifx_dev->gpio.reset);
1222error_ret3:
1223 gpio_free(ifx_dev->gpio.po);
1224error_ret2:
1225 gpio_free(ifx_dev->gpio.reset_out);
1226error_ret:
1227 ifx_spi_free_device(ifx_dev);
1228 saved_ifx_dev = NULL;
1229 return ret;
1230}
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240static int ifx_spi_spi_remove(struct spi_device *spi)
1241{
1242 struct ifx_spi_device *ifx_dev = spi_get_drvdata(spi);
1243
1244 tasklet_kill(&ifx_dev->io_work_tasklet);
1245
1246 free_irq(gpio_to_irq(ifx_dev->gpio.reset_out), (void *)ifx_dev);
1247 free_irq(gpio_to_irq(ifx_dev->gpio.srdy), (void *)ifx_dev);
1248
1249 gpio_free(ifx_dev->gpio.srdy);
1250 gpio_free(ifx_dev->gpio.mrdy);
1251 gpio_free(ifx_dev->gpio.reset);
1252 gpio_free(ifx_dev->gpio.po);
1253 gpio_free(ifx_dev->gpio.reset_out);
1254
1255
1256 ifx_spi_free_device(ifx_dev);
1257
1258 saved_ifx_dev = NULL;
1259 return 0;
1260}
1261
1262
1263
1264
1265
1266
1267
1268
1269static void ifx_spi_spi_shutdown(struct spi_device *spi)
1270{
1271 struct ifx_spi_device *ifx_dev = spi_get_drvdata(spi);
1272
1273 ifx_modem_power_off(ifx_dev);
1274}
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288static int ifx_spi_pm_suspend(struct device *dev)
1289{
1290 return 0;
1291}
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301static int ifx_spi_pm_resume(struct device *dev)
1302{
1303 return 0;
1304}
1305
1306
1307
1308
1309
1310
1311
1312static int ifx_spi_pm_runtime_resume(struct device *dev)
1313{
1314 return 0;
1315}
1316
1317
1318
1319
1320
1321
1322
1323
1324static int ifx_spi_pm_runtime_suspend(struct device *dev)
1325{
1326 return 0;
1327}
1328
1329
1330
1331
1332
1333
1334
1335static int ifx_spi_pm_runtime_idle(struct device *dev)
1336{
1337 struct spi_device *spi = to_spi_device(dev);
1338 struct ifx_spi_device *ifx_dev = spi_get_drvdata(spi);
1339
1340 if (!ifx_dev->power_status)
1341 pm_runtime_suspend(dev);
1342
1343 return 0;
1344}
1345
1346static const struct dev_pm_ops ifx_spi_pm = {
1347 .resume = ifx_spi_pm_resume,
1348 .suspend = ifx_spi_pm_suspend,
1349 .runtime_resume = ifx_spi_pm_runtime_resume,
1350 .runtime_suspend = ifx_spi_pm_runtime_suspend,
1351 .runtime_idle = ifx_spi_pm_runtime_idle
1352};
1353
1354static const struct spi_device_id ifx_id_table[] = {
1355 {"ifx6160", 0},
1356 {"ifx6260", 0},
1357 { }
1358};
1359MODULE_DEVICE_TABLE(spi, ifx_id_table);
1360
1361
1362static struct spi_driver ifx_spi_driver = {
1363 .driver = {
1364 .name = DRVNAME,
1365 .pm = &ifx_spi_pm,
1366 .owner = THIS_MODULE},
1367 .probe = ifx_spi_spi_probe,
1368 .shutdown = ifx_spi_spi_shutdown,
1369 .remove = ifx_spi_spi_remove,
1370 .id_table = ifx_id_table
1371};
1372
1373
1374
1375
1376
1377
1378
1379static void __exit ifx_spi_exit(void)
1380{
1381
1382 tty_unregister_driver(tty_drv);
1383 put_tty_driver(tty_drv);
1384 spi_unregister_driver((void *)&ifx_spi_driver);
1385 unregister_reboot_notifier(&ifx_modem_reboot_notifier_block);
1386}
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396static int __init ifx_spi_init(void)
1397{
1398 int result;
1399
1400 tty_drv = alloc_tty_driver(1);
1401 if (!tty_drv) {
1402 pr_err("%s: alloc_tty_driver failed", DRVNAME);
1403 return -ENOMEM;
1404 }
1405
1406 tty_drv->driver_name = DRVNAME;
1407 tty_drv->name = TTYNAME;
1408 tty_drv->minor_start = IFX_SPI_TTY_ID;
1409 tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
1410 tty_drv->subtype = SERIAL_TYPE_NORMAL;
1411 tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1412 tty_drv->init_termios = tty_std_termios;
1413
1414 tty_set_operations(tty_drv, &ifx_spi_serial_ops);
1415
1416 result = tty_register_driver(tty_drv);
1417 if (result) {
1418 pr_err("%s: tty_register_driver failed(%d)",
1419 DRVNAME, result);
1420 goto err_free_tty;
1421 }
1422
1423 result = spi_register_driver((void *)&ifx_spi_driver);
1424 if (result) {
1425 pr_err("%s: spi_register_driver failed(%d)",
1426 DRVNAME, result);
1427 goto err_unreg_tty;
1428 }
1429
1430 result = register_reboot_notifier(&ifx_modem_reboot_notifier_block);
1431 if (result) {
1432 pr_err("%s: register ifx modem reboot notifier failed(%d)",
1433 DRVNAME, result);
1434 goto err_unreg_spi;
1435 }
1436
1437 return 0;
1438err_unreg_spi:
1439 spi_unregister_driver((void *)&ifx_spi_driver);
1440err_unreg_tty:
1441 tty_unregister_driver(tty_drv);
1442err_free_tty:
1443 put_tty_driver(tty_drv);
1444
1445 return result;
1446}
1447
1448module_init(ifx_spi_init);
1449module_exit(ifx_spi_exit);
1450
1451MODULE_AUTHOR("Intel");
1452MODULE_DESCRIPTION("IFX6x60 spi driver");
1453MODULE_LICENSE("GPL");
1454MODULE_INFO(Version, "0.1-IFX6x60");
1455