Merge remote-tracking branch 'stable/linux-3.0.y' into develop-3.0
[firefly-linux-kernel-4.4.55.git] / drivers / tty / serial / sc8800.c
1 /*
2  *
3  *  Copyright (C) 2010 liuyixing <lyx@rock-chips.com>
4  *
5  *
6  * Example platform data:
7
8  static struct plat_sc8800 sc8800_plat_data = {
9          .slav_rts_pin = RK29_PIN4_PD0,
10          .slav_rdy_pin = RK29_PIN4_PD0,
11          .master_rts_pin = RK29_PIN4_PD0,
12          .master_rdy_pin = RK29_PIN4_PD0,
13          //.poll_time = 100,
14  };
15
16  static struct spi_board_info spi_board_info[] = {
17          {
18          .modalias      = "sc8800",
19          .platform_data = &sc8800_plat_data,
20          .max_speed_hz  = 12*1000*1000,
21          .chip_select   = 0,
22          },
23  };
24
25  * The initial minor number is 209 in the low-density serial port:
26  * mknod /dev/ttySPI0 c 204 209
27  */
28 #include <linux/delay.h>
29 #include <linux/device.h>
30 #include <linux/serial_core.h>
31 #include <linux/serial.h>
32 #include <linux/spi/spi.h>
33 #include <linux/freezer.h>
34 #include <linux/gpio.h>
35
36 #include "sc8800.h"
37
38 struct sc8800_port {
39         struct uart_port port;
40         struct spi_device *spi;
41
42         int cts;                /* last CTS received for flow ctrl */
43         int tx_empty;           /* last TX empty bit */
44
45         spinlock_t conf_lock;   /* shared data */
46         int conf;               /* configuration for the SC88000
47                                  * (bits 0-7, bits 8-11 are irqs) */
48
49         int rx_enabled;         /* if we should rx chars */
50
51         int irq;                /* irq assigned to the sc8800 */
52
53         int minor;              /* minor number */
54         int loopback;           /* 1 if we are in loopback mode */
55
56         /* for handling irqs: need workqueue since we do spi_sync */
57         struct workqueue_struct *workqueue;
58         struct work_struct work;
59         /* set to 1 to make the workhandler exit as soon as possible */
60         int  force_end_work;
61         /* need to know we are suspending to avoid deadlock on workqueue */
62         int suspending;
63
64         /* hook for suspending SC8800 via dedicated pin */
65         void (*sc8800_hw_suspend) (int suspend);
66
67         /* poll time (in ms) for ctrl lines */
68         int poll_time;
69         /* and its timer */
70         struct timer_list       timer;
71
72         /*signal define, always gpio*/
73         int slav_rts;
74         int slav_rdy;
75         int master_rts;
76         int master_rdy;
77 };
78
79 #define SC8800_MAJOR 204
80 #define SC8800_MINOR 209
81 #define MAX_SC8800 1
82
83 #define SPI_MAX_PACKET (8192-128)
84 #define FREAM_SIZE 64
85 #define SPI_DMA_SIZE PAGE_SIZE
86
87 void * g_dma_buffer = NULL;
88 dma_addr_t g_dma_addr;
89
90 #if 1
91 #define sc8800_dbg(x...) printk(x)
92 #else
93 #define sc8800_dbg(x...)
94 #endif
95
96 static struct sc8800_port *sc8800s[MAX_SC8800]; /* the chips */
97 static DEFINE_MUTEX(sc8800s_lock);                 /* race on probe */
98
99 static int sc8800_get_slave_rts_status(struct sc8800_port *s)
100 {
101         return gpio_get_value(s->slav_rts);
102 }
103
104 static int sc8800_get_slave_rdy_status(struct sc8800_port *s)
105 {
106         return gpio_get_value(s->slav_rdy);
107 }
108
109 static void sc8800_set_master_rts_status(struct sc8800_port *s, int value)
110 {
111         gpio_set_value(s->master_rts, value);
112 }
113
114 static void sc8800_set_master_rdy_status(struct sc8800_port *s, int value)
115 {
116         gpio_set_value(s->master_rdy, value);
117 }
118
119 static int sc8800_send_head_data(struct sc8800_port *s, u32 data_len)
120 {
121         char head[64] = {0};
122         struct spi_message message;
123         int status;
124         struct spi_transfer tran;
125
126         head[0] = 0x7f;
127         head[1] = 0x7e;
128         head[2] = 0x55;
129         head[3] = 0xaa;
130         head[4] = data_len & 0xff;
131         head[5] = (data_len>>8) & 0xff;
132         head[6] = (data_len>>16) & 0xff;
133         head[7] = (data_len>>24) & 0xff;
134         
135         tran.tx_buf = (void *)(head);
136         tran.len = FREAM_SIZE;
137         
138         spi_message_init(&message);
139         spi_message_add_tail(&tran, &message);
140         status = spi_sync(s->spi, &message);
141         if (status) {
142                 dev_warn(&s->spi->dev, "error while calling spi_sync\n");
143                 return -EIO;
144         }
145         
146         return 0;
147 }
148 static int sc8800_recv_and_parse_head_data(struct sc8800_port *s, u32 *len)
149 {
150         struct spi_message message;
151         int status;
152         struct spi_transfer tran;
153         char buf[64] = {0};
154         u32 data_len = 0;
155                 
156         tran.rx_buf = (void *)buf;
157         tran.len = FREAM_SIZE;
158         
159         spi_message_init(&message);
160         spi_message_add_tail(&tran, &message);
161         status = spi_sync(s->spi, &message);
162         if (status) {
163                 dev_warn(&s->spi->dev, "error while calling spi_sync\n");
164                 return -EIO;
165         }
166
167         if ((buf[0]!=0x7f) || (buf[1]!=0x7e) || (buf[2]!=0x55) || (buf[3]!=0xaa)) {
168                 dev_warn(&s->spi->dev, "line %d, error head data", __LINE__);
169                 return -EIO;
170         }
171         
172         data_len = buf[5] + (buf[6]<<8) + (buf[7]<<16) + (buf[8]<<24);
173
174         *len = data_len;
175         
176         sc8800_dbg("line %d, %d data need to read\n", __LINE__, *len);
177         
178         return 0;
179 }
180
181 static int sc8800_send_data(struct sc8800_port *s, char *buf, int len)
182 {
183 #if 1
184         int status;
185         struct spi_message message;
186         struct spi_transfer tran;
187         
188         tran.tx_buf = (void *)buf;
189         tran.len = len;
190         
191         spi_message_init(&message);
192         spi_message_add_tail(&tran, &message);
193         status = spi_sync(s->spi, &message);
194         if (status) {
195                 dev_warn(&s->spi->dev, "error while calling spi_sync\n");
196                 return -EIO;
197         }
198 #else
199         int status;
200         struct spi_message message;
201         struct spi_transfer tran;
202
203         spi_message_init(&message);
204         
205         if (!g_dma_buffer) {
206                 g_dma_buffer = dma_alloc_coherent(NULL, SPI_DMA_SIZE, &g_dma_addr, GFP_KERNEL | GFP_DMA);
207                 if (!g_dma_buffer) {
208                         dev_err(&s->spi->dev, "alloc dma memory fail\n");
209                 }
210         }
211
212         if (!g_dma_buffer) {    //nomal
213                 sc8800_dbg("send data nomal");
214                 tran.tx_buf = (void *)buf;
215                 tran.len = len;
216         }
217         else {  //dma   
218                 //message.is_dma_mapped = 1;
219                 //memcpy(g_dma_buffer, buf, );
220         }
221
222         spi_message_add_tail(&tran, &message);
223         status = spi_sync(s->spi, &message);
224         if (status) {
225                 dev_warn(&s->spi->dev, "error while calling spi_sync\n");
226                 return -EIO;
227         }
228 #endif
229
230         return 0;
231 }
232
233 static int sc8800_recv_data(struct sc8800_port *s, char *buf, int len)
234 {
235         struct spi_message message;
236         int status;
237         struct spi_transfer tran;
238         
239         tran.rx_buf = (void *)buf;
240         tran.len = len;
241         
242         spi_message_init(&message);
243         spi_message_add_tail(&tran, &message);
244         status = spi_sync(s->spi, &message);
245         if (status) {
246                 dev_warn(&s->spi->dev, "error while calling spi_sync\n");
247                 return -EIO;
248         }
249         
250         return 0;
251 }
252
253 static void sc8800_work(struct work_struct *w);
254
255 static void sc8800_dowork(struct sc8800_port *s)
256 {
257         if (!s->force_end_work && !work_pending(&s->work) &&
258             !freezing(current) && !s->suspending)
259                 queue_work(s->workqueue, &s->work);
260 }
261
262 static void sc8800_timeout(unsigned long data)
263 {
264         struct sc8800_port *s = (struct sc8800_port *)data;
265
266         if (s->port.state) {
267                 sc8800_dowork(s);
268                 mod_timer(&s->timer, jiffies + s->poll_time);
269         }
270 }
271
272 static void sc8800_work(struct work_struct *w)
273 {
274         struct sc8800_port *s = container_of(w, struct sc8800_port, work);
275         struct circ_buf *xmit = &s->port.state->xmit;
276         u32 len,i;
277         char *buf = NULL;
278         unsigned char ch;
279         
280         dev_dbg(&s->spi->dev, "%s\n", __func__);
281
282         if (sc8800_get_slave_rts_status(s) == GPIO_HIGH) {      //do wirte, master--->slave
283                 if (sc8800_get_slave_rdy_status(s) == GPIO_HIGH) {      /*1.check slave rdy, must be high*/
284                         if (!(uart_circ_empty(xmit))) {
285                                 len = uart_circ_chars_pending(xmit);
286                                 len = (len > SPI_MAX_PACKET) ? SPI_MAX_PACKET : len;
287                                 sc8800_dbg("send data length = %d\n", len);
288                                 
289                                 sc8800_set_master_rts_status(s, GPIO_LOW);      /*2.set master rts low*/
290                                 sc8800_send_head_data(s,len);   /*3.send 64byte head data*/
291                                 while (sc8800_get_slave_rdy_status(s)) {        /*4.check slav rdy, wait for low */
292                                         msleep(1);
293                                 }
294                                 
295                                 /*5.long data transmit*/
296                                 sc8800_send_data(s, xmit->buf+xmit->tail, len);
297                                 
298                                 while(sc8800_get_slave_rdy_status(s)==GPIO_LOW) {       /*6.wait for slave rdy high*/
299                                         msleep(1);
300                                 }
301
302                                 sc8800_set_master_rts_status(s, GPIO_HIGH);     /*end transmit, set master rts high*/
303                                 xmit->tail = (xmit->tail + len) & (UART_XMIT_SIZE - 1);
304                                 s->port.icount.tx += len;
305                         }
306                 }
307                 else {  //slave not ready, do it next time
308                         queue_work(s->workqueue, &s->work);
309                 }                       
310         }
311         else {  //do read, slave--->master
312                 sc8800_set_master_rdy_status(s, GPIO_LOW);
313                 sc8800_recv_and_parse_head_data(s, &len);
314
315                 buf = (char *)kzalloc(len, GFP_KERNEL);
316                 if (!buf) {
317                         dev_err(&s->spi->dev, "line %d, err while malloc mem\n", __LINE__);
318                         sc8800_set_master_rdy_status(s, GPIO_HIGH);
319                         return ;
320                 }
321                 memset(buf, 0, len);
322                 sc8800_recv_data(s, buf, len);
323                 
324                 while (sc8800_get_slave_rts_status(s) == GPIO_LOW) {
325                         msleep(1);
326                 }
327                 sc8800_set_master_rdy_status(s, GPIO_HIGH);
328
329                 for (i=0; i<len; i++) {
330                         ch = buf[i];            
331                         uart_insert_char(&s->port, 0, 0, ch, TTY_NORMAL);
332                 }
333                 tty_flip_buffer_push(s->port.state->port.tty);
334         }
335 }
336
337 static irqreturn_t sc8800_irq(int irqno, void *dev_id)
338 {
339         struct sc8800_port *s = dev_id;
340
341         dev_dbg(&s->spi->dev, "%s\n", __func__);
342
343         sc8800_dowork(s);
344         return IRQ_HANDLED;
345 }
346
347 static void sc8800_enable_ms(struct uart_port *port)
348 {
349         struct sc8800_port *s = container_of(port,
350                                               struct sc8800_port,
351                                               port);
352
353         if (s->poll_time > 0)
354                 mod_timer(&s->timer, jiffies);
355         dev_dbg(&s->spi->dev, "%s\n", __func__);
356 }
357
358 static void sc8800_start_tx(struct uart_port *port)
359 {
360         struct sc8800_port *s = container_of(port,
361                                               struct sc8800_port,
362                                               port);
363
364         dev_dbg(&s->spi->dev, "%s\n", __func__);
365         
366         sc8800_dowork(s);
367 }
368
369 static void sc8800_stop_rx(struct uart_port *port)
370 {
371         struct sc8800_port *s = container_of(port,
372                                               struct sc8800_port,
373                                               port);
374
375         dev_dbg(&s->spi->dev, "%s\n", __func__);
376
377         s->rx_enabled = 0;
378
379         sc8800_dowork(s);
380 }
381
382 static void sc8800_shutdown(struct uart_port *port)
383 {
384         struct sc8800_port *s = container_of(port,
385                                               struct sc8800_port,
386                                               port);
387
388         dev_dbg(&s->spi->dev, "%s\n", __func__);
389
390         if (s->suspending)
391                 return;
392
393         s->force_end_work = 1;
394
395         if (s->poll_time > 0)
396                 del_timer_sync(&s->timer);
397
398         if (s->workqueue) {
399                 flush_workqueue(s->workqueue);
400                 destroy_workqueue(s->workqueue);
401                 s->workqueue = NULL;
402         }
403         if (s->irq)
404                 free_irq(s->irq, s);
405
406         gpio_free(s->master_rdy);
407         gpio_free(s->master_rts);
408         gpio_free(s->slav_rdy);
409         gpio_free(s->slav_rts);
410
411         /* set shutdown mode to save power */
412         if (s->sc8800_hw_suspend)
413                 s->sc8800_hw_suspend(1);
414 }
415
416 static int sc8800_startup(struct uart_port *port)
417 {
418         struct sc8800_port *s = container_of(port,
419                                               struct sc8800_port,
420                                               port);
421         int ret;
422         char b[12];
423
424         dev_dbg(&s->spi->dev, "%s\n", __func__);
425
426         s->rx_enabled = 1;
427
428         if (s->suspending)
429                 return 0;
430         
431         s->force_end_work = 0;
432         
433         sprintf(b, "sc8800-%d", s->minor);
434         s->workqueue = create_freezeable_workqueue(b);
435         if (!s->workqueue) {
436                 dev_warn(&s->spi->dev, "cannot create workqueue\n");
437                 return -EBUSY;
438         }
439         INIT_WORK(&s->work, sc8800_work);
440
441         ret = gpio_request(s->slav_rts, "slav rts");
442         if (ret) {
443                 dev_err(&s->spi->dev, "line %d: gpio request err\n", __LINE__);
444                 ret = -EBUSY;
445                 goto gpio_err1;
446         }
447         ret = gpio_request(s->slav_rdy, "slav rdy");
448         if (ret) {
449                 dev_err(&s->spi->dev, "line %d: gpio request err\n", __LINE__);
450                 ret = -EBUSY;
451                 goto gpio_err2;
452         }
453         ret = gpio_request(s->master_rts, "master rts");
454         if (ret) {
455                 dev_err(&s->spi->dev, "line %d: gpio request err\n", __LINE__);
456                 ret = -EBUSY;
457                 goto gpio_err3;
458         }
459         ret = gpio_request(s->master_rdy, "master rdy");
460         if (ret) {
461                 dev_err(&s->spi->dev, "line %d: gpio request err\n", __LINE__);
462                 ret = -EBUSY;
463                 goto gpio_err4;
464         }
465
466         gpio_direction_input(s->slav_rts);
467         gpio_pull_updown(s->slav_rts, GPIOPullUp);
468         gpio_direction_input(s->slav_rdy);
469         gpio_pull_updown(s->slav_rdy, GPIOPullUp);
470         gpio_direction_output(s->master_rts, GPIO_HIGH);
471         gpio_direction_output(s->master_rdy, GPIO_HIGH);
472         
473         if (request_irq(s->irq, sc8800_irq,
474                         IRQF_TRIGGER_FALLING, "sc8800", s) < 0) {
475                 dev_warn(&s->spi->dev, "cannot allocate irq %d\n", s->irq);
476                 s->irq = 0;
477                 goto irq_err;
478         }
479
480         if (s->sc8800_hw_suspend)
481                 s->sc8800_hw_suspend(0);
482         
483         sc8800_dowork(s);
484
485         sc8800_enable_ms(&s->port);
486
487         return 0;
488         
489 irq_err:
490         gpio_free(s->master_rdy);
491 gpio_err4:
492         gpio_free(s->master_rts);
493 gpio_err3:
494         gpio_free(s->slav_rdy);
495 gpio_err2:
496         gpio_free(s->slav_rts);
497 gpio_err1:
498         destroy_workqueue(s->workqueue);
499         s->workqueue = NULL;    
500         return ret;
501
502 }
503
504 static struct uart_ops sc8800_ops = {
505         .start_tx       = sc8800_start_tx,
506         .stop_rx        = sc8800_stop_rx,
507         .startup        = sc8800_startup,
508         .shutdown       = sc8800_shutdown,
509 };
510
511 static struct uart_driver sc8800_uart_driver = {
512         .owner          = THIS_MODULE,
513         .driver_name    = "ttySPI",
514         .dev_name       = "ttySPI",
515         .major          = SC8800_MAJOR,
516         .minor          = SC8800_MINOR,
517         .nr             = MAX_SC8800,
518 };
519 static int uart_driver_registered;
520
521 static int __devinit sc8800_probe(struct spi_device *spi)
522 {
523         int i, retval;
524         struct plat_sc8800 *pdata;
525         
526         mutex_lock(&sc8800s_lock);
527
528         if (!uart_driver_registered) {
529                 uart_driver_registered = 1;
530                 retval = uart_register_driver(&sc8800_uart_driver);
531                 if (retval) {
532                         printk(KERN_ERR "Couldn't register sc8800 uart driver\n");
533                         mutex_unlock(&sc8800s_lock);
534                         return retval;
535                 }
536         }
537
538         for (i = 0; i < MAX_SC8800; i++)
539                 if (!sc8800s[i])
540                         break;
541         if (i == MAX_SC8800) {
542                 dev_warn(&spi->dev, "too many SC8800 chips\n");
543                 mutex_unlock(&sc8800s_lock);
544                 return -ENOMEM;
545         }
546
547         sc8800s[i] = kzalloc(sizeof(struct sc8800_port), GFP_KERNEL);
548         if (!sc8800s[i]) {
549                 dev_warn(&spi->dev,
550                          "kmalloc for sc8800 structure %d failed!\n", i);
551                 mutex_unlock(&sc8800s_lock);
552                 return -ENOMEM;
553         }
554         sc8800s[i]->spi = spi;
555         spin_lock_init(&sc8800s[i]->conf_lock);
556         dev_set_drvdata(&spi->dev, sc8800s[i]);
557         pdata = spi->dev.platform_data;
558         sc8800s[i]->irq = gpio_to_irq(pdata->slav_rts_pin);
559         sc8800s[i]->slav_rts = pdata->slav_rts_pin;
560         sc8800s[i]->slav_rdy = pdata->slav_rdy_pin;
561         sc8800s[i]->master_rts = pdata->master_rts_pin;
562         sc8800s[i]->master_rdy = pdata->master_rdy_pin;
563         //sc8800s[i]->sc8800_hw_suspend = pdata->sc8800_hw_suspend;
564         sc8800s[i]->minor = i;
565         init_timer(&sc8800s[i]->timer);
566         sc8800s[i]->timer.function = sc8800_timeout;
567         sc8800s[i]->timer.data = (unsigned long) sc8800s[i];
568
569         dev_dbg(&spi->dev, "%s: adding port %d\n", __func__, i);
570         sc8800s[i]->port.irq = sc8800s[i]->irq;
571         sc8800s[i]->port.uartclk = 24000000;
572         sc8800s[i]->port.fifosize = 64;
573         sc8800s[i]->port.ops = &sc8800_ops;
574         sc8800s[i]->port.flags = UPF_SKIP_TEST | UPF_BOOT_AUTOCONF;
575         sc8800s[i]->port.line = i;
576         sc8800s[i]->port.dev = &spi->dev;
577         retval = uart_add_one_port(&sc8800_uart_driver, &sc8800s[i]->port);
578         if (retval < 0)
579                 dev_warn(&spi->dev,
580                          "uart_add_one_port failed for line %d with error %d\n",
581                          i, retval);
582
583         /* set shutdown mode to save power. Will be woken-up on open */
584         if (sc8800s[i]->sc8800_hw_suspend)
585                 sc8800s[i]->sc8800_hw_suspend(1);
586
587         mutex_unlock(&sc8800s_lock);
588         return 0;
589 }
590
591 static int __devexit sc8800_remove(struct spi_device *spi)
592 {
593         struct sc8800_port *s = dev_get_drvdata(&spi->dev);
594         int i;
595
596         mutex_lock(&sc8800s_lock);
597
598         /* find out the index for the chip we are removing */
599         for (i = 0; i < MAX_SC8800; i++)
600                 if (sc8800s[i] == s)
601                         break;
602
603         dev_dbg(&spi->dev, "%s: removing port %d\n", __func__, i);
604         uart_remove_one_port(&sc8800_uart_driver, &sc8800s[i]->port);
605         kfree(sc8800s[i]);
606         sc8800s[i] = NULL;
607
608         /* check if this is the last chip we have */
609         for (i = 0; i < MAX_SC8800; i++)
610                 if (sc8800s[i]) {
611                         mutex_unlock(&sc8800s_lock);
612                         return 0;
613                 }
614         pr_debug("removing sc8800 driver\n");
615         uart_unregister_driver(&sc8800_uart_driver);
616
617         mutex_unlock(&sc8800s_lock);
618         return 0;
619 }
620
621 #ifdef CONFIG_PM
622
623 static int sc8800_suspend(struct spi_device *spi, pm_message_t state)
624 {
625         struct sc8800_port *s = dev_get_drvdata(&spi->dev);
626
627         dev_dbg(&s->spi->dev, "%s\n", __func__);
628
629         disable_irq(s->irq);
630
631         s->suspending = 1;
632         uart_suspend_port(&sc8800_uart_driver, &s->port);
633
634         if (s->sc8800_hw_suspend)
635                 s->sc8800_hw_suspend(1);
636
637         return 0;
638 }
639
640 static int sc8800_resume(struct spi_device *spi)
641 {
642         struct sc8800_port *s = dev_get_drvdata(&spi->dev);
643
644         dev_dbg(&s->spi->dev, "%s\n", __func__);
645
646         if (s->sc8800_hw_suspend)
647                 s->sc8800_hw_suspend(0);
648         uart_resume_port(&sc8800_uart_driver, &s->port);
649         s->suspending = 0;
650
651         enable_irq(s->irq);
652
653         if (s->workqueue)
654                 sc8800_dowork(s);
655
656         return 0;
657 }
658
659 #else
660 #define sc8800_suspend NULL
661 #define sc8800_resume  NULL
662 #endif
663
664 static struct spi_driver sc8800_driver = {
665         .driver = {
666                 .name           = "sc8800",
667                 .bus            = &spi_bus_type,
668                 .owner          = THIS_MODULE,
669         },
670
671         .probe          = sc8800_probe,
672         .remove         = __devexit_p(sc8800_remove),
673         .suspend        = sc8800_suspend,
674         .resume         = sc8800_resume,
675 };
676
677 static int __init sc8800_init(void)
678 {
679         return spi_register_driver(&sc8800_driver);
680 }
681 module_init(sc8800_init);
682
683 static void __exit sc8800_exit(void)
684 {
685         spi_unregister_driver(&sc8800_driver);
686 }
687 module_exit(sc8800_exit);
688
689 MODULE_DESCRIPTION("SC8800 driver");
690 MODULE_AUTHOR("liuyixing <lyx@rock-chips.com>");
691 MODULE_LICENSE("GPL");
692 MODULE_ALIAS("spi:SC8800");