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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
|
/*
* Copyright (C) 2012 Spreadtrum Communications Inc.
*
* This software is licensed under the terms of the GNU General Public
* License version 2, as published by the Free Software Foundation, and
* may be copied, distributed, and modified under those terms.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/poll.h>
#include <linux/fs.h>
#include <linux/irq.h>
#include <linux/mm.h>
#include <linux/interrupt.h>
#include <linux/platform_device.h>
#include <linux/miscdevice.h>
#include <asm/io.h>
#include <linux/file.h>
#include <linux/slab.h>
#include <linux/err.h>
#include <linux/types.h>
#include <linux/string.h>
#include <linux/sched.h>
#include <linux/spinlock_types.h>
#include <linux/semaphore.h>
#include <linux/delay.h>
#include <linux/proc_fs.h>
#include <video/isp_drv_kernel.h>
#include <soc/sprd/sci.h>
#include <linux/clk.h>
#include <asm/cacheflush.h>
#ifndef CONFIG_64BIT
#include <soc/sprd/hardware.h>
#endif
#include "compat_isp_drv_kernel.h"
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/vmalloc.h>
#include <linux/kthread.h>
#include "Shark_reg_isp.h"
#include "parse_hwinfo.h"
#define DEBUG_ISP_DRV
#ifdef DEBUG_ISP_DRV
#define ISP_PRINT printk
#else
#define ISP_PRINT(...)
#endif
#define ISP_QUEUE_LENGTH 16
#define SA_SHIRQ IRQF_SHARED
static uint32_t g_isp_irq = 0x12345678;/*for share irq handler function*/
//static uint32_t g_dcam_irq = 0x12345678;/*for share irq handler function*/
#define ISP_MINOR MISC_DYNAMIC_MINOR/*isp minor number*/
#define init_MUTEX(sem) sema_init(sem, 1)
#define init_MUTEX_LOCKED(sem) sema_init(sem, 0)
#define IO_PTR volatile void __iomem *
#define ISP_READL(a) __raw_readl((IO_PTR)a)
#define ISP_WRITEL(a,v) __raw_writel(v,(IO_PTR)a)
#define ISP_OWR(a,v) __raw_writel((__raw_readl((IO_PTR)a) | v), (IO_PTR)a)
#define ISP_AWR(a,v) __raw_writel((__raw_readl((IO_PTR)a) & v), (IO_PTR)a)
#define ISP_NAWR(a,v) __raw_writel((__raw_readl((IO_PTR)a) & ~v), (IO_PTR)a)
#define ISP_REG_RD(a) ISP_READL(((IO_PTR)a))
#define DEBUG_STR "Error L %d, %s \n"
#define DEBUG_ARGS __LINE__,__FUNCTION__
#define ISP_LOWEST_ADDR 0x800
#define ISP_ADDR_INVALID(addr) ((unsigned long)(addr) < (unsigned long)ISP_LOWEST_ADDR)
#define ISP_CHECK_ZERO(a) \
do { \
if (ISP_ADDR_INVALID(a)) { \
printk("isp_k, zero pointer \n"); \
printk(DEBUG_STR, DEBUG_ARGS); \
return -EFAULT; \
} \
} while(0)
#define ISP_CHECK_ZERO_VOID(a) \
do { \
if (ISP_ADDR_INVALID(a)) { \
printk("isp_k, zero pointer \n"); \
printk(DEBUG_STR, DEBUG_ARGS); \
return; \
} \
} while(0)
#define ISP_DCAM_IRQ_MASK 0x03
#define ISP_DCAM_IRQ_NUM 0x02
#define ISP_BUF_MAX_SIZE ISP_TMP_BUF_SIZE_MAX_V0001
#define ISP_IRQ_NUM ISP_IRQ_NUM_V0001
#define ISP_IRQ_HW_MASK ISP_IRQ_HW_MASK_V0001
#define ISP_TIME_OUT_MAX (500)
struct isp_node {
uint32_t isp_irq_val;
uint32_t dcam_irq_val;
uint64_t system_time;
};
struct isp_queue {
struct isp_node node[ISP_QUEUE_LENGTH];
struct isp_node *write;
struct isp_node *read;
};
struct isp_lnc_load {
uint8_t load_lnc_flag;
uint8_t cap_eof_flag;
uint32_t param_counts;
unsigned long load_buf;
struct semaphore load_done_sem;
};
struct isp_device_t
{
unsigned long reg_base_addr;/*the pointer of isp register context*/
uint32_t size;/* struct size*/
unsigned long buf_addr;
uint32_t buf_len;
struct isp_queue queue;
struct semaphore sem_isr;/*for interrupts*/
struct semaphore sem_isp;/*for the isp device, protect the isp hardware; protect only one caller use the oi*/
/*controll/read/write functions*/
struct clk* s_isp_clk_mm_i;
struct clk *s_isp_clk;
struct semaphore kernel_sem;
struct task_struct* kernel_thread;
struct isp_lnc_load lnc_load;
uint32_t is_kernel_thread_stop;
};
enum dcam_irq_id {
DCAM_SN_SOF = 0,
DCAM_SN_EOF,
DCAM_CAP_SOF,
DCAM_CAP_EOF,
DCAM_PATH0_DONE,
DCAM_PATH0_OV,
DCAM_PATH1_DONE,
DCAM_PATH1_OV,
DCAM_PATH2_DONE,
DCAM_PATH2_OV,
DCAM_SN_LINE_ERR,
DCAM_SN_FRAME_ERR,
DCAM_JPEG_BUF_OV,
DCAM_ISP_OV,
DCAM_MIPI_OV,
DCAM_ROT_DONE,
DCAM_PATH1_SLICE_DONE,
DCAM_PATH2_SLICE_DONE,
DCAM_RAW_SLICE_DONE,
DCAM_PATH1_SOF,
DCAM_PATH2_SOF,
DCAM_IRQ_NUMBER
};
struct dcam_frame {
uint32_t type;
uint32_t lock;
uint32_t flags;
uint32_t fid;
uint32_t width;
uint32_t height;
uint32_t yaddr;
uint32_t uaddr;
uint32_t vaddr;
struct dcam_frame *prev;
struct dcam_frame *next;
};
typedef int (*dcam_isr_func)(struct dcam_frame* frame, void* u_data);
extern int32_t dcam_reg_isr(enum dcam_irq_id id, dcam_isr_func user_func, void* user_data);
static atomic_t s_isp_users = ATOMIC_INIT(0);
static struct mutex s_isp_lock; /*for the isp driver, protect the isp module; protect only one user open this module*/
static struct proc_dir_entry* isp_proc_file;
static struct isp_device_t *g_isp_dev_ptr = NULL;
uint32_t s_dcam_int_eb = 0x00;
unsigned long s_isp_alloc_addr = 0x00;
uint32_t s_isp_alloc_order = 0x00;
uint32_t s_isp_alloc_len = 0x00;
static DEFINE_SPINLOCK(isp_spin_lock);
static int32_t _isp_set_clk(enum isp_clk_sel clk_sel);
static int32_t _isp_module_eb(void);
static int32_t _isp_module_dis(void);
static int _isp_registerirq(void);
static void _isp_unregisterirq(void);
static irqreturn_t _isp_irq_root(int irq, void *dev_id);
void _dcam_isp_root(void);
static int _isp_queue_init(struct isp_queue *queue);
static int _isp_queue_write(struct isp_queue *queue, struct isp_node *node);
static int _isp_queue_read(struct isp_queue *queue, struct isp_node *node);
static inline void _isp_regread(char *dst, char *src, size_t n);
static inline void _isp_regwrite(char *dst, char *src, size_t n);
static void _read_reg(struct isp_reg_bits *reg_bits_ptr, uint32_t counts);
static void _write_reg(struct isp_reg_bits *reg_bits_ptr, uint32_t counts);
/**file operation functions declare**/
static int32_t _isp_kernel_open(struct inode *node, struct file *filp);
static int32_t _isp_kernel_release(struct inode *node, struct file *filp);
static long _isp_kernel_ioctl( struct file *fl, unsigned int cmd, unsigned long param);
/**driver' functions declare**/
static int32_t _isp_probe(struct platform_device *pdev);
static int32_t _isp_remove(struct platform_device *dev);
/**module' functions declare**/
static int32_t __init isp_kernel_init(void);
static void isp_kernel_exit(void);
static int32_t _isp_lnc_param_load_ex(struct isp_reg_bits *reg_bits_ptr, uint32_t counts);
static int _isp_stop_kernel_thread(void* param);
static struct file_operations isp_fops = {
.owner = THIS_MODULE,
.open = _isp_kernel_open,
.unlocked_ioctl = _isp_kernel_ioctl,
.compat_ioctl = compat_isp_kernel_ioctl,
.release = _isp_kernel_release,
};
static struct miscdevice isp_dev = {
.minor = ISP_MINOR,
.name = "sprd_isp",
.fops = &isp_fops,
};
static const struct of_device_id of_match_table_isp[] = {
{ .compatible = "sprd,sprd_isp", },
{ },
};
static struct platform_driver isp_driver = {
.probe = _isp_probe,
.remove = _isp_remove,
.driver = {
.owner = THIS_MODULE,
.name = "sprd_isp",
.of_match_table = of_match_ptr(of_match_table_isp),
},
};
static int32_t _isp_get_systemtime(struct timeval *tv)
{
struct timespec ts;
ktime_get_ts(&ts);
tv->tv_sec = ts.tv_sec;
tv->tv_usec = ts.tv_nsec / NSEC_PER_USEC;
return 0;
}
static int32_t _isp_module_eb(void)
{
int32_t ret = 0;
if (0x01 == atomic_inc_return(&s_isp_users)) {
ret = clk_mm_i_eb(isp_dev.this_device->of_node,1);
ret = _isp_set_clk(ISP_CLK_312M);
if (unlikely(0 != ret)) {
ISP_PRINT("isp_k: set clock error\n");
ret = -EIO;
}
}
ISP_PRINT("_isp_module_eb: end\n");
return ret;
}
static int32_t _isp_module_dis(void)
{
int32_t ret = 0;
if (0x00 == atomic_dec_return(&s_isp_users)) {
ret = _isp_set_clk(ISP_CLK_NONE);
if (unlikely(0 != ret)) {
ISP_PRINT("isp_k: close clock error\n");
ret = -EFAULT;
return ret;
}
ret = clk_mm_i_eb(isp_dev.this_device->of_node,0);
}
return ret;
}
static int32_t _isp_module_rst(void)
{
int32_t ret = 0;
uint32_t reg_value=0x00;
int32_t time_out_cnt = 0;
if (0x00 != atomic_read(&s_isp_users)) {
ISP_OWR(ISP_AXI_MASTER_STOP, BIT_0);
reg_value=ISP_READL(ISP_AXI_MASTER);
while((0x00==(reg_value&0x08)) && (time_out_cnt < (ISP_TIME_OUT_MAX * 2)))
{
time_out_cnt++;
udelay(50);
reg_value=ISP_READL(ISP_AXI_MASTER);
}
if (time_out_cnt >= ISP_TIME_OUT_MAX) {
ret = -1;
ISP_PRINT("_isp_module_rst: time out\n");
}
ISP_WRITEL(ISP_INT_CLEAR, ISP_IRQ_HW_MASK);
sci_glb_set(ISP_MODULE_RESET, ISP_RST_LOG_BIT);
sci_glb_set(ISP_MODULE_RESET, ISP_RST_CFG_BIT);
sci_glb_set(ISP_MODULE_RESET, ISP_RST_LOG_BIT);
sci_glb_set(ISP_MODULE_RESET, ISP_RST_CFG_BIT);
sci_glb_set(ISP_MODULE_RESET, ISP_RST_LOG_BIT);
sci_glb_set(ISP_MODULE_RESET, ISP_RST_CFG_BIT);
sci_glb_clr(ISP_MODULE_RESET, ISP_RST_CFG_BIT);
sci_glb_clr(ISP_MODULE_RESET, ISP_RST_LOG_BIT);
}
ISP_PRINT("_isp_module_rst: exit\n");
return 0;
}
void* _isp_cap_eof(struct dcam_frame* frame, void* u_data)
{
struct isp_device_t * p_isp = (struct isp_device_t *)u_data;
if (p_isp) {
p_isp->lnc_load.cap_eof_flag = 1;
if (p_isp->lnc_load.load_lnc_flag) {
up(&p_isp->kernel_sem);
p_isp->lnc_load.load_lnc_flag = 0;
} else {
}
}
}
int _isp_kernel_proc(void *arg)
{
struct isp_device_t *dev = (struct isp_device_t*)arg;
int32_t time_out_cnt = 0;
uint32_t reg_value=0x00;
int ret = 0;
struct isp_reg_bits *reg_bits_ptr;
uint32_t counts;
while(1) {
if (0 == down_interruptible(&dev->kernel_sem)) {
if (dev->is_kernel_thread_stop) {
printk("isp_k: _isp_kernel_proc stop \n");
break;
}
reg_bits_ptr = (struct isp_reg_bits*) g_isp_dev_ptr->lnc_load.load_buf;
counts = g_isp_dev_ptr->lnc_load.param_counts;
ret = _isp_lnc_param_load_ex(reg_bits_ptr, counts);
memset((void *)g_isp_dev_ptr->lnc_load.load_buf, 0x00, ISP_BUF_MAX_SIZE);
}
}
dev->is_kernel_thread_stop = 0;
return 0;
}
int _isp_kernel_thread(void* param)
{
struct isp_device_t *dev = (struct isp_device_t*)param;
if (dev == NULL) {
printk("isp_k: _isp_kernel_thread, dev is NULL \n");
return -1;
}
dev->lnc_load.load_lnc_flag = 0;
dev->lnc_load.cap_eof_flag = 0;
dev->is_kernel_thread_stop = 0;
sema_init(&dev->kernel_sem, 0);
sema_init(&dev->lnc_load.load_done_sem, 0);
dev->kernel_thread = kthread_run(_isp_kernel_proc, param, "isp_kernel_proc_thread");
if (IS_ERR(dev->kernel_thread)) {
printk("isp_k: _isp_kernel_thread error!\n");
return -1;
}
return 0;
}
static int _isp_stop_kernel_thread(void* param)
{
struct isp_device_t *dev = (struct isp_device_t*)param;
int cnt = 0;
if (dev == NULL) {
printk("SPRD_IMG: sprd_img_opt_flash, dev is NULL \n");
return -1;
}
if (dev->kernel_thread) {
dev->is_kernel_thread_stop = 1;
up(&dev->kernel_sem);
if (0 != dev->is_kernel_thread_stop) {
while (cnt < 500) {
cnt++;
if (0 == dev->is_kernel_thread_stop)
break;
msleep(1);
}
}
dev->kernel_thread = NULL;
}
return 0;
}
static int32_t _isp_lnc_param_load_ex(struct isp_reg_bits *reg_bits_ptr, uint32_t counts)
{
int32_t ret = 0;
int32_t time_out_cnt = 0;
volatile uint32_t reg_value=0x00;
if((0x00!=s_isp_alloc_addr)
&&(0x00!=s_isp_alloc_len)) {
void *ptr = (void*)s_isp_alloc_addr;
uint32_t len=s_isp_alloc_len;
#ifndef CONFIG_64BIT
dmac_flush_range(ptr, ptr + len);
outer_flush_range(__pa(ptr), __pa(ptr) + len);
#endif
reg_bits_ptr->reg_value=(uint32_t)__pa(reg_bits_ptr->reg_value);
//#if defined(CONFIG_MACH_CORE3)
reg_value=ISP_READL(ISP_LNC_STATUS);
while((0x00==(reg_value&ISP_LNC_STATUS_OK)) && (time_out_cnt < (ISP_TIME_OUT_MAX*1000))) {
udelay(1);
reg_value=ISP_READL(ISP_LNC_STATUS);
time_out_cnt++;
}
if (time_out_cnt >= (ISP_TIME_OUT_MAX*1000)) {
ret = -1;
ISP_PRINT("isp_k: isp lnc status time out\n");
}
ISP_OWR(ISP_INT_EN, ISP_INT_LENS_LOAD);
_write_reg(reg_bits_ptr, counts);
}else {
ISP_PRINT("isp_k: isp load lnc param error\n");
}
return ret;
}
static int32_t _isp_lnc_param_load(struct isp_reg_bits *reg_bits_ptr, uint32_t counts)
{
int32_t ret = 0;
int32_t time_out_cnt = 0;
uint32_t reg_value=0x00;
printk("isp_k: _isp_lnc_param_load \n");
if((0x00!=s_isp_alloc_addr)
&&(0x00!=s_isp_alloc_len)) {
void *ptr = (void*)s_isp_alloc_addr;
uint32_t len=s_isp_alloc_len;
#ifndef CONFIG_64BIT
dmac_flush_range(ptr, ptr + len);
outer_flush_range(__pa(ptr), __pa(ptr) + len);
#endif
reg_bits_ptr->reg_value=(uint32_t)__pa(reg_bits_ptr->reg_value);
{
unsigned long flags;
reg_value=ISP_READL(ISP_LNC_STATUS);
while((0x00==(reg_value&ISP_LNC_STATUS_OK)) && (time_out_cnt < (ISP_TIME_OUT_MAX*1000))) {
udelay(1);
reg_value=ISP_READL(ISP_LNC_STATUS);
time_out_cnt++;
}
if (time_out_cnt >= (ISP_TIME_OUT_MAX*1000)) {
ret = -1;
ISP_PRINT("isp_k: isp lnc status time out\n");
}
_write_reg(reg_bits_ptr, counts);
printk("isp_k: load, 0x%x: 0x%x, 0x%x, counts %d \n", reg_bits_ptr, reg_bits_ptr->reg_addr, reg_bits_ptr->reg_value, counts);
}
time_out_cnt = 0;
reg_value=ISP_READL(ISP_INT_RAW);
while ((0x00 == (reg_value&ISP_INT_LENS_LOAD)) && (time_out_cnt < ISP_TIME_OUT_MAX)) {
udelay(1);
reg_value=ISP_READL(ISP_INT_RAW);
time_out_cnt++;
}
if (time_out_cnt >= ISP_TIME_OUT_MAX) {
ret = -1;
ISP_PRINT("isp_k: isp load lnc param time out\n");
}
ISP_OWR(ISP_INT_CLEAR, ISP_INT_LENS_LOAD);
}else {
ISP_PRINT("isp_k: isp load lnc param error\n");
}
return ret;
}
static int32_t _isp_lnc_param_set(uint32_t* addr, uint32_t len)
{
int32_t ret = 0;
if((0x00!=s_isp_alloc_addr)
&&(0x00!=addr)) {
memcpy((void*)s_isp_alloc_addr, (void*)addr, len);
}
return ret;
}
static int32_t _isp_free(void)
{
int32_t ret = 0;
if((0x00!=s_isp_alloc_addr)
&&(0x00!=s_isp_alloc_order)) {
free_pages(s_isp_alloc_addr, s_isp_alloc_order);
s_isp_alloc_order = 0x00;
s_isp_alloc_addr = 0x00;
s_isp_alloc_len=0x00;
}
return ret;
}
static int32_t _isp_alloc(unsigned long* addr, uint32_t len)
{
int32_t ret = 0x00;
unsigned long buf=0x00;
void *ptr = 0x00;
if((0x00!=s_isp_alloc_addr)
&&(len<=s_isp_alloc_len)) {
*addr = s_isp_alloc_addr;
} else {
_isp_free();
}
if(0x00==s_isp_alloc_addr) {
s_isp_alloc_len=len;
s_isp_alloc_order = get_order(len);
s_isp_alloc_addr = __get_free_pages(GFP_KERNEL | __GFP_COMP, s_isp_alloc_order);
if (NULL == (void*)s_isp_alloc_addr) {
ISP_PRINT("ISP_RAW:_isp_alloc order:0x%x, addr:0x%lx, len:0x%x error\n", s_isp_alloc_order, s_isp_alloc_addr, s_isp_alloc_len);
return 1;
}
ptr = (void*)s_isp_alloc_addr;
*addr = s_isp_alloc_addr;
buf = virt_to_phys((volatile void *)s_isp_alloc_addr);
#ifndef CONFIG_64BIT
dmac_flush_range(ptr, ptr + len);
outer_flush_range(__pa(ptr), __pa(ptr) + len);
#endif
}
return ret;
}
static int32_t _isp_set_clk(enum isp_clk_sel clk_sel)
{
struct clk *clk_parent;
char *parent = "clk_256m";
int32_t rtn = 0;
ISP_CHECK_ZERO(g_isp_dev_ptr);
#ifdef CONFIG_SC_FPGA
return 0;
#endif
switch (clk_sel) {
case ISP_CLK_312M:
parent = "clk_312m";
break;
case ISP_CLK_256M:
parent = "clk_256m";
break;
case ISP_CLK_128M:
parent = "clk_128m";
break;
case ISP_CLK_48M:
parent = "clk_48m";
break;
case ISP_CLK_76M8:
parent = "clk_76p8m";
break;
case ISP_CLK_NONE:
ISP_PRINT("isp_k: ISP close CLK %d \n", (int)clk_get_rate(g_isp_dev_ptr->s_isp_clk));
if (g_isp_dev_ptr->s_isp_clk) {
clk_disable(g_isp_dev_ptr->s_isp_clk);
clk_put(g_isp_dev_ptr->s_isp_clk);
g_isp_dev_ptr->s_isp_clk = NULL;
}
return 0;
default:
parent = "clk_128m";
break;
}
if (NULL == g_isp_dev_ptr->s_isp_clk) {
g_isp_dev_ptr->s_isp_clk = parse_clk(isp_dev.this_device->of_node, "clk_isp");
if (IS_ERR(g_isp_dev_ptr->s_isp_clk)) {
ISP_PRINT("isp_k: parse_clk fail, %p \n", (int)g_isp_dev_ptr->s_isp_clk);
return -1;
} else {
ISP_PRINT("isp_k: get clk_parent ok \n");
}
} else {
clk_disable(g_isp_dev_ptr->s_isp_clk);
}
clk_parent = clk_get(NULL, parent);
if (IS_ERR(clk_parent)) {
ISP_PRINT("isp_k: dcam_set_clk fail, %p \n", clk_parent);
return -1;
} else {
ISP_PRINT("isp_k: get clk_parent ok \n");
}
rtn = clk_set_parent(g_isp_dev_ptr->s_isp_clk, clk_parent);
if(rtn){
ISP_PRINT("isp_k: clk_set_parent fail, %d \n", rtn);
}
rtn = clk_enable(g_isp_dev_ptr->s_isp_clk);
if (rtn) {
ISP_PRINT("isp_k: enable isp clk error.\n");
return -1;
}
return rtn;
}
static int _isp_queue_init(struct isp_queue *queue)
{
if (NULL == queue)
return -EINVAL;
memset(queue, 0x00, sizeof(*queue));
queue->write = &queue->node[0];
queue->read = &queue->node[0];
return 0;
}
static int _isp_queue_write(struct isp_queue *queue, struct isp_node *node)
{
struct isp_node *ori_node;
if (NULL == queue || NULL == node)
return -EINVAL;
ori_node = queue->write;
//ISP_PRINT("_isp_queue_write called!\n");
*queue->write++ = *node;
if (queue->write > &queue->node[ISP_QUEUE_LENGTH-1]) {
queue->write = &queue->node[0];
}
if (queue->write == queue->read) {
queue->write = ori_node;
}
//ISP_PRINT("_isp_queue_write finished!\n");
return 0;
}
static int _isp_queue_read(struct isp_queue *queue, struct isp_node *node)
{
int ret = 0;
if (NULL == queue || NULL == node)
return -EINVAL;
//ISP_PRINT("_isp_queue_read called!\n");
if (queue->read != queue->write) {
*node = *queue->read++;
if (queue->read > &queue->node[ISP_QUEUE_LENGTH-1]) {
queue->read = &queue->node[0];
}
}
//ISP_PRINT("_isp_queue_read finished!\n");
return ret;
}
static inline void _isp_regread(char *dst, char *src, size_t n)
{
char tmp = 0;
uint32_t tmp2 = 0;
char *char_src = 0,*d = 0;
uint32_t *d2 = (uint32_t*) dst;
uint32_t *int_src = (uint32_t*) src;
uint32_t counts = (n>>2)<<2;
uint32_t res_counts = n -counts;
counts = counts>>2;
while (counts--) {
tmp2 = ISP_READL(int_src);
*d2++ = tmp2;
int_src++;
}
if(res_counts) {
d = (char*) d2;
char_src = (char*) int_src;
while(res_counts--) {
tmp = __raw_readb(char_src);
*d = tmp;
char_src++;
}
}
}
static inline void _isp_regwrite(char *dst, char *src, size_t n)
{
uint32_t tmp2 = 0;
char *char_src = 0, *d = 0;
uint32_t *int_src = 0, *d2 = 0;
uint32_t counts = 0, res_counts = 0;
int_src = (uint32_t*) src;
d2 = (uint32_t*) dst;
counts = (n>>2)<<2;
res_counts = n - counts;
counts = counts>>2;
while (counts--) {
tmp2 = *int_src++;
ISP_WRITEL(d2, tmp2);
d2++;
}
if(res_counts) {
d = (char*) d2;
char_src = (char*) int_src;
while(res_counts--) {
tmp2 = *char_src++;
__raw_writeb(tmp2, d);
d++;
}
}
}
static int32_t _isp_get_ctlr(void *param)
{
struct isp_device_t *dev_ptr = (struct isp_device_t *) param;
down(&dev_ptr->sem_isp);
return 0;
}
static int32_t _isp_put_ctlr(void *param)
{
struct isp_device_t *dev_ptr = (struct isp_device_t *) param;
up(&dev_ptr->sem_isp);
return 0;
}
static int _isp_en_irq(unsigned long int_num)
{
uint32_t ret = 0;
ISP_WRITEL(ISP_INT_CLEAR, ISP_IRQ_HW_MASK);
ISP_WRITEL(ISP_INT_EN, int_num);
return ret;
}
static int _isp_registerirq(void)
{
uint32_t ret = 0;
ret = request_irq(ISP_IRQ, _isp_irq_root, IRQF_SHARED, "ISP", &g_isp_irq);
return ret;
}
static void _isp_unregisterirq(void)
{
free_irq (ISP_IRQ, &g_isp_irq);
}
static int _isp_cfg_dcam_int(uint32_t param)
{
uint32_t ret = 0;
s_dcam_int_eb = param;
return ret;
}
static void _read_reg(struct isp_reg_bits *reg_bits_ptr, uint32_t counts)
{
uint32_t i = 0;
unsigned long reg_val = 0;
unsigned long reg_addr = 0;
for (i = 0; i<counts; ++i) {
//reg_addr = reg_bits_ptr[i].reg_addr;
reg_addr = ISP_BASE_ADDR + reg_bits_ptr[i].reg_addr;
reg_val = ISP_READL(reg_addr);
reg_bits_ptr[i].reg_value = reg_val;
}
}
static void _write_reg(struct isp_reg_bits *reg_bits_ptr, uint32_t counts)
{
uint32_t i = 0;
unsigned long reg_val = 0;
unsigned long reg_addr = 0;
for (i = 0; i<counts; ++i) {
reg_addr = reg_bits_ptr[i].reg_addr+ISP_BASE_ADDR;
reg_val = reg_bits_ptr[i].reg_value;
ISP_WRITEL(reg_addr, reg_val);
}
}
/**********************************************************
*open the device
*
***********************************************************/
static int32_t _isp_kernel_open (struct inode *node, struct file *pf)
{
int32_t ret = 0;
ISP_PRINT ("isp_k: open start \n");
ISP_CHECK_ZERO(g_isp_dev_ptr);
ret = _isp_get_ctlr(g_isp_dev_ptr);
if (unlikely(ret)) {
ISP_PRINT ("isp_k: get control error \n");
ret = -EFAULT;
return ret;
}
g_isp_dev_ptr->reg_base_addr = (unsigned long)ISP_BASE_ADDR;
g_isp_dev_ptr->size = ISP_REG_MAX_SIZE;
ret = _isp_queue_init(&(g_isp_dev_ptr->queue));
if (unlikely(0 != ret)) {
ISP_PRINT("isp_k: queue init error\n");
ret = -EIO;
goto ISP_K_OPEN_ERROR_EXIT;
}
ret = _isp_module_eb();
if (unlikely(0 != ret)) {
ISP_PRINT("isp_k: enable isp module error\n");
ret = -EIO;
goto ISP_K_OPEN_ERROR_EXIT;
}
ret = _isp_module_rst();
if (unlikely(0 != ret)) {
ISP_PRINT("isp_k: reset isp module error \n");
ret = -EIO;
_isp_module_dis();
goto ISP_K_OPEN_ERROR_EXIT;
}
dcam_reg_isr(DCAM_CAP_EOF, _isp_cap_eof, g_isp_dev_ptr);
ret = _isp_kernel_thread(g_isp_dev_ptr);
if (unlikely(0 != ret)) {
ISP_PRINT("isp_k: create isp_kernel_thread fail \n");
ret = -EIO;
_isp_module_dis();
goto ISP_K_OPEN_ERROR_EXIT;
}
ret = _isp_registerirq();
if (unlikely(0 != ret)) {
ISP_PRINT("isp_k: reg irq fail \n");
ret = -EIO;
_isp_stop_kernel_thread(g_isp_dev_ptr);
_isp_module_dis();
goto ISP_K_OPEN_ERROR_EXIT;
}
ISP_PRINT ("isp_k: open end \n");
return ret;
ISP_K_OPEN_ERROR_EXIT:
ret = _isp_put_ctlr(g_isp_dev_ptr);
if (unlikely(ret)) {
ISP_PRINT ("isp_k: get control error \n");
ret = -EFAULT;
}
ISP_PRINT ("isp_k: open error \n");
return -EIO;
}
static irqreturn_t _isp_irq_root(int irq, void *dev_id)
{
int32_t ret = 0;
uint32_t status = 0;
uint32_t irq_line = 0;
uint32_t irq_status = 0;
unsigned long flag = 0;
int32_t i = 0;
struct isp_node node = { 0 };
struct timeval system_time;
status = ISP_REG_RD(ISP_INT_STATUS);
irq_line = status&ISP_IRQ_HW_MASK;
//ISP_PRINT("ISP_RAW:isp_k: isp irq: 0x%x\n", irq_line);
if ( 0 == irq_line ) {
return IRQ_NONE;
}
spin_lock_irqsave(&isp_spin_lock,flag);
if ((ISP_INT_LENS_LOAD & irq_line) == ISP_INT_LENS_LOAD) {
irq_line &= ~ISP_INT_LENS_LOAD;
ISP_OWR(ISP_INT_CLEAR, ISP_INT_LENS_LOAD);
up(&g_isp_dev_ptr->lnc_load.load_done_sem);
if (!irq_line) {
spin_unlock_irqrestore(&isp_spin_lock, flag);
return IRQ_HANDLED;
}
}
for (i = ISP_IRQ_NUM- 1; i >= 0; i--) {
if (irq_line & (1 << (uint32_t)i)) {
irq_status |= 1 << (uint32_t)i;
}
irq_line &= ~(uint32_t)(1 << (uint32_t)i); //clear the interrupt flag
if(!irq_line) //no interrupt source left
break;
}
ISP_WRITEL(ISP_INT_CLEAR, irq_status);
node.isp_irq_val = irq_status;
_isp_get_systemtime(&system_time);
system_time.tv_sec = system_time.tv_sec * 1000000;
node.system_time = (uint64_t)(system_time.tv_sec+system_time.tv_usec);
ret = _isp_queue_write((struct isp_queue *)&g_isp_dev_ptr->queue, (struct isp_node*)&node);
spin_unlock_irqrestore(&isp_spin_lock, flag);
up(&g_isp_dev_ptr->sem_isr);
return IRQ_HANDLED;
}
//static irqreturn_t _dcam_irq_root(int irq, void *dev_id)
void _dcam_isp_root(void)
{
int32_t ret = 0;
unsigned long flag = 0;
struct isp_node node = { 0 };
struct timeval system_time;
//ISP_PRINT ("ISP_RAW: isp_k: _dcam_isp_root %d \n", s_dcam_int_eb);
if(0x00 !=s_dcam_int_eb)
{
spin_lock_irqsave(&isp_spin_lock,flag);
#if defined(CONFIG_MACH_CORE3)
node.dcam_irq_val = ISP_INT_FETCH_EOF;
#else
node.dcam_irq_val = ISP_INT_FETCH_SOF;
#endif
//ISP_PRINT("isp_k: dcam sof irq :0x%x\n", node.dcam_irq_val);
_isp_get_systemtime(&system_time);
system_time.tv_sec = system_time.tv_sec * 1000000;
node.system_time = (uint64_t)(system_time.tv_sec+system_time.tv_usec);
ret = _isp_queue_write((struct isp_queue *)&g_isp_dev_ptr->queue, (struct isp_node*)&node);
spin_unlock_irqrestore(&isp_spin_lock, flag);
up(&g_isp_dev_ptr->sem_isr);
}
//return IRQ_HANDLED;
}
/**********************************************************
*release the device
*
***********************************************************/
static int32_t _isp_kernel_release (struct inode *node, struct file *pf)
{
int ret = 0;
ISP_PRINT ("isp_k: release start \n");
mutex_lock(&s_isp_lock);
_isp_unregisterirq();
ret = _isp_module_dis();
mutex_unlock(&s_isp_lock);
_isp_stop_kernel_thread(g_isp_dev_ptr);
ISP_CHECK_ZERO(g_isp_dev_ptr);
ret = _isp_put_ctlr(g_isp_dev_ptr);
if (unlikely (ret) ) {
ISP_PRINT ("isp_k: release control error \n");
return -EFAULT;
}
ISP_PRINT ("isp_k: release end \n");
return ret;
}
/**********************************************************
*read info from file
*size_t size:
*loff_t p:
***********************************************************/
#if 0
static int32_t _isp_kernel_proc_read (char *page, char **start, off_t off, int count, int *eof, void *data)
{
int len = 0;
uint32_t reg_buf_len = 200;
uint32_t print_len = 0, print_cnt = 0;
uint32_t *reg_ptr = 0;
ISP_PRINT ("isp_k: _isp_kernel_proc_read 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x \n", (uint32_t)page, (uint32_t)start, (uint32_t)off, (uint32_t)count, (uint32_t)eof, (uint32_t)data);
(void)start; (void)off; (void)count; (void)eof;
if(0x00==g_isp_device.reg_base_addr)
{
return 0x00;
}
reg_ptr = (uint32_t*)g_isp_device.reg_base_addr;
len += sprintf(page + len, "Context for ISP device \n");
len += sprintf(page + len, "********************************************* \n");
while (print_len < reg_buf_len) {
len += sprintf(page + len, "offset 0x%x : 0x%x, 0x%x, 0x%x, 0x%x \n",
print_len,
reg_ptr[print_cnt],
reg_ptr[print_cnt+1],
reg_ptr[print_cnt+2],
reg_ptr[print_cnt+3]);
print_cnt += 4;
print_len += 16;
}
len += sprintf(page + len, "********************************************* \n");
len += sprintf(page + len, "The end of ISP device \n");
return len;
}
#endif
/**********************************************************
*the io controller of isp
*unsigned int cmd:
*unsigned long param:
***********************************************************/
static long _isp_kernel_ioctl( struct file *fl, unsigned int cmd, unsigned long param)
{
long ret = 0;
uint32_t isp_irq, dcam_irq;
struct isp_irq_param irq_param = { 0 };
struct isp_node isp_node = { 0 };
struct isp_reg_param reg_param = { 0 };
struct isp_reg_bits *reg_bits_ptr = 0;
//ISP_PRINT("isp_k:_ioctl called, cmd: %x\n", cmd);
if (!fl) {
return -EINVAL;
}
ISP_CHECK_ZERO(g_isp_dev_ptr);
if(ISP_IO_IRQ==cmd)
{
ret = down_interruptible(&g_isp_dev_ptr->sem_isr);
if (ret) {
ISP_PRINT("isp_k: ioctl irq: down failed ret = %ld",ret);
memset(&irq_param, 0, sizeof(irq_param));
irq_param.ret_val = ret;
ret = copy_to_user ((void*) param, (void*)&irq_param, sizeof(irq_param));
if ( 0 != ret) {
ISP_PRINT("isp_k: ioctl irq: copy_to_user failed ret = %ld", ret);
}
ret = -ERESTARTSYS;
goto ISP_IOCTL_EXIT;
}
ret=_isp_queue_read(&g_isp_dev_ptr->queue, &isp_node);
if ( 0 != ret) {
ISP_PRINT("isp_k: ioctl irq: _isp_queue_read error, ret = 0x%x", (uint32_t)ret);
ret = -EFAULT;
goto ISP_IOCTL_EXIT;
}
irq_param.dcam_irq_val = isp_node.dcam_irq_val;
irq_param.isp_irq_val = isp_node.isp_irq_val;
isp_irq = isp_node.isp_irq_val;
dcam_irq = isp_node.dcam_irq_val;
irq_param.irq_val = dcam_irq|isp_irq;
irq_param.system_time = isp_node.system_time;
ret = copy_to_user ((void*) param, (void*)&irq_param, sizeof(struct isp_irq_param));
if ( 0 != ret) {
ISP_PRINT("isp_k: ioctl irq: copy_to_user error, ret = 0x%x", (uint32_t)ret);
ret = -EFAULT;
}
} else {
mutex_lock(&s_isp_lock);
switch (cmd)
{
case ISP_IO_READ: {
uint32_t buf_size = 0;
//ISP_PRINT(" isp_k:_ioctl read called \n");
ret = copy_from_user((void*)®_param, (void*)param, sizeof(struct isp_reg_param));
if ( 0 != ret) {
ISP_PRINT("isp_k: ioctl read: copy_to_user error, ret = 0x%x", (uint32_t)ret);
ret = -EFAULT;
goto IO_READ_EXIT;
}
buf_size = reg_param.counts*sizeof(struct isp_reg_bits);
if (buf_size > g_isp_dev_ptr->buf_len) {
ret =-EFAULT;
goto IO_READ_EXIT;
}
reg_bits_ptr = (struct isp_reg_bits*) g_isp_dev_ptr->buf_addr;
ret = copy_from_user((void*)reg_bits_ptr, (void*)reg_param.reg_param, buf_size);
if ( 0 != ret) {
ISP_PRINT("isp_k: ioctl read: copy_to_user error, ret = 0x%x", (uint32_t)ret);
ret =-EFAULT;
goto IO_READ_EXIT;
}
_read_reg(reg_bits_ptr, reg_param.counts);
ret = copy_to_user((void*)reg_param.reg_param, (void*)reg_bits_ptr, buf_size);
if ( 0 != ret) {
ISP_PRINT("isp_k: ioctl read: copy_to_user error, ret = 0x%x", (uint32_t)ret);
ret = -EFAULT;
goto IO_READ_EXIT;
}
IO_READ_EXIT:
if(reg_bits_ptr) {
memset((void *)g_isp_dev_ptr->buf_addr, 0x00, buf_size);
reg_bits_ptr = NULL;
}
}
break;
case ISP_IO_WRITE: {
uint32_t buf_size = 0;
//ISP_PRINT(" isp_k:_ioctl write called \n");
ret = copy_from_user((void*)®_param, (void*)param, sizeof(struct isp_reg_param));
if ( 0 != ret) {
ISP_PRINT("isp_k: ioctl write: copy_to_user error, ret = 0x%x", (uint32_t)ret);
ret = -EFAULT;
goto IO_WRITE_EXIT;
}
buf_size = reg_param.counts*sizeof(struct isp_reg_bits);
if(buf_size > g_isp_dev_ptr->buf_len)
{
ret = -EFAULT;
goto IO_WRITE_EXIT;
}
reg_bits_ptr = (struct isp_reg_bits*) g_isp_dev_ptr->buf_addr;
ret = copy_from_user((void*)reg_bits_ptr, (void*)reg_param.reg_param, buf_size);
if ( 0 != ret) {
ISP_PRINT("isp_k: ioctl write: copy_to_user error, ret = 0x%x", (uint32_t)ret);
ret = -EFAULT;
goto IO_WRITE_EXIT;
}
_write_reg(reg_bits_ptr, reg_param.counts);
IO_WRITE_EXIT:
if(reg_bits_ptr) {
memset((void *)g_isp_dev_ptr->buf_addr, 0x00, buf_size);
reg_bits_ptr = NULL;
}
}
break;
case ISP_IO_RST: {
ISP_PRINT(" isp_k:ioctl restet start \n");
g_isp_dev_ptr->lnc_load.cap_eof_flag = 0;
ret = _isp_module_rst();
if (ret) {
ISP_PRINT("isp_k: ioctl restet error!\n");
ret = -EFAULT;
}
}
break;
case ISP_IO_SETCLK: {
ISP_PRINT(" isp_k:ioctl set clock start \n");
}
break;
case ISP_IO_STOP: {
unsigned long flag = 0;
struct isp_node node = { 0 };
ret = _isp_en_irq(0);//dis-enable the interrupt
ISP_PRINT("isp_k: ioctl stop start !\n");
spin_lock_irqsave(&isp_spin_lock,flag);
node.dcam_irq_val = ISP_INT_STOP;
ret = _isp_queue_write((struct isp_queue *)&g_isp_dev_ptr->queue, (struct isp_node*)&node);
spin_unlock_irqrestore(&isp_spin_lock, flag);
up(&g_isp_dev_ptr->sem_isr);
}
break;
case ISP_IO_INT: {
unsigned long int_num;
ret = copy_from_user((void*)&int_num, (void*)param, 0x04);
if (ret) {
ISP_PRINT ("isp_k:io int copy param error, ret = %d \n", (uint32_t)ret);
ret = -EFAULT;
goto ISP_IOCTL_LOCKED_CMD_EXIT;
}
ret = _isp_en_irq(int_num);
//ret = _isp_registerirq();
if (unlikely(ret)) {
ISP_PRINT ("isp_k:enable interrupt error \n");
ret = -EFAULT;
}
}
break;
case ISP_IO_DCAM_INT: {
unsigned long int_param;
ret = copy_from_user((void*)&int_param, (void*)param, 0x04);
if (ret) {
ISP_PRINT ("isp_k:dcam int copy params error, ret = %d \n", (uint32_t)ret);
ret = -EFAULT;
goto ISP_IOCTL_LOCKED_CMD_EXIT;
}
ret = _isp_cfg_dcam_int(int_param);
if (unlikely(ret)) {
ISP_PRINT ("isp_k:cfg dcam interrupt error \n");
ret = -EFAULT;
}
}
break;
case ISP_IO_LNC_PARAM: {
uint32_t buf_size = 0;
uint32_t* addr = 0;
ret = copy_from_user((void*)®_param, (void*)param, sizeof(struct isp_reg_param));
if ( 0 != ret) {
ISP_PRINT("isp_k: ioctl lnc param: copy_to_user error, ret = 0x%x", (uint32_t)ret);
ret = -EFAULT;
goto IO_LNC_PARAM_EXIT;
}
buf_size = reg_param.counts;
if(buf_size > g_isp_dev_ptr->buf_len){
ret = -EFAULT;
goto IO_LNC_PARAM_EXIT;
}
addr = (uint32_t*) g_isp_dev_ptr->buf_addr;
ret = copy_from_user((void*)addr, (void*)reg_param.reg_param, buf_size);
if ( 0 != ret) {
ISP_PRINT("isp_k: ioctl lnc param: copy_to_user error, ret = 0x%x", (uint32_t)ret);
ret = -EFAULT;
goto IO_LNC_PARAM_EXIT;
}
ret = _isp_lnc_param_set(addr, buf_size);
if ( 0 != ret) {
ISP_PRINT("isp_k: ioctl lnc param error, ret = 0x%x", (uint32_t)ret);
ret = -EFAULT;
goto IO_LNC_PARAM_EXIT;
}
IO_LNC_PARAM_EXIT:
if(addr) {
memset((void *)g_isp_dev_ptr->buf_addr, 0x00, buf_size);
addr = NULL;
}
}
break;
case ISP_IO_LNC: {
uint32_t buf_size = 0;
ret = copy_from_user((void*)®_param, (void*)param, sizeof(struct isp_reg_param));
if ( 0 != ret) {
ISP_PRINT("isp_k: ioctl lnc: copy_to_user error, ret = 0x%x", (uint32_t)ret);
ret = -EFAULT;
goto IO_LNC_EXIT;
}
buf_size = reg_param.counts*sizeof(struct isp_reg_bits);
if(buf_size > g_isp_dev_ptr->buf_len)
{
ret = -EFAULT;
ISP_PRINT("isp_k: isp_io_lnc: buf len failed\n");
goto IO_LNC_EXIT;
}
reg_bits_ptr = (struct isp_reg_bits*) g_isp_dev_ptr->buf_addr;
ret = copy_from_user((void*)reg_bits_ptr, (void*)reg_param.reg_param, buf_size);
if ( 0 != ret) {
ISP_PRINT("isp_k: ioctl lnc: copy_to_user error, ret = 0x%x", (uint32_t)ret);
ret = -EFAULT;
goto IO_LNC_EXIT;
}
if (g_isp_dev_ptr->lnc_load.cap_eof_flag == 0) {
ret = _isp_lnc_param_load(reg_bits_ptr, reg_param.counts);
} else {
memcpy((void *)g_isp_dev_ptr->lnc_load.load_buf, (void *)reg_bits_ptr, buf_size);
g_isp_dev_ptr->lnc_load.param_counts = reg_param.counts;
g_isp_dev_ptr->lnc_load.load_lnc_flag = 1;
ret = down_timeout(&g_isp_dev_ptr->lnc_load.load_done_sem, msecs_to_jiffies(500));
}
if (unlikely(ret)) {
ISP_PRINT ("isp_k:load lnc error \n");
ret = -EFAULT;
}
IO_LNC_EXIT:
if(reg_bits_ptr) {
memset((void *)g_isp_dev_ptr->buf_addr, 0x00, buf_size);
reg_bits_ptr = NULL;
}
}
break;
case ISP_IO_ALLOC: {
ret = copy_from_user((void*)®_param, (void*)param, sizeof(struct isp_reg_param));
if ( 0 != ret) {
ISP_PRINT("isp_k: ioctl write: copy_to_user error, ret = 0x%x", (uint32_t)ret);
ret = -EFAULT;
break;
}
ret = _isp_alloc(®_param.reg_param, reg_param.counts);
if ( 0 == ret) {
ret = copy_to_user((void*)param, (void*)®_param, sizeof(struct isp_reg_param));
} else {
ISP_PRINT("isp_k: ioctl alloc error, ret = 0x%x", (uint32_t)ret);
ret = -EFAULT;
}
}
break;
default:
mutex_unlock(&s_isp_lock);
ISP_PRINT("isp_k:_ioctl cmd is unsupported, cmd = %x\n", (int32_t)cmd);
return -EFAULT;
}
ISP_IOCTL_LOCKED_CMD_EXIT:
mutex_unlock(&s_isp_lock);
}
//ISP_PRINT("isp_k:_ioctl finished\n");
ISP_IOCTL_EXIT:
return ret;
}
static int _isp_probe(struct platform_device *pdev)
{
int ret = 0;
ISP_PRINT ("isp_k:probe start\n");
ret = misc_register(&isp_dev);
if (ret) {
ISP_PRINT ( "isp_k:probe cannot register miscdev on minor=%d (%d)\n",(int32_t)ISP_MINOR, (int32_t)ret);
return ret;
}
mutex_init(&s_isp_lock);
/*
isp_proc_file = create_proc_read_entry("driver/sprd_isp" ,
0444,
NULL,
_isp_kernel_proc_read,
NULL);
if (unlikely(NULL == isp_proc_file)) {
ISP_PRINT("isp_k:probe Can't create an entry for isp in /proc \n");
ret = ENOMEM;
return ret;
}
*/
isp_dev.this_device->of_node = pdev->dev.of_node;
parse_baseaddress(pdev->dev.of_node);
ISP_PRINT (" isp_k:probe end\n");
return 0;
}
static int _isp_remove(struct platform_device * dev)
{
ISP_PRINT ("isp_k: remove start \n");
misc_deregister(&isp_dev);
if (isp_proc_file) {
remove_proc_entry("driver/sprd_isp", NULL);
}
ISP_PRINT ("isp_k: remove end !\n");
return 0;
}
static int32_t __init isp_kernel_init(void)
{
int32_t ret = 0;
unsigned long addr = 0;
ISP_PRINT ("isp_k: init start \n");
if (platform_driver_register(&isp_driver) != 0) {
ISP_PRINT ("isp_kernel_init: platform device register error \n");
return -1;
}
g_isp_dev_ptr = (struct isp_device_t*)vzalloc(sizeof(struct isp_device_t));
ISP_CHECK_ZERO(g_isp_dev_ptr);
init_MUTEX(&g_isp_dev_ptr->sem_isp);
init_MUTEX_LOCKED(&g_isp_dev_ptr->sem_isr); /*for interrupt */
g_isp_dev_ptr->s_isp_clk = NULL;
g_isp_dev_ptr->s_isp_clk_mm_i = NULL;
g_isp_dev_ptr->buf_addr = 0;
g_isp_dev_ptr->buf_len = 0;
g_isp_dev_ptr->buf_addr = (unsigned long)vzalloc(ISP_BUF_MAX_SIZE);
if (0 == g_isp_dev_ptr->buf_addr) {
ret = -1;
ISP_PRINT ("isp_kernel_init 1: alloc error \n");
goto ISP_K_INIT_EXIT;
}
g_isp_dev_ptr->buf_len = ISP_BUF_MAX_SIZE;
g_isp_dev_ptr->lnc_load.load_buf = (unsigned long)vzalloc(ISP_BUF_MAX_SIZE);
if (0 == g_isp_dev_ptr->lnc_load.load_buf) {
ret = -1;
ISP_PRINT ("isp_kernel_init 3: alloc error \n");
goto ISP_K_INIT_EXIT;
}
ret = _isp_alloc(&addr, ISP_BUF_MAX_SIZE);
if (ret) {
ret = -1;
ISP_PRINT ("isp_kernel_init 2: alloc error \n");
goto ISP_K_INIT_EXIT;
}
ISP_PRINT ("isp_k: init end\n");
return ret;
ISP_K_INIT_EXIT:
_isp_free();
if (g_isp_dev_ptr->buf_addr) {
vfree((void *)g_isp_dev_ptr->buf_addr);
g_isp_dev_ptr->buf_addr = 0;
g_isp_dev_ptr->buf_len = 0;
}
if (g_isp_dev_ptr->lnc_load.load_buf) {
vfree((void *)g_isp_dev_ptr->lnc_load.load_buf);
g_isp_dev_ptr->lnc_load.load_buf = 0;
}
if (g_isp_dev_ptr) {
vfree(g_isp_dev_ptr);
g_isp_dev_ptr = 0;
}
return ret;
}
static void isp_kernel_exit(void)
{
ISP_PRINT ("isp_k: exit start \n");
platform_driver_unregister(&isp_driver);
mutex_destroy(&s_isp_lock);
_isp_free();
ISP_CHECK_ZERO_VOID(g_isp_dev_ptr);
if (g_isp_dev_ptr->buf_addr) {
vfree((void *)g_isp_dev_ptr->buf_addr);
g_isp_dev_ptr->buf_addr = 0;
g_isp_dev_ptr->buf_len = 0;
}
if(g_isp_dev_ptr->lnc_load.load_buf) {
vfree((void *)g_isp_dev_ptr->lnc_load.load_buf);
g_isp_dev_ptr->lnc_load.load_buf = 0;
}
vfree(g_isp_dev_ptr);
g_isp_dev_ptr = NULL;
ISP_PRINT ("isp_k: exit end \n");
}
module_init(isp_kernel_init);
module_exit(isp_kernel_exit);
MODULE_DESCRIPTION("Isp Driver");
MODULE_LICENSE("GPL");
|