OSDN Git Service

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