-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathat91-isi.c
1974 lines (1692 loc) · 53.2 KB
/
at91-isi.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2007 Atmel Corporation
*
* Based on the bttv driver for Bt848 with respective copyright holders
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#ifndef DEBUG
#define DEBUG 1
#endif
#include <linux/clk.h>
#include <linux/completion.h>
#include <linux/dma-mapping.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/ioctl.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <linux/version.h>
#include <linux/videodev2.h>
#include <linux/wait.h>
#include <linux/bootmem.h>
#include <linux/kfifo.h>
#include <asm/io.h>
#include <media/v4l2-common.h>
#include <media/v4l2-ioctl.h>
#include <media/at91-isi.h>
#define AT91_ISI_VERSION KERNEL_VERSION(0, 1, 0)
#define ISI_CODEC 0
/* Default ISI capture buffer size */
#define ISI_CAPTURE_BUFFER_SIZE 1600*1200*2
/* Default ISI video frame size */
#define ISI_VIDEO_BUFFER_SIZE 320*240*2
/* Default number of ISI video buffers */
#define ISI_VIDEO_BUFFERS 1
/* Maximum number of video buffers */
#define ISI_VIDEO_BUFFERS_MAX 8
/* Interrupt mask for a single capture */
#define ISI_CAPTURE_MASK (ISI_BIT(SOF) | ISI_BIT(FO_C_EMP))
/* ISI capture buffer size */
static int capture_buffer_size = ISI_CAPTURE_BUFFER_SIZE;
/* Number of buffers used for streaming video */
static int video_buffers = ISI_VIDEO_BUFFERS;
static int video_buffer_size = ISI_VIDEO_BUFFER_SIZE;
static int input_format = AT91_ISI_PIXFMT_YCbYCr;
static u8 has_emb_sync = 0;
static u8 emb_crc_sync = 0;
static u8 hsync_act_low = 0;
static u8 vsync_act_low = 1;
//pclk here refers to the pixel clock coming from a sensor
static u8 pclk_act_falling = 0;
static u8 isi_full_mode = 0;
static u8 gs_mode = 0;
/* Preview path horizontal size */
static int prev_hsize = 320;
/* Preview path vertical size */
static int prev_vsize = 240;
/* Scaling factor of the preview path */
static int prev_decimation_factor = 16;
/* Input image horizontal size */
static int image_hsize = 1600;
/* Input image vertical size */
static int image_vsize = 1200;
/* Frame rate scaler
* 1 = capture every second frame
* 2 = capture every third frame
* ...
* */
static int frame_rate_scaler = 2;
/* Set this value if we want to pretend a specific V4L2 output format
* This format is for the capturing interface
*/
static int capture_v4l2_fmt = V4L2_PIX_FMT_VYUY;
/* Set this value if we want to pretend a specific V4L2 output format
* This format is for the streaming interface
*/
static int streaming_v4l2_fmt = V4L2_PIX_FMT_VYUY;
MODULE_PARM_DESC(video_buffers,"Number of frame buffers used for streaming");
module_param(video_buffers, int, 0664);
MODULE_PARM_DESC(capture_buffer_size,"Capture buffer size");
module_param(capture_buffer_size, int, 0664);
MODULE_PARM_DESC(image_hsize,"Horizontal size of input image");
module_param(image_hsize, int, 0664);
MODULE_PARM_DESC(image_vsize,"Vertical size of input image");
module_param(image_vsize, int, 0664);
MODULE_PARM_DESC(frame_rate_scaler, "Frame rate scaler");
module_param(frame_rate_scaler, int, 0664);
MODULE_PARM_DESC(prev_hsize, "Horizontal image size of preview path output");
module_param(prev_hsize, int, 0664);
MODULE_PARM_DESC(prev_vsize, "Vertical image size of preview path output");
module_param(prev_vsize, int, 0664);
MODULE_PARM_DESC(prev_decimation_factor, "Preview path decimaion factor");
module_param(prev_decimation_factor, int, 0664);
/* Single frame capturing states */
enum {
STATE_IDLE = 0,
STATE_CAPTURE_READY,
STATE_CAPTURE_WAIT_SOF,
STATE_CAPTURE_IN_PROGRESS,
STATE_CAPTURE_DONE,
STATE_CAPTURE_ERROR,
};
/* Frame buffer states
* FRAME_UNUSED Frame(buffer) is not used by the ISI module -> an application
* can usually read out data in this state
* FRAME_QUEUED An application has queued the buffer in the incoming queue
* FRAME_DONE The ISI module has filled the buffer with data and placed is on
* the outgoing queue
* FRAME_ERROR Not used at the moment
* */
enum frame_status {
FRAME_UNUSED,
FRAME_QUEUED,
FRAME_DONE,
FRAME_ERROR,
};
/* Frame buffer descriptor
* Used by the ISI module as a linked list for the DMA controller.
*/
struct fbd {
/* Physical address of the frame buffer */
dma_addr_t fb_address;
/* Physical address of the next fbd */
dma_addr_t next_fbd_address;
};
/* Frame buffer data
*/
struct frame_buffer {
/* Frame buffer descriptor
* Used by the ISI DMA controller to provide linked list DMA operation
*/
struct fbd fb_desc;
/* Pointer to the start of the frame buffer */
void *frame_buffer;
/* Timestamp of the captured frame */
struct timeval timestamp;
/* Frame number of the frame */
unsigned long sequence;
/* Buffer number*/
int index;
/* Bytes used in the buffer for data, needed as buffers are always
* aligned to pages and thus may be bigger than the amount of data*/
int bytes_used;
/* Mmap count
* Counter to measure how often this buffer is mmapped
*/
int mmap_count;
/* Buffer status */
enum frame_status status;
};
struct at91_isi {
/* ISI module spin lock. Protects against concurrent access of variables
* that are shared with the ISR */
spinlock_t lock;
void __iomem *regs;
/* Pointer to the start of the fbd list */
dma_addr_t fbd_list_start;
/* Frame buffers */
struct frame_buffer video_buffer[ISI_VIDEO_BUFFERS_MAX];
/* Frame buffer currently used by the ISI module */
struct frame_buffer *current_buffer;
/* Size of a frame buffer */
size_t capture_buffer_size;
/* Streaming status
* If set ISI is in streaming mode */
int streaming;
/* Queue for incoming buffers
* The buffer number (index) is stored in the fifo as reference
*/
struct kfifo *grabq;
/* Spinlock for the incoming queue */
spinlock_t grabq_lock;
/* Queue for outgoing buffers
* Buffer number is stored in the fifo as reference
*/
struct kfifo *doneq;
/* Spinlock for the incoming queue */
spinlock_t doneq_lock;
/* State of the ISI module in capturing mode */
int state;
/* Pointer to ISI buffer */
void *capture_buf;
/* Physical address of the capture buffer */
dma_addr_t capture_phys;
/* Size of the ISI buffer */
size_t capture_buf_size;
/* Capture/streaming wait queue */
wait_queue_head_t capture_wq;
struct at91_isi_camera *camera;
struct at91_isi_format format;
struct at91_isi_format streaming_format;
struct mutex mutex;
/* User counter for the streaming interface */
int stream_users;
/* User counter of the capture interface */
int capture_users;
/* Video device for capturing (Codec path) */
struct video_device cdev;
/* Video device for streaming (Preview path) */
struct video_device vdev;
struct completion reset_complete;
struct clk *pclk;
struct clk *hclk;
struct platform_device *pdev;
unsigned int irq;
};
#define to_at91_isi(vdev) container_of(vdev, struct at91_isi, vdev)
struct at91_isi_fh {
struct at91_isi *isi;
unsigned int read_off;
};
/*-----------------------------------------------------------------------------
* Interface to the actual camera.
*/
static LIST_HEAD(camera_list);
static DEFINE_MUTEX(camera_list_mutex);
static void at91_isi_release_camera(struct at91_isi *isi, struct at91_isi_camera *cam)
{
mutex_lock(&camera_list_mutex);
cam->isi = NULL;
isi->camera = NULL;
module_put(cam->owner);
mutex_unlock(&camera_list_mutex);
}
int at91_isi_register_camera(struct at91_isi_camera *cam)
{
pr_debug("at91_isi: register camera %s\n", cam->name);
mutex_lock(&camera_list_mutex);
list_add_tail(&cam->list, &camera_list);
mutex_unlock(&camera_list_mutex);
return 0;
}
EXPORT_SYMBOL_GPL(at91_isi_register_camera);
void at91_isi_unregister_camera(struct at91_isi_camera *cam)
{
pr_debug("at91_isi: unregister camera %s\n", cam->name);
mutex_lock(&camera_list_mutex);
if (cam->isi)
cam->isi->camera = NULL;
list_del(&cam->list);
mutex_unlock(&camera_list_mutex);
}
EXPORT_SYMBOL_GPL(at91_isi_unregister_camera);
static struct at91_isi_camera * at91_isi_grab_camera(struct at91_isi *isi)
{
struct at91_isi_camera *entry, *cam = NULL;
mutex_lock(&camera_list_mutex);
list_for_each_entry(entry, &camera_list, list)
{
/* Just grab the first camera available */
if (!entry->isi)
{
if (!try_module_get(entry->owner))
continue;
cam = entry;
cam->isi = isi;
pr_debug("%s: got camera: %s\n",
isi->vdev.name, cam->name);
break;
}
}
mutex_unlock(&camera_list_mutex);
return cam;
}
static int at91_isi_set_camera_input(struct at91_isi *isi)
{
struct at91_isi_camera *cam = isi->camera;
int ret;
ret = cam->set_format(cam, &isi->format);
if (ret)
return ret;
return 0;
}
static void at91_isi_set_default_format(struct at91_isi *isi)
{
isi->format.pix.width = min((int)2048l, image_hsize);
isi->format.pix.height = min((int)2048l, image_vsize);
/* Set capture format if we have explicitely specified one */
if(capture_v4l2_fmt){
isi->format.pix.pixelformat = capture_v4l2_fmt;
}
else {
/* Codec path output format */
isi->format.pix.pixelformat = V4L2_PIX_FMT_VYUY;
}
/* The ISI module codec path tries to output YUV 4:2:2
* Therefore two pixels will be in a 32bit word */
isi->format.pix.bytesperline = ALIGN(isi->format.pix.width * 2, 4);
isi->format.pix.sizeimage = isi->format.pix.bytesperline *
isi->format.pix.height;
pr_debug("set capture format: width=%d height=%d\n",
isi->format.pix.width, isi->format.pix.height);
#ifdef ISI_CODEC
isi->streaming_format.pix.width = isi->format.pix.width;
isi->streaming_format.pix.height = isi->format.pix.height;
isi->streaming_format.pix.bytesperline = isi->format.pix.bytesperline;
isi->streaming_format.pix.sizeimage = isi->format.pix.sizeimage;
#else
isi->streaming_format.pix.width = min(320U, prev_hsize);
isi->streaming_format.pix.height = min(240U, prev_vsize);
/* The ISI module preview path outputs either RGB 5:5:5
* or grayscale mode. Normally 2 pixels are stored in one word.
* But since the grayscale mode offers the possibility to store 1 pixel
* in one word we have to adjust the size here.
*/
if(input_format == AT91_ISI_PXFMT_GREY
&& gs_mode == ISI_GS_1PIX_PER_WORD) {
isi->streaming_format.pix.bytesperline =
ALIGN(isi->streaming_format.pix.width *4, 4);
}
else {
isi->streaming_format.pix.bytesperline =
ALIGN(isi->streaming_format.pix.width * 2, 4);
}
isi->streaming_format.pix.sizeimage =
isi->streaming_format.pix.bytesperline *
isi->streaming_format.pix.height;
#endif
/* Set streaming format if we have explicitely specified one */
if(streaming_v4l2_fmt){
isi->streaming_format.pix.pixelformat = streaming_v4l2_fmt;
}
else {
/* Preview path output format
* Would be logically V4L2_PIX_FMT_BGR555X
* but this format does not exist in the specification
* So for now we pretend V4L2_PIX_FMT_RGB555X
* Also the Greyscale format does not fit on top of the V4L2
* format but for now we just return it.
*/
if(input_format == AT91_ISI_PIXFMT_GREY)
isi->streaming_format.pix.pixelformat = V4L2_PIX_FMT_GREY;
else
isi->streaming_format.pix.pixelformat = V4L2_PIX_FMT_RGB555X;
}
if(input_format){
isi->format.input_format = input_format;
/* Not needed but for completeness*/
isi->streaming_format.input_format = input_format;
}
}
static int at91_isi_init_hardware(struct at91_isi *isi)
{
u32 cr2, cr1, cr;
cr = 0;
switch (isi->format.input_format) {
case AT91_ISI_PIXFMT_GREY:
cr = ISI_BIT(GRAYSCALE);
break;
case AT91_ISI_PIXFMT_CbYCrY:
cr = ISI_BF(YCC_SWAP, 0);
break;
case AT91_ISI_PIXFMT_CrYCbY:
cr = ISI_BF(YCC_SWAP, 1);
break;
case AT91_ISI_PIXFMT_YCbYCr:
cr = ISI_BF(YCC_SWAP, 2);
break;
case AT91_ISI_PIXFMT_YCrYCb:
cr = ISI_BF(YCC_SWAP, 3);
break;
case AT91_ISI_PIXFMT_RGB24:
cr = ISI_BIT(COL_SPACE) | ISI_BF(RGB_CFG, 0);
break;
case AT91_ISI_PIXFMT_BGR24:
cr = ISI_BIT(COL_SPACE) | ISI_BF(RGB_CFG, 1);
break;
case AT91_ISI_PIXFMT_RGB16:
cr = (ISI_BIT(COL_SPACE) | ISI_BIT(RGB_MODE)
| ISI_BF(RGB_CFG, 0));
break;
case AT91_ISI_PIXFMT_BGR16:
cr = (ISI_BIT(COL_SPACE) | ISI_BIT(RGB_MODE)
| ISI_BF(RGB_CFG, 1));
break;
case AT91_ISI_PIXFMT_GRB16:
cr = (ISI_BIT(COL_SPACE) | ISI_BIT(RGB_MODE)
| ISI_BF(RGB_CFG, 2));
break;
case AT91_ISI_PIXFMT_GBR16:
cr = (ISI_BIT(COL_SPACE) | ISI_BIT(RGB_MODE)
| ISI_BF(RGB_CFG, 3));
break;
case AT91_ISI_PIXFMT_RGB24_REV:
cr = (ISI_BIT(COL_SPACE) | ISI_BIT(RGB_SWAP)
| ISI_BF(RGB_CFG, 0));
break;
case AT91_ISI_PIXFMT_BGR24_REV:
cr = (ISI_BIT(COL_SPACE) | ISI_BIT(RGB_SWAP)
| ISI_BF(RGB_CFG, 1));
break;
case AT91_ISI_PIXFMT_RGB16_REV:
cr = (ISI_BIT(COL_SPACE) | ISI_BIT(RGB_SWAP)
| ISI_BIT(RGB_MODE) | ISI_BF(RGB_CFG, 0));
break;
case AT91_ISI_PIXFMT_BGR16_REV:
cr = (ISI_BIT(COL_SPACE) | ISI_BIT(RGB_SWAP)
| ISI_BIT(RGB_MODE) | ISI_BF(RGB_CFG, 1));
break;
case AT91_ISI_PIXFMT_GRB16_REV:
cr = (ISI_BIT(COL_SPACE) | ISI_BIT(RGB_SWAP)
| ISI_BIT(RGB_MODE) | ISI_BF(RGB_CFG, 2));
break;
case AT91_ISI_PIXFMT_GBR16_REV:
cr = (ISI_BIT(COL_SPACE) | ISI_BIT(RGB_SWAP)
| ISI_BIT(RGB_MODE) | ISI_BF(RGB_CFG, 3));
break;
default:
return -EINVAL;
}
cr1 = ISI_BF(EMB_SYNC, has_emb_sync)
| ISI_BF(HSYNC_POL, hsync_act_low)
| ISI_BF(VSYNC_POL, vsync_act_low)
| ISI_BF(PIXCLK_POL, pclk_act_falling)
| ISI_BF(GS_MODE, gs_mode)
| ISI_BF(FULL, isi_full_mode)
| ISI_BIT(DIS);
isi_writel(isi, CR1, cr1);
#ifndef ISI_CODEC
/* These values depend on the sensor output image size */
isi_writel(isi, PDECF, prev_decimation_factor);/* 1/16 * 16 = 1*/
isi_writel(isi,PSIZE , ISI_BF(PREV_HSIZE,prev_hsize - 1)
| ISI_BF(PREV_VSIZE, prev_vsize - 1));
#endif
cr2 = isi_readl(isi, CR2);
cr2 |= cr;
cr2 = ISI_BFINS(IM_VSIZE, isi->format.pix.height - 1, cr2);
cr2 = ISI_BFINS(IM_HSIZE, isi->format.pix.width - 1, cr2);
isi_writel(isi, CR2, cr2);
pr_debug("set_format: cr1=0x%08x\n cr2=0x%08x\n",
isi_readl(isi, CR1), isi_readl(isi, CR2));
pr_debug("psize=0x%08x\n", isi_readl(isi, PSIZE));
return 0;
}
static int at91_isi_start_capture(struct at91_isi *isi)
{
u32 cr1;
int ret;
spin_lock_irq(&isi->lock);
isi->state = STATE_IDLE;
isi_readl(isi, SR); /* clear any pending SOF interrupt */
isi_writel(isi, IER, ISI_BIT(SOF));
isi_writel(isi, CR1, isi_readl(isi, CR1) & ~ISI_BIT(DIS));
spin_unlock_irq(&isi->lock);
pr_debug("isi: waiting for SOF\n");
ret = wait_event_interruptible(isi->capture_wq,
isi->state != STATE_IDLE);
if (ret)
return ret;
if (isi->state != STATE_CAPTURE_READY)
return -EIO;
/*
* Do a codec request. Next SOF indicates start of capture,
* the one after that indicates end of capture.
*/
pr_debug("isi: starting capture\n");
isi_writel(isi, CDBA, isi->capture_phys);
spin_lock_irq(&isi->lock);
isi->state = STATE_CAPTURE_WAIT_SOF;
cr1 = isi_readl(isi, CR1);
cr1 |= ISI_BIT(CODEC_ON);
isi_writel(isi, CR1, cr1);
isi_writel(isi, IER, ISI_CAPTURE_MASK);
spin_unlock_irq(&isi->lock);
return 0;
}
static void at91_isi_capture_done(struct at91_isi *isi,
int state)
{
u32 cr1;
cr1 = isi_readl(isi, CR1);
cr1 &= ~ISI_BIT(CODEC_ON);
isi_writel(isi, CR1, cr1);
isi->state = state;
wake_up_interruptible(&isi->capture_wq);
isi_writel(isi, IDR, ISI_CAPTURE_MASK);
}
static irqreturn_t at91_isi_handle_streaming(struct at91_isi *isi,
int sequence){
int reqnr;
if(kfifo_get(isi->grabq, (unsigned char *) &reqnr,
sizeof(int)) != sizeof(int)){
/* as no new buffer is available we keep the
* current one
*/
pr_debug("isi: dropping frame\n");
#ifdef ISI_CODEC
isi_writel(isi, CDBA,
isi->current_buffer->fb_desc.fb_address);
isi_writel(isi, CR1, ISI_BIT(CODEC_ON) |
isi_readl(isi, CR1));
#else
/* TEST this has to be tested if it messes up the ISI
* streaming process */
isi_writel(isi, PPFBD, (unsigned long)
&isi->video_buffer[isi->current_buffer->index]);
#endif
}
else{
isi->current_buffer->status = FRAME_DONE;
isi->current_buffer->sequence = sequence;
do_gettimeofday(&isi->current_buffer->timestamp);
/*isi->current_buffer->bytes_used =
ISI_VIDEO_MAX_FRAME_SIZE; */
kfifo_put(isi->doneq, (unsigned char *)
&(isi->current_buffer->index), sizeof(int));
isi->current_buffer = &(isi->video_buffer[reqnr]);
#ifdef ISI_CODEC
isi_writel(isi, CDBA,
isi->current_buffer->fb_desc.fb_address);
isi_writel(isi, CR1, ISI_BIT(CODEC_ON) |
isi_readl(isi, CR1));
#else
/*TODO check if fbd corresponds to frame buffer */
#endif
wake_up_interruptible(&isi->capture_wq);
}
return IRQ_HANDLED;
}
/* FIXME move code from ISR here
static irqreturn_t at91_isi_handle_capturing(struct at91_isi *isi){
}*/
/* isi interrupt service routine */
static irqreturn_t isi_interrupt(int irq, void *dev_id)
{
struct at91_isi *isi = dev_id;
u32 status, mask, pending;
irqreturn_t ret = IRQ_NONE;
/* TODO Should we set sequence to 0 upon each start sequence? */
static int sequence = 0;
spin_lock(&isi->lock);
status = isi_readl(isi, SR);
mask = isi_readl(isi, IMR);
pending = status & mask;
pr_debug("isi: interrupt status %x pending %x\n",
status, pending);
if(isi->streaming){
if(likely(pending & (ISI_BIT(FO_C_EMP) | ISI_BIT(FO_P_EMP)))){
sequence++;
ret = at91_isi_handle_streaming(isi, sequence);
}
}
else{
while (pending) {
if (pending & (ISI_BIT(FO_C_OVF) | ISI_BIT(FR_OVR))) {
at91_isi_capture_done(isi, STATE_CAPTURE_ERROR);
pr_debug("%s: FIFO overrun (status=0x%x)\n",
isi->vdev.name, status);
} else if (pending & ISI_BIT(SOF)) {
switch (isi->state) {
case STATE_IDLE:
isi->state = STATE_CAPTURE_READY;
wake_up_interruptible(&isi->capture_wq);
break;
case STATE_CAPTURE_READY:
break;
case STATE_CAPTURE_WAIT_SOF:
isi->state = STATE_CAPTURE_IN_PROGRESS;
break;
/*
case STATE_CAPTURE_IN_PROGRESS:
at91_isi_capture_done(isi, STATE_CAPTURE_DONE);
break;
*/
}
}
if (pending & ISI_BIT(FO_C_EMP)){
if( isi->state == STATE_CAPTURE_IN_PROGRESS)
at91_isi_capture_done(isi, STATE_CAPTURE_DONE);
}
if (pending & ISI_BIT(SOFTRST)) {
complete(&isi->reset_complete);
isi_writel(isi, IDR, ISI_BIT(SOFTRST));
}
status = isi_readl(isi, SR);
mask = isi_readl(isi, IMR);
pending = status & mask;
ret = IRQ_HANDLED;
}
}
spin_unlock(&isi->lock);
return ret;
}
/* ------------------------------------------------------------------------
* IOCTL videoc handling
* ----------------------------------------------------------------------*/
/* --------Capture ioctls ------------------------------------------------*/
/* Device capabilities callback function.
*/
static int at91_isi_capture_querycap(struct file *file, void *priv,
struct v4l2_capability *cap)
{
strcpy(cap->driver, "at91-isi");
strcpy(cap->card, "Atmel Image Sensor Interface");
cap->version = AT91_ISI_VERSION;
/* V4L2_CAP_VIDEO_CAPTURE -> This is a capture device
* V4L2_CAP_READWRITE -> read/write interface used
*/
cap->capabilities = (V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_READWRITE
);
return 0;
}
/* Input enumeration callback function.
* Enumerates available input devices.
* This can be called many times from the V4L2-layer by
* incrementing the index to get all avaliable input devices.
*/
static int at91_isi_capture_enum_input(struct file *file, void *priv,
struct v4l2_input *input)
{
struct at91_isi_fh *fh = priv;
struct at91_isi *isi = fh->isi;
/* Just one input (ISI) is available */
if (input->index != 0)
return -EINVAL;
/* Set input name as camera name */
strlcpy(input->name, isi->camera->name, sizeof(input->name));
input->type = V4L2_INPUT_TYPE_CAMERA;
/* Set to this value just because this should be set to a
* defined value
*/
input->std = V4L2_STD_PAL;
return 0;
}
/* Selects an input device.
* One input device (ISI) currently supported.
*/
static int at91_isi_capture_s_input(struct file *file, void *priv,
unsigned int index)
{
if (index != 0)
return -EINVAL;
return 0;
}
/* Gets current input device.
*/
static int at91_isi_capture_g_input(struct file *file, void *priv,
unsigned int *index)
{
*index = 0;
return 0;
}
/* Format callback function
* Returns a v4l2_fmtdesc structure with according values to a
* index.
* This function is called from user space until it returns
* -EINVAL.
*/
static int at91_isi_capture_enum_fmt_cap(struct file *file, void *priv,
struct v4l2_fmtdesc *fmt)
{
if (fmt->index != 0)
return -EINVAL;
/* if we want to pretend another ISI output
* this is usefull if we input an other input format from a camera
* than specified in the ISI -> makes it possible to swap bytes
* in the ISI output format but messes up the preview path output
*/
if(capture_v4l2_fmt){
fmt->pixelformat = capture_v4l2_fmt;
}
else {
/* This is the format the ISI tries to output */
strcpy(fmt->description, "CrYCbY (VYUY) 4:2:2");
fmt->pixelformat = V4L2_PIX_FMT_VYUY;
}
return 0;
}
static int at91_isi_capture_try_fmt_cap(struct file *file, void *priv,
struct v4l2_format *vfmt)
{
struct at91_isi_fh *fh = priv;
struct at91_isi *isi = fh->isi;
/* Just return the current format for now */
memcpy(&vfmt->fmt.pix, &isi->format.pix,
sizeof(struct v4l2_pix_format));
return 0;
}
/* Gets current hardware configuration
* For capture devices the pixel format settings are
* important.
*/
static int at91_isi_capture_g_fmt_cap(struct file *file, void *priv,
struct v4l2_format *vfmt)
{
struct at91_isi_fh *fh = priv;
struct at91_isi *isi = fh->isi;
/* Return current pixel format */
memcpy(&vfmt->fmt.pix, &isi->format.pix,
sizeof(struct v4l2_pix_format));
return 0;
}
static int at91_isi_capture_s_fmt_cap(struct file *file, void *priv,
struct v4l2_format *vfmt)
{
struct at91_isi_fh *fh = priv;
struct at91_isi *isi = fh->isi;
int ret = 0;
/* We have a fixed format so just copy the current format
* back
*/
memcpy(&vfmt->fmt.pix, &isi->format.pix,
sizeof(struct v4l2_pix_format));
return ret;
}
/* ------------ Preview path ioctls ------------------------------*/
/* Device capabilities callback function.
*/
static int at91_isi_streaming_querycap(struct file *file, void *priv,
struct v4l2_capability *cap)
{
strcpy(cap->driver, "at91-isi");
strcpy(cap->card, "Atmel Image Sensor Interface");
cap->version = AT91_ISI_VERSION;
/* V4L2_CAP_VIDEO_CAPTURE -> This is a capture device
* V4L2_CAP_READWRITE -> read/write interface used
* V4L2_CAP_STREAMING -> ioctl + mmap interface used
*/
cap->capabilities = (V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_READWRITE
| V4L2_CAP_STREAMING
);
return 0;
}
/* Input enumeration callback function.
* Enumerates available input devices.
* This can be called many times from the V4L2-layer by
* incrementing the index to get all avaliable input devices.
*/
static int at91_isi_streaming_enum_input(struct file *file, void *priv,
struct v4l2_input *input)
{
struct at91_isi_fh *fh = priv;
struct at91_isi *isi = fh->isi;
/* Just one input (ISI) is available */
if (input->index != 0)
return -EINVAL;
/* Set input name as camera name */
strlcpy(input->name, isi->camera->name, sizeof(input->name));
input->type = V4L2_INPUT_TYPE_CAMERA;
/* Set to this value just because this should be set to a
* defined value
*/
input->std = V4L2_STD_PAL;
return 0;
}
/* Selects an input device.
* One input device (ISI) currently supported.
*/
static int at91_isi_streaming_s_input(struct file *file, void *priv,
unsigned int index)
{
if (index != 0)
return -EINVAL;
return 0;
}
/* Gets current input device.
*/
static int at91_isi_streaming_g_input(struct file *file, void *priv,
unsigned int *index)
{
*index = 0;
return 0;
}
/* Format callback function
* Returns a v4l2_fmtdesc structure with according values to a
* index.
* This function is called from user space until it returns
* -EINVAL.
*/
static int at91_isi_streaming_enum_fmt_cap(struct file *file, void *priv,
struct v4l2_fmtdesc *fmt)
{
struct at91_isi_fh *fh = priv;
struct at91_isi *isi = fh->isi;
if (fmt->index != 0)
return -EINVAL;
/* TODO: Return all possible formats
* This depends on ISI and camera.
* A enum_fmt function or a data structure should be
* added to the camera driver.
* For now just one format supported
*/
if(streaming_v4l2_fmt){
strcpy(fmt->description, "Pretended format");
}
else{
strcpy(fmt->description, "Normal format");
}
/* The pretended and normal format are already set earlier */
fmt->pixelformat = isi->streaming_format.pix.pixelformat;
return 0;
}
static int at91_isi_streaming_try_fmt_cap(struct file *file, void *priv,
struct v4l2_format *vfmt)
{
struct at91_isi_fh *fh = priv;
struct at91_isi *isi = fh->isi;
/* FIXME For now we just return the current format*/
memcpy(&vfmt->fmt.pix, &isi->streaming_format.pix,
sizeof(struct v4l2_pix_format));
return 0;
}
/* Gets current hardware configuration
* For capture devices the pixel format settings are
* important.
*/
static int at91_isi_streaming_g_fmt_cap(struct file *file, void *priv,
struct v4l2_format *vfmt)
{
struct at91_isi_fh *fh = priv;
struct at91_isi *isi = fh->isi;
/*Copy current pixel format structure to user space*/
memcpy(&vfmt->fmt.pix, &isi->streaming_format.pix,
sizeof(struct v4l2_pix_format));
return 0;
}
static int at91_isi_streaming_s_fmt_cap(struct file *file, void *priv,
struct v4l2_format *vfmt)
{
struct at91_isi_fh *fh = priv;
struct at91_isi *isi = fh->isi;
int ret = 0;
/* Just return the current format as we do not support
* format switching */
memcpy(&vfmt->fmt.pix, &isi->streaming_format.pix,
sizeof(struct v4l2_pix_format));
return ret;
}
/* Checks if control is supported in driver
* No controls currently supported yet
*/
/*
static int at91_isi_streaming_queryctrl(struct file *file, void *priv,
struct v4l2_queryctrl *qc)
{
switch(qc->id){
case V4L2_CID_BRIGHTNESS:
strcpy(qc->name, "Brightness");
qc->minimum = 0;
qc->maximum = 100;
qc->step = 1;
qc->default_value = 50;
qc->flags = 0;
break;
default:
return -EINVAL;
}
return 0;
}
static int at91_isi_streaming_g_ctrl(struct file *file, void *priv,
struct v4l2_control *ctrl)
{
switch(ctrl->id){
case V4L2_CID_BRIGHTNESS:
ctrl->value = 0;
break;
default:
return -EINVAL;
}
return 0;
}
static int at91_isi_streaming_s_ctrl(struct file *file, void *priv,
struct v4l2_control *ctrl)
{
switch(ctrl->id){
case V4L2_CID_BRIGHTNESS:
break;
default:
return -EINVAL;