OSDN Git Service

2332cf6fab85550c0f68ed7d88aef3d1346319a4
[scilog/spike-ad.git] / spike-ad.c
1 /*
2   sciLogger SPI driver
3   spike.c
4
5   GPIO144 DRDY signal from PIC24@CPU2010
6   SPI2 AD data receive and CMD send for PIC24@CPU2010
7   
8   Copyright Naoya Takamura, 2011
9   This program based on spike.c
10 ---------------------------------------------
11   spike.c
12  
13   Copyright Scott Ellis, 2010
14  
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.
19  
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.
24  
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.
28 */
29
30 #include <linux/init.h>
31 #include <linux/module.h>
32 #include <linux/fs.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>
41
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>
47
48 #include "spike-ad.h"
49
50 #define SPI_BUFF_SIZE   2048    // for spike_ctl
51 #define USER_BUFF_SIZE  128
52
53 #define SPI_BUS 1
54 #define SPI_BUS_CS0 0
55 #define SPI_BUS_SPEED 500000    // Hz
56
57 #define SPI_DATA_SIZE   965     // $含む PICから受信するデータ長
58
59 #define GPIO_DRDY_IN 144        // DRDY Input = GPIO144
60
61 //#define       DEBUG_TOGGLE_OUT        // デバッグ時定義する
62 #ifdef  DEBUG_TOGGLE_OUT
63         #define GPIO_TOGGLE_OUT 145     // Debug用toggle出力 = GPIO145
64 #endif
65
66 /**** 注意! Version */
67 #define VERSION "1.0"
68 #define MODULE_NAME             "spike-ad"
69
70 const char this_driver_name[] = MODULE_NAME;
71
72 struct spike_control {
73         struct spi_message msg;
74         struct spi_transfer transfer;
75         u32 busy;       // 1=spi_async()終了待ち
76         u32 spi_callbacks;
77 //      u32 busy_counter;
78         u8 *tx_buff; 
79         u8 *rx_buff;
80         int     received_len;
81 };
82
83 static struct spike_control spike_ctl;
84
85 struct irq_device {
86         struct semaphore sem;
87         int irq;
88 };
89
90 static struct irq_device irq_dev;
91
92
93 struct spike_dev {
94         spinlock_t spi_lock;
95         struct semaphore fop_sem;
96         dev_t devt;
97         struct cdev cdev;
98         struct class *class;
99         struct spi_device *spi_device;
100         char *user_buff;
101 //      u8 test_data;
102 };
103
104 static struct spike_dev spike_dev;
105
106 // ファイル管理領域
107 typedef struct {
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=割り込み入った
112 } FileInfo;
113
114 static FileInfo finfo;
115
116 void spike_tasklet_func(unsigned long data);
117 DECLARE_TASKLET(spike_tasklet, spike_tasklet_func, 0);
118
119 //
120 /**** SPI送信データ バッファ ******************************************
121 */
122 #define SPI_TX_MAX      (64)    // SPI送信データの最大長
123
124 // 送信データバッファ(CMD for PIC)
125 typedef struct {
126         unsigned char   txbuf[SPI_TX_MAX];
127         int     tx_len; // SPIで送信するデータ長
128         struct semaphore txbuf_sem;
129 } SpiTxBuf;
130
131 static SpiTxBuf spi_txbuf;
132
133
134
135 //
136 /**** SPI受信データ リングバッファ ******************************************
137 */
138 #define RING_NUM        (30)    // リングバッファ個数
139 #define SPI_MAX (SPI_DATA_SIZE+256)     // 1回のデータバイト数max
140 static unsigned char    spibuf[RING_NUM][SPI_MAX];
141
142 /*
143         受信データのデコード結果と
144         ソケット送信・記録パケット
145 */
146
147 // 読み出し位置
148 static int      ring_read;
149 // 書き込み位置 データ受信で使用
150 static int      ring_write;
151
152 void ring_init(void)
153 {
154         ring_read = 0;
155         ring_write = ring_read;
156         memset(spibuf, 0, sizeof(spibuf));
157 }
158 #define ring_clear()    ring_read_set(ring_write_get())
159
160 // 読み出し位置
161 int ring_read_get(void)
162 {
163         return ring_read;
164 }
165 void ring_read_set(int i)
166 {
167         ring_read = i;
168 }
169 void ring_read_plus(void)
170 {
171         ring_read++;
172         if (ring_read >= RING_NUM) ring_read = 0;
173 }
174
175 // 書き込み位置 受信で使用
176 int ring_write_get(void)
177 {
178         return ring_write;
179 }
180 void ring_write_plus(void)
181 {
182         ring_write++;
183         if (ring_write >= RING_NUM) ring_write = 0;
184 }
185 // 読み込んでいないデータ数
186 int ring_num_get(void)
187 {
188         int     i;
189         
190         i = ring_write - ring_read;
191         if (i < 0) i += RING_NUM;
192         return i;
193 }
194
195 // 位置指定してデータ取得
196 unsigned char* ring_get(int ptr)
197 {
198         return spibuf[ptr];
199 }
200 /*
201         書き込み位置のパケットをゼロクリア
202 */
203 void ring_zero(void)
204 {
205         memset(spibuf[ring_write], 0, SPI_MAX);
206 }
207 /*
208         パケットバッファフル?
209         1=Full
210         0=not Full
211 */
212 int ring_full(void)
213 {
214         if (ring_num_get() >= RING_NUM-1) return 1;
215         return 0;
216 }
217
218
219 /*
220         spike_queue_spi_write()で開始した
221         spi_async()終了後に呼ばれる
222         つまりSPIでデータを受信完了した状態
223 */
224 static void spike_spi_completion_handler(void *arg)
225 {
226         unsigned char   *p;
227
228         spike_ctl.spi_callbacks++;
229         spike_ctl.busy = 0;
230
231         // 受信したデータをリングバッファに保存
232         p = ring_get(ring_write_get());
233         memcpy(p, spike_ctl.rx_buff, SPI_DATA_SIZE);
234         // 実際に受信できたデータ長
235         spike_ctl.received_len = spike_ctl.msg.actual_length;
236
237         // 書き込み位置進める
238         ring_write_plus();
239
240         // 寝ているものを起こす
241         if (finfo.sleep_mode) {
242                 // 待ちを
243                 wake_up_interruptible(&(finfo.wait));        // 起こす
244 //printk(KERN_INFO "intsel_interrupt: wakeup %ld\n", fi[i].f_version);
245                 finfo.sleep_mode = 0;
246                 finfo.intflag = 1;
247         }
248 }
249 /*
250         spi_async()で送信開始
251         受信データはcallback funcで受ける
252 */
253 static int spike_queue_spi_write(void)
254 {
255         int status;
256         unsigned long flags;
257
258         // struct spi_messageを初期化 ゼロクリア
259         spi_message_init(&spike_ctl.msg);
260
261         // Callback関数設定
262         spike_ctl.msg.complete = spike_spi_completion_handler;
263         spike_ctl.msg.context = NULL;
264
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);
268
269         if (down_interruptible(&spi_txbuf.txbuf_sem)) 
270                 return -ERESTARTSYS;
271                 // TXコマンドセット
272                 memcpy(spike_ctl.tx_buff, spi_txbuf.txbuf, spi_txbuf.tx_len);
273                 // TXコマンドクリア
274                 memset(spi_txbuf.txbuf, 0, SPI_TX_MAX);
275         up(&spi_txbuf.txbuf_sem);
276
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;
280
281         spi_message_add_tail(&spike_ctl.transfer, &spike_ctl.msg);
282
283         spin_lock_irqsave(&spike_dev.spi_lock, flags);
284
285         if (spike_dev.spi_device)
286                 status = spi_async(spike_dev.spi_device, &spike_ctl.msg);
287         else
288                 status = -ENODEV;
289
290         spin_unlock_irqrestore(&spike_dev.spi_lock, flags);
291         
292         if (status == 0)
293                 spike_ctl.busy = 1;
294         
295         return status;  
296 }
297 /*
298   Tasklet
299   IRQ Handlerで呼び出される
300   SPI送受信を開始する
301 */
302 void spike_tasklet_func(unsigned long data)
303 {
304         int status;
305         // SPI送受信開始
306         status = spike_queue_spi_write();
307         if (status) {
308                 // error
309         } else {
310                 // ok
311         }
312 }
313 /*
314         DRDY Interrupt Handler
315 */
316 static irqreturn_t irq_handler(int irq, void *dev_id)
317 {
318 #ifdef  DEBUG_TOGGLE_OUT
319         if (gpio_get_value(GPIO_TOGGLE_OUT)) {
320                 gpio_set_value(GPIO_TOGGLE_OUT, 0);
321         } else {
322                 gpio_set_value(GPIO_TOGGLE_OUT, 1);
323         }
324 #endif
325         // タスクレットにまかせる
326         tasklet_schedule(&spike_tasklet);
327
328         return IRQ_HANDLED;
329 }
330 #if 0
331 static ssize_t spike_file_read(struct file *filp, char __user *buff, size_t count,
332                         loff_t *offp)
333 {
334         size_t len;
335         ssize_t status = 0;
336
337         if (!buff) 
338                 return -EFAULT;
339
340         if (*offp > 0) 
341                 return 0;
342
343         if (down_interruptible(&spike_dev.fop_sem)) 
344                 return -ERESTARTSYS;
345
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);
350         return count;
351 } else {
352 //sprintf(spike_dev.user_buff, "DMA\n");
353
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));
368
369 }
370 //      status = spike_do_one_message();
371 //status = spike_send_cmd();
372 //status = spike_rcv_data();
373
374 #if 0
375 // SPI送受信開始
376 status = spike_queue_spi_write();
377
378         if (status) {
379                 sprintf(spike_dev.user_buff, 
380                         "spike_do_one_message failed : %d\n",
381                         status);
382         }
383         else {
384         }
385 #endif
386
387         len = strlen(spike_dev.user_buff);
388  
389         if (len < count) 
390                 count = len;
391
392         if (copy_to_user(buff, spike_dev.user_buff, count))  {
393                 printk(KERN_ALERT "spike_read(): copy_to_user() failed\n");
394                 status = -EFAULT;
395         } else {
396                 *offp += count;
397                 status = count;
398         }
399
400         up(&spike_dev.fop_sem);
401
402         return status;  
403 }
404 #endif
405
406 static int spike_file_open(struct inode *inode, struct file *filp)
407 {       
408         int status = 0;
409
410         printk(KERN_INFO "spike_open: (%Lu)\n", filp->f_version);
411
412         if (down_interruptible(&spike_dev.fop_sem)) 
413                 return -ERESTARTSYS;
414
415         if (!spike_dev.user_buff) {
416                 spike_dev.user_buff = kmalloc(USER_BUFF_SIZE, GFP_KERNEL);
417                 if (!spike_dev.user_buff) 
418                         status = -ENOMEM;
419         }       
420
421         if (finfo.f_version != 0) {
422                 printk(KERN_INFO "spike_open: busy\n");
423                 return -EBUSY;
424         }
425
426         finfo.f_version = filp->f_version;
427         finfo.sleep_mode = 0;
428         finfo.intflag = 0;
429         init_waitqueue_head(&(finfo.wait));
430         filp->private_data = (void*)&finfo;      // プライベートデータに構造体ポインタ設定
431
432         ring_clear();
433
434         up(&spike_dev.fop_sem);
435
436         return status;
437 }
438
439 static int spike_file_close(struct inode * inode, struct file * file)
440 {
441         printk(KERN_INFO "spike_close: (%Lu)\n",file->f_version);
442
443         ((FileInfo *)(file->private_data))->f_version = 0;
444         ((FileInfo *)(file->private_data))->sleep_mode = 0;
445         ((FileInfo *)(file->private_data))->intflag = 0;
446         return 0;
447 }
448
449 static unsigned int spike_file_poll(struct file *file, struct poll_table_struct *ptab)
450 {
451 //printk(KERN_INFO "spike_file_poll: (%ld)\n",file->f_version);
452         // 割り込み入っている
453         if ( ((FileInfo *)(file->private_data))->intflag ) {
454                 // フラグクリア
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
458         }
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);
462
463         // sleep_mode=寝ている
464         ((FileInfo *)(file->private_data))->sleep_mode = 1;
465
466         return 0;
467 }
468
469 static long spike_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
470 {
471         int     i;
472         unsigned char   *p;
473
474 //  printk(KERN_INFO "spike_file_ioctl: (%ld)\n",file->f_version);
475         switch(cmd) {
476         // SPI送信データ長セット
477         case CMD_TX_LEN:
478                 if (down_interruptible(&spi_txbuf.txbuf_sem)) 
479                         return -ERESTARTSYS;
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);
483                         return -EFAULT;
484                 }
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);
488                 return 0;
489         // SPI送信データセット
490         case CMD_TX_SET:
491                 if (down_interruptible(&spi_txbuf.txbuf_sem)) 
492                         return -ERESTARTSYS;
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);
496                         return -EFAULT;
497                 }
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);
500                 return 0;
501         // SPI受信データ返す
502         case CMD_RX_GET:
503                 // リングバッファからデータ取得
504                 p = ring_get(ring_read_get());
505                 // 読み込み位置進める
506                 ring_read_plus();
507                 if (copy_to_user((void *)arg, p, SPI_DATA_SIZE)) {
508                         printk(KERN_ALERT "spike_file_ioctl(): copy_to_user() failed\n");
509                         return -EFAULT;
510                 }
511                 return 0;
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");
517                         return -EFAULT;
518                 }
519                 return 0;
520         // リングバッファにあるデータ数を返す
521         case CMD_DNUM_GET:
522                 i = ring_num_get();
523                 if (copy_to_user((void *)arg, &i, sizeof(int))) {
524                         printk(KERN_ALERT "spike_file_ioctl(): copy_to_user() failed\n");
525                         return -EFAULT;
526                 }
527                 return 0;
528         // リングバッファクリア
529         case CMD_BUF_CLEAR:
530                 ring_clear();
531                 return 0;
532         default:
533                 printk(KERN_INFO "spike_file_ioctl: unknown cmd=%d\n", cmd);
534                 return 0;
535         }
536 }
537
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,
545 };
546
547
548 static int spike_probe(struct spi_device *spi_device)
549 {
550         unsigned long flags;
551
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);
555
556         return 0;
557 }
558
559 static int spike_remove(struct spi_device *spi_device)
560 {
561         unsigned long flags;
562
563         spin_lock_irqsave(&spike_dev.spi_lock, flags);
564         spike_dev.spi_device = NULL;
565         spin_unlock_irqrestore(&spike_dev.spi_lock, flags);
566
567         return 0;
568 }
569 /*
570         SPIデバイスの設定
571 */
572 static int __init add_spike_device_to_bus(void)
573 {
574         struct spi_master *spi_master;
575         struct spi_device *spi_device;
576         struct device *pdev;
577         char buff[64];
578         int status = 0;
579
580         spi_master = spi_busnum_to_master(SPI_BUS);
581         if (!spi_master) {
582                 printk(KERN_ALERT "spi_busnum_to_master(%d) returned NULL\n",
583                         SPI_BUS);
584                 printk(KERN_ALERT "Missing modprobe omap2_mcspi?\n");
585                 return -1;
586         }
587
588         spi_device = spi_alloc_device(spi_master);
589         if (!spi_device) {
590                 put_device(&spi_master->dev);
591                 printk(KERN_ALERT "spi_alloc_device() failed\n");
592                 return -1;
593         }
594
595         spi_device->chip_select = SPI_BUS_CS0;
596
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);
601
602         pdev = bus_find_device_by_name(spi_device->dev.bus, NULL, buff);
603         if (pdev) {
604                 /* We are not going to use this spi_device, so free it */ 
605                 spi_dev_put(spi_device);
606                 
607                 /* 
608                  * There is already a device configured for this bus.cs  
609                  * It is okay if it us, otherwise complain and fail.
610                  */
611                 if (pdev->driver && pdev->driver->name && 
612                                 strcmp(this_driver_name, pdev->driver->name)) {
613                         printk(KERN_ALERT 
614                                 "Driver [%s] already registered for %s\n",
615                                 pdev->driver->name, buff);
616                         status = -1;
617                 } 
618         } else {
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);
626                 
627                 status = spi_add_device(spi_device);            
628                 if (status < 0) {       
629                         spi_dev_put(spi_device);
630                         printk(KERN_ALERT "spi_add_device() failed: %d\n", 
631                                 status);                
632                 }                               
633         }
634
635         put_device(&spi_master->dev);
636
637         return status;
638 }
639
640 static struct spi_driver spike_driver = {
641         .driver = {
642                 .name = this_driver_name,
643                 .owner = THIS_MODULE,
644         },
645         .probe = spike_probe,
646         .remove = __devexit_p(spike_remove),    
647 };
648
649 static int __init spike_init_spi(void)
650 {
651         int error;
652
653         spike_ctl.tx_buff = kmalloc(SPI_BUFF_SIZE, GFP_KERNEL | GFP_DMA);
654         if (!spike_ctl.tx_buff) {
655                 error = -ENOMEM;
656                 goto spike_init_error;
657         }
658
659         spike_ctl.rx_buff = kmalloc(SPI_BUFF_SIZE, GFP_KERNEL | GFP_DMA);
660         if (!spike_ctl.rx_buff) {
661                 error = -ENOMEM;
662                 goto spike_init_error;
663         }
664
665         error = spi_register_driver(&spike_driver);
666         if (error < 0) {
667                 printk(KERN_ALERT "spi_register_driver() failed %d\n", error);
668                 goto spike_init_error;
669         }
670
671         error = add_spike_device_to_bus();
672         if (error < 0) {
673                 printk(KERN_ALERT "add_spike_to_bus() failed\n");
674                 spi_unregister_driver(&spike_driver);
675                 goto spike_init_error;  
676         }
677 // 一貫性のあるDMAマッピング
678 /*
679 spike_dev.spi_device->dev.coherent_dma_mask = 0xFFFFFFFF;
680
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) {
683                 error = -ENOMEM;
684                 goto spike_init_error;
685         }
686
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) {
690                 error = -ENOMEM;
691                 goto spike_init_error;
692         }
693 */
694
695         return 0;
696
697 spike_init_error:
698
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;
704         }
705
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;
711         }
712         
713         return error;
714 }
715
716
717 static int __init spike_init_cdev(void)
718 {
719         int error;
720
721         spike_dev.devt = MKDEV(0, 0);
722
723         error = alloc_chrdev_region(&spike_dev.devt, 0, 1, this_driver_name);
724         if (error < 0) {
725                 printk(KERN_ALERT "alloc_chrdev_region() failed: %d \n", 
726                         error);
727                 return -1;
728         }
729
730         cdev_init(&spike_dev.cdev, &spike_fops);
731         spike_dev.cdev.owner = THIS_MODULE;
732         
733         error = cdev_add(&spike_dev.cdev, spike_dev.devt, 1);
734         if (error) {
735                 printk(KERN_ALERT "cdev_add() failed: %d\n", error);
736                 unregister_chrdev_region(spike_dev.devt, 1);
737                 return -1;
738         }       
739
740         return 0;
741 }
742
743 static int __init spike_init_class(void)
744 {
745         spike_dev.class = class_create(THIS_MODULE, this_driver_name);
746
747         if (!spike_dev.class) {
748                 printk(KERN_ALERT "class_create() failed\n");
749                 return -1;
750         }
751
752         if (!device_create(spike_dev.class, NULL, spike_dev.devt, NULL,         
753                         this_driver_name)) {
754                 printk(KERN_ALERT "device_create(..., %s) failed\n",
755                         this_driver_name);
756                 class_destroy(spike_dev.class);
757                 return -1;
758         }
759
760         return 0;
761 }
762 int spike_init_gpio(void)
763 {
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;
767         }
768
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;
772         }
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;
777         }
778
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;
782         }
783 #endif
784
785         return 0;
786
787 #ifdef  DEBUG_TOGGLE_OUT
788 init_gpio_fail_3:
789         gpio_free(GPIO_TOGGLE_OUT);
790 #endif
791
792 init_gpio_fail_2:
793         gpio_free(GPIO_DRDY_IN);
794
795 init_gpio_fail_1:
796
797         return -1;
798 }
799 void spike_free_gpio(void)
800 {
801         gpio_free(GPIO_DRDY_IN);
802 #ifdef  DEBUG_TOGGLE_OUT
803         gpio_free(GPIO_TOGGLE_OUT);
804 #endif
805 }
806 int spike_init_irq(void)
807 {
808         int result;
809
810         irq_dev.irq = OMAP_GPIO_IRQ(GPIO_DRDY_IN);
811         result = request_irq(irq_dev.irq,
812                                 irq_handler,
813                                 IRQF_TRIGGER_FALLING,
814                                 "spike",
815                                 &irq_dev);
816
817         if (result < 0) {
818                 printk(KERN_ALERT "request_irq failed: %d\n", result);
819                 return -1;
820         }
821
822         return 0;
823 }
824 void spike_free_irq(void)
825 {
826         free_irq(irq_dev.irq, &irq_dev);
827 }
828
829 static int __init spike_init(void)
830 {
831         memset(&spike_dev, 0, sizeof(spike_dev));
832         memset(&spike_ctl, 0, sizeof(spike_ctl));
833
834         sema_init(&spike_dev.fop_sem, 1);
835         spin_lock_init(&spike_dev.spi_lock);
836
837         memset(&irq_dev, 0, sizeof(irq_dev));
838         sema_init(&irq_dev.sem, 1);
839
840         // TXコマンドクリア
841         memset(&spi_txbuf, 0, sizeof(spi_txbuf));
842         sema_init(&spi_txbuf.txbuf_sem, 1);
843
844         finfo.f_version = 0;            // 未使用マーク
845         // リングバッファ初期化
846         ring_init();
847
848         if (spike_init_cdev() < 0) 
849                 goto fail_1;
850         
851         if (spike_init_class() < 0)  
852                 goto fail_2;
853
854         if (spike_init_spi() < 0) 
855                 goto fail_3;
856         // DRDY GPIO144 Input config
857         if (spike_init_gpio() < 0)
858                 goto fail_4;
859 #if 1
860         // DRDY GPIO144 Interrupt config
861         if (spike_init_irq() < 0)
862                 goto fail_5;
863 #endif
864
865         printk(KERN_INFO "%s %s initialized\n", MODULE_NAME, VERSION);
866         return 0;
867
868 fail_5:
869         spike_free_gpio();
870
871 fail_4:
872         if (spike_ctl.tx_buff)
873                 kfree(spike_ctl.tx_buff);
874         if (spike_ctl.rx_buff)
875                 kfree(spike_ctl.rx_buff);
876
877 fail_3:
878         device_destroy(spike_dev.class, spike_dev.devt);
879         class_destroy(spike_dev.class);
880
881 fail_2:
882         cdev_del(&spike_dev.cdev);
883         unregister_chrdev_region(spike_dev.devt, 1);
884
885 fail_1:
886         return -1;
887 }
888 module_init(spike_init);
889
890 static void __exit spike_exit(void)
891 {
892         spi_unregister_device(spike_dev.spi_device);
893         spi_unregister_driver(&spike_driver);
894
895 #if 1
896         spike_free_irq();
897 #endif
898         spike_free_gpio();
899
900         device_destroy(spike_dev.class, spike_dev.devt);
901         class_destroy(spike_dev.class);
902
903         cdev_del(&spike_dev.cdev);
904         unregister_chrdev_region(spike_dev.devt, 1);
905
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);
909
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);
913
914         if (spike_dev.user_buff)
915                 kfree(spike_dev.user_buff);
916
917         printk(KERN_INFO "%s %s removed\n", MODULE_NAME, VERSION);
918 }
919 module_exit(spike_exit);
920
921 MODULE_AUTHOR("Naoya Takamura");
922 MODULE_DESCRIPTION("sciLog AD SPI driver based on spike");
923 MODULE_LICENSE("GPL");
924 MODULE_VERSION("1.0");
925