3 * Copyright (C) 2010 liuyixing <lyx@rock-chips.com>
6 * Example platform data:
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,
16 static struct spi_board_info spi_board_info[] = {
19 .platform_data = &sc8800_plat_data,
20 .max_speed_hz = 12*1000*1000,
25 * The initial minor number is 209 in the low-density serial port:
26 * mknod /dev/ttySPI0 c 204 209
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>
39 struct uart_port port;
40 struct spi_device *spi;
42 int cts; /* last CTS received for flow ctrl */
43 int tx_empty; /* last TX empty bit */
45 spinlock_t conf_lock; /* shared data */
46 int conf; /* configuration for the SC88000
47 * (bits 0-7, bits 8-11 are irqs) */
49 int rx_enabled; /* if we should rx chars */
51 int irq; /* irq assigned to the sc8800 */
53 int minor; /* minor number */
54 int loopback; /* 1 if we are in loopback mode */
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 */
61 /* need to know we are suspending to avoid deadlock on workqueue */
64 /* hook for suspending SC8800 via dedicated pin */
65 void (*sc8800_hw_suspend) (int suspend);
67 /* poll time (in ms) for ctrl lines */
70 struct timer_list timer;
72 /*signal define, always gpio*/
79 #define SC8800_MAJOR 204
80 #define SC8800_MINOR 209
83 #define SPI_MAX_PACKET (8192-128)
85 #define SPI_DMA_SIZE PAGE_SIZE
87 void * g_dma_buffer = NULL;
88 dma_addr_t g_dma_addr;
91 #define sc8800_dbg(x...) printk(x)
93 #define sc8800_dbg(x...)
96 static struct sc8800_port *sc8800s[MAX_SC8800]; /* the chips */
97 static DEFINE_MUTEX(sc8800s_lock); /* race on probe */
99 static int sc8800_get_slave_rts_status(struct sc8800_port *s)
101 return gpio_get_value(s->slav_rts);
104 static int sc8800_get_slave_rdy_status(struct sc8800_port *s)
106 return gpio_get_value(s->slav_rdy);
109 static void sc8800_set_master_rts_status(struct sc8800_port *s, int value)
111 gpio_set_value(s->master_rts, value);
114 static void sc8800_set_master_rdy_status(struct sc8800_port *s, int value)
116 gpio_set_value(s->master_rdy, value);
119 static int sc8800_send_head_data(struct sc8800_port *s, u32 data_len)
122 struct spi_message message;
124 struct spi_transfer tran;
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;
135 tran.tx_buf = (void *)(head);
136 tran.len = FREAM_SIZE;
138 spi_message_init(&message);
139 spi_message_add_tail(&tran, &message);
140 status = spi_sync(s->spi, &message);
142 dev_warn(&s->spi->dev, "error while calling spi_sync\n");
148 static int sc8800_recv_and_parse_head_data(struct sc8800_port *s, u32 *len)
150 struct spi_message message;
152 struct spi_transfer tran;
156 tran.rx_buf = (void *)buf;
157 tran.len = FREAM_SIZE;
159 spi_message_init(&message);
160 spi_message_add_tail(&tran, &message);
161 status = spi_sync(s->spi, &message);
163 dev_warn(&s->spi->dev, "error while calling spi_sync\n");
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__);
172 data_len = buf[5] + (buf[6]<<8) + (buf[7]<<16) + (buf[8]<<24);
176 sc8800_dbg("line %d, %d data need to read\n", __LINE__, *len);
181 static int sc8800_send_data(struct sc8800_port *s, char *buf, int len)
185 struct spi_message message;
186 struct spi_transfer tran;
188 tran.tx_buf = (void *)buf;
191 spi_message_init(&message);
192 spi_message_add_tail(&tran, &message);
193 status = spi_sync(s->spi, &message);
195 dev_warn(&s->spi->dev, "error while calling spi_sync\n");
200 struct spi_message message;
201 struct spi_transfer tran;
203 spi_message_init(&message);
206 g_dma_buffer = dma_alloc_coherent(NULL, SPI_DMA_SIZE, &g_dma_addr, GFP_KERNEL | GFP_DMA);
208 dev_err(&s->spi->dev, "alloc dma memory fail\n");
212 if (!g_dma_buffer) { //nomal
213 sc8800_dbg("send data nomal");
214 tran.tx_buf = (void *)buf;
218 //message.is_dma_mapped = 1;
219 //memcpy(g_dma_buffer, buf, );
222 spi_message_add_tail(&tran, &message);
223 status = spi_sync(s->spi, &message);
225 dev_warn(&s->spi->dev, "error while calling spi_sync\n");
233 static int sc8800_recv_data(struct sc8800_port *s, char *buf, int len)
235 struct spi_message message;
237 struct spi_transfer tran;
239 tran.rx_buf = (void *)buf;
242 spi_message_init(&message);
243 spi_message_add_tail(&tran, &message);
244 status = spi_sync(s->spi, &message);
246 dev_warn(&s->spi->dev, "error while calling spi_sync\n");
253 static void sc8800_work(struct work_struct *w);
255 static void sc8800_dowork(struct sc8800_port *s)
257 if (!s->force_end_work && !work_pending(&s->work) &&
258 !freezing(current) && !s->suspending)
259 queue_work(s->workqueue, &s->work);
262 static void sc8800_timeout(unsigned long data)
264 struct sc8800_port *s = (struct sc8800_port *)data;
268 mod_timer(&s->timer, jiffies + s->poll_time);
272 static void sc8800_work(struct work_struct *w)
274 struct sc8800_port *s = container_of(w, struct sc8800_port, work);
275 struct circ_buf *xmit = &s->port.state->xmit;
280 dev_dbg(&s->spi->dev, "%s\n", __func__);
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);
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 */
295 /*5.long data transmit*/
296 sc8800_send_data(s, xmit->buf+xmit->tail, len);
298 while(sc8800_get_slave_rdy_status(s)==GPIO_LOW) { /*6.wait for slave rdy high*/
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;
307 else { //slave not ready, do it next time
308 queue_work(s->workqueue, &s->work);
311 else { //do read, slave--->master
312 sc8800_set_master_rdy_status(s, GPIO_LOW);
313 sc8800_recv_and_parse_head_data(s, &len);
315 buf = (char *)kzalloc(len, GFP_KERNEL);
317 dev_err(&s->spi->dev, "line %d, err while malloc mem\n", __LINE__);
318 sc8800_set_master_rdy_status(s, GPIO_HIGH);
322 sc8800_recv_data(s, buf, len);
324 while (sc8800_get_slave_rts_status(s) == GPIO_LOW) {
327 sc8800_set_master_rdy_status(s, GPIO_HIGH);
329 for (i=0; i<len; i++) {
331 uart_insert_char(&s->port, 0, 0, ch, TTY_NORMAL);
333 tty_flip_buffer_push(s->port.state->port.tty);
337 static irqreturn_t sc8800_irq(int irqno, void *dev_id)
339 struct sc8800_port *s = dev_id;
341 dev_dbg(&s->spi->dev, "%s\n", __func__);
347 static void sc8800_enable_ms(struct uart_port *port)
349 struct sc8800_port *s = container_of(port,
353 if (s->poll_time > 0)
354 mod_timer(&s->timer, jiffies);
355 dev_dbg(&s->spi->dev, "%s\n", __func__);
358 static void sc8800_start_tx(struct uart_port *port)
360 struct sc8800_port *s = container_of(port,
364 dev_dbg(&s->spi->dev, "%s\n", __func__);
369 static void sc8800_stop_rx(struct uart_port *port)
371 struct sc8800_port *s = container_of(port,
375 dev_dbg(&s->spi->dev, "%s\n", __func__);
382 static void sc8800_shutdown(struct uart_port *port)
384 struct sc8800_port *s = container_of(port,
388 dev_dbg(&s->spi->dev, "%s\n", __func__);
393 s->force_end_work = 1;
395 if (s->poll_time > 0)
396 del_timer_sync(&s->timer);
399 flush_workqueue(s->workqueue);
400 destroy_workqueue(s->workqueue);
406 gpio_free(s->master_rdy);
407 gpio_free(s->master_rts);
408 gpio_free(s->slav_rdy);
409 gpio_free(s->slav_rts);
411 /* set shutdown mode to save power */
412 if (s->sc8800_hw_suspend)
413 s->sc8800_hw_suspend(1);
416 static int sc8800_startup(struct uart_port *port)
418 struct sc8800_port *s = container_of(port,
424 dev_dbg(&s->spi->dev, "%s\n", __func__);
431 s->force_end_work = 0;
433 sprintf(b, "sc8800-%d", s->minor);
434 s->workqueue = create_freezeable_workqueue(b);
436 dev_warn(&s->spi->dev, "cannot create workqueue\n");
439 INIT_WORK(&s->work, sc8800_work);
441 ret = gpio_request(s->slav_rts, "slav rts");
443 dev_err(&s->spi->dev, "line %d: gpio request err\n", __LINE__);
447 ret = gpio_request(s->slav_rdy, "slav rdy");
449 dev_err(&s->spi->dev, "line %d: gpio request err\n", __LINE__);
453 ret = gpio_request(s->master_rts, "master rts");
455 dev_err(&s->spi->dev, "line %d: gpio request err\n", __LINE__);
459 ret = gpio_request(s->master_rdy, "master rdy");
461 dev_err(&s->spi->dev, "line %d: gpio request err\n", __LINE__);
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);
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);
480 if (s->sc8800_hw_suspend)
481 s->sc8800_hw_suspend(0);
485 sc8800_enable_ms(&s->port);
490 gpio_free(s->master_rdy);
492 gpio_free(s->master_rts);
494 gpio_free(s->slav_rdy);
496 gpio_free(s->slav_rts);
498 destroy_workqueue(s->workqueue);
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,
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,
519 static int uart_driver_registered;
521 static int __devinit sc8800_probe(struct spi_device *spi)
524 struct plat_sc8800 *pdata;
526 mutex_lock(&sc8800s_lock);
528 if (!uart_driver_registered) {
529 uart_driver_registered = 1;
530 retval = uart_register_driver(&sc8800_uart_driver);
532 printk(KERN_ERR "Couldn't register sc8800 uart driver\n");
533 mutex_unlock(&sc8800s_lock);
538 for (i = 0; i < MAX_SC8800; i++)
541 if (i == MAX_SC8800) {
542 dev_warn(&spi->dev, "too many SC8800 chips\n");
543 mutex_unlock(&sc8800s_lock);
547 sc8800s[i] = kzalloc(sizeof(struct sc8800_port), GFP_KERNEL);
550 "kmalloc for sc8800 structure %d failed!\n", i);
551 mutex_unlock(&sc8800s_lock);
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];
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);
580 "uart_add_one_port failed for line %d with error %d\n",
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);
587 mutex_unlock(&sc8800s_lock);
591 static int __devexit sc8800_remove(struct spi_device *spi)
593 struct sc8800_port *s = dev_get_drvdata(&spi->dev);
596 mutex_lock(&sc8800s_lock);
598 /* find out the index for the chip we are removing */
599 for (i = 0; i < MAX_SC8800; i++)
603 dev_dbg(&spi->dev, "%s: removing port %d\n", __func__, i);
604 uart_remove_one_port(&sc8800_uart_driver, &sc8800s[i]->port);
608 /* check if this is the last chip we have */
609 for (i = 0; i < MAX_SC8800; i++)
611 mutex_unlock(&sc8800s_lock);
614 pr_debug("removing sc8800 driver\n");
615 uart_unregister_driver(&sc8800_uart_driver);
617 mutex_unlock(&sc8800s_lock);
623 static int sc8800_suspend(struct spi_device *spi, pm_message_t state)
625 struct sc8800_port *s = dev_get_drvdata(&spi->dev);
627 dev_dbg(&s->spi->dev, "%s\n", __func__);
632 uart_suspend_port(&sc8800_uart_driver, &s->port);
634 if (s->sc8800_hw_suspend)
635 s->sc8800_hw_suspend(1);
640 static int sc8800_resume(struct spi_device *spi)
642 struct sc8800_port *s = dev_get_drvdata(&spi->dev);
644 dev_dbg(&s->spi->dev, "%s\n", __func__);
646 if (s->sc8800_hw_suspend)
647 s->sc8800_hw_suspend(0);
648 uart_resume_port(&sc8800_uart_driver, &s->port);
660 #define sc8800_suspend NULL
661 #define sc8800_resume NULL
664 static struct spi_driver sc8800_driver = {
667 .bus = &spi_bus_type,
668 .owner = THIS_MODULE,
671 .probe = sc8800_probe,
672 .remove = __devexit_p(sc8800_remove),
673 .suspend = sc8800_suspend,
674 .resume = sc8800_resume,
677 static int __init sc8800_init(void)
679 return spi_register_driver(&sc8800_driver);
681 module_init(sc8800_init);
683 static void __exit sc8800_exit(void)
685 spi_unregister_driver(&sc8800_driver);
687 module_exit(sc8800_exit);
689 MODULE_DESCRIPTION("SC8800 driver");
690 MODULE_AUTHOR("liuyixing <lyx@rock-chips.com>");
691 MODULE_LICENSE("GPL");
692 MODULE_ALIAS("spi:SC8800");