5 GPIO144 DRDY signal from PIC24@CPU2010
6 SPI2 AD data receive and CMD send for PIC24@CPU2010
8 Copyright Naoya Takamura, 2011
9 This program based on spike.c
10 ---------------------------------------------
13 Copyright Scott Ellis, 2010
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation; either version 2 of the License, or
18 (at your option) any later version.
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the Free Software
27 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 #include <linux/init.h>
31 #include <linux/module.h>
33 #include <linux/device.h>
34 #include <linux/mutex.h>
35 #include <linux/slab.h>
36 #include <linux/cdev.h>
37 #include <linux/spi/spi.h>
38 #include <linux/string.h>
39 #include <asm/uaccess.h>
40 #include <linux/dma-mapping.h>
42 #include <linux/gpio.h>
43 #include <linux/interrupt.h>
44 #include <linux/poll.h>
45 #include <linux/wait.h>
46 #include <linux/sched.h>
50 #define SPI_BUFF_SIZE 2048 // for spike_ctl
51 #define USER_BUFF_SIZE 128
55 #define SPI_BUS_SPEED 500000 // Hz
57 #define SPI_DATA_SIZE 965 // $含む PICから受信するデータ長
59 #define GPIO_DRDY_IN 144 // DRDY Input = GPIO144
61 //#define DEBUG_TOGGLE_OUT // デバッグ時定義する
62 #ifdef DEBUG_TOGGLE_OUT
63 #define GPIO_TOGGLE_OUT 145 // Debug用toggle出力 = GPIO145
68 #define MODULE_NAME "spike-ad"
70 const char this_driver_name[] = MODULE_NAME;
72 struct spike_control {
73 struct spi_message msg;
74 struct spi_transfer transfer;
75 u32 busy; // 1=spi_async()終了待ち
83 static struct spike_control spike_ctl;
90 static struct irq_device irq_dev;
95 struct semaphore fop_sem;
99 struct spi_device *spi_device;
104 static struct spike_dev spike_dev;
108 unsigned long f_version; // 識別用 f_version
109 wait_queue_head_t wait; /* read and write queues */
110 int sleep_mode; // 0: 起きてる 1: 待ち
111 int intflag; // 割り込みフラグ 1=割り込み入った
114 static FileInfo finfo;
116 void spike_tasklet_func(unsigned long data);
117 DECLARE_TASKLET(spike_tasklet, spike_tasklet_func, 0);
120 /**** SPI送信データ バッファ ******************************************
122 #define SPI_TX_MAX (64) // SPI送信データの最大長
124 // 送信データバッファ(CMD for PIC)
126 unsigned char txbuf[SPI_TX_MAX];
127 int tx_len; // SPIで送信するデータ長
128 struct semaphore txbuf_sem;
131 static SpiTxBuf spi_txbuf;
136 /**** SPI受信データ リングバッファ ******************************************
138 #define RING_NUM (30) // リングバッファ個数
139 #define SPI_MAX (SPI_DATA_SIZE+256) // 1回のデータバイト数max
140 static unsigned char spibuf[RING_NUM][SPI_MAX];
148 static int ring_read;
150 static int ring_write;
155 ring_write = ring_read;
156 memset(spibuf, 0, sizeof(spibuf));
158 #define ring_clear() ring_read_set(ring_write_get())
161 int ring_read_get(void)
165 void ring_read_set(int i)
169 void ring_read_plus(void)
172 if (ring_read >= RING_NUM) ring_read = 0;
176 int ring_write_get(void)
180 void ring_write_plus(void)
183 if (ring_write >= RING_NUM) ring_write = 0;
186 int ring_num_get(void)
190 i = ring_write - ring_read;
191 if (i < 0) i += RING_NUM;
196 unsigned char* ring_get(int ptr)
205 memset(spibuf[ring_write], 0, SPI_MAX);
214 if (ring_num_get() >= RING_NUM-1) return 1;
220 spike_queue_spi_write()で開始した
224 static void spike_spi_completion_handler(void *arg)
228 spike_ctl.spi_callbacks++;
231 // 受信したデータをリングバッファに保存
232 p = ring_get(ring_write_get());
233 memcpy(p, spike_ctl.rx_buff, SPI_DATA_SIZE);
235 spike_ctl.received_len = spike_ctl.msg.actual_length;
241 if (finfo.sleep_mode) {
243 wake_up_interruptible(&(finfo.wait)); // 起こす
244 //printk(KERN_INFO "intsel_interrupt: wakeup %ld\n", fi[i].f_version);
245 finfo.sleep_mode = 0;
251 受信データはcallback funcで受ける
253 static int spike_queue_spi_write(void)
258 // struct spi_messageを初期化 ゼロクリア
259 spi_message_init(&spike_ctl.msg);
262 spike_ctl.msg.complete = spike_spi_completion_handler;
263 spike_ctl.msg.context = NULL;
265 memset(spike_ctl.tx_buff, 0, SPI_BUFF_SIZE);
266 // spike_ctl.tx_buff[0] = '$';
267 memset(spike_ctl.rx_buff, 0, SPI_BUFF_SIZE);
269 if (down_interruptible(&spi_txbuf.txbuf_sem))
272 memcpy(spike_ctl.tx_buff, spi_txbuf.txbuf, spi_txbuf.tx_len);
274 memset(spi_txbuf.txbuf, 0, SPI_TX_MAX);
275 up(&spi_txbuf.txbuf_sem);
277 spike_ctl.transfer.tx_buf = spike_ctl.tx_buff;
278 spike_ctl.transfer.rx_buf = spike_ctl.rx_buff;
279 spike_ctl.transfer.len = SPI_DATA_SIZE;
281 spi_message_add_tail(&spike_ctl.transfer, &spike_ctl.msg);
283 spin_lock_irqsave(&spike_dev.spi_lock, flags);
285 if (spike_dev.spi_device)
286 status = spi_async(spike_dev.spi_device, &spike_ctl.msg);
290 spin_unlock_irqrestore(&spike_dev.spi_lock, flags);
302 void spike_tasklet_func(unsigned long data)
306 status = spike_queue_spi_write();
314 DRDY Interrupt Handler
316 static irqreturn_t irq_handler(int irq, void *dev_id)
318 #ifdef DEBUG_TOGGLE_OUT
319 if (gpio_get_value(GPIO_TOGGLE_OUT)) {
320 gpio_set_value(GPIO_TOGGLE_OUT, 0);
322 gpio_set_value(GPIO_TOGGLE_OUT, 1);
326 tasklet_schedule(&spike_tasklet);
331 static ssize_t spike_file_read(struct file *filp, char __user *buff, size_t count,
343 if (down_interruptible(&spike_dev.fop_sem))
346 if (spike_ctl.busy) {
347 sprintf(spike_dev.user_buff, "spike_ctl.busy==1\n");
348 count = strlen(spike_dev.user_buff);
349 up(&spike_dev.fop_sem);
352 //sprintf(spike_dev.user_buff, "DMA\n");
354 sprintf(spike_dev.user_buff,
355 "Status: %d\nTX: %d %d %d %d\nRX: %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d\nCallback=%d DRDY=%d\n",
356 spike_ctl.msg.status,
357 spike_ctl.tx_buff[0], spike_ctl.tx_buff[1],
358 spike_ctl.tx_buff[2], spike_ctl.tx_buff[3],
359 spike_ctl.rx_buff[0], spike_ctl.rx_buff[1],
360 spike_ctl.rx_buff[2], spike_ctl.rx_buff[3],
361 spike_ctl.rx_buff[4], spike_ctl.rx_buff[5],
362 spike_ctl.rx_buff[6], spike_ctl.rx_buff[7],
363 spike_ctl.rx_buff[8], spike_ctl.rx_buff[9],
364 spike_ctl.rx_buff[10], spike_ctl.rx_buff[11],
365 spike_ctl.rx_buff[12], spike_ctl.rx_buff[13],
366 spike_ctl.rx_buff[14], spike_ctl.rx_buff[15],
367 spike_ctl.spi_callbacks, gpio_get_value(GPIO_DRDY_IN));
370 // status = spike_do_one_message();
371 //status = spike_send_cmd();
372 //status = spike_rcv_data();
376 status = spike_queue_spi_write();
379 sprintf(spike_dev.user_buff,
380 "spike_do_one_message failed : %d\n",
387 len = strlen(spike_dev.user_buff);
392 if (copy_to_user(buff, spike_dev.user_buff, count)) {
393 printk(KERN_ALERT "spike_read(): copy_to_user() failed\n");
400 up(&spike_dev.fop_sem);
406 static int spike_file_open(struct inode *inode, struct file *filp)
410 printk(KERN_INFO "spike_open: (%Lu)\n", filp->f_version);
412 if (down_interruptible(&spike_dev.fop_sem))
415 if (!spike_dev.user_buff) {
416 spike_dev.user_buff = kmalloc(USER_BUFF_SIZE, GFP_KERNEL);
417 if (!spike_dev.user_buff)
421 if (finfo.f_version != 0) {
422 printk(KERN_INFO "spike_open: busy\n");
426 finfo.f_version = filp->f_version;
427 finfo.sleep_mode = 0;
429 init_waitqueue_head(&(finfo.wait));
430 filp->private_data = (void*)&finfo; // プライベートデータに構造体ポインタ設定
434 up(&spike_dev.fop_sem);
439 static int spike_file_close(struct inode * inode, struct file * file)
441 printk(KERN_INFO "spike_close: (%Lu)\n",file->f_version);
443 ((FileInfo *)(file->private_data))->f_version = 0;
444 ((FileInfo *)(file->private_data))->sleep_mode = 0;
445 ((FileInfo *)(file->private_data))->intflag = 0;
449 static unsigned int spike_file_poll(struct file *file, struct poll_table_struct *ptab)
451 //printk(KERN_INFO "spike_file_poll: (%ld)\n",file->f_version);
453 if ( ((FileInfo *)(file->private_data))->intflag ) {
455 ((FileInfo *)(file->private_data))->intflag = 0;
456 //printk(KERN_INFO "spike_file_poll: (%ld) interrupted\n",file->f_version);
457 return POLLIN | POLLRDNORM; // 読み込みOK
459 // 割り込み入っていないので待ち行列に追加する 割り込みハンドラが起こす
460 poll_wait(file, &(((FileInfo *)(file->private_data))->wait), ptab);
461 //printk(KERN_INFO "spike_file_poll: (%ld) poll_wait\n",file->f_version);
464 ((FileInfo *)(file->private_data))->sleep_mode = 1;
469 static long spike_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
474 // printk(KERN_INFO "spike_file_ioctl: (%ld)\n",file->f_version);
478 if (down_interruptible(&spi_txbuf.txbuf_sem))
480 if (copy_from_user(&spi_txbuf.tx_len, (void *)arg, sizeof(int))) {
481 printk(KERN_ALERT "spike_file_ioctl(): copy_from_user() failed\n");
482 up(&spi_txbuf.txbuf_sem);
485 printk(KERN_INFO "spike_file_ioctl: CMD_TX_LEN %d\n", spi_txbuf.tx_len);
486 if (spi_txbuf.tx_len > SPI_TX_MAX) spi_txbuf.tx_len = SPI_TX_MAX;
487 up(&spi_txbuf.txbuf_sem);
491 if (down_interruptible(&spi_txbuf.txbuf_sem))
493 if (copy_from_user(&spi_txbuf.txbuf, (void *)arg, spi_txbuf.tx_len)) {
494 printk(KERN_ALERT "spike_file_ioctl(): copy_from_user() failed\n");
495 up(&spi_txbuf.txbuf_sem);
498 printk(KERN_INFO "spike_file_ioctl: CMD_TX_SET %02X %02X %02X %02X\n", spi_txbuf.txbuf[0], spi_txbuf.txbuf[1], spi_txbuf.txbuf[2], spi_txbuf.txbuf[3]);
499 up(&spi_txbuf.txbuf_sem);
504 p = ring_get(ring_read_get());
507 if (copy_to_user((void *)arg, p, SPI_DATA_SIZE)) {
508 printk(KERN_ALERT "spike_file_ioctl(): copy_to_user() failed\n");
512 // SPIで実際に受信しているデータ長を返す
513 case CMD_RECEIVED_LEN_GET:
514 i = spike_ctl.received_len;
515 if (copy_to_user((void *)arg, &i, sizeof(int))) {
516 printk(KERN_ALERT "spike_file_ioctl(): copy_to_user() failed\n");
523 if (copy_to_user((void *)arg, &i, sizeof(int))) {
524 printk(KERN_ALERT "spike_file_ioctl(): copy_to_user() failed\n");
533 printk(KERN_INFO "spike_file_ioctl: unknown cmd=%d\n", cmd);
538 static const struct file_operations spike_fops = {
539 .owner = THIS_MODULE,
540 // .read = spike_file_read,
541 .open = spike_file_open,
542 .release = spike_file_close,
543 .poll = spike_file_poll,
544 .unlocked_ioctl = spike_file_ioctl,
548 static int spike_probe(struct spi_device *spi_device)
552 spin_lock_irqsave(&spike_dev.spi_lock, flags);
553 spike_dev.spi_device = spi_device;
554 spin_unlock_irqrestore(&spike_dev.spi_lock, flags);
559 static int spike_remove(struct spi_device *spi_device)
563 spin_lock_irqsave(&spike_dev.spi_lock, flags);
564 spike_dev.spi_device = NULL;
565 spin_unlock_irqrestore(&spike_dev.spi_lock, flags);
572 static int __init add_spike_device_to_bus(void)
574 struct spi_master *spi_master;
575 struct spi_device *spi_device;
580 spi_master = spi_busnum_to_master(SPI_BUS);
582 printk(KERN_ALERT "spi_busnum_to_master(%d) returned NULL\n",
584 printk(KERN_ALERT "Missing modprobe omap2_mcspi?\n");
588 spi_device = spi_alloc_device(spi_master);
590 put_device(&spi_master->dev);
591 printk(KERN_ALERT "spi_alloc_device() failed\n");
595 spi_device->chip_select = SPI_BUS_CS0;
597 /* Check whether this SPI bus.cs is already claimed */
598 snprintf(buff, sizeof(buff), "%s.%u",
599 dev_name(&spi_device->master->dev),
600 spi_device->chip_select);
602 pdev = bus_find_device_by_name(spi_device->dev.bus, NULL, buff);
604 /* We are not going to use this spi_device, so free it */
605 spi_dev_put(spi_device);
608 * There is already a device configured for this bus.cs
609 * It is okay if it us, otherwise complain and fail.
611 if (pdev->driver && pdev->driver->name &&
612 strcmp(this_driver_name, pdev->driver->name)) {
614 "Driver [%s] already registered for %s\n",
615 pdev->driver->name, buff);
619 spi_device->max_speed_hz = SPI_BUS_SPEED;
620 spi_device->mode = SPI_MODE_0;
621 spi_device->bits_per_word = 8;
622 spi_device->irq = -1;
623 spi_device->controller_state = NULL;
624 spi_device->controller_data = NULL;
625 strlcpy(spi_device->modalias, this_driver_name, SPI_NAME_SIZE);
627 status = spi_add_device(spi_device);
629 spi_dev_put(spi_device);
630 printk(KERN_ALERT "spi_add_device() failed: %d\n",
635 put_device(&spi_master->dev);
640 static struct spi_driver spike_driver = {
642 .name = this_driver_name,
643 .owner = THIS_MODULE,
645 .probe = spike_probe,
646 .remove = __devexit_p(spike_remove),
649 static int __init spike_init_spi(void)
653 spike_ctl.tx_buff = kmalloc(SPI_BUFF_SIZE, GFP_KERNEL | GFP_DMA);
654 if (!spike_ctl.tx_buff) {
656 goto spike_init_error;
659 spike_ctl.rx_buff = kmalloc(SPI_BUFF_SIZE, GFP_KERNEL | GFP_DMA);
660 if (!spike_ctl.rx_buff) {
662 goto spike_init_error;
665 error = spi_register_driver(&spike_driver);
667 printk(KERN_ALERT "spi_register_driver() failed %d\n", error);
668 goto spike_init_error;
671 error = add_spike_device_to_bus();
673 printk(KERN_ALERT "add_spike_to_bus() failed\n");
674 spi_unregister_driver(&spike_driver);
675 goto spike_init_error;
679 spike_dev.spi_device->dev.coherent_dma_mask = 0xFFFFFFFF;
681 spike_ctl.tx_buff = dma_alloc_coherent(&(spike_dev.spi_device->dev), SPI_BUFF_SIZE, &spike_ctl.tx_dma, GFP_ATOMIC);
682 if (!spike_ctl.tx_buff) {
684 goto spike_init_error;
687 // spike_ctl.rx_buff = kmalloc(SPI_BUFF_SIZE, GFP_KERNEL | GFP_DMA);
688 spike_ctl.rx_buff = dma_alloc_coherent(&(spike_dev.spi_device->dev), SPI_BUFF_SIZE, &spike_ctl.rx_dma, GFP_ATOMIC);
689 if (!spike_ctl.rx_buff) {
691 goto spike_init_error;
699 if (spike_ctl.tx_buff) {
700 kfree(spike_ctl.tx_buff);
701 spike_ctl.tx_buff = 0;
702 //dma_free_coherent(&(spike_dev.spi_device->dev), SPI_BUFF_SIZE, spike_ctl.tx_buff, spike_ctl.tx_dma);
703 //spike_ctl.tx_dma = 0;
706 if (spike_ctl.rx_buff) {
707 kfree(spike_ctl.rx_buff);
708 spike_ctl.rx_buff = 0;
709 //dma_free_coherent(&(spike_dev.spi_device->dev), SPI_BUFF_SIZE, spike_ctl.rx_buff, spike_ctl.rx_dma);
710 //spike_ctl.rx_dma = 0;
717 static int __init spike_init_cdev(void)
721 spike_dev.devt = MKDEV(0, 0);
723 error = alloc_chrdev_region(&spike_dev.devt, 0, 1, this_driver_name);
725 printk(KERN_ALERT "alloc_chrdev_region() failed: %d \n",
730 cdev_init(&spike_dev.cdev, &spike_fops);
731 spike_dev.cdev.owner = THIS_MODULE;
733 error = cdev_add(&spike_dev.cdev, spike_dev.devt, 1);
735 printk(KERN_ALERT "cdev_add() failed: %d\n", error);
736 unregister_chrdev_region(spike_dev.devt, 1);
743 static int __init spike_init_class(void)
745 spike_dev.class = class_create(THIS_MODULE, this_driver_name);
747 if (!spike_dev.class) {
748 printk(KERN_ALERT "class_create() failed\n");
752 if (!device_create(spike_dev.class, NULL, spike_dev.devt, NULL,
754 printk(KERN_ALERT "device_create(..., %s) failed\n",
756 class_destroy(spike_dev.class);
762 int spike_init_gpio(void)
764 if (gpio_request(GPIO_DRDY_IN, "GPIO_DRDY_IN")) {
765 printk(KERN_ALERT "gpio_request(GPIO_DRDY_IN) failed\n");
766 goto init_gpio_fail_1;
769 if (gpio_direction_input(GPIO_DRDY_IN)) {
770 printk(KERN_ALERT "gpio_direction_input(GPIO_DRDY_IN) failed\n");
771 goto init_gpio_fail_2;
773 #ifdef DEBUG_TOGGLE_OUT
774 if (gpio_request(GPIO_TOGGLE_OUT, "GPIO_TOGGLE_OUT")) {
775 printk(KERN_ALERT "gpio_request(GPIO_TOGGLE_OUT) failed\n");
776 goto init_gpio_fail_2;
779 if (gpio_direction_output(GPIO_TOGGLE_OUT, 0)) {
780 printk(KERN_ALERT "gpio_direction_output(GPIO_TOGGLE_OUT) failed\n");
781 goto init_gpio_fail_3;
787 #ifdef DEBUG_TOGGLE_OUT
789 gpio_free(GPIO_TOGGLE_OUT);
793 gpio_free(GPIO_DRDY_IN);
799 void spike_free_gpio(void)
801 gpio_free(GPIO_DRDY_IN);
802 #ifdef DEBUG_TOGGLE_OUT
803 gpio_free(GPIO_TOGGLE_OUT);
806 int spike_init_irq(void)
810 irq_dev.irq = OMAP_GPIO_IRQ(GPIO_DRDY_IN);
811 result = request_irq(irq_dev.irq,
813 IRQF_TRIGGER_FALLING,
818 printk(KERN_ALERT "request_irq failed: %d\n", result);
824 void spike_free_irq(void)
826 free_irq(irq_dev.irq, &irq_dev);
829 static int __init spike_init(void)
831 memset(&spike_dev, 0, sizeof(spike_dev));
832 memset(&spike_ctl, 0, sizeof(spike_ctl));
834 sema_init(&spike_dev.fop_sem, 1);
835 spin_lock_init(&spike_dev.spi_lock);
837 memset(&irq_dev, 0, sizeof(irq_dev));
838 sema_init(&irq_dev.sem, 1);
841 memset(&spi_txbuf, 0, sizeof(spi_txbuf));
842 sema_init(&spi_txbuf.txbuf_sem, 1);
844 finfo.f_version = 0; // 未使用マーク
848 if (spike_init_cdev() < 0)
851 if (spike_init_class() < 0)
854 if (spike_init_spi() < 0)
856 // DRDY GPIO144 Input config
857 if (spike_init_gpio() < 0)
860 // DRDY GPIO144 Interrupt config
861 if (spike_init_irq() < 0)
865 printk(KERN_INFO "%s %s initialized\n", MODULE_NAME, VERSION);
872 if (spike_ctl.tx_buff)
873 kfree(spike_ctl.tx_buff);
874 if (spike_ctl.rx_buff)
875 kfree(spike_ctl.rx_buff);
878 device_destroy(spike_dev.class, spike_dev.devt);
879 class_destroy(spike_dev.class);
882 cdev_del(&spike_dev.cdev);
883 unregister_chrdev_region(spike_dev.devt, 1);
888 module_init(spike_init);
890 static void __exit spike_exit(void)
892 spi_unregister_device(spike_dev.spi_device);
893 spi_unregister_driver(&spike_driver);
900 device_destroy(spike_dev.class, spike_dev.devt);
901 class_destroy(spike_dev.class);
903 cdev_del(&spike_dev.cdev);
904 unregister_chrdev_region(spike_dev.devt, 1);
906 if (spike_ctl.tx_buff)
907 kfree(spike_ctl.tx_buff);
908 //dma_free_coherent(&(spike_dev.spi_device->dev), SPI_BUFF_SIZE, spike_ctl.tx_buff, spike_ctl.tx_dma);
910 if (spike_ctl.rx_buff)
911 kfree(spike_ctl.rx_buff);
912 //dma_free_coherent(&(spike_dev.spi_device->dev), SPI_BUFF_SIZE, spike_ctl.rx_buff, spike_ctl.rx_dma);
914 if (spike_dev.user_buff)
915 kfree(spike_dev.user_buff);
917 printk(KERN_INFO "%s %s removed\n", MODULE_NAME, VERSION);
919 module_exit(spike_exit);
921 MODULE_AUTHOR("Naoya Takamura");
922 MODULE_DESCRIPTION("sciLog AD SPI driver based on spike");
923 MODULE_LICENSE("GPL");
924 MODULE_VERSION("1.0");