00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141 #include <linux/module.h>
00142 #include <linux/sched.h>
00143 #include <linux/slab.h>
00144 #include <linux/errno.h>
00145 #include <linux/kernel.h>
00146 #include <linux/fs.h>
00147 #include <linux/string.h>
00148 #include <linux/init.h>
00149 #include <linux/autoconf.h>
00150 #include <linux/interrupt.h>
00151 #include <linux/time.h>
00152 #include <linux/vmalloc.h>
00153
00154 #include <asm/system.h>
00155 #include <asm/byteorder.h>
00156 #include <asm/io.h>
00157
00158 #include <asm/arch/hwregs/intr_vect_defs.h>
00159
00160 #include <asm/irq.h>
00161
00162 #include <asm/delay.h>
00163 #include <asm/uaccess.h>
00164 #include <asm/elphel/driver_numbers.h>
00165 #include <asm/elphel/c313a.h>
00166 #include <asm/elphel/fpgaconfa.h>
00167 #include <asm/elphel/exifa.h>
00168 #include "fpgactrl.h"
00169 #include "fpga_sdram.h"
00170
00171 #include "x3x3.h"
00172
00173 #include "cxdma.h"
00174
00175 #include "framepars.h"
00176 #include "sensor_common.h"
00177 #include "pgm_functions.h"
00178 #include "circbuf.h"
00179 #include "exif353.h"
00180 #include "histograms.h"
00181 #include "gamma_tables.h"
00182 #include "quantization_tables.h"
00183
00184 #if ELPHEL_DEBUG
00185 #define ELPHEL_DEBUG_THIS 0
00186 #else
00187 #define ELPHEL_DEBUG_THIS 0
00188 #endif
00189 #if ELPHEL_DEBUG_THIS
00190 #define MDF2(x) printk("%s:%d:%s ",__FILE__,__LINE__,__FUNCTION__); x
00191 #define MDD1(x) printk("%s:%d:",__FILE__,__LINE__); x ; udelay (ELPHEL_DEBUG_DELAY)
00192 #define MD1(x) printk("%s:%d:",__FILE__,__LINE__);x
00193
00194 #define MD12(x) printk("%s:%d:",__FILE__,__LINE__);x
00195
00196 #define MD13(x) printk("%s:%d:",__FILE__,__LINE__);x
00197
00198 #else
00199 #define MDF2(x)
00200 #define MD1(x)
00201 #define MDD1(x)
00202 #define MD12(x)
00203 #define MD13(x)
00204 #endif
00205
00206
00207
00208 #define X3X3_ELPHEL_DRIVER_NAME "Elphel (R) Model 353 Camera Driver"
00209 static const char elphel_cam_name[] = "elphelcam353";
00210
00211 static volatile int JPEG_wp;
00212 static volatile int JPEG_rp;
00213 static int fpga_counter_prev=0;
00214 static struct meta_offsets_t {
00215 int Image_DateTime;
00216 int Photo_DateTimeOriginal;
00217 int Photo_ExposureTime;
00218 int Image_FrameNumber;
00219 } meta_offsets;
00220
00221
00222
00223
00224
00226
00227
00228
00229
00230
00231
00232 int camSeqGetJPEG_wp(void) {return JPEG_wp;}
00233 int camSeqGetJPEG_rp(void) {return JPEG_rp;}
00234 void camSeqSetJPEG_rp(int p) {
00235 JPEG_rp=p;
00236 set_globalParam(G_CIRCBUFRP, p<< 2);
00237 set_globalParam(G_FREECIRCBUF,
00238 (((get_globalParam(G_CIRCBUFRP) <= get_globalParam(G_CIRCBUFWP))?
00239 get_globalParam(G_CIRCBUFSIZE):0)+ get_globalParam(G_CIRCBUFRP))
00240 - get_globalParam(G_CIRCBUFWP));
00241 }
00242
00251 #define X3X3_IMAGEACQ_DRIVER_NAME "Elphel (R) Model 353 Image Acquisition device driver"
00252 static struct sensorproc_t s_sensorproc;
00253 struct sensorproc_t * sensorproc = NULL;
00254
00255
00256 struct image_acq_pd {
00257 int minor;
00258
00259
00260 };
00261
00262 void tasklet_fpga_function(unsigned long arg);
00263
00264
00266
00268
00273 int init_FPGA(void) {
00274 int i;
00275
00276
00277 if ((fpga_state & FPGA_STATE_LOADED) == 0) return -1;
00278
00279 if ((i=port_csp0_addr[X313__RA__MODEL]) < X313_MINMODREV) {
00280 printk ("too old fpga rev - found %x, software wants >= %x\n",i,X313_MINMODREV);
00281 return -1;
00282 }
00283 if (i > X313_MAXMODREV) {
00284 printk ("too new fpga rev - found %x, software wants <= %x\n",i,X313_MAXMODREV);
00285 return -1;
00286 }
00287 fpga_state |= FPGA_STATE_INITIALIZED;
00288
00289 if (!X313_IS_SDRAM_ON) fpga_initSDRAM();
00290
00291 MD1(printk("init_FPGA, fpga_state=0x%x\n",fpga_state));
00292 if (X313_CHN0_USED!=0) return 0;
00293 return 1;
00294 }
00295
00307 int init_acq_sensor(void);
00308
00309 static int __init image_acq_init(void);
00310
00311 DECLARE_TASKLET(tasklet_fpga, tasklet_fpga_function, 0);
00312
00313
00314
00320 inline int updateIRQJPEG_wp(void) {
00321 int xferred;
00322 int fpga_cntr= X313_XFERCNTR;
00323 xferred= fpga_cntr-fpga_counter_prev;
00324 #if ELPHEL_DEBUG_THIS
00325 set_globalParam (0x300,get_globalParam (0x300)+1);
00326 set_globalParam (0x302,fpga_cntr);
00327 if (xferred==0) set_globalParam (0x305,get_globalParam (0x305)+1);
00328 #endif
00329 if (xferred==0) return 0;
00330 fpga_counter_prev= fpga_cntr;
00331 if (xferred <0) xferred+= (1 <<24) ;
00332 JPEG_wp+= (xferred << 3);
00333 int circbuf_size=get_globalParam (G_CIRCBUFSIZE)>>2;
00334 if (JPEG_wp > circbuf_size) JPEG_wp-=circbuf_size;
00335 #if ELPHEL_DEBUG_THIS
00336 set_globalParam (0x301,get_globalParam (0x301)+1);
00337 set_globalParam (0x303,xferred);
00338 set_globalParam (0x304,JPEG_wp);
00339 #endif
00340 return 1;
00341 }
00345 inline void updateIRQCircbuf(void) {
00346 set_globalParam (G_CIRCBUFWP, JPEG_wp<<2);
00347 set_globalParam (G_FREECIRCBUF, (((get_globalParam (G_CIRCBUFRP) <= get_globalParam (G_CIRCBUFWP))? get_globalParam (G_CIRCBUFSIZE):0)+
00348 get_globalParam (G_CIRCBUFRP)) - get_globalParam (G_CIRCBUFWP));
00349 }
00350
00355 inline void updateIRQFocus(void) {
00356 set_globalParam (G_GFOCUS_VALUE, X313_HIGHFREQ);
00357 set_imageParamsThis (P_FOCUS_VALUE, X313_HIGHFREQ);
00358 }
00359
00360
00361
00366 inline struct interframe_params_t* updateIRQ_interframe(void) {
00367 int circbuf_size=get_globalParam (G_CIRCBUFSIZE)>>2;
00368 int alen = JPEG_wp-9; if (alen<0) alen+=circbuf_size;
00369 int jpeg_len=ccam_dma_buf_ptr[alen] & 0xffffff;
00370 int aframe_params=(alen & 0xfffffff8)-
00371 (((jpeg_len + CCAM_MMAP_META + 3) & 0xffffffe0)>>2)
00372 -8;
00373 if(aframe_params < 0) aframe_params += circbuf_size;
00374 struct interframe_params_t* interframe= (struct interframe_params_t*) &ccam_dma_buf_ptr[aframe_params];
00376 interframe->frame_length=jpeg_len;
00377 interframe->signffff=0xffff;
00378 #if ELPHEL_DEBUG_THIS
00379 set_globalParam (0x306,get_globalParam (0x306)+1);
00380 #endif
00381
00382 return interframe;
00383 }
00384
00389 inline void updateIRQ_Exif(struct interframe_params_t* interframe) {
00390 int index_time = JPEG_wp-11; if (index_time<0) index_time+=get_globalParam (G_CIRCBUFSIZE)>>2;
00392 char * exif_meta_time_string=encode_time(ccam_dma_buf_ptr[index_time], ccam_dma_buf_ptr[index_time+1]);
00394 write_meta_irq(exif_meta_time_string, &meta_offsets.Photo_DateTimeOriginal, Exif_Photo_DateTimeOriginal, 27);
00395 write_meta_irq(exif_meta_time_string, &meta_offsets.Image_DateTime, Exif_Image_DateTime, 20);
00396 putlong_meta_irq(get_imageParamsThis(P_EXPOS), &meta_offsets.Photo_ExposureTime, Exif_Photo_ExposureTime);
00397 putlong_meta_irq(get_imageParamsThis(P_FRAME), &meta_offsets.Image_FrameNumber, Exif_Image_FrameNumber);
00398 interframe->meta_index=store_meta();
00399 }
00400
00408 static irqreturn_t elphel_FPGA_interrupt(int irq, void *dev_id) {
00409 unsigned long irq_state;
00410
00411 irq_state = X313_IRQSTATE;
00412 DIS_INTERRUPTS;
00413 PROFILE_NEXT(0);
00416 if (updateIRQJPEG_wp()) {
00417 updateIRQCircbuf();
00418 updateIRQFocus();
00419 struct interframe_params_t* interframe= updateIRQ_interframe();
00420
00421
00422
00423 updateIRQ_Exif(interframe);
00424 updateFramePars(X3X3_I2C_FRAME, interframe);
00425 wake_up_interruptible(&circbuf_wait_queue);
00426 } else {
00427 updateFramePars(X3X3_I2C_FRAME, NULL);
00428 }
00429 PROFILE_NOW(1);
00430 wake_up_interruptible(&framepars_wait_queue);
00431 tasklet_schedule(&tasklet_fpga);
00432
00433 EN_INTERRUPT(SMART);
00434
00435 return IRQ_HANDLED;
00436 }
00437
00460
00461
00462
00463 void tasklet_fpga_function(unsigned long arg) {
00464 int hist_en;
00465 int tasklet_disable=get_globalParam(G_TASKLET_CTL);
00466 unsigned long thisFrameNumber=getThisFrameNumber();
00467 unsigned long prevFrameNumber=thisFrameNumber-1;
00468 unsigned long * hash32p=&(framepars[(thisFrameNumber-1) & PARS_FRAMES_MASK].pars[P_GTAB_R]);
00470 if ((getThisFrameNumber() ^ X3X3_I2C_FRAME) & PARS_FRAMES_MASK) return;
00471
00472
00473 PROFILE_NOW(2);
00474 switch ((tasklet_disable >> TASKLET_CTL_HISTY_BIT) & 7) {
00475 case TASKLET_HIST_NEVER:
00476 hist_en=0;
00477 break;
00478 case TASKLET_HIST_HALF:
00479 hist_en= ((thisFrameNumber & 1) ==0) || (get_imageParamsPrev(P_HISTRQ) & (1<<HISTRQ_BIT_Y));
00480 break;
00481 case TASKLET_HIST_QUATER:
00482 hist_en= ((thisFrameNumber & 3) ==0) || (get_imageParamsPrev(P_HISTRQ) & (1<<HISTRQ_BIT_Y));
00483 break;
00484 case TASKLET_HIST_ONCE:
00485 hist_en= ((thisFrameNumber & 7) ==0) || (get_imageParamsPrev(P_HISTRQ) & (1<<HISTRQ_BIT_Y));
00486 break;
00487 case TASKLET_HIST_RQONLY:
00488 hist_en= (get_imageParamsPrev(P_HISTRQ) & (1<<HISTRQ_BIT_Y));
00489 break;
00490 case TASKLET_HIST_ALL:
00491 default:
00492 hist_en=1;
00493 }
00494 if (hist_en) {
00496 set_histograms (prevFrameNumber, (1 << COLOR_Y_NUMBER), hash32p);
00497 GLOBALPARS(G_HIST_Y_FRAME)=prevFrameNumber;
00498 PROFILE_NOW(3);
00500 if ((getThisFrameNumber() ^ X3X3_I2C_FRAME) & PARS_FRAMES_MASK) return;
00501 #if HISTOGRAMS_WAKEUP_ALWAYS
00502 }
00503 wake_up_interruptible(&hist_y_wait_queue);
00504 #else
00505 wake_up_interruptible(&hist_y_wait_queue);
00506 }
00507 #endif
00509 if ((tasklet_disable & (1 << TASKLET_CTL_PGM)) == 0) {
00510 processPars (sensorproc, getThisFrameNumber(), get_globalParam(G_MAXAHEAD));
00511 PROFILE_NOW(4);
00512 }
00514 if ((getThisFrameNumber() ^ X3X3_I2C_FRAME) & PARS_FRAMES_MASK) return;
00515
00516 switch ((tasklet_disable >> TASKLET_CTL_HISTC_BIT) & 7) {
00517 case TASKLET_HIST_NEVER:
00518 hist_en=0;
00519 break;
00520 case TASKLET_HIST_HALF:
00521 hist_en= ((thisFrameNumber & 1) ==0) || (get_imageParamsPrev(P_HISTRQ) & (1<<HISTRQ_BIT_C));
00522 break;
00523 case TASKLET_HIST_QUATER:
00524 hist_en= ((thisFrameNumber & 3) ==0) || (get_imageParamsPrev(P_HISTRQ) & (1<<HISTRQ_BIT_C));
00525 break;
00526 case TASKLET_HIST_ONCE:
00527 hist_en= ((thisFrameNumber & 7) ==0) || (get_imageParamsPrev(P_HISTRQ) & (1<<HISTRQ_BIT_C));
00528 break;
00529 case TASKLET_HIST_RQONLY:
00530 hist_en= (get_imageParamsPrev(P_HISTRQ) & (1<<HISTRQ_BIT_C));
00531 break;
00532 case TASKLET_HIST_ALL:
00533 default:
00534 hist_en=1;
00535 }
00536
00537
00538
00539
00540
00541
00542
00543 if (hist_en) {
00545 set_histograms (prevFrameNumber, 0xf, hash32p);
00546 GLOBALPARS(G_HIST_C_FRAME)=prevFrameNumber;
00547 PROFILE_NOW(5);
00549 if ((getThisFrameNumber() ^ X3X3_I2C_FRAME) & PARS_FRAMES_MASK) return;
00550 #if HISTOGRAMS_WAKEUP_ALWAYS
00551 }
00552 wake_up_interruptible(&hist_c_wait_queue);
00553 #else
00554 wake_up_interruptible(&hist_c_wait_queue);
00555 }
00556 #endif
00557 }
00558
00562 void reset_compressor(void) {
00563 unsigned long flags;
00564 local_irq_save(flags);
00565 port_csp0_addr[X313_WA_COMP_CMD]= COMPCMD_RESET;
00566 if (framepars) set_imageParamsR_all( P_COMPRESSOR_RUN, COMPRESSOR_RUN_STOP );
00567 else printk ("framepars is not initialized\n");
00569 JPEG_wp=0;
00570 JPEG_rp=0;
00571 updateIRQCircbuf();
00572 fpga_counter_prev=0;
00573 local_irq_restore(flags);
00574 }
00575
00580 void camera_interrupts (int on) {
00581 MDF2(printk ("camera_interrupts(%d)\n",on));
00582 if (on) {
00583 EN_INTERRUPT(SMART);
00584 } else {
00585 DIS_INTERRUPTS;
00586 }
00588 port_csp0_addr[X313_WA_SMART_IRQ]=0x8000;
00589
00590 reg_intr_vect_rw_mask intr_mask;
00591 intr_mask = REG_RD(intr_vect, regi_irq, rw_mask);
00592 intr_mask.ext = on ? 1 : 0;
00593 REG_WR(intr_vect, regi_irq, rw_mask, intr_mask);
00594 }
00595
00596
00597 int image_acq_open(struct inode *inode, struct file *filp) ;
00598 int image_acq_release(struct inode *inode, struct file *filp) ;
00599 loff_t image_acq_fops_lseek (struct file * file, loff_t offset, int orig) ;
00600 ssize_t image_acq_fops_write(struct file * file, const char * buf, size_t count, loff_t *off) ;
00601 int image_acq_mmap (struct file *file, struct vm_area_struct *vma) ;
00602
00603 static struct file_operations image_acq_fops = {
00604 owner: THIS_MODULE,
00605 llseek: image_acq_fops_lseek,
00606
00607 write: image_acq_fops_write,
00608
00609 open: image_acq_open,
00610 mmap: image_acq_mmap,
00611 release: image_acq_release
00612 };
00613 static int __init image_acq_init(void) {
00614 int res;
00615 sensorproc= &s_sensorproc;
00616 MDD1(printk("sensorproc=0x%x\n",(int) sensorproc));
00617 init_ccam_dma_buf_ptr();
00618
00619 res = register_chrdev(ELPHEL_MAJOR, elphel_cam_name, &image_acq_fops);
00620 if(res < 0) {
00621 printk(KERN_ERR "image_acq_init: couldn't get a major number %d.\n",ELPHEL_MAJOR);
00622 return res;
00623 }
00624 if(request_irq(EXT_INTR_VECT,
00625 elphel_FPGA_interrupt,
00626 SA_INTERRUPT,
00627 "Elphel FPGA interrupts",
00628 NULL)) {
00629 printk(KERN_ERR "Can't allocate Elphel FPGA interrupts");
00630 return -EBUSY;
00631 }
00632 printk("Elphel FPGA interrupts initialized\n");
00633
00634
00635 MDD1(printk("reset_compressor()\n"));
00636 reset_compressor();
00637 MDD1(printk("x313_dma_init()\n"));
00638 x313_dma_init();
00639 MDD1(printk("init_pgm_proc ()\n"));
00640 init_pgm_proc ();
00641 MDD1(printk("reset_qtables()\n"));
00642 reset_qtables();
00643 printk(X3X3_ELPHEL_DRIVER_NAME" - %d\n",ELPHEL_MAJOR);
00644 return 0;
00645 }
00646
00647
00648 int image_acq_open(struct inode *inode, struct file *filp) {
00649 return 0;
00650 }
00651 int image_acq_release(struct inode *inode, struct file *filp) {
00652 return 0;
00653 }
00654 loff_t image_acq_fops_lseek (struct file * file, loff_t offset, int orig) {
00655 return 0;
00656 }
00657 ssize_t image_acq_fops_write(struct file * file, const char * buf, size_t count, loff_t *off) {
00658 return 0;
00659 }
00660 int image_acq_mmap (struct file *file, struct vm_area_struct *vma) {
00661 return 0;
00662 }
00663
00664 module_init(image_acq_init);
00665 MODULE_LICENSE("GPLv3.0");
00666 MODULE_AUTHOR("Andrey Filippov <andrey@elphel.com>.");
00667 MODULE_DESCRIPTION(X3X3_IMAGEACQ_DRIVER_NAME);